]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/match.c
missing changelog
[gcc.git] / gcc / fortran / match.c
CommitLineData
6de9cd9a 1/* Matching subroutines in all sizes, shapes and colors.
835aac92 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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
d234d788 10Software Foundation; either version 3, or (at your option) any later
9fc4d79b 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
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a 21
6de9cd9a
DN
22#include "config.h"
23#include "system.h"
24#include "flags.h"
6de9cd9a
DN
25#include "gfortran.h"
26#include "match.h"
27#include "parse.h"
28
8fb74da4 29int gfc_matching_procptr_assignment = 0;
3df684e2 30bool gfc_matching_prefix = false;
6de9cd9a 31
7431bf06
JW
32/* Stack of SELECT TYPE statements. */
33gfc_select_type_stack *select_type_stack = NULL;
cf2b3c22 34
ba3ba492
RS
35/* For debugging and diagnostic purposes. Return the textual representation
36 of the intrinsic operator OP. */
37const char *
38gfc_op2string (gfc_intrinsic_op op)
39{
40 switch (op)
41 {
42 case INTRINSIC_UPLUS:
43 case INTRINSIC_PLUS:
44 return "+";
45
46 case INTRINSIC_UMINUS:
47 case INTRINSIC_MINUS:
48 return "-";
49
50 case INTRINSIC_POWER:
51 return "**";
52 case INTRINSIC_CONCAT:
53 return "//";
54 case INTRINSIC_TIMES:
55 return "*";
56 case INTRINSIC_DIVIDE:
57 return "/";
58
59 case INTRINSIC_AND:
60 return ".and.";
61 case INTRINSIC_OR:
62 return ".or.";
63 case INTRINSIC_EQV:
64 return ".eqv.";
65 case INTRINSIC_NEQV:
66 return ".neqv.";
67
68 case INTRINSIC_EQ_OS:
69 return ".eq.";
70 case INTRINSIC_EQ:
71 return "==";
72 case INTRINSIC_NE_OS:
73 return ".ne.";
74 case INTRINSIC_NE:
75 return "/=";
76 case INTRINSIC_GE_OS:
77 return ".ge.";
78 case INTRINSIC_GE:
79 return ">=";
80 case INTRINSIC_LE_OS:
81 return ".le.";
82 case INTRINSIC_LE:
83 return "<=";
84 case INTRINSIC_LT_OS:
85 return ".lt.";
86 case INTRINSIC_LT:
87 return "<";
88 case INTRINSIC_GT_OS:
89 return ".gt.";
90 case INTRINSIC_GT:
91 return ">";
92 case INTRINSIC_NOT:
93 return ".not.";
94
95 case INTRINSIC_ASSIGN:
96 return "=";
97
98 case INTRINSIC_PARENTHESES:
99 return "parens";
100
101 default:
102 break;
103 }
104
105 gfc_internal_error ("gfc_op2string(): Bad code");
106 /* Not reached. */
107}
108
6de9cd9a
DN
109
110/******************** Generic matching subroutines ************************/
111
f9b9fb82
JD
112/* This function scans the current statement counting the opened and closed
113 parenthesis to make sure they are balanced. */
114
115match
116gfc_match_parens (void)
117{
118 locus old_loc, where;
8fc541d3
FXC
119 int count, instring;
120 gfc_char_t c, quote;
f9b9fb82
JD
121
122 old_loc = gfc_current_locus;
123 count = 0;
124 instring = 0;
125 quote = ' ';
126
127 for (;;)
128 {
129 c = gfc_next_char_literal (instring);
130 if (c == '\n')
131 break;
132 if (quote == ' ' && ((c == '\'') || (c == '"')))
133 {
8fc541d3 134 quote = c;
f9b9fb82
JD
135 instring = 1;
136 continue;
137 }
138 if (quote != ' ' && c == quote)
139 {
140 quote = ' ';
141 instring = 0;
142 continue;
143 }
144
145 if (c == '(' && quote == ' ')
146 {
147 count++;
148 where = gfc_current_locus;
149 }
150 if (c == ')' && quote == ' ')
151 {
152 count--;
153 where = gfc_current_locus;
154 }
155 }
156
157 gfc_current_locus = old_loc;
158
159 if (count > 0)
160 {
acb388a0 161 gfc_error ("Missing ')' in statement at or before %L", &where);
f9b9fb82
JD
162 return MATCH_ERROR;
163 }
164 if (count < 0)
165 {
acb388a0 166 gfc_error ("Missing '(' in statement at or before %L", &where);
f9b9fb82
JD
167 return MATCH_ERROR;
168 }
169
170 return MATCH_YES;
171}
172
173
a88a266c
SK
174/* See if the next character is a special character that has
175 escaped by a \ via the -fbackslash option. */
176
177match
8fc541d3 178gfc_match_special_char (gfc_char_t *res)
a88a266c 179{
8fc541d3
FXC
180 int len, i;
181 gfc_char_t c, n;
a88a266c
SK
182 match m;
183
184 m = MATCH_YES;
185
8fc541d3 186 switch ((c = gfc_next_char_literal (1)))
a88a266c
SK
187 {
188 case 'a':
8fc541d3 189 *res = '\a';
a88a266c
SK
190 break;
191 case 'b':
8fc541d3 192 *res = '\b';
a88a266c
SK
193 break;
194 case 't':
8fc541d3 195 *res = '\t';
a88a266c
SK
196 break;
197 case 'f':
8fc541d3 198 *res = '\f';
a88a266c
SK
199 break;
200 case 'n':
8fc541d3 201 *res = '\n';
a88a266c
SK
202 break;
203 case 'r':
8fc541d3 204 *res = '\r';
a88a266c
SK
205 break;
206 case 'v':
8fc541d3 207 *res = '\v';
a88a266c
SK
208 break;
209 case '\\':
8fc541d3 210 *res = '\\';
a88a266c
SK
211 break;
212 case '0':
8fc541d3
FXC
213 *res = '\0';
214 break;
215
216 case 'x':
217 case 'u':
218 case 'U':
219 /* Hexadecimal form of wide characters. */
220 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
221 n = 0;
222 for (i = 0; i < len; i++)
223 {
224 char buf[2] = { '\0', '\0' };
225
226 c = gfc_next_char_literal (1);
227 if (!gfc_wide_fits_in_byte (c)
228 || !gfc_check_digit ((unsigned char) c, 16))
229 return MATCH_NO;
230
231 buf[0] = (unsigned char) c;
232 n = n << 4;
233 n += strtol (buf, NULL, 16);
234 }
235 *res = n;
a88a266c 236 break;
8fc541d3 237
a88a266c
SK
238 default:
239 /* Unknown backslash codes are simply not expanded. */
240 m = MATCH_NO;
241 break;
242 }
243
244 return m;
245}
246
247
6de9cd9a
DN
248/* In free form, match at least one space. Always matches in fixed
249 form. */
250
251match
252gfc_match_space (void)
253{
254 locus old_loc;
8fc541d3 255 char c;
6de9cd9a 256
d4fa05b9 257 if (gfc_current_form == FORM_FIXED)
6de9cd9a
DN
258 return MATCH_YES;
259
63645982 260 old_loc = gfc_current_locus;
6de9cd9a 261
8fc541d3 262 c = gfc_next_ascii_char ();
6de9cd9a
DN
263 if (!gfc_is_whitespace (c))
264 {
63645982 265 gfc_current_locus = old_loc;
6de9cd9a
DN
266 return MATCH_NO;
267 }
268
269 gfc_gobble_whitespace ();
270
271 return MATCH_YES;
272}
273
274
275/* Match an end of statement. End of statement is optional
276 whitespace, followed by a ';' or '\n' or comment '!'. If a
277 semicolon is found, we continue to eat whitespace and semicolons. */
278
279match
280gfc_match_eos (void)
281{
282 locus old_loc;
8fc541d3
FXC
283 int flag;
284 char c;
6de9cd9a
DN
285
286 flag = 0;
287
288 for (;;)
289 {
63645982 290 old_loc = gfc_current_locus;
6de9cd9a
DN
291 gfc_gobble_whitespace ();
292
8fc541d3 293 c = gfc_next_ascii_char ();
6de9cd9a
DN
294 switch (c)
295 {
296 case '!':
297 do
298 {
8fc541d3 299 c = gfc_next_ascii_char ();
6de9cd9a
DN
300 }
301 while (c != '\n');
302
66e4ab31 303 /* Fall through. */
6de9cd9a
DN
304
305 case '\n':
306 return MATCH_YES;
307
308 case ';':
309 flag = 1;
310 continue;
311 }
312
313 break;
314 }
315
63645982 316 gfc_current_locus = old_loc;
6de9cd9a
DN
317 return (flag) ? MATCH_YES : MATCH_NO;
318}
319
320
321/* Match a literal integer on the input, setting the value on
322 MATCH_YES. Literal ints occur in kind-parameters as well as
5cf54585
TS
323 old-style character length specifications. If cnt is non-NULL it
324 will be set to the number of digits. */
6de9cd9a
DN
325
326match
8a8f7eca 327gfc_match_small_literal_int (int *value, int *cnt)
6de9cd9a
DN
328{
329 locus old_loc;
330 char c;
8a8f7eca 331 int i, j;
6de9cd9a 332
63645982 333 old_loc = gfc_current_locus;
6de9cd9a 334
8fc541d3 335 *value = -1;
6de9cd9a 336 gfc_gobble_whitespace ();
8fc541d3 337 c = gfc_next_ascii_char ();
5cf54585
TS
338 if (cnt)
339 *cnt = 0;
6de9cd9a
DN
340
341 if (!ISDIGIT (c))
342 {
63645982 343 gfc_current_locus = old_loc;
6de9cd9a
DN
344 return MATCH_NO;
345 }
346
347 i = c - '0';
8a8f7eca 348 j = 1;
6de9cd9a
DN
349
350 for (;;)
351 {
63645982 352 old_loc = gfc_current_locus;
8fc541d3 353 c = gfc_next_ascii_char ();
6de9cd9a
DN
354
355 if (!ISDIGIT (c))
356 break;
357
358 i = 10 * i + c - '0';
8a8f7eca 359 j++;
6de9cd9a
DN
360
361 if (i > 99999999)
362 {
363 gfc_error ("Integer too large at %C");
364 return MATCH_ERROR;
365 }
366 }
367
63645982 368 gfc_current_locus = old_loc;
6de9cd9a
DN
369
370 *value = i;
5cf54585
TS
371 if (cnt)
372 *cnt = j;
6de9cd9a
DN
373 return MATCH_YES;
374}
375
376
377/* Match a small, constant integer expression, like in a kind
378 statement. On MATCH_YES, 'value' is set. */
379
380match
381gfc_match_small_int (int *value)
382{
383 gfc_expr *expr;
384 const char *p;
385 match m;
386 int i;
387
388 m = gfc_match_expr (&expr);
389 if (m != MATCH_YES)
390 return m;
391
392 p = gfc_extract_int (expr, &i);
393 gfc_free_expr (expr);
394
395 if (p != NULL)
396 {
397 gfc_error (p);
398 m = MATCH_ERROR;
399 }
400
401 *value = i;
402 return m;
403}
404
405
a8b3b0b6
CR
406/* This function is the same as the gfc_match_small_int, except that
407 we're keeping the pointer to the expr. This function could just be
408 removed and the previously mentioned one modified, though all calls
409 to it would have to be modified then (and there were a number of
410 them). Return MATCH_ERROR if fail to extract the int; otherwise,
411 return the result of gfc_match_expr(). The expr (if any) that was
412 matched is returned in the parameter expr. */
413
414match
415gfc_match_small_int_expr (int *value, gfc_expr **expr)
416{
417 const char *p;
418 match m;
419 int i;
420
421 m = gfc_match_expr (expr);
422 if (m != MATCH_YES)
423 return m;
424
425 p = gfc_extract_int (*expr, &i);
426
427 if (p != NULL)
428 {
429 gfc_error (p);
430 m = MATCH_ERROR;
431 }
432
433 *value = i;
434 return m;
435}
436
437
6de9cd9a
DN
438/* Matches a statement label. Uses gfc_match_small_literal_int() to
439 do most of the work. */
440
441match
b251af97 442gfc_match_st_label (gfc_st_label **label)
6de9cd9a
DN
443{
444 locus old_loc;
445 match m;
8a8f7eca 446 int i, cnt;
6de9cd9a 447
63645982 448 old_loc = gfc_current_locus;
6de9cd9a 449
8a8f7eca 450 m = gfc_match_small_literal_int (&i, &cnt);
6de9cd9a
DN
451 if (m != MATCH_YES)
452 return m;
453
8a8f7eca 454 if (cnt > 5)
6de9cd9a 455 {
8a8f7eca
SK
456 gfc_error ("Too many digits in statement label at %C");
457 goto cleanup;
6de9cd9a
DN
458 }
459
a34a91f0 460 if (i == 0)
8a8f7eca
SK
461 {
462 gfc_error ("Statement label at %C is zero");
463 goto cleanup;
464 }
465
466 *label = gfc_get_st_label (i);
467 return MATCH_YES;
468
469cleanup:
470
63645982 471 gfc_current_locus = old_loc;
6de9cd9a
DN
472 return MATCH_ERROR;
473}
474
475
476/* Match and validate a label associated with a named IF, DO or SELECT
477 statement. If the symbol does not have the label attribute, we add
478 it. We also make sure the symbol does not refer to another
479 (active) block. A matched label is pointed to by gfc_new_block. */
480
481match
482gfc_match_label (void)
483{
484 char name[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
485 match m;
486
487 gfc_new_block = NULL;
488
489 m = gfc_match (" %n :", name);
490 if (m != MATCH_YES)
491 return m;
492
493 if (gfc_get_symbol (name, NULL, &gfc_new_block))
494 {
495 gfc_error ("Label name '%s' at %C is ambiguous", name);
496 return MATCH_ERROR;
497 }
498
cb1d4dce
SK
499 if (gfc_new_block->attr.flavor == FL_LABEL)
500 {
501 gfc_error ("Duplicate construct label '%s' at %C", name);
502 return MATCH_ERROR;
503 }
6de9cd9a 504
cb1d4dce
SK
505 if (gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
506 gfc_new_block->name, NULL) == FAILURE)
507 return MATCH_ERROR;
6de9cd9a
DN
508
509 return MATCH_YES;
510}
511
512
6de9cd9a 513/* See if the current input looks like a name of some sort. Modifies
090021e9
BM
514 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
515 Note that options.c restricts max_identifier_length to not more
516 than GFC_MAX_SYMBOL_LEN. */
6de9cd9a
DN
517
518match
519gfc_match_name (char *buffer)
520{
521 locus old_loc;
8fc541d3
FXC
522 int i;
523 char c;
6de9cd9a 524
63645982 525 old_loc = gfc_current_locus;
6de9cd9a
DN
526 gfc_gobble_whitespace ();
527
8fc541d3 528 c = gfc_next_ascii_char ();
e6472bce 529 if (!(ISALPHA (c) || (c == '_' && gfc_option.flag_allow_leading_underscore)))
6de9cd9a 530 {
9a528648 531 if (gfc_error_flag_test() == 0 && c != '(')
b251af97 532 gfc_error ("Invalid character in name at %C");
63645982 533 gfc_current_locus = old_loc;
6de9cd9a
DN
534 return MATCH_NO;
535 }
536
537 i = 0;
538
539 do
540 {
541 buffer[i++] = c;
542
543 if (i > gfc_option.max_identifier_length)
544 {
545 gfc_error ("Name at %C is too long");
546 return MATCH_ERROR;
547 }
548
63645982 549 old_loc = gfc_current_locus;
8fc541d3 550 c = gfc_next_ascii_char ();
6de9cd9a 551 }
b251af97 552 while (ISALNUM (c) || c == '_' || (gfc_option.flag_dollar_ok && c == '$'));
6de9cd9a 553
89a5afda
TB
554 if (c == '$' && !gfc_option.flag_dollar_ok)
555 {
8fc541d3
FXC
556 gfc_error ("Invalid character '$' at %C. Use -fdollar-ok to allow it "
557 "as an extension");
89a5afda
TB
558 return MATCH_ERROR;
559 }
560
6de9cd9a 561 buffer[i] = '\0';
63645982 562 gfc_current_locus = old_loc;
6de9cd9a
DN
563
564 return MATCH_YES;
565}
566
567
a8b3b0b6
CR
568/* Match a valid name for C, which is almost the same as for Fortran,
569 except that you can start with an underscore, etc.. It could have
570 been done by modifying the gfc_match_name, but this way other
571 things C allows can be added, such as no limits on the length.
572 Right now, the length is limited to the same thing as Fortran..
573 Also, by rewriting it, we use the gfc_next_char_C() to prevent the
574 input characters from being automatically lower cased, since C is
575 case sensitive. The parameter, buffer, is used to return the name
576 that is matched. Return MATCH_ERROR if the name is too long
577 (though this is a self-imposed limit), MATCH_NO if what we're
578 seeing isn't a name, and MATCH_YES if we successfully match a C
579 name. */
580
581match
582gfc_match_name_C (char *buffer)
583{
584 locus old_loc;
585 int i = 0;
8fc541d3 586 gfc_char_t c;
a8b3b0b6
CR
587
588 old_loc = gfc_current_locus;
589 gfc_gobble_whitespace ();
590
591 /* Get the next char (first possible char of name) and see if
592 it's valid for C (either a letter or an underscore). */
593 c = gfc_next_char_literal (1);
594
595 /* If the user put nothing expect spaces between the quotes, it is valid
596 and simply means there is no name= specifier and the name is the fortran
597 symbol name, all lowercase. */
598 if (c == '"' || c == '\'')
599 {
600 buffer[0] = '\0';
601 gfc_current_locus = old_loc;
602 return MATCH_YES;
603 }
604
605 if (!ISALPHA (c) && c != '_')
606 {
607 gfc_error ("Invalid C name in NAME= specifier at %C");
608 return MATCH_ERROR;
609 }
610
611 /* Continue to read valid variable name characters. */
612 do
613 {
8fc541d3
FXC
614 gcc_assert (gfc_wide_fits_in_byte (c));
615
616 buffer[i++] = (unsigned char) c;
a8b3b0b6
CR
617
618 /* C does not define a maximum length of variable names, to my
619 knowledge, but the compiler typically places a limit on them.
620 For now, i'll use the same as the fortran limit for simplicity,
621 but this may need to be changed to a dynamic buffer that can
622 be realloc'ed here if necessary, or more likely, a larger
623 upper-bound set. */
624 if (i > gfc_option.max_identifier_length)
625 {
626 gfc_error ("Name at %C is too long");
627 return MATCH_ERROR;
628 }
629
630 old_loc = gfc_current_locus;
631
632 /* Get next char; param means we're in a string. */
633 c = gfc_next_char_literal (1);
634 } while (ISALNUM (c) || c == '_');
635
636 buffer[i] = '\0';
637 gfc_current_locus = old_loc;
638
639 /* See if we stopped because of whitespace. */
640 if (c == ' ')
641 {
642 gfc_gobble_whitespace ();
8fc541d3 643 c = gfc_peek_ascii_char ();
a8b3b0b6
CR
644 if (c != '"' && c != '\'')
645 {
646 gfc_error ("Embedded space in NAME= specifier at %C");
647 return MATCH_ERROR;
648 }
649 }
650
651 /* If we stopped because we had an invalid character for a C name, report
652 that to the user by returning MATCH_NO. */
653 if (c != '"' && c != '\'')
654 {
655 gfc_error ("Invalid C name in NAME= specifier at %C");
656 return MATCH_ERROR;
657 }
658
659 return MATCH_YES;
660}
661
662
6de9cd9a
DN
663/* Match a symbol on the input. Modifies the pointer to the symbol
664 pointer if successful. */
665
666match
b251af97 667gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
6de9cd9a
DN
668{
669 char buffer[GFC_MAX_SYMBOL_LEN + 1];
670 match m;
671
672 m = gfc_match_name (buffer);
673 if (m != MATCH_YES)
674 return m;
675
676 if (host_assoc)
677 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
66e4ab31 678 ? MATCH_ERROR : MATCH_YES;
6de9cd9a 679
08a6b8e0 680 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
6de9cd9a
DN
681 return MATCH_ERROR;
682
683 return MATCH_YES;
684}
685
686
687match
b251af97 688gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
6de9cd9a
DN
689{
690 gfc_symtree *st;
691 match m;
692
693 m = gfc_match_sym_tree (&st, host_assoc);
694
695 if (m == MATCH_YES)
696 {
697 if (st)
b251af97 698 *matched_symbol = st->n.sym;
6de9cd9a 699 else
b251af97 700 *matched_symbol = NULL;
6de9cd9a 701 }
32cafd73
MH
702 else
703 *matched_symbol = NULL;
6de9cd9a
DN
704 return m;
705}
706
b251af97 707
6de9cd9a
DN
708/* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
709 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
710 in matchexp.c. */
711
712match
b251af97 713gfc_match_intrinsic_op (gfc_intrinsic_op *result)
6de9cd9a 714{
f4d8e0d1 715 locus orig_loc = gfc_current_locus;
8fc541d3 716 char ch;
6de9cd9a 717
f4d8e0d1 718 gfc_gobble_whitespace ();
8fc541d3 719 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
720 switch (ch)
721 {
722 case '+':
723 /* Matched "+". */
724 *result = INTRINSIC_PLUS;
725 return MATCH_YES;
6de9cd9a 726
f4d8e0d1
RS
727 case '-':
728 /* Matched "-". */
729 *result = INTRINSIC_MINUS;
730 return MATCH_YES;
6de9cd9a 731
f4d8e0d1 732 case '=':
8fc541d3 733 if (gfc_next_ascii_char () == '=')
f4d8e0d1
RS
734 {
735 /* Matched "==". */
736 *result = INTRINSIC_EQ;
737 return MATCH_YES;
738 }
739 break;
740
741 case '<':
8fc541d3 742 if (gfc_peek_ascii_char () == '=')
f4d8e0d1
RS
743 {
744 /* Matched "<=". */
8fc541d3 745 gfc_next_ascii_char ();
f4d8e0d1
RS
746 *result = INTRINSIC_LE;
747 return MATCH_YES;
748 }
749 /* Matched "<". */
750 *result = INTRINSIC_LT;
751 return MATCH_YES;
752
753 case '>':
8fc541d3 754 if (gfc_peek_ascii_char () == '=')
f4d8e0d1
RS
755 {
756 /* Matched ">=". */
8fc541d3 757 gfc_next_ascii_char ();
f4d8e0d1
RS
758 *result = INTRINSIC_GE;
759 return MATCH_YES;
760 }
761 /* Matched ">". */
762 *result = INTRINSIC_GT;
763 return MATCH_YES;
764
765 case '*':
8fc541d3 766 if (gfc_peek_ascii_char () == '*')
f4d8e0d1
RS
767 {
768 /* Matched "**". */
8fc541d3 769 gfc_next_ascii_char ();
f4d8e0d1
RS
770 *result = INTRINSIC_POWER;
771 return MATCH_YES;
772 }
773 /* Matched "*". */
774 *result = INTRINSIC_TIMES;
775 return MATCH_YES;
776
777 case '/':
8fc541d3 778 ch = gfc_peek_ascii_char ();
f4d8e0d1
RS
779 if (ch == '=')
780 {
781 /* Matched "/=". */
8fc541d3 782 gfc_next_ascii_char ();
f4d8e0d1
RS
783 *result = INTRINSIC_NE;
784 return MATCH_YES;
785 }
786 else if (ch == '/')
787 {
788 /* Matched "//". */
8fc541d3 789 gfc_next_ascii_char ();
f4d8e0d1
RS
790 *result = INTRINSIC_CONCAT;
791 return MATCH_YES;
792 }
793 /* Matched "/". */
794 *result = INTRINSIC_DIVIDE;
795 return MATCH_YES;
796
797 case '.':
8fc541d3 798 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
799 switch (ch)
800 {
801 case 'a':
8fc541d3
FXC
802 if (gfc_next_ascii_char () == 'n'
803 && gfc_next_ascii_char () == 'd'
804 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
805 {
806 /* Matched ".and.". */
807 *result = INTRINSIC_AND;
808 return MATCH_YES;
809 }
810 break;
811
812 case 'e':
8fc541d3 813 if (gfc_next_ascii_char () == 'q')
f4d8e0d1 814 {
8fc541d3 815 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
816 if (ch == '.')
817 {
818 /* Matched ".eq.". */
819 *result = INTRINSIC_EQ_OS;
820 return MATCH_YES;
821 }
822 else if (ch == 'v')
823 {
8fc541d3 824 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
825 {
826 /* Matched ".eqv.". */
827 *result = INTRINSIC_EQV;
828 return MATCH_YES;
829 }
830 }
831 }
832 break;
833
834 case 'g':
8fc541d3 835 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
836 if (ch == 'e')
837 {
8fc541d3 838 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
839 {
840 /* Matched ".ge.". */
841 *result = INTRINSIC_GE_OS;
842 return MATCH_YES;
843 }
844 }
845 else if (ch == 't')
846 {
8fc541d3 847 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
848 {
849 /* Matched ".gt.". */
850 *result = INTRINSIC_GT_OS;
851 return MATCH_YES;
852 }
853 }
854 break;
855
856 case 'l':
8fc541d3 857 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
858 if (ch == 'e')
859 {
8fc541d3 860 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
861 {
862 /* Matched ".le.". */
863 *result = INTRINSIC_LE_OS;
864 return MATCH_YES;
865 }
866 }
867 else if (ch == 't')
868 {
8fc541d3 869 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
870 {
871 /* Matched ".lt.". */
872 *result = INTRINSIC_LT_OS;
873 return MATCH_YES;
874 }
875 }
876 break;
877
878 case 'n':
8fc541d3 879 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
880 if (ch == 'e')
881 {
8fc541d3 882 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
883 if (ch == '.')
884 {
885 /* Matched ".ne.". */
886 *result = INTRINSIC_NE_OS;
887 return MATCH_YES;
888 }
889 else if (ch == 'q')
890 {
8fc541d3
FXC
891 if (gfc_next_ascii_char () == 'v'
892 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
893 {
894 /* Matched ".neqv.". */
895 *result = INTRINSIC_NEQV;
896 return MATCH_YES;
897 }
898 }
899 }
900 else if (ch == 'o')
901 {
8fc541d3
FXC
902 if (gfc_next_ascii_char () == 't'
903 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
904 {
905 /* Matched ".not.". */
906 *result = INTRINSIC_NOT;
907 return MATCH_YES;
908 }
909 }
910 break;
911
912 case 'o':
8fc541d3
FXC
913 if (gfc_next_ascii_char () == 'r'
914 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
915 {
916 /* Matched ".or.". */
917 *result = INTRINSIC_OR;
918 return MATCH_YES;
919 }
920 break;
921
922 default:
923 break;
924 }
925 break;
926
927 default:
928 break;
929 }
930
931 gfc_current_locus = orig_loc;
932 return MATCH_NO;
6de9cd9a
DN
933}
934
935
936/* Match a loop control phrase:
937
938 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
939
940 If the final integer expression is not present, a constant unity
941 expression is returned. We don't return MATCH_ERROR until after
942 the equals sign is seen. */
943
944match
b251af97 945gfc_match_iterator (gfc_iterator *iter, int init_flag)
6de9cd9a
DN
946{
947 char name[GFC_MAX_SYMBOL_LEN + 1];
948 gfc_expr *var, *e1, *e2, *e3;
949 locus start;
950 match m;
951
b251af97 952 /* Match the start of an iterator without affecting the symbol table. */
6de9cd9a 953
63645982 954 start = gfc_current_locus;
6de9cd9a 955 m = gfc_match (" %n =", name);
63645982 956 gfc_current_locus = start;
6de9cd9a
DN
957
958 if (m != MATCH_YES)
959 return MATCH_NO;
960
961 m = gfc_match_variable (&var, 0);
962 if (m != MATCH_YES)
963 return MATCH_NO;
964
965 gfc_match_char ('=');
966
967 e1 = e2 = e3 = NULL;
968
969 if (var->ref != NULL)
970 {
971 gfc_error ("Loop variable at %C cannot be a sub-component");
972 goto cleanup;
973 }
974
975 if (var->symtree->n.sym->attr.intent == INTENT_IN)
976 {
977 gfc_error ("Loop variable '%s' at %C cannot be INTENT(IN)",
978 var->symtree->n.sym->name);
979 goto cleanup;
980 }
981
9a3db5a3
PT
982 var->symtree->n.sym->attr.implied_index = 1;
983
6de9cd9a
DN
984 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
985 if (m == MATCH_NO)
986 goto syntax;
987 if (m == MATCH_ERROR)
988 goto cleanup;
989
990 if (gfc_match_char (',') != MATCH_YES)
991 goto syntax;
992
993 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
994 if (m == MATCH_NO)
995 goto syntax;
996 if (m == MATCH_ERROR)
997 goto cleanup;
998
999 if (gfc_match_char (',') != MATCH_YES)
1000 {
1001 e3 = gfc_int_expr (1);
1002 goto done;
1003 }
1004
1005 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1006 if (m == MATCH_ERROR)
1007 goto cleanup;
1008 if (m == MATCH_NO)
1009 {
1010 gfc_error ("Expected a step value in iterator at %C");
1011 goto cleanup;
1012 }
1013
1014done:
1015 iter->var = var;
1016 iter->start = e1;
1017 iter->end = e2;
1018 iter->step = e3;
1019 return MATCH_YES;
1020
1021syntax:
1022 gfc_error ("Syntax error in iterator at %C");
1023
1024cleanup:
1025 gfc_free_expr (e1);
1026 gfc_free_expr (e2);
1027 gfc_free_expr (e3);
1028
1029 return MATCH_ERROR;
1030}
1031
1032
1033/* Tries to match the next non-whitespace character on the input.
1034 This subroutine does not return MATCH_ERROR. */
1035
1036match
1037gfc_match_char (char c)
1038{
1039 locus where;
1040
63645982 1041 where = gfc_current_locus;
6de9cd9a
DN
1042 gfc_gobble_whitespace ();
1043
8fc541d3 1044 if (gfc_next_ascii_char () == c)
6de9cd9a
DN
1045 return MATCH_YES;
1046
63645982 1047 gfc_current_locus = where;
6de9cd9a
DN
1048 return MATCH_NO;
1049}
1050
1051
1052/* General purpose matching subroutine. The target string is a
1053 scanf-like format string in which spaces correspond to arbitrary
1054 whitespace (including no whitespace), characters correspond to
1055 themselves. The %-codes are:
1056
1057 %% Literal percent sign
1058 %e Expression, pointer to a pointer is set
1059 %s Symbol, pointer to the symbol is set
1060 %n Name, character buffer is set to name
1061 %t Matches end of statement.
1062 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1063 %l Matches a statement label
1064 %v Matches a variable expression (an lvalue)
1065 % Matches a required space (in free form) and optional spaces. */
1066
1067match
1068gfc_match (const char *target, ...)
1069{
1070 gfc_st_label **label;
1071 int matches, *ip;
1072 locus old_loc;
1073 va_list argp;
1074 char c, *np;
1075 match m, n;
1076 void **vp;
1077 const char *p;
1078
63645982 1079 old_loc = gfc_current_locus;
6de9cd9a
DN
1080 va_start (argp, target);
1081 m = MATCH_NO;
1082 matches = 0;
1083 p = target;
1084
1085loop:
1086 c = *p++;
1087 switch (c)
1088 {
1089 case ' ':
1090 gfc_gobble_whitespace ();
1091 goto loop;
1092 case '\0':
1093 m = MATCH_YES;
1094 break;
1095
1096 case '%':
1097 c = *p++;
1098 switch (c)
1099 {
1100 case 'e':
1101 vp = va_arg (argp, void **);
1102 n = gfc_match_expr ((gfc_expr **) vp);
1103 if (n != MATCH_YES)
1104 {
1105 m = n;
1106 goto not_yes;
1107 }
1108
1109 matches++;
1110 goto loop;
1111
1112 case 'v':
1113 vp = va_arg (argp, void **);
1114 n = gfc_match_variable ((gfc_expr **) vp, 0);
1115 if (n != MATCH_YES)
1116 {
1117 m = n;
1118 goto not_yes;
1119 }
1120
1121 matches++;
1122 goto loop;
1123
1124 case 's':
1125 vp = va_arg (argp, void **);
1126 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1127 if (n != MATCH_YES)
1128 {
1129 m = n;
1130 goto not_yes;
1131 }
1132
1133 matches++;
1134 goto loop;
1135
1136 case 'n':
1137 np = va_arg (argp, char *);
1138 n = gfc_match_name (np);
1139 if (n != MATCH_YES)
1140 {
1141 m = n;
1142 goto not_yes;
1143 }
1144
1145 matches++;
1146 goto loop;
1147
1148 case 'l':
1149 label = va_arg (argp, gfc_st_label **);
a34a91f0 1150 n = gfc_match_st_label (label);
6de9cd9a
DN
1151 if (n != MATCH_YES)
1152 {
1153 m = n;
1154 goto not_yes;
1155 }
1156
1157 matches++;
1158 goto loop;
1159
1160 case 'o':
1161 ip = va_arg (argp, int *);
1162 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1163 if (n != MATCH_YES)
1164 {
1165 m = n;
1166 goto not_yes;
1167 }
1168
1169 matches++;
1170 goto loop;
1171
1172 case 't':
1173 if (gfc_match_eos () != MATCH_YES)
1174 {
1175 m = MATCH_NO;
1176 goto not_yes;
1177 }
1178 goto loop;
1179
1180 case ' ':
1181 if (gfc_match_space () == MATCH_YES)
1182 goto loop;
1183 m = MATCH_NO;
1184 goto not_yes;
1185
1186 case '%':
66e4ab31 1187 break; /* Fall through to character matcher. */
6de9cd9a
DN
1188
1189 default:
1190 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1191 }
1192
1193 default:
befdf741
DK
1194
1195 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1196 expect an upper case character here! */
1197 gcc_assert (TOLOWER (c) == c);
1198
8fc541d3 1199 if (c == gfc_next_ascii_char ())
6de9cd9a
DN
1200 goto loop;
1201 break;
1202 }
1203
1204not_yes:
1205 va_end (argp);
1206
1207 if (m != MATCH_YES)
1208 {
1209 /* Clean up after a failed match. */
63645982 1210 gfc_current_locus = old_loc;
6de9cd9a
DN
1211 va_start (argp, target);
1212
1213 p = target;
1214 for (; matches > 0; matches--)
1215 {
1216 while (*p++ != '%');
1217
1218 switch (*p++)
1219 {
1220 case '%':
1221 matches++;
66e4ab31 1222 break; /* Skip. */
6de9cd9a 1223
6de9cd9a
DN
1224 /* Matches that don't have to be undone */
1225 case 'o':
1226 case 'l':
1227 case 'n':
1228 case 's':
b251af97 1229 (void) va_arg (argp, void **);
6de9cd9a
DN
1230 break;
1231
1232 case 'e':
6de9cd9a 1233 case 'v':
6de9cd9a 1234 vp = va_arg (argp, void **);
ece3f663 1235 gfc_free_expr ((struct gfc_expr *)*vp);
6de9cd9a
DN
1236 *vp = NULL;
1237 break;
1238 }
1239 }
1240
1241 va_end (argp);
1242 }
1243
1244 return m;
1245}
1246
1247
1248/*********************** Statement level matching **********************/
1249
1250/* Matches the start of a program unit, which is the program keyword
e08b5a75 1251 followed by an obligatory symbol. */
6de9cd9a
DN
1252
1253match
1254gfc_match_program (void)
1255{
1256 gfc_symbol *sym;
1257 match m;
1258
6de9cd9a
DN
1259 m = gfc_match ("% %s%t", &sym);
1260
1261 if (m == MATCH_NO)
1262 {
1263 gfc_error ("Invalid form of PROGRAM statement at %C");
1264 m = MATCH_ERROR;
1265 }
1266
1267 if (m == MATCH_ERROR)
1268 return m;
1269
231b2fcc 1270 if (gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL) == FAILURE)
6de9cd9a
DN
1271 return MATCH_ERROR;
1272
1273 gfc_new_block = sym;
1274
1275 return MATCH_YES;
1276}
1277
1278
1279/* Match a simple assignment statement. */
1280
1281match
1282gfc_match_assignment (void)
1283{
1284 gfc_expr *lvalue, *rvalue;
1285 locus old_loc;
1286 match m;
1287
63645982 1288 old_loc = gfc_current_locus;
6de9cd9a 1289
5056a350 1290 lvalue = NULL;
6de9cd9a
DN
1291 m = gfc_match (" %v =", &lvalue);
1292 if (m != MATCH_YES)
c9583ed2 1293 {
5056a350
SK
1294 gfc_current_locus = old_loc;
1295 gfc_free_expr (lvalue);
1296 return MATCH_NO;
c9583ed2
TS
1297 }
1298
5056a350 1299 rvalue = NULL;
6de9cd9a
DN
1300 m = gfc_match (" %e%t", &rvalue);
1301 if (m != MATCH_YES)
5056a350
SK
1302 {
1303 gfc_current_locus = old_loc;
1304 gfc_free_expr (lvalue);
1305 gfc_free_expr (rvalue);
1306 return m;
1307 }
6de9cd9a
DN
1308
1309 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1310
1311 new_st.op = EXEC_ASSIGN;
a513927a 1312 new_st.expr1 = lvalue;
6de9cd9a
DN
1313 new_st.expr2 = rvalue;
1314
c9583ed2
TS
1315 gfc_check_do_variable (lvalue->symtree);
1316
6de9cd9a 1317 return MATCH_YES;
6de9cd9a
DN
1318}
1319
1320
1321/* Match a pointer assignment statement. */
1322
1323match
1324gfc_match_pointer_assignment (void)
1325{
1326 gfc_expr *lvalue, *rvalue;
1327 locus old_loc;
1328 match m;
1329
63645982 1330 old_loc = gfc_current_locus;
6de9cd9a
DN
1331
1332 lvalue = rvalue = NULL;
8fb74da4 1333 gfc_matching_procptr_assignment = 0;
6de9cd9a
DN
1334
1335 m = gfc_match (" %v =>", &lvalue);
1336 if (m != MATCH_YES)
1337 {
1338 m = MATCH_NO;
1339 goto cleanup;
1340 }
1341
713485cc 1342 if (lvalue->symtree->n.sym->attr.proc_pointer
f64edc8b 1343 || gfc_is_proc_ptr_comp (lvalue, NULL))
8fb74da4
JW
1344 gfc_matching_procptr_assignment = 1;
1345
6de9cd9a 1346 m = gfc_match (" %e%t", &rvalue);
8fb74da4 1347 gfc_matching_procptr_assignment = 0;
6de9cd9a
DN
1348 if (m != MATCH_YES)
1349 goto cleanup;
1350
1351 new_st.op = EXEC_POINTER_ASSIGN;
a513927a 1352 new_st.expr1 = lvalue;
6de9cd9a
DN
1353 new_st.expr2 = rvalue;
1354
1355 return MATCH_YES;
1356
1357cleanup:
63645982 1358 gfc_current_locus = old_loc;
6de9cd9a
DN
1359 gfc_free_expr (lvalue);
1360 gfc_free_expr (rvalue);
1361 return m;
1362}
1363
1364
43e1c5f7 1365/* We try to match an easy arithmetic IF statement. This only happens
835d64ab
FXC
1366 when just after having encountered a simple IF statement. This code
1367 is really duplicate with parts of the gfc_match_if code, but this is
1368 *much* easier. */
b251af97 1369
f55e72ce 1370static match
835d64ab 1371match_arithmetic_if (void)
43e1c5f7
FXC
1372{
1373 gfc_st_label *l1, *l2, *l3;
1374 gfc_expr *expr;
1375 match m;
1376
1377 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1378 if (m != MATCH_YES)
1379 return m;
1380
1381 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1382 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1383 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1384 {
1385 gfc_free_expr (expr);
1386 return MATCH_ERROR;
1387 }
1388
e2ab8b09
JW
1389 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
1390 "statement at %C") == FAILURE)
51c3f0f6
FXC
1391 return MATCH_ERROR;
1392
43e1c5f7 1393 new_st.op = EXEC_ARITHMETIC_IF;
a513927a 1394 new_st.expr1 = expr;
79bd1948 1395 new_st.label1 = l1;
43e1c5f7
FXC
1396 new_st.label2 = l2;
1397 new_st.label3 = l3;
1398
1399 return MATCH_YES;
1400}
1401
1402
6de9cd9a
DN
1403/* The IF statement is a bit of a pain. First of all, there are three
1404 forms of it, the simple IF, the IF that starts a block and the
1405 arithmetic IF.
1406
1407 There is a problem with the simple IF and that is the fact that we
1408 only have a single level of undo information on symbols. What this
1409 means is for a simple IF, we must re-match the whole IF statement
1410 multiple times in order to guarantee that the symbol table ends up
1411 in the proper state. */
1412
c874ae73
TS
1413static match match_simple_forall (void);
1414static match match_simple_where (void);
1415
6de9cd9a 1416match
b251af97 1417gfc_match_if (gfc_statement *if_type)
6de9cd9a
DN
1418{
1419 gfc_expr *expr;
1420 gfc_st_label *l1, *l2, *l3;
f9b9fb82 1421 locus old_loc, old_loc2;
6de9cd9a
DN
1422 gfc_code *p;
1423 match m, n;
1424
1425 n = gfc_match_label ();
1426 if (n == MATCH_ERROR)
1427 return n;
1428
63645982 1429 old_loc = gfc_current_locus;
6de9cd9a
DN
1430
1431 m = gfc_match (" if ( %e", &expr);
1432 if (m != MATCH_YES)
1433 return m;
1434
f9b9fb82
JD
1435 old_loc2 = gfc_current_locus;
1436 gfc_current_locus = old_loc;
1437
1438 if (gfc_match_parens () == MATCH_ERROR)
1439 return MATCH_ERROR;
1440
1441 gfc_current_locus = old_loc2;
1442
6de9cd9a
DN
1443 if (gfc_match_char (')') != MATCH_YES)
1444 {
1445 gfc_error ("Syntax error in IF-expression at %C");
1446 gfc_free_expr (expr);
1447 return MATCH_ERROR;
1448 }
1449
1450 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1451
1452 if (m == MATCH_YES)
1453 {
1454 if (n == MATCH_YES)
1455 {
b251af97
SK
1456 gfc_error ("Block label not appropriate for arithmetic IF "
1457 "statement at %C");
6de9cd9a
DN
1458 gfc_free_expr (expr);
1459 return MATCH_ERROR;
1460 }
1461
1462 if (gfc_reference_st_label (l1, ST_LABEL_TARGET) == FAILURE
1463 || gfc_reference_st_label (l2, ST_LABEL_TARGET) == FAILURE
1464 || gfc_reference_st_label (l3, ST_LABEL_TARGET) == FAILURE)
1465 {
6de9cd9a
DN
1466 gfc_free_expr (expr);
1467 return MATCH_ERROR;
1468 }
51c3f0f6 1469
e2ab8b09 1470 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Arithmetic IF "
b251af97
SK
1471 "statement at %C") == FAILURE)
1472 return MATCH_ERROR;
6de9cd9a
DN
1473
1474 new_st.op = EXEC_ARITHMETIC_IF;
a513927a 1475 new_st.expr1 = expr;
79bd1948 1476 new_st.label1 = l1;
6de9cd9a
DN
1477 new_st.label2 = l2;
1478 new_st.label3 = l3;
1479
1480 *if_type = ST_ARITHMETIC_IF;
1481 return MATCH_YES;
1482 }
1483
172b8799 1484 if (gfc_match (" then%t") == MATCH_YES)
6de9cd9a
DN
1485 {
1486 new_st.op = EXEC_IF;
a513927a 1487 new_st.expr1 = expr;
6de9cd9a
DN
1488 *if_type = ST_IF_BLOCK;
1489 return MATCH_YES;
1490 }
1491
1492 if (n == MATCH_YES)
1493 {
f9b9fb82 1494 gfc_error ("Block label is not appropriate for IF statement at %C");
6de9cd9a
DN
1495 gfc_free_expr (expr);
1496 return MATCH_ERROR;
1497 }
1498
1499 /* At this point the only thing left is a simple IF statement. At
1500 this point, n has to be MATCH_NO, so we don't have to worry about
1501 re-matching a block label. From what we've got so far, try
1502 matching an assignment. */
1503
1504 *if_type = ST_SIMPLE_IF;
1505
1506 m = gfc_match_assignment ();
1507 if (m == MATCH_YES)
1508 goto got_match;
1509
1510 gfc_free_expr (expr);
1511 gfc_undo_symbols ();
63645982 1512 gfc_current_locus = old_loc;
6de9cd9a 1513
5056a350
SK
1514 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1515 assignment was found. For MATCH_NO, continue to call the various
1516 matchers. */
17bbca74
SK
1517 if (m == MATCH_ERROR)
1518 return MATCH_ERROR;
1519
66e4ab31 1520 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
6de9cd9a
DN
1521
1522 m = gfc_match_pointer_assignment ();
1523 if (m == MATCH_YES)
1524 goto got_match;
1525
1526 gfc_free_expr (expr);
1527 gfc_undo_symbols ();
63645982 1528 gfc_current_locus = old_loc;
6de9cd9a 1529
66e4ab31 1530 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
6de9cd9a
DN
1531
1532 /* Look at the next keyword to see which matcher to call. Matching
1533 the keyword doesn't affect the symbol table, so we don't have to
1534 restore between tries. */
1535
1536#define match(string, subr, statement) \
1537 if (gfc_match(string) == MATCH_YES) { m = subr(); goto got_match; }
1538
1539 gfc_clear_error ();
1540
1541 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
5056a350
SK
1542 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1543 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1544 match ("call", gfc_match_call, ST_CALL)
1545 match ("close", gfc_match_close, ST_CLOSE)
1546 match ("continue", gfc_match_continue, ST_CONTINUE)
1547 match ("cycle", gfc_match_cycle, ST_CYCLE)
1548 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1549 match ("end file", gfc_match_endfile, ST_END_FILE)
1550 match ("exit", gfc_match_exit, ST_EXIT)
1551 match ("flush", gfc_match_flush, ST_FLUSH)
1552 match ("forall", match_simple_forall, ST_FORALL)
1553 match ("go to", gfc_match_goto, ST_GOTO)
1554 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1555 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1556 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1557 match ("open", gfc_match_open, ST_OPEN)
1558 match ("pause", gfc_match_pause, ST_NONE)
1559 match ("print", gfc_match_print, ST_WRITE)
1560 match ("read", gfc_match_read, ST_READ)
1561 match ("return", gfc_match_return, ST_RETURN)
1562 match ("rewind", gfc_match_rewind, ST_REWIND)
1563 match ("stop", gfc_match_stop, ST_STOP)
6f0f0b2e 1564 match ("wait", gfc_match_wait, ST_WAIT)
5056a350
SK
1565 match ("where", match_simple_where, ST_WHERE)
1566 match ("write", gfc_match_write, ST_WRITE)
1567
1568 /* The gfc_match_assignment() above may have returned a MATCH_NO
884f22e3 1569 where the assignment was to a named constant. Check that
5056a350
SK
1570 special case here. */
1571 m = gfc_match_assignment ();
1572 if (m == MATCH_NO)
1573 {
1574 gfc_error ("Cannot assign to a named constant at %C");
1575 gfc_free_expr (expr);
1576 gfc_undo_symbols ();
1577 gfc_current_locus = old_loc;
1578 return MATCH_ERROR;
1579 }
6de9cd9a
DN
1580
1581 /* All else has failed, so give up. See if any of the matchers has
1582 stored an error message of some sort. */
b251af97 1583 if (gfc_error_check () == 0)
6de9cd9a
DN
1584 gfc_error ("Unclassifiable statement in IF-clause at %C");
1585
1586 gfc_free_expr (expr);
1587 return MATCH_ERROR;
1588
1589got_match:
1590 if (m == MATCH_NO)
1591 gfc_error ("Syntax error in IF-clause at %C");
1592 if (m != MATCH_YES)
1593 {
1594 gfc_free_expr (expr);
1595 return MATCH_ERROR;
1596 }
1597
1598 /* At this point, we've matched the single IF and the action clause
1599 is in new_st. Rearrange things so that the IF statement appears
1600 in new_st. */
1601
1602 p = gfc_get_code ();
1603 p->next = gfc_get_code ();
1604 *p->next = new_st;
63645982 1605 p->next->loc = gfc_current_locus;
6de9cd9a 1606
a513927a 1607 p->expr1 = expr;
6de9cd9a
DN
1608 p->op = EXEC_IF;
1609
1610 gfc_clear_new_st ();
1611
1612 new_st.op = EXEC_IF;
1613 new_st.block = p;
1614
1615 return MATCH_YES;
1616}
1617
1618#undef match
1619
1620
1621/* Match an ELSE statement. */
1622
1623match
1624gfc_match_else (void)
1625{
1626 char name[GFC_MAX_SYMBOL_LEN + 1];
1627
1628 if (gfc_match_eos () == MATCH_YES)
1629 return MATCH_YES;
1630
1631 if (gfc_match_name (name) != MATCH_YES
1632 || gfc_current_block () == NULL
1633 || gfc_match_eos () != MATCH_YES)
1634 {
1635 gfc_error ("Unexpected junk after ELSE statement at %C");
1636 return MATCH_ERROR;
1637 }
1638
1639 if (strcmp (name, gfc_current_block ()->name) != 0)
1640 {
1641 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1642 name, gfc_current_block ()->name);
1643 return MATCH_ERROR;
1644 }
1645
1646 return MATCH_YES;
1647}
1648
1649
1650/* Match an ELSE IF statement. */
1651
1652match
1653gfc_match_elseif (void)
1654{
1655 char name[GFC_MAX_SYMBOL_LEN + 1];
1656 gfc_expr *expr;
1657 match m;
1658
1659 m = gfc_match (" ( %e ) then", &expr);
1660 if (m != MATCH_YES)
1661 return m;
1662
1663 if (gfc_match_eos () == MATCH_YES)
1664 goto done;
1665
1666 if (gfc_match_name (name) != MATCH_YES
1667 || gfc_current_block () == NULL
1668 || gfc_match_eos () != MATCH_YES)
1669 {
1670 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1671 goto cleanup;
1672 }
1673
1674 if (strcmp (name, gfc_current_block ()->name) != 0)
1675 {
1676 gfc_error ("Label '%s' at %C doesn't match IF label '%s'",
1677 name, gfc_current_block ()->name);
1678 goto cleanup;
1679 }
1680
1681done:
1682 new_st.op = EXEC_IF;
a513927a 1683 new_st.expr1 = expr;
6de9cd9a
DN
1684 return MATCH_YES;
1685
1686cleanup:
1687 gfc_free_expr (expr);
1688 return MATCH_ERROR;
1689}
1690
1691
1692/* Free a gfc_iterator structure. */
1693
1694void
b251af97 1695gfc_free_iterator (gfc_iterator *iter, int flag)
6de9cd9a 1696{
66e4ab31 1697
6de9cd9a
DN
1698 if (iter == NULL)
1699 return;
1700
1701 gfc_free_expr (iter->var);
1702 gfc_free_expr (iter->start);
1703 gfc_free_expr (iter->end);
1704 gfc_free_expr (iter->step);
1705
1706 if (flag)
1707 gfc_free (iter);
1708}
1709
1710
9abe5e56
DK
1711/* Match a BLOCK statement. */
1712
1713match
1714gfc_match_block (void)
1715{
1716 match m;
1717
1718 if (gfc_match_label () == MATCH_ERROR)
1719 return MATCH_ERROR;
1720
1721 if (gfc_match (" block") != MATCH_YES)
1722 return MATCH_NO;
1723
1724 /* For this to be a correct BLOCK statement, the line must end now. */
1725 m = gfc_match_eos ();
1726 if (m == MATCH_ERROR)
1727 return MATCH_ERROR;
1728 if (m == MATCH_NO)
1729 return MATCH_NO;
1730
1731 return MATCH_YES;
1732}
1733
1734
6de9cd9a
DN
1735/* Match a DO statement. */
1736
1737match
1738gfc_match_do (void)
1739{
1740 gfc_iterator iter, *ip;
1741 locus old_loc;
1742 gfc_st_label *label;
1743 match m;
1744
63645982 1745 old_loc = gfc_current_locus;
6de9cd9a
DN
1746
1747 label = NULL;
1748 iter.var = iter.start = iter.end = iter.step = NULL;
1749
1750 m = gfc_match_label ();
1751 if (m == MATCH_ERROR)
1752 return m;
1753
1754 if (gfc_match (" do") != MATCH_YES)
1755 return MATCH_NO;
1756
a34a91f0 1757 m = gfc_match_st_label (&label);
9b089e05
TS
1758 if (m == MATCH_ERROR)
1759 goto cleanup;
1760
66e4ab31 1761 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
6de9cd9a
DN
1762
1763 if (gfc_match_eos () == MATCH_YES)
1764 {
1765 iter.end = gfc_logical_expr (1, NULL);
1766 new_st.op = EXEC_DO_WHILE;
1767 goto done;
1768 }
1769
66e4ab31
SK
1770 /* Match an optional comma, if no comma is found, a space is obligatory. */
1771 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
6de9cd9a
DN
1772 return MATCH_NO;
1773
acb388a0
JD
1774 /* Check for balanced parens. */
1775
1776 if (gfc_match_parens () == MATCH_ERROR)
1777 return MATCH_ERROR;
1778
6de9cd9a
DN
1779 /* See if we have a DO WHILE. */
1780 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
1781 {
1782 new_st.op = EXEC_DO_WHILE;
1783 goto done;
1784 }
1785
1786 /* The abortive DO WHILE may have done something to the symbol
66e4ab31 1787 table, so we start over. */
6de9cd9a 1788 gfc_undo_symbols ();
63645982 1789 gfc_current_locus = old_loc;
6de9cd9a 1790
66e4ab31
SK
1791 gfc_match_label (); /* This won't error. */
1792 gfc_match (" do "); /* This will work. */
6de9cd9a 1793
66e4ab31
SK
1794 gfc_match_st_label (&label); /* Can't error out. */
1795 gfc_match_char (','); /* Optional comma. */
6de9cd9a
DN
1796
1797 m = gfc_match_iterator (&iter, 0);
1798 if (m == MATCH_NO)
1799 return MATCH_NO;
1800 if (m == MATCH_ERROR)
1801 goto cleanup;
1802
6291f3ba 1803 iter.var->symtree->n.sym->attr.implied_index = 0;
c9583ed2
TS
1804 gfc_check_do_variable (iter.var->symtree);
1805
6de9cd9a
DN
1806 if (gfc_match_eos () != MATCH_YES)
1807 {
1808 gfc_syntax_error (ST_DO);
1809 goto cleanup;
1810 }
1811
1812 new_st.op = EXEC_DO;
1813
1814done:
1815 if (label != NULL
1816 && gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
1817 goto cleanup;
1818
79bd1948 1819 new_st.label1 = label;
6de9cd9a
DN
1820
1821 if (new_st.op == EXEC_DO_WHILE)
a513927a 1822 new_st.expr1 = iter.end;
6de9cd9a
DN
1823 else
1824 {
1825 new_st.ext.iterator = ip = gfc_get_iterator ();
1826 *ip = iter;
1827 }
1828
1829 return MATCH_YES;
1830
1831cleanup:
1832 gfc_free_iterator (&iter, 0);
1833
1834 return MATCH_ERROR;
1835}
1836
1837
1838/* Match an EXIT or CYCLE statement. */
1839
1840static match
1841match_exit_cycle (gfc_statement st, gfc_exec_op op)
1842{
6c7a4dfd 1843 gfc_state_data *p, *o;
6de9cd9a
DN
1844 gfc_symbol *sym;
1845 match m;
1846
1847 if (gfc_match_eos () == MATCH_YES)
1848 sym = NULL;
1849 else
1850 {
1851 m = gfc_match ("% %s%t", &sym);
1852 if (m == MATCH_ERROR)
1853 return MATCH_ERROR;
1854 if (m == MATCH_NO)
1855 {
1856 gfc_syntax_error (st);
1857 return MATCH_ERROR;
1858 }
1859
1860 if (sym->attr.flavor != FL_LABEL)
1861 {
1862 gfc_error ("Name '%s' in %s statement at %C is not a loop name",
1863 sym->name, gfc_ascii_statement (st));
1864 return MATCH_ERROR;
1865 }
1866 }
1867
66e4ab31 1868 /* Find the loop mentioned specified by the label (or lack of a label). */
6c7a4dfd 1869 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
6de9cd9a
DN
1870 if (p->state == COMP_DO && (sym == NULL || sym == p->sym))
1871 break;
6c7a4dfd
JJ
1872 else if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
1873 o = p;
6de9cd9a
DN
1874
1875 if (p == NULL)
1876 {
1877 if (sym == NULL)
1878 gfc_error ("%s statement at %C is not within a loop",
1879 gfc_ascii_statement (st));
1880 else
1881 gfc_error ("%s statement at %C is not within loop '%s'",
1882 gfc_ascii_statement (st), sym->name);
1883
1884 return MATCH_ERROR;
1885 }
1886
6c7a4dfd
JJ
1887 if (o != NULL)
1888 {
1889 gfc_error ("%s statement at %C leaving OpenMP structured block",
1890 gfc_ascii_statement (st));
1891 return MATCH_ERROR;
1892 }
1893 else if (st == ST_EXIT
1894 && p->previous != NULL
1895 && p->previous->state == COMP_OMP_STRUCTURED_BLOCK
1896 && (p->previous->head->op == EXEC_OMP_DO
1897 || p->previous->head->op == EXEC_OMP_PARALLEL_DO))
1898 {
1899 gcc_assert (p->previous->head->next != NULL);
1900 gcc_assert (p->previous->head->next->op == EXEC_DO
1901 || p->previous->head->next->op == EXEC_DO_WHILE);
1902 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
1903 return MATCH_ERROR;
1904 }
1905
6de9cd9a
DN
1906 /* Save the first statement in the loop - needed by the backend. */
1907 new_st.ext.whichloop = p->head;
1908
1909 new_st.op = op;
6de9cd9a
DN
1910
1911 return MATCH_YES;
1912}
1913
1914
1915/* Match the EXIT statement. */
1916
1917match
1918gfc_match_exit (void)
1919{
6de9cd9a
DN
1920 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
1921}
1922
1923
1924/* Match the CYCLE statement. */
1925
1926match
1927gfc_match_cycle (void)
1928{
6de9cd9a
DN
1929 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
1930}
1931
1932
1933/* Match a number or character constant after a STOP or PAUSE statement. */
1934
1935static match
1936gfc_match_stopcode (gfc_statement st)
1937{
1938 int stop_code;
1939 gfc_expr *e;
1940 match m;
8a8f7eca 1941 int cnt;
6de9cd9a 1942
33de49ea 1943 stop_code = -1;
6de9cd9a
DN
1944 e = NULL;
1945
1946 if (gfc_match_eos () != MATCH_YES)
1947 {
8a8f7eca 1948 m = gfc_match_small_literal_int (&stop_code, &cnt);
6de9cd9a 1949 if (m == MATCH_ERROR)
b251af97 1950 goto cleanup;
6de9cd9a 1951
8a8f7eca
SK
1952 if (m == MATCH_YES && cnt > 5)
1953 {
1954 gfc_error ("Too many digits in STOP code at %C");
1955 goto cleanup;
1956 }
6de9cd9a
DN
1957
1958 if (m == MATCH_NO)
b251af97
SK
1959 {
1960 /* Try a character constant. */
1961 m = gfc_match_expr (&e);
1962 if (m == MATCH_ERROR)
1963 goto cleanup;
1964 if (m == MATCH_NO)
1965 goto syntax;
1966 if (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1967 goto syntax;
1968 }
6de9cd9a
DN
1969
1970 if (gfc_match_eos () != MATCH_YES)
b251af97 1971 goto syntax;
6de9cd9a
DN
1972 }
1973
1974 if (gfc_pure (NULL))
1975 {
1976 gfc_error ("%s statement not allowed in PURE procedure at %C",
b251af97 1977 gfc_ascii_statement (st));
6de9cd9a
DN
1978 goto cleanup;
1979 }
1980
1981 new_st.op = st == ST_STOP ? EXEC_STOP : EXEC_PAUSE;
a513927a 1982 new_st.expr1 = e;
6de9cd9a
DN
1983 new_st.ext.stop_code = stop_code;
1984
1985 return MATCH_YES;
1986
1987syntax:
1988 gfc_syntax_error (st);
1989
1990cleanup:
1991
1992 gfc_free_expr (e);
1993 return MATCH_ERROR;
1994}
1995
66e4ab31 1996
6de9cd9a
DN
1997/* Match the (deprecated) PAUSE statement. */
1998
1999match
2000gfc_match_pause (void)
2001{
2002 match m;
2003
2004 m = gfc_match_stopcode (ST_PAUSE);
2005 if (m == MATCH_YES)
2006 {
79e7840d
JD
2007 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: PAUSE statement"
2008 " at %C")
6de9cd9a
DN
2009 == FAILURE)
2010 m = MATCH_ERROR;
2011 }
2012 return m;
2013}
2014
2015
2016/* Match the STOP statement. */
2017
2018match
2019gfc_match_stop (void)
2020{
2021 return gfc_match_stopcode (ST_STOP);
2022}
2023
2024
2025/* Match a CONTINUE statement. */
2026
2027match
2028gfc_match_continue (void)
2029{
6de9cd9a
DN
2030 if (gfc_match_eos () != MATCH_YES)
2031 {
2032 gfc_syntax_error (ST_CONTINUE);
2033 return MATCH_ERROR;
2034 }
2035
2036 new_st.op = EXEC_CONTINUE;
2037 return MATCH_YES;
2038}
2039
2040
2041/* Match the (deprecated) ASSIGN statement. */
2042
2043match
2044gfc_match_assign (void)
2045{
2046 gfc_expr *expr;
2047 gfc_st_label *label;
2048
2049 if (gfc_match (" %l", &label) == MATCH_YES)
2050 {
2051 if (gfc_reference_st_label (label, ST_LABEL_UNKNOWN) == FAILURE)
b251af97 2052 return MATCH_ERROR;
6de9cd9a 2053 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
b251af97 2054 {
79e7840d 2055 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGN "
b251af97 2056 "statement at %C")
6de9cd9a
DN
2057 == FAILURE)
2058 return MATCH_ERROR;
2059
b251af97 2060 expr->symtree->n.sym->attr.assign = 1;
6de9cd9a 2061
b251af97 2062 new_st.op = EXEC_LABEL_ASSIGN;
79bd1948 2063 new_st.label1 = label;
a513927a 2064 new_st.expr1 = expr;
b251af97
SK
2065 return MATCH_YES;
2066 }
6de9cd9a
DN
2067 }
2068 return MATCH_NO;
2069}
2070
2071
2072/* Match the GO TO statement. As a computed GOTO statement is
2073 matched, it is transformed into an equivalent SELECT block. No
2074 tree is necessary, and the resulting jumps-to-jumps are
2075 specifically optimized away by the back end. */
2076
2077match
2078gfc_match_goto (void)
2079{
2080 gfc_code *head, *tail;
2081 gfc_expr *expr;
2082 gfc_case *cp;
2083 gfc_st_label *label;
2084 int i;
2085 match m;
2086
2087 if (gfc_match (" %l%t", &label) == MATCH_YES)
2088 {
2089 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2090 return MATCH_ERROR;
2091
2092 new_st.op = EXEC_GOTO;
79bd1948 2093 new_st.label1 = label;
6de9cd9a
DN
2094 return MATCH_YES;
2095 }
2096
2097 /* The assigned GO TO statement. */
2098
2099 if (gfc_match_variable (&expr, 0) == MATCH_YES)
2100 {
79e7840d 2101 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: Assigned GOTO "
b251af97 2102 "statement at %C")
6de9cd9a
DN
2103 == FAILURE)
2104 return MATCH_ERROR;
2105
6de9cd9a 2106 new_st.op = EXEC_GOTO;
a513927a 2107 new_st.expr1 = expr;
6de9cd9a
DN
2108
2109 if (gfc_match_eos () == MATCH_YES)
2110 return MATCH_YES;
2111
2112 /* Match label list. */
2113 gfc_match_char (',');
2114 if (gfc_match_char ('(') != MATCH_YES)
2115 {
2116 gfc_syntax_error (ST_GOTO);
2117 return MATCH_ERROR;
2118 }
2119 head = tail = NULL;
2120
2121 do
2122 {
a34a91f0 2123 m = gfc_match_st_label (&label);
6de9cd9a
DN
2124 if (m != MATCH_YES)
2125 goto syntax;
2126
2127 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2128 goto cleanup;
2129
2130 if (head == NULL)
2131 head = tail = gfc_get_code ();
2132 else
2133 {
2134 tail->block = gfc_get_code ();
2135 tail = tail->block;
2136 }
2137
79bd1948 2138 tail->label1 = label;
6de9cd9a
DN
2139 tail->op = EXEC_GOTO;
2140 }
2141 while (gfc_match_char (',') == MATCH_YES);
2142
2143 if (gfc_match (")%t") != MATCH_YES)
2144 goto syntax;
2145
2146 if (head == NULL)
2147 {
b251af97 2148 gfc_error ("Statement label list in GOTO at %C cannot be empty");
6de9cd9a
DN
2149 goto syntax;
2150 }
2151 new_st.block = head;
2152
2153 return MATCH_YES;
2154 }
2155
2156 /* Last chance is a computed GO TO statement. */
2157 if (gfc_match_char ('(') != MATCH_YES)
2158 {
2159 gfc_syntax_error (ST_GOTO);
2160 return MATCH_ERROR;
2161 }
2162
2163 head = tail = NULL;
2164 i = 1;
2165
2166 do
2167 {
a34a91f0 2168 m = gfc_match_st_label (&label);
6de9cd9a
DN
2169 if (m != MATCH_YES)
2170 goto syntax;
2171
2172 if (gfc_reference_st_label (label, ST_LABEL_TARGET) == FAILURE)
2173 goto cleanup;
2174
2175 if (head == NULL)
2176 head = tail = gfc_get_code ();
2177 else
2178 {
2179 tail->block = gfc_get_code ();
2180 tail = tail->block;
2181 }
2182
2183 cp = gfc_get_case ();
2184 cp->low = cp->high = gfc_int_expr (i++);
2185
2186 tail->op = EXEC_SELECT;
2187 tail->ext.case_list = cp;
2188
2189 tail->next = gfc_get_code ();
2190 tail->next->op = EXEC_GOTO;
79bd1948 2191 tail->next->label1 = label;
6de9cd9a
DN
2192 }
2193 while (gfc_match_char (',') == MATCH_YES);
2194
2195 if (gfc_match_char (')') != MATCH_YES)
2196 goto syntax;
2197
2198 if (head == NULL)
2199 {
2200 gfc_error ("Statement label list in GOTO at %C cannot be empty");
2201 goto syntax;
2202 }
2203
2204 /* Get the rest of the statement. */
2205 gfc_match_char (',');
2206
2207 if (gfc_match (" %e%t", &expr) != MATCH_YES)
2208 goto syntax;
2209
e2ab8b09
JW
2210 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Computed GOTO "
2211 "at %C") == FAILURE)
2212 return MATCH_ERROR;
2213
6de9cd9a
DN
2214 /* At this point, a computed GOTO has been fully matched and an
2215 equivalent SELECT statement constructed. */
2216
2217 new_st.op = EXEC_SELECT;
a513927a 2218 new_st.expr1 = NULL;
6de9cd9a
DN
2219
2220 /* Hack: For a "real" SELECT, the expression is in expr. We put
2221 it in expr2 so we can distinguish then and produce the correct
2222 diagnostics. */
2223 new_st.expr2 = expr;
2224 new_st.block = head;
2225 return MATCH_YES;
2226
2227syntax:
2228 gfc_syntax_error (ST_GOTO);
2229cleanup:
2230 gfc_free_statements (head);
2231 return MATCH_ERROR;
2232}
2233
2234
2235/* Frees a list of gfc_alloc structures. */
2236
2237void
b251af97 2238gfc_free_alloc_list (gfc_alloc *p)
6de9cd9a
DN
2239{
2240 gfc_alloc *q;
2241
2242 for (; p; p = q)
2243 {
2244 q = p->next;
2245 gfc_free_expr (p->expr);
2246 gfc_free (p);
2247 }
2248}
2249
2250
cf2b3c22
TB
2251/* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2252 an accessible derived type. */
2253
2254static match
2255match_derived_type_spec (gfc_typespec *ts)
2256{
2257 locus old_locus;
2258 gfc_symbol *derived;
2259
2260 old_locus = gfc_current_locus;
2261
2262 if (gfc_match_symbol (&derived, 1) == MATCH_YES)
2263 {
2264 if (derived->attr.flavor == FL_DERIVED)
2265 {
2266 ts->type = BT_DERIVED;
2267 ts->u.derived = derived;
2268 return MATCH_YES;
2269 }
2270 else
2271 {
2272 /* Enforce F03:C476. */
2273 gfc_error ("'%s' at %L is not an accessible derived type",
2274 derived->name, &gfc_current_locus);
2275 return MATCH_ERROR;
2276 }
2277 }
2278
2279 gfc_current_locus = old_locus;
2280 return MATCH_NO;
2281}
2282
2283
e74f1cc8
JW
2284/* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2285 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2286 It only includes the intrinsic types from the Fortran 2003 standard
2287 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2288 the implicit_flag is not needed, so it was removed. Derived types are
2289 identified by their name alone. */
8234e5e0
SK
2290
2291static match
e74f1cc8 2292match_type_spec (gfc_typespec *ts)
8234e5e0
SK
2293{
2294 match m;
e74f1cc8 2295 locus old_locus;
8234e5e0
SK
2296
2297 gfc_clear_ts (ts);
e74f1cc8 2298 old_locus = gfc_current_locus;
8234e5e0
SK
2299
2300 if (gfc_match ("integer") == MATCH_YES)
2301 {
2302 ts->type = BT_INTEGER;
2303 ts->kind = gfc_default_integer_kind;
2304 goto kind_selector;
2305 }
2306
2307 if (gfc_match ("real") == MATCH_YES)
2308 {
2309 ts->type = BT_REAL;
2310 ts->kind = gfc_default_real_kind;
2311 goto kind_selector;
2312 }
2313
2314 if (gfc_match ("double precision") == MATCH_YES)
2315 {
2316 ts->type = BT_REAL;
2317 ts->kind = gfc_default_double_kind;
2318 return MATCH_YES;
2319 }
2320
2321 if (gfc_match ("complex") == MATCH_YES)
2322 {
2323 ts->type = BT_COMPLEX;
2324 ts->kind = gfc_default_complex_kind;
2325 goto kind_selector;
2326 }
2327
2328 if (gfc_match ("character") == MATCH_YES)
2329 {
2330 ts->type = BT_CHARACTER;
2331 goto char_selector;
2332 }
2333
2334 if (gfc_match ("logical") == MATCH_YES)
2335 {
2336 ts->type = BT_LOGICAL;
2337 ts->kind = gfc_default_logical_kind;
2338 goto kind_selector;
2339 }
2340
cf2b3c22
TB
2341 m = match_derived_type_spec (ts);
2342 if (m == MATCH_YES)
e74f1cc8 2343 {
cf2b3c22
TB
2344 old_locus = gfc_current_locus;
2345 if (gfc_match (" :: ") != MATCH_YES)
2346 return MATCH_ERROR;
2347 gfc_current_locus = old_locus;
2348 /* Enfore F03:C401. */
2349 if (ts->u.derived->attr.abstract)
e74f1cc8 2350 {
cf2b3c22
TB
2351 gfc_error ("Derived type '%s' at %L may not be ABSTRACT",
2352 ts->u.derived->name, &old_locus);
2353 return MATCH_ERROR;
e74f1cc8 2354 }
cf2b3c22 2355 return MATCH_YES;
e74f1cc8 2356 }
cf2b3c22
TB
2357 else if (m == MATCH_ERROR && gfc_match (" :: ") == MATCH_YES)
2358 return MATCH_ERROR;
e74f1cc8 2359
cf2b3c22
TB
2360 /* If a type is not matched, simply return MATCH_NO. */
2361 gfc_current_locus = old_locus;
8234e5e0
SK
2362 return MATCH_NO;
2363
2364kind_selector:
2365
2366 gfc_gobble_whitespace ();
2367 if (gfc_peek_ascii_char () == '*')
2368 {
2369 gfc_error ("Invalid type-spec at %C");
2370 return MATCH_ERROR;
2371 }
2372
2373 m = gfc_match_kind_spec (ts, false);
2374
2375 if (m == MATCH_NO)
2376 m = MATCH_YES; /* No kind specifier found. */
2377
2378 return m;
2379
2380char_selector:
2381
2382 m = gfc_match_char_spec (ts);
2383
2384 if (m == MATCH_NO)
2385 m = MATCH_YES; /* No kind specifier found. */
2386
2387 return m;
2388}
2389
2390
2391/* Used in gfc_match_allocate to check that a allocation-object and
2392 a source-expr are conformable. This does not catch all possible
2393 cases; in particular a runtime checking is needed. */
2394
2395static gfc_try
2396conformable_arrays (gfc_expr *e1, gfc_expr *e2)
2397{
2398 /* First compare rank. */
2399 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
2400 {
2401 gfc_error ("Source-expr at %L must be scalar or have the "
2402 "same rank as the allocate-object at %L",
2403 &e1->where, &e2->where);
2404 return FAILURE;
2405 }
2406
2407 if (e1->shape)
2408 {
2409 int i;
2410 mpz_t s;
2411
2412 mpz_init (s);
2413
2414 for (i = 0; i < e1->rank; i++)
2415 {
2416 if (e2->ref->u.ar.end[i])
2417 {
2418 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
2419 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
2420 mpz_add_ui (s, s, 1);
2421 }
2422 else
2423 {
2424 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
2425 }
2426
2427 if (mpz_cmp (e1->shape[i], s) != 0)
2428 {
2429 gfc_error ("Source-expr at %L and allocate-object at %L must "
2430 "have the same shape", &e1->where, &e2->where);
2431 mpz_clear (s);
2432 return FAILURE;
2433 }
2434 }
2435
2436 mpz_clear (s);
2437 }
2438
2439 return SUCCESS;
2440}
2441
2442
6de9cd9a
DN
2443/* Match an ALLOCATE statement. */
2444
2445match
2446gfc_match_allocate (void)
2447{
2448 gfc_alloc *head, *tail;
8234e5e0
SK
2449 gfc_expr *stat, *errmsg, *tmp, *source;
2450 gfc_typespec ts;
cf2b3c22 2451 gfc_symbol *sym;
6de9cd9a 2452 match m;
8234e5e0
SK
2453 locus old_locus;
2454 bool saw_stat, saw_errmsg, saw_source, b1, b2, b3;
6de9cd9a
DN
2455
2456 head = tail = NULL;
8234e5e0
SK
2457 stat = errmsg = source = tmp = NULL;
2458 saw_stat = saw_errmsg = saw_source = false;
6de9cd9a
DN
2459
2460 if (gfc_match_char ('(') != MATCH_YES)
2461 goto syntax;
2462
e74f1cc8 2463 /* Match an optional type-spec. */
8234e5e0 2464 old_locus = gfc_current_locus;
e74f1cc8 2465 m = match_type_spec (&ts);
8234e5e0
SK
2466 if (m == MATCH_ERROR)
2467 goto cleanup;
2468 else if (m == MATCH_NO)
2469 ts.type = BT_UNKNOWN;
2470 else
2471 {
2472 if (gfc_match (" :: ") == MATCH_YES)
2473 {
2474 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: typespec in "
2475 "ALLOCATE at %L", &old_locus) == FAILURE)
2476 goto cleanup;
2477 }
2478 else
2479 {
2480 ts.type = BT_UNKNOWN;
2481 gfc_current_locus = old_locus;
2482 }
2483 }
2484
6de9cd9a
DN
2485 for (;;)
2486 {
2487 if (head == NULL)
2488 head = tail = gfc_get_alloc ();
2489 else
2490 {
2491 tail->next = gfc_get_alloc ();
2492 tail = tail->next;
2493 }
2494
2495 m = gfc_match_variable (&tail->expr, 0);
2496 if (m == MATCH_NO)
2497 goto syntax;
2498 if (m == MATCH_ERROR)
2499 goto cleanup;
2500
c9583ed2
TS
2501 if (gfc_check_do_variable (tail->expr->symtree))
2502 goto cleanup;
2503
3759634f 2504 if (gfc_pure (NULL) && gfc_impure_variable (tail->expr->symtree->n.sym))
6de9cd9a 2505 {
3759634f 2506 gfc_error ("Bad allocate-object at %C for a PURE procedure");
6de9cd9a
DN
2507 goto cleanup;
2508 }
2509
8234e5e0
SK
2510 /* The ALLOCATE statement had an optional typespec. Check the
2511 constraints. */
2512 if (ts.type != BT_UNKNOWN)
2513 {
e74f1cc8
JW
2514 /* Enforce F03:C624. */
2515 if (!gfc_type_compatible (&tail->expr->ts, &ts))
8234e5e0
SK
2516 {
2517 gfc_error ("Type of entity at %L is type incompatible with "
2518 "typespec", &tail->expr->where);
2519 goto cleanup;
2520 }
2521
e74f1cc8 2522 /* Enforce F03:C627. */
8234e5e0
SK
2523 if (ts.kind != tail->expr->ts.kind)
2524 {
2525 gfc_error ("Kind type parameter for entity at %L differs from "
2526 "the kind type parameter of the typespec",
2527 &tail->expr->where);
2528 goto cleanup;
2529 }
2530 }
2531
3e978d30 2532 if (tail->expr->ts.type == BT_DERIVED)
bc21d315 2533 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
3e978d30 2534
3759634f 2535 /* FIXME: disable the checking on derived types and arrays. */
cf2b3c22 2536 sym = tail->expr->symtree->n.sym;
8234e5e0 2537 b1 = !(tail->expr->ref
3759634f 2538 && (tail->expr->ref->type == REF_COMPONENT
8234e5e0 2539 || tail->expr->ref->type == REF_ARRAY));
cf2b3c22
TB
2540 if (sym && sym->ts.type == BT_CLASS)
2541 b2 = !(sym->ts.u.derived->components->attr.allocatable
2542 || sym->ts.u.derived->components->attr.pointer);
2543 else
2544 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
2545 || sym->attr.proc_pointer);
2546 b3 = sym && sym->ns && sym->ns->proc_name
2547 && (sym->ns->proc_name->attr.allocatable
2548 || sym->ns->proc_name->attr.pointer
2549 || sym->ns->proc_name->attr.proc_pointer);
8234e5e0 2550 if (b1 && b2 && !b3)
3759634f
SK
2551 {
2552 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2553 "or an allocatable variable");
2554 goto cleanup;
2555 }
2556
6de9cd9a
DN
2557 if (gfc_match_char (',') != MATCH_YES)
2558 break;
2559
3759634f
SK
2560alloc_opt_list:
2561
2562 m = gfc_match (" stat = %v", &tmp);
6de9cd9a
DN
2563 if (m == MATCH_ERROR)
2564 goto cleanup;
2565 if (m == MATCH_YES)
3759634f 2566 {
8234e5e0 2567 /* Enforce C630. */
3759634f
SK
2568 if (saw_stat)
2569 {
2570 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3759634f
SK
2571 goto cleanup;
2572 }
2573
2574 stat = tmp;
2575 saw_stat = true;
2576
2577 if (gfc_check_do_variable (stat->symtree))
2578 goto cleanup;
2579
2580 if (gfc_match_char (',') == MATCH_YES)
2581 goto alloc_opt_list;
2582 }
2583
2584 m = gfc_match (" errmsg = %v", &tmp);
2585 if (m == MATCH_ERROR)
2586 goto cleanup;
2587 if (m == MATCH_YES)
2588 {
8234e5e0 2589 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG tag at %L",
3759634f
SK
2590 &tmp->where) == FAILURE)
2591 goto cleanup;
2592
8234e5e0 2593 /* Enforce C630. */
3759634f
SK
2594 if (saw_errmsg)
2595 {
2596 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3759634f
SK
2597 goto cleanup;
2598 }
2599
2600 errmsg = tmp;
2601 saw_errmsg = true;
2602
2603 if (gfc_match_char (',') == MATCH_YES)
2604 goto alloc_opt_list;
2605 }
2606
8234e5e0
SK
2607 m = gfc_match (" source = %e", &tmp);
2608 if (m == MATCH_ERROR)
2609 goto cleanup;
2610 if (m == MATCH_YES)
2611 {
2612 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SOURCE tag at %L",
2613 &tmp->where) == FAILURE)
2614 goto cleanup;
2615
2616 /* Enforce C630. */
2617 if (saw_source)
2618 {
2619 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
2620 goto cleanup;
2621 }
2622
2623 /* The next 3 conditionals check C631. */
2624 if (ts.type != BT_UNKNOWN)
2625 {
2626 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
2627 &tmp->where, &old_locus);
2628 goto cleanup;
2629 }
2630
2631 if (head->next)
2632 {
2633 gfc_error ("SOURCE tag at %L requires only a single entity in "
2634 "the allocation-list", &tmp->where);
2635 goto cleanup;
2636 }
2637
2638 gfc_resolve_expr (tmp);
2639
cf2b3c22 2640 if (!gfc_type_compatible (&head->expr->ts, &tmp->ts))
8234e5e0
SK
2641 {
2642 gfc_error ("Type of entity at %L is type incompatible with "
2643 "source-expr at %L", &head->expr->where, &tmp->where);
2644 goto cleanup;
2645 }
2646
2647 /* Check C633. */
2648 if (tmp->ts.kind != head->expr->ts.kind)
2649 {
2650 gfc_error ("The allocate-object at %L and the source-expr at %L "
2651 "shall have the same kind type parameter",
2652 &head->expr->where, &tmp->where);
2653 goto cleanup;
2654 }
2655
2656 /* Check C632 and restriction following Note 6.18. */
2657 if (tmp->rank > 0 && conformable_arrays (tmp, head->expr) == FAILURE)
2658 goto cleanup;
2659
2660 source = tmp;
2661 saw_source = true;
2662
2663 if (gfc_match_char (',') == MATCH_YES)
2664 goto alloc_opt_list;
2665 }
2666
3759634f
SK
2667 gfc_gobble_whitespace ();
2668
2669 if (gfc_peek_char () == ')')
2670 break;
6de9cd9a
DN
2671 }
2672
6de9cd9a
DN
2673
2674 if (gfc_match (" )%t") != MATCH_YES)
2675 goto syntax;
2676
2677 new_st.op = EXEC_ALLOCATE;
a513927a 2678 new_st.expr1 = stat;
3759634f 2679 new_st.expr2 = errmsg;
8234e5e0 2680 new_st.expr3 = source;
cf2b3c22
TB
2681 new_st.ext.alloc.list = head;
2682 new_st.ext.alloc.ts = ts;
6de9cd9a
DN
2683
2684 return MATCH_YES;
2685
2686syntax:
2687 gfc_syntax_error (ST_ALLOCATE);
2688
2689cleanup:
3759634f 2690 gfc_free_expr (errmsg);
8234e5e0 2691 gfc_free_expr (source);
6de9cd9a 2692 gfc_free_expr (stat);
8234e5e0 2693 gfc_free_expr (tmp);
6de9cd9a
DN
2694 gfc_free_alloc_list (head);
2695 return MATCH_ERROR;
2696}
2697
2698
2699/* Match a NULLIFY statement. A NULLIFY statement is transformed into
2700 a set of pointer assignments to intrinsic NULL(). */
2701
2702match
2703gfc_match_nullify (void)
2704{
2705 gfc_code *tail;
2706 gfc_expr *e, *p;
2707 match m;
2708
2709 tail = NULL;
2710
2711 if (gfc_match_char ('(') != MATCH_YES)
2712 goto syntax;
2713
2714 for (;;)
2715 {
2716 m = gfc_match_variable (&p, 0);
2717 if (m == MATCH_ERROR)
2718 goto cleanup;
2719 if (m == MATCH_NO)
2720 goto syntax;
2721
66e4ab31 2722 if (gfc_check_do_variable (p->symtree))
c9583ed2
TS
2723 goto cleanup;
2724
6de9cd9a
DN
2725 if (gfc_pure (NULL) && gfc_impure_variable (p->symtree->n.sym))
2726 {
b251af97 2727 gfc_error ("Illegal variable in NULLIFY at %C for a PURE procedure");
6de9cd9a
DN
2728 goto cleanup;
2729 }
2730
66e4ab31 2731 /* build ' => NULL() '. */
6de9cd9a 2732 e = gfc_get_expr ();
63645982 2733 e->where = gfc_current_locus;
6de9cd9a
DN
2734 e->expr_type = EXPR_NULL;
2735 e->ts.type = BT_UNKNOWN;
2736
66e4ab31 2737 /* Chain to list. */
6de9cd9a
DN
2738 if (tail == NULL)
2739 tail = &new_st;
2740 else
2741 {
2742 tail->next = gfc_get_code ();
2743 tail = tail->next;
2744 }
2745
2746 tail->op = EXEC_POINTER_ASSIGN;
a513927a 2747 tail->expr1 = p;
6de9cd9a
DN
2748 tail->expr2 = e;
2749
172b8799 2750 if (gfc_match (" )%t") == MATCH_YES)
6de9cd9a
DN
2751 break;
2752 if (gfc_match_char (',') != MATCH_YES)
2753 goto syntax;
2754 }
2755
2756 return MATCH_YES;
2757
2758syntax:
2759 gfc_syntax_error (ST_NULLIFY);
2760
2761cleanup:
43bad4be 2762 gfc_free_statements (new_st.next);
9a0bab0b
TB
2763 new_st.next = NULL;
2764 gfc_free_expr (new_st.expr1);
2765 new_st.expr1 = NULL;
2766 gfc_free_expr (new_st.expr2);
2767 new_st.expr2 = NULL;
6de9cd9a
DN
2768 return MATCH_ERROR;
2769}
2770
2771
2772/* Match a DEALLOCATE statement. */
2773
2774match
2775gfc_match_deallocate (void)
2776{
2777 gfc_alloc *head, *tail;
3759634f 2778 gfc_expr *stat, *errmsg, *tmp;
cf2b3c22 2779 gfc_symbol *sym;
6de9cd9a 2780 match m;
cf2b3c22 2781 bool saw_stat, saw_errmsg, b1, b2;
6de9cd9a
DN
2782
2783 head = tail = NULL;
3759634f
SK
2784 stat = errmsg = tmp = NULL;
2785 saw_stat = saw_errmsg = false;
6de9cd9a
DN
2786
2787 if (gfc_match_char ('(') != MATCH_YES)
2788 goto syntax;
2789
2790 for (;;)
2791 {
2792 if (head == NULL)
2793 head = tail = gfc_get_alloc ();
2794 else
2795 {
2796 tail->next = gfc_get_alloc ();
2797 tail = tail->next;
2798 }
2799
2800 m = gfc_match_variable (&tail->expr, 0);
2801 if (m == MATCH_ERROR)
2802 goto cleanup;
2803 if (m == MATCH_NO)
2804 goto syntax;
2805
c9583ed2
TS
2806 if (gfc_check_do_variable (tail->expr->symtree))
2807 goto cleanup;
2808
cf2b3c22
TB
2809 sym = tail->expr->symtree->n.sym;
2810
2811 if (gfc_pure (NULL) && gfc_impure_variable (sym))
6de9cd9a 2812 {
3759634f
SK
2813 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
2814 goto cleanup;
2815 }
2816
2817 /* FIXME: disable the checking on derived types. */
cf2b3c22 2818 b1 = !(tail->expr->ref
3759634f 2819 && (tail->expr->ref->type == REF_COMPONENT
cf2b3c22
TB
2820 || tail->expr->ref->type == REF_ARRAY));
2821 if (sym && sym->ts.type == BT_CLASS)
2822 b2 = !(sym->ts.u.derived->components->attr.allocatable
2823 || sym->ts.u.derived->components->attr.pointer);
2824 else
2825 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
2826 || sym->attr.proc_pointer);
2827 if (b1 && b2)
3759634f
SK
2828 {
2829 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
2830 "or an allocatable variable");
6de9cd9a
DN
2831 goto cleanup;
2832 }
2833
2834 if (gfc_match_char (',') != MATCH_YES)
2835 break;
2836
3759634f
SK
2837dealloc_opt_list:
2838
2839 m = gfc_match (" stat = %v", &tmp);
6de9cd9a
DN
2840 if (m == MATCH_ERROR)
2841 goto cleanup;
2842 if (m == MATCH_YES)
3759634f
SK
2843 {
2844 if (saw_stat)
2845 {
2846 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2847 gfc_free_expr (tmp);
2848 goto cleanup;
2849 }
2850
2851 stat = tmp;
2852 saw_stat = true;
2853
2854 if (gfc_check_do_variable (stat->symtree))
2855 goto cleanup;
6de9cd9a 2856
3759634f
SK
2857 if (gfc_match_char (',') == MATCH_YES)
2858 goto dealloc_opt_list;
2859 }
2860
2861 m = gfc_match (" errmsg = %v", &tmp);
2862 if (m == MATCH_ERROR)
2863 goto cleanup;
2864 if (m == MATCH_YES)
2865 {
2866 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ERRMSG at %L",
2867 &tmp->where) == FAILURE)
2868 goto cleanup;
2869
2870 if (saw_errmsg)
2871 {
2872 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2873 gfc_free_expr (tmp);
2874 goto cleanup;
2875 }
2876
2877 errmsg = tmp;
2878 saw_errmsg = true;
2879
2880 if (gfc_match_char (',') == MATCH_YES)
2881 goto dealloc_opt_list;
2882 }
2883
2884 gfc_gobble_whitespace ();
2885
2886 if (gfc_peek_char () == ')')
2887 break;
2888 }
6de9cd9a
DN
2889
2890 if (gfc_match (" )%t") != MATCH_YES)
2891 goto syntax;
2892
2893 new_st.op = EXEC_DEALLOCATE;
a513927a 2894 new_st.expr1 = stat;
3759634f 2895 new_st.expr2 = errmsg;
cf2b3c22 2896 new_st.ext.alloc.list = head;
6de9cd9a
DN
2897
2898 return MATCH_YES;
2899
2900syntax:
2901 gfc_syntax_error (ST_DEALLOCATE);
2902
2903cleanup:
3759634f 2904 gfc_free_expr (errmsg);
6de9cd9a
DN
2905 gfc_free_expr (stat);
2906 gfc_free_alloc_list (head);
2907 return MATCH_ERROR;
2908}
2909
2910
2911/* Match a RETURN statement. */
2912
2913match
2914gfc_match_return (void)
2915{
2916 gfc_expr *e;
2917 match m;
e08b5a75 2918 gfc_compile_state s;
6de9cd9a
DN
2919
2920 e = NULL;
2921 if (gfc_match_eos () == MATCH_YES)
2922 goto done;
2923
2924 if (gfc_find_state (COMP_SUBROUTINE) == FAILURE)
2925 {
2926 gfc_error ("Alternate RETURN statement at %C is only allowed within "
2927 "a SUBROUTINE");
2928 goto cleanup;
2929 }
2930
e2ab8b09
JW
2931 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: Alternate RETURN "
2932 "at %C") == FAILURE)
2933 return MATCH_ERROR;
2934
7f42f27f
TS
2935 if (gfc_current_form == FORM_FREE)
2936 {
2937 /* The following are valid, so we can't require a blank after the
b251af97
SK
2938 RETURN keyword:
2939 return+1
2940 return(1) */
8fc541d3 2941 char c = gfc_peek_ascii_char ();
7f42f27f 2942 if (ISALPHA (c) || ISDIGIT (c))
b251af97 2943 return MATCH_NO;
7f42f27f
TS
2944 }
2945
2946 m = gfc_match (" %e%t", &e);
6de9cd9a
DN
2947 if (m == MATCH_YES)
2948 goto done;
2949 if (m == MATCH_ERROR)
2950 goto cleanup;
2951
2952 gfc_syntax_error (ST_RETURN);
2953
2954cleanup:
2955 gfc_free_expr (e);
2956 return MATCH_ERROR;
2957
2958done:
7f42f27f
TS
2959 gfc_enclosing_unit (&s);
2960 if (s == COMP_PROGRAM
2961 && gfc_notify_std (GFC_STD_GNU, "Extension: RETURN statement in "
b251af97 2962 "main program at %C") == FAILURE)
7f42f27f
TS
2963 return MATCH_ERROR;
2964
6de9cd9a 2965 new_st.op = EXEC_RETURN;
a513927a 2966 new_st.expr1 = e;
6de9cd9a
DN
2967
2968 return MATCH_YES;
2969}
2970
2971
8e1f752a
DK
2972/* Match the call of a type-bound procedure, if CALL%var has already been
2973 matched and var found to be a derived-type variable. */
2974
2975static match
2976match_typebound_call (gfc_symtree* varst)
2977{
2978 gfc_symbol* var;
2979 gfc_expr* base;
2980 match m;
2981
2982 var = varst->n.sym;
2983
2984 base = gfc_get_expr ();
2985 base->expr_type = EXPR_VARIABLE;
2986 base->symtree = varst;
2987 base->where = gfc_current_locus;
e157f736 2988 gfc_set_sym_referenced (varst->n.sym);
8e1f752a 2989
713485cc 2990 m = gfc_match_varspec (base, 0, true, true);
8e1f752a
DK
2991 if (m == MATCH_NO)
2992 gfc_error ("Expected component reference at %C");
2993 if (m != MATCH_YES)
2994 return MATCH_ERROR;
2995
2996 if (gfc_match_eos () != MATCH_YES)
2997 {
2998 gfc_error ("Junk after CALL at %C");
2999 return MATCH_ERROR;
3000 }
3001
713485cc
JW
3002 if (base->expr_type == EXPR_COMPCALL)
3003 new_st.op = EXEC_COMPCALL;
3004 else if (base->expr_type == EXPR_PPC)
3005 new_st.op = EXEC_CALL_PPC;
3006 else
8e1f752a 3007 {
713485cc
JW
3008 gfc_error ("Expected type-bound procedure or procedure pointer component "
3009 "at %C");
8e1f752a
DK
3010 return MATCH_ERROR;
3011 }
a513927a 3012 new_st.expr1 = base;
8e1f752a
DK
3013
3014 return MATCH_YES;
3015}
3016
3017
6de9cd9a
DN
3018/* Match a CALL statement. The tricky part here are possible
3019 alternate return specifiers. We handle these by having all
3020 "subroutines" actually return an integer via a register that gives
3021 the return number. If the call specifies alternate returns, we
3022 generate code for a SELECT statement whose case clauses contain
3023 GOTOs to the various labels. */
3024
3025match
3026gfc_match_call (void)
3027{
3028 char name[GFC_MAX_SYMBOL_LEN + 1];
3029 gfc_actual_arglist *a, *arglist;
3030 gfc_case *new_case;
3031 gfc_symbol *sym;
3032 gfc_symtree *st;
3033 gfc_code *c;
3034 match m;
3035 int i;
3036
3037 arglist = NULL;
3038
3039 m = gfc_match ("% %n", name);
3040 if (m == MATCH_NO)
3041 goto syntax;
3042 if (m != MATCH_YES)
3043 return m;
3044
3045 if (gfc_get_ha_sym_tree (name, &st))
3046 return MATCH_ERROR;
3047
3048 sym = st->n.sym;
6de9cd9a 3049
8e1f752a
DK
3050 /* If this is a variable of derived-type, it probably starts a type-bound
3051 procedure call. */
cf2b3c22
TB
3052 if (sym->attr.flavor != FL_PROCEDURE
3053 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
8e1f752a
DK
3054 return match_typebound_call (st);
3055
334e912a
PT
3056 /* If it does not seem to be callable (include functions so that the
3057 right association is made. They are thrown out in resolution.)
3058 ... */
6291f3ba 3059 if (!sym->attr.generic
334e912a
PT
3060 && !sym->attr.subroutine
3061 && !sym->attr.function)
6291f3ba 3062 {
eda0ed25
PT
3063 if (!(sym->attr.external && !sym->attr.referenced))
3064 {
3065 /* ...create a symbol in this scope... */
3066 if (sym->ns != gfc_current_ns
08a6b8e0 3067 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
eda0ed25 3068 return MATCH_ERROR;
6de9cd9a 3069
eda0ed25
PT
3070 if (sym != st->n.sym)
3071 sym = st->n.sym;
3072 }
8de10a62 3073
6291f3ba
PT
3074 /* ...and then to try to make the symbol into a subroutine. */
3075 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3076 return MATCH_ERROR;
3077 }
8de10a62
PT
3078
3079 gfc_set_sym_referenced (sym);
3080
6de9cd9a
DN
3081 if (gfc_match_eos () != MATCH_YES)
3082 {
3083 m = gfc_match_actual_arglist (1, &arglist);
3084 if (m == MATCH_NO)
3085 goto syntax;
3086 if (m == MATCH_ERROR)
3087 goto cleanup;
3088
3089 if (gfc_match_eos () != MATCH_YES)
3090 goto syntax;
3091 }
3092
3093 /* If any alternate return labels were found, construct a SELECT
3094 statement that will jump to the right place. */
3095
3096 i = 0;
3097 for (a = arglist; a; a = a->next)
3098 if (a->expr == NULL)
66e4ab31 3099 i = 1;
6de9cd9a
DN
3100
3101 if (i)
3102 {
3103 gfc_symtree *select_st;
3104 gfc_symbol *select_sym;
3105 char name[GFC_MAX_SYMBOL_LEN + 1];
3106
3107 new_st.next = c = gfc_get_code ();
3108 c->op = EXEC_SELECT;
b251af97 3109 sprintf (name, "_result_%s", sym->name);
66e4ab31 3110 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
6de9cd9a
DN
3111
3112 select_sym = select_st->n.sym;
3113 select_sym->ts.type = BT_INTEGER;
9d64df18 3114 select_sym->ts.kind = gfc_default_integer_kind;
6de9cd9a 3115 gfc_set_sym_referenced (select_sym);
a513927a
SK
3116 c->expr1 = gfc_get_expr ();
3117 c->expr1->expr_type = EXPR_VARIABLE;
3118 c->expr1->symtree = select_st;
3119 c->expr1->ts = select_sym->ts;
3120 c->expr1->where = gfc_current_locus;
6de9cd9a
DN
3121
3122 i = 0;
3123 for (a = arglist; a; a = a->next)
3124 {
3125 if (a->expr != NULL)
3126 continue;
3127
3128 if (gfc_reference_st_label (a->label, ST_LABEL_TARGET) == FAILURE)
3129 continue;
3130
3131 i++;
3132
3133 c->block = gfc_get_code ();
3134 c = c->block;
3135 c->op = EXEC_SELECT;
3136
3137 new_case = gfc_get_case ();
3138 new_case->high = new_case->low = gfc_int_expr (i);
3139 c->ext.case_list = new_case;
3140
3141 c->next = gfc_get_code ();
3142 c->next->op = EXEC_GOTO;
79bd1948 3143 c->next->label1 = a->label;
6de9cd9a
DN
3144 }
3145 }
3146
3147 new_st.op = EXEC_CALL;
3148 new_st.symtree = st;
3149 new_st.ext.actual = arglist;
3150
3151 return MATCH_YES;
3152
3153syntax:
3154 gfc_syntax_error (ST_CALL);
3155
3156cleanup:
3157 gfc_free_actual_arglist (arglist);
3158 return MATCH_ERROR;
3159}
3160
3161
9056bd70 3162/* Given a name, return a pointer to the common head structure,
13795658 3163 creating it if it does not exist. If FROM_MODULE is nonzero, we
53814b8f
TS
3164 mangle the name so that it doesn't interfere with commons defined
3165 in the using namespace.
9056bd70
TS
3166 TODO: Add to global symbol tree. */
3167
3168gfc_common_head *
53814b8f 3169gfc_get_common (const char *name, int from_module)
9056bd70
TS
3170{
3171 gfc_symtree *st;
53814b8f 3172 static int serial = 0;
b251af97 3173 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
9056bd70 3174
53814b8f
TS
3175 if (from_module)
3176 {
3177 /* A use associated common block is only needed to correctly layout
3178 the variables it contains. */
b251af97 3179 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
53814b8f
TS
3180 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
3181 }
3182 else
3183 {
3184 st = gfc_find_symtree (gfc_current_ns->common_root, name);
3185
3186 if (st == NULL)
3187 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
3188 }
9056bd70
TS
3189
3190 if (st->n.common == NULL)
3191 {
3192 st->n.common = gfc_get_common_head ();
3193 st->n.common->where = gfc_current_locus;
53814b8f 3194 strcpy (st->n.common->name, name);
9056bd70
TS
3195 }
3196
3197 return st->n.common;
3198}
3199
3200
6de9cd9a
DN
3201/* Match a common block name. */
3202
a8b3b0b6 3203match match_common_name (char *name)
6de9cd9a
DN
3204{
3205 match m;
3206
3207 if (gfc_match_char ('/') == MATCH_NO)
9056bd70
TS
3208 {
3209 name[0] = '\0';
3210 return MATCH_YES;
3211 }
6de9cd9a
DN
3212
3213 if (gfc_match_char ('/') == MATCH_YES)
3214 {
9056bd70 3215 name[0] = '\0';
6de9cd9a
DN
3216 return MATCH_YES;
3217 }
3218
9056bd70 3219 m = gfc_match_name (name);
6de9cd9a
DN
3220
3221 if (m == MATCH_ERROR)
3222 return MATCH_ERROR;
3223 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
3224 return MATCH_YES;
3225
3226 gfc_error ("Syntax error in common block name at %C");
3227 return MATCH_ERROR;
3228}
3229
3230
3231/* Match a COMMON statement. */
3232
3233match
3234gfc_match_common (void)
3235{
30aabb86 3236 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
b251af97 3237 char name[GFC_MAX_SYMBOL_LEN + 1];
9056bd70 3238 gfc_common_head *t;
6de9cd9a 3239 gfc_array_spec *as;
b251af97 3240 gfc_equiv *e1, *e2;
6de9cd9a 3241 match m;
68ea355b 3242 gfc_gsymbol *gsym;
6de9cd9a 3243
9056bd70 3244 old_blank_common = gfc_current_ns->blank_common.head;
6de9cd9a
DN
3245 if (old_blank_common)
3246 {
3247 while (old_blank_common->common_next)
3248 old_blank_common = old_blank_common->common_next;
3249 }
3250
6de9cd9a
DN
3251 as = NULL;
3252
6de9cd9a
DN
3253 for (;;)
3254 {
9056bd70 3255 m = match_common_name (name);
6de9cd9a
DN
3256 if (m == MATCH_ERROR)
3257 goto cleanup;
3258
68ea355b
PT
3259 gsym = gfc_get_gsymbol (name);
3260 if (gsym->type != GSYM_UNKNOWN && gsym->type != GSYM_COMMON)
3261 {
b251af97
SK
3262 gfc_error ("Symbol '%s' at %C is already an external symbol that "
3263 "is not COMMON", name);
68ea355b
PT
3264 goto cleanup;
3265 }
3266
3267 if (gsym->type == GSYM_UNKNOWN)
3268 {
3269 gsym->type = GSYM_COMMON;
3270 gsym->where = gfc_current_locus;
3271 gsym->defined = 1;
3272 }
3273
3274 gsym->used = 1;
3275
9056bd70
TS
3276 if (name[0] == '\0')
3277 {
3278 t = &gfc_current_ns->blank_common;
3279 if (t->head == NULL)
3280 t->where = gfc_current_locus;
9056bd70 3281 }
6de9cd9a
DN
3282 else
3283 {
53814b8f 3284 t = gfc_get_common (name, 0);
6de9cd9a 3285 }
41433497 3286 head = &t->head;
6de9cd9a
DN
3287
3288 if (*head == NULL)
3289 tail = NULL;
3290 else
3291 {
3292 tail = *head;
3293 while (tail->common_next)
3294 tail = tail->common_next;
3295 }
3296
3297 /* Grab the list of symbols. */
3298 for (;;)
3299 {
3300 m = gfc_match_symbol (&sym, 0);
3301 if (m == MATCH_ERROR)
3302 goto cleanup;
3303 if (m == MATCH_NO)
3304 goto syntax;
3305
a8b3b0b6
CR
3306 /* Store a ref to the common block for error checking. */
3307 sym->common_block = t;
3308
3309 /* See if we know the current common block is bind(c), and if
3310 so, then see if we can check if the symbol is (which it'll
3311 need to be). This can happen if the bind(c) attr stmt was
3312 applied to the common block, and the variable(s) already
3313 defined, before declaring the common block. */
3314 if (t->is_bind_c == 1)
3315 {
3316 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
3317 {
3318 /* If we find an error, just print it and continue,
3319 cause it's just semantic, and we can see if there
3320 are more errors. */
3321 gfc_error_now ("Variable '%s' at %L in common block '%s' "
3322 "at %C must be declared with a C "
3323 "interoperable kind since common block "
3324 "'%s' is bind(c)",
3325 sym->name, &(sym->declared_at), t->name,
3326 t->name);
3327 }
3328
3329 if (sym->attr.is_bind_c == 1)
3330 gfc_error_now ("Variable '%s' in common block "
3331 "'%s' at %C can not be bind(c) since "
3332 "it is not global", sym->name, t->name);
3333 }
3334
6de9cd9a
DN
3335 if (sym->attr.in_common)
3336 {
3337 gfc_error ("Symbol '%s' at %C is already in a COMMON block",
3338 sym->name);
3339 goto cleanup;
3340 }
3341
f69ab0e0
JD
3342 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
3343 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
3344 {
3345 if (gfc_notify_std (GFC_STD_GNU, "Initialized symbol '%s' at %C "
3346 "can only be COMMON in "
3347 "BLOCK DATA", sym->name)
3348 == FAILURE)
3349 goto cleanup;
3350 }
3351
231b2fcc 3352 if (gfc_add_in_common (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
3353 goto cleanup;
3354
6de9cd9a
DN
3355 if (tail != NULL)
3356 tail->common_next = sym;
3357 else
3358 *head = sym;
3359
3360 tail = sym;
3361
3362 /* Deal with an optional array specification after the
b251af97 3363 symbol name. */
6de9cd9a
DN
3364 m = gfc_match_array_spec (&as);
3365 if (m == MATCH_ERROR)
3366 goto cleanup;
3367
3368 if (m == MATCH_YES)
3369 {
3370 if (as->type != AS_EXPLICIT)
3371 {
b251af97
SK
3372 gfc_error ("Array specification for symbol '%s' in COMMON "
3373 "at %C must be explicit", sym->name);
6de9cd9a
DN
3374 goto cleanup;
3375 }
3376
231b2fcc 3377 if (gfc_add_dimension (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
3378 goto cleanup;
3379
3380 if (sym->attr.pointer)
3381 {
b251af97
SK
3382 gfc_error ("Symbol '%s' in COMMON at %C cannot be a "
3383 "POINTER array", sym->name);
6de9cd9a
DN
3384 goto cleanup;
3385 }
3386
3387 sym->as = as;
3388 as = NULL;
30aabb86
PT
3389
3390 }
3391
3392 sym->common_head = t;
3393
3394 /* Check to see if the symbol is already in an equivalence group.
3395 If it is, set the other members as being in common. */
3396 if (sym->attr.in_equivalence)
3397 {
3398 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
b251af97
SK
3399 {
3400 for (e2 = e1; e2; e2 = e2->eq)
3401 if (e2->expr->symtree->n.sym == sym)
30aabb86
PT
3402 goto equiv_found;
3403
3404 continue;
3405
3406 equiv_found:
3407
3408 for (e2 = e1; e2; e2 = e2->eq)
3409 {
3410 other = e2->expr->symtree->n.sym;
3411 if (other->common_head
b251af97 3412 && other->common_head != sym->common_head)
30aabb86
PT
3413 {
3414 gfc_error ("Symbol '%s', in COMMON block '%s' at "
3415 "%C is being indirectly equivalenced to "
3416 "another COMMON block '%s'",
b251af97 3417 sym->name, sym->common_head->name,
30aabb86
PT
3418 other->common_head->name);
3419 goto cleanup;
3420 }
3421 other->attr.in_common = 1;
3422 other->common_head = t;
3423 }
3424 }
6de9cd9a
DN
3425 }
3426
30aabb86 3427
23acf4d4 3428 gfc_gobble_whitespace ();
6de9cd9a
DN
3429 if (gfc_match_eos () == MATCH_YES)
3430 goto done;
8fc541d3 3431 if (gfc_peek_ascii_char () == '/')
6de9cd9a
DN
3432 break;
3433 if (gfc_match_char (',') != MATCH_YES)
3434 goto syntax;
23acf4d4 3435 gfc_gobble_whitespace ();
8fc541d3 3436 if (gfc_peek_ascii_char () == '/')
6de9cd9a
DN
3437 break;
3438 }
3439 }
3440
3441done:
3442 return MATCH_YES;
3443
3444syntax:
3445 gfc_syntax_error (ST_COMMON);
3446
3447cleanup:
3448 if (old_blank_common)
3449 old_blank_common->common_next = NULL;
3450 else
9056bd70 3451 gfc_current_ns->blank_common.head = NULL;
6de9cd9a
DN
3452 gfc_free_array_spec (as);
3453 return MATCH_ERROR;
3454}
3455
3456
3457/* Match a BLOCK DATA program unit. */
3458
3459match
3460gfc_match_block_data (void)
3461{
3462 char name[GFC_MAX_SYMBOL_LEN + 1];
3463 gfc_symbol *sym;
3464 match m;
3465
3466 if (gfc_match_eos () == MATCH_YES)
3467 {
3468 gfc_new_block = NULL;
3469 return MATCH_YES;
3470 }
3471
e08b5a75 3472 m = gfc_match ("% %n%t", name);
6de9cd9a
DN
3473 if (m != MATCH_YES)
3474 return MATCH_ERROR;
3475
3476 if (gfc_get_symbol (name, NULL, &sym))
3477 return MATCH_ERROR;
3478
231b2fcc 3479 if (gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL) == FAILURE)
6de9cd9a
DN
3480 return MATCH_ERROR;
3481
3482 gfc_new_block = sym;
3483
3484 return MATCH_YES;
3485}
3486
3487
3488/* Free a namelist structure. */
3489
3490void
b251af97 3491gfc_free_namelist (gfc_namelist *name)
6de9cd9a
DN
3492{
3493 gfc_namelist *n;
3494
3495 for (; name; name = n)
3496 {
3497 n = name->next;
3498 gfc_free (name);
3499 }
3500}
3501
3502
3503/* Match a NAMELIST statement. */
3504
3505match
3506gfc_match_namelist (void)
3507{
3508 gfc_symbol *group_name, *sym;
3509 gfc_namelist *nl;
3510 match m, m2;
3511
3512 m = gfc_match (" / %s /", &group_name);
3513 if (m == MATCH_NO)
3514 goto syntax;
3515 if (m == MATCH_ERROR)
3516 goto error;
3517
3518 for (;;)
3519 {
3520 if (group_name->ts.type != BT_UNKNOWN)
3521 {
b251af97
SK
3522 gfc_error ("Namelist group name '%s' at %C already has a basic "
3523 "type of %s", group_name->name,
3524 gfc_typename (&group_name->ts));
6de9cd9a
DN
3525 return MATCH_ERROR;
3526 }
3527
e0e85e06 3528 if (group_name->attr.flavor == FL_NAMELIST
66e4ab31
SK
3529 && group_name->attr.use_assoc
3530 && gfc_notify_std (GFC_STD_GNU, "Namelist group name '%s' "
3531 "at %C already is USE associated and can"
3532 "not be respecified.", group_name->name)
3533 == FAILURE)
e0e85e06
PT
3534 return MATCH_ERROR;
3535
6de9cd9a 3536 if (group_name->attr.flavor != FL_NAMELIST
231b2fcc
TS
3537 && gfc_add_flavor (&group_name->attr, FL_NAMELIST,
3538 group_name->name, NULL) == FAILURE)
6de9cd9a
DN
3539 return MATCH_ERROR;
3540
3541 for (;;)
3542 {
3543 m = gfc_match_symbol (&sym, 1);
3544 if (m == MATCH_NO)
3545 goto syntax;
3546 if (m == MATCH_ERROR)
3547 goto error;
3548
3549 if (sym->attr.in_namelist == 0
231b2fcc 3550 && gfc_add_in_namelist (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
3551 goto error;
3552
cecc1235 3553 /* Use gfc_error_check here, rather than goto error, so that
e0e85e06
PT
3554 these are the only errors for the next two lines. */
3555 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
3556 {
17339e88 3557 gfc_error ("Assumed size array '%s' in namelist '%s' at "
b251af97 3558 "%C is not allowed", sym->name, group_name->name);
e0e85e06
PT
3559 gfc_error_check ();
3560 }
3561
bc21d315 3562 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl->length == NULL)
cecc1235
JD
3563 {
3564 gfc_error ("Assumed character length '%s' in namelist '%s' at "
3565 "%C is not allowed", sym->name, group_name->name);
3566 gfc_error_check ();
3567 }
3568
6de9cd9a
DN
3569 nl = gfc_get_namelist ();
3570 nl->sym = sym;
3e1cf500 3571 sym->refs++;
6de9cd9a
DN
3572
3573 if (group_name->namelist == NULL)
3574 group_name->namelist = group_name->namelist_tail = nl;
3575 else
3576 {
3577 group_name->namelist_tail->next = nl;
3578 group_name->namelist_tail = nl;
3579 }
3580
3581 if (gfc_match_eos () == MATCH_YES)
3582 goto done;
3583
3584 m = gfc_match_char (',');
3585
3586 if (gfc_match_char ('/') == MATCH_YES)
3587 {
3588 m2 = gfc_match (" %s /", &group_name);
3589 if (m2 == MATCH_YES)
3590 break;
3591 if (m2 == MATCH_ERROR)
3592 goto error;
3593 goto syntax;
3594 }
3595
3596 if (m != MATCH_YES)
3597 goto syntax;
3598 }
3599 }
3600
3601done:
3602 return MATCH_YES;
3603
3604syntax:
3605 gfc_syntax_error (ST_NAMELIST);
3606
3607error:
3608 return MATCH_ERROR;
3609}
3610
3611
3612/* Match a MODULE statement. */
3613
3614match
3615gfc_match_module (void)
3616{
3617 match m;
3618
3619 m = gfc_match (" %s%t", &gfc_new_block);
3620 if (m != MATCH_YES)
3621 return m;
3622
231b2fcc
TS
3623 if (gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
3624 gfc_new_block->name, NULL) == FAILURE)
6de9cd9a
DN
3625 return MATCH_ERROR;
3626
3627 return MATCH_YES;
3628}
3629
3630
3631/* Free equivalence sets and lists. Recursively is the easiest way to
3632 do this. */
3633
3634void
b251af97 3635gfc_free_equiv (gfc_equiv *eq)
6de9cd9a 3636{
6de9cd9a
DN
3637 if (eq == NULL)
3638 return;
3639
3640 gfc_free_equiv (eq->eq);
3641 gfc_free_equiv (eq->next);
6de9cd9a
DN
3642 gfc_free_expr (eq->expr);
3643 gfc_free (eq);
3644}
3645
3646
3647/* Match an EQUIVALENCE statement. */
3648
3649match
3650gfc_match_equivalence (void)
3651{
3652 gfc_equiv *eq, *set, *tail;
3653 gfc_ref *ref;
30aabb86 3654 gfc_symbol *sym;
6de9cd9a 3655 match m;
30aabb86
PT
3656 gfc_common_head *common_head = NULL;
3657 bool common_flag;
d0497a65 3658 int cnt;
6de9cd9a
DN
3659
3660 tail = NULL;
3661
3662 for (;;)
3663 {
3664 eq = gfc_get_equiv ();
3665 if (tail == NULL)
3666 tail = eq;
3667
3668 eq->next = gfc_current_ns->equiv;
3669 gfc_current_ns->equiv = eq;
3670
3671 if (gfc_match_char ('(') != MATCH_YES)
3672 goto syntax;
3673
3674 set = eq;
30aabb86 3675 common_flag = FALSE;
d0497a65 3676 cnt = 0;
6de9cd9a
DN
3677
3678 for (;;)
3679 {
30aabb86 3680 m = gfc_match_equiv_variable (&set->expr);
6de9cd9a
DN
3681 if (m == MATCH_ERROR)
3682 goto cleanup;
3683 if (m == MATCH_NO)
3684 goto syntax;
3685
d0497a65
SK
3686 /* count the number of objects. */
3687 cnt++;
3688
e8ec07e1
PT
3689 if (gfc_match_char ('%') == MATCH_YES)
3690 {
3691 gfc_error ("Derived type component %C is not a "
3692 "permitted EQUIVALENCE member");
3693 goto cleanup;
3694 }
3695
6de9cd9a
DN
3696 for (ref = set->expr->ref; ref; ref = ref->next)
3697 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3698 {
b251af97
SK
3699 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
3700 "be an array section");
6de9cd9a
DN
3701 goto cleanup;
3702 }
3703
e8ec07e1
PT
3704 sym = set->expr->symtree->n.sym;
3705
b251af97 3706 if (gfc_add_in_equivalence (&sym->attr, sym->name, NULL) == FAILURE)
e8ec07e1
PT
3707 goto cleanup;
3708
3709 if (sym->attr.in_common)
30aabb86
PT
3710 {
3711 common_flag = TRUE;
e8ec07e1 3712 common_head = sym->common_head;
30aabb86
PT
3713 }
3714
6de9cd9a
DN
3715 if (gfc_match_char (')') == MATCH_YES)
3716 break;
d0497a65 3717
6de9cd9a
DN
3718 if (gfc_match_char (',') != MATCH_YES)
3719 goto syntax;
3720
3721 set->eq = gfc_get_equiv ();
3722 set = set->eq;
3723 }
3724
d0497a65
SK
3725 if (cnt < 2)
3726 {
3727 gfc_error ("EQUIVALENCE at %C requires two or more objects");
3728 goto cleanup;
3729 }
3730
30aabb86
PT
3731 /* If one of the members of an equivalence is in common, then
3732 mark them all as being in common. Before doing this, check
3733 that members of the equivalence group are not in different
66e4ab31 3734 common blocks. */
30aabb86
PT
3735 if (common_flag)
3736 for (set = eq; set; set = set->eq)
3737 {
3738 sym = set->expr->symtree->n.sym;
3739 if (sym->common_head && sym->common_head != common_head)
3740 {
3741 gfc_error ("Attempt to indirectly overlap COMMON "
3742 "blocks %s and %s by EQUIVALENCE at %C",
b251af97 3743 sym->common_head->name, common_head->name);
30aabb86
PT
3744 goto cleanup;
3745 }
3746 sym->attr.in_common = 1;
3747 sym->common_head = common_head;
3748 }
3749
6de9cd9a
DN
3750 if (gfc_match_eos () == MATCH_YES)
3751 break;
3752 if (gfc_match_char (',') != MATCH_YES)
3753 goto syntax;
3754 }
3755
3756 return MATCH_YES;
3757
3758syntax:
3759 gfc_syntax_error (ST_EQUIVALENCE);
3760
3761cleanup:
3762 eq = tail->next;
3763 tail->next = NULL;
3764
3765 gfc_free_equiv (gfc_current_ns->equiv);
3766 gfc_current_ns->equiv = eq;
3767
3768 return MATCH_ERROR;
3769}
3770
b251af97 3771
4213f93b
PT
3772/* Check that a statement function is not recursive. This is done by looking
3773 for the statement function symbol(sym) by looking recursively through its
d68bd5a8
PT
3774 expression(e). If a reference to sym is found, true is returned.
3775 12.5.4 requires that any variable of function that is implicitly typed
3776 shall have that type confirmed by any subsequent type declaration. The
3777 implicit typing is conveniently done here. */
908a2235
PT
3778static bool
3779recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
d68bd5a8 3780
4213f93b 3781static bool
908a2235 3782check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
4213f93b 3783{
4213f93b
PT
3784
3785 if (e == NULL)
3786 return false;
3787
3788 switch (e->expr_type)
3789 {
3790 case EXPR_FUNCTION:
9081e356
PT
3791 if (e->symtree == NULL)
3792 return false;
3793
4213f93b
PT
3794 /* Check the name before testing for nested recursion! */
3795 if (sym->name == e->symtree->n.sym->name)
3796 return true;
3797
3798 /* Catch recursion via other statement functions. */
3799 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
b251af97
SK
3800 && e->symtree->n.sym->value
3801 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4213f93b
PT
3802 return true;
3803
d68bd5a8
PT
3804 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3805 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
3806
4213f93b
PT
3807 break;
3808
3809 case EXPR_VARIABLE:
9081e356 3810 if (e->symtree && sym->name == e->symtree->n.sym->name)
4213f93b 3811 return true;
d68bd5a8
PT
3812
3813 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
3814 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4213f93b
PT
3815 break;
3816
4213f93b
PT
3817 default:
3818 break;
3819 }
3820
908a2235
PT
3821 return false;
3822}
4213f93b 3823
4213f93b 3824
908a2235
PT
3825static bool
3826recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
3827{
3828 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
4213f93b
PT
3829}
3830
6de9cd9a
DN
3831
3832/* Match a statement function declaration. It is so easy to match
3833 non-statement function statements with a MATCH_ERROR as opposed to
3834 MATCH_NO that we suppress error message in most cases. */
3835
3836match
3837gfc_match_st_function (void)
3838{
3839 gfc_error_buf old_error;
3840 gfc_symbol *sym;
3841 gfc_expr *expr;
3842 match m;
3843
3844 m = gfc_match_symbol (&sym, 0);
3845 if (m != MATCH_YES)
3846 return m;
3847
3848 gfc_push_error (&old_error);
3849
231b2fcc
TS
3850 if (gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION,
3851 sym->name, NULL) == FAILURE)
6de9cd9a
DN
3852 goto undo_error;
3853
3854 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
3855 goto undo_error;
3856
3857 m = gfc_match (" = %e%t", &expr);
3858 if (m == MATCH_NO)
3859 goto undo_error;
d71b89ca
JJ
3860
3861 gfc_free_error (&old_error);
6de9cd9a
DN
3862 if (m == MATCH_ERROR)
3863 return m;
3864
4213f93b
PT
3865 if (recursive_stmt_fcn (expr, sym))
3866 {
b251af97 3867 gfc_error ("Statement function at %L is recursive", &expr->where);
4213f93b
PT
3868 return MATCH_ERROR;
3869 }
3870
6de9cd9a
DN
3871 sym->value = expr;
3872
e2ab8b09
JW
3873 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
3874 "Statement function at %C") == FAILURE)
3875 return MATCH_ERROR;
3876
6de9cd9a
DN
3877 return MATCH_YES;
3878
3879undo_error:
3880 gfc_pop_error (&old_error);
3881 return MATCH_NO;
3882}
3883
3884
6de9cd9a
DN
3885/***************** SELECT CASE subroutines ******************/
3886
3887/* Free a single case structure. */
3888
3889static void
b251af97 3890free_case (gfc_case *p)
6de9cd9a
DN
3891{
3892 if (p->low == p->high)
3893 p->high = NULL;
3894 gfc_free_expr (p->low);
3895 gfc_free_expr (p->high);
3896 gfc_free (p);
3897}
3898
3899
3900/* Free a list of case structures. */
3901
3902void
b251af97 3903gfc_free_case_list (gfc_case *p)
6de9cd9a
DN
3904{
3905 gfc_case *q;
3906
3907 for (; p; p = q)
3908 {
3909 q = p->next;
3910 free_case (p);
3911 }
3912}
3913
3914
3915/* Match a single case selector. */
3916
3917static match
b251af97 3918match_case_selector (gfc_case **cp)
6de9cd9a
DN
3919{
3920 gfc_case *c;
3921 match m;
3922
3923 c = gfc_get_case ();
63645982 3924 c->where = gfc_current_locus;
6de9cd9a
DN
3925
3926 if (gfc_match_char (':') == MATCH_YES)
3927 {
6ef42154 3928 m = gfc_match_init_expr (&c->high);
6de9cd9a
DN
3929 if (m == MATCH_NO)
3930 goto need_expr;
3931 if (m == MATCH_ERROR)
3932 goto cleanup;
3933 }
6de9cd9a
DN
3934 else
3935 {
6ef42154 3936 m = gfc_match_init_expr (&c->low);
6de9cd9a
DN
3937 if (m == MATCH_ERROR)
3938 goto cleanup;
3939 if (m == MATCH_NO)
3940 goto need_expr;
3941
3942 /* If we're not looking at a ':' now, make a range out of a single
f7b529fa 3943 target. Else get the upper bound for the case range. */
6de9cd9a
DN
3944 if (gfc_match_char (':') != MATCH_YES)
3945 c->high = c->low;
3946 else
3947 {
6ef42154 3948 m = gfc_match_init_expr (&c->high);
6de9cd9a
DN
3949 if (m == MATCH_ERROR)
3950 goto cleanup;
3951 /* MATCH_NO is fine. It's OK if nothing is there! */
3952 }
3953 }
3954
3955 *cp = c;
3956 return MATCH_YES;
3957
3958need_expr:
6ef42154 3959 gfc_error ("Expected initialization expression in CASE at %C");
6de9cd9a
DN
3960
3961cleanup:
3962 free_case (c);
3963 return MATCH_ERROR;
3964}
3965
3966
3967/* Match the end of a case statement. */
3968
3969static match
3970match_case_eos (void)
3971{
3972 char name[GFC_MAX_SYMBOL_LEN + 1];
3973 match m;
3974
3975 if (gfc_match_eos () == MATCH_YES)
3976 return MATCH_YES;
3977
d0bd09f6
TS
3978 /* If the case construct doesn't have a case-construct-name, we
3979 should have matched the EOS. */
3980 if (!gfc_current_block ())
cf2b3c22 3981 return MATCH_NO;
d0bd09f6 3982
6de9cd9a
DN
3983 gfc_gobble_whitespace ();
3984
3985 m = gfc_match_name (name);
3986 if (m != MATCH_YES)
3987 return m;
3988
3989 if (strcmp (name, gfc_current_block ()->name) != 0)
3990 {
cf2b3c22 3991 gfc_error ("Expected block name '%s' of SELECT construct at %C",
6de9cd9a
DN
3992 gfc_current_block ()->name);
3993 return MATCH_ERROR;
3994 }
3995
3996 return gfc_match_eos ();
3997}
3998
3999
4000/* Match a SELECT statement. */
4001
4002match
4003gfc_match_select (void)
4004{
4005 gfc_expr *expr;
4006 match m;
4007
4008 m = gfc_match_label ();
4009 if (m == MATCH_ERROR)
4010 return m;
4011
4012 m = gfc_match (" select case ( %e )%t", &expr);
4013 if (m != MATCH_YES)
4014 return m;
4015
4016 new_st.op = EXEC_SELECT;
a513927a 4017 new_st.expr1 = expr;
6de9cd9a
DN
4018
4019 return MATCH_YES;
4020}
4021
4022
7431bf06
JW
4023/* Push the current selector onto the SELECT TYPE stack. */
4024
4025static void
4026select_type_push (gfc_symbol *sel)
4027{
4028 gfc_select_type_stack *top = gfc_get_select_type_stack ();
4029 top->selector = sel;
4030 top->tmp = NULL;
4031 top->prev = select_type_stack;
4032
4033 select_type_stack = top;
4034}
4035
4036
4037/* Set the temporary for the current SELECT TYPE selector. */
4038
4039static void
4040select_type_set_tmp (gfc_typespec *ts)
4041{
4042 char name[GFC_MAX_SYMBOL_LEN];
4043 gfc_symtree *tmp;
4044
4045 sprintf (name, "tmp$%s", ts->u.derived->name);
4046 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
4047 tmp->n.sym->ts = *ts;
4048 tmp->n.sym->attr.referenced = 1;
4049 tmp->n.sym->attr.pointer = 1;
4050
4051 select_type_stack->tmp = tmp;
4052}
4053
4054
cf2b3c22
TB
4055/* Match a SELECT TYPE statement. */
4056
4057match
4058gfc_match_select_type (void)
4059{
93d76687 4060 gfc_expr *expr1, *expr2 = NULL;
cf2b3c22 4061 match m;
93d76687 4062 char name[GFC_MAX_SYMBOL_LEN];
cf2b3c22
TB
4063
4064 m = gfc_match_label ();
4065 if (m == MATCH_ERROR)
4066 return m;
4067
93d76687 4068 m = gfc_match (" select type ( ");
cf2b3c22
TB
4069 if (m != MATCH_YES)
4070 return m;
4071
93d76687
JW
4072 gfc_current_ns = gfc_build_block_ns (gfc_current_ns);
4073
4074 m = gfc_match (" %n => %e", name, &expr2);
cf2b3c22
TB
4075 if (m == MATCH_YES)
4076 {
93d76687
JW
4077 expr1 = gfc_get_expr();
4078 expr1->expr_type = EXPR_VARIABLE;
4079 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
4080 return MATCH_ERROR;
4081 expr1->symtree->n.sym->ts = expr2->ts;
4082 expr1->symtree->n.sym->attr.referenced = 1;
2e23972e 4083 expr1->symtree->n.sym->attr.class_ok = 1;
93d76687
JW
4084 }
4085 else
4086 {
4087 m = gfc_match (" %e ", &expr1);
4088 if (m != MATCH_YES)
4089 return m;
cf2b3c22
TB
4090 }
4091
4092 m = gfc_match (" )%t");
4093 if (m != MATCH_YES)
4094 return m;
4095
93d76687
JW
4096 /* Check for F03:C811. */
4097 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE || expr1->ref != NULL))
cf2b3c22 4098 {
93d76687
JW
4099 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
4100 "use associate-name=>");
cf2b3c22
TB
4101 return MATCH_ERROR;
4102 }
4103
4104 /* Check for F03:C813. */
93d76687 4105 if (expr1->ts.type != BT_CLASS && !(expr2 && expr2->ts.type == BT_CLASS))
cf2b3c22
TB
4106 {
4107 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
4108 "at %C");
4109 return MATCH_ERROR;
4110 }
4111
4112 new_st.op = EXEC_SELECT_TYPE;
93d76687
JW
4113 new_st.expr1 = expr1;
4114 new_st.expr2 = expr2;
4115 new_st.ext.ns = gfc_current_ns;
cf2b3c22 4116
7431bf06 4117 select_type_push (expr1->symtree->n.sym);
cf2b3c22
TB
4118
4119 return MATCH_YES;
4120}
4121
4122
6de9cd9a
DN
4123/* Match a CASE statement. */
4124
4125match
4126gfc_match_case (void)
4127{
4128 gfc_case *c, *head, *tail;
4129 match m;
4130
4131 head = tail = NULL;
4132
4133 if (gfc_current_state () != COMP_SELECT)
4134 {
4135 gfc_error ("Unexpected CASE statement at %C");
4136 return MATCH_ERROR;
4137 }
4138
4139 if (gfc_match ("% default") == MATCH_YES)
4140 {
4141 m = match_case_eos ();
4142 if (m == MATCH_NO)
4143 goto syntax;
4144 if (m == MATCH_ERROR)
4145 goto cleanup;
4146
4147 new_st.op = EXEC_SELECT;
4148 c = gfc_get_case ();
63645982 4149 c->where = gfc_current_locus;
6de9cd9a
DN
4150 new_st.ext.case_list = c;
4151 return MATCH_YES;
4152 }
4153
4154 if (gfc_match_char ('(') != MATCH_YES)
4155 goto syntax;
4156
4157 for (;;)
4158 {
4159 if (match_case_selector (&c) == MATCH_ERROR)
4160 goto cleanup;
4161
4162 if (head == NULL)
4163 head = c;
4164 else
4165 tail->next = c;
4166
4167 tail = c;
4168
4169 if (gfc_match_char (')') == MATCH_YES)
4170 break;
4171 if (gfc_match_char (',') != MATCH_YES)
4172 goto syntax;
4173 }
4174
4175 m = match_case_eos ();
4176 if (m == MATCH_NO)
4177 goto syntax;
4178 if (m == MATCH_ERROR)
4179 goto cleanup;
4180
4181 new_st.op = EXEC_SELECT;
4182 new_st.ext.case_list = head;
4183
4184 return MATCH_YES;
4185
4186syntax:
cf2b3c22 4187 gfc_error ("Syntax error in CASE specification at %C");
6de9cd9a
DN
4188
4189cleanup:
4190 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
4191 return MATCH_ERROR;
4192}
4193
cf2b3c22
TB
4194
4195/* Match a TYPE IS statement. */
4196
4197match
4198gfc_match_type_is (void)
4199{
4200 gfc_case *c = NULL;
4201 match m;
cf2b3c22
TB
4202
4203 if (gfc_current_state () != COMP_SELECT_TYPE)
4204 {
4205 gfc_error ("Unexpected TYPE IS statement at %C");
4206 return MATCH_ERROR;
4207 }
4208
4209 if (gfc_match_char ('(') != MATCH_YES)
4210 goto syntax;
4211
4212 c = gfc_get_case ();
4213 c->where = gfc_current_locus;
4214
4215 /* TODO: Once unlimited polymorphism is implemented, we will need to call
4216 match_type_spec here. */
4217 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
4218 goto cleanup;
4219
4220 if (gfc_match_char (')') != MATCH_YES)
4221 goto syntax;
4222
4223 m = match_case_eos ();
4224 if (m == MATCH_NO)
4225 goto syntax;
4226 if (m == MATCH_ERROR)
4227 goto cleanup;
4228
4229 new_st.op = EXEC_SELECT_TYPE;
4230 new_st.ext.case_list = c;
4231
4232 /* Create temporary variable. */
7431bf06 4233 select_type_set_tmp (&c->ts);
cf2b3c22
TB
4234
4235 return MATCH_YES;
4236
4237syntax:
4238 gfc_error ("Syntax error in TYPE IS specification at %C");
4239
4240cleanup:
4241 if (c != NULL)
4242 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
4243 return MATCH_ERROR;
4244}
4245
4246
4247/* Match a CLASS IS or CLASS DEFAULT statement. */
4248
4249match
4250gfc_match_class_is (void)
4251{
4252 gfc_case *c = NULL;
4253 match m;
4254
4255 if (gfc_current_state () != COMP_SELECT_TYPE)
4256 return MATCH_NO;
4257
4258 if (gfc_match ("% default") == MATCH_YES)
4259 {
4260 m = match_case_eos ();
4261 if (m == MATCH_NO)
4262 goto syntax;
4263 if (m == MATCH_ERROR)
4264 goto cleanup;
4265
4266 new_st.op = EXEC_SELECT_TYPE;
4267 c = gfc_get_case ();
4268 c->where = gfc_current_locus;
4269 c->ts.type = BT_UNKNOWN;
4270 new_st.ext.case_list = c;
4271 return MATCH_YES;
4272 }
4273
4274 m = gfc_match ("% is");
4275 if (m == MATCH_NO)
4276 goto syntax;
4277 if (m == MATCH_ERROR)
4278 goto cleanup;
4279
4280 if (gfc_match_char ('(') != MATCH_YES)
4281 goto syntax;
4282
4283 c = gfc_get_case ();
4284 c->where = gfc_current_locus;
4285
4286 if (match_derived_type_spec (&c->ts) == MATCH_ERROR)
4287 goto cleanup;
4288
4289 if (c->ts.type == BT_DERIVED)
4290 c->ts.type = BT_CLASS;
4291
4292 if (gfc_match_char (')') != MATCH_YES)
4293 goto syntax;
4294
4295 m = match_case_eos ();
4296 if (m == MATCH_NO)
4297 goto syntax;
4298 if (m == MATCH_ERROR)
4299 goto cleanup;
4300
4301 new_st.op = EXEC_SELECT_TYPE;
4302 new_st.ext.case_list = c;
4303
4304 gfc_error_now ("CLASS IS specification at %C is not yet supported");
4305
4306 return MATCH_YES;
4307
4308syntax:
4309 gfc_error ("Syntax error in CLASS IS specification at %C");
4310
4311cleanup:
4312 if (c != NULL)
4313 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
4314 return MATCH_ERROR;
4315}
4316
4317
6de9cd9a
DN
4318/********************* WHERE subroutines ********************/
4319
c874ae73
TS
4320/* Match the rest of a simple WHERE statement that follows an IF statement.
4321 */
4322
4323static match
4324match_simple_where (void)
4325{
4326 gfc_expr *expr;
4327 gfc_code *c;
4328 match m;
4329
4330 m = gfc_match (" ( %e )", &expr);
4331 if (m != MATCH_YES)
4332 return m;
4333
4334 m = gfc_match_assignment ();
4335 if (m == MATCH_NO)
4336 goto syntax;
4337 if (m == MATCH_ERROR)
4338 goto cleanup;
4339
4340 if (gfc_match_eos () != MATCH_YES)
4341 goto syntax;
4342
4343 c = gfc_get_code ();
4344
4345 c->op = EXEC_WHERE;
a513927a 4346 c->expr1 = expr;
c874ae73
TS
4347 c->next = gfc_get_code ();
4348
4349 *c->next = new_st;
4350 gfc_clear_new_st ();
4351
4352 new_st.op = EXEC_WHERE;
4353 new_st.block = c;
4354
4355 return MATCH_YES;
4356
4357syntax:
4358 gfc_syntax_error (ST_WHERE);
4359
4360cleanup:
4361 gfc_free_expr (expr);
4362 return MATCH_ERROR;
4363}
4364
66e4ab31 4365
6de9cd9a
DN
4366/* Match a WHERE statement. */
4367
4368match
b251af97 4369gfc_match_where (gfc_statement *st)
6de9cd9a
DN
4370{
4371 gfc_expr *expr;
4372 match m0, m;
4373 gfc_code *c;
4374
4375 m0 = gfc_match_label ();
4376 if (m0 == MATCH_ERROR)
4377 return m0;
4378
4379 m = gfc_match (" where ( %e )", &expr);
4380 if (m != MATCH_YES)
4381 return m;
4382
4383 if (gfc_match_eos () == MATCH_YES)
4384 {
4385 *st = ST_WHERE_BLOCK;
6de9cd9a 4386 new_st.op = EXEC_WHERE;
a513927a 4387 new_st.expr1 = expr;
6de9cd9a
DN
4388 return MATCH_YES;
4389 }
4390
4391 m = gfc_match_assignment ();
4392 if (m == MATCH_NO)
4393 gfc_syntax_error (ST_WHERE);
4394
4395 if (m != MATCH_YES)
4396 {
4397 gfc_free_expr (expr);
4398 return MATCH_ERROR;
4399 }
4400
4401 /* We've got a simple WHERE statement. */
4402 *st = ST_WHERE;
4403 c = gfc_get_code ();
4404
4405 c->op = EXEC_WHERE;
a513927a 4406 c->expr1 = expr;
6de9cd9a
DN
4407 c->next = gfc_get_code ();
4408
4409 *c->next = new_st;
4410 gfc_clear_new_st ();
4411
4412 new_st.op = EXEC_WHERE;
4413 new_st.block = c;
4414
4415 return MATCH_YES;
4416}
4417
4418
4419/* Match an ELSEWHERE statement. We leave behind a WHERE node in
4420 new_st if successful. */
4421
4422match
4423gfc_match_elsewhere (void)
4424{
4425 char name[GFC_MAX_SYMBOL_LEN + 1];
4426 gfc_expr *expr;
4427 match m;
4428
4429 if (gfc_current_state () != COMP_WHERE)
4430 {
4431 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
4432 return MATCH_ERROR;
4433 }
4434
4435 expr = NULL;
4436
4437 if (gfc_match_char ('(') == MATCH_YES)
4438 {
4439 m = gfc_match_expr (&expr);
4440 if (m == MATCH_NO)
4441 goto syntax;
4442 if (m == MATCH_ERROR)
4443 return MATCH_ERROR;
4444
4445 if (gfc_match_char (')') != MATCH_YES)
4446 goto syntax;
4447 }
4448
4449 if (gfc_match_eos () != MATCH_YES)
690af379
TS
4450 {
4451 /* Only makes sense if we have a where-construct-name. */
4452 if (!gfc_current_block ())
4453 {
4454 m = MATCH_ERROR;
4455 goto cleanup;
4456 }
66e4ab31 4457 /* Better be a name at this point. */
6de9cd9a
DN
4458 m = gfc_match_name (name);
4459 if (m == MATCH_NO)
4460 goto syntax;
4461 if (m == MATCH_ERROR)
4462 goto cleanup;
4463
4464 if (gfc_match_eos () != MATCH_YES)
4465 goto syntax;
4466
4467 if (strcmp (name, gfc_current_block ()->name) != 0)
4468 {
4469 gfc_error ("Label '%s' at %C doesn't match WHERE label '%s'",
4470 name, gfc_current_block ()->name);
4471 goto cleanup;
4472 }
4473 }
4474
4475 new_st.op = EXEC_WHERE;
a513927a 4476 new_st.expr1 = expr;
6de9cd9a
DN
4477 return MATCH_YES;
4478
4479syntax:
4480 gfc_syntax_error (ST_ELSEWHERE);
4481
4482cleanup:
4483 gfc_free_expr (expr);
4484 return MATCH_ERROR;
4485}
4486
4487
4488/******************** FORALL subroutines ********************/
4489
4490/* Free a list of FORALL iterators. */
4491
4492void
b251af97 4493gfc_free_forall_iterator (gfc_forall_iterator *iter)
6de9cd9a
DN
4494{
4495 gfc_forall_iterator *next;
4496
4497 while (iter)
4498 {
4499 next = iter->next;
6de9cd9a
DN
4500 gfc_free_expr (iter->var);
4501 gfc_free_expr (iter->start);
4502 gfc_free_expr (iter->end);
4503 gfc_free_expr (iter->stride);
6de9cd9a
DN
4504 gfc_free (iter);
4505 iter = next;
4506 }
4507}
4508
4509
4510/* Match an iterator as part of a FORALL statement. The format is:
4511
9bffa171
RS
4512 <var> = <start>:<end>[:<stride>]
4513
4514 On MATCH_NO, the caller tests for the possibility that there is a
4515 scalar mask expression. */
6de9cd9a
DN
4516
4517static match
b251af97 4518match_forall_iterator (gfc_forall_iterator **result)
6de9cd9a
DN
4519{
4520 gfc_forall_iterator *iter;
4521 locus where;
4522 match m;
4523
63645982 4524 where = gfc_current_locus;
ece3f663 4525 iter = XCNEW (gfc_forall_iterator);
6de9cd9a 4526
9bffa171 4527 m = gfc_match_expr (&iter->var);
6de9cd9a
DN
4528 if (m != MATCH_YES)
4529 goto cleanup;
4530
9bffa171 4531 if (gfc_match_char ('=') != MATCH_YES
66e4ab31 4532 || iter->var->expr_type != EXPR_VARIABLE)
6de9cd9a
DN
4533 {
4534 m = MATCH_NO;
4535 goto cleanup;
4536 }
4537
4538 m = gfc_match_expr (&iter->start);
29405f94 4539 if (m != MATCH_YES)
6de9cd9a
DN
4540 goto cleanup;
4541
4542 if (gfc_match_char (':') != MATCH_YES)
4543 goto syntax;
4544
4545 m = gfc_match_expr (&iter->end);
4546 if (m == MATCH_NO)
4547 goto syntax;
4548 if (m == MATCH_ERROR)
4549 goto cleanup;
4550
4551 if (gfc_match_char (':') == MATCH_NO)
4552 iter->stride = gfc_int_expr (1);
4553 else
4554 {
4555 m = gfc_match_expr (&iter->stride);
4556 if (m == MATCH_NO)
4557 goto syntax;
4558 if (m == MATCH_ERROR)
4559 goto cleanup;
4560 }
4561
31708dc6
RS
4562 /* Mark the iteration variable's symbol as used as a FORALL index. */
4563 iter->var->symtree->n.sym->forall_index = true;
4564
6de9cd9a
DN
4565 *result = iter;
4566 return MATCH_YES;
4567
4568syntax:
4569 gfc_error ("Syntax error in FORALL iterator at %C");
4570 m = MATCH_ERROR;
4571
4572cleanup:
d68bd5a8 4573
63645982 4574 gfc_current_locus = where;
6de9cd9a
DN
4575 gfc_free_forall_iterator (iter);
4576 return m;
4577}
4578
4579
c874ae73 4580/* Match the header of a FORALL statement. */
6de9cd9a 4581
c874ae73 4582static match
b251af97 4583match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
6de9cd9a 4584{
7b901ac4 4585 gfc_forall_iterator *head, *tail, *new_iter;
43bad4be 4586 gfc_expr *msk;
c874ae73 4587 match m;
6de9cd9a 4588
c874ae73 4589 gfc_gobble_whitespace ();
6de9cd9a 4590
c874ae73 4591 head = tail = NULL;
43bad4be 4592 msk = NULL;
6de9cd9a 4593
c874ae73
TS
4594 if (gfc_match_char ('(') != MATCH_YES)
4595 return MATCH_NO;
6de9cd9a 4596
7b901ac4 4597 m = match_forall_iterator (&new_iter);
6de9cd9a
DN
4598 if (m == MATCH_ERROR)
4599 goto cleanup;
4600 if (m == MATCH_NO)
4601 goto syntax;
4602
7b901ac4 4603 head = tail = new_iter;
6de9cd9a
DN
4604
4605 for (;;)
4606 {
4607 if (gfc_match_char (',') != MATCH_YES)
4608 break;
4609
7b901ac4 4610 m = match_forall_iterator (&new_iter);
6de9cd9a
DN
4611 if (m == MATCH_ERROR)
4612 goto cleanup;
43bad4be 4613
6de9cd9a
DN
4614 if (m == MATCH_YES)
4615 {
7b901ac4
KG
4616 tail->next = new_iter;
4617 tail = new_iter;
6de9cd9a
DN
4618 continue;
4619 }
4620
66e4ab31 4621 /* Have to have a mask expression. */
c874ae73 4622
43bad4be 4623 m = gfc_match_expr (&msk);
6de9cd9a
DN
4624 if (m == MATCH_NO)
4625 goto syntax;
4626 if (m == MATCH_ERROR)
4627 goto cleanup;
4628
4629 break;
4630 }
4631
4632 if (gfc_match_char (')') == MATCH_NO)
4633 goto syntax;
4634
c874ae73 4635 *phead = head;
43bad4be 4636 *mask = msk;
c874ae73
TS
4637 return MATCH_YES;
4638
4639syntax:
4640 gfc_syntax_error (ST_FORALL);
4641
4642cleanup:
43bad4be 4643 gfc_free_expr (msk);
c874ae73
TS
4644 gfc_free_forall_iterator (head);
4645
4646 return MATCH_ERROR;
4647}
4648
b251af97
SK
4649/* Match the rest of a simple FORALL statement that follows an
4650 IF statement. */
c874ae73
TS
4651
4652static match
4653match_simple_forall (void)
4654{
4655 gfc_forall_iterator *head;
4656 gfc_expr *mask;
4657 gfc_code *c;
4658 match m;
4659
4660 mask = NULL;
4661 head = NULL;
4662 c = NULL;
4663
4664 m = match_forall_header (&head, &mask);
4665
4666 if (m == MATCH_NO)
4667 goto syntax;
4668 if (m != MATCH_YES)
4669 goto cleanup;
4670
4671 m = gfc_match_assignment ();
4672
4673 if (m == MATCH_ERROR)
4674 goto cleanup;
4675 if (m == MATCH_NO)
4676 {
4677 m = gfc_match_pointer_assignment ();
4678 if (m == MATCH_ERROR)
4679 goto cleanup;
4680 if (m == MATCH_NO)
4681 goto syntax;
4682 }
4683
4684 c = gfc_get_code ();
4685 *c = new_st;
4686 c->loc = gfc_current_locus;
4687
4688 if (gfc_match_eos () != MATCH_YES)
4689 goto syntax;
4690
4691 gfc_clear_new_st ();
4692 new_st.op = EXEC_FORALL;
a513927a 4693 new_st.expr1 = mask;
c874ae73
TS
4694 new_st.ext.forall_iterator = head;
4695 new_st.block = gfc_get_code ();
4696
4697 new_st.block->op = EXEC_FORALL;
4698 new_st.block->next = c;
4699
4700 return MATCH_YES;
4701
4702syntax:
4703 gfc_syntax_error (ST_FORALL);
4704
4705cleanup:
4706 gfc_free_forall_iterator (head);
4707 gfc_free_expr (mask);
4708
4709 return MATCH_ERROR;
4710}
4711
4712
4713/* Match a FORALL statement. */
4714
4715match
b251af97 4716gfc_match_forall (gfc_statement *st)
c874ae73
TS
4717{
4718 gfc_forall_iterator *head;
4719 gfc_expr *mask;
4720 gfc_code *c;
4721 match m0, m;
4722
4723 head = NULL;
4724 mask = NULL;
4725 c = NULL;
4726
4727 m0 = gfc_match_label ();
4728 if (m0 == MATCH_ERROR)
4729 return MATCH_ERROR;
4730
4731 m = gfc_match (" forall");
4732 if (m != MATCH_YES)
4733 return m;
4734
4735 m = match_forall_header (&head, &mask);
4736 if (m == MATCH_ERROR)
4737 goto cleanup;
4738 if (m == MATCH_NO)
4739 goto syntax;
4740
6de9cd9a
DN
4741 if (gfc_match_eos () == MATCH_YES)
4742 {
4743 *st = ST_FORALL_BLOCK;
6de9cd9a 4744 new_st.op = EXEC_FORALL;
a513927a 4745 new_st.expr1 = mask;
6de9cd9a 4746 new_st.ext.forall_iterator = head;
6de9cd9a
DN
4747 return MATCH_YES;
4748 }
4749
4750 m = gfc_match_assignment ();
4751 if (m == MATCH_ERROR)
4752 goto cleanup;
4753 if (m == MATCH_NO)
4754 {
4755 m = gfc_match_pointer_assignment ();
4756 if (m == MATCH_ERROR)
4757 goto cleanup;
4758 if (m == MATCH_NO)
4759 goto syntax;
4760 }
4761
4762 c = gfc_get_code ();
4763 *c = new_st;
a4a11197 4764 c->loc = gfc_current_locus;
6de9cd9a 4765
6de9cd9a
DN
4766 gfc_clear_new_st ();
4767 new_st.op = EXEC_FORALL;
a513927a 4768 new_st.expr1 = mask;
6de9cd9a
DN
4769 new_st.ext.forall_iterator = head;
4770 new_st.block = gfc_get_code ();
6de9cd9a
DN
4771 new_st.block->op = EXEC_FORALL;
4772 new_st.block->next = c;
4773
4774 *st = ST_FORALL;
4775 return MATCH_YES;
4776
4777syntax:
4778 gfc_syntax_error (ST_FORALL);
4779
4780cleanup:
4781 gfc_free_forall_iterator (head);
4782 gfc_free_expr (mask);
4783 gfc_free_statements (c);
4784 return MATCH_NO;
4785}
This page took 2.04923 seconds and 5 git commands to generate.