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

Subversion Repositories scarts

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Matching subroutines in all sizes, shapes and colors.
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 "flags.h"
27
#include "gfortran.h"
28
#include "match.h"
29
#include "parse.h"
30
 
31
/* For matching and debugging purposes.  Order matters here!  The
32
   unary operators /must/ precede the binary plus and minus, or
33
   the expression parser breaks.  */
34
 
35
mstring intrinsic_operators[] = {
36
    minit ("+", INTRINSIC_UPLUS),
37
    minit ("-", INTRINSIC_UMINUS),
38
    minit ("+", INTRINSIC_PLUS),
39
    minit ("-", INTRINSIC_MINUS),
40
    minit ("**", INTRINSIC_POWER),
41
    minit ("//", INTRINSIC_CONCAT),
42
    minit ("*", INTRINSIC_TIMES),
43
    minit ("/", INTRINSIC_DIVIDE),
44
    minit (".and.", INTRINSIC_AND),
45
    minit (".or.", INTRINSIC_OR),
46
    minit (".eqv.", INTRINSIC_EQV),
47
    minit (".neqv.", INTRINSIC_NEQV),
48
    minit (".eq.", INTRINSIC_EQ),
49
    minit ("==", INTRINSIC_EQ),
50
    minit (".ne.", INTRINSIC_NE),
51
    minit ("/=", INTRINSIC_NE),
52
    minit (".ge.", INTRINSIC_GE),
53
    minit (">=", INTRINSIC_GE),
54
    minit (".le.", INTRINSIC_LE),
55
    minit ("<=", INTRINSIC_LE),
56
    minit (".lt.", INTRINSIC_LT),
57
    minit ("<", INTRINSIC_LT),
58
    minit (".gt.", INTRINSIC_GT),
59
    minit (">", INTRINSIC_GT),
60
    minit (".not.", INTRINSIC_NOT),
61
    minit ("parens", INTRINSIC_PARENTHESES),
62
    minit (NULL, INTRINSIC_NONE)
63
};
64
 
65
 
66
/******************** Generic matching subroutines ************************/
67
 
68
/* In free form, match at least one space.  Always matches in fixed
69
   form.  */
70
 
71
match
72
gfc_match_space (void)
73
{
74
  locus old_loc;
75
  int c;
76
 
77
  if (gfc_current_form == FORM_FIXED)
78
    return MATCH_YES;
79
 
80
  old_loc = gfc_current_locus;
81
 
82
  c = gfc_next_char ();
83
  if (!gfc_is_whitespace (c))
84
    {
85
      gfc_current_locus = old_loc;
86
      return MATCH_NO;
87
    }
88
 
89
  gfc_gobble_whitespace ();
90
 
91
  return MATCH_YES;
92
}
93
 
94
 
95
/* Match an end of statement.  End of statement is optional
96
   whitespace, followed by a ';' or '\n' or comment '!'.  If a
97
   semicolon is found, we continue to eat whitespace and semicolons.  */
98
 
99
match
100
gfc_match_eos (void)
101
{
102
  locus old_loc;
103
  int flag, c;
104
 
105
  flag = 0;
106
 
107
  for (;;)
108
    {
109
      old_loc = gfc_current_locus;
110
      gfc_gobble_whitespace ();
111
 
112
      c = gfc_next_char ();
113
      switch (c)
114
        {
115
        case '!':
116
          do
117
            {
118
              c = gfc_next_char ();
119
            }
120
          while (c != '\n');
121
 
122
          /* Fall through */
123
 
124
        case '\n':
125
          return MATCH_YES;
126
 
127
        case ';':
128
          flag = 1;
129
          continue;
130
        }
131
 
132
      break;
133
    }
134
 
135
  gfc_current_locus = old_loc;
136
  return (flag) ? MATCH_YES : MATCH_NO;
137
}
138
 
139
 
140
/* Match a literal integer on the input, setting the value on
141
   MATCH_YES.  Literal ints occur in kind-parameters as well as
142
   old-style character length specifications.  */
143
 
144
match
145
gfc_match_small_literal_int (int *value, int *cnt)
146
{
147
  locus old_loc;
148
  char c;
149
  int i, j;
150
 
151
  old_loc = gfc_current_locus;
152
 
153
  gfc_gobble_whitespace ();
154
  c = gfc_next_char ();
155
  *cnt = 0;
156
 
157
  if (!ISDIGIT (c))
158
    {
159
      gfc_current_locus = old_loc;
160
      return MATCH_NO;
161
    }
162
 
163
  i = c - '0';
164
  j = 1;
165
 
166
  for (;;)
167
    {
168
      old_loc = gfc_current_locus;
169
      c = gfc_next_char ();
170
 
171
      if (!ISDIGIT (c))
172
        break;
173
 
174
      i = 10 * i + c - '0';
175
      j++;
176
 
177
      if (i > 99999999)
178
        {
179
          gfc_error ("Integer too large at %C");
180
          return MATCH_ERROR;
181
        }
182
    }
183
 
184
  gfc_current_locus = old_loc;
185
 
186
  *value = i;
187
  *cnt = j;
188
  return MATCH_YES;
189
}
190
 
191
 
192
/* Match a small, constant integer expression, like in a kind
193
   statement.  On MATCH_YES, 'value' is set.  */
194
 
195
match
196
gfc_match_small_int (int *value)
197
{
198
  gfc_expr *expr;
199
  const char *p;
200
  match m;
201
  int i;
202
 
203
  m = gfc_match_expr (&expr);
204
  if (m != MATCH_YES)
205
    return m;
206
 
207
  p = gfc_extract_int (expr, &i);
208
  gfc_free_expr (expr);
209
 
210
  if (p != NULL)
211
    {
212
      gfc_error (p);
213
      m = MATCH_ERROR;
214
    }
215
 
216
  *value = i;
217
  return m;
218
}
219
 
220
 
221
/* Matches a statement label.  Uses gfc_match_small_literal_int() to
222
   do most of the work.  */
223
 
224
match
225
gfc_match_st_label (gfc_st_label ** label)
226
{
227
  locus old_loc;
228
  match m;
229
  int i, cnt;
230
 
231
  old_loc = gfc_current_locus;
232
 
233
  m = gfc_match_small_literal_int (&i, &cnt);
234
  if (m != MATCH_YES)
235
    return m;
236
 
237
  if (cnt > 5)
238
    {
239
      gfc_error ("Too many digits in statement label at %C");
240
      goto cleanup;
241
    }
242
 
243
  if (i == 0)
244
    {
245
      gfc_error ("Statement label at %C is zero");
246
      goto cleanup;
247
    }
248
 
249
  *label = gfc_get_st_label (i);
250
  return MATCH_YES;
251
 
252
cleanup:
253
 
254
  gfc_current_locus = old_loc;
255
  return MATCH_ERROR;
256
}
257
 
258
 
259
/* Match and validate a label associated with a named IF, DO or SELECT
260
   statement.  If the symbol does not have the label attribute, we add
261
   it.  We also make sure the symbol does not refer to another
262
   (active) block.  A matched label is pointed to by gfc_new_block.  */
263
 
264
match
265
gfc_match_label (void)
266
{
267
  char name[GFC_MAX_SYMBOL_LEN + 1];
268
  match m;
269
 
270
  gfc_new_block = NULL;
271
 
272
  m = gfc_match (" %n :", name);
273
  if (m != MATCH_YES)
274
    return m;
275
 
276
  if (gfc_get_symbol (name, NULL, &gfc_new_block))
277
    {
278
      gfc_error ("Label name '%s' at %C is ambiguous", name);
279
      return MATCH_ERROR;
280
    }
281
 
282
  if (gfc_new_block->attr.flavor == FL_LABEL)
283
    {
284
      gfc_error ("Duplicate construct label '%s' at %C", name);
285
      return MATCH_ERROR;
286
    }
287
 
288
  if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
289
                      gfc_new_block->name, NULL) == FAILURE)
290
    return MATCH_ERROR;
291
 
292
  return MATCH_YES;
293
}
294
 
295
 
296
/* Try and match the input against an array of possibilities.  If one
297
   potential matching string is a substring of another, the longest
298
   match takes precedence.  Spaces in the target strings are optional
299
   spaces that do not necessarily have to be found in the input
300
   stream.  In fixed mode, spaces never appear.  If whitespace is
301
   matched, it matches unlimited whitespace in the input.  For this
302
   reason, the 'mp' member of the mstring structure is used to track
303
   the progress of each potential match.
304
 
305
   If there is no match we return the tag associated with the
306
   terminating NULL mstring structure and leave the locus pointer
307
   where it started.  If there is a match we return the tag member of
308
   the matched mstring and leave the locus pointer after the matched
309
   character.
310
 
311
   A '%' character is a mandatory space.  */
312
 
313
int
314
gfc_match_strings (mstring * a)
315
{
316
  mstring *p, *best_match;
317
  int no_match, c, possibles;
318
  locus match_loc;
319
 
320
  possibles = 0;
321
 
322
  for (p = a; p->string != NULL; p++)
323
    {
324
      p->mp = p->string;
325
      possibles++;
326
    }
327
 
328
  no_match = p->tag;
329
 
330
  best_match = NULL;
331
  match_loc = gfc_current_locus;
332
 
333
  gfc_gobble_whitespace ();
334
 
335
  while (possibles > 0)
336
    {
337
      c = gfc_next_char ();
338
 
339
      /* Apply the next character to the current possibilities.  */
340
      for (p = a; p->string != NULL; p++)
341
        {
342
          if (p->mp == NULL)
343
            continue;
344
 
345
          if (*p->mp == ' ')
346
            {
347
              /* Space matches 1+ whitespace(s).  */
348
              if ((gfc_current_form == FORM_FREE)
349
                  && gfc_is_whitespace (c))
350
                continue;
351
 
352
              p->mp++;
353
            }
354
 
355
          if (*p->mp != c)
356
            {
357
              /* Match failed.  */
358
              p->mp = NULL;
359
              possibles--;
360
              continue;
361
            }
362
 
363
          p->mp++;
364
          if (*p->mp == '\0')
365
            {
366
              /* Found a match.  */
367
              match_loc = gfc_current_locus;
368
              best_match = p;
369
              possibles--;
370
              p->mp = NULL;
371
            }
372
        }
373
    }
374
 
375
  gfc_current_locus = match_loc;
376
 
377
  return (best_match == NULL) ? no_match : best_match->tag;
378
}
379
 
380
 
381
/* See if the current input looks like a name of some sort.  Modifies
382
   the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.  */
383
 
384
match
385
gfc_match_name (char *buffer)
386
{
387
  locus old_loc;
388
  int i, c;
389
 
390
  old_loc = gfc_current_locus;
391
  gfc_gobble_whitespace ();
392
 
393
  c = gfc_next_char ();
394
  if (!ISALPHA (c))
395
    {
396
      gfc_current_locus = old_loc;
397
      return MATCH_NO;
398
    }
399
 
400
  i = 0;
401
 
402
  do
403
    {
404
      buffer[i++] = c;
405
 
406
      if (i > gfc_option.max_identifier_length)
407
        {
408
          gfc_error ("Name at %C is too long");
409
          return MATCH_ERROR;
410
        }
411
 
412
      old_loc = gfc_current_locus;
413
      c = gfc_next_char ();
414
    }
415
  while (ISALNUM (c)
416
         || c == '_'
417
         || (gfc_option.flag_dollar_ok && c == '$'));
418
 
419
  buffer[i] = '\0';
420
  gfc_current_locus = old_loc;
421
 
422
  return MATCH_YES;
423
}
424
 
425
 
426
/* Match a symbol on the input.  Modifies the pointer to the symbol
427
   pointer if successful.  */
428
 
429
match
430
gfc_match_sym_tree (gfc_symtree ** matched_symbol, int host_assoc)
431
{
432
  char buffer[GFC_MAX_SYMBOL_LEN + 1];
433
  match m;
434
 
435
  m = gfc_match_name (buffer);
436
  if (m != MATCH_YES)
437
    return m;
438
 
439
  if (host_assoc)
440
    return (gfc_get_ha_sym_tree (buffer, matched_symbol))
441
      ? MATCH_ERROR : MATCH_YES;
442
 
443
  if (gfc_get_sym_tree (buffer, NULL, matched_symbol))
444
    return MATCH_ERROR;
445
 
446
  return MATCH_YES;
447
}
448
 
449
 
450
match
451
gfc_match_symbol (gfc_symbol ** matched_symbol, int host_assoc)
452
{
453
  gfc_symtree *st;
454
  match m;
455
 
456
  m = gfc_match_sym_tree (&st, host_assoc);
457
 
458
  if (m == MATCH_YES)
459
    {
460
      if (st)
461
        *matched_symbol = st->n.sym;
462
      else
463
        *matched_symbol = NULL;
464
    }
465
  else
466
    *matched_symbol = NULL;
467
  return m;
468
}
469
 
470
/* Match an intrinsic operator.  Returns an INTRINSIC enum. While matching,
471
   we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
472
   in matchexp.c.  */
473
 
474
match
475
gfc_match_intrinsic_op (gfc_intrinsic_op * result)
476
{
477
  gfc_intrinsic_op op;
478
 
479
  op = (gfc_intrinsic_op) gfc_match_strings (intrinsic_operators);
480
 
481
  if (op == INTRINSIC_NONE)
482
    return MATCH_NO;
483
 
484
  *result = op;
485
  return MATCH_YES;
486
}
487
 
488
 
489
/* Match a loop control phrase:
490
 
491
    <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
492
 
493
   If the final integer expression is not present, a constant unity
494
   expression is returned.  We don't return MATCH_ERROR until after
495
   the equals sign is seen.  */
