]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/match.c
omp-low.c (lower_omp_target): Fix up argument to is_reference.
[gcc.git] / gcc / fortran / match.c
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
28
29 int gfc_matching_ptr_assignment = 0;
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
32
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
35
36 /* For debugging and diagnostic purposes. Return the textual representation
37 of the intrinsic operator OP. */
38 const char *
39 gfc_op2string (gfc_intrinsic_op op)
40 {
41 switch (op)
42 {
43 case INTRINSIC_UPLUS:
44 case INTRINSIC_PLUS:
45 return "+";
46
47 case INTRINSIC_UMINUS:
48 case INTRINSIC_MINUS:
49 return "-";
50
51 case INTRINSIC_POWER:
52 return "**";
53 case INTRINSIC_CONCAT:
54 return "//";
55 case INTRINSIC_TIMES:
56 return "*";
57 case INTRINSIC_DIVIDE:
58 return "/";
59
60 case INTRINSIC_AND:
61 return ".and.";
62 case INTRINSIC_OR:
63 return ".or.";
64 case INTRINSIC_EQV:
65 return ".eqv.";
66 case INTRINSIC_NEQV:
67 return ".neqv.";
68
69 case INTRINSIC_EQ_OS:
70 return ".eq.";
71 case INTRINSIC_EQ:
72 return "==";
73 case INTRINSIC_NE_OS:
74 return ".ne.";
75 case INTRINSIC_NE:
76 return "/=";
77 case INTRINSIC_GE_OS:
78 return ".ge.";
79 case INTRINSIC_GE:
80 return ">=";
81 case INTRINSIC_LE_OS:
82 return ".le.";
83 case INTRINSIC_LE:
84 return "<=";
85 case INTRINSIC_LT_OS:
86 return ".lt.";
87 case INTRINSIC_LT:
88 return "<";
89 case INTRINSIC_GT_OS:
90 return ".gt.";
91 case INTRINSIC_GT:
92 return ">";
93 case INTRINSIC_NOT:
94 return ".not.";
95
96 case INTRINSIC_ASSIGN:
97 return "=";
98
99 case INTRINSIC_PARENTHESES:
100 return "parens";
101
102 case INTRINSIC_NONE:
103 return "none";
104
105 /* DTIO */
106 case INTRINSIC_FORMATTED:
107 return "formatted";
108 case INTRINSIC_UNFORMATTED:
109 return "unformatted";
110
111 default:
112 break;
113 }
114
115 gfc_internal_error ("gfc_op2string(): Bad code");
116 /* Not reached. */
117 }
118
119
120 /******************** Generic matching subroutines ************************/
121
122 /* Matches a member separator. With standard FORTRAN this is '%', but with
123 DEC structures we must carefully match dot ('.').
124 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
125 can be either a component reference chain or a combination of binary
126 operations.
127 There is no real way to win because the string may be grammatically
128 ambiguous. The following rules help avoid ambiguities - they match
129 some behavior of other (older) compilers. If the rules here are changed
130 the test cases should be updated. If the user has problems with these rules
131 they probably deserve the consequences. Consider "x.y.z":
132 (1) If any user defined operator ".y." exists, this is always y(x,z)
133 (even if ".y." is the wrong type and/or x has a member y).
134 (2) Otherwise if x has a member y, and y is itself a derived type,
135 this is (x->y)->z, even if an intrinsic operator exists which
136 can handle (x,z).
137 (3) If x has no member y or (x->y) is not a derived type but ".y."
138 is an intrinsic operator (such as ".eq."), this is y(x,z).
139 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
140 error.
141 It is worth noting that the logic here does not support mixed use of member
142 accessors within a single string. That is, even if x has component y and y
143 has component z, the following are all syntax errors:
144 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
145 */
146
147 match
148 gfc_match_member_sep(gfc_symbol *sym)
149 {
150 char name[GFC_MAX_SYMBOL_LEN + 1];
151 locus dot_loc, start_loc;
152 gfc_intrinsic_op iop;
153 match m;
154 gfc_symbol *tsym;
155 gfc_component *c = NULL;
156
157 /* What a relief: '%' is an unambiguous member separator. */
158 if (gfc_match_char ('%') == MATCH_YES)
159 return MATCH_YES;
160
161 /* Beware ye who enter here. */
162 if (!flag_dec_structure || !sym)
163 return MATCH_NO;
164
165 tsym = NULL;
166
167 /* We may be given either a derived type variable or the derived type
168 declaration itself (which actually contains the components);
169 we need the latter to search for components. */
170 if (gfc_fl_struct (sym->attr.flavor))
171 tsym = sym;
172 else if (gfc_bt_struct (sym->ts.type))
173 tsym = sym->ts.u.derived;
174
175 iop = INTRINSIC_NONE;
176 name[0] = '\0';
177 m = MATCH_NO;
178
179 /* If we have to reject come back here later. */
180 start_loc = gfc_current_locus;
181
182 /* Look for a component access next. */
183 if (gfc_match_char ('.') != MATCH_YES)
184 return MATCH_NO;
185
186 /* If we accept, come back here. */
187 dot_loc = gfc_current_locus;
188
189 /* Try to match a symbol name following the dot. */
190 if (gfc_match_name (name) != MATCH_YES)
191 {
192 gfc_error ("Expected structure component or operator name "
193 "after '.' at %C");
194 goto error;
195 }
196
197 /* If no dot follows we have "x.y" which should be a component access. */
198 if (gfc_match_char ('.') != MATCH_YES)
199 goto yes;
200
201 /* Now we have a string "x.y.z" which could be a nested member access
202 (x->y)->z or a binary operation y on x and z. */
203
204 /* First use any user-defined operators ".y." */
205 if (gfc_find_uop (name, sym->ns) != NULL)
206 goto no;
207
208 /* Match accesses to existing derived-type components for
209 derived-type vars: "x.y.z" = (x->y)->z */
210 c = gfc_find_component(tsym, name, false, true, NULL);
211 if (c && (gfc_bt_struct (c->ts.type) || c->ts.type == BT_CLASS))
212 goto yes;
213
214 /* If y is not a component or has no members, try intrinsic operators. */
215 gfc_current_locus = start_loc;
216 if (gfc_match_intrinsic_op (&iop) != MATCH_YES)
217 {
218 /* If ".y." is not an intrinsic operator but y was a valid non-
219 structure component, match and leave the trailing dot to be
220 dealt with later. */
221 if (c)
222 goto yes;
223
224 gfc_error ("'%s' is neither a defined operator nor a "
225 "structure component in dotted string at %C", name);
226 goto error;
227 }
228
229 /* .y. is an intrinsic operator, overriding any possible member access. */
230 goto no;
231
232 /* Return keeping the current locus consistent with the match result. */
233 error:
234 m = MATCH_ERROR;
235 no:
236 gfc_current_locus = start_loc;
237 return m;
238 yes:
239 gfc_current_locus = dot_loc;
240 return MATCH_YES;
241 }
242
243
244 /* This function scans the current statement counting the opened and closed
245 parenthesis to make sure they are balanced. */
246
247 match
248 gfc_match_parens (void)
249 {
250 locus old_loc, where;
251 int count;
252 gfc_instring instring;
253 gfc_char_t c, quote;
254
255 old_loc = gfc_current_locus;
256 count = 0;
257 instring = NONSTRING;
258 quote = ' ';
259
260 for (;;)
261 {
262 c = gfc_next_char_literal (instring);
263 if (c == '\n')
264 break;
265 if (quote == ' ' && ((c == '\'') || (c == '"')))
266 {
267 quote = c;
268 instring = INSTRING_WARN;
269 continue;
270 }
271 if (quote != ' ' && c == quote)
272 {
273 quote = ' ';
274 instring = NONSTRING;
275 continue;
276 }
277
278 if (c == '(' && quote == ' ')
279 {
280 count++;
281 where = gfc_current_locus;
282 }
283 if (c == ')' && quote == ' ')
284 {
285 count--;
286 where = gfc_current_locus;
287 }
288 }
289
290 gfc_current_locus = old_loc;
291
292 if (count > 0)
293 {
294 gfc_error ("Missing %<)%> in statement at or before %L", &where);
295 return MATCH_ERROR;
296 }
297 if (count < 0)
298 {
299 gfc_error ("Missing %<(%> in statement at or before %L", &where);
300 return MATCH_ERROR;
301 }
302
303 return MATCH_YES;
304 }
305
306
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
309
310 match
311 gfc_match_special_char (gfc_char_t *res)
312 {
313 int len, i;
314 gfc_char_t c, n;
315 match m;
316
317 m = MATCH_YES;
318
319 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
320 {
321 case 'a':
322 *res = '\a';
323 break;
324 case 'b':
325 *res = '\b';
326 break;
327 case 't':
328 *res = '\t';
329 break;
330 case 'f':
331 *res = '\f';
332 break;
333 case 'n':
334 *res = '\n';
335 break;
336 case 'r':
337 *res = '\r';
338 break;
339 case 'v':
340 *res = '\v';
341 break;
342 case '\\':
343 *res = '\\';
344 break;
345 case '0':
346 *res = '\0';
347 break;
348
349 case 'x':
350 case 'u':
351 case 'U':
352 /* Hexadecimal form of wide characters. */
353 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
354 n = 0;
355 for (i = 0; i < len; i++)
356 {
357 char buf[2] = { '\0', '\0' };
358
359 c = gfc_next_char_literal (INSTRING_WARN);
360 if (!gfc_wide_fits_in_byte (c)
361 || !gfc_check_digit ((unsigned char) c, 16))
362 return MATCH_NO;
363
364 buf[0] = (unsigned char) c;
365 n = n << 4;
366 n += strtol (buf, NULL, 16);
367 }
368 *res = n;
369 break;
370
371 default:
372 /* Unknown backslash codes are simply not expanded. */
373 m = MATCH_NO;
374 break;
375 }
376
377 return m;
378 }
379
380
381 /* In free form, match at least one space. Always matches in fixed
382 form. */
383
384 match
385 gfc_match_space (void)
386 {
387 locus old_loc;
388 char c;
389
390 if (gfc_current_form == FORM_FIXED)
391 return MATCH_YES;
392
393 old_loc = gfc_current_locus;
394
395 c = gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c))
397 {
398 gfc_current_locus = old_loc;
399 return MATCH_NO;
400 }
401
402 gfc_gobble_whitespace ();
403
404 return MATCH_YES;
405 }
406
407
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
411
412 match
413 gfc_match_eos (void)
414 {
415 locus old_loc;
416 int flag;
417 char c;
418
419 flag = 0;
420
421 for (;;)
422 {
423 old_loc = gfc_current_locus;
424 gfc_gobble_whitespace ();
425
426 c = gfc_next_ascii_char ();
427 switch (c)
428 {
429 case '!':
430 do
431 {
432 c = gfc_next_ascii_char ();
433 }
434 while (c != '\n');
435
436 /* Fall through. */
437
438 case '\n':
439 return MATCH_YES;
440
441 case ';':
442 flag = 1;
443 continue;
444 }
445
446 break;
447 }
448
449 gfc_current_locus = old_loc;
450 return (flag) ? MATCH_YES : MATCH_NO;
451 }
452
453
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits. */
458
459 match
460 gfc_match_small_literal_int (int *value, int *cnt)
461 {
462 locus old_loc;
463 char c;
464 int i, j;
465
466 old_loc = gfc_current_locus;
467
468 *value = -1;
469 gfc_gobble_whitespace ();
470 c = gfc_next_ascii_char ();
471 if (cnt)
472 *cnt = 0;
473
474 if (!ISDIGIT (c))
475 {
476 gfc_current_locus = old_loc;
477 return MATCH_NO;
478 }
479
480 i = c - '0';
481 j = 1;
482
483 for (;;)
484 {
485 old_loc = gfc_current_locus;
486 c = gfc_next_ascii_char ();
487
488 if (!ISDIGIT (c))
489 break;
490
491 i = 10 * i + c - '0';
492 j++;
493
494 if (i > 99999999)
495 {
496 gfc_error ("Integer too large at %C");
497 return MATCH_ERROR;
498 }
499 }
500
501 gfc_current_locus = old_loc;
502
503 *value = i;
504 if (cnt)
505 *cnt = j;
506 return MATCH_YES;
507 }
508
509
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
512
513 match
514 gfc_match_small_int (int *value)
515 {
516 gfc_expr *expr;
517 const char *p;
518 match m;
519 int i;
520
521 m = gfc_match_expr (&expr);
522 if (m != MATCH_YES)
523 return m;
524
525 p = gfc_extract_int (expr, &i);
526 gfc_free_expr (expr);
527
528 if (p != NULL)
529 {
530 gfc_error (p);
531 m = MATCH_ERROR;
532 }
533
534 *value = i;
535 return m;
536 }
537
538
539 /* This function is the same as the gfc_match_small_int, except that
540 we're keeping the pointer to the expr. This function could just be
541 removed and the previously mentioned one modified, though all calls
542 to it would have to be modified then (and there were a number of
543 them). Return MATCH_ERROR if fail to extract the int; otherwise,
544 return the result of gfc_match_expr(). The expr (if any) that was
545 matched is returned in the parameter expr. */
546
547 match
548 gfc_match_small_int_expr (int *value, gfc_expr **expr)
549 {
550 const char *p;
551 match m;
552 int i;
553
554 m = gfc_match_expr (expr);
555 if (m != MATCH_YES)
556 return m;
557
558 p = gfc_extract_int (*expr, &i);
559
560 if (p != NULL)
561 {
562 gfc_error (p);
563 m = MATCH_ERROR;
564 }
565
566 *value = i;
567 return m;
568 }
569
570
571 /* Matches a statement label. Uses gfc_match_small_literal_int() to
572 do most of the work. */
573
574 match
575 gfc_match_st_label (gfc_st_label **label)
576 {
577 locus old_loc;
578 match m;
579 int i, cnt;
580
581 old_loc = gfc_current_locus;
582
583 m = gfc_match_small_literal_int (&i, &cnt);
584 if (m != MATCH_YES)
585 return m;
586
587 if (cnt > 5)
588 {
589 gfc_error ("Too many digits in statement label at %C");
590 goto cleanup;
591 }
592
593 if (i == 0)
594 {
595 gfc_error ("Statement label at %C is zero");
596 goto cleanup;
597 }
598
599 *label = gfc_get_st_label (i);
600 return MATCH_YES;
601
602 cleanup:
603
604 gfc_current_locus = old_loc;
605 return MATCH_ERROR;
606 }
607
608
609 /* Match and validate a label associated with a named IF, DO or SELECT
610 statement. If the symbol does not have the label attribute, we add
611 it. We also make sure the symbol does not refer to another
612 (active) block. A matched label is pointed to by gfc_new_block. */
613
614 match
615 gfc_match_label (void)
616 {
617 char name[GFC_MAX_SYMBOL_LEN + 1];
618 match m;
619
620 gfc_new_block = NULL;
621
622 m = gfc_match (" %n :", name);
623 if (m != MATCH_YES)
624 return m;
625
626 if (gfc_get_symbol (name, NULL, &gfc_new_block))
627 {
628 gfc_error ("Label name %qs at %C is ambiguous", name);
629 return MATCH_ERROR;
630 }
631
632 if (gfc_new_block->attr.flavor == FL_LABEL)
633 {
634 gfc_error ("Duplicate construct label %qs at %C", name);
635 return MATCH_ERROR;
636 }
637
638 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
639 gfc_new_block->name, NULL))
640 return MATCH_ERROR;
641
642 return MATCH_YES;
643 }
644
645
646 /* See if the current input looks like a name of some sort. Modifies
647 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
648 Note that options.c restricts max_identifier_length to not more
649 than GFC_MAX_SYMBOL_LEN. */
650
651 match
652 gfc_match_name (char *buffer)
653 {
654 locus old_loc;
655 int i;
656 char c;
657
658 old_loc = gfc_current_locus;
659 gfc_gobble_whitespace ();
660
661 c = gfc_next_ascii_char ();
662 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
663 {
664 /* Special cases for unary minus and plus, which allows for a sensible
665 error message for code of the form 'c = exp(-a*b) )' where an
666 extra ')' appears at the end of statement. */
667 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
668 gfc_error ("Invalid character in name at %C");
669 gfc_current_locus = old_loc;
670 return MATCH_NO;
671 }
672
673 i = 0;
674
675 do
676 {
677 buffer[i++] = c;
678
679 if (i > gfc_option.max_identifier_length)
680 {
681 gfc_error ("Name at %C is too long");
682 return MATCH_ERROR;
683 }
684
685 old_loc = gfc_current_locus;
686 c = gfc_next_ascii_char ();
687 }
688 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
689
690 if (c == '$' && !flag_dollar_ok)
691 {
692 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
693 "allow it as an extension", &old_loc);
694 return MATCH_ERROR;
695 }
696
697 buffer[i] = '\0';
698 gfc_current_locus = old_loc;
699
700 return MATCH_YES;
701 }
702
703
704 /* Match a symbol on the input. Modifies the pointer to the symbol
705 pointer if successful. */
706
707 match
708 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
709 {
710 char buffer[GFC_MAX_SYMBOL_LEN + 1];
711 match m;
712
713 m = gfc_match_name (buffer);
714 if (m != MATCH_YES)
715 return m;
716
717 if (host_assoc)
718 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
719 ? MATCH_ERROR : MATCH_YES;
720
721 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
722 return MATCH_ERROR;
723
724 return MATCH_YES;
725 }
726
727
728 match
729 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
730 {
731 gfc_symtree *st;
732 match m;
733
734 m = gfc_match_sym_tree (&st, host_assoc);
735
736 if (m == MATCH_YES)
737 {
738 if (st)
739 *matched_symbol = st->n.sym;
740 else
741 *matched_symbol = NULL;
742 }
743 else
744 *matched_symbol = NULL;
745 return m;
746 }
747
748
749 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
750 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
751 in matchexp.c. */
752
753 match
754 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
755 {
756 locus orig_loc = gfc_current_locus;
757 char ch;
758
759 gfc_gobble_whitespace ();
760 ch = gfc_next_ascii_char ();
761 switch (ch)
762 {
763 case '+':
764 /* Matched "+". */
765 *result = INTRINSIC_PLUS;
766 return MATCH_YES;
767
768 case '-':
769 /* Matched "-". */
770 *result = INTRINSIC_MINUS;
771 return MATCH_YES;
772
773 case '=':
774 if (gfc_next_ascii_char () == '=')
775 {
776 /* Matched "==". */
777 *result = INTRINSIC_EQ;
778 return MATCH_YES;
779 }
780 break;
781
782 case '<':
783 if (gfc_peek_ascii_char () == '=')
784 {
785 /* Matched "<=". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_LE;
788 return MATCH_YES;
789 }
790 /* Matched "<". */
791 *result = INTRINSIC_LT;
792 return MATCH_YES;
793
794 case '>':
795 if (gfc_peek_ascii_char () == '=')
796 {
797 /* Matched ">=". */
798 gfc_next_ascii_char ();
799 *result = INTRINSIC_GE;
800 return MATCH_YES;
801 }
802 /* Matched ">". */
803 *result = INTRINSIC_GT;
804 return MATCH_YES;
805
806 case '*':
807 if (gfc_peek_ascii_char () == '*')
808 {
809 /* Matched "**". */
810 gfc_next_ascii_char ();
811 *result = INTRINSIC_POWER;
812 return MATCH_YES;
813 }
814 /* Matched "*". */
815 *result = INTRINSIC_TIMES;
816 return MATCH_YES;
817
818 case '/':
819 ch = gfc_peek_ascii_char ();
820 if (ch == '=')
821 {
822 /* Matched "/=". */
823 gfc_next_ascii_char ();
824 *result = INTRINSIC_NE;
825 return MATCH_YES;
826 }
827 else if (ch == '/')
828 {
829 /* Matched "//". */
830 gfc_next_ascii_char ();
831 *result = INTRINSIC_CONCAT;
832 return MATCH_YES;
833 }
834 /* Matched "/". */
835 *result = INTRINSIC_DIVIDE;
836 return MATCH_YES;
837
838 case '.':
839 ch = gfc_next_ascii_char ();
840 switch (ch)
841 {
842 case 'a':
843 if (gfc_next_ascii_char () == 'n'
844 && gfc_next_ascii_char () == 'd'
845 && gfc_next_ascii_char () == '.')
846 {
847 /* Matched ".and.". */
848 *result = INTRINSIC_AND;
849 return MATCH_YES;
850 }
851 break;
852
853 case 'e':
854 if (gfc_next_ascii_char () == 'q')
855 {
856 ch = gfc_next_ascii_char ();
857 if (ch == '.')
858 {
859 /* Matched ".eq.". */
860 *result = INTRINSIC_EQ_OS;
861 return MATCH_YES;
862 }
863 else if (ch == 'v')
864 {
865 if (gfc_next_ascii_char () == '.')
866 {
867 /* Matched ".eqv.". */
868 *result = INTRINSIC_EQV;
869 return MATCH_YES;
870 }
871 }
872 }
873 break;
874
875 case 'g':
876 ch = gfc_next_ascii_char ();
877 if (ch == 'e')
878 {
879 if (gfc_next_ascii_char () == '.')
880 {
881 /* Matched ".ge.". */
882 *result = INTRINSIC_GE_OS;
883 return MATCH_YES;
884 }
885 }
886 else if (ch == 't')
887 {
888 if (gfc_next_ascii_char () == '.')
889 {
890 /* Matched ".gt.". */
891 *result = INTRINSIC_GT_OS;
892 return MATCH_YES;
893 }
894 }
895 break;
896
897 case 'l':
898 ch = gfc_next_ascii_char ();
899 if (ch == 'e')
900 {
901 if (gfc_next_ascii_char () == '.')
902 {
903 /* Matched ".le.". */
904 *result = INTRINSIC_LE_OS;
905 return MATCH_YES;
906 }
907 }
908 else if (ch == 't')
909 {
910 if (gfc_next_ascii_char () == '.')
911 {
912 /* Matched ".lt.". */
913 *result = INTRINSIC_LT_OS;
914 return MATCH_YES;
915 }
916 }
917 break;
918
919 case 'n':
920 ch = gfc_next_ascii_char ();
921 if (ch == 'e')
922 {
923 ch = gfc_next_ascii_char ();
924 if (ch == '.')
925 {
926 /* Matched ".ne.". */
927 *result = INTRINSIC_NE_OS;
928 return MATCH_YES;
929 }
930 else if (ch == 'q')
931 {
932 if (gfc_next_ascii_char () == 'v'
933 && gfc_next_ascii_char () == '.')
934 {
935 /* Matched ".neqv.". */
936 *result = INTRINSIC_NEQV;
937 return MATCH_YES;
938 }
939 }
940 }
941 else if (ch == 'o')
942 {
943 if (gfc_next_ascii_char () == 't'
944 && gfc_next_ascii_char () == '.')
945 {
946 /* Matched ".not.". */
947 *result = INTRINSIC_NOT;
948 return MATCH_YES;
949 }
950 }
951 break;
952
953 case 'o':
954 if (gfc_next_ascii_char () == 'r'
955 && gfc_next_ascii_char () == '.')
956 {
957 /* Matched ".or.". */
958 *result = INTRINSIC_OR;
959 return MATCH_YES;
960 }
961 break;
962
963 case 'x':
964 if (gfc_next_ascii_char () == 'o'
965 && gfc_next_ascii_char () == 'r'
966 && gfc_next_ascii_char () == '.')
967 {
968 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
969 return MATCH_ERROR;
970 /* Matched ".xor." - equivalent to ".neqv.". */
971 *result = INTRINSIC_NEQV;
972 return MATCH_YES;
973 }
974 break;
975
976 default:
977 break;
978 }
979 break;
980
981 default:
982 break;
983 }
984
985 gfc_current_locus = orig_loc;
986 return MATCH_NO;
987 }
988
989
990 /* Match a loop control phrase:
991
992 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
993
994 If the final integer expression is not present, a constant unity
995 expression is returned. We don't return MATCH_ERROR until after
996 the equals sign is seen. */
997
998 match
999 gfc_match_iterator (gfc_iterator *iter, int init_flag)
1000 {
1001 char name[GFC_MAX_SYMBOL_LEN + 1];
1002 gfc_expr *var, *e1, *e2, *e3;
1003 locus start;
1004 match m;
1005
1006 e1 = e2 = e3 = NULL;
1007
1008 /* Match the start of an iterator without affecting the symbol table. */
1009
1010 start = gfc_current_locus;
1011 m = gfc_match (" %n =", name);
1012 gfc_current_locus = start;
1013
1014 if (m != MATCH_YES)
1015 return MATCH_NO;
1016
1017 m = gfc_match_variable (&var, 0);
1018 if (m != MATCH_YES)
1019 return MATCH_NO;
1020
1021 if (var->symtree->n.sym->attr.dimension)
1022 {
1023 gfc_error ("Loop variable at %C cannot be an array");
1024 goto cleanup;
1025 }
1026
1027 /* F2008, C617 & C565. */
1028 if (var->symtree->n.sym->attr.codimension)
1029 {
1030 gfc_error ("Loop variable at %C cannot be a coarray");
1031 goto cleanup;
1032 }
1033
1034 if (var->ref != NULL)
1035 {
1036 gfc_error ("Loop variable at %C cannot be a sub-component");
1037 goto cleanup;
1038 }
1039
1040 gfc_match_char ('=');
1041
1042 var->symtree->n.sym->attr.implied_index = 1;
1043
1044 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1045 if (m == MATCH_NO)
1046 goto syntax;
1047 if (m == MATCH_ERROR)
1048 goto cleanup;
1049
1050 if (gfc_match_char (',') != MATCH_YES)
1051 goto syntax;
1052
1053 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1054 if (m == MATCH_NO)
1055 goto syntax;
1056 if (m == MATCH_ERROR)
1057 goto cleanup;
1058
1059 if (gfc_match_char (',') != MATCH_YES)
1060 {
1061 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1062 goto done;
1063 }
1064
1065 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1066 if (m == MATCH_ERROR)
1067 goto cleanup;
1068 if (m == MATCH_NO)
1069 {
1070 gfc_error ("Expected a step value in iterator at %C");
1071 goto cleanup;
1072 }
1073
1074 done:
1075 iter->var = var;
1076 iter->start = e1;
1077 iter->end = e2;
1078 iter->step = e3;
1079 return MATCH_YES;
1080
1081 syntax:
1082 gfc_error ("Syntax error in iterator at %C");
1083
1084 cleanup:
1085 gfc_free_expr (e1);
1086 gfc_free_expr (e2);
1087 gfc_free_expr (e3);
1088
1089 return MATCH_ERROR;
1090 }
1091
1092
1093 /* Tries to match the next non-whitespace character on the input.
1094 This subroutine does not return MATCH_ERROR. */
1095
1096 match
1097 gfc_match_char (char c)
1098 {
1099 locus where;
1100
1101 where = gfc_current_locus;
1102 gfc_gobble_whitespace ();
1103
1104 if (gfc_next_ascii_char () == c)
1105 return MATCH_YES;
1106
1107 gfc_current_locus = where;
1108 return MATCH_NO;
1109 }
1110
1111
1112 /* General purpose matching subroutine. The target string is a
1113 scanf-like format string in which spaces correspond to arbitrary
1114 whitespace (including no whitespace), characters correspond to
1115 themselves. The %-codes are:
1116
1117 %% Literal percent sign
1118 %e Expression, pointer to a pointer is set
1119 %s Symbol, pointer to the symbol is set
1120 %n Name, character buffer is set to name
1121 %t Matches end of statement.
1122 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1123 %l Matches a statement label
1124 %v Matches a variable expression (an lvalue)
1125 % Matches a required space (in free form) and optional spaces. */
1126
1127 match
1128 gfc_match (const char *target, ...)
1129 {
1130 gfc_st_label **label;
1131 int matches, *ip;
1132 locus old_loc;
1133 va_list argp;
1134 char c, *np;
1135 match m, n;
1136 void **vp;
1137 const char *p;
1138
1139 old_loc = gfc_current_locus;
1140 va_start (argp, target);
1141 m = MATCH_NO;
1142 matches = 0;
1143 p = target;
1144
1145 loop:
1146 c = *p++;
1147 switch (c)
1148 {
1149 case ' ':
1150 gfc_gobble_whitespace ();
1151 goto loop;
1152 case '\0':
1153 m = MATCH_YES;
1154 break;
1155
1156 case '%':
1157 c = *p++;
1158 switch (c)
1159 {
1160 case 'e':
1161 vp = va_arg (argp, void **);
1162 n = gfc_match_expr ((gfc_expr **) vp);
1163 if (n != MATCH_YES)
1164 {
1165 m = n;
1166 goto not_yes;
1167 }
1168
1169 matches++;
1170 goto loop;
1171
1172 case 'v':
1173 vp = va_arg (argp, void **);
1174 n = gfc_match_variable ((gfc_expr **) vp, 0);
1175 if (n != MATCH_YES)
1176 {
1177 m = n;
1178 goto not_yes;
1179 }
1180
1181 matches++;
1182 goto loop;
1183
1184 case 's':
1185 vp = va_arg (argp, void **);
1186 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1187 if (n != MATCH_YES)
1188 {
1189 m = n;
1190 goto not_yes;
1191 }
1192
1193 matches++;
1194 goto loop;
1195
1196 case 'n':
1197 np = va_arg (argp, char *);
1198 n = gfc_match_name (np);
1199 if (n != MATCH_YES)
1200 {
1201 m = n;
1202 goto not_yes;
1203 }
1204
1205 matches++;
1206 goto loop;
1207
1208 case 'l':
1209 label = va_arg (argp, gfc_st_label **);
1210 n = gfc_match_st_label (label);
1211 if (n != MATCH_YES)
1212 {
1213 m = n;
1214 goto not_yes;
1215 }
1216
1217 matches++;
1218 goto loop;
1219
1220 case 'o':
1221 ip = va_arg (argp, int *);
1222 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1223 if (n != MATCH_YES)
1224 {
1225 m = n;
1226 goto not_yes;
1227 }
1228
1229 matches++;
1230 goto loop;
1231
1232 case 't':
1233 if (gfc_match_eos () != MATCH_YES)
1234 {
1235 m = MATCH_NO;
1236 goto not_yes;
1237 }
1238 goto loop;
1239
1240 case ' ':
1241 if (gfc_match_space () == MATCH_YES)
1242 goto loop;
1243 m = MATCH_NO;
1244 goto not_yes;
1245
1246 case '%':
1247 break; /* Fall through to character matcher. */
1248
1249 default:
1250 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1251 }
1252
1253 default:
1254
1255 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1256 expect an upper case character here! */
1257 gcc_assert (TOLOWER (c) == c);
1258
1259 if (c == gfc_next_ascii_char ())
1260 goto loop;
1261 break;
1262 }
1263
1264 not_yes:
1265 va_end (argp);
1266
1267 if (m != MATCH_YES)
1268 {
1269 /* Clean up after a failed match. */
1270 gfc_current_locus = old_loc;
1271 va_start (argp, target);
1272
1273 p = target;
1274 for (; matches > 0; matches--)
1275 {
1276 while (*p++ != '%');
1277
1278 switch (*p++)
1279 {
1280 case '%':
1281 matches++;
1282 break; /* Skip. */
1283
1284 /* Matches that don't have to be undone */
1285 case 'o':
1286 case 'l':
1287 case 'n':
1288 case 's':
1289 (void) va_arg (argp, void **);
1290 break;
1291
1292 case 'e':
1293 case 'v':
1294 vp = va_arg (argp, void **);
1295 gfc_free_expr ((struct gfc_expr *)*vp);
1296 *vp = NULL;
1297 break;
1298 }
1299 }
1300
1301 va_end (argp);
1302 }
1303
1304 return m;
1305 }
1306
1307
1308 /*********************** Statement level matching **********************/
1309
1310 /* Matches the start of a program unit, which is the program keyword
1311 followed by an obligatory symbol. */
1312
1313 match
1314 gfc_match_program (void)
1315 {
1316 gfc_symbol *sym;
1317 match m;
1318
1319 m = gfc_match ("% %s%t", &sym);
1320
1321 if (m == MATCH_NO)
1322 {
1323 gfc_error ("Invalid form of PROGRAM statement at %C");
1324 m = MATCH_ERROR;
1325 }
1326
1327 if (m == MATCH_ERROR)
1328 return m;
1329
1330 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1331 return MATCH_ERROR;
1332
1333 gfc_new_block = sym;
1334
1335 return MATCH_YES;
1336 }
1337
1338
1339 /* Match a simple assignment statement. */
1340
1341 match
1342 gfc_match_assignment (void)
1343 {
1344 gfc_expr *lvalue, *rvalue;
1345 locus old_loc;
1346 match m;
1347
1348 old_loc = gfc_current_locus;
1349
1350 lvalue = NULL;
1351 m = gfc_match (" %v =", &lvalue);
1352 if (m != MATCH_YES)
1353 {
1354 gfc_current_locus = old_loc;
1355 gfc_free_expr (lvalue);
1356 return MATCH_NO;
1357 }
1358
1359 rvalue = NULL;
1360 m = gfc_match (" %e%t", &rvalue);
1361 if (m != MATCH_YES)
1362 {
1363 gfc_current_locus = old_loc;
1364 gfc_free_expr (lvalue);
1365 gfc_free_expr (rvalue);
1366 return m;
1367 }
1368
1369 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1370
1371 new_st.op = EXEC_ASSIGN;
1372 new_st.expr1 = lvalue;
1373 new_st.expr2 = rvalue;
1374
1375 gfc_check_do_variable (lvalue->symtree);
1376
1377 return MATCH_YES;
1378 }
1379
1380
1381 /* Match a pointer assignment statement. */
1382
1383 match
1384 gfc_match_pointer_assignment (void)
1385 {
1386 gfc_expr *lvalue, *rvalue;
1387 locus old_loc;
1388 match m;
1389
1390 old_loc = gfc_current_locus;
1391
1392 lvalue = rvalue = NULL;
1393 gfc_matching_ptr_assignment = 0;
1394 gfc_matching_procptr_assignment = 0;
1395
1396 m = gfc_match (" %v =>", &lvalue);
1397 if (m != MATCH_YES)
1398 {
1399 m = MATCH_NO;
1400 goto cleanup;
1401 }
1402
1403 if (lvalue->symtree->n.sym->attr.proc_pointer
1404 || gfc_is_proc_ptr_comp (lvalue))
1405 gfc_matching_procptr_assignment = 1;
1406 else
1407 gfc_matching_ptr_assignment = 1;
1408
1409 m = gfc_match (" %e%t", &rvalue);
1410 gfc_matching_ptr_assignment = 0;
1411 gfc_matching_procptr_assignment = 0;
1412 if (m != MATCH_YES)
1413 goto cleanup;
1414
1415 new_st.op = EXEC_POINTER_ASSIGN;
1416 new_st.expr1 = lvalue;
1417 new_st.expr2 = rvalue;
1418
1419 return MATCH_YES;
1420
1421 cleanup:
1422 gfc_current_locus = old_loc;
1423 gfc_free_expr (lvalue);
1424 gfc_free_expr (rvalue);
1425 return m;
1426 }
1427
1428
1429 /* We try to match an easy arithmetic IF statement. This only happens
1430 when just after having encountered a simple IF statement. This code
1431 is really duplicate with parts of the gfc_match_if code, but this is
1432 *much* easier. */
1433
1434 static match
1435 match_arithmetic_if (void)
1436 {
1437 gfc_st_label *l1, *l2, *l3;
1438 gfc_expr *expr;
1439 match m;
1440
1441 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1442 if (m != MATCH_YES)
1443 return m;
1444
1445 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1446 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1447 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1448 {
1449 gfc_free_expr (expr);
1450 return MATCH_ERROR;
1451 }
1452
1453 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1454 return MATCH_ERROR;
1455
1456 new_st.op = EXEC_ARITHMETIC_IF;
1457 new_st.expr1 = expr;
1458 new_st.label1 = l1;
1459 new_st.label2 = l2;
1460 new_st.label3 = l3;
1461
1462 return MATCH_YES;
1463 }
1464
1465
1466 /* The IF statement is a bit of a pain. First of all, there are three
1467 forms of it, the simple IF, the IF that starts a block and the
1468 arithmetic IF.
1469
1470 There is a problem with the simple IF and that is the fact that we
1471 only have a single level of undo information on symbols. What this
1472 means is for a simple IF, we must re-match the whole IF statement
1473 multiple times in order to guarantee that the symbol table ends up
1474 in the proper state. */
1475
1476 static match match_simple_forall (void);
1477 static match match_simple_where (void);
1478
1479 match
1480 gfc_match_if (gfc_statement *if_type)
1481 {
1482 gfc_expr *expr;
1483 gfc_st_label *l1, *l2, *l3;
1484 locus old_loc, old_loc2;
1485 gfc_code *p;
1486 match m, n;
1487
1488 n = gfc_match_label ();
1489 if (n == MATCH_ERROR)
1490 return n;
1491
1492 old_loc = gfc_current_locus;
1493
1494 m = gfc_match (" if ( %e", &expr);
1495 if (m != MATCH_YES)
1496 return m;
1497
1498 old_loc2 = gfc_current_locus;
1499 gfc_current_locus = old_loc;
1500
1501 if (gfc_match_parens () == MATCH_ERROR)
1502 return MATCH_ERROR;
1503
1504 gfc_current_locus = old_loc2;
1505
1506 if (gfc_match_char (')') != MATCH_YES)
1507 {
1508 gfc_error ("Syntax error in IF-expression at %C");
1509 gfc_free_expr (expr);
1510 return MATCH_ERROR;
1511 }
1512
1513 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1514
1515 if (m == MATCH_YES)
1516 {
1517 if (n == MATCH_YES)
1518 {
1519 gfc_error ("Block label not appropriate for arithmetic IF "
1520 "statement at %C");
1521 gfc_free_expr (expr);
1522 return MATCH_ERROR;
1523 }
1524
1525 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1526 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1527 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1528 {
1529 gfc_free_expr (expr);
1530 return MATCH_ERROR;
1531 }
1532
1533 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1534 return MATCH_ERROR;
1535
1536 new_st.op = EXEC_ARITHMETIC_IF;
1537 new_st.expr1 = expr;
1538 new_st.label1 = l1;
1539 new_st.label2 = l2;
1540 new_st.label3 = l3;
1541
1542 *if_type = ST_ARITHMETIC_IF;
1543 return MATCH_YES;
1544 }
1545
1546 if (gfc_match (" then%t") == MATCH_YES)
1547 {
1548 new_st.op = EXEC_IF;
1549 new_st.expr1 = expr;
1550 *if_type = ST_IF_BLOCK;
1551 return MATCH_YES;
1552 }
1553
1554 if (n == MATCH_YES)
1555 {
1556 gfc_error ("Block label is not appropriate for IF statement at %C");
1557 gfc_free_expr (expr);
1558 return MATCH_ERROR;
1559 }
1560
1561 /* At this point the only thing left is a simple IF statement. At
1562 this point, n has to be MATCH_NO, so we don't have to worry about
1563 re-matching a block label. From what we've got so far, try
1564 matching an assignment. */
1565
1566 *if_type = ST_SIMPLE_IF;
1567
1568 m = gfc_match_assignment ();
1569 if (m == MATCH_YES)
1570 goto got_match;
1571
1572 gfc_free_expr (expr);
1573 gfc_undo_symbols ();
1574 gfc_current_locus = old_loc;
1575
1576 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1577 assignment was found. For MATCH_NO, continue to call the various
1578 matchers. */
1579 if (m == MATCH_ERROR)
1580 return MATCH_ERROR;
1581
1582 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1583
1584 m = gfc_match_pointer_assignment ();
1585 if (m == MATCH_YES)
1586 goto got_match;
1587
1588 gfc_free_expr (expr);
1589 gfc_undo_symbols ();
1590 gfc_current_locus = old_loc;
1591
1592 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1593
1594 /* Look at the next keyword to see which matcher to call. Matching
1595 the keyword doesn't affect the symbol table, so we don't have to
1596 restore between tries. */
1597
1598 #define match(string, subr, statement) \
1599 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1600
1601 gfc_clear_error ();
1602
1603 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1604 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1605 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1606 match ("call", gfc_match_call, ST_CALL)
1607 match ("close", gfc_match_close, ST_CLOSE)
1608 match ("continue", gfc_match_continue, ST_CONTINUE)
1609 match ("cycle", gfc_match_cycle, ST_CYCLE)
1610 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1611 match ("end file", gfc_match_endfile, ST_END_FILE)
1612 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1613 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1614 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1615 match ("exit", gfc_match_exit, ST_EXIT)
1616 match ("flush", gfc_match_flush, ST_FLUSH)
1617 match ("forall", match_simple_forall, ST_FORALL)
1618 match ("go to", gfc_match_goto, ST_GOTO)
1619 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1620 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1621 match ("lock", gfc_match_lock, ST_LOCK)
1622 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1623 match ("open", gfc_match_open, ST_OPEN)
1624 match ("pause", gfc_match_pause, ST_NONE)
1625 match ("print", gfc_match_print, ST_WRITE)
1626 match ("read", gfc_match_read, ST_READ)
1627 match ("return", gfc_match_return, ST_RETURN)
1628 match ("rewind", gfc_match_rewind, ST_REWIND)
1629 match ("stop", gfc_match_stop, ST_STOP)
1630 match ("wait", gfc_match_wait, ST_WAIT)
1631 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1632 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1633 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1634 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1635 match ("where", match_simple_where, ST_WHERE)
1636 match ("write", gfc_match_write, ST_WRITE)
1637
1638 if (flag_dec)
1639 match ("type", gfc_match_print, ST_WRITE)
1640
1641 /* The gfc_match_assignment() above may have returned a MATCH_NO
1642 where the assignment was to a named constant. Check that
1643 special case here. */
1644 m = gfc_match_assignment ();
1645 if (m == MATCH_NO)
1646 {
1647 gfc_error ("Cannot assign to a named constant at %C");
1648 gfc_free_expr (expr);
1649 gfc_undo_symbols ();
1650 gfc_current_locus = old_loc;
1651 return MATCH_ERROR;
1652 }
1653
1654 /* All else has failed, so give up. See if any of the matchers has
1655 stored an error message of some sort. */
1656 if (!gfc_error_check ())
1657 gfc_error ("Unclassifiable statement in IF-clause at %C");
1658
1659 gfc_free_expr (expr);
1660 return MATCH_ERROR;
1661
1662 got_match:
1663 if (m == MATCH_NO)
1664 gfc_error ("Syntax error in IF-clause at %C");
1665 if (m != MATCH_YES)
1666 {
1667 gfc_free_expr (expr);
1668 return MATCH_ERROR;
1669 }
1670
1671 /* At this point, we've matched the single IF and the action clause
1672 is in new_st. Rearrange things so that the IF statement appears
1673 in new_st. */
1674
1675 p = gfc_get_code (EXEC_IF);
1676 p->next = XCNEW (gfc_code);
1677 *p->next = new_st;
1678 p->next->loc = gfc_current_locus;
1679
1680 p->expr1 = expr;
1681
1682 gfc_clear_new_st ();
1683
1684 new_st.op = EXEC_IF;
1685 new_st.block = p;
1686
1687 return MATCH_YES;
1688 }
1689
1690 #undef match
1691
1692
1693 /* Match an ELSE statement. */
1694
1695 match
1696 gfc_match_else (void)
1697 {
1698 char name[GFC_MAX_SYMBOL_LEN + 1];
1699
1700 if (gfc_match_eos () == MATCH_YES)
1701 return MATCH_YES;
1702
1703 if (gfc_match_name (name) != MATCH_YES
1704 || gfc_current_block () == NULL
1705 || gfc_match_eos () != MATCH_YES)
1706 {
1707 gfc_error ("Unexpected junk after ELSE statement at %C");
1708 return MATCH_ERROR;
1709 }
1710
1711 if (strcmp (name, gfc_current_block ()->name) != 0)
1712 {
1713 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1714 name, gfc_current_block ()->name);
1715 return MATCH_ERROR;
1716 }
1717
1718 return MATCH_YES;
1719 }
1720
1721
1722 /* Match an ELSE IF statement. */
1723
1724 match
1725 gfc_match_elseif (void)
1726 {
1727 char name[GFC_MAX_SYMBOL_LEN + 1];
1728 gfc_expr *expr;
1729 match m;
1730
1731 m = gfc_match (" ( %e ) then", &expr);
1732 if (m != MATCH_YES)
1733 return m;
1734
1735 if (gfc_match_eos () == MATCH_YES)
1736 goto done;
1737
1738 if (gfc_match_name (name) != MATCH_YES
1739 || gfc_current_block () == NULL
1740 || gfc_match_eos () != MATCH_YES)
1741 {
1742 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1743 goto cleanup;
1744 }
1745
1746 if (strcmp (name, gfc_current_block ()->name) != 0)
1747 {
1748 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1749 name, gfc_current_block ()->name);
1750 goto cleanup;
1751 }
1752
1753 done:
1754 new_st.op = EXEC_IF;
1755 new_st.expr1 = expr;
1756 return MATCH_YES;
1757
1758 cleanup:
1759 gfc_free_expr (expr);
1760 return MATCH_ERROR;
1761 }
1762
1763
1764 /* Free a gfc_iterator structure. */
1765
1766 void
1767 gfc_free_iterator (gfc_iterator *iter, int flag)
1768 {
1769
1770 if (iter == NULL)
1771 return;
1772
1773 gfc_free_expr (iter->var);
1774 gfc_free_expr (iter->start);
1775 gfc_free_expr (iter->end);
1776 gfc_free_expr (iter->step);
1777
1778 if (flag)
1779 free (iter);
1780 }
1781
1782
1783 /* Match a CRITICAL statement. */
1784 match
1785 gfc_match_critical (void)
1786 {
1787 gfc_st_label *label = NULL;
1788
1789 if (gfc_match_label () == MATCH_ERROR)
1790 return MATCH_ERROR;
1791
1792 if (gfc_match (" critical") != MATCH_YES)
1793 return MATCH_NO;
1794
1795 if (gfc_match_st_label (&label) == MATCH_ERROR)
1796 return MATCH_ERROR;
1797
1798 if (gfc_match_eos () != MATCH_YES)
1799 {
1800 gfc_syntax_error (ST_CRITICAL);
1801 return MATCH_ERROR;
1802 }
1803
1804 if (gfc_pure (NULL))
1805 {
1806 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1807 return MATCH_ERROR;
1808 }
1809
1810 if (gfc_find_state (COMP_DO_CONCURRENT))
1811 {
1812 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1813 "block");
1814 return MATCH_ERROR;
1815 }
1816
1817 gfc_unset_implicit_pure (NULL);
1818
1819 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1820 return MATCH_ERROR;
1821
1822 if (flag_coarray == GFC_FCOARRAY_NONE)
1823 {
1824 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1825 "enable");
1826 return MATCH_ERROR;
1827 }
1828
1829 if (gfc_find_state (COMP_CRITICAL))
1830 {
1831 gfc_error ("Nested CRITICAL block at %C");
1832 return MATCH_ERROR;
1833 }
1834
1835 new_st.op = EXEC_CRITICAL;
1836
1837 if (label != NULL
1838 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1839 return MATCH_ERROR;
1840
1841 return MATCH_YES;
1842 }
1843
1844
1845 /* Match a BLOCK statement. */
1846
1847 match
1848 gfc_match_block (void)
1849 {
1850 match m;
1851
1852 if (gfc_match_label () == MATCH_ERROR)
1853 return MATCH_ERROR;
1854
1855 if (gfc_match (" block") != MATCH_YES)
1856 return MATCH_NO;
1857
1858 /* For this to be a correct BLOCK statement, the line must end now. */
1859 m = gfc_match_eos ();
1860 if (m == MATCH_ERROR)
1861 return MATCH_ERROR;
1862 if (m == MATCH_NO)
1863 return MATCH_NO;
1864
1865 return MATCH_YES;
1866 }
1867
1868
1869 /* Match an ASSOCIATE statement. */
1870
1871 match
1872 gfc_match_associate (void)
1873 {
1874 if (gfc_match_label () == MATCH_ERROR)
1875 return MATCH_ERROR;
1876
1877 if (gfc_match (" associate") != MATCH_YES)
1878 return MATCH_NO;
1879
1880 /* Match the association list. */
1881 if (gfc_match_char ('(') != MATCH_YES)
1882 {
1883 gfc_error ("Expected association list at %C");
1884 return MATCH_ERROR;
1885 }
1886 new_st.ext.block.assoc = NULL;
1887 while (true)
1888 {
1889 gfc_association_list* newAssoc = gfc_get_association_list ();
1890 gfc_association_list* a;
1891
1892 /* Match the next association. */
1893 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1894 != MATCH_YES)
1895 {
1896 gfc_error ("Expected association at %C");
1897 goto assocListError;
1898 }
1899 newAssoc->where = gfc_current_locus;
1900
1901 /* Check that the current name is not yet in the list. */
1902 for (a = new_st.ext.block.assoc; a; a = a->next)
1903 if (!strcmp (a->name, newAssoc->name))
1904 {
1905 gfc_error ("Duplicate name %qs in association at %C",
1906 newAssoc->name);
1907 goto assocListError;
1908 }
1909
1910 /* The target expression must not be coindexed. */
1911 if (gfc_is_coindexed (newAssoc->target))
1912 {
1913 gfc_error ("Association target at %C must not be coindexed");
1914 goto assocListError;
1915 }
1916
1917 /* The `variable' field is left blank for now; because the target is not
1918 yet resolved, we can't use gfc_has_vector_subscript to determine it
1919 for now. This is set during resolution. */
1920
1921 /* Put it into the list. */
1922 newAssoc->next = new_st.ext.block.assoc;
1923 new_st.ext.block.assoc = newAssoc;
1924
1925 /* Try next one or end if closing parenthesis is found. */
1926 gfc_gobble_whitespace ();
1927 if (gfc_peek_char () == ')')
1928 break;
1929 if (gfc_match_char (',') != MATCH_YES)
1930 {
1931 gfc_error ("Expected %<)%> or %<,%> at %C");
1932 return MATCH_ERROR;
1933 }
1934
1935 continue;
1936
1937 assocListError:
1938 free (newAssoc);
1939 goto error;
1940 }
1941 if (gfc_match_char (')') != MATCH_YES)
1942 {
1943 /* This should never happen as we peek above. */
1944 gcc_unreachable ();
1945 }
1946
1947 if (gfc_match_eos () != MATCH_YES)
1948 {
1949 gfc_error ("Junk after ASSOCIATE statement at %C");
1950 goto error;
1951 }
1952
1953 return MATCH_YES;
1954
1955 error:
1956 gfc_free_association_list (new_st.ext.block.assoc);
1957 return MATCH_ERROR;
1958 }
1959
1960
1961 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1962 an accessible derived type. */
1963
1964 static match
1965 match_derived_type_spec (gfc_typespec *ts)
1966 {
1967 char name[GFC_MAX_SYMBOL_LEN + 1];
1968 locus old_locus;
1969 gfc_symbol *derived;
1970
1971 old_locus = gfc_current_locus;
1972
1973 if (gfc_match ("%n", name) != MATCH_YES)
1974 {
1975 gfc_current_locus = old_locus;
1976 return MATCH_NO;
1977 }
1978
1979 gfc_find_symbol (name, NULL, 1, &derived);
1980
1981 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1982 derived = gfc_find_dt_in_generic (derived);
1983
1984 if (derived && derived->attr.flavor == FL_DERIVED)
1985 {
1986 ts->type = BT_DERIVED;
1987 ts->u.derived = derived;
1988 return MATCH_YES;
1989 }
1990
1991 gfc_current_locus = old_locus;
1992 return MATCH_NO;
1993 }
1994
1995
1996 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1997 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1998 It only includes the intrinsic types from the Fortran 2003 standard
1999 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2000 the implicit_flag is not needed, so it was removed. Derived types are
2001 identified by their name alone. */
2002
2003 match
2004 gfc_match_type_spec (gfc_typespec *ts)
2005 {
2006 match m;
2007 locus old_locus;
2008 char name[GFC_MAX_SYMBOL_LEN + 1];
2009
2010 gfc_clear_ts (ts);
2011 gfc_gobble_whitespace ();
2012 old_locus = gfc_current_locus;
2013
2014 if (match_derived_type_spec (ts) == MATCH_YES)
2015 {
2016 /* Enforce F03:C401. */
2017 if (ts->u.derived->attr.abstract)
2018 {
2019 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2020 ts->u.derived->name, &old_locus);
2021 return MATCH_ERROR;
2022 }
2023 return MATCH_YES;
2024 }
2025
2026 if (gfc_match ("integer") == MATCH_YES)
2027 {
2028 ts->type = BT_INTEGER;
2029 ts->kind = gfc_default_integer_kind;
2030 goto kind_selector;
2031 }
2032
2033 if (gfc_match ("double precision") == MATCH_YES)
2034 {
2035 ts->type = BT_REAL;
2036 ts->kind = gfc_default_double_kind;
2037 return MATCH_YES;
2038 }
2039
2040 if (gfc_match ("complex") == MATCH_YES)
2041 {
2042 ts->type = BT_COMPLEX;
2043 ts->kind = gfc_default_complex_kind;
2044 goto kind_selector;
2045 }
2046
2047 if (gfc_match ("character") == MATCH_YES)
2048 {
2049 ts->type = BT_CHARACTER;
2050
2051 m = gfc_match_char_spec (ts);
2052
2053 if (m == MATCH_NO)
2054 m = MATCH_YES;
2055
2056 return m;
2057 }
2058
2059 if (gfc_match ("logical") == MATCH_YES)
2060 {
2061 ts->type = BT_LOGICAL;
2062 ts->kind = gfc_default_logical_kind;
2063 goto kind_selector;
2064 }
2065
2066 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2067 or list item in a type-list of an OpenMP reduction clause. Need to
2068 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2069 REAL(A,[KIND]) and REAL(KIND,A). */
2070
2071 m = gfc_match (" %n", name);
2072 if (m == MATCH_YES && strcmp (name, "real") == 0)
2073 {
2074 char c;
2075 gfc_expr *e;
2076 locus where;
2077
2078 ts->type = BT_REAL;
2079 ts->kind = gfc_default_real_kind;
2080
2081 gfc_gobble_whitespace ();
2082
2083 /* Prevent REAL*4, etc. */
2084 c = gfc_peek_ascii_char ();
2085 if (c == '*')
2086 {
2087 gfc_error ("Invalid type-spec at %C");
2088 return MATCH_ERROR;
2089 }
2090
2091 /* Found leading colon in REAL::, a trailing ')' in for example
2092 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2093 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2094 return MATCH_YES;
2095
2096 /* Found something other than the opening '(' in REAL(... */
2097 if (c != '(')
2098 return MATCH_NO;
2099 else
2100 gfc_next_char (); /* Burn the '('. */
2101
2102 /* Look for the optional KIND=. */
2103 where = gfc_current_locus;
2104 m = gfc_match ("%n", name);
2105 if (m == MATCH_YES)
2106 {
2107 gfc_gobble_whitespace ();
2108 c = gfc_next_char ();
2109 if (c == '=')
2110 {
2111 if (strcmp(name, "a") == 0)
2112 return MATCH_NO;
2113 else if (strcmp(name, "kind") == 0)
2114 goto found;
2115 else
2116 return MATCH_ERROR;
2117 }
2118 else
2119 gfc_current_locus = where;
2120 }
2121 else
2122 gfc_current_locus = where;
2123
2124 found:
2125
2126 m = gfc_match_init_expr (&e);
2127 if (m == MATCH_NO || m == MATCH_ERROR)
2128 return MATCH_NO;
2129
2130 /* If a comma appears, it is an intrinsic subprogram. */
2131 gfc_gobble_whitespace ();
2132 c = gfc_peek_ascii_char ();
2133 if (c == ',')
2134 {
2135 gfc_free_expr (e);
2136 return MATCH_NO;
2137 }
2138
2139 /* If ')' appears, we have REAL(initialization-expr), here check for
2140 a scalar integer initialization-expr and valid kind parameter. */
2141 if (c == ')')
2142 {
2143 if (e->ts.type != BT_INTEGER || e->rank > 0)
2144 {
2145 gfc_free_expr (e);
2146 return MATCH_NO;
2147 }
2148
2149 gfc_next_char (); /* Burn the ')'. */
2150 ts->kind = (int) mpz_get_si (e->value.integer);
2151 if (gfc_validate_kind (BT_REAL, ts->kind , true) == -1)
2152 {
2153 gfc_error ("Invalid type-spec at %C");
2154 return MATCH_ERROR;
2155 }
2156
2157 gfc_free_expr (e);
2158
2159 return MATCH_YES;
2160 }
2161 }
2162
2163 /* If a type is not matched, simply return MATCH_NO. */
2164 gfc_current_locus = old_locus;
2165 return MATCH_NO;
2166
2167 kind_selector:
2168
2169 gfc_gobble_whitespace ();
2170
2171 /* This prevents INTEGER*4, etc. */
2172 if (gfc_peek_ascii_char () == '*')
2173 {
2174 gfc_error ("Invalid type-spec at %C");
2175 return MATCH_ERROR;
2176 }
2177
2178 m = gfc_match_kind_spec (ts, false);
2179
2180 /* No kind specifier found. */
2181 if (m == MATCH_NO)
2182 m = MATCH_YES;
2183
2184 return m;
2185 }
2186
2187
2188 /******************** FORALL subroutines ********************/
2189
2190 /* Free a list of FORALL iterators. */
2191
2192 void
2193 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2194 {
2195 gfc_forall_iterator *next;
2196
2197 while (iter)
2198 {
2199 next = iter->next;
2200 gfc_free_expr (iter->var);
2201 gfc_free_expr (iter->start);
2202 gfc_free_expr (iter->end);
2203 gfc_free_expr (iter->stride);
2204 free (iter);
2205 iter = next;
2206 }
2207 }
2208
2209
2210 /* Match an iterator as part of a FORALL statement. The format is:
2211
2212 <var> = <start>:<end>[:<stride>]
2213
2214 On MATCH_NO, the caller tests for the possibility that there is a
2215 scalar mask expression. */
2216
2217 static match
2218 match_forall_iterator (gfc_forall_iterator **result)
2219 {
2220 gfc_forall_iterator *iter;
2221 locus where;
2222 match m;
2223
2224 where = gfc_current_locus;
2225 iter = XCNEW (gfc_forall_iterator);
2226
2227 m = gfc_match_expr (&iter->var);
2228 if (m != MATCH_YES)
2229 goto cleanup;
2230
2231 if (gfc_match_char ('=') != MATCH_YES
2232 || iter->var->expr_type != EXPR_VARIABLE)
2233 {
2234 m = MATCH_NO;
2235 goto cleanup;
2236 }
2237
2238 m = gfc_match_expr (&iter->start);
2239 if (m != MATCH_YES)
2240 goto cleanup;
2241
2242 if (gfc_match_char (':') != MATCH_YES)
2243 goto syntax;
2244
2245 m = gfc_match_expr (&iter->end);
2246 if (m == MATCH_NO)
2247 goto syntax;
2248 if (m == MATCH_ERROR)
2249 goto cleanup;
2250
2251 if (gfc_match_char (':') == MATCH_NO)
2252 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2253 else
2254 {
2255 m = gfc_match_expr (&iter->stride);
2256 if (m == MATCH_NO)
2257 goto syntax;
2258 if (m == MATCH_ERROR)
2259 goto cleanup;
2260 }
2261
2262 /* Mark the iteration variable's symbol as used as a FORALL index. */
2263 iter->var->symtree->n.sym->forall_index = true;
2264
2265 *result = iter;
2266 return MATCH_YES;
2267
2268 syntax:
2269 gfc_error ("Syntax error in FORALL iterator at %C");
2270 m = MATCH_ERROR;
2271
2272 cleanup:
2273
2274 gfc_current_locus = where;
2275 gfc_free_forall_iterator (iter);
2276 return m;
2277 }
2278
2279
2280 /* Match the header of a FORALL statement. */
2281
2282 static match
2283 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2284 {
2285 gfc_forall_iterator *head, *tail, *new_iter;
2286 gfc_expr *msk;
2287 match m;
2288
2289 gfc_gobble_whitespace ();
2290
2291 head = tail = NULL;
2292 msk = NULL;
2293
2294 if (gfc_match_char ('(') != MATCH_YES)
2295 return MATCH_NO;
2296
2297 m = match_forall_iterator (&new_iter);
2298 if (m == MATCH_ERROR)
2299 goto cleanup;
2300 if (m == MATCH_NO)
2301 goto syntax;
2302
2303 head = tail = new_iter;
2304
2305 for (;;)
2306 {
2307 if (gfc_match_char (',') != MATCH_YES)
2308 break;
2309
2310 m = match_forall_iterator (&new_iter);
2311 if (m == MATCH_ERROR)
2312 goto cleanup;
2313
2314 if (m == MATCH_YES)
2315 {
2316 tail->next = new_iter;
2317 tail = new_iter;
2318 continue;
2319 }
2320
2321 /* Have to have a mask expression. */
2322
2323 m = gfc_match_expr (&msk);
2324 if (m == MATCH_NO)
2325 goto syntax;
2326 if (m == MATCH_ERROR)
2327 goto cleanup;
2328
2329 break;
2330 }
2331
2332 if (gfc_match_char (')') == MATCH_NO)
2333 goto syntax;
2334
2335 *phead = head;
2336 *mask = msk;
2337 return MATCH_YES;
2338
2339 syntax:
2340 gfc_syntax_error (ST_FORALL);
2341
2342 cleanup:
2343 gfc_free_expr (msk);
2344 gfc_free_forall_iterator (head);
2345
2346 return MATCH_ERROR;
2347 }
2348
2349 /* Match the rest of a simple FORALL statement that follows an
2350 IF statement. */
2351
2352 static match
2353 match_simple_forall (void)
2354 {
2355 gfc_forall_iterator *head;
2356 gfc_expr *mask;
2357 gfc_code *c;
2358 match m;
2359
2360 mask = NULL;
2361 head = NULL;
2362 c = NULL;
2363
2364 m = match_forall_header (&head, &mask);
2365
2366 if (m == MATCH_NO)
2367 goto syntax;
2368 if (m != MATCH_YES)
2369 goto cleanup;
2370
2371 m = gfc_match_assignment ();
2372
2373 if (m == MATCH_ERROR)
2374 goto cleanup;
2375 if (m == MATCH_NO)
2376 {
2377 m = gfc_match_pointer_assignment ();
2378 if (m == MATCH_ERROR)
2379 goto cleanup;
2380 if (m == MATCH_NO)
2381 goto syntax;
2382 }
2383
2384 c = XCNEW (gfc_code);
2385 *c = new_st;
2386 c->loc = gfc_current_locus;
2387
2388 if (gfc_match_eos () != MATCH_YES)
2389 goto syntax;
2390
2391 gfc_clear_new_st ();
2392 new_st.op = EXEC_FORALL;
2393 new_st.expr1 = mask;
2394 new_st.ext.forall_iterator = head;
2395 new_st.block = gfc_get_code (EXEC_FORALL);
2396 new_st.block->next = c;
2397
2398 return MATCH_YES;
2399
2400 syntax:
2401 gfc_syntax_error (ST_FORALL);
2402
2403 cleanup:
2404 gfc_free_forall_iterator (head);
2405 gfc_free_expr (mask);
2406
2407 return MATCH_ERROR;
2408 }
2409
2410
2411 /* Match a FORALL statement. */
2412
2413 match
2414 gfc_match_forall (gfc_statement *st)
2415 {
2416 gfc_forall_iterator *head;
2417 gfc_expr *mask;
2418 gfc_code *c;
2419 match m0, m;
2420
2421 head = NULL;
2422 mask = NULL;
2423 c = NULL;
2424
2425 m0 = gfc_match_label ();
2426 if (m0 == MATCH_ERROR)
2427 return MATCH_ERROR;
2428
2429 m = gfc_match (" forall");
2430 if (m != MATCH_YES)
2431 return m;
2432
2433 m = match_forall_header (&head, &mask);
2434 if (m == MATCH_ERROR)
2435 goto cleanup;
2436 if (m == MATCH_NO)
2437 goto syntax;
2438
2439 if (gfc_match_eos () == MATCH_YES)
2440 {
2441 *st = ST_FORALL_BLOCK;
2442 new_st.op = EXEC_FORALL;
2443 new_st.expr1 = mask;
2444 new_st.ext.forall_iterator = head;
2445 return MATCH_YES;
2446 }
2447
2448 m = gfc_match_assignment ();
2449 if (m == MATCH_ERROR)
2450 goto cleanup;
2451 if (m == MATCH_NO)
2452 {
2453 m = gfc_match_pointer_assignment ();
2454 if (m == MATCH_ERROR)
2455 goto cleanup;
2456 if (m == MATCH_NO)
2457 goto syntax;
2458 }
2459
2460 c = XCNEW (gfc_code);
2461 *c = new_st;
2462 c->loc = gfc_current_locus;
2463
2464 gfc_clear_new_st ();
2465 new_st.op = EXEC_FORALL;
2466 new_st.expr1 = mask;
2467 new_st.ext.forall_iterator = head;
2468 new_st.block = gfc_get_code (EXEC_FORALL);
2469 new_st.block->next = c;
2470
2471 *st = ST_FORALL;
2472 return MATCH_YES;
2473
2474 syntax:
2475 gfc_syntax_error (ST_FORALL);
2476
2477 cleanup:
2478 gfc_free_forall_iterator (head);
2479 gfc_free_expr (mask);
2480 gfc_free_statements (c);
2481 return MATCH_NO;
2482 }
2483
2484
2485 /* Match a DO statement. */
2486
2487 match
2488 gfc_match_do (void)
2489 {
2490 gfc_iterator iter, *ip;
2491 locus old_loc;
2492 gfc_st_label *label;
2493 match m;
2494
2495 old_loc = gfc_current_locus;
2496
2497 label = NULL;
2498 iter.var = iter.start = iter.end = iter.step = NULL;
2499
2500 m = gfc_match_label ();
2501 if (m == MATCH_ERROR)
2502 return m;
2503
2504 if (gfc_match (" do") != MATCH_YES)
2505 return MATCH_NO;
2506
2507 m = gfc_match_st_label (&label);
2508 if (m == MATCH_ERROR)
2509 goto cleanup;
2510
2511 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2512
2513 if (gfc_match_eos () == MATCH_YES)
2514 {
2515 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2516 new_st.op = EXEC_DO_WHILE;
2517 goto done;
2518 }
2519
2520 /* Match an optional comma, if no comma is found, a space is obligatory. */
2521 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2522 return MATCH_NO;
2523
2524 /* Check for balanced parens. */
2525
2526 if (gfc_match_parens () == MATCH_ERROR)
2527 return MATCH_ERROR;
2528
2529 if (gfc_match (" concurrent") == MATCH_YES)
2530 {
2531 gfc_forall_iterator *head;
2532 gfc_expr *mask;
2533
2534 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2535 return MATCH_ERROR;
2536
2537
2538 mask = NULL;
2539 head = NULL;
2540 m = match_forall_header (&head, &mask);
2541
2542 if (m == MATCH_NO)
2543 return m;
2544 if (m == MATCH_ERROR)
2545 goto concurr_cleanup;
2546
2547 if (gfc_match_eos () != MATCH_YES)
2548 goto concurr_cleanup;
2549
2550 if (label != NULL
2551 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2552 goto concurr_cleanup;
2553
2554 new_st.label1 = label;
2555 new_st.op = EXEC_DO_CONCURRENT;
2556 new_st.expr1 = mask;
2557 new_st.ext.forall_iterator = head;
2558
2559 return MATCH_YES;
2560
2561 concurr_cleanup:
2562 gfc_syntax_error (ST_DO);
2563 gfc_free_expr (mask);
2564 gfc_free_forall_iterator (head);
2565 return MATCH_ERROR;
2566 }
2567
2568 /* See if we have a DO WHILE. */
2569 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2570 {
2571 new_st.op = EXEC_DO_WHILE;
2572 goto done;
2573 }
2574
2575 /* The abortive DO WHILE may have done something to the symbol
2576 table, so we start over. */
2577 gfc_undo_symbols ();
2578 gfc_current_locus = old_loc;
2579
2580 gfc_match_label (); /* This won't error. */
2581 gfc_match (" do "); /* This will work. */
2582
2583 gfc_match_st_label (&label); /* Can't error out. */
2584 gfc_match_char (','); /* Optional comma. */
2585
2586 m = gfc_match_iterator (&iter, 0);
2587 if (m == MATCH_NO)
2588 return MATCH_NO;
2589 if (m == MATCH_ERROR)
2590 goto cleanup;
2591
2592 iter.var->symtree->n.sym->attr.implied_index = 0;
2593 gfc_check_do_variable (iter.var->symtree);
2594
2595 if (gfc_match_eos () != MATCH_YES)
2596 {
2597 gfc_syntax_error (ST_DO);
2598 goto cleanup;
2599 }
2600
2601 new_st.op = EXEC_DO;
2602
2603 done:
2604 if (label != NULL
2605 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2606 goto cleanup;
2607
2608 new_st.label1 = label;
2609
2610 if (new_st.op == EXEC_DO_WHILE)
2611 new_st.expr1 = iter.end;
2612 else
2613 {
2614 new_st.ext.iterator = ip = gfc_get_iterator ();
2615 *ip = iter;
2616 }
2617
2618 return MATCH_YES;
2619
2620 cleanup:
2621 gfc_free_iterator (&iter, 0);
2622
2623 return MATCH_ERROR;
2624 }
2625
2626
2627 /* Match an EXIT or CYCLE statement. */
2628
2629 static match
2630 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2631 {
2632 gfc_state_data *p, *o;
2633 gfc_symbol *sym;
2634 match m;
2635 int cnt;
2636
2637 if (gfc_match_eos () == MATCH_YES)
2638 sym = NULL;
2639 else
2640 {
2641 char name[GFC_MAX_SYMBOL_LEN + 1];
2642 gfc_symtree* stree;
2643
2644 m = gfc_match ("% %n%t", name);
2645 if (m == MATCH_ERROR)
2646 return MATCH_ERROR;
2647 if (m == MATCH_NO)
2648 {
2649 gfc_syntax_error (st);
2650 return MATCH_ERROR;
2651 }
2652
2653 /* Find the corresponding symbol. If there's a BLOCK statement
2654 between here and the label, it is not in gfc_current_ns but a parent
2655 namespace! */
2656 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2657 if (!stree)
2658 {
2659 gfc_error ("Name %qs in %s statement at %C is unknown",
2660 name, gfc_ascii_statement (st));
2661 return MATCH_ERROR;
2662 }
2663
2664 sym = stree->n.sym;
2665 if (sym->attr.flavor != FL_LABEL)
2666 {
2667 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2668 name, gfc_ascii_statement (st));
2669 return MATCH_ERROR;
2670 }
2671 }
2672
2673 /* Find the loop specified by the label (or lack of a label). */
2674 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2675 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2676 o = p;
2677 else if (p->state == COMP_CRITICAL)
2678 {
2679 gfc_error("%s statement at %C leaves CRITICAL construct",
2680 gfc_ascii_statement (st));
2681 return MATCH_ERROR;
2682 }
2683 else if (p->state == COMP_DO_CONCURRENT
2684 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2685 {
2686 /* F2008, C821 & C845. */
2687 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2688 gfc_ascii_statement (st));
2689 return MATCH_ERROR;
2690 }
2691 else if ((sym && sym == p->sym)
2692 || (!sym && (p->state == COMP_DO
2693 || p->state == COMP_DO_CONCURRENT)))
2694 break;
2695
2696 if (p == NULL)
2697 {
2698 if (sym == NULL)
2699 gfc_error ("%s statement at %C is not within a construct",
2700 gfc_ascii_statement (st));
2701 else
2702 gfc_error ("%s statement at %C is not within construct %qs",
2703 gfc_ascii_statement (st), sym->name);
2704
2705 return MATCH_ERROR;
2706 }
2707
2708 /* Special checks for EXIT from non-loop constructs. */
2709 switch (p->state)
2710 {
2711 case COMP_DO:
2712 case COMP_DO_CONCURRENT:
2713 break;
2714
2715 case COMP_CRITICAL:
2716 /* This is already handled above. */
2717 gcc_unreachable ();
2718
2719 case COMP_ASSOCIATE:
2720 case COMP_BLOCK:
2721 case COMP_IF:
2722 case COMP_SELECT:
2723 case COMP_SELECT_TYPE:
2724 gcc_assert (sym);
2725 if (op == EXEC_CYCLE)
2726 {
2727 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2728 " construct %qs", sym->name);
2729 return MATCH_ERROR;
2730 }
2731 gcc_assert (op == EXEC_EXIT);
2732 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2733 " do-construct-name at %C"))
2734 return MATCH_ERROR;
2735 break;
2736
2737 default:
2738 gfc_error ("%s statement at %C is not applicable to construct %qs",
2739 gfc_ascii_statement (st), sym->name);
2740 return MATCH_ERROR;
2741 }
2742
2743 if (o != NULL)
2744 {
2745 gfc_error (is_oacc (p)
2746 ? "%s statement at %C leaving OpenACC structured block"
2747 : "%s statement at %C leaving OpenMP structured block",
2748 gfc_ascii_statement (st));
2749 return MATCH_ERROR;
2750 }
2751
2752 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2753 o = o->previous;
2754 if (cnt > 0
2755 && o != NULL
2756 && o->state == COMP_OMP_STRUCTURED_BLOCK
2757 && (o->head->op == EXEC_OACC_LOOP
2758 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2759 {
2760 int collapse = 1;
2761 gcc_assert (o->head->next != NULL
2762 && (o->head->next->op == EXEC_DO
2763 || o->head->next->op == EXEC_DO_WHILE)
2764 && o->previous != NULL
2765 && o->previous->tail->op == o->head->op);
2766 if (o->previous->tail->ext.omp_clauses != NULL
2767 && o->previous->tail->ext.omp_clauses->collapse > 1)
2768 collapse = o->previous->tail->ext.omp_clauses->collapse;
2769 if (st == ST_EXIT && cnt <= collapse)
2770 {
2771 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2772 return MATCH_ERROR;
2773 }
2774 if (st == ST_CYCLE && cnt < collapse)
2775 {
2776 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2777 " !$ACC LOOP loop");
2778 return MATCH_ERROR;
2779 }
2780 }
2781 if (cnt > 0
2782 && o != NULL
2783 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2784 && (o->head->op == EXEC_OMP_DO
2785 || o->head->op == EXEC_OMP_PARALLEL_DO
2786 || o->head->op == EXEC_OMP_SIMD
2787 || o->head->op == EXEC_OMP_DO_SIMD
2788 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2789 {
2790 int count = 1;
2791 gcc_assert (o->head->next != NULL
2792 && (o->head->next->op == EXEC_DO
2793 || o->head->next->op == EXEC_DO_WHILE)
2794 && o->previous != NULL
2795 && o->previous->tail->op == o->head->op);
2796 if (o->previous->tail->ext.omp_clauses != NULL)
2797 {
2798 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2799 count = o->previous->tail->ext.omp_clauses->collapse;
2800 if (o->previous->tail->ext.omp_clauses->orderedc)
2801 count = o->previous->tail->ext.omp_clauses->orderedc;
2802 }
2803 if (st == ST_EXIT && cnt <= count)
2804 {
2805 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2806 return MATCH_ERROR;
2807 }
2808 if (st == ST_CYCLE && cnt < count)
2809 {
2810 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2811 " !$OMP DO loop");
2812 return MATCH_ERROR;
2813 }
2814 }
2815
2816 /* Save the first statement in the construct - needed by the backend. */
2817 new_st.ext.which_construct = p->construct;
2818
2819 new_st.op = op;
2820
2821 return MATCH_YES;
2822 }
2823
2824
2825 /* Match the EXIT statement. */
2826
2827 match
2828 gfc_match_exit (void)
2829 {
2830 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2831 }
2832
2833
2834 /* Match the CYCLE statement. */
2835
2836 match
2837 gfc_match_cycle (void)
2838 {
2839 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2840 }
2841
2842
2843 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2844 requirements for a stop-code differ in the standards.
2845
2846 Fortran 95 has
2847
2848 R840 stop-stmt is STOP [ stop-code ]
2849 R841 stop-code is scalar-char-constant
2850 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2851
2852 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2853 Fortran 2008 has
2854
2855 R855 stop-stmt is STOP [ stop-code ]
2856 R856 allstop-stmt is ALL STOP [ stop-code ]
2857 R857 stop-code is scalar-default-char-constant-expr
2858 or scalar-int-constant-expr
2859
2860 For free-form source code, all standards contain a statement of the form:
2861
2862 A blank shall be used to separate names, constants, or labels from
2863 adjacent keywords, names, constants, or labels.
2864
2865 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2866
2867 STOP123
2868
2869 is valid, but it is invalid Fortran 2008. */
2870
2871 static match
2872 gfc_match_stopcode (gfc_statement st)
2873 {
2874 gfc_expr *e = NULL;
2875 match m;
2876 bool f95, f03;
2877
2878 /* Set f95 for -std=f95. */
2879 f95 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2880 | GFC_STD_F2008_OBS);
2881
2882 /* Set f03 for -std=f2003. */
2883 f03 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2884 | GFC_STD_F2008_OBS | GFC_STD_F2003);
2885
2886 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2887 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2888 {
2889 char c = gfc_peek_ascii_char ();
2890
2891 /* Look for end-of-statement. There is no stop-code. */
2892 if (c == '\n' || c == '!' || c == ';')
2893 goto done;
2894
2895 if (c != ' ')
2896 {
2897 gfc_error ("Blank required in %s statement near %C",
2898 gfc_ascii_statement (st));
2899 return MATCH_ERROR;
2900 }
2901 }
2902
2903 if (gfc_match_eos () != MATCH_YES)
2904 {
2905 int stopcode;
2906 locus old_locus;
2907
2908 /* First look for the F95 or F2003 digit [...] construct. */
2909 old_locus = gfc_current_locus;
2910 m = gfc_match_small_int (&stopcode);
2911 if (m == MATCH_YES && (f95 || f03))
2912 {
2913 if (stopcode < 0)
2914 {
2915 gfc_error ("STOP code at %C cannot be negative");
2916 return MATCH_ERROR;
2917 }
2918
2919 if (stopcode > 99999)
2920 {
2921 gfc_error ("STOP code at %C contains too many digits");
2922 return MATCH_ERROR;
2923 }
2924 }
2925
2926 /* Reset the locus and now load gfc_expr. */
2927 gfc_current_locus = old_locus;
2928 m = gfc_match_expr (&e);
2929 if (m == MATCH_ERROR)
2930 goto cleanup;
2931 if (m == MATCH_NO)
2932 goto syntax;
2933
2934 if (gfc_match_eos () != MATCH_YES)
2935 goto syntax;
2936 }
2937
2938 if (gfc_pure (NULL))
2939 {
2940 if (st == ST_ERROR_STOP)
2941 {
2942 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2943 "procedure", gfc_ascii_statement (st)))
2944 goto cleanup;
2945 }
2946 else
2947 {
2948 gfc_error ("%s statement not allowed in PURE procedure at %C",
2949 gfc_ascii_statement (st));
2950 goto cleanup;
2951 }
2952 }
2953
2954 gfc_unset_implicit_pure (NULL);
2955
2956 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2957 {
2958 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2959 goto cleanup;
2960 }
2961 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2962 {
2963 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2964 goto cleanup;
2965 }
2966
2967 if (e != NULL)
2968 {
2969 gfc_simplify_expr (e, 0);
2970
2971 /* Test for F95 and F2003 style STOP stop-code. */
2972 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
2973 {
2974 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
2975 "digit[digit[digit[digit[digit]]]]", &e->where);
2976 goto cleanup;
2977 }
2978
2979 /* Use the machinery for an initialization expression to reduce the
2980 stop-code to a constant. */
2981 gfc_init_expr_flag = true;
2982 gfc_reduce_init_expr (e);
2983 gfc_init_expr_flag = false;
2984
2985 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2986 {
2987 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2988 &e->where);
2989 goto cleanup;
2990 }
2991
2992 if (e->rank != 0)
2993 {
2994 gfc_error ("STOP code at %L must be scalar", &e->where);
2995 goto cleanup;
2996 }
2997
2998 if (e->ts.type == BT_CHARACTER
2999 && e->ts.kind != gfc_default_character_kind)
3000 {
3001 gfc_error ("STOP code at %L must be default character KIND=%d",
3002 &e->where, (int) gfc_default_character_kind);
3003 goto cleanup;
3004 }
3005
3006 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3007 {
3008 gfc_error ("STOP code at %L must be default integer KIND=%d",
3009 &e->where, (int) gfc_default_integer_kind);
3010 goto cleanup;
3011 }
3012 }
3013
3014 done:
3015
3016 switch (st)
3017 {
3018 case ST_STOP:
3019 new_st.op = EXEC_STOP;
3020 break;
3021 case ST_ERROR_STOP:
3022 new_st.op = EXEC_ERROR_STOP;
3023 break;
3024 case ST_PAUSE:
3025 new_st.op = EXEC_PAUSE;
3026 break;
3027 default:
3028 gcc_unreachable ();
3029 }
3030
3031 new_st.expr1 = e;
3032 new_st.ext.stop_code = -1;
3033
3034 return MATCH_YES;
3035
3036 syntax:
3037 gfc_syntax_error (st);
3038
3039 cleanup:
3040
3041 gfc_free_expr (e);
3042 return MATCH_ERROR;
3043 }
3044
3045
3046 /* Match the (deprecated) PAUSE statement. */
3047
3048 match
3049 gfc_match_pause (void)
3050 {
3051 match m;
3052
3053 m = gfc_match_stopcode (ST_PAUSE);
3054 if (m == MATCH_YES)
3055 {
3056 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3057 m = MATCH_ERROR;
3058 }
3059 return m;
3060 }
3061
3062
3063 /* Match the STOP statement. */
3064
3065 match
3066 gfc_match_stop (void)
3067 {
3068 return gfc_match_stopcode (ST_STOP);
3069 }
3070
3071
3072 /* Match the ERROR STOP statement. */
3073
3074 match
3075 gfc_match_error_stop (void)
3076 {
3077 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3078 return MATCH_ERROR;
3079
3080 return gfc_match_stopcode (ST_ERROR_STOP);
3081 }
3082
3083 /* Match EVENT POST/WAIT statement. Syntax:
3084 EVENT POST ( event-variable [, sync-stat-list] )
3085 EVENT WAIT ( event-variable [, wait-spec-list] )
3086 with
3087 wait-spec-list is sync-stat-list or until-spec
3088 until-spec is UNTIL_COUNT = scalar-int-expr
3089 sync-stat is STAT= or ERRMSG=. */
3090
3091 static match
3092 event_statement (gfc_statement st)
3093 {
3094 match m;
3095 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3096 bool saw_until_count, saw_stat, saw_errmsg;
3097
3098 tmp = eventvar = until_count = stat = errmsg = NULL;
3099 saw_until_count = saw_stat = saw_errmsg = false;
3100
3101 if (gfc_pure (NULL))
3102 {
3103 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3104 st == ST_EVENT_POST ? "POST" : "WAIT");
3105 return MATCH_ERROR;
3106 }
3107
3108 gfc_unset_implicit_pure (NULL);
3109
3110 if (flag_coarray == GFC_FCOARRAY_NONE)
3111 {
3112 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3113 return MATCH_ERROR;
3114 }
3115
3116 if (gfc_find_state (COMP_CRITICAL))
3117 {
3118 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3119 st == ST_EVENT_POST ? "POST" : "WAIT");
3120 return MATCH_ERROR;
3121 }
3122
3123 if (gfc_find_state (COMP_DO_CONCURRENT))
3124 {
3125 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3126 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3127 return MATCH_ERROR;
3128 }
3129
3130 if (gfc_match_char ('(') != MATCH_YES)
3131 goto syntax;
3132
3133 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3134 goto syntax;
3135 m = gfc_match_char (',');
3136 if (m == MATCH_ERROR)
3137 goto syntax;
3138 if (m == MATCH_NO)
3139 {
3140 m = gfc_match_char (')');
3141 if (m == MATCH_YES)
3142 goto done;
3143 goto syntax;
3144 }
3145
3146 for (;;)
3147 {
3148 m = gfc_match (" stat = %v", &tmp);
3149 if (m == MATCH_ERROR)
3150 goto syntax;
3151 if (m == MATCH_YES)
3152 {
3153 if (saw_stat)
3154 {
3155 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3156 goto cleanup;
3157 }
3158 stat = tmp;
3159 saw_stat = true;
3160
3161 m = gfc_match_char (',');
3162 if (m == MATCH_YES)
3163 continue;
3164
3165 tmp = NULL;
3166 break;
3167 }
3168
3169 m = gfc_match (" errmsg = %v", &tmp);
3170 if (m == MATCH_ERROR)
3171 goto syntax;
3172 if (m == MATCH_YES)
3173 {
3174 if (saw_errmsg)
3175 {
3176 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3177 goto cleanup;
3178 }
3179 errmsg = tmp;
3180 saw_errmsg = true;
3181
3182 m = gfc_match_char (',');
3183 if (m == MATCH_YES)
3184 continue;
3185
3186 tmp = NULL;
3187 break;
3188 }
3189
3190 m = gfc_match (" until_count = %e", &tmp);
3191 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3192 goto syntax;
3193 if (m == MATCH_YES)
3194 {
3195 if (saw_until_count)
3196 {
3197 gfc_error ("Redundant UNTIL_COUNT tag found at %L ",
3198 &tmp->where);
3199 goto cleanup;
3200 }
3201 until_count = tmp;
3202 saw_until_count = true;
3203
3204 m = gfc_match_char (',');
3205 if (m == MATCH_YES)
3206 continue;
3207
3208 tmp = NULL;
3209 break;
3210 }
3211
3212 break;
3213 }
3214
3215 if (m == MATCH_ERROR)
3216 goto syntax;
3217
3218 if (gfc_match (" )%t") != MATCH_YES)
3219 goto syntax;
3220
3221 done:
3222 switch (st)
3223 {
3224 case ST_EVENT_POST:
3225 new_st.op = EXEC_EVENT_POST;
3226 break;
3227 case ST_EVENT_WAIT:
3228 new_st.op = EXEC_EVENT_WAIT;
3229 break;
3230 default:
3231 gcc_unreachable ();
3232 }
3233
3234 new_st.expr1 = eventvar;
3235 new_st.expr2 = stat;
3236 new_st.expr3 = errmsg;
3237 new_st.expr4 = until_count;
3238
3239 return MATCH_YES;
3240
3241 syntax:
3242 gfc_syntax_error (st);
3243
3244 cleanup:
3245 if (until_count != tmp)
3246 gfc_free_expr (until_count);
3247 if (errmsg != tmp)
3248 gfc_free_expr (errmsg);
3249 if (stat != tmp)
3250 gfc_free_expr (stat);
3251
3252 gfc_free_expr (tmp);
3253 gfc_free_expr (eventvar);
3254
3255 return MATCH_ERROR;
3256
3257 }
3258
3259
3260 match
3261 gfc_match_event_post (void)
3262 {
3263 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT POST statement at %C"))
3264 return MATCH_ERROR;
3265
3266 return event_statement (ST_EVENT_POST);
3267 }
3268
3269
3270 match
3271 gfc_match_event_wait (void)
3272 {
3273 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT WAIT statement at %C"))
3274 return MATCH_ERROR;
3275
3276 return event_statement (ST_EVENT_WAIT);
3277 }
3278
3279
3280 /* Match LOCK/UNLOCK statement. Syntax:
3281 LOCK ( lock-variable [ , lock-stat-list ] )
3282 UNLOCK ( lock-variable [ , sync-stat-list ] )
3283 where lock-stat is ACQUIRED_LOCK or sync-stat
3284 and sync-stat is STAT= or ERRMSG=. */
3285
3286 static match
3287 lock_unlock_statement (gfc_statement st)
3288 {
3289 match m;
3290 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3291 bool saw_acq_lock, saw_stat, saw_errmsg;
3292
3293 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3294 saw_acq_lock = saw_stat = saw_errmsg = false;
3295
3296 if (gfc_pure (NULL))
3297 {
3298 gfc_error ("Image control statement %s at %C in PURE procedure",
3299 st == ST_LOCK ? "LOCK" : "UNLOCK");
3300 return MATCH_ERROR;
3301 }
3302
3303 gfc_unset_implicit_pure (NULL);
3304
3305 if (flag_coarray == GFC_FCOARRAY_NONE)
3306 {
3307 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3308 return MATCH_ERROR;
3309 }
3310
3311 if (gfc_find_state (COMP_CRITICAL))
3312 {
3313 gfc_error ("Image control statement %s at %C in CRITICAL block",
3314 st == ST_LOCK ? "LOCK" : "UNLOCK");
3315 return MATCH_ERROR;
3316 }
3317
3318 if (gfc_find_state (COMP_DO_CONCURRENT))
3319 {
3320 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3321 st == ST_LOCK ? "LOCK" : "UNLOCK");
3322 return MATCH_ERROR;
3323 }
3324
3325 if (gfc_match_char ('(') != MATCH_YES)
3326 goto syntax;
3327
3328 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3329 goto syntax;
3330 m = gfc_match_char (',');
3331 if (m == MATCH_ERROR)
3332 goto syntax;
3333 if (m == MATCH_NO)
3334 {
3335 m = gfc_match_char (')');
3336 if (m == MATCH_YES)
3337 goto done;
3338 goto syntax;
3339 }
3340
3341 for (;;)
3342 {
3343 m = gfc_match (" stat = %v", &tmp);
3344 if (m == MATCH_ERROR)
3345 goto syntax;
3346 if (m == MATCH_YES)
3347 {
3348 if (saw_stat)
3349 {
3350 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3351 goto cleanup;
3352 }
3353 stat = tmp;
3354 saw_stat = true;
3355
3356 m = gfc_match_char (',');
3357 if (m == MATCH_YES)
3358 continue;
3359
3360 tmp = NULL;
3361 break;
3362 }
3363
3364 m = gfc_match (" errmsg = %v", &tmp);
3365 if (m == MATCH_ERROR)
3366 goto syntax;
3367 if (m == MATCH_YES)
3368 {
3369 if (saw_errmsg)
3370 {
3371 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3372 goto cleanup;
3373 }
3374 errmsg = tmp;
3375 saw_errmsg = true;
3376
3377 m = gfc_match_char (',');
3378 if (m == MATCH_YES)
3379 continue;
3380
3381 tmp = NULL;
3382 break;
3383 }
3384
3385 m = gfc_match (" acquired_lock = %v", &tmp);
3386 if (m == MATCH_ERROR || st == ST_UNLOCK)
3387 goto syntax;
3388 if (m == MATCH_YES)
3389 {
3390 if (saw_acq_lock)
3391 {
3392 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
3393 &tmp->where);
3394 goto cleanup;
3395 }
3396 acq_lock = tmp;
3397 saw_acq_lock = true;
3398
3399 m = gfc_match_char (',');
3400 if (m == MATCH_YES)
3401 continue;
3402
3403 tmp = NULL;
3404 break;
3405 }
3406
3407 break;
3408 }
3409
3410 if (m == MATCH_ERROR)
3411 goto syntax;
3412
3413 if (gfc_match (" )%t") != MATCH_YES)
3414 goto syntax;
3415
3416 done:
3417 switch (st)
3418 {
3419 case ST_LOCK:
3420 new_st.op = EXEC_LOCK;
3421 break;
3422 case ST_UNLOCK:
3423 new_st.op = EXEC_UNLOCK;
3424 break;
3425 default:
3426 gcc_unreachable ();
3427 }
3428
3429 new_st.expr1 = lockvar;
3430 new_st.expr2 = stat;
3431 new_st.expr3 = errmsg;
3432 new_st.expr4 = acq_lock;
3433
3434 return MATCH_YES;
3435
3436 syntax:
3437 gfc_syntax_error (st);
3438
3439 cleanup:
3440 if (acq_lock != tmp)
3441 gfc_free_expr (acq_lock);
3442 if (errmsg != tmp)
3443 gfc_free_expr (errmsg);
3444 if (stat != tmp)
3445 gfc_free_expr (stat);
3446
3447 gfc_free_expr (tmp);
3448 gfc_free_expr (lockvar);
3449
3450 return MATCH_ERROR;
3451 }
3452
3453
3454 match
3455 gfc_match_lock (void)
3456 {
3457 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3458 return MATCH_ERROR;
3459
3460 return lock_unlock_statement (ST_LOCK);
3461 }
3462
3463
3464 match
3465 gfc_match_unlock (void)
3466 {
3467 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3468 return MATCH_ERROR;
3469
3470 return lock_unlock_statement (ST_UNLOCK);
3471 }
3472
3473
3474 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3475 SYNC ALL [(sync-stat-list)]
3476 SYNC MEMORY [(sync-stat-list)]
3477 SYNC IMAGES (image-set [, sync-stat-list] )
3478 with sync-stat is int-expr or *. */
3479
3480 static match
3481 sync_statement (gfc_statement st)
3482 {
3483 match m;
3484 gfc_expr *tmp, *imageset, *stat, *errmsg;
3485 bool saw_stat, saw_errmsg;
3486
3487 tmp = imageset = stat = errmsg = NULL;
3488 saw_stat = saw_errmsg = false;
3489
3490 if (gfc_pure (NULL))
3491 {
3492 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3493 return MATCH_ERROR;
3494 }
3495
3496 gfc_unset_implicit_pure (NULL);
3497
3498 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3499 return MATCH_ERROR;
3500
3501 if (flag_coarray == GFC_FCOARRAY_NONE)
3502 {
3503 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3504 "enable");
3505 return MATCH_ERROR;
3506 }
3507
3508 if (gfc_find_state (COMP_CRITICAL))
3509 {
3510 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3511 return MATCH_ERROR;
3512 }
3513
3514 if (gfc_find_state (COMP_DO_CONCURRENT))
3515 {
3516 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3517 return MATCH_ERROR;
3518 }
3519
3520 if (gfc_match_eos () == MATCH_YES)
3521 {
3522 if (st == ST_SYNC_IMAGES)
3523 goto syntax;
3524 goto done;
3525 }
3526
3527 if (gfc_match_char ('(') != MATCH_YES)
3528 goto syntax;
3529
3530 if (st == ST_SYNC_IMAGES)
3531 {
3532 /* Denote '*' as imageset == NULL. */
3533 m = gfc_match_char ('*');
3534 if (m == MATCH_ERROR)
3535 goto syntax;
3536 if (m == MATCH_NO)
3537 {
3538 if (gfc_match ("%e", &imageset) != MATCH_YES)
3539 goto syntax;
3540 }
3541 m = gfc_match_char (',');
3542 if (m == MATCH_ERROR)
3543 goto syntax;
3544 if (m == MATCH_NO)
3545 {
3546 m = gfc_match_char (')');
3547 if (m == MATCH_YES)
3548 goto done;
3549 goto syntax;
3550 }
3551 }
3552
3553 for (;;)
3554 {
3555 m = gfc_match (" stat = %v", &tmp);
3556 if (m == MATCH_ERROR)
3557 goto syntax;
3558 if (m == MATCH_YES)
3559 {
3560 if (saw_stat)
3561 {
3562 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3563 goto cleanup;
3564 }
3565 stat = tmp;
3566 saw_stat = true;
3567
3568 if (gfc_match_char (',') == MATCH_YES)
3569 continue;
3570
3571 tmp = NULL;
3572 break;
3573 }
3574
3575 m = gfc_match (" errmsg = %v", &tmp);
3576 if (m == MATCH_ERROR)
3577 goto syntax;
3578 if (m == MATCH_YES)
3579 {
3580 if (saw_errmsg)
3581 {
3582 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3583 goto cleanup;
3584 }
3585 errmsg = tmp;
3586 saw_errmsg = true;
3587
3588 if (gfc_match_char (',') == MATCH_YES)
3589 continue;
3590
3591 tmp = NULL;
3592 break;
3593 }
3594
3595 break;
3596 }
3597
3598 if (gfc_match (" )%t") != MATCH_YES)
3599 goto syntax;
3600
3601 done:
3602 switch (st)
3603 {
3604 case ST_SYNC_ALL:
3605 new_st.op = EXEC_SYNC_ALL;
3606 break;
3607 case ST_SYNC_IMAGES:
3608 new_st.op = EXEC_SYNC_IMAGES;
3609 break;
3610 case ST_SYNC_MEMORY:
3611 new_st.op = EXEC_SYNC_MEMORY;
3612 break;
3613 default:
3614 gcc_unreachable ();
3615 }
3616
3617 new_st.expr1 = imageset;
3618 new_st.expr2 = stat;
3619 new_st.expr3 = errmsg;
3620
3621 return MATCH_YES;
3622
3623 syntax:
3624 gfc_syntax_error (st);
3625
3626 cleanup:
3627 if (stat != tmp)
3628 gfc_free_expr (stat);
3629 if (errmsg != tmp)
3630 gfc_free_expr (errmsg);
3631
3632 gfc_free_expr (tmp);
3633 gfc_free_expr (imageset);
3634
3635 return MATCH_ERROR;
3636 }
3637
3638
3639 /* Match SYNC ALL statement. */
3640
3641 match
3642 gfc_match_sync_all (void)
3643 {
3644 return sync_statement (ST_SYNC_ALL);
3645 }
3646
3647
3648 /* Match SYNC IMAGES statement. */
3649
3650 match
3651 gfc_match_sync_images (void)
3652 {
3653 return sync_statement (ST_SYNC_IMAGES);
3654 }
3655
3656
3657 /* Match SYNC MEMORY statement. */
3658
3659 match
3660 gfc_match_sync_memory (void)
3661 {
3662 return sync_statement (ST_SYNC_MEMORY);
3663 }
3664
3665
3666 /* Match a CONTINUE statement. */
3667
3668 match
3669 gfc_match_continue (void)
3670 {
3671 if (gfc_match_eos () != MATCH_YES)
3672 {
3673 gfc_syntax_error (ST_CONTINUE);
3674 return MATCH_ERROR;
3675 }
3676
3677 new_st.op = EXEC_CONTINUE;
3678 return MATCH_YES;
3679 }
3680
3681
3682 /* Match the (deprecated) ASSIGN statement. */
3683
3684 match
3685 gfc_match_assign (void)
3686 {
3687 gfc_expr *expr;
3688 gfc_st_label *label;
3689
3690 if (gfc_match (" %l", &label) == MATCH_YES)
3691 {
3692 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3693 return MATCH_ERROR;
3694 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3695 {
3696 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3697 return MATCH_ERROR;
3698
3699 expr->symtree->n.sym->attr.assign = 1;
3700
3701 new_st.op = EXEC_LABEL_ASSIGN;
3702 new_st.label1 = label;
3703 new_st.expr1 = expr;
3704 return MATCH_YES;
3705 }
3706 }
3707 return MATCH_NO;
3708 }
3709
3710
3711 /* Match the GO TO statement. As a computed GOTO statement is
3712 matched, it is transformed into an equivalent SELECT block. No
3713 tree is necessary, and the resulting jumps-to-jumps are
3714 specifically optimized away by the back end. */
3715
3716 match
3717 gfc_match_goto (void)
3718 {
3719 gfc_code *head, *tail;
3720 gfc_expr *expr;
3721 gfc_case *cp;
3722 gfc_st_label *label;
3723 int i;
3724 match m;
3725
3726 if (gfc_match (" %l%t", &label) == MATCH_YES)
3727 {
3728 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3729 return MATCH_ERROR;
3730
3731 new_st.op = EXEC_GOTO;
3732 new_st.label1 = label;
3733 return MATCH_YES;
3734 }
3735
3736 /* The assigned GO TO statement. */
3737
3738 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3739 {
3740 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3741 return MATCH_ERROR;
3742
3743 new_st.op = EXEC_GOTO;
3744 new_st.expr1 = expr;
3745
3746 if (gfc_match_eos () == MATCH_YES)
3747 return MATCH_YES;
3748
3749 /* Match label list. */
3750 gfc_match_char (',');
3751 if (gfc_match_char ('(') != MATCH_YES)
3752 {
3753 gfc_syntax_error (ST_GOTO);
3754 return MATCH_ERROR;
3755 }
3756 head = tail = NULL;
3757
3758 do
3759 {
3760 m = gfc_match_st_label (&label);
3761 if (m != MATCH_YES)
3762 goto syntax;
3763
3764 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3765 goto cleanup;
3766
3767 if (head == NULL)
3768 head = tail = gfc_get_code (EXEC_GOTO);
3769 else
3770 {
3771 tail->block = gfc_get_code (EXEC_GOTO);
3772 tail = tail->block;
3773 }
3774
3775 tail->label1 = label;
3776 }
3777 while (gfc_match_char (',') == MATCH_YES);
3778
3779 if (gfc_match (")%t") != MATCH_YES)
3780 goto syntax;
3781
3782 if (head == NULL)
3783 {
3784 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3785 goto syntax;
3786 }
3787 new_st.block = head;
3788
3789 return MATCH_YES;
3790 }
3791
3792 /* Last chance is a computed GO TO statement. */
3793 if (gfc_match_char ('(') != MATCH_YES)
3794 {
3795 gfc_syntax_error (ST_GOTO);
3796 return MATCH_ERROR;
3797 }
3798
3799 head = tail = NULL;
3800 i = 1;
3801
3802 do
3803 {
3804 m = gfc_match_st_label (&label);
3805 if (m != MATCH_YES)
3806 goto syntax;
3807
3808 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3809 goto cleanup;
3810
3811 if (head == NULL)
3812 head = tail = gfc_get_code (EXEC_SELECT);
3813 else
3814 {
3815 tail->block = gfc_get_code (EXEC_SELECT);
3816 tail = tail->block;
3817 }
3818
3819 cp = gfc_get_case ();
3820 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3821 NULL, i++);
3822
3823 tail->ext.block.case_list = cp;
3824
3825 tail->next = gfc_get_code (EXEC_GOTO);
3826 tail->next->label1 = label;
3827 }
3828 while (gfc_match_char (',') == MATCH_YES);
3829
3830 if (gfc_match_char (')') != MATCH_YES)
3831 goto syntax;
3832
3833 if (head == NULL)
3834 {
3835 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3836 goto syntax;
3837 }
3838
3839 /* Get the rest of the statement. */
3840 gfc_match_char (',');
3841
3842 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3843 goto syntax;
3844
3845 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3846 return MATCH_ERROR;
3847
3848 /* At this point, a computed GOTO has been fully matched and an
3849 equivalent SELECT statement constructed. */
3850
3851 new_st.op = EXEC_SELECT;
3852 new_st.expr1 = NULL;
3853
3854 /* Hack: For a "real" SELECT, the expression is in expr. We put
3855 it in expr2 so we can distinguish then and produce the correct
3856 diagnostics. */
3857 new_st.expr2 = expr;
3858 new_st.block = head;
3859 return MATCH_YES;
3860
3861 syntax:
3862 gfc_syntax_error (ST_GOTO);
3863 cleanup:
3864 gfc_free_statements (head);
3865 return MATCH_ERROR;
3866 }
3867
3868
3869 /* Frees a list of gfc_alloc structures. */
3870
3871 void
3872 gfc_free_alloc_list (gfc_alloc *p)
3873 {
3874 gfc_alloc *q;
3875
3876 for (; p; p = q)
3877 {
3878 q = p->next;
3879 gfc_free_expr (p->expr);
3880 free (p);
3881 }
3882 }
3883
3884
3885 /* Match an ALLOCATE statement. */
3886
3887 match
3888 gfc_match_allocate (void)
3889 {
3890 gfc_alloc *head, *tail;
3891 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3892 gfc_typespec ts;
3893 gfc_symbol *sym;
3894 match m;
3895 locus old_locus, deferred_locus;
3896 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3897 bool saw_unlimited = false;
3898
3899 head = tail = NULL;
3900 stat = errmsg = source = mold = tmp = NULL;
3901 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3902
3903 if (gfc_match_char ('(') != MATCH_YES)
3904 goto syntax;
3905
3906 /* Match an optional type-spec. */
3907 old_locus = gfc_current_locus;
3908 m = gfc_match_type_spec (&ts);
3909 if (m == MATCH_ERROR)
3910 goto cleanup;
3911 else if (m == MATCH_NO)
3912 {
3913 char name[GFC_MAX_SYMBOL_LEN + 3];
3914
3915 if (gfc_match ("%n :: ", name) == MATCH_YES)
3916 {
3917 gfc_error ("Error in type-spec at %L", &old_locus);
3918 goto cleanup;
3919 }
3920
3921 ts.type = BT_UNKNOWN;
3922 }
3923 else
3924 {
3925 if (gfc_match (" :: ") == MATCH_YES)
3926 {
3927 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3928 &old_locus))
3929 goto cleanup;
3930
3931 if (ts.deferred)
3932 {
3933 gfc_error ("Type-spec at %L cannot contain a deferred "
3934 "type parameter", &old_locus);
3935 goto cleanup;
3936 }
3937
3938 if (ts.type == BT_CHARACTER)
3939 ts.u.cl->length_from_typespec = true;
3940 }
3941 else
3942 {
3943 ts.type = BT_UNKNOWN;
3944 gfc_current_locus = old_locus;
3945 }
3946 }
3947
3948 for (;;)
3949 {
3950 if (head == NULL)
3951 head = tail = gfc_get_alloc ();
3952 else
3953 {
3954 tail->next = gfc_get_alloc ();
3955 tail = tail->next;
3956 }
3957
3958 m = gfc_match_variable (&tail->expr, 0);
3959 if (m == MATCH_NO)
3960 goto syntax;
3961 if (m == MATCH_ERROR)
3962 goto cleanup;
3963
3964 if (gfc_check_do_variable (tail->expr->symtree))
3965 goto cleanup;
3966
3967 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3968 if (impure && gfc_pure (NULL))
3969 {
3970 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3971 goto cleanup;
3972 }
3973
3974 if (impure)
3975 gfc_unset_implicit_pure (NULL);
3976
3977 if (tail->expr->ts.deferred)
3978 {
3979 saw_deferred = true;
3980 deferred_locus = tail->expr->where;
3981 }
3982
3983 if (gfc_find_state (COMP_DO_CONCURRENT)
3984 || gfc_find_state (COMP_CRITICAL))
3985 {
3986 gfc_ref *ref;
3987 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3988 for (ref = tail->expr->ref; ref; ref = ref->next)
3989 if (ref->type == REF_COMPONENT)
3990 coarray = ref->u.c.component->attr.codimension;
3991
3992 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
3993 {
3994 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3995 goto cleanup;
3996 }
3997 if (coarray && gfc_find_state (COMP_CRITICAL))
3998 {
3999 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4000 goto cleanup;
4001 }
4002 }
4003
4004 /* Check for F08:C628. */
4005 sym = tail->expr->symtree->n.sym;
4006 b1 = !(tail->expr->ref
4007 && (tail->expr->ref->type == REF_COMPONENT
4008 || tail->expr->ref->type == REF_ARRAY));
4009 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4010 b2 = !(CLASS_DATA (sym)->attr.allocatable
4011 || CLASS_DATA (sym)->attr.class_pointer);
4012 else
4013 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4014 || sym->attr.proc_pointer);
4015 b3 = sym && sym->ns && sym->ns->proc_name
4016 && (sym->ns->proc_name->attr.allocatable
4017 || sym->ns->proc_name->attr.pointer
4018 || sym->ns->proc_name->attr.proc_pointer);
4019 if (b1 && b2 && !b3)
4020 {
4021 gfc_error ("Allocate-object at %L is neither a data pointer "
4022 "nor an allocatable variable", &tail->expr->where);
4023 goto cleanup;
4024 }
4025
4026 /* The ALLOCATE statement had an optional typespec. Check the
4027 constraints. */
4028 if (ts.type != BT_UNKNOWN)
4029 {
4030 /* Enforce F03:C624. */
4031 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4032 {
4033 gfc_error ("Type of entity at %L is type incompatible with "
4034 "typespec", &tail->expr->where);
4035 goto cleanup;
4036 }
4037
4038 /* Enforce F03:C627. */
4039 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4040 {
4041 gfc_error ("Kind type parameter for entity at %L differs from "
4042 "the kind type parameter of the typespec",
4043 &tail->expr->where);
4044 goto cleanup;
4045 }
4046 }
4047
4048 if (tail->expr->ts.type == BT_DERIVED)
4049 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4050
4051 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4052
4053 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4054 {
4055 gfc_error ("Shape specification for allocatable scalar at %C");
4056 goto cleanup;
4057 }
4058
4059 if (gfc_match_char (',') != MATCH_YES)
4060 break;
4061
4062 alloc_opt_list:
4063
4064 m = gfc_match (" stat = %v", &tmp);
4065 if (m == MATCH_ERROR)
4066 goto cleanup;
4067 if (m == MATCH_YES)
4068 {
4069 /* Enforce C630. */
4070 if (saw_stat)
4071 {
4072 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4073 goto cleanup;
4074 }
4075
4076 stat = tmp;
4077 tmp = NULL;
4078 saw_stat = true;
4079
4080 if (gfc_check_do_variable (stat->symtree))
4081 goto cleanup;
4082
4083 if (gfc_match_char (',') == MATCH_YES)
4084 goto alloc_opt_list;
4085 }
4086
4087 m = gfc_match (" errmsg = %v", &tmp);
4088 if (m == MATCH_ERROR)
4089 goto cleanup;
4090 if (m == MATCH_YES)
4091 {
4092 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4093 goto cleanup;
4094
4095 /* Enforce C630. */
4096 if (saw_errmsg)
4097 {
4098 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4099 goto cleanup;
4100 }
4101
4102 errmsg = tmp;
4103 tmp = NULL;
4104 saw_errmsg = true;
4105
4106 if (gfc_match_char (',') == MATCH_YES)
4107 goto alloc_opt_list;
4108 }
4109
4110 m = gfc_match (" source = %e", &tmp);
4111 if (m == MATCH_ERROR)
4112 goto cleanup;
4113 if (m == MATCH_YES)
4114 {
4115 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4116 goto cleanup;
4117
4118 /* Enforce C630. */
4119 if (saw_source)
4120 {
4121 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
4122 goto cleanup;
4123 }
4124
4125 /* The next 2 conditionals check C631. */
4126 if (ts.type != BT_UNKNOWN)
4127 {
4128 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4129 &tmp->where, &old_locus);
4130 goto cleanup;
4131 }
4132
4133 if (head->next
4134 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4135 " with more than a single allocate object",
4136 &tmp->where))
4137 goto cleanup;
4138
4139 source = tmp;
4140 tmp = NULL;
4141 saw_source = true;
4142
4143 if (gfc_match_char (',') == MATCH_YES)
4144 goto alloc_opt_list;
4145 }
4146
4147 m = gfc_match (" mold = %e", &tmp);
4148 if (m == MATCH_ERROR)
4149 goto cleanup;
4150 if (m == MATCH_YES)
4151 {
4152 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4153 goto cleanup;
4154
4155 /* Check F08:C636. */
4156 if (saw_mold)
4157 {
4158 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
4159 goto cleanup;
4160 }
4161
4162 /* Check F08:C637. */
4163 if (ts.type != BT_UNKNOWN)
4164 {
4165 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4166 &tmp->where, &old_locus);
4167 goto cleanup;
4168 }
4169
4170 mold = tmp;
4171 tmp = NULL;
4172 saw_mold = true;
4173 mold->mold = 1;
4174
4175 if (gfc_match_char (',') == MATCH_YES)
4176 goto alloc_opt_list;
4177 }
4178
4179 gfc_gobble_whitespace ();
4180
4181 if (gfc_peek_char () == ')')
4182 break;
4183 }
4184
4185 if (gfc_match (" )%t") != MATCH_YES)
4186 goto syntax;
4187
4188 /* Check F08:C637. */
4189 if (source && mold)
4190 {
4191 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4192 &mold->where, &source->where);
4193 goto cleanup;
4194 }
4195
4196 /* Check F03:C623, */
4197 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4198 {
4199 gfc_error ("Allocate-object at %L with a deferred type parameter "
4200 "requires either a type-spec or SOURCE tag or a MOLD tag",
4201 &deferred_locus);
4202 goto cleanup;
4203 }
4204
4205 /* Check F03:C625, */
4206 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4207 {
4208 for (tail = head; tail; tail = tail->next)
4209 {
4210 if (UNLIMITED_POLY (tail->expr))
4211 gfc_error ("Unlimited polymorphic allocate-object at %L "
4212 "requires either a type-spec or SOURCE tag "
4213 "or a MOLD tag", &tail->expr->where);
4214 }
4215 goto cleanup;
4216 }
4217
4218 new_st.op = EXEC_ALLOCATE;
4219 new_st.expr1 = stat;
4220 new_st.expr2 = errmsg;
4221 if (source)
4222 new_st.expr3 = source;
4223 else
4224 new_st.expr3 = mold;
4225 new_st.ext.alloc.list = head;
4226 new_st.ext.alloc.ts = ts;
4227
4228 return MATCH_YES;
4229
4230 syntax:
4231 gfc_syntax_error (ST_ALLOCATE);
4232
4233 cleanup:
4234 gfc_free_expr (errmsg);
4235 gfc_free_expr (source);
4236 gfc_free_expr (stat);
4237 gfc_free_expr (mold);
4238 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4239 gfc_free_alloc_list (head);
4240 return MATCH_ERROR;
4241 }
4242
4243
4244 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4245 a set of pointer assignments to intrinsic NULL(). */
4246
4247 match
4248 gfc_match_nullify (void)
4249 {
4250 gfc_code *tail;
4251 gfc_expr *e, *p;
4252 match m;
4253
4254 tail = NULL;
4255
4256 if (gfc_match_char ('(') != MATCH_YES)
4257 goto syntax;
4258
4259 for (;;)
4260 {
4261 m = gfc_match_variable (&p, 0);
4262 if (m == MATCH_ERROR)
4263 goto cleanup;
4264 if (m == MATCH_NO)
4265 goto syntax;
4266
4267 if (gfc_check_do_variable (p->symtree))
4268 goto cleanup;
4269
4270 /* F2008, C1242. */
4271 if (gfc_is_coindexed (p))
4272 {
4273 gfc_error ("Pointer object at %C shall not be coindexed");
4274 goto cleanup;
4275 }
4276
4277 /* build ' => NULL() '. */
4278 e = gfc_get_null_expr (&gfc_current_locus);
4279
4280 /* Chain to list. */
4281 if (tail == NULL)
4282 {
4283 tail = &new_st;
4284 tail->op = EXEC_POINTER_ASSIGN;
4285 }
4286 else
4287 {
4288 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4289 tail = tail->next;
4290 }
4291
4292 tail->expr1 = p;
4293 tail->expr2 = e;
4294
4295 if (gfc_match (" )%t") == MATCH_YES)
4296 break;
4297 if (gfc_match_char (',') != MATCH_YES)
4298 goto syntax;
4299 }
4300
4301 return MATCH_YES;
4302
4303 syntax:
4304 gfc_syntax_error (ST_NULLIFY);
4305
4306 cleanup:
4307 gfc_free_statements (new_st.next);
4308 new_st.next = NULL;
4309 gfc_free_expr (new_st.expr1);
4310 new_st.expr1 = NULL;
4311 gfc_free_expr (new_st.expr2);
4312 new_st.expr2 = NULL;
4313 return MATCH_ERROR;
4314 }
4315
4316
4317 /* Match a DEALLOCATE statement. */
4318
4319 match
4320 gfc_match_deallocate (void)
4321 {
4322 gfc_alloc *head, *tail;
4323 gfc_expr *stat, *errmsg, *tmp;
4324 gfc_symbol *sym;
4325 match m;
4326 bool saw_stat, saw_errmsg, b1, b2;
4327
4328 head = tail = NULL;
4329 stat = errmsg = tmp = NULL;
4330 saw_stat = saw_errmsg = false;
4331
4332 if (gfc_match_char ('(') != MATCH_YES)
4333 goto syntax;
4334
4335 for (;;)
4336 {
4337 if (head == NULL)
4338 head = tail = gfc_get_alloc ();
4339 else
4340 {
4341 tail->next = gfc_get_alloc ();
4342 tail = tail->next;
4343 }
4344
4345 m = gfc_match_variable (&tail->expr, 0);
4346 if (m == MATCH_ERROR)
4347 goto cleanup;
4348 if (m == MATCH_NO)
4349 goto syntax;
4350
4351 if (gfc_check_do_variable (tail->expr->symtree))
4352 goto cleanup;
4353
4354 sym = tail->expr->symtree->n.sym;
4355
4356 bool impure = gfc_impure_variable (sym);
4357 if (impure && gfc_pure (NULL))
4358 {
4359 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4360 goto cleanup;
4361 }
4362
4363 if (impure)
4364 gfc_unset_implicit_pure (NULL);
4365
4366 if (gfc_is_coarray (tail->expr)
4367 && gfc_find_state (COMP_DO_CONCURRENT))
4368 {
4369 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4370 goto cleanup;
4371 }
4372
4373 if (gfc_is_coarray (tail->expr)
4374 && gfc_find_state (COMP_CRITICAL))
4375 {
4376 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4377 goto cleanup;
4378 }
4379
4380 /* FIXME: disable the checking on derived types. */
4381 b1 = !(tail->expr->ref
4382 && (tail->expr->ref->type == REF_COMPONENT
4383 || tail->expr->ref->type == REF_ARRAY));
4384 if (sym && sym->ts.type == BT_CLASS)
4385 b2 = !(CLASS_DATA (sym)->attr.allocatable
4386 || CLASS_DATA (sym)->attr.class_pointer);
4387 else
4388 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4389 || sym->attr.proc_pointer);
4390 if (b1 && b2)
4391 {
4392 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4393 "nor an allocatable variable");
4394 goto cleanup;
4395 }
4396
4397 if (gfc_match_char (',') != MATCH_YES)
4398 break;
4399
4400 dealloc_opt_list:
4401
4402 m = gfc_match (" stat = %v", &tmp);
4403 if (m == MATCH_ERROR)
4404 goto cleanup;
4405 if (m == MATCH_YES)
4406 {
4407 if (saw_stat)
4408 {
4409 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4410 gfc_free_expr (tmp);
4411 goto cleanup;
4412 }
4413
4414 stat = tmp;
4415 saw_stat = true;
4416
4417 if (gfc_check_do_variable (stat->symtree))
4418 goto cleanup;
4419
4420 if (gfc_match_char (',') == MATCH_YES)
4421 goto dealloc_opt_list;
4422 }
4423
4424 m = gfc_match (" errmsg = %v", &tmp);
4425 if (m == MATCH_ERROR)
4426 goto cleanup;
4427 if (m == MATCH_YES)
4428 {
4429 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4430 goto cleanup;
4431
4432 if (saw_errmsg)
4433 {
4434 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4435 gfc_free_expr (tmp);
4436 goto cleanup;
4437 }
4438
4439 errmsg = tmp;
4440 saw_errmsg = true;
4441
4442 if (gfc_match_char (',') == MATCH_YES)
4443 goto dealloc_opt_list;
4444 }
4445
4446 gfc_gobble_whitespace ();
4447
4448 if (gfc_peek_char () == ')')
4449 break;
4450 }
4451
4452 if (gfc_match (" )%t") != MATCH_YES)
4453 goto syntax;
4454
4455 new_st.op = EXEC_DEALLOCATE;
4456 new_st.expr1 = stat;
4457 new_st.expr2 = errmsg;
4458 new_st.ext.alloc.list = head;
4459
4460 return MATCH_YES;
4461
4462 syntax:
4463 gfc_syntax_error (ST_DEALLOCATE);
4464
4465 cleanup:
4466 gfc_free_expr (errmsg);
4467 gfc_free_expr (stat);
4468 gfc_free_alloc_list (head);
4469 return MATCH_ERROR;
4470 }
4471
4472
4473 /* Match a RETURN statement. */
4474
4475 match
4476 gfc_match_return (void)
4477 {
4478 gfc_expr *e;
4479 match m;
4480 gfc_compile_state s;
4481
4482 e = NULL;
4483
4484 if (gfc_find_state (COMP_CRITICAL))
4485 {
4486 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4487 return MATCH_ERROR;
4488 }
4489
4490 if (gfc_find_state (COMP_DO_CONCURRENT))
4491 {
4492 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4493 return MATCH_ERROR;
4494 }
4495
4496 if (gfc_match_eos () == MATCH_YES)
4497 goto done;
4498
4499 if (!gfc_find_state (COMP_SUBROUTINE))
4500 {
4501 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4502 "a SUBROUTINE");
4503 goto cleanup;
4504 }
4505
4506 if (gfc_current_form == FORM_FREE)
4507 {
4508 /* The following are valid, so we can't require a blank after the
4509 RETURN keyword:
4510 return+1
4511 return(1) */
4512 char c = gfc_peek_ascii_char ();
4513 if (ISALPHA (c) || ISDIGIT (c))
4514 return MATCH_NO;
4515 }
4516
4517 m = gfc_match (" %e%t", &e);
4518 if (m == MATCH_YES)
4519 goto done;
4520 if (m == MATCH_ERROR)
4521 goto cleanup;
4522
4523 gfc_syntax_error (ST_RETURN);
4524
4525 cleanup:
4526 gfc_free_expr (e);
4527 return MATCH_ERROR;
4528
4529 done:
4530 gfc_enclosing_unit (&s);
4531 if (s == COMP_PROGRAM
4532 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4533 "main program at %C"))
4534 return MATCH_ERROR;
4535
4536 new_st.op = EXEC_RETURN;
4537 new_st.expr1 = e;
4538
4539 return MATCH_YES;
4540 }
4541
4542
4543 /* Match the call of a type-bound procedure, if CALL%var has already been
4544 matched and var found to be a derived-type variable. */
4545
4546 static match
4547 match_typebound_call (gfc_symtree* varst)
4548 {
4549 gfc_expr* base;
4550 match m;
4551
4552 base = gfc_get_expr ();
4553 base->expr_type = EXPR_VARIABLE;
4554 base->symtree = varst;
4555 base->where = gfc_current_locus;
4556 gfc_set_sym_referenced (varst->n.sym);
4557
4558 m = gfc_match_varspec (base, 0, true, true);
4559 if (m == MATCH_NO)
4560 gfc_error ("Expected component reference at %C");
4561 if (m != MATCH_YES)
4562 {
4563 gfc_free_expr (base);
4564 return MATCH_ERROR;
4565 }
4566
4567 if (gfc_match_eos () != MATCH_YES)
4568 {
4569 gfc_error ("Junk after CALL at %C");
4570 gfc_free_expr (base);
4571 return MATCH_ERROR;
4572 }
4573
4574 if (base->expr_type == EXPR_COMPCALL)
4575 new_st.op = EXEC_COMPCALL;
4576 else if (base->expr_type == EXPR_PPC)
4577 new_st.op = EXEC_CALL_PPC;
4578 else
4579 {
4580 gfc_error ("Expected type-bound procedure or procedure pointer component "
4581 "at %C");
4582 gfc_free_expr (base);
4583 return MATCH_ERROR;
4584 }
4585 new_st.expr1 = base;
4586
4587 return MATCH_YES;
4588 }
4589
4590
4591 /* Match a CALL statement. The tricky part here are possible
4592 alternate return specifiers. We handle these by having all
4593 "subroutines" actually return an integer via a register that gives
4594 the return number. If the call specifies alternate returns, we
4595 generate code for a SELECT statement whose case clauses contain
4596 GOTOs to the various labels. */
4597
4598 match
4599 gfc_match_call (void)
4600 {
4601 char name[GFC_MAX_SYMBOL_LEN + 1];
4602 gfc_actual_arglist *a, *arglist;
4603 gfc_case *new_case;
4604 gfc_symbol *sym;
4605 gfc_symtree *st;
4606 gfc_code *c;
4607 match m;
4608 int i;
4609
4610 arglist = NULL;
4611
4612 m = gfc_match ("% %n", name);
4613 if (m == MATCH_NO)
4614 goto syntax;
4615 if (m != MATCH_YES)
4616 return m;
4617
4618 if (gfc_get_ha_sym_tree (name, &st))
4619 return MATCH_ERROR;
4620
4621 sym = st->n.sym;
4622
4623 /* If this is a variable of derived-type, it probably starts a type-bound
4624 procedure call. */
4625 if ((sym->attr.flavor != FL_PROCEDURE
4626 || gfc_is_function_return_value (sym, gfc_current_ns))
4627 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4628 return match_typebound_call (st);
4629
4630 /* If it does not seem to be callable (include functions so that the
4631 right association is made. They are thrown out in resolution.)
4632 ... */
4633 if (!sym->attr.generic
4634 && !sym->attr.subroutine
4635 && !sym->attr.function)
4636 {
4637 if (!(sym->attr.external && !sym->attr.referenced))
4638 {
4639 /* ...create a symbol in this scope... */
4640 if (sym->ns != gfc_current_ns
4641 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4642 return MATCH_ERROR;
4643
4644 if (sym != st->n.sym)
4645 sym = st->n.sym;
4646 }
4647
4648 /* ...and then to try to make the symbol into a subroutine. */
4649 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4650 return MATCH_ERROR;
4651 }
4652
4653 gfc_set_sym_referenced (sym);
4654
4655 if (gfc_match_eos () != MATCH_YES)
4656 {
4657 m = gfc_match_actual_arglist (1, &arglist);
4658 if (m == MATCH_NO)
4659 goto syntax;
4660 if (m == MATCH_ERROR)
4661 goto cleanup;
4662
4663 if (gfc_match_eos () != MATCH_YES)
4664 goto syntax;
4665 }
4666
4667 /* If any alternate return labels were found, construct a SELECT
4668 statement that will jump to the right place. */
4669
4670 i = 0;
4671 for (a = arglist; a; a = a->next)
4672 if (a->expr == NULL)
4673 {
4674 i = 1;
4675 break;
4676 }
4677
4678 if (i)
4679 {
4680 gfc_symtree *select_st;
4681 gfc_symbol *select_sym;
4682 char name[GFC_MAX_SYMBOL_LEN + 1];
4683
4684 new_st.next = c = gfc_get_code (EXEC_SELECT);
4685 sprintf (name, "_result_%s", sym->name);
4686 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4687
4688 select_sym = select_st->n.sym;
4689 select_sym->ts.type = BT_INTEGER;
4690 select_sym->ts.kind = gfc_default_integer_kind;
4691 gfc_set_sym_referenced (select_sym);
4692 c->expr1 = gfc_get_expr ();
4693 c->expr1->expr_type = EXPR_VARIABLE;
4694 c->expr1->symtree = select_st;
4695 c->expr1->ts = select_sym->ts;
4696 c->expr1->where = gfc_current_locus;
4697
4698 i = 0;
4699 for (a = arglist; a; a = a->next)
4700 {
4701 if (a->expr != NULL)
4702 continue;
4703
4704 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4705 continue;
4706
4707 i++;
4708
4709 c->block = gfc_get_code (EXEC_SELECT);
4710 c = c->block;
4711
4712 new_case = gfc_get_case ();
4713 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4714 new_case->low = new_case->high;
4715 c->ext.block.case_list = new_case;
4716
4717 c->next = gfc_get_code (EXEC_GOTO);
4718 c->next->label1 = a->label;
4719 }
4720 }
4721
4722 new_st.op = EXEC_CALL;
4723 new_st.symtree = st;
4724 new_st.ext.actual = arglist;
4725
4726 return MATCH_YES;
4727
4728 syntax:
4729 gfc_syntax_error (ST_CALL);
4730
4731 cleanup:
4732 gfc_free_actual_arglist (arglist);
4733 return MATCH_ERROR;
4734 }
4735
4736
4737 /* Given a name, return a pointer to the common head structure,
4738 creating it if it does not exist. If FROM_MODULE is nonzero, we
4739 mangle the name so that it doesn't interfere with commons defined
4740 in the using namespace.
4741 TODO: Add to global symbol tree. */
4742
4743 gfc_common_head *
4744 gfc_get_common (const char *name, int from_module)
4745 {
4746 gfc_symtree *st;
4747 static int serial = 0;
4748 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4749
4750 if (from_module)
4751 {
4752 /* A use associated common block is only needed to correctly layout
4753 the variables it contains. */
4754 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4755 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4756 }
4757 else
4758 {
4759 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4760
4761 if (st == NULL)
4762 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4763 }
4764
4765 if (st->n.common == NULL)
4766 {
4767 st->n.common = gfc_get_common_head ();
4768 st->n.common->where = gfc_current_locus;
4769 strcpy (st->n.common->name, name);
4770 }
4771
4772 return st->n.common;
4773 }
4774
4775
4776 /* Match a common block name. */
4777
4778 match match_common_name (char *name)
4779 {
4780 match m;
4781
4782 if (gfc_match_char ('/') == MATCH_NO)
4783 {
4784 name[0] = '\0';
4785 return MATCH_YES;
4786 }
4787
4788 if (gfc_match_char ('/') == MATCH_YES)
4789 {
4790 name[0] = '\0';
4791 return MATCH_YES;
4792 }
4793
4794 m = gfc_match_name (name);
4795
4796 if (m == MATCH_ERROR)
4797 return MATCH_ERROR;
4798 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4799 return MATCH_YES;
4800
4801 gfc_error ("Syntax error in common block name at %C");
4802 return MATCH_ERROR;
4803 }
4804
4805
4806 /* Match a COMMON statement. */
4807
4808 match
4809 gfc_match_common (void)
4810 {
4811 gfc_symbol *sym, **head, *tail, *other;
4812 char name[GFC_MAX_SYMBOL_LEN + 1];
4813 gfc_common_head *t;
4814 gfc_array_spec *as;
4815 gfc_equiv *e1, *e2;
4816 match m;
4817
4818 as = NULL;
4819
4820 for (;;)
4821 {
4822 m = match_common_name (name);
4823 if (m == MATCH_ERROR)
4824 goto cleanup;
4825
4826 if (name[0] == '\0')
4827 {
4828 t = &gfc_current_ns->blank_common;
4829 if (t->head == NULL)
4830 t->where = gfc_current_locus;
4831 }
4832 else
4833 {
4834 t = gfc_get_common (name, 0);
4835 }
4836 head = &t->head;
4837
4838 if (*head == NULL)
4839 tail = NULL;
4840 else
4841 {
4842 tail = *head;
4843 while (tail->common_next)
4844 tail = tail->common_next;
4845 }
4846
4847 /* Grab the list of symbols. */
4848 for (;;)
4849 {
4850 m = gfc_match_symbol (&sym, 0);
4851 if (m == MATCH_ERROR)
4852 goto cleanup;
4853 if (m == MATCH_NO)
4854 goto syntax;
4855
4856 /* See if we know the current common block is bind(c), and if
4857 so, then see if we can check if the symbol is (which it'll
4858 need to be). This can happen if the bind(c) attr stmt was
4859 applied to the common block, and the variable(s) already
4860 defined, before declaring the common block. */
4861 if (t->is_bind_c == 1)
4862 {
4863 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4864 {
4865 /* If we find an error, just print it and continue,
4866 cause it's just semantic, and we can see if there
4867 are more errors. */
4868 gfc_error_now ("Variable %qs at %L in common block %qs "
4869 "at %C must be declared with a C "
4870 "interoperable kind since common block "
4871 "%qs is bind(c)",
4872 sym->name, &(sym->declared_at), t->name,
4873 t->name);
4874 }
4875
4876 if (sym->attr.is_bind_c == 1)
4877 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4878 "be bind(c) since it is not global", sym->name,
4879 t->name);
4880 }
4881
4882 if (sym->attr.in_common)
4883 {
4884 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4885 sym->name);
4886 goto cleanup;
4887 }
4888
4889 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4890 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4891 {
4892 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4893 "%C can only be COMMON in BLOCK DATA",
4894 sym->name))
4895 goto cleanup;
4896 }
4897
4898 /* Deal with an optional array specification after the
4899 symbol name. */
4900 m = gfc_match_array_spec (&as, true, true);
4901 if (m == MATCH_ERROR)
4902 goto cleanup;
4903
4904 if (m == MATCH_YES)
4905 {
4906 if (as->type != AS_EXPLICIT)
4907 {
4908 gfc_error ("Array specification for symbol %qs in COMMON "
4909 "at %C must be explicit", sym->name);
4910 goto cleanup;
4911 }
4912
4913 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4914 goto cleanup;
4915
4916 if (sym->attr.pointer)
4917 {
4918 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4919 "POINTER array", sym->name);
4920 goto cleanup;
4921 }
4922
4923 sym->as = as;
4924 as = NULL;
4925
4926 }
4927
4928 /* Add the in_common attribute, but ignore the reported errors
4929 if any, and continue matching. */
4930 gfc_add_in_common (&sym->attr, sym->name, NULL);
4931
4932 sym->common_block = t;
4933 sym->common_block->refs++;
4934
4935 if (tail != NULL)
4936 tail->common_next = sym;
4937 else
4938 *head = sym;
4939
4940 tail = sym;
4941
4942 sym->common_head = t;
4943
4944 /* Check to see if the symbol is already in an equivalence group.
4945 If it is, set the other members as being in common. */
4946 if (sym->attr.in_equivalence)
4947 {
4948 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4949 {
4950 for (e2 = e1; e2; e2 = e2->eq)
4951 if (e2->expr->symtree->n.sym == sym)
4952 goto equiv_found;
4953
4954 continue;
4955
4956 equiv_found:
4957
4958 for (e2 = e1; e2; e2 = e2->eq)
4959 {
4960 other = e2->expr->symtree->n.sym;
4961 if (other->common_head
4962 && other->common_head != sym->common_head)
4963 {
4964 gfc_error ("Symbol %qs, in COMMON block %qs at "
4965 "%C is being indirectly equivalenced to "
4966 "another COMMON block %qs",
4967 sym->name, sym->common_head->name,
4968 other->common_head->name);
4969 goto cleanup;
4970 }
4971 other->attr.in_common = 1;
4972 other->common_head = t;
4973 }
4974 }
4975 }
4976
4977
4978 gfc_gobble_whitespace ();
4979 if (gfc_match_eos () == MATCH_YES)
4980 goto done;
4981 if (gfc_peek_ascii_char () == '/')
4982 break;
4983 if (gfc_match_char (',') != MATCH_YES)
4984 goto syntax;
4985 gfc_gobble_whitespace ();
4986 if (gfc_peek_ascii_char () == '/')
4987 break;
4988 }
4989 }
4990
4991 done:
4992 return MATCH_YES;
4993
4994 syntax:
4995 gfc_syntax_error (ST_COMMON);
4996
4997 cleanup:
4998 gfc_free_array_spec (as);
4999 return MATCH_ERROR;
5000 }
5001
5002
5003 /* Match a BLOCK DATA program unit. */
5004
5005 match
5006 gfc_match_block_data (void)
5007 {
5008 char name[GFC_MAX_SYMBOL_LEN + 1];
5009 gfc_symbol *sym;
5010 match m;
5011
5012 if (gfc_match_eos () == MATCH_YES)
5013 {
5014 gfc_new_block = NULL;
5015 return MATCH_YES;
5016 }
5017
5018 m = gfc_match ("% %n%t", name);
5019 if (m != MATCH_YES)
5020 return MATCH_ERROR;
5021
5022 if (gfc_get_symbol (name, NULL, &sym))
5023 return MATCH_ERROR;
5024
5025 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5026 return MATCH_ERROR;
5027
5028 gfc_new_block = sym;
5029
5030 return MATCH_YES;
5031 }
5032
5033
5034 /* Free a namelist structure. */
5035
5036 void
5037 gfc_free_namelist (gfc_namelist *name)
5038 {
5039 gfc_namelist *n;
5040
5041 for (; name; name = n)
5042 {
5043 n = name->next;
5044 free (name);
5045 }
5046 }
5047
5048
5049 /* Free an OpenMP namelist structure. */
5050
5051 void
5052 gfc_free_omp_namelist (gfc_omp_namelist *name)
5053 {
5054 gfc_omp_namelist *n;
5055
5056 for (; name; name = n)
5057 {
5058 gfc_free_expr (name->expr);
5059 if (name->udr)
5060 {
5061 if (name->udr->combiner)
5062 gfc_free_statement (name->udr->combiner);
5063 if (name->udr->initializer)
5064 gfc_free_statement (name->udr->initializer);
5065 free (name->udr);
5066 }
5067 n = name->next;
5068 free (name);
5069 }
5070 }
5071
5072
5073 /* Match a NAMELIST statement. */
5074
5075 match
5076 gfc_match_namelist (void)
5077 {
5078 gfc_symbol *group_name, *sym;
5079 gfc_namelist *nl;
5080 match m, m2;
5081
5082 m = gfc_match (" / %s /", &group_name);
5083 if (m == MATCH_NO)
5084 goto syntax;
5085 if (m == MATCH_ERROR)
5086 goto error;
5087
5088 for (;;)
5089 {
5090 if (group_name->ts.type != BT_UNKNOWN)
5091 {
5092 gfc_error ("Namelist group name %qs at %C already has a basic "
5093 "type of %s", group_name->name,
5094 gfc_typename (&group_name->ts));
5095 return MATCH_ERROR;
5096 }
5097
5098 if (group_name->attr.flavor == FL_NAMELIST
5099 && group_name->attr.use_assoc
5100 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5101 "at %C already is USE associated and can"
5102 "not be respecified.", group_name->name))
5103 return MATCH_ERROR;
5104
5105 if (group_name->attr.flavor != FL_NAMELIST
5106 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5107 group_name->name, NULL))
5108 return MATCH_ERROR;
5109
5110 for (;;)
5111 {
5112 m = gfc_match_symbol (&sym, 1);
5113 if (m == MATCH_NO)
5114 goto syntax;
5115 if (m == MATCH_ERROR)
5116 goto error;
5117
5118 if (sym->attr.in_namelist == 0
5119 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5120 goto error;
5121
5122 /* Use gfc_error_check here, rather than goto error, so that
5123 these are the only errors for the next two lines. */
5124 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5125 {
5126 gfc_error ("Assumed size array %qs in namelist %qs at "
5127 "%C is not allowed", sym->name, group_name->name);
5128 gfc_error_check ();
5129 }
5130
5131 nl = gfc_get_namelist ();
5132 nl->sym = sym;
5133 sym->refs++;
5134
5135 if (group_name->namelist == NULL)
5136 group_name->namelist = group_name->namelist_tail = nl;
5137 else
5138 {
5139 group_name->namelist_tail->next = nl;
5140 group_name->namelist_tail = nl;
5141 }
5142
5143 if (gfc_match_eos () == MATCH_YES)
5144 goto done;
5145
5146 m = gfc_match_char (',');
5147
5148 if (gfc_match_char ('/') == MATCH_YES)
5149 {
5150 m2 = gfc_match (" %s /", &group_name);
5151 if (m2 == MATCH_YES)
5152 break;
5153 if (m2 == MATCH_ERROR)
5154 goto error;
5155 goto syntax;
5156 }
5157
5158 if (m != MATCH_YES)
5159 goto syntax;
5160 }
5161 }
5162
5163 done:
5164 return MATCH_YES;
5165
5166 syntax:
5167 gfc_syntax_error (ST_NAMELIST);
5168
5169 error:
5170 return MATCH_ERROR;
5171 }
5172
5173
5174 /* Match a MODULE statement. */
5175
5176 match
5177 gfc_match_module (void)
5178 {
5179 match m;
5180
5181 m = gfc_match (" %s%t", &gfc_new_block);
5182 if (m != MATCH_YES)
5183 return m;
5184
5185 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5186 gfc_new_block->name, NULL))
5187 return MATCH_ERROR;
5188
5189 return MATCH_YES;
5190 }
5191
5192
5193 /* Free equivalence sets and lists. Recursively is the easiest way to
5194 do this. */
5195
5196 void
5197 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5198 {
5199 if (eq == stop)
5200 return;
5201
5202 gfc_free_equiv (eq->eq);
5203 gfc_free_equiv_until (eq->next, stop);
5204 gfc_free_expr (eq->expr);
5205 free (eq);
5206 }
5207
5208
5209 void
5210 gfc_free_equiv (gfc_equiv *eq)
5211 {
5212 gfc_free_equiv_until (eq, NULL);
5213 }
5214
5215
5216 /* Match an EQUIVALENCE statement. */
5217
5218 match
5219 gfc_match_equivalence (void)
5220 {
5221 gfc_equiv *eq, *set, *tail;
5222 gfc_ref *ref;
5223 gfc_symbol *sym;
5224 match m;
5225 gfc_common_head *common_head = NULL;
5226 bool common_flag;
5227 int cnt;
5228
5229 tail = NULL;
5230
5231 for (;;)
5232 {
5233 eq = gfc_get_equiv ();
5234 if (tail == NULL)
5235 tail = eq;
5236
5237 eq->next = gfc_current_ns->equiv;
5238 gfc_current_ns->equiv = eq;
5239
5240 if (gfc_match_char ('(') != MATCH_YES)
5241 goto syntax;
5242
5243 set = eq;
5244 common_flag = FALSE;
5245 cnt = 0;
5246
5247 for (;;)
5248 {
5249 m = gfc_match_equiv_variable (&set->expr);
5250 if (m == MATCH_ERROR)
5251 goto cleanup;
5252 if (m == MATCH_NO)
5253 goto syntax;
5254
5255 /* count the number of objects. */
5256 cnt++;
5257
5258 if (gfc_match_char ('%') == MATCH_YES)
5259 {
5260 gfc_error ("Derived type component %C is not a "
5261 "permitted EQUIVALENCE member");
5262 goto cleanup;
5263 }
5264
5265 for (ref = set->expr->ref; ref; ref = ref->next)
5266 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5267 {
5268 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5269 "be an array section");
5270 goto cleanup;
5271 }
5272
5273 sym = set->expr->symtree->n.sym;
5274
5275 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5276 goto cleanup;
5277
5278 if (sym->attr.in_common)
5279 {
5280 common_flag = TRUE;
5281 common_head = sym->common_head;
5282 }
5283
5284 if (gfc_match_char (')') == MATCH_YES)
5285 break;
5286
5287 if (gfc_match_char (',') != MATCH_YES)
5288 goto syntax;
5289
5290 set->eq = gfc_get_equiv ();
5291 set = set->eq;
5292 }
5293
5294 if (cnt < 2)
5295 {
5296 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5297 goto cleanup;
5298 }
5299
5300 /* If one of the members of an equivalence is in common, then
5301 mark them all as being in common. Before doing this, check
5302 that members of the equivalence group are not in different
5303 common blocks. */
5304 if (common_flag)
5305 for (set = eq; set; set = set->eq)
5306 {
5307 sym = set->expr->symtree->n.sym;
5308 if (sym->common_head && sym->common_head != common_head)
5309 {
5310 gfc_error ("Attempt to indirectly overlap COMMON "
5311 "blocks %s and %s by EQUIVALENCE at %C",
5312 sym->common_head->name, common_head->name);
5313 goto cleanup;
5314 }
5315 sym->attr.in_common = 1;
5316 sym->common_head = common_head;
5317 }
5318
5319 if (gfc_match_eos () == MATCH_YES)
5320 break;
5321 if (gfc_match_char (',') != MATCH_YES)
5322 {
5323 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5324 goto cleanup;
5325 }
5326 }
5327
5328 return MATCH_YES;
5329
5330 syntax:
5331 gfc_syntax_error (ST_EQUIVALENCE);
5332
5333 cleanup:
5334 eq = tail->next;
5335 tail->next = NULL;
5336
5337 gfc_free_equiv (gfc_current_ns->equiv);
5338 gfc_current_ns->equiv = eq;
5339
5340 return MATCH_ERROR;
5341 }
5342
5343
5344 /* Check that a statement function is not recursive. This is done by looking
5345 for the statement function symbol(sym) by looking recursively through its
5346 expression(e). If a reference to sym is found, true is returned.
5347 12.5.4 requires that any variable of function that is implicitly typed
5348 shall have that type confirmed by any subsequent type declaration. The
5349 implicit typing is conveniently done here. */
5350 static bool
5351 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5352
5353 static bool
5354 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5355 {
5356
5357 if (e == NULL)
5358 return false;
5359
5360 switch (e->expr_type)
5361 {
5362 case EXPR_FUNCTION:
5363 if (e->symtree == NULL)
5364 return false;
5365
5366 /* Check the name before testing for nested recursion! */
5367 if (sym->name == e->symtree->n.sym->name)
5368 return true;
5369
5370 /* Catch recursion via other statement functions. */
5371 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5372 && e->symtree->n.sym->value
5373 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5374 return true;
5375
5376 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5377 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5378
5379 break;
5380
5381 case EXPR_VARIABLE:
5382 if (e->symtree && sym->name == e->symtree->n.sym->name)
5383 return true;
5384
5385 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5386 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5387 break;
5388
5389 default:
5390 break;
5391 }
5392
5393 return false;
5394 }
5395
5396
5397 static bool
5398 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5399 {
5400 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5401 }
5402
5403
5404 /* Match a statement function declaration. It is so easy to match
5405 non-statement function statements with a MATCH_ERROR as opposed to
5406 MATCH_NO that we suppress error message in most cases. */
5407
5408 match
5409 gfc_match_st_function (void)
5410 {
5411 gfc_error_buffer old_error;
5412 gfc_symbol *sym;
5413 gfc_expr *expr;
5414 match m;
5415
5416 m = gfc_match_symbol (&sym, 0);
5417 if (m != MATCH_YES)
5418 return m;
5419
5420 gfc_push_error (&old_error);
5421
5422 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5423 goto undo_error;
5424
5425 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5426 goto undo_error;
5427
5428 m = gfc_match (" = %e%t", &expr);
5429 if (m == MATCH_NO)
5430 goto undo_error;
5431
5432 gfc_free_error (&old_error);
5433
5434 if (m == MATCH_ERROR)
5435 return m;
5436
5437 if (recursive_stmt_fcn (expr, sym))
5438 {
5439 gfc_error ("Statement function at %L is recursive", &expr->where);
5440 return MATCH_ERROR;
5441 }
5442
5443 sym->value = expr;
5444
5445 if ((gfc_current_state () == COMP_FUNCTION
5446 || gfc_current_state () == COMP_SUBROUTINE)
5447 && gfc_state_stack->previous->state == COMP_INTERFACE)
5448 {
5449 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5450 &expr->where);
5451 return MATCH_ERROR;
5452 }
5453
5454 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5455 return MATCH_ERROR;
5456
5457 return MATCH_YES;
5458
5459 undo_error:
5460 gfc_pop_error (&old_error);
5461 return MATCH_NO;
5462 }
5463
5464
5465 /* Match an assignment to a pointer function (F2008). This could, in
5466 general be ambiguous with a statement function. In this implementation
5467 it remains so if it is the first statement after the specification
5468 block. */
5469
5470 match
5471 gfc_match_ptr_fcn_assign (void)
5472 {
5473 gfc_error_buffer old_error;
5474 locus old_loc;
5475 gfc_symbol *sym;
5476 gfc_expr *expr;
5477 match m;
5478 char name[GFC_MAX_SYMBOL_LEN + 1];
5479
5480 old_loc = gfc_current_locus;
5481 m = gfc_match_name (name);
5482 if (m != MATCH_YES)
5483 return m;
5484
5485 gfc_find_symbol (name, NULL, 1, &sym);
5486 if (sym && sym->attr.flavor != FL_PROCEDURE)
5487 return MATCH_NO;
5488
5489 gfc_push_error (&old_error);
5490
5491 if (sym && sym->attr.function)
5492 goto match_actual_arglist;
5493
5494 gfc_current_locus = old_loc;
5495 m = gfc_match_symbol (&sym, 0);
5496 if (m != MATCH_YES)
5497 return m;
5498
5499 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5500 goto undo_error;
5501
5502 match_actual_arglist:
5503 gfc_current_locus = old_loc;
5504 m = gfc_match (" %e", &expr);
5505 if (m != MATCH_YES)
5506 goto undo_error;
5507
5508 new_st.op = EXEC_ASSIGN;
5509 new_st.expr1 = expr;
5510 expr = NULL;
5511
5512 m = gfc_match (" = %e%t", &expr);
5513 if (m != MATCH_YES)
5514 goto undo_error;
5515
5516 new_st.expr2 = expr;
5517 return MATCH_YES;
5518
5519 undo_error:
5520 gfc_pop_error (&old_error);
5521 return MATCH_NO;
5522 }
5523
5524
5525 /***************** SELECT CASE subroutines ******************/
5526
5527 /* Free a single case structure. */
5528
5529 static void
5530 free_case (gfc_case *p)
5531 {
5532 if (p->low == p->high)
5533 p->high = NULL;
5534 gfc_free_expr (p->low);
5535 gfc_free_expr (p->high);
5536 free (p);
5537 }
5538
5539
5540 /* Free a list of case structures. */
5541
5542 void
5543 gfc_free_case_list (gfc_case *p)
5544 {
5545 gfc_case *q;
5546
5547 for (; p; p = q)
5548 {
5549 q = p->next;
5550 free_case (p);
5551 }
5552 }
5553
5554
5555 /* Match a single case selector. Combining the requirements of F08:C830
5556 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5557 INTEGER, or LOGICAL type. */
5558
5559 static match
5560 match_case_selector (gfc_case **cp)
5561 {
5562 gfc_case *c;
5563 match m;
5564
5565 c = gfc_get_case ();
5566 c->where = gfc_current_locus;
5567
5568 if (gfc_match_char (':') == MATCH_YES)
5569 {
5570 m = gfc_match_init_expr (&c->high);
5571 if (m == MATCH_NO)
5572 goto need_expr;
5573 if (m == MATCH_ERROR)
5574 goto cleanup;
5575
5576 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5577 && c->high->ts.type != BT_CHARACTER)
5578 {
5579 gfc_error ("Expression in CASE selector at %L cannot be %s",
5580 &c->high->where, gfc_typename (&c->high->ts));
5581 goto cleanup;
5582 }
5583 }
5584 else
5585 {
5586 m = gfc_match_init_expr (&c->low);
5587 if (m == MATCH_ERROR)
5588 goto cleanup;
5589 if (m == MATCH_NO)
5590 goto need_expr;
5591
5592 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5593 && c->low->ts.type != BT_CHARACTER)
5594 {
5595 gfc_error ("Expression in CASE selector at %L cannot be %s",
5596 &c->low->where, gfc_typename (&c->low->ts));
5597 goto cleanup;
5598 }
5599
5600 /* If we're not looking at a ':' now, make a range out of a single
5601 target. Else get the upper bound for the case range. */
5602 if (gfc_match_char (':') != MATCH_YES)
5603 c->high = c->low;
5604 else
5605 {
5606 m = gfc_match_init_expr (&c->high);
5607 if (m == MATCH_ERROR)
5608 goto cleanup;
5609 /* MATCH_NO is fine. It's OK if nothing is there! */
5610 }
5611 }
5612
5613 *cp = c;
5614 return MATCH_YES;
5615
5616 need_expr:
5617 gfc_error ("Expected initialization expression in CASE at %C");
5618
5619 cleanup:
5620 free_case (c);
5621 return MATCH_ERROR;
5622 }
5623
5624
5625 /* Match the end of a case statement. */
5626
5627 static match
5628 match_case_eos (void)
5629 {
5630 char name[GFC_MAX_SYMBOL_LEN + 1];
5631 match m;
5632
5633 if (gfc_match_eos () == MATCH_YES)
5634 return MATCH_YES;
5635
5636 /* If the case construct doesn't have a case-construct-name, we
5637 should have matched the EOS. */
5638 if (!gfc_current_block ())
5639 return MATCH_NO;
5640
5641 gfc_gobble_whitespace ();
5642
5643 m = gfc_match_name (name);
5644 if (m != MATCH_YES)
5645 return m;
5646
5647 if (strcmp (name, gfc_current_block ()->name) != 0)
5648 {
5649 gfc_error ("Expected block name %qs of SELECT construct at %C",
5650 gfc_current_block ()->name);
5651 return MATCH_ERROR;
5652 }
5653
5654 return gfc_match_eos ();
5655 }
5656
5657
5658 /* Match a SELECT statement. */
5659
5660 match
5661 gfc_match_select (void)
5662 {
5663 gfc_expr *expr;
5664 match m;
5665
5666 m = gfc_match_label ();
5667 if (m == MATCH_ERROR)
5668 return m;
5669
5670 m = gfc_match (" select case ( %e )%t", &expr);
5671 if (m != MATCH_YES)
5672 return m;
5673
5674 new_st.op = EXEC_SELECT;
5675 new_st.expr1 = expr;
5676
5677 return MATCH_YES;
5678 }
5679
5680
5681 /* Transfer the selector typespec to the associate name. */
5682
5683 static void
5684 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5685 {
5686 gfc_ref *ref;
5687 gfc_symbol *assoc_sym;
5688
5689 assoc_sym = associate->symtree->n.sym;
5690
5691 /* At this stage the expression rank and arrayspec dimensions have
5692 not been completely sorted out. We must get the expr2->rank
5693 right here, so that the correct class container is obtained. */
5694 ref = selector->ref;
5695 while (ref && ref->next)
5696 ref = ref->next;
5697
5698 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5699 && ref && ref->type == REF_ARRAY)
5700 {
5701 /* Ensure that the array reference type is set. We cannot use
5702 gfc_resolve_expr at this point, so the usable parts of
5703 resolve.c(resolve_array_ref) are employed to do it. */
5704 if (ref->u.ar.type == AR_UNKNOWN)
5705 {
5706 ref->u.ar.type = AR_ELEMENT;
5707 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5708 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5709 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5710 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5711 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5712 {
5713 ref->u.ar.type = AR_SECTION;
5714 break;
5715 }
5716 }
5717
5718 if (ref->u.ar.type == AR_FULL)
5719 selector->rank = CLASS_DATA (selector)->as->rank;
5720 else if (ref->u.ar.type == AR_SECTION)
5721 selector->rank = ref->u.ar.dimen;
5722 else
5723 selector->rank = 0;
5724 }
5725
5726 if (selector->rank)
5727 {
5728 assoc_sym->attr.dimension = 1;
5729 assoc_sym->as = gfc_get_array_spec ();
5730 assoc_sym->as->rank = selector->rank;
5731 assoc_sym->as->type = AS_DEFERRED;
5732 }
5733 else
5734 assoc_sym->as = NULL;
5735
5736 if (selector->ts.type == BT_CLASS)
5737 {
5738 /* The correct class container has to be available. */
5739 assoc_sym->ts.type = BT_CLASS;
5740 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5741 assoc_sym->attr.pointer = 1;
5742 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5743 }
5744 }
5745
5746
5747 /* Push the current selector onto the SELECT TYPE stack. */
5748
5749 static void
5750 select_type_push (gfc_symbol *sel)
5751 {
5752 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5753 top->selector = sel;
5754 top->tmp = NULL;
5755 top->prev = select_type_stack;
5756
5757 select_type_stack = top;
5758 }
5759
5760
5761 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5762
5763 static gfc_symtree *
5764 select_intrinsic_set_tmp (gfc_typespec *ts)
5765 {
5766 char name[GFC_MAX_SYMBOL_LEN];
5767 gfc_symtree *tmp;
5768 int charlen = 0;
5769
5770 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5771 return NULL;
5772
5773 if (select_type_stack->selector->ts.type == BT_CLASS
5774 && !select_type_stack->selector->attr.class_ok)
5775 return NULL;
5776
5777 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5778 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5779 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5780
5781 if (ts->type != BT_CHARACTER)
5782 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5783 ts->kind);
5784 else
5785 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5786 charlen, ts->kind);
5787
5788 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5789 gfc_add_type (tmp->n.sym, ts, NULL);
5790
5791 /* Copy across the array spec to the selector. */
5792 if (select_type_stack->selector->ts.type == BT_CLASS
5793 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5794 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5795 {
5796 tmp->n.sym->attr.pointer = 1;
5797 tmp->n.sym->attr.dimension
5798 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5799 tmp->n.sym->attr.codimension
5800 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5801 tmp->n.sym->as
5802 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5803 }
5804
5805 gfc_set_sym_referenced (tmp->n.sym);
5806 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5807 tmp->n.sym->attr.select_type_temporary = 1;
5808
5809 return tmp;
5810 }
5811
5812
5813 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5814
5815 static void
5816 select_type_set_tmp (gfc_typespec *ts)
5817 {
5818 char name[GFC_MAX_SYMBOL_LEN];
5819 gfc_symtree *tmp = NULL;
5820
5821 if (!ts)
5822 {
5823 select_type_stack->tmp = NULL;
5824 return;
5825 }
5826
5827 tmp = select_intrinsic_set_tmp (ts);
5828
5829 if (tmp == NULL)
5830 {
5831 if (!ts->u.derived)
5832 return;
5833
5834 if (ts->type == BT_CLASS)
5835 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5836 else
5837 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5838 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5839 gfc_add_type (tmp->n.sym, ts, NULL);
5840
5841 if (select_type_stack->selector->ts.type == BT_CLASS
5842 && select_type_stack->selector->attr.class_ok)
5843 {
5844 tmp->n.sym->attr.pointer
5845 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5846
5847 /* Copy across the array spec to the selector. */
5848 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5849 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5850 {
5851 tmp->n.sym->attr.dimension
5852 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5853 tmp->n.sym->attr.codimension
5854 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5855 tmp->n.sym->as
5856 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5857 }
5858 }
5859
5860 gfc_set_sym_referenced (tmp->n.sym);
5861 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5862 tmp->n.sym->attr.select_type_temporary = 1;
5863
5864 if (ts->type == BT_CLASS)
5865 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5866 &tmp->n.sym->as);
5867 }
5868
5869 /* Add an association for it, so the rest of the parser knows it is
5870 an associate-name. The target will be set during resolution. */
5871 tmp->n.sym->assoc = gfc_get_association_list ();
5872 tmp->n.sym->assoc->dangling = 1;
5873 tmp->n.sym->assoc->st = tmp;
5874
5875 select_type_stack->tmp = tmp;
5876 }
5877
5878
5879 /* Match a SELECT TYPE statement. */
5880
5881 match
5882 gfc_match_select_type (void)
5883 {
5884 gfc_expr *expr1, *expr2 = NULL;
5885 match m;
5886 char name[GFC_MAX_SYMBOL_LEN];
5887 bool class_array;
5888 gfc_symbol *sym;
5889 gfc_namespace *ns = gfc_current_ns;
5890
5891 m = gfc_match_label ();
5892 if (m == MATCH_ERROR)
5893 return m;
5894
5895 m = gfc_match (" select type ( ");
5896 if (m != MATCH_YES)
5897 return m;
5898
5899 gfc_current_ns = gfc_build_block_ns (ns);
5900 m = gfc_match (" %n => %e", name, &expr2);
5901 if (m == MATCH_YES)
5902 {
5903 expr1 = gfc_get_expr ();
5904 expr1->expr_type = EXPR_VARIABLE;
5905 expr1->where = expr2->where;
5906 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5907 {
5908 m = MATCH_ERROR;
5909 goto cleanup;
5910 }
5911
5912 sym = expr1->symtree->n.sym;
5913 if (expr2->ts.type == BT_UNKNOWN)
5914 sym->attr.untyped = 1;
5915 else
5916 copy_ts_from_selector_to_associate (expr1, expr2);
5917
5918 sym->attr.flavor = FL_VARIABLE;
5919 sym->attr.referenced = 1;
5920 sym->attr.class_ok = 1;
5921 }
5922 else
5923 {
5924 m = gfc_match (" %e ", &expr1);
5925 if (m != MATCH_YES)
5926 {
5927 std::swap (ns, gfc_current_ns);
5928 gfc_free_namespace (ns);
5929 return m;
5930 }
5931 }
5932
5933 m = gfc_match (" )%t");
5934 if (m != MATCH_YES)
5935 {
5936 gfc_error ("parse error in SELECT TYPE statement at %C");
5937 goto cleanup;
5938 }
5939
5940 /* This ghastly expression seems to be needed to distinguish a CLASS
5941 array, which can have a reference, from other expressions that
5942 have references, such as derived type components, and are not
5943 allowed by the standard.
5944 TODO: see if it is sufficient to exclude component and substring
5945 references. */
5946 class_array = (expr1->expr_type == EXPR_VARIABLE
5947 && expr1->ts.type == BT_CLASS
5948 && CLASS_DATA (expr1)
5949 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5950 && (CLASS_DATA (expr1)->attr.dimension
5951 || CLASS_DATA (expr1)->attr.codimension)
5952 && expr1->ref
5953 && expr1->ref->type == REF_ARRAY
5954 && expr1->ref->next == NULL);
5955
5956 /* Check for F03:C811. */
5957 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5958 || (!class_array && expr1->ref != NULL)))
5959 {
5960 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5961 "use associate-name=>");
5962 m = MATCH_ERROR;
5963 goto cleanup;
5964 }
5965
5966 new_st.op = EXEC_SELECT_TYPE;
5967 new_st.expr1 = expr1;
5968 new_st.expr2 = expr2;
5969 new_st.ext.block.ns = gfc_current_ns;
5970
5971 select_type_push (expr1->symtree->n.sym);
5972 gfc_current_ns = ns;
5973
5974 return MATCH_YES;
5975
5976 cleanup:
5977 gfc_free_expr (expr1);
5978 gfc_free_expr (expr2);
5979 gfc_undo_symbols ();
5980 std::swap (ns, gfc_current_ns);
5981 gfc_free_namespace (ns);
5982 return m;
5983 }
5984
5985
5986 /* Match a CASE statement. */
5987
5988 match
5989 gfc_match_case (void)
5990 {
5991 gfc_case *c, *head, *tail;
5992 match m;
5993
5994 head = tail = NULL;
5995
5996 if (gfc_current_state () != COMP_SELECT)
5997 {
5998 gfc_error ("Unexpected CASE statement at %C");
5999 return MATCH_ERROR;
6000 }
6001
6002 if (gfc_match ("% default") == MATCH_YES)
6003 {
6004 m = match_case_eos ();
6005 if (m == MATCH_NO)
6006 goto syntax;
6007 if (m == MATCH_ERROR)
6008 goto cleanup;
6009
6010 new_st.op = EXEC_SELECT;
6011 c = gfc_get_case ();
6012 c->where = gfc_current_locus;
6013 new_st.ext.block.case_list = c;
6014 return MATCH_YES;
6015 }
6016
6017 if (gfc_match_char ('(') != MATCH_YES)
6018 goto syntax;
6019
6020 for (;;)
6021 {
6022 if (match_case_selector (&c) == MATCH_ERROR)
6023 goto cleanup;
6024
6025 if (head == NULL)
6026 head = c;
6027 else
6028 tail->next = c;
6029
6030 tail = c;
6031
6032 if (gfc_match_char (')') == MATCH_YES)
6033 break;
6034 if (gfc_match_char (',') != MATCH_YES)
6035 goto syntax;
6036 }
6037
6038 m = match_case_eos ();
6039 if (m == MATCH_NO)
6040 goto syntax;
6041 if (m == MATCH_ERROR)
6042 goto cleanup;
6043
6044 new_st.op = EXEC_SELECT;
6045 new_st.ext.block.case_list = head;
6046
6047 return MATCH_YES;
6048
6049 syntax:
6050 gfc_error ("Syntax error in CASE specification at %C");
6051
6052 cleanup:
6053 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6054 return MATCH_ERROR;
6055 }
6056
6057
6058 /* Match a TYPE IS statement. */
6059
6060 match
6061 gfc_match_type_is (void)
6062 {
6063 gfc_case *c = NULL;
6064 match m;
6065
6066 if (gfc_current_state () != COMP_SELECT_TYPE)
6067 {
6068 gfc_error ("Unexpected TYPE IS statement at %C");
6069 return MATCH_ERROR;
6070 }
6071
6072 if (gfc_match_char ('(') != MATCH_YES)
6073 goto syntax;
6074
6075 c = gfc_get_case ();
6076 c->where = gfc_current_locus;
6077
6078 m = gfc_match_type_spec (&c->ts);
6079 if (m == MATCH_NO)
6080 goto syntax;
6081 if (m == MATCH_ERROR)
6082 goto cleanup;
6083
6084 if (gfc_match_char (')') != MATCH_YES)
6085 goto syntax;
6086
6087 m = match_case_eos ();
6088 if (m == MATCH_NO)
6089 goto syntax;
6090 if (m == MATCH_ERROR)
6091 goto cleanup;
6092
6093 new_st.op = EXEC_SELECT_TYPE;
6094 new_st.ext.block.case_list = c;
6095
6096 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6097 && (c->ts.u.derived->attr.sequence
6098 || c->ts.u.derived->attr.is_bind_c))
6099 {
6100 gfc_error ("The type-spec shall not specify a sequence derived "
6101 "type or a type with the BIND attribute in SELECT "
6102 "TYPE at %C [F2003:C815]");
6103 return MATCH_ERROR;
6104 }
6105
6106 /* Create temporary variable. */
6107 select_type_set_tmp (&c->ts);
6108
6109 return MATCH_YES;
6110
6111 syntax:
6112 gfc_error ("Syntax error in TYPE IS specification at %C");
6113
6114 cleanup:
6115 if (c != NULL)
6116 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6117 return MATCH_ERROR;
6118 }
6119
6120
6121 /* Match a CLASS IS or CLASS DEFAULT statement. */
6122
6123 match
6124 gfc_match_class_is (void)
6125 {
6126 gfc_case *c = NULL;
6127 match m;
6128
6129 if (gfc_current_state () != COMP_SELECT_TYPE)
6130 return MATCH_NO;
6131
6132 if (gfc_match ("% default") == MATCH_YES)
6133 {
6134 m = match_case_eos ();
6135 if (m == MATCH_NO)
6136 goto syntax;
6137 if (m == MATCH_ERROR)
6138 goto cleanup;
6139
6140 new_st.op = EXEC_SELECT_TYPE;
6141 c = gfc_get_case ();
6142 c->where = gfc_current_locus;
6143 c->ts.type = BT_UNKNOWN;
6144 new_st.ext.block.case_list = c;
6145 select_type_set_tmp (NULL);
6146 return MATCH_YES;
6147 }
6148
6149 m = gfc_match ("% is");
6150 if (m == MATCH_NO)
6151 goto syntax;
6152 if (m == MATCH_ERROR)
6153 goto cleanup;
6154
6155 if (gfc_match_char ('(') != MATCH_YES)
6156 goto syntax;
6157
6158 c = gfc_get_case ();
6159 c->where = gfc_current_locus;
6160
6161 m = match_derived_type_spec (&c->ts);
6162 if (m == MATCH_NO)
6163 goto syntax;
6164 if (m == MATCH_ERROR)
6165 goto cleanup;
6166
6167 if (c->ts.type == BT_DERIVED)
6168 c->ts.type = BT_CLASS;
6169
6170 if (gfc_match_char (')') != MATCH_YES)
6171 goto syntax;
6172
6173 m = match_case_eos ();
6174 if (m == MATCH_NO)
6175 goto syntax;
6176 if (m == MATCH_ERROR)
6177 goto cleanup;
6178
6179 new_st.op = EXEC_SELECT_TYPE;
6180 new_st.ext.block.case_list = c;
6181
6182 /* Create temporary variable. */
6183 select_type_set_tmp (&c->ts);
6184
6185 return MATCH_YES;
6186
6187 syntax:
6188 gfc_error ("Syntax error in CLASS IS specification at %C");
6189
6190 cleanup:
6191 if (c != NULL)
6192 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6193 return MATCH_ERROR;
6194 }
6195
6196
6197 /********************* WHERE subroutines ********************/
6198
6199 /* Match the rest of a simple WHERE statement that follows an IF statement.
6200 */
6201
6202 static match
6203 match_simple_where (void)
6204 {
6205 gfc_expr *expr;
6206 gfc_code *c;
6207 match m;
6208
6209 m = gfc_match (" ( %e )", &expr);
6210 if (m != MATCH_YES)
6211 return m;
6212
6213 m = gfc_match_assignment ();
6214 if (m == MATCH_NO)
6215 goto syntax;
6216 if (m == MATCH_ERROR)
6217 goto cleanup;
6218
6219 if (gfc_match_eos () != MATCH_YES)
6220 goto syntax;
6221
6222 c = gfc_get_code (EXEC_WHERE);
6223 c->expr1 = expr;
6224
6225 c->next = XCNEW (gfc_code);
6226 *c->next = new_st;
6227 c->next->loc = gfc_current_locus;
6228 gfc_clear_new_st ();
6229
6230 new_st.op = EXEC_WHERE;
6231 new_st.block = c;
6232
6233 return MATCH_YES;
6234
6235 syntax:
6236 gfc_syntax_error (ST_WHERE);
6237
6238 cleanup:
6239 gfc_free_expr (expr);
6240 return MATCH_ERROR;
6241 }
6242
6243
6244 /* Match a WHERE statement. */
6245
6246 match
6247 gfc_match_where (gfc_statement *st)
6248 {
6249 gfc_expr *expr;
6250 match m0, m;
6251 gfc_code *c;
6252
6253 m0 = gfc_match_label ();
6254 if (m0 == MATCH_ERROR)
6255 return m0;
6256
6257 m = gfc_match (" where ( %e )", &expr);
6258 if (m != MATCH_YES)
6259 return m;
6260
6261 if (gfc_match_eos () == MATCH_YES)
6262 {
6263 *st = ST_WHERE_BLOCK;
6264 new_st.op = EXEC_WHERE;
6265 new_st.expr1 = expr;
6266 return MATCH_YES;
6267 }
6268
6269 m = gfc_match_assignment ();
6270 if (m == MATCH_NO)
6271 gfc_syntax_error (ST_WHERE);
6272
6273 if (m != MATCH_YES)
6274 {
6275 gfc_free_expr (expr);
6276 return MATCH_ERROR;
6277 }
6278
6279 /* We've got a simple WHERE statement. */
6280 *st = ST_WHERE;
6281 c = gfc_get_code (EXEC_WHERE);
6282 c->expr1 = expr;
6283
6284 /* Put in the assignment. It will not be processed by add_statement, so we
6285 need to copy the location here. */
6286
6287 c->next = XCNEW (gfc_code);
6288 *c->next = new_st;
6289 c->next->loc = gfc_current_locus;
6290 gfc_clear_new_st ();
6291
6292 new_st.op = EXEC_WHERE;
6293 new_st.block = c;
6294
6295 return MATCH_YES;
6296 }
6297
6298
6299 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6300 new_st if successful. */
6301
6302 match
6303 gfc_match_elsewhere (void)
6304 {
6305 char name[GFC_MAX_SYMBOL_LEN + 1];
6306 gfc_expr *expr;
6307 match m;
6308
6309 if (gfc_current_state () != COMP_WHERE)
6310 {
6311 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6312 return MATCH_ERROR;
6313 }
6314
6315 expr = NULL;
6316
6317 if (gfc_match_char ('(') == MATCH_YES)
6318 {
6319 m = gfc_match_expr (&expr);
6320 if (m == MATCH_NO)
6321 goto syntax;
6322 if (m == MATCH_ERROR)
6323 return MATCH_ERROR;
6324
6325 if (gfc_match_char (')') != MATCH_YES)
6326 goto syntax;
6327 }
6328
6329 if (gfc_match_eos () != MATCH_YES)
6330 {
6331 /* Only makes sense if we have a where-construct-name. */
6332 if (!gfc_current_block ())
6333 {
6334 m = MATCH_ERROR;
6335 goto cleanup;
6336 }
6337 /* Better be a name at this point. */
6338 m = gfc_match_name (name);
6339 if (m == MATCH_NO)
6340 goto syntax;
6341 if (m == MATCH_ERROR)
6342 goto cleanup;
6343
6344 if (gfc_match_eos () != MATCH_YES)
6345 goto syntax;
6346
6347 if (strcmp (name, gfc_current_block ()->name) != 0)
6348 {
6349 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6350 name, gfc_current_block ()->name);
6351 goto cleanup;
6352 }
6353 }
6354
6355 new_st.op = EXEC_WHERE;
6356 new_st.expr1 = expr;
6357 return MATCH_YES;
6358
6359 syntax:
6360 gfc_syntax_error (ST_ELSEWHERE);
6361
6362 cleanup:
6363 gfc_free_expr (expr);
6364 return MATCH_ERROR;
6365 }
This page took 0.326443 seconds and 5 git commands to generate.