OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [parse.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Main parser.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3
   Free Software Foundation, Inc.
4
   Contributed by Andy Vaught
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 2, or (at your option) any later
11
version.
12
 
13
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16
for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING.  If not, write to the Free
20
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21
02110-1301, USA.  */
22
 
23
 
24
#include "config.h"
25
#include "system.h"
26
#include <setjmp.h>
27
#include "gfortran.h"
28
#include "match.h"
29
#include "parse.h"
30
 
31
/* Current statement label.  Zero means no statement label.  Because
32
   new_st can get wiped during statement matching, we have to keep it
33
   separate.  */
34
 
35
gfc_st_label *gfc_statement_label;
36
 
37
static locus label_locus;
38
static jmp_buf eof_buf;
39
 
40
gfc_state_data *gfc_state_stack;
41
 
42
/* TODO: Re-order functions to kill these forward decls.  */
43
static void check_statement_label (gfc_statement);
44
static void undo_new_statement (void);
45
static void reject_statement (void);
46
 
47
/* A sort of half-matching function.  We try to match the word on the
48
   input with the passed string.  If this succeeds, we call the
49
   keyword-dependent matching function that will match the rest of the
50
   statement.  For single keywords, the matching subroutine is
51
   gfc_match_eos().  */
52
 
53
static match
54
match_word (const char *str, match (*subr) (void), locus * old_locus)
55
{
56
  match m;
57
 
58
  if (str != NULL)
59
    {
60
      m = gfc_match (str);
61
      if (m != MATCH_YES)
62
        return m;
63
    }
64
 
65
  m = (*subr) ();
66
 
67
  if (m != MATCH_YES)
68
    {
69
      gfc_current_locus = *old_locus;
70
      reject_statement ();
71
    }
72
 
73
  return m;
74
}
75
 
76
 
77
/* Figure out what the next statement is, (mostly) regardless of
78
   proper ordering.  The do...while(0) is there to prevent if/else
79
   ambiguity.  */
80
 
81
#define match(keyword, subr, st)                                \
82
    do {                                                        \
83
      if (match_word(keyword, subr, &old_locus) == MATCH_YES)   \
84
        return st;                                              \
85
      else                                                      \
86
        undo_new_statement ();                                  \
87
    } while (0);
88
 
89
static gfc_statement
90
decode_statement (void)
91
{
92
  gfc_statement st;
93
  locus old_locus;
94
  match m;
95
  int c;
96
 
97
#ifdef GFC_DEBUG
98
  gfc_symbol_state ();
99
#endif
100
 
101
  gfc_clear_error ();   /* Clear any pending errors.  */
102
  gfc_clear_warning (); /* Clear any pending warnings.  */
103
 
104
  if (gfc_match_eos () == MATCH_YES)
105
    return ST_NONE;
106
 
107
  old_locus = gfc_current_locus;
108
 
109
  /* Try matching a data declaration or function declaration. The
110
      input "REALFUNCTIONA(N)" can mean several things in different
111
      contexts, so it (and its relatives) get special treatment.  */
112
 
113
  if (gfc_current_state () == COMP_NONE
114
      || gfc_current_state () == COMP_INTERFACE
115
      || gfc_current_state () == COMP_CONTAINS)
116
    {
117
      m = gfc_match_function_decl ();
118
      if (m == MATCH_YES)
119
        return ST_FUNCTION;
120
      else if (m == MATCH_ERROR)
121
        reject_statement ();
122
 
123
      gfc_undo_symbols ();
124
      gfc_current_locus = old_locus;
125
    }
126
 
127
  /* Match statements whose error messages are meant to be overwritten
128
     by something better.  */
129
 
130
  match (NULL, gfc_match_assignment, ST_ASSIGNMENT);
131
  match (NULL, gfc_match_pointer_assignment, ST_POINTER_ASSIGNMENT);
132
  match (NULL, gfc_match_st_function, ST_STATEMENT_FUNCTION);
133
 
134
  match (NULL, gfc_match_data_decl, ST_DATA_DECL);
135
  match (NULL, gfc_match_enumerator_def, ST_ENUMERATOR);
136
 
137
  /* Try to match a subroutine statement, which has the same optional
138
     prefixes that functions can have.  */
139
 
140
  if (gfc_match_subroutine () == MATCH_YES)
141
    return ST_SUBROUTINE;
142
  gfc_undo_symbols ();
143
  gfc_current_locus = old_locus;
144
 
145
  /* Check for the IF, DO, SELECT, WHERE and FORALL statements, which
146
     might begin with a block label.  The match functions for these
147
     statements are unusual in that their keyword is not seen before
148
     the matcher is called.  */
149
 
150
  if (gfc_match_if (&st) == MATCH_YES)
151
    return st;
152
  gfc_undo_symbols ();
153
  gfc_current_locus = old_locus;
154
 
155
  if (gfc_match_where (&st) == MATCH_YES)
156
    return st;
157
  gfc_undo_symbols ();
158
  gfc_current_locus = old_locus;
159
 
160
  if (gfc_match_forall (&st) == MATCH_YES)
161
    return st;
162
  gfc_undo_symbols ();
163
  gfc_current_locus = old_locus;
164
 
165
  match (NULL, gfc_match_do, ST_DO);
166
  match (NULL, gfc_match_select, ST_SELECT_CASE);
167
 
168
  /* General statement matching: Instead of testing every possible
169
     statement, we eliminate most possibilities by peeking at the
170
     first character.  */
171
 
172
  c = gfc_peek_char ();
173
 
174
  switch (c)
175
    {
176
    case 'a':
177
      match ("allocate", gfc_match_allocate, ST_ALLOCATE);
178
      match ("allocatable", gfc_match_allocatable, ST_ATTR_DECL);
179
      match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT);
180
      break;
181
 
182
    case 'b':
183
      match ("backspace", gfc_match_backspace, ST_BACKSPACE);
184
      match ("block data", gfc_match_block_data, ST_BLOCK_DATA);
185
      break;
186
 
187
    case 'c':
188
      match ("call", gfc_match_call, ST_CALL);
189
      match ("close", gfc_match_close, ST_CLOSE);
190
      match ("continue", gfc_match_continue, ST_CONTINUE);
191
      match ("cycle", gfc_match_cycle, ST_CYCLE);
192
      match ("case", gfc_match_case, ST_CASE);
193
      match ("common", gfc_match_common, ST_COMMON);
194
      match ("contains", gfc_match_eos, ST_CONTAINS);
195
      break;
196
 
197
    case 'd':
198
      match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE);
199
      match ("data", gfc_match_data, ST_DATA);
200
      match ("dimension", gfc_match_dimension, ST_ATTR_DECL);
201
      break;
202
 
203
    case 'e':
204
      match ("end file", gfc_match_endfile, ST_END_FILE);
205
      match ("exit", gfc_match_exit, ST_EXIT);
206
      match ("else", gfc_match_else, ST_ELSE);
207
      match ("else where", gfc_match_elsewhere, ST_ELSEWHERE);
208
      match ("else if", gfc_match_elseif, ST_ELSEIF);
209
      match ("enum , bind ( c )", gfc_match_enum, ST_ENUM);
210
 
211
      if (gfc_match_end (&st) == MATCH_YES)
212
        return st;
213
 
214
      match ("entry% ", gfc_match_entry, ST_ENTRY);
215
      match ("equivalence", gfc_match_equivalence, ST_EQUIVALENCE);
216
      match ("external", gfc_match_external, ST_ATTR_DECL);
217
      break;
218
 
219
    case 'f':
220
      match ("flush", gfc_match_flush, ST_FLUSH);
221
      match ("format", gfc_match_format, ST_FORMAT);
222
      break;
223
 
224
    case 'g':
225
      match ("go to", gfc_match_goto, ST_GOTO);
226
      break;
227
 
228
    case 'i':
229
      match ("inquire", gfc_match_inquire, ST_INQUIRE);
230
      match ("implicit", gfc_match_implicit, ST_IMPLICIT);
231
      match ("implicit% none", gfc_match_implicit_none, ST_IMPLICIT_NONE);
232
      match ("interface", gfc_match_interface, ST_INTERFACE);
233
      match ("intent", gfc_match_intent, ST_ATTR_DECL);
234
      match ("intrinsic", gfc_match_intrinsic, ST_ATTR_DECL);
235
      break;
236
 
237
    case 'm':
238
      match ("module% procedure% ", gfc_match_modproc, ST_MODULE_PROC);
239
      match ("module", gfc_match_module, ST_MODULE);
240
      break;
241
 
242
    case 'n':
243
      match ("nullify", gfc_match_nullify, ST_NULLIFY);
244
      match ("namelist", gfc_match_namelist, ST_NAMELIST);
245
      break;
246
 
247
    case 'o':
248
      match ("open", gfc_match_open, ST_OPEN);
249
      match ("optional", gfc_match_optional, ST_ATTR_DECL);
250
      break;
251
 
252
    case 'p':
253
      match ("print", gfc_match_print, ST_WRITE);
254
      match ("parameter", gfc_match_parameter, ST_PARAMETER);
255
      match ("pause", gfc_match_pause, ST_PAUSE);
256
      match ("pointer", gfc_match_pointer, ST_ATTR_DECL);
257
      if (gfc_match_private (&st) == MATCH_YES)
258
        return st;
259
      match ("program", gfc_match_program, ST_PROGRAM);
260
      if (gfc_match_public (&st) == MATCH_YES)
261
        return st;
262
      break;
263
 
264
    case 'r':
265
      match ("read", gfc_match_read, ST_READ);
266
      match ("return", gfc_match_return, ST_RETURN);
267
      match ("rewind", gfc_match_rewind, ST_REWIND);
268
      break;
269
 
270
    case 's':
271
      match ("sequence", gfc_match_eos, ST_SEQUENCE);
272
      match ("stop", gfc_match_stop, ST_STOP);
273
      match ("save", gfc_match_save, ST_ATTR_DECL);
274
      break;
275
 
276
    case 't':
277
      match ("target", gfc_match_target, ST_ATTR_DECL);
278
      match ("type", gfc_match_derived_decl, ST_DERIVED_DECL);
279
      break;
280
 
281
    case 'u':
282
      match ("use% ", gfc_match_use, ST_USE);
283
      break;
284
 
285
    case 'w':
286
      match ("write", gfc_match_write, ST_WRITE);
287
      break;
288
    }
289
 
290
  /* All else has failed, so give up.  See if any of the matchers has
291
     stored an error message of some sort.  */
292
 
293
  if (gfc_error_check () == 0)
294
    gfc_error_now ("Unclassifiable statement at %C");
