]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
re PR fortran/91552 (ICE with valid array constructor)
[thirdparty/gcc.git] / gcc / fortran / primary.c
CommitLineData
6de9cd9a 1/* Primary expression subroutines
a5544970 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Andy Vaught
4
d1d61a00 5This file is part of GCC.
6de9cd9a 6
d1d61a00
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
d1d61a00 10version.
6de9cd9a 11
d1d61a00
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a 20
6de9cd9a
DN
21#include "config.h"
22#include "system.h"
953bee7c 23#include "coretypes.h"
1916bcb5 24#include "options.h"
6de9cd9a
DN
25#include "gfortran.h"
26#include "arith.h"
27#include "match.h"
28#include "parse.h"
b7e75771 29#include "constructor.h"
6de9cd9a 30
837c4b78
JW
31int matching_actual_arglist = 0;
32
6de9cd9a
DN
33/* Matches a kind-parameter expression, which is either a named
34 symbolic constant or a nonnegative integer constant. If
bee64a2b
JW
35 successful, sets the kind value to the correct integer.
36 The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
37 symbol like e.g. 'c_int'. */
6de9cd9a
DN
38
39static match
bee64a2b 40match_kind_param (int *kind, int *is_iso_c)
6de9cd9a
DN
41{
42 char name[GFC_MAX_SYMBOL_LEN + 1];
43 gfc_symbol *sym;
6de9cd9a
DN
44 match m;
45
bee64a2b
JW
46 *is_iso_c = 0;
47
5cf54585 48 m = gfc_match_small_literal_int (kind, NULL);
6de9cd9a
DN
49 if (m != MATCH_NO)
50 return m;
51
52 m = gfc_match_name (name);
53 if (m != MATCH_YES)
54 return m;
55
56 if (gfc_find_symbol (name, NULL, 1, &sym))
57 return MATCH_ERROR;
58
59 if (sym == NULL)
60 return MATCH_NO;
61
fbe468a5
JW
62 *is_iso_c = sym->attr.is_iso_c;
63
6de9cd9a
DN
64 if (sym->attr.flavor != FL_PARAMETER)
65 return MATCH_NO;
66
1d8e1d5d
PT
67 if (sym->value == NULL)
68 return MATCH_NO;
69
51f03c6b 70 if (gfc_extract_int (sym->value, kind))
6de9cd9a
DN
71 return MATCH_NO;
72
a39fafac
FXC
73 gfc_set_sym_referenced (sym);
74
6de9cd9a
DN
75 if (*kind < 0)
76 return MATCH_NO;
77
78 return MATCH_YES;
79}
80
81
82/* Get a trailing kind-specification for non-character variables.
83 Returns:
bee64a2b
JW
84 * the integer kind value or
85 * -1 if an error was generated,
86 * -2 if no kind was found.
87 The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
88 symbol like e.g. 'c_int'. */
6de9cd9a
DN
89
90static int
bee64a2b 91get_kind (int *is_iso_c)
6de9cd9a
DN
92{
93 int kind;
94 match m;
95
bee64a2b
JW
96 *is_iso_c = 0;
97
6de9cd9a
DN
98 if (gfc_match_char ('_') != MATCH_YES)
99 return -2;
100
bee64a2b 101 m = match_kind_param (&kind, is_iso_c);
6de9cd9a
DN
102 if (m == MATCH_NO)
103 gfc_error ("Missing kind-parameter at %C");
104
105 return (m == MATCH_YES) ? kind : -1;
106}
107
108
109/* Given a character and a radix, see if the character is a valid
110 digit in that radix. */
111
8fc541d3
FXC
112int
113gfc_check_digit (char c, int radix)
6de9cd9a
DN
114{
115 int r;
116
117 switch (radix)
118 {
119 case 2:
120 r = ('0' <= c && c <= '1');
121 break;
122
123 case 8:
124 r = ('0' <= c && c <= '7');
125 break;
126
127 case 10:
128 r = ('0' <= c && c <= '9');
129 break;
130
131 case 16:
da8309c6 132 r = ISXDIGIT (c);
6de9cd9a
DN
133 break;
134
135 default:
8fc541d3 136 gfc_internal_error ("gfc_check_digit(): bad radix");
6de9cd9a
DN
137 }
138
139 return r;
140}
141
142
143/* Match the digit string part of an integer if signflag is not set,
bf1b77dd
PT
144 the signed digit string part if signflag is set. If the buffer
145 is NULL, we just count characters for the resolution pass. Returns
6de9cd9a
DN
146 the number of characters matched, -1 for no match. */
147
148static int
149match_digits (int signflag, int radix, char *buffer)
150{
151 locus old_loc;
8fc541d3
FXC
152 int length;
153 char c;
6de9cd9a
DN
154
155 length = 0;
8fc541d3 156 c = gfc_next_ascii_char ();
6de9cd9a
DN
157
158 if (signflag && (c == '+' || c == '-'))
159 {
160 if (buffer != NULL)
161 *buffer++ = c;
69029c61 162 gfc_gobble_whitespace ();
8fc541d3 163 c = gfc_next_ascii_char ();
6de9cd9a
DN
164 length++;
165 }
166
8fc541d3 167 if (!gfc_check_digit (c, radix))
6de9cd9a
DN
168 return -1;
169
170 length++;
171 if (buffer != NULL)
172 *buffer++ = c;
173
174 for (;;)
175 {
63645982 176 old_loc = gfc_current_locus;
8fc541d3 177 c = gfc_next_ascii_char ();
6de9cd9a 178
8fc541d3 179 if (!gfc_check_digit (c, radix))
6de9cd9a
DN
180 break;
181
182 if (buffer != NULL)
183 *buffer++ = c;
184 length++;
185 }
186
63645982 187 gfc_current_locus = old_loc;
6de9cd9a
DN
188
189 return length;
190}
191
8dc63166
SK
192/* Convert an integer string to an expression node. */
193
194static gfc_expr *
195convert_integer (const char *buffer, int kind, int radix, locus *where)
196{
197 gfc_expr *e;
198 const char *t;
199
200 e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 /* A leading plus is allowed, but not by mpz_set_str. */
202 if (buffer[0] == '+')
203 t = buffer + 1;
204 else
205 t = buffer;
206 mpz_set_str (e->value.integer, t, radix);
207
208 return e;
209}
210
211
212/* Convert a real string to an expression node. */
213
214static gfc_expr *
215convert_real (const char *buffer, int kind, locus *where)
216{
217 gfc_expr *e;
218
219 e = gfc_get_constant_expr (BT_REAL, kind, where);
220 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
221
222 return e;
223}
224
225
226/* Convert a pair of real, constant expression nodes to a single
227 complex expression node. */
228
229static gfc_expr *
230convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
231{
232 gfc_expr *e;
233
234 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
235 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
236 GFC_MPC_RND_MODE);
237
238 return e;
239}
240
6de9cd9a 241
bf1b77dd 242/* Match an integer (digit string and optional kind).
6de9cd9a
DN
243 A sign will be accepted if signflag is set. */
244
245static match
edf1eac2 246match_integer_constant (gfc_expr **result, int signflag)
6de9cd9a 247{
bee64a2b 248 int length, kind, is_iso_c;
6de9cd9a
DN
249 locus old_loc;
250 char *buffer;
251 gfc_expr *e;
252
63645982 253 old_loc = gfc_current_locus;
6de9cd9a
DN
254 gfc_gobble_whitespace ();
255
256 length = match_digits (signflag, 10, NULL);
63645982 257 gfc_current_locus = old_loc;
6de9cd9a
DN
258 if (length == -1)
259 return MATCH_NO;
260
ece3f663 261 buffer = (char *) alloca (length + 1);
6de9cd9a
DN
262 memset (buffer, '\0', length + 1);
263
264 gfc_gobble_whitespace ();
265
266 match_digits (signflag, 10, buffer);
267
bee64a2b 268 kind = get_kind (&is_iso_c);
6de9cd9a 269 if (kind == -2)
9d64df18 270 kind = gfc_default_integer_kind;
6de9cd9a
DN
271 if (kind == -1)
272 return MATCH_ERROR;
273
203c7ebf 274 if (kind == 4 && flag_integer4_kind == 8)
f4347334
ZG
275 kind = 8;
276
e7a2d5fb 277 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
6de9cd9a
DN
278 {
279 gfc_error ("Integer kind %d at %C not available", kind);
280 return MATCH_ERROR;
281 }
282
8dc63166 283 e = convert_integer (buffer, kind, 10, &gfc_current_locus);
bee64a2b 284 e->ts.is_c_interop = is_iso_c;
6de9cd9a
DN
285
286 if (gfc_range_check (e) != ARITH_OK)
287 {
b2458f91 288 gfc_error ("Integer too big for its kind at %C. This check can be "
a3f9f006 289 "disabled with the option %<-fno-range-check%>");
6de9cd9a
DN
290
291 gfc_free_expr (e);
292 return MATCH_ERROR;
293 }
294
295 *result = e;
296 return MATCH_YES;
297}
298
299
d3642f89
FW
300/* Match a Hollerith constant. */
301
302static match
edf1eac2 303match_hollerith_constant (gfc_expr **result)
d3642f89
FW
304{
305 locus old_loc;
edf1eac2 306 gfc_expr *e = NULL;
401fcd3b 307 int num, pad;
bf1b77dd 308 int i;
d3642f89
FW
309
310 old_loc = gfc_current_locus;
311 gfc_gobble_whitespace ();
312
313 if (match_integer_constant (&e, 0) == MATCH_YES
edf1eac2 314 && gfc_match_char ('h') == MATCH_YES)
d3642f89 315 {
524af0d6 316 if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
d3642f89
FW
317 goto cleanup;
318
51f03c6b
JJ
319 if (gfc_extract_int (e, &num, 1))
320 goto cleanup;
d3642f89
FW
321 if (num == 0)
322 {
edf1eac2
SK
323 gfc_error ("Invalid Hollerith constant: %L must contain at least "
324 "one character", &old_loc);
d3642f89
FW
325 goto cleanup;
326 }
327 if (e->ts.kind != gfc_default_integer_kind)
328 {
eb6d74fa 329 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
edf1eac2 330 "should be default", &old_loc);
d3642f89
FW
331 goto cleanup;
332 }
333 else
334 {
d3642f89 335 gfc_free_expr (e);
b7e75771
JD
336 e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
337 &gfc_current_locus);
20585ad6 338
401fcd3b
JD
339 /* Calculate padding needed to fit default integer memory. */
340 pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
341
342 e->representation.string = XCNEWVEC (char, num + pad + 1);
8fc541d3 343
20585ad6 344 for (i = 0; i < num; i++)
00660189 345 {
696abb30 346 gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
00660189
FXC
347 if (! gfc_wide_fits_in_byte (c))
348 {
349 gfc_error ("Invalid Hollerith constant at %L contains a "
350 "wide character", &old_loc);
351 goto cleanup;
352 }
353
354 e->representation.string[i] = (unsigned char) c;
355 }
8fc541d3 356
401fcd3b
JD
357 /* Now pad with blanks and end with a null char. */
358 for (i = 0; i < pad; i++)
359 e->representation.string[num + i] = ' ';
360
361 e->representation.string[num + i] = '\0';
362 e->representation.length = num + pad;
363 e->ts.u.pad = pad;
20585ad6 364
d3642f89
FW
365 *result = e;
366 return MATCH_YES;
367 }
368 }
369
370 gfc_free_expr (e);
371 gfc_current_locus = old_loc;
372 return MATCH_NO;
373
374cleanup:
375 gfc_free_expr (e);
376 return MATCH_ERROR;
377}
378
379
6de9cd9a 380/* Match a binary, octal or hexadecimal constant that can be found in
78019d16
SK
381 a DATA statement. The standard permits b'010...', o'73...', and
382 z'a1...' where b, o, and z can be capital letters. This function
383 also accepts postfixed forms of the constants: '01...'b, '73...'o,
384 and 'a1...'z. An additional extension is the use of x for z. */
6de9cd9a
DN
385
386static match
edf1eac2 387match_boz_constant (gfc_expr **result)
6de9cd9a 388{
8dc63166 389 int radix, length, x_hex;
78019d16 390 locus old_loc, start_loc;
8fc541d3 391 char *buffer, post, delim;
6de9cd9a 392 gfc_expr *e;
6de9cd9a 393
78019d16 394 start_loc = old_loc = gfc_current_locus;
6de9cd9a
DN
395 gfc_gobble_whitespace ();
396
88199e7c 397 x_hex = 0;
8fc541d3 398 switch (post = gfc_next_ascii_char ())
6de9cd9a
DN
399 {
400 case 'b':
401 radix = 2;
78019d16 402 post = 0;
6de9cd9a
DN
403 break;
404 case 'o':
405 radix = 8;
78019d16 406 post = 0;
6de9cd9a
DN
407 break;
408 case 'x':
88199e7c 409 x_hex = 1;
6de9cd9a
DN
410 /* Fall through. */
411 case 'z':
412 radix = 16;
78019d16
SK
413 post = 0;
414 break;
415 case '\'':
416 /* Fall through. */
417 case '\"':
418 delim = post;
419 post = 1;
420 radix = 16; /* Set to accept any valid digit string. */
6de9cd9a
DN
421 break;
422 default:
423 goto backup;
424 }
425
426 /* No whitespace allowed here. */
427
78019d16 428 if (post == 0)
8fc541d3 429 delim = gfc_next_ascii_char ();
78019d16 430
6de9cd9a
DN
431 if (delim != '\'' && delim != '\"')
432 goto backup;
433
00a4618b 434 if (x_hex
8dc63166
SK
435 && gfc_invalid_boz ("Hexadecimal constant at %L uses "
436 "nonstandard syntax", &gfc_current_locus))
437 return MATCH_ERROR;
5d874166 438
63645982 439 old_loc = gfc_current_locus;
6de9cd9a
DN
440
441 length = match_digits (0, radix, NULL);
442 if (length == -1)
443 {
78019d16 444 gfc_error ("Empty set of digits in BOZ constant at %C");
6de9cd9a
DN
445 return MATCH_ERROR;
446 }
447
8fc541d3 448 if (gfc_next_ascii_char () != delim)
6de9cd9a 449 {
78019d16
SK
450 gfc_error ("Illegal character in BOZ constant at %C");
451 return MATCH_ERROR;
452 }
453
454 if (post == 1)
455 {
8fc541d3 456 switch (gfc_next_ascii_char ())
78019d16
SK
457 {
458 case 'b':
459 radix = 2;
21dac32c 460 break;
78019d16
SK
461 case 'o':
462 radix = 8;
21dac32c 463 break;
78019d16
SK
464 case 'x':
465 /* Fall through. */
466 case 'z':
467 radix = 16;
21dac32c 468 break;
31043f6c 469 default:
78019d16 470 goto backup;
31043f6c 471 }
0bf0efd5 472
8dc63166
SK
473 if (gfc_invalid_boz ("BOZ constant at %C uses nonstandard postfix "
474 "syntax", &gfc_current_locus))
0bf0efd5 475 return MATCH_ERROR;
6de9cd9a
DN
476 }
477
63645982 478 gfc_current_locus = old_loc;
6de9cd9a 479
ece3f663 480 buffer = (char *) alloca (length + 1);
6de9cd9a
DN
481 memset (buffer, '\0', length + 1);
482
483 match_digits (0, radix, buffer);
8fc541d3 484 gfc_next_ascii_char (); /* Eat delimiter. */
78019d16 485 if (post == 1)
8fc541d3 486 gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
6de9cd9a 487
8dc63166
SK
488 e = gfc_get_expr ();
489 e->expr_type = EXPR_CONSTANT;
490 e->ts.type = BT_BOZ;
491 e->where = gfc_current_locus;
492 e->boz.rdx = radix;
493 e->boz.len = length;
494 e->boz.str = XCNEWVEC (char, length + 1);
495 strncpy (e->boz.str, buffer, length);
88199e7c 496
0bf0efd5 497 if (!gfc_in_match_data ()
524af0d6 498 && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
8dc63166
SK
499 "statement at %L", &e->where)))
500 return MATCH_ERROR;
0bf0efd5 501
6de9cd9a
DN
502 *result = e;
503 return MATCH_YES;
504
505backup:
78019d16 506 gfc_current_locus = start_loc;
6de9cd9a
DN
507 return MATCH_NO;
508}
509
510
69029c61 511/* Match a real constant of some sort. Allow a signed constant if signflag
00a4618b 512 is nonzero. */
6de9cd9a
DN
513
514static match
edf1eac2 515match_real_constant (gfc_expr **result, int signflag)
6de9cd9a 516{
6869e9c6 517 int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
6de9cd9a 518 locus old_loc, temp_loc;
8fc541d3 519 char *p, *buffer, c, exp_char;
6de9cd9a 520 gfc_expr *e;
69029c61 521 bool negate;
6de9cd9a 522
63645982 523 old_loc = gfc_current_locus;
6de9cd9a
DN
524 gfc_gobble_whitespace ();
525
526 e = NULL;
527
6869e9c6 528 default_exponent = 0;
6de9cd9a
DN
529 count = 0;
530 seen_dp = 0;
531 seen_digits = 0;
532 exp_char = ' ';
69029c61 533 negate = FALSE;
6de9cd9a 534
8fc541d3 535 c = gfc_next_ascii_char ();
6de9cd9a
DN
536 if (signflag && (c == '+' || c == '-'))
537 {
69029c61
PB
538 if (c == '-')
539 negate = TRUE;
540
541 gfc_gobble_whitespace ();
8fc541d3 542 c = gfc_next_ascii_char ();
6de9cd9a
DN
543 }
544
02712c16 545 /* Scan significand. */
8fc541d3 546 for (;; c = gfc_next_ascii_char (), count++)
6de9cd9a
DN
547 {
548 if (c == '.')
549 {
550 if (seen_dp)
551 goto done;
552
bf1b77dd 553 /* Check to see if "." goes with a following operator like
edf1eac2 554 ".eq.". */
63645982 555 temp_loc = gfc_current_locus;
8fc541d3 556 c = gfc_next_ascii_char ();
6de9cd9a
DN
557
558 if (c == 'e' || c == 'd' || c == 'q')
559 {
8fc541d3 560 c = gfc_next_ascii_char ();
6de9cd9a 561 if (c == '.')
f7b529fa 562 goto done; /* Operator named .e. or .d. */
6de9cd9a
DN
563 }
564
565 if (ISALPHA (c))
566 goto done; /* Distinguish 1.e9 from 1.eq.2 */
567
63645982 568 gfc_current_locus = temp_loc;
6de9cd9a
DN
569 seen_dp = 1;
570 continue;
571 }
572
573 if (ISDIGIT (c))
574 {
575 seen_digits = 1;
576 continue;
577 }
578
579 break;
580 }
581
edf1eac2 582 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
6de9cd9a
DN
583 goto done;
584 exp_char = c;
585
5a17346a
SK
586
587 if (c == 'q')
588 {
524af0d6
JB
589 if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter 'q' in "
590 "real-literal-constant at %C"))
5a17346a 591 return MATCH_ERROR;
73e42eef 592 else if (warn_real_q_constant)
48749dbc
MLI
593 gfc_warning (OPT_Wreal_q_constant,
594 "Extension: exponent-letter %<q%> in real-literal-constant "
595 "at %C");
5a17346a
SK
596 }
597
6de9cd9a 598 /* Scan exponent. */
8fc541d3 599 c = gfc_next_ascii_char ();
6de9cd9a
DN
600 count++;
601
602 if (c == '+' || c == '-')
603 { /* optional sign */
8fc541d3 604 c = gfc_next_ascii_char ();
6de9cd9a
DN
605 count++;
606 }
607
608 if (!ISDIGIT (c))
609 {
6869e9c6
FR
610 /* With -fdec, default exponent to 0 instead of complaining. */
611 if (flag_dec)
612 default_exponent = 1;
613 else
614 {
615 gfc_error ("Missing exponent in real number at %C");
616 return MATCH_ERROR;
617 }
6de9cd9a
DN
618 }
619
620 while (ISDIGIT (c))
621 {
8fc541d3 622 c = gfc_next_ascii_char ();
6de9cd9a
DN
623 count++;
624 }
625
626done:
69029c61 627 /* Check that we have a numeric constant. */
6de9cd9a
DN
628 if (!seen_digits || (!seen_dp && exp_char == ' '))
629 {
63645982 630 gfc_current_locus = old_loc;
6de9cd9a
DN
631 return MATCH_NO;
632 }
633
634 /* Convert the number. */
63645982 635 gfc_current_locus = old_loc;
6de9cd9a
DN
636 gfc_gobble_whitespace ();
637
6869e9c6
FR
638 buffer = (char *) alloca (count + default_exponent + 1);
639 memset (buffer, '\0', count + default_exponent + 1);
6de9cd9a 640
6de9cd9a 641 p = buffer;
8fc541d3 642 c = gfc_next_ascii_char ();
69029c61 643 if (c == '+' || c == '-')
6de9cd9a 644 {
69029c61 645 gfc_gobble_whitespace ();
8fc541d3 646 c = gfc_next_ascii_char ();
69029c61
PB
647 }
648
649 /* Hack for mpfr_set_str(). */
650 for (;;)
651 {
652 if (c == 'd' || c == 'q')
6de9cd9a 653 *p = 'e';
69029c61
PB
654 else
655 *p = c;
6de9cd9a 656 p++;
69029c61
PB
657 if (--count == 0)
658 break;
659
8fc541d3 660 c = gfc_next_ascii_char ();
6de9cd9a 661 }
6869e9c6
FR
662 if (default_exponent)
663 *p++ = '0';
6de9cd9a 664
bee64a2b 665 kind = get_kind (&is_iso_c);
6de9cd9a
DN
666 if (kind == -1)
667 goto cleanup;
668
669 switch (exp_char)
670 {
671 case 'd':
672 if (kind != -2)
673 {
a4d9b221 674 gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
edf1eac2 675 "kind");
6de9cd9a
DN
676 goto cleanup;
677 }
9d64df18 678 kind = gfc_default_double_kind;
f4347334
ZG
679
680 if (kind == 4)
681 {
203c7ebf 682 if (flag_real4_kind == 8)
f4347334 683 kind = 8;
203c7ebf 684 if (flag_real4_kind == 10)
f4347334 685 kind = 10;
203c7ebf 686 if (flag_real4_kind == 16)
f4347334
ZG
687 kind = 16;
688 }
689
690 if (kind == 8)
691 {
203c7ebf 692 if (flag_real8_kind == 4)
f4347334 693 kind = 4;
203c7ebf 694 if (flag_real8_kind == 10)
f4347334 695 kind = 10;
203c7ebf 696 if (flag_real8_kind == 16)
f4347334
ZG
697 kind = 16;
698 }
6de9cd9a
DN
699 break;
700
5a17346a
SK
701 case 'q':
702 if (kind != -2)
703 {
a4d9b221 704 gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
5a17346a
SK
705 "kind");
706 goto cleanup;
707 }
708
709 /* The maximum possible real kind type parameter is 16. First, try
710 that for the kind, then fallback to trying kind=10 (Intel 80 bit)
711 extended precision. If neither value works, just given up. */
712 kind = 16;
713 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
714 {
715 kind = 10;
716 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
717 {
a4d9b221 718 gfc_error ("Invalid exponent-letter %<q%> in "
5a17346a
SK
719 "real-literal-constant at %C");
720 goto cleanup;
721 }
722 }
723 break;
724
6de9cd9a
DN
725 default:
726 if (kind == -2)
9d64df18 727 kind = gfc_default_real_kind;
6de9cd9a 728
f4347334
ZG
729 if (kind == 4)
730 {
203c7ebf 731 if (flag_real4_kind == 8)
f4347334 732 kind = 8;
203c7ebf 733 if (flag_real4_kind == 10)
f4347334 734 kind = 10;
203c7ebf 735 if (flag_real4_kind == 16)
f4347334
ZG
736 kind = 16;
737 }
738
739 if (kind == 8)
740 {
203c7ebf 741 if (flag_real8_kind == 4)
f4347334 742 kind = 4;
203c7ebf 743 if (flag_real8_kind == 10)
f4347334 744 kind = 10;
203c7ebf 745 if (flag_real8_kind == 16)
f4347334
ZG
746 kind = 16;
747 }
748
e7a2d5fb 749 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
6de9cd9a
DN
750 {
751 gfc_error ("Invalid real kind %d at %C", kind);
752 goto cleanup;
753 }
754 }
755
8dc63166 756 e = convert_real (buffer, kind, &gfc_current_locus);
69029c61
PB
757 if (negate)
758 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
bee64a2b 759 e->ts.is_c_interop = is_iso_c;
6de9cd9a
DN
760
761 switch (gfc_range_check (e))
762 {
763 case ARITH_OK:
764 break;
765 case ARITH_OVERFLOW:
766 gfc_error ("Real constant overflows its kind at %C");
767 goto cleanup;
768
769 case ARITH_UNDERFLOW:
73e42eef 770 if (warn_underflow)
48749dbc 771 gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
f8e566e5 772 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
2d8b59df 773 break;
6de9cd9a
DN
774
775 default:
776 gfc_internal_error ("gfc_range_check() returned bad value");
777 }
778
cbf560d7
TK
779 /* Warn about trailing digits which suggest the user added too many
780 trailing digits, which may cause the appearance of higher pecision
781 than the kind kan support.
782
783 This is done by replacing the rightmost non-zero digit with zero
784 and comparing with the original value. If these are equal, we
785 assume the user supplied more digits than intended (or forgot to
786 convert to the correct kind).
787 */
788
789 if (warn_conversion_extra)
790 {
791 mpfr_t r;
792 char *c, *p;
793 bool did_break;
794
795 c = strchr (buffer, 'e');
796 if (c == NULL)
797 c = buffer + strlen(buffer);
798
799 did_break = false;
800 for (p = c - 1; p >= buffer; p--)
801 {
802 if (*p == '.')
803 continue;
38217d3e 804
cbf560d7
TK
805 if (*p != '0')
806 {
807 *p = '0';
808 did_break = true;
809 break;
810 }
811 }
812
813 if (did_break)
814 {
815 mpfr_init (r);
816 mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
817 if (negate)
818 mpfr_neg (r, r, GFC_RND_MODE);
819
820 mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
821
822 if (mpfr_cmp_ui (r, 0) == 0)
823 gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
824 "in %qs number at %C, maybe incorrect KIND",
825 gfc_typename (&e->ts));
826
827 mpfr_clear (r);
828 }
829 }
830
6de9cd9a
DN
831 *result = e;
832 return MATCH_YES;
833
834cleanup:
835 gfc_free_expr (e);
836 return MATCH_ERROR;
837}
838
839
840/* Match a substring reference. */
841
842static match
38217d3e 843match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
6de9cd9a
DN
844{
845 gfc_expr *start, *end;
846 locus old_loc;
847 gfc_ref *ref;
848 match m;
849
850 start = NULL;
851 end = NULL;
852
63645982 853 old_loc = gfc_current_locus;
6de9cd9a
DN
854
855 m = gfc_match_char ('(');
856 if (m != MATCH_YES)
857 return MATCH_NO;
858
859 if (gfc_match_char (':') != MATCH_YES)
860 {
861 if (init)
862 m = gfc_match_init_expr (&start);
863 else
864 m = gfc_match_expr (&start);
865
866 if (m != MATCH_YES)
867 {
868 m = MATCH_NO;
869 goto cleanup;
870 }
871
872 m = gfc_match_char (':');
873 if (m != MATCH_YES)
874 goto cleanup;
875 }
876
877 if (gfc_match_char (')') != MATCH_YES)
878 {
879 if (init)
880 m = gfc_match_init_expr (&end);
881 else
882 m = gfc_match_expr (&end);
883
884 if (m == MATCH_NO)
885 goto syntax;
886 if (m == MATCH_ERROR)
887 goto cleanup;
888
889 m = gfc_match_char (')');
890 if (m == MATCH_NO)
891 goto syntax;
892 }
893
894 /* Optimize away the (:) reference. */
38217d3e 895 if (start == NULL && end == NULL && !deferred)
6de9cd9a
DN
896 ref = NULL;
897 else
898 {
899 ref = gfc_get_ref ();
900
901 ref->type = REF_SUBSTRING;
902 if (start == NULL)
f622221a 903 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
6de9cd9a
DN
904 ref->u.ss.start = start;
905 if (end == NULL && cl)
906 end = gfc_copy_expr (cl->length);
907 ref->u.ss.end = end;
908 ref->u.ss.length = cl;
909 }
910
911 *result = ref;
912 return MATCH_YES;
913
914syntax:
915 gfc_error ("Syntax error in SUBSTRING specification at %C");
916 m = MATCH_ERROR;
917
918cleanup:
919 gfc_free_expr (start);
920 gfc_free_expr (end);
921
63645982 922 gfc_current_locus = old_loc;
6de9cd9a
DN
923 return m;
924}
925
926
927/* Reads the next character of a string constant, taking care to
928 return doubled delimiters on the input as a single instance of
929 the delimiter.
930
8fc541d3 931 Special return values for "ret" argument are:
6de9cd9a
DN
932 -1 End of the string, as determined by the delimiter
933 -2 Unterminated string detected
934
935 Backslash codes are also expanded at this time. */
936
8fc541d3
FXC
937static gfc_char_t
938next_string_char (gfc_char_t delimiter, int *ret)
6de9cd9a
DN
939{
940 locus old_locus;
8fc541d3 941 gfc_char_t c;
6de9cd9a 942
696abb30 943 c = gfc_next_char_literal (INSTRING_WARN);
8fc541d3 944 *ret = 0;
6de9cd9a
DN
945
946 if (c == '\n')
8fc541d3
FXC
947 {
948 *ret = -2;
949 return 0;
950 }
6de9cd9a 951
c61819ff 952 if (flag_backslash && c == '\\')
6de9cd9a 953 {
63645982 954 old_locus = gfc_current_locus;
6de9cd9a 955
a88a266c
SK
956 if (gfc_match_special_char (&c) == MATCH_NO)
957 gfc_current_locus = old_locus;
2e6a83a7
SK
958
959 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
db30e21c 960 gfc_warning (0, "Extension: backslash character at %C");
6de9cd9a
DN
961 }
962
963 if (c != delimiter)
964 return c;
965
63645982 966 old_locus = gfc_current_locus;
696abb30 967 c = gfc_next_char_literal (NONSTRING);
6de9cd9a
DN
968
969 if (c == delimiter)
970 return c;
63645982 971 gfc_current_locus = old_locus;
6de9cd9a 972
8fc541d3
FXC
973 *ret = -1;
974 return 0;
6de9cd9a
DN
975}
976
977
978/* Special case of gfc_match_name() that matches a parameter kind name
979 before a string constant. This takes case of the weird but legal
4f8ea09e 980 case of:
6de9cd9a
DN
981
982 kind_____'string'
983
984 where kind____ is a parameter. gfc_match_name() will happily slurp
985 up all the underscores, which leads to problems. If we return
986 MATCH_YES, the parse pointer points to the final underscore, which
987 is not part of the name. We never return MATCH_ERROR-- errors in
988 the name will be detected later. */
989
990static match
991match_charkind_name (char *name)
992{
993 locus old_loc;
994 char c, peek;
995 int len;
996
997 gfc_gobble_whitespace ();
8fc541d3 998 c = gfc_next_ascii_char ();
6de9cd9a
DN
999 if (!ISALPHA (c))
1000 return MATCH_NO;
1001
1002 *name++ = c;
1003 len = 1;
1004
1005 for (;;)
1006 {
63645982 1007 old_loc = gfc_current_locus;
8fc541d3 1008 c = gfc_next_ascii_char ();
6de9cd9a
DN
1009
1010 if (c == '_')
1011 {
8fc541d3 1012 peek = gfc_peek_ascii_char ();
6de9cd9a
DN
1013
1014 if (peek == '\'' || peek == '\"')
1015 {
63645982 1016 gfc_current_locus = old_loc;
6de9cd9a
DN
1017 *name = '\0';
1018 return MATCH_YES;
1019 }
1020 }
1021
1022 if (!ISALNUM (c)
1023 && c != '_'
c61819ff 1024 && (c != '$' || !flag_dollar_ok))
6de9cd9a
DN
1025 break;
1026
1027 *name++ = c;
1028 if (++len > GFC_MAX_SYMBOL_LEN)
1029 break;
1030 }
1031
1032 return MATCH_NO;
1033}
1034
1035
1036/* See if the current input matches a character constant. Lots of
1037 contortions have to be done to match the kind parameter which comes
1038 before the actual string. The main consideration is that we don't
1039 want to error out too quickly. For example, we don't actually do
1040 any validation of the kinds until we have actually seen a legal
1041 delimiter. Using match_kind_param() generates errors too quickly. */
1042
1043static match
edf1eac2 1044match_string_constant (gfc_expr **result)
6de9cd9a 1045{
00660189 1046 char name[GFC_MAX_SYMBOL_LEN + 1], peek;
6b271a2e
JB
1047 size_t length;
1048 int kind,save_warn_ampersand, ret;
6de9cd9a
DN
1049 locus old_locus, start_locus;
1050 gfc_symbol *sym;
1051 gfc_expr *e;
6de9cd9a 1052 match m;
00660189 1053 gfc_char_t c, delimiter, *p;
6de9cd9a 1054
63645982 1055 old_locus = gfc_current_locus;
6de9cd9a
DN
1056
1057 gfc_gobble_whitespace ();
1058
6de9cd9a
DN
1059 c = gfc_next_char ();
1060 if (c == '\'' || c == '"')
1061 {
9d64df18 1062 kind = gfc_default_character_kind;
66faed76 1063 start_locus = gfc_current_locus;
6de9cd9a
DN
1064 goto got_delim;
1065 }
1066
8fc541d3 1067 if (gfc_wide_is_digit (c))
6de9cd9a
DN
1068 {
1069 kind = 0;
1070
8fc541d3 1071 while (gfc_wide_is_digit (c))
6de9cd9a
DN
1072 {
1073 kind = kind * 10 + c - '0';
1074 if (kind > 9999999)
1075 goto no_match;
1076 c = gfc_next_char ();
1077 }
1078
1079 }
1080 else
1081 {
63645982 1082 gfc_current_locus = old_locus;
6de9cd9a
DN
1083
1084 m = match_charkind_name (name);
1085 if (m != MATCH_YES)
1086 goto no_match;
1087
1088 if (gfc_find_symbol (name, NULL, 1, &sym)
1089 || sym == NULL
1090 || sym->attr.flavor != FL_PARAMETER)
1091 goto no_match;
1092
1093 kind = -1;
1094 c = gfc_next_char ();
1095 }
1096
1097 if (c == ' ')
1098 {
1099 gfc_gobble_whitespace ();
1100 c = gfc_next_char ();
1101 }
1102
1103 if (c != '_')
1104 goto no_match;
1105
1106 gfc_gobble_whitespace ();
6de9cd9a
DN
1107
1108 c = gfc_next_char ();
1109 if (c != '\'' && c != '"')
1110 goto no_match;
1111
66faed76
DF
1112 start_locus = gfc_current_locus;
1113
6de9cd9a
DN
1114 if (kind == -1)
1115 {
51f03c6b
JJ
1116 if (gfc_extract_int (sym->value, &kind, 1))
1117 return MATCH_ERROR;
a39fafac 1118 gfc_set_sym_referenced (sym);
6de9cd9a
DN
1119 }
1120
e7a2d5fb 1121 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1122 {
1123 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
1124 return MATCH_ERROR;
1125 }
1126
1127got_delim:
1128 /* Scan the string into a block of memory by first figuring out how
1129 long it is, allocating the structure, then re-reading it. This
1130 isn't particularly efficient, but string constants aren't that
1131 common in most code. TODO: Use obstacks? */
1132
1133 delimiter = c;
1134 length = 0;
1135
1136 for (;;)
1137 {
8fc541d3
FXC
1138 c = next_string_char (delimiter, &ret);
1139 if (ret == -1)
6de9cd9a 1140 break;
8fc541d3 1141 if (ret == -2)
6de9cd9a 1142 {
63645982 1143 gfc_current_locus = start_locus;
6de9cd9a
DN
1144 gfc_error ("Unterminated character constant beginning at %C");
1145 return MATCH_ERROR;
1146 }
1147
1148 length++;
1149 }
1150
78019d16
SK
1151 /* Peek at the next character to see if it is a b, o, z, or x for the
1152 postfixed BOZ literal constants. */
8fc541d3
FXC
1153 peek = gfc_peek_ascii_char ();
1154 if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
78019d16
SK
1155 goto no_match;
1156
b7e75771 1157 e = gfc_get_character_expr (kind, &start_locus, NULL, length);
6de9cd9a 1158
63645982 1159 gfc_current_locus = start_locus;
6de9cd9a 1160
1355d8e7
TB
1161 /* We disable the warning for the following loop as the warning has already
1162 been printed in the loop above. */
73e42eef 1163 save_warn_ampersand = warn_ampersand;
48749dbc 1164 warn_ampersand = false;
1355d8e7 1165
b7e75771 1166 p = e->value.character.string;
6b271a2e 1167 for (size_t i = 0; i < length; i++)
8fc541d3
FXC
1168 {
1169 c = next_string_char (delimiter, &ret);
1170
d393bbd7 1171 if (!gfc_check_character_range (c, kind))
8fc541d3 1172 {
efb63364 1173 gfc_free_expr (e);
a4d9b221 1174 gfc_error ("Character %qs in string at %C is not representable "
d393bbd7 1175 "in character kind %d", gfc_print_wide_char (c), kind);
8fc541d3
FXC
1176 return MATCH_ERROR;
1177 }
1178
00660189 1179 *p++ = c;
8fc541d3 1180 }
6de9cd9a
DN
1181
1182 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
73e42eef 1183 warn_ampersand = save_warn_ampersand;
6de9cd9a 1184
8fc541d3
FXC
1185 next_string_char (delimiter, &ret);
1186 if (ret != -1)
6de9cd9a
DN
1187 gfc_internal_error ("match_string_constant(): Delimiter not found");
1188
38217d3e 1189 if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
6de9cd9a
DN
1190 e->expr_type = EXPR_SUBSTRING;
1191
1192 *result = e;
1193
1194 return MATCH_YES;
1195
1196no_match:
63645982 1197 gfc_current_locus = old_locus;
6de9cd9a
DN
1198 return MATCH_NO;
1199}
1200
1201
500f8f7b
RS
1202/* Match a .true. or .false. Returns 1 if a .true. was found,
1203 0 if a .false. was found, and -1 otherwise. */
1204static int
1205match_logical_constant_string (void)
1206{
1207 locus orig_loc = gfc_current_locus;
1208
1209 gfc_gobble_whitespace ();
8fc541d3 1210 if (gfc_next_ascii_char () == '.')
500f8f7b 1211 {
8fc541d3 1212 char ch = gfc_next_ascii_char ();
500f8f7b
RS
1213 if (ch == 'f')
1214 {
8fc541d3
FXC
1215 if (gfc_next_ascii_char () == 'a'
1216 && gfc_next_ascii_char () == 'l'
1217 && gfc_next_ascii_char () == 's'
1218 && gfc_next_ascii_char () == 'e'
1219 && gfc_next_ascii_char () == '.')
500f8f7b
RS
1220 /* Matched ".false.". */
1221 return 0;
1222 }
1223 else if (ch == 't')
1224 {
8fc541d3
FXC
1225 if (gfc_next_ascii_char () == 'r'
1226 && gfc_next_ascii_char () == 'u'
1227 && gfc_next_ascii_char () == 'e'
1228 && gfc_next_ascii_char () == '.')
500f8f7b
RS
1229 /* Matched ".true.". */
1230 return 1;
1231 }
1232 }
1233 gfc_current_locus = orig_loc;
1234 return -1;
1235}
1236
6de9cd9a
DN
1237/* Match a .true. or .false. */
1238
1239static match
edf1eac2 1240match_logical_constant (gfc_expr **result)
6de9cd9a 1241{
6de9cd9a 1242 gfc_expr *e;
bee64a2b 1243 int i, kind, is_iso_c;
6de9cd9a 1244
500f8f7b 1245 i = match_logical_constant_string ();
6de9cd9a
DN
1246 if (i == -1)
1247 return MATCH_NO;
1248
bee64a2b 1249 kind = get_kind (&is_iso_c);
6de9cd9a
DN
1250 if (kind == -1)
1251 return MATCH_ERROR;
1252 if (kind == -2)
9d64df18 1253 kind = gfc_default_logical_kind;
6de9cd9a 1254
e7a2d5fb 1255 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
eb62be44
SK
1256 {
1257 gfc_error ("Bad kind for logical constant at %C");
1258 return MATCH_ERROR;
1259 }
6de9cd9a 1260
b7e75771 1261 e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
bee64a2b 1262 e->ts.is_c_interop = is_iso_c;
6de9cd9a
DN
1263
1264 *result = e;
1265 return MATCH_YES;
1266}
1267
1268
1269/* Match a real or imaginary part of a complex constant that is a
1270 symbolic constant. */
1271
1272static match
edf1eac2 1273match_sym_complex_part (gfc_expr **result)
6de9cd9a
DN
1274{
1275 char name[GFC_MAX_SYMBOL_LEN + 1];
1276 gfc_symbol *sym;
1277 gfc_expr *e;
1278 match m;
1279
1280 m = gfc_match_name (name);
1281 if (m != MATCH_YES)
1282 return m;
1283
1284 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1285 return MATCH_NO;
1286
1287 if (sym->attr.flavor != FL_PARAMETER)
1288 {
284ee475 1289 /* Give the matcher for implied do-loops a chance to run. This yields
a5fbc2f3 1290 a much saner error message for "write(*,*) (i, i=1, 6" where the
284ee475
SK
1291 right parenthesis is missing. */
1292 char c;
1293 gfc_gobble_whitespace ();
1294 c = gfc_peek_ascii_char ();
1295 if (c == '=' || c == ',')
1296 {
1297 m = MATCH_NO;
1298 }
1299 else
1300 {
1301 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1302 m = MATCH_ERROR;
1303 }
1304 return m;
6de9cd9a
DN
1305 }
1306
70db5f02
SK
1307 if (!sym->value)
1308 goto error;
1309
6de9cd9a
DN
1310 if (!gfc_numeric_ts (&sym->value->ts))
1311 {
1312 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1313 return MATCH_ERROR;
1314 }
1315
1316 if (sym->value->rank != 0)
1317 {
1318 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1319 return MATCH_ERROR;
1320 }
1321
524af0d6
JB
1322 if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1323 "complex constant at %C"))
e227ac57
FXC
1324 return MATCH_ERROR;
1325
6de9cd9a
DN
1326 switch (sym->value->ts.type)
1327 {
1328 case BT_REAL:
1329 e = gfc_copy_expr (sym->value);
1330 break;
1331
1332 case BT_COMPLEX:
1333 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1334 if (e == NULL)
1335 goto error;
1336 break;
1337
1338 case BT_INTEGER:
9d64df18 1339 e = gfc_int2real (sym->value, gfc_default_real_kind);
6de9cd9a
DN
1340 if (e == NULL)
1341 goto error;
1342 break;
1343
1344 default:
1345 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1346 }
1347
edf1eac2 1348 *result = e; /* e is a scalar, real, constant expression. */
6de9cd9a
DN
1349 return MATCH_YES;
1350
1351error:
1352 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1353 return MATCH_ERROR;
1354}
1355
1356
6de9cd9a
DN
1357/* Match a real or imaginary part of a complex number. */
1358
1359static match
edf1eac2 1360match_complex_part (gfc_expr **result)
6de9cd9a
DN
1361{
1362 match m;
1363
1364 m = match_sym_complex_part (result);
1365 if (m != MATCH_NO)
1366 return m;
1367
69029c61
PB
1368 m = match_real_constant (result, 1);
1369 if (m != MATCH_NO)
1370 return m;
1371
1372 return match_integer_constant (result, 1);
6de9cd9a
DN
1373}
1374
1375
1376/* Try to match a complex constant. */
1377
1378static match
edf1eac2 1379match_complex_constant (gfc_expr **result)
6de9cd9a
DN
1380{
1381 gfc_expr *e, *real, *imag;
fea70c99 1382 gfc_error_buffer old_error;
6de9cd9a
DN
1383 gfc_typespec target;
1384 locus old_loc;
1385 int kind;
1386 match m;
1387
63645982 1388 old_loc = gfc_current_locus;
6de9cd9a
DN
1389 real = imag = e = NULL;
1390
1391 m = gfc_match_char ('(');
1392 if (m != MATCH_YES)
1393 return m;
1394
fea70c99 1395 gfc_push_error (&old_error);
6de9cd9a
DN
1396
1397 m = match_complex_part (&real);
1398 if (m == MATCH_NO)
d71b89ca 1399 {
fea70c99 1400 gfc_free_error (&old_error);
d71b89ca
JJ
1401 goto cleanup;
1402 }
6de9cd9a
DN
1403
1404 if (gfc_match_char (',') == MATCH_NO)
1405 {
396e56d2
TK
1406 /* It is possible that gfc_int2real issued a warning when
1407 converting an integer to real. Throw this away here. */
1408
1409 gfc_clear_warning ();
fea70c99 1410 gfc_pop_error (&old_error);
6de9cd9a
DN
1411 m = MATCH_NO;
1412 goto cleanup;
1413 }
1414
1415 /* If m is error, then something was wrong with the real part and we
1416 assume we have a complex constant because we've seen the ','. An
1417 ambiguous case here is the start of an iterator list of some
1418 sort. These sort of lists are matched prior to coming here. */
1419
1420 if (m == MATCH_ERROR)
d71b89ca 1421 {
fea70c99 1422 gfc_free_error (&old_error);
d71b89ca
JJ
1423 goto cleanup;
1424 }
fea70c99 1425 gfc_pop_error (&old_error);
6de9cd9a
DN
1426
1427 m = match_complex_part (&imag);
1428 if (m == MATCH_NO)
1429 goto syntax;
1430 if (m == MATCH_ERROR)
1431 goto cleanup;
1432
1433 m = gfc_match_char (')');
1434 if (m == MATCH_NO)
87ebdf2f
SK
1435 {
1436 /* Give the matcher for implied do-loops a chance to run. This
1437 yields a much saner error message for (/ (i, 4=i, 6) /). */
8fc541d3 1438 if (gfc_peek_ascii_char () == '=')
87ebdf2f
SK
1439 {
1440 m = MATCH_ERROR;
1441 goto cleanup;
1442 }
1443 else
6de9cd9a 1444 goto syntax;
87ebdf2f 1445 }
6de9cd9a
DN
1446
1447 if (m == MATCH_ERROR)
1448 goto cleanup;
1449
1450 /* Decide on the kind of this complex number. */
69029c61
PB
1451 if (real->ts.type == BT_REAL)
1452 {
1453 if (imag->ts.type == BT_REAL)
1454 kind = gfc_kind_max (real, imag);
1455 else
1456 kind = real->ts.kind;
1457 }
1458 else
1459 {
1460 if (imag->ts.type == BT_REAL)
1461 kind = imag->ts.kind;
1462 else
1463 kind = gfc_default_real_kind;
1464 }
d91909c0 1465 gfc_clear_ts (&target);
6de9cd9a
DN
1466 target.type = BT_REAL;
1467 target.kind = kind;
1468
69029c61 1469 if (real->ts.type != BT_REAL || kind != real->ts.kind)
6de9cd9a 1470 gfc_convert_type (real, &target, 2);
69029c61 1471 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
6de9cd9a
DN
1472 gfc_convert_type (imag, &target, 2);
1473
8dc63166 1474 e = convert_complex (real, imag, kind);
63645982 1475 e->where = gfc_current_locus;
6de9cd9a
DN
1476
1477 gfc_free_expr (real);
1478 gfc_free_expr (imag);
1479
1480 *result = e;
1481 return MATCH_YES;
1482
1483syntax:
1484 gfc_error ("Syntax error in COMPLEX constant at %C");
1485 m = MATCH_ERROR;
1486
1487cleanup:
1488 gfc_free_expr (e);
1489 gfc_free_expr (real);
1490 gfc_free_expr (imag);
63645982 1491 gfc_current_locus = old_loc;
6de9cd9a
DN
1492
1493 return m;
1494}
1495
1496
1497/* Match constants in any of several forms. Returns nonzero for a
1498 match, zero for no match. */
1499
1500match
edf1eac2 1501gfc_match_literal_constant (gfc_expr **result, int signflag)
6de9cd9a
DN
1502{
1503 match m;
1504
1505 m = match_complex_constant (result);
1506 if (m != MATCH_NO)
1507 return m;
1508
1509 m = match_string_constant (result);
1510 if (m != MATCH_NO)
1511 return m;
1512
1513 m = match_boz_constant (result);
1514 if (m != MATCH_NO)
1515 return m;
1516
1517 m = match_real_constant (result, signflag);
1518 if (m != MATCH_NO)
1519 return m;
1520
d3642f89
FW
1521 m = match_hollerith_constant (result);
1522 if (m != MATCH_NO)
1523 return m;
1524
6de9cd9a
DN
1525 m = match_integer_constant (result, signflag);
1526 if (m != MATCH_NO)
1527 return m;
1528
1529 m = match_logical_constant (result);
1530 if (m != MATCH_NO)
1531 return m;
1532
1533 return MATCH_NO;
1534}
1535
1536
2d71b918
JW
1537/* This checks if a symbol is the return value of an encompassing function.
1538 Function nesting can be maximally two levels deep, but we may have
1539 additional local namespaces like BLOCK etc. */
1540
1541bool
1542gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1543{
1544 if (!sym->attr.function || (sym->result != sym))
1545 return false;
1546 while (ns)
1547 {
1548 if (ns->proc_name == sym)
1549 return true;
1550 ns = ns->parent;
1551 }
1552 return false;
1553}
1554
1555
6de9cd9a
DN
1556/* Match a single actual argument value. An actual argument is
1557 usually an expression, but can also be a procedure name. If the
1558 argument is a single name, it is not always possible to tell
1559 whether the name is a dummy procedure or not. We treat these cases
1560 by creating an argument that looks like a dummy procedure and
1561 fixing things later during resolution. */
1562
1563static match
edf1eac2 1564match_actual_arg (gfc_expr **result)
6de9cd9a
DN
1565{
1566 char name[GFC_MAX_SYMBOL_LEN + 1];
1567 gfc_symtree *symtree;
1568 locus where, w;
1569 gfc_expr *e;
8fc541d3 1570 char c;
6de9cd9a 1571
618f4f46 1572 gfc_gobble_whitespace ();
63645982 1573 where = gfc_current_locus;
6de9cd9a
DN
1574
1575 switch (gfc_match_name (name))
1576 {
1577 case MATCH_ERROR:
1578 return MATCH_ERROR;
1579
1580 case MATCH_NO:
1581 break;
1582
1583 case MATCH_YES:
63645982 1584 w = gfc_current_locus;
6de9cd9a 1585 gfc_gobble_whitespace ();
8fc541d3 1586 c = gfc_next_ascii_char ();
63645982 1587 gfc_current_locus = w;
6de9cd9a
DN
1588
1589 if (c != ',' && c != ')')
1590 break;
1591
1592 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1593 break;
1594 /* Handle error elsewhere. */
1595
1596 /* Eliminate a couple of common cases where we know we don't
edf1eac2 1597 have a function argument. */
6de9cd9a 1598 if (symtree == NULL)
edf1eac2 1599 {
08a6b8e0 1600 gfc_get_sym_tree (name, NULL, &symtree, false);
edf1eac2
SK
1601 gfc_set_sym_referenced (symtree->n.sym);
1602 }
6de9cd9a
DN
1603 else
1604 {
edf1eac2 1605 gfc_symbol *sym;
6de9cd9a 1606
edf1eac2
SK
1607 sym = symtree->n.sym;
1608 gfc_set_sym_referenced (sym);
c0f0e35a
JD
1609 if (sym->attr.flavor == FL_NAMELIST)
1610 {
67914693 1611 gfc_error ("Namelist %qs cannot be an argument at %L",
c0f0e35a
JD
1612 sym->name, &where);
1613 break;
1614 }
6de9cd9a
DN
1615 if (sym->attr.flavor != FL_PROCEDURE
1616 && sym->attr.flavor != FL_UNKNOWN)
1617 break;
1618
6f9c9d6d
TB
1619 if (sym->attr.in_common && !sym->attr.proc_pointer)
1620 {
bf1b77dd 1621 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE,
524af0d6 1622 sym->name, &sym->declared_at))
efb63364 1623 return MATCH_ERROR;
6f9c9d6d
TB
1624 break;
1625 }
1626
6de9cd9a
DN
1627 /* If the symbol is a function with itself as the result and
1628 is being defined, then we have a variable. */
7a4ef45b
JJ
1629 if (sym->attr.function && sym->result == sym)
1630 {
2d71b918 1631 if (gfc_is_function_return_value (sym, gfc_current_ns))
7a4ef45b
JJ
1632 break;
1633
1634 if (sym->attr.entry
1635 && (sym->ns == gfc_current_ns
1636 || sym->ns == gfc_current_ns->parent))
1637 {
1638 gfc_entry_list *el = NULL;
1639
1640 for (el = sym->ns->entries; el; el = el->next)
1641 if (sym == el->sym)
1642 break;
1643
1644 if (el)
1645 break;
1646 }
1647 }
6de9cd9a
DN
1648 }
1649
1650 e = gfc_get_expr (); /* Leave it unknown for now */
1651 e->symtree = symtree;
1652 e->expr_type = EXPR_VARIABLE;
1653 e->ts.type = BT_PROCEDURE;
1654 e->where = where;
1655
1656 *result = e;
1657 return MATCH_YES;
1658 }
1659
63645982 1660 gfc_current_locus = where;
6de9cd9a
DN
1661 return gfc_match_expr (result);
1662}
1663
1664
5bab4c96 1665/* Match a keyword argument or type parameter spec list.. */
6de9cd9a
DN
1666
1667static match
5bab4c96 1668match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
6de9cd9a
DN
1669{
1670 char name[GFC_MAX_SYMBOL_LEN + 1];
1671 gfc_actual_arglist *a;
1672 locus name_locus;
1673 match m;
1674
63645982 1675 name_locus = gfc_current_locus;
6de9cd9a
DN
1676 m = gfc_match_name (name);
1677
1678 if (m != MATCH_YES)
1679 goto cleanup;
1680 if (gfc_match_char ('=') != MATCH_YES)
1681 {
1682 m = MATCH_NO;
1683 goto cleanup;
1684 }
1685
5bab4c96
PT
1686 if (pdt)
1687 {
1688 if (gfc_match_char ('*') == MATCH_YES)
1689 {
1690 actual->spec_type = SPEC_ASSUMED;
1691 goto add_name;
1692 }
1693 else if (gfc_match_char (':') == MATCH_YES)
1694 {
1695 actual->spec_type = SPEC_DEFERRED;
1696 goto add_name;
1697 }
1698 else
1699 actual->spec_type = SPEC_EXPLICIT;
1700 }
1701
6de9cd9a
DN
1702 m = match_actual_arg (&actual->expr);
1703 if (m != MATCH_YES)
1704 goto cleanup;
1705
1706 /* Make sure this name has not appeared yet. */
5bab4c96 1707add_name:
6de9cd9a
DN
1708 if (name[0] != '\0')
1709 {
1710 for (a = base; a; a = a->next)
cb9e4f55 1711 if (a->name != NULL && strcmp (a->name, name) == 0)
6de9cd9a 1712 {
a4d9b221 1713 gfc_error ("Keyword %qs at %C has already appeared in the "
edf1eac2 1714 "current argument list", name);
6de9cd9a
DN
1715 return MATCH_ERROR;
1716 }
1717 }
1718
51f03c6b 1719 actual->name = gfc_get_string ("%s", name);
6de9cd9a
DN
1720 return MATCH_YES;
1721
1722cleanup:
63645982 1723 gfc_current_locus = name_locus;
6de9cd9a
DN
1724 return m;
1725}
1726
1727
7fcafa71
PT
1728/* Match an argument list function, such as %VAL. */
1729
1730static match
1731match_arg_list_function (gfc_actual_arglist *result)
1732{
1733 char name[GFC_MAX_SYMBOL_LEN + 1];
1734 locus old_locus;
1735 match m;
1736
1737 old_locus = gfc_current_locus;
1738
1739 if (gfc_match_char ('%') != MATCH_YES)
1740 {
1741 m = MATCH_NO;
1742 goto cleanup;
1743 }
1744
1745 m = gfc_match ("%n (", name);
1746 if (m != MATCH_YES)
1747 goto cleanup;
1748
1749 if (name[0] != '\0')
1750 {
1751 switch (name[0])
1752 {
1753 case 'l':
2eb3745a 1754 if (gfc_str_startswith (name, "loc"))
7fcafa71
PT
1755 {
1756 result->name = "%LOC";
1757 break;
1758 }
191816a3 1759 /* FALLTHRU */
7fcafa71 1760 case 'r':
2eb3745a 1761 if (gfc_str_startswith (name, "ref"))
7fcafa71
PT
1762 {
1763 result->name = "%REF";
1764 break;
1765 }
191816a3 1766 /* FALLTHRU */
7fcafa71 1767 case 'v':
2eb3745a 1768 if (gfc_str_startswith (name, "val"))
7fcafa71
PT
1769 {
1770 result->name = "%VAL";
1771 break;
1772 }
191816a3 1773 /* FALLTHRU */
7fcafa71
PT
1774 default:
1775 m = MATCH_ERROR;
1776 goto cleanup;
1777 }
1778 }
1779
524af0d6 1780 if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
7fcafa71
PT
1781 {
1782 m = MATCH_ERROR;
1783 goto cleanup;
1784 }
1785
1786 m = match_actual_arg (&result->expr);
1787 if (m != MATCH_YES)
1788 goto cleanup;
1789
1790 if (gfc_match_char (')') != MATCH_YES)
1791 {
1792 m = MATCH_NO;
1793 goto cleanup;
1794 }
1795
1796 return MATCH_YES;
1797
1798cleanup:
1799 gfc_current_locus = old_locus;
1800 return m;
1801}
1802
1803
6de9cd9a
DN
1804/* Matches an actual argument list of a function or subroutine, from
1805 the opening parenthesis to the closing parenthesis. The argument
1806 list is assumed to allow keyword arguments because we don't know if
1807 the symbol associated with the procedure has an implicit interface
ed5ee445 1808 or not. We make sure keywords are unique. If sub_flag is set,
5bab4c96
PT
1809 we're matching the argument list of a subroutine.
1810
1811 NOTE: An alternative use for this function is to match type parameter
1812 spec lists, which are so similar to actual argument lists that the
1813 machinery can be reused. This use is flagged by the optional argument
1814 'pdt'. */
6de9cd9a
DN
1815
1816match
5bab4c96 1817gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
6de9cd9a
DN
1818{
1819 gfc_actual_arglist *head, *tail;
1820 int seen_keyword;
1821 gfc_st_label *label;
1822 locus old_loc;
1823 match m;
1824
1825 *argp = tail = NULL;
63645982 1826 old_loc = gfc_current_locus;
6de9cd9a
DN
1827
1828 seen_keyword = 0;
1829
1830 if (gfc_match_char ('(') == MATCH_NO)
1831 return (sub_flag) ? MATCH_YES : MATCH_NO;
1832
1833 if (gfc_match_char (')') == MATCH_YES)
1834 return MATCH_YES;
5bab4c96 1835
6de9cd9a
DN
1836 head = NULL;
1837
837c4b78
JW
1838 matching_actual_arglist++;
1839
6de9cd9a
DN
1840 for (;;)
1841 {
1842 if (head == NULL)
1843 head = tail = gfc_get_actual_arglist ();
1844 else
1845 {
1846 tail->next = gfc_get_actual_arglist ();
1847 tail = tail->next;
1848 }
1849
5bab4c96 1850 if (sub_flag && !pdt && gfc_match_char ('*') == MATCH_YES)
6de9cd9a 1851 {
a34a91f0 1852 m = gfc_match_st_label (&label);
6de9cd9a
DN
1853 if (m == MATCH_NO)
1854 gfc_error ("Expected alternate return label at %C");
1855 if (m != MATCH_YES)
1856 goto cleanup;
1857
524af0d6
JB
1858 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
1859 "at %C"))
fbdeeaac
JW
1860 goto cleanup;
1861
6de9cd9a
DN
1862 tail->label = label;
1863 goto next;
1864 }
1865
5bab4c96
PT
1866 if (pdt && !seen_keyword)
1867 {
1868 if (gfc_match_char (':') == MATCH_YES)
1869 {
1870 tail->spec_type = SPEC_DEFERRED;
1871 goto next;
1872 }
1873 else if (gfc_match_char ('*') == MATCH_YES)
1874 {
1875 tail->spec_type = SPEC_ASSUMED;
1876 goto next;
1877 }
1878 else
1879 tail->spec_type = SPEC_EXPLICIT;
18a4e7e3
PT
1880
1881 m = match_keyword_arg (tail, head, pdt);
1882 if (m == MATCH_YES)
1883 {
1884 seen_keyword = 1;
1885 goto next;
1886 }
1887 if (m == MATCH_ERROR)
1888 goto cleanup;
5bab4c96
PT
1889 }
1890
6de9cd9a 1891 /* After the first keyword argument is seen, the following
edf1eac2 1892 arguments must also have keywords. */
6de9cd9a
DN
1893 if (seen_keyword)
1894 {
5bab4c96 1895 m = match_keyword_arg (tail, head, pdt);
6de9cd9a
DN
1896
1897 if (m == MATCH_ERROR)
1898 goto cleanup;
1899 if (m == MATCH_NO)
1900 {
edf1eac2 1901 gfc_error ("Missing keyword name in actual argument list at %C");
6de9cd9a
DN
1902 goto cleanup;
1903 }
1904
1905 }
1906 else
1907 {
7fcafa71
PT
1908 /* Try an argument list function, like %VAL. */
1909 m = match_arg_list_function (tail);
6de9cd9a
DN
1910 if (m == MATCH_ERROR)
1911 goto cleanup;
1912
7fcafa71
PT
1913 /* See if we have the first keyword argument. */
1914 if (m == MATCH_NO)
1915 {
5bab4c96 1916 m = match_keyword_arg (tail, head, false);
7fcafa71
PT
1917 if (m == MATCH_YES)
1918 seen_keyword = 1;
1919 if (m == MATCH_ERROR)
1920 goto cleanup;
1921 }
1922
6de9cd9a
DN
1923 if (m == MATCH_NO)
1924 {
1925 /* Try for a non-keyword argument. */
1926 m = match_actual_arg (&tail->expr);
1927 if (m == MATCH_ERROR)
1928 goto cleanup;
1929 if (m == MATCH_NO)
1930 goto syntax;
1931 }
1932 }
1933
7fcafa71 1934
6de9cd9a
DN
1935 next:
1936 if (gfc_match_char (')') == MATCH_YES)
1937 break;
1938 if (gfc_match_char (',') != MATCH_YES)
1939 goto syntax;
1940 }
1941
1942 *argp = head;
837c4b78 1943 matching_actual_arglist--;
6de9cd9a
DN
1944 return MATCH_YES;
1945
1946syntax:
1947 gfc_error ("Syntax error in argument list at %C");
1948
1949cleanup:
1950 gfc_free_actual_arglist (head);
63645982 1951 gfc_current_locus = old_loc;
837c4b78 1952 matching_actual_arglist--;
6de9cd9a
DN
1953 return MATCH_ERROR;
1954}
1955
1956
8e1f752a 1957/* Used by gfc_match_varspec() to extend the reference list by one
6de9cd9a
DN
1958 element. */
1959
1960static gfc_ref *
edf1eac2 1961extend_ref (gfc_expr *primary, gfc_ref *tail)
6de9cd9a 1962{
6de9cd9a
DN
1963 if (primary->ref == NULL)
1964 primary->ref = tail = gfc_get_ref ();
1965 else
1966 {
1967 if (tail == NULL)
1968 gfc_internal_error ("extend_ref(): Bad tail");
1969 tail->next = gfc_get_ref ();
1970 tail = tail->next;
1971 }
1972
1973 return tail;
1974}
1975
1976
a5fbc2f3
PT
1977/* Used by gfc_match_varspec() to match an inquiry reference. */
1978
1979static bool
1980is_inquiry_ref (const char *name, gfc_ref **ref)
1981{
1982 inquiry_type type;
1983
1984 if (name == NULL)
1985 return false;
1986
1987 if (ref) *ref = NULL;
1988
1989 if (strcmp (name, "re") == 0)
1990 type = INQUIRY_RE;
1991 else if (strcmp (name, "im") == 0)
1992 type = INQUIRY_IM;
1993 else if (strcmp (name, "kind") == 0)
1994 type = INQUIRY_KIND;
1995 else if (strcmp (name, "len") == 0)
1996 type = INQUIRY_LEN;
1997 else
1998 return false;
1999
2000 if (ref)
2001 {
2002 *ref = gfc_get_ref ();
2003 (*ref)->type = REF_INQUIRY;
2004 (*ref)->u.i = type;
2005 }
2006
2007 return true;
2008}
2009
2010
6de9cd9a
DN
2011/* Match any additional specifications associated with the current
2012 variable like member references or substrings. If equiv_flag is
2013 set we only match stuff that is allowed inside an EQUIVALENCE
8e1f752a 2014 statement. sub_flag tells whether we expect a type-bound procedure found
713485cc
JW
2015 to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2016 components, 'ppc_arg' determines whether the PPC may be called (with an
2017 argument list), or whether it may just be referred to as a pointer. */
6de9cd9a 2018
8e1f752a 2019match
713485cc
JW
2020gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2021 bool ppc_arg)
6de9cd9a
DN
2022{
2023 char name[GFC_MAX_SYMBOL_LEN + 1];
f6288c24 2024 gfc_ref *substring, *tail, *tmp;
6de9cd9a 2025 gfc_component *component;
a8006d09 2026 gfc_symbol *sym = primary->symtree->n.sym;
b89a63b9 2027 gfc_expr *tgt_expr = NULL;
6de9cd9a 2028 match m;
f2d3cb25 2029 bool unknown;
a5fbc2f3
PT
2030 bool inquiry;
2031 locus old_loc;
f6288c24 2032 char sep;
6de9cd9a
DN
2033
2034 tail = NULL;
2035
3c721513 2036 gfc_gobble_whitespace ();
d3a9eea2
TB
2037
2038 if (gfc_peek_ascii_char () == '[')
2039 {
c49ea23d
PT
2040 if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2041 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2042 && CLASS_DATA (sym)->attr.dimension))
d3a9eea2
TB
2043 {
2044 gfc_error ("Array section designator, e.g. '(:)', is required "
2045 "besides the coarray designator '[...]' at %C");
2046 return MATCH_ERROR;
2047 }
c49ea23d
PT
2048 if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2049 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2050 && !CLASS_DATA (sym)->attr.codimension))
d3a9eea2 2051 {
a4d9b221 2052 gfc_error ("Coarray designator at %C but %qs is not a coarray",
d3a9eea2
TB
2053 sym->name);
2054 return MATCH_ERROR;
2055 }
2056 }
2057
b89a63b9
PT
2058 if (sym->assoc && sym->assoc->target)
2059 tgt_expr = sym->assoc->target;
2060
52bf62f9 2061 /* For associate names, we may not yet know whether they are arrays or not.
b125dc1e
PT
2062 If the selector expression is unambiguously an array; eg. a full array
2063 or an array section, then the associate name must be an array and we can
2064 fix it now. Otherwise, if parentheses follow and it is not a character
2065 type, we have to assume that it actually is one for now. The final
2066 decision will be made at resolution, of course. */
2067 if (sym->assoc
2068 && gfc_peek_ascii_char () == '('
2069 && sym->ts.type != BT_CLASS
2070 && !sym->attr.dimension)
2071 {
b89a63b9
PT
2072 gfc_ref *ref = NULL;
2073
2074 if (!sym->assoc->dangling && tgt_expr)
2075 {
2076 if (tgt_expr->expr_type == EXPR_VARIABLE)
2077 gfc_resolve_expr (tgt_expr);
2078
2079 ref = tgt_expr->ref;
2080 for (; ref; ref = ref->next)
2081 if (ref->type == REF_ARRAY
2082 && (ref->u.ar.type == AR_FULL
2083 || ref->u.ar.type == AR_SECTION))
2084 break;
2085 }
2086
2087 if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2088 && sym->assoc->st
2089 && sym->assoc->st->n.sym
2090 && sym->assoc->st->n.sym->attr.dimension == 0))
b125dc1e 2091 {
b89a63b9
PT
2092 sym->attr.dimension = 1;
2093 if (sym->as == NULL
b125dc1e
PT
2094 && sym->assoc->st
2095 && sym->assoc->st->n.sym
2096 && sym->assoc->st->n.sym->as)
2097 sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2098 }
2099 }
b89a63b9
PT
2100 else if (sym->ts.type == BT_CLASS
2101 && tgt_expr
2102 && tgt_expr->expr_type == EXPR_VARIABLE
2103 && sym->ts.u.derived != tgt_expr->ts.u.derived)
2104 {
2105 gfc_resolve_expr (tgt_expr);
2106 if (tgt_expr->rank)
2107 sym->ts.u.derived = tgt_expr->ts.u.derived;
2108 }
52bf62f9 2109
c74b74a8 2110 if ((equiv_flag && gfc_peek_ascii_char () == '(')
d3a9eea2 2111 || gfc_peek_ascii_char () == '[' || sym->attr.codimension
ce2ab24c 2112 || (sym->attr.dimension && sym->ts.type != BT_CLASS
2a573572 2113 && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
f64edc8b 2114 && !(gfc_matching_procptr_assignment
cf2b3c22 2115 && sym->attr.flavor == FL_PROCEDURE))
22061030 2116 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
492792ed
TB
2117 && (CLASS_DATA (sym)->attr.dimension
2118 || CLASS_DATA (sym)->attr.codimension)))
6de9cd9a 2119 {
102344e2
TB
2120 gfc_array_spec *as;
2121
2122 tail = extend_ref (primary, tail);
2123 tail->type = REF_ARRAY;
2124
a8006d09
JJ
2125 /* In EQUIVALENCE, we don't know yet whether we are seeing
2126 an array, character variable or array of character
edf1eac2 2127 variables. We'll leave the decision till resolve time. */
6de9cd9a 2128
102344e2
TB
2129 if (equiv_flag)
2130 as = NULL;
2131 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2132 as = CLASS_DATA (sym)->as;
2133 else
2134 as = sym->as;
2135
2136 m = gfc_match_array_ref (&tail->u.ar, as, equiv_flag,
2137 as ? as->corank : 0);
6de9cd9a
DN
2138 if (m != MATCH_YES)
2139 return m;
a8006d09 2140
3c721513 2141 gfc_gobble_whitespace ();
8fc541d3 2142 if (equiv_flag && gfc_peek_ascii_char () == '(')
a8006d09
JJ
2143 {
2144 tail = extend_ref (primary, tail);
2145 tail->type = REF_ARRAY;
2146
d3a9eea2 2147 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
a8006d09
JJ
2148 if (m != MATCH_YES)
2149 return m;
2150 }
6de9cd9a
DN
2151 }
2152
6de9cd9a
DN
2153 primary->ts = sym->ts;
2154
a8006d09
JJ
2155 if (equiv_flag)
2156 return MATCH_YES;
2157
f6288c24
FR
2158 /* With DEC extensions, member separator may be '.' or '%'. */
2159 sep = gfc_peek_ascii_char ();
2160 m = gfc_match_member_sep (sym);
2161 if (m == MATCH_ERROR)
2162 return MATCH_ERROR;
2163
a5fbc2f3
PT
2164 inquiry = false;
2165 if (m == MATCH_YES && sep == '%'
2166 && primary->ts.type != BT_CLASS
2167 && primary->ts.type != BT_DERIVED)
2168 {
2169 match mm;
2170 old_loc = gfc_current_locus;
2171 mm = gfc_match_name (name);
2172 if (mm == MATCH_YES && is_inquiry_ref (name, &tmp))
2173 inquiry = true;
2174 gfc_current_locus = old_loc;
2175 }
2176
f6288c24 2177 if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
713485cc 2178 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
ebac6d9c
DK
2179 gfc_set_default_type (sym, 0, sym->ns);
2180
b89a63b9 2181 /* See if there is a usable typespec in the "no IMPLICIT type" error. */
f6288c24 2182 if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES)
6ee65df3 2183 {
b89a63b9
PT
2184 bool permissible;
2185
a8399af8 2186 /* These target expressions can be resolved at any time. */
b89a63b9
PT
2187 permissible = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym
2188 && (tgt_expr->symtree->n.sym->attr.use_assoc
2189 || tgt_expr->symtree->n.sym->attr.host_assoc
2190 || tgt_expr->symtree->n.sym->attr.if_source
2191 == IFSRC_DECL);
2192 permissible = permissible
2193 || (tgt_expr && tgt_expr->expr_type == EXPR_OP);
2194
2195 if (permissible)
62d3c075 2196 {
b89a63b9
PT
2197 gfc_resolve_expr (tgt_expr);
2198 sym->ts = tgt_expr->ts;
62d3c075
PT
2199 }
2200
2201 if (sym->ts.type == BT_UNKNOWN)
2202 {
2203 gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2204 return MATCH_ERROR;
2205 }
6ee65df3
TB
2206 }
2207 else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
a5fbc2f3 2208 && m == MATCH_YES && !inquiry)
6ee65df3 2209 {
f6288c24
FR
2210 gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2211 sep, sym->name);
6ee65df3
TB
2212 return MATCH_ERROR;
2213 }
2214
a5fbc2f3 2215 if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
f6288c24 2216 || m != MATCH_YES)
6de9cd9a
DN
2217 goto check_substring;
2218
a5fbc2f3
PT
2219 if (!inquiry)
2220 sym = sym->ts.u.derived;
2221 else
2222 sym = NULL;
6de9cd9a
DN
2223
2224 for (;;)
2225 {
524af0d6 2226 bool t;
e157f736 2227 gfc_symtree *tbp;
8e1f752a 2228
6de9cd9a
DN
2229 m = gfc_match_name (name);
2230 if (m == MATCH_NO)
2231 gfc_error ("Expected structure component name at %C");
2232 if (m != MATCH_YES)
2233 return MATCH_ERROR;
2234
a5fbc2f3
PT
2235 if (primary->ts.type != BT_CLASS && primary->ts.type != BT_DERIVED)
2236 {
2237 inquiry = is_inquiry_ref (name, &tmp);
2238 if (inquiry)
2239 sym = NULL;
2240 }
2241 else
2242 inquiry = false;
2243
d5c50b02 2244 if (sym && sym->f2k_derived)
b2acf594
PT
2245 tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2246 else
2247 tbp = NULL;
2248
8e1f752a
DK
2249 if (tbp)
2250 {
2251 gfc_symbol* tbp_sym;
2252
524af0d6 2253 if (!t)
8e1f752a
DK
2254 return MATCH_ERROR;
2255
2256 gcc_assert (!tail || !tail->next);
236e3815
JW
2257
2258 if (!(primary->expr_type == EXPR_VARIABLE
2259 || (primary->expr_type == EXPR_STRUCTURE
2260 && primary->symtree && primary->symtree->n.sym
2261 && primary->symtree->n.sym->attr.flavor)))
2262 return MATCH_ERROR;
8e1f752a 2263
e34ccb4c 2264 if (tbp->n.tb->is_generic)
e157f736
DK
2265 tbp_sym = NULL;
2266 else
e34ccb4c 2267 tbp_sym = tbp->n.tb->u.specific->n.sym;
8e1f752a
DK
2268
2269 primary->expr_type = EXPR_COMPCALL;
e34ccb4c 2270 primary->value.compcall.tbp = tbp->n.tb;
e157f736 2271 primary->value.compcall.name = tbp->name;
4a44a72d
DK
2272 primary->value.compcall.ignore_pass = 0;
2273 primary->value.compcall.assign = 0;
2274 primary->value.compcall.base_object = NULL;
e157f736
DK
2275 gcc_assert (primary->symtree->n.sym->attr.referenced);
2276 if (tbp_sym)
2277 primary->ts = tbp_sym->ts;
049bb74e
JW
2278 else
2279 gfc_clear_ts (&primary->ts);
e157f736 2280
e34ccb4c 2281 m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
8e1f752a
DK
2282 &primary->value.compcall.actual);
2283 if (m == MATCH_ERROR)
2284 return MATCH_ERROR;
2285 if (m == MATCH_NO)
2286 {
2287 if (sub_flag)
2288 primary->value.compcall.actual = NULL;
2289 else
2290 {
2291 gfc_error ("Expected argument list at %C");
2292 return MATCH_ERROR;
2293 }
2294 }
2295
8e1f752a
DK
2296 break;
2297 }
2298
a5fbc2f3
PT
2299 if (!inquiry)
2300 component = gfc_find_component (sym, name, false, false, &tmp);
2301 else
2302 component = NULL;
2303
2304 if (component == NULL && !inquiry)
6de9cd9a
DN
2305 return MATCH_ERROR;
2306
a5fbc2f3
PT
2307 /* Extend the reference chain determined by gfc_find_component or
2308 is_inquiry_ref. */
f6288c24 2309 if (primary->ref == NULL)
a5fbc2f3 2310 primary->ref = tmp;
f6288c24 2311 else
a5fbc2f3
PT
2312 {
2313 /* Set by the for loop below for the last component ref. */
2314 gcc_assert (tail != NULL);
2315 tail->next = tmp;
2316 }
6de9cd9a 2317
f6288c24 2318 /* The reference chain may be longer than one hop for union
a5fbc2f3 2319 subcomponents; find the new tail. */
f6288c24 2320 for (tail = tmp; tail->next; tail = tail->next)
a5fbc2f3
PT
2321 ;
2322
2323 if (tmp && tmp->type == REF_INQUIRY)
2324 {
2325 gfc_simplify_expr (primary, 0);
2326
2327 if (primary->expr_type == EXPR_CONSTANT)
2328 goto check_done;
2329
2330 switch (tmp->u.i)
2331 {
2332 case INQUIRY_RE:
2333 case INQUIRY_IM:
2334 if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2335 return MATCH_ERROR;
2336
2337 if (primary->ts.type != BT_COMPLEX)
2338 {
2339 gfc_error ("The RE or IM part_ref at %C must be "
2340 "applied to a COMPLEX expression");
2341 return MATCH_ERROR;
2342 }
2343 primary->ts.type = BT_REAL;
2344 break;
2345
2346 case INQUIRY_LEN:
2347 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2348 return MATCH_ERROR;
2349
2350 if (primary->ts.type != BT_CHARACTER)
2351 {
2352 gfc_error ("The LEN part_ref at %C must be applied "
2353 "to a CHARACTER expression");
2354 return MATCH_ERROR;
2355 }
2356 primary->ts.u.cl = NULL;
2357 primary->ts.type = BT_INTEGER;
2358 primary->ts.kind = gfc_default_integer_kind;
2359 break;
2360
2361 case INQUIRY_KIND:
2362 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2363 return MATCH_ERROR;
2364
2365 if (primary->ts.type == BT_CLASS
2366 || primary->ts.type == BT_DERIVED)
2367 {
2368 gfc_error ("The KIND part_ref at %C must be applied "
2369 "to an expression of intrinsic type");
2370 return MATCH_ERROR;
2371 }
2372 primary->ts.type = BT_INTEGER;
2373 primary->ts.kind = gfc_default_integer_kind;
2374 break;
2375
2376 default:
2377 gcc_unreachable ();
2378 }
2379
2380 goto check_done;
2381 }
6de9cd9a
DN
2382
2383 primary->ts = component->ts;
2384
a4a76e52 2385 if (component->attr.proc_pointer && ppc_arg)
713485cc 2386 {
837c4b78 2387 /* Procedure pointer component call: Look for argument list. */
23878536 2388 m = gfc_match_actual_arglist (sub_flag,
713485cc
JW
2389 &primary->value.compcall.actual);
2390 if (m == MATCH_ERROR)
2391 return MATCH_ERROR;
837c4b78
JW
2392
2393 if (m == MATCH_NO && !gfc_matching_ptr_assignment
a4a76e52 2394 && !gfc_matching_procptr_assignment && !matching_actual_arglist)
837c4b78 2395 {
a4d9b221 2396 gfc_error ("Procedure pointer component %qs requires an "
837c4b78
JW
2397 "argument list at %C", component->name);
2398 return MATCH_ERROR;
2399 }
2400
23878536
JW
2401 if (m == MATCH_YES)
2402 primary->expr_type = EXPR_PPC;
713485cc
JW
2403
2404 break;
2405 }
2406
c74b74a8 2407 if (component->as != NULL && !component->attr.proc_pointer)
6de9cd9a
DN
2408 {
2409 tail = extend_ref (primary, tail);
2410 tail->type = REF_ARRAY;
2411
d3a9eea2
TB
2412 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2413 component->as->corank);
6de9cd9a
DN
2414 if (m != MATCH_YES)
2415 return m;
2416 }
156c0160
JW
2417 else if (component->ts.type == BT_CLASS && component->attr.class_ok
2418 && CLASS_DATA (component)->as && !component->attr.proc_pointer)
cf2b3c22
TB
2419 {
2420 tail = extend_ref (primary, tail);
2421 tail->type = REF_ARRAY;
6de9cd9a 2422
7a08eda1 2423 m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
d3a9eea2 2424 equiv_flag,
7a08eda1 2425 CLASS_DATA (component)->as->corank);
cf2b3c22
TB
2426 if (m != MATCH_YES)
2427 return m;
2428 }
2429
a5fbc2f3
PT
2430check_done:
2431 /* In principle, we could have eg. expr%re%kind so we must allow for
2432 this possibility. */
2433 if (gfc_match_char ('%') == MATCH_YES)
2434 {
2435 if (component && (component->ts.type == BT_DERIVED
2436 || component->ts.type == BT_CLASS))
2437 sym = component->ts.u.derived;
2438 continue;
2439 }
2440 else if (inquiry)
2441 break;
2442
cf2b3c22 2443 if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
a5fbc2f3 2444 || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
6de9cd9a
DN
2445 break;
2446
a5fbc2f3
PT
2447 if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2448 sym = component->ts.u.derived;
6de9cd9a
DN
2449 }
2450
2451check_substring:
f2d3cb25 2452 unknown = false;
f6288c24 2453 if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
c040ffff 2454 {
713485cc 2455 if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
c040ffff 2456 {
edf1eac2
SK
2457 gfc_set_default_type (sym, 0, sym->ns);
2458 primary->ts = sym->ts;
f2d3cb25 2459 unknown = true;
c040ffff
TS
2460 }
2461 }
2462
6de9cd9a
DN
2463 if (primary->ts.type == BT_CHARACTER)
2464 {
38217d3e
PT
2465 bool def = primary->ts.deferred == 1;
2466 switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
6de9cd9a
DN
2467 {
2468 case MATCH_YES:
2469 if (tail == NULL)
2470 primary->ref = substring;
2471 else
2472 tail->next = substring;
2473
2474 if (primary->expr_type == EXPR_CONSTANT)
2475 primary->expr_type = EXPR_SUBSTRING;
2476
860c8f3b 2477 if (substring)
bc21d315 2478 primary->ts.u.cl = NULL;
860c8f3b 2479
6de9cd9a
DN
2480 break;
2481
2482 case MATCH_NO:
f2d3cb25 2483 if (unknown)
858f1fa2
DK
2484 {
2485 gfc_clear_ts (&primary->ts);
2486 gfc_clear_ts (&sym->ts);
2487 }
6de9cd9a
DN
2488 break;
2489
2490 case MATCH_ERROR:
2491 return MATCH_ERROR;
2492 }
2493 }
2494
4ceda204
JW
2495 /* F08:C611. */
2496 if (primary->ts.type == BT_DERIVED && primary->ref
2497 && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2498 {
2499 gfc_error ("Nonpolymorphic reference to abstract type at %C");
2500 return MATCH_ERROR;
2501 }
2502
2503 /* F08:C727. */
d3a9eea2
TB
2504 if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2505 {
2506 gfc_error ("Coindexed procedure-pointer component at %C");
2507 return MATCH_ERROR;
2508 }
2509
6de9cd9a
DN
2510 return MATCH_YES;
2511}
2512
2513
2514/* Given an expression that is a variable, figure out what the
2515 ultimate variable's type and attribute is, traversing the reference
2516 structures if necessary.
2517
2518 This subroutine is trickier than it looks. We start at the base
2519 symbol and store the attribute. Component references load a
2520 completely new attribute.
2521
2522 A couple of rules come into play. Subobjects of targets are always
2523 targets themselves. If we see a component that goes through a
2524 pointer, then the expression must also be a target, since the
2525 pointer is associated with something (if it isn't core will soon be
2526 dumped). If we see a full part or section of an array, the
2527 expression is also an array.
2528
f7b529fa 2529 We can have at most one full array reference. */
6de9cd9a
DN
2530
2531symbol_attribute
edf1eac2 2532gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
6de9cd9a 2533{
16acb1a8 2534 int dimension, codimension, pointer, allocatable, target;
6de9cd9a
DN
2535 symbol_attribute attr;
2536 gfc_ref *ref;
cf2b3c22
TB
2537 gfc_symbol *sym;
2538 gfc_component *comp;
a5fbc2f3 2539 bool has_inquiry_part;
6de9cd9a 2540
50dbf0b4 2541 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
6de9cd9a
DN
2542 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2543
cf2b3c22
TB
2544 sym = expr->symtree->n.sym;
2545 attr = sym->attr;
6de9cd9a 2546
528622fd 2547 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
cf2b3c22 2548 {
7a08eda1 2549 dimension = CLASS_DATA (sym)->attr.dimension;
83ba23b7 2550 codimension = CLASS_DATA (sym)->attr.codimension;
d40477b4 2551 pointer = CLASS_DATA (sym)->attr.class_pointer;
7a08eda1 2552 allocatable = CLASS_DATA (sym)->attr.allocatable;
cf2b3c22
TB
2553 }
2554 else
2555 {
2556 dimension = attr.dimension;
83ba23b7 2557 codimension = attr.codimension;
cf2b3c22
TB
2558 pointer = attr.pointer;
2559 allocatable = attr.allocatable;
2560 }
6de9cd9a
DN
2561
2562 target = attr.target;
713485cc 2563 if (pointer || attr.proc_pointer)
6de9cd9a
DN
2564 target = 1;
2565
2566 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
cf2b3c22 2567 *ts = sym->ts;
6de9cd9a 2568
a5fbc2f3
PT
2569 has_inquiry_part = false;
2570 for (ref = expr->ref; ref; ref = ref->next)
2571 if (ref->type == REF_INQUIRY)
2572 {
2573 has_inquiry_part = true;
2574 break;
2575 }
2576
8c91ab34 2577 for (ref = expr->ref; ref; ref = ref->next)
6de9cd9a
DN
2578 switch (ref->type)
2579 {
2580 case REF_ARRAY:
2581
2582 switch (ref->u.ar.type)
2583 {
2584 case AR_FULL:
2585 dimension = 1;
2586 break;
2587
2588 case AR_SECTION:
5046aff5 2589 allocatable = pointer = 0;
6de9cd9a
DN
2590 dimension = 1;
2591 break;
2592
2593 case AR_ELEMENT:
d3a9eea2
TB
2594 /* Handle coarrays. */
2595 if (ref->u.ar.dimen > 0)
2596 allocatable = pointer = 0;
6de9cd9a
DN
2597 break;
2598
2599 case AR_UNKNOWN:
4f81c2a3
SK
2600 /* For standard conforming code, AR_UNKNOWN should not happen.
2601 For nonconforming code, gfortran can end up here. Treat it
2602 as a no-op. */
2603 break;
6de9cd9a
DN
2604 }
2605
2606 break;
2607
2608 case REF_COMPONENT:
cf2b3c22
TB
2609 comp = ref->u.c.component;
2610 attr = comp->attr;
a5fbc2f3 2611 if (ts != NULL && !has_inquiry_part)
e8a25349 2612 {
cf2b3c22 2613 *ts = comp->ts;
e8a25349
TS
2614 /* Don't set the string length if a substring reference
2615 follows. */
2616 if (ts->type == BT_CHARACTER
2617 && ref->next && ref->next->type == REF_SUBSTRING)
bc21d315 2618 ts->u.cl = NULL;
e8a25349 2619 }
6de9cd9a 2620
cf2b3c22
TB
2621 if (comp->ts.type == BT_CLASS)
2622 {
83ba23b7 2623 codimension = CLASS_DATA (comp)->attr.codimension;
d40477b4 2624 pointer = CLASS_DATA (comp)->attr.class_pointer;
7a08eda1 2625 allocatable = CLASS_DATA (comp)->attr.allocatable;
cf2b3c22
TB
2626 }
2627 else
2628 {
83ba23b7 2629 codimension = comp->attr.codimension;
cf2b3c22
TB
2630 pointer = comp->attr.pointer;
2631 allocatable = comp->attr.allocatable;
2632 }
713485cc 2633 if (pointer || attr.proc_pointer)
6de9cd9a
DN
2634 target = 1;
2635
2636 break;
2637
a5fbc2f3 2638 case REF_INQUIRY:
6de9cd9a 2639 case REF_SUBSTRING:
5046aff5 2640 allocatable = pointer = 0;
6de9cd9a
DN
2641 break;
2642 }
2643
2644 attr.dimension = dimension;
83ba23b7 2645 attr.codimension = codimension;
6de9cd9a 2646 attr.pointer = pointer;
5046aff5 2647 attr.allocatable = allocatable;
6de9cd9a 2648 attr.target = target;
80f95228 2649 attr.save = sym->attr.save;
6de9cd9a
DN
2650
2651 return attr;
2652}
2653
2654
2655/* Return the attribute from a general expression. */
2656
2657symbol_attribute
edf1eac2 2658gfc_expr_attr (gfc_expr *e)
6de9cd9a
DN
2659{
2660 symbol_attribute attr;
2661
2662 switch (e->expr_type)
2663 {
2664 case EXPR_VARIABLE:
2665 attr = gfc_variable_attr (e, NULL);
2666 break;
2667
2668 case EXPR_FUNCTION:
2669 gfc_clear_attr (&attr);
2670
50c7654b 2671 if (e->value.function.esym && e->value.function.esym->result)
cf2b3c22
TB
2672 {
2673 gfc_symbol *sym = e->value.function.esym->result;
2674 attr = sym->attr;
2675 if (sym->ts.type == BT_CLASS)
2676 {
7a08eda1 2677 attr.dimension = CLASS_DATA (sym)->attr.dimension;
d40477b4 2678 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
7a08eda1 2679 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
cf2b3c22
TB
2680 }
2681 }
574284e9
AV
2682 else if (e->value.function.isym
2683 && e->value.function.isym->transformational
2684 && e->ts.type == BT_CLASS)
2685 attr = CLASS_DATA (e)->attr;
50dbf0b4
JW
2686 else
2687 attr = gfc_variable_attr (e, NULL);
6de9cd9a
DN
2688
2689 /* TODO: NULL() returns pointers. May have to take care of this
edf1eac2 2690 here. */
6de9cd9a
DN
2691
2692 break;
2693
2694 default:
3c9f5092
AV
2695 gfc_clear_attr (&attr);
2696 break;
2697 }
2698
2699 return attr;
2700}
2701
2702
2703/* Given an expression, figure out what the ultimate expression
2704 attribute is. This routine is similar to gfc_variable_attr with
2705 parts of gfc_expr_attr, but focuses more on the needs of
2706 coarrays. For coarrays a codimension attribute is kind of
ba85c8c3
AV
2707 "infectious" being propagated once set and never cleared.
2708 The coarray_comp is only set, when the expression refs a coarray
2709 component. REFS_COMP is set when present to true only, when this EXPR
2710 refs a (non-_data) component. To check whether EXPR refs an allocatable
2711 component in a derived type coarray *refs_comp needs to be set and
2712 coarray_comp has to false. */
3c9f5092
AV
2713
2714static symbol_attribute
ba85c8c3 2715caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3c9f5092 2716{
de91486c 2717 int dimension, codimension, pointer, allocatable, target, coarray_comp;
3c9f5092
AV
2718 symbol_attribute attr;
2719 gfc_ref *ref;
2720 gfc_symbol *sym;
2721 gfc_component *comp;
2722
2723 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2724 gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
2725
2726 sym = expr->symtree->n.sym;
2727 gfc_clear_attr (&attr);
2728
ba85c8c3 2729 if (refs_comp)
525a5e33 2730 *refs_comp = false;
ba85c8c3 2731
3c9f5092
AV
2732 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2733 {
2734 dimension = CLASS_DATA (sym)->attr.dimension;
2735 codimension = CLASS_DATA (sym)->attr.codimension;
2736 pointer = CLASS_DATA (sym)->attr.class_pointer;
2737 allocatable = CLASS_DATA (sym)->attr.allocatable;
de91486c
AV
2738 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2739 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3c9f5092
AV
2740 }
2741 else
2742 {
2743 dimension = sym->attr.dimension;
2744 codimension = sym->attr.codimension;
2745 pointer = sym->attr.pointer;
2746 allocatable = sym->attr.allocatable;
de91486c 2747 attr.alloc_comp = sym->ts.type == BT_DERIVED
3c9f5092 2748 ? sym->ts.u.derived->attr.alloc_comp : 0;
de91486c
AV
2749 attr.pointer_comp = sym->ts.type == BT_DERIVED
2750 ? sym->ts.u.derived->attr.pointer_comp : 0;
3c9f5092
AV
2751 }
2752
ba85c8c3 2753 target = coarray_comp = 0;
3c9f5092
AV
2754 if (pointer || attr.proc_pointer)
2755 target = 1;
2756
2757 for (ref = expr->ref; ref; ref = ref->next)
2758 switch (ref->type)
2759 {
2760 case REF_ARRAY:
2761
2762 switch (ref->u.ar.type)
2763 {
2764 case AR_FULL:
2765 case AR_SECTION:
2766 dimension = 1;
2767 break;
2768
2769 case AR_ELEMENT:
2770 /* Handle coarrays. */
2771 if (ref->u.ar.dimen > 0 && !in_allocate)
2772 allocatable = pointer = 0;
2773 break;
2774
2775 case AR_UNKNOWN:
2776 /* If any of start, end or stride is not integer, there will
2777 already have been an error issued. */
2778 int errors;
2779 gfc_get_errors (NULL, &errors);
2780 if (errors == 0)
2781 gfc_internal_error ("gfc_caf_attr(): Bad array reference");
2782 }
2783
2784 break;
2785
2786 case REF_COMPONENT:
2787 comp = ref->u.c.component;
2788
2789 if (comp->ts.type == BT_CLASS)
2790 {
ba85c8c3
AV
2791 /* Set coarray_comp only, when this component introduces the
2792 coarray. */
2793 coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3c9f5092
AV
2794 codimension |= CLASS_DATA (comp)->attr.codimension;
2795 pointer = CLASS_DATA (comp)->attr.class_pointer;
2796 allocatable = CLASS_DATA (comp)->attr.allocatable;
3c9f5092
AV
2797 }
2798 else
2799 {
ba85c8c3
AV
2800 /* Set coarray_comp only, when this component introduces the
2801 coarray. */
2802 coarray_comp = !codimension && comp->attr.codimension;
3c9f5092
AV
2803 codimension |= comp->attr.codimension;
2804 pointer = comp->attr.pointer;
2805 allocatable = comp->attr.allocatable;
3c9f5092
AV
2806 }
2807
525a5e33
AV
2808 if (refs_comp && strcmp (comp->name, "_data") != 0
2809 && (ref->next == NULL
2810 || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
2811 *refs_comp = true;
ba85c8c3 2812
3c9f5092
AV
2813 if (pointer || attr.proc_pointer)
2814 target = 1;
2815
2816 break;
2817
2818 case REF_SUBSTRING:
a5fbc2f3 2819 case REF_INQUIRY:
3c9f5092
AV
2820 allocatable = pointer = 0;
2821 break;
2822 }
2823
2824 attr.dimension = dimension;
2825 attr.codimension = codimension;
2826 attr.pointer = pointer;
2827 attr.allocatable = allocatable;
2828 attr.target = target;
2829 attr.save = sym->attr.save;
2830 attr.coarray_comp = coarray_comp;
3c9f5092
AV
2831
2832 return attr;
2833}
2834
2835
2836symbol_attribute
ba85c8c3 2837gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3c9f5092
AV
2838{
2839 symbol_attribute attr;
2840
2841 switch (e->expr_type)
2842 {
2843 case EXPR_VARIABLE:
ba85c8c3 2844 attr = caf_variable_attr (e, in_allocate, refs_comp);
3c9f5092
AV
2845 break;
2846
2847 case EXPR_FUNCTION:
2848 gfc_clear_attr (&attr);
2849
2850 if (e->value.function.esym && e->value.function.esym->result)
2851 {
2852 gfc_symbol *sym = e->value.function.esym->result;
2853 attr = sym->attr;
2854 if (sym->ts.type == BT_CLASS)
2855 {
2856 attr.dimension = CLASS_DATA (sym)->attr.dimension;
2857 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2858 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2859 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
de91486c
AV
2860 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
2861 ->attr.pointer_comp;
3c9f5092
AV
2862 }
2863 }
2864 else if (e->symtree)
ba85c8c3 2865 attr = caf_variable_attr (e, in_allocate, refs_comp);
3c9f5092
AV
2866 else
2867 gfc_clear_attr (&attr);
2868 break;
2869
2870 default:
6de9cd9a
DN
2871 gfc_clear_attr (&attr);
2872 break;
2873 }
2874
2875 return attr;
2876}
2877
2878
2879/* Match a structure constructor. The initial symbol has already been
2880 seen. */
2881
fa9290d3
DK
2882typedef struct gfc_structure_ctor_component
2883{
2884 char* name;
2885 gfc_expr* val;
2886 locus where;
2887 struct gfc_structure_ctor_component* next;
2888}
2889gfc_structure_ctor_component;
2890
ece3f663 2891#define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
fa9290d3
DK
2892
2893static void
2894gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2895{
cede9502 2896 free (comp->name);
fa9290d3 2897 gfc_free_expr (comp->val);
cede9502 2898 free (comp);
fa9290d3
DK
2899}
2900
7d1f1e61
PT
2901
2902/* Translate the component list into the actual constructor by sorting it in
2903 the order required; this also checks along the way that each and every
2904 component actually has an initializer and handles default initializers
2905 for components without explicit value given. */
524af0d6 2906static bool
7d1f1e61 2907build_actual_constructor (gfc_structure_ctor_component **comp_head,
b7e75771 2908 gfc_constructor_base *ctor_head, gfc_symbol *sym)
6de9cd9a 2909{
fa9290d3 2910 gfc_structure_ctor_component *comp_iter;
7d1f1e61
PT
2911 gfc_component *comp;
2912
2913 for (comp = sym->components; comp; comp = comp->next)
2914 {
2915 gfc_structure_ctor_component **next_ptr;
2916 gfc_expr *value = NULL;
2917
2918 /* Try to find the initializer for the current component by name. */
2919 next_ptr = comp_head;
2920 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2921 {
2922 if (!strcmp (comp_iter->name, comp->name))
2923 break;
2924 next_ptr = &comp_iter->next;
2925 }
2926
2927 /* If an extension, try building the parent derived type by building
2928 a value expression for the parent derived type and calling self. */
2929 if (!comp_iter && comp == sym->components && sym->attr.extension)
2930 {
b7e75771
JD
2931 value = gfc_get_structure_constructor_expr (comp->ts.type,
2932 comp->ts.kind,
2933 &gfc_current_locus);
7d1f1e61 2934 value->ts = comp->ts;
7d1f1e61 2935
bf1b77dd
PT
2936 if (!build_actual_constructor (comp_head,
2937 &value->value.constructor,
524af0d6 2938 comp->ts.u.derived))
7d1f1e61
PT
2939 {
2940 gfc_free_expr (value);
524af0d6 2941 return false;
7d1f1e61 2942 }
b7e75771
JD
2943
2944 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2945 continue;
2946 }
2947
2948 /* If it was not found, try the default initializer if there's any;
2b3dc0db 2949 otherwise, it's an error unless this is a deferred parameter. */
7d1f1e61
PT
2950 if (!comp_iter)
2951 {
2952 if (comp->initializer)
2953 {
524af0d6
JB
2954 if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
2955 "with missing optional arguments at %C"))
2956 return false;
7d1f1e61
PT
2957 value = gfc_copy_expr (comp->initializer);
2958 }
9b548517
AV
2959 else if (comp->attr.allocatable
2960 || (comp->ts.type == BT_CLASS
2961 && CLASS_DATA (comp)->attr.allocatable))
7430df97
JW
2962 {
2963 if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
2f029c08 2964 "allocatable component %qs given in the "
9b548517 2965 "structure constructor at %C", comp->name))
7430df97
JW
2966 return false;
2967 }
9b548517 2968 else if (!comp->attr.artificial)
7d1f1e61 2969 {
a4d9b221 2970 gfc_error ("No initializer for component %qs given in the"
546c8974 2971 " structure constructor at %C", comp->name);
524af0d6 2972 return false;
7d1f1e61
PT
2973 }
2974 }
2975 else
2976 value = comp_iter->val;
2977
2978 /* Add the value to the constructor chain built. */
b7e75771 2979 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2980
2981 /* Remove the entry from the component list. We don't want the expression
2982 value to be free'd, so set it to NULL. */
2983 if (comp_iter)
2984 {
2985 *next_ptr = comp_iter->next;
2986 comp_iter->val = NULL;
2987 gfc_free_structure_ctor_component (comp_iter);
2988 }
2989 }
524af0d6 2990 return true;
7d1f1e61
PT
2991}
2992
c3f34952 2993
524af0d6 2994bool
c3f34952
TB
2995gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
2996 gfc_actual_arglist **arglist,
2997 bool parent)
7d1f1e61 2998{
c3f34952 2999 gfc_actual_arglist *actual;
7d1f1e61 3000 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
b7e75771 3001 gfc_constructor_base ctor_head = NULL;
fa9290d3 3002 gfc_component *comp; /* Is set NULL when named component is first seen */
fa9290d3 3003 const char* last_name = NULL;
c3f34952
TB
3004 locus old_locus;
3005 gfc_expr *expr;
6de9cd9a 3006
c3f34952
TB
3007 expr = parent ? *cexpr : e;
3008 old_locus = gfc_current_locus;
3009 if (parent)
3010 ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3011 else
3012 gfc_current_locus = expr->where;
6de9cd9a 3013
c3f34952 3014 comp_tail = comp_head = NULL;
6de9cd9a 3015
52f49934
DK
3016 if (!parent && sym->attr.abstract)
3017 {
1fe61adf 3018 gfc_error ("Cannot construct ABSTRACT type %qs at %L",
c3f34952
TB
3019 sym->name, &expr->where);
3020 goto cleanup;
52f49934
DK
3021 }
3022
c3f34952
TB
3023 comp = sym->components;
3024 actual = parent ? *arglist : expr->value.function.actual;
3025 for ( ; actual; )
6de9cd9a 3026 {
c3f34952 3027 gfc_component *this_comp = NULL;
6de9cd9a 3028
c3f34952
TB
3029 if (!comp_head)
3030 comp_tail = comp_head = gfc_get_structure_ctor_component ();
3031 else
3032 {
3033 comp_tail->next = gfc_get_structure_ctor_component ();
3034 comp_tail = comp_tail->next;
3035 }
3036 if (actual->name)
3037 {
524af0d6
JB
3038 if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3039 " constructor with named arguments at %C"))
c3f34952 3040 goto cleanup;
6de9cd9a 3041
c3f34952
TB
3042 comp_tail->name = xstrdup (actual->name);
3043 last_name = comp_tail->name;
3044 comp = NULL;
3045 }
3046 else
3047 {
3048 /* Components without name are not allowed after the first named
3049 component initializer! */
9b548517 3050 if (!comp || comp->attr.artificial)
fa9290d3 3051 {
c3f34952
TB
3052 if (last_name)
3053 gfc_error ("Component initializer without name after component"
546c8974 3054 " named %s at %L", last_name,
c3f34952
TB
3055 actual->expr ? &actual->expr->where
3056 : &gfc_current_locus);
3057 else
3058 gfc_error ("Too many components in structure constructor at "
546c8974
DM
3059 "%L", actual->expr ? &actual->expr->where
3060 : &gfc_current_locus);
c3f34952 3061 goto cleanup;
fa9290d3 3062 }
fa9290d3 3063
c3f34952
TB
3064 comp_tail->name = xstrdup (comp->name);
3065 }
fa9290d3 3066
c3f34952 3067 /* Find the current component in the structure definition and check
9d1210f4 3068 its access is not private. */
c3f34952 3069 if (comp)
f6288c24 3070 this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
c3f34952
TB
3071 else
3072 {
3073 this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
f6288c24 3074 false, false, NULL);
c3f34952
TB
3075 comp = NULL; /* Reset needed! */
3076 }
6de9cd9a 3077
c3f34952
TB
3078 /* Here we can check if a component name is given which does not
3079 correspond to any component of the defined structure. */
3080 if (!this_comp)
3081 goto cleanup;
fa9290d3 3082
04946c6b
TK
3083 /* For a constant string constructor, make sure the length is
3084 correct; truncate of fill with blanks if needed. */
3085 if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3086 && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3087 && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
13a7688f 3088 && actual->expr->ts.type == BT_CHARACTER
04946c6b
TK
3089 && actual->expr->expr_type == EXPR_CONSTANT)
3090 {
3091 ptrdiff_t c, e;
3092 c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3093 e = actual->expr->value.character.length;
3094
3095 if (c != e)
3096 {
3097 ptrdiff_t i, to;
3098 gfc_char_t *dest;
3099 dest = gfc_get_wide_string (c + 1);
3100
3101 to = e < c ? e : c;
3102 for (i = 0; i < to; i++)
3103 dest[i] = actual->expr->value.character.string[i];
a5fbc2f3 3104
04946c6b
TK
3105 for (i = e; i < c; i++)
3106 dest[i] = ' ';
3107
3108 dest[c] = '\0';
3109 free (actual->expr->value.character.string);
3110
3111 actual->expr->value.character.length = c;
3112 actual->expr->value.character.string = dest;
cf015ca2
TK
3113
3114 if (warn_line_truncation && c < e)
3115 gfc_warning_now (OPT_Wcharacter_truncation,
3116 "CHARACTER expression will be truncated "
3117 "in constructor (%ld/%ld) at %L", (long int) c,
3118 (long int) e, &actual->expr->where);
04946c6b
TK
3119 }
3120 }
3121
c3f34952
TB
3122 comp_tail->val = actual->expr;
3123 if (actual->expr != NULL)
3124 comp_tail->where = actual->expr->where;
3125 actual->expr = NULL;
fa9290d3 3126
c3f34952 3127 /* Check if this component is already given a value. */
bf1b77dd 3128 for (comp_iter = comp_head; comp_iter != comp_tail;
c3f34952
TB
3129 comp_iter = comp_iter->next)
3130 {
3131 gcc_assert (comp_iter);
3132 if (!strcmp (comp_iter->name, comp_tail->name))
d3a9eea2 3133 {
c4100eae 3134 gfc_error ("Component %qs is initialized twice in the structure"
546c8974 3135 " constructor at %L", comp_tail->name,
c3f34952
TB
3136 comp_tail->val ? &comp_tail->where
3137 : &gfc_current_locus);
d3a9eea2 3138 goto cleanup;
c3f34952
TB
3139 }
3140 }
d3a9eea2 3141
c3f34952
TB
3142 /* F2008, R457/C725, for PURE C1283. */
3143 if (this_comp->attr.pointer && comp_tail->val
3144 && gfc_is_coindexed (comp_tail->val))
3145 {
a4d9b221 3146 gfc_error ("Coindexed expression to pointer component %qs in "
546c8974 3147 "structure constructor at %L", comp_tail->name,
c3f34952
TB
3148 &comp_tail->where);
3149 goto cleanup;
3150 }
d3a9eea2 3151
c3f34952
TB
3152 /* If not explicitly a parent constructor, gather up the components
3153 and build one. */
3154 if (comp && comp == sym->components
3155 && sym->attr.extension
3156 && comp_tail->val
f6288c24 3157 && (!gfc_bt_struct (comp_tail->val->ts.type)
c3f34952
TB
3158 ||
3159 comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3160 {
524af0d6 3161 bool m;
c3f34952 3162 gfc_actual_arglist *arg_null = NULL;
6de9cd9a 3163
c3f34952
TB
3164 actual->expr = comp_tail->val;
3165 comp_tail->val = NULL;
6de9cd9a 3166
c3f34952
TB
3167 m = gfc_convert_to_structure_constructor (NULL,
3168 comp->ts.u.derived, &comp_tail->val,
3169 comp->ts.u.derived->attr.zero_comp
3170 ? &arg_null : &actual, true);
524af0d6 3171 if (!m)
c3f34952 3172 goto cleanup;
2eae3dc7 3173
c3f34952
TB
3174 if (comp->ts.u.derived->attr.zero_comp)
3175 {
3176 comp = comp->next;
3177 continue;
3178 }
3179 }
fa9290d3 3180
c3f34952
TB
3181 if (comp)
3182 comp = comp->next;
3183 if (parent && !comp)
3184 break;
fa9290d3 3185
792f7301
MM
3186 if (actual)
3187 actual = actual->next;
6de9cd9a
DN
3188 }
3189
524af0d6 3190 if (!build_actual_constructor (&comp_head, &ctor_head, sym))
7d1f1e61
PT
3191 goto cleanup;
3192
fa9290d3
DK
3193 /* No component should be left, as this should have caused an error in the
3194 loop constructing the component-list (name that does not correspond to any
3195 component in the structure definition). */
c3f34952 3196 if (comp_head && sym->attr.extension)
7d1f1e61
PT
3197 {
3198 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3199 {
a4d9b221 3200 gfc_error ("component %qs at %L has already been set by a "
7d1f1e61
PT
3201 "parent derived type constructor", comp_iter->name,
3202 &comp_iter->where);
3203 }
3204 goto cleanup;
3205 }
c3f34952
TB
3206 else
3207 gcc_assert (!comp_head);
fa9290d3 3208
c3f34952
TB
3209 if (parent)
3210 {
3211 expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3212 expr->ts.u.derived = sym;
3213 expr->value.constructor = ctor_head;
3214 *cexpr = expr;
3215 }
3216 else
3217 {
3218 expr->ts.u.derived = sym;
3219 expr->ts.kind = 0;
3220 expr->ts.type = BT_DERIVED;
3221 expr->value.constructor = ctor_head;
3222 expr->expr_type = EXPR_STRUCTURE;
3223 }
6de9cd9a 3224
bf1b77dd 3225 gfc_current_locus = old_locus;
c3f34952
TB
3226 if (parent)
3227 *arglist = actual;
524af0d6 3228 return true;
6de9cd9a 3229
c3f34952 3230 cleanup:
bf1b77dd 3231 gfc_current_locus = old_locus;
6de9cd9a 3232
fa9290d3
DK
3233 for (comp_iter = comp_head; comp_iter; )
3234 {
3235 gfc_structure_ctor_component *next = comp_iter->next;
3236 gfc_free_structure_ctor_component (comp_iter);
3237 comp_iter = next;
3238 }
b7e75771 3239 gfc_constructor_free (ctor_head);
c3f34952 3240
524af0d6 3241 return false;
c3f34952
TB
3242}
3243
3244
3245match
3246gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
3247{
3248 match m;
3249 gfc_expr *e;
3250 gfc_symtree *symtree;
3251
b64c3d06 3252 gfc_get_ha_sym_tree (sym->name, &symtree);
c3f34952
TB
3253
3254 e = gfc_get_expr ();
3255 e->symtree = symtree;
3256 e->expr_type = EXPR_FUNCTION;
2f00fb3d 3257 e->where = gfc_current_locus;
c3f34952 3258
f6288c24 3259 gcc_assert (gfc_fl_struct (sym->attr.flavor)
c3f34952
TB
3260 && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3261 e->value.function.esym = sym;
3262 e->symtree->n.sym->attr.generic = 1;
3263
49032565
SK
3264 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3265 if (m != MATCH_YES)
3266 {
3267 gfc_free_expr (e);
3268 return m;
3269 }
3270
3271 if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3272 {
3273 gfc_free_expr (e);
3274 return MATCH_ERROR;
3275 }
3276
586dc38b
SK
3277 /* If a structure constructor is in a DATA statement, then each entity
3278 in the structure constructor must be a constant. Try to reduce the
3279 expression here. */
3280 if (gfc_in_match_data ())
3281 gfc_reduce_init_expr (e);
5bab4c96 3282
49032565
SK
3283 *result = e;
3284 return MATCH_YES;
6de9cd9a
DN
3285}
3286
3287
9a3db5a3
PT
3288/* If the symbol is an implicit do loop index and implicitly typed,
3289 it should not be host associated. Provide a symtree from the
3290 current namespace. */
3291static match
3292check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3293{
3294 if ((*sym)->attr.flavor == FL_VARIABLE
3295 && (*sym)->ns != gfc_current_ns
3296 && (*sym)->attr.implied_index
3297 && (*sym)->attr.implicit_type
3298 && !(*sym)->attr.use_assoc)
3299 {
3300 int i;
08a6b8e0 3301 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
9a3db5a3
PT
3302 if (i)
3303 return MATCH_ERROR;
3304 *sym = (*st)->n.sym;
3305 }
3306 return MATCH_YES;
3307}
3308
3309
3070bab4
JW
3310/* Procedure pointer as function result: Replace the function symbol by the
3311 auto-generated hidden result variable named "ppr@". */
3312
524af0d6 3313static bool
3070bab4
JW
3314replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3315{
3316 /* Check for procedure pointer result variable. */
3317 if ((*sym)->attr.function && !(*sym)->attr.external
3318 && (*sym)->result && (*sym)->result != *sym
3319 && (*sym)->result->attr.proc_pointer
3320 && (*sym) == gfc_current_ns->proc_name
3321 && (*sym) == (*sym)->result->ns->proc_name
3322 && strcmp ("ppr@", (*sym)->result->name) == 0)
3323 {
3324 /* Automatic replacement with "hidden" result variable. */
3325 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3326 *sym = (*sym)->result;
3327 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
524af0d6 3328 return true;
3070bab4 3329 }
524af0d6 3330 return false;
3070bab4
JW
3331}
3332
3333
6de9cd9a
DN
3334/* Matches a variable name followed by anything that might follow it--
3335 array reference, argument list of a function, etc. */
3336
3337match
edf1eac2 3338gfc_match_rvalue (gfc_expr **result)
6de9cd9a
DN
3339{
3340 gfc_actual_arglist *actual_arglist;
d3fcc995 3341 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
3342 gfc_state_data *st;
3343 gfc_symbol *sym;
3344 gfc_symtree *symtree;
d3fcc995 3345 locus where, old_loc;
6de9cd9a 3346 gfc_expr *e;
d3fcc995 3347 match m, m2;
6de9cd9a 3348 int i;
5270c302
AL
3349 gfc_typespec *ts;
3350 bool implicit_char;
a99288e5 3351 gfc_ref *ref;
6de9cd9a 3352
cd714e1e
FR
3353 m = gfc_match ("%%loc");
3354 if (m == MATCH_YES)
3355 {
3356 if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3357 return MATCH_ERROR;
3358 strncpy (name, "loc", 4);
3359 }
3360
3361 else
3362 {
3363 m = gfc_match_name (name);
3364 if (m != MATCH_YES)
3365 return m;
3366 }
6de9cd9a 3367
f6288c24
FR
3368 /* Check if the symbol exists. */
3369 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
6de9cd9a
DN
3370 return MATCH_ERROR;
3371
f6288c24
FR
3372 /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3373 type. For derived types we create a generic symbol which links to the
3374 derived type symbol; STRUCTUREs are simpler and must not conflict with
3375 variables. */
3376 if (!symtree)
3377 if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3378 return MATCH_ERROR;
3379 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3380 {
3381 if (gfc_find_state (COMP_INTERFACE)
3382 && !gfc_current_ns->has_import_set)
3383 i = gfc_get_sym_tree (name, NULL, &symtree, false);
3384 else
3385 i = gfc_get_ha_sym_tree (name, &symtree);
3386 if (i)
3387 return MATCH_ERROR;
3388 }
3389
3390
6de9cd9a
DN
3391 sym = symtree->n.sym;
3392 e = NULL;
63645982 3393 where = gfc_current_locus;
6de9cd9a 3394
3070bab4
JW
3395 replace_hidden_procptr_result (&sym, &symtree);
3396
9a3db5a3
PT
3397 /* If this is an implicit do loop index and implicitly typed,
3398 it should not be host associated. */
3399 m = check_for_implicit_index (&symtree, &sym);
3400 if (m != MATCH_YES)
3401 return m;
3402
6de9cd9a 3403 gfc_set_sym_referenced (sym);
9a3db5a3 3404 sym->attr.implied_index = 0;
6de9cd9a 3405
0921bc44
JJ
3406 if (sym->attr.function && sym->result == sym)
3407 {
811849c0
PT
3408 /* See if this is a directly recursive function call. */
3409 gfc_gobble_whitespace ();
3410 if (sym->attr.recursive
8fc541d3 3411 && gfc_peek_ascii_char () == '('
fc2d8680
PT
3412 && gfc_current_ns->proc_name == sym
3413 && !sym->attr.dimension)
811849c0 3414 {
a4d9b221 3415 gfc_error ("%qs at %C is the name of a recursive function "
fc2d8680
PT
3416 "and so refers to the result variable. Use an "
3417 "explicit RESULT variable for direct recursion "
3418 "(12.5.2.1)", sym->name);
811849c0
PT
3419 return MATCH_ERROR;
3420 }
fc2d8680 3421
2d71b918 3422 if (gfc_is_function_return_value (sym, gfc_current_ns))
0921bc44
JJ
3423 goto variable;
3424
3425 if (sym->attr.entry
3426 && (sym->ns == gfc_current_ns
3427 || sym->ns == gfc_current_ns->parent))
3428 {
3429 gfc_entry_list *el = NULL;
bf1b77dd 3430
0921bc44
JJ
3431 for (el = sym->ns->entries; el; el = el->next)
3432 if (sym == el->sym)
3433 goto variable;
3434 }
3435 }
6de9cd9a 3436
8fb74da4
JW
3437 if (gfc_matching_procptr_assignment)
3438 goto procptr0;
3439
6de9cd9a
DN
3440 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3441 goto function0;
3442
3443 if (sym->attr.generic)
3444 goto generic_function;
3445
3446 switch (sym->attr.flavor)
3447 {
3448 case FL_VARIABLE:
3449 variable:
6de9cd9a
DN
3450 e = gfc_get_expr ();
3451
3452 e->expr_type = EXPR_VARIABLE;
3453 e->symtree = symtree;
3454
713485cc 3455 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3456 break;
3457
3458 case FL_PARAMETER:
b7263e8f 3459 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
bf1b77dd 3460 end up here. Unfortunately, sym->value->expr_type is set to
b7263e8f
EE
3461 EXPR_CONSTANT, and so the if () branch would be followed without
3462 the !sym->as check. */
3463 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
6de9cd9a
DN
3464 e = gfc_copy_expr (sym->value);
3465 else
3466 {
3467 e = gfc_get_expr ();
3468 e->expr_type = EXPR_VARIABLE;
3469 }
3470
3471 e->symtree = symtree;
713485cc 3472 m = gfc_match_varspec (e, 0, false, true);
a99288e5
PT
3473
3474 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3475 break;
3476
927171bf
PT
3477 /* Variable array references to derived type parameters cause
3478 all sorts of headaches in simplification. Treating such
3479 expressions as variable works just fine for all array
3480 references. */
3481 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
a99288e5
PT
3482 {
3483 for (ref = e->ref; ref; ref = ref->next)
3484 if (ref->type == REF_ARRAY)
3485 break;
3486
927171bf 3487 if (ref == NULL || ref->u.ar.type == AR_FULL)
a99288e5
PT
3488 break;
3489
3490 ref = e->ref;
3491 e->ref = NULL;
3492 gfc_free_expr (e);
3493 e = gfc_get_expr ();
3494 e->expr_type = EXPR_VARIABLE;
3495 e->symtree = symtree;
3496 e->ref = ref;
a99288e5
PT
3497 }
3498
6de9cd9a
DN
3499 break;
3500
f6288c24 3501 case FL_STRUCT:
6de9cd9a
DN
3502 case FL_DERIVED:
3503 sym = gfc_use_derived (sym);
3504 if (sym == NULL)
3505 m = MATCH_ERROR;
3506 else
c3f34952 3507 goto generic_function;
6de9cd9a
DN
3508 break;
3509
3510 /* If we're here, then the name is known to be the name of a
3511 procedure, yet it is not sure to be the name of a function. */
3512 case FL_PROCEDURE:
8fb74da4 3513
1cc0e193 3514 /* Procedure Pointer Assignments. */
8fb74da4
JW
3515 procptr0:
3516 if (gfc_matching_procptr_assignment)
3517 {
3518 gfc_gobble_whitespace ();
e35bbb23 3519 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
8fb74da4
JW
3520 /* Parse functions returning a procptr. */
3521 goto function0;
3522
8fb74da4
JW
3523 e = gfc_get_expr ();
3524 e->expr_type = EXPR_VARIABLE;
3525 e->symtree = symtree;
713485cc 3526 m = gfc_match_varspec (e, 0, false, true);
2dda89a8
JW
3527 if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3528 && sym->ts.type == BT_UNKNOWN
524af0d6 3529 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
2dda89a8
JW
3530 {
3531 m = MATCH_ERROR;
3532 break;
3533 }
8fb74da4
JW
3534 break;
3535 }
3536
6de9cd9a
DN
3537 if (sym->attr.subroutine)
3538 {
a4d9b221 3539 gfc_error ("Unexpected use of subroutine name %qs at %C",
6de9cd9a
DN
3540 sym->name);
3541 m = MATCH_ERROR;
3542 break;
3543 }
3544
3545 /* At this point, the name has to be a non-statement function.
edf1eac2
SK
3546 If the name is the same as the current function being
3547 compiled, then we have a variable reference (to the function
3548 result) if the name is non-recursive. */
6de9cd9a
DN
3549
3550 st = gfc_enclosing_unit (NULL);
3551
4668d6f9
PT
3552 if (st != NULL
3553 && st->state == COMP_FUNCTION
6de9cd9a
DN
3554 && st->sym == sym
3555 && !sym->attr.recursive)
3556 {
3557 e = gfc_get_expr ();
3558 e->symtree = symtree;
3559 e->expr_type = EXPR_VARIABLE;
3560
713485cc 3561 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3562 break;
3563 }
3564
3565 /* Match a function reference. */
3566 function0:
3567 m = gfc_match_actual_arglist (0, &actual_arglist);
3568 if (m == MATCH_NO)
3569 {
3570 if (sym->attr.proc == PROC_ST_FUNCTION)
a4d9b221 3571 gfc_error ("Statement function %qs requires argument list at %C",
6de9cd9a
DN
3572 sym->name);
3573 else
a4d9b221 3574 gfc_error ("Function %qs requires an argument list at %C",
6de9cd9a
DN
3575 sym->name);
3576
3577 m = MATCH_ERROR;
3578 break;
3579 }
3580
3581 if (m != MATCH_YES)
3582 {
3583 m = MATCH_ERROR;
3584 break;
3585 }
3586
3587 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
3588 sym = symtree->n.sym;
3589
3070bab4
JW
3590 replace_hidden_procptr_result (&sym, &symtree);
3591
6de9cd9a
DN
3592 e = gfc_get_expr ();
3593 e->symtree = symtree;
3594 e->expr_type = EXPR_FUNCTION;
3595 e->value.function.actual = actual_arglist;
63645982 3596 e->where = gfc_current_locus;
6de9cd9a 3597
102344e2
TB
3598 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
3599 && CLASS_DATA (sym)->as)
3600 e->rank = CLASS_DATA (sym)->as->rank;
3601 else if (sym->as != NULL)
6de9cd9a
DN
3602 e->rank = sym->as->rank;
3603
3604 if (!sym->attr.function
524af0d6 3605 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3606 {
3607 m = MATCH_ERROR;
3608 break;
3609 }
3610
a8b3b0b6
CR
3611 /* Check here for the existence of at least one argument for the
3612 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
3613 argument(s) given will be checked in gfc_iso_c_func_interface,
3614 during resolution of the function call. */
3615 if (sym->attr.is_iso_c == 1
3616 && (sym->from_intmod == INTMOD_ISO_C_BINDING
3617 && (sym->intmod_sym_id == ISOCBINDING_LOC
3618 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
3619 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
3620 {
3621 /* make sure we were given a param */
3622 if (actual_arglist == NULL)
3623 {
a4d9b221 3624 gfc_error ("Missing argument to %qs at %C", sym->name);
a8b3b0b6
CR
3625 m = MATCH_ERROR;
3626 break;
3627 }
3628 }
3629
6de9cd9a
DN
3630 if (sym->result == NULL)
3631 sym->result = sym;
3632
20fee4a9
JW
3633 gfc_gobble_whitespace ();
3634 /* F08:C612. */
3635 if (gfc_peek_ascii_char() == '%')
3636 {
67914693 3637 gfc_error ("The leftmost part-ref in a data-ref cannot be a "
20fee4a9
JW
3638 "function reference at %C");
3639 m = MATCH_ERROR;
3640 }
3641
6de9cd9a
DN
3642 m = MATCH_YES;
3643 break;
3644
3645 case FL_UNKNOWN:
3646
3647 /* Special case for derived type variables that get their types
edf1eac2
SK
3648 via an IMPLICIT statement. This can't wait for the
3649 resolution phase. */
6de9cd9a 3650
f6288c24
FR
3651 old_loc = gfc_current_locus;
3652 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 3653 && sym->ts.type == BT_UNKNOWN
713485cc 3654 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
6de9cd9a 3655 gfc_set_default_type (sym, 0, sym->ns);
f6288c24 3656 gfc_current_locus = old_loc;
6de9cd9a 3657
492792ed 3658 /* If the symbol has a (co)dimension attribute, the expression is a
edf1eac2 3659 variable. */
6de9cd9a 3660
492792ed 3661 if (sym->attr.dimension || sym->attr.codimension)
6de9cd9a 3662 {
524af0d6 3663 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3664 {
3665 m = MATCH_ERROR;
3666 break;
3667 }
3668
3669 e = gfc_get_expr ();
3670 e->symtree = symtree;
3671 e->expr_type = EXPR_VARIABLE;
713485cc 3672 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3673 break;
3674 }
3675
cd99c23c 3676 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
492792ed
TB
3677 && (CLASS_DATA (sym)->attr.dimension
3678 || CLASS_DATA (sym)->attr.codimension))
c49ea23d 3679 {
524af0d6 3680 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
c49ea23d
PT
3681 {
3682 m = MATCH_ERROR;
3683 break;
3684 }
3685
3686 e = gfc_get_expr ();
3687 e->symtree = symtree;
3688 e->expr_type = EXPR_VARIABLE;
3689 m = gfc_match_varspec (e, 0, false, true);
3690 break;
3691 }
3692
6de9cd9a 3693 /* Name is not an array, so we peek to see if a '(' implies a
edf1eac2
SK
3694 function call or a substring reference. Otherwise the
3695 variable is just a scalar. */
6de9cd9a
DN
3696
3697 gfc_gobble_whitespace ();
8fc541d3 3698 if (gfc_peek_ascii_char () != '(')
6de9cd9a
DN
3699 {
3700 /* Assume a scalar variable */
3701 e = gfc_get_expr ();
3702 e->symtree = symtree;
3703 e->expr_type = EXPR_VARIABLE;
3704
524af0d6 3705 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3706 {
3707 m = MATCH_ERROR;
3708 break;
3709 }
3710
8e1f752a 3711 /*FIXME:??? gfc_match_varspec does set this for us: */
6de9cd9a 3712 e->ts = sym->ts;
713485cc 3713 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3714 break;
3715 }
3716
d3fcc995
TS
3717 /* See if this is a function reference with a keyword argument
3718 as first argument. We do this because otherwise a spurious
3719 symbol would end up in the symbol table. */
3720
3721 old_loc = gfc_current_locus;
3722 m2 = gfc_match (" ( %n =", argname);
3723 gfc_current_locus = old_loc;
6de9cd9a
DN
3724
3725 e = gfc_get_expr ();
3726 e->symtree = symtree;
3727
d3fcc995 3728 if (m2 != MATCH_YES)
6de9cd9a 3729 {
5270c302 3730 /* Try to figure out whether we're dealing with a character type.
bf1b77dd 3731 We're peeking ahead here, because we don't want to call
5270c302
AL
3732 match_substring if we're dealing with an implicitly typed
3733 non-character variable. */
3734 implicit_char = false;
3735 if (sym->ts.type == BT_UNKNOWN)
3736 {
713485cc 3737 ts = gfc_get_default_type (sym->name, NULL);
5270c302
AL
3738 if (ts->type == BT_CHARACTER)
3739 implicit_char = true;
3740 }
3741
d3fcc995
TS
3742 /* See if this could possibly be a substring reference of a name
3743 that we're not sure is a variable yet. */
6de9cd9a 3744
5270c302 3745 if ((implicit_char || sym->ts.type == BT_CHARACTER)
38217d3e 3746 && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
6de9cd9a 3747 {
6de9cd9a 3748
d3fcc995
TS
3749 e->expr_type = EXPR_VARIABLE;
3750
3751 if (sym->attr.flavor != FL_VARIABLE
bf1b77dd 3752 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
524af0d6 3753 sym->name, NULL))
d3fcc995
TS
3754 {
3755 m = MATCH_ERROR;
3756 break;
3757 }
3758
3759 if (sym->ts.type == BT_UNKNOWN
524af0d6 3760 && !gfc_set_default_type (sym, 1, NULL))
d3fcc995
TS
3761 {
3762 m = MATCH_ERROR;
3763 break;
3764 }
3765
3766 e->ts = sym->ts;
860c8f3b 3767 if (e->ref)
bc21d315 3768 e->ts.u.cl = NULL;
d3fcc995 3769 m = MATCH_YES;
6de9cd9a
DN
3770 break;
3771 }
6de9cd9a
DN
3772 }
3773
3774 /* Give up, assume we have a function. */
3775
08a6b8e0 3776 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3777 sym = symtree->n.sym;
3778 e->expr_type = EXPR_FUNCTION;
3779
3780 if (!sym->attr.function
524af0d6 3781 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3782 {
3783 m = MATCH_ERROR;
3784 break;
3785 }
3786
3787 sym->result = sym;
3788
3789 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3790 if (m == MATCH_NO)
a4d9b221 3791 gfc_error ("Missing argument list in function %qs at %C", sym->name);
6de9cd9a
DN
3792
3793 if (m != MATCH_YES)
3794 {
3795 m = MATCH_ERROR;
3796 break;
3797 }
3798
3799 /* If our new function returns a character, array or structure
edf1eac2 3800 type, it might have subsequent references. */
6de9cd9a 3801
713485cc 3802 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3803 if (m == MATCH_NO)
3804 m = MATCH_YES;
3805
3806 break;
3807
3808 generic_function:
f6288c24
FR
3809 /* Look for symbol first; if not found, look for STRUCTURE type symbol
3810 specially. Creates a generic symbol for derived types. */
3811 gfc_find_sym_tree (name, NULL, 1, &symtree);
3812 if (!symtree)
3813 gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
3814 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3815 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3816
3817 e = gfc_get_expr ();
3818 e->symtree = symtree;
3819 e->expr_type = EXPR_FUNCTION;
3820
f6288c24 3821 if (gfc_fl_struct (sym->attr.flavor))
c3f34952
TB
3822 {
3823 e->value.function.esym = sym;
3824 e->symtree->n.sym->attr.generic = 1;
3825 }
3826
6de9cd9a
DN
3827 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3828 break;
3829
c0f0e35a
JD
3830 case FL_NAMELIST:
3831 m = MATCH_ERROR;
3832 break;
3833
6de9cd9a
DN
3834 default:
3835 gfc_error ("Symbol at %C is not appropriate for an expression");
3836 return MATCH_ERROR;
3837 }
3838
3839 if (m == MATCH_YES)
3840 {
3841 e->where = where;
3842 *result = e;
3843 }
3844 else
3845 gfc_free_expr (e);
3846
3847 return m;
3848}
3849
3850
df2fba9e 3851/* Match a variable, i.e. something that can be assigned to. This
6de9cd9a
DN
3852 starts as a symbol, can be a structure component or an array
3853 reference. It can be a function if the function doesn't have a
3854 separate RESULT variable. If the symbol has not been previously
30aabb86 3855 seen, we assume it is a variable.
6de9cd9a 3856
30aabb86
PT
3857 This function is called by two interface functions:
3858 gfc_match_variable, which has host_flag = 1, and
3859 gfc_match_equiv_variable, with host_flag = 0, to restrict the
3860 match of the symbol to the local scope. */
3861
3862static match
edf1eac2 3863match_variable (gfc_expr **result, int equiv_flag, int host_flag)
6de9cd9a 3864{
f6288c24 3865 gfc_symbol *sym, *dt_sym;
6de9cd9a
DN
3866 gfc_symtree *st;
3867 gfc_expr *expr;
f6288c24 3868 locus where, old_loc;
6de9cd9a
DN
3869 match m;
3870
fd2aa7ad
PT
3871 /* Since nothing has any business being an lvalue in a module
3872 specification block, an interface block or a contains section,
3873 we force the changed_symbols mechanism to work by setting
3874 host_flag to 0. This prevents valid symbols that have the name
3875 of keywords, such as 'end', being turned into variables by
df2fba9e 3876 failed matching to assignments for, e.g., END INTERFACE. */
fd2aa7ad 3877 if (gfc_current_state () == COMP_MODULE
4668d6f9 3878 || gfc_current_state () == COMP_SUBMODULE
fd2aa7ad
PT
3879 || gfc_current_state () == COMP_INTERFACE
3880 || gfc_current_state () == COMP_CONTAINS)
3881 host_flag = 0;
3882
618f4f46 3883 where = gfc_current_locus;
30aabb86 3884 m = gfc_match_sym_tree (&st, host_flag);
6de9cd9a
DN
3885 if (m != MATCH_YES)
3886 return m;
6de9cd9a
DN
3887
3888 sym = st->n.sym;
9a3db5a3
PT
3889
3890 /* If this is an implicit do loop index and implicitly typed,
3891 it should not be host associated. */
3892 m = check_for_implicit_index (&st, &sym);
3893 if (m != MATCH_YES)
3894 return m;
3895
3896 sym->attr.implied_index = 0;
3897
6de9cd9a 3898 gfc_set_sym_referenced (sym);
f6288c24
FR
3899
3900 /* STRUCTUREs may share names with variables, but derived types may not. */
3901 if (sym->attr.flavor == FL_PROCEDURE && sym->generic
3902 && (dt_sym = gfc_find_dt_in_generic (sym)))
3903 {
3904 if (dt_sym->attr.flavor == FL_DERIVED)
2f029c08 3905 gfc_error ("Derived type %qs cannot be used as a variable at %C",
f6288c24
FR
3906 sym->name);
3907 return MATCH_ERROR;
3908 }
3909
6de9cd9a
DN
3910 switch (sym->attr.flavor)
3911 {
3912 case FL_VARIABLE:
8c91ab34 3913 /* Everything is alright. */
6de9cd9a
DN
3914 break;
3915
3916 case FL_UNKNOWN:
d7e2fcd0
TB
3917 {
3918 sym_flavor flavor = FL_UNKNOWN;
3919
3920 gfc_gobble_whitespace ();
3921
3922 if (sym->attr.external || sym->attr.procedure
3923 || sym->attr.function || sym->attr.subroutine)
3924 flavor = FL_PROCEDURE;
b9332b09
PT
3925
3926 /* If it is not a procedure, is not typed and is host associated,
3927 we cannot give it a flavor yet. */
3928 else if (sym->ns == gfc_current_ns->parent
3929 && sym->ts.type == BT_UNKNOWN)
3930 break;
3931
3932 /* These are definitive indicators that this is a variable. */
8fc541d3 3933 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
d7e2fcd0
TB
3934 || sym->attr.pointer || sym->as != NULL)
3935 flavor = FL_VARIABLE;
3936
3937 if (flavor != FL_UNKNOWN
524af0d6 3938 && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
d7e2fcd0
TB
3939 return MATCH_ERROR;
3940 }
6de9cd9a
DN
3941 break;
3942
5056a350
SK
3943 case FL_PARAMETER:
3944 if (equiv_flag)
8c91ab34
DK
3945 {
3946 gfc_error ("Named constant at %C in an EQUIVALENCE");
3947 return MATCH_ERROR;
3948 }
3949 /* Otherwise this is checked for and an error given in the
3950 variable definition context checks. */
5056a350
SK
3951 break;
3952
6de9cd9a 3953 case FL_PROCEDURE:
01d2a7d7
DF
3954 /* Check for a nonrecursive function result variable. */
3955 if (sym->attr.function
8c91ab34
DK
3956 && !sym->attr.external
3957 && sym->result == sym
3958 && (gfc_is_function_return_value (sym, gfc_current_ns)
3959 || (sym->attr.entry
3960 && sym->ns == gfc_current_ns)
3961 || (sym->attr.entry
3962 && sym->ns == gfc_current_ns->parent)))
6de9cd9a 3963 {
6de9cd9a
DN
3964 /* If a function result is a derived type, then the derived
3965 type may still have to be resolved. */
3966
3967 if (sym->ts.type == BT_DERIVED
bc21d315 3968 && gfc_use_derived (sym->ts.u.derived) == NULL)
6de9cd9a 3969 return MATCH_ERROR;
6de9cd9a
DN
3970 break;
3971 }
3972
3070bab4 3973 if (sym->attr.proc_pointer
524af0d6 3974 || replace_hidden_procptr_result (&sym, &st))
8fb74da4
JW
3975 break;
3976
6de9cd9a 3977 /* Fall through to error */
81fea426 3978 gcc_fallthrough ();
6de9cd9a
DN
3979
3980 default:
a4d9b221 3981 gfc_error ("%qs at %C is not a variable", sym->name);
6de9cd9a
DN
3982 return MATCH_ERROR;
3983 }
3984
0dd973dd
PB
3985 /* Special case for derived type variables that get their types
3986 via an IMPLICIT statement. This can't wait for the
3987 resolution phase. */
3988
3989 {
3990 gfc_namespace * implicit_ns;
3991
3992 if (gfc_current_ns->proc_name == sym)
3993 implicit_ns = gfc_current_ns;
3994 else
3995 implicit_ns = sym->ns;
5bab4c96 3996
f6288c24
FR
3997 old_loc = gfc_current_locus;
3998 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 3999 && sym->ts.type == BT_UNKNOWN
713485cc 4000 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
0dd973dd 4001 gfc_set_default_type (sym, 0, implicit_ns);
f6288c24 4002 gfc_current_locus = old_loc;
0dd973dd
PB
4003 }
4004
6de9cd9a
DN
4005 expr = gfc_get_expr ();
4006
4007 expr->expr_type = EXPR_VARIABLE;
4008 expr->symtree = st;
4009 expr->ts = sym->ts;
4010 expr->where = where;
4011
4012 /* Now see if we have to do more. */
713485cc 4013 m = gfc_match_varspec (expr, equiv_flag, false, false);
6de9cd9a
DN
4014 if (m != MATCH_YES)
4015 {
4016 gfc_free_expr (expr);
4017 return m;
4018 }
4019
4020 *result = expr;
4021 return MATCH_YES;
4022}
30aabb86 4023
edf1eac2 4024
30aabb86 4025match
edf1eac2 4026gfc_match_variable (gfc_expr **result, int equiv_flag)
30aabb86
PT
4027{
4028 return match_variable (result, equiv_flag, 1);
4029}
4030
edf1eac2 4031
30aabb86 4032match
edf1eac2 4033gfc_match_equiv_variable (gfc_expr **result)
30aabb86
PT
4034{
4035 return match_variable (result, 1, 0);
4036}
4037