Line data Source code
1 : /* Primary expression subroutines
2 : Copyright (C) 2000-2026 Free Software Foundation, Inc.
3 : Contributed by Andy Vaught
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "options.h"
25 : #include "gfortran.h"
26 : #include "arith.h"
27 : #include "match.h"
28 : #include "parse.h"
29 : #include "constructor.h"
30 :
31 : int matching_actual_arglist = 0;
32 :
33 : /* Matches a kind-parameter expression, which is either a named
34 : symbolic constant or a nonnegative integer constant. If
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'. */
38 :
39 : static match
40 476875 : match_kind_param (int *kind, int *is_iso_c)
41 : {
42 476875 : char name[GFC_MAX_SYMBOL_LEN + 1];
43 476875 : gfc_symbol *sym;
44 476875 : match m;
45 :
46 476875 : *is_iso_c = 0;
47 :
48 476875 : m = gfc_match_small_literal_int (kind, NULL, false);
49 476875 : if (m != MATCH_NO)
50 : return m;
51 :
52 95124 : m = gfc_match_name (name, false);
53 95124 : if (m != MATCH_YES)
54 : return m;
55 :
56 93392 : if (gfc_find_symbol (name, NULL, 1, &sym))
57 : return MATCH_ERROR;
58 :
59 93392 : if (sym == NULL)
60 : return MATCH_NO;
61 :
62 93391 : *is_iso_c = sym->attr.is_iso_c;
63 :
64 93391 : if (sym->attr.flavor != FL_PARAMETER)
65 : return MATCH_NO;
66 :
67 93391 : if (sym->value == NULL)
68 : return MATCH_NO;
69 :
70 93390 : if (gfc_extract_int (sym->value, kind))
71 : return MATCH_NO;
72 :
73 93390 : gfc_set_sym_referenced (sym);
74 :
75 93390 : if (*kind < 0)
76 0 : return MATCH_NO;
77 :
78 : return MATCH_YES;
79 : }
80 :
81 :
82 : /* Get a trailing kind-specification for non-character variables.
83 : Returns:
84 : * the integer kind value or
85 : * -1 if an error was generated,
86 : * -2 if no kind was found.
87 : The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
88 : symbol like e.g. 'c_int'. */
89 :
90 : static int
91 4715093 : get_kind (int *is_iso_c)
92 : {
93 4715093 : int kind;
94 4715093 : match m;
95 :
96 4715093 : *is_iso_c = 0;
97 :
98 4715093 : if (gfc_match_char ('_', false) != MATCH_YES)
99 : return -2;
100 :
101 476875 : m = match_kind_param (&kind, is_iso_c);
102 476875 : if (m == MATCH_NO)
103 1734 : gfc_error ("Missing kind-parameter at %C");
104 :
105 476875 : return (m == MATCH_YES) ? kind : -1;
106 : }
107 :
108 :
109 : /* Given a character and a radix, see if the character is a valid
110 : digit in that radix. */
111 :
112 : bool
113 31743999 : gfc_check_digit (char c, int radix)
114 : {
115 31743999 : bool r;
116 :
117 31743999 : switch (radix)
118 : {
119 15638 : case 2:
120 15638 : r = ('0' <= c && c <= '1');
121 15638 : break;
122 :
123 19182 : case 8:
124 19182 : r = ('0' <= c && c <= '7');
125 19182 : break;
126 :
127 31644306 : case 10:
128 31644306 : r = ('0' <= c && c <= '9');
129 31644306 : break;
130 :
131 64873 : case 16:
132 64873 : r = ISXDIGIT (c);
133 64873 : break;
134 :
135 0 : default:
136 0 : gfc_internal_error ("gfc_check_digit(): bad radix");
137 : }
138 :
139 31743999 : return r;
140 : }
141 :
142 :
143 : /* Match the digit string part of an integer if signflag is not set,
144 : the signed digit string part if signflag is set. If the buffer
145 : is NULL, we just count characters for the resolution pass. Returns
146 : the number of characters matched, -1 for no match. */
147 :
148 : static int
149 18555301 : match_digits (int signflag, int radix, char *buffer)
150 : {
151 18555301 : locus old_loc;
152 18555301 : int length;
153 18555301 : char c;
154 :
155 18555301 : length = 0;
156 18555301 : c = gfc_next_ascii_char ();
157 :
158 18555301 : if (signflag && (c == '+' || c == '-'))
159 : {
160 5716 : if (buffer != NULL)
161 2285 : *buffer++ = c;
162 5716 : gfc_gobble_whitespace ();
163 5716 : c = gfc_next_ascii_char ();
164 5716 : length++;
165 : }
166 :
167 18555301 : if (!gfc_check_digit (c, radix))
168 : return -1;
169 :
170 8893915 : length++;
171 8893915 : if (buffer != NULL)
172 4438228 : *buffer++ = c;
173 :
174 17433017 : for (;;)
175 : {
176 13163466 : old_loc = gfc_current_locus;
177 13163466 : c = gfc_next_ascii_char ();
178 :
179 13163466 : if (!gfc_check_digit (c, radix))
180 : break;
181 :
182 4269551 : if (buffer != NULL)
183 2131940 : *buffer++ = c;
184 4269551 : length++;
185 : }
186 :
187 8893915 : gfc_current_locus = old_loc;
188 :
189 8893915 : return length;
190 : }
191 :
192 : /* Convert an integer string to an expression node. */
193 :
194 : static gfc_expr *
195 4330038 : convert_integer (const char *buffer, int kind, int radix, locus *where)
196 : {
197 4330038 : gfc_expr *e;
198 4330038 : const char *t;
199 :
200 4330038 : e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 : /* A leading plus is allowed, but not by mpz_set_str. */
202 4330038 : if (buffer[0] == '+')
203 21 : t = buffer + 1;
204 : else
205 : t = buffer;
206 4330038 : mpz_set_str (e->value.integer, t, radix);
207 :
208 4330038 : return e;
209 : }
210 :
211 :
212 : /* Convert an unsigned string to an expression node. XXX:
213 : This needs a calculation modulo 2^n. TODO: Implement restriction
214 : that no unary minus is permitted. */
215 : static gfc_expr *
216 102290 : convert_unsigned (const char *buffer, int kind, int radix, locus *where)
217 : {
218 102290 : gfc_expr *e;
219 102290 : const char *t;
220 102290 : int k;
221 102290 : arith rc;
222 :
223 102290 : e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
224 : /* A leading plus is allowed, but not by mpz_set_str. */
225 102290 : if (buffer[0] == '+')
226 0 : t = buffer + 1;
227 : else
228 : t = buffer;
229 :
230 102290 : mpz_set_str (e->value.integer, t, radix);
231 :
232 102290 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
233 :
234 : /* TODO Maybe move this somewhere else. */
235 102290 : rc = gfc_range_check (e);
236 102290 : if (rc != ARITH_OK)
237 : {
238 2 : if (pedantic)
239 1 : gfc_error_now (gfc_arith_error (rc), &e->where);
240 : else
241 1 : gfc_warning (0, gfc_arith_error (rc), &e->where);
242 : }
243 :
244 102290 : gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
245 : false);
246 :
247 102290 : return e;
248 : }
249 :
250 : /* Convert a real string to an expression node. */
251 :
252 : static gfc_expr *
253 223546 : convert_real (const char *buffer, int kind, locus *where)
254 : {
255 223546 : gfc_expr *e;
256 :
257 223546 : e = gfc_get_constant_expr (BT_REAL, kind, where);
258 223546 : mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259 :
260 223546 : return e;
261 : }
262 :
263 :
264 : /* Convert a pair of real, constant expression nodes to a single
265 : complex expression node. */
266 :
267 : static gfc_expr *
268 7049 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 : {
270 7049 : gfc_expr *e;
271 :
272 7049 : e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 7049 : mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 : GFC_MPC_RND_MODE);
275 :
276 7049 : return e;
277 : }
278 :
279 :
280 : /* Match an integer (digit string and optional kind).
281 : A sign will be accepted if signflag is set. */
282 :
283 : static match
284 13515216 : match_integer_constant (gfc_expr **result, int signflag)
285 : {
286 13515216 : int length, kind, is_iso_c;
287 13515216 : locus old_loc;
288 13515216 : char *buffer;
289 13515216 : gfc_expr *e;
290 :
291 13515216 : old_loc = gfc_current_locus;
292 13515216 : gfc_gobble_whitespace ();
293 :
294 13515216 : length = match_digits (signflag, 10, NULL);
295 13515216 : gfc_current_locus = old_loc;
296 13515216 : if (length == -1)
297 : return MATCH_NO;
298 :
299 4331772 : buffer = (char *) alloca (length + 1);
300 4331772 : memset (buffer, '\0', length + 1);
301 :
302 4331772 : gfc_gobble_whitespace ();
303 :
304 4331772 : match_digits (signflag, 10, buffer);
305 :
306 4331772 : kind = get_kind (&is_iso_c);
307 4331772 : if (kind == -2)
308 4022946 : kind = gfc_default_integer_kind;
309 4331772 : if (kind == -1)
310 : return MATCH_ERROR;
311 :
312 4330042 : if (kind == 4 && flag_integer4_kind == 8)
313 0 : kind = 8;
314 :
315 4330042 : if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
316 : {
317 4 : gfc_error ("Integer kind %d at %C not available", kind);
318 4 : return MATCH_ERROR;
319 : }
320 :
321 4330038 : e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 4330038 : e->ts.is_c_interop = is_iso_c;
323 :
324 4330038 : if (gfc_range_check (e) != ARITH_OK)
325 : {
326 9892 : gfc_error ("Integer too big for its kind at %C. This check can be "
327 : "disabled with the option %<-fno-range-check%>");
328 :
329 9892 : gfc_free_expr (e);
330 9892 : return MATCH_ERROR;
331 : }
332 :
333 4320146 : *result = e;
334 4320146 : return MATCH_YES;
335 : }
336 :
337 : /* Match an unsigned constant (an integer with suffix u). No sign
338 : is currently accepted, in accordance with 24-116.txt, but that
339 : could be changed later. This is very much like the integer
340 : constant matching above, but with enough differences to put it into
341 : its own function. */
342 :
343 : static match
344 597690 : match_unsigned_constant (gfc_expr **result)
345 : {
346 597690 : int length, kind, is_iso_c;
347 597690 : locus old_loc;
348 597690 : char *buffer;
349 597690 : gfc_expr *e;
350 597690 : match m;
351 :
352 597690 : old_loc = gfc_current_locus;
353 597690 : gfc_gobble_whitespace ();
354 :
355 597690 : length = match_digits (/* signflag = */ false, 10, NULL);
356 :
357 597690 : if (length == -1)
358 477942 : goto fail;
359 :
360 119748 : m = gfc_match_char ('u');
361 119748 : if (m == MATCH_NO)
362 17458 : goto fail;
363 :
364 102290 : gfc_current_locus = old_loc;
365 :
366 102290 : buffer = (char *) alloca (length + 1);
367 102290 : memset (buffer, '\0', length + 1);
368 :
369 102290 : gfc_gobble_whitespace ();
370 :
371 102290 : match_digits (false, 10, buffer);
372 :
373 102290 : m = gfc_match_char ('u');
374 102290 : if (m == MATCH_NO)
375 0 : goto fail;
376 :
377 102290 : kind = get_kind (&is_iso_c);
378 102290 : if (kind == -2)
379 8357 : kind = gfc_default_unsigned_kind;
380 102290 : if (kind == -1)
381 : return MATCH_ERROR;
382 :
383 102290 : if (kind == 4 && flag_integer4_kind == 8)
384 0 : kind = 8;
385 :
386 102290 : if (gfc_validate_kind (BT_UNSIGNED, kind, true) < 0)
387 : {
388 0 : gfc_error ("Unsigned kind %d at %C not available", kind);
389 0 : return MATCH_ERROR;
390 : }
391 :
392 102290 : e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
393 102290 : e->ts.is_c_interop = is_iso_c;
394 :
395 102290 : *result = e;
396 102290 : return MATCH_YES;
397 :
398 495400 : fail:
399 495400 : gfc_current_locus = old_loc;
400 495400 : return MATCH_NO;
401 : }
402 :
403 : /* Match a Hollerith constant. */
404 :
405 : static match
406 6763330 : match_hollerith_constant (gfc_expr **result)
407 : {
408 6763330 : locus old_loc;
409 6763330 : gfc_expr *e = NULL;
410 6763330 : int num, pad;
411 6763330 : int i;
412 :
413 6763330 : old_loc = gfc_current_locus;
414 6763330 : gfc_gobble_whitespace ();
415 :
416 6763330 : if (match_integer_constant (&e, 0) == MATCH_YES
417 6763330 : && gfc_match_char ('h') == MATCH_YES)
418 : {
419 2636 : if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
420 14 : goto cleanup;
421 :
422 2622 : if (gfc_extract_int (e, &num, 1))
423 0 : goto cleanup;
424 2622 : if (num == 0)
425 : {
426 1 : gfc_error ("Invalid Hollerith constant: %L must contain at least "
427 : "one character", &old_loc);
428 1 : goto cleanup;
429 : }
430 2621 : if (e->ts.kind != gfc_default_integer_kind)
431 : {
432 1 : gfc_error ("Invalid Hollerith constant: Integer kind at %L "
433 : "should be default", &old_loc);
434 1 : goto cleanup;
435 : }
436 : else
437 : {
438 2620 : gfc_free_expr (e);
439 2620 : e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
440 : &gfc_current_locus);
441 :
442 : /* Calculate padding needed to fit default integer memory. */
443 2620 : pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
444 :
445 2620 : e->representation.string = XCNEWVEC (char, num + pad + 1);
446 :
447 14886 : for (i = 0; i < num; i++)
448 : {
449 12266 : gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
450 12266 : if (! gfc_wide_fits_in_byte (c))
451 : {
452 0 : gfc_error ("Invalid Hollerith constant at %L contains a "
453 : "wide character", &old_loc);
454 0 : goto cleanup;
455 : }
456 :
457 12266 : e->representation.string[i] = (unsigned char) c;
458 : }
459 :
460 : /* Now pad with blanks and end with a null char. */
461 11762 : for (i = 0; i < pad; i++)
462 9142 : e->representation.string[num + i] = ' ';
463 :
464 2620 : e->representation.string[num + i] = '\0';
465 2620 : e->representation.length = num + pad;
466 2620 : e->ts.u.pad = pad;
467 :
468 2620 : *result = e;
469 2620 : return MATCH_YES;
470 : }
471 : }
472 :
473 6760694 : gfc_free_expr (e);
474 6760694 : gfc_current_locus = old_loc;
475 6760694 : return MATCH_NO;
476 :
477 16 : cleanup:
478 16 : gfc_free_expr (e);
479 16 : return MATCH_ERROR;
480 : }
481 :
482 :
483 : /* Match a binary, octal or hexadecimal constant that can be found in
484 : a DATA statement. The standard permits b'010...', o'73...', and
485 : z'a1...' where b, o, and z can be capital letters. This function
486 : also accepts postfixed forms of the constants: '01...'b, '73...'o,
487 : and 'a1...'z. An additional extension is the use of x for z. */
488 :
489 : static match
490 6976566 : match_boz_constant (gfc_expr **result)
491 : {
492 6976566 : int radix, length, x_hex;
493 6976566 : locus old_loc, start_loc;
494 6976566 : char *buffer, post, delim;
495 6976566 : gfc_expr *e;
496 :
497 6976566 : start_loc = old_loc = gfc_current_locus;
498 6976566 : gfc_gobble_whitespace ();
499 :
500 6976566 : x_hex = 0;
501 6976566 : switch (post = gfc_next_ascii_char ())
502 : {
503 : case 'b':
504 : radix = 2;
505 : post = 0;
506 : break;
507 60091 : case 'o':
508 60091 : radix = 8;
509 60091 : post = 0;
510 60091 : break;
511 94580 : case 'x':
512 94580 : x_hex = 1;
513 : /* Fall through. */
514 : case 'z':
515 : radix = 16;
516 : post = 0;
517 : break;
518 : case '\'':
519 : /* Fall through. */
520 : case '\"':
521 : delim = post;
522 : post = 1;
523 : radix = 16; /* Set to accept any valid digit string. */
524 : break;
525 6680059 : default:
526 6680059 : goto backup;
527 : }
528 :
529 : /* No whitespace allowed here. */
530 :
531 60091 : if (post == 0)
532 296482 : delim = gfc_next_ascii_char ();
533 :
534 296507 : if (delim != '\'' && delim != '\"')
535 292338 : goto backup;
536 :
537 4169 : if (x_hex
538 4169 : && gfc_invalid_boz (G_("Hexadecimal constant at %L uses "
539 : "nonstandard X instead of Z"), &gfc_current_locus))
540 : return MATCH_ERROR;
541 :
542 4167 : old_loc = gfc_current_locus;
543 :
544 4167 : length = match_digits (0, radix, NULL);
545 4167 : if (length == -1)
546 : {
547 0 : gfc_error ("Empty set of digits in BOZ constant at %C");
548 0 : return MATCH_ERROR;
549 : }
550 :
551 4167 : if (gfc_next_ascii_char () != delim)
552 : {
553 0 : gfc_error ("Illegal character in BOZ constant at %C");
554 0 : return MATCH_ERROR;
555 : }
556 :
557 4167 : if (post == 1)
558 : {
559 25 : switch (gfc_next_ascii_char ())
560 : {
561 : case 'b':
562 : radix = 2;
563 : break;
564 6 : case 'o':
565 6 : radix = 8;
566 6 : break;
567 13 : case 'x':
568 : /* Fall through. */
569 13 : case 'z':
570 13 : radix = 16;
571 13 : break;
572 0 : default:
573 0 : goto backup;
574 : }
575 :
576 25 : if (gfc_invalid_boz (G_("BOZ constant at %C uses nonstandard postfix "
577 : "syntax"), &gfc_current_locus))
578 : return MATCH_ERROR;
579 : }
580 :
581 4166 : gfc_current_locus = old_loc;
582 :
583 4166 : buffer = (char *) alloca (length + 1);
584 4166 : memset (buffer, '\0', length + 1);
585 :
586 4166 : match_digits (0, radix, buffer);
587 4166 : gfc_next_ascii_char (); /* Eat delimiter. */
588 4166 : if (post == 1)
589 24 : gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
590 :
591 4166 : e = gfc_get_expr ();
592 4166 : e->expr_type = EXPR_CONSTANT;
593 4166 : e->ts.type = BT_BOZ;
594 4166 : e->where = gfc_current_locus;
595 4166 : e->boz.rdx = radix;
596 4166 : e->boz.len = length;
597 4166 : e->boz.str = XCNEWVEC (char, length + 1);
598 4166 : strncpy (e->boz.str, buffer, length);
599 :
600 4166 : if (!gfc_in_match_data ()
601 4166 : && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
602 : "statement at %L", &e->where)))
603 : return MATCH_ERROR;
604 :
605 4161 : *result = e;
606 4161 : return MATCH_YES;
607 :
608 6972397 : backup:
609 6972397 : gfc_current_locus = start_loc;
610 6972397 : return MATCH_NO;
611 : }
612 :
613 :
614 : /* Match a real constant of some sort. Allow a signed constant if signflag
615 : is nonzero. */
616 :
617 : static match
618 7080372 : match_real_constant (gfc_expr **result, int signflag)
619 : {
620 7080372 : int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 7080372 : locus old_loc, temp_loc;
622 7080372 : char *p, *buffer, c, exp_char;
623 7080372 : gfc_expr *e;
624 7080372 : bool negate;
625 :
626 7080372 : old_loc = gfc_current_locus;
627 7080372 : gfc_gobble_whitespace ();
628 :
629 7080372 : e = NULL;
630 :
631 7080372 : default_exponent = 0;
632 7080372 : count = 0;
633 7080372 : seen_dp = 0;
634 7080372 : seen_digits = 0;
635 7080372 : exp_char = ' ';
636 7080372 : negate = false;
637 :
638 7080372 : c = gfc_next_ascii_char ();
639 7080372 : if (signflag && (c == '+' || c == '-'))
640 : {
641 6516 : if (c == '-')
642 6380 : negate = true;
643 :
644 6516 : gfc_gobble_whitespace ();
645 6516 : c = gfc_next_ascii_char ();
646 : }
647 :
648 : /* Scan significand. */
649 4029915 : for (;; c = gfc_next_ascii_char (), count++)
650 : {
651 11110287 : if (c == '.')
652 : {
653 284448 : if (seen_dp)
654 204 : goto done;
655 :
656 : /* Check to see if "." goes with a following operator like
657 : ".eq.". */
658 284244 : temp_loc = gfc_current_locus;
659 284244 : c = gfc_next_ascii_char ();
660 :
661 284244 : if (c == 'e' || c == 'd' || c == 'q')
662 : {
663 17768 : c = gfc_next_ascii_char ();
664 17768 : if (c == '.')
665 0 : goto done; /* Operator named .e. or .d. */
666 : }
667 :
668 284244 : if (ISALPHA (c))
669 67831 : goto done; /* Distinguish 1.e9 from 1.eq.2 */
670 :
671 216413 : gfc_current_locus = temp_loc;
672 216413 : seen_dp = 1;
673 216413 : continue;
674 : }
675 :
676 10825839 : if (ISDIGIT (c))
677 : {
678 3813502 : seen_digits = 1;
679 3813502 : continue;
680 : }
681 :
682 7012337 : break;
683 : }
684 :
685 7012337 : if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 2405245 : goto done;
687 38770 : exp_char = c;
688 :
689 :
690 38770 : if (c == 'q')
691 : {
692 0 : if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter %<q%> in "
693 : "real-literal-constant at %C"))
694 : return MATCH_ERROR;
695 0 : else if (warn_real_q_constant)
696 0 : gfc_warning (OPT_Wreal_q_constant,
697 : "Extension: exponent-letter %<q%> in real-literal-constant "
698 : "at %C");
699 : }
700 :
701 : /* Scan exponent. */
702 38770 : c = gfc_next_ascii_char ();
703 38770 : count++;
704 :
705 38770 : if (c == '+' || c == '-')
706 : { /* optional sign */
707 7807 : c = gfc_next_ascii_char ();
708 7807 : count++;
709 : }
710 :
711 38770 : if (!ISDIGIT (c))
712 : {
713 : /* With -fdec, default exponent to 0 instead of complaining. */
714 40 : if (flag_dec)
715 38760 : default_exponent = 1;
716 : else
717 : {
718 10 : gfc_error ("Missing exponent in real number at %C");
719 10 : return MATCH_ERROR;
720 : }
721 : }
722 :
723 80591 : while (ISDIGIT (c))
724 : {
725 41831 : c = gfc_next_ascii_char ();
726 41831 : count++;
727 : }
728 :
729 7080362 : done:
730 : /* Check that we have a numeric constant. */
731 7080362 : if (!seen_digits || (!seen_dp && exp_char == ' '))
732 : {
733 6856812 : gfc_current_locus = old_loc;
734 6856812 : return MATCH_NO;
735 : }
736 :
737 : /* Convert the number. */
738 223550 : gfc_current_locus = old_loc;
739 223550 : gfc_gobble_whitespace ();
740 :
741 223550 : buffer = (char *) alloca (count + default_exponent + 1);
742 223550 : memset (buffer, '\0', count + default_exponent + 1);
743 :
744 223550 : p = buffer;
745 223550 : c = gfc_next_ascii_char ();
746 223550 : if (c == '+' || c == '-')
747 : {
748 3085 : gfc_gobble_whitespace ();
749 3085 : c = gfc_next_ascii_char ();
750 : }
751 :
752 : /* Hack for mpfr_set_str(). */
753 1451850 : for (;;)
754 : {
755 837700 : if (c == 'd' || c == 'q')
756 : *p = 'e';
757 : else
758 807337 : *p = c;
759 837700 : p++;
760 837700 : if (--count == 0)
761 : break;
762 :
763 614150 : c = gfc_next_ascii_char ();
764 : }
765 223550 : if (default_exponent)
766 30 : *p++ = '0';
767 :
768 223550 : kind = get_kind (&is_iso_c);
769 223550 : if (kind == -1)
770 4 : goto cleanup;
771 :
772 223546 : if (kind == 4)
773 : {
774 20952 : if (flag_real4_kind == 8)
775 192 : kind = 8;
776 20952 : if (flag_real4_kind == 10)
777 192 : kind = 10;
778 20952 : if (flag_real4_kind == 16)
779 384 : kind = 16;
780 : }
781 202594 : else if (kind == 8)
782 : {
783 28173 : if (flag_real8_kind == 4)
784 192 : kind = 4;
785 28173 : if (flag_real8_kind == 10)
786 192 : kind = 10;
787 28173 : if (flag_real8_kind == 16)
788 384 : kind = 16;
789 : }
790 :
791 223546 : switch (exp_char)
792 : {
793 30363 : case 'd':
794 30363 : if (kind != -2)
795 : {
796 0 : gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
797 : "kind");
798 0 : goto cleanup;
799 : }
800 30363 : kind = gfc_default_double_kind;
801 30363 : break;
802 :
803 0 : case 'q':
804 0 : if (kind != -2)
805 : {
806 0 : gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
807 : "kind");
808 0 : goto cleanup;
809 : }
810 :
811 : /* The maximum possible real kind type parameter is 16. First, try
812 : that for the kind, then fallback to trying kind=10 (Intel 80 bit)
813 : extended precision. If neither value works, just given up. */
814 0 : kind = 16;
815 0 : if (gfc_validate_kind (BT_REAL, kind, true) < 0)
816 : {
817 0 : kind = 10;
818 0 : if (gfc_validate_kind (BT_REAL, kind, true) < 0)
819 : {
820 0 : gfc_error ("Invalid exponent-letter %<q%> in "
821 : "real-literal-constant at %C");
822 0 : goto cleanup;
823 : }
824 : }
825 : break;
826 :
827 193183 : default:
828 193183 : if (kind == -2)
829 119566 : kind = gfc_default_real_kind;
830 :
831 193183 : if (gfc_validate_kind (BT_REAL, kind, true) < 0)
832 : {
833 0 : gfc_error ("Invalid real kind %d at %C", kind);
834 0 : goto cleanup;
835 : }
836 : }
837 :
838 223546 : e = convert_real (buffer, kind, &gfc_current_locus);
839 223546 : if (negate)
840 2980 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 223546 : e->ts.is_c_interop = is_iso_c;
842 :
843 223546 : switch (gfc_range_check (e))
844 : {
845 : case ARITH_OK:
846 : break;
847 1 : case ARITH_OVERFLOW:
848 1 : gfc_error ("Real constant overflows its kind at %C");
849 1 : goto cleanup;
850 :
851 0 : case ARITH_UNDERFLOW:
852 0 : if (warn_underflow)
853 0 : gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
854 0 : mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
855 0 : break;
856 :
857 0 : default:
858 0 : gfc_internal_error ("gfc_range_check() returned bad value");
859 : }
860 :
861 : /* Warn about trailing digits which suggest the user added too many
862 : trailing digits, which may cause the appearance of higher precision
863 : than the kind can support.
864 :
865 : This is done by replacing the rightmost non-zero digit with zero
866 : and comparing with the original value. If these are equal, we
867 : assume the user supplied more digits than intended (or forgot to
868 : convert to the correct kind).
869 : */
870 :
871 223545 : if (warn_conversion_extra)
872 : {
873 21 : mpfr_t r;
874 21 : char *c1;
875 21 : bool did_break;
876 :
877 21 : c1 = strchr (buffer, 'e');
878 21 : if (c1 == NULL)
879 18 : c1 = buffer + strlen(buffer);
880 :
881 21 : did_break = false;
882 30 : for (p = c1; p > buffer;)
883 : {
884 30 : p--;
885 30 : if (*p == '.')
886 7 : continue;
887 :
888 23 : if (*p != '0')
889 : {
890 21 : *p = '0';
891 21 : did_break = true;
892 21 : break;
893 : }
894 : }
895 :
896 21 : if (did_break)
897 : {
898 21 : mpfr_init (r);
899 21 : mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
900 21 : if (negate)
901 0 : mpfr_neg (r, r, GFC_RND_MODE);
902 :
903 21 : mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
904 :
905 21 : if (mpfr_cmp_ui (r, 0) == 0)
906 1 : gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
907 : "in %qs number at %C, maybe incorrect KIND",
908 : gfc_typename (&e->ts));
909 :
910 21 : mpfr_clear (r);
911 : }
912 : }
913 :
914 223545 : *result = e;
915 223545 : return MATCH_YES;
916 :
917 5 : cleanup:
918 5 : gfc_free_expr (e);
919 5 : return MATCH_ERROR;
920 : }
921 :
922 :
923 : /* Match a substring reference. */
924 :
925 : static match
926 627427 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 : {
928 627427 : gfc_expr *start, *end;
929 627427 : locus old_loc;
930 627427 : gfc_ref *ref;
931 627427 : match m;
932 :
933 627427 : start = NULL;
934 627427 : end = NULL;
935 :
936 627427 : old_loc = gfc_current_locus;
937 :
938 627427 : m = gfc_match_char ('(');
939 627427 : if (m != MATCH_YES)
940 : return MATCH_NO;
941 :
942 17240 : if (gfc_match_char (':') != MATCH_YES)
943 : {
944 16362 : if (init)
945 0 : m = gfc_match_init_expr (&start);
946 : else
947 16362 : m = gfc_match_expr (&start);
948 :
949 16362 : if (m != MATCH_YES)
950 : {
951 154 : m = MATCH_NO;
952 154 : goto cleanup;
953 : }
954 :
955 16208 : m = gfc_match_char (':');
956 16208 : if (m != MATCH_YES)
957 460 : goto cleanup;
958 : }
959 :
960 16626 : if (gfc_match_char (')') != MATCH_YES)
961 : {
962 15697 : if (init)
963 0 : m = gfc_match_init_expr (&end);
964 : else
965 15697 : m = gfc_match_expr (&end);
966 :
967 15697 : if (m == MATCH_NO)
968 2 : goto syntax;
969 15695 : if (m == MATCH_ERROR)
970 0 : goto cleanup;
971 :
972 15695 : m = gfc_match_char (')');
973 15695 : if (m == MATCH_NO)
974 3 : goto syntax;
975 : }
976 :
977 : /* Optimize away the (:) reference. */
978 16621 : if (start == NULL && end == NULL && !deferred)
979 : ref = NULL;
980 : else
981 : {
982 16416 : ref = gfc_get_ref ();
983 :
984 16416 : ref->type = REF_SUBSTRING;
985 16416 : if (start == NULL)
986 671 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 16416 : ref->u.ss.start = start;
988 16416 : if (end == NULL && cl)
989 722 : end = gfc_copy_expr (cl->length);
990 16416 : ref->u.ss.end = end;
991 16416 : ref->u.ss.length = cl;
992 : }
993 :
994 16621 : *result = ref;
995 16621 : return MATCH_YES;
996 :
997 5 : syntax:
998 5 : gfc_error ("Syntax error in SUBSTRING specification at %C");
999 5 : m = MATCH_ERROR;
1000 :
1001 619 : cleanup:
1002 619 : gfc_free_expr (start);
1003 619 : gfc_free_expr (end);
1004 :
1005 619 : gfc_current_locus = old_loc;
1006 619 : return m;
1007 : }
1008 :
1009 :
1010 : /* Reads the next character of a string constant, taking care to
1011 : return doubled delimiters on the input as a single instance of
1012 : the delimiter.
1013 :
1014 : Special return values for "ret" argument are:
1015 : -1 End of the string, as determined by the delimiter
1016 : -2 Unterminated string detected
1017 :
1018 : Backslash codes are also expanded at this time. */
1019 :
1020 : static gfc_char_t
1021 4344343 : next_string_char (gfc_char_t delimiter, int *ret)
1022 : {
1023 4344343 : locus old_locus;
1024 4344343 : gfc_char_t c;
1025 :
1026 4344343 : c = gfc_next_char_literal (INSTRING_WARN);
1027 4344343 : *ret = 0;
1028 :
1029 4344343 : if (c == '\n')
1030 : {
1031 4 : *ret = -2;
1032 4 : return 0;
1033 : }
1034 :
1035 4344339 : if (flag_backslash && c == '\\')
1036 : {
1037 12180 : old_locus = gfc_current_locus;
1038 :
1039 12180 : if (gfc_match_special_char (&c) == MATCH_NO)
1040 0 : gfc_current_locus = old_locus;
1041 :
1042 12180 : if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
1043 0 : gfc_warning (0, "Extension: backslash character at %C");
1044 : }
1045 :
1046 4344339 : if (c != delimiter)
1047 : return c;
1048 :
1049 623800 : old_locus = gfc_current_locus;
1050 623800 : c = gfc_next_char_literal (NONSTRING);
1051 :
1052 623800 : if (c == delimiter)
1053 : return c;
1054 622982 : gfc_current_locus = old_locus;
1055 :
1056 622982 : *ret = -1;
1057 622982 : return 0;
1058 : }
1059 :
1060 :
1061 : /* Special case of gfc_match_name() that matches a parameter kind name
1062 : before a string constant. This takes case of the weird but legal
1063 : case of:
1064 :
1065 : kind_____'string'
1066 :
1067 : where kind____ is a parameter. gfc_match_name() will happily slurp
1068 : up all the underscores, which leads to problems. If we return
1069 : MATCH_YES, the parse pointer points to the final underscore, which
1070 : is not part of the name. We never return MATCH_ERROR-- errors in
1071 : the name will be detected later. */
1072 :
1073 : static match
1074 4558743 : match_charkind_name (char *name)
1075 : {
1076 4558743 : locus old_loc;
1077 4558743 : char c, peek;
1078 4558743 : int len;
1079 :
1080 4558743 : gfc_gobble_whitespace ();
1081 4558743 : c = gfc_next_ascii_char ();
1082 4558743 : if (!ISALPHA (c))
1083 : return MATCH_NO;
1084 :
1085 4144269 : *name++ = c;
1086 4144269 : len = 1;
1087 :
1088 16917671 : for (;;)
1089 : {
1090 16917671 : old_loc = gfc_current_locus;
1091 16917671 : c = gfc_next_ascii_char ();
1092 :
1093 16917671 : if (c == '_')
1094 : {
1095 551757 : peek = gfc_peek_ascii_char ();
1096 :
1097 551757 : if (peek == '\'' || peek == '\"')
1098 : {
1099 996 : gfc_current_locus = old_loc;
1100 996 : *name = '\0';
1101 996 : return MATCH_YES;
1102 : }
1103 : }
1104 :
1105 16916675 : if (!ISALNUM (c)
1106 4694034 : && c != '_'
1107 4143273 : && (c != '$' || !flag_dollar_ok))
1108 : break;
1109 :
1110 12773402 : *name++ = c;
1111 12773402 : if (++len > GFC_MAX_SYMBOL_LEN)
1112 : break;
1113 : }
1114 :
1115 : return MATCH_NO;
1116 : }
1117 :
1118 :
1119 : /* See if the current input matches a character constant. Lots of
1120 : contortions have to be done to match the kind parameter which comes
1121 : before the actual string. The main consideration is that we don't
1122 : want to error out too quickly. For example, we don't actually do
1123 : any validation of the kinds until we have actually seen a legal
1124 : delimiter. Using match_kind_param() generates errors too quickly. */
1125 :
1126 : static match
1127 7288051 : match_string_constant (gfc_expr **result)
1128 : {
1129 7288051 : char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 7288051 : size_t length;
1131 7288051 : int kind,save_warn_ampersand, ret;
1132 7288051 : locus old_locus, start_locus;
1133 7288051 : gfc_symbol *sym;
1134 7288051 : gfc_expr *e;
1135 7288051 : match m;
1136 7288051 : gfc_char_t c, delimiter, *p;
1137 :
1138 7288051 : old_locus = gfc_current_locus;
1139 :
1140 7288051 : gfc_gobble_whitespace ();
1141 :
1142 7288051 : c = gfc_next_char ();
1143 7288051 : if (c == '\'' || c == '"')
1144 : {
1145 270941 : kind = gfc_default_character_kind;
1146 270941 : start_locus = gfc_current_locus;
1147 270941 : goto got_delim;
1148 : }
1149 :
1150 7017110 : if (gfc_wide_is_digit (c))
1151 : {
1152 2458367 : kind = 0;
1153 :
1154 5895685 : while (gfc_wide_is_digit (c))
1155 : {
1156 3451109 : kind = kind * 10 + c - '0';
1157 3451109 : if (kind > 9999999)
1158 13791 : goto no_match;
1159 3437318 : c = gfc_next_char ();
1160 : }
1161 :
1162 : }
1163 : else
1164 : {
1165 4558743 : gfc_current_locus = old_locus;
1166 :
1167 4558743 : m = match_charkind_name (name);
1168 4558743 : if (m != MATCH_YES)
1169 4557747 : goto no_match;
1170 :
1171 996 : if (gfc_find_symbol (name, NULL, 1, &sym)
1172 996 : || sym == NULL
1173 1991 : || sym->attr.flavor != FL_PARAMETER)
1174 1 : goto no_match;
1175 :
1176 995 : kind = -1;
1177 995 : c = gfc_next_char ();
1178 : }
1179 :
1180 2445571 : if (c != '_')
1181 2255568 : goto no_match;
1182 :
1183 190003 : c = gfc_next_char ();
1184 190003 : if (c != '\'' && c != '"')
1185 149434 : goto no_match;
1186 :
1187 40569 : start_locus = gfc_current_locus;
1188 :
1189 40569 : if (kind == -1)
1190 : {
1191 995 : if (gfc_extract_int (sym->value, &kind, 1))
1192 : return MATCH_ERROR;
1193 995 : gfc_set_sym_referenced (sym);
1194 : }
1195 :
1196 40569 : if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1197 : {
1198 0 : gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
1199 0 : return MATCH_ERROR;
1200 : }
1201 :
1202 40569 : got_delim:
1203 : /* Scan the string into a block of memory by first figuring out how
1204 : long it is, allocating the structure, then re-reading it. This
1205 : isn't particularly efficient, but string constants aren't that
1206 : common in most code. TODO: Use obstacks? */
1207 :
1208 311510 : delimiter = c;
1209 311510 : length = 0;
1210 :
1211 4033176 : for (;;)
1212 : {
1213 2172343 : c = next_string_char (delimiter, &ret);
1214 2172343 : if (ret == -1)
1215 : break;
1216 1860837 : if (ret == -2)
1217 : {
1218 4 : gfc_current_locus = start_locus;
1219 4 : gfc_error ("Unterminated character constant beginning at %C");
1220 4 : return MATCH_ERROR;
1221 : }
1222 :
1223 1860833 : length++;
1224 : }
1225 :
1226 : /* Peek at the next character to see if it is a b, o, z, or x for the
1227 : postfixed BOZ literal constants. */
1228 311506 : peek = gfc_peek_ascii_char ();
1229 311506 : if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 25 : goto no_match;
1231 :
1232 311481 : e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233 :
1234 311481 : gfc_current_locus = start_locus;
1235 :
1236 : /* We disable the warning for the following loop as the warning has already
1237 : been printed in the loop above. */
1238 311481 : save_warn_ampersand = warn_ampersand;
1239 311481 : warn_ampersand = false;
1240 :
1241 311481 : p = e->value.character.string;
1242 2172000 : for (size_t i = 0; i < length; i++)
1243 : {
1244 1860524 : c = next_string_char (delimiter, &ret);
1245 :
1246 1860524 : if (!gfc_check_character_range (c, kind))
1247 : {
1248 5 : gfc_free_expr (e);
1249 5 : gfc_error ("Character %qs in string at %C is not representable "
1250 : "in character kind %d", gfc_print_wide_char (c), kind);
1251 5 : return MATCH_ERROR;
1252 : }
1253 :
1254 1860519 : *p++ = c;
1255 : }
1256 :
1257 311476 : *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 311476 : warn_ampersand = save_warn_ampersand;
1259 :
1260 311476 : next_string_char (delimiter, &ret);
1261 311476 : if (ret != -1)
1262 0 : gfc_internal_error ("match_string_constant(): Delimiter not found");
1263 :
1264 311476 : if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
1265 307 : e->expr_type = EXPR_SUBSTRING;
1266 :
1267 : /* Substrings with constant starting and ending points are eligible as
1268 : designators (F2018, section 9.1). Simplify substrings to make them usable
1269 : e.g. in data statements. */
1270 311476 : if (e->expr_type == EXPR_SUBSTRING
1271 307 : && e->ref && e->ref->type == REF_SUBSTRING
1272 303 : && e->ref->u.ss.start->expr_type == EXPR_CONSTANT
1273 76 : && (e->ref->u.ss.end == NULL
1274 74 : || e->ref->u.ss.end->expr_type == EXPR_CONSTANT))
1275 : {
1276 74 : gfc_expr *res;
1277 74 : ptrdiff_t istart, iend;
1278 74 : size_t length;
1279 74 : bool equal_length = false;
1280 :
1281 : /* Basic checks on substring starting and ending indices. */
1282 74 : if (!gfc_resolve_substring (e->ref, &equal_length))
1283 6 : return MATCH_ERROR;
1284 :
1285 71 : length = e->value.character.length;
1286 71 : istart = gfc_mpz_get_hwi (e->ref->u.ss.start->value.integer);
1287 71 : if (e->ref->u.ss.end == NULL)
1288 : iend = length;
1289 : else
1290 69 : iend = gfc_mpz_get_hwi (e->ref->u.ss.end->value.integer);
1291 :
1292 71 : if (istart <= iend)
1293 : {
1294 66 : if (istart < 1)
1295 : {
1296 2 : gfc_error ("Substring start index (%td) at %L below 1",
1297 2 : istart, &e->ref->u.ss.start->where);
1298 2 : return MATCH_ERROR;
1299 : }
1300 64 : if (iend > (ssize_t) length)
1301 : {
1302 1 : gfc_error ("Substring end index (%td) at %L exceeds string "
1303 1 : "length", iend, &e->ref->u.ss.end->where);
1304 1 : return MATCH_ERROR;
1305 : }
1306 63 : length = iend - istart + 1;
1307 : }
1308 : else
1309 : length = 0;
1310 :
1311 68 : res = gfc_get_constant_expr (BT_CHARACTER, e->ts.kind, &e->where);
1312 68 : res->value.character.string = gfc_get_wide_string (length + 1);
1313 68 : res->value.character.length = length;
1314 68 : if (length > 0)
1315 63 : memcpy (res->value.character.string,
1316 63 : &e->value.character.string[istart - 1],
1317 : length * sizeof (gfc_char_t));
1318 68 : res->value.character.string[length] = '\0';
1319 68 : e = res;
1320 : }
1321 :
1322 311470 : *result = e;
1323 :
1324 311470 : return MATCH_YES;
1325 :
1326 6976566 : no_match:
1327 6976566 : gfc_current_locus = old_locus;
1328 6976566 : return MATCH_NO;
1329 : }
1330 :
1331 :
1332 : /* Match a .true. or .false. Returns 1 if a .true. was found,
1333 : 0 if a .false. was found, and -1 otherwise. */
1334 : static int
1335 4552552 : match_logical_constant_string (void)
1336 : {
1337 4552552 : locus orig_loc = gfc_current_locus;
1338 :
1339 4552552 : gfc_gobble_whitespace ();
1340 4552552 : if (gfc_next_ascii_char () == '.')
1341 : {
1342 57482 : char ch = gfc_next_ascii_char ();
1343 57482 : if (ch == 'f')
1344 : {
1345 29343 : if (gfc_next_ascii_char () == 'a'
1346 29343 : && gfc_next_ascii_char () == 'l'
1347 29343 : && gfc_next_ascii_char () == 's'
1348 29343 : && gfc_next_ascii_char () == 'e'
1349 58686 : && gfc_next_ascii_char () == '.')
1350 : /* Matched ".false.". */
1351 : return 0;
1352 : }
1353 28139 : else if (ch == 't')
1354 : {
1355 28138 : if (gfc_next_ascii_char () == 'r'
1356 28138 : && gfc_next_ascii_char () == 'u'
1357 28138 : && gfc_next_ascii_char () == 'e'
1358 56276 : && gfc_next_ascii_char () == '.')
1359 : /* Matched ".true.". */
1360 : return 1;
1361 : }
1362 : }
1363 4495071 : gfc_current_locus = orig_loc;
1364 4495071 : return -1;
1365 : }
1366 :
1367 : /* Match a .true. or .false. */
1368 :
1369 : static match
1370 4552552 : match_logical_constant (gfc_expr **result)
1371 : {
1372 4552552 : gfc_expr *e;
1373 4552552 : int i, kind, is_iso_c;
1374 :
1375 4552552 : i = match_logical_constant_string ();
1376 4552552 : if (i == -1)
1377 : return MATCH_NO;
1378 :
1379 57481 : kind = get_kind (&is_iso_c);
1380 57481 : if (kind == -1)
1381 : return MATCH_ERROR;
1382 57481 : if (kind == -2)
1383 56986 : kind = gfc_default_logical_kind;
1384 :
1385 57481 : if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1386 : {
1387 4 : gfc_error ("Bad kind for logical constant at %C");
1388 4 : return MATCH_ERROR;
1389 : }
1390 :
1391 57477 : e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 57477 : e->ts.is_c_interop = is_iso_c;
1393 :
1394 57477 : *result = e;
1395 57477 : return MATCH_YES;
1396 : }
1397 :
1398 :
1399 : /* Match a real or imaginary part of a complex constant that is a
1400 : symbolic constant. */
1401 :
1402 : static match
1403 142159 : match_sym_complex_part (gfc_expr **result)
1404 : {
1405 142159 : char name[GFC_MAX_SYMBOL_LEN + 1];
1406 142159 : gfc_symbol *sym;
1407 142159 : gfc_expr *e;
1408 142159 : match m;
1409 :
1410 142159 : m = gfc_match_name (name);
1411 142159 : if (m != MATCH_YES)
1412 : return m;
1413 :
1414 39943 : if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 : return MATCH_NO;
1416 :
1417 37200 : if (sym->attr.flavor != FL_PARAMETER)
1418 : {
1419 : /* Give the matcher for implied do-loops a chance to run. This yields
1420 : a much saner error message for "write(*,*) (i, i=1, 6" where the
1421 : right parenthesis is missing. */
1422 35608 : char c;
1423 35608 : gfc_gobble_whitespace ();
1424 35608 : c = gfc_peek_ascii_char ();
1425 35608 : if (c == '=' || c == ',')
1426 : {
1427 : m = MATCH_NO;
1428 : }
1429 : else
1430 : {
1431 32592 : gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 32592 : m = MATCH_ERROR;
1433 : }
1434 : return m;
1435 : }
1436 :
1437 1592 : if (!sym->value)
1438 2 : goto error;
1439 :
1440 1590 : if (!gfc_numeric_ts (&sym->value->ts))
1441 : {
1442 334 : gfc_error ("Numeric PARAMETER required in complex constant at %C");
1443 334 : return MATCH_ERROR;
1444 : }
1445 :
1446 1256 : if (sym->value->rank != 0)
1447 : {
1448 174 : gfc_error ("Scalar PARAMETER required in complex constant at %C");
1449 174 : return MATCH_ERROR;
1450 : }
1451 :
1452 1082 : if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1453 : "complex constant at %C"))
1454 : return MATCH_ERROR;
1455 :
1456 1079 : switch (sym->value->ts.type)
1457 : {
1458 68 : case BT_REAL:
1459 68 : e = gfc_copy_expr (sym->value);
1460 68 : break;
1461 :
1462 1 : case BT_COMPLEX:
1463 1 : e = gfc_complex2real (sym->value, sym->value->ts.kind);
1464 1 : if (e == NULL)
1465 0 : goto error;
1466 : break;
1467 :
1468 1008 : case BT_INTEGER:
1469 1008 : e = gfc_int2real (sym->value, gfc_default_real_kind);
1470 1008 : if (e == NULL)
1471 0 : goto error;
1472 : break;
1473 :
1474 2 : case BT_UNSIGNED:
1475 2 : goto error;
1476 :
1477 0 : default:
1478 0 : gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1479 : }
1480 :
1481 1077 : *result = e; /* e is a scalar, real, constant expression. */
1482 1077 : return MATCH_YES;
1483 :
1484 4 : error:
1485 4 : gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1486 4 : return MATCH_ERROR;
1487 : }
1488 :
1489 :
1490 : /* Match a real or imaginary part of a complex number. */
1491 :
1492 : static match
1493 142159 : match_complex_part (gfc_expr **result)
1494 : {
1495 142159 : match m;
1496 :
1497 142159 : m = match_sym_complex_part (result);
1498 142159 : if (m != MATCH_NO)
1499 : return m;
1500 :
1501 107975 : m = match_real_constant (result, 1);
1502 107975 : if (m != MATCH_NO)
1503 : return m;
1504 :
1505 93482 : return match_integer_constant (result, 1);
1506 : }
1507 :
1508 :
1509 : /* Try to match a complex constant. */
1510 :
1511 : static match
1512 7298405 : match_complex_constant (gfc_expr **result)
1513 : {
1514 7298405 : gfc_expr *e, *real, *imag;
1515 7298405 : gfc_error_buffer old_error;
1516 7298405 : gfc_typespec target;
1517 7298405 : locus old_loc;
1518 7298405 : int kind;
1519 7298405 : match m;
1520 :
1521 7298405 : old_loc = gfc_current_locus;
1522 7298405 : real = imag = e = NULL;
1523 :
1524 7298405 : m = gfc_match_char ('(');
1525 7298405 : if (m != MATCH_YES)
1526 : return m;
1527 :
1528 131809 : gfc_push_error (&old_error);
1529 :
1530 131809 : m = match_complex_part (&real);
1531 131809 : if (m == MATCH_NO)
1532 : {
1533 75127 : gfc_free_error (&old_error);
1534 75127 : goto cleanup;
1535 : }
1536 :
1537 56682 : if (gfc_match_char (',') == MATCH_NO)
1538 : {
1539 : /* It is possible that gfc_int2real issued a warning when
1540 : converting an integer to real. Throw this away here. */
1541 :
1542 46328 : gfc_clear_warning ();
1543 46328 : gfc_pop_error (&old_error);
1544 46328 : m = MATCH_NO;
1545 46328 : goto cleanup;
1546 : }
1547 :
1548 : /* If m is error, then something was wrong with the real part and we
1549 : assume we have a complex constant because we've seen the ','. An
1550 : ambiguous case here is the start of an iterator list of some
1551 : sort. These sort of lists are matched prior to coming here. */
1552 :
1553 10354 : if (m == MATCH_ERROR)
1554 : {
1555 4 : gfc_free_error (&old_error);
1556 4 : goto cleanup;
1557 : }
1558 10350 : gfc_pop_error (&old_error);
1559 :
1560 10350 : m = match_complex_part (&imag);
1561 10350 : if (m == MATCH_NO)
1562 3135 : goto syntax;
1563 7215 : if (m == MATCH_ERROR)
1564 153 : goto cleanup;
1565 :
1566 7062 : m = gfc_match_char (')');
1567 7062 : if (m == MATCH_NO)
1568 : {
1569 : /* Give the matcher for implied do-loops a chance to run. This
1570 : yields a much saner error message for (/ (i, 4=i, 6) /). */
1571 13 : if (gfc_peek_ascii_char () == '=')
1572 : {
1573 0 : m = MATCH_ERROR;
1574 0 : goto cleanup;
1575 : }
1576 : else
1577 13 : goto syntax;
1578 : }
1579 :
1580 7049 : if (m == MATCH_ERROR)
1581 0 : goto cleanup;
1582 :
1583 : /* Decide on the kind of this complex number. */
1584 7049 : if (real->ts.type == BT_REAL)
1585 : {
1586 6615 : if (imag->ts.type == BT_REAL)
1587 6590 : kind = gfc_kind_max (real, imag);
1588 : else
1589 25 : kind = real->ts.kind;
1590 : }
1591 : else
1592 : {
1593 434 : if (imag->ts.type == BT_REAL)
1594 7 : kind = imag->ts.kind;
1595 : else
1596 427 : kind = gfc_default_real_kind;
1597 : }
1598 7049 : gfc_clear_ts (&target);
1599 7049 : target.type = BT_REAL;
1600 7049 : target.kind = kind;
1601 :
1602 7049 : if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 435 : gfc_convert_type (real, &target, 2);
1604 7049 : if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 490 : gfc_convert_type (imag, &target, 2);
1606 :
1607 7049 : e = convert_complex (real, imag, kind);
1608 7049 : e->where = gfc_current_locus;
1609 :
1610 7049 : gfc_free_expr (real);
1611 7049 : gfc_free_expr (imag);
1612 :
1613 7049 : *result = e;
1614 7049 : return MATCH_YES;
1615 :
1616 3148 : syntax:
1617 3148 : gfc_error ("Syntax error in COMPLEX constant at %C");
1618 3148 : m = MATCH_ERROR;
1619 :
1620 124760 : cleanup:
1621 124760 : gfc_free_expr (e);
1622 124760 : gfc_free_expr (real);
1623 124760 : gfc_free_expr (imag);
1624 124760 : gfc_current_locus = old_loc;
1625 :
1626 124760 : return m;
1627 7298405 : }
1628 :
1629 :
1630 : /* Match constants in any of several forms. Returns nonzero for a
1631 : match, zero for no match. */
1632 :
1633 : match
1634 7298405 : gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 : {
1636 7298405 : match m;
1637 :
1638 7298405 : m = match_complex_constant (result);
1639 7298405 : if (m != MATCH_NO)
1640 : return m;
1641 :
1642 7288051 : m = match_string_constant (result);
1643 7288051 : if (m != MATCH_NO)
1644 : return m;
1645 :
1646 6976566 : m = match_boz_constant (result);
1647 6976566 : if (m != MATCH_NO)
1648 : return m;
1649 :
1650 6972397 : m = match_real_constant (result, signflag);
1651 6972397 : if (m != MATCH_NO)
1652 : return m;
1653 :
1654 6763330 : m = match_hollerith_constant (result);
1655 6763330 : if (m != MATCH_NO)
1656 : return m;
1657 :
1658 6760694 : if (flag_unsigned)
1659 : {
1660 597690 : m = match_unsigned_constant (result);
1661 597690 : if (m != MATCH_NO)
1662 : return m;
1663 : }
1664 :
1665 6658404 : m = match_integer_constant (result, signflag);
1666 6658404 : if (m != MATCH_NO)
1667 : return m;
1668 :
1669 4552552 : m = match_logical_constant (result);
1670 4552552 : if (m != MATCH_NO)
1671 : return m;
1672 :
1673 : return MATCH_NO;
1674 : }
1675 :
1676 :
1677 : /* This checks if a symbol is the return value of an encompassing function.
1678 : Function nesting can be maximally two levels deep, but we may have
1679 : additional local namespaces like BLOCK etc. */
1680 :
1681 : bool
1682 797210 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 : {
1684 797210 : if (!sym->attr.function || (sym->result != sym))
1685 : return false;
1686 1672178 : while (ns)
1687 : {
1688 947560 : if (ns->proc_name == sym)
1689 : return true;
1690 935539 : ns = ns->parent;
1691 : }
1692 : return false;
1693 : }
1694 :
1695 :
1696 : /* Match a single actual argument value. An actual argument is
1697 : usually an expression, but can also be a procedure name. If the
1698 : argument is a single name, it is not always possible to tell
1699 : whether the name is a dummy procedure or not. We treat these cases
1700 : by creating an argument that looks like a dummy procedure and
1701 : fixing things later during resolution. */
1702 :
1703 : static match
1704 2063825 : match_actual_arg (gfc_expr **result)
1705 : {
1706 2063825 : char name[GFC_MAX_SYMBOL_LEN + 1];
1707 2063825 : gfc_symtree *symtree;
1708 2063825 : locus where, w;
1709 2063825 : gfc_expr *e;
1710 2063825 : char c;
1711 :
1712 2063825 : gfc_gobble_whitespace ();
1713 2063825 : where = gfc_current_locus;
1714 :
1715 2063825 : switch (gfc_match_name (name))
1716 : {
1717 : case MATCH_ERROR:
1718 : return MATCH_ERROR;
1719 :
1720 : case MATCH_NO:
1721 : break;
1722 :
1723 1354078 : case MATCH_YES:
1724 1354078 : w = gfc_current_locus;
1725 1354078 : gfc_gobble_whitespace ();
1726 1354078 : c = gfc_next_ascii_char ();
1727 1354078 : gfc_current_locus = w;
1728 :
1729 1354078 : if (c != ',' && c != ')')
1730 : break;
1731 :
1732 709081 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1733 : break;
1734 : /* Handle error elsewhere. */
1735 :
1736 : /* Eliminate a couple of common cases where we know we don't
1737 : have a function argument. */
1738 709081 : if (symtree == NULL)
1739 : {
1740 14245 : gfc_get_sym_tree (name, NULL, &symtree, false);
1741 14245 : gfc_set_sym_referenced (symtree->n.sym);
1742 : }
1743 : else
1744 : {
1745 694836 : gfc_symbol *sym;
1746 :
1747 694836 : sym = symtree->n.sym;
1748 694836 : gfc_set_sym_referenced (sym);
1749 694836 : if (sym->attr.flavor == FL_NAMELIST)
1750 : {
1751 1171 : gfc_error ("Namelist %qs cannot be an argument at %L",
1752 : sym->name, &where);
1753 1171 : break;
1754 : }
1755 693665 : if (sym->attr.flavor != FL_PROCEDURE
1756 654292 : && sym->attr.flavor != FL_UNKNOWN)
1757 : break;
1758 :
1759 196191 : if (sym->attr.in_common && !sym->attr.proc_pointer)
1760 : {
1761 224 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE,
1762 : sym->name, &sym->declared_at))
1763 : return MATCH_ERROR;
1764 : break;
1765 : }
1766 :
1767 : /* If the symbol is a function with itself as the result and
1768 : is being defined, then we have a variable. */
1769 195967 : if (sym->attr.function && sym->result == sym)
1770 : {
1771 3874 : if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 : break;
1773 :
1774 3220 : if (sym->attr.entry
1775 55 : && (sym->ns == gfc_current_ns
1776 2 : || sym->ns == gfc_current_ns->parent))
1777 : {
1778 54 : gfc_entry_list *el = NULL;
1779 :
1780 54 : for (el = sym->ns->entries; el; el = el->next)
1781 54 : if (sym == el->sym)
1782 : break;
1783 :
1784 54 : if (el)
1785 : break;
1786 : }
1787 : }
1788 : }
1789 :
1790 209504 : e = gfc_get_expr (); /* Leave it unknown for now */
1791 209504 : e->symtree = symtree;
1792 209504 : e->expr_type = EXPR_VARIABLE;
1793 209504 : e->ts.type = BT_PROCEDURE;
1794 209504 : e->where = where;
1795 :
1796 209504 : *result = e;
1797 209504 : return MATCH_YES;
1798 : }
1799 :
1800 1854321 : gfc_current_locus = where;
1801 1854321 : return gfc_match_expr (result);
1802 : }
1803 :
1804 :
1805 : /* Match a keyword argument or type parameter spec list.. */
1806 :
1807 : static match
1808 2055847 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 : {
1810 2055847 : char name[GFC_MAX_SYMBOL_LEN + 1];
1811 2055847 : gfc_actual_arglist *a;
1812 2055847 : locus name_locus;
1813 2055847 : match m;
1814 :
1815 2055847 : name_locus = gfc_current_locus;
1816 2055847 : m = gfc_match_name (name);
1817 :
1818 2055847 : if (m != MATCH_YES)
1819 598597 : goto cleanup;
1820 1457250 : if (gfc_match_char ('=') != MATCH_YES)
1821 : {
1822 1293399 : m = MATCH_NO;
1823 1293399 : goto cleanup;
1824 : }
1825 :
1826 163851 : if (pdt)
1827 : {
1828 556 : if (gfc_match_char ('*') == MATCH_YES)
1829 : {
1830 92 : actual->spec_type = SPEC_ASSUMED;
1831 92 : goto add_name;
1832 : }
1833 464 : else if (gfc_match_char (':') == MATCH_YES)
1834 : {
1835 55 : actual->spec_type = SPEC_DEFERRED;
1836 55 : goto add_name;
1837 : }
1838 : else
1839 409 : actual->spec_type = SPEC_EXPLICIT;
1840 : }
1841 :
1842 163704 : m = match_actual_arg (&actual->expr);
1843 163704 : if (m != MATCH_YES)
1844 11788 : goto cleanup;
1845 :
1846 : /* Make sure this name has not appeared yet. */
1847 151916 : add_name:
1848 152063 : if (name[0] != '\0')
1849 : {
1850 488095 : for (a = base; a; a = a->next)
1851 336046 : if (a->name != NULL && strcmp (a->name, name) == 0)
1852 : {
1853 14 : gfc_error ("Keyword %qs at %C has already appeared in the "
1854 : "current argument list", name);
1855 14 : return MATCH_ERROR;
1856 : }
1857 : }
1858 :
1859 152049 : actual->name = gfc_get_string ("%s", name);
1860 152049 : return MATCH_YES;
1861 :
1862 1903784 : cleanup:
1863 1903784 : gfc_current_locus = name_locus;
1864 1903784 : return m;
1865 : }
1866 :
1867 :
1868 : /* Match an argument list function, such as %VAL. */
1869 :
1870 : static match
1871 2015655 : match_arg_list_function (gfc_actual_arglist *result)
1872 : {
1873 2015655 : char name[GFC_MAX_SYMBOL_LEN + 1];
1874 2015655 : locus old_locus;
1875 2015655 : match m;
1876 :
1877 2015655 : old_locus = gfc_current_locus;
1878 :
1879 2015655 : if (gfc_match_char ('%') != MATCH_YES)
1880 : {
1881 2015590 : m = MATCH_NO;
1882 2015590 : goto cleanup;
1883 : }
1884 :
1885 65 : m = gfc_match ("%n (", name);
1886 65 : if (m != MATCH_YES)
1887 0 : goto cleanup;
1888 :
1889 65 : if (name[0] != '\0')
1890 : {
1891 65 : switch (name[0])
1892 : {
1893 16 : case 'l':
1894 16 : if (startswith (name, "loc"))
1895 : {
1896 16 : result->name = "%LOC";
1897 16 : break;
1898 : }
1899 : /* FALLTHRU */
1900 12 : case 'r':
1901 12 : if (startswith (name, "ref"))
1902 : {
1903 12 : result->name = "%REF";
1904 12 : break;
1905 : }
1906 : /* FALLTHRU */
1907 37 : case 'v':
1908 37 : if (startswith (name, "val"))
1909 : {
1910 37 : result->name = "%VAL";
1911 37 : break;
1912 : }
1913 : /* FALLTHRU */
1914 0 : default:
1915 0 : m = MATCH_ERROR;
1916 0 : goto cleanup;
1917 : }
1918 : }
1919 :
1920 65 : if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
1921 : {
1922 1 : m = MATCH_ERROR;
1923 1 : goto cleanup;
1924 : }
1925 :
1926 64 : m = match_actual_arg (&result->expr);
1927 64 : if (m != MATCH_YES)
1928 0 : goto cleanup;
1929 :
1930 64 : if (gfc_match_char (')') != MATCH_YES)
1931 : {
1932 0 : m = MATCH_NO;
1933 0 : goto cleanup;
1934 : }
1935 :
1936 : return MATCH_YES;
1937 :
1938 2015591 : cleanup:
1939 2015591 : gfc_current_locus = old_locus;
1940 2015591 : return m;
1941 : }
1942 :
1943 :
1944 : /* Matches an actual argument list of a function or subroutine, from
1945 : the opening parenthesis to the closing parenthesis. The argument
1946 : list is assumed to allow keyword arguments because we don't know if
1947 : the symbol associated with the procedure has an implicit interface
1948 : or not. We make sure keywords are unique. If sub_flag is set,
1949 : we're matching the argument list of a subroutine.
1950 :
1951 : NOTE: An alternative use for this function is to match type parameter
1952 : spec lists, which are so similar to actual argument lists that the
1953 : machinery can be reused. This use is flagged by the optional argument
1954 : 'pdt'. */
1955 :
1956 : match
1957 2152286 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 : {
1959 2152286 : gfc_actual_arglist *head, *tail;
1960 2152286 : int seen_keyword;
1961 2152286 : gfc_st_label *label;
1962 2152286 : locus old_loc;
1963 2152286 : match m;
1964 :
1965 2152286 : *argp = tail = NULL;
1966 2152286 : old_loc = gfc_current_locus;
1967 :
1968 2152286 : seen_keyword = 0;
1969 :
1970 2152286 : if (gfc_match_char ('(') == MATCH_NO)
1971 657893 : return (sub_flag) ? MATCH_YES : MATCH_NO;
1972 :
1973 1494393 : if (gfc_match_char (')') == MATCH_YES)
1974 : return MATCH_YES;
1975 :
1976 1464018 : head = NULL;
1977 :
1978 1464018 : matching_actual_arglist++;
1979 :
1980 2055169 : for (;;)
1981 : {
1982 2055169 : if (head == NULL)
1983 1464018 : head = tail = gfc_get_actual_arglist ();
1984 : else
1985 : {
1986 591151 : tail->next = gfc_get_actual_arglist ();
1987 591151 : tail = tail->next;
1988 : }
1989 :
1990 2055169 : if (sub_flag && !pdt && gfc_match_char ('*') == MATCH_YES)
1991 : {
1992 238 : m = gfc_match_st_label (&label);
1993 238 : if (m == MATCH_NO)
1994 0 : gfc_error ("Expected alternate return label at %C");
1995 238 : if (m != MATCH_YES)
1996 0 : goto cleanup;
1997 :
1998 238 : if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
1999 : "at %C"))
2000 0 : goto cleanup;
2001 :
2002 238 : tail->label = label;
2003 238 : goto next;
2004 : }
2005 :
2006 2054931 : if (pdt && !seen_keyword)
2007 : {
2008 2511 : if (gfc_match_char (':') == MATCH_YES)
2009 : {
2010 144 : tail->spec_type = SPEC_DEFERRED;
2011 144 : goto next;
2012 : }
2013 2367 : else if (gfc_match_char ('*') == MATCH_YES)
2014 : {
2015 429 : tail->spec_type = SPEC_ASSUMED;
2016 429 : goto next;
2017 : }
2018 : else
2019 1938 : tail->spec_type = SPEC_EXPLICIT;
2020 :
2021 1938 : m = match_keyword_arg (tail, head, pdt);
2022 1938 : if (m == MATCH_YES)
2023 : {
2024 384 : seen_keyword = 1;
2025 384 : goto next;
2026 : }
2027 1554 : if (m == MATCH_ERROR)
2028 0 : goto cleanup;
2029 : }
2030 :
2031 : /* After the first keyword argument is seen, the following
2032 : arguments must also have keywords. */
2033 2053974 : if (seen_keyword)
2034 : {
2035 38319 : m = match_keyword_arg (tail, head, pdt);
2036 :
2037 38319 : if (m == MATCH_ERROR)
2038 34 : goto cleanup;
2039 38285 : if (m == MATCH_NO)
2040 : {
2041 1401 : gfc_error ("Missing keyword name in actual argument list at %C");
2042 1401 : goto cleanup;
2043 : }
2044 :
2045 : }
2046 : else
2047 : {
2048 : /* Try an argument list function, like %VAL. */
2049 2015655 : m = match_arg_list_function (tail);
2050 2015655 : if (m == MATCH_ERROR)
2051 1 : goto cleanup;
2052 :
2053 : /* See if we have the first keyword argument. */
2054 2015654 : if (m == MATCH_NO)
2055 : {
2056 2015590 : m = match_keyword_arg (tail, head, false);
2057 2015590 : if (m == MATCH_YES)
2058 : seen_keyword = 1;
2059 1900809 : if (m == MATCH_ERROR)
2060 752 : goto cleanup;
2061 : }
2062 :
2063 2014838 : if (m == MATCH_NO)
2064 : {
2065 : /* Try for a non-keyword argument. */
2066 1900057 : m = match_actual_arg (&tail->expr);
2067 1900057 : if (m == MATCH_ERROR)
2068 2301 : goto cleanup;
2069 1897756 : if (m == MATCH_NO)
2070 20516 : goto syntax;
2071 : }
2072 : }
2073 :
2074 : /* PDT kind expressions are acceptable as initialization expressions.
2075 : However, intrinsics with a KIND argument reject them. Convert the
2076 : expression now by use of the component initializer. */
2077 2028969 : if (tail->expr
2078 2028885 : && tail->expr->expr_type == EXPR_VARIABLE
2079 4057854 : && gfc_expr_attr (tail->expr).pdt_kind)
2080 : {
2081 592 : gfc_ref *ref;
2082 592 : gfc_expr *tmp = NULL;
2083 614 : for (ref = tail->expr->ref; ref; ref = ref->next)
2084 22 : if (!ref->next && ref->type == REF_COMPONENT
2085 22 : && ref->u.c.component->attr.pdt_kind
2086 22 : && ref->u.c.component->initializer)
2087 22 : tmp = gfc_copy_expr (ref->u.c.component->initializer);
2088 592 : if (tmp)
2089 22 : gfc_replace_expr (tail->expr, tmp);
2090 : }
2091 :
2092 2030164 : next:
2093 2030164 : if (gfc_match_char (')') == MATCH_YES)
2094 : break;
2095 600186 : if (gfc_match_char (',') != MATCH_YES)
2096 9035 : goto syntax;
2097 : }
2098 :
2099 1429978 : *argp = head;
2100 1429978 : matching_actual_arglist--;
2101 1429978 : return MATCH_YES;
2102 :
2103 29551 : syntax:
2104 29551 : gfc_error ("Syntax error in argument list at %C");
2105 :
2106 34040 : cleanup:
2107 34040 : gfc_free_actual_arglist (head);
2108 34040 : gfc_current_locus = old_loc;
2109 34040 : matching_actual_arglist--;
2110 34040 : return MATCH_ERROR;
2111 : }
2112 :
2113 :
2114 : /* Used by gfc_match_varspec() to extend the reference list by one
2115 : element. */
2116 :
2117 : static gfc_ref *
2118 760275 : extend_ref (gfc_expr *primary, gfc_ref *tail)
2119 : {
2120 760275 : if (primary->ref == NULL)
2121 686010 : primary->ref = tail = gfc_get_ref ();
2122 74265 : else if (tail == NULL)
2123 : {
2124 : /* Set tail to end of reference chain. */
2125 47 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2126 47 : if (ref->next == NULL)
2127 : {
2128 : tail = ref;
2129 : break;
2130 : }
2131 : }
2132 : else
2133 : {
2134 74227 : tail->next = gfc_get_ref ();
2135 74227 : tail = tail->next;
2136 : }
2137 :
2138 760275 : return tail;
2139 : }
2140 :
2141 :
2142 : /* Used by gfc_match_varspec() to match an inquiry reference. */
2143 :
2144 : bool
2145 5467 : is_inquiry_ref (const char *name, gfc_ref **ref)
2146 : {
2147 5467 : inquiry_type type;
2148 :
2149 5467 : if (name == NULL)
2150 : return false;
2151 :
2152 5467 : if (ref) *ref = NULL;
2153 :
2154 5467 : if (strcmp (name, "re") == 0)
2155 : type = INQUIRY_RE;
2156 4008 : else if (strcmp (name, "im") == 0)
2157 : type = INQUIRY_IM;
2158 2982 : else if (strcmp (name, "kind") == 0)
2159 : type = INQUIRY_KIND;
2160 2271 : else if (strcmp (name, "len") == 0)
2161 : type = INQUIRY_LEN;
2162 : else
2163 : return false;
2164 :
2165 4084 : if (ref)
2166 : {
2167 2273 : *ref = gfc_get_ref ();
2168 2273 : (*ref)->type = REF_INQUIRY;
2169 2273 : (*ref)->u.i = type;
2170 : }
2171 :
2172 : return true;
2173 : }
2174 :
2175 :
2176 : /* Skip any parenthesized or bracketed groups at the current locus. */
2177 :
2178 : static void
2179 1500 : skip_ref_groups (void)
2180 : {
2181 1817 : for (;;)
2182 : {
2183 1817 : gfc_gobble_whitespace ();
2184 1817 : gfc_char_t c = gfc_peek_char ();
2185 1817 : if (c != '(' && c != '[')
2186 : return;
2187 :
2188 : int depth = 0;
2189 : gfc_char_t quote = ' ';
2190 1949 : do
2191 : {
2192 2045 : c = gfc_next_char_literal (quote == ' ' ? NONSTRING
2193 : : INSTRING_NOWARN);
2194 1949 : if (c == '\n')
2195 : return;
2196 1949 : if (quote != ' ')
2197 : {
2198 96 : if (c == quote)
2199 24 : quote = ' ';
2200 : }
2201 1853 : else if (c == '\'' || c == '"')
2202 : quote = c;
2203 : else if (c == '(' || c == '[')
2204 377 : depth++;
2205 : else if (c == ')' || c == ']')
2206 377 : depth--;
2207 : }
2208 1949 : while (depth > 0);
2209 : }
2210 : }
2211 :
2212 :
2213 : /* Return true if the component references at the current locus can be
2214 : followed starting from derived type DT. The locus is not changed. */
2215 :
2216 : static bool
2217 1099 : inferred_type_chain_ok (gfc_symbol *dt)
2218 : {
2219 1099 : char name[GFC_MAX_SYMBOL_LEN + 1];
2220 1099 : locus old_loc = gfc_current_locus;
2221 1099 : bool ok = true;
2222 :
2223 1653 : while (gfc_match_name (name) == MATCH_YES)
2224 : {
2225 1653 : gfc_component *c = gfc_find_component (dt, name, true, true, NULL);
2226 1653 : if (!c)
2227 : {
2228 68 : ok = dt->f2k_derived
2229 68 : && gfc_find_typebound_proc (dt, NULL, name, true, NULL);
2230 : break;
2231 : }
2232 :
2233 : /* A subscript needs an array, a substring or a procedure pointer. */
2234 1585 : gfc_gobble_whitespace ();
2235 1585 : if (gfc_peek_ascii_char () == '('
2236 402 : && !c->as && c->ts.type != BT_CHARACTER && !c->attr.proc_pointer
2237 1824 : && !(c->ts.type == BT_CLASS && c->attr.class_ok
2238 154 : && CLASS_DATA (c)->as))
2239 : {
2240 : ok = false;
2241 : break;
2242 : }
2243 1500 : skip_ref_groups ();
2244 :
2245 1500 : if (gfc_match_char ('%') != MATCH_YES)
2246 : break;
2247 :
2248 626 : if (c->ts.type == BT_DERIVED)
2249 288 : dt = c->ts.u.derived;
2250 338 : else if (c->ts.type == BT_CLASS)
2251 : {
2252 266 : if (!c->attr.class_ok || !CLASS_DATA (c))
2253 : break;
2254 266 : dt = CLASS_DATA (c)->ts.u.derived;
2255 : }
2256 : else
2257 : {
2258 72 : ok = gfc_match_name (name) == MATCH_YES
2259 72 : && is_inquiry_ref (name, NULL);
2260 : break;
2261 : }
2262 : }
2263 :
2264 1099 : gfc_current_locus = old_loc;
2265 1099 : return ok;
2266 : }
2267 :
2268 :
2269 : /* Check to see if functions in operator expressions can be resolved now. */
2270 :
2271 : static bool
2272 126 : resolvable_fcns (gfc_expr *e,
2273 : gfc_symbol *sym ATTRIBUTE_UNUSED,
2274 : int *f ATTRIBUTE_UNUSED)
2275 : {
2276 126 : bool p;
2277 126 : gfc_symbol *s;
2278 :
2279 126 : if (e->expr_type != EXPR_FUNCTION)
2280 : return false;
2281 :
2282 54 : s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2283 54 : p = s && (s->attr.use_assoc
2284 54 : || s->attr.host_assoc
2285 54 : || s->attr.if_source == IFSRC_DECL
2286 54 : || s->attr.proc == PROC_INTRINSIC
2287 24 : || gfc_is_intrinsic (s, 0, e->where));
2288 54 : return !p;
2289 : }
2290 :
2291 :
2292 : /* Match any additional specifications associated with the current
2293 : variable like member references or substrings. If equiv_flag is
2294 : set we only match stuff that is allowed inside an EQUIVALENCE
2295 : statement. sub_flag tells whether we expect a type-bound procedure found
2296 : to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2297 : components, 'ppc_arg' determines whether the PPC may be called (with an
2298 : argument list), or whether it may just be referred to as a pointer. */
2299 :
2300 : match
2301 5237313 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2302 : bool ppc_arg)
2303 : {
2304 5237313 : char name[GFC_MAX_SYMBOL_LEN + 1];
2305 5237313 : gfc_ref *substring, *tail, *tmp;
2306 5237313 : gfc_component *component = NULL;
2307 5237313 : gfc_component *previous = NULL;
2308 5237313 : gfc_symbol *sym = primary->symtree->n.sym;
2309 5237313 : gfc_expr *tgt_expr = NULL;
2310 5237313 : match m;
2311 5237313 : bool unknown;
2312 5237313 : bool inquiry;
2313 5237313 : bool intrinsic;
2314 5237313 : bool inferred_type;
2315 5237313 : locus old_loc;
2316 5237313 : char peeked_char;
2317 :
2318 5237313 : tail = NULL;
2319 :
2320 5237313 : gfc_gobble_whitespace ();
2321 :
2322 5237313 : if (gfc_peek_ascii_char () == '[')
2323 : {
2324 3366 : if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2325 3366 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2326 141 : && CLASS_DATA (sym)->attr.dimension))
2327 : {
2328 0 : gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2329 : "besides the coarray designator %<[...]%> at %C");
2330 0 : return MATCH_ERROR;
2331 : }
2332 3366 : if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2333 3365 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2334 141 : && !CLASS_DATA (sym)->attr.codimension))
2335 : {
2336 1 : gfc_error ("Coarray designator at %C but %qs is not a coarray",
2337 : sym->name);
2338 1 : return MATCH_ERROR;
2339 : }
2340 : }
2341 :
2342 5237312 : if (sym->assoc && sym->assoc->target)
2343 5237312 : tgt_expr = sym->assoc->target;
2344 :
2345 5237312 : inferred_type = IS_INFERRED_TYPE (primary);
2346 :
2347 : /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
2348 : been parsed, can generate errors with array refs.. The SELECT TYPE
2349 : namespace is marked with 'assoc_name_inferred'. During resolution, this is
2350 : detected and gfc_fixup_inferred_type_refs is called. */
2351 5236011 : if (!inferred_type
2352 5236011 : && sym->attr.select_type_temporary
2353 24766 : && sym->ns->assoc_name_inferred
2354 368 : && !sym->attr.select_rank_temporary)
2355 1669 : inferred_type = true;
2356 :
2357 : /* Try to resolve a typebound generic procedure so that the associate name
2358 : has a chance to get a type before being used in a second, nested associate
2359 : statement. Note that a copy is used for resolution so that failure does
2360 : not result in a mutilated selector expression further down the line. */
2361 8519 : if (tgt_expr && !sym->assoc->dangling
2362 8519 : && tgt_expr->ts.type == BT_UNKNOWN
2363 2300 : && tgt_expr->symtree
2364 1861 : && tgt_expr->symtree->n.sym
2365 5237385 : && gfc_expr_attr (tgt_expr).generic
2366 5237385 : && ((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_template)
2367 72 : || (sym->ts.type == BT_CLASS
2368 0 : && CLASS_DATA (sym)->ts.u.derived->attr.pdt_template)))
2369 : {
2370 1 : gfc_expr *cpy = gfc_copy_expr (tgt_expr);
2371 1 : if (gfc_resolve_expr (cpy)
2372 1 : && cpy->ts.type != BT_UNKNOWN)
2373 : {
2374 1 : gfc_replace_expr (tgt_expr, cpy);
2375 1 : sym->ts = tgt_expr->ts;
2376 : }
2377 : else
2378 0 : gfc_free_expr (cpy);
2379 1 : if (gfc_expr_attr (tgt_expr).generic)
2380 5237312 : inferred_type = true;
2381 : }
2382 :
2383 : /* For associate names, we may not yet know whether they are arrays or not.
2384 : If the selector expression is unambiguously an array; eg. a full array
2385 : or an array section, then the associate name must be an array and we can
2386 : fix it now. Otherwise, if parentheses follow and it is not a character
2387 : type, we have to assume that it actually is one for now. The final
2388 : decision will be made at resolution, of course. */
2389 5237312 : if (sym->assoc
2390 33285 : && gfc_peek_ascii_char () == '('
2391 9950 : && sym->ts.type != BT_CLASS
2392 5247059 : && !sym->attr.dimension)
2393 : {
2394 468 : gfc_ref *ref = NULL;
2395 :
2396 468 : if (!sym->assoc->dangling && tgt_expr)
2397 : {
2398 408 : if (tgt_expr->expr_type == EXPR_VARIABLE)
2399 45 : gfc_resolve_expr (tgt_expr);
2400 :
2401 408 : ref = tgt_expr->ref;
2402 422 : for (; ref; ref = ref->next)
2403 14 : if (ref->type == REF_ARRAY
2404 7 : && (ref->u.ar.type == AR_FULL
2405 7 : || ref->u.ar.type == AR_SECTION))
2406 : break;
2407 : }
2408 :
2409 468 : if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2410 294 : && sym->assoc->st
2411 294 : && sym->assoc->st->n.sym
2412 294 : && sym->assoc->st->n.sym->attr.dimension == 0))
2413 : {
2414 294 : sym->attr.dimension = 1;
2415 294 : if (sym->as == NULL
2416 294 : && sym->assoc->st
2417 294 : && sym->assoc->st->n.sym
2418 294 : && sym->assoc->st->n.sym->as)
2419 0 : sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2420 : }
2421 : }
2422 5236844 : else if (sym->ts.type == BT_CLASS
2423 46803 : && !(sym->assoc && sym->assoc->ar)
2424 46731 : && tgt_expr
2425 298 : && tgt_expr->expr_type == EXPR_VARIABLE
2426 148 : && sym->ts.u.derived != tgt_expr->ts.u.derived)
2427 : {
2428 19 : gfc_resolve_expr (tgt_expr);
2429 19 : if (tgt_expr->rank)
2430 0 : sym->ts.u.derived = tgt_expr->ts.u.derived;
2431 : }
2432 :
2433 5237312 : peeked_char = gfc_peek_ascii_char ();
2434 1669 : if ((inferred_type && !sym->as && peeked_char == '(')
2435 5237007 : || (equiv_flag && peeked_char == '(') || peeked_char == '['
2436 5232071 : || sym->attr.codimension
2437 5213704 : || (sym->attr.dimension && sym->ts.type != BT_CLASS
2438 643432 : && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2439 643417 : && !(gfc_matching_procptr_assignment
2440 38 : && sym->attr.flavor == FL_PROCEDURE))
2441 9807625 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2442 46614 : && sym->ts.u.derived && CLASS_DATA (sym)
2443 46610 : && (CLASS_DATA (sym)->attr.dimension
2444 28163 : || CLASS_DATA (sym)->attr.codimension)))
2445 : {
2446 686013 : gfc_array_spec *as;
2447 21591 : bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2448 697126 : && sym->ts.type == BT_CHARACTER;
2449 686013 : gfc_ref *ref, *strarr = NULL;
2450 :
2451 686013 : tail = extend_ref (primary, tail);
2452 686013 : if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
2453 : {
2454 3 : gcc_assert (sym->attr.dimension);
2455 : /* Find array reference for substrings of character arrays. */
2456 3 : for (ref = primary->ref; ref && ref->next; ref = ref->next)
2457 3 : if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
2458 : {
2459 : strarr = ref;
2460 : break;
2461 : }
2462 : }
2463 : else
2464 686010 : tail->type = REF_ARRAY;
2465 :
2466 : /* In EQUIVALENCE, we don't know yet whether we are seeing
2467 : an array, character variable or array of character
2468 : variables. We'll leave the decision till resolve time. */
2469 :
2470 686013 : if (equiv_flag)
2471 : as = NULL;
2472 684012 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2473 19156 : as = CLASS_DATA (sym)->as;
2474 : else
2475 664856 : as = sym->as;
2476 :
2477 686013 : ref = strarr ? strarr : tail;
2478 686013 : m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
2479 : coarray_only);
2480 686013 : if (m != MATCH_YES)
2481 : return m;
2482 :
2483 685921 : gfc_gobble_whitespace ();
2484 685921 : if (coarray_only)
2485 : {
2486 2011 : primary->ts = sym->ts;
2487 2011 : goto check_substring;
2488 : }
2489 :
2490 683910 : if (equiv_flag && gfc_peek_ascii_char () == '(')
2491 : {
2492 74 : tail = extend_ref (primary, tail);
2493 74 : tail->type = REF_ARRAY;
2494 :
2495 74 : m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2496 74 : if (m != MATCH_YES)
2497 : return m;
2498 : }
2499 : }
2500 :
2501 5235209 : primary->ts = sym->ts;
2502 :
2503 5235209 : if (equiv_flag)
2504 : return MATCH_YES;
2505 :
2506 : /* With DEC extensions, member separator may be '.' or '%'. */
2507 5232263 : peeked_char = gfc_peek_ascii_char ();
2508 5232263 : m = gfc_match_member_sep (sym);
2509 5232263 : if (m == MATCH_ERROR)
2510 : return MATCH_ERROR;
2511 :
2512 5232262 : inquiry = false;
2513 5232262 : if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2514 145313 : && (primary->ts.type != BT_DERIVED || inferred_type))
2515 : {
2516 3117 : match mm;
2517 3117 : old_loc = gfc_current_locus;
2518 3117 : mm = gfc_match_name (name);
2519 :
2520 : /* Check to see if this has a default complex. */
2521 645 : if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2522 3138 : && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2523 : {
2524 7 : gfc_set_default_type (sym, 0, sym->ns);
2525 7 : primary->ts = sym->ts;
2526 : }
2527 :
2528 : /* This is a usable inquiry reference, if the symbol is already known
2529 : to have a type or no derived types with a component of this name
2530 : can be found. If this was an inquiry reference with the same name
2531 : as a derived component and the associate-name type is not derived
2532 : or class, this is fixed up in 'gfc_fixup_inferred_type_refs'. */
2533 3117 : if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2534 5128 : && !(sym->ts.type == BT_UNKNOWN
2535 236 : && gfc_find_derived_types (sym, gfc_current_ns, name)))
2536 : inquiry = true;
2537 3117 : gfc_current_locus = old_loc;
2538 : }
2539 :
2540 : /* Use the default type if there is one. */
2541 2782688 : if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2542 5232900 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2543 0 : gfc_set_default_type (sym, 0, sym->ns);
2544 :
2545 : /* See if the type can be determined by resolution of the selector expression,
2546 : if allowable now, or inferred from references. */
2547 5232262 : if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2548 2784024 : && m == MATCH_YES)
2549 : {
2550 1763 : bool sym_present, resolved = false;
2551 1763 : gfc_symbol *tgt_sym;
2552 1763 : int ndt;
2553 :
2554 1763 : sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2555 1357 : tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
2556 :
2557 : /* These target expressions can be resolved at any time:
2558 : (i) With a declared symbol or intrinsic function; or
2559 : (ii) An operator expression,
2560 : just as long as (iii) all the functions in the expression have been
2561 : declared or are intrinsic. */
2562 1357 : if (((sym_present // (i)
2563 1357 : && (tgt_sym->attr.use_assoc
2564 1357 : || tgt_sym->attr.host_assoc
2565 1357 : || tgt_sym->attr.if_source == IFSRC_DECL
2566 1357 : || tgt_sym->attr.proc == PROC_INTRINSIC
2567 1357 : || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2568 1751 : || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2569 24 : && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2570 18 : && gfc_resolve_expr (tgt_expr))
2571 : {
2572 18 : sym->ts = tgt_expr->ts;
2573 18 : primary->ts = sym->ts;
2574 18 : resolved = true;
2575 : }
2576 :
2577 : /* If this hasn't done the trick and the target expression is a function,
2578 : or an unresolved operator expression, then this must be a derived type
2579 : if 'name' matches an accessible type both in this namespace and in the
2580 : as yet unparsed contained function. In principle, the type could have
2581 : already been inferred to be complex and yet a derived type with a
2582 : component name 're' or 'im' could be found. */
2583 18 : if (tgt_expr
2584 1381 : && (tgt_expr->expr_type == EXPR_FUNCTION
2585 85 : || tgt_expr->expr_type == EXPR_ARRAY
2586 73 : || (!resolved && tgt_expr->expr_type == EXPR_OP))
2587 1314 : && (sym->ts.type == BT_UNKNOWN
2588 709 : || (inferred_type && sym->ts.type != BT_COMPLEX))
2589 2939 : && (ndt = gfc_find_derived_types (sym, gfc_current_ns, name, true)))
2590 : {
2591 978 : sym->assoc->inferred_type = 1;
2592 : /* Pick a candidate type for now. The final determination is made in
2593 : 'gfc_fixup_inferred_type_refs'. Prefer the first candidate that
2594 : has the whole component chain. */
2595 1956 : gfc_symbol **dts = &sym->assoc->derived_types;
2596 978 : gfc_symbol *dt = *dts;
2597 1099 : for (gfc_symbol *cand = *dts; cand && ndt-- > 0; cand = cand->dt_next)
2598 1099 : if (inferred_type_chain_ok (cand))
2599 : {
2600 : dt = cand;
2601 : break;
2602 : }
2603 978 : tgt_expr->ts.type = BT_DERIVED;
2604 978 : tgt_expr->ts.kind = 0;
2605 978 : tgt_expr->ts.u.derived = dt;
2606 978 : sym->ts = tgt_expr->ts;
2607 978 : primary->ts = sym->ts;
2608 : /* Delete the dt list even if this process has to be done again for
2609 : another primary expression. */
2610 2050 : while (*dts && (*dts)->dt_next)
2611 : {
2612 1072 : gfc_symbol **tmp = &(*dts)->dt_next;
2613 1072 : *dts = NULL;
2614 1072 : dts = tmp;
2615 : }
2616 : }
2617 : /* If there is a usable inquiry reference not there are no matching
2618 : derived types, force the inquiry reference by setting unknown the
2619 : type of the primary expression. */
2620 356 : else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2621 833 : && !gfc_find_derived_types (sym, gfc_current_ns, name))
2622 48 : primary->ts.type = BT_UNKNOWN;
2623 :
2624 : /* Otherwise try resolving a copy of a component call. If it succeeds,
2625 : use that for the selector expression. */
2626 737 : else if (tgt_expr && tgt_expr->expr_type == EXPR_COMPCALL)
2627 : {
2628 1 : gfc_expr *cpy = gfc_copy_expr (tgt_expr);
2629 1 : if (gfc_resolve_expr (cpy))
2630 : {
2631 1 : gfc_replace_expr (tgt_expr, cpy);
2632 1 : sym->ts = tgt_expr->ts;
2633 : }
2634 : else
2635 0 : gfc_free_expr (cpy);
2636 : }
2637 :
2638 : /* An inquiry reference might determine the type, otherwise we have an
2639 : error. */
2640 1763 : if (sym->ts.type == BT_UNKNOWN && !inquiry)
2641 : {
2642 12 : gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2643 12 : return MATCH_ERROR;
2644 : }
2645 : }
2646 5230499 : else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2647 4982566 : && m == MATCH_YES && !inquiry)
2648 : {
2649 7 : gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2650 : peeked_char, sym->name);
2651 7 : return MATCH_ERROR;
2652 : }
2653 :
2654 5232243 : if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2655 251031 : || m != MATCH_YES)
2656 5066424 : goto check_substring;
2657 :
2658 165819 : if (!inquiry)
2659 164116 : sym = sym->ts.u.derived;
2660 : else
2661 165819 : sym = NULL;
2662 :
2663 192354 : for (;;)
2664 : {
2665 192354 : bool t;
2666 192354 : gfc_symtree *tbp;
2667 192354 : gfc_typespec *ts = &primary->ts;
2668 :
2669 192354 : m = gfc_match_name (name);
2670 192354 : if (m == MATCH_NO)
2671 0 : gfc_error ("Expected structure component name at %C");
2672 192354 : if (m != MATCH_YES)
2673 138 : return MATCH_ERROR;
2674 :
2675 : /* For derived type components find typespec of ultimate component. */
2676 192354 : if (ts->type == BT_DERIVED && primary->ref)
2677 : {
2678 158425 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2679 : {
2680 90805 : if (ref->type == REF_COMPONENT && ref->u.c.component)
2681 26246 : ts = &ref->u.c.component->ts;
2682 : }
2683 : }
2684 :
2685 192354 : intrinsic = false;
2686 192354 : if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
2687 : {
2688 2266 : inquiry = is_inquiry_ref (name, &tmp);
2689 2266 : if (inquiry)
2690 2261 : sym = NULL;
2691 :
2692 2266 : if (peeked_char == '%')
2693 : {
2694 2266 : if (tmp)
2695 : {
2696 2261 : gfc_symbol *s;
2697 2261 : switch (tmp->u.i)
2698 : {
2699 1369 : case INQUIRY_RE:
2700 1369 : case INQUIRY_IM:
2701 1369 : if (!gfc_notify_std (GFC_STD_F2008,
2702 : "RE or IM part_ref at %C"))
2703 : return MATCH_ERROR;
2704 : break;
2705 :
2706 414 : case INQUIRY_KIND:
2707 414 : if (!gfc_notify_std (GFC_STD_F2003,
2708 : "KIND part_ref at %C"))
2709 : return MATCH_ERROR;
2710 : break;
2711 :
2712 478 : case INQUIRY_LEN:
2713 478 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2714 : return MATCH_ERROR;
2715 : break;
2716 : }
2717 :
2718 : /* If necessary, infer the type of the primary expression
2719 : and the associate-name using the the inquiry ref.. */
2720 2252 : s = primary->symtree ? primary->symtree->n.sym : NULL;
2721 2224 : if (s && s->assoc && s->assoc->target
2722 594 : && (s->ts.type == BT_UNKNOWN
2723 450 : || (primary->ts.type == BT_UNKNOWN
2724 48 : && s->assoc->inferred_type
2725 48 : && s->ts.type == BT_DERIVED)))
2726 : {
2727 192 : if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2728 : {
2729 96 : s->ts.type = BT_COMPLEX;
2730 96 : s->ts.kind = gfc_default_real_kind;;
2731 96 : s->assoc->inferred_type = 1;
2732 96 : primary->ts = s->ts;
2733 : }
2734 96 : else if (tmp->u.i == INQUIRY_LEN)
2735 : {
2736 48 : s->ts.type = BT_CHARACTER;
2737 48 : s->ts.kind = gfc_default_character_kind;;
2738 48 : s->assoc->inferred_type = 1;
2739 48 : primary->ts = s->ts;
2740 : }
2741 48 : else if (s->ts.type == BT_UNKNOWN)
2742 : {
2743 : /* KIND inquiry gives no clue as to symbol type. */
2744 48 : primary->ref = tmp;
2745 48 : primary->ts.type = BT_INTEGER;
2746 48 : primary->ts.kind = gfc_default_integer_kind;
2747 48 : return MATCH_YES;
2748 : }
2749 : }
2750 :
2751 2204 : if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2752 1365 : && primary->ts.type != BT_COMPLEX)
2753 : {
2754 13 : gfc_error ("The RE or IM part_ref at %C must be "
2755 : "applied to a COMPLEX expression");
2756 13 : gfc_free_ref_list (tmp);
2757 13 : return MATCH_ERROR;
2758 : }
2759 2191 : else if (tmp->u.i == INQUIRY_LEN
2760 476 : && ts->type != BT_CHARACTER)
2761 : {
2762 6 : gfc_error ("The LEN part_ref at %C must be applied "
2763 : "to a CHARACTER expression");
2764 6 : gfc_free_ref_list (tmp);
2765 6 : return MATCH_ERROR;
2766 : }
2767 : }
2768 2190 : if (primary->ts.type != BT_UNKNOWN)
2769 192278 : intrinsic = true;
2770 : }
2771 : }
2772 : else
2773 : inquiry = false;
2774 :
2775 192278 : if (sym && sym->f2k_derived)
2776 187289 : tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2777 : else
2778 : tbp = NULL;
2779 :
2780 187289 : if (tbp)
2781 : {
2782 4190 : gfc_symbol* tbp_sym;
2783 :
2784 4190 : if (!t)
2785 : return MATCH_ERROR;
2786 :
2787 4188 : gcc_assert (!tail || !tail->next);
2788 :
2789 4188 : if (!(primary->expr_type == EXPR_VARIABLE
2790 : || (primary->expr_type == EXPR_STRUCTURE
2791 1 : && primary->symtree && primary->symtree->n.sym
2792 1 : && primary->symtree->n.sym->attr.flavor)))
2793 : return MATCH_ERROR;
2794 :
2795 4186 : if (tbp->n.tb->is_generic)
2796 : tbp_sym = NULL;
2797 : else
2798 3350 : tbp_sym = tbp->n.tb->u.specific->n.sym;
2799 :
2800 4186 : primary->expr_type = EXPR_COMPCALL;
2801 4186 : primary->value.compcall.tbp = tbp->n.tb;
2802 4186 : primary->value.compcall.name = tbp->name;
2803 4186 : primary->value.compcall.ignore_pass = 0;
2804 4186 : primary->value.compcall.assign = 0;
2805 4186 : primary->value.compcall.base_object = NULL;
2806 4186 : gcc_assert (primary->symtree->n.sym->attr.referenced);
2807 4186 : if (tbp_sym)
2808 3350 : primary->ts = tbp_sym->ts;
2809 : else
2810 836 : gfc_clear_ts (&primary->ts);
2811 :
2812 4186 : m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2813 : &primary->value.compcall.actual);
2814 4186 : if (m == MATCH_ERROR)
2815 : return MATCH_ERROR;
2816 4186 : if (m == MATCH_NO)
2817 : {
2818 180 : if (sub_flag)
2819 179 : primary->value.compcall.actual = NULL;
2820 : else
2821 : {
2822 : /* Before erroring, check whether there is also a data
2823 : component with this name. Use noaccess=true so
2824 : that private components are also found. */
2825 1 : if (sym && gfc_find_component (sym, name, true, true, NULL))
2826 : {
2827 : /* Restore expr to EXPR_VARIABLE and let the data
2828 : component path below handle it. */
2829 0 : primary->expr_type = EXPR_VARIABLE;
2830 0 : gfc_free_actual_arglist (primary->value.compcall.actual);
2831 0 : primary->value.compcall.actual = NULL;
2832 0 : tbp = NULL;
2833 0 : goto try_data_component;
2834 : }
2835 1 : gfc_error ("Expected argument list at %C");
2836 1 : return MATCH_ERROR;
2837 : }
2838 : }
2839 :
2840 165681 : break;
2841 : }
2842 :
2843 183099 : try_data_component:
2844 :
2845 188088 : previous = component;
2846 :
2847 188088 : if (!inquiry && !intrinsic)
2848 : {
2849 185900 : component = gfc_find_component (sym, name, false, false, &tmp);
2850 : /* For inferred-type ASSOCIATE names the parse-time candidate type
2851 : may not be the final type; a private component in the candidate
2852 : type may correspond to a public component in the correct type.
2853 : Accept it tentatively so that resolution can fix up the type. */
2854 185900 : if (!component && !tbp
2855 48 : && primary->symtree && primary->symtree->n.sym->assoc
2856 1 : && primary->symtree->n.sym->assoc->inferred_type)
2857 1 : component = gfc_find_component (sym, name, true, false, &tmp);
2858 : }
2859 : else
2860 : component = NULL;
2861 :
2862 188088 : if (previous && inquiry
2863 499 : && (previous->attr.pdt_kind || previous->attr.pdt_len))
2864 : {
2865 4 : gfc_error_now ("R901: A type parameter ref is not a designator and "
2866 : "cannot be followed by the type inquiry ref at %C");
2867 4 : return MATCH_ERROR;
2868 : }
2869 :
2870 188084 : if (intrinsic && !inquiry)
2871 : {
2872 3 : if (previous)
2873 2 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2874 : "type component %qs", name, previous->name);
2875 : else
2876 1 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2877 : "type component", name);
2878 : return MATCH_ERROR;
2879 : }
2880 188081 : else if (component == NULL && !inquiry)
2881 : return MATCH_ERROR;
2882 :
2883 : /* Extend the reference chain determined by gfc_find_component or
2884 : is_inquiry_ref. */
2885 188034 : if (primary->ref == NULL)
2886 112303 : primary->ref = tmp;
2887 : else
2888 : {
2889 : /* Find end of reference chain if inquiry reference and tail not
2890 : set. */
2891 75731 : if (tail == NULL && inquiry && tmp)
2892 35 : tail = extend_ref (primary, tail);
2893 :
2894 : /* Set by the for loop below for the last component ref. */
2895 75731 : gcc_assert (tail != NULL);
2896 75731 : tail->next = tmp;
2897 : }
2898 :
2899 : /* The reference chain may be longer than one hop for union
2900 : subcomponents; find the new tail. */
2901 190010 : for (tail = tmp; tail->next; tail = tail->next)
2902 : ;
2903 :
2904 188034 : if (tmp && tmp->type == REF_INQUIRY)
2905 : {
2906 2181 : if (!primary->where.u.lb || !primary->where.nextc)
2907 1997 : primary->where = gfc_current_locus;
2908 2181 : gfc_simplify_expr (primary, 0);
2909 :
2910 2181 : if (primary->expr_type == EXPR_CONSTANT)
2911 510 : goto check_done;
2912 :
2913 1671 : if (primary->ref == NULL)
2914 60 : goto check_done;
2915 :
2916 1611 : switch (tmp->u.i)
2917 : {
2918 1208 : case INQUIRY_RE:
2919 1208 : case INQUIRY_IM:
2920 1208 : if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2921 : return MATCH_ERROR;
2922 :
2923 1208 : if (primary->ts.type != BT_COMPLEX)
2924 : {
2925 0 : gfc_error ("The RE or IM part_ref at %C must be "
2926 : "applied to a COMPLEX expression");
2927 0 : return MATCH_ERROR;
2928 : }
2929 1208 : primary->ts.type = BT_REAL;
2930 1208 : break;
2931 :
2932 351 : case INQUIRY_LEN:
2933 351 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2934 : return MATCH_ERROR;
2935 :
2936 351 : if (primary->ts.type != BT_CHARACTER)
2937 : {
2938 0 : gfc_error ("The LEN part_ref at %C must be applied "
2939 : "to a CHARACTER expression");
2940 0 : return MATCH_ERROR;
2941 : }
2942 351 : primary->ts.u.cl = NULL;
2943 351 : primary->ts.deferred = false;
2944 351 : primary->ts.type = BT_INTEGER;
2945 351 : primary->ts.kind = gfc_default_integer_kind;
2946 351 : break;
2947 :
2948 52 : case INQUIRY_KIND:
2949 52 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2950 : return MATCH_ERROR;
2951 :
2952 52 : if (primary->ts.type == BT_CLASS
2953 52 : || primary->ts.type == BT_DERIVED)
2954 : {
2955 0 : gfc_error ("The KIND part_ref at %C must be applied "
2956 : "to an expression of intrinsic type");
2957 0 : return MATCH_ERROR;
2958 : }
2959 52 : if (primary->ts.type == BT_CHARACTER)
2960 : {
2961 0 : primary->ts.u.cl = NULL;
2962 0 : primary->ts.deferred = false;
2963 : }
2964 52 : primary->ts.type = BT_INTEGER;
2965 52 : primary->ts.kind = gfc_default_integer_kind;
2966 52 : break;
2967 :
2968 0 : default:
2969 0 : gcc_unreachable ();
2970 : }
2971 :
2972 1611 : goto check_done;
2973 : }
2974 :
2975 185853 : primary->ts = component->ts;
2976 :
2977 185853 : if (component->attr.proc_pointer && ppc_arg)
2978 : {
2979 : /* Procedure pointer component call: Look for argument list. */
2980 1260 : m = gfc_match_actual_arglist (sub_flag,
2981 : &primary->value.compcall.actual);
2982 1260 : if (m == MATCH_ERROR)
2983 : return MATCH_ERROR;
2984 :
2985 1260 : if (m == MATCH_NO && !gfc_matching_ptr_assignment
2986 346 : && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2987 : {
2988 3 : gfc_error ("Procedure pointer component %qs requires an "
2989 : "argument list at %C", component->name);
2990 3 : return MATCH_ERROR;
2991 : }
2992 :
2993 1257 : if (m == MATCH_YES)
2994 913 : primary->expr_type = EXPR_PPC;
2995 :
2996 : break;
2997 : }
2998 :
2999 184593 : if (component->as != NULL && !component->attr.proc_pointer)
3000 : {
3001 68490 : tail = extend_ref (primary, tail);
3002 68490 : tail->type = REF_ARRAY;
3003 :
3004 136980 : m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
3005 68490 : component->as->corank);
3006 68490 : if (m != MATCH_YES)
3007 : return m;
3008 : }
3009 116103 : else if (component->ts.type == BT_CLASS && component->attr.class_ok
3010 11672 : && CLASS_DATA (component)->as && !component->attr.proc_pointer)
3011 : {
3012 5663 : tail = extend_ref (primary, tail);
3013 5663 : tail->type = REF_ARRAY;
3014 :
3015 11326 : m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
3016 : equiv_flag,
3017 5663 : CLASS_DATA (component)->as->corank);
3018 5663 : if (m != MATCH_YES)
3019 : return m;
3020 : }
3021 :
3022 110440 : check_done:
3023 : /* In principle, we could have eg. expr%re%kind so we must allow for
3024 : this possibility. */
3025 186774 : if (gfc_match_char ('%') == MATCH_YES)
3026 : {
3027 26165 : if (component && (component->ts.type == BT_DERIVED
3028 3844 : || component->ts.type == BT_CLASS))
3029 25606 : sym = component->ts.u.derived;
3030 26165 : continue;
3031 : }
3032 160609 : else if (inquiry)
3033 : break;
3034 :
3035 148324 : if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
3036 166815 : || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
3037 : break;
3038 :
3039 370 : if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
3040 370 : sym = component->ts.u.derived;
3041 : }
3042 :
3043 5234116 : check_substring:
3044 5234116 : unknown = false;
3045 5234116 : if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
3046 : {
3047 2782050 : if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
3048 : {
3049 352 : gfc_set_default_type (sym, 0, sym->ns);
3050 352 : primary->ts = sym->ts;
3051 352 : unknown = true;
3052 : }
3053 : }
3054 :
3055 5234116 : if (primary->ts.type == BT_CHARACTER)
3056 : {
3057 314486 : bool def = primary->ts.deferred == 1;
3058 314486 : switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
3059 : {
3060 15317 : case MATCH_YES:
3061 15317 : if (tail == NULL)
3062 10086 : primary->ref = substring;
3063 : else
3064 5231 : tail->next = substring;
3065 :
3066 15317 : if (primary->expr_type == EXPR_CONSTANT)
3067 758 : primary->expr_type = EXPR_SUBSTRING;
3068 :
3069 15317 : if (substring)
3070 15137 : primary->ts.u.cl = NULL;
3071 :
3072 15317 : gfc_gobble_whitespace ();
3073 15317 : if (gfc_peek_ascii_char () == '(')
3074 : {
3075 5 : gfc_error_now ("Unexpected array/substring ref at %C");
3076 5 : return MATCH_ERROR;
3077 : }
3078 : break;
3079 :
3080 299169 : case MATCH_NO:
3081 299169 : if (unknown)
3082 : {
3083 351 : gfc_clear_ts (&primary->ts);
3084 351 : gfc_clear_ts (&sym->ts);
3085 : }
3086 : break;
3087 :
3088 : case MATCH_ERROR:
3089 : return MATCH_ERROR;
3090 : }
3091 : }
3092 :
3093 : /* F08:C611. */
3094 5234111 : if (primary->ts.type == BT_DERIVED && primary->ref
3095 30319 : && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
3096 : {
3097 6 : gfc_error ("Nonpolymorphic reference to abstract type at %C");
3098 6 : return MATCH_ERROR;
3099 : }
3100 :
3101 : /* F08:C727. */
3102 5234105 : if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
3103 : {
3104 3 : gfc_error ("Coindexed procedure-pointer component at %C");
3105 3 : return MATCH_ERROR;
3106 : }
3107 :
3108 : return MATCH_YES;
3109 : }
3110 :
3111 :
3112 : /* Given an expression that is a variable, figure out what the
3113 : ultimate variable's attribute is, traversing the reference
3114 : structures if necessary.
3115 :
3116 : This subroutine is trickier than it looks. We start at the base
3117 : symbol and store the attribute. Component references load a
3118 : completely new attribute.
3119 :
3120 : A couple of rules come into play. Subobjects of targets are always
3121 : targets themselves. If we see a component that goes through a
3122 : pointer, then the expression must also be a target, since the
3123 : pointer is associated with something (if it isn't core will soon be
3124 : dumped). If we see a full part or section of an array, the
3125 : expression is also an array.
3126 :
3127 : We can have at most one full array reference. */
3128 :
3129 : symbol_attribute
3130 4222652 : gfc_variable_attr (gfc_expr *expr)
3131 : {
3132 4222652 : int dimension, codimension, pointer, allocatable, target, optional;
3133 4222652 : symbol_attribute attr;
3134 4222652 : gfc_ref *ref;
3135 4222652 : gfc_symbol *sym;
3136 4222652 : gfc_component *comp;
3137 4222652 : gfc_typespec *current_ts;
3138 :
3139 4222652 : if (expr->expr_type != EXPR_VARIABLE
3140 72512 : && expr->expr_type != EXPR_FUNCTION
3141 9 : && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
3142 0 : gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
3143 :
3144 4222652 : sym = expr->symtree->n.sym;
3145 4222652 : attr = sym->attr;
3146 4222652 : current_ts = &sym->ts;
3147 :
3148 : /* If we are in the body of a function, the function name references the
3149 : function result, not the function itself. */
3150 4222652 : if (attr.function
3151 160629 : && sym->result == sym
3152 127287 : && gfc_current_ns
3153 127287 : && gfc_current_ns->proc_name == sym)
3154 : {
3155 46270 : attr.function = 0;
3156 46270 : attr.elemental = 0;
3157 46270 : attr.pure = 0;
3158 46270 : attr.recursive = 0;
3159 46270 : attr.flavor = FL_VARIABLE;
3160 46270 : attr.result = 1;
3161 : }
3162 :
3163 4222652 : optional = attr.optional;
3164 4222652 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
3165 : {
3166 142059 : dimension = CLASS_DATA (sym)->attr.dimension;
3167 142059 : codimension = CLASS_DATA (sym)->attr.codimension;
3168 142059 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3169 142059 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3170 : }
3171 : else
3172 : {
3173 4080593 : dimension = attr.dimension;
3174 4080593 : codimension = attr.codimension;
3175 4080593 : pointer = attr.pointer;
3176 4080593 : allocatable = attr.allocatable;
3177 : }
3178 :
3179 4222652 : target = attr.target;
3180 4222652 : if (pointer || attr.proc_pointer)
3181 211107 : target = 1;
3182 :
3183 : /* F2018:11.1.3.3: Other attributes of associate names
3184 : "The associating entity does not have the ALLOCATABLE or POINTER
3185 : attributes; it has the TARGET attribute if and only if the selector is
3186 : a variable and has either the TARGET or POINTER attribute." */
3187 4222652 : if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
3188 : {
3189 27293 : if (sym->assoc->target->expr_type == EXPR_VARIABLE)
3190 : {
3191 24219 : symbol_attribute tgt_attr;
3192 24219 : tgt_attr = gfc_expr_attr (sym->assoc->target);
3193 31328 : target = (tgt_attr.pointer || tgt_attr.target);
3194 : }
3195 : else
3196 : target = 0;
3197 : }
3198 :
3199 5809690 : for (ref = expr->ref; ref; ref = ref->next)
3200 1588692 : if (ref->type == REF_SUBSTRING)
3201 : optional = false;
3202 1575686 : else if (ref->type == REF_INQUIRY)
3203 : {
3204 : optional = false;
3205 : break;
3206 : }
3207 :
3208 5811344 : for (ref = expr->ref; ref; ref = ref->next)
3209 1588692 : switch (ref->type)
3210 : {
3211 1223290 : case REF_ARRAY:
3212 :
3213 1223290 : switch (ref->u.ar.type)
3214 : {
3215 : case AR_FULL:
3216 1588692 : dimension = 1;
3217 : break;
3218 :
3219 100851 : case AR_SECTION:
3220 100851 : allocatable = pointer = 0;
3221 100851 : dimension = 1;
3222 100851 : optional = false;
3223 100851 : break;
3224 :
3225 228711 : case AR_ELEMENT:
3226 : /* Handle coarrays. */
3227 228711 : if (ref->u.ar.dimen > 0)
3228 1588692 : allocatable = pointer = optional = false;
3229 : break;
3230 :
3231 : case AR_UNKNOWN:
3232 : /* For standard conforming code, AR_UNKNOWN should not happen.
3233 : For nonconforming code, gfortran can end up here. Treat it
3234 : as a no-op. */
3235 : break;
3236 : }
3237 :
3238 : break;
3239 :
3240 350742 : case REF_COMPONENT:
3241 350742 : comp = ref->u.c.component;
3242 350742 : if (!(current_ts->type == BT_CLASS
3243 69009 : && current_ts->u.derived->attr.is_class
3244 68994 : && strcmp (comp->name, "_data") == 0))
3245 : {
3246 314353 : optional = false;
3247 314353 : attr = comp->attr;
3248 : }
3249 350742 : current_ts = &sym->ts;
3250 :
3251 350742 : if (comp->ts.type == BT_CLASS)
3252 : {
3253 26779 : dimension = CLASS_DATA (comp)->attr.dimension;
3254 26779 : codimension = CLASS_DATA (comp)->attr.codimension;
3255 26779 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3256 26779 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3257 : }
3258 : else
3259 : {
3260 323963 : dimension = comp->attr.dimension;
3261 323963 : codimension = comp->attr.codimension;
3262 323963 : if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3263 19743 : pointer = comp->attr.class_pointer;
3264 : else
3265 304220 : pointer = comp->attr.pointer;
3266 323963 : allocatable = comp->attr.allocatable;
3267 : }
3268 350742 : if (pointer || attr.proc_pointer)
3269 62357 : target = 1;
3270 :
3271 : break;
3272 :
3273 14660 : case REF_INQUIRY:
3274 14660 : case REF_SUBSTRING:
3275 14660 : allocatable = pointer = optional = false;
3276 14660 : break;
3277 : }
3278 :
3279 4222652 : attr.dimension = dimension;
3280 4222652 : attr.codimension = codimension;
3281 4222652 : attr.pointer = pointer;
3282 4222652 : attr.allocatable = allocatable;
3283 4222652 : attr.target = target;
3284 4222652 : attr.save = sym->attr.save;
3285 4222652 : attr.optional = optional;
3286 :
3287 4222652 : return attr;
3288 : }
3289 :
3290 :
3291 : /* Return the attribute from a general expression. */
3292 :
3293 : symbol_attribute
3294 5212444 : gfc_expr_attr (gfc_expr *e)
3295 : {
3296 5212444 : symbol_attribute attr;
3297 :
3298 5212444 : switch (e->expr_type)
3299 : {
3300 4140972 : case EXPR_VARIABLE:
3301 4140972 : attr = gfc_variable_attr (e);
3302 4140972 : break;
3303 :
3304 100271 : case EXPR_FUNCTION:
3305 100271 : gfc_clear_attr (&attr);
3306 :
3307 100271 : if (e->value.function.esym && e->value.function.esym->result)
3308 : {
3309 27432 : gfc_symbol *sym = e->value.function.esym->result;
3310 27432 : attr = sym->attr;
3311 27432 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3312 : {
3313 2505 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3314 2505 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3315 2505 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3316 : }
3317 : }
3318 72839 : else if (e->value.function.isym
3319 69555 : && e->value.function.isym->transformational
3320 34913 : && e->ts.type == BT_CLASS)
3321 330 : attr = CLASS_DATA (e)->attr;
3322 72509 : else if (e->symtree)
3323 72503 : attr = gfc_variable_attr (e);
3324 :
3325 : /* TODO: NULL() returns pointers. May have to take care of this
3326 : here. */
3327 :
3328 : break;
3329 :
3330 971201 : default:
3331 971201 : gfc_clear_attr (&attr);
3332 971201 : break;
3333 : }
3334 :
3335 5212444 : return attr;
3336 : }
3337 :
3338 :
3339 : /* Given an expression, figure out what the ultimate expression
3340 : attribute is. This routine is similar to gfc_variable_attr with
3341 : parts of gfc_expr_attr, but focuses more on the needs of
3342 : coarrays. For coarrays a codimension attribute is kind of
3343 : "infectious" being propagated once set and never cleared.
3344 : The coarray_comp is only set, when the expression refs a coarray
3345 : component. REFS_COMP is set when present to true only, when this EXPR
3346 : refs a (non-_data) component. To check whether EXPR refs an allocatable
3347 : component in a derived type coarray *refs_comp needs to be set and
3348 : coarray_comp has to false. */
3349 :
3350 : static symbol_attribute
3351 17047 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3352 : {
3353 17047 : int dimension, codimension, pointer, allocatable, target, coarray_comp;
3354 17047 : symbol_attribute attr;
3355 17047 : gfc_ref *ref;
3356 17047 : gfc_symbol *sym;
3357 17047 : gfc_component *comp;
3358 :
3359 17047 : if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3360 0 : gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3361 :
3362 17047 : sym = expr->symtree->n.sym;
3363 17047 : gfc_clear_attr (&attr);
3364 :
3365 17047 : if (refs_comp)
3366 11659 : *refs_comp = false;
3367 :
3368 17047 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3369 : {
3370 430 : dimension = CLASS_DATA (sym)->attr.dimension;
3371 430 : codimension = CLASS_DATA (sym)->attr.codimension;
3372 430 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3373 430 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3374 430 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3375 430 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3376 : }
3377 : else
3378 : {
3379 16617 : dimension = sym->attr.dimension;
3380 16617 : codimension = sym->attr.codimension;
3381 16617 : pointer = sym->attr.pointer;
3382 16617 : allocatable = sym->attr.allocatable;
3383 33234 : attr.alloc_comp = sym->ts.type == BT_DERIVED
3384 16617 : ? sym->ts.u.derived->attr.alloc_comp : 0;
3385 16617 : attr.pointer_comp = sym->ts.type == BT_DERIVED
3386 16617 : ? sym->ts.u.derived->attr.pointer_comp : 0;
3387 : }
3388 :
3389 17047 : target = coarray_comp = 0;
3390 17047 : if (pointer || attr.proc_pointer)
3391 653 : target = 1;
3392 :
3393 29648 : for (ref = expr->ref; ref; ref = ref->next)
3394 12601 : switch (ref->type)
3395 : {
3396 8796 : case REF_ARRAY:
3397 :
3398 8796 : switch (ref->u.ar.type)
3399 : {
3400 : case AR_FULL:
3401 : case AR_SECTION:
3402 : dimension = 1;
3403 8796 : break;
3404 :
3405 4232 : case AR_ELEMENT:
3406 : /* Handle coarrays. */
3407 4232 : if (ref->u.ar.dimen > 0 && !in_allocate)
3408 8796 : allocatable = pointer = 0;
3409 : break;
3410 :
3411 0 : case AR_UNKNOWN:
3412 : /* If any of start, end or stride is not integer, there will
3413 : already have been an error issued. */
3414 0 : int errors;
3415 0 : gfc_get_errors (NULL, &errors);
3416 0 : if (errors == 0)
3417 0 : gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3418 : }
3419 :
3420 : break;
3421 :
3422 3803 : case REF_COMPONENT:
3423 3803 : comp = ref->u.c.component;
3424 :
3425 3803 : if (comp->ts.type == BT_CLASS)
3426 : {
3427 : /* Set coarray_comp only, when this component introduces the
3428 : coarray. */
3429 13 : coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3430 13 : codimension |= CLASS_DATA (comp)->attr.codimension;
3431 13 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3432 13 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3433 : }
3434 : else
3435 : {
3436 : /* Set coarray_comp only, when this component introduces the
3437 : coarray. */
3438 3790 : coarray_comp = !codimension && comp->attr.codimension;
3439 3790 : codimension |= comp->attr.codimension;
3440 3790 : pointer = comp->attr.pointer;
3441 3790 : allocatable = comp->attr.allocatable;
3442 : }
3443 :
3444 3803 : if (refs_comp && strcmp (comp->name, "_data") != 0
3445 2253 : && (ref->next == NULL
3446 1724 : || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3447 1684 : *refs_comp = true;
3448 :
3449 3803 : if (pointer || attr.proc_pointer)
3450 690 : target = 1;
3451 :
3452 : break;
3453 :
3454 : case REF_SUBSTRING:
3455 : case REF_INQUIRY:
3456 12601 : allocatable = pointer = 0;
3457 : break;
3458 : }
3459 :
3460 17047 : attr.dimension = dimension;
3461 17047 : attr.codimension = codimension;
3462 17047 : attr.pointer = pointer;
3463 17047 : attr.allocatable = allocatable;
3464 17047 : attr.target = target;
3465 17047 : attr.save = sym->attr.save;
3466 17047 : attr.coarray_comp = coarray_comp;
3467 :
3468 17047 : return attr;
3469 : }
3470 :
3471 :
3472 : symbol_attribute
3473 21209 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3474 : {
3475 21209 : symbol_attribute attr;
3476 :
3477 21209 : switch (e->expr_type)
3478 : {
3479 15288 : case EXPR_VARIABLE:
3480 15288 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3481 15288 : break;
3482 :
3483 1765 : case EXPR_FUNCTION:
3484 1765 : gfc_clear_attr (&attr);
3485 :
3486 1765 : if (e->value.function.esym && e->value.function.esym->result)
3487 : {
3488 6 : gfc_symbol *sym = e->value.function.esym->result;
3489 6 : attr = sym->attr;
3490 6 : if (sym->ts.type == BT_CLASS)
3491 : {
3492 0 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3493 0 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3494 0 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3495 0 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3496 0 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3497 0 : ->attr.pointer_comp;
3498 : }
3499 : }
3500 1759 : else if (e->symtree)
3501 1759 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3502 : else
3503 0 : gfc_clear_attr (&attr);
3504 : break;
3505 :
3506 4156 : default:
3507 4156 : gfc_clear_attr (&attr);
3508 4156 : break;
3509 : }
3510 :
3511 21209 : return attr;
3512 : }
3513 :
3514 :
3515 : /* Match a structure constructor. The initial symbol has already been
3516 : seen. */
3517 :
3518 : typedef struct gfc_structure_ctor_component
3519 : {
3520 : char* name;
3521 : gfc_expr* val;
3522 : locus where;
3523 : struct gfc_structure_ctor_component* next;
3524 : }
3525 : gfc_structure_ctor_component;
3526 :
3527 : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3528 :
3529 : static void
3530 11587 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3531 : {
3532 11587 : free (comp->name);
3533 11587 : gfc_free_expr (comp->val);
3534 11587 : free (comp);
3535 11587 : }
3536 :
3537 :
3538 : /* Translate the component list into the actual constructor by sorting it in
3539 : the order required; this also checks along the way that each and every
3540 : component actually has an initializer and handles default initializers
3541 : for components without explicit value given. */
3542 : static bool
3543 7988 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
3544 : gfc_constructor_base *ctor_head, gfc_symbol *sym)
3545 : {
3546 7988 : gfc_structure_ctor_component *comp_iter;
3547 7988 : gfc_component *comp;
3548 :
3549 21043 : for (comp = sym->components; comp; comp = comp->next)
3550 : {
3551 13067 : gfc_structure_ctor_component **next_ptr;
3552 13067 : gfc_expr *value = NULL;
3553 :
3554 : /* Try to find the initializer for the current component by name. */
3555 13067 : next_ptr = comp_head;
3556 14300 : for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3557 : {
3558 12796 : if (!strcmp (comp_iter->name, comp->name))
3559 : break;
3560 1233 : next_ptr = &comp_iter->next;
3561 : }
3562 :
3563 : /* If an extension, try building the parent derived type by building
3564 : a value expression for the parent derived type and calling self. */
3565 13067 : if (!comp_iter && comp == sym->components && sym->attr.extension)
3566 : {
3567 132 : value = gfc_get_structure_constructor_expr (comp->ts.type,
3568 : comp->ts.kind,
3569 : &gfc_current_locus);
3570 132 : value->ts = comp->ts;
3571 :
3572 132 : if (!build_actual_constructor (comp_head,
3573 : &value->value.constructor,
3574 132 : comp->ts.u.derived))
3575 : {
3576 0 : gfc_free_expr (value);
3577 0 : return false;
3578 : }
3579 :
3580 132 : gfc_constructor_append_expr (ctor_head, value, NULL);
3581 132 : continue;
3582 : }
3583 :
3584 : /* If it was not found, apply NULL expression to set the component as
3585 : unallocated. Then try the default initializer if there's any;
3586 : otherwise, it's an error unless this is a deferred parameter. */
3587 1372 : if (!comp_iter)
3588 : {
3589 : /* F2018 7.5.10: If an allocatable component has no corresponding
3590 : component-data-source, then that component has an allocation
3591 : status of unallocated.... */
3592 1372 : if (comp->attr.allocatable
3593 1237 : || (comp->ts.type == BT_CLASS
3594 15 : && CLASS_DATA (comp)->attr.allocatable))
3595 : {
3596 144 : if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3597 : "allocatable component %qs given in the "
3598 : "structure constructor at %C", comp->name))
3599 : return false;
3600 144 : value = gfc_get_null_expr (&gfc_current_locus);
3601 : }
3602 : /* ....(Preceding sentence) If a component with default
3603 : initialization has no corresponding component-data-source, then
3604 : the default initialization is applied to that component. */
3605 1228 : else if (comp->initializer)
3606 : {
3607 702 : if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3608 : "with missing optional arguments at %C"))
3609 : return false;
3610 700 : value = gfc_copy_expr (comp->initializer);
3611 : }
3612 : /* Do not trap components such as the string length for deferred
3613 : length character components. */
3614 526 : else if (!comp->attr.artificial)
3615 : {
3616 10 : gfc_error ("No initializer for component %qs given in the"
3617 : " structure constructor at %C", comp->name);
3618 10 : return false;
3619 : }
3620 : }
3621 : else
3622 11563 : value = comp_iter->val;
3623 :
3624 : /* Add the value to the constructor chain built. */
3625 12923 : gfc_constructor_append_expr (ctor_head, value, NULL);
3626 :
3627 : /* Remove the entry from the component list. We don't want the expression
3628 : value to be free'd, so set it to NULL. */
3629 12923 : if (comp_iter)
3630 : {
3631 11563 : *next_ptr = comp_iter->next;
3632 11563 : comp_iter->val = NULL;
3633 11563 : gfc_free_structure_ctor_component (comp_iter);
3634 : }
3635 : }
3636 : return true;
3637 : }
3638 :
3639 :
3640 : bool
3641 7871 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3642 : gfc_actual_arglist **arglist,
3643 : bool parent)
3644 : {
3645 7871 : gfc_actual_arglist *actual;
3646 7871 : gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3647 7871 : gfc_constructor_base ctor_head = NULL;
3648 7871 : gfc_component *comp; /* Is set NULL when named component is first seen */
3649 7871 : const char* last_name = NULL;
3650 7871 : locus old_locus;
3651 7871 : gfc_expr *expr;
3652 :
3653 7871 : expr = parent ? *cexpr : e;
3654 7871 : old_locus = gfc_current_locus;
3655 7871 : if (parent)
3656 : ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3657 : else
3658 7085 : gfc_current_locus = expr->where;
3659 :
3660 7871 : comp_tail = comp_head = NULL;
3661 :
3662 7871 : if (!parent && sym->attr.abstract)
3663 : {
3664 1 : gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3665 : sym->name, &expr->where);
3666 1 : goto cleanup;
3667 : }
3668 :
3669 7870 : comp = sym->components;
3670 7870 : actual = parent ? *arglist : expr->value.function.actual;
3671 18790 : for ( ; actual; )
3672 : {
3673 11587 : gfc_component *this_comp = NULL;
3674 :
3675 11587 : if (!comp_head)
3676 7429 : comp_tail = comp_head = gfc_get_structure_ctor_component ();
3677 : else
3678 : {
3679 4158 : comp_tail->next = gfc_get_structure_ctor_component ();
3680 4158 : comp_tail = comp_tail->next;
3681 : }
3682 11587 : if (actual->name)
3683 : {
3684 1609 : if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3685 : " constructor with named arguments at %C"))
3686 1 : goto cleanup;
3687 :
3688 1608 : comp_tail->name = xstrdup (actual->name);
3689 1608 : last_name = comp_tail->name;
3690 1608 : comp = NULL;
3691 : }
3692 : else
3693 : {
3694 : /* Components without name are not allowed after the first named
3695 : component initializer! */
3696 9978 : if (!comp || comp->attr.artificial)
3697 : {
3698 2 : if (last_name)
3699 0 : gfc_error ("Component initializer without name after component"
3700 : " named %s at %L", last_name,
3701 0 : actual->expr ? &actual->expr->where
3702 : : &gfc_current_locus);
3703 : else
3704 2 : gfc_error ("Too many components in structure constructor at "
3705 2 : "%L", actual->expr ? &actual->expr->where
3706 : : &gfc_current_locus);
3707 2 : goto cleanup;
3708 : }
3709 :
3710 9976 : comp_tail->name = xstrdup (comp->name);
3711 : }
3712 :
3713 : /* Find the current component in the structure definition and check
3714 : its access is not private. */
3715 11584 : if (comp)
3716 9976 : this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3717 : else
3718 : {
3719 1608 : this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3720 : false, false, NULL);
3721 1608 : comp = NULL; /* Reset needed! */
3722 : }
3723 :
3724 : /* Here we can check if a component name is given which does not
3725 : correspond to any component of the defined structure. */
3726 11584 : if (!this_comp)
3727 8 : goto cleanup;
3728 :
3729 : /* For a constant string constructor, make sure the length is
3730 : correct; truncate or fill with blanks if needed. */
3731 11576 : if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3732 1215 : && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3733 1213 : && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3734 1195 : && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3735 1194 : && actual->expr
3736 1190 : && actual->expr->ts.type == BT_CHARACTER
3737 1072 : && actual->expr->expr_type == EXPR_CONSTANT)
3738 : {
3739 819 : ptrdiff_t c, e1;
3740 819 : c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3741 819 : e1 = actual->expr->value.character.length;
3742 :
3743 819 : if (c != e1)
3744 : {
3745 255 : ptrdiff_t i, to;
3746 255 : gfc_char_t *dest;
3747 255 : dest = gfc_get_wide_string (c + 1);
3748 :
3749 255 : to = e1 < c ? e1 : c;
3750 4500 : for (i = 0; i < to; i++)
3751 4245 : dest[i] = actual->expr->value.character.string[i];
3752 :
3753 5818 : for (i = e1; i < c; i++)
3754 5563 : dest[i] = ' ';
3755 :
3756 255 : dest[c] = '\0';
3757 255 : free (actual->expr->value.character.string);
3758 :
3759 255 : actual->expr->value.character.length = c;
3760 255 : actual->expr->value.character.string = dest;
3761 :
3762 255 : if (warn_line_truncation && c < e1)
3763 20 : gfc_warning_now (OPT_Wcharacter_truncation,
3764 : "CHARACTER expression will be truncated "
3765 : "in constructor (%td/%td) at %L", c,
3766 : e1, &actual->expr->where);
3767 : }
3768 : }
3769 :
3770 11576 : comp_tail->val = actual->expr;
3771 11576 : if (actual->expr != NULL)
3772 11571 : comp_tail->where = actual->expr->where;
3773 11576 : actual->expr = NULL;
3774 :
3775 : /* Check if this component is already given a value. */
3776 18344 : for (comp_iter = comp_head; comp_iter != comp_tail;
3777 6768 : comp_iter = comp_iter->next)
3778 : {
3779 6769 : gcc_assert (comp_iter);
3780 6769 : if (!strcmp (comp_iter->name, comp_tail->name))
3781 : {
3782 1 : gfc_error ("Component %qs is initialized twice in the structure"
3783 : " constructor at %L", comp_tail->name,
3784 : comp_tail->val ? &comp_tail->where
3785 : : &gfc_current_locus);
3786 1 : goto cleanup;
3787 : }
3788 : }
3789 :
3790 : /* F2008, R457/C725, for PURE C1283. */
3791 72 : if (this_comp->attr.pointer && comp_tail->val
3792 11647 : && gfc_is_coindexed (comp_tail->val))
3793 : {
3794 2 : gfc_error ("Coindexed expression to pointer component %qs in "
3795 : "structure constructor at %L", comp_tail->name,
3796 : &comp_tail->where);
3797 2 : goto cleanup;
3798 : }
3799 :
3800 : /* If not explicitly a parent constructor, gather up the components
3801 : and build one. */
3802 11573 : if (comp && comp == sym->components
3803 6881 : && sym->attr.extension
3804 834 : && comp_tail->val
3805 834 : && (!gfc_bt_struct (comp_tail->val->ts.type)
3806 78 : || comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3807 : {
3808 786 : bool m;
3809 786 : gfc_actual_arglist *arg_null = NULL;
3810 :
3811 786 : actual->expr = comp_tail->val;
3812 786 : comp_tail->val = NULL;
3813 : #define shorter gfc_convert_to_structure_constructor
3814 786 : m = shorter (NULL, comp->ts.u.derived, &comp_tail->val,
3815 786 : comp->ts.u.derived->attr.zero_comp ? &arg_null :
3816 : &actual, true);
3817 : #undef shorter
3818 :
3819 786 : if (!m)
3820 0 : goto cleanup;
3821 :
3822 786 : if (comp->ts.u.derived->attr.zero_comp)
3823 : {
3824 132 : comp = comp->next;
3825 132 : continue;
3826 : }
3827 : }
3828 :
3829 654 : if (comp)
3830 9836 : comp = comp->next;
3831 11441 : if (parent && !comp)
3832 : break;
3833 :
3834 10788 : if (actual)
3835 10787 : actual = actual->next;
3836 : }
3837 :
3838 7856 : if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3839 12 : goto cleanup;
3840 :
3841 : /* No component should be left, as this should have caused an error in the
3842 : loop constructing the component-list (name that does not correspond to any
3843 : component in the structure definition). */
3844 7844 : if (comp_head && sym->attr.extension)
3845 : {
3846 2 : for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3847 : {
3848 1 : gfc_error ("component %qs at %L has already been set by a "
3849 : "parent derived type constructor", comp_iter->name,
3850 : &comp_iter->where);
3851 : }
3852 1 : goto cleanup;
3853 : }
3854 : else
3855 7843 : gcc_assert (!comp_head);
3856 :
3857 7843 : if (parent)
3858 : {
3859 786 : expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3860 786 : expr->ts.u.derived = sym;
3861 786 : expr->value.constructor = ctor_head;
3862 786 : *cexpr = expr;
3863 : }
3864 : else
3865 : {
3866 7057 : expr->ts.u.derived = sym;
3867 7057 : expr->ts.kind = 0;
3868 7057 : expr->ts.type = BT_DERIVED;
3869 7057 : expr->value.constructor = ctor_head;
3870 7057 : expr->expr_type = EXPR_STRUCTURE;
3871 : }
3872 :
3873 7843 : gfc_current_locus = old_locus;
3874 7843 : if (parent)
3875 786 : *arglist = actual;
3876 : return true;
3877 :
3878 28 : cleanup:
3879 28 : gfc_current_locus = old_locus;
3880 :
3881 52 : for (comp_iter = comp_head; comp_iter; )
3882 : {
3883 24 : gfc_structure_ctor_component *next = comp_iter->next;
3884 24 : gfc_free_structure_ctor_component (comp_iter);
3885 24 : comp_iter = next;
3886 : }
3887 28 : gfc_constructor_free (ctor_head);
3888 :
3889 28 : return false;
3890 : }
3891 :
3892 :
3893 : match
3894 60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3895 : gfc_expr **result)
3896 : {
3897 60 : match m;
3898 60 : gfc_expr *e;
3899 60 : bool t = true;
3900 :
3901 60 : e = gfc_get_expr ();
3902 60 : e->expr_type = EXPR_FUNCTION;
3903 60 : e->symtree = symtree;
3904 60 : e->where = gfc_current_locus;
3905 :
3906 60 : gcc_assert (gfc_fl_struct (sym->attr.flavor)
3907 : && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3908 60 : e->value.function.esym = sym;
3909 60 : e->symtree->n.sym->attr.generic = 1;
3910 :
3911 60 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
3912 60 : if (m != MATCH_YES)
3913 : {
3914 0 : gfc_free_expr (e);
3915 0 : return m;
3916 : }
3917 :
3918 60 : if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3919 : {
3920 1 : gfc_free_expr (e);
3921 1 : return MATCH_ERROR;
3922 : }
3923 :
3924 : /* If a structure constructor is in a DATA statement, then each entity
3925 : in the structure constructor must be a constant. Try to reduce the
3926 : expression here. */
3927 59 : if (gfc_in_match_data ())
3928 59 : t = gfc_reduce_init_expr (e);
3929 :
3930 59 : if (t)
3931 : {
3932 49 : *result = e;
3933 49 : return MATCH_YES;
3934 : }
3935 : else
3936 : {
3937 10 : gfc_free_expr (e);
3938 10 : return MATCH_ERROR;
3939 : }
3940 : }
3941 :
3942 :
3943 : /* If the symbol is an implicit do loop index and implicitly typed,
3944 : it should not be host associated. Provide a symtree from the
3945 : current namespace. */
3946 : static match
3947 7095431 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3948 : {
3949 7095431 : if ((*sym)->attr.flavor == FL_VARIABLE
3950 2033936 : && (*sym)->ns != gfc_current_ns
3951 64934 : && (*sym)->attr.implied_index
3952 618 : && (*sym)->attr.implicit_type
3953 32 : && !(*sym)->attr.use_assoc)
3954 : {
3955 32 : int i;
3956 32 : i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3957 32 : if (i)
3958 : return MATCH_ERROR;
3959 32 : *sym = (*st)->n.sym;
3960 : }
3961 : return MATCH_YES;
3962 : }
3963 :
3964 :
3965 : /* Procedure pointer as function result: Replace the function symbol by the
3966 : auto-generated hidden result variable named "ppr@". */
3967 :
3968 : static bool
3969 5281889 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3970 : {
3971 : /* Check for procedure pointer result variable. */
3972 5281889 : if ((*sym)->attr.function && !(*sym)->attr.external
3973 1432604 : && (*sym)->result && (*sym)->result != *sym
3974 12640 : && (*sym)->result->attr.proc_pointer
3975 343 : && (*sym) == gfc_current_ns->proc_name
3976 285 : && (*sym) == (*sym)->result->ns->proc_name
3977 285 : && strcmp ("ppr@", (*sym)->result->name) == 0)
3978 : {
3979 : /* Automatic replacement with "hidden" result variable. */
3980 285 : (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3981 285 : *sym = (*sym)->result;
3982 285 : *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3983 285 : return true;
3984 : }
3985 : return false;
3986 : }
3987 :
3988 :
3989 : /* Matches a variable name followed by anything that might follow it--
3990 : array reference, argument list of a function, etc. */
3991 :
3992 : match
3993 4356858 : gfc_match_rvalue (gfc_expr **result)
3994 : {
3995 4356858 : gfc_actual_arglist *actual_arglist;
3996 4356858 : char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3997 4356858 : gfc_state_data *st;
3998 4356858 : gfc_symbol *sym;
3999 4356858 : gfc_symtree *symtree;
4000 4356858 : locus where, old_loc;
4001 4356858 : gfc_expr *e;
4002 4356858 : match m, m2;
4003 4356858 : int i;
4004 4356858 : gfc_typespec *ts;
4005 4356858 : bool implicit_char;
4006 4356858 : gfc_ref *ref;
4007 4356858 : gfc_symtree *pdt_st;
4008 :
4009 4356858 : m = gfc_match ("%%loc");
4010 4356858 : if (m == MATCH_YES)
4011 : {
4012 10878 : if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
4013 : return MATCH_ERROR;
4014 10877 : strncpy (name, "loc", 4);
4015 : }
4016 :
4017 : else
4018 : {
4019 4345980 : m = gfc_match_name (name);
4020 4345980 : if (m != MATCH_YES)
4021 : return m;
4022 : }
4023 :
4024 : /* Check if the symbol exists. */
4025 4150110 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
4026 : return MATCH_ERROR;
4027 :
4028 : /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
4029 : type. For derived types we create a generic symbol which links to the
4030 : derived type symbol; STRUCTUREs are simpler and must not conflict with
4031 : variables. */
4032 4150108 : if (!symtree)
4033 187594 : if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
4034 : return MATCH_ERROR;
4035 4150108 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4036 : {
4037 4150108 : if (gfc_find_state (COMP_INTERFACE)
4038 4150108 : && !gfc_current_ns->has_import_set)
4039 100185 : i = gfc_get_sym_tree (name, NULL, &symtree, false);
4040 : else
4041 4049923 : i = gfc_get_ha_sym_tree (name, &symtree);
4042 4150108 : if (i)
4043 : return MATCH_ERROR;
4044 : }
4045 :
4046 :
4047 4150108 : sym = symtree->n.sym;
4048 4150108 : e = NULL;
4049 4150108 : where = gfc_current_locus;
4050 :
4051 4150108 : replace_hidden_procptr_result (&sym, &symtree);
4052 :
4053 : /* If this is an implicit do loop index and implicitly typed,
4054 : it should not be host associated. */
4055 4150108 : m = check_for_implicit_index (&symtree, &sym);
4056 4150108 : if (m != MATCH_YES)
4057 : return m;
4058 :
4059 4150108 : gfc_set_sym_referenced (sym);
4060 4150108 : sym->attr.implied_index = 0;
4061 :
4062 4150108 : if (sym->attr.function && sym->result == sym)
4063 : {
4064 : /* See if this is a directly recursive function call. */
4065 719108 : gfc_gobble_whitespace ();
4066 719108 : if (sym->attr.recursive
4067 100 : && gfc_peek_ascii_char () == '('
4068 93 : && gfc_current_ns->proc_name == sym
4069 719115 : && !sym->attr.dimension)
4070 : {
4071 4 : gfc_error ("%qs at %C is the name of a recursive function "
4072 : "and so refers to the result variable. Use an "
4073 : "explicit RESULT variable for direct recursion "
4074 : "(12.5.2.1)", sym->name);
4075 4 : return MATCH_ERROR;
4076 : }
4077 :
4078 719104 : if (gfc_is_function_return_value (sym, gfc_current_ns))
4079 1731 : goto variable;
4080 :
4081 717373 : if (sym->attr.entry
4082 187 : && (sym->ns == gfc_current_ns
4083 27 : || sym->ns == gfc_current_ns->parent))
4084 : {
4085 180 : gfc_entry_list *el = NULL;
4086 :
4087 180 : for (el = sym->ns->entries; el; el = el->next)
4088 180 : if (sym == el->sym)
4089 180 : goto variable;
4090 : }
4091 : }
4092 :
4093 4148193 : if (gfc_matching_procptr_assignment)
4094 : {
4095 : /* It can be a procedure or a derived-type procedure or a not-yet-known
4096 : type. */
4097 1389 : if (sym->attr.flavor != FL_UNKNOWN
4098 1025 : && sym->attr.flavor != FL_PROCEDURE
4099 65 : && sym->attr.flavor != FL_PARAMETER
4100 65 : && sym->attr.flavor != FL_VARIABLE)
4101 : {
4102 2 : gfc_error ("Symbol at %C is not appropriate for an expression");
4103 2 : return MATCH_ERROR;
4104 : }
4105 1387 : goto procptr0;
4106 : }
4107 :
4108 4146804 : if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
4109 733615 : goto function0;
4110 :
4111 3413189 : if (sym->attr.generic)
4112 69128 : goto generic_function;
4113 :
4114 3344061 : switch (sym->attr.flavor)
4115 : {
4116 1762533 : case FL_VARIABLE:
4117 1762533 : variable:
4118 1762533 : e = gfc_get_expr ();
4119 :
4120 1762533 : e->expr_type = EXPR_VARIABLE;
4121 1762533 : e->symtree = symtree;
4122 :
4123 1762533 : m = gfc_match_varspec (e, 0, false, true);
4124 1762533 : break;
4125 :
4126 230599 : case FL_PARAMETER:
4127 : /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
4128 : end up here. Unfortunately, sym->value->expr_type is set to
4129 : EXPR_CONSTANT, and so the if () branch would be followed without
4130 : the !sym->as check. */
4131 230599 : if (sym->value && sym->value->expr_type != EXPR_ARRAY
4132 195628 : && sym->value->expr_type != EXPR_VARIABLE && !sym->as)
4133 195618 : e = gfc_copy_expr (sym->value);
4134 : else
4135 : {
4136 34981 : e = gfc_get_expr ();
4137 34981 : e->expr_type = EXPR_VARIABLE;
4138 : }
4139 :
4140 230599 : e->symtree = symtree;
4141 230599 : m = gfc_match_varspec (e, 0, false, true);
4142 :
4143 230599 : if (sym->ts.is_c_interop || sym->ts.is_iso_c)
4144 : break;
4145 :
4146 : /* Variable array references to derived type parameters cause
4147 : all sorts of headaches in simplification. Treating such
4148 : expressions as variable works just fine for all array
4149 : references. */
4150 178643 : if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
4151 : {
4152 2868 : for (ref = e->ref; ref; ref = ref->next)
4153 2677 : if (ref->type == REF_ARRAY)
4154 : break;
4155 :
4156 2631 : if (ref == NULL || ref->u.ar.type == AR_FULL)
4157 : break;
4158 :
4159 1018 : ref = e->ref;
4160 1018 : e->ref = NULL;
4161 1018 : gfc_free_expr (e);
4162 1018 : e = gfc_get_expr ();
4163 1018 : e->expr_type = EXPR_VARIABLE;
4164 1018 : e->symtree = symtree;
4165 1018 : e->ref = ref;
4166 : }
4167 :
4168 : break;
4169 :
4170 0 : case FL_STRUCT:
4171 0 : case FL_DERIVED:
4172 0 : sym = gfc_use_derived (sym);
4173 0 : if (sym == NULL)
4174 : m = MATCH_ERROR;
4175 : else
4176 0 : goto generic_function;
4177 : break;
4178 :
4179 : /* If we're here, then the name is known to be the name of a
4180 : procedure, yet it is not sure to be the name of a function. */
4181 1052163 : case FL_PROCEDURE:
4182 :
4183 : /* Procedure Pointer Assignments. */
4184 1052163 : procptr0:
4185 1052163 : if (gfc_matching_procptr_assignment)
4186 : {
4187 1387 : gfc_gobble_whitespace ();
4188 1387 : if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
4189 : /* Parse functions returning a procptr. */
4190 220 : goto function0;
4191 :
4192 1167 : e = gfc_get_expr ();
4193 1167 : e->expr_type = EXPR_VARIABLE;
4194 1167 : e->symtree = symtree;
4195 1167 : m = gfc_match_varspec (e, 0, false, true);
4196 1098 : if (!e->ref && sym->attr.flavor == FL_UNKNOWN
4197 209 : && sym->ts.type == BT_UNKNOWN
4198 1366 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4199 : {
4200 : m = MATCH_ERROR;
4201 : break;
4202 : }
4203 : break;
4204 : }
4205 :
4206 1050776 : if (sym->attr.subroutine)
4207 : {
4208 57 : gfc_error ("Unexpected use of subroutine name %qs at %C",
4209 : sym->name);
4210 57 : m = MATCH_ERROR;
4211 57 : break;
4212 : }
4213 :
4214 : /* At this point, the name has to be a non-statement function.
4215 : If the name is the same as the current function being
4216 : compiled, then we have a variable reference (to the function
4217 : result) if the name is non-recursive. */
4218 :
4219 1050719 : st = gfc_enclosing_unit (NULL);
4220 :
4221 1050719 : if (st != NULL
4222 1004802 : && st->state == COMP_FUNCTION
4223 88599 : && st->sym == sym
4224 0 : && !sym->attr.recursive)
4225 : {
4226 0 : e = gfc_get_expr ();
4227 0 : e->symtree = symtree;
4228 0 : e->expr_type = EXPR_VARIABLE;
4229 :
4230 0 : m = gfc_match_varspec (e, 0, false, true);
4231 0 : break;
4232 : }
4233 :
4234 : /* Match a function reference. */
4235 1050719 : function0:
4236 1784554 : m = gfc_match_actual_arglist (0, &actual_arglist);
4237 1784554 : if (m == MATCH_NO)
4238 : {
4239 622020 : if (sym->attr.proc == PROC_ST_FUNCTION)
4240 1 : gfc_error ("Statement function %qs requires argument list at %C",
4241 : sym->name);
4242 : else
4243 622019 : gfc_error ("Function %qs requires an argument list at %C",
4244 : sym->name);
4245 :
4246 : m = MATCH_ERROR;
4247 : break;
4248 : }
4249 :
4250 1162534 : if (m != MATCH_YES)
4251 : {
4252 : m = MATCH_ERROR;
4253 : break;
4254 : }
4255 :
4256 : /* Check to see if this is a PDT constructor. The format of these
4257 : constructors is rather unusual:
4258 : name [(type_params)](component_values)
4259 : where, component_values excludes the type_params. With the present
4260 : gfortran representation this is rather awkward because the two are not
4261 : distinguished, other than by their attributes.
4262 :
4263 : Even if 'name' is that of a PDT template, priority has to be given to
4264 : specific procedures, other than the constructor, in the generic
4265 : interface. */
4266 :
4267 1128634 : gfc_gobble_whitespace ();
4268 1128634 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &pdt_st);
4269 12264 : if (sym->attr.generic && pdt_st != NULL
4270 1138759 : && !(sym->generic->next && gfc_peek_ascii_char() != '('))
4271 : {
4272 9825 : gfc_symbol *pdt_sym;
4273 9825 : gfc_actual_arglist *ctr_arglist = NULL, *tmp;
4274 9825 : gfc_component *c;
4275 :
4276 : /* Use the template. */
4277 9825 : if (pdt_st->n.sym && pdt_st->n.sym->attr.pdt_template)
4278 : {
4279 1527 : bool type_spec_list = false;
4280 1527 : pdt_sym = pdt_st->n.sym;
4281 1527 : gfc_gobble_whitespace ();
4282 : /* Look for a second actual arglist. If present, try the first
4283 : for the type parameters. Otherwise, or if there is no match,
4284 : depend on default values by setting the type parameters to
4285 : NULL. */
4286 1527 : if (gfc_peek_ascii_char() == '(')
4287 327 : type_spec_list = true;
4288 1527 : if (!actual_arglist && !type_spec_list)
4289 : {
4290 3 : gfc_error_now ("F2023 R755: The empty type specification at %C "
4291 : "is not allowed");
4292 3 : m = MATCH_ERROR;
4293 3 : break;
4294 : }
4295 : /* Generate this instance using the type parameters from the
4296 : first argument list and return the parameter list in
4297 : ctr_arglist. */
4298 1524 : m = gfc_get_pdt_instance (actual_arglist, &pdt_sym, &ctr_arglist);
4299 1524 : if (m != MATCH_YES || !ctr_arglist)
4300 : {
4301 43 : if (ctr_arglist)
4302 0 : gfc_free_actual_arglist (ctr_arglist);
4303 : /* See if all the type parameters have default values. */
4304 43 : m = gfc_get_pdt_instance (NULL, &pdt_sym, &ctr_arglist);
4305 43 : if (m != MATCH_YES)
4306 : {
4307 : m = MATCH_NO;
4308 : break;
4309 : }
4310 : }
4311 :
4312 : /* Now match the component_values if the type parameters were
4313 : present. */
4314 1515 : if (type_spec_list)
4315 : {
4316 327 : m = gfc_match_actual_arglist (0, &actual_arglist);
4317 327 : if (m != MATCH_YES)
4318 : {
4319 : m = MATCH_ERROR;
4320 : break;
4321 : }
4322 : }
4323 :
4324 : /* Make sure that the component names are in place so that this
4325 : list can be safely appended to the type parameters. */
4326 1515 : tmp = actual_arglist;
4327 5110 : for (c = pdt_sym->components; c && tmp; c = c->next)
4328 : {
4329 3595 : if (c->attr.pdt_kind || c->attr.pdt_len)
4330 1891 : continue;
4331 1704 : if (!tmp->name)
4332 1382 : tmp->name = c->name;
4333 1704 : tmp = tmp->next;
4334 : }
4335 :
4336 1515 : gfc_find_sym_tree (gfc_dt_lower_string (pdt_sym->name),
4337 : NULL, 1, &symtree);
4338 1515 : if (!symtree)
4339 : {
4340 736 : gfc_get_ha_sym_tree (gfc_dt_lower_string (pdt_sym->name) ,
4341 : &symtree);
4342 736 : symtree->n.sym = pdt_sym;
4343 736 : symtree->n.sym->ts.u.derived = pdt_sym;
4344 736 : symtree->n.sym->ts.type = BT_DERIVED;
4345 : }
4346 :
4347 1515 : if (type_spec_list)
4348 : {
4349 : /* Append the type_params and the component_values. */
4350 419 : for (tmp = ctr_arglist; tmp && tmp->next;)
4351 : tmp = tmp->next;
4352 327 : tmp->next = actual_arglist;
4353 327 : actual_arglist = ctr_arglist;
4354 327 : tmp = actual_arglist;
4355 : /* Can now add all the component names. */
4356 1105 : for (c = pdt_sym->components; c && tmp; c = c->next)
4357 : {
4358 778 : if (!tmp->name)
4359 1 : tmp->name = c->name;
4360 778 : tmp = tmp->next;
4361 : }
4362 : }
4363 : }
4364 : }
4365 :
4366 1128622 : gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4367 1128622 : sym = symtree->n.sym;
4368 :
4369 1128622 : replace_hidden_procptr_result (&sym, &symtree);
4370 :
4371 1128622 : e = gfc_get_expr ();
4372 1128622 : e->symtree = symtree;
4373 1128622 : e->expr_type = EXPR_FUNCTION;
4374 1128622 : e->value.function.actual = actual_arglist;
4375 1128622 : e->where = gfc_current_locus;
4376 :
4377 1128622 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4378 218 : && CLASS_DATA (sym)->as)
4379 : {
4380 91 : e->rank = CLASS_DATA (sym)->as->rank;
4381 91 : e->corank = CLASS_DATA (sym)->as->corank;
4382 : }
4383 1128531 : else if (sym->as != NULL)
4384 : {
4385 1703 : e->rank = sym->as->rank;
4386 1703 : e->corank = sym->as->corank;
4387 : }
4388 :
4389 1128622 : if (!sym->attr.function
4390 1128622 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4391 : {
4392 : m = MATCH_ERROR;
4393 : break;
4394 : }
4395 :
4396 : /* Check here for the existence of at least one argument for the
4397 : iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4398 1128622 : if (sym->attr.is_iso_c == 1
4399 2 : && (sym->from_intmod == INTMOD_ISO_C_BINDING
4400 2 : && (sym->intmod_sym_id == ISOCBINDING_LOC
4401 2 : || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4402 2 : || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4403 2 : || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4404 : {
4405 : /* make sure we were given a param */
4406 0 : if (actual_arglist == NULL)
4407 : {
4408 0 : gfc_error ("Missing argument to %qs at %C", sym->name);
4409 0 : m = MATCH_ERROR;
4410 0 : break;
4411 : }
4412 : }
4413 :
4414 1128622 : if (sym->result == NULL)
4415 403619 : sym->result = sym;
4416 :
4417 1128622 : gfc_gobble_whitespace ();
4418 : /* F08:C612. */
4419 1128622 : if (gfc_peek_ascii_char() == '%')
4420 : {
4421 12 : gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4422 : "function reference at %C");
4423 12 : m = MATCH_ERROR;
4424 12 : break;
4425 : }
4426 :
4427 : m = MATCH_YES;
4428 : break;
4429 :
4430 300467 : case FL_UNKNOWN:
4431 :
4432 : /* Special case for derived type variables that get their types
4433 : via an IMPLICIT statement. This can't wait for the
4434 : resolution phase. */
4435 :
4436 300467 : old_loc = gfc_current_locus;
4437 300467 : if (gfc_match_member_sep (sym) == MATCH_YES
4438 11255 : && sym->ts.type == BT_UNKNOWN
4439 300475 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4440 0 : gfc_set_default_type (sym, 0, sym->ns);
4441 300467 : gfc_current_locus = old_loc;
4442 :
4443 : /* If the symbol has a (co)dimension attribute, the expression is a
4444 : variable. */
4445 :
4446 300467 : if (sym->attr.dimension || sym->attr.codimension)
4447 : {
4448 36906 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4449 : {
4450 : m = MATCH_ERROR;
4451 : break;
4452 : }
4453 :
4454 36906 : e = gfc_get_expr ();
4455 36906 : e->symtree = symtree;
4456 36906 : e->expr_type = EXPR_VARIABLE;
4457 36906 : m = gfc_match_varspec (e, 0, false, true);
4458 36906 : break;
4459 : }
4460 :
4461 263561 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4462 5163 : && (CLASS_DATA (sym)->attr.dimension
4463 3523 : || CLASS_DATA (sym)->attr.codimension))
4464 : {
4465 1740 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4466 : {
4467 : m = MATCH_ERROR;
4468 : break;
4469 : }
4470 :
4471 1740 : e = gfc_get_expr ();
4472 1740 : e->symtree = symtree;
4473 1740 : e->expr_type = EXPR_VARIABLE;
4474 1740 : m = gfc_match_varspec (e, 0, false, true);
4475 1740 : break;
4476 : }
4477 :
4478 : /* Name is not an array, so we peek to see if a '(' implies a
4479 : function call or a substring reference. Otherwise the
4480 : variable is just a scalar. */
4481 :
4482 261821 : gfc_gobble_whitespace ();
4483 261821 : if (gfc_peek_ascii_char () != '(')
4484 : {
4485 : /* Assume a scalar variable */
4486 79248 : e = gfc_get_expr ();
4487 79248 : e->symtree = symtree;
4488 79248 : e->expr_type = EXPR_VARIABLE;
4489 :
4490 79248 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4491 : {
4492 : m = MATCH_ERROR;
4493 : break;
4494 : }
4495 :
4496 : /*FIXME:??? gfc_match_varspec does set this for us: */
4497 79248 : e->ts = sym->ts;
4498 79248 : m = gfc_match_varspec (e, 0, false, true);
4499 79248 : break;
4500 : }
4501 :
4502 : /* See if this is a function reference with a keyword argument
4503 : as first argument. We do this because otherwise a spurious
4504 : symbol would end up in the symbol table. */
4505 :
4506 182573 : old_loc = gfc_current_locus;
4507 182573 : m2 = gfc_match (" ( %n =", argname);
4508 182573 : gfc_current_locus = old_loc;
4509 :
4510 182573 : e = gfc_get_expr ();
4511 182573 : e->symtree = symtree;
4512 :
4513 182573 : if (m2 != MATCH_YES)
4514 : {
4515 : /* Try to figure out whether we're dealing with a character type.
4516 : We're peeking ahead here, because we don't want to call
4517 : match_substring if we're dealing with an implicitly typed
4518 : non-character variable. */
4519 181480 : implicit_char = false;
4520 181480 : if (sym->ts.type == BT_UNKNOWN)
4521 : {
4522 176689 : ts = gfc_get_default_type (sym->name, NULL);
4523 176689 : if (ts->type == BT_CHARACTER)
4524 : implicit_char = true;
4525 : }
4526 :
4527 : /* See if this could possibly be a substring reference of a name
4528 : that we're not sure is a variable yet. */
4529 :
4530 181463 : if ((implicit_char || sym->ts.type == BT_CHARACTER)
4531 1465 : && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4532 : {
4533 :
4534 1001 : e->expr_type = EXPR_VARIABLE;
4535 :
4536 1001 : if (sym->attr.flavor != FL_VARIABLE
4537 1001 : && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4538 : sym->name, NULL))
4539 : {
4540 : m = MATCH_ERROR;
4541 : break;
4542 : }
4543 :
4544 1001 : if (sym->ts.type == BT_UNKNOWN
4545 1001 : && !gfc_set_default_type (sym, 1, NULL))
4546 : {
4547 : m = MATCH_ERROR;
4548 : break;
4549 : }
4550 :
4551 1001 : e->ts = sym->ts;
4552 1001 : if (e->ref)
4553 976 : e->ts.u.cl = NULL;
4554 : m = MATCH_YES;
4555 : break;
4556 : }
4557 : }
4558 :
4559 : /* Give up, assume we have a function. */
4560 :
4561 181572 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4562 181572 : sym = symtree->n.sym;
4563 181572 : e->expr_type = EXPR_FUNCTION;
4564 :
4565 181572 : if (!sym->attr.function
4566 181572 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4567 : {
4568 : m = MATCH_ERROR;
4569 : break;
4570 : }
4571 :
4572 181572 : sym->result = sym;
4573 :
4574 181572 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4575 181572 : if (m == MATCH_NO)
4576 0 : gfc_error ("Missing argument list in function %qs at %C", sym->name);
4577 :
4578 181572 : if (m != MATCH_YES)
4579 : {
4580 : m = MATCH_ERROR;
4581 : break;
4582 : }
4583 :
4584 : /* If our new function returns a character, array or structure
4585 : type, it might have subsequent references. */
4586 :
4587 181442 : m = gfc_match_varspec (e, 0, false, true);
4588 181442 : if (m == MATCH_NO)
4589 : m = MATCH_YES;
4590 :
4591 : break;
4592 :
4593 69128 : generic_function:
4594 : /* Look for symbol first; if not found, look for STRUCTURE type symbol
4595 : specially. Creates a generic symbol for derived types. */
4596 69128 : gfc_find_sym_tree (name, NULL, 1, &symtree);
4597 69128 : if (!symtree)
4598 0 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4599 69128 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4600 69128 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4601 :
4602 69128 : e = gfc_get_expr ();
4603 69128 : e->symtree = symtree;
4604 69128 : e->expr_type = EXPR_FUNCTION;
4605 :
4606 69128 : if (gfc_fl_struct (sym->attr.flavor))
4607 : {
4608 0 : e->value.function.esym = sym;
4609 0 : e->symtree->n.sym->attr.generic = 1;
4610 : }
4611 :
4612 69128 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4613 69128 : break;
4614 :
4615 : case FL_NAMELIST:
4616 : m = MATCH_ERROR;
4617 : break;
4618 :
4619 5 : default:
4620 5 : gfc_error ("Symbol at %C is not appropriate for an expression");
4621 5 : return MATCH_ERROR;
4622 : }
4623 :
4624 : /* Scan for possible inquiry references. */
4625 623077 : if (m == MATCH_YES
4626 3492186 : && e->expr_type == EXPR_VARIABLE
4627 4276342 : && gfc_peek_ascii_char () == '%')
4628 : {
4629 38 : m = gfc_match_varspec (e, 0, false, false);
4630 38 : if (m == MATCH_NO)
4631 : m = MATCH_YES;
4632 : }
4633 :
4634 4150096 : if (m == MATCH_YES)
4635 : {
4636 3492186 : e->where = where;
4637 3492186 : *result = e;
4638 : }
4639 : else
4640 657910 : gfc_free_expr (e);
4641 :
4642 : return m;
4643 : }
4644 :
4645 :
4646 : /* Match a variable, i.e. something that can be assigned to. This
4647 : starts as a symbol, can be a structure component or an array
4648 : reference. It can be a function if the function doesn't have a
4649 : separate RESULT variable. If the symbol has not been previously
4650 : seen, we assume it is a variable.
4651 :
4652 : This function is called by two interface functions:
4653 : gfc_match_variable, which has host_flag = 1, and
4654 : gfc_match_equiv_variable, with host_flag = 0, to restrict the
4655 : match of the symbol to the local scope. */
4656 :
4657 : static match
4658 2945355 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4659 : {
4660 2945355 : gfc_symbol *sym, *dt_sym;
4661 2945355 : gfc_symtree *st;
4662 2945355 : gfc_expr *expr;
4663 2945355 : locus where, old_loc;
4664 2945355 : match m;
4665 :
4666 2945355 : *result = NULL;
4667 :
4668 : /* Since nothing has any business being an lvalue in a module
4669 : specification block, an interface block or a contains section,
4670 : we force the changed_symbols mechanism to work by setting
4671 : host_flag to 0. This prevents valid symbols that have the name
4672 : of keywords, such as 'end', being turned into variables by
4673 : failed matching to assignments for, e.g., END INTERFACE. */
4674 2945355 : if (gfc_current_state () == COMP_MODULE
4675 2945355 : || gfc_current_state () == COMP_SUBMODULE
4676 : || gfc_current_state () == COMP_INTERFACE
4677 : || gfc_current_state () == COMP_CONTAINS)
4678 206651 : host_flag = 0;
4679 :
4680 2945355 : where = gfc_current_locus;
4681 2945355 : m = gfc_match_sym_tree (&st, host_flag);
4682 2945354 : if (m != MATCH_YES)
4683 : return m;
4684 :
4685 2945323 : sym = st->n.sym;
4686 :
4687 : /* If this is an implicit do loop index and implicitly typed,
4688 : it should not be host associated. */
4689 2945323 : m = check_for_implicit_index (&st, &sym);
4690 2945323 : if (m != MATCH_YES)
4691 : return m;
4692 :
4693 2945323 : sym->attr.implied_index = 0;
4694 :
4695 2945323 : gfc_set_sym_referenced (sym);
4696 :
4697 : /* STRUCTUREs may share names with variables, but derived types may not. */
4698 14941 : if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4699 2945391 : && (dt_sym = gfc_find_dt_in_generic (sym)))
4700 : {
4701 7 : if (dt_sym->attr.flavor == FL_DERIVED)
4702 7 : gfc_error ("Derived type %qs cannot be used as a variable at %C",
4703 : sym->name);
4704 : return MATCH_ERROR;
4705 : }
4706 :
4707 2945316 : switch (sym->attr.flavor)
4708 : {
4709 : case FL_VARIABLE:
4710 : /* Everything is alright. */
4711 : break;
4712 :
4713 2656950 : case FL_UNKNOWN:
4714 2656950 : {
4715 2656950 : sym_flavor flavor = FL_UNKNOWN;
4716 :
4717 2656950 : gfc_gobble_whitespace ();
4718 :
4719 2656950 : if (sym->attr.external || sym->attr.procedure
4720 2656918 : || sym->attr.function || sym->attr.subroutine)
4721 : flavor = FL_PROCEDURE;
4722 :
4723 : /* If it is not a procedure, is not typed and is host associated,
4724 : we cannot give it a flavor yet. */
4725 2656918 : else if (sym->ns == gfc_current_ns->parent
4726 3099 : && sym->ts.type == BT_UNKNOWN)
4727 : break;
4728 :
4729 : /* These are definitive indicators that this is a variable. */
4730 3537982 : else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4731 3519788 : || sym->attr.pointer || sym->as != NULL)
4732 : flavor = FL_VARIABLE;
4733 :
4734 : if (flavor != FL_UNKNOWN
4735 1794681 : && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4736 : return MATCH_ERROR;
4737 : }
4738 : break;
4739 :
4740 17 : case FL_PARAMETER:
4741 17 : if (equiv_flag)
4742 : {
4743 0 : gfc_error ("Named constant at %C in an EQUIVALENCE");
4744 0 : return MATCH_ERROR;
4745 : }
4746 17 : if (gfc_in_match_data())
4747 : {
4748 4 : gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4749 : sym->name);
4750 4 : return MATCH_ERROR;
4751 : }
4752 : /* Otherwise this is checked for an error given in the
4753 : variable definition context checks. */
4754 : break;
4755 :
4756 14934 : case FL_PROCEDURE:
4757 : /* Check for a nonrecursive function result variable. */
4758 14934 : if (sym->attr.function
4759 12601 : && (!sym->attr.external || sym->abr_modproc_decl)
4760 12200 : && sym->result == sym
4761 26769 : && (gfc_is_function_return_value (sym, gfc_current_ns)
4762 2201 : || (sym->attr.entry
4763 499 : && sym->ns == gfc_current_ns)
4764 1709 : || (sym->attr.entry
4765 7 : && sym->ns == gfc_current_ns->parent)))
4766 : {
4767 : /* If a function result is a derived type, then the derived
4768 : type may still have to be resolved. */
4769 :
4770 10133 : if (sym->ts.type == BT_DERIVED
4771 10133 : && gfc_use_derived (sym->ts.u.derived) == NULL)
4772 : return MATCH_ERROR;
4773 : break;
4774 : }
4775 :
4776 4801 : if (sym->attr.proc_pointer
4777 4801 : || replace_hidden_procptr_result (&sym, &st))
4778 : break;
4779 :
4780 : /* Fall through to error */
4781 3124 : gcc_fallthrough ();
4782 :
4783 3124 : default:
4784 3124 : gfc_error ("%qs at %C is not a variable", sym->name);
4785 3124 : return MATCH_ERROR;
4786 : }
4787 :
4788 : /* Special case for derived type variables that get their types
4789 : via an IMPLICIT statement. This can't wait for the
4790 : resolution phase. */
4791 :
4792 2942184 : {
4793 2942184 : gfc_namespace * implicit_ns;
4794 :
4795 2942184 : if (gfc_current_ns->proc_name == sym)
4796 : implicit_ns = gfc_current_ns;
4797 : else
4798 2932928 : implicit_ns = sym->ns;
4799 :
4800 2942184 : old_loc = gfc_current_locus;
4801 2942184 : if (gfc_match_member_sep (sym) == MATCH_YES
4802 22394 : && sym->ts.type == BT_UNKNOWN
4803 2942196 : && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4804 3 : gfc_set_default_type (sym, 0, implicit_ns);
4805 2942184 : gfc_current_locus = old_loc;
4806 : }
4807 :
4808 2942184 : expr = gfc_get_expr ();
4809 :
4810 2942184 : expr->expr_type = EXPR_VARIABLE;
4811 2942184 : expr->symtree = st;
4812 2942184 : expr->ts = sym->ts;
4813 :
4814 : /* Now see if we have to do more. */
4815 2942184 : m = gfc_match_varspec (expr, equiv_flag, false, false);
4816 2942184 : if (m != MATCH_YES)
4817 : {
4818 83 : gfc_free_expr (expr);
4819 83 : return m;
4820 : }
4821 :
4822 2942101 : expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4823 2942101 : *result = expr;
4824 2942101 : return MATCH_YES;
4825 : }
4826 :
4827 :
4828 : match
4829 2942408 : gfc_match_variable (gfc_expr **result, int equiv_flag)
4830 : {
4831 2942408 : return match_variable (result, equiv_flag, 1);
4832 : }
4833 :
4834 :
4835 : match
4836 2947 : gfc_match_equiv_variable (gfc_expr **result)
4837 : {
4838 2947 : return match_variable (result, 1, 0);
4839 : }
|