295
 
296
  reject_statement ();
297
 
298
  gfc_error_recovery ();
299
 
300
  return ST_NONE;
301
}
302
 
303
#undef match
304
 
305
 
306
/* Get the next statement in free form source.  */
307
 
308
static gfc_statement
309
next_free (void)
310
{
311
  match m;
312
  int c, d, cnt;
313
 
314
  gfc_gobble_whitespace ();
315
 
316
  c = gfc_peek_char ();
317
 
318
  if (ISDIGIT (c))
319
    {
320
      /* Found a statement label?  */
321
      m = gfc_match_st_label (&gfc_statement_label);
322
 
323
      d = gfc_peek_char ();
324
      if (m != MATCH_YES || !gfc_is_whitespace (d))
325
        {
326
          gfc_match_small_literal_int (&c, &cnt);
327
 
328
          if (cnt > 5)
329
            gfc_error_now ("Too many digits in statement label at %C");
330
 
331
          if (c == 0)
332
            gfc_error_now ("Statement label at %C is zero");
333
 
334
          do
335
            c = gfc_next_char ();
336
          while (ISDIGIT(c));
337
 
338
          if (!gfc_is_whitespace (c))
339
            gfc_error_now ("Non-numeric character in statement label at %C");
340
 
341
          return ST_NONE;
342
        }
343
      else
344
        {
345
          label_locus = gfc_current_locus;
346
 
347
          gfc_gobble_whitespace ();
348
 
349
          if (gfc_match_eos () == MATCH_YES)
350
            {
351
              gfc_warning_now
352
                ("Ignoring statement label in empty statement at %C");
353
              gfc_free_st_label (gfc_statement_label);
354
              gfc_statement_label = NULL;
355
              return ST_NONE;
356
            }
357
        }
358
    }
359
 
360
  return decode_statement ();
361
}
362
 
363
 
364
/* Get the next statement in fixed-form source.  */
365
 
366
static gfc_statement
367
next_fixed (void)
368
{
369
  int label, digit_flag, i;
370
  locus loc;
371
  char c;
372
 
373
  if (!gfc_at_bol ())
374
    return decode_statement ();
375
 
376
  /* Skip past the current label field, parsing a statement label if
377
     one is there.  This is a weird number parser, since the number is
378
     contained within five columns and can have any kind of embedded
379
     spaces.  We also check for characters that make the rest of the
380
     line a comment.  */
381
 
382
  label = 0;
383
  digit_flag = 0;
384
 
385
  for (i = 0; i < 5; i++)
386
    {
387
      c = gfc_next_char_literal (0);
388
 
389
      switch (c)
390
        {
391
        case ' ':
392
          break;
393
 
394
        case '0':
395
        case '1':
396
        case '2':
397
        case '3':
398
        case '4':
399
        case '5':
400
        case '6':
401
        case '7':
402
        case '8':
403
        case '9':
404
          label = label * 10 + c - '0';
405
          label_locus = gfc_current_locus;
406
          digit_flag = 1;
407
          break;
408
 
409
          /* Comments have already been skipped by the time we get
410
             here so don't bother checking for them.  */
411
 
412
        default:
413
          gfc_buffer_error (0);
414
          gfc_error ("Non-numeric character in statement label at %C");
415
          return ST_NONE;
416
        }
417
    }
418
 
419
  if (digit_flag)
420
    {
421
      if (label == 0)
422
        gfc_warning_now ("Zero is not a valid statement label at %C");
423
      else
424
        {
425
          /* We've found a valid statement label.  */
426
          gfc_statement_label = gfc_get_st_label (label);
427
        }
428
    }
429
 
430
  /* Since this line starts a statement, it cannot be a continuation
431
     of a previous statement.  If we see something here besides a
432
     space or zero, it must be a bad continuation line.  */
433
 
434
  c = gfc_next_char_literal (0);
435
  if (c == '\n')
436
    goto blank_line;
437
 
438
  if (c != ' ' && c!= '0')
439
    {
440
      gfc_buffer_error (0);
441
      gfc_error ("Bad continuation line at %C");
442
      return ST_NONE;
443
    }
444
 
445
  /* Now that we've taken care of the statement label columns, we have
446
     to make sure that the first nonblank character is not a '!'.  If
447
     it is, the rest of the line is a comment.  */
448
 
449
  do
450
    {
451
      loc = gfc_current_locus;
452
      c = gfc_next_char_literal (0);
453
    }
454
  while (gfc_is_whitespace (c));
455
 
456
  if (c == '!')
457
    goto blank_line;
458
  gfc_current_locus = loc;
459
 
460
  if (gfc_match_eos () == MATCH_YES)
461
    goto blank_line;
462
 
463
  /* At this point, we've got a nonblank statement to parse.  */
464
  return decode_statement ();
465
 
466
blank_line:
467
  if (digit_flag)
468
    gfc_warning ("Statement label in blank line will be " "ignored at %C");
469
  gfc_advance_line ();
470
  return ST_NONE;
471
}
472
 
473
 
474
/* Return the next non-ST_NONE statement to the caller.  We also worry
475
   about including files and the ends of include files at this stage.  */
476
 
477
static gfc_statement
478
next_statement (void)
479
{
480
  gfc_statement st;
481
 
482
  gfc_new_block = NULL;
483
 
484
  for (;;)
485
    {
486
      gfc_statement_label = NULL;
487
      gfc_buffer_error (1);
488
 
489
      if (gfc_at_eol ())
490
        {
491
          if (gfc_option.warn_line_truncation
492
              && gfc_current_locus.lb
493
              && gfc_current_locus.lb->truncated)
494
            gfc_warning_now ("Line truncated at %C");
495
 
496
          gfc_advance_line ();
497
        }
498
 
499
      gfc_skip_comments ();
500
 
501
      if (gfc_at_end ())
502
        {
503
          st = ST_NONE;
504
          break;
505
        }
506
 
507
      st =
508
        (gfc_current_form == FORM_FIXED) ? next_fixed () : next_free ();
509
 
510
      if (st != ST_NONE)
511
        break;
512
    }
513
 
514
  gfc_buffer_error (0);
515
 
516
  if (st != ST_NONE)
517
    check_statement_label (st);
518
 
519
  return st;
520
}
521
 
522
 
523
/****************************** Parser ***********************************/
524
 
525
/* The parser subroutines are of type 'try' that fail if the file ends
526
   unexpectedly.  */
527
 
528
/* Macros that expand to case-labels for various classes of
529
   statements.  Start with executable statements that directly do
530
   things.  */
531
 
532
#define case_executable case ST_ALLOCATE: case ST_BACKSPACE: case ST_CALL: \
533
  case ST_CLOSE: case ST_CONTINUE: case ST_DEALLOCATE: case ST_END_FILE: \
534
  case ST_GOTO: case ST_INQUIRE: case ST_NULLIFY: case ST_OPEN: \
535
  case ST_READ: case ST_RETURN: case ST_REWIND: case ST_SIMPLE_IF: \
536
  case ST_PAUSE: case ST_STOP: case ST_WRITE: case ST_ASSIGNMENT: \
537
  case ST_POINTER_ASSIGNMENT: case ST_EXIT: case ST_CYCLE: \
538
  case ST_ARITHMETIC_IF: case ST_WHERE: case ST_FORALL: \
539
  case ST_LABEL_ASSIGNMENT: case ST_FLUSH
540
 
541
/* Statements that mark other executable statements.  */
542
 
543
#define case_exec_markers case ST_DO: case ST_FORALL_BLOCK: case ST_IF_BLOCK: \
544
  case ST_WHERE_BLOCK: case ST_SELECT_CASE
545
 
546
/* Declaration statements */
547
 
548
#define case_decl case ST_ATTR_DECL: case ST_COMMON: case ST_DATA_DECL: \
549
  case ST_EQUIVALENCE: case ST_NAMELIST: case ST_STATEMENT_FUNCTION: \
550
  case ST_TYPE: case ST_INTERFACE
551
 
552
/* Block end statements.  Errors associated with interchanging these
553
   are detected in gfc_match_end().  */
554
 
555
#define case_end case ST_END_BLOCK_DATA: case ST_END_FUNCTION: \
556
                 case ST_END_PROGRAM: case ST_END_SUBROUTINE
557
 
558
 
559
/* Push a new state onto the stack.  */
560
 
561
static void
562
push_state (gfc_state_data * p, gfc_compile_state new_state, gfc_symbol * sym)
563
{
564
 
565
  p->state = new_state;
566
  p->previous = gfc_state_stack;
567
  p->sym = sym;
568
  p->head = p->tail = NULL;
569
  p->do_variable = NULL;
570
 
571
  gfc_state_stack = p;
572
}
573
 
574
 
575
/* Pop the current state.  */
576
 
577
static void
578
pop_state (void)
579
{
580
 
581
  gfc_state_stack = gfc_state_stack->previous;
582
}
583
 
584
 
585
/* Try to find the given state in the state stack.  */
586
 
587
try
588
gfc_find_state (gfc_compile_state state)
589
{
590
  gfc_state_data *p;
591
 
592
  for (p = gfc_state_stack; p; p = p->previous)
593
    if (p->state == state)
594
      break;
595
 
596
  return (p == NULL) ? FAILURE : SUCCESS;
597
}
598
 
599
 
600
/* Starts a new level in the statement list.  */
601
 
602
static gfc_code *
603
new_level (gfc_code * q)
604
{
605
  gfc_code *p;
606
 
607
  p = q->block = gfc_get_code ();
608
 
609
  gfc_state_stack->head = gfc_state_stack->tail = p;
610
 
611
  return p;
612
}
613
 
614
 
615
/* Add the current new_st code structure and adds it to the current
616
   program unit.  As a side-effect, it zeroes the new_st.  */
617
 
618
static gfc_code *
619
add_statement (void)
620
{
621
  gfc_code *p;
622
 
623
  p = gfc_get_code ();
624
  *p = new_st;
625
 
626
  p->loc = gfc_current_locus;
627
 
628
  if (gfc_state_stack->head == NULL)
629
    gfc_state_stack->head = p;
630
  else
631
    gfc_state_stack->tail->next = p;
632
 
633
  while (p->next != NULL)
634
    p = p->next;
635
 
636
  gfc_state_stack->tail = p;
637
 
638
  gfc_clear_new_st ();
639
 
640
  return p;
641
}
642
 
643
 
644
/* Frees everything associated with the current statement.  */
645
 
