]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
re PR fortran/34335 (valid code rejected when reordering USE statements)
[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
d234d788 10Software Foundation; either version 3, or (at your option) any later
d1d61a00 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
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a 21
6de9cd9a
DN
22#include "config.h"
23#include "system.h"
24#include "flags.h"
6de9cd9a
DN
25#include "gfortran.h"
26#include "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
34static match
35match_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
5cf54585 42 m = gfc_match_small_literal_int (kind, NULL);
6de9cd9a
DN
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
76static int
77get_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
96static int
97check_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:
da8309c6 116 r = ISXDIGIT (c);
6de9cd9a
DN
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
132static int
133match_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;
69029c61 145 gfc_gobble_whitespace ();
6de9cd9a
DN
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 {
63645982 159 old_loc = gfc_current_locus;
6de9cd9a
DN
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
63645982 170 gfc_current_locus = old_loc;
6de9cd9a
DN
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
179static match
edf1eac2 180match_integer_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
181{
182 int length, kind;
183 locus old_loc;
184 char *buffer;
185 gfc_expr *e;
186
63645982 187 old_loc = gfc_current_locus;
6de9cd9a
DN
188 gfc_gobble_whitespace ();
189
190 length = match_digits (signflag, 10, NULL);
63645982 191 gfc_current_locus = old_loc;
6de9cd9a
DN
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)
9d64df18 204 kind = gfc_default_integer_kind;
6de9cd9a
DN
205 if (kind == -1)
206 return MATCH_ERROR;
207
e7a2d5fb 208 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
6de9cd9a
DN
209 {
210 gfc_error ("Integer kind %d at %C not available", kind);
211 return MATCH_ERROR;
212 }
213
63645982 214 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
6de9cd9a
DN
215
216 if (gfc_range_check (e) != ARITH_OK)
217 {
b2458f91
TB
218 gfc_error ("Integer too big for its kind at %C. This check can be "
219 "disabled with the option -fno-range-check");
6de9cd9a
DN
220
221 gfc_free_expr (e);
222 return MATCH_ERROR;
223 }
224
225 *result = e;
226 return MATCH_YES;
227}
228
229
d3642f89
FW
230/* Match a Hollerith constant. */
231
232static match
edf1eac2 233match_hollerith_constant (gfc_expr **result)
d3642f89
FW
234{
235 locus old_loc;
edf1eac2
SK
236 gfc_expr *e = NULL;
237 const char *msg;
24bce1fd
SB
238 int num;
239 int i;
d3642f89
FW
240
241 old_loc = gfc_current_locus;
242 gfc_gobble_whitespace ();
243
244 if (match_integer_constant (&e, 0) == MATCH_YES
edf1eac2 245 && gfc_match_char ('h') == MATCH_YES)
d3642f89 246 {
edf1eac2
SK
247 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Hollerith constant "
248 "at %C") == FAILURE)
d3642f89
FW
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 {
edf1eac2
SK
259 gfc_error ("Invalid Hollerith constant: %L must contain at least "
260 "one character", &old_loc);
d3642f89
FW
261 goto cleanup;
262 }
263 if (e->ts.kind != gfc_default_integer_kind)
264 {
eb6d74fa 265 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
edf1eac2 266 "should be default", &old_loc);
d3642f89
FW
267 goto cleanup;
268 }
269 else
270 {
d3642f89 271 gfc_free_expr (e);
edf1eac2
SK
272 e = gfc_constant_result (BT_HOLLERITH, gfc_default_character_kind,
273 &gfc_current_locus);
20585ad6
BM
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
d3642f89
FW
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
292cleanup:
293 gfc_free_expr (e);
294 return MATCH_ERROR;
295}
296
297
6de9cd9a 298/* Match a binary, octal or hexadecimal constant that can be found in
78019d16
SK
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. */
6de9cd9a
DN
303
304static match
edf1eac2 305match_boz_constant (gfc_expr **result)
6de9cd9a 306{
78019d16
SK
307 int post, radix, delim, length, x_hex, kind;
308 locus old_loc, start_loc;
6de9cd9a
DN
309 char *buffer;
310 gfc_expr *e;
6de9cd9a 311
78019d16 312 start_loc = old_loc = gfc_current_locus;
6de9cd9a
DN
313 gfc_gobble_whitespace ();
314
88199e7c 315 x_hex = 0;
78019d16 316 switch (post = gfc_next_char ())
6de9cd9a
DN
317 {
318 case 'b':
319 radix = 2;
78019d16 320 post = 0;
6de9cd9a
DN
321 break;
322 case 'o':
323 radix = 8;
78019d16 324 post = 0;
6de9cd9a
DN
325 break;
326 case 'x':
88199e7c 327 x_hex = 1;
6de9cd9a
DN
328 /* Fall through. */
329 case 'z':
330 radix = 16;
78019d16
SK
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. */
6de9cd9a
DN
339 break;
340 default:
341 goto backup;
342 }
343
344 /* No whitespace allowed here. */
345
78019d16
SK
346 if (post == 0)
347 delim = gfc_next_char ();
348
6de9cd9a
DN
349 if (delim != '\'' && delim != '\"')
350 goto backup;
351
5d874166
TS
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
63645982 358 old_loc = gfc_current_locus;
6de9cd9a
DN
359
360 length = match_digits (0, radix, NULL);
361 if (length == -1)
362 {
78019d16 363 gfc_error ("Empty set of digits in BOZ constant at %C");
6de9cd9a
DN
364 return MATCH_ERROR;
365 }
366
367 if (gfc_next_char () != delim)
368 {
78019d16
SK
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;
21dac32c 379 break;
78019d16
SK
380 case 'o':
381 radix = 8;
21dac32c 382 break;
78019d16
SK
383 case 'x':
384 /* Fall through. */
385 case 'z':
386 radix = 16;
21dac32c 387 break;
31043f6c 388 default:
78019d16 389 goto backup;
31043f6c 390 }
78019d16
SK
391 gfc_notify_std (GFC_STD_GNU, "Extension: BOZ constant "
392 "at %C uses non-standard postfix syntax.");
6de9cd9a
DN
393 }
394
63645982 395 gfc_current_locus = old_loc;
6de9cd9a
DN
396
397 buffer = alloca (length + 1);
398 memset (buffer, '\0', length + 1);
399
400 match_digits (0, radix, buffer);
78019d16
SK
401 gfc_next_char (); /* Eat delimiter. */
402 if (post == 1)
403 gfc_next_char (); /* Eat postfixed b, o, z, or x. */
6de9cd9a 404
f4e7375a
SK
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;
5d874166 413 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
6de9cd9a
DN
414
415 if (gfc_range_check (e) != ARITH_OK)
416 {
5d874166 417 gfc_error ("Integer too big for integer kind %i at %C", kind);
88199e7c
TS
418 gfc_free_expr (e);
419 return MATCH_ERROR;
420 }
421
6de9cd9a
DN
422 *result = e;
423 return MATCH_YES;
424
425backup:
78019d16 426 gfc_current_locus = start_loc;
6de9cd9a
DN
427 return MATCH_NO;
428}
429
430
69029c61
PB
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. */
6de9cd9a
DN
433
434static match
edf1eac2 435match_real_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
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;
69029c61 441 bool negate;
6de9cd9a 442
63645982 443 old_loc = gfc_current_locus;
6de9cd9a
DN
444 gfc_gobble_whitespace ();
445
446 e = NULL;
447
448 count = 0;
449 seen_dp = 0;
450 seen_digits = 0;
451 exp_char = ' ';
69029c61 452 negate = FALSE;
6de9cd9a
DN
453
454 c = gfc_next_char ();
455 if (signflag && (c == '+' || c == '-'))
456 {
69029c61
PB
457 if (c == '-')
458 negate = TRUE;
459
460 gfc_gobble_whitespace ();
6de9cd9a 461 c = gfc_next_char ();
6de9cd9a
DN
462 }
463
02712c16 464 /* Scan significand. */
6de9cd9a
DN
465 for (;; c = gfc_next_char (), count++)
466 {
467 if (c == '.')
468 {
469 if (seen_dp)
470 goto done;
471
edf1eac2
SK
472 /* Check to see if "." goes with a following operator like
473 ".eq.". */
63645982 474 temp_loc = gfc_current_locus;
6de9cd9a
DN
475 c = gfc_next_char ();
476
477 if (c == 'e' || c == 'd' || c == 'q')
478 {
479 c = gfc_next_char ();
480 if (c == '.')
f7b529fa 481 goto done; /* Operator named .e. or .d. */
6de9cd9a
DN
482 }
483
484 if (ISALPHA (c))
485 goto done; /* Distinguish 1.e9 from 1.eq.2 */
486
63645982 487 gfc_current_locus = temp_loc;
6de9cd9a
DN
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
edf1eac2 501 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
6de9cd9a
DN
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 {
6de9cd9a
DN
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
527done:
69029c61 528 /* Check that we have a numeric constant. */
6de9cd9a
DN
529 if (!seen_digits || (!seen_dp && exp_char == ' '))
530 {
63645982 531 gfc_current_locus = old_loc;
6de9cd9a
DN
532 return MATCH_NO;
533 }
534
535 /* Convert the number. */
63645982 536 gfc_current_locus = old_loc;
6de9cd9a
DN
537 gfc_gobble_whitespace ();
538
539 buffer = alloca (count + 1);
540 memset (buffer, '\0', count + 1);
541
6de9cd9a 542 p = buffer;
69029c61
PB
543 c = gfc_next_char ();
544 if (c == '+' || c == '-')
6de9cd9a 545 {
69029c61
PB
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')
6de9cd9a 554 *p = 'e';
69029c61
PB
555 else
556 *p = c;
6de9cd9a 557 p++;
69029c61
PB
558 if (--count == 0)
559 break;
560
561 c = gfc_next_char ();
6de9cd9a
DN
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 {
edf1eac2
SK
573 gfc_error ("Real number at %C has a 'd' exponent and an explicit "
574 "kind");
6de9cd9a
DN
575 goto cleanup;
576 }
9d64df18 577 kind = gfc_default_double_kind;
6de9cd9a
DN
578 break;
579
6de9cd9a
DN
580 default:
581 if (kind == -2)
9d64df18 582 kind = gfc_default_real_kind;
6de9cd9a 583
e7a2d5fb 584 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
6de9cd9a
DN
585 {
586 gfc_error ("Invalid real kind %d at %C", kind);
587 goto cleanup;
588 }
589 }
590
63645982 591 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
69029c61
PB
592 if (negate)
593 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
6de9cd9a
DN
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:
2d8b59df 604 if (gfc_option.warn_underflow)
edf1eac2 605 gfc_warning ("Real constant underflows its kind at %C");
f8e566e5 606 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
2d8b59df 607 break;
6de9cd9a
DN
608
609 default:
610 gfc_internal_error ("gfc_range_check() returned bad value");
611 }
612
613 *result = e;
614 return MATCH_YES;
615
616cleanup:
617 gfc_free_expr (e);
618 return MATCH_ERROR;
619}
620
621
622/* Match a substring reference. */
623
624static match
edf1eac2 625match_substring (gfc_charlen *cl, int init, gfc_ref **result)
6de9cd9a
DN
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
63645982 635 old_loc = gfc_current_locus;
6de9cd9a
DN
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
696syntax:
697 gfc_error ("Syntax error in SUBSTRING specification at %C");
698 m = MATCH_ERROR;
699
700cleanup:
701 gfc_free_expr (start);
702 gfc_free_expr (end);
703
63645982 704 gfc_current_locus = old_loc;
6de9cd9a
DN
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
719static int
720next_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
131c66cd 730 if (gfc_option.flag_backslash && c == '\\')
6de9cd9a 731 {
63645982 732 old_locus = gfc_current_locus;
6de9cd9a 733
a88a266c
SK
734 if (gfc_match_special_char (&c) == MATCH_NO)
735 gfc_current_locus = old_locus;
2e6a83a7
SK
736
737 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
738 gfc_warning ("Extension: backslash character at %C");
6de9cd9a
DN
739 }
740
741 if (c != delimiter)
742 return c;
743
63645982 744 old_locus = gfc_current_locus;
1355d8e7 745 c = gfc_next_char_literal (0);
6de9cd9a
DN
746
747 if (c == delimiter)
748 return c;
63645982 749 gfc_current_locus = old_locus;
6de9cd9a
DN
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
4f8ea09e 757 case of:
6de9cd9a
DN
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
767static match
768match_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 {
63645982 784 old_loc = gfc_current_locus;
6de9cd9a
DN
785 c = gfc_next_char ();
786
787 if (c == '_')
788 {
789 peek = gfc_peek_char ();
790
791 if (peek == '\'' || peek == '\"')
792 {
63645982 793 gfc_current_locus = old_loc;
6de9cd9a
DN
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
820static match
edf1eac2 821match_string_constant (gfc_expr **result)
6de9cd9a
DN
822{
823 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
1355d8e7 824 int i, c, kind, length, delimiter, warn_ampersand;
6de9cd9a
DN
825 locus old_locus, start_locus;
826 gfc_symbol *sym;
827 gfc_expr *e;
828 const char *q;
829 match m;
830
63645982 831 old_locus = gfc_current_locus;
6de9cd9a
DN
832
833 gfc_gobble_whitespace ();
834
63645982 835 start_locus = gfc_current_locus;
6de9cd9a
DN
836
837 c = gfc_next_char ();
838 if (c == '\'' || c == '"')
839 {
9d64df18 840 kind = gfc_default_character_kind;
6de9cd9a
DN
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 {
63645982 859 gfc_current_locus = old_locus;
6de9cd9a
DN
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 ();
63645982 884 start_locus = gfc_current_locus;
6de9cd9a
DN
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
e7a2d5fb 900 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
901 {
902 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
903 return MATCH_ERROR;
904 }
905
906got_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 {
63645982 922 gfc_current_locus = start_locus;
6de9cd9a
DN
923 gfc_error ("Unterminated character constant beginning at %C");
924 return MATCH_ERROR;
925 }
926
927 length++;
928 }
929
78019d16
SK
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
6de9cd9a
DN
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;
a8b3b0b6
CR
943 e->ts.is_c_interop = 0;
944 e->ts.is_iso_c = 0;
6de9cd9a
DN
945 e->where = start_locus;
946
947 e->value.character.string = p = gfc_getmem (length + 1);
948 e->value.character.length = length;
949
63645982 950 gfc_current_locus = start_locus;
6de9cd9a
DN
951 gfc_next_char (); /* Skip delimiter */
952
1355d8e7
TB
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
6de9cd9a
DN
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. */
1355d8e7 962 gfc_option.warn_ampersand = warn_ampersand;
6de9cd9a
DN
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
974no_match:
63645982 975 gfc_current_locus = old_locus;
6de9cd9a
DN
976 return MATCH_NO;
977}
978
979
500f8f7b
RS
980/* Match a .true. or .false. Returns 1 if a .true. was found,
981 0 if a .false. was found, and -1 otherwise. */
982static int
983match_logical_constant_string (void)
984{
985 locus orig_loc = gfc_current_locus;
986
987 gfc_gobble_whitespace ();
988 if (gfc_next_char () == '.')
989 {
990 int ch = gfc_next_char();
991 if (ch == 'f')
992 {
993 if (gfc_next_char () == 'a'
994 && gfc_next_char () == 'l'
995 && gfc_next_char () == 's'
996 && gfc_next_char () == 'e'
997 && gfc_next_char () == '.')
998 /* Matched ".false.". */
999 return 0;
1000 }
1001 else if (ch == 't')
1002 {
1003 if (gfc_next_char () == 'r'
1004 && gfc_next_char () == 'u'
1005 && gfc_next_char () == 'e'
1006 && gfc_next_char () == '.')
1007 /* Matched ".true.". */
1008 return 1;
1009 }
1010 }
1011 gfc_current_locus = orig_loc;
1012 return -1;
1013}
1014
6de9cd9a
DN
1015/* Match a .true. or .false. */
1016
1017static match
edf1eac2 1018match_logical_constant (gfc_expr **result)
6de9cd9a 1019{
6de9cd9a
DN
1020 gfc_expr *e;
1021 int i, kind;
1022
500f8f7b 1023 i = match_logical_constant_string ();
6de9cd9a
DN
1024 if (i == -1)
1025 return MATCH_NO;
1026
1027 kind = get_kind ();
1028 if (kind == -1)
1029 return MATCH_ERROR;
1030 if (kind == -2)
9d64df18 1031 kind = gfc_default_logical_kind;
6de9cd9a 1032
e7a2d5fb 1033 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
eb62be44
SK
1034 {
1035 gfc_error ("Bad kind for logical constant at %C");
1036 return MATCH_ERROR;
1037 }
6de9cd9a
DN
1038
1039 e = gfc_get_expr ();
1040
1041 e->expr_type = EXPR_CONSTANT;
1042 e->value.logical = i;
1043 e->ts.type = BT_LOGICAL;
1044 e->ts.kind = kind;
a8b3b0b6
CR
1045 e->ts.is_c_interop = 0;
1046 e->ts.is_iso_c = 0;
63645982 1047 e->where = gfc_current_locus;
6de9cd9a
DN
1048
1049 *result = e;
1050 return MATCH_YES;
1051}
1052
1053
1054/* Match a real or imaginary part of a complex constant that is a
1055 symbolic constant. */
1056
1057static match
edf1eac2 1058match_sym_complex_part (gfc_expr **result)
6de9cd9a
DN
1059{
1060 char name[GFC_MAX_SYMBOL_LEN + 1];
1061 gfc_symbol *sym;
1062 gfc_expr *e;
1063 match m;
1064
1065 m = gfc_match_name (name);
1066 if (m != MATCH_YES)
1067 return m;
1068
1069 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1070 return MATCH_NO;
1071
1072 if (sym->attr.flavor != FL_PARAMETER)
1073 {
1074 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1075 return MATCH_ERROR;
1076 }
1077
1078 if (!gfc_numeric_ts (&sym->value->ts))
1079 {
1080 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1081 return MATCH_ERROR;
1082 }
1083
1084 if (sym->value->rank != 0)
1085 {
1086 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1087 return MATCH_ERROR;
1088 }
1089
e227ac57
FXC
1090 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
1091 "complex constant at %C") == FAILURE)
1092 return MATCH_ERROR;
1093
6de9cd9a
DN
1094 switch (sym->value->ts.type)
1095 {
1096 case BT_REAL:
1097 e = gfc_copy_expr (sym->value);
1098 break;
1099
1100 case BT_COMPLEX:
1101 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1102 if (e == NULL)
1103 goto error;
1104 break;
1105
1106 case BT_INTEGER:
9d64df18 1107 e = gfc_int2real (sym->value, gfc_default_real_kind);
6de9cd9a
DN
1108 if (e == NULL)
1109 goto error;
1110 break;
1111
1112 default:
1113 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1114 }
1115
edf1eac2 1116 *result = e; /* e is a scalar, real, constant expression. */
6de9cd9a
DN
1117 return MATCH_YES;
1118
1119error:
1120 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1121 return MATCH_ERROR;
1122}
1123
1124
6de9cd9a
DN
1125/* Match a real or imaginary part of a complex number. */
1126
1127static match
edf1eac2 1128match_complex_part (gfc_expr **result)
6de9cd9a
DN
1129{
1130 match m;
1131
1132 m = match_sym_complex_part (result);
1133 if (m != MATCH_NO)
1134 return m;
1135
69029c61
PB
1136 m = match_real_constant (result, 1);
1137 if (m != MATCH_NO)
1138 return m;
1139
1140 return match_integer_constant (result, 1);
6de9cd9a
DN
1141}
1142
1143
1144/* Try to match a complex constant. */
1145
1146static match
edf1eac2 1147match_complex_constant (gfc_expr **result)
6de9cd9a
DN
1148{
1149 gfc_expr *e, *real, *imag;
1150 gfc_error_buf old_error;
1151 gfc_typespec target;
1152 locus old_loc;
1153 int kind;
1154 match m;
1155
63645982 1156 old_loc = gfc_current_locus;
6de9cd9a
DN
1157 real = imag = e = NULL;
1158
1159 m = gfc_match_char ('(');
1160 if (m != MATCH_YES)
1161 return m;
1162
1163 gfc_push_error (&old_error);
1164
1165 m = match_complex_part (&real);
1166 if (m == MATCH_NO)
d71b89ca
JJ
1167 {
1168 gfc_free_error (&old_error);
1169 goto cleanup;
1170 }
6de9cd9a
DN
1171
1172 if (gfc_match_char (',') == MATCH_NO)
1173 {
1174 gfc_pop_error (&old_error);
1175 m = MATCH_NO;
1176 goto cleanup;
1177 }
1178
1179 /* If m is error, then something was wrong with the real part and we
1180 assume we have a complex constant because we've seen the ','. An
1181 ambiguous case here is the start of an iterator list of some
1182 sort. These sort of lists are matched prior to coming here. */
1183
1184 if (m == MATCH_ERROR)
d71b89ca
JJ
1185 {
1186 gfc_free_error (&old_error);
1187 goto cleanup;
1188 }
6de9cd9a
DN
1189 gfc_pop_error (&old_error);
1190
1191 m = match_complex_part (&imag);
1192 if (m == MATCH_NO)
1193 goto syntax;
1194 if (m == MATCH_ERROR)
1195 goto cleanup;
1196
1197 m = gfc_match_char (')');
1198 if (m == MATCH_NO)
87ebdf2f
SK
1199 {
1200 /* Give the matcher for implied do-loops a chance to run. This
1201 yields a much saner error message for (/ (i, 4=i, 6) /). */
1202 if (gfc_peek_char () == '=')
1203 {
1204 m = MATCH_ERROR;
1205 goto cleanup;
1206 }
1207 else
6de9cd9a 1208 goto syntax;
87ebdf2f 1209 }
6de9cd9a
DN
1210
1211 if (m == MATCH_ERROR)
1212 goto cleanup;
1213
1214 /* Decide on the kind of this complex number. */
69029c61
PB
1215 if (real->ts.type == BT_REAL)
1216 {
1217 if (imag->ts.type == BT_REAL)
1218 kind = gfc_kind_max (real, imag);
1219 else
1220 kind = real->ts.kind;
1221 }
1222 else
1223 {
1224 if (imag->ts.type == BT_REAL)
1225 kind = imag->ts.kind;
1226 else
1227 kind = gfc_default_real_kind;
1228 }
6de9cd9a
DN
1229 target.type = BT_REAL;
1230 target.kind = kind;
a8b3b0b6
CR
1231 target.is_c_interop = 0;
1232 target.is_iso_c = 0;
6de9cd9a 1233
69029c61 1234 if (real->ts.type != BT_REAL || kind != real->ts.kind)
6de9cd9a 1235 gfc_convert_type (real, &target, 2);
69029c61 1236 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
6de9cd9a
DN
1237 gfc_convert_type (imag, &target, 2);
1238
1239 e = gfc_convert_complex (real, imag, kind);
63645982 1240 e->where = gfc_current_locus;
6de9cd9a
DN
1241
1242 gfc_free_expr (real);
1243 gfc_free_expr (imag);
1244
1245 *result = e;
1246 return MATCH_YES;
1247
1248syntax:
1249 gfc_error ("Syntax error in COMPLEX constant at %C");
1250 m = MATCH_ERROR;
1251
1252cleanup:
1253 gfc_free_expr (e);
1254 gfc_free_expr (real);
1255 gfc_free_expr (imag);
63645982 1256 gfc_current_locus = old_loc;
6de9cd9a
DN
1257
1258 return m;
1259}
1260
1261
1262/* Match constants in any of several forms. Returns nonzero for a
1263 match, zero for no match. */
1264
1265match
edf1eac2 1266gfc_match_literal_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
1267{
1268 match m;
1269
1270 m = match_complex_constant (result);
1271 if (m != MATCH_NO)
1272 return m;
1273
1274 m = match_string_constant (result);
1275 if (m != MATCH_NO)
1276 return m;
1277
1278 m = match_boz_constant (result);
1279 if (m != MATCH_NO)
1280 return m;
1281
1282 m = match_real_constant (result, signflag);
1283 if (m != MATCH_NO)
1284 return m;
1285
d3642f89
FW
1286 m = match_hollerith_constant (result);
1287 if (m != MATCH_NO)
1288 return m;
1289
6de9cd9a
DN
1290 m = match_integer_constant (result, signflag);
1291 if (m != MATCH_NO)
1292 return m;
1293
1294 m = match_logical_constant (result);
1295 if (m != MATCH_NO)
1296 return m;
1297
1298 return MATCH_NO;
1299}
1300
1301
1302/* Match a single actual argument value. An actual argument is
1303 usually an expression, but can also be a procedure name. If the
1304 argument is a single name, it is not always possible to tell
1305 whether the name is a dummy procedure or not. We treat these cases
1306 by creating an argument that looks like a dummy procedure and
1307 fixing things later during resolution. */
1308
1309static match
edf1eac2 1310match_actual_arg (gfc_expr **result)
6de9cd9a
DN
1311{
1312 char name[GFC_MAX_SYMBOL_LEN + 1];
1313 gfc_symtree *symtree;
1314 locus where, w;
1315 gfc_expr *e;
1316 int c;
1317
63645982 1318 where = gfc_current_locus;
6de9cd9a
DN
1319
1320 switch (gfc_match_name (name))
1321 {
1322 case MATCH_ERROR:
1323 return MATCH_ERROR;
1324
1325 case MATCH_NO:
1326 break;
1327
1328 case MATCH_YES:
63645982 1329 w = gfc_current_locus;
6de9cd9a
DN
1330 gfc_gobble_whitespace ();
1331 c = gfc_next_char ();
63645982 1332 gfc_current_locus = w;
6de9cd9a
DN
1333
1334 if (c != ',' && c != ')')
1335 break;
1336
1337 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1338 break;
1339 /* Handle error elsewhere. */
1340
1341 /* Eliminate a couple of common cases where we know we don't
edf1eac2 1342 have a function argument. */
6de9cd9a 1343 if (symtree == NULL)
edf1eac2 1344 {
6de9cd9a 1345 gfc_get_sym_tree (name, NULL, &symtree);
edf1eac2
SK
1346 gfc_set_sym_referenced (symtree->n.sym);
1347 }
6de9cd9a
DN
1348 else
1349 {
edf1eac2 1350 gfc_symbol *sym;
6de9cd9a 1351
edf1eac2
SK
1352 sym = symtree->n.sym;
1353 gfc_set_sym_referenced (sym);
6de9cd9a
DN
1354 if (sym->attr.flavor != FL_PROCEDURE
1355 && sym->attr.flavor != FL_UNKNOWN)
1356 break;
1357
1358 /* If the symbol is a function with itself as the result and
1359 is being defined, then we have a variable. */
7a4ef45b
JJ
1360 if (sym->attr.function && sym->result == sym)
1361 {
1362 if (gfc_current_ns->proc_name == sym
6de9cd9a 1363 || (gfc_current_ns->parent != NULL
7a4ef45b
JJ
1364 && gfc_current_ns->parent->proc_name == sym))
1365 break;
1366
1367 if (sym->attr.entry
1368 && (sym->ns == gfc_current_ns
1369 || sym->ns == gfc_current_ns->parent))
1370 {
1371 gfc_entry_list *el = NULL;
1372
1373 for (el = sym->ns->entries; el; el = el->next)
1374 if (sym == el->sym)
1375 break;
1376
1377 if (el)
1378 break;
1379 }
1380 }
6de9cd9a
DN
1381 }
1382
1383 e = gfc_get_expr (); /* Leave it unknown for now */
1384 e->symtree = symtree;
1385 e->expr_type = EXPR_VARIABLE;
1386 e->ts.type = BT_PROCEDURE;
1387 e->where = where;
1388
1389 *result = e;
1390 return MATCH_YES;
1391 }
1392
63645982 1393 gfc_current_locus = where;
6de9cd9a
DN
1394 return gfc_match_expr (result);
1395}
1396
1397
1398/* Match a keyword argument. */
1399
1400static match
edf1eac2 1401match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
6de9cd9a
DN
1402{
1403 char name[GFC_MAX_SYMBOL_LEN + 1];
1404 gfc_actual_arglist *a;
1405 locus name_locus;
1406 match m;
1407
63645982 1408 name_locus = gfc_current_locus;
6de9cd9a
DN
1409 m = gfc_match_name (name);
1410
1411 if (m != MATCH_YES)
1412 goto cleanup;
1413 if (gfc_match_char ('=') != MATCH_YES)
1414 {
1415 m = MATCH_NO;
1416 goto cleanup;
1417 }
1418
1419 m = match_actual_arg (&actual->expr);
1420 if (m != MATCH_YES)
1421 goto cleanup;
1422
1423 /* Make sure this name has not appeared yet. */
1424
1425 if (name[0] != '\0')
1426 {
1427 for (a = base; a; a = a->next)
cb9e4f55 1428 if (a->name != NULL && strcmp (a->name, name) == 0)
6de9cd9a 1429 {
edf1eac2
SK
1430 gfc_error ("Keyword '%s' at %C has already appeared in the "
1431 "current argument list", name);
6de9cd9a
DN
1432 return MATCH_ERROR;
1433 }
1434 }
1435
cb9e4f55 1436 actual->name = gfc_get_string (name);
6de9cd9a
DN
1437 return MATCH_YES;
1438
1439cleanup:
63645982 1440 gfc_current_locus = name_locus;
6de9cd9a
DN
1441 return m;
1442}
1443
1444
7fcafa71
PT
1445/* Match an argument list function, such as %VAL. */
1446
1447static match
1448match_arg_list_function (gfc_actual_arglist *result)
1449{
1450 char name[GFC_MAX_SYMBOL_LEN + 1];
1451 locus old_locus;
1452 match m;
1453
1454 old_locus = gfc_current_locus;
1455
1456 if (gfc_match_char ('%') != MATCH_YES)
1457 {
1458 m = MATCH_NO;
1459 goto cleanup;
1460 }
1461
1462 m = gfc_match ("%n (", name);
1463 if (m != MATCH_YES)
1464 goto cleanup;
1465
1466 if (name[0] != '\0')
1467 {
1468 switch (name[0])
1469 {
1470 case 'l':
edf1eac2 1471 if (strncmp (name, "loc", 3) == 0)
7fcafa71
PT
1472 {
1473 result->name = "%LOC";
1474 break;
1475 }
1476 case 'r':
edf1eac2 1477 if (strncmp (name, "ref", 3) == 0)
7fcafa71
PT
1478 {
1479 result->name = "%REF";
1480 break;
1481 }
1482 case 'v':
edf1eac2 1483 if (strncmp (name, "val", 3) == 0)
7fcafa71
PT
1484 {
1485 result->name = "%VAL";
1486 break;
1487 }
1488 default:
1489 m = MATCH_ERROR;
1490 goto cleanup;
1491 }
1492 }
1493
1494 if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
1495 "function at %C") == FAILURE)
1496 {
1497 m = MATCH_ERROR;
1498 goto cleanup;
1499 }
1500
1501 m = match_actual_arg (&result->expr);
1502 if (m != MATCH_YES)
1503 goto cleanup;
1504
1505 if (gfc_match_char (')') != MATCH_YES)
1506 {
1507 m = MATCH_NO;
1508 goto cleanup;
1509 }
1510
1511 return MATCH_YES;
1512
1513cleanup:
1514 gfc_current_locus = old_locus;
1515 return m;
1516}
1517
1518
6de9cd9a
DN
1519/* Matches an actual argument list of a function or subroutine, from
1520 the opening parenthesis to the closing parenthesis. The argument
1521 list is assumed to allow keyword arguments because we don't know if
1522 the symbol associated with the procedure has an implicit interface
ed5ee445 1523 or not. We make sure keywords are unique. If sub_flag is set,
d3fcc995 1524 we're matching the argument list of a subroutine. */
6de9cd9a
DN
1525
1526match
edf1eac2 1527gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
6de9cd9a
DN
1528{
1529 gfc_actual_arglist *head, *tail;
1530 int seen_keyword;
1531 gfc_st_label *label;
1532 locus old_loc;
1533 match m;
1534
1535 *argp = tail = NULL;
63645982 1536 old_loc = gfc_current_locus;
6de9cd9a
DN
1537
1538 seen_keyword = 0;
1539
1540 if (gfc_match_char ('(') == MATCH_NO)
1541 return (sub_flag) ? MATCH_YES : MATCH_NO;
1542
1543 if (gfc_match_char (')') == MATCH_YES)
1544 return MATCH_YES;
1545 head = NULL;
1546
1547 for (;;)
1548 {
1549 if (head == NULL)
1550 head = tail = gfc_get_actual_arglist ();
1551 else
1552 {
1553 tail->next = gfc_get_actual_arglist ();
1554 tail = tail->next;
1555 }
1556
1557 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1558 {
a34a91f0 1559 m = gfc_match_st_label (&label);
6de9cd9a
DN
1560 if (m == MATCH_NO)
1561 gfc_error ("Expected alternate return label at %C");
1562 if (m != MATCH_YES)
1563 goto cleanup;
1564
1565 tail->label = label;
1566 goto next;
1567 }
1568
1569 /* After the first keyword argument is seen, the following
edf1eac2 1570 arguments must also have keywords. */
6de9cd9a
DN
1571 if (seen_keyword)
1572 {
1573 m = match_keyword_arg (tail, head);
1574
1575 if (m == MATCH_ERROR)
1576 goto cleanup;
1577 if (m == MATCH_NO)
1578 {
edf1eac2 1579 gfc_error ("Missing keyword name in actual argument list at %C");
6de9cd9a
DN
1580 goto cleanup;
1581 }
1582
1583 }
1584 else
1585 {
7fcafa71
PT
1586 /* Try an argument list function, like %VAL. */
1587 m = match_arg_list_function (tail);
6de9cd9a
DN
1588 if (m == MATCH_ERROR)
1589 goto cleanup;
1590
7fcafa71
PT
1591 /* See if we have the first keyword argument. */
1592 if (m == MATCH_NO)
1593 {
1594 m = match_keyword_arg (tail, head);
1595 if (m == MATCH_YES)
1596 seen_keyword = 1;
1597 if (m == MATCH_ERROR)
1598 goto cleanup;
1599 }
1600
6de9cd9a
DN
1601 if (m == MATCH_NO)
1602 {
1603 /* Try for a non-keyword argument. */
1604 m = match_actual_arg (&tail->expr);
1605 if (m == MATCH_ERROR)
1606 goto cleanup;
1607 if (m == MATCH_NO)
1608 goto syntax;
1609 }
1610 }
1611
7fcafa71 1612
6de9cd9a
DN
1613 next:
1614 if (gfc_match_char (')') == MATCH_YES)
1615 break;
1616 if (gfc_match_char (',') != MATCH_YES)
1617 goto syntax;
1618 }
1619
1620 *argp = head;
1621 return MATCH_YES;
1622
1623syntax:
1624 gfc_error ("Syntax error in argument list at %C");
1625
1626cleanup:
1627 gfc_free_actual_arglist (head);
63645982 1628 gfc_current_locus = old_loc;
6de9cd9a
DN
1629
1630 return MATCH_ERROR;
1631}
1632
1633
1634/* Used by match_varspec() to extend the reference list by one
1635 element. */
1636
1637static gfc_ref *
edf1eac2 1638extend_ref (gfc_expr *primary, gfc_ref *tail)
6de9cd9a 1639{
6de9cd9a
DN
1640 if (primary->ref == NULL)
1641 primary->ref = tail = gfc_get_ref ();
1642 else
1643 {
1644 if (tail == NULL)
1645 gfc_internal_error ("extend_ref(): Bad tail");
1646 tail->next = gfc_get_ref ();
1647 tail = tail->next;
1648 }
1649
1650 return tail;
1651}
1652
1653
1654/* Match any additional specifications associated with the current
1655 variable like member references or substrings. If equiv_flag is
1656 set we only match stuff that is allowed inside an EQUIVALENCE
1657 statement. */
1658
1659static match
edf1eac2 1660match_varspec (gfc_expr *primary, int equiv_flag)
6de9cd9a
DN
1661{
1662 char name[GFC_MAX_SYMBOL_LEN + 1];
1663 gfc_ref *substring, *tail;
1664 gfc_component *component;
a8006d09 1665 gfc_symbol *sym = primary->symtree->n.sym;
6de9cd9a
DN
1666 match m;
1667
1668 tail = NULL;
1669
edf1eac2 1670 if ((equiv_flag && gfc_peek_char () == '(') || sym->attr.dimension)
6de9cd9a 1671 {
a8006d09
JJ
1672 /* In EQUIVALENCE, we don't know yet whether we are seeing
1673 an array, character variable or array of character
edf1eac2 1674 variables. We'll leave the decision till resolve time. */
6de9cd9a
DN
1675 tail = extend_ref (primary, tail);
1676 tail->type = REF_ARRAY;
1677
a8006d09
JJ
1678 m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1679 equiv_flag);
6de9cd9a
DN
1680 if (m != MATCH_YES)
1681 return m;
a8006d09
JJ
1682
1683 if (equiv_flag && gfc_peek_char () == '(')
1684 {
1685 tail = extend_ref (primary, tail);
1686 tail->type = REF_ARRAY;
1687
1688 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1689 if (m != MATCH_YES)
1690 return m;
1691 }
6de9cd9a
DN
1692 }
1693
6de9cd9a
DN
1694 primary->ts = sym->ts;
1695
a8006d09
JJ
1696 if (equiv_flag)
1697 return MATCH_YES;
1698
6de9cd9a
DN
1699 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1700 goto check_substring;
1701
1702 sym = sym->ts.derived;
1703
1704 for (;;)
1705 {
1706 m = gfc_match_name (name);
1707 if (m == MATCH_NO)
1708 gfc_error ("Expected structure component name at %C");
1709 if (m != MATCH_YES)
1710 return MATCH_ERROR;
1711
1712 component = gfc_find_component (sym, name);
1713 if (component == NULL)
1714 return MATCH_ERROR;
1715
1716 tail = extend_ref (primary, tail);
1717 tail->type = REF_COMPONENT;
1718
1719 tail->u.c.component = component;
1720 tail->u.c.sym = sym;
1721
1722 primary->ts = component->ts;
1723
1724 if (component->as != NULL)
1725 {
1726 tail = extend_ref (primary, tail);
1727 tail->type = REF_ARRAY;
1728
1729 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1730 if (m != MATCH_YES)
1731 return m;
1732 }
1733
1734 if (component->ts.type != BT_DERIVED
1735 || gfc_match_char ('%') != MATCH_YES)
1736 break;
1737
1738 sym = component->ts.derived;
1739 }
1740
1741check_substring:
c040ffff
TS
1742 if (primary->ts.type == BT_UNKNOWN)
1743 {
1744 if (gfc_get_default_type (sym, sym->ns)->type == BT_CHARACTER)
1745 {
edf1eac2
SK
1746 gfc_set_default_type (sym, 0, sym->ns);
1747 primary->ts = sym->ts;
c040ffff
TS
1748 }
1749 }
1750
6de9cd9a
DN
1751 if (primary->ts.type == BT_CHARACTER)
1752 {
1753 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1754 {
1755 case MATCH_YES:
1756 if (tail == NULL)
1757 primary->ref = substring;
1758 else
1759 tail->next = substring;
1760
1761 if (primary->expr_type == EXPR_CONSTANT)
1762 primary->expr_type = EXPR_SUBSTRING;
1763
860c8f3b
PB
1764 if (substring)
1765 primary->ts.cl = NULL;
1766
6de9cd9a
DN
1767 break;
1768
1769 case MATCH_NO:
1770 break;
1771
1772 case MATCH_ERROR:
1773 return MATCH_ERROR;
1774 }
1775 }
1776
1777 return MATCH_YES;
1778}
1779
1780
1781/* Given an expression that is a variable, figure out what the
1782 ultimate variable's type and attribute is, traversing the reference
1783 structures if necessary.
1784
1785 This subroutine is trickier than it looks. We start at the base
1786 symbol and store the attribute. Component references load a
1787 completely new attribute.
1788
1789 A couple of rules come into play. Subobjects of targets are always
1790 targets themselves. If we see a component that goes through a
1791 pointer, then the expression must also be a target, since the
1792 pointer is associated with something (if it isn't core will soon be
1793 dumped). If we see a full part or section of an array, the
1794 expression is also an array.
1795
f7b529fa 1796 We can have at most one full array reference. */
6de9cd9a
DN
1797
1798symbol_attribute
edf1eac2 1799gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
6de9cd9a 1800{
5046aff5 1801 int dimension, pointer, allocatable, target;
6de9cd9a
DN
1802 symbol_attribute attr;
1803 gfc_ref *ref;
1804
1805 if (expr->expr_type != EXPR_VARIABLE)
1806 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1807
1808 ref = expr->ref;
1809 attr = expr->symtree->n.sym->attr;
1810
1811 dimension = attr.dimension;
1812 pointer = attr.pointer;
5046aff5 1813 allocatable = attr.allocatable;
6de9cd9a
DN
1814
1815 target = attr.target;
1816 if (pointer)
1817 target = 1;
1818
1819 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1820 *ts = expr->symtree->n.sym->ts;
1821
1822 for (; ref; ref = ref->next)
1823 switch (ref->type)
1824 {
1825 case REF_ARRAY:
1826
1827 switch (ref->u.ar.type)
1828 {
1829 case AR_FULL:
1830 dimension = 1;
1831 break;
1832
1833 case AR_SECTION:
5046aff5 1834 allocatable = pointer = 0;
6de9cd9a
DN
1835 dimension = 1;
1836 break;
1837
1838 case AR_ELEMENT:
5046aff5 1839 allocatable = pointer = 0;
6de9cd9a
DN
1840 break;
1841
1842 case AR_UNKNOWN:
1843 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1844 }
1845
1846 break;
1847
1848 case REF_COMPONENT:
1849 gfc_get_component_attr (&attr, ref->u.c.component);
1850 if (ts != NULL)
e8a25349
TS
1851 {
1852 *ts = ref->u.c.component->ts;
1853 /* Don't set the string length if a substring reference
1854 follows. */
1855 if (ts->type == BT_CHARACTER
1856 && ref->next && ref->next->type == REF_SUBSTRING)
1857 ts->cl = NULL;
1858 }
6de9cd9a
DN
1859
1860 pointer = ref->u.c.component->pointer;
5046aff5 1861 allocatable = ref->u.c.component->allocatable;
6de9cd9a
DN
1862 if (pointer)
1863 target = 1;
1864
1865 break;
1866
1867 case REF_SUBSTRING:
5046aff5 1868 allocatable = pointer = 0;
6de9cd9a
DN
1869 break;
1870 }
1871
1872 attr.dimension = dimension;
1873 attr.pointer = pointer;
5046aff5 1874 attr.allocatable = allocatable;
6de9cd9a
DN
1875 attr.target = target;
1876
1877 return attr;
1878}
1879
1880
1881/* Return the attribute from a general expression. */
1882
1883symbol_attribute
edf1eac2 1884gfc_expr_attr (gfc_expr *e)
6de9cd9a
DN
1885{
1886 symbol_attribute attr;
1887
1888 switch (e->expr_type)
1889 {
1890 case EXPR_VARIABLE:
1891 attr = gfc_variable_attr (e, NULL);
1892 break;
1893
1894 case EXPR_FUNCTION:
1895 gfc_clear_attr (&attr);
1896
1897 if (e->value.function.esym != NULL)
1898 attr = e->value.function.esym->result->attr;
1899
1900 /* TODO: NULL() returns pointers. May have to take care of this
edf1eac2 1901 here. */
6de9cd9a
DN
1902
1903 break;
1904
1905 default:
1906 gfc_clear_attr (&attr);
1907 break;
1908 }
1909
1910 return attr;
1911}
1912
1913
1914/* Match a structure constructor. The initial symbol has already been
1915 seen. */
1916
d663434b 1917match
edf1eac2 1918gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
6de9cd9a
DN
1919{
1920 gfc_constructor *head, *tail;
1921 gfc_component *comp;
1922 gfc_expr *e;
1923 locus where;
1924 match m;
2eae3dc7 1925 bool private_comp = false;
6de9cd9a
DN
1926
1927 head = tail = NULL;
1928
1929 if (gfc_match_char ('(') != MATCH_YES)
1930 goto syntax;
1931
63645982 1932 where = gfc_current_locus;
6de9cd9a
DN
1933
1934 gfc_find_component (sym, NULL);
1935
1936 for (comp = sym->components; comp; comp = comp->next)
1937 {
2eae3dc7
TB
1938 if (comp->access == ACCESS_PRIVATE)
1939 {
1940 private_comp = true;
1941 break;
1942 }
6de9cd9a
DN
1943 if (head == NULL)
1944 tail = head = gfc_get_constructor ();
1945 else
1946 {
1947 tail->next = gfc_get_constructor ();
1948 tail = tail->next;
1949 }
1950
1951 m = gfc_match_expr (&tail->expr);
1952 if (m == MATCH_NO)
1953 goto syntax;
1954 if (m == MATCH_ERROR)
1955 goto cleanup;
1956
1957 if (gfc_match_char (',') == MATCH_YES)
1958 {
1959 if (comp->next == NULL)
1960 {
edf1eac2 1961 gfc_error ("Too many components in structure constructor at %C");
6de9cd9a
DN
1962 goto cleanup;
1963 }
1964
1965 continue;
1966 }
1967
1968 break;
1969 }
1970
2eae3dc7
TB
1971 if (sym->attr.use_assoc
1972 && (sym->component_access == ACCESS_PRIVATE || private_comp))
1973 {
1974 gfc_error ("Structure constructor for '%s' at %C has PRIVATE "
1975 "components", sym->name);
1976 goto cleanup;
1977 }
1978
6de9cd9a
DN
1979 if (gfc_match_char (')') != MATCH_YES)
1980 goto syntax;
1981
d4d757e6 1982 if (comp && comp->next != NULL)
6de9cd9a
DN
1983 {
1984 gfc_error ("Too few components in structure constructor at %C");
1985 goto cleanup;
1986 }
1987
1988 e = gfc_get_expr ();
1989
1990 e->expr_type = EXPR_STRUCTURE;
1991
1992 e->ts.type = BT_DERIVED;
1993 e->ts.derived = sym;
1994 e->where = where;
1995
1996 e->value.constructor = head;
1997
1998 *result = e;
1999 return MATCH_YES;
2000
2001syntax:
2002 gfc_error ("Syntax error in structure constructor at %C");
2003
2004cleanup:
2005 gfc_free_constructor (head);
2006 return MATCH_ERROR;
2007}
2008
2009
9a3db5a3
PT
2010/* If the symbol is an implicit do loop index and implicitly typed,
2011 it should not be host associated. Provide a symtree from the
2012 current namespace. */
2013static match
2014check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
2015{
2016 if ((*sym)->attr.flavor == FL_VARIABLE
2017 && (*sym)->ns != gfc_current_ns
2018 && (*sym)->attr.implied_index
2019 && (*sym)->attr.implicit_type
2020 && !(*sym)->attr.use_assoc)
2021 {
2022 int i;
2023 i = gfc_get_sym_tree ((*sym)->name, NULL, st);
2024 if (i)
2025 return MATCH_ERROR;
2026 *sym = (*st)->n.sym;
2027 }
2028 return MATCH_YES;
2029}
2030
2031
6de9cd9a
DN
2032/* Matches a variable name followed by anything that might follow it--
2033 array reference, argument list of a function, etc. */
2034
2035match
edf1eac2 2036gfc_match_rvalue (gfc_expr **result)
6de9cd9a
DN
2037{
2038 gfc_actual_arglist *actual_arglist;
d3fcc995 2039 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
2040 gfc_state_data *st;
2041 gfc_symbol *sym;
2042 gfc_symtree *symtree;
d3fcc995 2043 locus where, old_loc;
6de9cd9a 2044 gfc_expr *e;
d3fcc995 2045 match m, m2;
6de9cd9a 2046 int i;
5270c302
AL
2047 gfc_typespec *ts;
2048 bool implicit_char;
a99288e5 2049 gfc_ref *ref;
6de9cd9a
DN
2050
2051 m = gfc_match_name (name);
2052 if (m != MATCH_YES)
2053 return m;
2054
2a6dcee5
TB
2055 if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2056 && !gfc_current_ns->has_import_set)
6de9cd9a
DN
2057 i = gfc_get_sym_tree (name, NULL, &symtree);
2058 else
2059 i = gfc_get_ha_sym_tree (name, &symtree);
2060
2061 if (i)
2062 return MATCH_ERROR;
2063
2064 sym = symtree->n.sym;
2065 e = NULL;
63645982 2066 where = gfc_current_locus;
6de9cd9a 2067
9a3db5a3
PT
2068 /* If this is an implicit do loop index and implicitly typed,
2069 it should not be host associated. */
2070 m = check_for_implicit_index (&symtree, &sym);
2071 if (m != MATCH_YES)
2072 return m;
2073
6de9cd9a 2074 gfc_set_sym_referenced (sym);
9a3db5a3 2075 sym->attr.implied_index = 0;
6de9cd9a 2076
0921bc44
JJ
2077 if (sym->attr.function && sym->result == sym)
2078 {
811849c0
PT
2079 /* See if this is a directly recursive function call. */
2080 gfc_gobble_whitespace ();
2081 if (sym->attr.recursive
edf1eac2 2082 && gfc_peek_char () == '('
fc2d8680
PT
2083 && gfc_current_ns->proc_name == sym
2084 && !sym->attr.dimension)
811849c0 2085 {
fc2d8680
PT
2086 gfc_error ("'%s' at %C is the name of a recursive function "
2087 "and so refers to the result variable. Use an "
2088 "explicit RESULT variable for direct recursion "
2089 "(12.5.2.1)", sym->name);
811849c0
PT
2090 return MATCH_ERROR;
2091 }
fc2d8680 2092
0921bc44 2093 if (gfc_current_ns->proc_name == sym
6de9cd9a 2094 || (gfc_current_ns->parent != NULL
0921bc44
JJ
2095 && gfc_current_ns->parent->proc_name == sym))
2096 goto variable;
2097
2098 if (sym->attr.entry
2099 && (sym->ns == gfc_current_ns
2100 || sym->ns == gfc_current_ns->parent))
2101 {
2102 gfc_entry_list *el = NULL;
2103
2104 for (el = sym->ns->entries; el; el = el->next)
2105 if (sym == el->sym)
2106 goto variable;
2107 }
2108 }
6de9cd9a
DN
2109
2110 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2111 goto function0;
2112
2113 if (sym->attr.generic)
2114 goto generic_function;
2115
2116 switch (sym->attr.flavor)
2117 {
2118 case FL_VARIABLE:
2119 variable:
2120 if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
2121 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2122 gfc_set_default_type (sym, 0, sym->ns);
2123
2124 e = gfc_get_expr ();
2125
2126 e->expr_type = EXPR_VARIABLE;
2127 e->symtree = symtree;
2128
2129 m = match_varspec (e, 0);
2130 break;
2131
2132 case FL_PARAMETER:
b7263e8f
EE
2133 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2134 end up here. Unfortunately, sym->value->expr_type is set to
2135 EXPR_CONSTANT, and so the if () branch would be followed without
2136 the !sym->as check. */
2137 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
6de9cd9a
DN
2138 e = gfc_copy_expr (sym->value);
2139 else
2140 {
2141 e = gfc_get_expr ();
2142 e->expr_type = EXPR_VARIABLE;
2143 }
2144
2145 e->symtree = symtree;
2146 m = match_varspec (e, 0);
a99288e5
PT
2147
2148 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
2149 break;
2150
927171bf
PT
2151 /* Variable array references to derived type parameters cause
2152 all sorts of headaches in simplification. Treating such
2153 expressions as variable works just fine for all array
2154 references. */
2155 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
a99288e5
PT
2156 {
2157 for (ref = e->ref; ref; ref = ref->next)
2158 if (ref->type == REF_ARRAY)
2159 break;
2160
927171bf 2161 if (ref == NULL || ref->u.ar.type == AR_FULL)
a99288e5
PT
2162 break;
2163
2164 ref = e->ref;
2165 e->ref = NULL;
2166 gfc_free_expr (e);
2167 e = gfc_get_expr ();
2168 e->expr_type = EXPR_VARIABLE;
2169 e->symtree = symtree;
2170 e->ref = ref;
a99288e5
PT
2171 }
2172
6de9cd9a
DN
2173 break;
2174
2175 case FL_DERIVED:
2176 sym = gfc_use_derived (sym);
2177 if (sym == NULL)
2178 m = MATCH_ERROR;
2179 else
edf1eac2 2180 m = gfc_match_structure_constructor (sym, &e);
6de9cd9a
DN
2181 break;
2182
2183 /* If we're here, then the name is known to be the name of a
2184 procedure, yet it is not sure to be the name of a function. */
2185 case FL_PROCEDURE:
2186 if (sym->attr.subroutine)
2187 {
2188 gfc_error ("Unexpected use of subroutine name '%s' at %C",
2189 sym->name);
2190 m = MATCH_ERROR;
2191 break;
2192 }
2193
2194 /* At this point, the name has to be a non-statement function.
edf1eac2
SK
2195 If the name is the same as the current function being
2196 compiled, then we have a variable reference (to the function
2197 result) if the name is non-recursive. */
6de9cd9a
DN
2198
2199 st = gfc_enclosing_unit (NULL);
2200
2201 if (st != NULL && st->state == COMP_FUNCTION
2202 && st->sym == sym
2203 && !sym->attr.recursive)
2204 {
2205 e = gfc_get_expr ();
2206 e->symtree = symtree;
2207 e->expr_type = EXPR_VARIABLE;
2208
2209 m = match_varspec (e, 0);
2210 break;
2211 }
2212
2213 /* Match a function reference. */
2214 function0:
2215 m = gfc_match_actual_arglist (0, &actual_arglist);
2216 if (m == MATCH_NO)
2217 {
2218 if (sym->attr.proc == PROC_ST_FUNCTION)
2219 gfc_error ("Statement function '%s' requires argument list at %C",
2220 sym->name);
2221 else
2222 gfc_error ("Function '%s' requires an argument list at %C",
2223 sym->name);
2224
2225 m = MATCH_ERROR;
2226 break;
2227 }
2228
2229 if (m != MATCH_YES)
2230 {
2231 m = MATCH_ERROR;
2232 break;
2233 }
2234
2235 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
2236 sym = symtree->n.sym;
2237
2238 e = gfc_get_expr ();
2239 e->symtree = symtree;
2240 e->expr_type = EXPR_FUNCTION;
2241 e->value.function.actual = actual_arglist;
63645982 2242 e->where = gfc_current_locus;
6de9cd9a
DN
2243
2244 if (sym->as != NULL)
2245 e->rank = sym->as->rank;
2246
2247 if (!sym->attr.function
231b2fcc 2248 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2249 {
2250 m = MATCH_ERROR;
2251 break;
2252 }
2253
a8b3b0b6
CR
2254 /* Check here for the existence of at least one argument for the
2255 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
2256 argument(s) given will be checked in gfc_iso_c_func_interface,
2257 during resolution of the function call. */
2258 if (sym->attr.is_iso_c == 1
2259 && (sym->from_intmod == INTMOD_ISO_C_BINDING
2260 && (sym->intmod_sym_id == ISOCBINDING_LOC
2261 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2262 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2263 {
2264 /* make sure we were given a param */
2265 if (actual_arglist == NULL)
2266 {
2267 gfc_error ("Missing argument to '%s' at %C", sym->name);
2268 m = MATCH_ERROR;
2269 break;
2270 }
2271 }
2272
6de9cd9a
DN
2273 if (sym->result == NULL)
2274 sym->result = sym;
2275
2276 m = MATCH_YES;
2277 break;
2278
2279 case FL_UNKNOWN:
2280
2281 /* Special case for derived type variables that get their types
edf1eac2
SK
2282 via an IMPLICIT statement. This can't wait for the
2283 resolution phase. */
6de9cd9a
DN
2284
2285 if (gfc_peek_char () == '%'
0dd973dd 2286 && sym->ts.type == BT_UNKNOWN
6de9cd9a
DN
2287 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2288 gfc_set_default_type (sym, 0, sym->ns);
2289
2290 /* If the symbol has a dimension attribute, the expression is a
edf1eac2 2291 variable. */
6de9cd9a
DN
2292
2293 if (sym->attr.dimension)
2294 {
231b2fcc
TS
2295 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2296 sym->name, NULL) == FAILURE)
6de9cd9a
DN
2297 {
2298 m = MATCH_ERROR;
2299 break;
2300 }
2301
2302 e = gfc_get_expr ();
2303 e->symtree = symtree;
2304 e->expr_type = EXPR_VARIABLE;
2305 m = match_varspec (e, 0);
2306 break;
2307 }
2308
2309 /* Name is not an array, so we peek to see if a '(' implies a
edf1eac2
SK
2310 function call or a substring reference. Otherwise the
2311 variable is just a scalar. */
6de9cd9a
DN
2312
2313 gfc_gobble_whitespace ();
2314 if (gfc_peek_char () != '(')
2315 {
2316 /* Assume a scalar variable */
2317 e = gfc_get_expr ();
2318 e->symtree = symtree;
2319 e->expr_type = EXPR_VARIABLE;
2320
231b2fcc
TS
2321 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2322 sym->name, NULL) == FAILURE)
6de9cd9a
DN
2323 {
2324 m = MATCH_ERROR;
2325 break;
2326 }
2327
ed5ee445 2328 /*FIXME:??? match_varspec does set this for us: */
6de9cd9a
DN
2329 e->ts = sym->ts;
2330 m = match_varspec (e, 0);
2331 break;
2332 }
2333
d3fcc995
TS
2334 /* See if this is a function reference with a keyword argument
2335 as first argument. We do this because otherwise a spurious
2336 symbol would end up in the symbol table. */
2337
2338 old_loc = gfc_current_locus;
2339 m2 = gfc_match (" ( %n =", argname);
2340 gfc_current_locus = old_loc;
6de9cd9a
DN
2341
2342 e = gfc_get_expr ();
2343 e->symtree = symtree;
2344
d3fcc995 2345 if (m2 != MATCH_YES)
6de9cd9a 2346 {
5270c302
AL
2347 /* Try to figure out whether we're dealing with a character type.
2348 We're peeking ahead here, because we don't want to call
2349 match_substring if we're dealing with an implicitly typed
2350 non-character variable. */
2351 implicit_char = false;
2352 if (sym->ts.type == BT_UNKNOWN)
2353 {
2354 ts = gfc_get_default_type (sym,NULL);
2355 if (ts->type == BT_CHARACTER)
2356 implicit_char = true;
2357 }
2358
d3fcc995
TS
2359 /* See if this could possibly be a substring reference of a name
2360 that we're not sure is a variable yet. */
6de9cd9a 2361
5270c302 2362 if ((implicit_char || sym->ts.type == BT_CHARACTER)
d3fcc995 2363 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
6de9cd9a 2364 {
6de9cd9a 2365
d3fcc995
TS
2366 e->expr_type = EXPR_VARIABLE;
2367
2368 if (sym->attr.flavor != FL_VARIABLE
231b2fcc
TS
2369 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2370 sym->name, NULL) == FAILURE)
d3fcc995
TS
2371 {
2372 m = MATCH_ERROR;
2373 break;
2374 }
2375
2376 if (sym->ts.type == BT_UNKNOWN
2377 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2378 {
2379 m = MATCH_ERROR;
2380 break;
2381 }
2382
2383 e->ts = sym->ts;
860c8f3b
PB
2384 if (e->ref)
2385 e->ts.cl = NULL;
d3fcc995 2386 m = MATCH_YES;
6de9cd9a
DN
2387 break;
2388 }
6de9cd9a
DN
2389 }
2390
2391 /* Give up, assume we have a function. */
2392
2393 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2394 sym = symtree->n.sym;
2395 e->expr_type = EXPR_FUNCTION;
2396
2397 if (!sym->attr.function
231b2fcc 2398 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2399 {
2400 m = MATCH_ERROR;
2401 break;
2402 }
2403
2404 sym->result = sym;
2405
2406 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2407 if (m == MATCH_NO)
2408 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2409
2410 if (m != MATCH_YES)
2411 {
2412 m = MATCH_ERROR;
2413 break;
2414 }
2415
2416 /* If our new function returns a character, array or structure
edf1eac2 2417 type, it might have subsequent references. */
6de9cd9a
DN
2418
2419 m = match_varspec (e, 0);
2420 if (m == MATCH_NO)
2421 m = MATCH_YES;
2422
2423 break;
2424
2425 generic_function:
2426 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2427
2428 e = gfc_get_expr ();
2429 e->symtree = symtree;
2430 e->expr_type = EXPR_FUNCTION;
2431
2432 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2433 break;
2434
2435 default:
2436 gfc_error ("Symbol at %C is not appropriate for an expression");
2437 return MATCH_ERROR;
2438 }
2439
2440 if (m == MATCH_YES)
2441 {
2442 e->where = where;
2443 *result = e;
2444 }
2445 else
2446 gfc_free_expr (e);
2447
2448 return m;
2449}
2450
2451
2452/* Match a variable, ie something that can be assigned to. This
2453 starts as a symbol, can be a structure component or an array
2454 reference. It can be a function if the function doesn't have a
2455 separate RESULT variable. If the symbol has not been previously
30aabb86 2456 seen, we assume it is a variable.
6de9cd9a 2457
30aabb86
PT
2458 This function is called by two interface functions:
2459 gfc_match_variable, which has host_flag = 1, and
2460 gfc_match_equiv_variable, with host_flag = 0, to restrict the
2461 match of the symbol to the local scope. */
2462
2463static match
edf1eac2 2464match_variable (gfc_expr **result, int equiv_flag, int host_flag)
6de9cd9a
DN
2465{
2466 gfc_symbol *sym;
2467 gfc_symtree *st;
2468 gfc_expr *expr;
2469 locus where;
2470 match m;
2471
fd2aa7ad
PT
2472 /* Since nothing has any business being an lvalue in a module
2473 specification block, an interface block or a contains section,
2474 we force the changed_symbols mechanism to work by setting
2475 host_flag to 0. This prevents valid symbols that have the name
2476 of keywords, such as 'end', being turned into variables by
2477 failed matching to assignments for, eg., END INTERFACE. */
2478 if (gfc_current_state () == COMP_MODULE
2479 || gfc_current_state () == COMP_INTERFACE
2480 || gfc_current_state () == COMP_CONTAINS)
2481 host_flag = 0;
2482
30aabb86 2483 m = gfc_match_sym_tree (&st, host_flag);
6de9cd9a
DN
2484 if (m != MATCH_YES)
2485 return m;
63645982 2486 where = gfc_current_locus;
6de9cd9a
DN
2487
2488 sym = st->n.sym;
9a3db5a3
PT
2489
2490 /* If this is an implicit do loop index and implicitly typed,
2491 it should not be host associated. */
2492 m = check_for_implicit_index (&st, &sym);
2493 if (m != MATCH_YES)
2494 return m;
2495
2496 sym->attr.implied_index = 0;
2497
6de9cd9a
DN
2498 gfc_set_sym_referenced (sym);
2499 switch (sym->attr.flavor)
2500 {
2501 case FL_VARIABLE:
ee7e677f 2502 if (sym->attr.protected && sym->attr.use_assoc)
edf1eac2 2503 {
ee7e677f 2504 gfc_error ("Assigning to PROTECTED variable at %C");
edf1eac2
SK
2505 return MATCH_ERROR;
2506 }
6de9cd9a
DN
2507 break;
2508
2509 case FL_UNKNOWN:
4958f4ee
JD
2510 if (sym->attr.access == ACCESS_PUBLIC
2511 || sym->attr.access == ACCESS_PRIVATE)
2512 break;
231b2fcc
TS
2513 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2514 sym->name, NULL) == FAILURE)
6de9cd9a 2515 return MATCH_ERROR;
6de9cd9a
DN
2516 break;
2517
5056a350
SK
2518 case FL_PARAMETER:
2519 if (equiv_flag)
2520 gfc_error ("Named constant at %C in an EQUIVALENCE");
2521 else
2522 gfc_error ("Cannot assign to a named constant at %C");
2523 return MATCH_ERROR;
2524 break;
2525
6de9cd9a
DN
2526 case FL_PROCEDURE:
2527 /* Check for a nonrecursive function result */
b5bf3e4d 2528 if (sym->attr.function && sym->result == sym && !sym->attr.external)
6de9cd9a 2529 {
6de9cd9a
DN
2530 /* If a function result is a derived type, then the derived
2531 type may still have to be resolved. */
2532
2533 if (sym->ts.type == BT_DERIVED
2534 && gfc_use_derived (sym->ts.derived) == NULL)
2535 return MATCH_ERROR;
6de9cd9a
DN
2536 break;
2537 }
2538
2539 /* Fall through to error */
2540
2541 default:
2542 gfc_error ("Expected VARIABLE at %C");
2543 return MATCH_ERROR;
2544 }
2545
0dd973dd
PB
2546 /* Special case for derived type variables that get their types
2547 via an IMPLICIT statement. This can't wait for the
2548 resolution phase. */
2549
2550 {
2551 gfc_namespace * implicit_ns;
2552
2553 if (gfc_current_ns->proc_name == sym)
2554 implicit_ns = gfc_current_ns;
2555 else
2556 implicit_ns = sym->ns;
2557
2558 if (gfc_peek_char () == '%'
2559 && sym->ts.type == BT_UNKNOWN
2560 && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2561 gfc_set_default_type (sym, 0, implicit_ns);
2562 }
2563
6de9cd9a
DN
2564 expr = gfc_get_expr ();
2565
2566 expr->expr_type = EXPR_VARIABLE;
2567 expr->symtree = st;
2568 expr->ts = sym->ts;
2569 expr->where = where;
2570
2571 /* Now see if we have to do more. */
2572 m = match_varspec (expr, equiv_flag);
2573 if (m != MATCH_YES)
2574 {
2575 gfc_free_expr (expr);
2576 return m;
2577 }
2578
2579 *result = expr;
2580 return MATCH_YES;
2581}
30aabb86 2582
edf1eac2 2583
30aabb86 2584match
edf1eac2 2585gfc_match_variable (gfc_expr **result, int equiv_flag)
30aabb86
PT
2586{
2587 return match_variable (result, equiv_flag, 1);
2588}
2589
edf1eac2 2590
30aabb86 2591match
edf1eac2 2592gfc_match_equiv_variable (gfc_expr **result)
30aabb86
PT
2593{
2594 return match_variable (result, 1, 0);
2595}
2596