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