646
static void
647
undo_new_statement (void)
648
{
649
  gfc_free_statements (new_st.block);
650
  gfc_free_statements (new_st.next);
651
  gfc_free_statement (&new_st);
652
  gfc_clear_new_st ();
653
}
654
 
655
 
656
/* If the current statement has a statement label, make sure that it
657
   is allowed to, or should have one.  */
658
 
659
static void
660
check_statement_label (gfc_statement st)
661
{
662
  gfc_sl_type type;
663
 
664
  if (gfc_statement_label == NULL)
665
    {
666
      if (st == ST_FORMAT)
667
        gfc_error ("FORMAT statement at %L does not have a statement label",
668
                   &new_st.loc);
669
      return;
670
    }
671
 
672
  switch (st)
673
    {
674
    case ST_END_PROGRAM:
675
    case ST_END_FUNCTION:
676
    case ST_END_SUBROUTINE:
677
    case ST_ENDDO:
678
    case ST_ENDIF:
679
    case ST_END_SELECT:
680
    case_executable:
681
    case_exec_markers:
682
      type = ST_LABEL_TARGET;
683
      break;
684
 
685
    case ST_FORMAT:
686
      type = ST_LABEL_FORMAT;
687
      break;
688
 
689
      /* Statement labels are not restricted from appearing on a
690
         particular line.  However, there are plenty of situations
691
         where the resulting label can't be referenced.  */
692
 
693
    default:
694
      type = ST_LABEL_BAD_TARGET;
695
      break;
696
    }
697
 
698
  gfc_define_st_label (gfc_statement_label, type, &label_locus);
699
 
700
  new_st.here = gfc_statement_label;
701
}
702
 
703
 
704
/* Figures out what the enclosing program unit is.  This will be a
705
   function, subroutine, program, block data or module.  */
706
 
707
gfc_state_data *
708
gfc_enclosing_unit (gfc_compile_state * result)
709
{
710
  gfc_state_data *p;
711
 
712
  for (p = gfc_state_stack; p; p = p->previous)
713
    if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
714
        || p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
715
        || p->state == COMP_PROGRAM)
716
      {
717
 
718
        if (result != NULL)
719
          *result = p->state;
720
        return p;
721
      }
722
 
723
  if (result != NULL)
724
    *result = COMP_PROGRAM;
725
  return NULL;
726
}
727
 
728
 
729
/* Translate a statement enum to a string.  */
730
 
731
const char *
732
gfc_ascii_statement (gfc_statement st)
733
{
734
  const char *p;
735
 
736
  switch (st)
737
    {
738
    case ST_ARITHMETIC_IF:
739
      p = _("arithmetic IF");
740
      break;
741
    case ST_ALLOCATE:
742
      p = "ALLOCATE";
743
      break;
744
    case ST_ATTR_DECL:
745
      p = _("attribute declaration");
746
      break;
747
    case ST_BACKSPACE:
748
      p = "BACKSPACE";
749
      break;
750
    case ST_BLOCK_DATA:
751
      p = "BLOCK DATA";
752
      break;
753
    case ST_CALL:
754
      p = "CALL";
755
      break;
756
    case ST_CASE:
757
      p = "CASE";
758
      break;
759
    case ST_CLOSE:
760
      p = "CLOSE";
761
      break;
762
    case ST_COMMON:
763
      p = "COMMON";
764
      break;
765
    case ST_CONTINUE:
766
      p = "CONTINUE";
767
      break;
768
    case ST_CONTAINS:
769
      p = "CONTAINS";
770
      break;
771
    case ST_CYCLE:
772
      p = "CYCLE";
773
      break;
774
    case ST_DATA_DECL:
775
      p = _("data declaration");
776
      break;
777
    case ST_DATA:
778
      p = "DATA";
779
      break;
780
    case ST_DEALLOCATE:
781
      p = "DEALLOCATE";
782
      break;
783
    case ST_DERIVED_DECL:
784
      p = _("derived type declaration");
785
      break;
786
    case ST_DO:
787
      p = "DO";
788
      break;
789
    case ST_ELSE:
790
      p = "ELSE";
791
      break;
792
    case ST_ELSEIF:
793
      p = "ELSE IF";
794
      break;
795
    case ST_ELSEWHERE:
796
      p = "ELSEWHERE";
797
      break;
798
    case ST_END_BLOCK_DATA:
799
      p = "END BLOCK DATA";
800
      break;
801
    case ST_ENDDO:
802
      p = "END DO";
803
      break;
804
    case ST_END_FILE:
805
      p = "END FILE";
806
      break;
807
    case ST_END_FORALL:
808
      p = "END FORALL";
809
      break;
810
    case ST_END_FUNCTION:
811
      p = "END FUNCTION";
812
      break;
813
    case ST_ENDIF:
814
      p = "END IF";
815
      break;
816
    case ST_END_INTERFACE:
817
      p = "END INTERFACE";
818
      break;
819
    case ST_END_MODULE:
820
      p = "END MODULE";
821
      break;
822
    case ST_END_PROGRAM:
823
      p = "END PROGRAM";
824
      break;
825
    case ST_END_SELECT:
826
      p = "END SELECT";
827
      break;
828
    case ST_END_SUBROUTINE:
829
      p = "END SUBROUTINE";
830
      break;
831
    case ST_END_WHERE:
832
      p = "END WHERE";
833
      break;
834
    case ST_END_TYPE:
835
      p = "END TYPE";
836
      break;
837
    case ST_ENTRY:
838
      p = "ENTRY";
839
      break;
840
    case ST_EQUIVALENCE:
841
      p = "EQUIVALENCE";
842
      break;
843
    case ST_EXIT:
844
      p = "EXIT";
845
      break;
846
    case ST_FLUSH:
847
      p = "FLUSH";
848
      break;
849
    case ST_FORALL_BLOCK:       /* Fall through */
850
    case ST_FORALL:
851
      p = "FORALL";
852
      break;
853
    case ST_FORMAT:
854
      p = "FORMAT";
855
      break;
856
    case ST_FUNCTION:
857
      p = "FUNCTION";
858
      break;
859
    case ST_GOTO:
860
      p = "GOTO";
861
      break;
862
    case ST_IF_BLOCK:
863
      p = _("block IF");
864
      break;
865
    case ST_IMPLICIT:
866
      p = "IMPLICIT";
867
      break;
868
    case ST_IMPLICIT_NONE:
869
      p = "IMPLICIT NONE";
870
      break;
871
    case ST_IMPLIED_ENDDO:
872
      p = _("implied END DO");
873
      break;
874
    case ST_INQUIRE:
875
      p = "INQUIRE";
876
      break;
877
    case ST_INTERFACE:
878
      p = "INTERFACE";
879
      break;
880
    case ST_PARAMETER:
881
      p = "PARAMETER";
882
      break;
883
    case ST_PRIVATE:
884
      p = "PRIVATE";
885
      break;
886
    case ST_PUBLIC:
887
      p = "PUBLIC";
888
      break;
889
    case ST_MODULE:
890
      p = "MODULE";
891
      break;
892
    case ST_PAUSE:
893
      p = "PAUSE";
894
      break;
895
    case ST_MODULE_PROC:
896
      p = "MODULE PROCEDURE";
897
      break;
898
    case ST_NAMELIST:
899
      p = "NAMELIST";
900
      break;
901
    case ST_NULLIFY:
902
      p = "NULLIFY";
903
      break;
904
    case ST_OPEN:
905
      p = "OPEN";
906
      break;
907
    case ST_PROGRAM:
908
      p = "PROGRAM";
909
      break;
910
    case ST_READ:
911
      p = "READ";
912
      break;
913
    case ST_RETURN:
914
      p = "RETURN";
915
      break;
916
    case ST_REWIND:
917
      p = "REWIND";
918
      break;
919
    case ST_STOP:
920
      p = "STOP";
921
      break;
922
    case ST_SUBROUTINE:
923
      p = "SUBROUTINE";
924
      break;
925
    case ST_TYPE:
926
      p = "TYPE";
927
      break;
928
    case ST_USE:
929
      p = "USE";
930
      break;
931
    case ST_WHERE_BLOCK:        /* Fall through */
932
    case ST_WHERE:
933
      p = "WHERE";
934
      break;
935
    case ST_WRITE:
936
      p = "WRITE";
937
      break;
938
    case ST_ASSIGNMENT:
939
      p = _("assignment");
940
      break;
941
    case ST_POINTER_ASSIGNMENT:
942
      p = _("pointer assignment");
943
      break;
944
    case ST_SELECT_CASE:
945
      p = "SELECT CASE";
946
      break;
947
    case ST_SEQUENCE:
948
      p = "SEQUENCE";
949
      break;
950
    case ST_SIMPLE_IF:
951
      p = _("simple IF");
952
      break;
953
    case ST_STATEMENT_FUNCTION:
954
      p = "STATEMENT FUNCTION";
955
      break;
956
    case ST_LABEL_ASSIGNMENT:
957
      p = "LABEL ASSIGNMENT";
958
      break;
959
    case ST_ENUM:
960
      p = "ENUM DEFINITION";
961
      break;
962
    case ST_ENUMERATOR:
963
      p = "ENUMERATOR DEFINITION";
964
      break;
965
    case ST_END_ENUM:
966
      p = "END ENUM";
967
      break;
968
    default:
969
      gfc_internal_error ("gfc_ascii_statement(): Bad statement code");
970
    }
971
 
972
  return p;
973
}
974
 
975
 
976
/* Create a symbol for the main program and assign it to ns->proc_name.  */
977
 
978
static void
979
main_program_symbol (gfc_namespace * ns)
980
{
981
  gfc_symbol *main_program;
982
  symbol_attribute attr;
983
 
984
  gfc_get_symbol ("MAIN__", ns, &main_program);
985
  gfc_clear_attr (&attr);
986
  attr.flavor = FL_PROCEDURE;
987
  attr.proc = PROC_UNKNOWN;
988
  attr.subroutine = 1;
989
  attr.access = ACCESS_PUBLIC;
990
  attr.is_main_program = 1;
991
  main_program->attr = attr;
992
  main_program->declared_at = gfc_current_locus;
993
  ns->proc_name = main_program;
994
  gfc_commit_symbols ();
995
}
996
 
997
 
998
/* Do whatever is necessary to accept the last statement.  */
999
 