496
 
497
match
498
gfc_match_iterator (gfc_iterator * iter, int init_flag)
499
{
500
  char name[GFC_MAX_SYMBOL_LEN + 1];
501
  gfc_expr *var, *e1, *e2, *e3;
502
  locus start;
503
  match m;
504
 
505
  /* Match the start of an iterator without affecting the symbol
506
     table.  */
507
 
508
  start = gfc_current_locus;
509
  m = gfc_match (" %n =", name);
510
  gfc_current_locus = start;
511
 
512
  if (m != MATCH_YES)
513
    return MATCH_NO;
514
 
515
  m = gfc_match_variable (&var, 0);
516
  if (m != MATCH_YES)
517
    return MATCH_NO;
518
 
519
  gfc_match_char ('=');
520
 
521
  e1 = e2 = e3 = NULL;
522
 
523
  if (var->ref != NULL)
524
    {
525
      gfc_error ("Loop variable at %C cannot be a sub-component");
526
      goto cleanup;
527
    }
528
 
529
  if (var->symtree->n.sym->attr.intent == INTENT_IN)
530
    {
531
      gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
532
                 var->symtree->n.sym->name);
533
      goto cleanup;
534
    }
535
 
536
  if (var->symtree->n.sym->attr.pointer)
537
    {
538
      gfc_error ("Loop variable at %C cannot have the POINTER attribute");
539
      goto cleanup;
540
    }
541
 
542
  m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
543
  if (m == MATCH_NO)
544
    goto syntax;
545
  if (m == MATCH_ERROR)
546
    goto cleanup;
547
 
548
  if (gfc_match_char (',') != MATCH_YES)
549
    goto syntax;
550
 
551
  m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
552
  if (m == MATCH_NO)
553
    goto syntax;
554
  if (m == MATCH_ERROR)
555
    goto cleanup;
556
 
557
  if (gfc_match_char (',') != MATCH_YES)
558
    {
559
      e3 = gfc_int_expr (1);
560
      goto done;
561
    }
562
 
563
  m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
564
  if (m == MATCH_ERROR)
565
    goto cleanup;
566
  if (m == MATCH_NO)
567
    {
568
      gfc_error ("Expected a step value in iterator at %C");
569
      goto cleanup;
570
    }
571
 
572
done:
573
  iter->var = var;
574
  iter->start = e1;
575
  iter->end = e2;
576
  iter->step = e3;
577
  return MATCH_YES;
578
 
579
syntax:
580
  gfc_error ("Syntax error in iterator at %C");
581
 
582
cleanup:
583
  gfc_free_expr (e1);
584
  gfc_free_expr (e2);
585
  gfc_free_expr (e3);
586
 
587
  return MATCH_ERROR;
588
}
589
 
590
 
591
/* Tries to match the next non-whitespace character on the input.
592
   This subroutine does not return MATCH_ERROR.  */
593
 
594
match
595
gfc_match_char (char c)
596
{
597
  locus where;
598
 
599
  where = gfc_current_locus;
600
  gfc_gobble_whitespace ();
601
 
602
  if (gfc_next_char () == c)
603
    return MATCH_YES;
604
 
605
  gfc_current_locus = where;
606
  return MATCH_NO;
607
}
608
 
609
 
610
/* General purpose matching subroutine.  The target string is a
611
   scanf-like format string in which spaces correspond to arbitrary
612
   whitespace (including no whitespace), characters correspond to
613
   themselves.  The %-codes are:
614
 
615
   %%  Literal percent sign
616
   %e  Expression, pointer to a pointer is set
617
   %s  Symbol, pointer to the symbol is set
618
   %n  Name, character buffer is set to name
619
   %t  Matches end of statement.
620
   %o  Matches an intrinsic operator, returned as an INTRINSIC enum.
621
   %l  Matches a statement label
622
   %v  Matches a variable expression (an lvalue)
623
   %   Matches a required space (in free form) and optional spaces.  */
624
 
625
match
626
gfc_match (const char *target, ...)
627
{
628
  gfc_st_label **label;
629
  int matches, *ip;
630
  locus old_loc;
631
  va_list argp;
632
  char c, *np;
633
  match m, n;
634
  void **vp;
635
  const char *p;
636
 
637
  old_loc = gfc_current_locus;
638
  va_start (argp, target);
639
  m = MATCH_NO;
640
  matches = 0;
641
  p = target;
642
 
643
loop:
644
  c = *p++;
645
  switch (c)
646
    {
647
    case ' ':
648
      gfc_gobble_whitespace ();
649
      goto loop;
650
    case '\0':
651
      m = MATCH_YES;
652
      break;
653
 
654
    case '%':
655
      c = *p++;
656
      switch (c)
657
        {
658
        case 'e':
659
          vp = va_arg (argp, void **);
660
          n = gfc_match_expr ((gfc_expr **) vp);
661
          if (n != MATCH_YES)
662
            {
663
              m = n;
664
              goto not_yes;
665
            }
666
 
667
          matches++;
668
          goto loop;
669
 
670
        case 'v':
671
          vp = va_arg (argp, void **);
672
          n = gfc_match_variable ((gfc_expr **) vp, 0);
673
          if (n != MATCH_YES)
674
            {
675
              m = n;
676
              goto not_yes;
677
            }
678
 
679
          matches++;
680
          goto loop;
681
 
682
        case 's':
683
          vp = va_arg (argp, void **);
684
          n = gfc_match_symbol ((gfc_symbol **) vp, 0);
685
          if (n != MATCH_YES)
686
            {
687
              m = n;
688
              goto not_yes;
689
            }
690
 
691
          matches++;
692
          goto loop;
693
 
694
        case 'n':
695
          np = va_arg (argp, char *);
696
          n = gfc_match_name (np);
697
          if (n != MATCH_YES)
698
            {
699
              m = n;
700
              goto not_yes;
701
            }
702
 
703
          matches++;
704
          goto loop;
705
 
706
        case 'l':
707
          label = va_arg (argp, gfc_st_label **);
708
          n = gfc_match_st_label (label);
709
          if (n != MATCH_YES)
710
            {
711
              m = n;
712
              goto not_yes;
713
            }
714
 
715
          matches++;
716
          goto loop;
717
 
718
        case 'o':
719
          ip = va_arg (argp, int *);
720
          n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
721
          if (n != MATCH_YES)
722
            {
723
              m = n;
724
              goto not_yes;
725
            }
726
 
727
          matches++;
728
          goto loop;
729
 
730
        case 't':
731
          if (gfc_match_eos () != MATCH_YES)
732
            {
733
              m = MATCH_NO;
734
              goto not_yes;
735
            }
736
          goto loop;
737
 
738
        case ' ':
739
          if (gfc_match_space () == MATCH_YES)
740
            goto loop;
741
          m = MATCH_NO;
742
          goto not_yes;
743
 
744
        case '%':
745
          break;        /* Fall through to character matcher */
746
 
747
        default:
748
          gfc_internal_error ("gfc_match(): Bad match code %c", c);
749
        }
750
 
751
    default:
752
      if (c == gfc_next_char ())
753
        goto loop;
754
      break;
755
    }
756
 
757
not_yes:
758
  va_end (argp);
759
 
760
  if (m != MATCH_YES)
761
    {
762
      /* Clean up after a failed match.  */
763
      gfc_current_locus = old_loc;
764
      va_start (argp, target);
765
 
766
      p = target;
767
      for (; matches > 0; matches--)
768
        {
769
          while (*p++ != '%');
770
 
771
          switch (*p++)
772
            {
773
            case '%':
774
              matches++;
775
              break;            /* Skip */
776
 
777
            /* Matches that don't have to be undone */
778
            case 'o':
779
            case 'l':
780
            case 'n':
781
            case 's':
782
              (void)va_arg (argp, void **);
783
              break;
784
 
785
            case 'e':
786
            case 'v':
787
              vp = va_arg (argp, void **);
788
              gfc_free_expr (*vp);
789
              *vp = NULL;
790
              break;
791
            }
792
        }
793
 
794
      va_end (argp);
795
    }
796
 
797
  return m;
798
}
799
 
800
 
801
/*********************** Statement level matching **********************/
802
 
803
/* Matches the start of a program unit, which is the program keyword
804
   followed by an obligatory symbol.  */
805
 
806
match
807
gfc_match_program (void)
808
{
809
  gfc_symbol *sym;
810
  match m;
811
 
812
  m = gfc_match ("% %s%t", &sym);
813
 
814
  if (m == MATCH_NO)
815
    {
816
      gfc_error ("Invalid form of PROGRAM statement at %C");
817
      m = MATCH_ERROR;
818
    }
819
 
820
  if (m == MATCH_ERROR)
821
    return m;
822
 
823
  if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
824
    return MATCH_ERROR;
825
 
826
  gfc_new_block = sym;
827
 
828
  return MATCH_YES;
829
}
830
 
831
 
832
/* Match a simple assignment statement.  */
833
 
834
match
835
gfc_match_assignment (void)
836
{
837
  gfc_expr *lvalue, *rvalue;
838
  locus old_loc;
839
  match m;
840
 
841
  old_loc = gfc_current_locus;
842
 
843
  lvalue = rvalue = NULL;
844
  m = gfc_match (" %v =", &lvalue);
845
  if (m != MATCH_YES)
846
    goto cleanup;
847
 
848
  if (lvalue->symtree->n.sym->attr.flavor == FL_PARAMETER)
849
    {
850
      gfc_error ("Cannot assign to a PARAMETER variable at %C");
851
      m = MATCH_ERROR;
852
      goto cleanup;
853
    }
854
 
855
  m = gfc_match (" %e%t", &rvalue);
856
  if (m != MATCH_YES)
857
    goto cleanup;
858
 
859
  gfc_set_sym_referenced (lvalue->symtree->n.sym);
860
 
861
  new_st.op = EXEC_ASSIGN;
862
  new_st.expr = lvalue;
863
  new_st.expr2 = rvalue;
864
 
865
  gfc_check_do_variable (lvalue->symtree);
866
 
867
  return MATCH_YES;
868
 
869
cleanup:
870
  gfc_current_locus = old_loc;
871
  gfc_free_expr (lvalue);
872
  gfc_free_expr (rvalue);
873
  return m;
874
}
875
 
876
 
877
/* Match a pointer assignment statement.  */
878
 
879
match
880
gfc_match_pointer_assignment (void)
881
{
882
  gfc_expr *lvalue, *rvalue;
883
  locus old_loc;
884
  match m;
885
 
886
  old_loc = gfc_current_locus;
887
 
888
  lvalue = rvalue = NULL;
889
 
890
  m = gfc_match (" %v =>", &lvalue);
891
  if (m != MATCH_YES)
892
    {
893
      m = MATCH_NO;
894
      goto cleanup;
895
    }
896
 
897
  m = gfc_match (" %e%t", &rvalue);
898
  if (m != MATCH_YES)
899
    goto cleanup;
900
 
901
  new_st.op = EXEC_POINTER_ASSIGN;
902
  new_st.expr = lvalue;
903
  new_st.expr2 = rvalue;
904
 
905
  return MATCH_YES;
906
 
907
cleanup:
908
  gfc_current_locus = old_loc;
909
  gfc_free_expr (lvalue);
910
  gfc_free_expr (rvalue);
911
  return m;
912
}
913
 
914
 
915
/* We try to match an easy arithmetic IF statement. This only happens
916
   when just after having encountered a simple IF statement. This code
917
   is really duplicate with parts of the gfc_match_if code, but this is
918
   *much* easier.  */
919
static match
920
match_arithmetic_if (void)
921
{
922
  gfc_st_label *l1, *l2, *l3;
923
  gfc_expr *expr;
924
  match m;
925
 
926
  m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
927
  if (m != MATCH_YES)
928
    return m;
929
 
930
  if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
931
      || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
932
      || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
933
    {
934
      gfc_free_expr (expr);
935
      return MATCH_ERROR;
936
    }
937
 
938
  if (gfc_notify_std (GFC_STD_F95_DEL,
939
                      "Obsolete: arithmetic IF statement at %C") == FAILURE)
940
    return MATCH_ERROR;
941
 
942
  new_st.op = EXEC_ARITHMETIC_IF;
943
  new_st.expr = expr;
944
  new_st.label = l1;
945
  new_st.label2 = l2;
946
  new_st.label3 = l3;
947
 
948
  return MATCH_YES;
949
}
950
 
951
 
952
/* The IF statement is a bit of a pain.  First of all, there are three
953
   forms of it, the simple IF, the IF that starts a block and the
954
   arithmetic IF.
955
 
956
   There is a problem with the simple IF and that is the fact that we
957
   only have a single level of undo information on symbols.  What this
958
   means is for a simple IF, we must re-match the whole IF statement
959
   multiple times in order to guarantee that the symbol table ends up
960
   in the proper state.  */
961
 
962
static match match_simple_forall (void);
963
static match match_simple_where (void);
964
 
