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