1000
static void
1001
accept_statement (gfc_statement st)
1002
{
1003
 
1004
  switch (st)
1005
    {
1006
    case ST_USE:
1007
      gfc_use_module ();
1008
      break;
1009
 
1010
    case ST_IMPLICIT_NONE:
1011
      gfc_set_implicit_none ();
1012
      break;
1013
 
1014
    case ST_IMPLICIT:
1015
      break;
1016
 
1017
    case ST_FUNCTION:
1018
    case ST_SUBROUTINE:
1019
    case ST_MODULE:
1020
      gfc_current_ns->proc_name = gfc_new_block;
1021
      break;
1022
 
1023
      /* If the statement is the end of a block, lay down a special code
1024
         that allows a branch to the end of the block from within the
1025
         construct.  */
1026
 
1027
    case ST_ENDIF:
1028
    case ST_END_SELECT:
1029
      if (gfc_statement_label != NULL)
1030
        {
1031
          new_st.op = EXEC_NOP;
1032
          add_statement ();
1033
        }
1034
 
1035
      break;
1036
 
1037
      /* The end-of-program unit statements do not get the special
1038
         marker and require a statement of some sort if they are a
1039
         branch target.  */
1040
 
1041
    case ST_END_PROGRAM:
1042
    case ST_END_FUNCTION:
1043
    case ST_END_SUBROUTINE:
1044
      if (gfc_statement_label != NULL)
1045
        {
1046
          new_st.op = EXEC_RETURN;
1047
          add_statement ();
1048
        }
1049
 
1050
      break;
1051
 
1052
    case ST_ENTRY:
1053
    case_executable:
1054
    case_exec_markers:
1055
      add_statement ();
1056
      break;
1057
 
1058
    default:
1059
      break;
1060
    }
1061
 
1062
  gfc_commit_symbols ();
1063
  gfc_warning_check ();
1064
  gfc_clear_new_st ();
1065
}
1066
 
1067
 
1068
/* Undo anything tentative that has been built for the current
1069
   statement.  */
1070
 
1071
static void
1072
reject_statement (void)
1073
{
1074
  gfc_new_block = NULL;
1075
  gfc_undo_symbols ();
1076
  gfc_clear_warning ();
1077
  undo_new_statement ();
1078
}
1079
 
1080
 
1081
/* Generic complaint about an out of order statement.  We also do
1082
   whatever is necessary to clean up.  */
1083
 
1084
static void
1085
unexpected_statement (gfc_statement st)
1086
{
1087
 
1088
  gfc_error ("Unexpected %s statement at %C", gfc_ascii_statement (st));
1089
 
1090
  reject_statement ();
1091
}
1092
 
1093
 
1094
/* Given the next statement seen by the matcher, make sure that it is
1095
   in proper order with the last.  This subroutine is initialized by
1096
   calling it with an argument of ST_NONE.  If there is a problem, we
1097
   issue an error and return FAILURE.  Otherwise we return SUCCESS.
1098
 
1099
   Individual parsers need to verify that the statements seen are
1100
   valid before calling here, ie ENTRY statements are not allowed in
1101
   INTERFACE blocks.  The following diagram is taken from the standard:
1102
 
1103
            +---------------------------------------+
1104
            | program  subroutine  function  module |
1105
            +---------------------------------------+
1106
            |                 use                   |
1107
            |---------------------------------------+
1108
            |        |        implicit none         |
1109
            |        +-----------+------------------+
1110
            |        | parameter |  implicit        |
1111
            |        +-----------+------------------+
1112
            | format |           |  derived type    |
1113
            | entry  | parameter |  interface       |
1114
            |        |   data    |  specification   |
1115
            |        |           |  statement func  |
1116
            |        +-----------+------------------+
1117
            |        |   data    |    executable    |
1118
            +--------+-----------+------------------+
1119
            |                contains               |
1120
            +---------------------------------------+
1121
            |      internal module/subprogram       |
1122
            +---------------------------------------+
1123
            |                   end                 |
1124
            +---------------------------------------+
1125
 
1126
*/
1127
 
1128
typedef struct
1129
{
1130
  enum
1131
  { ORDER_START, ORDER_USE, ORDER_IMPLICIT_NONE, ORDER_IMPLICIT,
1132
    ORDER_SPEC, ORDER_EXEC
1133
  }
1134
  state;
1135
  gfc_statement last_statement;
1136
  locus where;
1137
}
1138
st_state;
1139
 
1140
static try
1141
verify_st_order (st_state * p, gfc_statement st)
1142
{
1143
 
1144
  switch (st)
1145
    {
1146
    case ST_NONE:
1147
      p->state = ORDER_START;
1148
      break;
1149
 
1150
    case ST_USE:
1151
      if (p->state > ORDER_USE)
1152
        goto order;
1153
      p->state = ORDER_USE;
1154
      break;
1155
 
1156
    case ST_IMPLICIT_NONE:
1157
      if (p->state > ORDER_IMPLICIT_NONE)
1158
        goto order;
1159
 
1160
   /* The '>' sign cannot be a '>=', because a FORMAT or ENTRY
1161
      statement disqualifies a USE but not an IMPLICIT NONE.
1162
      Duplicate IMPLICIT NONEs are caught when the implicit types
1163
      are set.  */
1164
 
1165
      p->state = ORDER_IMPLICIT_NONE;
1166
      break;
1167
 
1168
    case ST_IMPLICIT:
1169
      if (p->state > ORDER_IMPLICIT)
1170
        goto order;
1171
      p->state = ORDER_IMPLICIT;
1172
      break;
1173
 
1174
    case ST_FORMAT:
1175
    case ST_ENTRY:
1176
      if (p->state < ORDER_IMPLICIT_NONE)
1177
        p->state = ORDER_IMPLICIT_NONE;
1178
      break;
1179
 
1180
    case ST_PARAMETER:
1181
      if (p->state >= ORDER_EXEC)
1182
        goto order;
1183
      if (p->state < ORDER_IMPLICIT)
1184
        p->state = ORDER_IMPLICIT;
1185
      break;
1186
 
1187
    case ST_DATA:
1188
      if (p->state < ORDER_SPEC)
1189
        p->state = ORDER_SPEC;
1190
      break;
1191
 
1192
    case ST_PUBLIC:
1193
    case ST_PRIVATE:
1194
    case ST_DERIVED_DECL:
1195
    case_decl:
1196
      if (p->state >= ORDER_EXEC)
1197
        goto order;
1198
      if (p->state < ORDER_SPEC)
1199
        p->state = ORDER_SPEC;
1200
      break;
1201
 
1202
    case_executable:
1203
    case_exec_markers:
1204
      if (p->state < ORDER_EXEC)
1205
        p->state = ORDER_EXEC;
1206
      break;
1207
 
1208
    default:
1209
      gfc_internal_error
1210
        ("Unexpected %s statement in verify_st_order() at %C",
1211
         gfc_ascii_statement (st));
1212
    }
1213
 
1214
  /* All is well, record the statement in case we need it next time.  */
1215
  p->where = gfc_current_locus;
1216
  p->last_statement = st;
1217
  return SUCCESS;
1218
 
1219
order:
1220
  gfc_error ("%s statement at %C cannot follow %s statement at %L",
1221
             gfc_ascii_statement (st),
1222
             gfc_ascii_statement (p->last_statement), &p->where);
1223
 
1224
  return FAILURE;
1225
}
1226
 
1227
 
1228
/* Handle an unexpected end of file.  This is a show-stopper...  */
1229
 
1230
static void unexpected_eof (void) ATTRIBUTE_NORETURN;
1231
 
1232
static void
1233
unexpected_eof (void)
1234
{
1235
  gfc_state_data *p;
1236
 
1237
  gfc_error ("Unexpected end of file in '%s'", gfc_source_file);
1238
 
1239
  /* Memory cleanup.  Move to "second to last".  */
1240
  for (p = gfc_state_stack; p && p->previous && p->previous->previous;
1241
       p = p->previous);
1242
 
1243
  gfc_current_ns->code = (p && p->previous) ? p->head : NULL;
1244
  gfc_done_2 ();
1245
 
1246
  longjmp (eof_buf, 1);
1247
}
1248
 
1249
 
1250
/* Parse a derived type.  */
1251
 
1252
static void
1253
parse_derived (void)
1254
{
1255
  int compiling_type, seen_private, seen_sequence, seen_component, error_flag;
1256
  gfc_statement st;
1257
  gfc_component *c;
1258
  gfc_state_data s;
1259
 
1260
  error_flag = 0;
1261
 
1262
  accept_statement (ST_DERIVED_DECL);
1263
  push_state (&s, COMP_DERIVED, gfc_new_block);
1264
 
1265
  gfc_new_block->component_access = ACCESS_PUBLIC;
1266
  seen_private = 0;
1267
  seen_sequence = 0;
1268
  seen_component = 0;
1269
 
1270
  compiling_type = 1;
1271
 
1272
  while (compiling_type)
1273
    {
1274
      st = next_statement ();
1275
      switch (st)
1276
        {
1277
        case ST_NONE:
1278
          unexpected_eof ();
1279
 
1280
        case ST_DATA_DECL:
1281
          accept_statement (st);
1282
          seen_component = 1;
1283
          break;
1284
 
1285
        case ST_END_TYPE:
1286
          compiling_type = 0;
1287
 
1288
          if (!seen_component)
1289
            {
1290
              gfc_error ("Derived type definition at %C has no components");
1291
              error_flag = 1;
1292
            }
1293
 
1294
          accept_statement (ST_END_TYPE);
1295
          break;
1296
 
1297
        case ST_PRIVATE:
1298
          if (gfc_find_state (COMP_MODULE) == FAILURE)
1299
            {
1300
              gfc_error
1301
                ("PRIVATE statement in TYPE at %C must be inside a MODULE");
1302
              error_flag = 1;
1303
              break;
1304
            }
1305
 
1306
          if (seen_component)
1307
            {
1308
              gfc_error ("PRIVATE statement at %C must precede "
1309
                         "structure components");
1310
              error_flag = 1;
1311
              break;
1312
            }
1313
 
1314
          if (seen_private)
1315
            {
1316
              gfc_error ("Duplicate PRIVATE statement at %C");
1317
              error_flag = 1;
1318
            }
1319
 
1320
          s.sym->component_access = ACCESS_PRIVATE;
1321
          accept_statement (ST_PRIVATE);
1322
          seen_private = 1;
1323
          break;
1324
 
1325
        case ST_SEQUENCE:
1326
          if (seen_component)
1327
            {
1328
              gfc_error ("SEQUENCE statement at %C must precede "
1329
                         "structure components");
1330
              error_flag = 1;
1331
              break;
1332
            }
1333
 
1334
          if (gfc_current_block ()->attr.sequence)
1335
            gfc_warning ("SEQUENCE attribute at %C already specified in "
1336
                         "TYPE statement");
1337
 
1338
          if (seen_sequence)
1339
            {
1340
              gfc_error ("Duplicate SEQUENCE statement at %C");
1341
              error_flag = 1;
1342
            }
1343
 
1344
          seen_sequence = 1;
1345
          gfc_add_sequence (&gfc_current_block ()->attr,
1346
                            gfc_current_block ()->name, NULL);
1347
          break;
1348
 
1349
        default:
1350
          unexpected_statement (st);
1351
          break;
1352
        }
1353
    }
1354
 
1355
  /* Sanity checks on the structure.  If the structure has the
1356
     SEQUENCE attribute, then all component structures must also have
1357
     SEQUENCE.  */
1358
  if (error_flag == 0 && gfc_current_block ()->attr.sequence)
1359
    for (c = gfc_current_block ()->components; c; c = c->next)
1360
      {
1361
        if (c->ts.type == BT_DERIVED && c->ts.derived->attr.sequence == 0)
1362
          {
1363
            gfc_error
1364
              ("Component %s of SEQUENCE type declared at %C does not "
1365
               "have the SEQUENCE attribute", c->ts.derived->name);
1366
          }
1367
      }
1368
 
1369
  pop_state ();
1370
}
1371
 