965
match
966
gfc_match_if (gfc_statement * if_type)
967
{
968
  gfc_expr *expr;
969
  gfc_st_label *l1, *l2, *l3;
970
  locus old_loc;
971
  gfc_code *p;
972
  match m, n;
973
 
974
  n = gfc_match_label ();
975
  if (n == MATCH_ERROR)
976
    return n;
977
 
978
  old_loc = gfc_current_locus;
979
 
980
  m = gfc_match (" if ( %e", &expr);
981
  if (m != MATCH_YES)
982
    return m;
983
 
984
  if (gfc_match_char (')') != MATCH_YES)
985
    {
986
      gfc_error ("Syntax error in IF-expression at %C");
987
      gfc_free_expr (expr);
988
      return MATCH_ERROR;
989
    }
990
 
991
  m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
992
 
993
  if (m == MATCH_YES)
994
    {
995
      if (n == MATCH_YES)
996
        {
997
          gfc_error
998
            ("Block label not appropriate for arithmetic IF statement "
999
             "at %C");
1000
 
1001
          gfc_free_expr (expr);
1002
          return MATCH_ERROR;
1003
        }
1004
 
1005
      if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1006
          || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1007
          || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1008
        {
1009
 
1010
          gfc_free_expr (expr);
1011
          return MATCH_ERROR;
1012
        }
1013
 
1014
      if (gfc_notify_std (GFC_STD_F95_DEL,
1015
                          "Obsolete: arithmetic IF statement at %C")
1016
          == FAILURE)
1017
        return MATCH_ERROR;
1018
 
1019
      new_st.op = EXEC_ARITHMETIC_IF;
1020
      new_st.expr = expr;
1021
      new_st.label = l1;
1022
      new_st.label2 = l2;
1023
      new_st.label3 = l3;
1024
 
1025
      *if_type = ST_ARITHMETIC_IF;
1026
      return MATCH_YES;
1027
    }
1028
 
1029
  if (gfc_match (" then%t") == MATCH_YES)
1030
    {
1031
      new_st.op = EXEC_IF;
1032
      new_st.expr = expr;
1033
 
1034
      *if_type = ST_IF_BLOCK;
1035
      return MATCH_YES;
1036
    }
1037
 
1038
  if (n == MATCH_YES)
1039
    {
1040
      gfc_error ("Block label is not appropriate IF statement at %C");
1041
 
1042
      gfc_free_expr (expr);
1043
      return MATCH_ERROR;
1044
    }
1045
 
1046
  /* At this point the only thing left is a simple IF statement.  At
1047
     this point, n has to be MATCH_NO, so we don't have to worry about
1048
     re-matching a block label.  From what we've got so far, try
1049
     matching an assignment.  */
1050
 
1051
  *if_type = ST_SIMPLE_IF;
1052
 
1053
  m = gfc_match_assignment ();
1054
  if (m == MATCH_YES)
1055
    goto got_match;
1056
 
1057
  gfc_free_expr (expr);
1058
  gfc_undo_symbols ();
1059
  gfc_current_locus = old_loc;
1060
 
1061
  gfc_match (" if ( %e ) ", &expr);     /* Guaranteed to match */
1062
 
1063
  m = gfc_match_pointer_assignment ();
1064
  if (m == MATCH_YES)
1065
    goto got_match;
1066
 
1067
  gfc_free_expr (expr);
1068
  gfc_undo_symbols ();
1069
  gfc_current_locus = old_loc;
1070
 
1071
  gfc_match (" if ( %e ) ", &expr);     /* Guaranteed to match */
1072
 
1073
  /* Look at the next keyword to see which matcher to call.  Matching
1074
     the keyword doesn't affect the symbol table, so we don't have to
1075
     restore between tries.  */
1076
 
1077
#define match(string, subr, statement) \
1078
  if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1079
 
1080
  gfc_clear_error ();
1081
 
1082
  match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1083
    match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1084
    match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1085
    match ("call", gfc_match_call, ST_CALL)
1086
    match ("close", gfc_match_close, ST_CLOSE)
1087
    match ("continue", gfc_match_continue, ST_CONTINUE)
1088
    match ("cycle", gfc_match_cycle, ST_CYCLE)
1089
    match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1090
    match ("end file", gfc_match_endfile, ST_END_FILE)
1091
    match ("exit", gfc_match_exit, ST_EXIT)
1092
    match ("flush", gfc_match_flush, ST_FLUSH)
1093
    match ("forall", match_simple_forall, ST_FORALL)
1094
    match ("go to", gfc_match_goto, ST_GOTO)
1095
    match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1096
    match ("inquire", gfc_match_inquire, ST_INQUIRE)
1097
    match ("nullify", gfc_match_nullify, ST_NULLIFY)
1098
    match ("open", gfc_match_open, ST_OPEN)
1099
    match ("pause", gfc_match_pause, ST_NONE)
1100
    match ("print", gfc_match_print, ST_WRITE)
1101
    match ("read", gfc_match_read, ST_READ)
1102
    match ("return", gfc_match_return, ST_RETURN)
1103
    match ("rewind", gfc_match_rewind, ST_REWIND)
1104
    match ("stop", gfc_match_stop, ST_STOP)
1105
    match ("where", match_simple_where, ST_WHERE)
1106
    match ("write", gfc_match_write, ST_WRITE)
1107
 
1108
  /* All else has failed, so give up.  See if any of the matchers has
1109
     stored an error message of some sort.  */
1110
    if (gfc_error_check () == 0)
1111
    gfc_error ("Unclassifiable statement in IF-clause at %C");
1112
 
1113
  gfc_free_expr (expr);
1114
  return MATCH_ERROR;
1115
 
1116
got_match:
1117
  if (m == MATCH_NO)
1118
    gfc_error ("Syntax error in IF-clause at %C");
1119
  if (m != MATCH_YES)
1120
    {
1121
      gfc_free_expr (expr);
1122
      return MATCH_ERROR;
1123
    }
1124
 
1125
  /* At this point, we've matched the single IF and the action clause
1126
     is in new_st.  Rearrange things so that the IF statement appears
1127
     in new_st.  */
1128
 
1129
  p = gfc_get_code ();
1130
  p->next = gfc_get_code ();
1131
  *p->next = new_st;
1132
  p->next->loc = gfc_current_locus;
1133
 
1134
  p->expr = expr;
1135
  p->op = EXEC_IF;
1136
 
1137
  gfc_clear_new_st ();
1138
 
1139
  new_st.op = EXEC_IF;
1140
  new_st.block = p;
1141
 
1142
  return MATCH_YES;
1143
}
1144
 
1145
#undef match
1146
 
1147
 
1148
/* Match an ELSE statement.  */
1149
 
1150
match
1151
gfc_match_else (void)
1152
{
1153
  char name[GFC_MAX_SYMBOL_LEN + 1];
1154
 
1155
  if (gfc_match_eos () == MATCH_YES)
1156
    return MATCH_YES;
1157
 
1158
  if (gfc_match_name (name) != MATCH_YES
1159
      || gfc_current_block () == NULL
1160
      || gfc_match_eos () != MATCH_YES)
1161
    {
1162
      gfc_error ("Unexpected junk after ELSE statement at %C");
1163
      return MATCH_ERROR;
1164
    }
1165
 
1166
  if (strcmp (name, gfc_current_block ()->name) != 0)
1167
    {
1168
      gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1169
                 name, gfc_current_block ()->name);
1170
      return MATCH_ERROR;
1171
    }
1172
 
1173
  return MATCH_YES;
1174
}
1175
 
1176
 
1177
/* Match an ELSE IF statement.  */
1178
 
1179
match
1180
gfc_match_elseif (void)
1181
{
1182
  char name[GFC_MAX_SYMBOL_LEN + 1];
1183
  gfc_expr *expr;
1184
  match m;
1185
 
1186
  m = gfc_match (" ( %e ) then", &expr);
1187
  if (m != MATCH_YES)
1188
    return m;
1189
 
1190
  if (gfc_match_eos () == MATCH_YES)
1191
    goto done;
1192
 
1193
  if (gfc_match_name (name) != MATCH_YES
1194
      || gfc_current_block () == NULL
1195
      || gfc_match_eos () != MATCH_YES)
1196
    {
1197
      gfc_error ("Unexpected junk after ELSE IF statement at %C");
1198
      goto cleanup;
1199
    }
1200
 
1201
  if (strcmp (name, gfc_current_block ()->name) != 0)
1202
    {
1203
      gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1204
                 name, gfc_current_block ()->name);
1205
      goto cleanup;
1206
    }
1207
 
1208
done:
1209
  new_st.op = EXEC_IF;
1210
  new_st.expr = expr;
1211
  return MATCH_YES;
1212
 
1213
cleanup:
1214
  gfc_free_expr (expr);
1215
  return MATCH_ERROR;
1216
}
1217
 
1218
 
1219
/* Free a gfc_iterator structure.  */
1220
 
1221
void
1222
gfc_free_iterator (gfc_iterator * iter, int flag)
1223
{
1224
 
1225
  if (iter == NULL)
1226
    return;
1227
 
1228
  gfc_free_expr (iter->var);
1229
  gfc_free_expr (iter->start);
1230
  gfc_free_expr (iter->end);
1231
  gfc_free_expr (iter->step);
1232
 
1233
  if (flag)
1234
    gfc_free (iter);
1235
}
1236
 
1237
 
1238
/* Match a DO statement.  */
1239
 
1240
match
1241
gfc_match_do (void)
1242
{
1243
  gfc_iterator iter, *ip;
1244
  locus old_loc;
1245
  gfc_st_label *label;
1246
  match m;
1247
 
1248
  old_loc = gfc_current_locus;
1249
 
1250
  label = NULL;
1251
  iter.var = iter.start = iter.end = iter.step = NULL;
1252
 
1253
  m = gfc_match_label ();
1254
  if (m == MATCH_ERROR)
1255
    return m;
1256
 
1257
  if (gfc_match (" do") != MATCH_YES)
1258
    return MATCH_NO;
1259
 
1260
  m = gfc_match_st_label (&label);
1261
  if (m == MATCH_ERROR)
1262
    goto cleanup;
1263
 
1264
/* Match an infinite DO, make it like a DO WHILE(.TRUE.) */
1265
 
1266
  if (gfc_match_eos () == MATCH_YES)
1267
    {
1268
      iter.end = gfc_logical_expr (1, NULL);
1269
      new_st.op = EXEC_DO_WHILE;
1270
      goto done;
1271
    }
1272
 
1273
  /* match an optional comma, if no comma is found a space is obligatory.  */
1274
  if (gfc_match_char(',') != MATCH_YES
1275
      && gfc_match ("% ") != MATCH_YES)
1276
    return MATCH_NO;
1277
 
1278
  /* See if we have a DO WHILE.  */
1279
  if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1280
    {
1281
      new_st.op = EXEC_DO_WHILE;
1282
      goto done;
1283
    }
1284
 
1285
  /* The abortive DO WHILE may have done something to the symbol
1286
     table, so we start over: */
1287
  gfc_undo_symbols ();
1288
  gfc_current_locus = old_loc;
1289
 
1290
  gfc_match_label ();           /* This won't error */
1291
  gfc_match (" do ");           /* This will work */
1292
 
1293
  gfc_match_st_label (&label);  /* Can't error out */
1294
  gfc_match_char (',');         /* Optional comma */
1295
 
1296
  m = gfc_match_iterator (&iter, 0);
1297
  if (m == MATCH_NO)
1298
    return MATCH_NO;
1299
  if (m == MATCH_ERROR)
1300
    goto cleanup;
1301
 
1302
  gfc_check_do_variable (iter.var->symtree);
1303
 
1304
  if (gfc_match_eos () != MATCH_YES)
1305
    {
1306
      gfc_syntax_error (ST_DO);
1307
      goto cleanup;
1308
    }
1309
 
1310
  new_st.op = EXEC_DO;
1311
 
1312
done:
1313
  if (label != NULL
1314
      && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1315
    goto cleanup;
1316
 
1317
  new_st.label = label;
1318
 
1319
  if (new_st.op == EXEC_DO_WHILE)
1320
    new_st.expr = iter.end;
1321
  else
1322
    {
1323
      new_st.ext.iterator = ip = gfc_get_iterator ();
1324
      *ip = iter;
1325
    }
1326
 
1327
  return MATCH_YES;
1328
 
1329
cleanup:
1330
  gfc_free_iterator (&iter, 0);
1331
 
1332
  return MATCH_ERROR;
1333
}
1334
 
1335
 
1336
/* Match an EXIT or CYCLE statement.  */
1337
 
1338
static match
1339
match_exit_cycle (gfc_statement st, gfc_exec_op op)
1340
{
1341
  gfc_state_data *p;
1342
  gfc_symbol *sym;
1343
  match m;
1344
 
1345
  if (gfc_match_eos () == MATCH_YES)
1346
    sym = NULL;
1347
  else
1348
    {
1349
      m = gfc_match ("% %s%t", &sym);
1350
      if (m == MATCH_ERROR)
1351
        return MATCH_ERROR;
1352
      if (m == MATCH_NO)
1353
        {
1354
          gfc_syntax_error (st);
1355
          return MATCH_ERROR;
1356
        }
1357
 
1358
      if (sym->attr.flavor != FL_LABEL)
1359
        {
1360
          gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1361
                     sym->name, gfc_ascii_statement (st));
1362
          return MATCH_ERROR;
1363
        }
1364
    }
1365
 
1366
  /* Find the loop mentioned specified by the label (or lack of a
1367
     label).  */
1368
  for (p = gfc_state_stack; p; p = p->previous)
1369
    if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1370
      break;
1371
 
1372
  if (p == NULL)
1373
    {
1374
      if (sym == NULL)
1375
        gfc_error ("%s statement at %C is not within a loop",
1376
                   gfc_ascii_statement (st));
1377
      else
1378
        gfc_error ("%s statement at %C is not within loop '%s'",
1379
                   gfc_ascii_statement (st), sym->name);
1380
 
1381
      return MATCH_ERROR;
1382
    }
1383
 
1384
  /* Save the first statement in the loop - needed by the backend.  */
1385
  new_st.ext.whichloop = p->head;
1386
 
1387
  new_st.op = op;
1388
/*  new_st.sym = sym;*/
1389
 
1390
  return MATCH_YES;
1391
}
1392
 
