re PR fortran/31270 (print subscript value and array bounds when out-of-bounds error...
[gcc.git] / gcc / fortran / primary.c
1 /* Primary expression subroutines
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
28 #include "parse.h"
29
30 /* Matches a kind-parameter expression, which is either a named
31 symbolic constant or a nonnegative integer constant. If
32 successful, sets the kind value to the correct integer. */
33
34 static match
35 match_kind_param (int *kind)
36 {
37 char name[GFC_MAX_SYMBOL_LEN + 1];
38 gfc_symbol *sym;
39 const char *p;
40 match m;
41
42 m = gfc_match_small_literal_int (kind, NULL);
43 if (m != MATCH_NO)
44 return m;
45
46 m = gfc_match_name (name);
47 if (m != MATCH_YES)
48 return m;
49
50 if (gfc_find_symbol (name, NULL, 1, &sym))
51 return MATCH_ERROR;
52
53 if (sym == NULL)
54 return MATCH_NO;
55
56 if (sym->attr.flavor != FL_PARAMETER)
57 return MATCH_NO;
58
59 p = gfc_extract_int (sym->value, kind);
60 if (p != NULL)
61 return MATCH_NO;
62
63 if (*kind < 0)
64 return MATCH_NO;
65
66 return MATCH_YES;
67 }
68
69
70 /* Get a trailing kind-specification for non-character variables.
71 Returns:
72 the integer kind value or:
73 -1 if an error was generated
74 -2 if no kind was found */
75
76 static int
77 get_kind (void)
78 {
79 int kind;
80 match m;
81
82 if (gfc_match_char ('_') != MATCH_YES)
83 return -2;
84
85 m = match_kind_param (&kind);
86 if (m == MATCH_NO)
87 gfc_error ("Missing kind-parameter at %C");
88
89 return (m == MATCH_YES) ? kind : -1;
90 }
91
92
93 /* Given a character and a radix, see if the character is a valid
94 digit in that radix. */
95
96 static int
97 check_digit (int c, int radix)
98 {
99 int r;
100
101 switch (radix)
102 {
103 case 2:
104 r = ('0' <= c && c <= '1');
105 break;
106
107 case 8:
108 r = ('0' <= c && c <= '7');
109 break;
110
111 case 10:
112 r = ('0' <= c && c <= '9');
113 break;
114
115 case 16:
116 r = ISXDIGIT (c);
117 break;
118
119 default:
120 gfc_internal_error ("check_digit(): bad radix");
121 }
122
123 return r;
124 }
125
126
127 /* Match the digit string part of an integer if signflag is not set,
128 the signed digit string part if signflag is set. If the buffer
129 is NULL, we just count characters for the resolution pass. Returns
130 the number of characters matched, -1 for no match. */
131
132 static int
133 match_digits (int signflag, int radix, char *buffer)
134 {
135 locus old_loc;
136 int length, c;
137
138 length = 0;
139 c = gfc_next_char ();
140
141 if (signflag && (c == '+' || c == '-'))
142 {
143 if (buffer != NULL)
144 *buffer++ = c;
145 gfc_gobble_whitespace ();
146 c = gfc_next_char ();
147 length++;
148 }
149
150 if (!check_digit (c, radix))
151 return -1;
152
153 length++;
154 if (buffer != NULL)
155 *buffer++ = c;
156
157 for (;;)
158 {
159 old_loc = gfc_current_locus;
160 c = gfc_next_char ();
161
162 if (!check_digit (c, radix))
163 break;
164
165 if (buffer != NULL)
166 *buffer++ = c;
167 length++;
168 }
169
170 gfc_current_locus = old_loc;
171
172 return length;
173 }
174
175
176 /* Match an integer (digit string and optional kind).
177 A sign will be accepted if signflag is set. */
178
179 static match
180 match_integer_constant (gfc_expr **result, int signflag)
181 {
182 int length, kind;
183 locus old_loc;
184 char *buffer;
185 gfc_expr *e;
186
187 old_loc = gfc_current_locus;
188 gfc_gobble_whitespace ();
189
190 length = match_digits (signflag, 10, NULL);
191 gfc_current_locus = old_loc;
192 if (length == -1)
193 return MATCH_NO;
194
195 buffer = alloca (length + 1);
196 memset (buffer, '\0', length + 1);
197
198 gfc_gobble_whitespace ();
199
200 match_digits (signflag, 10, buffer);
201
202 kind = get_kind ();
203 if (kind == -2)
204 kind = gfc_default_integer_kind;
205 if (kind == -1)
206 return MATCH_ERROR;
207
208 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
209 {
210 gfc_error ("Integer kind %d at %C not available", kind);
211 return MATCH_ERROR;
212 }
213
214 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
215
216 if (gfc_range_check (e) != ARITH_OK)
217 {
218 gfc_error ("Integer too big for its kind at %C. This check can be "
219 "disabled with the option -fno-range-check");
220
221 gfc_free_expr (e);
222 return MATCH_ERROR;
223 }
224
225 *result = e;
226 return MATCH_YES;
227 }
228
229
230 /* Match a Hollerith constant. */
231
232 static match
233 match_hollerith_constant (gfc_expr **result)
234 {
235 locus old_loc;
236 gfc_expr *e = NULL;
237 const char *msg;
238 int num;
239 int i;
240
241 old_loc = gfc_current_locus;
242 gfc_gobble_whitespace ();
243
244 if (match_integer_constant (&e, 0) == MATCH_YES
245 && gfc_match_char ('h') == MATCH_YES)
246 {
247 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Hollerith constant "
248 "at %C") == FAILURE)
249 goto cleanup;
250
251 msg = gfc_extract_int (e, &num);
252 if (msg != NULL)
253 {
254 gfc_error (msg);
255 goto cleanup;
256 }
257 if (num == 0)
258 {
259 gfc_error ("Invalid Hollerith constant: %L must contain at least "
260 "one character", &old_loc);
261 goto cleanup;
262 }
263 if (e->ts.kind != gfc_default_integer_kind)
264 {
265 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
266 "should be default", &old_loc);
267 goto cleanup;
268 }
269 else
270 {
271 gfc_free_expr (e);
272 e = gfc_constant_result (BT_HOLLERITH, gfc_default_character_kind,
273 &gfc_current_locus);
274
275 e->representation.string = gfc_getmem (num + 1);
276 for (i = 0; i < num; i++)
277 {
278 e->representation.string[i] = gfc_next_char_literal (1);
279 }
280 e->representation.string[num] = '\0';
281 e->representation.length = num;
282
283 *result = e;
284 return MATCH_YES;
285 }
286 }
287
288 gfc_free_expr (e);
289 gfc_current_locus = old_loc;
290 return MATCH_NO;
291
292 cleanup:
293 gfc_free_expr (e);
294 return MATCH_ERROR;
295 }
296
297
298 /* Match a binary, octal or hexadecimal constant that can be found in
299 a DATA statement. The standard permits b'010...', o'73...', and
300 z'a1...' where b, o, and z can be capital letters. This function
301 also accepts postfixed forms of the constants: '01...'b, '73...'o,
302 and 'a1...'z. An additional extension is the use of x for z. */
303
304 static match
305 match_boz_constant (gfc_expr **result)
306 {
307 int post, radix, delim, length, x_hex, kind;
308 locus old_loc, start_loc;
309 char *buffer;
310 gfc_expr *e;
311
312 start_loc = old_loc = gfc_current_locus;
313 gfc_gobble_whitespace ();
314
315 x_hex = 0;
316 switch (post = gfc_next_char ())
317 {
318 case 'b':
319 radix = 2;
320 post = 0;
321 break;
322 case 'o':
323 radix = 8;
324 post = 0;
325 break;
326 case 'x':
327 x_hex = 1;
328 /* Fall through. */
329 case 'z':
330 radix = 16;
331 post = 0;
332 break;
333 case '\'':
334 /* Fall through. */
335 case '\"':
336 delim = post;
337 post = 1;
338 radix = 16; /* Set to accept any valid digit string. */
339 break;
340 default:
341 goto backup;
342 }
343
344 /* No whitespace allowed here. */
345
346 if (post == 0)
347 delim = gfc_next_char ();
348
349 if (delim != '\'' && delim != '\"')
350 goto backup;
351
352 if (x_hex && pedantic
353 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
354 "constant at %C uses non-standard syntax.")
355 == FAILURE))
356 return MATCH_ERROR;
357
358 old_loc = gfc_current_locus;
359
360 length = match_digits (0, radix, NULL);
361 if (length == -1)
362 {
363 gfc_error ("Empty set of digits in BOZ constant at %C");
364 return MATCH_ERROR;
365 }
366
367 if (gfc_next_char () != delim)
368 {
369 gfc_error ("Illegal character in BOZ constant at %C");
370 return MATCH_ERROR;
371 }
372
373 if (post == 1)
374 {
375 switch (gfc_next_char ())
376 {
377 case 'b':
378 radix = 2;
379 break;
380 case 'o':
381 radix = 8;
382 break;
383 case 'x':
384 /* Fall through. */
385 case 'z':
386 radix = 16;
387 break;
388 default:
389 goto backup;
390 }
391 gfc_notify_std (GFC_STD_GNU, "Extension: BOZ constant "
392 "at %C uses non-standard postfix syntax.");
393 }
394
395 gfc_current_locus = old_loc;
396
397 buffer = alloca (length + 1);
398 memset (buffer, '\0', length + 1);
399
400 match_digits (0, radix, buffer);
401 gfc_next_char (); /* Eat delimiter. */
402 if (post == 1)
403 gfc_next_char (); /* Eat postfixed b, o, z, or x. */
404
405 /* In section 5.2.5 and following C567 in the Fortran 2003 standard, we find
406 "If a data-stmt-constant is a boz-literal-constant, the corresponding
407 variable shall be of type integer. The boz-literal-constant is treated
408 as if it were an int-literal-constant with a kind-param that specifies
409 the representation method with the largest decimal exponent range
410 supported by the processor." */
411
412 kind = gfc_max_integer_kind;
413 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
414
415 if (gfc_range_check (e) != ARITH_OK)
416 {
417 gfc_error ("Integer too big for integer kind %i at %C", kind);
418 gfc_free_expr (e);
419 return MATCH_ERROR;
420 }
421
422 *result = e;
423 return MATCH_YES;
424
425 backup:
426 gfc_current_locus = start_loc;
427 return MATCH_NO;
428 }
429
430
431 /* Match a real constant of some sort. Allow a signed constant if signflag
432 is nonzero. Allow integer constants if allow_int is true. */
433
434 static match
435 match_real_constant (gfc_expr **result, int signflag)
436 {
437 int kind, c, count, seen_dp, seen_digits, exp_char;
438 locus old_loc, temp_loc;
439 char *p, *buffer;
440 gfc_expr *e;
441 bool negate;
442
443 old_loc = gfc_current_locus;
444 gfc_gobble_whitespace ();
445
446 e = NULL;
447
448 count = 0;
449 seen_dp = 0;
450 seen_digits = 0;
451 exp_char = ' ';
452 negate = FALSE;
453
454 c = gfc_next_char ();
455 if (signflag && (c == '+' || c == '-'))
456 {
457 if (c == '-')
458 negate = TRUE;
459
460 gfc_gobble_whitespace ();
461 c = gfc_next_char ();
462 }
463
464 /* Scan significand. */
465 for (;; c = gfc_next_char (), count++)
466 {
467 if (c == '.')
468 {
469 if (seen_dp)
470 goto done;
471
472 /* Check to see if "." goes with a following operator like
473 ".eq.". */
474 temp_loc = gfc_current_locus;
475 c = gfc_next_char ();
476
477 if (c == 'e' || c == 'd' || c == 'q')
478 {
479 c = gfc_next_char ();
480 if (c == '.')
481 goto done; /* Operator named .e. or .d. */
482 }
483
484 if (ISALPHA (c))
485 goto done; /* Distinguish 1.e9 from 1.eq.2 */
486
487 gfc_current_locus = temp_loc;
488 seen_dp = 1;
489 continue;
490 }
491
492 if (ISDIGIT (c))
493 {
494 seen_digits = 1;
495 continue;
496 }
497
498 break;
499 }
500
501 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
502 goto done;
503 exp_char = c;
504
505 /* Scan exponent. */
506 c = gfc_next_char ();
507 count++;
508
509 if (c == '+' || c == '-')
510 { /* optional sign */
511 c = gfc_next_char ();
512 count++;
513 }
514
515 if (!ISDIGIT (c))
516 {
517 gfc_error ("Missing exponent in real number at %C");
518 return MATCH_ERROR;
519 }
520
521 while (ISDIGIT (c))
522 {
523 c = gfc_next_char ();
524 count++;
525 }
526
527 done:
528 /* Check that we have a numeric constant. */
529 if (!seen_digits || (!seen_dp && exp_char == ' '))
530 {
531 gfc_current_locus = old_loc;
532 return MATCH_NO;
533 }
534
535 /* Convert the number. */
536 gfc_current_locus = old_loc;
537 gfc_gobble_whitespace ();
538
539 buffer = alloca (count + 1);
540 memset (buffer, '\0', count + 1);
541
542 p = buffer;
543 c = gfc_next_char ();
544 if (c == '+' || c == '-')
545 {
546 gfc_gobble_whitespace ();
547 c = gfc_next_char ();
548 }
549
550 /* Hack for mpfr_set_str(). */
551 for (;;)
552 {
553 if (c == 'd' || c == 'q')
554 *p = 'e';
555 else
556 *p = c;
557 p++;
558 if (--count == 0)
559 break;
560
561 c = gfc_next_char ();
562 }
563
564 kind = get_kind ();
565 if (kind == -1)
566 goto cleanup;
567
568 switch (exp_char)
569 {
570 case 'd':
571 if (kind != -2)
572 {
573 gfc_error ("Real number at %C has a 'd' exponent and an explicit "
574 "kind");
575 goto cleanup;
576 }
577 kind = gfc_default_double_kind;
578 break;
579
580 default:
581 if (kind == -2)
582 kind = gfc_default_real_kind;
583
584 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
585 {
586 gfc_error ("Invalid real kind %d at %C", kind);
587 goto cleanup;
588 }
589 }
590
591 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
592 if (negate)
593 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
594
595 switch (gfc_range_check (e))
596 {
597 case ARITH_OK:
598 break;
599 case ARITH_OVERFLOW:
600 gfc_error ("Real constant overflows its kind at %C");
601 goto cleanup;
602
603 case ARITH_UNDERFLOW:
604 if (gfc_option.warn_underflow)
605 gfc_warning ("Real constant underflows its kind at %C");
606 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
607 break;
608
609 default:
610 gfc_internal_error ("gfc_range_check() returned bad value");
611 }
612
613 *result = e;
614 return MATCH_YES;
615
616 cleanup:
617 gfc_free_expr (e);
618 return MATCH_ERROR;
619 }
620
621
622 /* Match a substring reference. */
623
624 static match
625 match_substring (gfc_charlen *cl, int init, gfc_ref **result)
626 {
627 gfc_expr *start, *end;
628 locus old_loc;
629 gfc_ref *ref;
630 match m;
631
632 start = NULL;
633 end = NULL;
634
635 old_loc = gfc_current_locus;
636
637 m = gfc_match_char ('(');
638 if (m != MATCH_YES)
639 return MATCH_NO;
640
641 if (gfc_match_char (':') != MATCH_YES)
642 {
643 if (init)
644 m = gfc_match_init_expr (&start);
645 else
646 m = gfc_match_expr (&start);
647
648 if (m != MATCH_YES)
649 {
650 m = MATCH_NO;
651 goto cleanup;
652 }
653
654 m = gfc_match_char (':');
655 if (m != MATCH_YES)
656 goto cleanup;
657 }
658
659 if (gfc_match_char (')') != MATCH_YES)
660 {
661 if (init)
662 m = gfc_match_init_expr (&end);
663 else
664 m = gfc_match_expr (&end);
665
666 if (m == MATCH_NO)
667 goto syntax;
668 if (m == MATCH_ERROR)
669 goto cleanup;
670
671 m = gfc_match_char (')');
672 if (m == MATCH_NO)
673 goto syntax;
674 }
675
676 /* Optimize away the (:) reference. */
677 if (start == NULL && end == NULL)
678 ref = NULL;
679 else
680 {
681 ref = gfc_get_ref ();
682
683 ref->type = REF_SUBSTRING;
684 if (start == NULL)
685 start = gfc_int_expr (1);
686 ref->u.ss.start = start;
687 if (end == NULL && cl)
688 end = gfc_copy_expr (cl->length);
689 ref->u.ss.end = end;
690 ref->u.ss.length = cl;
691 }
692
693 *result = ref;
694 return MATCH_YES;
695
696 syntax:
697 gfc_error ("Syntax error in SUBSTRING specification at %C");
698 m = MATCH_ERROR;
699
700 cleanup:
701 gfc_free_expr (start);
702 gfc_free_expr (end);
703
704 gfc_current_locus = old_loc;
705 return m;
706 }
707
708
709 /* Reads the next character of a string constant, taking care to
710 return doubled delimiters on the input as a single instance of
711 the delimiter.
712
713 Special return values are:
714 -1 End of the string, as determined by the delimiter
715 -2 Unterminated string detected
716
717 Backslash codes are also expanded at this time. */
718
719 static int
720 next_string_char (char delimiter)
721 {
722 locus old_locus;
723 int c;
724
725 c = gfc_next_char_literal (1);
726
727 if (c == '\n')
728 return -2;
729
730 if (gfc_option.flag_backslash && c == '\\')
731 {
732 old_locus = gfc_current_locus;
733
734 if (gfc_match_special_char (&c) == MATCH_NO)
735 gfc_current_locus = old_locus;
736
737 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
738 gfc_warning ("Extension: backslash character at %C");
739 }
740
741 if (c != delimiter)
742 return c;
743
744 old_locus = gfc_current_locus;
745 c = gfc_next_char_literal (0);
746
747 if (c == delimiter)
748 return c;
749 gfc_current_locus = old_locus;
750
751 return -1;
752 }
753
754
755 /* Special case of gfc_match_name() that matches a parameter kind name
756 before a string constant. This takes case of the weird but legal
757 case of:
758
759 kind_____'string'
760
761 where kind____ is a parameter. gfc_match_name() will happily slurp
762 up all the underscores, which leads to problems. If we return
763 MATCH_YES, the parse pointer points to the final underscore, which
764 is not part of the name. We never return MATCH_ERROR-- errors in
765 the name will be detected later. */
766
767 static match
768 match_charkind_name (char *name)
769 {
770 locus old_loc;
771 char c, peek;
772 int len;
773
774 gfc_gobble_whitespace ();
775 c = gfc_next_char ();
776 if (!ISALPHA (c))
777 return MATCH_NO;
778
779 *name++ = c;
780 len = 1;
781
782 for (;;)
783 {
784 old_loc = gfc_current_locus;
785 c = gfc_next_char ();
786
787 if (c == '_')
788 {
789 peek = gfc_peek_char ();
790
791 if (peek == '\'' || peek == '\"')
792 {
793 gfc_current_locus = old_loc;
794 *name = '\0';
795 return MATCH_YES;
796 }
797 }
798
799 if (!ISALNUM (c)
800 && c != '_'
801 && (gfc_option.flag_dollar_ok && c != '$'))
802 break;
803
804 *name++ = c;
805 if (++len > GFC_MAX_SYMBOL_LEN)
806 break;
807 }
808
809 return MATCH_NO;
810 }
811
812
813 /* See if the current input matches a character constant. Lots of
814 contortions have to be done to match the kind parameter which comes
815 before the actual string. The main consideration is that we don't
816 want to error out too quickly. For example, we don't actually do
817 any validation of the kinds until we have actually seen a legal
818 delimiter. Using match_kind_param() generates errors too quickly. */
819
820 static match
821 match_string_constant (gfc_expr **result)
822 {
823 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
824 int i, c, kind, length, delimiter, warn_ampersand;
825 locus old_locus, start_locus;
826 gfc_symbol *sym;
827 gfc_expr *e;
828 const char *q;
829 match m;
830
831 old_locus = gfc_current_locus;
832
833 gfc_gobble_whitespace ();
834
835 start_locus = gfc_current_locus;
836
837 c = gfc_next_char ();
838 if (c == '\'' || c == '"')
839 {
840 kind = gfc_default_character_kind;
841 goto got_delim;
842 }
843
844 if (ISDIGIT (c))
845 {
846 kind = 0;
847
848 while (ISDIGIT (c))
849 {
850 kind = kind * 10 + c - '0';
851 if (kind > 9999999)
852 goto no_match;
853 c = gfc_next_char ();
854 }
855
856 }
857 else
858 {
859 gfc_current_locus = old_locus;
860
861 m = match_charkind_name (name);
862 if (m != MATCH_YES)
863 goto no_match;
864
865 if (gfc_find_symbol (name, NULL, 1, &sym)
866 || sym == NULL
867 || sym->attr.flavor != FL_PARAMETER)
868 goto no_match;
869
870 kind = -1;
871 c = gfc_next_char ();
872 }
873
874 if (c == ' ')
875 {
876 gfc_gobble_whitespace ();
877 c = gfc_next_char ();
878 }
879
880 if (c != '_')
881 goto no_match;
882
883 gfc_gobble_whitespace ();
884 start_locus = gfc_current_locus;
885
886 c = gfc_next_char ();
887 if (c != '\'' && c != '"')
888 goto no_match;
889
890 if (kind == -1)
891 {
892 q = gfc_extract_int (sym->value, &kind);
893 if (q != NULL)
894 {
895 gfc_error (q);
896 return MATCH_ERROR;
897 }
898 }
899
900 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
901 {
902 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
903 return MATCH_ERROR;
904 }
905
906 got_delim:
907 /* Scan the string into a block of memory by first figuring out how
908 long it is, allocating the structure, then re-reading it. This
909 isn't particularly efficient, but string constants aren't that
910 common in most code. TODO: Use obstacks? */
911
912 delimiter = c;
913 length = 0;
914
915 for (;;)
916 {
917 c = next_string_char (delimiter);
918 if (c == -1)
919 break;
920 if (c == -2)
921 {
922 gfc_current_locus = start_locus;
923 gfc_error ("Unterminated character constant beginning at %C");
924 return MATCH_ERROR;
925 }
926
927 length++;
928 }
929
930 /* Peek at the next character to see if it is a b, o, z, or x for the
931 postfixed BOZ literal constants. */
932 c = gfc_peek_char ();
933 if (c == 'b' || c == 'o' || c =='z' || c == 'x')
934 goto no_match;
935
936
937 e = gfc_get_expr ();
938
939 e->expr_type = EXPR_CONSTANT;
940 e->ref = NULL;
941 e->ts.type = BT_CHARACTER;
942 e->ts.kind = kind;
943 e->ts.is_c_interop = 0;
944 e->ts.is_iso_c = 0;
945 e->where = start_locus;
946
947 e->value.character.string = p = gfc_getmem (length + 1);
948 e->value.character.length = length;
949
950 gfc_current_locus = start_locus;
951 gfc_next_char (); /* Skip delimiter */
952
953 /* We disable the warning for the following loop as the warning has already
954 been printed in the loop above. */
955 warn_ampersand = gfc_option.warn_ampersand;
956 gfc_option.warn_ampersand = 0;
957
958 for (i = 0; i < length; i++)
959 *p++ = next_string_char (delimiter);
960
961 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
962 gfc_option.warn_ampersand = warn_ampersand;
963
964 if (next_string_char (delimiter) != -1)
965 gfc_internal_error ("match_string_constant(): Delimiter not found");
966
967 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
968 e->expr_type = EXPR_SUBSTRING;
969
970 *result = e;
971
972 return MATCH_YES;
973
974 no_match:
975 gfc_current_locus = old_locus;
976 return MATCH_NO;
977 }
978
979
980 /* Match a .true. or .false. */
981
982 static match
983 match_logical_constant (gfc_expr **result)
984 {
985 static mstring logical_ops[] = {
986 minit (".false.", 0),
987 minit (".true.", 1),
988 minit (NULL, -1)
989 };
990
991 gfc_expr *e;
992 int i, kind;
993
994 i = gfc_match_strings (logical_ops);
995 if (i == -1)
996 return MATCH_NO;
997
998 kind = get_kind ();
999 if (kind == -1)
1000 return MATCH_ERROR;
1001 if (kind == -2)
1002 kind = gfc_default_logical_kind;
1003
1004 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1005 {
1006 gfc_error ("Bad kind for logical constant at %C");
1007 return MATCH_ERROR;
1008 }
1009
1010 e = gfc_get_expr ();
1011
1012 e->expr_type = EXPR_CONSTANT;
1013 e->value.logical = i;
1014 e->ts.type = BT_LOGICAL;
1015 e->ts.kind = kind;
1016 e->ts.is_c_interop = 0;
1017 e->ts.is_iso_c = 0;
1018 e->where = gfc_current_locus;
1019
1020 *result = e;
1021 return MATCH_YES;
1022 }
1023
1024
1025 /* Match a real or imaginary part of a complex constant that is a
1026 symbolic constant. */
1027
1028 static match
1029 match_sym_complex_part (gfc_expr **result)
1030 {
1031 char name[GFC_MAX_SYMBOL_LEN + 1];
1032 gfc_symbol *sym;
1033 gfc_expr *e;
1034 match m;
1035
1036 m = gfc_match_name (name);
1037 if (m != MATCH_YES)
1038 return m;
1039
1040 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1041 return MATCH_NO;
1042
1043 if (sym->attr.flavor != FL_PARAMETER)
1044 {
1045 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1046 return MATCH_ERROR;
1047 }
1048
1049 if (!gfc_numeric_ts (&sym->value->ts))
1050 {
1051 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1052 return MATCH_ERROR;
1053 }
1054
1055 if (sym->value->rank != 0)
1056 {
1057 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1058 return MATCH_ERROR;
1059 }
1060
1061 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
1062 "complex constant at %C") == FAILURE)
1063 return MATCH_ERROR;
1064
1065 switch (sym->value->ts.type)
1066 {
1067 case BT_REAL:
1068 e = gfc_copy_expr (sym->value);
1069 break;
1070
1071 case BT_COMPLEX:
1072 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1073 if (e == NULL)
1074 goto error;
1075 break;
1076
1077 case BT_INTEGER:
1078 e = gfc_int2real (sym->value, gfc_default_real_kind);
1079 if (e == NULL)
1080 goto error;
1081 break;
1082
1083 default:
1084 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1085 }
1086
1087 *result = e; /* e is a scalar, real, constant expression. */
1088 return MATCH_YES;
1089
1090 error:
1091 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1092 return MATCH_ERROR;
1093 }
1094
1095
1096 /* Match a real or imaginary part of a complex number. */
1097
1098 static match
1099 match_complex_part (gfc_expr **result)
1100 {
1101 match m;
1102
1103 m = match_sym_complex_part (result);
1104 if (m != MATCH_NO)
1105 return m;
1106
1107 m = match_real_constant (result, 1);
1108 if (m != MATCH_NO)
1109 return m;
1110
1111 return match_integer_constant (result, 1);
1112 }
1113
1114
1115 /* Try to match a complex constant. */
1116
1117 static match
1118 match_complex_constant (gfc_expr **result)
1119 {
1120 gfc_expr *e, *real, *imag;
1121 gfc_error_buf old_error;
1122 gfc_typespec target;
1123 locus old_loc;
1124 int kind;
1125 match m;
1126
1127 old_loc = gfc_current_locus;
1128 real = imag = e = NULL;
1129
1130 m = gfc_match_char ('(');
1131 if (m != MATCH_YES)
1132 return m;
1133
1134 gfc_push_error (&old_error);
1135
1136 m = match_complex_part (&real);
1137 if (m == MATCH_NO)
1138 {
1139 gfc_free_error (&old_error);
1140 goto cleanup;
1141 }
1142
1143 if (gfc_match_char (',') == MATCH_NO)
1144 {
1145 gfc_pop_error (&old_error);
1146 m = MATCH_NO;
1147 goto cleanup;
1148 }
1149
1150 /* If m is error, then something was wrong with the real part and we
1151 assume we have a complex constant because we've seen the ','. An
1152 ambiguous case here is the start of an iterator list of some
1153 sort. These sort of lists are matched prior to coming here. */
1154
1155 if (m == MATCH_ERROR)
1156 {
1157 gfc_free_error (&old_error);
1158 goto cleanup;
1159 }
1160 gfc_pop_error (&old_error);
1161
1162 m = match_complex_part (&imag);
1163 if (m == MATCH_NO)
1164 goto syntax;
1165 if (m == MATCH_ERROR)
1166 goto cleanup;
1167
1168 m = gfc_match_char (')');
1169 if (m == MATCH_NO)
1170 {
1171 /* Give the matcher for implied do-loops a chance to run. This
1172 yields a much saner error message for (/ (i, 4=i, 6) /). */
1173 if (gfc_peek_char () == '=')
1174 {
1175 m = MATCH_ERROR;
1176 goto cleanup;
1177 }
1178 else
1179 goto syntax;
1180 }
1181
1182 if (m == MATCH_ERROR)
1183 goto cleanup;
1184
1185 /* Decide on the kind of this complex number. */
1186 if (real->ts.type == BT_REAL)
1187 {
1188 if (imag->ts.type == BT_REAL)
1189 kind = gfc_kind_max (real, imag);
1190 else
1191 kind = real->ts.kind;
1192 }
1193 else
1194 {
1195 if (imag->ts.type == BT_REAL)
1196 kind = imag->ts.kind;
1197 else
1198 kind = gfc_default_real_kind;
1199 }
1200 target.type = BT_REAL;
1201 target.kind = kind;
1202 target.is_c_interop = 0;
1203 target.is_iso_c = 0;
1204
1205 if (real->ts.type != BT_REAL || kind != real->ts.kind)
1206 gfc_convert_type (real, &target, 2);
1207 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1208 gfc_convert_type (imag, &target, 2);
1209
1210 e = gfc_convert_complex (real, imag, kind);
1211 e->where = gfc_current_locus;
1212
1213 gfc_free_expr (real);
1214 gfc_free_expr (imag);
1215
1216 *result = e;
1217 return MATCH_YES;
1218
1219 syntax:
1220 gfc_error ("Syntax error in COMPLEX constant at %C");
1221 m = MATCH_ERROR;
1222
1223 cleanup:
1224 gfc_free_expr (e);
1225 gfc_free_expr (real);
1226 gfc_free_expr (imag);
1227 gfc_current_locus = old_loc;
1228
1229 return m;
1230 }
1231
1232
1233 /* Match constants in any of several forms. Returns nonzero for a
1234 match, zero for no match. */
1235
1236 match
1237 gfc_match_literal_constant (gfc_expr **result, int signflag)
1238 {
1239 match m;
1240
1241 m = match_complex_constant (result);
1242 if (m != MATCH_NO)
1243 return m;
1244
1245 m = match_string_constant (result);
1246 if (m != MATCH_NO)
1247 return m;
1248
1249 m = match_boz_constant (result);
1250 if (m != MATCH_NO)
1251 return m;
1252
1253 m = match_real_constant (result, signflag);
1254 if (m != MATCH_NO)
1255 return m;
1256
1257 m = match_hollerith_constant (result);
1258 if (m != MATCH_NO)
1259 return m;
1260
1261 m = match_integer_constant (result, signflag);
1262 if (m != MATCH_NO)
1263 return m;
1264
1265 m = match_logical_constant (result);
1266 if (m != MATCH_NO)
1267 return m;
1268
1269 return MATCH_NO;
1270 }
1271
1272
1273 /* Match a single actual argument value. An actual argument is
1274 usually an expression, but can also be a procedure name. If the
1275 argument is a single name, it is not always possible to tell
1276 whether the name is a dummy procedure or not. We treat these cases
1277 by creating an argument that looks like a dummy procedure and
1278 fixing things later during resolution. */
1279
1280 static match
1281 match_actual_arg (gfc_expr **result)
1282 {
1283 char name[GFC_MAX_SYMBOL_LEN + 1];
1284 gfc_symtree *symtree;
1285 locus where, w;
1286 gfc_expr *e;
1287 int c;
1288
1289 where = gfc_current_locus;
1290
1291 switch (gfc_match_name (name))
1292 {
1293 case MATCH_ERROR:
1294 return MATCH_ERROR;
1295
1296 case MATCH_NO:
1297 break;
1298
1299 case MATCH_YES:
1300 w = gfc_current_locus;
1301 gfc_gobble_whitespace ();
1302 c = gfc_next_char ();
1303 gfc_current_locus = w;
1304
1305 if (c != ',' && c != ')')
1306 break;
1307
1308 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1309 break;
1310 /* Handle error elsewhere. */
1311
1312 /* Eliminate a couple of common cases where we know we don't
1313 have a function argument. */
1314 if (symtree == NULL)
1315 {
1316 gfc_get_sym_tree (name, NULL, &symtree);
1317 gfc_set_sym_referenced (symtree->n.sym);
1318 }
1319 else
1320 {
1321 gfc_symbol *sym;
1322
1323 sym = symtree->n.sym;
1324 gfc_set_sym_referenced (sym);
1325 if (sym->attr.flavor != FL_PROCEDURE
1326 && sym->attr.flavor != FL_UNKNOWN)
1327 break;
1328
1329 /* If the symbol is a function with itself as the result and
1330 is being defined, then we have a variable. */
1331 if (sym->attr.function && sym->result == sym)
1332 {
1333 if (gfc_current_ns->proc_name == sym
1334 || (gfc_current_ns->parent != NULL
1335 && gfc_current_ns->parent->proc_name == sym))
1336 break;
1337
1338 if (sym->attr.entry
1339 && (sym->ns == gfc_current_ns
1340 || sym->ns == gfc_current_ns->parent))
1341 {
1342 gfc_entry_list *el = NULL;
1343
1344 for (el = sym->ns->entries; el; el = el->next)
1345 if (sym == el->sym)
1346 break;
1347
1348 if (el)
1349 break;
1350 }
1351 }
1352 }
1353
1354 e = gfc_get_expr (); /* Leave it unknown for now */
1355 e->symtree = symtree;
1356 e->expr_type = EXPR_VARIABLE;
1357 e->ts.type = BT_PROCEDURE;
1358 e->where = where;
1359
1360 *result = e;
1361 return MATCH_YES;
1362 }
1363
1364 gfc_current_locus = where;
1365 return gfc_match_expr (result);
1366 }
1367
1368
1369 /* Match a keyword argument. */
1370
1371 static match
1372 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
1373 {
1374 char name[GFC_MAX_SYMBOL_LEN + 1];
1375 gfc_actual_arglist *a;
1376 locus name_locus;
1377 match m;
1378
1379 name_locus = gfc_current_locus;
1380 m = gfc_match_name (name);
1381
1382 if (m != MATCH_YES)
1383 goto cleanup;
1384 if (gfc_match_char ('=') != MATCH_YES)
1385 {
1386 m = MATCH_NO;
1387 goto cleanup;
1388 }
1389
1390 m = match_actual_arg (&actual->expr);
1391 if (m != MATCH_YES)
1392 goto cleanup;
1393
1394 /* Make sure this name has not appeared yet. */
1395
1396 if (name[0] != '\0')
1397 {
1398 for (a = base; a; a = a->next)
1399 if (a->name != NULL && strcmp (a->name, name) == 0)
1400 {
1401 gfc_error ("Keyword '%s' at %C has already appeared in the "
1402 "current argument list", name);
1403 return MATCH_ERROR;
1404 }
1405 }
1406
1407 actual->name = gfc_get_string (name);
1408 return MATCH_YES;
1409
1410 cleanup:
1411 gfc_current_locus = name_locus;
1412 return m;
1413 }
1414
1415
1416 /* Match an argument list function, such as %VAL. */
1417
1418 static match
1419 match_arg_list_function (gfc_actual_arglist *result)
1420 {
1421 char name[GFC_MAX_SYMBOL_LEN + 1];
1422 locus old_locus;
1423 match m;
1424
1425 old_locus = gfc_current_locus;
1426
1427 if (gfc_match_char ('%') != MATCH_YES)
1428 {
1429 m = MATCH_NO;
1430 goto cleanup;
1431 }
1432
1433 m = gfc_match ("%n (", name);
1434 if (m != MATCH_YES)
1435 goto cleanup;
1436
1437 if (name[0] != '\0')
1438 {
1439 switch (name[0])
1440 {
1441 case 'l':
1442 if (strncmp (name, "loc", 3) == 0)
1443 {
1444 result->name = "%LOC";
1445 break;
1446 }
1447 case 'r':
1448 if (strncmp (name, "ref", 3) == 0)
1449 {
1450 result->name = "%REF";
1451 break;
1452 }
1453 case 'v':
1454 if (strncmp (name, "val", 3) == 0)
1455 {
1456 result->name = "%VAL";
1457 break;
1458 }
1459 default:
1460 m = MATCH_ERROR;
1461 goto cleanup;
1462 }
1463 }
1464
1465 if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
1466 "function at %C") == FAILURE)
1467 {
1468 m = MATCH_ERROR;
1469 goto cleanup;
1470 }
1471
1472 m = match_actual_arg (&result->expr);
1473 if (m != MATCH_YES)
1474 goto cleanup;
1475
1476 if (gfc_match_char (')') != MATCH_YES)
1477 {
1478 m = MATCH_NO;
1479 goto cleanup;
1480 }
1481
1482 return MATCH_YES;
1483
1484 cleanup:
1485 gfc_current_locus = old_locus;
1486 return m;
1487 }
1488
1489
1490 /* Matches an actual argument list of a function or subroutine, from
1491 the opening parenthesis to the closing parenthesis. The argument
1492 list is assumed to allow keyword arguments because we don't know if
1493 the symbol associated with the procedure has an implicit interface
1494 or not. We make sure keywords are unique. If sub_flag is set,
1495 we're matching the argument list of a subroutine. */
1496
1497 match
1498 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
1499 {
1500 gfc_actual_arglist *head, *tail;
1501 int seen_keyword;
1502 gfc_st_label *label;
1503 locus old_loc;
1504 match m;
1505
1506 *argp = tail = NULL;
1507 old_loc = gfc_current_locus;
1508
1509 seen_keyword = 0;
1510
1511 if (gfc_match_char ('(') == MATCH_NO)
1512 return (sub_flag) ? MATCH_YES : MATCH_NO;
1513
1514 if (gfc_match_char (')') == MATCH_YES)
1515 return MATCH_YES;
1516 head = NULL;
1517
1518 for (;;)
1519 {
1520 if (head == NULL)
1521 head = tail = gfc_get_actual_arglist ();
1522 else
1523 {
1524 tail->next = gfc_get_actual_arglist ();
1525 tail = tail->next;
1526 }
1527
1528 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1529 {
1530 m = gfc_match_st_label (&label);
1531 if (m == MATCH_NO)
1532 gfc_error ("Expected alternate return label at %C");
1533 if (m != MATCH_YES)
1534 goto cleanup;
1535
1536 tail->label = label;
1537 goto next;
1538 }
1539
1540 /* After the first keyword argument is seen, the following
1541 arguments must also have keywords. */
1542 if (seen_keyword)
1543 {
1544 m = match_keyword_arg (tail, head);
1545
1546 if (m == MATCH_ERROR)
1547 goto cleanup;
1548 if (m == MATCH_NO)
1549 {
1550 gfc_error ("Missing keyword name in actual argument list at %C");
1551 goto cleanup;
1552 }
1553
1554 }
1555 else
1556 {
1557 /* Try an argument list function, like %VAL. */
1558 m = match_arg_list_function (tail);
1559 if (m == MATCH_ERROR)
1560 goto cleanup;
1561
1562 /* See if we have the first keyword argument. */
1563 if (m == MATCH_NO)
1564 {
1565 m = match_keyword_arg (tail, head);
1566 if (m == MATCH_YES)
1567 seen_keyword = 1;
1568 if (m == MATCH_ERROR)
1569 goto cleanup;
1570 }
1571
1572 if (m == MATCH_NO)
1573 {
1574 /* Try for a non-keyword argument. */
1575 m = match_actual_arg (&tail->expr);
1576 if (m == MATCH_ERROR)
1577 goto cleanup;
1578 if (m == MATCH_NO)
1579 goto syntax;
1580 }
1581 }
1582
1583
1584 next:
1585 if (gfc_match_char (')') == MATCH_YES)
1586 break;
1587 if (gfc_match_char (',') != MATCH_YES)
1588 goto syntax;
1589 }
1590
1591 *argp = head;
1592 return MATCH_YES;
1593
1594 syntax:
1595 gfc_error ("Syntax error in argument list at %C");
1596
1597 cleanup:
1598 gfc_free_actual_arglist (head);
1599 gfc_current_locus = old_loc;
1600
1601 return MATCH_ERROR;
1602 }
1603
1604
1605 /* Used by match_varspec() to extend the reference list by one
1606 element. */
1607
1608 static gfc_ref *
1609 extend_ref (gfc_expr *primary, gfc_ref *tail)
1610 {
1611 if (primary->ref == NULL)
1612 primary->ref = tail = gfc_get_ref ();
1613 else
1614 {
1615 if (tail == NULL)
1616 gfc_internal_error ("extend_ref(): Bad tail");
1617 tail->next = gfc_get_ref ();
1618 tail = tail->next;
1619 }
1620
1621 return tail;
1622 }
1623
1624
1625 /* Match any additional specifications associated with the current
1626 variable like member references or substrings. If equiv_flag is
1627 set we only match stuff that is allowed inside an EQUIVALENCE
1628 statement. */
1629
1630 static match
1631 match_varspec (gfc_expr *primary, int equiv_flag)
1632 {
1633 char name[GFC_MAX_SYMBOL_LEN + 1];
1634 gfc_ref *substring, *tail;
1635 gfc_component *component;
1636 gfc_symbol *sym = primary->symtree->n.sym;
1637 match m;
1638
1639 tail = NULL;
1640
1641 if ((equiv_flag && gfc_peek_char () == '(') || sym->attr.dimension)
1642 {
1643 /* In EQUIVALENCE, we don't know yet whether we are seeing
1644 an array, character variable or array of character
1645 variables. We'll leave the decision till resolve time. */
1646 tail = extend_ref (primary, tail);
1647 tail->type = REF_ARRAY;
1648
1649 m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1650 equiv_flag);
1651 if (m != MATCH_YES)
1652 return m;
1653
1654 if (equiv_flag && gfc_peek_char () == '(')
1655 {
1656 tail = extend_ref (primary, tail);
1657 tail->type = REF_ARRAY;
1658
1659 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1660 if (m != MATCH_YES)
1661 return m;
1662 }
1663 }
1664
1665 primary->ts = sym->ts;
1666
1667 if (equiv_flag)
1668 return MATCH_YES;
1669
1670 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1671 goto check_substring;
1672
1673 sym = sym->ts.derived;
1674
1675 for (;;)
1676 {
1677 m = gfc_match_name (name);
1678 if (m == MATCH_NO)
1679 gfc_error ("Expected structure component name at %C");
1680 if (m != MATCH_YES)
1681 return MATCH_ERROR;
1682
1683 component = gfc_find_component (sym, name);
1684 if (component == NULL)
1685 return MATCH_ERROR;
1686
1687 tail = extend_ref (primary, tail);
1688 tail->type = REF_COMPONENT;
1689
1690 tail->u.c.component = component;
1691 tail->u.c.sym = sym;
1692
1693 primary->ts = component->ts;
1694
1695 if (component->as != NULL)
1696 {
1697 tail = extend_ref (primary, tail);
1698 tail->type = REF_ARRAY;
1699
1700 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1701 if (m != MATCH_YES)
1702 return m;
1703 }
1704
1705 if (component->ts.type != BT_DERIVED
1706 || gfc_match_char ('%') != MATCH_YES)
1707 break;
1708
1709 sym = component->ts.derived;
1710 }
1711
1712 check_substring:
1713 if (primary->ts.type == BT_UNKNOWN)
1714 {
1715 if (gfc_get_default_type (sym, sym->ns)->type == BT_CHARACTER)
1716 {
1717 gfc_set_default_type (sym, 0, sym->ns);
1718 primary->ts = sym->ts;
1719 }
1720 }
1721
1722 if (primary->ts.type == BT_CHARACTER)
1723 {
1724 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1725 {
1726 case MATCH_YES:
1727 if (tail == NULL)
1728 primary->ref = substring;
1729 else
1730 tail->next = substring;
1731
1732 if (primary->expr_type == EXPR_CONSTANT)
1733 primary->expr_type = EXPR_SUBSTRING;
1734
1735 if (substring)
1736 primary->ts.cl = NULL;
1737
1738 break;
1739
1740 case MATCH_NO:
1741 break;
1742
1743 case MATCH_ERROR:
1744 return MATCH_ERROR;
1745 }
1746 }
1747
1748 return MATCH_YES;
1749 }
1750
1751
1752 /* Given an expression that is a variable, figure out what the
1753 ultimate variable's type and attribute is, traversing the reference
1754 structures if necessary.
1755
1756 This subroutine is trickier than it looks. We start at the base
1757 symbol and store the attribute. Component references load a
1758 completely new attribute.
1759
1760 A couple of rules come into play. Subobjects of targets are always
1761 targets themselves. If we see a component that goes through a
1762 pointer, then the expression must also be a target, since the
1763 pointer is associated with something (if it isn't core will soon be
1764 dumped). If we see a full part or section of an array, the
1765 expression is also an array.
1766
1767 We can have at most one full array reference. */
1768
1769 symbol_attribute
1770 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
1771 {
1772 int dimension, pointer, allocatable, target;
1773 symbol_attribute attr;
1774 gfc_ref *ref;
1775
1776 if (expr->expr_type != EXPR_VARIABLE)
1777 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1778
1779 ref = expr->ref;
1780 attr = expr->symtree->n.sym->attr;
1781
1782 dimension = attr.dimension;
1783 pointer = attr.pointer;
1784 allocatable = attr.allocatable;
1785
1786 target = attr.target;
1787 if (pointer)
1788 target = 1;
1789
1790 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1791 *ts = expr->symtree->n.sym->ts;
1792
1793 for (; ref; ref = ref->next)
1794 switch (ref->type)
1795 {
1796 case REF_ARRAY:
1797
1798 switch (ref->u.ar.type)
1799 {
1800 case AR_FULL:
1801 dimension = 1;
1802 break;
1803
1804 case AR_SECTION:
1805 allocatable = pointer = 0;
1806 dimension = 1;
1807 break;
1808
1809 case AR_ELEMENT:
1810 allocatable = pointer = 0;
1811 break;
1812
1813 case AR_UNKNOWN:
1814 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1815 }
1816
1817 break;
1818
1819 case REF_COMPONENT:
1820 gfc_get_component_attr (&attr, ref->u.c.component);
1821 if (ts != NULL)
1822 {
1823 *ts = ref->u.c.component->ts;
1824 /* Don't set the string length if a substring reference
1825 follows. */
1826 if (ts->type == BT_CHARACTER
1827 && ref->next && ref->next->type == REF_SUBSTRING)
1828 ts->cl = NULL;
1829 }
1830
1831 pointer = ref->u.c.component->pointer;
1832 allocatable = ref->u.c.component->allocatable;
1833 if (pointer)
1834 target = 1;
1835
1836 break;
1837
1838 case REF_SUBSTRING:
1839 allocatable = pointer = 0;
1840 break;
1841 }
1842
1843 attr.dimension = dimension;
1844 attr.pointer = pointer;
1845 attr.allocatable = allocatable;
1846 attr.target = target;
1847
1848 return attr;
1849 }
1850
1851
1852 /* Return the attribute from a general expression. */
1853
1854 symbol_attribute
1855 gfc_expr_attr (gfc_expr *e)
1856 {
1857 symbol_attribute attr;
1858
1859 switch (e->expr_type)
1860 {
1861 case EXPR_VARIABLE:
1862 attr = gfc_variable_attr (e, NULL);
1863 break;
1864
1865 case EXPR_FUNCTION:
1866 gfc_clear_attr (&attr);
1867
1868 if (e->value.function.esym != NULL)
1869 attr = e->value.function.esym->result->attr;
1870
1871 /* TODO: NULL() returns pointers. May have to take care of this
1872 here. */
1873
1874 break;
1875
1876 default:
1877 gfc_clear_attr (&attr);
1878 break;
1879 }
1880
1881 return attr;
1882 }
1883
1884
1885 /* Match a structure constructor. The initial symbol has already been
1886 seen. */
1887
1888 match
1889 gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
1890 {
1891 gfc_constructor *head, *tail;
1892 gfc_component *comp;
1893 gfc_expr *e;
1894 locus where;
1895 match m;
1896 bool private_comp = false;
1897
1898 head = tail = NULL;
1899
1900 if (gfc_match_char ('(') != MATCH_YES)
1901 goto syntax;
1902
1903 where = gfc_current_locus;
1904
1905 gfc_find_component (sym, NULL);
1906
1907 for (comp = sym->components; comp; comp = comp->next)
1908 {
1909 if (comp->access == ACCESS_PRIVATE)
1910 {
1911 private_comp = true;
1912 break;
1913 }
1914 if (head == NULL)
1915 tail = head = gfc_get_constructor ();
1916 else
1917 {
1918 tail->next = gfc_get_constructor ();
1919 tail = tail->next;
1920 }
1921
1922 m = gfc_match_expr (&tail->expr);
1923 if (m == MATCH_NO)
1924 goto syntax;
1925 if (m == MATCH_ERROR)
1926 goto cleanup;
1927
1928 if (gfc_match_char (',') == MATCH_YES)
1929 {
1930 if (comp->next == NULL)
1931 {
1932 gfc_error ("Too many components in structure constructor at %C");
1933 goto cleanup;
1934 }
1935
1936 continue;
1937 }
1938
1939 break;
1940 }
1941
1942 if (sym->attr.use_assoc
1943 && (sym->component_access == ACCESS_PRIVATE || private_comp))
1944 {
1945 gfc_error ("Structure constructor for '%s' at %C has PRIVATE "
1946 "components", sym->name);
1947 goto cleanup;
1948 }
1949
1950 if (gfc_match_char (')') != MATCH_YES)
1951 goto syntax;
1952
1953 if (comp->next != NULL)
1954 {
1955 gfc_error ("Too few components in structure constructor at %C");
1956 goto cleanup;
1957 }
1958
1959 e = gfc_get_expr ();
1960
1961 e->expr_type = EXPR_STRUCTURE;
1962
1963 e->ts.type = BT_DERIVED;
1964 e->ts.derived = sym;
1965 e->where = where;
1966
1967 e->value.constructor = head;
1968
1969 *result = e;
1970 return MATCH_YES;
1971
1972 syntax:
1973 gfc_error ("Syntax error in structure constructor at %C");
1974
1975 cleanup:
1976 gfc_free_constructor (head);
1977 return MATCH_ERROR;
1978 }
1979
1980
1981 /* If the symbol is an implicit do loop index and implicitly typed,
1982 it should not be host associated. Provide a symtree from the
1983 current namespace. */
1984 static match
1985 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
1986 {
1987 if ((*sym)->attr.flavor == FL_VARIABLE
1988 && (*sym)->ns != gfc_current_ns
1989 && (*sym)->attr.implied_index
1990 && (*sym)->attr.implicit_type
1991 && !(*sym)->attr.use_assoc)
1992 {
1993 int i;
1994 i = gfc_get_sym_tree ((*sym)->name, NULL, st);
1995 if (i)
1996 return MATCH_ERROR;
1997 *sym = (*st)->n.sym;
1998 }
1999 return MATCH_YES;
2000 }
2001
2002
2003 /* Matches a variable name followed by anything that might follow it--
2004 array reference, argument list of a function, etc. */
2005
2006 match
2007 gfc_match_rvalue (gfc_expr **result)
2008 {
2009 gfc_actual_arglist *actual_arglist;
2010 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
2011 gfc_state_data *st;
2012 gfc_symbol *sym;
2013 gfc_symtree *symtree;
2014 locus where, old_loc;
2015 gfc_expr *e;
2016 match m, m2;
2017 int i;
2018 gfc_typespec *ts;
2019 bool implicit_char;
2020
2021 m = gfc_match_name (name);
2022 if (m != MATCH_YES)
2023 return m;
2024
2025 if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2026 && !gfc_current_ns->has_import_set)
2027 i = gfc_get_sym_tree (name, NULL, &symtree);
2028 else
2029 i = gfc_get_ha_sym_tree (name, &symtree);
2030
2031 if (i)
2032 return MATCH_ERROR;
2033
2034 sym = symtree->n.sym;
2035 e = NULL;
2036 where = gfc_current_locus;
2037
2038 /* If this is an implicit do loop index and implicitly typed,
2039 it should not be host associated. */
2040 m = check_for_implicit_index (&symtree, &sym);
2041 if (m != MATCH_YES)
2042 return m;
2043
2044 gfc_set_sym_referenced (sym);
2045 sym->attr.implied_index = 0;
2046
2047 if (sym->attr.function && sym->result == sym)
2048 {
2049 /* See if this is a directly recursive function call. */
2050 gfc_gobble_whitespace ();
2051 if (sym->attr.recursive
2052 && gfc_peek_char () == '('
2053 && gfc_current_ns->proc_name == sym
2054 && !sym->attr.dimension)
2055 {
2056 gfc_error ("'%s' at %C is the name of a recursive function "
2057 "and so refers to the result variable. Use an "
2058 "explicit RESULT variable for direct recursion "
2059 "(12.5.2.1)", sym->name);
2060 return MATCH_ERROR;
2061 }
2062
2063 if (gfc_current_ns->proc_name == sym
2064 || (gfc_current_ns->parent != NULL
2065 && gfc_current_ns->parent->proc_name == sym))
2066 goto variable;
2067
2068 if (sym->attr.entry
2069 && (sym->ns == gfc_current_ns
2070 || sym->ns == gfc_current_ns->parent))
2071 {
2072 gfc_entry_list *el = NULL;
2073
2074 for (el = sym->ns->entries; el; el = el->next)
2075 if (sym == el->sym)
2076 goto variable;
2077 }
2078 }
2079
2080 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2081 goto function0;
2082
2083 if (sym->attr.generic)
2084 goto generic_function;
2085
2086 switch (sym->attr.flavor)
2087 {
2088 case FL_VARIABLE:
2089 variable:
2090 if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
2091 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2092 gfc_set_default_type (sym, 0, sym->ns);
2093
2094 e = gfc_get_expr ();
2095
2096 e->expr_type = EXPR_VARIABLE;
2097 e->symtree = symtree;
2098
2099 m = match_varspec (e, 0);
2100 break;
2101
2102 case FL_PARAMETER:
2103 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2104 end up here. Unfortunately, sym->value->expr_type is set to
2105 EXPR_CONSTANT, and so the if () branch would be followed without
2106 the !sym->as check. */
2107 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
2108 e = gfc_copy_expr (sym->value);
2109 else
2110 {
2111 e = gfc_get_expr ();
2112 e->expr_type = EXPR_VARIABLE;
2113 }
2114
2115 e->symtree = symtree;
2116 m = match_varspec (e, 0);
2117 break;
2118
2119 case FL_DERIVED:
2120 sym = gfc_use_derived (sym);
2121 if (sym == NULL)
2122 m = MATCH_ERROR;
2123 else
2124 m = gfc_match_structure_constructor (sym, &e);
2125 break;
2126
2127 /* If we're here, then the name is known to be the name of a
2128 procedure, yet it is not sure to be the name of a function. */
2129 case FL_PROCEDURE:
2130 if (sym->attr.subroutine)
2131 {
2132 gfc_error ("Unexpected use of subroutine name '%s' at %C",
2133 sym->name);
2134 m = MATCH_ERROR;
2135 break;
2136 }
2137
2138 /* At this point, the name has to be a non-statement function.
2139 If the name is the same as the current function being
2140 compiled, then we have a variable reference (to the function
2141 result) if the name is non-recursive. */
2142
2143 st = gfc_enclosing_unit (NULL);
2144
2145 if (st != NULL && st->state == COMP_FUNCTION
2146 && st->sym == sym
2147 && !sym->attr.recursive)
2148 {
2149 e = gfc_get_expr ();
2150 e->symtree = symtree;
2151 e->expr_type = EXPR_VARIABLE;
2152
2153 m = match_varspec (e, 0);
2154 break;
2155 }
2156
2157 /* Match a function reference. */
2158 function0:
2159 m = gfc_match_actual_arglist (0, &actual_arglist);
2160 if (m == MATCH_NO)
2161 {
2162 if (sym->attr.proc == PROC_ST_FUNCTION)
2163 gfc_error ("Statement function '%s' requires argument list at %C",
2164 sym->name);
2165 else
2166 gfc_error ("Function '%s' requires an argument list at %C",
2167 sym->name);
2168
2169 m = MATCH_ERROR;
2170 break;
2171 }
2172
2173 if (m != MATCH_YES)
2174 {
2175 m = MATCH_ERROR;
2176 break;
2177 }
2178
2179 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
2180 sym = symtree->n.sym;
2181
2182 e = gfc_get_expr ();
2183 e->symtree = symtree;
2184 e->expr_type = EXPR_FUNCTION;
2185 e->value.function.actual = actual_arglist;
2186 e->where = gfc_current_locus;
2187
2188 if (sym->as != NULL)
2189 e->rank = sym->as->rank;
2190
2191 if (!sym->attr.function
2192 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2193 {
2194 m = MATCH_ERROR;
2195 break;
2196 }
2197
2198 /* Check here for the existence of at least one argument for the
2199 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
2200 argument(s) given will be checked in gfc_iso_c_func_interface,
2201 during resolution of the function call. */
2202 if (sym->attr.is_iso_c == 1
2203 && (sym->from_intmod == INTMOD_ISO_C_BINDING
2204 && (sym->intmod_sym_id == ISOCBINDING_LOC
2205 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2206 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2207 {
2208 /* make sure we were given a param */
2209 if (actual_arglist == NULL)
2210 {
2211 gfc_error ("Missing argument to '%s' at %C", sym->name);
2212 m = MATCH_ERROR;
2213 break;
2214 }
2215 }
2216
2217 if (sym->result == NULL)
2218 sym->result = sym;
2219
2220 m = MATCH_YES;
2221 break;
2222
2223 case FL_UNKNOWN:
2224
2225 /* Special case for derived type variables that get their types
2226 via an IMPLICIT statement. This can't wait for the
2227 resolution phase. */
2228
2229 if (gfc_peek_char () == '%'
2230 && sym->ts.type == BT_UNKNOWN
2231 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2232 gfc_set_default_type (sym, 0, sym->ns);
2233
2234 /* If the symbol has a dimension attribute, the expression is a
2235 variable. */
2236
2237 if (sym->attr.dimension)
2238 {
2239 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2240 sym->name, NULL) == FAILURE)
2241 {
2242 m = MATCH_ERROR;
2243 break;
2244 }
2245
2246 e = gfc_get_expr ();
2247 e->symtree = symtree;
2248 e->expr_type = EXPR_VARIABLE;
2249 m = match_varspec (e, 0);
2250 break;
2251 }
2252
2253 /* Name is not an array, so we peek to see if a '(' implies a
2254 function call or a substring reference. Otherwise the
2255 variable is just a scalar. */
2256
2257 gfc_gobble_whitespace ();
2258 if (gfc_peek_char () != '(')
2259 {
2260 /* Assume a scalar variable */
2261 e = gfc_get_expr ();
2262 e->symtree = symtree;
2263 e->expr_type = EXPR_VARIABLE;
2264
2265 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2266 sym->name, NULL) == FAILURE)
2267 {
2268 m = MATCH_ERROR;
2269 break;
2270 }
2271
2272 /*FIXME:??? match_varspec does set this for us: */
2273 e->ts = sym->ts;
2274 m = match_varspec (e, 0);
2275 break;
2276 }
2277
2278 /* See if this is a function reference with a keyword argument
2279 as first argument. We do this because otherwise a spurious
2280 symbol would end up in the symbol table. */
2281
2282 old_loc = gfc_current_locus;
2283 m2 = gfc_match (" ( %n =", argname);
2284 gfc_current_locus = old_loc;
2285
2286 e = gfc_get_expr ();
2287 e->symtree = symtree;
2288
2289 if (m2 != MATCH_YES)
2290 {
2291 /* Try to figure out whether we're dealing with a character type.
2292 We're peeking ahead here, because we don't want to call
2293 match_substring if we're dealing with an implicitly typed
2294 non-character variable. */
2295 implicit_char = false;
2296 if (sym->ts.type == BT_UNKNOWN)
2297 {
2298 ts = gfc_get_default_type (sym,NULL);
2299 if (ts->type == BT_CHARACTER)
2300 implicit_char = true;
2301 }
2302
2303 /* See if this could possibly be a substring reference of a name
2304 that we're not sure is a variable yet. */
2305
2306 if ((implicit_char || sym->ts.type == BT_CHARACTER)
2307 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2308 {
2309
2310 e->expr_type = EXPR_VARIABLE;
2311
2312 if (sym->attr.flavor != FL_VARIABLE
2313 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2314 sym->name, NULL) == FAILURE)
2315 {
2316 m = MATCH_ERROR;
2317 break;
2318 }
2319
2320 if (sym->ts.type == BT_UNKNOWN
2321 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2322 {
2323 m = MATCH_ERROR;
2324 break;
2325 }
2326
2327 e->ts = sym->ts;
2328 if (e->ref)
2329 e->ts.cl = NULL;
2330 m = MATCH_YES;
2331 break;
2332 }
2333 }
2334
2335 /* Give up, assume we have a function. */
2336
2337 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2338 sym = symtree->n.sym;
2339 e->expr_type = EXPR_FUNCTION;
2340
2341 if (!sym->attr.function
2342 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2343 {
2344 m = MATCH_ERROR;
2345 break;
2346 }
2347
2348 sym->result = sym;
2349
2350 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2351 if (m == MATCH_NO)
2352 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2353
2354 if (m != MATCH_YES)
2355 {
2356 m = MATCH_ERROR;
2357 break;
2358 }
2359
2360 /* If our new function returns a character, array or structure
2361 type, it might have subsequent references. */
2362
2363 m = match_varspec (e, 0);
2364 if (m == MATCH_NO)
2365 m = MATCH_YES;
2366
2367 break;
2368
2369 generic_function:
2370 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2371
2372 e = gfc_get_expr ();
2373 e->symtree = symtree;
2374 e->expr_type = EXPR_FUNCTION;
2375
2376 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2377 break;
2378
2379 default:
2380 gfc_error ("Symbol at %C is not appropriate for an expression");
2381 return MATCH_ERROR;
2382 }
2383
2384 if (m == MATCH_YES)
2385 {
2386 e->where = where;
2387 *result = e;
2388 }
2389 else
2390 gfc_free_expr (e);
2391
2392 return m;
2393 }
2394
2395
2396 /* Match a variable, ie something that can be assigned to. This
2397 starts as a symbol, can be a structure component or an array
2398 reference. It can be a function if the function doesn't have a
2399 separate RESULT variable. If the symbol has not been previously
2400 seen, we assume it is a variable.
2401
2402 This function is called by two interface functions:
2403 gfc_match_variable, which has host_flag = 1, and
2404 gfc_match_equiv_variable, with host_flag = 0, to restrict the
2405 match of the symbol to the local scope. */
2406
2407 static match
2408 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
2409 {
2410 gfc_symbol *sym;
2411 gfc_symtree *st;
2412 gfc_expr *expr;
2413 locus where;
2414 match m;
2415
2416 /* Since nothing has any business being an lvalue in a module
2417 specification block, an interface block or a contains section,
2418 we force the changed_symbols mechanism to work by setting
2419 host_flag to 0. This prevents valid symbols that have the name
2420 of keywords, such as 'end', being turned into variables by
2421 failed matching to assignments for, eg., END INTERFACE. */
2422 if (gfc_current_state () == COMP_MODULE
2423 || gfc_current_state () == COMP_INTERFACE
2424 || gfc_current_state () == COMP_CONTAINS)
2425 host_flag = 0;
2426
2427 m = gfc_match_sym_tree (&st, host_flag);
2428 if (m != MATCH_YES)
2429 return m;
2430 where = gfc_current_locus;
2431
2432 sym = st->n.sym;
2433
2434 /* If this is an implicit do loop index and implicitly typed,
2435 it should not be host associated. */
2436 m = check_for_implicit_index (&st, &sym);
2437 if (m != MATCH_YES)
2438 return m;
2439
2440 sym->attr.implied_index = 0;
2441
2442 gfc_set_sym_referenced (sym);
2443 switch (sym->attr.flavor)
2444 {
2445 case FL_VARIABLE:
2446 if (sym->attr.protected && sym->attr.use_assoc)
2447 {
2448 gfc_error ("Assigning to PROTECTED variable at %C");
2449 return MATCH_ERROR;
2450 }
2451 break;
2452
2453 case FL_UNKNOWN:
2454 if (sym->attr.access == ACCESS_PUBLIC
2455 || sym->attr.access == ACCESS_PRIVATE)
2456 break;
2457 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2458 sym->name, NULL) == FAILURE)
2459 return MATCH_ERROR;
2460 break;
2461
2462 case FL_PARAMETER:
2463 if (equiv_flag)
2464 gfc_error ("Named constant at %C in an EQUIVALENCE");
2465 else
2466 gfc_error ("Cannot assign to a named constant at %C");
2467 return MATCH_ERROR;
2468 break;
2469
2470 case FL_PROCEDURE:
2471 /* Check for a nonrecursive function result */
2472 if (sym->attr.function && (sym->result == sym || sym->attr.entry)
2473 && !sym->attr.external)
2474 {
2475 /* If a function result is a derived type, then the derived
2476 type may still have to be resolved. */
2477
2478 if (sym->ts.type == BT_DERIVED
2479 && gfc_use_derived (sym->ts.derived) == NULL)
2480 return MATCH_ERROR;
2481 break;
2482 }
2483
2484 /* Fall through to error */
2485
2486 default:
2487 gfc_error ("Expected VARIABLE at %C");
2488 return MATCH_ERROR;
2489 }
2490
2491 /* Special case for derived type variables that get their types
2492 via an IMPLICIT statement. This can't wait for the
2493 resolution phase. */
2494
2495 {
2496 gfc_namespace * implicit_ns;
2497
2498 if (gfc_current_ns->proc_name == sym)
2499 implicit_ns = gfc_current_ns;
2500 else
2501 implicit_ns = sym->ns;
2502
2503 if (gfc_peek_char () == '%'
2504 && sym->ts.type == BT_UNKNOWN
2505 && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2506 gfc_set_default_type (sym, 0, implicit_ns);
2507 }
2508
2509 expr = gfc_get_expr ();
2510
2511 expr->expr_type = EXPR_VARIABLE;
2512 expr->symtree = st;
2513 expr->ts = sym->ts;
2514 expr->where = where;
2515
2516 /* Now see if we have to do more. */
2517 m = match_varspec (expr, equiv_flag);
2518 if (m != MATCH_YES)
2519 {
2520 gfc_free_expr (expr);
2521 return m;
2522 }
2523
2524 *result = expr;
2525 return MATCH_YES;
2526 }
2527
2528
2529 match
2530 gfc_match_variable (gfc_expr **result, int equiv_flag)
2531 {
2532 return match_variable (result, equiv_flag, 1);
2533 }
2534
2535
2536 match
2537 gfc_match_equiv_variable (gfc_expr **result)
2538 {
2539 return match_variable (result, 1, 0);
2540 }
2541