1372
 
1373
 
1374
/* Parse an ENUM.  */
1375
 
1376
static void
1377
parse_enum (void)
1378
{
1379
  int error_flag;
1380
  gfc_statement st;
1381
  int compiling_enum;
1382
  gfc_state_data s;
1383
  int seen_enumerator = 0;
1384
 
1385
  error_flag = 0;
1386
 
1387
  push_state (&s, COMP_ENUM, gfc_new_block);
1388
 
1389
  compiling_enum = 1;
1390
 
1391
  while (compiling_enum)
1392
    {
1393
      st = next_statement ();
1394
      switch (st)
1395
        {
1396
        case ST_NONE:
1397
          unexpected_eof ();
1398
          break;
1399
 
1400
        case ST_ENUMERATOR:
1401
          seen_enumerator = 1;
1402
          accept_statement (st);
1403
          break;
1404
 
1405
        case ST_END_ENUM:
1406
          compiling_enum = 0;
1407
          if (!seen_enumerator)
1408
            {
1409
              gfc_error ("ENUM declaration at %C has no ENUMERATORS");
1410
              error_flag = 1;
1411
            }
1412
          accept_statement (st);
1413
          break;
1414
 
1415
        default:
1416
          gfc_free_enum_history ();
1417
          unexpected_statement (st);
1418
          break;
1419
        }
1420
    }
1421
  pop_state ();
1422
}
1423
 
1424
/* Parse an interface.  We must be able to deal with the possibility
1425
   of recursive interfaces.  The parse_spec() subroutine is mutually
1426
   recursive with parse_interface().  */
1427
 
1428
static gfc_statement parse_spec (gfc_statement);
1429
 
1430
static void
1431
parse_interface (void)
1432
{
1433
  gfc_compile_state new_state, current_state;
1434
  gfc_symbol *prog_unit, *sym;
1435
  gfc_interface_info save;
1436
  gfc_state_data s1, s2;
1437
  gfc_statement st;
1438
 
1439
  accept_statement (ST_INTERFACE);
1440
 
1441
  current_interface.ns = gfc_current_ns;
1442
  save = current_interface;
1443
 
1444
  sym = (current_interface.type == INTERFACE_GENERIC
1445
         || current_interface.type == INTERFACE_USER_OP) ? gfc_new_block : NULL;
1446
 
1447
  push_state (&s1, COMP_INTERFACE, sym);
1448
  current_state = COMP_NONE;
1449
 
1450
loop:
1451
  gfc_current_ns = gfc_get_namespace (current_interface.ns, 0);
1452
 
1453
  st = next_statement ();
1454
  switch (st)
1455
    {
1456
    case ST_NONE:
1457
      unexpected_eof ();
1458
 
1459
    case ST_SUBROUTINE:
1460
      new_state = COMP_SUBROUTINE;
1461
      gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1462
                                  gfc_new_block->formal, NULL);
1463
      break;
1464
 
1465
    case ST_FUNCTION:
1466
      new_state = COMP_FUNCTION;
1467
      gfc_add_explicit_interface (gfc_new_block, IFSRC_IFBODY,
1468
                                  gfc_new_block->formal, NULL);
1469
      break;
1470
 
1471
    case ST_MODULE_PROC:        /* The module procedure matcher makes
1472
                                   sure the context is correct.  */
1473
      accept_statement (st);
1474
      gfc_free_namespace (gfc_current_ns);
1475
      goto loop;
1476
 
1477
    case ST_END_INTERFACE:
1478
      gfc_free_namespace (gfc_current_ns);
1479
      gfc_current_ns = current_interface.ns;
1480
      goto done;
1481
 
1482
    default:
1483
      gfc_error ("Unexpected %s statement in INTERFACE block at %C",
1484
                 gfc_ascii_statement (st));
1485
      reject_statement ();
1486
      gfc_free_namespace (gfc_current_ns);
1487
      goto loop;
1488
    }
1489
 
1490
 
1491
  /* Make sure that a generic interface has only subroutines or
1492
     functions and that the generic name has the right attribute.  */
1493
  if (current_interface.type == INTERFACE_GENERIC)
1494
    {
1495
      if (current_state == COMP_NONE)
1496
        {
1497
          if (new_state == COMP_FUNCTION)
1498
            gfc_add_function (&sym->attr, sym->name, NULL);
1499
          else if (new_state == COMP_SUBROUTINE)
1500
            gfc_add_subroutine (&sym->attr, sym->name, NULL);
1501
 
1502
          current_state = new_state;
1503
        }
1504
      else
1505
        {
1506
          if (new_state != current_state)
1507
            {
1508
              if (new_state == COMP_SUBROUTINE)
1509
                gfc_error
1510
                  ("SUBROUTINE at %C does not belong in a generic function "
1511
                   "interface");
1512
 
1513
              if (new_state == COMP_FUNCTION)
1514
                gfc_error
1515
                  ("FUNCTION at %C does not belong in a generic subroutine "
1516
                   "interface");
1517
            }
1518
        }
1519
    }
1520
 
1521
  push_state (&s2, new_state, gfc_new_block);
1522
  accept_statement (st);
1523
  prog_unit = gfc_new_block;
1524
  prog_unit->formal_ns = gfc_current_ns;
1525
 
1526
decl:
1527
  /* Read data declaration statements.  */
1528
  st = parse_spec (ST_NONE);
1529
 
1530
  if (st != ST_END_SUBROUTINE && st != ST_END_FUNCTION)
1531
    {
1532
      gfc_error ("Unexpected %s statement at %C in INTERFACE body",
1533
                 gfc_ascii_statement (st));
1534
      reject_statement ();
1535
      goto decl;
1536
    }
1537
 
1538
  current_interface = save;
1539
  gfc_add_interface (prog_unit);
1540
 
1541
  pop_state ();
1542
  goto loop;
1543
 
1544
done:
1545
  pop_state ();
1546
}
1547
 
1548
 
1549
/* Parse a set of specification statements.  Returns the statement
1550
   that doesn't fit.  */
1551
 
1552
static gfc_statement
1553
parse_spec (gfc_statement st)
1554
{
1555
  st_state ss;
1556
 
1557
  verify_st_order (&ss, ST_NONE);
1558
  if (st == ST_NONE)
1559
    st = next_statement ();
1560
 
1561
loop:
1562
  switch (st)
1563
    {
1564
    case ST_NONE:
1565
      unexpected_eof ();
1566
 
1567
    case ST_FORMAT:
1568
    case ST_ENTRY:
1569
    case ST_DATA:       /* Not allowed in interfaces */
1570
      if (gfc_current_state () == COMP_INTERFACE)
1571
        break;
1572
 
1573
      /* Fall through */
1574
 
1575
    case ST_USE:
1576
    case ST_IMPLICIT_NONE:
1577
    case ST_IMPLICIT:
1578
    case ST_PARAMETER:
1579
    case ST_PUBLIC:
1580
    case ST_PRIVATE:
1581
    case ST_DERIVED_DECL:
1582
    case_decl:
1583
      if (verify_st_order (&ss, st) == FAILURE)
1584
        {
1585
          reject_statement ();
1586
          st = next_statement ();
1587
          goto loop;
1588
        }
1589
 
1590
      switch (st)
1591
        {
1592
        case ST_INTERFACE:
1593
          parse_interface ();
1594
          break;
1595
 
1596
        case ST_DERIVED_DECL:
1597
          parse_derived ();
1598
          break;
1599
 
1600
        case ST_PUBLIC:
1601
        case ST_PRIVATE:
1602
          if (gfc_current_state () != COMP_MODULE)
1603
            {
1604
              gfc_error ("%s statement must appear in a MODULE",
1605
                         gfc_ascii_statement (st));
1606
              break;
1607
            }
1608
 
1609
          if (gfc_current_ns->default_access != ACCESS_UNKNOWN)
1610
            {
1611
              gfc_error ("%s statement at %C follows another accessibility "
1612
                         "specification", gfc_ascii_statement (st));
1613
              break;
1614
            }
1615
 
1616
          gfc_current_ns->default_access = (st == ST_PUBLIC)
1617
            ? ACCESS_PUBLIC : ACCESS_PRIVATE;
1618
 
1619
          break;
1620
 
1621
        default:
1622
          break;
1623
        }
1624
 
1625
      accept_statement (st);
1626
      st = next_statement ();
1627
      goto loop;
1628
 
1629
    case ST_ENUM:
1630
      accept_statement (st);
1631
      parse_enum();
1632
      st = next_statement ();
1633
      goto loop;
1634
 
1635
    default:
1636
      break;
1637
    }
1638
 
1639
  return st;
1640
}
1641
 
1642
 
1643
/* Parse a WHERE block, (not a simple WHERE statement).  */
1644
 