1393
 
1394
/* Match the EXIT statement.  */
1395
 
1396
match
1397
gfc_match_exit (void)
1398
{
1399
 
1400
  return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1401
}
1402
 
1403
 
1404
/* Match the CYCLE statement.  */
1405
 
1406
match
1407
gfc_match_cycle (void)
1408
{
1409
 
1410
  return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1411
}
1412
 
1413
 
1414
/* Match a number or character constant after a STOP or PAUSE statement.  */
1415
 
1416
static match
1417
gfc_match_stopcode (gfc_statement st)
1418
{
1419
  int stop_code;
1420
  gfc_expr *e;
1421
  match m;
1422
  int cnt;
1423
 
1424
  stop_code = -1;
1425
  e = NULL;
1426
 
1427
  if (gfc_match_eos () != MATCH_YES)
1428
    {
1429
      m = gfc_match_small_literal_int (&stop_code, &cnt);
1430
      if (m == MATCH_ERROR)
1431
        goto cleanup;
1432
 
1433
      if (m == MATCH_YES && cnt > 5)
1434
        {
1435
          gfc_error ("Too many digits in STOP code at %C");
1436
          goto cleanup;
1437
        }
1438
 
1439
      if (m == MATCH_NO)
1440
        {
1441
          /* Try a character constant.  */
1442
          m = gfc_match_expr (&e);
1443
          if (m == MATCH_ERROR)
1444
            goto cleanup;
1445
          if (m == MATCH_NO)
1446
            goto syntax;
1447
          if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1448
            goto syntax;
1449
        }
1450
 
1451
      if (gfc_match_eos () != MATCH_YES)
1452
        goto syntax;
1453
    }
1454
 
1455
  if (gfc_pure (NULL))
1456
    {
1457
      gfc_error ("%s statement not allowed in PURE procedure at %C",
1458
                 gfc_ascii_statement (st));
1459
      goto cleanup;
1460
    }
1461
 
1462
  new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
1463
  new_st.expr = e;
1464
  new_st.ext.stop_code = stop_code;
1465
 
1466
  return MATCH_YES;
1467
 
1468
syntax:
1469
  gfc_syntax_error (st);
1470
 
1471
cleanup:
1472
 
1473
  gfc_free_expr (e);
1474
  return MATCH_ERROR;
1475
}
1476
 
1477
/* Match the (deprecated) PAUSE statement.  */
1478
 
1479
match
1480
gfc_match_pause (void)
1481
{
1482
  match m;
1483
 
1484
  m = gfc_match_stopcode (ST_PAUSE);
1485
  if (m == MATCH_YES)
1486
    {
1487
      if (gfc_notify_std (GFC_STD_F95_DEL,
1488
            "Obsolete: PAUSE statement at %C")
1489
          == FAILURE)
1490
        m = MATCH_ERROR;
1491
    }
1492
  return m;
1493
}
1494
 
1495
 
1496
/* Match the STOP statement.  */
1497
 
1498
match
1499
gfc_match_stop (void)
1500
{
1501
  return gfc_match_stopcode (ST_STOP);
1502
}
1503
 
1504
 
1505
/* Match a CONTINUE statement.  */
1506
 
1507
match
1508
gfc_match_continue (void)
1509
{
1510
 
1511
  if (gfc_match_eos () != MATCH_YES)
1512
    {
1513
      gfc_syntax_error (ST_CONTINUE);
1514
      return MATCH_ERROR;
1515
    }
1516
 
1517
  new_st.op = EXEC_CONTINUE;
1518
  return MATCH_YES;
1519
}
1520
 
1521
 
1522
/* Match the (deprecated) ASSIGN statement.  */
1523
 
1524
match
1525
gfc_match_assign (void)
1526
{
1527
  gfc_expr *expr;
1528
  gfc_st_label *label;
1529
 
1530
  if (gfc_match (" %l", &label) == MATCH_YES)
1531
    {
1532
      if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
1533
        return MATCH_ERROR;
1534
      if (gfc_match (" to %v%t", &expr) == MATCH_YES)
1535
        {
1536
          if (gfc_notify_std (GFC_STD_F95_DEL,
1537
                "Obsolete: ASSIGN statement at %C")
1538
              == FAILURE)
1539
            return MATCH_ERROR;
1540
 
1541
          expr->symtree->n.sym->attr.assign = 1;
1542
 
1543
          new_st.op = EXEC_LABEL_ASSIGN;
1544
          new_st.label = label;
1545
          new_st.expr = expr;
1546
          return MATCH_YES;
1547
        }
1548
    }
1549
  return MATCH_NO;
1550
}
1551
 
1552
 
1553
/* Match the GO TO statement.  As a computed GOTO statement is
1554
   matched, it is transformed into an equivalent SELECT block.  No
1555
   tree is necessary, and the resulting jumps-to-jumps are
1556
   specifically optimized away by the back end.  */
1557
 
1558
match
1559
gfc_match_goto (void)
1560
{
1561
  gfc_code *head, *tail;
1562
  gfc_expr *expr;
1563
  gfc_case *cp;
1564
  gfc_st_label *label;
1565
  int i;
1566
  match m;
1567
 
1568
  if (gfc_match (" %l%t", &label) == MATCH_YES)
1569
    {
1570
      if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1571
        return MATCH_ERROR;
1572
 
1573
      new_st.op = EXEC_GOTO;
1574
      new_st.label = label;
1575
      return MATCH_YES;
1576
    }
1577
 
1578
  /* The assigned GO TO statement.  */
1579
 
1580
  if (gfc_match_variable (&expr, 0) == MATCH_YES)
1581
    {
1582
      if (gfc_notify_std (GFC_STD_F95_DEL,
1583
                          "Obsolete: Assigned GOTO statement at %C")
1584
          == FAILURE)
1585
        return MATCH_ERROR;
1586
 
1587
      new_st.op = EXEC_GOTO;
1588
      new_st.expr = expr;
1589
 
1590
      if (gfc_match_eos () == MATCH_YES)
1591
        return MATCH_YES;
1592
 
1593
      /* Match label list.  */
1594
      gfc_match_char (',');
1595
      if (gfc_match_char ('(') != MATCH_YES)
1596
        {
1597
          gfc_syntax_error (ST_GOTO);
1598
          return MATCH_ERROR;
1599
        }
1600
      head = tail = NULL;
1601
 
1602
      do
1603
        {
1604
          m = gfc_match_st_label (&label);
1605
          if (m != MATCH_YES)
1606
            goto syntax;
1607
 
1608
          if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1609
            goto cleanup;
1610
 
1611
          if (head == NULL)
1612
            head = tail = gfc_get_code ();
1613
          else
1614
            {
1615
              tail->block = gfc_get_code ();
1616
              tail = tail->block;
1617
            }
1618
 
1619
          tail->label = label;
1620
          tail->op = EXEC_GOTO;
1621
        }
1622
      while (gfc_match_char (',') == MATCH_YES);
1623
 
1624
      if (gfc_match (")%t") != MATCH_YES)
1625
        goto syntax;
1626
 
1627
      if (head == NULL)
1628
        {
1629
           gfc_error (
1630
               "Statement label list in GOTO at %C cannot be empty");
1631
           goto syntax;
1632
        }
1633
      new_st.block = head;
1634
 
1635
      return MATCH_YES;
1636
    }
1637
 
1638
  /* Last chance is a computed GO TO statement.  */
1639
  if (gfc_match_char ('(') != MATCH_YES)
1640
    {
1641
      gfc_syntax_error (ST_GOTO);
1642
      return MATCH_ERROR;
1643
    }
1644
 
1645
  head = tail = NULL;
1646
  i = 1;
1647
 
1648
  do
1649
    {
1650
      m = gfc_match_st_label (&label);
1651
      if (m != MATCH_YES)
1652
        goto syntax;
1653
 
1654
      if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1655
        goto cleanup;
1656
 
1657
      if (head == NULL)
1658
        head = tail = gfc_get_code ();
1659
      else
1660
        {
1661
          tail->block = gfc_get_code ();
1662
          tail = tail->block;
1663
        }
1664
 
1665
      cp = gfc_get_case ();
1666
      cp->low = cp->high = gfc_int_expr (i++);
1667
 
1668
      tail->op = EXEC_SELECT;
1669
      tail->ext.case_list = cp;
1670
 
1671
      tail->next = gfc_get_code ();
1672
      tail->next->op = EXEC_GOTO;
1673
      tail->next->label = label;
1674
    }
1675
  while (gfc_match_char (',') == MATCH_YES);
1676
 
1677
  if (gfc_match_char (')') != MATCH_YES)
1678
    goto syntax;
1679
 
1680
  if (head == NULL)
1681
    {
1682
      gfc_error ("Statement label list in GOTO at %C cannot be empty");
1683
      goto syntax;
1684
    }
1685
 
1686
  /* Get the rest of the statement.  */
1687
  gfc_match_char (',');
1688
 
1689
  if (gfc_match (" %e%t", &expr) != MATCH_YES)
1690
    goto syntax;
1691
 
1692
  /* At this point, a computed GOTO has been fully matched and an
1693
     equivalent SELECT statement constructed.  */
1694
 
1695
  new_st.op = EXEC_SELECT;
1696
  new_st.expr = NULL;
1697
 
1698
  /* Hack: For a "real" SELECT, the expression is in expr. We put
1699
     it in expr2 so we can distinguish then and produce the correct
1700
     diagnostics.  */
1701
  new_st.expr2 = expr;
1702
  new_st.block = head;
1703
  return MATCH_YES;
1704
 
1705
syntax:
1706
  gfc_syntax_error (ST_GOTO);
1707
cleanup:
1708
  gfc_free_statements (head);
1709
  return MATCH_ERROR;
1710
}
1711
 
1712
 
1713
/* Frees a list of gfc_alloc structures.  */
1714
 
1715
void
1716
gfc_free_alloc_list (gfc_alloc * p)
1717
{
1718
  gfc_alloc *q;
1719
 
1720
  for (; p; p = q)
1721
    {
1722
      q = p->next;
1723
      gfc_free_expr (p->expr);
1724
      gfc_free (p);
1725
    }
1726
}
1727
 
1728
 
1729
/* Match an ALLOCATE statement.  */
1730
 
1731
match
1732
gfc_match_allocate (void)
1733
{
1734
  gfc_alloc *head, *tail;
1735
  gfc_expr *stat;
1736
  match m;
1737
 
1738
  head = tail = NULL;
1739
  stat = NULL;
1740
 
1741
  if (gfc_match_char ('(') != MATCH_YES)
1742
    goto syntax;
1743
 
1744
  for (;;)
1745
    {
1746
      if (head == NULL)
1747
        head = tail = gfc_get_alloc ();
1748
      else
1749
        {
1750
          tail->next = gfc_get_alloc ();
1751
          tail = tail->next;
1752
        }
1753
 
1754
      m = gfc_match_variable (&tail->expr, 0);
1755
      if (m == MATCH_NO)
1756
        goto syntax;
1757
      if (m == MATCH_ERROR)
1758
        goto cleanup;
1759
 
1760
      if (gfc_check_do_variable (tail->expr->symtree))
1761
        goto cleanup;
1762
 
1763
      if (gfc_pure (NULL)
1764
          && gfc_impure_variable (tail->expr->symtree->n.sym))
1765
        {
1766
          gfc_error ("Bad allocate-object in ALLOCATE statement at %C for a "
1767
                     "PURE procedure");
1768
          goto cleanup;
1769
        }
1770
 
1771
      if (gfc_match_char (',') != MATCH_YES)
1772
        break;
1773
 
1774
      m = gfc_match (" stat = %v", &stat);
1775
      if (m == MATCH_ERROR)
1776
        goto cleanup;
1777
      if (m == MATCH_YES)
1778
        break;
1779
    }
1780
 
1781
  if (stat != NULL)
1782
    {
1783
      if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1784
        {
1785
          gfc_error
1786
            ("STAT variable '%s' of ALLOCATE statement at %C cannot be "
1787
             "INTENT(IN)", stat->symtree->n.sym->name);
1788
          goto cleanup;
1789
        }
1790
 
1791
      if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
1792
        {
1793
          gfc_error
1794
            ("Illegal STAT variable in ALLOCATE statement at %C for a PURE "
1795
             "procedure");
1796
          goto cleanup;
1797
        }
1798
 
1799
      if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1800
        {
1801
          gfc_error("STAT expression at %C must be a variable");
1802
          goto cleanup;
1803
        }
1804
 
1805
      gfc_check_do_variable(stat->symtree);
1806
    }
1807
 
1808
  if (gfc_match (" )%t") != MATCH_YES)
1809
    goto syntax;
1810
 
1811
  new_st.op = EXEC_ALLOCATE;
1812
  new_st.expr = stat;
1813
  new_st.ext.alloc_list = head;
1814
 
1815
  return MATCH_YES;
1816
 
1817
syntax:
1818
  gfc_syntax_error (ST_ALLOCATE);
1819
 
1820
cleanup:
1821
  gfc_free_expr (stat);
1822
  gfc_free_alloc_list (head);
1823
  return MATCH_ERROR;
1824
}
1825
 
1826
 
1827
/* Match a NULLIFY statement. A NULLIFY statement is transformed into
1828
   a set of pointer assignments to intrinsic NULL().  */
1829
 
