]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
ipa-prop.c (ipa_print_node_jump_functions): Print jump functions also for indirect...
[thirdparty/gcc.git] / gcc / fortran / primary.c
CommitLineData
6de9cd9a 1/* Primary expression subroutines
b7e75771 2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
8fc541d3 29#include "toplev.h"
b7e75771 30#include "constructor.h"
6de9cd9a
DN
31
32/* Matches a kind-parameter expression, which is either a named
33 symbolic constant or a nonnegative integer constant. If
34 successful, sets the kind value to the correct integer. */
35
36static match
37match_kind_param (int *kind)
38{
39 char name[GFC_MAX_SYMBOL_LEN + 1];
40 gfc_symbol *sym;
41 const char *p;
42 match m;
43
5cf54585 44 m = gfc_match_small_literal_int (kind, NULL);
6de9cd9a
DN
45 if (m != MATCH_NO)
46 return m;
47
48 m = gfc_match_name (name);
49 if (m != MATCH_YES)
50 return m;
51
52 if (gfc_find_symbol (name, NULL, 1, &sym))
53 return MATCH_ERROR;
54
55 if (sym == NULL)
56 return MATCH_NO;
57
58 if (sym->attr.flavor != FL_PARAMETER)
59 return MATCH_NO;
60
1d8e1d5d
PT
61 if (sym->value == NULL)
62 return MATCH_NO;
63
6de9cd9a
DN
64 p = gfc_extract_int (sym->value, kind);
65 if (p != NULL)
66 return MATCH_NO;
67
a39fafac
FXC
68 gfc_set_sym_referenced (sym);
69
6de9cd9a
DN
70 if (*kind < 0)
71 return MATCH_NO;
72
73 return MATCH_YES;
74}
75
76
77/* Get a trailing kind-specification for non-character variables.
78 Returns:
79 the integer kind value or:
80 -1 if an error was generated
81 -2 if no kind was found */
82
83static int
84get_kind (void)
85{
86 int kind;
87 match m;
88
89 if (gfc_match_char ('_') != MATCH_YES)
90 return -2;
91
92 m = match_kind_param (&kind);
93 if (m == MATCH_NO)
94 gfc_error ("Missing kind-parameter at %C");
95
96 return (m == MATCH_YES) ? kind : -1;
97}
98
99
100/* Given a character and a radix, see if the character is a valid
101 digit in that radix. */
102
8fc541d3
FXC
103int
104gfc_check_digit (char c, int radix)
6de9cd9a
DN
105{
106 int r;
107
108 switch (radix)
109 {
110 case 2:
111 r = ('0' <= c && c <= '1');
112 break;
113
114 case 8:
115 r = ('0' <= c && c <= '7');
116 break;
117
118 case 10:
119 r = ('0' <= c && c <= '9');
120 break;
121
122 case 16:
da8309c6 123 r = ISXDIGIT (c);
6de9cd9a
DN
124 break;
125
126 default:
8fc541d3 127 gfc_internal_error ("gfc_check_digit(): bad radix");
6de9cd9a
DN
128 }
129
130 return r;
131}
132
133
134/* Match the digit string part of an integer if signflag is not set,
135 the signed digit string part if signflag is set. If the buffer
136 is NULL, we just count characters for the resolution pass. Returns
137 the number of characters matched, -1 for no match. */
138
139static int
140match_digits (int signflag, int radix, char *buffer)
141{
142 locus old_loc;
8fc541d3
FXC
143 int length;
144 char c;
6de9cd9a
DN
145
146 length = 0;
8fc541d3 147 c = gfc_next_ascii_char ();
6de9cd9a
DN
148
149 if (signflag && (c == '+' || c == '-'))
150 {
151 if (buffer != NULL)
152 *buffer++ = c;
69029c61 153 gfc_gobble_whitespace ();
8fc541d3 154 c = gfc_next_ascii_char ();
6de9cd9a
DN
155 length++;
156 }
157
8fc541d3 158 if (!gfc_check_digit (c, radix))
6de9cd9a
DN
159 return -1;
160
161 length++;
162 if (buffer != NULL)
163 *buffer++ = c;
164
165 for (;;)
166 {
63645982 167 old_loc = gfc_current_locus;
8fc541d3 168 c = gfc_next_ascii_char ();
6de9cd9a 169
8fc541d3 170 if (!gfc_check_digit (c, radix))
6de9cd9a
DN
171 break;
172
173 if (buffer != NULL)
174 *buffer++ = c;
175 length++;
176 }
177
63645982 178 gfc_current_locus = old_loc;
6de9cd9a
DN
179
180 return length;
181}
182
183
184/* Match an integer (digit string and optional kind).
185 A sign will be accepted if signflag is set. */
186
187static match
edf1eac2 188match_integer_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
189{
190 int length, kind;
191 locus old_loc;
192 char *buffer;
193 gfc_expr *e;
194
63645982 195 old_loc = gfc_current_locus;
6de9cd9a
DN
196 gfc_gobble_whitespace ();
197
198 length = match_digits (signflag, 10, NULL);
63645982 199 gfc_current_locus = old_loc;
6de9cd9a
DN
200 if (length == -1)
201 return MATCH_NO;
202
ece3f663 203 buffer = (char *) alloca (length + 1);
6de9cd9a
DN
204 memset (buffer, '\0', length + 1);
205
206 gfc_gobble_whitespace ();
207
208 match_digits (signflag, 10, buffer);
209
210 kind = get_kind ();
211 if (kind == -2)
9d64df18 212 kind = gfc_default_integer_kind;
6de9cd9a
DN
213 if (kind == -1)
214 return MATCH_ERROR;
215
e7a2d5fb 216 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
6de9cd9a
DN
217 {
218 gfc_error ("Integer kind %d at %C not available", kind);
219 return MATCH_ERROR;
220 }
221
63645982 222 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
6de9cd9a
DN
223
224 if (gfc_range_check (e) != ARITH_OK)
225 {
b2458f91
TB
226 gfc_error ("Integer too big for its kind at %C. This check can be "
227 "disabled with the option -fno-range-check");
6de9cd9a
DN
228
229 gfc_free_expr (e);
230 return MATCH_ERROR;
231 }
232
233 *result = e;
234 return MATCH_YES;
235}
236
237
d3642f89
FW
238/* Match a Hollerith constant. */
239
240static match
edf1eac2 241match_hollerith_constant (gfc_expr **result)
d3642f89
FW
242{
243 locus old_loc;
edf1eac2
SK
244 gfc_expr *e = NULL;
245 const char *msg;
24bce1fd
SB
246 int num;
247 int i;
d3642f89
FW
248
249 old_loc = gfc_current_locus;
250 gfc_gobble_whitespace ();
251
252 if (match_integer_constant (&e, 0) == MATCH_YES
edf1eac2 253 && gfc_match_char ('h') == MATCH_YES)
d3642f89 254 {
edf1eac2
SK
255 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Hollerith constant "
256 "at %C") == FAILURE)
d3642f89
FW
257 goto cleanup;
258
259 msg = gfc_extract_int (e, &num);
260 if (msg != NULL)
261 {
262 gfc_error (msg);
263 goto cleanup;
264 }
265 if (num == 0)
266 {
edf1eac2
SK
267 gfc_error ("Invalid Hollerith constant: %L must contain at least "
268 "one character", &old_loc);
d3642f89
FW
269 goto cleanup;
270 }
271 if (e->ts.kind != gfc_default_integer_kind)
272 {
eb6d74fa 273 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
edf1eac2 274 "should be default", &old_loc);
d3642f89
FW
275 goto cleanup;
276 }
277 else
278 {
d3642f89 279 gfc_free_expr (e);
b7e75771
JD
280 e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
281 &gfc_current_locus);
20585ad6 282
ece3f663 283 e->representation.string = XCNEWVEC (char, num + 1);
8fc541d3 284
20585ad6 285 for (i = 0; i < num; i++)
00660189
FXC
286 {
287 gfc_char_t c = gfc_next_char_literal (1);
288 if (! gfc_wide_fits_in_byte (c))
289 {
290 gfc_error ("Invalid Hollerith constant at %L contains a "
291 "wide character", &old_loc);
292 goto cleanup;
293 }
294
295 e->representation.string[i] = (unsigned char) c;
296 }
8fc541d3 297
20585ad6
BM
298 e->representation.string[num] = '\0';
299 e->representation.length = num;
300
d3642f89
FW
301 *result = e;
302 return MATCH_YES;
303 }
304 }
305
306 gfc_free_expr (e);
307 gfc_current_locus = old_loc;
308 return MATCH_NO;
309
310cleanup:
311 gfc_free_expr (e);
312 return MATCH_ERROR;
313}
314
315
6de9cd9a 316/* Match a binary, octal or hexadecimal constant that can be found in
78019d16
SK
317 a DATA statement. The standard permits b'010...', o'73...', and
318 z'a1...' where b, o, and z can be capital letters. This function
319 also accepts postfixed forms of the constants: '01...'b, '73...'o,
320 and 'a1...'z. An additional extension is the use of x for z. */
6de9cd9a
DN
321
322static match
edf1eac2 323match_boz_constant (gfc_expr **result)
6de9cd9a 324{
8fc541d3 325 int radix, length, x_hex, kind;
78019d16 326 locus old_loc, start_loc;
8fc541d3 327 char *buffer, post, delim;
6de9cd9a 328 gfc_expr *e;
6de9cd9a 329
78019d16 330 start_loc = old_loc = gfc_current_locus;
6de9cd9a
DN
331 gfc_gobble_whitespace ();
332
88199e7c 333 x_hex = 0;
8fc541d3 334 switch (post = gfc_next_ascii_char ())
6de9cd9a
DN
335 {
336 case 'b':
337 radix = 2;
78019d16 338 post = 0;
6de9cd9a
DN
339 break;
340 case 'o':
341 radix = 8;
78019d16 342 post = 0;
6de9cd9a
DN
343 break;
344 case 'x':
88199e7c 345 x_hex = 1;
6de9cd9a
DN
346 /* Fall through. */
347 case 'z':
348 radix = 16;
78019d16
SK
349 post = 0;
350 break;
351 case '\'':
352 /* Fall through. */
353 case '\"':
354 delim = post;
355 post = 1;
356 radix = 16; /* Set to accept any valid digit string. */
6de9cd9a
DN
357 break;
358 default:
359 goto backup;
360 }
361
362 /* No whitespace allowed here. */
363
78019d16 364 if (post == 0)
8fc541d3 365 delim = gfc_next_ascii_char ();
78019d16 366
6de9cd9a
DN
367 if (delim != '\'' && delim != '\"')
368 goto backup;
369
00a4618b 370 if (x_hex
5d874166 371 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
0bf0efd5 372 "constant at %C uses non-standard syntax")
5d874166
TS
373 == FAILURE))
374 return MATCH_ERROR;
375
63645982 376 old_loc = gfc_current_locus;
6de9cd9a
DN
377
378 length = match_digits (0, radix, NULL);
379 if (length == -1)
380 {
78019d16 381 gfc_error ("Empty set of digits in BOZ constant at %C");
6de9cd9a
DN
382 return MATCH_ERROR;
383 }
384
8fc541d3 385 if (gfc_next_ascii_char () != delim)
6de9cd9a 386 {
78019d16
SK
387 gfc_error ("Illegal character in BOZ constant at %C");
388 return MATCH_ERROR;
389 }
390
391 if (post == 1)
392 {
8fc541d3 393 switch (gfc_next_ascii_char ())
78019d16
SK
394 {
395 case 'b':
396 radix = 2;
21dac32c 397 break;
78019d16
SK
398 case 'o':
399 radix = 8;
21dac32c 400 break;
78019d16
SK
401 case 'x':
402 /* Fall through. */
403 case 'z':
404 radix = 16;
21dac32c 405 break;
31043f6c 406 default:
78019d16 407 goto backup;
31043f6c 408 }
0bf0efd5
TB
409
410 if (gfc_notify_std (GFC_STD_GNU, "Extension: BOZ constant "
411 "at %C uses non-standard postfix syntax")
412 == FAILURE)
413 return MATCH_ERROR;
6de9cd9a
DN
414 }
415
63645982 416 gfc_current_locus = old_loc;
6de9cd9a 417
ece3f663 418 buffer = (char *) alloca (length + 1);
6de9cd9a
DN
419 memset (buffer, '\0', length + 1);
420
421 match_digits (0, radix, buffer);
8fc541d3 422 gfc_next_ascii_char (); /* Eat delimiter. */
78019d16 423 if (post == 1)
8fc541d3 424 gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
6de9cd9a 425
f4e7375a
SK
426 /* In section 5.2.5 and following C567 in the Fortran 2003 standard, we find
427 "If a data-stmt-constant is a boz-literal-constant, the corresponding
428 variable shall be of type integer. The boz-literal-constant is treated
429 as if it were an int-literal-constant with a kind-param that specifies
430 the representation method with the largest decimal exponent range
431 supported by the processor." */
432
433 kind = gfc_max_integer_kind;
5d874166 434 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
6de9cd9a 435
00a4618b
TB
436 /* Mark as boz variable. */
437 e->is_boz = 1;
438
6de9cd9a
DN
439 if (gfc_range_check (e) != ARITH_OK)
440 {
5d874166 441 gfc_error ("Integer too big for integer kind %i at %C", kind);
88199e7c
TS
442 gfc_free_expr (e);
443 return MATCH_ERROR;
444 }
445
0bf0efd5 446 if (!gfc_in_match_data ()
00a4618b 447 && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BOZ used outside a DATA "
0bf0efd5
TB
448 "statement at %C")
449 == FAILURE))
450 return MATCH_ERROR;
451
6de9cd9a
DN
452 *result = e;
453 return MATCH_YES;
454
455backup:
78019d16 456 gfc_current_locus = start_loc;
6de9cd9a
DN
457 return MATCH_NO;
458}
459
460
69029c61 461/* Match a real constant of some sort. Allow a signed constant if signflag
00a4618b 462 is nonzero. */
6de9cd9a
DN
463
464static match
edf1eac2 465match_real_constant (gfc_expr **result, int signflag)
6de9cd9a 466{
8fc541d3 467 int kind, count, seen_dp, seen_digits;
6de9cd9a 468 locus old_loc, temp_loc;
8fc541d3 469 char *p, *buffer, c, exp_char;
6de9cd9a 470 gfc_expr *e;
69029c61 471 bool negate;
6de9cd9a 472
63645982 473 old_loc = gfc_current_locus;
6de9cd9a
DN
474 gfc_gobble_whitespace ();
475
476 e = NULL;
477
478 count = 0;
479 seen_dp = 0;
480 seen_digits = 0;
481 exp_char = ' ';
69029c61 482 negate = FALSE;
6de9cd9a 483
8fc541d3 484 c = gfc_next_ascii_char ();
6de9cd9a
DN
485 if (signflag && (c == '+' || c == '-'))
486 {
69029c61
PB
487 if (c == '-')
488 negate = TRUE;
489
490 gfc_gobble_whitespace ();
8fc541d3 491 c = gfc_next_ascii_char ();
6de9cd9a
DN
492 }
493
02712c16 494 /* Scan significand. */
8fc541d3 495 for (;; c = gfc_next_ascii_char (), count++)
6de9cd9a
DN
496 {
497 if (c == '.')
498 {
499 if (seen_dp)
500 goto done;
501
edf1eac2
SK
502 /* Check to see if "." goes with a following operator like
503 ".eq.". */
63645982 504 temp_loc = gfc_current_locus;
8fc541d3 505 c = gfc_next_ascii_char ();
6de9cd9a
DN
506
507 if (c == 'e' || c == 'd' || c == 'q')
508 {
8fc541d3 509 c = gfc_next_ascii_char ();
6de9cd9a 510 if (c == '.')
f7b529fa 511 goto done; /* Operator named .e. or .d. */
6de9cd9a
DN
512 }
513
514 if (ISALPHA (c))
515 goto done; /* Distinguish 1.e9 from 1.eq.2 */
516
63645982 517 gfc_current_locus = temp_loc;
6de9cd9a
DN
518 seen_dp = 1;
519 continue;
520 }
521
522 if (ISDIGIT (c))
523 {
524 seen_digits = 1;
525 continue;
526 }
527
528 break;
529 }
530
edf1eac2 531 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
6de9cd9a
DN
532 goto done;
533 exp_char = c;
534
535 /* Scan exponent. */
8fc541d3 536 c = gfc_next_ascii_char ();
6de9cd9a
DN
537 count++;
538
539 if (c == '+' || c == '-')
540 { /* optional sign */
8fc541d3 541 c = gfc_next_ascii_char ();
6de9cd9a
DN
542 count++;
543 }
544
545 if (!ISDIGIT (c))
546 {
6de9cd9a
DN
547 gfc_error ("Missing exponent in real number at %C");
548 return MATCH_ERROR;
549 }
550
551 while (ISDIGIT (c))
552 {
8fc541d3 553 c = gfc_next_ascii_char ();
6de9cd9a
DN
554 count++;
555 }
556
557done:
69029c61 558 /* Check that we have a numeric constant. */
6de9cd9a
DN
559 if (!seen_digits || (!seen_dp && exp_char == ' '))
560 {
63645982 561 gfc_current_locus = old_loc;
6de9cd9a
DN
562 return MATCH_NO;
563 }
564
565 /* Convert the number. */
63645982 566 gfc_current_locus = old_loc;
6de9cd9a
DN
567 gfc_gobble_whitespace ();
568
ece3f663 569 buffer = (char *) alloca (count + 1);
6de9cd9a
DN
570 memset (buffer, '\0', count + 1);
571
6de9cd9a 572 p = buffer;
8fc541d3 573 c = gfc_next_ascii_char ();
69029c61 574 if (c == '+' || c == '-')
6de9cd9a 575 {
69029c61 576 gfc_gobble_whitespace ();
8fc541d3 577 c = gfc_next_ascii_char ();
69029c61
PB
578 }
579
580 /* Hack for mpfr_set_str(). */
581 for (;;)
582 {
583 if (c == 'd' || c == 'q')
6de9cd9a 584 *p = 'e';
69029c61
PB
585 else
586 *p = c;
6de9cd9a 587 p++;
69029c61
PB
588 if (--count == 0)
589 break;
590
8fc541d3 591 c = gfc_next_ascii_char ();
6de9cd9a
DN
592 }
593
594 kind = get_kind ();
595 if (kind == -1)
596 goto cleanup;
597
598 switch (exp_char)
599 {
600 case 'd':
601 if (kind != -2)
602 {
edf1eac2
SK
603 gfc_error ("Real number at %C has a 'd' exponent and an explicit "
604 "kind");
6de9cd9a
DN
605 goto cleanup;
606 }
9d64df18 607 kind = gfc_default_double_kind;
6de9cd9a
DN
608 break;
609
6de9cd9a
DN
610 default:
611 if (kind == -2)
9d64df18 612 kind = gfc_default_real_kind;
6de9cd9a 613
e7a2d5fb 614 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
6de9cd9a
DN
615 {
616 gfc_error ("Invalid real kind %d at %C", kind);
617 goto cleanup;
618 }
619 }
620
63645982 621 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
69029c61
PB
622 if (negate)
623 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
6de9cd9a
DN
624
625 switch (gfc_range_check (e))
626 {
627 case ARITH_OK:
628 break;
629 case ARITH_OVERFLOW:
630 gfc_error ("Real constant overflows its kind at %C");
631 goto cleanup;
632
633 case ARITH_UNDERFLOW:
2d8b59df 634 if (gfc_option.warn_underflow)
edf1eac2 635 gfc_warning ("Real constant underflows its kind at %C");
f8e566e5 636 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
2d8b59df 637 break;
6de9cd9a
DN
638
639 default:
640 gfc_internal_error ("gfc_range_check() returned bad value");
641 }
642
643 *result = e;
644 return MATCH_YES;
645
646cleanup:
647 gfc_free_expr (e);
648 return MATCH_ERROR;
649}
650
651
652/* Match a substring reference. */
653
654static match
edf1eac2 655match_substring (gfc_charlen *cl, int init, gfc_ref **result)
6de9cd9a
DN
656{
657 gfc_expr *start, *end;
658 locus old_loc;
659 gfc_ref *ref;
660 match m;
661
662 start = NULL;
663 end = NULL;
664
63645982 665 old_loc = gfc_current_locus;
6de9cd9a
DN
666
667 m = gfc_match_char ('(');
668 if (m != MATCH_YES)
669 return MATCH_NO;
670
671 if (gfc_match_char (':') != MATCH_YES)
672 {
673 if (init)
674 m = gfc_match_init_expr (&start);
675 else
676 m = gfc_match_expr (&start);
677
678 if (m != MATCH_YES)
679 {
680 m = MATCH_NO;
681 goto cleanup;
682 }
683
684 m = gfc_match_char (':');
685 if (m != MATCH_YES)
686 goto cleanup;
687 }
688
689 if (gfc_match_char (')') != MATCH_YES)
690 {
691 if (init)
692 m = gfc_match_init_expr (&end);
693 else
694 m = gfc_match_expr (&end);
695
696 if (m == MATCH_NO)
697 goto syntax;
698 if (m == MATCH_ERROR)
699 goto cleanup;
700
701 m = gfc_match_char (')');
702 if (m == MATCH_NO)
703 goto syntax;
704 }
705
706 /* Optimize away the (:) reference. */
707 if (start == NULL && end == NULL)
708 ref = NULL;
709 else
710 {
711 ref = gfc_get_ref ();
712
713 ref->type = REF_SUBSTRING;
714 if (start == NULL)
b7e75771 715 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6de9cd9a
DN
716 ref->u.ss.start = start;
717 if (end == NULL && cl)
718 end = gfc_copy_expr (cl->length);
719 ref->u.ss.end = end;
720 ref->u.ss.length = cl;
721 }
722
723 *result = ref;
724 return MATCH_YES;
725
726syntax:
727 gfc_error ("Syntax error in SUBSTRING specification at %C");
728 m = MATCH_ERROR;
729
730cleanup:
731 gfc_free_expr (start);
732 gfc_free_expr (end);
733
63645982 734 gfc_current_locus = old_loc;
6de9cd9a
DN
735 return m;
736}
737
738
739/* Reads the next character of a string constant, taking care to
740 return doubled delimiters on the input as a single instance of
741 the delimiter.
742
8fc541d3 743 Special return values for "ret" argument are:
6de9cd9a
DN
744 -1 End of the string, as determined by the delimiter
745 -2 Unterminated string detected
746
747 Backslash codes are also expanded at this time. */
748
8fc541d3
FXC
749static gfc_char_t
750next_string_char (gfc_char_t delimiter, int *ret)
6de9cd9a
DN
751{
752 locus old_locus;
8fc541d3 753 gfc_char_t c;
6de9cd9a
DN
754
755 c = gfc_next_char_literal (1);
8fc541d3 756 *ret = 0;
6de9cd9a
DN
757
758 if (c == '\n')
8fc541d3
FXC
759 {
760 *ret = -2;
761 return 0;
762 }
6de9cd9a 763
131c66cd 764 if (gfc_option.flag_backslash && c == '\\')
6de9cd9a 765 {
63645982 766 old_locus = gfc_current_locus;
6de9cd9a 767
a88a266c
SK
768 if (gfc_match_special_char (&c) == MATCH_NO)
769 gfc_current_locus = old_locus;
2e6a83a7
SK
770
771 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
772 gfc_warning ("Extension: backslash character at %C");
6de9cd9a
DN
773 }
774
775 if (c != delimiter)
776 return c;
777
63645982 778 old_locus = gfc_current_locus;
1355d8e7 779 c = gfc_next_char_literal (0);
6de9cd9a
DN
780
781 if (c == delimiter)
782 return c;
63645982 783 gfc_current_locus = old_locus;
6de9cd9a 784
8fc541d3
FXC
785 *ret = -1;
786 return 0;
6de9cd9a
DN
787}
788
789
790/* Special case of gfc_match_name() that matches a parameter kind name
791 before a string constant. This takes case of the weird but legal
4f8ea09e 792 case of:
6de9cd9a
DN
793
794 kind_____'string'
795
796 where kind____ is a parameter. gfc_match_name() will happily slurp
797 up all the underscores, which leads to problems. If we return
798 MATCH_YES, the parse pointer points to the final underscore, which
799 is not part of the name. We never return MATCH_ERROR-- errors in
800 the name will be detected later. */
801
802static match
803match_charkind_name (char *name)
804{
805 locus old_loc;
806 char c, peek;
807 int len;
808
809 gfc_gobble_whitespace ();
8fc541d3 810 c = gfc_next_ascii_char ();
6de9cd9a
DN
811 if (!ISALPHA (c))
812 return MATCH_NO;
813
814 *name++ = c;
815 len = 1;
816
817 for (;;)
818 {
63645982 819 old_loc = gfc_current_locus;
8fc541d3 820 c = gfc_next_ascii_char ();
6de9cd9a
DN
821
822 if (c == '_')
823 {
8fc541d3 824 peek = gfc_peek_ascii_char ();
6de9cd9a
DN
825
826 if (peek == '\'' || peek == '\"')
827 {
63645982 828 gfc_current_locus = old_loc;
6de9cd9a
DN
829 *name = '\0';
830 return MATCH_YES;
831 }
832 }
833
834 if (!ISALNUM (c)
835 && c != '_'
ffa61a5e 836 && (c != '$' || !gfc_option.flag_dollar_ok))
6de9cd9a
DN
837 break;
838
839 *name++ = c;
840 if (++len > GFC_MAX_SYMBOL_LEN)
841 break;
842 }
843
844 return MATCH_NO;
845}
846
847
848/* See if the current input matches a character constant. Lots of
849 contortions have to be done to match the kind parameter which comes
850 before the actual string. The main consideration is that we don't
851 want to error out too quickly. For example, we don't actually do
852 any validation of the kinds until we have actually seen a legal
853 delimiter. Using match_kind_param() generates errors too quickly. */
854
855static match
edf1eac2 856match_string_constant (gfc_expr **result)
6de9cd9a 857{
00660189 858 char name[GFC_MAX_SYMBOL_LEN + 1], peek;
8fc541d3 859 int i, kind, length, warn_ampersand, ret;
6de9cd9a
DN
860 locus old_locus, start_locus;
861 gfc_symbol *sym;
862 gfc_expr *e;
863 const char *q;
864 match m;
00660189 865 gfc_char_t c, delimiter, *p;
6de9cd9a 866
63645982 867 old_locus = gfc_current_locus;
6de9cd9a
DN
868
869 gfc_gobble_whitespace ();
870
63645982 871 start_locus = gfc_current_locus;
6de9cd9a
DN
872
873 c = gfc_next_char ();
874 if (c == '\'' || c == '"')
875 {
9d64df18 876 kind = gfc_default_character_kind;
6de9cd9a
DN
877 goto got_delim;
878 }
879
8fc541d3 880 if (gfc_wide_is_digit (c))
6de9cd9a
DN
881 {
882 kind = 0;
883
8fc541d3 884 while (gfc_wide_is_digit (c))
6de9cd9a
DN
885 {
886 kind = kind * 10 + c - '0';
887 if (kind > 9999999)
888 goto no_match;
889 c = gfc_next_char ();
890 }
891
892 }
893 else
894 {
63645982 895 gfc_current_locus = old_locus;
6de9cd9a
DN
896
897 m = match_charkind_name (name);
898 if (m != MATCH_YES)
899 goto no_match;
900
901 if (gfc_find_symbol (name, NULL, 1, &sym)
902 || sym == NULL
903 || sym->attr.flavor != FL_PARAMETER)
904 goto no_match;
905
906 kind = -1;
907 c = gfc_next_char ();
908 }
909
910 if (c == ' ')
911 {
912 gfc_gobble_whitespace ();
913 c = gfc_next_char ();
914 }
915
916 if (c != '_')
917 goto no_match;
918
919 gfc_gobble_whitespace ();
63645982 920 start_locus = gfc_current_locus;
6de9cd9a
DN
921
922 c = gfc_next_char ();
923 if (c != '\'' && c != '"')
924 goto no_match;
925
926 if (kind == -1)
927 {
928 q = gfc_extract_int (sym->value, &kind);
929 if (q != NULL)
930 {
931 gfc_error (q);
932 return MATCH_ERROR;
933 }
a39fafac 934 gfc_set_sym_referenced (sym);
6de9cd9a
DN
935 }
936
e7a2d5fb 937 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
938 {
939 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
940 return MATCH_ERROR;
941 }
942
943got_delim:
944 /* Scan the string into a block of memory by first figuring out how
945 long it is, allocating the structure, then re-reading it. This
946 isn't particularly efficient, but string constants aren't that
947 common in most code. TODO: Use obstacks? */
948
949 delimiter = c;
950 length = 0;
951
952 for (;;)
953 {
8fc541d3
FXC
954 c = next_string_char (delimiter, &ret);
955 if (ret == -1)
6de9cd9a 956 break;
8fc541d3 957 if (ret == -2)
6de9cd9a 958 {
63645982 959 gfc_current_locus = start_locus;
6de9cd9a
DN
960 gfc_error ("Unterminated character constant beginning at %C");
961 return MATCH_ERROR;
962 }
963
964 length++;
965 }
966
78019d16
SK
967 /* Peek at the next character to see if it is a b, o, z, or x for the
968 postfixed BOZ literal constants. */
8fc541d3
FXC
969 peek = gfc_peek_ascii_char ();
970 if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
78019d16
SK
971 goto no_match;
972
b7e75771 973 e = gfc_get_character_expr (kind, &start_locus, NULL, length);
6de9cd9a 974 e->ref = NULL;
a8b3b0b6
CR
975 e->ts.is_c_interop = 0;
976 e->ts.is_iso_c = 0;
6de9cd9a 977
63645982 978 gfc_current_locus = start_locus;
6de9cd9a
DN
979 gfc_next_char (); /* Skip delimiter */
980
1355d8e7
TB
981 /* We disable the warning for the following loop as the warning has already
982 been printed in the loop above. */
983 warn_ampersand = gfc_option.warn_ampersand;
984 gfc_option.warn_ampersand = 0;
985
b7e75771 986 p = e->value.character.string;
6de9cd9a 987 for (i = 0; i < length; i++)
8fc541d3
FXC
988 {
989 c = next_string_char (delimiter, &ret);
990
d393bbd7 991 if (!gfc_check_character_range (c, kind))
8fc541d3 992 {
d393bbd7
FXC
993 gfc_error ("Character '%s' in string at %C is not representable "
994 "in character kind %d", gfc_print_wide_char (c), kind);
8fc541d3
FXC
995 return MATCH_ERROR;
996 }
997
00660189 998 *p++ = c;
8fc541d3 999 }
6de9cd9a
DN
1000
1001 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1355d8e7 1002 gfc_option.warn_ampersand = warn_ampersand;
6de9cd9a 1003
8fc541d3
FXC
1004 next_string_char (delimiter, &ret);
1005 if (ret != -1)
6de9cd9a
DN
1006 gfc_internal_error ("match_string_constant(): Delimiter not found");
1007
1008 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
1009 e->expr_type = EXPR_SUBSTRING;
1010
1011 *result = e;
1012
1013 return MATCH_YES;
1014
1015no_match:
63645982 1016 gfc_current_locus = old_locus;
6de9cd9a
DN
1017 return MATCH_NO;
1018}
1019
1020
500f8f7b
RS
1021/* Match a .true. or .false. Returns 1 if a .true. was found,
1022 0 if a .false. was found, and -1 otherwise. */
1023static int
1024match_logical_constant_string (void)
1025{
1026 locus orig_loc = gfc_current_locus;
1027
1028 gfc_gobble_whitespace ();
8fc541d3 1029 if (gfc_next_ascii_char () == '.')
500f8f7b 1030 {
8fc541d3 1031 char ch = gfc_next_ascii_char ();
500f8f7b
RS
1032 if (ch == 'f')
1033 {
8fc541d3
FXC
1034 if (gfc_next_ascii_char () == 'a'
1035 && gfc_next_ascii_char () == 'l'
1036 && gfc_next_ascii_char () == 's'
1037 && gfc_next_ascii_char () == 'e'
1038 && gfc_next_ascii_char () == '.')
500f8f7b
RS
1039 /* Matched ".false.". */
1040 return 0;
1041 }
1042 else if (ch == 't')
1043 {
8fc541d3
FXC
1044 if (gfc_next_ascii_char () == 'r'
1045 && gfc_next_ascii_char () == 'u'
1046 && gfc_next_ascii_char () == 'e'
1047 && gfc_next_ascii_char () == '.')
500f8f7b
RS
1048 /* Matched ".true.". */
1049 return 1;
1050 }
1051 }
1052 gfc_current_locus = orig_loc;
1053 return -1;
1054}
1055
6de9cd9a
DN
1056/* Match a .true. or .false. */
1057
1058static match
edf1eac2 1059match_logical_constant (gfc_expr **result)
6de9cd9a 1060{
6de9cd9a
DN
1061 gfc_expr *e;
1062 int i, kind;
1063
500f8f7b 1064 i = match_logical_constant_string ();
6de9cd9a
DN
1065 if (i == -1)
1066 return MATCH_NO;
1067
1068 kind = get_kind ();
1069 if (kind == -1)
1070 return MATCH_ERROR;
1071 if (kind == -2)
9d64df18 1072 kind = gfc_default_logical_kind;
6de9cd9a 1073
e7a2d5fb 1074 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
eb62be44
SK
1075 {
1076 gfc_error ("Bad kind for logical constant at %C");
1077 return MATCH_ERROR;
1078 }
6de9cd9a 1079
b7e75771 1080 e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
a8b3b0b6
CR
1081 e->ts.is_c_interop = 0;
1082 e->ts.is_iso_c = 0;
6de9cd9a
DN
1083
1084 *result = e;
1085 return MATCH_YES;
1086}
1087
1088
1089/* Match a real or imaginary part of a complex constant that is a
1090 symbolic constant. */
1091
1092static match
edf1eac2 1093match_sym_complex_part (gfc_expr **result)
6de9cd9a
DN
1094{
1095 char name[GFC_MAX_SYMBOL_LEN + 1];
1096 gfc_symbol *sym;
1097 gfc_expr *e;
1098 match m;
1099
1100 m = gfc_match_name (name);
1101 if (m != MATCH_YES)
1102 return m;
1103
1104 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1105 return MATCH_NO;
1106
1107 if (sym->attr.flavor != FL_PARAMETER)
1108 {
1109 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1110 return MATCH_ERROR;
1111 }
1112
1113 if (!gfc_numeric_ts (&sym->value->ts))
1114 {
1115 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1116 return MATCH_ERROR;
1117 }
1118
1119 if (sym->value->rank != 0)
1120 {
1121 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1122 return MATCH_ERROR;
1123 }
1124
e227ac57
FXC
1125 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
1126 "complex constant at %C") == FAILURE)
1127 return MATCH_ERROR;
1128
6de9cd9a
DN
1129 switch (sym->value->ts.type)
1130 {
1131 case BT_REAL:
1132 e = gfc_copy_expr (sym->value);
1133 break;
1134
1135 case BT_COMPLEX:
1136 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1137 if (e == NULL)
1138 goto error;
1139 break;
1140
1141 case BT_INTEGER:
9d64df18 1142 e = gfc_int2real (sym->value, gfc_default_real_kind);
6de9cd9a
DN
1143 if (e == NULL)
1144 goto error;
1145 break;
1146
1147 default:
1148 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1149 }
1150
edf1eac2 1151 *result = e; /* e is a scalar, real, constant expression. */
6de9cd9a
DN
1152 return MATCH_YES;
1153
1154error:
1155 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1156 return MATCH_ERROR;
1157}
1158
1159
6de9cd9a
DN
1160/* Match a real or imaginary part of a complex number. */
1161
1162static match
edf1eac2 1163match_complex_part (gfc_expr **result)
6de9cd9a
DN
1164{
1165 match m;
1166
1167 m = match_sym_complex_part (result);
1168 if (m != MATCH_NO)
1169 return m;
1170
69029c61
PB
1171 m = match_real_constant (result, 1);
1172 if (m != MATCH_NO)
1173 return m;
1174
1175 return match_integer_constant (result, 1);
6de9cd9a
DN
1176}
1177
1178
1179/* Try to match a complex constant. */
1180
1181static match
edf1eac2 1182match_complex_constant (gfc_expr **result)
6de9cd9a
DN
1183{
1184 gfc_expr *e, *real, *imag;
1185 gfc_error_buf old_error;
1186 gfc_typespec target;
1187 locus old_loc;
1188 int kind;
1189 match m;
1190
63645982 1191 old_loc = gfc_current_locus;
6de9cd9a
DN
1192 real = imag = e = NULL;
1193
1194 m = gfc_match_char ('(');
1195 if (m != MATCH_YES)
1196 return m;
1197
1198 gfc_push_error (&old_error);
1199
1200 m = match_complex_part (&real);
1201 if (m == MATCH_NO)
d71b89ca
JJ
1202 {
1203 gfc_free_error (&old_error);
1204 goto cleanup;
1205 }
6de9cd9a
DN
1206
1207 if (gfc_match_char (',') == MATCH_NO)
1208 {
1209 gfc_pop_error (&old_error);
1210 m = MATCH_NO;
1211 goto cleanup;
1212 }
1213
1214 /* If m is error, then something was wrong with the real part and we
1215 assume we have a complex constant because we've seen the ','. An
1216 ambiguous case here is the start of an iterator list of some
1217 sort. These sort of lists are matched prior to coming here. */
1218
1219 if (m == MATCH_ERROR)
d71b89ca
JJ
1220 {
1221 gfc_free_error (&old_error);
1222 goto cleanup;
1223 }
6de9cd9a
DN
1224 gfc_pop_error (&old_error);
1225
1226 m = match_complex_part (&imag);
1227 if (m == MATCH_NO)
1228 goto syntax;
1229 if (m == MATCH_ERROR)
1230 goto cleanup;
1231
1232 m = gfc_match_char (')');
1233 if (m == MATCH_NO)
87ebdf2f
SK
1234 {
1235 /* Give the matcher for implied do-loops a chance to run. This
1236 yields a much saner error message for (/ (i, 4=i, 6) /). */
8fc541d3 1237 if (gfc_peek_ascii_char () == '=')
87ebdf2f
SK
1238 {
1239 m = MATCH_ERROR;
1240 goto cleanup;
1241 }
1242 else
6de9cd9a 1243 goto syntax;
87ebdf2f 1244 }
6de9cd9a
DN
1245
1246 if (m == MATCH_ERROR)
1247 goto cleanup;
1248
1249 /* Decide on the kind of this complex number. */
69029c61
PB
1250 if (real->ts.type == BT_REAL)
1251 {
1252 if (imag->ts.type == BT_REAL)
1253 kind = gfc_kind_max (real, imag);
1254 else
1255 kind = real->ts.kind;
1256 }
1257 else
1258 {
1259 if (imag->ts.type == BT_REAL)
1260 kind = imag->ts.kind;
1261 else
1262 kind = gfc_default_real_kind;
1263 }
6de9cd9a
DN
1264 target.type = BT_REAL;
1265 target.kind = kind;
a8b3b0b6
CR
1266 target.is_c_interop = 0;
1267 target.is_iso_c = 0;
6de9cd9a 1268
69029c61 1269 if (real->ts.type != BT_REAL || kind != real->ts.kind)
6de9cd9a 1270 gfc_convert_type (real, &target, 2);
69029c61 1271 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
6de9cd9a
DN
1272 gfc_convert_type (imag, &target, 2);
1273
1274 e = gfc_convert_complex (real, imag, kind);
63645982 1275 e->where = gfc_current_locus;
6de9cd9a
DN
1276
1277 gfc_free_expr (real);
1278 gfc_free_expr (imag);
1279
1280 *result = e;
1281 return MATCH_YES;
1282
1283syntax:
1284 gfc_error ("Syntax error in COMPLEX constant at %C");
1285 m = MATCH_ERROR;
1286
1287cleanup:
1288 gfc_free_expr (e);
1289 gfc_free_expr (real);
1290 gfc_free_expr (imag);
63645982 1291 gfc_current_locus = old_loc;
6de9cd9a
DN
1292
1293 return m;
1294}
1295
1296
1297/* Match constants in any of several forms. Returns nonzero for a
1298 match, zero for no match. */
1299
1300match
edf1eac2 1301gfc_match_literal_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
1302{
1303 match m;
1304
1305 m = match_complex_constant (result);
1306 if (m != MATCH_NO)
1307 return m;
1308
1309 m = match_string_constant (result);
1310 if (m != MATCH_NO)
1311 return m;
1312
1313 m = match_boz_constant (result);
1314 if (m != MATCH_NO)
1315 return m;
1316
1317 m = match_real_constant (result, signflag);
1318 if (m != MATCH_NO)
1319 return m;
1320
d3642f89
FW
1321 m = match_hollerith_constant (result);
1322 if (m != MATCH_NO)
1323 return m;
1324
6de9cd9a
DN
1325 m = match_integer_constant (result, signflag);
1326 if (m != MATCH_NO)
1327 return m;
1328
1329 m = match_logical_constant (result);
1330 if (m != MATCH_NO)
1331 return m;
1332
1333 return MATCH_NO;
1334}
1335
1336
2d71b918
JW
1337/* This checks if a symbol is the return value of an encompassing function.
1338 Function nesting can be maximally two levels deep, but we may have
1339 additional local namespaces like BLOCK etc. */
1340
1341bool
1342gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1343{
1344 if (!sym->attr.function || (sym->result != sym))
1345 return false;
1346 while (ns)
1347 {
1348 if (ns->proc_name == sym)
1349 return true;
1350 ns = ns->parent;
1351 }
1352 return false;
1353}
1354
1355
6de9cd9a
DN
1356/* Match a single actual argument value. An actual argument is
1357 usually an expression, but can also be a procedure name. If the
1358 argument is a single name, it is not always possible to tell
1359 whether the name is a dummy procedure or not. We treat these cases
1360 by creating an argument that looks like a dummy procedure and
1361 fixing things later during resolution. */
1362
1363static match
edf1eac2 1364match_actual_arg (gfc_expr **result)
6de9cd9a
DN
1365{
1366 char name[GFC_MAX_SYMBOL_LEN + 1];
1367 gfc_symtree *symtree;
1368 locus where, w;
1369 gfc_expr *e;
8fc541d3 1370 char c;
6de9cd9a 1371
618f4f46 1372 gfc_gobble_whitespace ();
63645982 1373 where = gfc_current_locus;
6de9cd9a
DN
1374
1375 switch (gfc_match_name (name))
1376 {
1377 case MATCH_ERROR:
1378 return MATCH_ERROR;
1379
1380 case MATCH_NO:
1381 break;
1382
1383 case MATCH_YES:
63645982 1384 w = gfc_current_locus;
6de9cd9a 1385 gfc_gobble_whitespace ();
8fc541d3 1386 c = gfc_next_ascii_char ();
63645982 1387 gfc_current_locus = w;
6de9cd9a
DN
1388
1389 if (c != ',' && c != ')')
1390 break;
1391
1392 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1393 break;
1394 /* Handle error elsewhere. */
1395
1396 /* Eliminate a couple of common cases where we know we don't
edf1eac2 1397 have a function argument. */
6de9cd9a 1398 if (symtree == NULL)
edf1eac2 1399 {
08a6b8e0 1400 gfc_get_sym_tree (name, NULL, &symtree, false);
edf1eac2
SK
1401 gfc_set_sym_referenced (symtree->n.sym);
1402 }
6de9cd9a
DN
1403 else
1404 {
edf1eac2 1405 gfc_symbol *sym;
6de9cd9a 1406
edf1eac2
SK
1407 sym = symtree->n.sym;
1408 gfc_set_sym_referenced (sym);
6de9cd9a
DN
1409 if (sym->attr.flavor != FL_PROCEDURE
1410 && sym->attr.flavor != FL_UNKNOWN)
1411 break;
1412
6f9c9d6d
TB
1413 if (sym->attr.in_common && !sym->attr.proc_pointer)
1414 {
1415 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name,
1416 &sym->declared_at);
1417 break;
1418 }
1419
6de9cd9a
DN
1420 /* If the symbol is a function with itself as the result and
1421 is being defined, then we have a variable. */
7a4ef45b
JJ
1422 if (sym->attr.function && sym->result == sym)
1423 {
2d71b918 1424 if (gfc_is_function_return_value (sym, gfc_current_ns))
7a4ef45b
JJ
1425 break;
1426
1427 if (sym->attr.entry
1428 && (sym->ns == gfc_current_ns
1429 || sym->ns == gfc_current_ns->parent))
1430 {
1431 gfc_entry_list *el = NULL;
1432
1433 for (el = sym->ns->entries; el; el = el->next)
1434 if (sym == el->sym)
1435 break;
1436
1437 if (el)
1438 break;
1439 }
1440 }
6de9cd9a
DN
1441 }
1442
1443 e = gfc_get_expr (); /* Leave it unknown for now */
1444 e->symtree = symtree;
1445 e->expr_type = EXPR_VARIABLE;
1446 e->ts.type = BT_PROCEDURE;
1447 e->where = where;
1448
1449 *result = e;
1450 return MATCH_YES;
1451 }
1452
63645982 1453 gfc_current_locus = where;
6de9cd9a
DN
1454 return gfc_match_expr (result);
1455}
1456
1457
1458/* Match a keyword argument. */
1459
1460static match
edf1eac2 1461match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
6de9cd9a
DN
1462{
1463 char name[GFC_MAX_SYMBOL_LEN + 1];
1464 gfc_actual_arglist *a;
1465 locus name_locus;
1466 match m;
1467
63645982 1468 name_locus = gfc_current_locus;
6de9cd9a
DN
1469 m = gfc_match_name (name);
1470
1471 if (m != MATCH_YES)
1472 goto cleanup;
1473 if (gfc_match_char ('=') != MATCH_YES)
1474 {
1475 m = MATCH_NO;
1476 goto cleanup;
1477 }
1478
1479 m = match_actual_arg (&actual->expr);
1480 if (m != MATCH_YES)
1481 goto cleanup;
1482
1483 /* Make sure this name has not appeared yet. */
1484
1485 if (name[0] != '\0')
1486 {
1487 for (a = base; a; a = a->next)
cb9e4f55 1488 if (a->name != NULL && strcmp (a->name, name) == 0)
6de9cd9a 1489 {
edf1eac2
SK
1490 gfc_error ("Keyword '%s' at %C has already appeared in the "
1491 "current argument list", name);
6de9cd9a
DN
1492 return MATCH_ERROR;
1493 }
1494 }
1495
cb9e4f55 1496 actual->name = gfc_get_string (name);
6de9cd9a
DN
1497 return MATCH_YES;
1498
1499cleanup:
63645982 1500 gfc_current_locus = name_locus;
6de9cd9a
DN
1501 return m;
1502}
1503
1504
7fcafa71
PT
1505/* Match an argument list function, such as %VAL. */
1506
1507static match
1508match_arg_list_function (gfc_actual_arglist *result)
1509{
1510 char name[GFC_MAX_SYMBOL_LEN + 1];
1511 locus old_locus;
1512 match m;
1513
1514 old_locus = gfc_current_locus;
1515
1516 if (gfc_match_char ('%') != MATCH_YES)
1517 {
1518 m = MATCH_NO;
1519 goto cleanup;
1520 }
1521
1522 m = gfc_match ("%n (", name);
1523 if (m != MATCH_YES)
1524 goto cleanup;
1525
1526 if (name[0] != '\0')
1527 {
1528 switch (name[0])
1529 {
1530 case 'l':
edf1eac2 1531 if (strncmp (name, "loc", 3) == 0)
7fcafa71
PT
1532 {
1533 result->name = "%LOC";
1534 break;
1535 }
1536 case 'r':
edf1eac2 1537 if (strncmp (name, "ref", 3) == 0)
7fcafa71
PT
1538 {
1539 result->name = "%REF";
1540 break;
1541 }
1542 case 'v':
edf1eac2 1543 if (strncmp (name, "val", 3) == 0)
7fcafa71
PT
1544 {
1545 result->name = "%VAL";
1546 break;
1547 }
1548 default:
1549 m = MATCH_ERROR;
1550 goto cleanup;
1551 }
1552 }
1553
1554 if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
1555 "function at %C") == FAILURE)
1556 {
1557 m = MATCH_ERROR;
1558 goto cleanup;
1559 }
1560
1561 m = match_actual_arg (&result->expr);
1562 if (m != MATCH_YES)
1563 goto cleanup;
1564
1565 if (gfc_match_char (')') != MATCH_YES)
1566 {
1567 m = MATCH_NO;
1568 goto cleanup;
1569 }
1570
1571 return MATCH_YES;
1572
1573cleanup:
1574 gfc_current_locus = old_locus;
1575 return m;
1576}
1577
1578
6de9cd9a
DN
1579/* Matches an actual argument list of a function or subroutine, from
1580 the opening parenthesis to the closing parenthesis. The argument
1581 list is assumed to allow keyword arguments because we don't know if
1582 the symbol associated with the procedure has an implicit interface
ed5ee445 1583 or not. We make sure keywords are unique. If sub_flag is set,
d3fcc995 1584 we're matching the argument list of a subroutine. */
6de9cd9a
DN
1585
1586match
edf1eac2 1587gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
6de9cd9a
DN
1588{
1589 gfc_actual_arglist *head, *tail;
1590 int seen_keyword;
1591 gfc_st_label *label;
1592 locus old_loc;
1593 match m;
1594
1595 *argp = tail = NULL;
63645982 1596 old_loc = gfc_current_locus;
6de9cd9a
DN
1597
1598 seen_keyword = 0;
1599
1600 if (gfc_match_char ('(') == MATCH_NO)
1601 return (sub_flag) ? MATCH_YES : MATCH_NO;
1602
1603 if (gfc_match_char (')') == MATCH_YES)
1604 return MATCH_YES;
1605 head = NULL;
1606
1607 for (;;)
1608 {
1609 if (head == NULL)
1610 head = tail = gfc_get_actual_arglist ();
1611 else
1612 {
1613 tail->next = gfc_get_actual_arglist ();
1614 tail = tail->next;
1615 }
1616
1617 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1618 {
a34a91f0 1619 m = gfc_match_st_label (&label);
6de9cd9a
DN
1620 if (m == MATCH_NO)
1621 gfc_error ("Expected alternate return label at %C");
1622 if (m != MATCH_YES)
1623 goto cleanup;
1624
1625 tail->label = label;
1626 goto next;
1627 }
1628
1629 /* After the first keyword argument is seen, the following
edf1eac2 1630 arguments must also have keywords. */
6de9cd9a
DN
1631 if (seen_keyword)
1632 {
1633 m = match_keyword_arg (tail, head);
1634
1635 if (m == MATCH_ERROR)
1636 goto cleanup;
1637 if (m == MATCH_NO)
1638 {
edf1eac2 1639 gfc_error ("Missing keyword name in actual argument list at %C");
6de9cd9a
DN
1640 goto cleanup;
1641 }
1642
1643 }
1644 else
1645 {
7fcafa71
PT
1646 /* Try an argument list function, like %VAL. */
1647 m = match_arg_list_function (tail);
6de9cd9a
DN
1648 if (m == MATCH_ERROR)
1649 goto cleanup;
1650
7fcafa71
PT
1651 /* See if we have the first keyword argument. */
1652 if (m == MATCH_NO)
1653 {
1654 m = match_keyword_arg (tail, head);
1655 if (m == MATCH_YES)
1656 seen_keyword = 1;
1657 if (m == MATCH_ERROR)
1658 goto cleanup;
1659 }
1660
6de9cd9a
DN
1661 if (m == MATCH_NO)
1662 {
1663 /* Try for a non-keyword argument. */
1664 m = match_actual_arg (&tail->expr);
1665 if (m == MATCH_ERROR)
1666 goto cleanup;
1667 if (m == MATCH_NO)
1668 goto syntax;
1669 }
1670 }
1671
7fcafa71 1672
6de9cd9a
DN
1673 next:
1674 if (gfc_match_char (')') == MATCH_YES)
1675 break;
1676 if (gfc_match_char (',') != MATCH_YES)
1677 goto syntax;
1678 }
1679
1680 *argp = head;
1681 return MATCH_YES;
1682
1683syntax:
1684 gfc_error ("Syntax error in argument list at %C");
1685
1686cleanup:
1687 gfc_free_actual_arglist (head);
63645982 1688 gfc_current_locus = old_loc;
6de9cd9a
DN
1689
1690 return MATCH_ERROR;
1691}
1692
1693
8e1f752a 1694/* Used by gfc_match_varspec() to extend the reference list by one
6de9cd9a
DN
1695 element. */
1696
1697static gfc_ref *
edf1eac2 1698extend_ref (gfc_expr *primary, gfc_ref *tail)
6de9cd9a 1699{
6de9cd9a
DN
1700 if (primary->ref == NULL)
1701 primary->ref = tail = gfc_get_ref ();
1702 else
1703 {
1704 if (tail == NULL)
1705 gfc_internal_error ("extend_ref(): Bad tail");
1706 tail->next = gfc_get_ref ();
1707 tail = tail->next;
1708 }
1709
1710 return tail;
1711}
1712
1713
1714/* Match any additional specifications associated with the current
1715 variable like member references or substrings. If equiv_flag is
1716 set we only match stuff that is allowed inside an EQUIVALENCE
8e1f752a 1717 statement. sub_flag tells whether we expect a type-bound procedure found
713485cc
JW
1718 to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
1719 components, 'ppc_arg' determines whether the PPC may be called (with an
1720 argument list), or whether it may just be referred to as a pointer. */
6de9cd9a 1721
8e1f752a 1722match
713485cc
JW
1723gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
1724 bool ppc_arg)
6de9cd9a
DN
1725{
1726 char name[GFC_MAX_SYMBOL_LEN + 1];
1727 gfc_ref *substring, *tail;
1728 gfc_component *component;
a8006d09 1729 gfc_symbol *sym = primary->symtree->n.sym;
6de9cd9a 1730 match m;
f2d3cb25 1731 bool unknown;
6de9cd9a
DN
1732
1733 tail = NULL;
1734
3c721513 1735 gfc_gobble_whitespace ();
d3a9eea2
TB
1736
1737 if (gfc_peek_ascii_char () == '[')
1738 {
1739 if (sym->attr.dimension)
1740 {
1741 gfc_error ("Array section designator, e.g. '(:)', is required "
1742 "besides the coarray designator '[...]' at %C");
1743 return MATCH_ERROR;
1744 }
1745 if (!sym->attr.codimension)
1746 {
1747 gfc_error ("Coarray designator at %C but '%s' is not a coarray",
1748 sym->name);
1749 return MATCH_ERROR;
1750 }
1751 }
1752
c74b74a8 1753 if ((equiv_flag && gfc_peek_ascii_char () == '(')
d3a9eea2 1754 || gfc_peek_ascii_char () == '[' || sym->attr.codimension
f64edc8b
JW
1755 || (sym->attr.dimension && !sym->attr.proc_pointer
1756 && !gfc_is_proc_ptr_comp (primary, NULL)
1757 && !(gfc_matching_procptr_assignment
cf2b3c22
TB
1758 && sym->attr.flavor == FL_PROCEDURE))
1759 || (sym->ts.type == BT_CLASS
1760 && sym->ts.u.derived->components->attr.dimension))
6de9cd9a 1761 {
a8006d09
JJ
1762 /* In EQUIVALENCE, we don't know yet whether we are seeing
1763 an array, character variable or array of character
edf1eac2 1764 variables. We'll leave the decision till resolve time. */
6de9cd9a
DN
1765 tail = extend_ref (primary, tail);
1766 tail->type = REF_ARRAY;
1767
a8006d09 1768 m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
d3a9eea2 1769 equiv_flag, sym->as ? sym->as->corank : 0);
6de9cd9a
DN
1770 if (m != MATCH_YES)
1771 return m;
a8006d09 1772
3c721513 1773 gfc_gobble_whitespace ();
8fc541d3 1774 if (equiv_flag && gfc_peek_ascii_char () == '(')
a8006d09
JJ
1775 {
1776 tail = extend_ref (primary, tail);
1777 tail->type = REF_ARRAY;
1778
d3a9eea2 1779 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
a8006d09
JJ
1780 if (m != MATCH_YES)
1781 return m;
1782 }
6de9cd9a
DN
1783 }
1784
6de9cd9a
DN
1785 primary->ts = sym->ts;
1786
a8006d09
JJ
1787 if (equiv_flag)
1788 return MATCH_YES;
1789
ebac6d9c 1790 if (sym->ts.type == BT_UNKNOWN && gfc_peek_ascii_char () == '%'
713485cc 1791 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
ebac6d9c
DK
1792 gfc_set_default_type (sym, 0, sym->ns);
1793
cf2b3c22
TB
1794 if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
1795 || gfc_match_char ('%') != MATCH_YES)
6de9cd9a
DN
1796 goto check_substring;
1797
bc21d315 1798 sym = sym->ts.u.derived;
6de9cd9a
DN
1799
1800 for (;;)
1801 {
8e1f752a 1802 gfc_try t;
e157f736 1803 gfc_symtree *tbp;
8e1f752a 1804
6de9cd9a
DN
1805 m = gfc_match_name (name);
1806 if (m == MATCH_NO)
1807 gfc_error ("Expected structure component name at %C");
1808 if (m != MATCH_YES)
1809 return MATCH_ERROR;
1810
b2acf594
PT
1811 if (sym->f2k_derived)
1812 tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
1813 else
1814 tbp = NULL;
1815
8e1f752a
DK
1816 if (tbp)
1817 {
1818 gfc_symbol* tbp_sym;
1819
1820 if (t == FAILURE)
1821 return MATCH_ERROR;
1822
1823 gcc_assert (!tail || !tail->next);
1824 gcc_assert (primary->expr_type == EXPR_VARIABLE);
1825
e34ccb4c 1826 if (tbp->n.tb->is_generic)
e157f736
DK
1827 tbp_sym = NULL;
1828 else
e34ccb4c 1829 tbp_sym = tbp->n.tb->u.specific->n.sym;
8e1f752a
DK
1830
1831 primary->expr_type = EXPR_COMPCALL;
e34ccb4c 1832 primary->value.compcall.tbp = tbp->n.tb;
e157f736 1833 primary->value.compcall.name = tbp->name;
4a44a72d
DK
1834 primary->value.compcall.ignore_pass = 0;
1835 primary->value.compcall.assign = 0;
1836 primary->value.compcall.base_object = NULL;
e157f736
DK
1837 gcc_assert (primary->symtree->n.sym->attr.referenced);
1838 if (tbp_sym)
1839 primary->ts = tbp_sym->ts;
1840
e34ccb4c 1841 m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
8e1f752a
DK
1842 &primary->value.compcall.actual);
1843 if (m == MATCH_ERROR)
1844 return MATCH_ERROR;
1845 if (m == MATCH_NO)
1846 {
1847 if (sub_flag)
1848 primary->value.compcall.actual = NULL;
1849 else
1850 {
1851 gfc_error ("Expected argument list at %C");
1852 return MATCH_ERROR;
1853 }
1854 }
1855
8e1f752a
DK
1856 break;
1857 }
1858
9d1210f4 1859 component = gfc_find_component (sym, name, false, false);
6de9cd9a
DN
1860 if (component == NULL)
1861 return MATCH_ERROR;
1862
1863 tail = extend_ref (primary, tail);
1864 tail->type = REF_COMPONENT;
1865
1866 tail->u.c.component = component;
1867 tail->u.c.sym = sym;
1868
1869 primary->ts = component->ts;
1870
713485cc
JW
1871 if (component->attr.proc_pointer && ppc_arg
1872 && !gfc_matching_procptr_assignment)
1873 {
23878536 1874 m = gfc_match_actual_arglist (sub_flag,
713485cc
JW
1875 &primary->value.compcall.actual);
1876 if (m == MATCH_ERROR)
1877 return MATCH_ERROR;
23878536
JW
1878 if (m == MATCH_YES)
1879 primary->expr_type = EXPR_PPC;
713485cc
JW
1880
1881 break;
1882 }
1883
c74b74a8 1884 if (component->as != NULL && !component->attr.proc_pointer)
6de9cd9a
DN
1885 {
1886 tail = extend_ref (primary, tail);
1887 tail->type = REF_ARRAY;
1888
d3a9eea2
TB
1889 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
1890 component->as->corank);
6de9cd9a
DN
1891 if (m != MATCH_YES)
1892 return m;
1893 }
cf2b3c22
TB
1894 else if (component->ts.type == BT_CLASS
1895 && component->ts.u.derived->components->as != NULL
1896 && !component->attr.proc_pointer)
1897 {
1898 tail = extend_ref (primary, tail);
1899 tail->type = REF_ARRAY;
6de9cd9a 1900
cf2b3c22
TB
1901 m = gfc_match_array_ref (&tail->u.ar,
1902 component->ts.u.derived->components->as,
d3a9eea2
TB
1903 equiv_flag,
1904 component->ts.u.derived->components->as->corank);
cf2b3c22
TB
1905 if (m != MATCH_YES)
1906 return m;
1907 }
1908
1909 if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
6de9cd9a
DN
1910 || gfc_match_char ('%') != MATCH_YES)
1911 break;
1912
bc21d315 1913 sym = component->ts.u.derived;
6de9cd9a
DN
1914 }
1915
1916check_substring:
f2d3cb25 1917 unknown = false;
cf2b3c22 1918 if (primary->ts.type == BT_UNKNOWN && sym->attr.flavor != FL_DERIVED)
c040ffff 1919 {
713485cc 1920 if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
c040ffff 1921 {
edf1eac2
SK
1922 gfc_set_default_type (sym, 0, sym->ns);
1923 primary->ts = sym->ts;
f2d3cb25 1924 unknown = true;
c040ffff
TS
1925 }
1926 }
1927
6de9cd9a
DN
1928 if (primary->ts.type == BT_CHARACTER)
1929 {
bc21d315 1930 switch (match_substring (primary->ts.u.cl, equiv_flag, &substring))
6de9cd9a
DN
1931 {
1932 case MATCH_YES:
1933 if (tail == NULL)
1934 primary->ref = substring;
1935 else
1936 tail->next = substring;
1937
1938 if (primary->expr_type == EXPR_CONSTANT)
1939 primary->expr_type = EXPR_SUBSTRING;
1940
860c8f3b 1941 if (substring)
bc21d315 1942 primary->ts.u.cl = NULL;
860c8f3b 1943
6de9cd9a
DN
1944 break;
1945
1946 case MATCH_NO:
f2d3cb25 1947 if (unknown)
858f1fa2
DK
1948 {
1949 gfc_clear_ts (&primary->ts);
1950 gfc_clear_ts (&sym->ts);
1951 }
6de9cd9a
DN
1952 break;
1953
1954 case MATCH_ERROR:
1955 return MATCH_ERROR;
1956 }
1957 }
1958
d3a9eea2
TB
1959 /* F2008, C727. */
1960 if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
1961 {
1962 gfc_error ("Coindexed procedure-pointer component at %C");
1963 return MATCH_ERROR;
1964 }
1965
6de9cd9a
DN
1966 return MATCH_YES;
1967}
1968
1969
1970/* Given an expression that is a variable, figure out what the
1971 ultimate variable's type and attribute is, traversing the reference
1972 structures if necessary.
1973
1974 This subroutine is trickier than it looks. We start at the base
1975 symbol and store the attribute. Component references load a
1976 completely new attribute.
1977
1978 A couple of rules come into play. Subobjects of targets are always
1979 targets themselves. If we see a component that goes through a
1980 pointer, then the expression must also be a target, since the
1981 pointer is associated with something (if it isn't core will soon be
1982 dumped). If we see a full part or section of an array, the
1983 expression is also an array.
1984
f7b529fa 1985 We can have at most one full array reference. */
6de9cd9a
DN
1986
1987symbol_attribute
edf1eac2 1988gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
6de9cd9a 1989{
5046aff5 1990 int dimension, pointer, allocatable, target;
6de9cd9a
DN
1991 symbol_attribute attr;
1992 gfc_ref *ref;
cf2b3c22
TB
1993 gfc_symbol *sym;
1994 gfc_component *comp;
6de9cd9a 1995
50dbf0b4 1996 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
6de9cd9a
DN
1997 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1998
1999 ref = expr->ref;
cf2b3c22
TB
2000 sym = expr->symtree->n.sym;
2001 attr = sym->attr;
6de9cd9a 2002
cf2b3c22
TB
2003 if (sym->ts.type == BT_CLASS)
2004 {
2005 dimension = sym->ts.u.derived->components->attr.dimension;
2006 pointer = sym->ts.u.derived->components->attr.pointer;
2007 allocatable = sym->ts.u.derived->components->attr.allocatable;
2008 }
2009 else
2010 {
2011 dimension = attr.dimension;
2012 pointer = attr.pointer;
2013 allocatable = attr.allocatable;
2014 }
6de9cd9a
DN
2015
2016 target = attr.target;
713485cc 2017 if (pointer || attr.proc_pointer)
6de9cd9a
DN
2018 target = 1;
2019
2020 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
cf2b3c22 2021 *ts = sym->ts;
6de9cd9a
DN
2022
2023 for (; ref; ref = ref->next)
2024 switch (ref->type)
2025 {
2026 case REF_ARRAY:
2027
2028 switch (ref->u.ar.type)
2029 {
2030 case AR_FULL:
2031 dimension = 1;
2032 break;
2033
2034 case AR_SECTION:
5046aff5 2035 allocatable = pointer = 0;
6de9cd9a
DN
2036 dimension = 1;
2037 break;
2038
2039 case AR_ELEMENT:
d3a9eea2
TB
2040 /* Handle coarrays. */
2041 if (ref->u.ar.dimen > 0)
2042 allocatable = pointer = 0;
6de9cd9a
DN
2043 break;
2044
2045 case AR_UNKNOWN:
2046 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
2047 }
2048
2049 break;
2050
2051 case REF_COMPONENT:
cf2b3c22
TB
2052 comp = ref->u.c.component;
2053 attr = comp->attr;
6de9cd9a 2054 if (ts != NULL)
e8a25349 2055 {
cf2b3c22 2056 *ts = comp->ts;
e8a25349
TS
2057 /* Don't set the string length if a substring reference
2058 follows. */
2059 if (ts->type == BT_CHARACTER
2060 && ref->next && ref->next->type == REF_SUBSTRING)
bc21d315 2061 ts->u.cl = NULL;
e8a25349 2062 }
6de9cd9a 2063
cf2b3c22
TB
2064 if (comp->ts.type == BT_CLASS)
2065 {
2066 pointer = comp->ts.u.derived->components->attr.pointer;
2067 allocatable = comp->ts.u.derived->components->attr.allocatable;
2068 }
2069 else
2070 {
2071 pointer = comp->attr.pointer;
2072 allocatable = comp->attr.allocatable;
2073 }
713485cc 2074 if (pointer || attr.proc_pointer)
6de9cd9a
DN
2075 target = 1;
2076
2077 break;
2078
2079 case REF_SUBSTRING:
5046aff5 2080 allocatable = pointer = 0;
6de9cd9a
DN
2081 break;
2082 }
2083
2084 attr.dimension = dimension;
2085 attr.pointer = pointer;
5046aff5 2086 attr.allocatable = allocatable;
6de9cd9a
DN
2087 attr.target = target;
2088
2089 return attr;
2090}
2091
2092
2093/* Return the attribute from a general expression. */
2094
2095symbol_attribute
edf1eac2 2096gfc_expr_attr (gfc_expr *e)
6de9cd9a
DN
2097{
2098 symbol_attribute attr;
2099
2100 switch (e->expr_type)
2101 {
2102 case EXPR_VARIABLE:
2103 attr = gfc_variable_attr (e, NULL);
2104 break;
2105
2106 case EXPR_FUNCTION:
2107 gfc_clear_attr (&attr);
2108
2109 if (e->value.function.esym != NULL)
cf2b3c22
TB
2110 {
2111 gfc_symbol *sym = e->value.function.esym->result;
2112 attr = sym->attr;
2113 if (sym->ts.type == BT_CLASS)
2114 {
2115 attr.dimension = sym->ts.u.derived->components->attr.dimension;
2116 attr.pointer = sym->ts.u.derived->components->attr.pointer;
2117 attr.allocatable = sym->ts.u.derived->components->attr.allocatable;
2118 }
2119 }
50dbf0b4
JW
2120 else
2121 attr = gfc_variable_attr (e, NULL);
6de9cd9a
DN
2122
2123 /* TODO: NULL() returns pointers. May have to take care of this
edf1eac2 2124 here. */
6de9cd9a
DN
2125
2126 break;
2127
2128 default:
2129 gfc_clear_attr (&attr);
2130 break;
2131 }
2132
2133 return attr;
2134}
2135
2136
2137/* Match a structure constructor. The initial symbol has already been
2138 seen. */
2139
fa9290d3
DK
2140typedef struct gfc_structure_ctor_component
2141{
2142 char* name;
2143 gfc_expr* val;
2144 locus where;
2145 struct gfc_structure_ctor_component* next;
2146}
2147gfc_structure_ctor_component;
2148
ece3f663 2149#define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
fa9290d3
DK
2150
2151static void
2152gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2153{
2154 gfc_free (comp->name);
2155 gfc_free_expr (comp->val);
2156}
2157
7d1f1e61
PT
2158
2159/* Translate the component list into the actual constructor by sorting it in
2160 the order required; this also checks along the way that each and every
2161 component actually has an initializer and handles default initializers
2162 for components without explicit value given. */
2163static gfc_try
2164build_actual_constructor (gfc_structure_ctor_component **comp_head,
b7e75771 2165 gfc_constructor_base *ctor_head, gfc_symbol *sym)
6de9cd9a 2166{
fa9290d3 2167 gfc_structure_ctor_component *comp_iter;
7d1f1e61
PT
2168 gfc_component *comp;
2169
2170 for (comp = sym->components; comp; comp = comp->next)
2171 {
2172 gfc_structure_ctor_component **next_ptr;
2173 gfc_expr *value = NULL;
2174
2175 /* Try to find the initializer for the current component by name. */
2176 next_ptr = comp_head;
2177 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2178 {
2179 if (!strcmp (comp_iter->name, comp->name))
2180 break;
2181 next_ptr = &comp_iter->next;
2182 }
2183
2184 /* If an extension, try building the parent derived type by building
2185 a value expression for the parent derived type and calling self. */
2186 if (!comp_iter && comp == sym->components && sym->attr.extension)
2187 {
b7e75771
JD
2188 value = gfc_get_structure_constructor_expr (comp->ts.type,
2189 comp->ts.kind,
2190 &gfc_current_locus);
7d1f1e61 2191 value->ts = comp->ts;
7d1f1e61
PT
2192
2193 if (build_actual_constructor (comp_head, &value->value.constructor,
bc21d315 2194 comp->ts.u.derived) == FAILURE)
7d1f1e61
PT
2195 {
2196 gfc_free_expr (value);
2197 return FAILURE;
2198 }
b7e75771
JD
2199
2200 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2201 continue;
2202 }
2203
2204 /* If it was not found, try the default initializer if there's any;
2205 otherwise, it's an error. */
2206 if (!comp_iter)
2207 {
2208 if (comp->initializer)
2209 {
2210 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Structure"
2211 " constructor with missing optional arguments"
2212 " at %C") == FAILURE)
2213 return FAILURE;
2214 value = gfc_copy_expr (comp->initializer);
2215 }
2216 else
2217 {
2218 gfc_error ("No initializer for component '%s' given in the"
2219 " structure constructor at %C!", comp->name);
2220 return FAILURE;
2221 }
2222 }
2223 else
2224 value = comp_iter->val;
2225
2226 /* Add the value to the constructor chain built. */
b7e75771 2227 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2228
2229 /* Remove the entry from the component list. We don't want the expression
2230 value to be free'd, so set it to NULL. */
2231 if (comp_iter)
2232 {
2233 *next_ptr = comp_iter->next;
2234 comp_iter->val = NULL;
2235 gfc_free_structure_ctor_component (comp_iter);
2236 }
2237 }
2238 return SUCCESS;
2239}
2240
2241match
52f49934
DK
2242gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result,
2243 bool parent)
7d1f1e61
PT
2244{
2245 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
b7e75771 2246 gfc_constructor_base ctor_head = NULL;
fa9290d3 2247 gfc_component *comp; /* Is set NULL when named component is first seen */
6de9cd9a
DN
2248 gfc_expr *e;
2249 locus where;
2250 match m;
fa9290d3 2251 const char* last_name = NULL;
6de9cd9a 2252
7d1f1e61 2253 comp_tail = comp_head = NULL;
6de9cd9a 2254
7d1f1e61 2255 if (!parent && gfc_match_char ('(') != MATCH_YES)
6de9cd9a
DN
2256 goto syntax;
2257
63645982 2258 where = gfc_current_locus;
6de9cd9a 2259
9d1210f4 2260 gfc_find_component (sym, NULL, false, true);
6de9cd9a 2261
52f49934
DK
2262 /* Check that we're not about to construct an ABSTRACT type. */
2263 if (!parent && sym->attr.abstract)
2264 {
2265 gfc_error ("Can't construct ABSTRACT type '%s' at %C", sym->name);
2266 return MATCH_ERROR;
2267 }
2268
fa9290d3
DK
2269 /* Match the component list and store it in a list together with the
2270 corresponding component names. Check for empty argument list first. */
2271 if (gfc_match_char (')') != MATCH_YES)
6de9cd9a 2272 {
fa9290d3
DK
2273 comp = sym->components;
2274 do
2eae3dc7 2275 {
fa9290d3 2276 gfc_component *this_comp = NULL;
6de9cd9a 2277
fa9290d3
DK
2278 if (!comp_head)
2279 comp_tail = comp_head = gfc_get_structure_ctor_component ();
2280 else
2281 {
2282 comp_tail->next = gfc_get_structure_ctor_component ();
2283 comp_tail = comp_tail->next;
2284 }
ece3f663 2285 comp_tail->name = XCNEWVEC (char, GFC_MAX_SYMBOL_LEN + 1);
fa9290d3
DK
2286 comp_tail->val = NULL;
2287 comp_tail->where = gfc_current_locus;
6de9cd9a 2288
fa9290d3
DK
2289 /* Try matching a component name. */
2290 if (gfc_match_name (comp_tail->name) == MATCH_YES
2291 && gfc_match_char ('=') == MATCH_YES)
2292 {
2293 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Structure"
2294 " constructor with named arguments at %C")
2295 == FAILURE)
2296 goto cleanup;
2297
2298 last_name = comp_tail->name;
2299 comp = NULL;
2300 }
2301 else
2302 {
2303 /* Components without name are not allowed after the first named
2304 component initializer! */
2305 if (!comp)
2306 {
2307 if (last_name)
2308 gfc_error ("Component initializer without name after"
2309 " component named %s at %C!", last_name);
7d1f1e61 2310 else if (!parent)
fa9290d3
DK
2311 gfc_error ("Too many components in structure constructor at"
2312 " %C!");
2313 goto cleanup;
2314 }
2315
2316 gfc_current_locus = comp_tail->where;
2317 strncpy (comp_tail->name, comp->name, GFC_MAX_SYMBOL_LEN + 1);
2318 }
2319
9d1210f4
DK
2320 /* Find the current component in the structure definition and check
2321 its access is not private. */
fa9290d3 2322 if (comp)
9d1210f4 2323 this_comp = gfc_find_component (sym, comp->name, false, false);
fa9290d3
DK
2324 else
2325 {
9d1210f4
DK
2326 this_comp = gfc_find_component (sym,
2327 (const char *)comp_tail->name,
2328 false, false);
fa9290d3 2329 comp = NULL; /* Reset needed! */
fa9290d3 2330 }
fa9290d3 2331
7d1f1e61
PT
2332 /* Here we can check if a component name is given which does not
2333 correspond to any component of the defined structure. */
2334 if (!this_comp)
2335 goto cleanup;
6de9cd9a 2336
fa9290d3
DK
2337 /* Check if this component is already given a value. */
2338 for (comp_iter = comp_head; comp_iter != comp_tail;
2339 comp_iter = comp_iter->next)
2340 {
2341 gcc_assert (comp_iter);
2342 if (!strcmp (comp_iter->name, comp_tail->name))
2343 {
2344 gfc_error ("Component '%s' is initialized twice in the"
2345 " structure constructor at %C!", comp_tail->name);
2346 goto cleanup;
2347 }
2348 }
2349
2350 /* Match the current initializer expression. */
2351 m = gfc_match_expr (&comp_tail->val);
2352 if (m == MATCH_NO)
2353 goto syntax;
2354 if (m == MATCH_ERROR)
2355 goto cleanup;
2356
d3a9eea2
TB
2357 /* F2008, R457/C725, for PURE C1283. */
2358 if (this_comp->attr.pointer && gfc_is_coindexed (comp_tail->val))
2359 {
2360 gfc_error ("Coindexed expression to pointer component '%s' in "
2361 "structure constructor at %C!", comp_tail->name);
2362 goto cleanup;
2363 }
2364
2365
7d1f1e61
PT
2366 /* If not explicitly a parent constructor, gather up the components
2367 and build one. */
2368 if (comp && comp == sym->components
2369 && sym->attr.extension
2370 && (comp_tail->val->ts.type != BT_DERIVED
2371 ||
bc21d315 2372 comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
7d1f1e61
PT
2373 {
2374 gfc_current_locus = where;
2375 gfc_free_expr (comp_tail->val);
52f49934 2376 comp_tail->val = NULL;
6de9cd9a 2377
bc21d315 2378 m = gfc_match_structure_constructor (comp->ts.u.derived,
7d1f1e61
PT
2379 &comp_tail->val, true);
2380 if (m == MATCH_NO)
2381 goto syntax;
2382 if (m == MATCH_ERROR)
2383 goto cleanup;
2384 }
6de9cd9a 2385
7d1f1e61
PT
2386 if (comp)
2387 comp = comp->next;
2eae3dc7 2388
7d1f1e61 2389 if (parent && !comp)
fa9290d3 2390 break;
fa9290d3 2391 }
fa9290d3 2392
7d1f1e61 2393 while (gfc_match_char (',') == MATCH_YES);
fa9290d3 2394
7d1f1e61
PT
2395 if (!parent && gfc_match_char (')') != MATCH_YES)
2396 goto syntax;
6de9cd9a
DN
2397 }
2398
7d1f1e61
PT
2399 if (build_actual_constructor (&comp_head, &ctor_head, sym) == FAILURE)
2400 goto cleanup;
2401
fa9290d3
DK
2402 /* No component should be left, as this should have caused an error in the
2403 loop constructing the component-list (name that does not correspond to any
2404 component in the structure definition). */
7d1f1e61
PT
2405 if (comp_head && sym->attr.extension)
2406 {
2407 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
2408 {
2409 gfc_error ("component '%s' at %L has already been set by a "
2410 "parent derived type constructor", comp_iter->name,
2411 &comp_iter->where);
2412 }
2413 goto cleanup;
2414 }
2415 else
2416 gcc_assert (!comp_head);
fa9290d3 2417
b7e75771 2418 e = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &where);
bc21d315 2419 e->ts.u.derived = sym;
fa9290d3 2420 e->value.constructor = ctor_head;
6de9cd9a
DN
2421
2422 *result = e;
2423 return MATCH_YES;
2424
2425syntax:
2426 gfc_error ("Syntax error in structure constructor at %C");
2427
2428cleanup:
fa9290d3
DK
2429 for (comp_iter = comp_head; comp_iter; )
2430 {
2431 gfc_structure_ctor_component *next = comp_iter->next;
2432 gfc_free_structure_ctor_component (comp_iter);
2433 comp_iter = next;
2434 }
b7e75771 2435 gfc_constructor_free (ctor_head);
6de9cd9a
DN
2436 return MATCH_ERROR;
2437}
2438
2439
9a3db5a3
PT
2440/* If the symbol is an implicit do loop index and implicitly typed,
2441 it should not be host associated. Provide a symtree from the
2442 current namespace. */
2443static match
2444check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
2445{
2446 if ((*sym)->attr.flavor == FL_VARIABLE
2447 && (*sym)->ns != gfc_current_ns
2448 && (*sym)->attr.implied_index
2449 && (*sym)->attr.implicit_type
2450 && !(*sym)->attr.use_assoc)
2451 {
2452 int i;
08a6b8e0 2453 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
9a3db5a3
PT
2454 if (i)
2455 return MATCH_ERROR;
2456 *sym = (*st)->n.sym;
2457 }
2458 return MATCH_YES;
2459}
2460
2461
3070bab4
JW
2462/* Procedure pointer as function result: Replace the function symbol by the
2463 auto-generated hidden result variable named "ppr@". */
2464
2465static gfc_try
2466replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
2467{
2468 /* Check for procedure pointer result variable. */
2469 if ((*sym)->attr.function && !(*sym)->attr.external
2470 && (*sym)->result && (*sym)->result != *sym
2471 && (*sym)->result->attr.proc_pointer
2472 && (*sym) == gfc_current_ns->proc_name
2473 && (*sym) == (*sym)->result->ns->proc_name
2474 && strcmp ("ppr@", (*sym)->result->name) == 0)
2475 {
2476 /* Automatic replacement with "hidden" result variable. */
2477 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
2478 *sym = (*sym)->result;
2479 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
2480 return SUCCESS;
2481 }
2482 return FAILURE;
2483}
2484
2485
6de9cd9a
DN
2486/* Matches a variable name followed by anything that might follow it--
2487 array reference, argument list of a function, etc. */
2488
2489match
edf1eac2 2490gfc_match_rvalue (gfc_expr **result)
6de9cd9a
DN
2491{
2492 gfc_actual_arglist *actual_arglist;
d3fcc995 2493 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
2494 gfc_state_data *st;
2495 gfc_symbol *sym;
2496 gfc_symtree *symtree;
d3fcc995 2497 locus where, old_loc;
6de9cd9a 2498 gfc_expr *e;
d3fcc995 2499 match m, m2;
6de9cd9a 2500 int i;
5270c302
AL
2501 gfc_typespec *ts;
2502 bool implicit_char;
a99288e5 2503 gfc_ref *ref;
6de9cd9a
DN
2504
2505 m = gfc_match_name (name);
2506 if (m != MATCH_YES)
2507 return m;
2508
2a6dcee5
TB
2509 if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2510 && !gfc_current_ns->has_import_set)
08a6b8e0 2511 i = gfc_get_sym_tree (name, NULL, &symtree, false);
6de9cd9a
DN
2512 else
2513 i = gfc_get_ha_sym_tree (name, &symtree);
2514
2515 if (i)
2516 return MATCH_ERROR;
2517
2518 sym = symtree->n.sym;
2519 e = NULL;
63645982 2520 where = gfc_current_locus;
6de9cd9a 2521
3070bab4
JW
2522 replace_hidden_procptr_result (&sym, &symtree);
2523
9a3db5a3
PT
2524 /* If this is an implicit do loop index and implicitly typed,
2525 it should not be host associated. */
2526 m = check_for_implicit_index (&symtree, &sym);
2527 if (m != MATCH_YES)
2528 return m;
2529
6de9cd9a 2530 gfc_set_sym_referenced (sym);
9a3db5a3 2531 sym->attr.implied_index = 0;
6de9cd9a 2532
0921bc44
JJ
2533 if (sym->attr.function && sym->result == sym)
2534 {
811849c0
PT
2535 /* See if this is a directly recursive function call. */
2536 gfc_gobble_whitespace ();
2537 if (sym->attr.recursive
8fc541d3 2538 && gfc_peek_ascii_char () == '('
fc2d8680
PT
2539 && gfc_current_ns->proc_name == sym
2540 && !sym->attr.dimension)
811849c0 2541 {
fc2d8680
PT
2542 gfc_error ("'%s' at %C is the name of a recursive function "
2543 "and so refers to the result variable. Use an "
2544 "explicit RESULT variable for direct recursion "
2545 "(12.5.2.1)", sym->name);
811849c0
PT
2546 return MATCH_ERROR;
2547 }
fc2d8680 2548
2d71b918 2549 if (gfc_is_function_return_value (sym, gfc_current_ns))
0921bc44
JJ
2550 goto variable;
2551
2552 if (sym->attr.entry
2553 && (sym->ns == gfc_current_ns
2554 || sym->ns == gfc_current_ns->parent))
2555 {
2556 gfc_entry_list *el = NULL;
2557
2558 for (el = sym->ns->entries; el; el = el->next)
2559 if (sym == el->sym)
2560 goto variable;
2561 }
2562 }
6de9cd9a 2563
8fb74da4
JW
2564 if (gfc_matching_procptr_assignment)
2565 goto procptr0;
2566
6de9cd9a
DN
2567 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2568 goto function0;
2569
2570 if (sym->attr.generic)
2571 goto generic_function;
2572
2573 switch (sym->attr.flavor)
2574 {
2575 case FL_VARIABLE:
2576 variable:
6de9cd9a
DN
2577 e = gfc_get_expr ();
2578
2579 e->expr_type = EXPR_VARIABLE;
2580 e->symtree = symtree;
2581
713485cc 2582 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
2583 break;
2584
2585 case FL_PARAMETER:
b7263e8f
EE
2586 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2587 end up here. Unfortunately, sym->value->expr_type is set to
2588 EXPR_CONSTANT, and so the if () branch would be followed without
2589 the !sym->as check. */
2590 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
6de9cd9a
DN
2591 e = gfc_copy_expr (sym->value);
2592 else
2593 {
2594 e = gfc_get_expr ();
2595 e->expr_type = EXPR_VARIABLE;
2596 }
2597
2598 e->symtree = symtree;
713485cc 2599 m = gfc_match_varspec (e, 0, false, true);
a99288e5
PT
2600
2601 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
2602 break;
2603
927171bf
PT
2604 /* Variable array references to derived type parameters cause
2605 all sorts of headaches in simplification. Treating such
2606 expressions as variable works just fine for all array
2607 references. */
2608 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
a99288e5
PT
2609 {
2610 for (ref = e->ref; ref; ref = ref->next)
2611 if (ref->type == REF_ARRAY)
2612 break;
2613
927171bf 2614 if (ref == NULL || ref->u.ar.type == AR_FULL)
a99288e5
PT
2615 break;
2616
2617 ref = e->ref;
2618 e->ref = NULL;
2619 gfc_free_expr (e);
2620 e = gfc_get_expr ();
2621 e->expr_type = EXPR_VARIABLE;
2622 e->symtree = symtree;
2623 e->ref = ref;
a99288e5
PT
2624 }
2625
6de9cd9a
DN
2626 break;
2627
2628 case FL_DERIVED:
2629 sym = gfc_use_derived (sym);
2630 if (sym == NULL)
2631 m = MATCH_ERROR;
2632 else
7d1f1e61 2633 m = gfc_match_structure_constructor (sym, &e, false);
6de9cd9a
DN
2634 break;
2635
2636 /* If we're here, then the name is known to be the name of a
2637 procedure, yet it is not sure to be the name of a function. */
2638 case FL_PROCEDURE:
8fb74da4
JW
2639
2640 /* Procedure Pointer Assignments. */
2641 procptr0:
2642 if (gfc_matching_procptr_assignment)
2643 {
2644 gfc_gobble_whitespace ();
e35bbb23 2645 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
8fb74da4
JW
2646 /* Parse functions returning a procptr. */
2647 goto function0;
2648
c3005b0f
DK
2649 if (gfc_is_intrinsic (sym, 0, gfc_current_locus)
2650 || gfc_is_intrinsic (sym, 1, gfc_current_locus))
8fb74da4
JW
2651 sym->attr.intrinsic = 1;
2652 e = gfc_get_expr ();
2653 e->expr_type = EXPR_VARIABLE;
2654 e->symtree = symtree;
713485cc 2655 m = gfc_match_varspec (e, 0, false, true);
8fb74da4
JW
2656 break;
2657 }
2658
6de9cd9a
DN
2659 if (sym->attr.subroutine)
2660 {
2661 gfc_error ("Unexpected use of subroutine name '%s' at %C",
2662 sym->name);
2663 m = MATCH_ERROR;
2664 break;
2665 }
2666
2667 /* At this point, the name has to be a non-statement function.
edf1eac2
SK
2668 If the name is the same as the current function being
2669 compiled, then we have a variable reference (to the function
2670 result) if the name is non-recursive. */
6de9cd9a
DN
2671
2672 st = gfc_enclosing_unit (NULL);
2673
2674 if (st != NULL && st->state == COMP_FUNCTION
2675 && st->sym == sym
2676 && !sym->attr.recursive)
2677 {
2678 e = gfc_get_expr ();
2679 e->symtree = symtree;
2680 e->expr_type = EXPR_VARIABLE;
2681
713485cc 2682 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
2683 break;
2684 }
2685
2686 /* Match a function reference. */
2687 function0:
2688 m = gfc_match_actual_arglist (0, &actual_arglist);
2689 if (m == MATCH_NO)
2690 {
2691 if (sym->attr.proc == PROC_ST_FUNCTION)
2692 gfc_error ("Statement function '%s' requires argument list at %C",
2693 sym->name);
2694 else
2695 gfc_error ("Function '%s' requires an argument list at %C",
2696 sym->name);
2697
2698 m = MATCH_ERROR;
2699 break;
2700 }
2701
2702 if (m != MATCH_YES)
2703 {
2704 m = MATCH_ERROR;
2705 break;
2706 }
2707
2708 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
2709 sym = symtree->n.sym;
2710
3070bab4
JW
2711 replace_hidden_procptr_result (&sym, &symtree);
2712
6de9cd9a
DN
2713 e = gfc_get_expr ();
2714 e->symtree = symtree;
2715 e->expr_type = EXPR_FUNCTION;
2716 e->value.function.actual = actual_arglist;
63645982 2717 e->where = gfc_current_locus;
6de9cd9a
DN
2718
2719 if (sym->as != NULL)
2720 e->rank = sym->as->rank;
2721
2722 if (!sym->attr.function
231b2fcc 2723 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2724 {
2725 m = MATCH_ERROR;
2726 break;
2727 }
2728
a8b3b0b6
CR
2729 /* Check here for the existence of at least one argument for the
2730 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
2731 argument(s) given will be checked in gfc_iso_c_func_interface,
2732 during resolution of the function call. */
2733 if (sym->attr.is_iso_c == 1
2734 && (sym->from_intmod == INTMOD_ISO_C_BINDING
2735 && (sym->intmod_sym_id == ISOCBINDING_LOC
2736 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2737 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2738 {
2739 /* make sure we were given a param */
2740 if (actual_arglist == NULL)
2741 {
2742 gfc_error ("Missing argument to '%s' at %C", sym->name);
2743 m = MATCH_ERROR;
2744 break;
2745 }
2746 }
2747
6de9cd9a
DN
2748 if (sym->result == NULL)
2749 sym->result = sym;
2750
2751 m = MATCH_YES;
2752 break;
2753
2754 case FL_UNKNOWN:
2755
2756 /* Special case for derived type variables that get their types
edf1eac2
SK
2757 via an IMPLICIT statement. This can't wait for the
2758 resolution phase. */
6de9cd9a 2759
8fc541d3 2760 if (gfc_peek_ascii_char () == '%'
0dd973dd 2761 && sym->ts.type == BT_UNKNOWN
713485cc 2762 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
6de9cd9a
DN
2763 gfc_set_default_type (sym, 0, sym->ns);
2764
2765 /* If the symbol has a dimension attribute, the expression is a
edf1eac2 2766 variable. */
6de9cd9a
DN
2767
2768 if (sym->attr.dimension)
2769 {
231b2fcc
TS
2770 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2771 sym->name, NULL) == FAILURE)
6de9cd9a
DN
2772 {
2773 m = MATCH_ERROR;
2774 break;
2775 }
2776
2777 e = gfc_get_expr ();
2778 e->symtree = symtree;
2779 e->expr_type = EXPR_VARIABLE;
713485cc 2780 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
2781 break;
2782 }
2783
2784 /* Name is not an array, so we peek to see if a '(' implies a
edf1eac2
SK
2785 function call or a substring reference. Otherwise the
2786 variable is just a scalar. */
6de9cd9a
DN
2787
2788 gfc_gobble_whitespace ();
8fc541d3 2789 if (gfc_peek_ascii_char () != '(')
6de9cd9a
DN
2790 {
2791 /* Assume a scalar variable */
2792 e = gfc_get_expr ();
2793 e->symtree = symtree;
2794 e->expr_type = EXPR_VARIABLE;
2795
231b2fcc
TS
2796 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2797 sym->name, NULL) == FAILURE)
6de9cd9a
DN
2798 {
2799 m = MATCH_ERROR;
2800 break;
2801 }
2802
8e1f752a 2803 /*FIXME:??? gfc_match_varspec does set this for us: */
6de9cd9a 2804 e->ts = sym->ts;
713485cc 2805 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
2806 break;
2807 }
2808
d3fcc995
TS
2809 /* See if this is a function reference with a keyword argument
2810 as first argument. We do this because otherwise a spurious
2811 symbol would end up in the symbol table. */
2812
2813 old_loc = gfc_current_locus;
2814 m2 = gfc_match (" ( %n =", argname);
2815 gfc_current_locus = old_loc;
6de9cd9a
DN
2816
2817 e = gfc_get_expr ();
2818 e->symtree = symtree;
2819
d3fcc995 2820 if (m2 != MATCH_YES)
6de9cd9a 2821 {
5270c302
AL
2822 /* Try to figure out whether we're dealing with a character type.
2823 We're peeking ahead here, because we don't want to call
2824 match_substring if we're dealing with an implicitly typed
2825 non-character variable. */
2826 implicit_char = false;
2827 if (sym->ts.type == BT_UNKNOWN)
2828 {
713485cc 2829 ts = gfc_get_default_type (sym->name, NULL);
5270c302
AL
2830 if (ts->type == BT_CHARACTER)
2831 implicit_char = true;
2832 }
2833
d3fcc995
TS
2834 /* See if this could possibly be a substring reference of a name
2835 that we're not sure is a variable yet. */
6de9cd9a 2836
5270c302 2837 if ((implicit_char || sym->ts.type == BT_CHARACTER)
bc21d315 2838 && match_substring (sym->ts.u.cl, 0, &e->ref) == MATCH_YES)
6de9cd9a 2839 {
6de9cd9a 2840
d3fcc995
TS
2841 e->expr_type = EXPR_VARIABLE;
2842
2843 if (sym->attr.flavor != FL_VARIABLE
231b2fcc
TS
2844 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2845 sym->name, NULL) == FAILURE)
d3fcc995
TS
2846 {
2847 m = MATCH_ERROR;
2848 break;
2849 }
2850
2851 if (sym->ts.type == BT_UNKNOWN
2852 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2853 {
2854 m = MATCH_ERROR;
2855 break;
2856 }
2857
2858 e->ts = sym->ts;
860c8f3b 2859 if (e->ref)
bc21d315 2860 e->ts.u.cl = NULL;
d3fcc995 2861 m = MATCH_YES;
6de9cd9a
DN
2862 break;
2863 }
6de9cd9a
DN
2864 }
2865
2866 /* Give up, assume we have a function. */
2867
08a6b8e0 2868 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
2869 sym = symtree->n.sym;
2870 e->expr_type = EXPR_FUNCTION;
2871
2872 if (!sym->attr.function
231b2fcc 2873 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2874 {
2875 m = MATCH_ERROR;
2876 break;
2877 }
2878
2879 sym->result = sym;
2880
2881 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2882 if (m == MATCH_NO)
2883 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2884
2885 if (m != MATCH_YES)
2886 {
2887 m = MATCH_ERROR;
2888 break;
2889 }
2890
2891 /* If our new function returns a character, array or structure
edf1eac2 2892 type, it might have subsequent references. */
6de9cd9a 2893
713485cc 2894 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
2895 if (m == MATCH_NO)
2896 m = MATCH_YES;
2897
2898 break;
2899
2900 generic_function:
08a6b8e0 2901 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
2902
2903 e = gfc_get_expr ();
2904 e->symtree = symtree;
2905 e->expr_type = EXPR_FUNCTION;
2906
2907 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2908 break;
2909
2910 default:
2911 gfc_error ("Symbol at %C is not appropriate for an expression");
2912 return MATCH_ERROR;
2913 }
2914
2915 if (m == MATCH_YES)
2916 {
2917 e->where = where;
2918 *result = e;
2919 }
2920 else
2921 gfc_free_expr (e);
2922
2923 return m;
2924}
2925
2926
df2fba9e 2927/* Match a variable, i.e. something that can be assigned to. This
6de9cd9a
DN
2928 starts as a symbol, can be a structure component or an array
2929 reference. It can be a function if the function doesn't have a
2930 separate RESULT variable. If the symbol has not been previously
30aabb86 2931 seen, we assume it is a variable.
6de9cd9a 2932
30aabb86
PT
2933 This function is called by two interface functions:
2934 gfc_match_variable, which has host_flag = 1, and
2935 gfc_match_equiv_variable, with host_flag = 0, to restrict the
2936 match of the symbol to the local scope. */
2937
2938static match
edf1eac2 2939match_variable (gfc_expr **result, int equiv_flag, int host_flag)
6de9cd9a
DN
2940{
2941 gfc_symbol *sym;
2942 gfc_symtree *st;
2943 gfc_expr *expr;
2944 locus where;
2945 match m;
2946
fd2aa7ad
PT
2947 /* Since nothing has any business being an lvalue in a module
2948 specification block, an interface block or a contains section,
2949 we force the changed_symbols mechanism to work by setting
2950 host_flag to 0. This prevents valid symbols that have the name
2951 of keywords, such as 'end', being turned into variables by
df2fba9e 2952 failed matching to assignments for, e.g., END INTERFACE. */
fd2aa7ad
PT
2953 if (gfc_current_state () == COMP_MODULE
2954 || gfc_current_state () == COMP_INTERFACE
2955 || gfc_current_state () == COMP_CONTAINS)
2956 host_flag = 0;
2957
618f4f46 2958 where = gfc_current_locus;
30aabb86 2959 m = gfc_match_sym_tree (&st, host_flag);
6de9cd9a
DN
2960 if (m != MATCH_YES)
2961 return m;
6de9cd9a
DN
2962
2963 sym = st->n.sym;
9a3db5a3
PT
2964
2965 /* If this is an implicit do loop index and implicitly typed,
2966 it should not be host associated. */
2967 m = check_for_implicit_index (&st, &sym);
2968 if (m != MATCH_YES)
2969 return m;
2970
2971 sym->attr.implied_index = 0;
2972
6de9cd9a
DN
2973 gfc_set_sym_referenced (sym);
2974 switch (sym->attr.flavor)
2975 {
2976 case FL_VARIABLE:
9aa433c2 2977 if (sym->attr.is_protected && sym->attr.use_assoc)
edf1eac2 2978 {
ee7e677f 2979 gfc_error ("Assigning to PROTECTED variable at %C");
edf1eac2
SK
2980 return MATCH_ERROR;
2981 }
6de9cd9a
DN
2982 break;
2983
2984 case FL_UNKNOWN:
d7e2fcd0
TB
2985 {
2986 sym_flavor flavor = FL_UNKNOWN;
2987
2988 gfc_gobble_whitespace ();
2989
2990 if (sym->attr.external || sym->attr.procedure
2991 || sym->attr.function || sym->attr.subroutine)
2992 flavor = FL_PROCEDURE;
b9332b09
PT
2993
2994 /* If it is not a procedure, is not typed and is host associated,
2995 we cannot give it a flavor yet. */
2996 else if (sym->ns == gfc_current_ns->parent
2997 && sym->ts.type == BT_UNKNOWN)
2998 break;
2999
3000 /* These are definitive indicators that this is a variable. */
8fc541d3 3001 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
d7e2fcd0
TB
3002 || sym->attr.pointer || sym->as != NULL)
3003 flavor = FL_VARIABLE;
3004
3005 if (flavor != FL_UNKNOWN
3006 && gfc_add_flavor (&sym->attr, flavor, sym->name, NULL) == FAILURE)
3007 return MATCH_ERROR;
3008 }
6de9cd9a
DN
3009 break;
3010
5056a350
SK
3011 case FL_PARAMETER:
3012 if (equiv_flag)
3013 gfc_error ("Named constant at %C in an EQUIVALENCE");
3014 else
3015 gfc_error ("Cannot assign to a named constant at %C");
3016 return MATCH_ERROR;
3017 break;
3018
6de9cd9a 3019 case FL_PROCEDURE:
01d2a7d7
DF
3020 /* Check for a nonrecursive function result variable. */
3021 if (sym->attr.function
3022 && !sym->attr.external
3023 && sym->result == sym
2d71b918 3024 && (gfc_is_function_return_value (sym, gfc_current_ns)
01d2a7d7
DF
3025 || (sym->attr.entry
3026 && sym->ns == gfc_current_ns)
3027 || (sym->attr.entry
3028 && sym->ns == gfc_current_ns->parent)))
6de9cd9a 3029 {
6de9cd9a
DN
3030 /* If a function result is a derived type, then the derived
3031 type may still have to be resolved. */
3032
3033 if (sym->ts.type == BT_DERIVED
bc21d315 3034 && gfc_use_derived (sym->ts.u.derived) == NULL)
6de9cd9a 3035 return MATCH_ERROR;
6de9cd9a
DN
3036 break;
3037 }
3038
3070bab4
JW
3039 if (sym->attr.proc_pointer
3040 || replace_hidden_procptr_result (&sym, &st) == SUCCESS)
8fb74da4
JW
3041 break;
3042
6de9cd9a
DN
3043 /* Fall through to error */
3044
3045 default:
d7e2fcd0 3046 gfc_error ("'%s' at %C is not a variable", sym->name);
6de9cd9a
DN
3047 return MATCH_ERROR;
3048 }
3049
0dd973dd
PB
3050 /* Special case for derived type variables that get their types
3051 via an IMPLICIT statement. This can't wait for the
3052 resolution phase. */
3053
3054 {
3055 gfc_namespace * implicit_ns;
3056
3057 if (gfc_current_ns->proc_name == sym)
3058 implicit_ns = gfc_current_ns;
3059 else
3060 implicit_ns = sym->ns;
3061
8fc541d3 3062 if (gfc_peek_ascii_char () == '%'
0dd973dd 3063 && sym->ts.type == BT_UNKNOWN
713485cc 3064 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
0dd973dd
PB
3065 gfc_set_default_type (sym, 0, implicit_ns);
3066 }
3067
6de9cd9a
DN
3068 expr = gfc_get_expr ();
3069
3070 expr->expr_type = EXPR_VARIABLE;
3071 expr->symtree = st;
3072 expr->ts = sym->ts;
3073 expr->where = where;
3074
3075 /* Now see if we have to do more. */
713485cc 3076 m = gfc_match_varspec (expr, equiv_flag, false, false);
6de9cd9a
DN
3077 if (m != MATCH_YES)
3078 {
3079 gfc_free_expr (expr);
3080 return m;
3081 }
3082
3083 *result = expr;
3084 return MATCH_YES;
3085}
30aabb86 3086
edf1eac2 3087
30aabb86 3088match
edf1eac2 3089gfc_match_variable (gfc_expr **result, int equiv_flag)
30aabb86
PT
3090{
3091 return match_variable (result, equiv_flag, 1);
3092}
3093
edf1eac2 3094
30aabb86 3095match
edf1eac2 3096gfc_match_equiv_variable (gfc_expr **result)
30aabb86
PT
3097{
3098 return match_variable (result, 1, 0);
3099}
3100