1645
static void
1646
parse_where_block (void)
1647
{
1648
  int seen_empty_else;
1649
  gfc_code *top, *d;
1650
  gfc_state_data s;
1651
  gfc_statement st;
1652
 
1653
  accept_statement (ST_WHERE_BLOCK);
1654
  top = gfc_state_stack->tail;
1655
 
1656
  push_state (&s, COMP_WHERE, gfc_new_block);
1657
 
1658
  d = add_statement ();
1659
  d->expr = top->expr;
1660
  d->op = EXEC_WHERE;
1661
 
1662
  top->expr = NULL;
1663
  top->block = d;
1664
 
1665
  seen_empty_else = 0;
1666
 
1667
  do
1668
    {
1669
      st = next_statement ();
1670
      switch (st)
1671
        {
1672
        case ST_NONE:
1673
          unexpected_eof ();
1674
 
1675
        case ST_WHERE_BLOCK:
1676
          parse_where_block ();
1677
          break;
1678
 
1679
        case ST_ASSIGNMENT:
1680
        case ST_WHERE:
1681
          accept_statement (st);
1682
          break;
1683
 
1684
        case ST_ELSEWHERE:
1685
          if (seen_empty_else)
1686
            {
1687
              gfc_error
1688
                ("ELSEWHERE statement at %C follows previous unmasked "
1689
                 "ELSEWHERE");
1690
              break;
1691
            }
1692
 
1693
          if (new_st.expr == NULL)
1694
            seen_empty_else = 1;
1695
 
1696
          d = new_level (gfc_state_stack->head);
1697
          d->op = EXEC_WHERE;
1698
          d->expr = new_st.expr;
1699
 
1700
          accept_statement (st);
1701
 
1702
          break;
1703
 
1704
        case ST_END_WHERE:
1705
          accept_statement (st);
1706
          break;
1707
 
1708
        default:
1709
          gfc_error ("Unexpected %s statement in WHERE block at %C",
1710
                     gfc_ascii_statement (st));
1711
          reject_statement ();
1712
          break;
1713
        }
1714
 
1715
    }
1716
  while (st != ST_END_WHERE);
1717
 
1718
  pop_state ();
1719
}
1720
 
1721
 
1722
/* Parse a FORALL block (not a simple FORALL statement).  */
1723
 
1724
static void
1725
parse_forall_block (void)
1726
{
1727
  gfc_code *top, *d;
1728
  gfc_state_data s;
1729
  gfc_statement st;
1730
 
1731
  accept_statement (ST_FORALL_BLOCK);
1732
  top = gfc_state_stack->tail;
1733
 
1734
  push_state (&s, COMP_FORALL, gfc_new_block);
1735
 
1736
  d = add_statement ();
1737
  d->op = EXEC_FORALL;
1738
  top->block = d;
1739
 
1740
  do
1741
    {
1742
      st = next_statement ();
1743
      switch (st)
1744
        {
1745
 
1746
        case ST_ASSIGNMENT:
1747
        case ST_POINTER_ASSIGNMENT:
1748
        case ST_WHERE:
1749
        case ST_FORALL:
1750
          accept_statement (st);
1751
          break;
1752
 
1753
        case ST_WHERE_BLOCK:
1754
          parse_where_block ();
1755
          break;
1756
 
1757
        case ST_FORALL_BLOCK:
1758
          parse_forall_block ();
1759
          break;
1760
 
1761
        case ST_END_FORALL:
1762
          accept_statement (st);
1763
          break;
1764
 
1765
        case ST_NONE:
1766
          unexpected_eof ();
1767
 
1768
        default:
1769
          gfc_error ("Unexpected %s statement in FORALL block at %C",
1770
                     gfc_ascii_statement (st));
1771
 
1772
          reject_statement ();
1773
          break;
1774
        }
1775
    }
1776
  while (st != ST_END_FORALL);
1777
 
1778
  pop_state ();
1779
}
1780
 
1781
 
1782
static gfc_statement parse_executable (gfc_statement);
1783
 
1784
/* parse the statements of an IF-THEN-ELSEIF-ELSE-ENDIF block.  */
1785
 
1786
static void
1787
parse_if_block (void)
1788
{
1789
  gfc_code *top, *d;
1790
  gfc_statement st;
1791
  locus else_locus;
1792
  gfc_state_data s;
1793
  int seen_else;
1794
 
1795
  seen_else = 0;
1796
  accept_statement (ST_IF_BLOCK);
1797
 
1798
  top = gfc_state_stack->tail;
1799
  push_state (&s, COMP_IF, gfc_new_block);
1800
 
1801
  new_st.op = EXEC_IF;
1802
  d = add_statement ();
1803
 
1804
  d->expr = top->expr;
1805
  top->expr = NULL;
1806
  top->block = d;
1807
 
1808
  do
1809
    {
1810
      st = parse_executable (ST_NONE);
1811
 
1812
      switch (st)
1813
        {
1814
        case ST_NONE:
1815
          unexpected_eof ();
1816
 
1817
        case ST_ELSEIF:
1818
          if (seen_else)
1819
            {
1820
              gfc_error
1821
                ("ELSE IF statement at %C cannot follow ELSE statement at %L",
1822
                 &else_locus);
1823
 
1824
              reject_statement ();
1825
              break;
1826
            }
1827
 
1828
          d = new_level (gfc_state_stack->head);
1829
          d->op = EXEC_IF;
1830
          d->expr = new_st.expr;
1831
 
1832
          accept_statement (st);
1833
 
1834
          break;
1835
 
1836
        case ST_ELSE:
1837
          if (seen_else)
1838
            {
1839
              gfc_error ("Duplicate ELSE statements at %L and %C",
1840
                         &else_locus);
1841
              reject_statement ();
1842
              break;
1843
            }
1844
 
1845
          seen_else = 1;
1846
          else_locus = gfc_current_locus;
1847
 
1848
          d = new_level (gfc_state_stack->head);
1849
          d->op = EXEC_IF;
1850
 
1851
          accept_statement (st);
1852
 
1853
          break;
1854
 
1855
        case ST_ENDIF:
1856
          break;
1857
 
1858
        default:
1859
          unexpected_statement (st);
1860
          break;
1861
        }
1862
    }
1863
  while (st != ST_ENDIF);
1864
 
1865
  pop_state ();
1866
  accept_statement (st);
1867
}
1868
 
1869
 
1870
/* Parse a SELECT block.  */
1871
 
1872
static void
1873
parse_select_block (void)
1874
{
1875
  gfc_statement st;
1876
  gfc_code *cp;
1877
  gfc_state_data s;
1878
 
1879
  accept_statement (ST_SELECT_CASE);
1880
 
1881
  cp = gfc_state_stack->tail;
1882
  push_state (&s, COMP_SELECT, gfc_new_block);
1883
 
1884
  /* Make sure that the next statement is a CASE or END SELECT.  */
1885
  for (;;)
1886
    {
1887
      st = next_statement ();
1888
      if (st == ST_NONE)
1889
        unexpected_eof ();
1890
      if (st == ST_END_SELECT)
1891
        {
1892
          /* Empty SELECT CASE is OK.  */
1893
          accept_statement (st);
1894
          pop_state ();
1895
          return;
1896
        }
1897
      if (st == ST_CASE)
1898
        break;
1899
 
1900
      gfc_error
1901
        ("Expected a CASE or END SELECT statement following SELECT CASE "
1902
         "at %C");
1903
 
1904
      reject_statement ();
1905
    }
1906
 
1907
  /* At this point, we're got a nonempty select block.  */
1908
  cp = new_level (cp);
1909
  *cp = new_st;
1910
 
1911
  accept_statement (st);
1912
 
1913
  do
1914
    {
1915
      st = parse_executable (ST_NONE);
1916
      switch (st)
1917
        {
1918
        case ST_NONE:
1919
          unexpected_eof ();
1920
 
1921
        case ST_CASE:
1922
          cp = new_level (gfc_state_stack->head);
1923
          *cp = new_st;
1924
          gfc_clear_new_st ();
1925
 
1926
          accept_statement (st);
1927
          /* Fall through */
1928
 
1929
        case ST_END_SELECT:
1930
          break;
1931
 
1932
        /* Can't have an executable statement because of
1933
           parse_executable().  */
1934
        default:
1935
          unexpected_statement (st);
1936
          break;
1937
        }
1938
    }
1939
  while (st != ST_END_SELECT);
1940
 
1941
  pop_state ();
1942
  accept_statement (st);
1943
}
1944
 
1945
 
1946
/* Given a symbol, make sure it is not an iteration variable for a DO
1947
   statement.  This subroutine is called when the symbol is seen in a
1948
   context that causes it to become redefined.  If the symbol is an
1949
   iterator, we generate an error message and return nonzero.  */
1950
 
1951
int
1952
gfc_check_do_variable (gfc_symtree *st)
1953
{
1954
  gfc_state_data *s;
1955
 
1956
  for (s=gfc_state_stack; s; s = s->previous)
1957
    if (s->do_variable == st)
1958
      {
1959
        gfc_error_now("Variable '%s' at %C cannot be redefined inside "
1960
                      "loop beginning at %L", st->name, &s->head->loc);
1961
        return 1;
1962
      }
1963
 
1964
  return 0;
1965
}
1966
 
1967
 
1968
/* Checks to see if the current statement label closes an enddo.
1969
   Returns 0 if not, 1 if closes an ENDDO correctly, or 2 (and issues
1970
   an error) if it incorrectly closes an ENDDO.  */
1971
 
1972
static int
1973
check_do_closure (void)
1974
{
1975
  gfc_state_data *p;
1976
 
1977
  if (gfc_statement_label == NULL)
1978
    return 0;
1979
 
1980
  for (p = gfc_state_stack; p; p = p->previous)
1981
    if (p->state == COMP_DO)
1982
      break;
1983
 
1984
  if (p == NULL)
1985
    return 0;            /* No loops to close */
1986
 
1987
  if (p->ext.end_do_label == gfc_statement_label)
1988
    {
1989
 
1990
      if (p == gfc_state_stack)
1991
        return 1;
1992
 
1993
      gfc_error
1994
        ("End of nonblock DO statement at %C is within another block");
1995
      return 2;
1996
    }
1997
 
1998
  /* At this point, the label doesn't terminate the innermost loop.
1999
     Make sure it doesn't terminate another one.  */
2000
  for (; p; p = p->previous)
2001
    if (p->state == COMP_DO && p->ext.end_do_label == gfc_statement_label)
2002
      {
2003
        gfc_error ("End of nonblock DO statement at %C is interwoven "
2004
                   "with another DO loop");
2005
        return 2;
2006
      }
2007
 
2008
  return 0;
2009
}
2010
 
2011
 
2012
/* Parse a DO loop.  Note that the ST_CYCLE and ST_EXIT statements are
2013
   handled inside of parse_executable(), because they aren't really
2014
   loop statements.  */
2015
 