1830
match
1831
gfc_match_nullify (void)
1832
{
1833
  gfc_code *tail;
1834
  gfc_expr *e, *p;
1835
  match m;
1836
 
1837
  tail = NULL;
1838
 
1839
  if (gfc_match_char ('(') != MATCH_YES)
1840
    goto syntax;
1841
 
1842
  for (;;)
1843
    {
1844
      m = gfc_match_variable (&p, 0);
1845
      if (m == MATCH_ERROR)
1846
        goto cleanup;
1847
      if (m == MATCH_NO)
1848
        goto syntax;
1849
 
1850
      if (gfc_check_do_variable(p->symtree))
1851
        goto cleanup;
1852
 
1853
      if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
1854
        {
1855
          gfc_error
1856
            ("Illegal variable in NULLIFY at %C for a PURE procedure");
1857
          goto cleanup;
1858
        }
1859
 
1860
      /* build ' => NULL() ' */
1861
      e = gfc_get_expr ();
1862
      e->where = gfc_current_locus;
1863
      e->expr_type = EXPR_NULL;
1864
      e->ts.type = BT_UNKNOWN;
1865
 
1866
      /* Chain to list */
1867
      if (tail == NULL)
1868
        tail = &new_st;
1869
      else
1870
        {
1871
          tail->next = gfc_get_code ();
1872
          tail = tail->next;
1873
        }
1874
 
1875
      tail->op = EXEC_POINTER_ASSIGN;
1876
      tail->expr = p;
1877
      tail->expr2 = e;
1878
 
1879
      if (gfc_match (" )%t") == MATCH_YES)
1880
        break;
1881
      if (gfc_match_char (',') != MATCH_YES)
1882
        goto syntax;
1883
    }
1884
 
1885
  return MATCH_YES;
1886
 
1887
syntax:
1888
  gfc_syntax_error (ST_NULLIFY);
1889
 
1890
cleanup:
1891
  gfc_free_statements (new_st.next);
1892
  return MATCH_ERROR;
1893
}
1894
 
1895
 
1896
/* Match a DEALLOCATE statement.  */
1897
 
1898
match
1899
gfc_match_deallocate (void)
1900
{
1901
  gfc_alloc *head, *tail;
1902
  gfc_expr *stat;
1903
  match m;
1904
 
1905
  head = tail = NULL;
1906
  stat = NULL;
1907
 
1908
  if (gfc_match_char ('(') != MATCH_YES)
1909
    goto syntax;
1910
 
1911
  for (;;)
1912
    {
1913
      if (head == NULL)
1914
        head = tail = gfc_get_alloc ();
1915
      else
1916
        {
1917
          tail->next = gfc_get_alloc ();
1918
          tail = tail->next;
1919
        }
1920
 
1921
      m = gfc_match_variable (&tail->expr, 0);
1922
      if (m == MATCH_ERROR)
1923
        goto cleanup;
1924
      if (m == MATCH_NO)
1925
        goto syntax;
1926
 
1927
      if (gfc_check_do_variable (tail->expr->symtree))
1928
        goto cleanup;
1929
 
1930
      if (gfc_pure (NULL)
1931
          && gfc_impure_variable (tail->expr->symtree->n.sym))
1932
        {
1933
          gfc_error
1934
            ("Illegal deallocate-expression in DEALLOCATE at %C for a PURE "
1935
             "procedure");
1936
          goto cleanup;
1937
        }
1938
 
1939
      if (gfc_match_char (',') != MATCH_YES)
1940
        break;
1941
 
1942
      m = gfc_match (" stat = %v", &stat);
1943
      if (m == MATCH_ERROR)
1944
        goto cleanup;
1945
      if (m == MATCH_YES)
1946
        break;
1947
    }
1948
 
1949
  if (stat != NULL)
1950
    {
1951
      if (stat->symtree->n.sym->attr.intent == INTENT_IN)
1952
        {
1953
          gfc_error ("STAT variable '%s' of DEALLOCATE statement at %C "
1954
                     "cannot be INTENT(IN)", stat->symtree->n.sym->name);
1955
          goto cleanup;
1956
        }
1957
 
1958
      if (gfc_pure(NULL) && gfc_impure_variable (stat->symtree->n.sym))
1959
        {
1960
          gfc_error ("Illegal STAT variable in DEALLOCATE statement at %C "
1961
                     "for a PURE procedure");
1962
          goto cleanup;
1963
        }
1964
 
1965
      if (stat->symtree->n.sym->attr.flavor != FL_VARIABLE)
1966
        {
1967
          gfc_error("STAT expression at %C must be a variable");
1968
          goto cleanup;
1969
        }
1970
 
1971
      gfc_check_do_variable(stat->symtree);
1972
    }
1973
 
1974
  if (gfc_match (" )%t") != MATCH_YES)
1975
    goto syntax;
1976
 
1977
  new_st.op = EXEC_DEALLOCATE;
1978
  new_st.expr = stat;
1979
  new_st.ext.alloc_list = head;
1980
 
1981
  return MATCH_YES;
1982
 
1983
syntax:
1984
  gfc_syntax_error (ST_DEALLOCATE);
1985
 
1986
cleanup:
1987
  gfc_free_expr (stat);
1988
  gfc_free_alloc_list (head);
1989
  return MATCH_ERROR;
1990
}
1991
 
1992
 
1993
/* Match a RETURN statement.  */
1994
 
1995
match
1996
gfc_match_return (void)
1997
{
1998
  gfc_expr *e;
1999
  match m;
2000
  gfc_compile_state s;
2001
  int c;
2002
 
2003
  e = NULL;
2004
  if (gfc_match_eos () == MATCH_YES)
2005
    goto done;
2006
 
2007
  if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2008
    {
2009
      gfc_error ("Alternate RETURN statement at %C is only allowed within "
2010
                 "a SUBROUTINE");
2011
      goto cleanup;
2012
    }
2013
 
2014
  if (gfc_current_form == FORM_FREE)
2015
    {
2016
      /* The following are valid, so we can't require a blank after the
2017
        RETURN keyword:
2018
          return+1
2019
          return(1)  */
2020
      c = gfc_peek_char ();
2021
      if (ISALPHA (c) || ISDIGIT (c))
2022
       return MATCH_NO;
2023
    }
2024
 
2025
  m = gfc_match (" %e%t", &e);
2026
  if (m == MATCH_YES)
2027
    goto done;
2028
  if (m == MATCH_ERROR)
2029
    goto cleanup;
2030
 
2031
  gfc_syntax_error (ST_RETURN);
2032
 
2033
cleanup:
2034
  gfc_free_expr (e);
2035
  return MATCH_ERROR;
2036
 
2037
done:
2038
  gfc_enclosing_unit (&s);
2039
  if (s == COMP_PROGRAM
2040
      && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
2041
                        "main program at %C") == FAILURE)
2042
      return MATCH_ERROR;
2043
 
2044
  new_st.op = EXEC_RETURN;
2045
  new_st.expr = e;
2046
 
2047
  return MATCH_YES;
2048
}
2049
 
2050
 
2051
/* Match a CALL statement.  The tricky part here are possible
2052
   alternate return specifiers.  We handle these by having all
2053
   "subroutines" actually return an integer via a register that gives
2054
   the return number.  If the call specifies alternate returns, we
2055
   generate code for a SELECT statement whose case clauses contain
2056
   GOTOs to the various labels.  */
2057
 
