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