2016
static void
2017
parse_do_block (void)
2018
{
2019
  gfc_statement st;
2020
  gfc_code *top;
2021
  gfc_state_data s;
2022
  gfc_symtree *stree;
2023
 
2024
  s.ext.end_do_label = new_st.label;
2025
 
2026
  if (new_st.ext.iterator != NULL)
2027
    stree = new_st.ext.iterator->var->symtree;
2028
  else
2029
    stree = NULL;
2030
 
2031
  accept_statement (ST_DO);
2032
 
2033
  top = gfc_state_stack->tail;
2034
  push_state (&s, COMP_DO, gfc_new_block);
2035
 
2036
  s.do_variable = stree;
2037
 
2038
  top->block = new_level (top);
2039
  top->block->op = EXEC_DO;
2040
 
2041
loop:
2042
  st = parse_executable (ST_NONE);
2043
 
2044
  switch (st)
2045
    {
2046
    case ST_NONE:
2047
      unexpected_eof ();
2048
 
2049
    case ST_ENDDO:
2050
      if (s.ext.end_do_label != NULL
2051
          && s.ext.end_do_label != gfc_statement_label)
2052
        gfc_error_now
2053
          ("Statement label in ENDDO at %C doesn't match DO label");
2054
 
2055
      if (gfc_statement_label != NULL)
2056
        {
2057
          new_st.op = EXEC_NOP;
2058
          add_statement ();
2059
        }
2060
      break;
2061
 
2062
    case ST_IMPLIED_ENDDO:
2063
      break;
2064
 
2065
    default:
2066
      unexpected_statement (st);
2067
      goto loop;
2068
    }
2069
 
2070
  pop_state ();
2071
  accept_statement (st);
2072
}
2073
 
2074
 
2075
/* Accept a series of executable statements.  We return the first
2076
   statement that doesn't fit to the caller.  Any block statements are
2077
   passed on to the correct handler, which usually passes the buck
2078
   right back here.  */
2079
 
2080
static gfc_statement
2081
parse_executable (gfc_statement st)
2082
{
2083
  int close_flag;
2084
 
2085
  if (st == ST_NONE)
2086
    st = next_statement ();
2087
 
2088
  for (;; st = next_statement ())
2089
    {
2090
 
2091
      close_flag = check_do_closure ();
2092
      if (close_flag)
2093
        switch (st)
2094
          {
2095
          case ST_GOTO:
2096
          case ST_END_PROGRAM:
2097
          case ST_RETURN:
2098
          case ST_EXIT:
2099
          case ST_END_FUNCTION:
2100
          case ST_CYCLE:
2101
          case ST_PAUSE:
2102
          case ST_STOP:
2103
          case ST_END_SUBROUTINE:
2104
 
2105
          case ST_DO:
2106
          case ST_FORALL:
2107
          case ST_WHERE:
2108
          case ST_SELECT_CASE:
2109
            gfc_error
2110
              ("%s statement at %C cannot terminate a non-block DO loop",
2111
               gfc_ascii_statement (st));
2112
            break;
2113
 
2114
          default:
2115
            break;
2116
          }
2117
 
2118
      switch (st)
2119
        {
2120
        case ST_NONE:
2121
          unexpected_eof ();
2122
 
2123
        case ST_FORMAT:
2124
        case ST_DATA:
2125
        case ST_ENTRY:
2126
        case_executable:
2127
          accept_statement (st);
2128
          if (close_flag == 1)
2129
            return ST_IMPLIED_ENDDO;
2130
          continue;
2131
 
2132
        case ST_IF_BLOCK:
2133
          parse_if_block ();
2134
          continue;
2135
 
2136
        case ST_SELECT_CASE:
2137
          parse_select_block ();
2138
          continue;
2139
 
2140
        case ST_DO:
2141
          parse_do_block ();
2142
          if (check_do_closure () == 1)
2143
            return ST_IMPLIED_ENDDO;
2144
          continue;
2145
 
2146
        case ST_WHERE_BLOCK:
2147
          parse_where_block ();
2148
          continue;
2149
 
2150
        case ST_FORALL_BLOCK:
2151
          parse_forall_block ();
2152
          continue;
2153
 
2154
        default:
2155
          break;
2156
        }
2157
 
2158
      break;
2159
    }
2160
 
2161
  return st;
2162
}
2163
 
2164
 
2165
/* Parse a series of contained program units.  */
2166
 
2167
static void parse_progunit (gfc_statement);
2168
 
2169
 
2170
/* Fix the symbols for sibling functions.  These are incorrectly added to
2171
   the child namespace as the parser didn't know about this procedure.  */
2172
 
2173
static void
2174
gfc_fixup_sibling_symbols (gfc_symbol * sym, gfc_namespace * siblings)
2175
{
2176
  gfc_namespace *ns;
2177
  gfc_symtree *st;
2178
  gfc_symbol *old_sym;
2179
 
2180
  sym->attr.referenced = 1;
2181
  for (ns = siblings; ns; ns = ns->sibling)
2182
    {
2183
      gfc_find_sym_tree (sym->name, ns, 0, &st);
2184
      if (!st)
2185
        continue;
2186
 
2187
      old_sym = st->n.sym;
2188
      if ((old_sym->attr.flavor == FL_PROCEDURE
2189
           || old_sym->ts.type == BT_UNKNOWN)
2190
          && old_sym->ns == ns
2191
          && ! old_sym->attr.contained)
2192
        {
2193
          /* Replace it with the symbol from the parent namespace.  */
2194
          st->n.sym = sym;
2195
          sym->refs++;
2196
 
2197
          /* Free the old (local) symbol.  */
2198
          old_sym->refs--;
2199
          if (old_sym->refs == 0)
2200
            gfc_free_symbol (old_sym);
2201
        }
2202
 
2203
      /* Do the same for any contained procedures.  */
2204
      gfc_fixup_sibling_symbols (sym, ns->contained);
2205
    }
2206
}
2207
 
2208
static void
2209
parse_contained (int module)
2210
{
2211
  gfc_namespace *ns, *parent_ns;
2212
  gfc_state_data s1, s2;
2213
  gfc_statement st;
2214
  gfc_symbol *sym;
2215
  gfc_entry_list *el;
2216
 
2217
  push_state (&s1, COMP_CONTAINS, NULL);
2218
  parent_ns = gfc_current_ns;
2219
 
2220
  do
2221
    {
2222
      gfc_current_ns = gfc_get_namespace (parent_ns, 1);
2223
 
2224
      gfc_current_ns->sibling = parent_ns->contained;
2225
      parent_ns->contained = gfc_current_ns;
2226
 
2227
      st = next_statement ();
2228
 
2229
      switch (st)
2230
        {
2231
        case ST_NONE:
2232
          unexpected_eof ();
2233
 
2234
        case ST_FUNCTION:
2235
        case ST_SUBROUTINE:
2236
          accept_statement (st);
2237
 
2238
          push_state (&s2,
2239
                      (st == ST_FUNCTION) ? COMP_FUNCTION : COMP_SUBROUTINE,
2240
                      gfc_new_block);
2241
 
2242
          /* For internal procedures, create/update the symbol in the
2243
             parent namespace.  */
2244
 
2245
          if (!module)
2246
            {
2247
              if (gfc_get_symbol (gfc_new_block->name, parent_ns, &sym))
2248
                gfc_error
2249
                  ("Contained procedure '%s' at %C is already ambiguous",
2250
                   gfc_new_block->name);
2251
              else
2252
                {
2253
                  if (gfc_add_procedure (&sym->attr, PROC_INTERNAL, sym->name,
2254
                                         &gfc_new_block->declared_at) ==
2255
                      SUCCESS)
2256
                    {
2257
                      if (st == ST_FUNCTION)
2258
                        gfc_add_function (&sym->attr, sym->name,
2259
                                          &gfc_new_block->declared_at);
2260
                      else
2261
                        gfc_add_subroutine (&sym->attr, sym->name,
2262
                                            &gfc_new_block->declared_at);
2263
                    }
2264
                }
2265
 
2266
              gfc_commit_symbols ();
2267
            }
2268
          else
2269
            sym = gfc_new_block;
2270
 
2271
          /* Mark this as a contained function, so it isn't replaced
2272
             by other module functions.  */
2273
          sym->attr.contained = 1;
2274
          sym->attr.referenced = 1;
2275
 
2276
          parse_progunit (ST_NONE);
2277
 
2278
          /* Fix up any sibling functions that refer to this one.  */
2279
          gfc_fixup_sibling_symbols (sym, gfc_current_ns);
2280
          /* Or refer to any of its alternate entry points.  */
2281
          for (el = gfc_current_ns->entries; el; el = el->next)
2282
            gfc_fixup_sibling_symbols (el->sym, gfc_current_ns);
2283
 
2284
          gfc_current_ns->code = s2.head;
2285
          gfc_current_ns = parent_ns;
2286
 
2287
          pop_state ();
2288
          break;
2289
 
2290
        /* These statements are associated with the end of the host
2291
           unit.  */
2292
        case ST_END_FUNCTION:
2293
        case ST_END_MODULE:
2294
        case ST_END_PROGRAM:
2295
        case ST_END_SUBROUTINE:
2296
          accept_statement (st);
2297
          break;
2298
 
2299
        default:
2300
          gfc_error ("Unexpected %s statement in CONTAINS section at %C",
2301
                     gfc_ascii_statement (st));
2302
          reject_statement ();
2303
          break;
2304
        }
2305
    }
2306
  while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
2307
         && st != ST_END_MODULE && st != ST_END_PROGRAM);
2308
 
2309
  /* The first namespace in the list is guaranteed to not have
2310
     anything (worthwhile) in it.  */
2311
 
2312
  gfc_current_ns = parent_ns;
2313
 
2314
  ns = gfc_current_ns->contained;
2315
  gfc_current_ns->contained = ns->sibling;
2316
  gfc_free_namespace (ns);
2317
 
2318
  pop_state ();
2319
}
2320
 
2321
 
2322
/* Parse a PROGRAM, SUBROUTINE or FUNCTION unit.  */
2323
 