2058
match
2059
gfc_match_call (void)
2060
{
2061
  char name[GFC_MAX_SYMBOL_LEN + 1];
2062
  gfc_actual_arglist *a, *arglist;
2063
  gfc_case *new_case;
2064
  gfc_symbol *sym;
2065
  gfc_symtree *st;
2066
  gfc_code *c;
2067
  match m;
2068
  int i;
2069
 
2070
  arglist = NULL;
2071
 
2072
  m = gfc_match ("% %n", name);
2073
  if (m == MATCH_NO)
2074
    goto syntax;
2075
  if (m != MATCH_YES)
2076
    return m;
2077
 
2078
  if (gfc_get_ha_sym_tree (name, &st))
2079
    return MATCH_ERROR;
2080
 
2081
  sym = st->n.sym;
2082
  gfc_set_sym_referenced (sym);
2083
 
2084
  if (!sym->attr.generic
2085
      && !sym->attr.subroutine
2086
      && gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2087
    return MATCH_ERROR;
2088
 
2089
  if (gfc_match_eos () != MATCH_YES)
2090
    {
2091
      m = gfc_match_actual_arglist (1, &arglist);
2092
      if (m == MATCH_NO)
2093
        goto syntax;
2094
      if (m == MATCH_ERROR)
2095
        goto cleanup;
2096
 
2097
      if (gfc_match_eos () != MATCH_YES)
2098
        goto syntax;
2099
    }
2100
 
2101
  /* If any alternate return labels were found, construct a SELECT
2102
     statement that will jump to the right place.  */
2103
 
2104
  i = 0;
2105
  for (a = arglist; a; a = a->next)
2106
    if (a->expr == NULL)
2107
        i = 1;
2108
 
2109
  if (i)
2110
    {
2111
      gfc_symtree *select_st;
2112
      gfc_symbol *select_sym;
2113
      char name[GFC_MAX_SYMBOL_LEN + 1];
2114
 
2115
      new_st.next = c = gfc_get_code ();
2116
      c->op = EXEC_SELECT;
2117
      sprintf (name, "_result_%s",sym->name);
2118
      gfc_get_ha_sym_tree (name, &select_st);  /* Can't fail */
2119
 
2120
      select_sym = select_st->n.sym;
2121
      select_sym->ts.type = BT_INTEGER;
2122
      select_sym->ts.kind = gfc_default_integer_kind;
2123
      gfc_set_sym_referenced (select_sym);
2124
      c->expr = gfc_get_expr ();
2125
      c->expr->expr_type = EXPR_VARIABLE;
2126
      c->expr->symtree = select_st;
2127
      c->expr->ts = select_sym->ts;
2128
      c->expr->where = gfc_current_locus;
2129
 
2130
      i = 0;
2131
      for (a = arglist; a; a = a->next)
2132
        {
2133
          if (a->expr != NULL)
2134
            continue;
2135
 
2136
          if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
2137
            continue;
2138
 
2139
          i++;
2140
 
2141
          c->block = gfc_get_code ();
2142
          c = c->block;
2143
          c->op = EXEC_SELECT;
2144
 
2145
          new_case = gfc_get_case ();
2146
          new_case->high = new_case->low = gfc_int_expr (i);
2147
          c->ext.case_list = new_case;
2148
 
2149
          c->next = gfc_get_code ();
2150
          c->next->op = EXEC_GOTO;
2151
          c->next->label = a->label;
2152
        }
2153
    }
2154
 
2155
  new_st.op = EXEC_CALL;
2156
  new_st.symtree = st;
2157
  new_st.ext.actual = arglist;
2158
 
2159
  return MATCH_YES;
2160
 
2161
syntax:
2162
  gfc_syntax_error (ST_CALL);
2163
 
2164
cleanup:
2165
  gfc_free_actual_arglist (arglist);
2166
  return MATCH_ERROR;
2167
}
2168
 
2169
 
2170
/* Given a name, return a pointer to the common head structure,
2171
   creating it if it does not exist. If FROM_MODULE is nonzero, we
2172
   mangle the name so that it doesn't interfere with commons defined
2173
   in the using namespace.
2174
   TODO: Add to global symbol tree.  */
2175
 
2176
gfc_common_head *
2177
gfc_get_common (const char *name, int from_module)
2178
{
2179
  gfc_symtree *st;
2180
  static int serial = 0;
2181
  char mangled_name[GFC_MAX_SYMBOL_LEN+1];
2182
 
2183
  if (from_module)
2184
    {
2185
      /* A use associated common block is only needed to correctly layout
2186
         the variables it contains.  */
2187
      snprintf(mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
2188
      st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
2189
    }
2190
  else
2191
    {
2192
      st = gfc_find_symtree (gfc_current_ns->common_root, name);
2193
 
2194
      if (st == NULL)
2195
        st = gfc_new_symtree (&gfc_current_ns->common_root, name);
2196
    }
2197
 
2198
  if (st->n.common == NULL)
2199
    {
2200
      st->n.common = gfc_get_common_head ();
2201
      st->n.common->where = gfc_current_locus;
2202
      strcpy (st->n.common->name, name);
2203
    }
2204
 
2205
  return st->n.common;
2206
}
2207
 
2208
 
2209
/* Match a common block name.  */
2210
 
2211
static match
2212
match_common_name (char *name)
2213
{
2214
  match m;
2215
 
2216
  if (gfc_match_char ('/') == MATCH_NO)
2217
    {
2218
      name[0] = '\0';
2219
      return MATCH_YES;
2220
    }
2221
 
2222
  if (gfc_match_char ('/') == MATCH_YES)
2223
    {
2224
      name[0] = '\0';
2225
      return MATCH_YES;
2226
    }
2227
 
2228
  m = gfc_match_name (name);
2229
 
2230
  if (m == MATCH_ERROR)
2231
    return MATCH_ERROR;
2232
  if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
2233
    return MATCH_YES;
2234
 
2235
  gfc_error ("Syntax error in common block name at %C");
2236
  return MATCH_ERROR;
2237
}
2238
 
2239
 
2240
/* Match a COMMON statement.  */
2241
 
2242
match
2243
gfc_match_common (void)
2244
{
2245
  gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
2246
  char name[GFC_MAX_SYMBOL_LEN+1];
2247
  gfc_common_head *t;
2248
  gfc_array_spec *as;
2249
  gfc_equiv * e1, * e2;
2250
  match m;
2251
  gfc_gsymbol *gsym;
2252
 
2253
  old_blank_common = gfc_current_ns->blank_common.head;
2254
  if (old_blank_common)
2255
    {
2256
      while (old_blank_common->common_next)
2257
        old_blank_common = old_blank_common->common_next;
2258
    }
2259
 
2260
  as = NULL;
2261
 
2262
  for (;;)
2263
    {
2264
      m = match_common_name (name);
2265
      if (m == MATCH_ERROR)
2266
        goto cleanup;
2267
 
2268
      gsym = gfc_get_gsymbol (name);
2269
      if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
2270
        {
2271
          gfc_error ("Symbol '%s' at %C is already an external symbol that is not COMMON",
2272
                     sym->name);
2273
          goto cleanup;
2274
        }
2275
 
2276
      if (gsym->type == GSYM_UNKNOWN)
2277
        {
2278
          gsym->type = GSYM_COMMON;
2279
          gsym->where = gfc_current_locus;
2280
          gsym->defined = 1;
2281
        }
2282
 
2283
      gsym->used = 1;
2284
 
2285
      if (name[0] == '\0')
2286
        {
2287
          t = &gfc_current_ns->blank_common;
2288
          if (t->head == NULL)
2289
            t->where = gfc_current_locus;
2290
          head = &t->head;
2291
        }
2292
      else
2293
        {
2294
          t = gfc_get_common (name, 0);
2295
          head = &t->head;
2296
        }
2297
 
2298
      if (*head == NULL)
2299
        tail = NULL;
2300
      else
2301
        {
2302
          tail = *head;
2303
          while (tail->common_next)
2304
            tail = tail->common_next;
2305
        }
2306
 
2307
      /* Grab the list of symbols.  */
2308
      for (;;)
2309
        {
2310
          m = gfc_match_symbol (&sym, 0);
2311
          if (m == MATCH_ERROR)
2312
            goto cleanup;
2313
          if (m == MATCH_NO)
2314
            goto syntax;
2315
 
2316
          if (sym->attr.in_common)
2317
            {
2318
              gfc_error ("Symbol '%s' at %C is already in a COMMON block",
2319
                         sym->name);
2320
              goto cleanup;
2321
            }
2322
 
2323
          if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2324
            goto cleanup;
2325
 
2326
          if (sym->value != NULL
2327
              && (name[0] == '\0' || !sym->attr.data))
2328
            {
2329
              if (name[0] == '\0')
2330
                gfc_error ("Previously initialized symbol '%s' in "
2331
                           "blank COMMON block at %C", sym->name);
2332
              else
2333
                gfc_error ("Previously initialized symbol '%s' in "
2334
                           "COMMON block '%s' at %C", sym->name, name);
2335
              goto cleanup;
2336
            }
2337
 
2338
          if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
2339
            goto cleanup;
2340
 
2341
          /* Derived type names must have the SEQUENCE attribute.  */
2342
          if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.sequence)
2343
            {
2344
              gfc_error
2345
                ("Derived type variable in COMMON at %C does not have the "
2346
                 "SEQUENCE attribute");
2347
              goto cleanup;
2348
            }
2349
 
2350
          if (tail != NULL)
2351
            tail->common_next = sym;
2352
          else
2353
            *head = sym;
2354
 
2355
          tail = sym;
2356
 
2357
          /* Deal with an optional array specification after the
2358
             symbol name.  */
2359
          m = gfc_match_array_spec (&as);
2360
          if (m == MATCH_ERROR)
2361
            goto cleanup;
2362
 
2363
          if (m == MATCH_YES)
2364
            {
2365
              if (as->type != AS_EXPLICIT)
2366
                {
2367
                  gfc_error
2368
                    ("Array specification for symbol '%s' in COMMON at %C "
2369
                     "must be explicit", sym->name);
2370
                  goto cleanup;
2371
                }
2372
 
2373
              if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
2374
                goto cleanup;
2375
 
2376
              if (sym->attr.pointer)
2377
                {
2378
                  gfc_error
2379
                    ("Symbol '%s' in COMMON at %C cannot be a POINTER array",
2380
                     sym->name);
2381
                  goto cleanup;
2382
                }
2383
 
2384
              sym->as = as;
2385
              as = NULL;
2386
 
2387
            }
2388
 
2389
          sym->common_head = t;
2390
 
2391
          /* Check to see if the symbol is already in an equivalence group.
2392
             If it is, set the other members as being in common.  */
2393
          if (sym->attr.in_equivalence)
2394
            {
2395
              for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
2396
                {
2397
                  for (e2 = e1; e2; e2 = e2->eq)
2398
                    if (e2->expr->symtree->n.sym == sym)
2399
                      goto equiv_found;
2400
 
2401
                  continue;
2402
 
2403
          equiv_found:
2404
 
2405
                  for (e2 = e1; e2; e2 = e2->eq)
2406
                    {
2407
                      other = e2->expr->symtree->n.sym;
2408
                      if (other->common_head
2409
                            && other->common_head != sym->common_head)
2410
                        {
2411
                          gfc_error ("Symbol '%s', in COMMON block '%s' at "
2412
                                     "%C is being indirectly equivalenced to "
2413
                                     "another COMMON block '%s'",
2414
                                     sym->name,
2415
                                     sym->common_head->name,
2416
                                     other->common_head->name);
2417
                            goto cleanup;
2418
                        }
2419
                      other->attr.in_common = 1;
2420
                      other->common_head = t;
2421
                    }
2422
                }
2423
            }
2424
 
2425
 
2426
          gfc_gobble_whitespace ();
2427
          if (gfc_match_eos () == MATCH_YES)
2428
            goto done;
2429
          if (gfc_peek_char () == '/')
2430
            break;
2431
          if (gfc_match_char (',') != MATCH_YES)
2432
            goto syntax;
2433
          gfc_gobble_whitespace ();
2434
          if (gfc_peek_char () == '/')
2435
            break;
2436
        }
2437
    }
2438
 
2439
done:
2440
  return MATCH_YES;
2441
 
2442
syntax:
2443
  gfc_syntax_error (ST_COMMON);
2444
 
2445
cleanup:
2446
  if (old_blank_common)
2447
    old_blank_common->common_next = NULL;
2448
  else
2449
    gfc_current_ns->blank_common.head = NULL;
2450
  gfc_free_array_spec (as);
2451
  return MATCH_ERROR;
2452
}
2453
 
2454
 
2455
/* Match a BLOCK DATA program unit.  */
2456
 
2457
match
2458
gfc_match_block_data (void)
2459
{
2460
  char name[GFC_MAX_SYMBOL_LEN + 1];
2461
  gfc_symbol *sym;
2462
  match m;
2463
 
2464
  if (gfc_match_eos () == MATCH_YES)
2465
    {
2466
      gfc_new_block = NULL;
2467
      return MATCH_YES;
2468
    }
2469
 
2470
  m = gfc_match ("% %n%t", name);
2471
  if (m != MATCH_YES)
2472
    return MATCH_ERROR;
2473
 
2474
  if (gfc_get_symbol (name, NULL, &sym))
2475
    return MATCH_ERROR;
2476
 
2477
  if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
2478
    return MATCH_ERROR;
2479
 
2480
  gfc_new_block = sym;
2481
 
2482
  return MATCH_YES;
2483
}
2484
 
2485
 
2486
/* Free a namelist structure.  */
2487
 
2488
void
2489
gfc_free_namelist (gfc_namelist * name)
2490
{
2491
  gfc_namelist *n;
2492
 
2493
  for (; name; name = n)
2494
    {
2495
      n = name->next;
2496
      gfc_free (name);
2497
    }
2498
}
2499
 
2500
 
2501
/* Match a NAMELIST statement.  */
2502
 
2503
match
2504
gfc_match_namelist (void)
2505
{
2506
  gfc_symbol *group_name, *sym;
2507
  gfc_namelist *nl;
2508
  match m, m2;
2509
 
2510
  m = gfc_match (" / %s /", &group_name);
2511
  if (m == MATCH_NO)
2512
    goto syntax;
2513
  if (m == MATCH_ERROR)
2514
    goto error;
2515
 
2516
  for (;;)
2517
    {
2518
      if (group_name->ts.type != BT_UNKNOWN)
2519
        {
2520
          gfc_error
2521
            ("Namelist group name '%s' at %C already has a basic type "
2522
             "of %s", group_name->name, gfc_typename (&group_name->ts));
2523
          return MATCH_ERROR;
2524
        }
2525
 
2526
      if (group_name->attr.flavor == FL_NAMELIST
2527
            && group_name->attr.use_assoc
2528
            && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
2529
                               "at %C already is USE associated and can"
2530
                               "not be respecified.", group_name->name)
2531
                 == FAILURE)
2532
        return MATCH_ERROR;
2533
 
2534
      if (group_name->attr.flavor != FL_NAMELIST
2535
          && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
2536
                             group_name->name, NULL) == FAILURE)
2537
        return MATCH_ERROR;
2538
 
2539
      for (;;)
2540
        {
2541
          m = gfc_match_symbol (&sym, 1);
2542
          if (m == MATCH_NO)
2543
            goto syntax;
2544
          if (m == MATCH_ERROR)
2545
            goto error;
2546
 
2547
          if (sym->attr.in_namelist == 0
2548
              && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
2549
            goto error;
2550
 
2551
          /* Use gfc_error_check here, rather than goto error, so that this
2552
             these are the only errors for the next two lines.  */
2553
          if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
2554
            {
2555
              gfc_error ("Assumed size array '%s' in namelist '%s'at "
2556
                         "%C is not allowed.", sym->name, group_name->name);
2557
              gfc_error_check ();
2558
            }
2559
 
2560
          if (sym->as && sym->as->type == AS_ASSUMED_SHAPE
2561
                && gfc_notify_std (GFC_STD_GNU, "Assumed shape array '%s' in "
2562
                                   "namelist '%s' at %C is an extension.",
2563
                                   sym->name, group_name->name) == FAILURE)
2564
            gfc_error_check ();
2565
 
2566
          nl = gfc_get_namelist ();
2567
          nl->sym = sym;
2568
          sym->refs++;
2569
 
2570
          if (group_name->namelist == NULL)
2571
            group_name->namelist = group_name->namelist_tail = nl;
2572
          else
2573
            {
2574
              group_name->namelist_tail->next = nl;
2575
              group_name->namelist_tail = nl;
2576
            }
2577
 
2578
          if (gfc_match_eos () == MATCH_YES)
2579
            goto done;
2580
 
2581
          m = gfc_match_char (',');
2582
 
2583
          if (gfc_match_char ('/') == MATCH_YES)
2584
            {
2585
              m2 = gfc_match (" %s /", &group_name);
2586
              if (m2 == MATCH_YES)
2587
                break;
2588
              if (m2 == MATCH_ERROR)
2589
                goto error;
2590
              goto syntax;
2591
            }
2592
 
2593
          if (m != MATCH_YES)
2594
            goto syntax;
2595
        }
2596
    }
2597
 
2598
done:
2599
  return MATCH_YES;
2600
 
2601
syntax:
2602
  gfc_syntax_error (ST_NAMELIST);
2603
 
2604
error:
2605
  return MATCH_ERROR;
2606
}
2607
 
2608
 
2609
/* Match a MODULE statement.  */
2610
 
2611
match
2612
gfc_match_module (void)
2613
{
2614
  match m;
2615
 
2616
  m = gfc_match (" %s%t", &gfc_new_block);
2617
  if (m != MATCH_YES)
2618
    return m;
2619
 
2620
  if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
2621
                      gfc_new_block->name, NULL) == FAILURE)
2622
    return MATCH_ERROR;
2623
 
2624
  return MATCH_YES;
2625
}
2626
 
2627
 
2628
/* Free equivalence sets and lists.  Recursively is the easiest way to
2629
   do this.  */
2630
 
2631
void
2632
gfc_free_equiv (gfc_equiv * eq)
2633
{
2634
 
2635
  if (eq == NULL)
2636
    return;
2637
 
2638
  gfc_free_equiv (eq->eq);
2639
  gfc_free_equiv (eq->next);
2640
 
2641
  gfc_free_expr (eq->expr);
2642
  gfc_free (eq);
2643
}
2644
 
2645
 
2646
/* Match an EQUIVALENCE statement.  */
2647
 
2648
match
2649
gfc_match_equivalence (void)
2650
{
2651
  gfc_equiv *eq, *set, *tail;
2652
  gfc_ref *ref;
2653
  gfc_symbol *sym;
2654
  match m;
2655
  gfc_common_head *common_head = NULL;
2656
  bool common_flag;
2657
  int cnt;
2658
 
2659
  tail = NULL;
2660
 
2661
  for (;;)
2662
    {
2663
      eq = gfc_get_equiv ();
2664
      if (tail == NULL)
2665
        tail = eq;
2666
 
2667
      eq->next = gfc_current_ns->equiv;
2668
      gfc_current_ns->equiv = eq;
2669
 
2670
      if (gfc_match_char ('(') != MATCH_YES)
2671
        goto syntax;
2672
 
2673
      set = eq;
2674
      common_flag = FALSE;
2675
      cnt = 0;
2676
 
2677
      for (;;)
2678
        {
2679
          m = gfc_match_equiv_variable (&set->expr);
2680
          if (m == MATCH_ERROR)
2681
            goto cleanup;
2682
          if (m == MATCH_NO)
2683
            goto syntax;
2684
 
2685
          /*  count the number of objects.  */
2686
          cnt++;
2687
 
2688
          if (gfc_match_char ('%') == MATCH_YES)
2689
            {
2690
              gfc_error ("Derived type component %C is not a "
2691
                         "permitted EQUIVALENCE member");
2692
              goto cleanup;
2693
            }
2694
 
2695
          for (ref = set->expr->ref; ref; ref = ref->next)
2696
            if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2697
              {
2698
                gfc_error
2699
                  ("Array reference in EQUIVALENCE at %C cannot be an "
2700
                   "array section");
2701
                goto cleanup;
2702
              }
2703
 
2704
          sym = set->expr->symtree->n.sym;
2705
 
2706
          if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL)
2707
                == FAILURE)
2708
            goto cleanup;
2709
 
2710
          if (sym->attr.in_common)
2711
            {
2712
              common_flag = TRUE;
2713
              common_head = sym->common_head;
2714
            }
2715
 
2716
          if (gfc_match_char (')') == MATCH_YES)
2717
            break;
2718
 
2719
          if (gfc_match_char (',') != MATCH_YES)
2720
            goto syntax;
2721
 
2722
          set->eq = gfc_get_equiv ();
2723
          set = set->eq;
2724
        }
