]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
emit-rtl.c (verify_insn_sharing): Call verify_rtx_sharing instead of reset_used_flags.
[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
b2acf594
PT
2053 if (sym->f2k_derived)
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
2421 "infectious" being propagated once set and never cleared. */
2422
2423static symbol_attribute
2424caf_variable_attr (gfc_expr *expr, bool in_allocate)
2425{
2426 int dimension, codimension, pointer, allocatable, target, coarray_comp,
2427 alloc_comp;
2428 symbol_attribute attr;
2429 gfc_ref *ref;
2430 gfc_symbol *sym;
2431 gfc_component *comp;
2432
2433 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2434 gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
2435
2436 sym = expr->symtree->n.sym;
2437 gfc_clear_attr (&attr);
2438
2439 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2440 {
2441 dimension = CLASS_DATA (sym)->attr.dimension;
2442 codimension = CLASS_DATA (sym)->attr.codimension;
2443 pointer = CLASS_DATA (sym)->attr.class_pointer;
2444 allocatable = CLASS_DATA (sym)->attr.allocatable;
2445 coarray_comp = CLASS_DATA (sym)->attr.coarray_comp;
2446 alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2447 }
2448 else
2449 {
2450 dimension = sym->attr.dimension;
2451 codimension = sym->attr.codimension;
2452 pointer = sym->attr.pointer;
2453 allocatable = sym->attr.allocatable;
2454 coarray_comp = sym->attr.coarray_comp;
2455 alloc_comp = sym->ts.type == BT_DERIVED
2456 ? sym->ts.u.derived->attr.alloc_comp : 0;
2457 }
2458
2459 target = attr.target;
2460 if (pointer || attr.proc_pointer)
2461 target = 1;
2462
2463 for (ref = expr->ref; ref; ref = ref->next)
2464 switch (ref->type)
2465 {
2466 case REF_ARRAY:
2467
2468 switch (ref->u.ar.type)
2469 {
2470 case AR_FULL:
2471 case AR_SECTION:
2472 dimension = 1;
2473 break;
2474
2475 case AR_ELEMENT:
2476 /* Handle coarrays. */
2477 if (ref->u.ar.dimen > 0 && !in_allocate)
2478 allocatable = pointer = 0;
2479 break;
2480
2481 case AR_UNKNOWN:
2482 /* If any of start, end or stride is not integer, there will
2483 already have been an error issued. */
2484 int errors;
2485 gfc_get_errors (NULL, &errors);
2486 if (errors == 0)
2487 gfc_internal_error ("gfc_caf_attr(): Bad array reference");
2488 }
2489
2490 break;
2491
2492 case REF_COMPONENT:
2493 comp = ref->u.c.component;
2494
2495 if (comp->ts.type == BT_CLASS)
2496 {
2497 codimension |= CLASS_DATA (comp)->attr.codimension;
2498 pointer = CLASS_DATA (comp)->attr.class_pointer;
2499 allocatable = CLASS_DATA (comp)->attr.allocatable;
2500 coarray_comp |= CLASS_DATA (comp)->attr.coarray_comp;
2501 }
2502 else
2503 {
2504 codimension |= comp->attr.codimension;
2505 pointer = comp->attr.pointer;
2506 allocatable = comp->attr.allocatable;
2507 coarray_comp |= comp->attr.coarray_comp;
2508 }
2509
2510 if (pointer || attr.proc_pointer)
2511 target = 1;
2512
2513 break;
2514
2515 case REF_SUBSTRING:
2516 allocatable = pointer = 0;
2517 break;
2518 }
2519
2520 attr.dimension = dimension;
2521 attr.codimension = codimension;
2522 attr.pointer = pointer;
2523 attr.allocatable = allocatable;
2524 attr.target = target;
2525 attr.save = sym->attr.save;
2526 attr.coarray_comp = coarray_comp;
2527 attr.alloc_comp = alloc_comp;
2528
2529 return attr;
2530}
2531
2532
2533symbol_attribute
2534gfc_caf_attr (gfc_expr *e, bool in_allocate)
2535{
2536 symbol_attribute attr;
2537
2538 switch (e->expr_type)
2539 {
2540 case EXPR_VARIABLE:
2541 attr = caf_variable_attr (e, in_allocate);
2542 break;
2543
2544 case EXPR_FUNCTION:
2545 gfc_clear_attr (&attr);
2546
2547 if (e->value.function.esym && e->value.function.esym->result)
2548 {
2549 gfc_symbol *sym = e->value.function.esym->result;
2550 attr = sym->attr;
2551 if (sym->ts.type == BT_CLASS)
2552 {
2553 attr.dimension = CLASS_DATA (sym)->attr.dimension;
2554 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2555 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2556 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2557 }
2558 }
2559 else if (e->symtree)
2560 attr = caf_variable_attr (e, in_allocate);
2561 else
2562 gfc_clear_attr (&attr);
2563 break;
2564
2565 default:
6de9cd9a
DN
2566 gfc_clear_attr (&attr);
2567 break;
2568 }
2569
2570 return attr;
2571}
2572
2573
2574/* Match a structure constructor. The initial symbol has already been
2575 seen. */
2576
fa9290d3
DK
2577typedef struct gfc_structure_ctor_component
2578{
2579 char* name;
2580 gfc_expr* val;
2581 locus where;
2582 struct gfc_structure_ctor_component* next;
2583}
2584gfc_structure_ctor_component;
2585
ece3f663 2586#define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
fa9290d3
DK
2587
2588static void
2589gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2590{
cede9502 2591 free (comp->name);
fa9290d3 2592 gfc_free_expr (comp->val);
cede9502 2593 free (comp);
fa9290d3
DK
2594}
2595
7d1f1e61
PT
2596
2597/* Translate the component list into the actual constructor by sorting it in
2598 the order required; this also checks along the way that each and every
2599 component actually has an initializer and handles default initializers
2600 for components without explicit value given. */
524af0d6 2601static bool
7d1f1e61 2602build_actual_constructor (gfc_structure_ctor_component **comp_head,
b7e75771 2603 gfc_constructor_base *ctor_head, gfc_symbol *sym)
6de9cd9a 2604{
fa9290d3 2605 gfc_structure_ctor_component *comp_iter;
7d1f1e61
PT
2606 gfc_component *comp;
2607
2608 for (comp = sym->components; comp; comp = comp->next)
2609 {
2610 gfc_structure_ctor_component **next_ptr;
2611 gfc_expr *value = NULL;
2612
2613 /* Try to find the initializer for the current component by name. */
2614 next_ptr = comp_head;
2615 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2616 {
2617 if (!strcmp (comp_iter->name, comp->name))
2618 break;
2619 next_ptr = &comp_iter->next;
2620 }
2621
2622 /* If an extension, try building the parent derived type by building
2623 a value expression for the parent derived type and calling self. */
2624 if (!comp_iter && comp == sym->components && sym->attr.extension)
2625 {
b7e75771
JD
2626 value = gfc_get_structure_constructor_expr (comp->ts.type,
2627 comp->ts.kind,
2628 &gfc_current_locus);
7d1f1e61 2629 value->ts = comp->ts;
7d1f1e61 2630
bf1b77dd
PT
2631 if (!build_actual_constructor (comp_head,
2632 &value->value.constructor,
524af0d6 2633 comp->ts.u.derived))
7d1f1e61
PT
2634 {
2635 gfc_free_expr (value);
524af0d6 2636 return false;
7d1f1e61 2637 }
b7e75771
JD
2638
2639 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2640 continue;
2641 }
2642
2643 /* If it was not found, try the default initializer if there's any;
2b3dc0db 2644 otherwise, it's an error unless this is a deferred parameter. */
7d1f1e61
PT
2645 if (!comp_iter)
2646 {
2647 if (comp->initializer)
2648 {
524af0d6
JB
2649 if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
2650 "with missing optional arguments at %C"))
2651 return false;
7d1f1e61
PT
2652 value = gfc_copy_expr (comp->initializer);
2653 }
9b548517
AV
2654 else if (comp->attr.allocatable
2655 || (comp->ts.type == BT_CLASS
2656 && CLASS_DATA (comp)->attr.allocatable))
7430df97
JW
2657 {
2658 if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
9b548517
AV
2659 "allocatable component '%qs' given in the "
2660 "structure constructor at %C", comp->name))
7430df97
JW
2661 return false;
2662 }
9b548517 2663 else if (!comp->attr.artificial)
7d1f1e61 2664 {
a4d9b221 2665 gfc_error ("No initializer for component %qs given in the"
7d1f1e61 2666 " structure constructor at %C!", comp->name);
524af0d6 2667 return false;
7d1f1e61
PT
2668 }
2669 }
2670 else
2671 value = comp_iter->val;
2672
2673 /* Add the value to the constructor chain built. */
b7e75771 2674 gfc_constructor_append_expr (ctor_head, value, NULL);
7d1f1e61
PT
2675
2676 /* Remove the entry from the component list. We don't want the expression
2677 value to be free'd, so set it to NULL. */
2678 if (comp_iter)
2679 {
2680 *next_ptr = comp_iter->next;
2681 comp_iter->val = NULL;
2682 gfc_free_structure_ctor_component (comp_iter);
2683 }
2684 }
524af0d6 2685 return true;
7d1f1e61
PT
2686}
2687
c3f34952 2688
524af0d6 2689bool
c3f34952
TB
2690gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
2691 gfc_actual_arglist **arglist,
2692 bool parent)
7d1f1e61 2693{
c3f34952 2694 gfc_actual_arglist *actual;
7d1f1e61 2695 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
b7e75771 2696 gfc_constructor_base ctor_head = NULL;
fa9290d3 2697 gfc_component *comp; /* Is set NULL when named component is first seen */
fa9290d3 2698 const char* last_name = NULL;
c3f34952
TB
2699 locus old_locus;
2700 gfc_expr *expr;
6de9cd9a 2701
c3f34952
TB
2702 expr = parent ? *cexpr : e;
2703 old_locus = gfc_current_locus;
2704 if (parent)
2705 ; /* gfc_current_locus = *arglist->expr ? ->where;*/
2706 else
2707 gfc_current_locus = expr->where;
6de9cd9a 2708
c3f34952 2709 comp_tail = comp_head = NULL;
6de9cd9a 2710
52f49934
DK
2711 if (!parent && sym->attr.abstract)
2712 {
a4d9b221 2713 gfc_error ("Can't construct ABSTRACT type %qs at %L",
c3f34952
TB
2714 sym->name, &expr->where);
2715 goto cleanup;
52f49934
DK
2716 }
2717
c3f34952
TB
2718 comp = sym->components;
2719 actual = parent ? *arglist : expr->value.function.actual;
2720 for ( ; actual; )
6de9cd9a 2721 {
c3f34952 2722 gfc_component *this_comp = NULL;
6de9cd9a 2723
c3f34952
TB
2724 if (!comp_head)
2725 comp_tail = comp_head = gfc_get_structure_ctor_component ();
2726 else
2727 {
2728 comp_tail->next = gfc_get_structure_ctor_component ();
2729 comp_tail = comp_tail->next;
2730 }
2731 if (actual->name)
2732 {
524af0d6
JB
2733 if (!gfc_notify_std (GFC_STD_F2003, "Structure"
2734 " constructor with named arguments at %C"))
c3f34952 2735 goto cleanup;
6de9cd9a 2736
c3f34952
TB
2737 comp_tail->name = xstrdup (actual->name);
2738 last_name = comp_tail->name;
2739 comp = NULL;
2740 }
2741 else
2742 {
2743 /* Components without name are not allowed after the first named
2744 component initializer! */
9b548517 2745 if (!comp || comp->attr.artificial)
fa9290d3 2746 {
c3f34952
TB
2747 if (last_name)
2748 gfc_error ("Component initializer without name after component"
2749 " named %s at %L!", last_name,
2750 actual->expr ? &actual->expr->where
2751 : &gfc_current_locus);
2752 else
2753 gfc_error ("Too many components in structure constructor at "
2754 "%L!", actual->expr ? &actual->expr->where
2755 : &gfc_current_locus);
2756 goto cleanup;
fa9290d3 2757 }
fa9290d3 2758
c3f34952
TB
2759 comp_tail->name = xstrdup (comp->name);
2760 }
fa9290d3 2761
c3f34952 2762 /* Find the current component in the structure definition and check
9d1210f4 2763 its access is not private. */
c3f34952 2764 if (comp)
f6288c24 2765 this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
c3f34952
TB
2766 else
2767 {
2768 this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
f6288c24 2769 false, false, NULL);
c3f34952
TB
2770 comp = NULL; /* Reset needed! */
2771 }
6de9cd9a 2772
c3f34952
TB
2773 /* Here we can check if a component name is given which does not
2774 correspond to any component of the defined structure. */
2775 if (!this_comp)
2776 goto cleanup;
fa9290d3 2777
c3f34952
TB
2778 comp_tail->val = actual->expr;
2779 if (actual->expr != NULL)
2780 comp_tail->where = actual->expr->where;
2781 actual->expr = NULL;
fa9290d3 2782
c3f34952 2783 /* Check if this component is already given a value. */
bf1b77dd 2784 for (comp_iter = comp_head; comp_iter != comp_tail;
c3f34952
TB
2785 comp_iter = comp_iter->next)
2786 {
2787 gcc_assert (comp_iter);
2788 if (!strcmp (comp_iter->name, comp_tail->name))
d3a9eea2 2789 {
c4100eae 2790 gfc_error ("Component %qs is initialized twice in the structure"
c3f34952
TB
2791 " constructor at %L!", comp_tail->name,
2792 comp_tail->val ? &comp_tail->where
2793 : &gfc_current_locus);
d3a9eea2 2794 goto cleanup;
c3f34952
TB
2795 }
2796 }
d3a9eea2 2797
c3f34952
TB
2798 /* F2008, R457/C725, for PURE C1283. */
2799 if (this_comp->attr.pointer && comp_tail->val
2800 && gfc_is_coindexed (comp_tail->val))
2801 {
a4d9b221 2802 gfc_error ("Coindexed expression to pointer component %qs in "
c3f34952
TB
2803 "structure constructor at %L!", comp_tail->name,
2804 &comp_tail->where);
2805 goto cleanup;
2806 }
d3a9eea2 2807
c3f34952
TB
2808 /* If not explicitly a parent constructor, gather up the components
2809 and build one. */
2810 if (comp && comp == sym->components
2811 && sym->attr.extension
2812 && comp_tail->val
f6288c24 2813 && (!gfc_bt_struct (comp_tail->val->ts.type)
c3f34952
TB
2814 ||
2815 comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
2816 {
524af0d6 2817 bool m;
c3f34952 2818 gfc_actual_arglist *arg_null = NULL;
6de9cd9a 2819
c3f34952
TB
2820 actual->expr = comp_tail->val;
2821 comp_tail->val = NULL;
6de9cd9a 2822
c3f34952
TB
2823 m = gfc_convert_to_structure_constructor (NULL,
2824 comp->ts.u.derived, &comp_tail->val,
2825 comp->ts.u.derived->attr.zero_comp
2826 ? &arg_null : &actual, true);
524af0d6 2827 if (!m)
c3f34952 2828 goto cleanup;
2eae3dc7 2829
c3f34952
TB
2830 if (comp->ts.u.derived->attr.zero_comp)
2831 {
2832 comp = comp->next;
2833 continue;
2834 }
2835 }
fa9290d3 2836
c3f34952
TB
2837 if (comp)
2838 comp = comp->next;
2839 if (parent && !comp)
2840 break;
fa9290d3 2841
792f7301
MM
2842 if (actual)
2843 actual = actual->next;
6de9cd9a
DN
2844 }
2845
524af0d6 2846 if (!build_actual_constructor (&comp_head, &ctor_head, sym))
7d1f1e61
PT
2847 goto cleanup;
2848
fa9290d3
DK
2849 /* No component should be left, as this should have caused an error in the
2850 loop constructing the component-list (name that does not correspond to any
2851 component in the structure definition). */
c3f34952 2852 if (comp_head && sym->attr.extension)
7d1f1e61
PT
2853 {
2854 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
2855 {
a4d9b221 2856 gfc_error ("component %qs at %L has already been set by a "
7d1f1e61
PT
2857 "parent derived type constructor", comp_iter->name,
2858 &comp_iter->where);
2859 }
2860 goto cleanup;
2861 }
c3f34952
TB
2862 else
2863 gcc_assert (!comp_head);
fa9290d3 2864
c3f34952
TB
2865 if (parent)
2866 {
2867 expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
2868 expr->ts.u.derived = sym;
2869 expr->value.constructor = ctor_head;
2870 *cexpr = expr;
2871 }
2872 else
2873 {
2874 expr->ts.u.derived = sym;
2875 expr->ts.kind = 0;
2876 expr->ts.type = BT_DERIVED;
2877 expr->value.constructor = ctor_head;
2878 expr->expr_type = EXPR_STRUCTURE;
2879 }
6de9cd9a 2880
bf1b77dd 2881 gfc_current_locus = old_locus;
c3f34952
TB
2882 if (parent)
2883 *arglist = actual;
524af0d6 2884 return true;
6de9cd9a 2885
c3f34952 2886 cleanup:
bf1b77dd 2887 gfc_current_locus = old_locus;
6de9cd9a 2888
fa9290d3
DK
2889 for (comp_iter = comp_head; comp_iter; )
2890 {
2891 gfc_structure_ctor_component *next = comp_iter->next;
2892 gfc_free_structure_ctor_component (comp_iter);
2893 comp_iter = next;
2894 }
b7e75771 2895 gfc_constructor_free (ctor_head);
c3f34952 2896
524af0d6 2897 return false;
c3f34952
TB
2898}
2899
2900
2901match
2902gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
2903{
2904 match m;
2905 gfc_expr *e;
2906 gfc_symtree *symtree;
2907
b64c3d06 2908 gfc_get_ha_sym_tree (sym->name, &symtree);
c3f34952
TB
2909
2910 e = gfc_get_expr ();
2911 e->symtree = symtree;
2912 e->expr_type = EXPR_FUNCTION;
2913
f6288c24 2914 gcc_assert (gfc_fl_struct (sym->attr.flavor)
c3f34952
TB
2915 && symtree->n.sym->attr.flavor == FL_PROCEDURE);
2916 e->value.function.esym = sym;
2917 e->symtree->n.sym->attr.generic = 1;
2918
49032565
SK
2919 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2920 if (m != MATCH_YES)
2921 {
2922 gfc_free_expr (e);
2923 return m;
2924 }
2925
2926 if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
2927 {
2928 gfc_free_expr (e);
2929 return MATCH_ERROR;
2930 }
2931
586dc38b
SK
2932 /* If a structure constructor is in a DATA statement, then each entity
2933 in the structure constructor must be a constant. Try to reduce the
2934 expression here. */
2935 if (gfc_in_match_data ())
2936 gfc_reduce_init_expr (e);
2937
49032565
SK
2938 *result = e;
2939 return MATCH_YES;
6de9cd9a
DN
2940}
2941
2942
9a3db5a3
PT
2943/* If the symbol is an implicit do loop index and implicitly typed,
2944 it should not be host associated. Provide a symtree from the
2945 current namespace. */
2946static match
2947check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
2948{
2949 if ((*sym)->attr.flavor == FL_VARIABLE
2950 && (*sym)->ns != gfc_current_ns
2951 && (*sym)->attr.implied_index
2952 && (*sym)->attr.implicit_type
2953 && !(*sym)->attr.use_assoc)
2954 {
2955 int i;
08a6b8e0 2956 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
9a3db5a3
PT
2957 if (i)
2958 return MATCH_ERROR;
2959 *sym = (*st)->n.sym;
2960 }
2961 return MATCH_YES;
2962}
2963
2964
3070bab4
JW
2965/* Procedure pointer as function result: Replace the function symbol by the
2966 auto-generated hidden result variable named "ppr@". */
2967
524af0d6 2968static bool
3070bab4
JW
2969replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
2970{
2971 /* Check for procedure pointer result variable. */
2972 if ((*sym)->attr.function && !(*sym)->attr.external
2973 && (*sym)->result && (*sym)->result != *sym
2974 && (*sym)->result->attr.proc_pointer
2975 && (*sym) == gfc_current_ns->proc_name
2976 && (*sym) == (*sym)->result->ns->proc_name
2977 && strcmp ("ppr@", (*sym)->result->name) == 0)
2978 {
2979 /* Automatic replacement with "hidden" result variable. */
2980 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
2981 *sym = (*sym)->result;
2982 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
524af0d6 2983 return true;
3070bab4 2984 }
524af0d6 2985 return false;
3070bab4
JW
2986}
2987
2988
6de9cd9a
DN
2989/* Matches a variable name followed by anything that might follow it--
2990 array reference, argument list of a function, etc. */
2991
2992match
edf1eac2 2993gfc_match_rvalue (gfc_expr **result)
6de9cd9a
DN
2994{
2995 gfc_actual_arglist *actual_arglist;
d3fcc995 2996 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
2997 gfc_state_data *st;
2998 gfc_symbol *sym;
2999 gfc_symtree *symtree;
d3fcc995 3000 locus where, old_loc;
6de9cd9a 3001 gfc_expr *e;
d3fcc995 3002 match m, m2;
6de9cd9a 3003 int i;
5270c302
AL
3004 gfc_typespec *ts;
3005 bool implicit_char;
a99288e5 3006 gfc_ref *ref;
6de9cd9a 3007
cd714e1e
FR
3008 m = gfc_match ("%%loc");
3009 if (m == MATCH_YES)
3010 {
3011 if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3012 return MATCH_ERROR;
3013 strncpy (name, "loc", 4);
3014 }
3015
3016 else
3017 {
3018 m = gfc_match_name (name);
3019 if (m != MATCH_YES)
3020 return m;
3021 }
6de9cd9a 3022
f6288c24
FR
3023 /* Check if the symbol exists. */
3024 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
6de9cd9a
DN
3025 return MATCH_ERROR;
3026
f6288c24
FR
3027 /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3028 type. For derived types we create a generic symbol which links to the
3029 derived type symbol; STRUCTUREs are simpler and must not conflict with
3030 variables. */
3031 if (!symtree)
3032 if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3033 return MATCH_ERROR;
3034 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3035 {
3036 if (gfc_find_state (COMP_INTERFACE)
3037 && !gfc_current_ns->has_import_set)
3038 i = gfc_get_sym_tree (name, NULL, &symtree, false);
3039 else
3040 i = gfc_get_ha_sym_tree (name, &symtree);
3041 if (i)
3042 return MATCH_ERROR;
3043 }
3044
3045
6de9cd9a
DN
3046 sym = symtree->n.sym;
3047 e = NULL;
63645982 3048 where = gfc_current_locus;
6de9cd9a 3049
3070bab4
JW
3050 replace_hidden_procptr_result (&sym, &symtree);
3051
9a3db5a3
PT
3052 /* If this is an implicit do loop index and implicitly typed,
3053 it should not be host associated. */
3054 m = check_for_implicit_index (&symtree, &sym);
3055 if (m != MATCH_YES)
3056 return m;
3057
6de9cd9a 3058 gfc_set_sym_referenced (sym);
9a3db5a3 3059 sym->attr.implied_index = 0;
6de9cd9a 3060
0921bc44
JJ
3061 if (sym->attr.function && sym->result == sym)
3062 {
811849c0
PT
3063 /* See if this is a directly recursive function call. */
3064 gfc_gobble_whitespace ();
3065 if (sym->attr.recursive
8fc541d3 3066 && gfc_peek_ascii_char () == '('
fc2d8680
PT
3067 && gfc_current_ns->proc_name == sym
3068 && !sym->attr.dimension)
811849c0 3069 {
a4d9b221 3070 gfc_error ("%qs at %C is the name of a recursive function "
fc2d8680
PT
3071 "and so refers to the result variable. Use an "
3072 "explicit RESULT variable for direct recursion "
3073 "(12.5.2.1)", sym->name);
811849c0
PT
3074 return MATCH_ERROR;
3075 }
fc2d8680 3076
2d71b918 3077 if (gfc_is_function_return_value (sym, gfc_current_ns))
0921bc44
JJ
3078 goto variable;
3079
3080 if (sym->attr.entry
3081 && (sym->ns == gfc_current_ns
3082 || sym->ns == gfc_current_ns->parent))
3083 {
3084 gfc_entry_list *el = NULL;
bf1b77dd 3085
0921bc44
JJ
3086 for (el = sym->ns->entries; el; el = el->next)
3087 if (sym == el->sym)
3088 goto variable;
3089 }
3090 }
6de9cd9a 3091
8fb74da4
JW
3092 if (gfc_matching_procptr_assignment)
3093 goto procptr0;
3094
6de9cd9a
DN
3095 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3096 goto function0;
3097
3098 if (sym->attr.generic)
3099 goto generic_function;
3100
3101 switch (sym->attr.flavor)
3102 {
3103 case FL_VARIABLE:
3104 variable:
6de9cd9a
DN
3105 e = gfc_get_expr ();
3106
3107 e->expr_type = EXPR_VARIABLE;
3108 e->symtree = symtree;
3109
713485cc 3110 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3111 break;
3112
3113 case FL_PARAMETER:
b7263e8f 3114 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
bf1b77dd 3115 end up here. Unfortunately, sym->value->expr_type is set to
b7263e8f
EE
3116 EXPR_CONSTANT, and so the if () branch would be followed without
3117 the !sym->as check. */
3118 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
6de9cd9a
DN
3119 e = gfc_copy_expr (sym->value);
3120 else
3121 {
3122 e = gfc_get_expr ();
3123 e->expr_type = EXPR_VARIABLE;
3124 }
3125
3126 e->symtree = symtree;
713485cc 3127 m = gfc_match_varspec (e, 0, false, true);
a99288e5
PT
3128
3129 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3130 break;
3131
927171bf
PT
3132 /* Variable array references to derived type parameters cause
3133 all sorts of headaches in simplification. Treating such
3134 expressions as variable works just fine for all array
3135 references. */
3136 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
a99288e5
PT
3137 {
3138 for (ref = e->ref; ref; ref = ref->next)
3139 if (ref->type == REF_ARRAY)
3140 break;
3141
927171bf 3142 if (ref == NULL || ref->u.ar.type == AR_FULL)
a99288e5
PT
3143 break;
3144
3145 ref = e->ref;
3146 e->ref = NULL;
3147 gfc_free_expr (e);
3148 e = gfc_get_expr ();
3149 e->expr_type = EXPR_VARIABLE;
3150 e->symtree = symtree;
3151 e->ref = ref;
a99288e5
PT
3152 }
3153
6de9cd9a
DN
3154 break;
3155
f6288c24 3156 case FL_STRUCT:
6de9cd9a
DN
3157 case FL_DERIVED:
3158 sym = gfc_use_derived (sym);
3159 if (sym == NULL)
3160 m = MATCH_ERROR;
3161 else
c3f34952 3162 goto generic_function;
6de9cd9a
DN
3163 break;
3164
3165 /* If we're here, then the name is known to be the name of a
3166 procedure, yet it is not sure to be the name of a function. */
3167 case FL_PROCEDURE:
8fb74da4 3168
1cc0e193 3169 /* Procedure Pointer Assignments. */
8fb74da4
JW
3170 procptr0:
3171 if (gfc_matching_procptr_assignment)
3172 {
3173 gfc_gobble_whitespace ();
e35bbb23 3174 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
8fb74da4
JW
3175 /* Parse functions returning a procptr. */
3176 goto function0;
3177
8fb74da4
JW
3178 e = gfc_get_expr ();
3179 e->expr_type = EXPR_VARIABLE;
3180 e->symtree = symtree;
713485cc 3181 m = gfc_match_varspec (e, 0, false, true);
2dda89a8
JW
3182 if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3183 && sym->ts.type == BT_UNKNOWN
524af0d6 3184 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
2dda89a8
JW
3185 {
3186 m = MATCH_ERROR;
3187 break;
3188 }
8fb74da4
JW
3189 break;
3190 }
3191
6de9cd9a
DN
3192 if (sym->attr.subroutine)
3193 {
a4d9b221 3194 gfc_error ("Unexpected use of subroutine name %qs at %C",
6de9cd9a
DN
3195 sym->name);
3196 m = MATCH_ERROR;
3197 break;
3198 }
3199
3200 /* At this point, the name has to be a non-statement function.
edf1eac2
SK
3201 If the name is the same as the current function being
3202 compiled, then we have a variable reference (to the function
3203 result) if the name is non-recursive. */
6de9cd9a
DN
3204
3205 st = gfc_enclosing_unit (NULL);
3206
4668d6f9
PT
3207 if (st != NULL
3208 && st->state == COMP_FUNCTION
6de9cd9a
DN
3209 && st->sym == sym
3210 && !sym->attr.recursive)
3211 {
3212 e = gfc_get_expr ();
3213 e->symtree = symtree;
3214 e->expr_type = EXPR_VARIABLE;
3215
713485cc 3216 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3217 break;
3218 }
3219
3220 /* Match a function reference. */
3221 function0:
3222 m = gfc_match_actual_arglist (0, &actual_arglist);
3223 if (m == MATCH_NO)
3224 {
3225 if (sym->attr.proc == PROC_ST_FUNCTION)
a4d9b221 3226 gfc_error ("Statement function %qs requires argument list at %C",
6de9cd9a
DN
3227 sym->name);
3228 else
a4d9b221 3229 gfc_error ("Function %qs requires an argument list at %C",
6de9cd9a
DN
3230 sym->name);
3231
3232 m = MATCH_ERROR;
3233 break;
3234 }
3235
3236 if (m != MATCH_YES)
3237 {
3238 m = MATCH_ERROR;
3239 break;
3240 }
3241
3242 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
3243 sym = symtree->n.sym;
3244
3070bab4
JW
3245 replace_hidden_procptr_result (&sym, &symtree);
3246
6de9cd9a
DN
3247 e = gfc_get_expr ();
3248 e->symtree = symtree;
3249 e->expr_type = EXPR_FUNCTION;
3250 e->value.function.actual = actual_arglist;
63645982 3251 e->where = gfc_current_locus;
6de9cd9a 3252
102344e2
TB
3253 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
3254 && CLASS_DATA (sym)->as)
3255 e->rank = CLASS_DATA (sym)->as->rank;
3256 else if (sym->as != NULL)
6de9cd9a
DN
3257 e->rank = sym->as->rank;
3258
3259 if (!sym->attr.function
524af0d6 3260 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3261 {
3262 m = MATCH_ERROR;
3263 break;
3264 }
3265
a8b3b0b6
CR
3266 /* Check here for the existence of at least one argument for the
3267 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
3268 argument(s) given will be checked in gfc_iso_c_func_interface,
3269 during resolution of the function call. */
3270 if (sym->attr.is_iso_c == 1
3271 && (sym->from_intmod == INTMOD_ISO_C_BINDING
3272 && (sym->intmod_sym_id == ISOCBINDING_LOC
3273 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
3274 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
3275 {
3276 /* make sure we were given a param */
3277 if (actual_arglist == NULL)
3278 {
a4d9b221 3279 gfc_error ("Missing argument to %qs at %C", sym->name);
a8b3b0b6
CR
3280 m = MATCH_ERROR;
3281 break;
3282 }
3283 }
3284
6de9cd9a
DN
3285 if (sym->result == NULL)
3286 sym->result = sym;
3287
3288 m = MATCH_YES;
3289 break;
3290
3291 case FL_UNKNOWN:
3292
3293 /* Special case for derived type variables that get their types
edf1eac2
SK
3294 via an IMPLICIT statement. This can't wait for the
3295 resolution phase. */
6de9cd9a 3296
f6288c24
FR
3297 old_loc = gfc_current_locus;
3298 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 3299 && sym->ts.type == BT_UNKNOWN
713485cc 3300 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
6de9cd9a 3301 gfc_set_default_type (sym, 0, sym->ns);
f6288c24 3302 gfc_current_locus = old_loc;
6de9cd9a 3303
492792ed 3304 /* If the symbol has a (co)dimension attribute, the expression is a
edf1eac2 3305 variable. */
6de9cd9a 3306
492792ed 3307 if (sym->attr.dimension || sym->attr.codimension)
6de9cd9a 3308 {
524af0d6 3309 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3310 {
3311 m = MATCH_ERROR;
3312 break;
3313 }
3314
3315 e = gfc_get_expr ();
3316 e->symtree = symtree;
3317 e->expr_type = EXPR_VARIABLE;
713485cc 3318 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3319 break;
3320 }
3321
cd99c23c 3322 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
492792ed
TB
3323 && (CLASS_DATA (sym)->attr.dimension
3324 || CLASS_DATA (sym)->attr.codimension))
c49ea23d 3325 {
524af0d6 3326 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
c49ea23d
PT
3327 {
3328 m = MATCH_ERROR;
3329 break;
3330 }
3331
3332 e = gfc_get_expr ();
3333 e->symtree = symtree;
3334 e->expr_type = EXPR_VARIABLE;
3335 m = gfc_match_varspec (e, 0, false, true);
3336 break;
3337 }
3338
6de9cd9a 3339 /* Name is not an array, so we peek to see if a '(' implies a
edf1eac2
SK
3340 function call or a substring reference. Otherwise the
3341 variable is just a scalar. */
6de9cd9a
DN
3342
3343 gfc_gobble_whitespace ();
8fc541d3 3344 if (gfc_peek_ascii_char () != '(')
6de9cd9a
DN
3345 {
3346 /* Assume a scalar variable */
3347 e = gfc_get_expr ();
3348 e->symtree = symtree;
3349 e->expr_type = EXPR_VARIABLE;
3350
524af0d6 3351 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
6de9cd9a
DN
3352 {
3353 m = MATCH_ERROR;
3354 break;
3355 }
3356
8e1f752a 3357 /*FIXME:??? gfc_match_varspec does set this for us: */
6de9cd9a 3358 e->ts = sym->ts;
713485cc 3359 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3360 break;
3361 }
3362
d3fcc995
TS
3363 /* See if this is a function reference with a keyword argument
3364 as first argument. We do this because otherwise a spurious
3365 symbol would end up in the symbol table. */
3366
3367 old_loc = gfc_current_locus;
3368 m2 = gfc_match (" ( %n =", argname);
3369 gfc_current_locus = old_loc;
6de9cd9a
DN
3370
3371 e = gfc_get_expr ();
3372 e->symtree = symtree;
3373
d3fcc995 3374 if (m2 != MATCH_YES)
6de9cd9a 3375 {
5270c302 3376 /* Try to figure out whether we're dealing with a character type.
bf1b77dd 3377 We're peeking ahead here, because we don't want to call
5270c302
AL
3378 match_substring if we're dealing with an implicitly typed
3379 non-character variable. */
3380 implicit_char = false;
3381 if (sym->ts.type == BT_UNKNOWN)
3382 {
713485cc 3383 ts = gfc_get_default_type (sym->name, NULL);
5270c302
AL
3384 if (ts->type == BT_CHARACTER)
3385 implicit_char = true;
3386 }
3387
d3fcc995
TS
3388 /* See if this could possibly be a substring reference of a name
3389 that we're not sure is a variable yet. */
6de9cd9a 3390
5270c302 3391 if ((implicit_char || sym->ts.type == BT_CHARACTER)
38217d3e 3392 && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
6de9cd9a 3393 {
6de9cd9a 3394
d3fcc995
TS
3395 e->expr_type = EXPR_VARIABLE;
3396
3397 if (sym->attr.flavor != FL_VARIABLE
bf1b77dd 3398 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
524af0d6 3399 sym->name, NULL))
d3fcc995
TS
3400 {
3401 m = MATCH_ERROR;
3402 break;
3403 }
3404
3405 if (sym->ts.type == BT_UNKNOWN
524af0d6 3406 && !gfc_set_default_type (sym, 1, NULL))
d3fcc995
TS
3407 {
3408 m = MATCH_ERROR;
3409 break;
3410 }
3411
3412 e->ts = sym->ts;
860c8f3b 3413 if (e->ref)
bc21d315 3414 e->ts.u.cl = NULL;
d3fcc995 3415 m = MATCH_YES;
6de9cd9a
DN
3416 break;
3417 }
6de9cd9a
DN
3418 }
3419
3420 /* Give up, assume we have a function. */
3421
08a6b8e0 3422 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3423 sym = symtree->n.sym;
3424 e->expr_type = EXPR_FUNCTION;
3425
3426 if (!sym->attr.function
524af0d6 3427 && !gfc_add_function (&sym->attr, sym->name, NULL))
6de9cd9a
DN
3428 {
3429 m = MATCH_ERROR;
3430 break;
3431 }
3432
3433 sym->result = sym;
3434
3435 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3436 if (m == MATCH_NO)
a4d9b221 3437 gfc_error ("Missing argument list in function %qs at %C", sym->name);
6de9cd9a
DN
3438
3439 if (m != MATCH_YES)
3440 {
3441 m = MATCH_ERROR;
3442 break;
3443 }
3444
3445 /* If our new function returns a character, array or structure
edf1eac2 3446 type, it might have subsequent references. */
6de9cd9a 3447
713485cc 3448 m = gfc_match_varspec (e, 0, false, true);
6de9cd9a
DN
3449 if (m == MATCH_NO)
3450 m = MATCH_YES;
3451
3452 break;
3453
3454 generic_function:
f6288c24
FR
3455 /* Look for symbol first; if not found, look for STRUCTURE type symbol
3456 specially. Creates a generic symbol for derived types. */
3457 gfc_find_sym_tree (name, NULL, 1, &symtree);
3458 if (!symtree)
3459 gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
3460 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3461 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
6de9cd9a
DN
3462
3463 e = gfc_get_expr ();
3464 e->symtree = symtree;
3465 e->expr_type = EXPR_FUNCTION;
3466
f6288c24 3467 if (gfc_fl_struct (sym->attr.flavor))
c3f34952
TB
3468 {
3469 e->value.function.esym = sym;
3470 e->symtree->n.sym->attr.generic = 1;
3471 }
3472
6de9cd9a
DN
3473 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3474 break;
3475
c0f0e35a
JD
3476 case FL_NAMELIST:
3477 m = MATCH_ERROR;
3478 break;
3479
6de9cd9a
DN
3480 default:
3481 gfc_error ("Symbol at %C is not appropriate for an expression");
3482 return MATCH_ERROR;
3483 }
3484
3485 if (m == MATCH_YES)
3486 {
3487 e->where = where;
3488 *result = e;
3489 }
3490 else
3491 gfc_free_expr (e);
3492
3493 return m;
3494}
3495
3496
df2fba9e 3497/* Match a variable, i.e. something that can be assigned to. This
6de9cd9a
DN
3498 starts as a symbol, can be a structure component or an array
3499 reference. It can be a function if the function doesn't have a
3500 separate RESULT variable. If the symbol has not been previously
30aabb86 3501 seen, we assume it is a variable.
6de9cd9a 3502
30aabb86
PT
3503 This function is called by two interface functions:
3504 gfc_match_variable, which has host_flag = 1, and
3505 gfc_match_equiv_variable, with host_flag = 0, to restrict the
3506 match of the symbol to the local scope. */
3507
3508static match
edf1eac2 3509match_variable (gfc_expr **result, int equiv_flag, int host_flag)
6de9cd9a 3510{
f6288c24 3511 gfc_symbol *sym, *dt_sym;
6de9cd9a
DN
3512 gfc_symtree *st;
3513 gfc_expr *expr;
f6288c24 3514 locus where, old_loc;
6de9cd9a
DN
3515 match m;
3516
fd2aa7ad
PT
3517 /* Since nothing has any business being an lvalue in a module
3518 specification block, an interface block or a contains section,
3519 we force the changed_symbols mechanism to work by setting
3520 host_flag to 0. This prevents valid symbols that have the name
3521 of keywords, such as 'end', being turned into variables by
df2fba9e 3522 failed matching to assignments for, e.g., END INTERFACE. */
fd2aa7ad 3523 if (gfc_current_state () == COMP_MODULE
4668d6f9 3524 || gfc_current_state () == COMP_SUBMODULE
fd2aa7ad
PT
3525 || gfc_current_state () == COMP_INTERFACE
3526 || gfc_current_state () == COMP_CONTAINS)
3527 host_flag = 0;
3528
618f4f46 3529 where = gfc_current_locus;
30aabb86 3530 m = gfc_match_sym_tree (&st, host_flag);
6de9cd9a
DN
3531 if (m != MATCH_YES)
3532 return m;
6de9cd9a
DN
3533
3534 sym = st->n.sym;
9a3db5a3
PT
3535
3536 /* If this is an implicit do loop index and implicitly typed,
3537 it should not be host associated. */
3538 m = check_for_implicit_index (&st, &sym);
3539 if (m != MATCH_YES)
3540 return m;
3541
3542 sym->attr.implied_index = 0;
3543
6de9cd9a 3544 gfc_set_sym_referenced (sym);
f6288c24
FR
3545
3546 /* STRUCTUREs may share names with variables, but derived types may not. */
3547 if (sym->attr.flavor == FL_PROCEDURE && sym->generic
3548 && (dt_sym = gfc_find_dt_in_generic (sym)))
3549 {
3550 if (dt_sym->attr.flavor == FL_DERIVED)
3551 gfc_error ("Derived type '%s' cannot be used as a variable at %C",
3552 sym->name);
3553 return MATCH_ERROR;
3554 }
3555
6de9cd9a
DN
3556 switch (sym->attr.flavor)
3557 {
3558 case FL_VARIABLE:
8c91ab34 3559 /* Everything is alright. */
6de9cd9a
DN
3560 break;
3561
3562 case FL_UNKNOWN:
d7e2fcd0
TB
3563 {
3564 sym_flavor flavor = FL_UNKNOWN;
3565
3566 gfc_gobble_whitespace ();
3567
3568 if (sym->attr.external || sym->attr.procedure
3569 || sym->attr.function || sym->attr.subroutine)
3570 flavor = FL_PROCEDURE;
b9332b09
PT
3571
3572 /* If it is not a procedure, is not typed and is host associated,
3573 we cannot give it a flavor yet. */
3574 else if (sym->ns == gfc_current_ns->parent
3575 && sym->ts.type == BT_UNKNOWN)
3576 break;
3577
3578 /* These are definitive indicators that this is a variable. */
8fc541d3 3579 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
d7e2fcd0
TB
3580 || sym->attr.pointer || sym->as != NULL)
3581 flavor = FL_VARIABLE;
3582
3583 if (flavor != FL_UNKNOWN
524af0d6 3584 && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
d7e2fcd0
TB
3585 return MATCH_ERROR;
3586 }
6de9cd9a
DN
3587 break;
3588
5056a350
SK
3589 case FL_PARAMETER:
3590 if (equiv_flag)
8c91ab34
DK
3591 {
3592 gfc_error ("Named constant at %C in an EQUIVALENCE");
3593 return MATCH_ERROR;
3594 }
3595 /* Otherwise this is checked for and an error given in the
3596 variable definition context checks. */
5056a350
SK
3597 break;
3598
6de9cd9a 3599 case FL_PROCEDURE:
01d2a7d7
DF
3600 /* Check for a nonrecursive function result variable. */
3601 if (sym->attr.function
8c91ab34
DK
3602 && !sym->attr.external
3603 && sym->result == sym
3604 && (gfc_is_function_return_value (sym, gfc_current_ns)
3605 || (sym->attr.entry
3606 && sym->ns == gfc_current_ns)
3607 || (sym->attr.entry
3608 && sym->ns == gfc_current_ns->parent)))
6de9cd9a 3609 {
6de9cd9a
DN
3610 /* If a function result is a derived type, then the derived
3611 type may still have to be resolved. */
3612
3613 if (sym->ts.type == BT_DERIVED
bc21d315 3614 && gfc_use_derived (sym->ts.u.derived) == NULL)
6de9cd9a 3615 return MATCH_ERROR;
6de9cd9a
DN
3616 break;
3617 }
3618
3070bab4 3619 if (sym->attr.proc_pointer
524af0d6 3620 || replace_hidden_procptr_result (&sym, &st))
8fb74da4
JW
3621 break;
3622
6de9cd9a 3623 /* Fall through to error */
81fea426 3624 gcc_fallthrough ();
6de9cd9a
DN
3625
3626 default:
a4d9b221 3627 gfc_error ("%qs at %C is not a variable", sym->name);
6de9cd9a
DN
3628 return MATCH_ERROR;
3629 }
3630
0dd973dd
PB
3631 /* Special case for derived type variables that get their types
3632 via an IMPLICIT statement. This can't wait for the
3633 resolution phase. */
3634
3635 {
3636 gfc_namespace * implicit_ns;
3637
3638 if (gfc_current_ns->proc_name == sym)
3639 implicit_ns = gfc_current_ns;
3640 else
3641 implicit_ns = sym->ns;
f6288c24
FR
3642
3643 old_loc = gfc_current_locus;
3644 if (gfc_match_member_sep (sym) == MATCH_YES
0dd973dd 3645 && sym->ts.type == BT_UNKNOWN
713485cc 3646 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
0dd973dd 3647 gfc_set_default_type (sym, 0, implicit_ns);
f6288c24 3648 gfc_current_locus = old_loc;
0dd973dd
PB
3649 }
3650
6de9cd9a
DN
3651 expr = gfc_get_expr ();
3652
3653 expr->expr_type = EXPR_VARIABLE;
3654 expr->symtree = st;
3655 expr->ts = sym->ts;
3656 expr->where = where;
3657
3658 /* Now see if we have to do more. */
713485cc 3659 m = gfc_match_varspec (expr, equiv_flag, false, false);
6de9cd9a
DN
3660 if (m != MATCH_YES)
3661 {
3662 gfc_free_expr (expr);
3663 return m;
3664 }
3665
3666 *result = expr;
3667 return MATCH_YES;
3668}
30aabb86 3669
edf1eac2 3670
30aabb86 3671match
edf1eac2 3672gfc_match_variable (gfc_expr **result, int equiv_flag)
30aabb86
PT
3673{
3674 return match_variable (result, equiv_flag, 1);
3675}
3676
edf1eac2 3677
30aabb86 3678match
edf1eac2 3679gfc_match_equiv_variable (gfc_expr **result)
30aabb86
PT
3680{
3681 return match_variable (result, 1, 0);
3682}
3683