2324
static void
2325
parse_progunit (gfc_statement st)
2326
{
2327
  gfc_state_data *p;
2328
  int n;
2329
 
2330
  st = parse_spec (st);
2331
  switch (st)
2332
    {
2333
    case ST_NONE:
2334
      unexpected_eof ();
2335
 
2336
    case ST_CONTAINS:
2337
      goto contains;
2338
 
2339
    case_end:
2340
      accept_statement (st);
2341
      goto done;
2342
 
2343
    default:
2344
      break;
2345
    }
2346
 
2347
loop:
2348
  for (;;)
2349
    {
2350
      st = parse_executable (st);
2351
 
2352
      switch (st)
2353
        {
2354
        case ST_NONE:
2355
          unexpected_eof ();
2356
 
2357
        case ST_CONTAINS:
2358
          goto contains;
2359
 
2360
        case_end:
2361
          accept_statement (st);
2362
          goto done;
2363
 
2364
        default:
2365
          break;
2366
        }
2367
 
2368
      unexpected_statement (st);
2369
      reject_statement ();
2370
      st = next_statement ();
2371
    }
2372
 
2373
contains:
2374
  n = 0;
2375
 
2376
  for (p = gfc_state_stack; p; p = p->previous)
2377
    if (p->state == COMP_CONTAINS)
2378
      n++;
2379
 
2380
  if (gfc_find_state (COMP_MODULE) == SUCCESS)
2381
    n--;
2382
 
2383
  if (n > 0)
2384
    {
2385
      gfc_error ("CONTAINS statement at %C is already in a contained "
2386
                 "program unit");
2387
      st = next_statement ();
2388
      goto loop;
2389
    }
2390
 
2391
  parse_contained (0);
2392
 
2393
done:
2394
  gfc_current_ns->code = gfc_state_stack->head;
2395
}
2396
 
2397
 
2398
/* Come here to complain about a global symbol already in use as
2399
   something else.  */
2400
 
2401
void
2402
global_used (gfc_gsymbol *sym, locus *where)
2403
{
2404
  const char *name;
2405
 
2406
  if (where == NULL)
2407
    where = &gfc_current_locus;
2408
 
2409
  switch(sym->type)
2410
    {
2411
    case GSYM_PROGRAM:
2412
      name = "PROGRAM";
2413
      break;
2414
    case GSYM_FUNCTION:
2415
      name = "FUNCTION";
2416
      break;
2417
    case GSYM_SUBROUTINE:
2418
      name = "SUBROUTINE";
2419
      break;
2420
    case GSYM_COMMON:
2421
      name = "COMMON";
2422
      break;
2423
    case GSYM_BLOCK_DATA:
2424
      name = "BLOCK DATA";
2425
      break;
2426
    case GSYM_MODULE:
2427
      name = "MODULE";
2428
      break;
2429
    default:
2430
      gfc_internal_error ("gfc_gsymbol_type(): Bad type");
2431
      name = NULL;
2432
    }
2433
 
2434
  gfc_error("Global name '%s' at %L is already being used as a %s at %L",
2435
              sym->name, where, name, &sym->where);
2436
}
2437
 
2438
 
2439
/* Parse a block data program unit.  */
2440
 
2441
static void
2442
parse_block_data (void)
2443
{
2444
  gfc_statement st;
2445
  static locus blank_locus;
2446
  static int blank_block=0;
2447
  gfc_gsymbol *s;
2448
 
2449
  gfc_current_ns->proc_name = gfc_new_block;
2450
  gfc_current_ns->is_block_data = 1;
2451
 
2452
  if (gfc_new_block == NULL)
2453
    {
2454
      if (blank_block)
2455
       gfc_error ("Blank BLOCK DATA at %C conflicts with "
2456
                  "prior BLOCK DATA at %L", &blank_locus);
2457
      else
2458
       {
2459
         blank_block = 1;
2460
         blank_locus = gfc_current_locus;
2461
       }
2462
    }
2463
  else
2464
    {
2465
      s = gfc_get_gsymbol (gfc_new_block->name);
2466
      if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_BLOCK_DATA))
2467
       global_used(s, NULL);
2468
      else
2469
       {
2470
         s->type = GSYM_BLOCK_DATA;
2471
         s->where = gfc_current_locus;
2472
         s->defined = 1;
2473
       }
2474
    }
2475
 
2476
  st = parse_spec (ST_NONE);
2477
 
2478
  while (st != ST_END_BLOCK_DATA)
2479
    {
2480
      gfc_error ("Unexpected %s statement in BLOCK DATA at %C",
2481
                 gfc_ascii_statement (st));
2482
      reject_statement ();
2483
      st = next_statement ();
2484
    }
2485
}
2486
 
2487
 
2488
/* Parse a module subprogram.  */
2489
 
2490
static void
2491
parse_module (void)
2492
{
2493
  gfc_statement st;
2494
  gfc_gsymbol *s;
2495
 
2496
  s = gfc_get_gsymbol (gfc_new_block->name);
2497
  if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
2498
    global_used(s, NULL);
2499
  else
2500
    {
2501
      s->type = GSYM_MODULE;
2502
      s->where = gfc_current_locus;
2503
      s->defined = 1;
2504
    }
2505
 
2506
  st = parse_spec (ST_NONE);
2507
 
2508
loop:
2509
  switch (st)
2510
    {
2511
    case ST_NONE:
2512
      unexpected_eof ();
2513
 
2514
    case ST_CONTAINS:
2515
      parse_contained (1);
2516
      break;
2517
 
2518
    case ST_END_MODULE:
2519
      accept_statement (st);
2520
      break;
2521
 
2522
    default:
2523
      gfc_error ("Unexpected %s statement in MODULE at %C",
2524
                 gfc_ascii_statement (st));
2525
 
2526
      reject_statement ();
2527
      st = next_statement ();
2528
      goto loop;
2529
    }
2530
}
2531
 
2532
 
2533
/* Add a procedure name to the global symbol table.  */
2534
 
2535
static void
2536
add_global_procedure (int sub)
2537
{
2538
  gfc_gsymbol *s;
2539
 
2540
  s = gfc_get_gsymbol(gfc_new_block->name);
2541
 
2542
  if (s->defined
2543
        || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2544
    global_used(s, NULL);
2545
  else
2546
    {
2547
      s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2548
      s->where = gfc_current_locus;
2549
      s->defined = 1;
2550
    }
2551
}
2552
 
2553
 
2554
/* Add a program to the global symbol table.  */
2555
 
2556
static void
2557
add_global_program (void)
2558
{
2559
  gfc_gsymbol *s;
2560
 
2561
  if (gfc_new_block == NULL)
2562
    return;
2563
  s = gfc_get_gsymbol (gfc_new_block->name);
2564
 
2565
  if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_PROGRAM))
2566
    global_used(s, NULL);
2567
  else
2568
    {
2569
      s->type = GSYM_PROGRAM;
2570
      s->where = gfc_current_locus;
2571
      s->defined = 1;
2572
    }
2573
}
2574
 
2575
 
2576
/* Top level parser.  */
2577
 
2578
try
2579
gfc_parse_file (void)
2580
{
2581
  int seen_program, errors_before, errors;
2582
  gfc_state_data top, s;
2583
  gfc_statement st;
2584
  locus prog_locus;
2585
 
2586
  top.state = COMP_NONE;
2587
  top.sym = NULL;
2588
  top.previous = NULL;
2589
  top.head = top.tail = NULL;
2590
  top.do_variable = NULL;
2591
 
2592
  gfc_state_stack = &top;
2593
 
2594
  gfc_clear_new_st ();
2595
 
2596
  gfc_statement_label = NULL;
2597
 
2598
  if (setjmp (eof_buf))
2599
    return FAILURE;     /* Come here on unexpected EOF */
2600
 
2601
  seen_program = 0;
2602
 
2603
  /* Exit early for empty files.  */
2604
  if (gfc_at_eof ())
2605
    goto done;
2606
 
2607
loop:
2608
  gfc_init_2 ();
2609
  st = next_statement ();
2610
  switch (st)
2611
    {
2612
    case ST_NONE:
2613
      gfc_done_2 ();
2614
      goto done;
2615
 
2616
    case ST_PROGRAM:
2617
      if (seen_program)
2618
        goto duplicate_main;
2619
      seen_program = 1;
2620
      prog_locus = gfc_current_locus;
2621
 
2622
      push_state (&s, COMP_PROGRAM, gfc_new_block);
2623
      main_program_symbol(gfc_current_ns);
2624
      accept_statement (st);
2625
      add_global_program ();
2626
      parse_progunit (ST_NONE);
2627
      break;
2628
 
2629
    case ST_SUBROUTINE:
2630
      add_global_procedure (1);
2631
      push_state (&s, COMP_SUBROUTINE, gfc_new_block);
2632
      accept_statement (st);
2633
      parse_progunit (ST_NONE);
2634
      break;
2635
 
2636
    case ST_FUNCTION:
2637
      add_global_procedure (0);
2638
      push_state (&s, COMP_FUNCTION, gfc_new_block);
2639
      accept_statement (st);
2640
      parse_progunit (ST_NONE);
2641
      break;
2642
 
2643
    case ST_BLOCK_DATA:
2644
      push_state (&s, COMP_BLOCK_DATA, gfc_new_block);
2645
      accept_statement (st);
2646
      parse_block_data ();
2647
      break;
2648
 
2649
    case ST_MODULE:
2650
      push_state (&s, COMP_MODULE, gfc_new_block);
2651
      accept_statement (st);
2652
 
2653
      gfc_get_errors (NULL, &errors_before);
2654
      parse_module ();
2655
      break;
2656
 
2657
    /* Anything else starts a nameless main program block.  */
2658
    default:
2659
      if (seen_program)
2660
        goto duplicate_main;
2661
      seen_program = 1;
2662
      prog_locus = gfc_current_locus;
2663
 
2664
      push_state (&s, COMP_PROGRAM, gfc_new_block);
2665
      main_program_symbol(gfc_current_ns);
2666
      parse_progunit (st);
2667
      break;
2668
    }
2669
 
2670
  gfc_current_ns->code = s.head;
2671
 
2672
  gfc_resolve (gfc_current_ns);
2673
 
2674
  /* Dump the parse tree if requested.  */
2675
  if (gfc_option.verbose)
2676
    gfc_show_namespace (gfc_current_ns);
2677
 
2678
  gfc_get_errors (NULL, &errors);
2679
  if (s.state == COMP_MODULE)
2680
    {
2681
      gfc_dump_module (s.sym->name, errors_before == errors);
2682
      if (errors == 0 && ! gfc_option.flag_no_backend)
2683
        gfc_generate_module_code (gfc_current_ns);
2684
    }
2685
  else
2686
    {
2687
      if (errors == 0 && ! gfc_option.flag_no_backend)
2688
        gfc_generate_code (gfc_current_ns);
2689
    }
2690
 
2691
  pop_state ();
2692
  gfc_done_2 ();
2693
  goto loop;
2694
 
2695
done:
2696
  return SUCCESS;
2697
 
2698
duplicate_main:
2699
  /* If we see a duplicate main program, shut down.  If the second
2700
     instance is an implied main program, ie data decls or executable
2701
     statements, we're in for lots of errors.  */
2702
  gfc_error ("Two main PROGRAMs at %L and %C", &prog_locus);
2703
  reject_statement ();
2704
  gfc_done_2 ();
2705
  return SUCCESS;
2706
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.