2725
 
2726
      if (cnt < 2)
2727
        {
2728
          gfc_error ("EQUIVALENCE at %C requires two or more objects");
2729
          goto cleanup;
2730
        }
2731
 
2732
      /* If one of the members of an equivalence is in common, then
2733
         mark them all as being in common.  Before doing this, check
2734
         that members of the equivalence group are not in different
2735
         common blocks. */
2736
      if (common_flag)
2737
        for (set = eq; set; set = set->eq)
2738
          {
2739
            sym = set->expr->symtree->n.sym;
2740
            if (sym->common_head && sym->common_head != common_head)
2741
              {
2742
                gfc_error ("Attempt to indirectly overlap COMMON "
2743
                           "blocks %s and %s by EQUIVALENCE at %C",
2744
                           sym->common_head->name,
2745
                           common_head->name);
2746
                goto cleanup;
2747
              }
2748
            sym->attr.in_common = 1;
2749
            sym->common_head = common_head;
2750
          }
2751
 
2752
      if (gfc_match_eos () == MATCH_YES)
2753
        break;
2754
      if (gfc_match_char (',') != MATCH_YES)
2755
        goto syntax;
2756
    }
2757
 
2758
  return MATCH_YES;
2759
 
2760
syntax:
2761
  gfc_syntax_error (ST_EQUIVALENCE);
2762
 
2763
cleanup:
2764
  eq = tail->next;
2765
  tail->next = NULL;
2766
 
2767
  gfc_free_equiv (gfc_current_ns->equiv);
2768
  gfc_current_ns->equiv = eq;
2769
 
2770
  return MATCH_ERROR;
2771
}
2772
 
2773
/* Check that a statement function is not recursive. This is done by looking
2774
   for the statement function symbol(sym) by looking recursively through its
2775
   expression(e).  If a reference to sym is found, true is returned.  */
2776
static bool
2777
recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
2778
{
2779
  gfc_actual_arglist *arg;
2780
  gfc_ref *ref;
2781
  int i;
2782
 
2783
  if (e == NULL)
2784
    return false;
2785
 
2786
  switch (e->expr_type)
2787
    {
2788
    case EXPR_FUNCTION:
2789
      for (arg = e->value.function.actual; arg; arg = arg->next)
2790
        {
2791
          if (sym->name == arg->name
2792
                || recursive_stmt_fcn (arg->expr, sym))
2793
            return true;
2794
        }
2795
 
2796
      if (e->symtree == NULL)
2797
        return false;
2798
 
2799
      /* Check the name before testing for nested recursion!  */
2800
      if (sym->name == e->symtree->n.sym->name)
2801
        return true;
2802
 
2803
      /* Catch recursion via other statement functions.  */
2804
      if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
2805
            && e->symtree->n.sym->value
2806
            && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
2807
        return true;
2808
 
2809
      break;
2810
 
2811
    case EXPR_VARIABLE:
2812
      if (e->symtree && sym->name == e->symtree->n.sym->name)
2813
        return true;
2814
      break;
2815
 
2816
    case EXPR_OP:
2817
      if (recursive_stmt_fcn (e->value.op.op1, sym)
2818
            || recursive_stmt_fcn (e->value.op.op2, sym))
2819
        return true;
2820
      break;
2821
 
2822
    default:
2823
      break;
2824
    }
2825
 
2826
  /* Component references do not need to be checked.  */
2827
  if (e->ref)
2828
    {
2829
      for (ref = e->ref; ref; ref = ref->next)
2830
        {
2831
          switch (ref->type)
2832
            {
2833
            case REF_ARRAY:
2834
              for (i = 0; i < ref->u.ar.dimen; i++)
2835
                {
2836
                  if (recursive_stmt_fcn (ref->u.ar.start[i], sym)
2837
                        || recursive_stmt_fcn (ref->u.ar.end[i], sym)
2838
                        || recursive_stmt_fcn (ref->u.ar.stride[i], sym))
2839
                    return true;
2840
                }
2841
              break;
2842
 
2843
            case REF_SUBSTRING:
2844
              if (recursive_stmt_fcn (ref->u.ss.start, sym)
2845
                    || recursive_stmt_fcn (ref->u.ss.end, sym))
2846
                return true;
2847
 
2848
              break;
2849
 
2850
            default:
2851
              break;
2852
            }
2853
        }
2854
    }
2855
  return false;
2856
}
2857
 
2858
 
2859
/* Match a statement function declaration.  It is so easy to match
2860
   non-statement function statements with a MATCH_ERROR as opposed to
2861
   MATCH_NO that we suppress error message in most cases.  */
2862
 
2863
match
2864
gfc_match_st_function (void)
2865
{
2866
  gfc_error_buf old_error;
2867
  gfc_symbol *sym;
2868
  gfc_expr *expr;
2869
  match m;
2870
 
2871
  m = gfc_match_symbol (&sym, 0);
2872
  if (m != MATCH_YES)
2873
    return m;
2874
 
2875
  gfc_push_error (&old_error);
2876
 
2877
  if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
2878
                         sym->name, NULL) == FAILURE)
2879
    goto undo_error;
2880
 
2881
  if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
2882
    goto undo_error;
2883
 
2884
  m = gfc_match (" = %e%t", &expr);
2885
  if (m == MATCH_NO)
2886
    goto undo_error;
2887
 
2888
  gfc_free_error (&old_error);
2889
  if (m == MATCH_ERROR)
2890
    return m;
2891
 
2892
  if (recursive_stmt_fcn (expr, sym))
2893
    {
2894
      gfc_error ("Statement function at %L is recursive",
2895
                 &expr->where);
2896
      return MATCH_ERROR;
2897
    }
2898
 
2899
  sym->value = expr;
2900
 
2901
  return MATCH_YES;
2902
 
2903
undo_error:
2904
  gfc_pop_error (&old_error);
2905
  return MATCH_NO;
2906
}
2907
 
2908
 
2909
/***************** SELECT CASE subroutines ******************/
2910
 
2911
/* Free a single case structure.  */
2912
 
2913
static void
2914
free_case (gfc_case * p)
2915
{
2916
  if (p->low == p->high)
2917
    p->high = NULL;
2918
  gfc_free_expr (p->low);
2919
  gfc_free_expr (p->high);
2920
  gfc_free (p);
2921
}
2922
 
2923
 
2924
/* Free a list of case structures.  */
2925
 
2926
void
2927
gfc_free_case_list (gfc_case * p)
2928
{
2929
  gfc_case *q;
2930
 
2931
  for (; p; p = q)
2932
    {
2933
      q = p->next;
2934
      free_case (p);
2935
    }
2936
}
2937
 
2938
 
2939
/* Match a single case selector.  */
2940
 
2941
static match
2942
match_case_selector (gfc_case ** cp)
2943
{
2944
  gfc_case *c;
2945
  match m;
2946
 
2947
  c = gfc_get_case ();
2948
  c->where = gfc_current_locus;
2949
 
2950
  if (gfc_match_char (':') == MATCH_YES)
2951
    {
2952
      m = gfc_match_init_expr (&c->high);
2953
      if (m == MATCH_NO)
2954
        goto need_expr;
2955
      if (m == MATCH_ERROR)
2956
        goto cleanup;
2957
    }
2958
 
2959
  else
2960
    {
2961
      m = gfc_match_init_expr (&c->low);
2962
      if (m == MATCH_ERROR)
2963
        goto cleanup;
2964
      if (m == MATCH_NO)
2965
        goto need_expr;
2966
 
2967
      /* If we're not looking at a ':' now, make a range out of a single
2968
         target.  Else get the upper bound for the case range.  */
2969
      if (gfc_match_char (':') != MATCH_YES)
2970
        c->high = c->low;
2971
      else
2972
        {
2973
          m = gfc_match_init_expr (&c->high);
2974
          if (m == MATCH_ERROR)
2975
            goto cleanup;
2976
          /* MATCH_NO is fine.  It's OK if nothing is there!  */
2977
        }
2978
    }
2979
 
2980
  *cp = c;
2981
  return MATCH_YES;
2982
 
2983
need_expr:
2984
  gfc_error ("Expected initialization expression in CASE at %C");
2985
 
2986
cleanup:
2987
  free_case (c);
2988
  return MATCH_ERROR;
2989
}
2990
 
2991
 
2992
/* Match the end of a case statement.  */
2993
 
2994
static match
2995
match_case_eos (void)
2996
{
2997
  char name[GFC_MAX_SYMBOL_LEN + 1];
2998
  match m;
2999
 
3000
  if (gfc_match_eos () == MATCH_YES)
3001
    return MATCH_YES;
3002
 
3003
  gfc_gobble_whitespace ();
3004
 
3005
  m = gfc_match_name (name);
3006
  if (m != MATCH_YES)
3007
    return m;
3008
 
3009
  if (strcmp (name, gfc_current_block ()->name) != 0)
3010
    {
3011
      gfc_error ("Expected case name of '%s' at %C",
3012
                 gfc_current_block ()->name);
3013
      return MATCH_ERROR;
3014
    }
3015
 
3016
  return gfc_match_eos ();
3017
}
3018
 
3019
 
3020
/* Match a SELECT statement.  */
3021
 
3022
match
3023
gfc_match_select (void)
3024
{
3025
  gfc_expr *expr;
3026
  match m;
3027
 
3028
  m = gfc_match_label ();
3029
  if (m == MATCH_ERROR)
3030
    return m;
3031
 
3032
  m = gfc_match (" select case ( %e )%t", &expr);
3033
  if (m != MATCH_YES)
3034
    return m;
3035
 
3036
  new_st.op = EXEC_SELECT;
3037
  new_st.expr = expr;
3038
 
3039
  return MATCH_YES;
3040
}
3041
 
3042
 
3043
/* Match a CASE statement.  */
3044
 
3045
match
3046
gfc_match_case (void)
3047
{
3048
  gfc_case *c, *head, *tail;
3049
  match m;
3050
 
3051
  head = tail = NULL;
3052
 
3053
  if (gfc_current_state () != COMP_SELECT)
3054
    {
3055
      gfc_error ("Unexpected CASE statement at %C");
3056
      return MATCH_ERROR;
3057
    }
3058
 
3059
  if (gfc_match ("% default") == MATCH_YES)
3060
    {
3061
      m = match_case_eos ();
3062
      if (m == MATCH_NO)
3063
        goto syntax;
3064
      if (m == MATCH_ERROR)
3065
        goto cleanup;
3066
 
3067
      new_st.op = EXEC_SELECT;
3068
      c = gfc_get_case ();
3069
      c->where = gfc_current_locus;
3070
      new_st.ext.case_list = c;
3071
      return MATCH_YES;
3072
    }
3073
 
3074
  if (gfc_match_char ('(') != MATCH_YES)
3075
    goto syntax;
3076
 
3077
  for (;;)
3078
    {
3079
      if (match_case_selector (&c) == MATCH_ERROR)
3080
        goto cleanup;
3081
 
3082
      if (head == NULL)
3083
        head = c;
3084
      else
3085
        tail->next = c;
3086
 
3087
      tail = c;
3088
 
3089
      if (gfc_match_char (')') == MATCH_YES)
3090
        break;
3091
      if (gfc_match_char (',') != MATCH_YES)
3092
        goto syntax;
3093
    }
3094
 
3095
  m = match_case_eos ();
3096
  if (m == MATCH_NO)
3097
    goto syntax;
3098
  if (m == MATCH_ERROR)
3099
    goto cleanup;
3100
 
3101
  new_st.op = EXEC_SELECT;
3102
  new_st.ext.case_list = head;
3103
 
3104
  return MATCH_YES;
3105
 
3106
syntax:
3107
  gfc_error ("Syntax error in CASE-specification at %C");
3108
 
3109
cleanup:
3110
  gfc_free_case_list (head);  /* new_st is cleaned up in parse.c.  */
3111
  return MATCH_ERROR;
3112
}
3113
 
3114
/********************* WHERE subroutines ********************/
3115
 
3116
/* Match the rest of a simple WHERE statement that follows an IF statement.
3117
 */
3118
 
