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 474817 : match_kind_param (int *kind, int *is_iso_c)
41 : {
42 474817 : char name[GFC_MAX_SYMBOL_LEN + 1];
43 474817 : gfc_symbol *sym;
44 474817 : match m;
45 :
46 474817 : *is_iso_c = 0;
47 :
48 474817 : m = gfc_match_small_literal_int (kind, NULL, false);
49 474817 : if (m != MATCH_NO)
50 : return m;
51 :
52 95094 : m = gfc_match_name (name, false);
53 95094 : if (m != MATCH_YES)
54 : return m;
55 :
56 93362 : if (gfc_find_symbol (name, NULL, 1, &sym))
57 : return MATCH_ERROR;
58 :
59 93362 : if (sym == NULL)
60 : return MATCH_NO;
61 :
62 93361 : *is_iso_c = sym->attr.is_iso_c;
63 :
64 93361 : if (sym->attr.flavor != FL_PARAMETER)
65 : return MATCH_NO;
66 :
67 93361 : if (sym->value == NULL)
68 : return MATCH_NO;
69 :
70 93360 : if (gfc_extract_int (sym->value, kind))
71 : return MATCH_NO;
72 :
73 93360 : gfc_set_sym_referenced (sym);
74 :
75 93360 : 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 4669295 : get_kind (int *is_iso_c)
92 : {
93 4669295 : int kind;
94 4669295 : match m;
95 :
96 4669295 : *is_iso_c = 0;
97 :
98 4669295 : if (gfc_match_char ('_', false) != MATCH_YES)
99 : return -2;
100 :
101 474817 : m = match_kind_param (&kind, is_iso_c);
102 474817 : if (m == MATCH_NO)
103 1734 : gfc_error ("Missing kind-parameter at %C");
104 :
105 474817 : 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 31403788 : gfc_check_digit (char c, int radix)
114 : {
115 31403788 : bool r;
116 :
117 31403788 : 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 31304095 : case 10:
128 31304095 : r = ('0' <= c && c <= '9');
129 31304095 : 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 31403788 : 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 18363063 : match_digits (int signflag, int radix, char *buffer)
150 : {
151 18363063 : locus old_loc;
152 18363063 : int length;
153 18363063 : char c;
154 :
155 18363063 : length = 0;
156 18363063 : c = gfc_next_ascii_char ();
157 :
158 18363063 : 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 18363063 : if (!gfc_check_digit (c, radix))
168 : return -1;
169 :
170 8804798 : length++;
171 8804798 : if (buffer != NULL)
172 4394242 : *buffer++ = c;
173 :
174 17226188 : for (;;)
175 : {
176 13015493 : old_loc = gfc_current_locus;
177 13015493 : c = gfc_next_ascii_char ();
178 :
179 13015493 : if (!gfc_check_digit (c, radix))
180 : break;
181 :
182 4210695 : if (buffer != NULL)
183 2103076 : *buffer++ = c;
184 4210695 : length++;
185 : }
186 :
187 8804798 : gfc_current_locus = old_loc;
188 :
189 8804798 : return length;
190 : }
191 :
192 : /* Convert an integer string to an expression node. */
193 :
194 : static gfc_expr *
195 4286970 : convert_integer (const char *buffer, int kind, int radix, locus *where)
196 : {
197 4286970 : gfc_expr *e;
198 4286970 : const char *t;
199 :
200 4286970 : e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 : /* A leading plus is allowed, but not by mpz_set_str. */
202 4286970 : if (buffer[0] == '+')
203 21 : t = buffer + 1;
204 : else
205 : t = buffer;
206 4286970 : mpz_set_str (e->value.integer, t, radix);
207 :
208 4286970 : 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 101372 : convert_unsigned (const char *buffer, int kind, int radix, locus *where)
217 : {
218 101372 : gfc_expr *e;
219 101372 : const char *t;
220 101372 : int k;
221 101372 : arith rc;
222 :
223 101372 : e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
224 : /* A leading plus is allowed, but not by mpz_set_str. */
225 101372 : if (buffer[0] == '+')
226 0 : t = buffer + 1;
227 : else
228 : t = buffer;
229 :
230 101372 : mpz_set_str (e->value.integer, t, radix);
231 :
232 101372 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
233 :
234 : /* TODO Maybe move this somewhere else. */
235 101372 : rc = gfc_range_check (e);
236 101372 : 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 101372 : gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
245 : false);
246 :
247 101372 : return e;
248 : }
249 :
250 : /* Convert a real string to an expression node. */
251 :
252 : static gfc_expr *
253 221808 : convert_real (const char *buffer, int kind, locus *where)
254 : {
255 221808 : gfc_expr *e;
256 :
257 221808 : e = gfc_get_constant_expr (BT_REAL, kind, where);
258 221808 : mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259 :
260 221808 : 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 7023 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 : {
270 7023 : gfc_expr *e;
271 :
272 7023 : e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 7023 : mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 : GFC_MPC_RND_MODE);
275 :
276 7023 : 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 13375658 : match_integer_constant (gfc_expr **result, int signflag)
285 : {
286 13375658 : int length, kind, is_iso_c;
287 13375658 : locus old_loc;
288 13375658 : char *buffer;
289 13375658 : gfc_expr *e;
290 :
291 13375658 : old_loc = gfc_current_locus;
292 13375658 : gfc_gobble_whitespace ();
293 :
294 13375658 : length = match_digits (signflag, 10, NULL);
295 13375658 : gfc_current_locus = old_loc;
296 13375658 : if (length == -1)
297 : return MATCH_NO;
298 :
299 4288704 : buffer = (char *) alloca (length + 1);
300 4288704 : memset (buffer, '\0', length + 1);
301 :
302 4288704 : gfc_gobble_whitespace ();
303 :
304 4288704 : match_digits (signflag, 10, buffer);
305 :
306 4288704 : kind = get_kind (&is_iso_c);
307 4288704 : if (kind == -2)
308 3980904 : kind = gfc_default_integer_kind;
309 4288704 : if (kind == -1)
310 : return MATCH_ERROR;
311 :
312 4286974 : if (kind == 4 && flag_integer4_kind == 8)
313 0 : kind = 8;
314 :
315 4286974 : 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 4286970 : e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 4286970 : e->ts.is_c_interop = is_iso_c;
323 :
324 4286970 : if (gfc_range_check (e) != ARITH_OK)
325 : {
326 9586 : gfc_error ("Integer too big for its kind at %C. This check can be "
327 : "disabled with the option %<-fno-range-check%>");
328 :
329 9586 : gfc_free_expr (e);
330 9586 : return MATCH_ERROR;
331 : }
332 :
333 4277384 : *result = e;
334 4277384 : 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 588996 : match_unsigned_constant (gfc_expr **result)
345 : {
346 588996 : int length, kind, is_iso_c;
347 588996 : locus old_loc;
348 588996 : char *buffer;
349 588996 : gfc_expr *e;
350 588996 : match m;
351 :
352 588996 : old_loc = gfc_current_locus;
353 588996 : gfc_gobble_whitespace ();
354 :
355 588996 : length = match_digits (/* signflag = */ false, 10, NULL);
356 :
357 588996 : if (length == -1)
358 471311 : goto fail;
359 :
360 117685 : m = gfc_match_char ('u');
361 117685 : if (m == MATCH_NO)
362 16313 : goto fail;
363 :
364 101372 : gfc_current_locus = old_loc;
365 :
366 101372 : buffer = (char *) alloca (length + 1);
367 101372 : memset (buffer, '\0', length + 1);
368 :
369 101372 : gfc_gobble_whitespace ();
370 :
371 101372 : match_digits (false, 10, buffer);
372 :
373 101372 : m = gfc_match_char ('u');
374 101372 : if (m == MATCH_NO)
375 0 : goto fail;
376 :
377 101372 : kind = get_kind (&is_iso_c);
378 101372 : if (kind == -2)
379 8357 : kind = gfc_default_unsigned_kind;
380 101372 : if (kind == -1)
381 : return MATCH_ERROR;
382 :
383 101372 : if (kind == 4 && flag_integer4_kind == 8)
384 0 : kind = 8;
385 :
386 101372 : 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 101372 : e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
393 101372 : e->ts.is_c_interop = is_iso_c;
394 :
395 101372 : *result = e;
396 101372 : return MATCH_YES;
397 :
398 487624 : fail:
399 487624 : gfc_current_locus = old_loc;
400 487624 : return MATCH_NO;
401 : }
402 :
403 : /* Match a Hollerith constant. */
404 :
405 : static match
406 6693144 : match_hollerith_constant (gfc_expr **result)
407 : {
408 6693144 : locus old_loc;
409 6693144 : gfc_expr *e = NULL;
410 6693144 : int num, pad;
411 6693144 : int i;
412 :
413 6693144 : old_loc = gfc_current_locus;
414 6693144 : gfc_gobble_whitespace ();
415 :
416 6693144 : if (match_integer_constant (&e, 0) == MATCH_YES
417 6693144 : && 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 6690508 : gfc_free_expr (e);
474 6690508 : gfc_current_locus = old_loc;
475 6690508 : 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 6904694 : match_boz_constant (gfc_expr **result)
491 : {
492 6904694 : int radix, length, x_hex;
493 6904694 : locus old_loc, start_loc;
494 6904694 : char *buffer, post, delim;
495 6904694 : gfc_expr *e;
496 :
497 6904694 : start_loc = old_loc = gfc_current_locus;
498 6904694 : gfc_gobble_whitespace ();
499 :
500 6904694 : x_hex = 0;
501 6904694 : switch (post = gfc_next_ascii_char ())
502 : {
503 : case 'b':
504 : radix = 2;
505 : post = 0;
506 : break;
507 59082 : case 'o':
508 59082 : radix = 8;
509 59082 : post = 0;
510 59082 : break;
511 93518 : case 'x':
512 93518 : 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 6611685 : default:
526 6611685 : goto backup;
527 : }
528 :
529 : /* No whitespace allowed here. */
530 :
531 59082 : if (post == 0)
532 292984 : delim = gfc_next_ascii_char ();
533 :
534 293009 : if (delim != '\'' && delim != '\"')
535 288840 : 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 6900525 : backup:
609 6900525 : gfc_current_locus = start_loc;
610 6900525 : 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 7008344 : match_real_constant (gfc_expr **result, int signflag)
619 : {
620 7008344 : int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 7008344 : locus old_loc, temp_loc;
622 7008344 : char *p, *buffer, c, exp_char;
623 7008344 : gfc_expr *e;
624 7008344 : bool negate;
625 :
626 7008344 : old_loc = gfc_current_locus;
627 7008344 : gfc_gobble_whitespace ();
628 :
629 7008344 : e = NULL;
630 :
631 7008344 : default_exponent = 0;
632 7008344 : count = 0;
633 7008344 : seen_dp = 0;
634 7008344 : seen_digits = 0;
635 7008344 : exp_char = ' ';
636 7008344 : negate = false;
637 :
638 7008344 : c = gfc_next_ascii_char ();
639 7008344 : 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 3989178 : for (;; c = gfc_next_ascii_char (), count++)
650 : {
651 10997522 : if (c == '.')
652 : {
653 282726 : if (seen_dp)
654 204 : goto done;
655 :
656 : /* Check to see if "." goes with a following operator like
657 : ".eq.". */
658 282522 : temp_loc = gfc_current_locus;
659 282522 : c = gfc_next_ascii_char ();
660 :
661 282522 : if (c == 'e' || c == 'd' || c == 'q')
662 : {
663 17722 : c = gfc_next_ascii_char ();
664 17722 : if (c == '.')
665 0 : goto done; /* Operator named .e. or .d. */
666 : }
667 :
668 282522 : if (ISALPHA (c))
669 67757 : goto done; /* Distinguish 1.e9 from 1.eq.2 */
670 :
671 214765 : gfc_current_locus = temp_loc;
672 214765 : seen_dp = 1;
673 214765 : continue;
674 : }
675 :
676 10714796 : if (ISDIGIT (c))
677 : {
678 3774413 : seen_digits = 1;
679 3774413 : continue;
680 : }
681 :
682 6940383 : break;
683 : }
684 :
685 6940383 : if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 2381774 : goto done;
687 38504 : exp_char = c;
688 :
689 :
690 38504 : 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 38504 : c = gfc_next_ascii_char ();
703 38504 : count++;
704 :
705 38504 : if (c == '+' || c == '-')
706 : { /* optional sign */
707 7643 : c = gfc_next_ascii_char ();
708 7643 : count++;
709 : }
710 :
711 38504 : if (!ISDIGIT (c))
712 : {
713 : /* With -fdec, default exponent to 0 instead of complaining. */
714 40 : if (flag_dec)
715 38494 : 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 80031 : while (ISDIGIT (c))
724 : {
725 41537 : c = gfc_next_ascii_char ();
726 41537 : count++;
727 : }
728 :
729 7008334 : done:
730 : /* Check that we have a numeric constant. */
731 7008334 : if (!seen_digits || (!seen_dp && exp_char == ' '))
732 : {
733 6786522 : gfc_current_locus = old_loc;
734 6786522 : return MATCH_NO;
735 : }
736 :
737 : /* Convert the number. */
738 221812 : gfc_current_locus = old_loc;
739 221812 : gfc_gobble_whitespace ();
740 :
741 221812 : buffer = (char *) alloca (count + default_exponent + 1);
742 221812 : memset (buffer, '\0', count + default_exponent + 1);
743 :
744 221812 : p = buffer;
745 221812 : c = gfc_next_ascii_char ();
746 221812 : 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 1443540 : for (;;)
754 : {
755 832676 : if (c == 'd' || c == 'q')
756 : *p = 'e';
757 : else
758 802415 : *p = c;
759 832676 : p++;
760 832676 : if (--count == 0)
761 : break;
762 :
763 610864 : c = gfc_next_ascii_char ();
764 : }
765 221812 : if (default_exponent)
766 30 : *p++ = '0';
767 :
768 221812 : kind = get_kind (&is_iso_c);
769 221812 : if (kind == -1)
770 4 : goto cleanup;
771 :
772 221808 : if (kind == 4)
773 : {
774 20844 : if (flag_real4_kind == 8)
775 192 : kind = 8;
776 20844 : if (flag_real4_kind == 10)
777 192 : kind = 10;
778 20844 : if (flag_real4_kind == 16)
779 384 : kind = 16;
780 : }
781 200964 : else if (kind == 8)
782 : {
783 28167 : if (flag_real8_kind == 4)
784 192 : kind = 4;
785 28167 : if (flag_real8_kind == 10)
786 192 : kind = 10;
787 28167 : if (flag_real8_kind == 16)
788 384 : kind = 16;
789 : }
790 :
791 221808 : switch (exp_char)
792 : {
793 30261 : case 'd':
794 30261 : 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 30261 : kind = gfc_default_double_kind;
801 30261 : 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 191547 : default:
828 191547 : if (kind == -2)
829 118044 : kind = gfc_default_real_kind;
830 :
831 191547 : 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 221808 : e = convert_real (buffer, kind, &gfc_current_locus);
839 221808 : if (negate)
840 2980 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 221808 : e->ts.is_c_interop = is_iso_c;
842 :
843 221808 : 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 221807 : 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 221807 : *result = e;
915 221807 : 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 623951 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 : {
928 623951 : gfc_expr *start, *end;
929 623951 : locus old_loc;
930 623951 : gfc_ref *ref;
931 623951 : match m;
932 :
933 623951 : start = NULL;
934 623951 : end = NULL;
935 :
936 623951 : old_loc = gfc_current_locus;
937 :
938 623951 : m = gfc_match_char ('(');
939 623951 : if (m != MATCH_YES)
940 : return MATCH_NO;
941 :
942 17177 : if (gfc_match_char (':') != MATCH_YES)
943 : {
944 16299 : if (init)
945 0 : m = gfc_match_init_expr (&start);
946 : else
947 16299 : m = gfc_match_expr (&start);
948 :
949 16299 : if (m != MATCH_YES)
950 : {
951 154 : m = MATCH_NO;
952 154 : goto cleanup;
953 : }
954 :
955 16145 : m = gfc_match_char (':');
956 16145 : if (m != MATCH_YES)
957 460 : goto cleanup;
958 : }
959 :
960 16563 : if (gfc_match_char (')') != MATCH_YES)
961 : {
962 15634 : if (init)
963 0 : m = gfc_match_init_expr (&end);
964 : else
965 15634 : m = gfc_match_expr (&end);
966 :
967 15634 : if (m == MATCH_NO)
968 2 : goto syntax;
969 15632 : if (m == MATCH_ERROR)
970 0 : goto cleanup;
971 :
972 15632 : m = gfc_match_char (')');
973 15632 : if (m == MATCH_NO)
974 3 : goto syntax;
975 : }
976 :
977 : /* Optimize away the (:) reference. */
978 16558 : if (start == NULL && end == NULL && !deferred)
979 : ref = NULL;
980 : else
981 : {
982 16353 : ref = gfc_get_ref ();
983 :
984 16353 : ref->type = REF_SUBSTRING;
985 16353 : if (start == NULL)
986 671 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 16353 : ref->u.ss.start = start;
988 16353 : if (end == NULL && cl)
989 722 : end = gfc_copy_expr (cl->length);
990 16353 : ref->u.ss.end = end;
991 16353 : ref->u.ss.length = cl;
992 : }
993 :
994 16558 : *result = ref;
995 16558 : 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 4323817 : next_string_char (gfc_char_t delimiter, int *ret)
1022 : {
1023 4323817 : locus old_locus;
1024 4323817 : gfc_char_t c;
1025 :
1026 4323817 : c = gfc_next_char_literal (INSTRING_WARN);
1027 4323817 : *ret = 0;
1028 :
1029 4323817 : if (c == '\n')
1030 : {
1031 4 : *ret = -2;
1032 4 : return 0;
1033 : }
1034 :
1035 4323813 : 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 4323813 : if (c != delimiter)
1047 : return c;
1048 :
1049 620880 : old_locus = gfc_current_locus;
1050 620880 : c = gfc_next_char_literal (NONSTRING);
1051 :
1052 620880 : if (c == delimiter)
1053 : return c;
1054 620062 : gfc_current_locus = old_locus;
1055 :
1056 620062 : *ret = -1;
1057 620062 : 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 4510544 : match_charkind_name (char *name)
1075 : {
1076 4510544 : locus old_loc;
1077 4510544 : char c, peek;
1078 4510544 : int len;
1079 :
1080 4510544 : gfc_gobble_whitespace ();
1081 4510544 : c = gfc_next_ascii_char ();
1082 4510544 : if (!ISALPHA (c))
1083 : return MATCH_NO;
1084 :
1085 4098878 : *name++ = c;
1086 4098878 : len = 1;
1087 :
1088 16729865 : for (;;)
1089 : {
1090 16729865 : old_loc = gfc_current_locus;
1091 16729865 : c = gfc_next_ascii_char ();
1092 :
1093 16729865 : if (c == '_')
1094 : {
1095 546882 : peek = gfc_peek_ascii_char ();
1096 :
1097 546882 : if (peek == '\'' || peek == '\"')
1098 : {
1099 996 : gfc_current_locus = old_loc;
1100 996 : *name = '\0';
1101 996 : return MATCH_YES;
1102 : }
1103 : }
1104 :
1105 16728869 : if (!ISALNUM (c)
1106 4643768 : && c != '_'
1107 4097882 : && (c != '$' || !flag_dollar_ok))
1108 : break;
1109 :
1110 12630987 : *name++ = c;
1111 12630987 : 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 7214719 : match_string_constant (gfc_expr **result)
1128 : {
1129 7214719 : char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 7214719 : size_t length;
1131 7214719 : int kind,save_warn_ampersand, ret;
1132 7214719 : locus old_locus, start_locus;
1133 7214719 : gfc_symbol *sym;
1134 7214719 : gfc_expr *e;
1135 7214719 : match m;
1136 7214719 : gfc_char_t c, delimiter, *p;
1137 :
1138 7214719 : old_locus = gfc_current_locus;
1139 :
1140 7214719 : gfc_gobble_whitespace ();
1141 :
1142 7214719 : c = gfc_next_char ();
1143 7214719 : if (c == '\'' || c == '"')
1144 : {
1145 269535 : kind = gfc_default_character_kind;
1146 269535 : start_locus = gfc_current_locus;
1147 269535 : goto got_delim;
1148 : }
1149 :
1150 6945184 : if (gfc_wide_is_digit (c))
1151 : {
1152 2434640 : kind = 0;
1153 :
1154 5838274 : while (gfc_wide_is_digit (c))
1155 : {
1156 3417092 : kind = kind * 10 + c - '0';
1157 3417092 : if (kind > 9999999)
1158 13458 : goto no_match;
1159 3403634 : c = gfc_next_char ();
1160 : }
1161 :
1162 : }
1163 : else
1164 : {
1165 4510544 : gfc_current_locus = old_locus;
1166 :
1167 4510544 : m = match_charkind_name (name);
1168 4510544 : if (m != MATCH_YES)
1169 4509548 : 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 2422177 : if (c != '_')
1181 2232720 : goto no_match;
1182 :
1183 189457 : c = gfc_next_char ();
1184 189457 : if (c != '\'' && c != '"')
1185 148942 : goto no_match;
1186 :
1187 40515 : start_locus = gfc_current_locus;
1188 :
1189 40515 : 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 40515 : 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 40515 : 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 310050 : delimiter = c;
1209 310050 : length = 0;
1210 :
1211 4014110 : for (;;)
1212 : {
1213 2162080 : c = next_string_char (delimiter, &ret);
1214 2162080 : if (ret == -1)
1215 : break;
1216 1852034 : 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 1852030 : 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 310046 : peek = gfc_peek_ascii_char ();
1229 310046 : if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 25 : goto no_match;
1231 :
1232 310021 : e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233 :
1234 310021 : 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 310021 : save_warn_ampersand = warn_ampersand;
1239 310021 : warn_ampersand = false;
1240 :
1241 310021 : p = e->value.character.string;
1242 2161737 : for (size_t i = 0; i < length; i++)
1243 : {
1244 1851721 : c = next_string_char (delimiter, &ret);
1245 :
1246 1851721 : 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 1851716 : *p++ = c;
1255 : }
1256 :
1257 310016 : *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 310016 : warn_ampersand = save_warn_ampersand;
1259 :
1260 310016 : next_string_char (delimiter, &ret);
1261 310016 : if (ret != -1)
1262 0 : gfc_internal_error ("match_string_constant(): Delimiter not found");
1263 :
1264 310016 : 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 310016 : 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 310010 : *result = e;
1323 :
1324 310010 : return MATCH_YES;
1325 :
1326 6904694 : no_match:
1327 6904694 : gfc_current_locus = old_locus;
1328 6904694 : 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 4504353 : match_logical_constant_string (void)
1336 : {
1337 4504353 : locus orig_loc = gfc_current_locus;
1338 :
1339 4504353 : gfc_gobble_whitespace ();
1340 4504353 : if (gfc_next_ascii_char () == '.')
1341 : {
1342 57408 : char ch = gfc_next_ascii_char ();
1343 57408 : if (ch == 'f')
1344 : {
1345 29323 : if (gfc_next_ascii_char () == 'a'
1346 29323 : && gfc_next_ascii_char () == 'l'
1347 29323 : && gfc_next_ascii_char () == 's'
1348 29323 : && gfc_next_ascii_char () == 'e'
1349 58646 : && gfc_next_ascii_char () == '.')
1350 : /* Matched ".false.". */
1351 : return 0;
1352 : }
1353 28085 : else if (ch == 't')
1354 : {
1355 28084 : if (gfc_next_ascii_char () == 'r'
1356 28084 : && gfc_next_ascii_char () == 'u'
1357 28084 : && gfc_next_ascii_char () == 'e'
1358 56168 : && gfc_next_ascii_char () == '.')
1359 : /* Matched ".true.". */
1360 : return 1;
1361 : }
1362 : }
1363 4446946 : gfc_current_locus = orig_loc;
1364 4446946 : return -1;
1365 : }
1366 :
1367 : /* Match a .true. or .false. */
1368 :
1369 : static match
1370 4504353 : match_logical_constant (gfc_expr **result)
1371 : {
1372 4504353 : gfc_expr *e;
1373 4504353 : int i, kind, is_iso_c;
1374 :
1375 4504353 : i = match_logical_constant_string ();
1376 4504353 : if (i == -1)
1377 : return MATCH_NO;
1378 :
1379 57407 : kind = get_kind (&is_iso_c);
1380 57407 : if (kind == -1)
1381 : return MATCH_ERROR;
1382 57407 : if (kind == -2)
1383 56912 : kind = gfc_default_logical_kind;
1384 :
1385 57407 : 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 57403 : e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 57403 : e->ts.is_c_interop = is_iso_c;
1393 :
1394 57403 : *result = e;
1395 57403 : 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 141749 : match_sym_complex_part (gfc_expr **result)
1404 : {
1405 141749 : char name[GFC_MAX_SYMBOL_LEN + 1];
1406 141749 : gfc_symbol *sym;
1407 141749 : gfc_expr *e;
1408 141749 : match m;
1409 :
1410 141749 : m = gfc_match_name (name);
1411 141749 : if (m != MATCH_YES)
1412 : return m;
1413 :
1414 39635 : if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 : return MATCH_NO;
1416 :
1417 36925 : 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 35334 : char c;
1423 35334 : gfc_gobble_whitespace ();
1424 35334 : c = gfc_peek_ascii_char ();
1425 35334 : if (c == '=' || c == ',')
1426 : {
1427 : m = MATCH_NO;
1428 : }
1429 : else
1430 : {
1431 32339 : gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 32339 : m = MATCH_ERROR;
1433 : }
1434 : return m;
1435 : }
1436 :
1437 1591 : if (!sym->value)
1438 2 : goto error;
1439 :
1440 1589 : 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 1255 : 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 1081 : if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1453 : "complex constant at %C"))
1454 : return MATCH_ERROR;
1455 :
1456 1078 : 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 1007 : case BT_INTEGER:
1469 1007 : e = gfc_int2real (sym->value, gfc_default_real_kind);
1470 1007 : 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 1076 : *result = e; /* e is a scalar, real, constant expression. */
1482 1076 : 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 141749 : match_complex_part (gfc_expr **result)
1494 : {
1495 141749 : match m;
1496 :
1497 141749 : m = match_sym_complex_part (result);
1498 141749 : if (m != MATCH_NO)
1499 : return m;
1500 :
1501 107819 : m = match_real_constant (result, 1);
1502 107819 : if (m != MATCH_NO)
1503 : return m;
1504 :
1505 93378 : return match_integer_constant (result, 1);
1506 : }
1507 :
1508 :
1509 : /* Try to match a complex constant. */
1510 :
1511 : static match
1512 7225041 : match_complex_constant (gfc_expr **result)
1513 : {
1514 7225041 : gfc_expr *e, *real, *imag;
1515 7225041 : gfc_error_buffer old_error;
1516 7225041 : gfc_typespec target;
1517 7225041 : locus old_loc;
1518 7225041 : int kind;
1519 7225041 : match m;
1520 :
1521 7225041 : old_loc = gfc_current_locus;
1522 7225041 : real = imag = e = NULL;
1523 :
1524 7225041 : m = gfc_match_char ('(');
1525 7225041 : if (m != MATCH_YES)
1526 : return m;
1527 :
1528 131431 : gfc_push_error (&old_error);
1529 :
1530 131431 : m = match_complex_part (&real);
1531 131431 : if (m == MATCH_NO)
1532 : {
1533 75041 : gfc_free_error (&old_error);
1534 75041 : goto cleanup;
1535 : }
1536 :
1537 56390 : 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 46068 : gfc_clear_warning ();
1543 46068 : gfc_pop_error (&old_error);
1544 46068 : m = MATCH_NO;
1545 46068 : 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 10322 : if (m == MATCH_ERROR)
1554 : {
1555 4 : gfc_free_error (&old_error);
1556 4 : goto cleanup;
1557 : }
1558 10318 : gfc_pop_error (&old_error);
1559 :
1560 10318 : m = match_complex_part (&imag);
1561 10318 : if (m == MATCH_NO)
1562 3129 : goto syntax;
1563 7189 : if (m == MATCH_ERROR)
1564 153 : goto cleanup;
1565 :
1566 7036 : m = gfc_match_char (')');
1567 7036 : 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 7023 : if (m == MATCH_ERROR)
1581 0 : goto cleanup;
1582 :
1583 : /* Decide on the kind of this complex number. */
1584 7023 : if (real->ts.type == BT_REAL)
1585 : {
1586 6589 : if (imag->ts.type == BT_REAL)
1587 6564 : 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 7023 : gfc_clear_ts (&target);
1599 7023 : target.type = BT_REAL;
1600 7023 : target.kind = kind;
1601 :
1602 7023 : if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 435 : gfc_convert_type (real, &target, 2);
1604 7023 : if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 490 : gfc_convert_type (imag, &target, 2);
1606 :
1607 7023 : e = convert_complex (real, imag, kind);
1608 7023 : e->where = gfc_current_locus;
1609 :
1610 7023 : gfc_free_expr (real);
1611 7023 : gfc_free_expr (imag);
1612 :
1613 7023 : *result = e;
1614 7023 : return MATCH_YES;
1615 :
1616 3142 : syntax:
1617 3142 : gfc_error ("Syntax error in COMPLEX constant at %C");
1618 3142 : m = MATCH_ERROR;
1619 :
1620 124408 : cleanup:
1621 124408 : gfc_free_expr (e);
1622 124408 : gfc_free_expr (real);
1623 124408 : gfc_free_expr (imag);
1624 124408 : gfc_current_locus = old_loc;
1625 :
1626 124408 : return m;
1627 7225041 : }
1628 :
1629 :
1630 : /* Match constants in any of several forms. Returns nonzero for a
1631 : match, zero for no match. */
1632 :
1633 : match
1634 7225041 : gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 : {
1636 7225041 : match m;
1637 :
1638 7225041 : m = match_complex_constant (result);
1639 7225041 : if (m != MATCH_NO)
1640 : return m;
1641 :
1642 7214719 : m = match_string_constant (result);
1643 7214719 : if (m != MATCH_NO)
1644 : return m;
1645 :
1646 6904694 : m = match_boz_constant (result);
1647 6904694 : if (m != MATCH_NO)
1648 : return m;
1649 :
1650 6900525 : m = match_real_constant (result, signflag);
1651 6900525 : if (m != MATCH_NO)
1652 : return m;
1653 :
1654 6693144 : m = match_hollerith_constant (result);
1655 6693144 : if (m != MATCH_NO)
1656 : return m;
1657 :
1658 6690508 : if (flag_unsigned)
1659 : {
1660 588996 : m = match_unsigned_constant (result);
1661 588996 : if (m != MATCH_NO)
1662 : return m;
1663 : }
1664 :
1665 6589136 : m = match_integer_constant (result, signflag);
1666 6589136 : if (m != MATCH_NO)
1667 : return m;
1668 :
1669 4504353 : m = match_logical_constant (result);
1670 4504353 : 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 788541 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 : {
1684 788541 : if (!sym->attr.function || (sym->result != sym))
1685 : return false;
1686 1654173 : while (ns)
1687 : {
1688 937508 : if (ns->proc_name == sym)
1689 : return true;
1690 925613 : 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 2042903 : match_actual_arg (gfc_expr **result)
1705 : {
1706 2042903 : char name[GFC_MAX_SYMBOL_LEN + 1];
1707 2042903 : gfc_symtree *symtree;
1708 2042903 : locus where, w;
1709 2042903 : gfc_expr *e;
1710 2042903 : char c;
1711 :
1712 2042903 : gfc_gobble_whitespace ();
1713 2042903 : where = gfc_current_locus;
1714 :
1715 2042903 : switch (gfc_match_name (name))
1716 : {
1717 : case MATCH_ERROR:
1718 : return MATCH_ERROR;
1719 :
1720 : case MATCH_NO:
1721 : break;
1722 :
1723 1338002 : case MATCH_YES:
1724 1338002 : w = gfc_current_locus;
1725 1338002 : gfc_gobble_whitespace ();
1726 1338002 : c = gfc_next_ascii_char ();
1727 1338002 : gfc_current_locus = w;
1728 :
1729 1338002 : if (c != ',' && c != ')')
1730 : break;
1731 :
1732 701697 : 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 701697 : if (symtree == NULL)
1739 : {
1740 14113 : gfc_get_sym_tree (name, NULL, &symtree, false);
1741 14113 : gfc_set_sym_referenced (symtree->n.sym);
1742 : }
1743 : else
1744 : {
1745 687584 : gfc_symbol *sym;
1746 :
1747 687584 : sym = symtree->n.sym;
1748 687584 : gfc_set_sym_referenced (sym);
1749 687584 : if (sym->attr.flavor == FL_NAMELIST)
1750 : {
1751 1159 : gfc_error ("Namelist %qs cannot be an argument at %L",
1752 : sym->name, &where);
1753 1159 : break;
1754 : }
1755 686425 : if (sym->attr.flavor != FL_PROCEDURE
1756 647693 : && sym->attr.flavor != FL_UNKNOWN)
1757 : break;
1758 :
1759 194388 : 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 194164 : if (sym->attr.function && sym->result == sym)
1770 : {
1771 3799 : if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 : break;
1773 :
1774 3145 : 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 207569 : e = gfc_get_expr (); /* Leave it unknown for now */
1791 207569 : e->symtree = symtree;
1792 207569 : e->expr_type = EXPR_VARIABLE;
1793 207569 : e->ts.type = BT_PROCEDURE;
1794 207569 : e->where = where;
1795 :
1796 207569 : *result = e;
1797 207569 : return MATCH_YES;
1798 : }
1799 :
1800 1835334 : gfc_current_locus = where;
1801 1835334 : return gfc_match_expr (result);
1802 : }
1803 :
1804 :
1805 : /* Match a keyword argument or type parameter spec list.. */
1806 :
1807 : static match
1808 2034590 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 : {
1810 2034590 : char name[GFC_MAX_SYMBOL_LEN + 1];
1811 2034590 : gfc_actual_arglist *a;
1812 2034590 : locus name_locus;
1813 2034590 : match m;
1814 :
1815 2034590 : name_locus = gfc_current_locus;
1816 2034590 : m = gfc_match_name (name);
1817 :
1818 2034590 : if (m != MATCH_YES)
1819 593877 : goto cleanup;
1820 1440713 : if (gfc_match_char ('=') != MATCH_YES)
1821 : {
1822 1277792 : m = MATCH_NO;
1823 1277792 : goto cleanup;
1824 : }
1825 :
1826 162921 : 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 162774 : m = match_actual_arg (&actual->expr);
1843 162774 : if (m != MATCH_YES)
1844 11571 : goto cleanup;
1845 :
1846 : /* Make sure this name has not appeared yet. */
1847 151203 : add_name:
1848 151350 : if (name[0] != '\0')
1849 : {
1850 486238 : for (a = base; a; a = a->next)
1851 334902 : 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 151336 : actual->name = gfc_get_string ("%s", name);
1860 151336 : return MATCH_YES;
1861 :
1862 1883240 : cleanup:
1863 1883240 : gfc_current_locus = name_locus;
1864 1883240 : return m;
1865 : }
1866 :
1867 :
1868 : /* Match an argument list function, such as %VAL. */
1869 :
1870 : static match
1871 1995041 : match_arg_list_function (gfc_actual_arglist *result)
1872 : {
1873 1995041 : char name[GFC_MAX_SYMBOL_LEN + 1];
1874 1995041 : locus old_locus;
1875 1995041 : match m;
1876 :
1877 1995041 : old_locus = gfc_current_locus;
1878 :
1879 1995041 : if (gfc_match_char ('%') != MATCH_YES)
1880 : {
1881 1994976 : m = MATCH_NO;
1882 1994976 : 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 1994977 : cleanup:
1939 1994977 : gfc_current_locus = old_locus;
1940 1994977 : 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 2122624 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 : {
1959 2122624 : gfc_actual_arglist *head, *tail;
1960 2122624 : int seen_keyword;
1961 2122624 : gfc_st_label *label;
1962 2122624 : locus old_loc;
1963 2122624 : match m;
1964 :
1965 2122624 : *argp = tail = NULL;
1966 2122624 : old_loc = gfc_current_locus;
1967 :
1968 2122624 : seen_keyword = 0;
1969 :
1970 2122624 : if (gfc_match_char ('(') == MATCH_NO)
1971 648134 : return (sub_flag) ? MATCH_YES : MATCH_NO;
1972 :
1973 1474490 : if (gfc_match_char (')') == MATCH_YES)
1974 : return MATCH_YES;
1975 :
1976 1445342 : head = NULL;
1977 :
1978 1445342 : matching_actual_arglist++;
1979 :
1980 2034116 : for (;;)
1981 : {
1982 2034116 : if (head == NULL)
1983 1445342 : head = tail = gfc_get_actual_arglist ();
1984 : else
1985 : {
1986 588774 : tail->next = gfc_get_actual_arglist ();
1987 588774 : tail = tail->next;
1988 : }
1989 :
1990 2034116 : 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 2033878 : if (pdt && !seen_keyword)
2007 : {
2008 1647 : if (gfc_match_char (':') == MATCH_YES)
2009 : {
2010 102 : tail->spec_type = SPEC_DEFERRED;
2011 102 : goto next;
2012 : }
2013 1545 : else if (gfc_match_char ('*') == MATCH_YES)
2014 : {
2015 141 : tail->spec_type = SPEC_ASSUMED;
2016 141 : goto next;
2017 : }
2018 : else
2019 1404 : tail->spec_type = SPEC_EXPLICIT;
2020 :
2021 1404 : m = match_keyword_arg (tail, head, pdt);
2022 1404 : if (m == MATCH_YES)
2023 : {
2024 384 : seen_keyword = 1;
2025 384 : goto next;
2026 : }
2027 1020 : 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 2033251 : if (seen_keyword)
2034 : {
2035 38210 : m = match_keyword_arg (tail, head, pdt);
2036 :
2037 38210 : if (m == MATCH_ERROR)
2038 34 : goto cleanup;
2039 38176 : if (m == MATCH_NO)
2040 : {
2041 1395 : gfc_error ("Missing keyword name in actual argument list at %C");
2042 1395 : goto cleanup;
2043 : }
2044 :
2045 : }
2046 : else
2047 : {
2048 : /* Try an argument list function, like %VAL. */
2049 1995041 : m = match_arg_list_function (tail);
2050 1995041 : if (m == MATCH_ERROR)
2051 1 : goto cleanup;
2052 :
2053 : /* See if we have the first keyword argument. */
2054 1995040 : if (m == MATCH_NO)
2055 : {
2056 1994976 : m = match_keyword_arg (tail, head, false);
2057 1994976 : if (m == MATCH_YES)
2058 : seen_keyword = 1;
2059 1880805 : if (m == MATCH_ERROR)
2060 740 : goto cleanup;
2061 : }
2062 :
2063 1994236 : if (m == MATCH_NO)
2064 : {
2065 : /* Try for a non-keyword argument. */
2066 1880065 : m = match_actual_arg (&tail->expr);
2067 1880065 : if (m == MATCH_ERROR)
2068 2037 : goto cleanup;
2069 1878028 : if (m == MATCH_NO)
2070 20179 : 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 2008865 : if (tail->expr
2078 2008781 : && tail->expr->expr_type == EXPR_VARIABLE
2079 4017646 : && gfc_expr_attr (tail->expr).pdt_kind)
2080 : {
2081 520 : gfc_ref *ref;
2082 520 : gfc_expr *tmp = NULL;
2083 542 : 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 520 : if (tmp)
2089 22 : gfc_replace_expr (tail->expr, tmp);
2090 : }
2091 :
2092 2009730 : next:
2093 2009730 : if (gfc_match_char (')') == MATCH_YES)
2094 : break;
2095 597612 : if (gfc_match_char (',') != MATCH_YES)
2096 8838 : goto syntax;
2097 : }
2098 :
2099 1412118 : *argp = head;
2100 1412118 : matching_actual_arglist--;
2101 1412118 : return MATCH_YES;
2102 :
2103 29017 : syntax:
2104 29017 : gfc_error ("Syntax error in argument list at %C");
2105 :
2106 33224 : cleanup:
2107 33224 : gfc_free_actual_arglist (head);
2108 33224 : gfc_current_locus = old_loc;
2109 33224 : matching_actual_arglist--;
2110 33224 : 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 752970 : extend_ref (gfc_expr *primary, gfc_ref *tail)
2119 : {
2120 752970 : if (primary->ref == NULL)
2121 681396 : primary->ref = tail = gfc_get_ref ();
2122 71574 : 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 71536 : tail->next = gfc_get_ref ();
2135 71536 : tail = tail->next;
2136 : }
2137 :
2138 752970 : return tail;
2139 : }
2140 :
2141 :
2142 : /* Used by gfc_match_varspec() to match an inquiry reference. */
2143 :
2144 : bool
2145 4921 : is_inquiry_ref (const char *name, gfc_ref **ref)
2146 : {
2147 4921 : inquiry_type type;
2148 :
2149 4921 : if (name == NULL)
2150 : return false;
2151 :
2152 4921 : if (ref) *ref = NULL;
2153 :
2154 4921 : if (strcmp (name, "re") == 0)
2155 : type = INQUIRY_RE;
2156 3476 : else if (strcmp (name, "im") == 0)
2157 : type = INQUIRY_IM;
2158 2498 : else if (strcmp (name, "kind") == 0)
2159 : type = INQUIRY_KIND;
2160 1787 : else if (strcmp (name, "len") == 0)
2161 : type = INQUIRY_LEN;
2162 : else
2163 : return false;
2164 :
2165 3960 : if (ref)
2166 : {
2167 2211 : *ref = gfc_get_ref ();
2168 2211 : (*ref)->type = REF_INQUIRY;
2169 2211 : (*ref)->u.i = type;
2170 : }
2171 :
2172 : return true;
2173 : }
2174 :
2175 :
2176 : /* Check to see if functions in operator expressions can be resolved now. */
2177 :
2178 : static bool
2179 126 : resolvable_fcns (gfc_expr *e,
2180 : gfc_symbol *sym ATTRIBUTE_UNUSED,
2181 : int *f ATTRIBUTE_UNUSED)
2182 : {
2183 126 : bool p;
2184 126 : gfc_symbol *s;
2185 :
2186 126 : if (e->expr_type != EXPR_FUNCTION)
2187 : return false;
2188 :
2189 54 : s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2190 54 : p = s && (s->attr.use_assoc
2191 54 : || s->attr.host_assoc
2192 54 : || s->attr.if_source == IFSRC_DECL
2193 54 : || s->attr.proc == PROC_INTRINSIC
2194 24 : || gfc_is_intrinsic (s, 0, e->where));
2195 54 : return !p;
2196 : }
2197 :
2198 :
2199 : /* Match any additional specifications associated with the current
2200 : variable like member references or substrings. If equiv_flag is
2201 : set we only match stuff that is allowed inside an EQUIVALENCE
2202 : statement. sub_flag tells whether we expect a type-bound procedure found
2203 : to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2204 : components, 'ppc_arg' determines whether the PPC may be called (with an
2205 : argument list), or whether it may just be referred to as a pointer. */
2206 :
2207 : match
2208 5178679 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2209 : bool ppc_arg)
2210 : {
2211 5178679 : char name[GFC_MAX_SYMBOL_LEN + 1];
2212 5178679 : gfc_ref *substring, *tail, *tmp;
2213 5178679 : gfc_component *component = NULL;
2214 5178679 : gfc_component *previous = NULL;
2215 5178679 : gfc_symbol *sym = primary->symtree->n.sym;
2216 5178679 : gfc_expr *tgt_expr = NULL;
2217 5178679 : match m;
2218 5178679 : bool unknown;
2219 5178679 : bool inquiry;
2220 5178679 : bool intrinsic;
2221 5178679 : bool inferred_type;
2222 5178679 : locus old_loc;
2223 5178679 : char peeked_char;
2224 :
2225 5178679 : tail = NULL;
2226 :
2227 5178679 : gfc_gobble_whitespace ();
2228 :
2229 5178679 : if (gfc_peek_ascii_char () == '[')
2230 : {
2231 3266 : if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2232 3266 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2233 141 : && CLASS_DATA (sym)->attr.dimension))
2234 : {
2235 0 : gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2236 : "besides the coarray designator %<[...]%> at %C");
2237 0 : return MATCH_ERROR;
2238 : }
2239 3266 : if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2240 3265 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2241 141 : && !CLASS_DATA (sym)->attr.codimension))
2242 : {
2243 1 : gfc_error ("Coarray designator at %C but %qs is not a coarray",
2244 : sym->name);
2245 1 : return MATCH_ERROR;
2246 : }
2247 : }
2248 :
2249 5178678 : if (sym->assoc && sym->assoc->target)
2250 5178678 : tgt_expr = sym->assoc->target;
2251 :
2252 5178678 : inferred_type = IS_INFERRED_TYPE (primary);
2253 :
2254 : /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
2255 : been parsed, can generate errors with array refs.. The SELECT TYPE
2256 : namespace is marked with 'assoc_name_inferred'. During resolution, this is
2257 : detected and gfc_fixup_inferred_type_refs is called. */
2258 5177658 : if (!inferred_type
2259 5177658 : && sym->attr.select_type_temporary
2260 23980 : && sym->ns->assoc_name_inferred
2261 344 : && !sym->attr.select_rank_temporary)
2262 1364 : inferred_type = true;
2263 :
2264 : /* Try to resolve a typebound generic procedure so that the associate name
2265 : has a chance to get a type before being used in a second, nested associate
2266 : statement. Note that a copy is used for resolution so that failure does
2267 : not result in a mutilated selector expression further down the line. */
2268 8023 : if (tgt_expr && !sym->assoc->dangling
2269 8023 : && tgt_expr->ts.type == BT_UNKNOWN
2270 2115 : && tgt_expr->symtree
2271 1676 : && tgt_expr->symtree->n.sym
2272 5178751 : && gfc_expr_attr (tgt_expr).generic
2273 5178751 : && ((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_template)
2274 72 : || (sym->ts.type == BT_CLASS
2275 0 : && CLASS_DATA (sym)->ts.u.derived->attr.pdt_template)))
2276 : {
2277 1 : gfc_expr *cpy = gfc_copy_expr (tgt_expr);
2278 1 : if (gfc_resolve_expr (cpy)
2279 1 : && cpy->ts.type != BT_UNKNOWN)
2280 : {
2281 1 : gfc_replace_expr (tgt_expr, cpy);
2282 1 : sym->ts = tgt_expr->ts;
2283 : }
2284 : else
2285 0 : gfc_free_expr (cpy);
2286 1 : if (gfc_expr_attr (tgt_expr).generic)
2287 5178678 : inferred_type = true;
2288 : }
2289 :
2290 : /* For associate names, we may not yet know whether they are arrays or not.
2291 : If the selector expression is unambiguously an array; eg. a full array
2292 : or an array section, then the associate name must be an array and we can
2293 : fix it now. Otherwise, if parentheses follow and it is not a character
2294 : type, we have to assume that it actually is one for now. The final
2295 : decision will be made at resolution, of course. */
2296 5178678 : if (sym->assoc
2297 32003 : && gfc_peek_ascii_char () == '('
2298 9713 : && sym->ts.type != BT_CLASS
2299 5188188 : && !sym->attr.dimension)
2300 : {
2301 438 : gfc_ref *ref = NULL;
2302 :
2303 438 : if (!sym->assoc->dangling && tgt_expr)
2304 : {
2305 378 : if (tgt_expr->expr_type == EXPR_VARIABLE)
2306 45 : gfc_resolve_expr (tgt_expr);
2307 :
2308 378 : ref = tgt_expr->ref;
2309 392 : for (; ref; ref = ref->next)
2310 14 : if (ref->type == REF_ARRAY
2311 7 : && (ref->u.ar.type == AR_FULL
2312 7 : || ref->u.ar.type == AR_SECTION))
2313 : break;
2314 : }
2315 :
2316 438 : if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2317 264 : && sym->assoc->st
2318 264 : && sym->assoc->st->n.sym
2319 264 : && sym->assoc->st->n.sym->attr.dimension == 0))
2320 : {
2321 264 : sym->attr.dimension = 1;
2322 264 : if (sym->as == NULL
2323 264 : && sym->assoc->st
2324 264 : && sym->assoc->st->n.sym
2325 264 : && sym->assoc->st->n.sym->as)
2326 0 : sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2327 : }
2328 : }
2329 5178240 : else if (sym->ts.type == BT_CLASS
2330 45776 : && !(sym->assoc && sym->assoc->ar)
2331 45704 : && tgt_expr
2332 272 : && tgt_expr->expr_type == EXPR_VARIABLE
2333 146 : && sym->ts.u.derived != tgt_expr->ts.u.derived)
2334 : {
2335 19 : gfc_resolve_expr (tgt_expr);
2336 19 : if (tgt_expr->rank)
2337 0 : sym->ts.u.derived = tgt_expr->ts.u.derived;
2338 : }
2339 :
2340 5178678 : peeked_char = gfc_peek_ascii_char ();
2341 1364 : if ((inferred_type && !sym->as && peeked_char == '(')
2342 5178457 : || (equiv_flag && peeked_char == '(') || peeked_char == '['
2343 5173621 : || sym->attr.codimension
2344 5155466 : || (sym->attr.dimension && sym->ts.type != BT_CLASS
2345 639633 : && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2346 639618 : && !(gfc_matching_procptr_assignment
2347 38 : && sym->attr.flavor == FL_PROCEDURE))
2348 9694552 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2349 45587 : && sym->ts.u.derived && CLASS_DATA (sym)
2350 45583 : && (CLASS_DATA (sym)->attr.dimension
2351 27555 : || CLASS_DATA (sym)->attr.codimension)))
2352 : {
2353 681399 : gfc_array_spec *as;
2354 21279 : bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2355 692308 : && sym->ts.type == BT_CHARACTER;
2356 681399 : gfc_ref *ref, *strarr = NULL;
2357 :
2358 681399 : tail = extend_ref (primary, tail);
2359 681399 : if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
2360 : {
2361 3 : gcc_assert (sym->attr.dimension);
2362 : /* Find array reference for substrings of character arrays. */
2363 3 : for (ref = primary->ref; ref && ref->next; ref = ref->next)
2364 3 : if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
2365 : {
2366 : strarr = ref;
2367 : break;
2368 : }
2369 : }
2370 : else
2371 681396 : tail->type = REF_ARRAY;
2372 :
2373 : /* In EQUIVALENCE, we don't know yet whether we are seeing
2374 : an array, character variable or array of character
2375 : variables. We'll leave the decision till resolve time. */
2376 :
2377 681399 : if (equiv_flag)
2378 : as = NULL;
2379 679398 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2380 18737 : as = CLASS_DATA (sym)->as;
2381 : else
2382 660661 : as = sym->as;
2383 :
2384 681399 : ref = strarr ? strarr : tail;
2385 681399 : m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
2386 : coarray_only);
2387 681399 : if (m != MATCH_YES)
2388 : return m;
2389 :
2390 681307 : gfc_gobble_whitespace ();
2391 681307 : if (coarray_only)
2392 : {
2393 2011 : primary->ts = sym->ts;
2394 2011 : goto check_substring;
2395 : }
2396 :
2397 679296 : if (equiv_flag && gfc_peek_ascii_char () == '(')
2398 : {
2399 74 : tail = extend_ref (primary, tail);
2400 74 : tail->type = REF_ARRAY;
2401 :
2402 74 : m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2403 74 : if (m != MATCH_YES)
2404 : return m;
2405 : }
2406 : }
2407 :
2408 5176575 : primary->ts = sym->ts;
2409 :
2410 5176575 : if (equiv_flag)
2411 : return MATCH_YES;
2412 :
2413 : /* With DEC extensions, member separator may be '.' or '%'. */
2414 5173629 : peeked_char = gfc_peek_ascii_char ();
2415 5173629 : m = gfc_match_member_sep (sym);
2416 5173629 : if (m == MATCH_ERROR)
2417 : return MATCH_ERROR;
2418 :
2419 5173628 : inquiry = false;
2420 5173628 : if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2421 140183 : && (primary->ts.type != BT_DERIVED || inferred_type))
2422 : {
2423 2705 : match mm;
2424 2705 : old_loc = gfc_current_locus;
2425 2705 : mm = gfc_match_name (name);
2426 :
2427 : /* Check to see if this has a default complex. */
2428 523 : if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2429 2724 : && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2430 : {
2431 7 : gfc_set_default_type (sym, 0, sym->ns);
2432 7 : primary->ts = sym->ts;
2433 : }
2434 :
2435 : /* This is a usable inquiry reference, if the symbol is already known
2436 : to have a type or no derived types with a component of this name
2437 : can be found. If this was an inquiry reference with the same name
2438 : as a derived component and the associate-name type is not derived
2439 : or class, this is fixed up in 'gfc_fixup_inferred_type_refs'. */
2440 2705 : if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2441 4688 : && !(sym->ts.type == BT_UNKNOWN
2442 234 : && gfc_find_derived_types (sym, gfc_current_ns, name)))
2443 : inquiry = true;
2444 2705 : gfc_current_locus = old_loc;
2445 : }
2446 :
2447 : /* Use the default type if there is one. */
2448 2744776 : if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2449 5174144 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2450 0 : gfc_set_default_type (sym, 0, sym->ns);
2451 :
2452 : /* See if the type can be determined by resolution of the selector expression,
2453 : if allowable now, or inferred from references. */
2454 5173628 : if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2455 2745846 : && m == MATCH_YES)
2456 : {
2457 1375 : bool sym_present, resolved = false;
2458 1375 : gfc_symbol *tgt_sym;
2459 :
2460 1375 : sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2461 995 : tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
2462 :
2463 : /* These target expressions can be resolved at any time:
2464 : (i) With a declared symbol or intrinsic function; or
2465 : (ii) An operator expression,
2466 : just as long as (iii) all the functions in the expression have been
2467 : declared or are intrinsic. */
2468 995 : if (((sym_present // (i)
2469 995 : && (tgt_sym->attr.use_assoc
2470 995 : || tgt_sym->attr.host_assoc
2471 995 : || tgt_sym->attr.if_source == IFSRC_DECL
2472 995 : || tgt_sym->attr.proc == PROC_INTRINSIC
2473 995 : || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2474 1363 : || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2475 24 : && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2476 18 : && gfc_resolve_expr (tgt_expr))
2477 : {
2478 18 : sym->ts = tgt_expr->ts;
2479 18 : primary->ts = sym->ts;
2480 18 : resolved = true;
2481 : }
2482 :
2483 : /* If this hasn't done the trick and the target expression is a function,
2484 : or an unresolved operator expression, then this must be a derived type
2485 : if 'name' matches an accessible type both in this namespace and in the
2486 : as yet unparsed contained function. In principle, the type could have
2487 : already been inferred to be complex and yet a derived type with a
2488 : component name 're' or 'im' could be found. */
2489 18 : if (tgt_expr
2490 1019 : && (tgt_expr->expr_type == EXPR_FUNCTION
2491 85 : || tgt_expr->expr_type == EXPR_ARRAY
2492 73 : || (!resolved && tgt_expr->expr_type == EXPR_OP))
2493 952 : && (sym->ts.type == BT_UNKNOWN
2494 467 : || (inferred_type && sym->ts.type != BT_COMPLEX))
2495 2189 : && gfc_find_derived_types (sym, gfc_current_ns, name, true))
2496 : {
2497 616 : sym->assoc->inferred_type = 1;
2498 : /* The first returned type is as good as any at this stage. The final
2499 : determination is made in 'gfc_fixup_inferred_type_refs'*/
2500 616 : gfc_symbol **dts = &sym->assoc->derived_types;
2501 616 : tgt_expr->ts.type = BT_DERIVED;
2502 616 : tgt_expr->ts.kind = 0;
2503 616 : tgt_expr->ts.u.derived = *dts;
2504 616 : sym->ts = tgt_expr->ts;
2505 616 : primary->ts = sym->ts;
2506 : /* Delete the dt list even if this process has to be done again for
2507 : another primary expression. */
2508 1254 : while (*dts && (*dts)->dt_next)
2509 : {
2510 638 : gfc_symbol **tmp = &(*dts)->dt_next;
2511 638 : *dts = NULL;
2512 638 : dts = tmp;
2513 : }
2514 : }
2515 : /* If there is a usable inquiry reference not there are no matching
2516 : derived types, force the inquiry reference by setting unknown the
2517 : type of the primary expression. */
2518 354 : else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2519 807 : && !gfc_find_derived_types (sym, gfc_current_ns, name))
2520 48 : primary->ts.type = BT_UNKNOWN;
2521 :
2522 : /* Otherwise try resolving a copy of a component call. If it succeeds,
2523 : use that for the selector expression. */
2524 711 : else if (tgt_expr && tgt_expr->expr_type == EXPR_COMPCALL)
2525 : {
2526 1 : gfc_expr *cpy = gfc_copy_expr (tgt_expr);
2527 1 : if (gfc_resolve_expr (cpy))
2528 : {
2529 1 : gfc_replace_expr (tgt_expr, cpy);
2530 1 : sym->ts = tgt_expr->ts;
2531 : }
2532 : else
2533 0 : gfc_free_expr (cpy);
2534 : }
2535 :
2536 : /* An inquiry reference might determine the type, otherwise we have an
2537 : error. */
2538 1375 : if (sym->ts.type == BT_UNKNOWN && !inquiry)
2539 : {
2540 12 : gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2541 12 : return MATCH_ERROR;
2542 : }
2543 : }
2544 5172253 : else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2545 4931519 : && m == MATCH_YES && !inquiry)
2546 : {
2547 7 : gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2548 : peeked_char, sym->name);
2549 7 : return MATCH_ERROR;
2550 : }
2551 :
2552 5173609 : if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2553 243420 : || m != MATCH_YES)
2554 5013412 : goto check_substring;
2555 :
2556 160197 : if (!inquiry)
2557 158520 : sym = sym->ts.u.derived;
2558 : else
2559 160197 : sym = NULL;
2560 :
2561 185460 : for (;;)
2562 : {
2563 185460 : bool t;
2564 185460 : gfc_symtree *tbp;
2565 185460 : gfc_typespec *ts = &primary->ts;
2566 :
2567 185460 : m = gfc_match_name (name);
2568 185460 : if (m == MATCH_NO)
2569 0 : gfc_error ("Expected structure component name at %C");
2570 185460 : if (m != MATCH_YES)
2571 135 : return MATCH_ERROR;
2572 :
2573 : /* For derived type components find typespec of ultimate component. */
2574 185460 : if (ts->type == BT_DERIVED && primary->ref)
2575 : {
2576 153369 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2577 : {
2578 88070 : if (ref->type == REF_COMPONENT && ref->u.c.component)
2579 25376 : ts = &ref->u.c.component->ts;
2580 : }
2581 : }
2582 :
2583 185460 : intrinsic = false;
2584 185460 : if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
2585 : {
2586 2204 : inquiry = is_inquiry_ref (name, &tmp);
2587 2204 : if (inquiry)
2588 2199 : sym = NULL;
2589 :
2590 2204 : if (peeked_char == '%')
2591 : {
2592 2204 : if (tmp)
2593 : {
2594 2199 : gfc_symbol *s;
2595 2199 : switch (tmp->u.i)
2596 : {
2597 1338 : case INQUIRY_RE:
2598 1338 : case INQUIRY_IM:
2599 1338 : if (!gfc_notify_std (GFC_STD_F2008,
2600 : "RE or IM part_ref at %C"))
2601 : return MATCH_ERROR;
2602 : break;
2603 :
2604 414 : case INQUIRY_KIND:
2605 414 : if (!gfc_notify_std (GFC_STD_F2003,
2606 : "KIND part_ref at %C"))
2607 : return MATCH_ERROR;
2608 : break;
2609 :
2610 447 : case INQUIRY_LEN:
2611 447 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2612 : return MATCH_ERROR;
2613 : break;
2614 : }
2615 :
2616 : /* If necessary, infer the type of the primary expression
2617 : and the associate-name using the the inquiry ref.. */
2618 2190 : s = primary->symtree ? primary->symtree->n.sym : NULL;
2619 2162 : if (s && s->assoc && s->assoc->target
2620 558 : && (s->ts.type == BT_UNKNOWN
2621 414 : || (primary->ts.type == BT_UNKNOWN
2622 48 : && s->assoc->inferred_type
2623 48 : && s->ts.type == BT_DERIVED)))
2624 : {
2625 192 : if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2626 : {
2627 96 : s->ts.type = BT_COMPLEX;
2628 96 : s->ts.kind = gfc_default_real_kind;;
2629 96 : s->assoc->inferred_type = 1;
2630 96 : primary->ts = s->ts;
2631 : }
2632 96 : else if (tmp->u.i == INQUIRY_LEN)
2633 : {
2634 48 : s->ts.type = BT_CHARACTER;
2635 48 : s->ts.kind = gfc_default_character_kind;;
2636 48 : s->assoc->inferred_type = 1;
2637 48 : primary->ts = s->ts;
2638 : }
2639 48 : else if (s->ts.type == BT_UNKNOWN)
2640 : {
2641 : /* KIND inquiry gives no clue as to symbol type. */
2642 48 : primary->ref = tmp;
2643 48 : primary->ts.type = BT_INTEGER;
2644 48 : primary->ts.kind = gfc_default_integer_kind;
2645 48 : return MATCH_YES;
2646 : }
2647 : }
2648 :
2649 2142 : if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2650 1334 : && primary->ts.type != BT_COMPLEX)
2651 : {
2652 12 : gfc_error ("The RE or IM part_ref at %C must be "
2653 : "applied to a COMPLEX expression");
2654 12 : return MATCH_ERROR;
2655 : }
2656 2130 : else if (tmp->u.i == INQUIRY_LEN
2657 445 : && ts->type != BT_CHARACTER)
2658 : {
2659 5 : gfc_error ("The LEN part_ref at %C must be applied "
2660 : "to a CHARACTER expression");
2661 5 : return MATCH_ERROR;
2662 : }
2663 : }
2664 2130 : if (primary->ts.type != BT_UNKNOWN)
2665 185386 : intrinsic = true;
2666 : }
2667 : }
2668 : else
2669 : inquiry = false;
2670 :
2671 185386 : if (sym && sym->f2k_derived)
2672 180457 : tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2673 : else
2674 : tbp = NULL;
2675 :
2676 180457 : if (tbp)
2677 : {
2678 4142 : gfc_symbol* tbp_sym;
2679 :
2680 4142 : if (!t)
2681 : return MATCH_ERROR;
2682 :
2683 4140 : gcc_assert (!tail || !tail->next);
2684 :
2685 4140 : if (!(primary->expr_type == EXPR_VARIABLE
2686 : || (primary->expr_type == EXPR_STRUCTURE
2687 1 : && primary->symtree && primary->symtree->n.sym
2688 1 : && primary->symtree->n.sym->attr.flavor)))
2689 : return MATCH_ERROR;
2690 :
2691 4138 : if (tbp->n.tb->is_generic)
2692 : tbp_sym = NULL;
2693 : else
2694 3302 : tbp_sym = tbp->n.tb->u.specific->n.sym;
2695 :
2696 4138 : primary->expr_type = EXPR_COMPCALL;
2697 4138 : primary->value.compcall.tbp = tbp->n.tb;
2698 4138 : primary->value.compcall.name = tbp->name;
2699 4138 : primary->value.compcall.ignore_pass = 0;
2700 4138 : primary->value.compcall.assign = 0;
2701 4138 : primary->value.compcall.base_object = NULL;
2702 4138 : gcc_assert (primary->symtree->n.sym->attr.referenced);
2703 4138 : if (tbp_sym)
2704 3302 : primary->ts = tbp_sym->ts;
2705 : else
2706 836 : gfc_clear_ts (&primary->ts);
2707 :
2708 4138 : m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2709 : &primary->value.compcall.actual);
2710 4138 : if (m == MATCH_ERROR)
2711 : return MATCH_ERROR;
2712 4138 : if (m == MATCH_NO)
2713 : {
2714 180 : if (sub_flag)
2715 179 : primary->value.compcall.actual = NULL;
2716 : else
2717 : {
2718 : /* Before erroring, check whether there is also a data
2719 : component with this name. Use noaccess=true so
2720 : that private components are also found. */
2721 1 : if (sym && gfc_find_component (sym, name, true, true, NULL))
2722 : {
2723 : /* Restore expr to EXPR_VARIABLE and let the data
2724 : component path below handle it. */
2725 0 : primary->expr_type = EXPR_VARIABLE;
2726 0 : gfc_free_actual_arglist (primary->value.compcall.actual);
2727 0 : primary->value.compcall.actual = NULL;
2728 0 : tbp = NULL;
2729 0 : goto try_data_component;
2730 : }
2731 1 : gfc_error ("Expected argument list at %C");
2732 1 : return MATCH_ERROR;
2733 : }
2734 : }
2735 :
2736 160062 : break;
2737 : }
2738 :
2739 176315 : try_data_component:
2740 :
2741 181244 : previous = component;
2742 :
2743 181244 : if (!inquiry && !intrinsic)
2744 : {
2745 179116 : component = gfc_find_component (sym, name, false, false, &tmp);
2746 : /* For inferred-type ASSOCIATE names the parse-time candidate type
2747 : may not be the final type; a private component in the candidate
2748 : type may correspond to a public component in the correct type.
2749 : Accept it tentatively so that resolution can fix up the type. */
2750 179116 : if (!component && !tbp
2751 47 : && primary->symtree && primary->symtree->n.sym->assoc
2752 0 : && primary->symtree->n.sym->assoc->inferred_type)
2753 0 : component = gfc_find_component (sym, name, true, false, &tmp);
2754 : }
2755 : else
2756 : component = NULL;
2757 :
2758 181244 : if (previous && inquiry
2759 463 : && (previous->attr.pdt_kind || previous->attr.pdt_len))
2760 : {
2761 4 : gfc_error_now ("R901: A type parameter ref is not a designator and "
2762 : "cannot be followed by the type inquiry ref at %C");
2763 4 : return MATCH_ERROR;
2764 : }
2765 :
2766 181240 : if (intrinsic && !inquiry)
2767 : {
2768 3 : if (previous)
2769 2 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2770 : "type component %qs", name, previous->name);
2771 : else
2772 1 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2773 : "type component", name);
2774 : return MATCH_ERROR;
2775 : }
2776 181237 : else if (component == NULL && !inquiry)
2777 : return MATCH_ERROR;
2778 :
2779 : /* Extend the reference chain determined by gfc_find_component or
2780 : is_inquiry_ref. */
2781 181190 : if (primary->ref == NULL)
2782 108386 : primary->ref = tmp;
2783 : else
2784 : {
2785 : /* Find end of reference chain if inquiry reference and tail not
2786 : set. */
2787 72804 : if (tail == NULL && inquiry && tmp)
2788 35 : tail = extend_ref (primary, tail);
2789 :
2790 : /* Set by the for loop below for the last component ref. */
2791 72804 : gcc_assert (tail != NULL);
2792 72804 : tail->next = tmp;
2793 : }
2794 :
2795 : /* The reference chain may be longer than one hop for union
2796 : subcomponents; find the new tail. */
2797 183166 : for (tail = tmp; tail->next; tail = tail->next)
2798 : ;
2799 :
2800 181190 : if (tmp && tmp->type == REF_INQUIRY)
2801 : {
2802 2121 : if (!primary->where.u.lb || !primary->where.nextc)
2803 1937 : primary->where = gfc_current_locus;
2804 2121 : gfc_simplify_expr (primary, 0);
2805 :
2806 2121 : if (primary->expr_type == EXPR_CONSTANT)
2807 510 : goto check_done;
2808 :
2809 1611 : if (primary->ref == NULL)
2810 60 : goto check_done;
2811 :
2812 1551 : switch (tmp->u.i)
2813 : {
2814 1178 : case INQUIRY_RE:
2815 1178 : case INQUIRY_IM:
2816 1178 : if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2817 : return MATCH_ERROR;
2818 :
2819 1178 : if (primary->ts.type != BT_COMPLEX)
2820 : {
2821 0 : gfc_error ("The RE or IM part_ref at %C must be "
2822 : "applied to a COMPLEX expression");
2823 0 : return MATCH_ERROR;
2824 : }
2825 1178 : primary->ts.type = BT_REAL;
2826 1178 : break;
2827 :
2828 321 : case INQUIRY_LEN:
2829 321 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2830 : return MATCH_ERROR;
2831 :
2832 321 : if (primary->ts.type != BT_CHARACTER)
2833 : {
2834 0 : gfc_error ("The LEN part_ref at %C must be applied "
2835 : "to a CHARACTER expression");
2836 0 : return MATCH_ERROR;
2837 : }
2838 321 : primary->ts.u.cl = NULL;
2839 321 : primary->ts.deferred = false;
2840 321 : primary->ts.type = BT_INTEGER;
2841 321 : primary->ts.kind = gfc_default_integer_kind;
2842 321 : break;
2843 :
2844 52 : case INQUIRY_KIND:
2845 52 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2846 : return MATCH_ERROR;
2847 :
2848 52 : if (primary->ts.type == BT_CLASS
2849 52 : || primary->ts.type == BT_DERIVED)
2850 : {
2851 0 : gfc_error ("The KIND part_ref at %C must be applied "
2852 : "to an expression of intrinsic type");
2853 0 : return MATCH_ERROR;
2854 : }
2855 52 : if (primary->ts.type == BT_CHARACTER)
2856 : {
2857 0 : primary->ts.u.cl = NULL;
2858 0 : primary->ts.deferred = false;
2859 : }
2860 52 : primary->ts.type = BT_INTEGER;
2861 52 : primary->ts.kind = gfc_default_integer_kind;
2862 52 : break;
2863 :
2864 0 : default:
2865 0 : gcc_unreachable ();
2866 : }
2867 :
2868 1551 : goto check_done;
2869 : }
2870 :
2871 179069 : primary->ts = component->ts;
2872 :
2873 179069 : if (component->attr.proc_pointer && ppc_arg)
2874 : {
2875 : /* Procedure pointer component call: Look for argument list. */
2876 1155 : m = gfc_match_actual_arglist (sub_flag,
2877 : &primary->value.compcall.actual);
2878 1155 : if (m == MATCH_ERROR)
2879 : return MATCH_ERROR;
2880 :
2881 1155 : if (m == MATCH_NO && !gfc_matching_ptr_assignment
2882 272 : && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2883 : {
2884 2 : gfc_error ("Procedure pointer component %qs requires an "
2885 : "argument list at %C", component->name);
2886 2 : return MATCH_ERROR;
2887 : }
2888 :
2889 1153 : if (m == MATCH_YES)
2890 882 : primary->expr_type = EXPR_PPC;
2891 :
2892 : break;
2893 : }
2894 :
2895 177914 : if (component->as != NULL && !component->attr.proc_pointer)
2896 : {
2897 66279 : tail = extend_ref (primary, tail);
2898 66279 : tail->type = REF_ARRAY;
2899 :
2900 132558 : m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2901 66279 : component->as->corank);
2902 66279 : if (m != MATCH_YES)
2903 : return m;
2904 : }
2905 111635 : else if (component->ts.type == BT_CLASS && component->attr.class_ok
2906 10898 : && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2907 : {
2908 5183 : tail = extend_ref (primary, tail);
2909 5183 : tail->type = REF_ARRAY;
2910 :
2911 10366 : m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2912 : equiv_flag,
2913 5183 : CLASS_DATA (component)->as->corank);
2914 5183 : if (m != MATCH_YES)
2915 : return m;
2916 : }
2917 :
2918 106452 : check_done:
2919 : /* In principle, we could have eg. expr%re%kind so we must allow for
2920 : this possibility. */
2921 180035 : if (gfc_match_char ('%') == MATCH_YES)
2922 : {
2923 24893 : if (component && (component->ts.type == BT_DERIVED
2924 3394 : || component->ts.type == BT_CLASS))
2925 24370 : sym = component->ts.u.derived;
2926 24893 : continue;
2927 : }
2928 155142 : else if (inquiry)
2929 : break;
2930 :
2931 143023 : if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2932 161048 : || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2933 : break;
2934 :
2935 370 : if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2936 370 : sym = component->ts.u.derived;
2937 : }
2938 :
2939 5175485 : check_substring:
2940 5175485 : unknown = false;
2941 5175485 : if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2942 : {
2943 2744260 : if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2944 : {
2945 352 : gfc_set_default_type (sym, 0, sym->ns);
2946 352 : primary->ts = sym->ts;
2947 352 : unknown = true;
2948 : }
2949 : }
2950 :
2951 5175485 : if (primary->ts.type == BT_CHARACTER)
2952 : {
2953 312482 : bool def = primary->ts.deferred == 1;
2954 312482 : switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2955 : {
2956 15266 : case MATCH_YES:
2957 15266 : if (tail == NULL)
2958 10059 : primary->ref = substring;
2959 : else
2960 5207 : tail->next = substring;
2961 :
2962 15266 : if (primary->expr_type == EXPR_CONSTANT)
2963 755 : primary->expr_type = EXPR_SUBSTRING;
2964 :
2965 15266 : if (substring)
2966 15086 : primary->ts.u.cl = NULL;
2967 :
2968 15266 : gfc_gobble_whitespace ();
2969 15266 : if (gfc_peek_ascii_char () == '(')
2970 : {
2971 5 : gfc_error_now ("Unexpected array/substring ref at %C");
2972 5 : return MATCH_ERROR;
2973 : }
2974 : break;
2975 :
2976 297216 : case MATCH_NO:
2977 297216 : if (unknown)
2978 : {
2979 351 : gfc_clear_ts (&primary->ts);
2980 351 : gfc_clear_ts (&sym->ts);
2981 : }
2982 : break;
2983 :
2984 : case MATCH_ERROR:
2985 : return MATCH_ERROR;
2986 : }
2987 : }
2988 :
2989 : /* F08:C611. */
2990 5175480 : if (primary->ts.type == BT_DERIVED && primary->ref
2991 29745 : && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2992 : {
2993 6 : gfc_error ("Nonpolymorphic reference to abstract type at %C");
2994 6 : return MATCH_ERROR;
2995 : }
2996 :
2997 : /* F08:C727. */
2998 5175474 : if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2999 : {
3000 3 : gfc_error ("Coindexed procedure-pointer component at %C");
3001 3 : return MATCH_ERROR;
3002 : }
3003 :
3004 : return MATCH_YES;
3005 : }
3006 :
3007 :
3008 : /* Given an expression that is a variable, figure out what the
3009 : ultimate variable's attribute is, traversing the reference
3010 : structures if necessary.
3011 :
3012 : This subroutine is trickier than it looks. We start at the base
3013 : symbol and store the attribute. Component references load a
3014 : completely new attribute.
3015 :
3016 : A couple of rules come into play. Subobjects of targets are always
3017 : targets themselves. If we see a component that goes through a
3018 : pointer, then the expression must also be a target, since the
3019 : pointer is associated with something (if it isn't core will soon be
3020 : dumped). If we see a full part or section of an array, the
3021 : expression is also an array.
3022 :
3023 : We can have at most one full array reference. */
3024 :
3025 : symbol_attribute
3026 4119211 : gfc_variable_attr (gfc_expr *expr)
3027 : {
3028 4119211 : int dimension, codimension, pointer, allocatable, target, optional;
3029 4119211 : symbol_attribute attr;
3030 4119211 : gfc_ref *ref;
3031 4119211 : gfc_symbol *sym;
3032 4119211 : gfc_component *comp;
3033 4119211 : gfc_typespec *current_ts;
3034 :
3035 4119211 : if (expr->expr_type != EXPR_VARIABLE
3036 68043 : && expr->expr_type != EXPR_FUNCTION
3037 9 : && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
3038 0 : gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
3039 :
3040 4119211 : sym = expr->symtree->n.sym;
3041 4119211 : attr = sym->attr;
3042 4119211 : current_ts = &sym->ts;
3043 :
3044 : /* If we are in the body of a function, the function name references the
3045 : function result, not the function itself. */
3046 4119211 : if (attr.function
3047 153996 : && sym->result == sym
3048 121308 : && gfc_current_ns
3049 121308 : && gfc_current_ns->proc_name == sym)
3050 : {
3051 44787 : attr.function = 0;
3052 44787 : attr.elemental = 0;
3053 44787 : attr.pure = 0;
3054 44787 : attr.recursive = 0;
3055 44787 : attr.flavor = FL_VARIABLE;
3056 44787 : attr.result = 1;
3057 : }
3058 :
3059 4119211 : optional = attr.optional;
3060 4119211 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
3061 : {
3062 133632 : dimension = CLASS_DATA (sym)->attr.dimension;
3063 133632 : codimension = CLASS_DATA (sym)->attr.codimension;
3064 133632 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3065 133632 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3066 : }
3067 : else
3068 : {
3069 3985579 : dimension = attr.dimension;
3070 3985579 : codimension = attr.codimension;
3071 3985579 : pointer = attr.pointer;
3072 3985579 : allocatable = attr.allocatable;
3073 : }
3074 :
3075 4119211 : target = attr.target;
3076 4119211 : if (pointer || attr.proc_pointer)
3077 198828 : target = 1;
3078 :
3079 : /* F2018:11.1.3.3: Other attributes of associate names
3080 : "The associating entity does not have the ALLOCATABLE or POINTER
3081 : attributes; it has the TARGET attribute if and only if the selector is
3082 : a variable and has either the TARGET or POINTER attribute." */
3083 4119211 : if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
3084 : {
3085 24559 : if (sym->assoc->target->expr_type == EXPR_VARIABLE)
3086 : {
3087 22300 : symbol_attribute tgt_attr;
3088 22300 : tgt_attr = gfc_expr_attr (sym->assoc->target);
3089 28458 : target = (tgt_attr.pointer || tgt_attr.target);
3090 : }
3091 : else
3092 : target = 0;
3093 : }
3094 :
3095 5644357 : for (ref = expr->ref; ref; ref = ref->next)
3096 1526752 : if (ref->type == REF_SUBSTRING)
3097 : optional = false;
3098 1514278 : else if (ref->type == REF_INQUIRY)
3099 : {
3100 : optional = false;
3101 : break;
3102 : }
3103 :
3104 5645963 : for (ref = expr->ref; ref; ref = ref->next)
3105 1526752 : switch (ref->type)
3106 : {
3107 1185108 : case REF_ARRAY:
3108 :
3109 1185108 : switch (ref->u.ar.type)
3110 : {
3111 : case AR_FULL:
3112 1526752 : dimension = 1;
3113 : break;
3114 :
3115 99966 : case AR_SECTION:
3116 99966 : allocatable = pointer = 0;
3117 99966 : dimension = 1;
3118 99966 : optional = false;
3119 99966 : break;
3120 :
3121 225211 : case AR_ELEMENT:
3122 : /* Handle coarrays. */
3123 225211 : if (ref->u.ar.dimen > 0)
3124 1526752 : allocatable = pointer = optional = false;
3125 : break;
3126 :
3127 : case AR_UNKNOWN:
3128 : /* For standard conforming code, AR_UNKNOWN should not happen.
3129 : For nonconforming code, gfortran can end up here. Treat it
3130 : as a no-op. */
3131 : break;
3132 : }
3133 :
3134 : break;
3135 :
3136 327564 : case REF_COMPONENT:
3137 327564 : comp = ref->u.c.component;
3138 327564 : if (!(current_ts->type == BT_CLASS
3139 64579 : && current_ts->u.derived->attr.is_class
3140 64565 : && strcmp (comp->name, "_data") == 0))
3141 : {
3142 293427 : optional = false;
3143 293427 : attr = comp->attr;
3144 : }
3145 327564 : current_ts = &sym->ts;
3146 :
3147 327564 : if (comp->ts.type == BT_CLASS)
3148 : {
3149 23858 : dimension = CLASS_DATA (comp)->attr.dimension;
3150 23858 : codimension = CLASS_DATA (comp)->attr.codimension;
3151 23858 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3152 23858 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3153 : }
3154 : else
3155 : {
3156 303706 : dimension = comp->attr.dimension;
3157 303706 : codimension = comp->attr.codimension;
3158 303706 : if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3159 18075 : pointer = comp->attr.class_pointer;
3160 : else
3161 285631 : pointer = comp->attr.pointer;
3162 303706 : allocatable = comp->attr.allocatable;
3163 : }
3164 327564 : if (pointer || attr.proc_pointer)
3165 57957 : target = 1;
3166 :
3167 : break;
3168 :
3169 14080 : case REF_INQUIRY:
3170 14080 : case REF_SUBSTRING:
3171 14080 : allocatable = pointer = optional = false;
3172 14080 : break;
3173 : }
3174 :
3175 4119211 : attr.dimension = dimension;
3176 4119211 : attr.codimension = codimension;
3177 4119211 : attr.pointer = pointer;
3178 4119211 : attr.allocatable = allocatable;
3179 4119211 : attr.target = target;
3180 4119211 : attr.save = sym->attr.save;
3181 4119211 : attr.optional = optional;
3182 :
3183 4119211 : return attr;
3184 : }
3185 :
3186 :
3187 : /* Return the attribute from a general expression. */
3188 :
3189 : symbol_attribute
3190 5099739 : gfc_expr_attr (gfc_expr *e)
3191 : {
3192 5099739 : symbol_attribute attr;
3193 :
3194 5099739 : switch (e->expr_type)
3195 : {
3196 4042180 : case EXPR_VARIABLE:
3197 4042180 : attr = gfc_variable_attr (e);
3198 4042180 : break;
3199 :
3200 94545 : case EXPR_FUNCTION:
3201 94545 : gfc_clear_attr (&attr);
3202 :
3203 94545 : if (e->value.function.esym && e->value.function.esym->result)
3204 : {
3205 26175 : gfc_symbol *sym = e->value.function.esym->result;
3206 26175 : attr = sym->attr;
3207 26175 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3208 : {
3209 2242 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3210 2242 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3211 2242 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3212 : }
3213 : }
3214 68370 : else if (e->value.function.isym
3215 65329 : && e->value.function.isym->transformational
3216 33729 : && e->ts.type == BT_CLASS)
3217 330 : attr = CLASS_DATA (e)->attr;
3218 68040 : else if (e->symtree)
3219 68034 : attr = gfc_variable_attr (e);
3220 :
3221 : /* TODO: NULL() returns pointers. May have to take care of this
3222 : here. */
3223 :
3224 : break;
3225 :
3226 963014 : default:
3227 963014 : gfc_clear_attr (&attr);
3228 963014 : break;
3229 : }
3230 :
3231 5099739 : return attr;
3232 : }
3233 :
3234 :
3235 : /* Given an expression, figure out what the ultimate expression
3236 : attribute is. This routine is similar to gfc_variable_attr with
3237 : parts of gfc_expr_attr, but focuses more on the needs of
3238 : coarrays. For coarrays a codimension attribute is kind of
3239 : "infectious" being propagated once set and never cleared.
3240 : The coarray_comp is only set, when the expression refs a coarray
3241 : component. REFS_COMP is set when present to true only, when this EXPR
3242 : refs a (non-_data) component. To check whether EXPR refs an allocatable
3243 : component in a derived type coarray *refs_comp needs to be set and
3244 : coarray_comp has to false. */
3245 :
3246 : static symbol_attribute
3247 16674 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3248 : {
3249 16674 : int dimension, codimension, pointer, allocatable, target, coarray_comp;
3250 16674 : symbol_attribute attr;
3251 16674 : gfc_ref *ref;
3252 16674 : gfc_symbol *sym;
3253 16674 : gfc_component *comp;
3254 :
3255 16674 : if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3256 0 : gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3257 :
3258 16674 : sym = expr->symtree->n.sym;
3259 16674 : gfc_clear_attr (&attr);
3260 :
3261 16674 : if (refs_comp)
3262 11352 : *refs_comp = false;
3263 :
3264 16674 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3265 : {
3266 430 : dimension = CLASS_DATA (sym)->attr.dimension;
3267 430 : codimension = CLASS_DATA (sym)->attr.codimension;
3268 430 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3269 430 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3270 430 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3271 430 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3272 : }
3273 : else
3274 : {
3275 16244 : dimension = sym->attr.dimension;
3276 16244 : codimension = sym->attr.codimension;
3277 16244 : pointer = sym->attr.pointer;
3278 16244 : allocatable = sym->attr.allocatable;
3279 32488 : attr.alloc_comp = sym->ts.type == BT_DERIVED
3280 16244 : ? sym->ts.u.derived->attr.alloc_comp : 0;
3281 16244 : attr.pointer_comp = sym->ts.type == BT_DERIVED
3282 16244 : ? sym->ts.u.derived->attr.pointer_comp : 0;
3283 : }
3284 :
3285 16674 : target = coarray_comp = 0;
3286 16674 : if (pointer || attr.proc_pointer)
3287 638 : target = 1;
3288 :
3289 29115 : for (ref = expr->ref; ref; ref = ref->next)
3290 12441 : switch (ref->type)
3291 : {
3292 8650 : case REF_ARRAY:
3293 :
3294 8650 : switch (ref->u.ar.type)
3295 : {
3296 : case AR_FULL:
3297 : case AR_SECTION:
3298 : dimension = 1;
3299 8650 : break;
3300 :
3301 4118 : case AR_ELEMENT:
3302 : /* Handle coarrays. */
3303 4118 : if (ref->u.ar.dimen > 0 && !in_allocate)
3304 8650 : allocatable = pointer = 0;
3305 : break;
3306 :
3307 0 : case AR_UNKNOWN:
3308 : /* If any of start, end or stride is not integer, there will
3309 : already have been an error issued. */
3310 0 : int errors;
3311 0 : gfc_get_errors (NULL, &errors);
3312 0 : if (errors == 0)
3313 0 : gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3314 : }
3315 :
3316 : break;
3317 :
3318 3789 : case REF_COMPONENT:
3319 3789 : comp = ref->u.c.component;
3320 :
3321 3789 : if (comp->ts.type == BT_CLASS)
3322 : {
3323 : /* Set coarray_comp only, when this component introduces the
3324 : coarray. */
3325 13 : coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3326 13 : codimension |= CLASS_DATA (comp)->attr.codimension;
3327 13 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3328 13 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3329 : }
3330 : else
3331 : {
3332 : /* Set coarray_comp only, when this component introduces the
3333 : coarray. */
3334 3776 : coarray_comp = !codimension && comp->attr.codimension;
3335 3776 : codimension |= comp->attr.codimension;
3336 3776 : pointer = comp->attr.pointer;
3337 3776 : allocatable = comp->attr.allocatable;
3338 : }
3339 :
3340 3789 : if (refs_comp && strcmp (comp->name, "_data") != 0
3341 2239 : && (ref->next == NULL
3342 1710 : || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3343 1670 : *refs_comp = true;
3344 :
3345 3789 : if (pointer || attr.proc_pointer)
3346 690 : target = 1;
3347 :
3348 : break;
3349 :
3350 : case REF_SUBSTRING:
3351 : case REF_INQUIRY:
3352 12441 : allocatable = pointer = 0;
3353 : break;
3354 : }
3355 :
3356 16674 : attr.dimension = dimension;
3357 16674 : attr.codimension = codimension;
3358 16674 : attr.pointer = pointer;
3359 16674 : attr.allocatable = allocatable;
3360 16674 : attr.target = target;
3361 16674 : attr.save = sym->attr.save;
3362 16674 : attr.coarray_comp = coarray_comp;
3363 :
3364 16674 : return attr;
3365 : }
3366 :
3367 :
3368 : symbol_attribute
3369 20734 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3370 : {
3371 20734 : symbol_attribute attr;
3372 :
3373 20734 : switch (e->expr_type)
3374 : {
3375 14963 : case EXPR_VARIABLE:
3376 14963 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3377 14963 : break;
3378 :
3379 1717 : case EXPR_FUNCTION:
3380 1717 : gfc_clear_attr (&attr);
3381 :
3382 1717 : if (e->value.function.esym && e->value.function.esym->result)
3383 : {
3384 6 : gfc_symbol *sym = e->value.function.esym->result;
3385 6 : attr = sym->attr;
3386 6 : if (sym->ts.type == BT_CLASS)
3387 : {
3388 0 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3389 0 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3390 0 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3391 0 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3392 0 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3393 0 : ->attr.pointer_comp;
3394 : }
3395 : }
3396 1711 : else if (e->symtree)
3397 1711 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3398 : else
3399 0 : gfc_clear_attr (&attr);
3400 : break;
3401 :
3402 4054 : default:
3403 4054 : gfc_clear_attr (&attr);
3404 4054 : break;
3405 : }
3406 :
3407 20734 : return attr;
3408 : }
3409 :
3410 :
3411 : /* Match a structure constructor. The initial symbol has already been
3412 : seen. */
3413 :
3414 : typedef struct gfc_structure_ctor_component
3415 : {
3416 : char* name;
3417 : gfc_expr* val;
3418 : locus where;
3419 : struct gfc_structure_ctor_component* next;
3420 : }
3421 : gfc_structure_ctor_component;
3422 :
3423 : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3424 :
3425 : static void
3426 10964 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3427 : {
3428 10964 : free (comp->name);
3429 10964 : gfc_free_expr (comp->val);
3430 10964 : free (comp);
3431 10964 : }
3432 :
3433 :
3434 : /* Translate the component list into the actual constructor by sorting it in
3435 : the order required; this also checks along the way that each and every
3436 : component actually has an initializer and handles default initializers
3437 : for components without explicit value given. */
3438 : static bool
3439 7605 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
3440 : gfc_constructor_base *ctor_head, gfc_symbol *sym)
3441 : {
3442 7605 : gfc_structure_ctor_component *comp_iter;
3443 7605 : gfc_component *comp;
3444 :
3445 19988 : for (comp = sym->components; comp; comp = comp->next)
3446 : {
3447 12395 : gfc_structure_ctor_component **next_ptr;
3448 12395 : gfc_expr *value = NULL;
3449 :
3450 : /* Try to find the initializer for the current component by name. */
3451 12395 : next_ptr = comp_head;
3452 13628 : for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3453 : {
3454 12173 : if (!strcmp (comp_iter->name, comp->name))
3455 : break;
3456 1233 : next_ptr = &comp_iter->next;
3457 : }
3458 :
3459 : /* If an extension, try building the parent derived type by building
3460 : a value expression for the parent derived type and calling self. */
3461 12395 : if (!comp_iter && comp == sym->components && sym->attr.extension)
3462 : {
3463 118 : value = gfc_get_structure_constructor_expr (comp->ts.type,
3464 : comp->ts.kind,
3465 : &gfc_current_locus);
3466 118 : value->ts = comp->ts;
3467 :
3468 118 : if (!build_actual_constructor (comp_head,
3469 : &value->value.constructor,
3470 118 : comp->ts.u.derived))
3471 : {
3472 0 : gfc_free_expr (value);
3473 0 : return false;
3474 : }
3475 :
3476 118 : gfc_constructor_append_expr (ctor_head, value, NULL);
3477 118 : continue;
3478 : }
3479 :
3480 : /* If it was not found, apply NULL expression to set the component as
3481 : unallocated. Then try the default initializer if there's any;
3482 : otherwise, it's an error unless this is a deferred parameter. */
3483 1337 : if (!comp_iter)
3484 : {
3485 : /* F2018 7.5.10: If an allocatable component has no corresponding
3486 : component-data-source, then that component has an allocation
3487 : status of unallocated.... */
3488 1337 : if (comp->attr.allocatable
3489 1202 : || (comp->ts.type == BT_CLASS
3490 15 : && CLASS_DATA (comp)->attr.allocatable))
3491 : {
3492 144 : if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3493 : "allocatable component %qs given in the "
3494 : "structure constructor at %C", comp->name))
3495 : return false;
3496 144 : value = gfc_get_null_expr (&gfc_current_locus);
3497 : }
3498 : /* ....(Preceding sentence) If a component with default
3499 : initialization has no corresponding component-data-source, then
3500 : the default initialization is applied to that component. */
3501 1193 : else if (comp->initializer)
3502 : {
3503 667 : if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3504 : "with missing optional arguments at %C"))
3505 : return false;
3506 665 : value = gfc_copy_expr (comp->initializer);
3507 : }
3508 : /* Do not trap components such as the string length for deferred
3509 : length character components. */
3510 526 : else if (!comp->attr.artificial)
3511 : {
3512 10 : gfc_error ("No initializer for component %qs given in the"
3513 : " structure constructor at %C", comp->name);
3514 10 : return false;
3515 : }
3516 : }
3517 : else
3518 10940 : value = comp_iter->val;
3519 :
3520 : /* Add the value to the constructor chain built. */
3521 12265 : gfc_constructor_append_expr (ctor_head, value, NULL);
3522 :
3523 : /* Remove the entry from the component list. We don't want the expression
3524 : value to be free'd, so set it to NULL. */
3525 12265 : if (comp_iter)
3526 : {
3527 10940 : *next_ptr = comp_iter->next;
3528 10940 : comp_iter->val = NULL;
3529 10940 : gfc_free_structure_ctor_component (comp_iter);
3530 : }
3531 : }
3532 : return true;
3533 : }
3534 :
3535 :
3536 : bool
3537 7502 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3538 : gfc_actual_arglist **arglist,
3539 : bool parent)
3540 : {
3541 7502 : gfc_actual_arglist *actual;
3542 7502 : gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3543 7502 : gfc_constructor_base ctor_head = NULL;
3544 7502 : gfc_component *comp; /* Is set NULL when named component is first seen */
3545 7502 : const char* last_name = NULL;
3546 7502 : locus old_locus;
3547 7502 : gfc_expr *expr;
3548 :
3549 7502 : expr = parent ? *cexpr : e;
3550 7502 : old_locus = gfc_current_locus;
3551 7502 : if (parent)
3552 : ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3553 : else
3554 6734 : gfc_current_locus = expr->where;
3555 :
3556 7502 : comp_tail = comp_head = NULL;
3557 :
3558 7502 : if (!parent && sym->attr.abstract)
3559 : {
3560 1 : gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3561 : sym->name, &expr->where);
3562 1 : goto cleanup;
3563 : }
3564 :
3565 7501 : comp = sym->components;
3566 7501 : actual = parent ? *arglist : expr->value.function.actual;
3567 17816 : for ( ; actual; )
3568 : {
3569 10964 : gfc_component *this_comp = NULL;
3570 :
3571 10964 : if (!comp_head)
3572 7075 : comp_tail = comp_head = gfc_get_structure_ctor_component ();
3573 : else
3574 : {
3575 3889 : comp_tail->next = gfc_get_structure_ctor_component ();
3576 3889 : comp_tail = comp_tail->next;
3577 : }
3578 10964 : if (actual->name)
3579 : {
3580 1447 : if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3581 : " constructor with named arguments at %C"))
3582 1 : goto cleanup;
3583 :
3584 1446 : comp_tail->name = xstrdup (actual->name);
3585 1446 : last_name = comp_tail->name;
3586 1446 : comp = NULL;
3587 : }
3588 : else
3589 : {
3590 : /* Components without name are not allowed after the first named
3591 : component initializer! */
3592 9517 : if (!comp || comp->attr.artificial)
3593 : {
3594 2 : if (last_name)
3595 0 : gfc_error ("Component initializer without name after component"
3596 : " named %s at %L", last_name,
3597 0 : actual->expr ? &actual->expr->where
3598 : : &gfc_current_locus);
3599 : else
3600 2 : gfc_error ("Too many components in structure constructor at "
3601 2 : "%L", actual->expr ? &actual->expr->where
3602 : : &gfc_current_locus);
3603 2 : goto cleanup;
3604 : }
3605 :
3606 9515 : comp_tail->name = xstrdup (comp->name);
3607 : }
3608 :
3609 : /* Find the current component in the structure definition and check
3610 : its access is not private. */
3611 10961 : if (comp)
3612 9515 : this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3613 : else
3614 : {
3615 1446 : this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3616 : false, false, NULL);
3617 1446 : comp = NULL; /* Reset needed! */
3618 : }
3619 :
3620 : /* Here we can check if a component name is given which does not
3621 : correspond to any component of the defined structure. */
3622 10961 : if (!this_comp)
3623 8 : goto cleanup;
3624 :
3625 : /* For a constant string constructor, make sure the length is
3626 : correct; truncate or fill with blanks if needed. */
3627 10953 : if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3628 1125 : && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3629 1123 : && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3630 1105 : && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3631 1104 : && actual->expr
3632 1100 : && actual->expr->ts.type == BT_CHARACTER
3633 982 : && actual->expr->expr_type == EXPR_CONSTANT)
3634 : {
3635 759 : ptrdiff_t c, e1;
3636 759 : c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3637 759 : e1 = actual->expr->value.character.length;
3638 :
3639 759 : if (c != e1)
3640 : {
3641 249 : ptrdiff_t i, to;
3642 249 : gfc_char_t *dest;
3643 249 : dest = gfc_get_wide_string (c + 1);
3644 :
3645 249 : to = e1 < c ? e1 : c;
3646 4482 : for (i = 0; i < to; i++)
3647 4233 : dest[i] = actual->expr->value.character.string[i];
3648 :
3649 5812 : for (i = e1; i < c; i++)
3650 5563 : dest[i] = ' ';
3651 :
3652 249 : dest[c] = '\0';
3653 249 : free (actual->expr->value.character.string);
3654 :
3655 249 : actual->expr->value.character.length = c;
3656 249 : actual->expr->value.character.string = dest;
3657 :
3658 249 : if (warn_line_truncation && c < e1)
3659 14 : gfc_warning_now (OPT_Wcharacter_truncation,
3660 : "CHARACTER expression will be truncated "
3661 : "in constructor (%td/%td) at %L", c,
3662 : e1, &actual->expr->where);
3663 : }
3664 : }
3665 :
3666 10953 : comp_tail->val = actual->expr;
3667 10953 : if (actual->expr != NULL)
3668 10948 : comp_tail->where = actual->expr->where;
3669 10953 : actual->expr = NULL;
3670 :
3671 : /* Check if this component is already given a value. */
3672 17367 : for (comp_iter = comp_head; comp_iter != comp_tail;
3673 6414 : comp_iter = comp_iter->next)
3674 : {
3675 6415 : gcc_assert (comp_iter);
3676 6415 : if (!strcmp (comp_iter->name, comp_tail->name))
3677 : {
3678 1 : gfc_error ("Component %qs is initialized twice in the structure"
3679 : " constructor at %L", comp_tail->name,
3680 : comp_tail->val ? &comp_tail->where
3681 : : &gfc_current_locus);
3682 1 : goto cleanup;
3683 : }
3684 : }
3685 :
3686 : /* F2008, R457/C725, for PURE C1283. */
3687 72 : if (this_comp->attr.pointer && comp_tail->val
3688 11024 : && gfc_is_coindexed (comp_tail->val))
3689 : {
3690 2 : gfc_error ("Coindexed expression to pointer component %qs in "
3691 : "structure constructor at %L", comp_tail->name,
3692 : &comp_tail->where);
3693 2 : goto cleanup;
3694 : }
3695 :
3696 : /* If not explicitly a parent constructor, gather up the components
3697 : and build one. */
3698 10950 : if (comp && comp == sym->components
3699 6593 : && sym->attr.extension
3700 816 : && comp_tail->val
3701 816 : && (!gfc_bt_struct (comp_tail->val->ts.type)
3702 78 : || comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3703 : {
3704 768 : bool m;
3705 768 : gfc_actual_arglist *arg_null = NULL;
3706 :
3707 768 : actual->expr = comp_tail->val;
3708 768 : comp_tail->val = NULL;
3709 : #define shorter gfc_convert_to_structure_constructor
3710 768 : m = shorter (NULL, comp->ts.u.derived, &comp_tail->val,
3711 768 : comp->ts.u.derived->attr.zero_comp ? &arg_null :
3712 : &actual, true);
3713 : #undef shorter
3714 :
3715 768 : if (!m)
3716 0 : goto cleanup;
3717 :
3718 768 : if (comp->ts.u.derived->attr.zero_comp)
3719 : {
3720 132 : comp = comp->next;
3721 132 : continue;
3722 : }
3723 : }
3724 :
3725 636 : if (comp)
3726 9375 : comp = comp->next;
3727 10818 : if (parent && !comp)
3728 : break;
3729 :
3730 10183 : if (actual)
3731 10182 : actual = actual->next;
3732 : }
3733 :
3734 7487 : if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3735 12 : goto cleanup;
3736 :
3737 : /* No component should be left, as this should have caused an error in the
3738 : loop constructing the component-list (name that does not correspond to any
3739 : component in the structure definition). */
3740 7475 : if (comp_head && sym->attr.extension)
3741 : {
3742 2 : for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3743 : {
3744 1 : gfc_error ("component %qs at %L has already been set by a "
3745 : "parent derived type constructor", comp_iter->name,
3746 : &comp_iter->where);
3747 : }
3748 1 : goto cleanup;
3749 : }
3750 : else
3751 7474 : gcc_assert (!comp_head);
3752 :
3753 7474 : if (parent)
3754 : {
3755 768 : expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3756 768 : expr->ts.u.derived = sym;
3757 768 : expr->value.constructor = ctor_head;
3758 768 : *cexpr = expr;
3759 : }
3760 : else
3761 : {
3762 6706 : expr->ts.u.derived = sym;
3763 6706 : expr->ts.kind = 0;
3764 6706 : expr->ts.type = BT_DERIVED;
3765 6706 : expr->value.constructor = ctor_head;
3766 6706 : expr->expr_type = EXPR_STRUCTURE;
3767 : }
3768 :
3769 7474 : gfc_current_locus = old_locus;
3770 7474 : if (parent)
3771 768 : *arglist = actual;
3772 : return true;
3773 :
3774 28 : cleanup:
3775 28 : gfc_current_locus = old_locus;
3776 :
3777 52 : for (comp_iter = comp_head; comp_iter; )
3778 : {
3779 24 : gfc_structure_ctor_component *next = comp_iter->next;
3780 24 : gfc_free_structure_ctor_component (comp_iter);
3781 24 : comp_iter = next;
3782 : }
3783 28 : gfc_constructor_free (ctor_head);
3784 :
3785 28 : return false;
3786 : }
3787 :
3788 :
3789 : match
3790 60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3791 : gfc_expr **result)
3792 : {
3793 60 : match m;
3794 60 : gfc_expr *e;
3795 60 : bool t = true;
3796 :
3797 60 : e = gfc_get_expr ();
3798 60 : e->expr_type = EXPR_FUNCTION;
3799 60 : e->symtree = symtree;
3800 60 : e->where = gfc_current_locus;
3801 :
3802 60 : gcc_assert (gfc_fl_struct (sym->attr.flavor)
3803 : && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3804 60 : e->value.function.esym = sym;
3805 60 : e->symtree->n.sym->attr.generic = 1;
3806 :
3807 60 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
3808 60 : if (m != MATCH_YES)
3809 : {
3810 0 : gfc_free_expr (e);
3811 0 : return m;
3812 : }
3813 :
3814 60 : if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3815 : {
3816 1 : gfc_free_expr (e);
3817 1 : return MATCH_ERROR;
3818 : }
3819 :
3820 : /* If a structure constructor is in a DATA statement, then each entity
3821 : in the structure constructor must be a constant. Try to reduce the
3822 : expression here. */
3823 59 : if (gfc_in_match_data ())
3824 59 : t = gfc_reduce_init_expr (e);
3825 :
3826 59 : if (t)
3827 : {
3828 49 : *result = e;
3829 49 : return MATCH_YES;
3830 : }
3831 : else
3832 : {
3833 10 : gfc_free_expr (e);
3834 10 : return MATCH_ERROR;
3835 : }
3836 : }
3837 :
3838 :
3839 : /* If the symbol is an implicit do loop index and implicitly typed,
3840 : it should not be host associated. Provide a symtree from the
3841 : current namespace. */
3842 : static match
3843 7012548 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3844 : {
3845 7012548 : if ((*sym)->attr.flavor == FL_VARIABLE
3846 2018482 : && (*sym)->ns != gfc_current_ns
3847 63660 : && (*sym)->attr.implied_index
3848 618 : && (*sym)->attr.implicit_type
3849 32 : && !(*sym)->attr.use_assoc)
3850 : {
3851 32 : int i;
3852 32 : i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3853 32 : if (i)
3854 : return MATCH_ERROR;
3855 32 : *sym = (*st)->n.sym;
3856 : }
3857 : return MATCH_YES;
3858 : }
3859 :
3860 :
3861 : /* Procedure pointer as function result: Replace the function symbol by the
3862 : auto-generated hidden result variable named "ppr@". */
3863 :
3864 : static bool
3865 5222102 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3866 : {
3867 : /* Check for procedure pointer result variable. */
3868 5222102 : if ((*sym)->attr.function && !(*sym)->attr.external
3869 1415060 : && (*sym)->result && (*sym)->result != *sym
3870 11288 : && (*sym)->result->attr.proc_pointer
3871 337 : && (*sym) == gfc_current_ns->proc_name
3872 285 : && (*sym) == (*sym)->result->ns->proc_name
3873 285 : && strcmp ("ppr@", (*sym)->result->name) == 0)
3874 : {
3875 : /* Automatic replacement with "hidden" result variable. */
3876 285 : (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3877 285 : *sym = (*sym)->result;
3878 285 : *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3879 285 : return true;
3880 : }
3881 : return false;
3882 : }
3883 :
3884 :
3885 : /* Matches a variable name followed by anything that might follow it--
3886 : array reference, argument list of a function, etc. */
3887 :
3888 : match
3889 4310280 : gfc_match_rvalue (gfc_expr **result)
3890 : {
3891 4310280 : gfc_actual_arglist *actual_arglist;
3892 4310280 : char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3893 4310280 : gfc_state_data *st;
3894 4310280 : gfc_symbol *sym;
3895 4310280 : gfc_symtree *symtree;
3896 4310280 : locus where, old_loc;
3897 4310280 : gfc_expr *e;
3898 4310280 : match m, m2;
3899 4310280 : int i;
3900 4310280 : gfc_typespec *ts;
3901 4310280 : bool implicit_char;
3902 4310280 : gfc_ref *ref;
3903 4310280 : gfc_symtree *pdt_st;
3904 :
3905 4310280 : m = gfc_match ("%%loc");
3906 4310280 : if (m == MATCH_YES)
3907 : {
3908 10878 : if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3909 : return MATCH_ERROR;
3910 10877 : strncpy (name, "loc", 4);
3911 : }
3912 :
3913 : else
3914 : {
3915 4299402 : m = gfc_match_name (name);
3916 4299402 : if (m != MATCH_YES)
3917 : return m;
3918 : }
3919 :
3920 : /* Check if the symbol exists. */
3921 4104683 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3922 : return MATCH_ERROR;
3923 :
3924 : /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3925 : type. For derived types we create a generic symbol which links to the
3926 : derived type symbol; STRUCTUREs are simpler and must not conflict with
3927 : variables. */
3928 4104681 : if (!symtree)
3929 183920 : if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3930 : return MATCH_ERROR;
3931 4104681 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3932 : {
3933 4104681 : if (gfc_find_state (COMP_INTERFACE)
3934 4104681 : && !gfc_current_ns->has_import_set)
3935 97840 : i = gfc_get_sym_tree (name, NULL, &symtree, false);
3936 : else
3937 4006841 : i = gfc_get_ha_sym_tree (name, &symtree);
3938 4104681 : if (i)
3939 : return MATCH_ERROR;
3940 : }
3941 :
3942 :
3943 4104681 : sym = symtree->n.sym;
3944 4104681 : e = NULL;
3945 4104681 : where = gfc_current_locus;
3946 :
3947 4104681 : replace_hidden_procptr_result (&sym, &symtree);
3948 :
3949 : /* If this is an implicit do loop index and implicitly typed,
3950 : it should not be host associated. */
3951 4104681 : m = check_for_implicit_index (&symtree, &sym);
3952 4104681 : if (m != MATCH_YES)
3953 : return m;
3954 :
3955 4104681 : gfc_set_sym_referenced (sym);
3956 4104681 : sym->attr.implied_index = 0;
3957 :
3958 4104681 : if (sym->attr.function && sym->result == sym)
3959 : {
3960 : /* See if this is a directly recursive function call. */
3961 711225 : gfc_gobble_whitespace ();
3962 711225 : if (sym->attr.recursive
3963 100 : && gfc_peek_ascii_char () == '('
3964 93 : && gfc_current_ns->proc_name == sym
3965 711232 : && !sym->attr.dimension)
3966 : {
3967 4 : gfc_error ("%qs at %C is the name of a recursive function "
3968 : "and so refers to the result variable. Use an "
3969 : "explicit RESULT variable for direct recursion "
3970 : "(12.5.2.1)", sym->name);
3971 4 : return MATCH_ERROR;
3972 : }
3973 :
3974 711221 : if (gfc_is_function_return_value (sym, gfc_current_ns))
3975 1725 : goto variable;
3976 :
3977 709496 : if (sym->attr.entry
3978 187 : && (sym->ns == gfc_current_ns
3979 27 : || sym->ns == gfc_current_ns->parent))
3980 : {
3981 180 : gfc_entry_list *el = NULL;
3982 :
3983 180 : for (el = sym->ns->entries; el; el = el->next)
3984 180 : if (sym == el->sym)
3985 180 : goto variable;
3986 : }
3987 : }
3988 :
3989 4102772 : if (gfc_matching_procptr_assignment)
3990 : {
3991 : /* It can be a procedure or a derived-type procedure or a not-yet-known
3992 : type. */
3993 1365 : if (sym->attr.flavor != FL_UNKNOWN
3994 1017 : && sym->attr.flavor != FL_PROCEDURE
3995 64 : && sym->attr.flavor != FL_PARAMETER
3996 64 : && sym->attr.flavor != FL_VARIABLE)
3997 : {
3998 2 : gfc_error ("Symbol at %C is not appropriate for an expression");
3999 2 : return MATCH_ERROR;
4000 : }
4001 1363 : goto procptr0;
4002 : }
4003 :
4004 4101407 : if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
4005 724464 : goto function0;
4006 :
4007 3376943 : if (sym->attr.generic)
4008 69128 : goto generic_function;
4009 :
4010 3307815 : switch (sym->attr.flavor)
4011 : {
4012 1748527 : case FL_VARIABLE:
4013 1748527 : variable:
4014 1748527 : e = gfc_get_expr ();
4015 :
4016 1748527 : e->expr_type = EXPR_VARIABLE;
4017 1748527 : e->symtree = symtree;
4018 :
4019 1748527 : m = gfc_match_varspec (e, 0, false, true);
4020 1748527 : break;
4021 :
4022 228140 : case FL_PARAMETER:
4023 : /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
4024 : end up here. Unfortunately, sym->value->expr_type is set to
4025 : EXPR_CONSTANT, and so the if () branch would be followed without
4026 : the !sym->as check. */
4027 228140 : if (sym->value && sym->value->expr_type != EXPR_ARRAY
4028 193214 : && sym->value->expr_type != EXPR_VARIABLE && !sym->as)
4029 193204 : e = gfc_copy_expr (sym->value);
4030 : else
4031 : {
4032 34936 : e = gfc_get_expr ();
4033 34936 : e->expr_type = EXPR_VARIABLE;
4034 : }
4035 :
4036 228140 : e->symtree = symtree;
4037 228140 : m = gfc_match_varspec (e, 0, false, true);
4038 :
4039 228140 : if (sym->ts.is_c_interop || sym->ts.is_iso_c)
4040 : break;
4041 :
4042 : /* Variable array references to derived type parameters cause
4043 : all sorts of headaches in simplification. Treating such
4044 : expressions as variable works just fine for all array
4045 : references. */
4046 177124 : if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
4047 : {
4048 2844 : for (ref = e->ref; ref; ref = ref->next)
4049 2658 : if (ref->type == REF_ARRAY)
4050 : break;
4051 :
4052 2613 : if (ref == NULL || ref->u.ar.type == AR_FULL)
4053 : break;
4054 :
4055 1018 : ref = e->ref;
4056 1018 : e->ref = NULL;
4057 1018 : gfc_free_expr (e);
4058 1018 : e = gfc_get_expr ();
4059 1018 : e->expr_type = EXPR_VARIABLE;
4060 1018 : e->symtree = symtree;
4061 1018 : e->ref = ref;
4062 : }
4063 :
4064 : break;
4065 :
4066 0 : case FL_STRUCT:
4067 0 : case FL_DERIVED:
4068 0 : sym = gfc_use_derived (sym);
4069 0 : if (sym == NULL)
4070 : m = MATCH_ERROR;
4071 : else
4072 0 : goto generic_function;
4073 : break;
4074 :
4075 : /* If we're here, then the name is known to be the name of a
4076 : procedure, yet it is not sure to be the name of a function. */
4077 1037315 : case FL_PROCEDURE:
4078 :
4079 : /* Procedure Pointer Assignments. */
4080 1037315 : procptr0:
4081 1037315 : if (gfc_matching_procptr_assignment)
4082 : {
4083 1363 : gfc_gobble_whitespace ();
4084 1363 : if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
4085 : /* Parse functions returning a procptr. */
4086 210 : goto function0;
4087 :
4088 1153 : e = gfc_get_expr ();
4089 1153 : e->expr_type = EXPR_VARIABLE;
4090 1153 : e->symtree = symtree;
4091 1153 : m = gfc_match_varspec (e, 0, false, true);
4092 1085 : if (!e->ref && sym->attr.flavor == FL_UNKNOWN
4093 203 : && sym->ts.type == BT_UNKNOWN
4094 1346 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4095 : {
4096 : m = MATCH_ERROR;
4097 : break;
4098 : }
4099 : break;
4100 : }
4101 :
4102 1035952 : if (sym->attr.subroutine)
4103 : {
4104 57 : gfc_error ("Unexpected use of subroutine name %qs at %C",
4105 : sym->name);
4106 57 : m = MATCH_ERROR;
4107 57 : break;
4108 : }
4109 :
4110 : /* At this point, the name has to be a non-statement function.
4111 : If the name is the same as the current function being
4112 : compiled, then we have a variable reference (to the function
4113 : result) if the name is non-recursive. */
4114 :
4115 1035895 : st = gfc_enclosing_unit (NULL);
4116 :
4117 1035895 : if (st != NULL
4118 990535 : && st->state == COMP_FUNCTION
4119 87068 : && st->sym == sym
4120 0 : && !sym->attr.recursive)
4121 : {
4122 0 : e = gfc_get_expr ();
4123 0 : e->symtree = symtree;
4124 0 : e->expr_type = EXPR_VARIABLE;
4125 :
4126 0 : m = gfc_match_varspec (e, 0, false, true);
4127 0 : break;
4128 : }
4129 :
4130 : /* Match a function reference. */
4131 1035895 : function0:
4132 1760569 : m = gfc_match_actual_arglist (0, &actual_arglist);
4133 1760569 : if (m == MATCH_NO)
4134 : {
4135 612959 : if (sym->attr.proc == PROC_ST_FUNCTION)
4136 1 : gfc_error ("Statement function %qs requires argument list at %C",
4137 : sym->name);
4138 : else
4139 612958 : gfc_error ("Function %qs requires an argument list at %C",
4140 : sym->name);
4141 :
4142 : m = MATCH_ERROR;
4143 : break;
4144 : }
4145 :
4146 1147610 : if (m != MATCH_YES)
4147 : {
4148 : m = MATCH_ERROR;
4149 : break;
4150 : }
4151 :
4152 : /* Check to see if this is a PDT constructor. The format of these
4153 : constructors is rather unusual:
4154 : name [(type_params)](component_values)
4155 : where, component_values excludes the type_params. With the present
4156 : gfortran representation this is rather awkward because the two are not
4157 : distinguished, other than by their attributes.
4158 :
4159 : Even if 'name' is that of a PDT template, priority has to be given to
4160 : specific procedures, other than the constructor, in the generic
4161 : interface. */
4162 :
4163 1114526 : gfc_gobble_whitespace ();
4164 1114526 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &pdt_st);
4165 11484 : if (sym->attr.generic && pdt_st != NULL
4166 1123871 : && !(sym->generic->next && gfc_peek_ascii_char() != '('))
4167 : {
4168 9045 : gfc_symbol *pdt_sym;
4169 9045 : gfc_actual_arglist *ctr_arglist = NULL, *tmp;
4170 9045 : gfc_component *c;
4171 :
4172 : /* Use the template. */
4173 9045 : if (pdt_st->n.sym && pdt_st->n.sym->attr.pdt_template)
4174 : {
4175 1119 : bool type_spec_list = false;
4176 1119 : pdt_sym = pdt_st->n.sym;
4177 1119 : gfc_gobble_whitespace ();
4178 : /* Look for a second actual arglist. If present, try the first
4179 : for the type parameters. Otherwise, or if there is no match,
4180 : depend on default values by setting the type parameters to
4181 : NULL. */
4182 1119 : if (gfc_peek_ascii_char() == '(')
4183 249 : type_spec_list = true;
4184 1119 : if (!actual_arglist && !type_spec_list)
4185 : {
4186 3 : gfc_error_now ("F2023 R755: The empty type specification at %C "
4187 : "is not allowed");
4188 3 : m = MATCH_ERROR;
4189 3 : break;
4190 : }
4191 : /* Generate this instance using the type parameters from the
4192 : first argument list and return the parameter list in
4193 : ctr_arglist. */
4194 1116 : m = gfc_get_pdt_instance (actual_arglist, &pdt_sym, &ctr_arglist);
4195 1116 : if (m != MATCH_YES || !ctr_arglist)
4196 : {
4197 43 : if (ctr_arglist)
4198 0 : gfc_free_actual_arglist (ctr_arglist);
4199 : /* See if all the type parameters have default values. */
4200 43 : m = gfc_get_pdt_instance (NULL, &pdt_sym, &ctr_arglist);
4201 43 : if (m != MATCH_YES)
4202 : {
4203 : m = MATCH_NO;
4204 : break;
4205 : }
4206 : }
4207 :
4208 : /* Now match the component_values if the type parameters were
4209 : present. */
4210 1107 : if (type_spec_list)
4211 : {
4212 249 : m = gfc_match_actual_arglist (0, &actual_arglist);
4213 249 : if (m != MATCH_YES)
4214 : {
4215 : m = MATCH_ERROR;
4216 : break;
4217 : }
4218 : }
4219 :
4220 : /* Make sure that the component names are in place so that this
4221 : list can be safely appended to the type parameters. */
4222 1107 : tmp = actual_arglist;
4223 3730 : for (c = pdt_sym->components; c && tmp; c = c->next)
4224 : {
4225 2623 : if (c->attr.pdt_kind || c->attr.pdt_len)
4226 1363 : continue;
4227 1260 : if (!tmp->name)
4228 938 : tmp->name = c->name;
4229 1260 : tmp = tmp->next;
4230 : }
4231 :
4232 1107 : gfc_find_sym_tree (gfc_dt_lower_string (pdt_sym->name),
4233 : NULL, 1, &symtree);
4234 1107 : if (!symtree)
4235 : {
4236 520 : gfc_get_ha_sym_tree (gfc_dt_lower_string (pdt_sym->name) ,
4237 : &symtree);
4238 520 : symtree->n.sym = pdt_sym;
4239 520 : symtree->n.sym->ts.u.derived = pdt_sym;
4240 520 : symtree->n.sym->ts.type = BT_DERIVED;
4241 : }
4242 :
4243 1107 : if (type_spec_list)
4244 : {
4245 : /* Append the type_params and the component_values. */
4246 287 : for (tmp = ctr_arglist; tmp && tmp->next;)
4247 : tmp = tmp->next;
4248 249 : tmp->next = actual_arglist;
4249 249 : actual_arglist = ctr_arglist;
4250 249 : tmp = actual_arglist;
4251 : /* Can now add all the component names. */
4252 817 : for (c = pdt_sym->components; c && tmp; c = c->next)
4253 : {
4254 568 : if (!tmp->name)
4255 1 : tmp->name = c->name;
4256 568 : tmp = tmp->next;
4257 : }
4258 : }
4259 : }
4260 : }
4261 :
4262 1114514 : gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4263 1114514 : sym = symtree->n.sym;
4264 :
4265 1114514 : replace_hidden_procptr_result (&sym, &symtree);
4266 :
4267 1114514 : e = gfc_get_expr ();
4268 1114514 : e->symtree = symtree;
4269 1114514 : e->expr_type = EXPR_FUNCTION;
4270 1114514 : e->value.function.actual = actual_arglist;
4271 1114514 : e->where = gfc_current_locus;
4272 :
4273 1114514 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4274 218 : && CLASS_DATA (sym)->as)
4275 : {
4276 91 : e->rank = CLASS_DATA (sym)->as->rank;
4277 91 : e->corank = CLASS_DATA (sym)->as->corank;
4278 : }
4279 1114423 : else if (sym->as != NULL)
4280 : {
4281 1157 : e->rank = sym->as->rank;
4282 1157 : e->corank = sym->as->corank;
4283 : }
4284 :
4285 1114514 : if (!sym->attr.function
4286 1114514 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4287 : {
4288 : m = MATCH_ERROR;
4289 : break;
4290 : }
4291 :
4292 : /* Check here for the existence of at least one argument for the
4293 : iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4294 1114514 : if (sym->attr.is_iso_c == 1
4295 2 : && (sym->from_intmod == INTMOD_ISO_C_BINDING
4296 2 : && (sym->intmod_sym_id == ISOCBINDING_LOC
4297 2 : || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4298 2 : || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4299 2 : || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4300 : {
4301 : /* make sure we were given a param */
4302 0 : if (actual_arglist == NULL)
4303 : {
4304 0 : gfc_error ("Missing argument to %qs at %C", sym->name);
4305 0 : m = MATCH_ERROR;
4306 0 : break;
4307 : }
4308 : }
4309 :
4310 1114514 : if (sym->result == NULL)
4311 398142 : sym->result = sym;
4312 :
4313 1114514 : gfc_gobble_whitespace ();
4314 : /* F08:C612. */
4315 1114514 : if (gfc_peek_ascii_char() == '%')
4316 : {
4317 12 : gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4318 : "function reference at %C");
4319 12 : m = MATCH_ERROR;
4320 12 : break;
4321 : }
4322 :
4323 : m = MATCH_YES;
4324 : break;
4325 :
4326 295516 : case FL_UNKNOWN:
4327 :
4328 : /* Special case for derived type variables that get their types
4329 : via an IMPLICIT statement. This can't wait for the
4330 : resolution phase. */
4331 :
4332 295516 : old_loc = gfc_current_locus;
4333 295516 : if (gfc_match_member_sep (sym) == MATCH_YES
4334 10806 : && sym->ts.type == BT_UNKNOWN
4335 295522 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4336 0 : gfc_set_default_type (sym, 0, sym->ns);
4337 295516 : gfc_current_locus = old_loc;
4338 :
4339 : /* If the symbol has a (co)dimension attribute, the expression is a
4340 : variable. */
4341 :
4342 295516 : if (sym->attr.dimension || sym->attr.codimension)
4343 : {
4344 36487 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4345 : {
4346 : m = MATCH_ERROR;
4347 : break;
4348 : }
4349 :
4350 36487 : e = gfc_get_expr ();
4351 36487 : e->symtree = symtree;
4352 36487 : e->expr_type = EXPR_VARIABLE;
4353 36487 : m = gfc_match_varspec (e, 0, false, true);
4354 36487 : break;
4355 : }
4356 :
4357 259029 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4358 4994 : && (CLASS_DATA (sym)->attr.dimension
4359 3445 : || CLASS_DATA (sym)->attr.codimension))
4360 : {
4361 1649 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4362 : {
4363 : m = MATCH_ERROR;
4364 : break;
4365 : }
4366 :
4367 1649 : e = gfc_get_expr ();
4368 1649 : e->symtree = symtree;
4369 1649 : e->expr_type = EXPR_VARIABLE;
4370 1649 : m = gfc_match_varspec (e, 0, false, true);
4371 1649 : break;
4372 : }
4373 :
4374 : /* Name is not an array, so we peek to see if a '(' implies a
4375 : function call or a substring reference. Otherwise the
4376 : variable is just a scalar. */
4377 :
4378 257380 : gfc_gobble_whitespace ();
4379 257380 : if (gfc_peek_ascii_char () != '(')
4380 : {
4381 : /* Assume a scalar variable */
4382 78262 : e = gfc_get_expr ();
4383 78262 : e->symtree = symtree;
4384 78262 : e->expr_type = EXPR_VARIABLE;
4385 :
4386 78262 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4387 : {
4388 : m = MATCH_ERROR;
4389 : break;
4390 : }
4391 :
4392 : /*FIXME:??? gfc_match_varspec does set this for us: */
4393 78262 : e->ts = sym->ts;
4394 78262 : m = gfc_match_varspec (e, 0, false, true);
4395 78262 : break;
4396 : }
4397 :
4398 : /* See if this is a function reference with a keyword argument
4399 : as first argument. We do this because otherwise a spurious
4400 : symbol would end up in the symbol table. */
4401 :
4402 179118 : old_loc = gfc_current_locus;
4403 179118 : m2 = gfc_match (" ( %n =", argname);
4404 179118 : gfc_current_locus = old_loc;
4405 :
4406 179118 : e = gfc_get_expr ();
4407 179118 : e->symtree = symtree;
4408 :
4409 179118 : if (m2 != MATCH_YES)
4410 : {
4411 : /* Try to figure out whether we're dealing with a character type.
4412 : We're peeking ahead here, because we don't want to call
4413 : match_substring if we're dealing with an implicitly typed
4414 : non-character variable. */
4415 178025 : implicit_char = false;
4416 178025 : if (sym->ts.type == BT_UNKNOWN)
4417 : {
4418 173246 : ts = gfc_get_default_type (sym->name, NULL);
4419 173246 : if (ts->type == BT_CHARACTER)
4420 : implicit_char = true;
4421 : }
4422 :
4423 : /* See if this could possibly be a substring reference of a name
4424 : that we're not sure is a variable yet. */
4425 :
4426 178008 : if ((implicit_char || sym->ts.type == BT_CHARACTER)
4427 1453 : && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4428 : {
4429 :
4430 989 : e->expr_type = EXPR_VARIABLE;
4431 :
4432 989 : if (sym->attr.flavor != FL_VARIABLE
4433 989 : && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4434 : sym->name, NULL))
4435 : {
4436 : m = MATCH_ERROR;
4437 : break;
4438 : }
4439 :
4440 989 : if (sym->ts.type == BT_UNKNOWN
4441 989 : && !gfc_set_default_type (sym, 1, NULL))
4442 : {
4443 : m = MATCH_ERROR;
4444 : break;
4445 : }
4446 :
4447 989 : e->ts = sym->ts;
4448 989 : if (e->ref)
4449 964 : e->ts.u.cl = NULL;
4450 : m = MATCH_YES;
4451 : break;
4452 : }
4453 : }
4454 :
4455 : /* Give up, assume we have a function. */
4456 :
4457 178129 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4458 178129 : sym = symtree->n.sym;
4459 178129 : e->expr_type = EXPR_FUNCTION;
4460 :
4461 178129 : if (!sym->attr.function
4462 178129 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4463 : {
4464 : m = MATCH_ERROR;
4465 : break;
4466 : }
4467 :
4468 178129 : sym->result = sym;
4469 :
4470 178129 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4471 178129 : if (m == MATCH_NO)
4472 0 : gfc_error ("Missing argument list in function %qs at %C", sym->name);
4473 :
4474 178129 : if (m != MATCH_YES)
4475 : {
4476 : m = MATCH_ERROR;
4477 : break;
4478 : }
4479 :
4480 : /* If our new function returns a character, array or structure
4481 : type, it might have subsequent references. */
4482 :
4483 177999 : m = gfc_match_varspec (e, 0, false, true);
4484 177999 : if (m == MATCH_NO)
4485 : m = MATCH_YES;
4486 :
4487 : break;
4488 :
4489 69128 : generic_function:
4490 : /* Look for symbol first; if not found, look for STRUCTURE type symbol
4491 : specially. Creates a generic symbol for derived types. */
4492 69128 : gfc_find_sym_tree (name, NULL, 1, &symtree);
4493 69128 : if (!symtree)
4494 0 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4495 69128 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4496 69128 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4497 :
4498 69128 : e = gfc_get_expr ();
4499 69128 : e->symtree = symtree;
4500 69128 : e->expr_type = EXPR_FUNCTION;
4501 :
4502 69128 : if (gfc_fl_struct (sym->attr.flavor))
4503 : {
4504 0 : e->value.function.esym = sym;
4505 0 : e->symtree->n.sym->attr.generic = 1;
4506 : }
4507 :
4508 69128 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4509 69128 : break;
4510 :
4511 : case FL_NAMELIST:
4512 : m = MATCH_ERROR;
4513 : break;
4514 :
4515 5 : default:
4516 5 : gfc_error ("Symbol at %C is not appropriate for an expression");
4517 5 : return MATCH_ERROR;
4518 : }
4519 :
4520 : /* Scan for possible inquiry references. */
4521 614004 : if (m == MATCH_YES
4522 3456651 : && e->expr_type == EXPR_VARIABLE
4523 4239421 : && gfc_peek_ascii_char () == '%')
4524 : {
4525 38 : m = gfc_match_varspec (e, 0, false, false);
4526 38 : if (m == MATCH_NO)
4527 : m = MATCH_YES;
4528 : }
4529 :
4530 4104669 : if (m == MATCH_YES)
4531 : {
4532 3456651 : e->where = where;
4533 3456651 : *result = e;
4534 : }
4535 : else
4536 648018 : gfc_free_expr (e);
4537 :
4538 : return m;
4539 : }
4540 :
4541 :
4542 : /* Match a variable, i.e. something that can be assigned to. This
4543 : starts as a symbol, can be a structure component or an array
4544 : reference. It can be a function if the function doesn't have a
4545 : separate RESULT variable. If the symbol has not been previously
4546 : seen, we assume it is a variable.
4547 :
4548 : This function is called by two interface functions:
4549 : gfc_match_variable, which has host_flag = 1, and
4550 : gfc_match_equiv_variable, with host_flag = 0, to restrict the
4551 : match of the symbol to the local scope. */
4552 :
4553 : static match
4554 2907897 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4555 : {
4556 2907897 : gfc_symbol *sym, *dt_sym;
4557 2907897 : gfc_symtree *st;
4558 2907897 : gfc_expr *expr;
4559 2907897 : locus where, old_loc;
4560 2907897 : match m;
4561 :
4562 2907897 : *result = NULL;
4563 :
4564 : /* Since nothing has any business being an lvalue in a module
4565 : specification block, an interface block or a contains section,
4566 : we force the changed_symbols mechanism to work by setting
4567 : host_flag to 0. This prevents valid symbols that have the name
4568 : of keywords, such as 'end', being turned into variables by
4569 : failed matching to assignments for, e.g., END INTERFACE. */
4570 2907897 : if (gfc_current_state () == COMP_MODULE
4571 2907897 : || gfc_current_state () == COMP_SUBMODULE
4572 : || gfc_current_state () == COMP_INTERFACE
4573 : || gfc_current_state () == COMP_CONTAINS)
4574 203009 : host_flag = 0;
4575 :
4576 2907897 : where = gfc_current_locus;
4577 2907897 : m = gfc_match_sym_tree (&st, host_flag);
4578 2907896 : if (m != MATCH_YES)
4579 : return m;
4580 :
4581 2907867 : sym = st->n.sym;
4582 :
4583 : /* If this is an implicit do loop index and implicitly typed,
4584 : it should not be host associated. */
4585 2907867 : m = check_for_implicit_index (&st, &sym);
4586 2907867 : if (m != MATCH_YES)
4587 : return m;
4588 :
4589 2907867 : sym->attr.implied_index = 0;
4590 :
4591 2907867 : gfc_set_sym_referenced (sym);
4592 :
4593 : /* STRUCTUREs may share names with variables, but derived types may not. */
4594 14561 : if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4595 2907935 : && (dt_sym = gfc_find_dt_in_generic (sym)))
4596 : {
4597 7 : if (dt_sym->attr.flavor == FL_DERIVED)
4598 7 : gfc_error ("Derived type %qs cannot be used as a variable at %C",
4599 : sym->name);
4600 : return MATCH_ERROR;
4601 : }
4602 :
4603 2907860 : switch (sym->attr.flavor)
4604 : {
4605 : case FL_VARIABLE:
4606 : /* Everything is alright. */
4607 : break;
4608 :
4609 2621327 : case FL_UNKNOWN:
4610 2621327 : {
4611 2621327 : sym_flavor flavor = FL_UNKNOWN;
4612 :
4613 2621327 : gfc_gobble_whitespace ();
4614 :
4615 2621327 : if (sym->attr.external || sym->attr.procedure
4616 2621295 : || sym->attr.function || sym->attr.subroutine)
4617 : flavor = FL_PROCEDURE;
4618 :
4619 : /* If it is not a procedure, is not typed and is host associated,
4620 : we cannot give it a flavor yet. */
4621 2621295 : else if (sym->ns == gfc_current_ns->parent
4622 2972 : && sym->ts.type == BT_UNKNOWN)
4623 : break;
4624 :
4625 : /* These are definitive indicators that this is a variable. */
4626 3490806 : else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4627 3472727 : || sym->attr.pointer || sym->as != NULL)
4628 : flavor = FL_VARIABLE;
4629 :
4630 : if (flavor != FL_UNKNOWN
4631 1770496 : && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4632 : return MATCH_ERROR;
4633 : }
4634 : break;
4635 :
4636 17 : case FL_PARAMETER:
4637 17 : if (equiv_flag)
4638 : {
4639 0 : gfc_error ("Named constant at %C in an EQUIVALENCE");
4640 0 : return MATCH_ERROR;
4641 : }
4642 17 : if (gfc_in_match_data())
4643 : {
4644 4 : gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4645 : sym->name);
4646 4 : return MATCH_ERROR;
4647 : }
4648 : /* Otherwise this is checked for an error given in the
4649 : variable definition context checks. */
4650 : break;
4651 :
4652 14554 : case FL_PROCEDURE:
4653 : /* Check for a nonrecursive function result variable. */
4654 14554 : if (sym->attr.function
4655 12465 : && (!sym->attr.external || sym->abr_modproc_decl)
4656 12068 : && sym->result == sym
4657 26269 : && (gfc_is_function_return_value (sym, gfc_current_ns)
4658 2201 : || (sym->attr.entry
4659 499 : && sym->ns == gfc_current_ns)
4660 1709 : || (sym->attr.entry
4661 7 : && sym->ns == gfc_current_ns->parent)))
4662 : {
4663 : /* If a function result is a derived type, then the derived
4664 : type may still have to be resolved. */
4665 :
4666 10013 : if (sym->ts.type == BT_DERIVED
4667 10013 : && gfc_use_derived (sym->ts.u.derived) == NULL)
4668 : return MATCH_ERROR;
4669 : break;
4670 : }
4671 :
4672 4541 : if (sym->attr.proc_pointer
4673 4541 : || replace_hidden_procptr_result (&sym, &st))
4674 : break;
4675 :
4676 : /* Fall through to error */
4677 2872 : gcc_fallthrough ();
4678 :
4679 2872 : default:
4680 2872 : gfc_error ("%qs at %C is not a variable", sym->name);
4681 2872 : return MATCH_ERROR;
4682 : }
4683 :
4684 : /* Special case for derived type variables that get their types
4685 : via an IMPLICIT statement. This can't wait for the
4686 : resolution phase. */
4687 :
4688 2904980 : {
4689 2904980 : gfc_namespace * implicit_ns;
4690 :
4691 2904980 : if (gfc_current_ns->proc_name == sym)
4692 : implicit_ns = gfc_current_ns;
4693 : else
4694 2895844 : implicit_ns = sym->ns;
4695 :
4696 2904980 : old_loc = gfc_current_locus;
4697 2904980 : if (gfc_match_member_sep (sym) == MATCH_YES
4698 21932 : && sym->ts.type == BT_UNKNOWN
4699 2904992 : && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4700 3 : gfc_set_default_type (sym, 0, implicit_ns);
4701 2904980 : gfc_current_locus = old_loc;
4702 : }
4703 :
4704 2904980 : expr = gfc_get_expr ();
4705 :
4706 2904980 : expr->expr_type = EXPR_VARIABLE;
4707 2904980 : expr->symtree = st;
4708 2904980 : expr->ts = sym->ts;
4709 :
4710 : /* Now see if we have to do more. */
4711 2904980 : m = gfc_match_varspec (expr, equiv_flag, false, false);
4712 2904980 : if (m != MATCH_YES)
4713 : {
4714 83 : gfc_free_expr (expr);
4715 83 : return m;
4716 : }
4717 :
4718 2904897 : expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4719 2904897 : *result = expr;
4720 2904897 : return MATCH_YES;
4721 : }
4722 :
4723 :
4724 : match
4725 2904950 : gfc_match_variable (gfc_expr **result, int equiv_flag)
4726 : {
4727 2904950 : return match_variable (result, equiv_flag, 1);
4728 : }
4729 :
4730 :
4731 : match
4732 2947 : gfc_match_equiv_variable (gfc_expr **result)
4733 : {
4734 2947 : return match_variable (result, 1, 0);
4735 : }
|