]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
re PR libgomp/91473 (Test case libgomp.fortran/appendix-a/a.28.5.f90 is invalid)
[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:
bf1b77dd
PT
2600 /* If any of start, end or stride is not integer, there will
2601 already have been an error issued. */
16acb1a8
DH
2602 int errors;
2603 gfc_get_errors (NULL, &errors);
2604 if (errors == 0)
bf1b77dd 2605 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
6de9cd9a
DN
2606 }
2607
2608 break;
2609
2610 case REF_COMPONENT:
cf2b3c22
TB
2611 comp = ref->u.c.component;
2612 attr = comp->attr;
a5fbc2f3 2613 if (ts != NULL && !has_inquiry_part)
e8a25349 2614 {
cf2b3c22 2615 *ts = comp->ts;
e8a25349
TS
2616 /* Don't set the string length if a substring reference
2617 follows. */
2618 if (ts->type == BT_CHARACTER
2619 && ref->next && ref->next->type == REF_SUBSTRING)
bc21d315 2620 ts->u.cl = NULL;
e8a25349 2621 }
6de9cd9a 2622
cf2b3c22
TB
2623 if (comp->ts.type == BT_CLASS)
2624 {
83ba23b7 2625 codimension = CLASS_DATA (comp)->attr.codimension;
d40477b4 2626 pointer = CLASS_DATA (comp)->attr.class_pointer;
7a08eda1 2627 allocatable = CLASS_DATA (comp)->attr.allocatable;
cf2b3c22
TB
2628 }
2629 else
2630 {
83ba23b7 2631 codimension = comp->attr.codimension;
cf2b3c22
TB
2632 pointer = comp->attr.pointer;
2633 allocatable = comp->attr.allocatable;
2634 }
713485cc 2635 if (pointer || attr.proc_pointer)
6de9cd9a
DN
2636 target = 1;
2637
2638 break;
2639
a5fbc2f3 2640 case REF_INQUIRY:
6de9cd9a 2641 case REF_SUBSTRING:
5046aff5 2642 allocatable = pointer = 0;
6de9cd9a
DN
2643 break;
2644 }
2645
2646 attr.dimension = dimension;
83ba23b7 2647 attr.codimension = codimension;
6de9cd9a 2648 attr.pointer = pointer;
5046aff5 2649 attr.allocatable = allocatable;
6de9cd9a 2650 attr.target = target;
80f95228 2651 attr.save = sym->attr.save;
6de9cd9a
DN
2652
2653 return attr;
2654}
2655
2656
2657/* Return the attribute from a general expression. */
2658
2659symbol_attribute
edf1eac2 2660gfc_expr_attr (gfc_expr *e)
6de9cd9a
DN
2661{
2662 symbol_attribute attr;
2663
2664 switch (e->expr_type)
2665 {
2666 case EXPR_VARIABLE:
2667 attr = gfc_variable_attr (e, NULL);
2668 break;
2669
2670 case EXPR_FUNCTION:
2671 gfc_clear_attr (&attr);
2672
50c7654b 2673 if (e->value.function.esym && e->value.function.esym->result)
cf2b3c22
TB
2674 {
2675 gfc_symbol *sym = e->value.function.esym->result;
2676 attr = sym->attr;
2677 if (sym->ts.type == BT_CLASS)
2678 {
7a08eda1 2679 attr.dimension = CLASS_DATA (sym)->attr.dimension;
d40477b4 2680 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
7a08eda1 2681 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
cf2b3c22
TB
2682 }
2683 }
574284e9
AV
2684 else if (e->value.function.isym
2685 && e->value.function.isym->transformational
2686 && e->ts.type == BT_CLASS)
2687 attr = CLASS_DATA (e)->attr;
50dbf0b4
JW
2688 else
2689 attr = gfc_variable_attr (e, NULL);
6de9cd9a
DN
2690
2691 /* TODO: NULL() returns pointers. May have to take care of this
edf1eac2 2692 here. */
6de9cd9a
DN
2693
2694 break;
2695
2696 default:
3c9f5092
AV
2697 gfc_clear_attr (&attr);
2698 break;
2699 }
2700
2701 return attr;
2702}
2703
2704
2705/* Given an expression, figure out what the ultimate expression
2706 attribute is. This routine is similar to gfc_variable_attr with
2707 parts of gfc_expr_attr, but focuses more on the needs of
2708 coarrays. For coarrays a codimension attribute is kind of
ba85c8c3
AV
2709 "infectious" being propagated once set and never cleared.
2710 The coarray_comp is only set, when the expression refs a coarray
2711 component. REFS_COMP is set when present to true only, when this EXPR
2712 refs a (non-_data) component. To check whether EXPR refs an allocatable
2713 component in a derived type coarray *refs_comp needs to be set and
2714 coarray_comp has to false. */
3c9f5092
AV
2715
2716static symbol_attribute
ba85c8c3 2717caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3c9f5092 2718{
de91486c 2719 int dimension, codimension, pointer, allocatable, target, coarray_comp;
3c9f5092
AV
2720 symbol_attribute attr;
2721 gfc_ref *ref;
2722 gfc_symbol *sym;
2723 gfc_component *comp;
2724
2725 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2726 gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
2727
2728 sym = expr->symtree->n.sym;
2729 gfc_clear_attr (&attr);
2730
ba85c8c3 2731 if (refs_comp)
525a5e33 2732 *refs_comp = false;
ba85c8c3 2733
3c9f5092
AV
2734 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2735 {
2736 dimension = CLASS_DATA (sym)->attr.dimension;
2737 codimension = CLASS_DATA (sym)->attr.codimension;
2738 pointer = CLASS_DATA (sym)->attr.class_pointer;
2739 allocatable = CLASS_DATA (sym)->attr.allocatable;
de91486c
AV
2740 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2741 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3c9f5092
AV
2742 }
2743 else
2744 {
2745 dimension = sym->attr.dimension;
2746 codimension = sym->attr.codimension;
2747 pointer = sym->attr.pointer;
2748 allocatable = sym->attr.allocatable;
de91486c 2749 attr.alloc_comp = sym->ts.type == BT_DERIVED
3c9f5092 2750 ? sym->ts.u.derived->attr.alloc_comp : 0;
de91486c
AV
2751 attr.pointer_comp = sym->ts.type == BT_DERIVED
2752 ? sym->ts.u.derived->attr.pointer_comp : 0;
3c9f5092
AV
2753 }
2754
ba85c8c3 2755 target = coarray_comp = 0;
3c9f5092
AV
2756 if (pointer || attr.proc_pointer)
2757 target = 1;
2758
2759 for (ref = expr->ref; ref; ref = ref->next)
2760 switch (ref->type)
2761 {
2762 case REF_ARRAY:
2763
2764 switch (ref->u.ar.type)
2765 {
2766 case AR_FULL:
2767 case AR_SECTION:
2768 dimension = 1;
2769 break;
2770
2771 case AR_ELEMENT:
2772 /* Handle coarrays. */
2773 if (ref->u.ar.dimen > 0 && !in_allocate)
2774 allocatable = pointer = 0;
2775 break;
2776
2777 case AR_UNKNOWN:
2778 /* If any of start, end or stride is not integer, there will
2779 already have been an error issued. */
2780 int errors;
2781 gfc_get_errors (NULL, &errors);
2782 if (errors == 0)
2783 gfc_internal_error ("gfc_caf_attr(): Bad array reference");
2784 }
2785
2786 break;
2787
2788 case REF_COMPONENT:
2789 comp = ref->u.c.component;
2790
2791 if (comp->ts.type == BT_CLASS)
2792 {
ba85c8c3
AV
2793 /* Set coarray_comp only, when this component introduces the
2794 coarray. */
2795 coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3c9f5092
AV
2796 codimension |= CLASS_DATA (comp)->attr.codimension;
2797 pointer = CLASS_DATA (comp)->attr.class_pointer;
2798 allocatable = CLASS_DATA (comp)->attr.allocatable;
3c9f5092
AV
2799 }
2800 else
2801 {
ba85c8c3
AV
2802 /* Set coarray_comp only, when this component introduces the
2803 coarray. */
2804 coarray_comp = !codimension && comp->attr.codimension;
3c9f5092
AV
2805 codimension |= comp->attr.codimension;
2806 pointer = comp->attr.pointer;
2807 allocatable = comp->attr.allocatable;
3c9f5092
AV
2808 }
2809
525a5e33
AV
2810 if (refs_comp && strcmp (comp->name, "_data") != 0
2811 && (ref->next == NULL
2812 || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
2813 *refs_comp = true;
ba85c8c3 2814
3c9f5092
AV
2815 if (pointer || attr.proc_pointer)
2816 target = 1;
2817
2818 break;
2819
2820 case REF_SUBSTRING:
a5fbc2f3 2821 case REF_INQUIRY:
3c9f5092
AV
2822 allocatable = pointer = 0;
2823 break;
2824 }
2825
2826 attr.dimension = dimension;
2827 attr.codimension = codimension;
2828 attr.pointer = pointer;
2829 attr.allocatable = allocatable;
2830 attr.target = target;
2831 attr.save = sym->attr.save;
2832 attr.coarray_comp = coarray_comp;
3c9f5092
AV
2833
2834 return attr;
2835}
2836
2837
2838symbol_attribute
ba85c8c3 2839gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3c9f5092
AV
2840{
2841 symbol_attribute attr;
2842
2843 switch (e->expr_type)
2844 {
2845 case EXPR_VARIABLE:
ba85c8c3 2846 attr = caf_variable_attr (e, in_allocate, refs_comp);
3c9f5092
AV
2847 break;
2848
2849 case EXPR_FUNCTION:
2850 gfc_clear_attr (&attr);
2851
2852 if (e->value.function.esym && e->value.function.esym->result)
2853 {
2854 gfc_symbol *sym = e->value.function.esym->result;
2855 attr = sym->attr;
2856 if (sym->ts.type == BT_CLASS)
2857 {
2858 attr.dimension = CLASS_DATA (sym)->attr.dimension;
2859 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2860 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2861 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
de91486c
AV
2862 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
2863 ->attr.pointer_comp;
3c9f5092
AV
2864 }
2865 }
2866 else if (e->symtree)
ba85c8c3 2867 attr = caf_variable_attr (e, in_allocate, refs_comp);
3c9f5092
AV
2868 else
2869 gfc_clear_attr (&attr);
2870 break;
2871
2872 default:
6de9cd9a
DN
2873 gfc_clear_attr (&attr);
2874 break;
2875 }
2876
2877 return attr;
2878}
2879
2880
2881/* Match a structure constructor. The initial symbol has already been
2882 seen. */
2883
fa9290d3
DK
2884typedef struct gfc_structure_ctor_component
2885{
2886 char* name;
2887 gfc_expr* val;
2888 locus where;
2889 struct gfc_structure_ctor_component* next;
2890}
2891gfc_structure_ctor_component;
2892
ece3f663 2893#define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
fa9290d3
DK
2894
2895static void
2896gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2897{
cede9502 2898 free (comp->name);
fa9290d3 2899 gfc_free_expr (comp->val);
cede9502 2900 free (comp);
fa9290d3
DK
2901}
2902
7d1f1e61
PT
2903
2904/* Translate the component list into the actual constructor by sorting it in
2905 the order required; this also checks along the way that each and every
2906 component actually has an initializer and handles default initializers
2907 for components without explicit value given. */
524af0d6 2908static bool
7d1f1e61 2909build_actual_constructor (gfc_structure_ctor_component **comp_head,
b7e75771 2910 gfc_constructor_base *ctor_head, gfc_symbol *sym)
6de9cd9a 2911{
fa9290d3 2912 gfc_structure_ctor_component *comp_iter;
7d1f1e61
PT
2913 gfc_component *comp;
2914
2915 for (comp = sym->components; comp; comp = comp->next)
2916 {
2917 gfc_structure_ctor_component **next_ptr;
2918 gfc_expr *value = NULL;
2919
2920 /* Try to find the initializer for the current component by name. */
2921 next_ptr = comp_head;
2922 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2923 {
2924 if (!strcmp (comp_iter->name, comp->name))
2925 break;
2926 next_ptr = &comp_iter->next;
2927 }
2928
2929 /* If an extension, try building the parent derived type by building
2930 a value expression for the parent derived type and calling self. */
2931 if (!comp_iter && comp == sym->components && sym->attr.extension)
2932 {
b7e75771
JD
2933 value = gfc_get_structure_constructor_expr (comp->ts.type,
2934 comp->ts.kind,
2935 &gfc_current_locus);
7d1f1e61 2936 value->ts = comp->ts;
7d1f1e61 2937
bf1b77dd
PT
2938 if (!build_actual_constructor (comp_head,
2939 &value->value.constructor,
524af0d6 2940 comp->ts.u.derived))
7d1f1e61
PT
2941 {
2942 gfc_free_expr (value);
524af0d6 2943 return false;
7d1f1e61 2944 }
b7e75771
JD
2945
2946 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2947 continue;
2948 }
2949
2950 /* If it was not found, try the default initializer if there's any;
2b3dc0db 2951 otherwise, it's an error unless this is a deferred parameter. */
7d1f1e61
PT
2952 if (!comp_iter)
2953 {
2954 if (comp->initializer)
2955 {
524af0d6
JB
2956 if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
2957 "with missing optional arguments at %C"))
2958 return false;
7d1f1e61
PT
2959 value = gfc_copy_expr (comp->initializer);
2960 }
9b548517
AV
2961 else if (comp->attr.allocatable
2962 || (comp->ts.type == BT_CLASS
2963 && CLASS_DATA (comp)->attr.allocatable))
7430df97
JW
2964 {
2965 if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
2f029c08 2966 "allocatable component %qs given in the "
9b548517 2967 "structure constructor at %C", comp->name))
7430df97
JW
2968 return false;
2969 }
9b548517 2970 else if (!comp->attr.artificial)
7d1f1e61 2971 {
a4d9b221 2972 gfc_error ("No initializer for component %qs given in the"
546c8974 2973 " structure constructor at %C", comp->name);
524af0d6 2974 return false;
7d1f1e61
PT
2975 }
2976 }
2977 else
2978 value = comp_iter->val;
2979
2980 /* Add the value to the constructor chain built. */
b7e75771 2981 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2982
2983 /* Remove the entry from the component list. We don't want the expression
2984 value to be free'd, so set it to NULL. */
2985 if (comp_iter)
2986 {
2987 *next_ptr = comp_iter->next;
2988 comp_iter->val = NULL;
2989 gfc_free_structure_ctor_component (comp_iter);
2990 }
2991 }
524af0d6 2992 return true;
7d1f1e61
PT
2993}
2994
c3f34952 2995
524af0d6 2996bool
c3f34952
TB
2997gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
2998 gfc_actual_arglist **arglist,
2999 bool parent)
7d1f1e61 3000{
c3f34952 3001 gfc_actual_arglist *actual;
7d1f1e61 3002 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
b7e75771 3003 gfc_constructor_base ctor_head = NULL;
fa9290d3 3004 gfc_component *comp; /* Is set NULL when named component is first seen */
fa9290d3 3005 const char* last_name = NULL;
c3f34952
TB
3006 locus old_locus;
3007 gfc_expr *expr;
6de9cd9a 3008
c3f34952
TB
3009 expr = parent ? *cexpr : e;
3010 old_locus = gfc_current_locus;
3011 if (parent)
3012 ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3013 else
3014 gfc_current_locus = expr->where;
6de9cd9a 3015
c3f34952 3016 comp_tail = comp_head = NULL;
6de9cd9a 3017
52f49934
DK
3018 if (!parent && sym->attr.abstract)
3019 {
1fe61adf 3020 gfc_error ("Cannot construct ABSTRACT type %qs at %L",
c3f34952
TB
3021 sym->name, &expr->where);
3022 goto cleanup;
52f49934
DK
3023 }
3024
c3f34952
TB
3025 comp = sym->components;
3026 actual = parent ? *arglist : expr->value.function.actual;
3027 for ( ; actual; )
6de9cd9a 3028 {
c3f34952 3029 gfc_component *this_comp = NULL;
6de9cd9a 3030
c3f34952
TB
3031 if (!comp_head)
3032 comp_tail = comp_head = gfc_get_structure_ctor_component ();
3033 else
3034 {
3035 comp_tail->next = gfc_get_structure_ctor_component ();
3036 comp_tail = comp_tail->next;
3037 }
3038 if (actual->name)
3039 {
524af0d6
JB
3040 if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3041 " constructor with named arguments at %C"))
c3f34952 3042 goto cleanup;
6de9cd9a 3043
c3f34952
TB
3044 comp_tail->name = xstrdup (actual->name);
3045 last_name = comp_tail->name;
3046 comp = NULL;
3047 }
3048 else
3049 {
3050 /* Components without name are not allowed after the first named
3051 component initializer! */
9b548517 3052 if (!comp || comp->attr.artificial)
fa9290d3 3053 {
c3f34952
TB
3054 if (last_name)
3055 gfc_error ("Component initializer without name after component"
546c8974 3056 " named %s at %L", last_name,
c3f34952
TB
3057 actual->expr ? &actual->expr->where
3058 : &gfc_current_locus);
3059 else
3060 gfc_error ("Too many components in structure constructor at "
546c8974
DM
3061 "%L", actual->expr ? &actual->expr->where
3062 : &gfc_current_locus);
c3f34952 3063 goto cleanup;
fa9290d3 3064 }
fa9290d3 3065
c3f34952
TB
3066 comp_tail->name = xstrdup (comp->name);
3067 }
fa9290d3 3068
c3f34952 3069 /* Find the current component in the structure definition and check
9d1210f4 3070 its access is not private. */
c3f34952 3071 if (comp)
f6288c24 3072 this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
c3f34952
TB
3073 else
3074 {
3075 this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
f6288c24 3076 false, false, NULL);
c3f34952
TB
3077 comp = NULL; /* Reset needed! */
3078 }
6de9cd9a 3079
c3f34952
TB
3080 /* Here we can check if a component name is given which does not
3081 correspond to any component of the defined structure. */
3082 if (!this_comp)
3083 goto cleanup;
fa9290d3 3084
04946c6b
TK
3085 /* For a constant string constructor, make sure the length is
3086 correct; truncate of fill with blanks if needed. */
3087 if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3088 && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3089 && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
13a7688f 3090 && actual->expr->ts.type == BT_CHARACTER
04946c6b
TK
3091 && actual->expr->expr_type == EXPR_CONSTANT)
3092 {
3093 ptrdiff_t c, e;
3094 c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3095 e = actual->expr->value.character.length;
3096
3097 if (c != e)
3098 {
3099 ptrdiff_t i, to;
3100 gfc_char_t *dest;
3101 dest = gfc_get_wide_string (c + 1);
3102
3103 to = e < c ? e : c;
3104 for (i = 0; i < to; i++)
3105 dest[i] = actual->expr->value.character.string[i];
a5fbc2f3 3106
04946c6b
TK
3107 for (i = e; i < c; i++)
3108 dest[i] = ' ';
3109
3110 dest[c] = '\0';
3111 free (actual->expr->value.character.string);
3112
3113 actual->expr->value.character.length = c;
3114 actual->expr->value.character.string = dest;
cf015ca2
TK
3115
3116 if (warn_line_truncation && c < e)
3117 gfc_warning_now (OPT_Wcharacter_truncation,
3118 "CHARACTER expression will be truncated "
3119 "in constructor (%ld/%ld) at %L", (long int) c,
3120 (long int) e, &actual->expr->where);
04946c6b
TK
3121 }
3122 }
3123
c3f34952
TB
3124 comp_tail->val = actual->expr;
3125 if (actual->expr != NULL)
3126 comp_tail->where = actual->expr->where;
3127 actual->expr = NULL;
fa9290d3 3128
c3f34952 3129 /* Check if this component is already given a value. */
bf1b77dd 3130 for (comp_iter = comp_head; comp_iter != comp_tail;
c3f34952
TB
3131 comp_iter = comp_iter->next)
3132 {
3133 gcc_assert (comp_iter);
3134 if (!strcmp (comp_iter->name, comp_tail->name))
d3a9eea2 3135 {
c4100eae 3136 gfc_error ("Component %qs is initialized twice in the structure"
546c8974 3137 " constructor at %L", comp_tail->name,
c3f34952
TB
3138 comp_tail->val ? &comp_tail->where
3139 : &gfc_current_locus);
d3a9eea2 3140 goto cleanup;
c3f34952
TB
3141 }
3142 }
d3a9eea2 3143
c3f34952
TB
3144 /* F2008, R457/C725, for PURE C1283. */
3145 if (this_comp->attr.pointer && comp_tail->val
3146 && gfc_is_coindexed (comp_tail->val))
3147 {
a4d9b221 3148 gfc_error ("Coindexed expression to pointer component %qs in "
546c8974 3149 "structure constructor at %L", comp_tail->name,
c3f34952
TB
3150 &comp_tail->where);
3151 goto cleanup;
3152 }
d3a9eea2 3153
c3f34952
TB
3154 /* If not explicitly a parent constructor, gather up the components
3155 and build one. */
3156 if (comp && comp == sym->components
3157 && sym->attr.extension
3158 && comp_tail->val
f6288c24 3159 && (!gfc_bt_struct (comp_tail->val->ts.type)
c3f34952
TB
3160 ||
3161 comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3162 {
524af0d6 3163 bool m;
c3f34952 3164 gfc_actual_arglist *arg_null = NULL;
6de9cd9a 3165
c3f34952
TB
3166 actual->expr = comp_tail->val;
3167 comp_tail->val = NULL;
6de9cd9a 3168
c3f34952
TB
3169 m = gfc_convert_to_structure_constructor (NULL,
3170 comp->ts.u.derived, &comp_tail->val,
3171 comp->ts.u.derived->attr.zero_comp
3172 ? &arg_null : &actual, true);
524af0d6 3173 if (!m)
c3f34952 3174 goto cleanup;
2eae3dc7 3175
c3f34952
TB
3176 if (comp->ts.u.derived->attr.zero_comp)
3177 {
3178 comp = comp->next;
3179 continue;
3180 }
3181 }
fa9290d3 3182
c3f34952
TB
3183 if (comp)
3184 comp = comp->next;
3185 if (parent && !comp)
3186 break;
fa9290d3 3187
792f7301
MM
3188 if (actual)
3189 actual = actual->next;
6de9cd9a
DN
3190 }
3191
524af0d6 3192 if (!build_actual_constructor (&comp_head, &ctor_head, sym))
7d1f1e61
PT
3193 goto cleanup;
3194
fa9290d3
DK
3195 /* No component should be left, as this should have caused an error in the
3196 loop constructing the component-list (name that does not correspond to any
3197 component in the structure definition). */
c3f34952 3198 if (comp_head && sym->attr.extension)
7d1f1e61
PT
3199 {
3200 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3201 {
a4d9b221 3202 gfc_error ("component %qs at %L has already been set by a "
7d1f1e61
PT
3203 "parent derived type constructor", comp_iter->name,
3204 &comp_iter->where);
3205 }
3206 goto cleanup;
3207 }
c3f34952
TB
3208 else
3209 gcc_assert (!comp_head);
fa9290d3 3210
c3f34952
TB
3211 if (parent)
3212 {
3213 expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3214 expr->ts.u.derived = sym;
3215 expr->value.constructor = ctor_head;
3216 *cexpr = expr;
3217 }
3218 else
3219 {
3220 expr->ts.u.derived = sym;
3221 expr->ts.kind = 0;
3222 expr->ts.type = BT_DERIVED;
3223 expr->value.constructor = ctor_head;
3224 expr->expr_type = EXPR_STRUCTURE;
3225 }
6de9cd9a 3226
bf1b77dd 3227 gfc_current_locus = old_locus;
c3f34952
TB
3228 if (parent)
3229 *arglist = actual;
524af0d6 3230 return true;
6de9cd9a 3231
c3f34952 3232 cleanup:
bf1b77dd 3233 gfc_current_locus = old_locus;
6de9cd9a 3234
fa9290d3
DK
3235 for (comp_iter = comp_head; comp_iter; )
3236 {
3237 gfc_structure_ctor_component *next = comp_iter->next;
3238 gfc_free_structure_ctor_component (comp_iter);
3239 comp_iter = next;
3240 }
b7e75771 3241 gfc_constructor_free (ctor_head);
c3f34952 3242
524af0d6 3243 return false;
c3f34952
TB
3244}
3245
3246
3247match
3248gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
3249{
3250 match m;
3251 gfc_expr *e;
3252 gfc_symtree *symtree;
3253
b64c3d06 3254 gfc_get_ha_sym_tree (sym->name, &symtree);
c3f34952
TB
3255
3256 e = gfc_get_expr ();
3257 e->symtree = symtree;
3258 e->expr_type = EXPR_FUNCTION;
2f00fb3d 3259 e->where = gfc_current_locus;
c3f34952 3260
f6288c24 3261 gcc_assert (gfc_fl_struct (sym->attr.flavor)
c3f34952
TB
3262 && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3263 e->value.function.esym = sym;
3264 e->symtree->n.sym->attr.generic = 1;
3265
49032565
SK
3266 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3267 if (m != MATCH_YES)
3268 {
3269 gfc_free_expr (e);
3270 return m;
3271 }
3272
3273 if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3274 {
3275 gfc_free_expr (e);
3276 return MATCH_ERROR;
3277 }
3278
586dc38b
SK
3279 /* If a structure constructor is in a DATA statement, then each entity
3280 in the structure constructor must be a constant. Try to reduce the
3281 expression here. */
3282 if (gfc_in_match_data ())
3283 gfc_reduce_init_expr (e);
5bab4c96 3284
49032565
SK
3285 *result = e;
3286 return MATCH_YES;
6de9cd9a
DN
3287}
3288
3289
9a3db5a3
PT
3290/* If the symbol is an implicit do loop index and implicitly typed,
3291 it should not be host associated. Provide a symtree from the
3292 current namespace. */
3293static match
3294check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3295{
3296 if ((*sym)->attr.flavor == FL_VARIABLE
3297 && (*sym)->ns != gfc_current_ns
3298 && (*sym)->attr.implied_index
3299 && (*sym)->attr.implicit_type
3300 && !(*sym)->attr.use_assoc)
3301 {
3302 int i;
08a6b8e0 3303 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
9a3db5a3
PT
3304 if (i)
3305 return MATCH_ERROR;
3306 *sym = (*st)->n.sym;
3307 }
3308 return MATCH_YES;
3309}
3310
3311
3070bab4
JW
3312/* Procedure pointer as function result: Replace the function symbol by the
3313 auto-generated hidden result variable named "ppr@". */
3314
524af0d6 3315static bool
3070bab4
JW
3316replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3317{
3318 /* Check for procedure pointer result variable. */
3319 if ((*sym)->attr.function && !(*sym)->attr.external
3320 && (*sym)->result && (*sym)->result != *sym
3321 && (*sym)->result->attr.proc_pointer
3322 && (*sym) == gfc_current_ns->proc_name
3323 && (*sym) == (*sym)->result->ns->proc_name
3324 && strcmp ("ppr@", (*sym)->result->name) == 0)
3325 {
3326 /* Automatic replacement with "hidden" result variable. */
3327 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3328 *sym = (*sym)->result;
3329 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
524af0d6 3330 return true;
3070bab4 3331 }
524af0d6 3332 return false;
3070bab4
JW
3333}
3334
3335
6de9cd9a
DN
3336/* Matches a variable name followed by anything that might follow it--
3337 array reference, argument list of a function, etc. */
3338
3339match
edf1eac2 3340gfc_match_rvalue (gfc_expr **result)
6de9cd9a
DN
3341{
3342 gfc_actual_arglist *actual_arglist;
d3fcc995 3343 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
3344 gfc_state_data *st;
3345 gfc_symbol *sym;
3346 gfc_symtree *symtree;
d3fcc995 3347 locus where, old_loc;
6de9cd9a 3348 gfc_expr *e;
d3fcc995 3349 match m, m2;
6de9cd9a 3350 int i;
5270c302
AL
3351 gfc_typespec *ts;
3352 bool implicit_char;
a99288e5 3353 gfc_ref *ref;
6de9cd9a 3354
cd714e1e
FR
3355 m = gfc_match ("%%loc");
3356 if (m == MATCH_YES)
3357 {
3358 if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3359 return MATCH_ERROR;
3360 strncpy (name, "loc", 4);
3361 }
3362
3363 else
3364 {
3365 m = gfc_match_name (name);
3366 if (m != MATCH_YES)
3367 return m;
3368 }
6de9cd9a 3369
f6288c24
FR
3370 /* Check if the symbol exists. */
3371 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
6de9cd9a
DN
3372 return MATCH_ERROR;
3373
f6288c24
FR
3374 /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3375 type. For derived types we create a generic symbol which links to the
3376 derived type symbol; STRUCTUREs are simpler and must not conflict with
3377 variables. */
3378 if (!symtree)
3379 if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3380 return MATCH_ERROR;
3381 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3382 {
3383 if (gfc_find_state (COMP_INTERFACE)
3384 && !gfc_current_ns->has_import_set)
3385 i = gfc_get_sym_tree (name, NULL, &symtree, false);
3386 else
3387 i = gfc_get_ha_sym_tree (name, &symtree);
3388 if (i)
3389 return MATCH_ERROR;
3390 }
3391
3392
6de9cd9a
DN
3393 sym = symtree->n.sym;
3394 e = NULL;
63645982 3395 where = gfc_current_locus;
6de9cd9a 3396
3070bab4
JW
3397 replace_hidden_procptr_result (&sym, &symtree);
3398
9a3db5a3
PT
3399 /* If this is an implicit do loop index and implicitly typed,
3400 it should not be host associated. */
3401 m = check_for_implicit_index (&symtree, &sym);
3402 if (m != MATCH_YES)
3403 return m;
3404
6de9cd9a 3405 gfc_set_sym_referenced (sym);
9a3db5a3 3406 sym->attr.implied_index = 0;
6de9cd9a 3407
0921bc44
JJ
3408 if (sym->attr.function && sym->result == sym)
3409 {
811849c0
PT
3410 /* See if this is a directly recursive function call. */
3411 gfc_gobble_whitespace ();
3412 if (sym->attr.recursive
8fc541d3 3413 && gfc_peek_ascii_char () == '('
fc2d8680
PT
3414 && gfc_current_ns->proc_name == sym
3415 && !sym->attr.dimension)
811849c0 3416 {
a4d9b221 3417 gfc_error ("%qs at %C is the name of a recursive function "
fc2d8680
PT
3418 "and so refers to the result variable. Use an "
3419 "explicit RESULT variable for direct recursion "
3420 "(12.5.2.1)", sym->name);
811849c0
PT
3421 return MATCH_ERROR;
3422 }
fc2d8680 3423
2d71b918 3424 if (gfc_is_function_return_value (sym, gfc_current_ns))
0921bc44
JJ
3425 goto variable;
3426
3427 if (sym->attr.entry
3428 && (sym->ns == gfc_current_ns
3429 || sym->ns == gfc_current_ns->parent))
3430 {
3431 gfc_entry_list *el = NULL;
bf1b77dd 3432
0921bc44
JJ
3433 for (el = sym->ns->entries; el; el = el->next)
3434 if (sym == el->sym)
3435 goto variable;
3436 }
3437 }
6de9cd9a 3438
8fb74da4
JW
3439 if (gfc_matching_procptr_assignment)
3440 goto procptr0;
3441
6de9cd9a
DN
3442 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3443 goto function0;
3444
3445 if (sym->attr.generic)
3446 goto generic_function;
3447
3448 switch (sym->attr.flavor)
3449 {
3450 case FL_VARIABLE:
3451 variable:
6de9cd9a
DN
3452 e = gfc_get_expr ();
3453
3454 e->expr_type = EXPR_VARIABLE;
3455 e->symtree = symtree;
3456
713485cc 3457 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3458 break;
3459
3460 case FL_PARAMETER:
b7263e8f 3461 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
bf1b77dd 3462 end up here. Unfortunately, sym->value->expr_type is set to
b7263e8f
EE
3463 EXPR_CONSTANT, and so the if () branch would be followed without
3464 the !sym->as check. */
3465 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
6de9cd9a
DN
3466 e = gfc_copy_expr (sym->value);
3467 else
3468 {
3469 e = gfc_get_expr ();
3470 e->expr_type = EXPR_VARIABLE;
3471 }
3472
3473 e->symtree = symtree;
713485cc 3474 m = gfc_match_varspec (e, 0, false, true);
a99288e5
PT
3475
3476 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3477 break;
3478
927171bf
PT
3479 /* Variable array references to derived type parameters cause
3480 all sorts of headaches in simplification. Treating such
3481 expressions as variable works just fine for all array
3482 references. */
3483 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
a99288e5
PT
3484 {
3485 for (ref = e->ref; ref; ref = ref->next)
3486 if (ref->type == REF_ARRAY)
3487 break;
3488
927171bf 3489 if (ref == NULL || ref->u.ar.type == AR_FULL)
a99288e5
PT
3490 break;
3491
3492 ref = e->ref;
3493 e->ref = NULL;
3494 gfc_free_expr (e);
3495 e = gfc_get_expr ();
3496 e->expr_type = EXPR_VARIABLE;
3497 e->symtree = symtree;
3498 e->ref = ref;
a99288e5
PT
3499 }
3500
6de9cd9a
DN
3501 break;
3502
f6288c24 3503 case FL_STRUCT:
6de9cd9a
DN
3504 case FL_DERIVED:
3505 sym = gfc_use_derived (sym);
3506 if (sym == NULL)
3507 m = MATCH_ERROR;
3508 else
c3f34952 3509 goto generic_function;
6de9cd9a
DN
3510 break;
3511
3512 /* If we're here, then the name is known to be the name of a
3513 procedure, yet it is not sure to be the name of a function. */
3514 case FL_PROCEDURE:
8fb74da4 3515
1cc0e193 3516 /* Procedure Pointer Assignments. */
8fb74da4
JW
3517 procptr0:
3518 if (gfc_matching_procptr_assignment)
3519 {
3520 gfc_gobble_whitespace ();
e35bbb23 3521 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
8fb74da4
JW
3522 /* Parse functions returning a procptr. */
3523 goto function0;
3524
8fb74da4
JW
3525 e = gfc_get_expr ();
3526 e->expr_type = EXPR_VARIABLE;
3527 e->symtree = symtree;
713485cc 3528 m = gfc_match_varspec (e, 0, false, true);
2dda89a8
JW
3529 if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3530 && sym->ts.type == BT_UNKNOWN
524af0d6 3531 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
2dda89a8
JW
3532 {
3533 m = MATCH_ERROR;
3534 break;
3535 }
8fb74da4
JW
3536 break;
3537 }
3538
6de9cd9a
DN
3539 if (sym->attr.subroutine)
3540 {
a4d9b221 3541 gfc_error ("Unexpected use of subroutine name %qs at %C",
6de9cd9a
DN
3542 sym->name);
3543 m = MATCH_ERROR;
3544 break;
3545 }
3546
3547 /* At this point, the name has to be a non-statement function.
edf1eac2
SK
3548 If the name is the same as the current function being
3549 compiled, then we have a variable reference (to the function
3550 result) if the name is non-recursive. */
6de9cd9a
DN
3551
3552 st = gfc_enclosing_unit (NULL);
3553
4668d6f9
PT
3554 if (st != NULL
3555 && st->state == COMP_FUNCTION
6de9cd9a
DN
3556 && st->sym == sym
3557 && !sym->attr.recursive)
3558 {
3559 e = gfc_get_expr ();
3560 e->symtree = symtree;
3561 e->expr_type = EXPR_VARIABLE;
3562
713485cc 3563 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3564 break;
3565 }
3566
3567 /* Match a function reference. */
3568 function0:
3569 m = gfc_match_actual_arglist (0, &actual_arglist);
3570 if (m == MATCH_NO)
3571 {
3572 if (sym->attr.proc == PROC_ST_FUNCTION)
a4d9b221 3573 gfc_error ("Statement function %qs requires argument list at %C",
6de9cd9a
DN
3574 sym->name);
3575 else
a4d9b221 3576 gfc_error ("Function %qs requires an argument list at %C",
6de9cd9a
DN
3577 sym->name);
3578
3579 m = MATCH_ERROR;
3580 break;
3581 }
3582
3583 if (m != MATCH_YES)
3584 {
3585 m = MATCH_ERROR;
3586 break;
3587 }
3588
3589 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
3590 sym = symtree->n.sym;
3591
3070bab4
JW
3592 replace_hidden_procptr_result (&sym, &symtree);
3593
6de9cd9a
DN
3594 e = gfc_get_expr ();
3595 e->symtree = symtree;
3596 e->expr_type = EXPR_FUNCTION;
3597 e->value.function.actual = actual_arglist;
63645982 3598 e->where = gfc_current_locus;
6de9cd9a 3599
102344e2
TB
3600 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
3601 && CLASS_DATA (sym)->as)
3602 e->rank = CLASS_DATA (sym)->as->rank;
3603 else if (sym->as != NULL)
6de9cd9a
DN
3604 e->rank = sym->as->rank;
3605
3606 if (!sym->attr.function
524af0d6 3607 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3608 {
3609 m = MATCH_ERROR;
3610 break;
3611 }
3612
a8b3b0b6
CR
3613 /* Check here for the existence of at least one argument for the
3614 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
3615 argument(s) given will be checked in gfc_iso_c_func_interface,
3616 during resolution of the function call. */
3617 if (sym->attr.is_iso_c == 1
3618 && (sym->from_intmod == INTMOD_ISO_C_BINDING
3619 && (sym->intmod_sym_id == ISOCBINDING_LOC
3620 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
3621 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
3622 {
3623 /* make sure we were given a param */
3624 if (actual_arglist == NULL)
3625 {
a4d9b221 3626 gfc_error ("Missing argument to %qs at %C", sym->name);
a8b3b0b6
CR
3627 m = MATCH_ERROR;
3628 break;
3629 }
3630 }
3631
6de9cd9a
DN
3632 if (sym->result == NULL)
3633 sym->result = sym;
3634
20fee4a9
JW
3635 gfc_gobble_whitespace ();
3636 /* F08:C612. */
3637 if (gfc_peek_ascii_char() == '%')
3638 {
67914693 3639 gfc_error ("The leftmost part-ref in a data-ref cannot be a "
20fee4a9
JW
3640 "function reference at %C");
3641 m = MATCH_ERROR;
3642 }
3643
6de9cd9a
DN
3644 m = MATCH_YES;
3645 break;
3646
3647 case FL_UNKNOWN:
3648
3649 /* Special case for derived type variables that get their types
edf1eac2
SK
3650 via an IMPLICIT statement. This can't wait for the
3651 resolution phase. */
6de9cd9a 3652
f6288c24
FR
3653 old_loc = gfc_current_locus;
3654 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 3655 && sym->ts.type == BT_UNKNOWN
713485cc 3656 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
6de9cd9a 3657 gfc_set_default_type (sym, 0, sym->ns);
f6288c24 3658 gfc_current_locus = old_loc;
6de9cd9a 3659
492792ed 3660 /* If the symbol has a (co)dimension attribute, the expression is a
edf1eac2 3661 variable. */
6de9cd9a 3662
492792ed 3663 if (sym->attr.dimension || sym->attr.codimension)
6de9cd9a 3664 {
524af0d6 3665 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3666 {
3667 m = MATCH_ERROR;
3668 break;
3669 }
3670
3671 e = gfc_get_expr ();
3672 e->symtree = symtree;
3673 e->expr_type = EXPR_VARIABLE;
713485cc 3674 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3675 break;
3676 }
3677
cd99c23c 3678 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
492792ed
TB
3679 && (CLASS_DATA (sym)->attr.dimension
3680 || CLASS_DATA (sym)->attr.codimension))
c49ea23d 3681 {
524af0d6 3682 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
c49ea23d
PT
3683 {
3684 m = MATCH_ERROR;
3685 break;
3686 }
3687
3688 e = gfc_get_expr ();
3689 e->symtree = symtree;
3690 e->expr_type = EXPR_VARIABLE;
3691 m = gfc_match_varspec (e, 0, false, true);
3692 break;
3693 }
3694
6de9cd9a 3695 /* Name is not an array, so we peek to see if a '(' implies a
edf1eac2
SK
3696 function call or a substring reference. Otherwise the
3697 variable is just a scalar. */
6de9cd9a
DN
3698
3699 gfc_gobble_whitespace ();
8fc541d3 3700 if (gfc_peek_ascii_char () != '(')
6de9cd9a
DN
3701 {
3702 /* Assume a scalar variable */
3703 e = gfc_get_expr ();
3704 e->symtree = symtree;
3705 e->expr_type = EXPR_VARIABLE;
3706
524af0d6 3707 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3708 {
3709 m = MATCH_ERROR;
3710 break;
3711 }
3712
8e1f752a 3713 /*FIXME:??? gfc_match_varspec does set this for us: */
6de9cd9a 3714 e->ts = sym->ts;
713485cc 3715 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3716 break;
3717 }
3718
d3fcc995
TS
3719 /* See if this is a function reference with a keyword argument
3720 as first argument. We do this because otherwise a spurious
3721 symbol would end up in the symbol table. */
3722
3723 old_loc = gfc_current_locus;
3724 m2 = gfc_match (" ( %n =", argname);
3725 gfc_current_locus = old_loc;
6de9cd9a
DN
3726
3727 e = gfc_get_expr ();
3728 e->symtree = symtree;
3729
d3fcc995 3730 if (m2 != MATCH_YES)
6de9cd9a 3731 {
5270c302 3732 /* Try to figure out whether we're dealing with a character type.
bf1b77dd 3733 We're peeking ahead here, because we don't want to call
5270c302
AL
3734 match_substring if we're dealing with an implicitly typed
3735 non-character variable. */
3736 implicit_char = false;
3737 if (sym->ts.type == BT_UNKNOWN)
3738 {
713485cc 3739 ts = gfc_get_default_type (sym->name, NULL);
5270c302
AL
3740 if (ts->type == BT_CHARACTER)
3741 implicit_char = true;
3742 }
3743
d3fcc995
TS
3744 /* See if this could possibly be a substring reference of a name
3745 that we're not sure is a variable yet. */
6de9cd9a 3746
5270c302 3747 if ((implicit_char || sym->ts.type == BT_CHARACTER)
38217d3e 3748 && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
6de9cd9a 3749 {
6de9cd9a 3750
d3fcc995
TS
3751 e->expr_type = EXPR_VARIABLE;
3752
3753 if (sym->attr.flavor != FL_VARIABLE
bf1b77dd 3754 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
524af0d6 3755 sym->name, NULL))
d3fcc995
TS
3756 {
3757 m = MATCH_ERROR;
3758 break;
3759 }
3760
3761 if (sym->ts.type == BT_UNKNOWN
524af0d6 3762 && !gfc_set_default_type (sym, 1, NULL))
d3fcc995
TS
3763 {
3764 m = MATCH_ERROR;
3765 break;
3766 }
3767
3768 e->ts = sym->ts;
860c8f3b 3769 if (e->ref)
bc21d315 3770 e->ts.u.cl = NULL;
d3fcc995 3771 m = MATCH_YES;
6de9cd9a
DN
3772 break;
3773 }
6de9cd9a
DN
3774 }
3775
3776 /* Give up, assume we have a function. */
3777
08a6b8e0 3778 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3779 sym = symtree->n.sym;
3780 e->expr_type = EXPR_FUNCTION;
3781
3782 if (!sym->attr.function
524af0d6 3783 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3784 {
3785 m = MATCH_ERROR;
3786 break;
3787 }
3788
3789 sym->result = sym;
3790
3791 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3792 if (m == MATCH_NO)
a4d9b221 3793 gfc_error ("Missing argument list in function %qs at %C", sym->name);
6de9cd9a
DN
3794
3795 if (m != MATCH_YES)
3796 {
3797 m = MATCH_ERROR;
3798 break;
3799 }
3800
3801 /* If our new function returns a character, array or structure
edf1eac2 3802 type, it might have subsequent references. */
6de9cd9a 3803
713485cc 3804 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3805 if (m == MATCH_NO)
3806 m = MATCH_YES;
3807
3808 break;
3809
3810 generic_function:
f6288c24
FR
3811 /* Look for symbol first; if not found, look for STRUCTURE type symbol
3812 specially. Creates a generic symbol for derived types. */
3813 gfc_find_sym_tree (name, NULL, 1, &symtree);
3814 if (!symtree)
3815 gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
3816 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3817 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3818
3819 e = gfc_get_expr ();
3820 e->symtree = symtree;
3821 e->expr_type = EXPR_FUNCTION;
3822
f6288c24 3823 if (gfc_fl_struct (sym->attr.flavor))
c3f34952
TB
3824 {
3825 e->value.function.esym = sym;
3826 e->symtree->n.sym->attr.generic = 1;
3827 }
3828
6de9cd9a
DN
3829 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3830 break;
3831
c0f0e35a
JD
3832 case FL_NAMELIST:
3833 m = MATCH_ERROR;
3834 break;
3835
6de9cd9a
DN
3836 default:
3837 gfc_error ("Symbol at %C is not appropriate for an expression");
3838 return MATCH_ERROR;
3839 }
3840
3841 if (m == MATCH_YES)
3842 {
3843 e->where = where;
3844 *result = e;
3845 }
3846 else
3847 gfc_free_expr (e);
3848
3849 return m;
3850}
3851
3852
df2fba9e 3853/* Match a variable, i.e. something that can be assigned to. This
6de9cd9a
DN
3854 starts as a symbol, can be a structure component or an array
3855 reference. It can be a function if the function doesn't have a
3856 separate RESULT variable. If the symbol has not been previously
30aabb86 3857 seen, we assume it is a variable.
6de9cd9a 3858
30aabb86
PT
3859 This function is called by two interface functions:
3860 gfc_match_variable, which has host_flag = 1, and
3861 gfc_match_equiv_variable, with host_flag = 0, to restrict the
3862 match of the symbol to the local scope. */
3863
3864static match
edf1eac2 3865match_variable (gfc_expr **result, int equiv_flag, int host_flag)
6de9cd9a 3866{
f6288c24 3867 gfc_symbol *sym, *dt_sym;
6de9cd9a
DN
3868 gfc_symtree *st;
3869 gfc_expr *expr;
f6288c24 3870 locus where, old_loc;
6de9cd9a
DN
3871 match m;
3872
fd2aa7ad
PT
3873 /* Since nothing has any business being an lvalue in a module
3874 specification block, an interface block or a contains section,
3875 we force the changed_symbols mechanism to work by setting
3876 host_flag to 0. This prevents valid symbols that have the name
3877 of keywords, such as 'end', being turned into variables by
df2fba9e 3878 failed matching to assignments for, e.g., END INTERFACE. */
fd2aa7ad 3879 if (gfc_current_state () == COMP_MODULE
4668d6f9 3880 || gfc_current_state () == COMP_SUBMODULE
fd2aa7ad
PT
3881 || gfc_current_state () == COMP_INTERFACE
3882 || gfc_current_state () == COMP_CONTAINS)
3883 host_flag = 0;
3884
618f4f46 3885 where = gfc_current_locus;
30aabb86 3886 m = gfc_match_sym_tree (&st, host_flag);
6de9cd9a
DN
3887 if (m != MATCH_YES)
3888 return m;
6de9cd9a
DN
3889
3890 sym = st->n.sym;
9a3db5a3
PT
3891
3892 /* If this is an implicit do loop index and implicitly typed,
3893 it should not be host associated. */
3894 m = check_for_implicit_index (&st, &sym);
3895 if (m != MATCH_YES)
3896 return m;
3897
3898 sym->attr.implied_index = 0;
3899
6de9cd9a 3900 gfc_set_sym_referenced (sym);
f6288c24
FR
3901
3902 /* STRUCTUREs may share names with variables, but derived types may not. */
3903 if (sym->attr.flavor == FL_PROCEDURE && sym->generic
3904 && (dt_sym = gfc_find_dt_in_generic (sym)))
3905 {
3906 if (dt_sym->attr.flavor == FL_DERIVED)
2f029c08 3907 gfc_error ("Derived type %qs cannot be used as a variable at %C",
f6288c24
FR
3908 sym->name);
3909 return MATCH_ERROR;
3910 }
3911
6de9cd9a
DN
3912 switch (sym->attr.flavor)
3913 {
3914 case FL_VARIABLE:
8c91ab34 3915 /* Everything is alright. */
6de9cd9a
DN
3916 break;
3917
3918 case FL_UNKNOWN:
d7e2fcd0
TB
3919 {
3920 sym_flavor flavor = FL_UNKNOWN;
3921
3922 gfc_gobble_whitespace ();
3923
3924 if (sym->attr.external || sym->attr.procedure
3925 || sym->attr.function || sym->attr.subroutine)
3926 flavor = FL_PROCEDURE;
b9332b09
PT
3927
3928 /* If it is not a procedure, is not typed and is host associated,
3929 we cannot give it a flavor yet. */
3930 else if (sym->ns == gfc_current_ns->parent
3931 && sym->ts.type == BT_UNKNOWN)
3932 break;
3933
3934 /* These are definitive indicators that this is a variable. */
8fc541d3 3935 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
d7e2fcd0
TB
3936 || sym->attr.pointer || sym->as != NULL)
3937 flavor = FL_VARIABLE;
3938
3939 if (flavor != FL_UNKNOWN
524af0d6 3940 && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
d7e2fcd0
TB
3941 return MATCH_ERROR;
3942 }
6de9cd9a
DN
3943 break;
3944
5056a350
SK
3945 case FL_PARAMETER:
3946 if (equiv_flag)
8c91ab34
DK
3947 {
3948 gfc_error ("Named constant at %C in an EQUIVALENCE");
3949 return MATCH_ERROR;
3950 }
3951 /* Otherwise this is checked for and an error given in the
3952 variable definition context checks. */
5056a350
SK
3953 break;
3954
6de9cd9a 3955 case FL_PROCEDURE:
01d2a7d7
DF
3956 /* Check for a nonrecursive function result variable. */
3957 if (sym->attr.function
8c91ab34
DK
3958 && !sym->attr.external
3959 && sym->result == sym
3960 && (gfc_is_function_return_value (sym, gfc_current_ns)
3961 || (sym->attr.entry
3962 && sym->ns == gfc_current_ns)
3963 || (sym->attr.entry
3964 && sym->ns == gfc_current_ns->parent)))
6de9cd9a 3965 {
6de9cd9a
DN
3966 /* If a function result is a derived type, then the derived
3967 type may still have to be resolved. */
3968
3969 if (sym->ts.type == BT_DERIVED
bc21d315 3970 && gfc_use_derived (sym->ts.u.derived) == NULL)
6de9cd9a 3971 return MATCH_ERROR;
6de9cd9a
DN
3972 break;
3973 }
3974
3070bab4 3975 if (sym->attr.proc_pointer
524af0d6 3976 || replace_hidden_procptr_result (&sym, &st))
8fb74da4
JW
3977 break;
3978
6de9cd9a 3979 /* Fall through to error */
81fea426 3980 gcc_fallthrough ();
6de9cd9a
DN
3981
3982 default:
a4d9b221 3983 gfc_error ("%qs at %C is not a variable", sym->name);
6de9cd9a
DN
3984 return MATCH_ERROR;
3985 }
3986
0dd973dd
PB
3987 /* Special case for derived type variables that get their types
3988 via an IMPLICIT statement. This can't wait for the
3989 resolution phase. */
3990
3991 {
3992 gfc_namespace * implicit_ns;
3993
3994 if (gfc_current_ns->proc_name == sym)
3995 implicit_ns = gfc_current_ns;
3996 else
3997 implicit_ns = sym->ns;
5bab4c96 3998
f6288c24
FR
3999 old_loc = gfc_current_locus;
4000 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 4001 && sym->ts.type == BT_UNKNOWN
713485cc 4002 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
0dd973dd 4003 gfc_set_default_type (sym, 0, implicit_ns);
f6288c24 4004 gfc_current_locus = old_loc;
0dd973dd
PB
4005 }
4006
6de9cd9a
DN
4007 expr = gfc_get_expr ();
4008
4009 expr->expr_type = EXPR_VARIABLE;
4010 expr->symtree = st;
4011 expr->ts = sym->ts;
4012 expr->where = where;
4013
4014 /* Now see if we have to do more. */
713485cc 4015 m = gfc_match_varspec (expr, equiv_flag, false, false);
6de9cd9a
DN
4016 if (m != MATCH_YES)
4017 {
4018 gfc_free_expr (expr);
4019 return m;
4020 }
4021
4022 *result = expr;
4023 return MATCH_YES;
4024}
30aabb86 4025
edf1eac2 4026
30aabb86 4027match
edf1eac2 4028gfc_match_variable (gfc_expr **result, int equiv_flag)
30aabb86
PT
4029{
4030 return match_variable (result, equiv_flag, 1);
4031}
4032
edf1eac2 4033
30aabb86 4034match
edf1eac2 4035gfc_match_equiv_variable (gfc_expr **result)
30aabb86
PT
4036{
4037 return match_variable (result, 1, 0);
4038}
4039