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