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