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