3119
static match
3120
match_simple_where (void)
3121
{
3122
  gfc_expr *expr;
3123
  gfc_code *c;
3124
  match m;
3125
 
3126
  m = gfc_match (" ( %e )", &expr);
3127
  if (m != MATCH_YES)
3128
    return m;
3129
 
3130
  m = gfc_match_assignment ();
3131
  if (m == MATCH_NO)
3132
    goto syntax;
3133
  if (m == MATCH_ERROR)
3134
    goto cleanup;
3135
 
3136
  if (gfc_match_eos () != MATCH_YES)
3137
    goto syntax;
3138
 
3139
  c = gfc_get_code ();
3140
 
3141
  c->op = EXEC_WHERE;
3142
  c->expr = expr;
3143
  c->next = gfc_get_code ();
3144
 
3145
  *c->next = new_st;
3146
  gfc_clear_new_st ();
3147
 
3148
  new_st.op = EXEC_WHERE;
3149
  new_st.block = c;
3150
 
3151
  return MATCH_YES;
3152
 
3153
syntax:
3154
  gfc_syntax_error (ST_WHERE);
3155
 
3156
cleanup:
3157
  gfc_free_expr (expr);
3158
  return MATCH_ERROR;
3159
}
3160
 
3161
/* Match a WHERE statement.  */
3162
 
3163
match
3164
gfc_match_where (gfc_statement * st)
3165
{
3166
  gfc_expr *expr;
3167
  match m0, m;
3168
  gfc_code *c;
3169
 
3170
  m0 = gfc_match_label ();
3171
  if (m0 == MATCH_ERROR)
3172
    return m0;
3173
 
3174
  m = gfc_match (" where ( %e )", &expr);
3175
  if (m != MATCH_YES)
3176
    return m;
3177
 
3178
  if (gfc_match_eos () == MATCH_YES)
3179
    {
3180
      *st = ST_WHERE_BLOCK;
3181
 
3182
      new_st.op = EXEC_WHERE;
3183
      new_st.expr = expr;
3184
      return MATCH_YES;
3185
    }
3186
 
3187
  m = gfc_match_assignment ();
3188
  if (m == MATCH_NO)
3189
    gfc_syntax_error (ST_WHERE);
3190
 
3191
  if (m != MATCH_YES)
3192
    {
3193
      gfc_free_expr (expr);
3194
      return MATCH_ERROR;
3195
    }
3196
 
3197
  /* We've got a simple WHERE statement.  */
3198
  *st = ST_WHERE;
3199
  c = gfc_get_code ();
3200
 
3201
  c->op = EXEC_WHERE;
3202
  c->expr = expr;
3203
  c->next = gfc_get_code ();
3204
 
3205
  *c->next = new_st;
3206
  gfc_clear_new_st ();
3207
 
3208
  new_st.op = EXEC_WHERE;
3209
  new_st.block = c;
3210
 
3211
  return MATCH_YES;
3212
}
3213
 
3214
 
3215
/* Match an ELSEWHERE statement.  We leave behind a WHERE node in
3216
   new_st if successful.  */
3217
 
3218
match
3219
gfc_match_elsewhere (void)
3220
{
3221
  char name[GFC_MAX_SYMBOL_LEN + 1];
3222
  gfc_expr *expr;
3223
  match m;
3224
 
3225
  if (gfc_current_state () != COMP_WHERE)
3226
    {
3227
      gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
3228
      return MATCH_ERROR;
3229
    }
3230
 
3231
  expr = NULL;
3232
 
3233
  if (gfc_match_char ('(') == MATCH_YES)
3234
    {
3235
      m = gfc_match_expr (&expr);
3236
      if (m == MATCH_NO)
3237
        goto syntax;
3238
      if (m == MATCH_ERROR)
3239
        return MATCH_ERROR;
3240
 
3241
      if (gfc_match_char (')') != MATCH_YES)
3242
        goto syntax;
3243
    }
3244
 
3245
  if (gfc_match_eos () != MATCH_YES)
3246
    {                           /* Better be a name at this point */
3247
      m = gfc_match_name (name);
3248
      if (m == MATCH_NO)
3249
        goto syntax;
3250
      if (m == MATCH_ERROR)
3251
        goto cleanup;
3252
 
3253
      if (gfc_match_eos () != MATCH_YES)
3254
        goto syntax;
3255
 
3256
      if (strcmp (name, gfc_current_block ()->name) != 0)
3257
        {
3258
          gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
3259
                     name, gfc_current_block ()->name);
3260
          goto cleanup;
3261
        }
3262
    }
3263
 
3264
  new_st.op = EXEC_WHERE;
3265
  new_st.expr = expr;
3266
  return MATCH_YES;
3267
 
3268
syntax:
3269
  gfc_syntax_error (ST_ELSEWHERE);
3270
 
3271
cleanup:
3272
  gfc_free_expr (expr);
3273
  return MATCH_ERROR;
3274
}
3275
 
3276
 
3277
/******************** FORALL subroutines ********************/
3278
 
3279
/* Free a list of FORALL iterators.  */
3280
 
3281
void
3282
gfc_free_forall_iterator (gfc_forall_iterator * iter)
3283
{
3284
  gfc_forall_iterator *next;
3285
 
3286
  while (iter)
3287
    {
3288
      next = iter->next;
3289
 
3290
      gfc_free_expr (iter->var);
3291
      gfc_free_expr (iter->start);
3292
      gfc_free_expr (iter->end);
3293
      gfc_free_expr (iter->stride);
3294
 
3295
      gfc_free (iter);
3296
      iter = next;
3297
    }
3298
}
3299
 
3300
 
3301
/* Match an iterator as part of a FORALL statement.  The format is:
3302
 
3303
     <var> = <start>:<end>[:<stride>][, <scalar mask>]  */
3304
 
3305
static match
3306
match_forall_iterator (gfc_forall_iterator ** result)
3307
{
3308
  gfc_forall_iterator *iter;
3309
  locus where;
3310
  match m;
3311
 
3312
  where = gfc_current_locus;
3313
  iter = gfc_getmem (sizeof (gfc_forall_iterator));
3314
 
3315
  m = gfc_match_variable (&iter->var, 0);
3316
  if (m != MATCH_YES)
3317
    goto cleanup;
3318
 
3319
  if (gfc_match_char ('=') != MATCH_YES)
3320
    {
3321
      m = MATCH_NO;
3322
      goto cleanup;
3323
    }
3324
 
3325
  m = gfc_match_expr (&iter->start);
3326
  if (m != MATCH_YES)
3327
    goto cleanup;
3328
 
3329
  if (gfc_match_char (':') != MATCH_YES)
3330
    goto syntax;
3331
 
3332
  m = gfc_match_expr (&iter->end);
3333
  if (m == MATCH_NO)
3334
    goto syntax;
3335
  if (m == MATCH_ERROR)
3336
    goto cleanup;
3337
 
3338
  if (gfc_match_char (':') == MATCH_NO)
3339
    iter->stride = gfc_int_expr (1);
3340
  else
3341
    {
3342
      m = gfc_match_expr (&iter->stride);
3343
      if (m == MATCH_NO)
3344
        goto syntax;
3345
      if (m == MATCH_ERROR)
3346
        goto cleanup;
3347
    }
3348
 
3349
  *result = iter;
3350
  return MATCH_YES;
3351
 
3352
syntax:
3353
  gfc_error ("Syntax error in FORALL iterator at %C");
3354
  m = MATCH_ERROR;
3355
 
3356
cleanup:
3357
  gfc_current_locus = where;
3358
  gfc_free_forall_iterator (iter);
3359
  return m;
3360
}
3361
 
3362
 
3363
/* Match the header of a FORALL statement.  */
3364
 
3365
static match
3366
match_forall_header (gfc_forall_iterator ** phead, gfc_expr ** mask)
3367
{
3368
  gfc_forall_iterator *head, *tail, *new;
3369
  gfc_expr *msk;
3370
  match m;
3371
 
3372
  gfc_gobble_whitespace ();
3373
 
3374
  head = tail = NULL;
3375
  msk = NULL;
3376
 
3377
  if (gfc_match_char ('(') != MATCH_YES)
3378
    return MATCH_NO;
3379
 
3380
  m = match_forall_iterator (&new);
3381
  if (m == MATCH_ERROR)
3382
    goto cleanup;
3383
  if (m == MATCH_NO)
3384
    goto syntax;
3385
 
3386
  head = tail = new;
3387
 
3388
  for (;;)
3389
    {
3390
      if (gfc_match_char (',') != MATCH_YES)
3391
        break;
3392
 
3393
      m = match_forall_iterator (&new);
3394
      if (m == MATCH_ERROR)
3395
        goto cleanup;
3396
 
3397
      if (m == MATCH_YES)
3398
        {
3399
          tail->next = new;
3400
          tail = new;
3401
          continue;
3402
        }
3403
 
3404
      /* Have to have a mask expression */
3405
 
3406
      m = gfc_match_expr (&msk);
3407
      if (m == MATCH_NO)
3408
        goto syntax;
3409
      if (m == MATCH_ERROR)
3410
        goto cleanup;
3411
 
3412
      break;
3413
    }
3414
 
3415
  if (gfc_match_char (')') == MATCH_NO)
3416
    goto syntax;
3417
 
3418
  *phead = head;
3419
  *mask = msk;
3420
  return MATCH_YES;
3421
 
3422
syntax:
3423
  gfc_syntax_error (ST_FORALL);
3424
 
3425
cleanup:
3426
  gfc_free_expr (msk);
3427
  gfc_free_forall_iterator (head);
3428
 
3429
  return MATCH_ERROR;
3430
}
3431
 
3432
/* Match the rest of a simple FORALL statement that follows an IF statement.
3433
 */
3434
 
3435
static match
3436
match_simple_forall (void)
3437
{
3438
  gfc_forall_iterator *head;
3439
  gfc_expr *mask;
3440
  gfc_code *c;
3441
  match m;
3442
 
3443
  mask = NULL;
3444
  head = NULL;
3445
  c = NULL;
3446
 
3447
  m = match_forall_header (&head, &mask);
3448
 
3449
  if (m == MATCH_NO)
3450
    goto syntax;
3451
  if (m != MATCH_YES)
3452
    goto cleanup;
3453
 
3454
  m = gfc_match_assignment ();
3455
 
3456
  if (m == MATCH_ERROR)
3457
    goto cleanup;
3458
  if (m == MATCH_NO)
3459
    {
3460
      m = gfc_match_pointer_assignment ();
3461
      if (m == MATCH_ERROR)
3462
        goto cleanup;
3463
      if (m == MATCH_NO)
3464
        goto syntax;
3465
    }
3466
 
3467
  c = gfc_get_code ();
3468
  *c = new_st;
3469
  c->loc = gfc_current_locus;
3470
 
3471
  if (gfc_match_eos () != MATCH_YES)
3472
    goto syntax;
3473
 
3474
  gfc_clear_new_st ();
3475
  new_st.op = EXEC_FORALL;
3476
  new_st.expr = mask;
3477
  new_st.ext.forall_iterator = head;
3478
  new_st.block = gfc_get_code ();
3479
 
3480
  new_st.block->op = EXEC_FORALL;
3481
  new_st.block->next = c;
3482
 
3483
  return MATCH_YES;
3484
 
3485
syntax:
3486
  gfc_syntax_error (ST_FORALL);
3487
 
3488
cleanup:
3489
  gfc_free_forall_iterator (head);
3490
  gfc_free_expr (mask);
3491
 
3492
  return MATCH_ERROR;
3493
}
3494
 
3495
 
3496
/* Match a FORALL statement.  */
3497
 
3498
match
3499
gfc_match_forall (gfc_statement * st)
3500
{
3501
  gfc_forall_iterator *head;
3502
  gfc_expr *mask;
3503
  gfc_code *c;
3504
  match m0, m;
3505
 
3506
  head = NULL;
3507
  mask = NULL;
3508
  c = NULL;
3509
 
3510
  m0 = gfc_match_label ();
3511
  if (m0 == MATCH_ERROR)
3512
    return MATCH_ERROR;
3513
 
3514
  m = gfc_match (" forall");
3515
  if (m != MATCH_YES)
3516
    return m;
3517
 
3518
  m = match_forall_header (&head, &mask);
3519
  if (m == MATCH_ERROR)
3520
    goto cleanup;
3521
  if (m == MATCH_NO)
3522
    goto syntax;
3523
 
3524
  if (gfc_match_eos () == MATCH_YES)
3525
    {
3526
      *st = ST_FORALL_BLOCK;
3527
 
3528
      new_st.op = EXEC_FORALL;
3529
      new_st.expr = mask;
3530
      new_st.ext.forall_iterator = head;
3531
 
3532
      return MATCH_YES;
3533
    }
3534
 
3535
  m = gfc_match_assignment ();
3536
  if (m == MATCH_ERROR)
3537
    goto cleanup;
3538
  if (m == MATCH_NO)
3539
    {
3540
      m = gfc_match_pointer_assignment ();
3541
      if (m == MATCH_ERROR)
3542
        goto cleanup;
3543
      if (m == MATCH_NO)
3544
        goto syntax;
3545
    }
3546
 
3547
  c = gfc_get_code ();
3548
  *c = new_st;
3549
 
3550
  if (gfc_match_eos () != MATCH_YES)
3551
    goto syntax;
3552
 
3553
  gfc_clear_new_st ();
3554
  new_st.op = EXEC_FORALL;
3555
  new_st.expr = mask;
3556
  new_st.ext.forall_iterator = head;
3557
  new_st.block = gfc_get_code ();
3558
 
3559
  new_st.block->op = EXEC_FORALL;
3560
  new_st.block->next = c;
3561
 
3562
  *st = ST_FORALL;
3563
  return MATCH_YES;
3564
 
3565
syntax:
3566
  gfc_syntax_error (ST_FORALL);
3567
 
3568
cleanup:
3569
  gfc_free_forall_iterator (head);
3570
  gfc_free_expr (mask);
3571
  gfc_free_statements (c);
3572
  return MATCH_NO;
3573
}

powered by: WebSVN 2.1.0

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