Branch data Line data Source code
1 : : /* Primary expression subroutines
2 : : Copyright (C) 2000-2025 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 : 469332 : match_kind_param (int *kind, int *is_iso_c)
41 : : {
42 : 469332 : char name[GFC_MAX_SYMBOL_LEN + 1];
43 : 469332 : gfc_symbol *sym;
44 : 469332 : match m;
45 : :
46 : 469332 : *is_iso_c = 0;
47 : :
48 : 469332 : m = gfc_match_small_literal_int (kind, NULL, false);
49 : 469332 : if (m != MATCH_NO)
50 : : return m;
51 : :
52 : 94013 : m = gfc_match_name (name, false);
53 : 94013 : if (m != MATCH_YES)
54 : : return m;
55 : :
56 : 92281 : if (gfc_find_symbol (name, NULL, 1, &sym))
57 : : return MATCH_ERROR;
58 : :
59 : 92281 : if (sym == NULL)
60 : : return MATCH_NO;
61 : :
62 : 92280 : *is_iso_c = sym->attr.is_iso_c;
63 : :
64 : 92280 : if (sym->attr.flavor != FL_PARAMETER)
65 : : return MATCH_NO;
66 : :
67 : 92280 : if (sym->value == NULL)
68 : : return MATCH_NO;
69 : :
70 : 92279 : if (gfc_extract_int (sym->value, kind))
71 : : return MATCH_NO;
72 : :
73 : 92279 : gfc_set_sym_referenced (sym);
74 : :
75 : 92279 : if (*kind < 0)
76 : : return MATCH_NO;
77 : :
78 : : return MATCH_YES;
79 : : }
80 : :
81 : :
82 : : /* Get a trailing kind-specification for non-character variables.
83 : : Returns:
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 : 4472906 : get_kind (int *is_iso_c)
92 : : {
93 : 4472906 : int kind;
94 : 4472906 : match m;
95 : :
96 : 4472906 : *is_iso_c = 0;
97 : :
98 : 4472906 : if (gfc_match_char ('_', false) != MATCH_YES)
99 : : return -2;
100 : :
101 : 469332 : m = match_kind_param (&kind, is_iso_c);
102 : 469332 : if (m == MATCH_NO)
103 : 1734 : gfc_error ("Missing kind-parameter at %C");
104 : :
105 : 469332 : 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 : 30197199 : gfc_check_digit (char c, int radix)
114 : : {
115 : 30197199 : bool r;
116 : :
117 : 30197199 : 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 : 30097698 : case 10:
128 : 30097698 : r = ('0' <= c && c <= '9');
129 : 30097698 : break;
130 : :
131 : 64681 : case 16:
132 : 64681 : r = ISXDIGIT (c);
133 : 64681 : break;
134 : :
135 : 0 : default:
136 : 0 : gfc_internal_error ("gfc_check_digit(): bad radix");
137 : : }
138 : :
139 : 30197199 : 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 : 17612714 : match_digits (int signflag, int radix, char *buffer)
150 : : {
151 : 17612714 : locus old_loc;
152 : 17612714 : int length;
153 : 17612714 : char c;
154 : :
155 : 17612714 : length = 0;
156 : 17612714 : c = gfc_next_ascii_char ();
157 : :
158 : 17612714 : if (signflag && (c == '+' || c == '-'))
159 : : {
160 : 4762 : if (buffer != NULL)
161 : 1863 : *buffer++ = c;
162 : 4762 : gfc_gobble_whitespace ();
163 : 4762 : c = gfc_next_ascii_char ();
164 : 4762 : length++;
165 : : }
166 : :
167 : 17612714 : if (!gfc_check_digit (c, radix))
168 : : return -1;
169 : :
170 : 8431794 : length++;
171 : 8431794 : if (buffer != NULL)
172 : 4207740 : *buffer++ = c;
173 : :
174 : 16686712 : for (;;)
175 : : {
176 : 12559253 : old_loc = gfc_current_locus;
177 : 12559253 : c = gfc_next_ascii_char ();
178 : :
179 : 12559253 : if (!gfc_check_digit (c, radix))
180 : : break;
181 : :
182 : 4127459 : if (buffer != NULL)
183 : 2061458 : *buffer++ = c;
184 : 4127459 : length++;
185 : : }
186 : :
187 : 8431794 : gfc_current_locus = old_loc;
188 : :
189 : 8431794 : return length;
190 : : }
191 : :
192 : : /* Convert an integer string to an expression node. */
193 : :
194 : : static gfc_expr *
195 : 4100489 : convert_integer (const char *buffer, int kind, int radix, locus *where)
196 : : {
197 : 4100489 : gfc_expr *e;
198 : 4100489 : const char *t;
199 : :
200 : 4100489 : e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 : : /* A leading plus is allowed, but not by mpz_set_str. */
202 : 4100489 : if (buffer[0] == '+')
203 : 21 : t = buffer + 1;
204 : : else
205 : : t = buffer;
206 : 4100489 : mpz_set_str (e->value.integer, t, radix);
207 : :
208 : 4100489 : 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 : 213029 : convert_real (const char *buffer, int kind, locus *where)
254 : : {
255 : 213029 : gfc_expr *e;
256 : :
257 : 213029 : e = gfc_get_constant_expr (BT_REAL, kind, where);
258 : 213029 : mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259 : :
260 : 213029 : 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 : 6612 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 : : {
270 : 6612 : gfc_expr *e;
271 : :
272 : 6612 : e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 : 6612 : mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 : : GFC_MPC_RND_MODE);
275 : :
276 : 6612 : 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 : 12811832 : match_integer_constant (gfc_expr **result, int signflag)
285 : : {
286 : 12811832 : int length, kind, is_iso_c;
287 : 12811832 : locus old_loc;
288 : 12811832 : char *buffer;
289 : 12811832 : gfc_expr *e;
290 : :
291 : 12811832 : old_loc = gfc_current_locus;
292 : 12811832 : gfc_gobble_whitespace ();
293 : :
294 : 12811832 : length = match_digits (signflag, 10, NULL);
295 : 12811832 : gfc_current_locus = old_loc;
296 : 12811832 : if (length == -1)
297 : : return MATCH_NO;
298 : :
299 : 4102223 : buffer = (char *) alloca (length + 1);
300 : 4102223 : memset (buffer, '\0', length + 1);
301 : :
302 : 4102223 : gfc_gobble_whitespace ();
303 : :
304 : 4102223 : match_digits (signflag, 10, buffer);
305 : :
306 : 4102223 : kind = get_kind (&is_iso_c);
307 : 4102223 : if (kind == -2)
308 : 3796716 : kind = gfc_default_integer_kind;
309 : 4102223 : if (kind == -1)
310 : : return MATCH_ERROR;
311 : :
312 : 4100493 : if (kind == 4 && flag_integer4_kind == 8)
313 : 0 : kind = 8;
314 : :
315 : 4100493 : 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 : 4100489 : e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 : 4100489 : e->ts.is_c_interop = is_iso_c;
323 : :
324 : 4100489 : 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 : 4090903 : *result = e;
334 : 4090903 : 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 : 6412424 : match_hollerith_constant (gfc_expr **result)
407 : : {
408 : 6412424 : locus old_loc;
409 : 6412424 : gfc_expr *e = NULL;
410 : 6412424 : int num, pad;
411 : 6412424 : int i;
412 : :
413 : 6412424 : old_loc = gfc_current_locus;
414 : 6412424 : gfc_gobble_whitespace ();
415 : :
416 : 6412424 : if (match_integer_constant (&e, 0) == MATCH_YES
417 : 6412424 : && 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 : 6409788 : gfc_free_expr (e);
474 : 6409788 : gfc_current_locus = old_loc;
475 : 6409788 : 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 : 6616205 : match_boz_constant (gfc_expr **result)
491 : : {
492 : 6616205 : int radix, length, x_hex;
493 : 6616205 : locus old_loc, start_loc;
494 : 6616205 : char *buffer, post, delim;
495 : 6616205 : gfc_expr *e;
496 : :
497 : 6616205 : start_loc = old_loc = gfc_current_locus;
498 : 6616205 : gfc_gobble_whitespace ();
499 : :
500 : 6616205 : x_hex = 0;
501 : 6616205 : switch (post = gfc_next_ascii_char ())
502 : : {
503 : : case 'b':
504 : : radix = 2;
505 : : post = 0;
506 : : break;
507 : 53498 : case 'o':
508 : 53498 : radix = 8;
509 : 53498 : post = 0;
510 : 53498 : break;
511 : 89469 : case 'x':
512 : 89469 : 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 : 6336596 : default:
526 : 6336596 : goto backup;
527 : : }
528 : :
529 : : /* No whitespace allowed here. */
530 : :
531 : 53498 : if (post == 0)
532 : 279584 : delim = gfc_next_ascii_char ();
533 : :
534 : 279609 : if (delim != '\'' && delim != '\"')
535 : 275461 : goto backup;
536 : :
537 : 4148 : if (x_hex
538 : 4148 : && gfc_invalid_boz (G_("Hexadecimal constant at %L uses "
539 : : "nonstandard X instead of Z"), &gfc_current_locus))
540 : : return MATCH_ERROR;
541 : :
542 : 4146 : old_loc = gfc_current_locus;
543 : :
544 : 4146 : length = match_digits (0, radix, NULL);
545 : 4146 : 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 : 4146 : 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 : 4146 : 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 : 4145 : gfc_current_locus = old_loc;
582 : :
583 : 4145 : buffer = (char *) alloca (length + 1);
584 : 4145 : memset (buffer, '\0', length + 1);
585 : :
586 : 4145 : match_digits (0, radix, buffer);
587 : 4145 : gfc_next_ascii_char (); /* Eat delimiter. */
588 : 4145 : if (post == 1)
589 : 24 : gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
590 : :
591 : 4145 : e = gfc_get_expr ();
592 : 4145 : e->expr_type = EXPR_CONSTANT;
593 : 4145 : e->ts.type = BT_BOZ;
594 : 4145 : e->where = gfc_current_locus;
595 : 4145 : e->boz.rdx = radix;
596 : 4145 : e->boz.len = length;
597 : 4145 : e->boz.str = XCNEWVEC (char, length + 1);
598 : 4145 : strncpy (e->boz.str, buffer, length);
599 : :
600 : 4145 : if (!gfc_in_match_data ()
601 : 4145 : && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
602 : : "statement at %L", &e->where)))
603 : : return MATCH_ERROR;
604 : :
605 : 4140 : *result = e;
606 : 4140 : return MATCH_YES;
607 : :
608 : 6612057 : backup:
609 : 6612057 : gfc_current_locus = start_loc;
610 : 6612057 : 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 : 6716459 : match_real_constant (gfc_expr **result, int signflag)
619 : : {
620 : 6716459 : int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 : 6716459 : locus old_loc, temp_loc;
622 : 6716459 : char *p, *buffer, c, exp_char;
623 : 6716459 : gfc_expr *e;
624 : 6716459 : bool negate;
625 : :
626 : 6716459 : old_loc = gfc_current_locus;
627 : 6716459 : gfc_gobble_whitespace ();
628 : :
629 : 6716459 : e = NULL;
630 : :
631 : 6716459 : default_exponent = 0;
632 : 6716459 : count = 0;
633 : 6716459 : seen_dp = 0;
634 : 6716459 : seen_digits = 0;
635 : 6716459 : exp_char = ' ';
636 : 6716459 : negate = false;
637 : :
638 : 6716459 : c = gfc_next_ascii_char ();
639 : 6716459 : if (signflag && (c == '+' || c == '-'))
640 : : {
641 : 5886 : if (c == '-')
642 : 5750 : negate = true;
643 : :
644 : 5886 : gfc_gobble_whitespace ();
645 : 5886 : c = gfc_next_ascii_char ();
646 : : }
647 : :
648 : : /* Scan significand. */
649 : 3842575 : for (;; c = gfc_next_ascii_char (), count++)
650 : : {
651 : 10559034 : if (c == '.')
652 : : {
653 : 273314 : if (seen_dp)
654 : 204 : goto done;
655 : :
656 : : /* Check to see if "." goes with a following operator like
657 : : ".eq.". */
658 : 273110 : temp_loc = gfc_current_locus;
659 : 273110 : c = gfc_next_ascii_char ();
660 : :
661 : 273110 : if (c == 'e' || c == 'd' || c == 'q')
662 : : {
663 : 17658 : c = gfc_next_ascii_char ();
664 : 17658 : if (c == '.')
665 : 0 : goto done; /* Operator named .e. or .d. */
666 : : }
667 : :
668 : 273110 : if (ISALPHA (c))
669 : 66550 : goto done; /* Distinguish 1.e9 from 1.eq.2 */
670 : :
671 : 206560 : gfc_current_locus = temp_loc;
672 : 206560 : seen_dp = 1;
673 : 206560 : continue;
674 : : }
675 : :
676 : 10285720 : if (ISDIGIT (c))
677 : : {
678 : 3636015 : seen_digits = 1;
679 : 3636015 : continue;
680 : : }
681 : :
682 : 6649705 : break;
683 : : }
684 : :
685 : 6649705 : if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 : 2280518 : goto done;
687 : 37150 : exp_char = c;
688 : :
689 : :
690 : 37150 : 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 : 37150 : c = gfc_next_ascii_char ();
703 : 37150 : count++;
704 : :
705 : 37150 : if (c == '+' || c == '-')
706 : : { /* optional sign */
707 : 6750 : c = gfc_next_ascii_char ();
708 : 6750 : count++;
709 : : }
710 : :
711 : 37150 : if (!ISDIGIT (c))
712 : : {
713 : : /* With -fdec, default exponent to 0 instead of complaining. */
714 : 40 : if (flag_dec)
715 : 37140 : 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 : 77071 : while (ISDIGIT (c))
724 : : {
725 : 39931 : c = gfc_next_ascii_char ();
726 : 39931 : count++;
727 : : }
728 : :
729 : 6716449 : done:
730 : : /* Check that we have a numeric constant. */
731 : 6716449 : if (!seen_digits || (!seen_dp && exp_char == ' '))
732 : : {
733 : 6503416 : gfc_current_locus = old_loc;
734 : 6503416 : return MATCH_NO;
735 : : }
736 : :
737 : : /* Convert the number. */
738 : 213033 : gfc_current_locus = old_loc;
739 : 213033 : gfc_gobble_whitespace ();
740 : :
741 : 213033 : buffer = (char *) alloca (count + default_exponent + 1);
742 : 213033 : memset (buffer, '\0', count + default_exponent + 1);
743 : :
744 : 213033 : p = buffer;
745 : 213033 : c = gfc_next_ascii_char ();
746 : 213033 : if (c == '+' || c == '-')
747 : : {
748 : 2987 : gfc_gobble_whitespace ();
749 : 2987 : c = gfc_next_ascii_char ();
750 : : }
751 : :
752 : : /* Hack for mpfr_set_str(). */
753 : 1380815 : for (;;)
754 : : {
755 : 796924 : if (c == 'd' || c == 'q')
756 : 29781 : *p = 'e';
757 : : else
758 : 767143 : *p = c;
759 : 796924 : p++;
760 : 796924 : if (--count == 0)
761 : : break;
762 : :
763 : 583891 : c = gfc_next_ascii_char ();
764 : : }
765 : 213033 : if (default_exponent)
766 : 30 : *p++ = '0';
767 : :
768 : 213033 : kind = get_kind (&is_iso_c);
769 : 213033 : if (kind == -1)
770 : 4 : goto cleanup;
771 : :
772 : 213029 : if (kind == 4)
773 : : {
774 : 20294 : if (flag_real4_kind == 8)
775 : 192 : kind = 8;
776 : 20294 : if (flag_real4_kind == 10)
777 : 192 : kind = 10;
778 : 20294 : if (flag_real4_kind == 16)
779 : 384 : kind = 16;
780 : : }
781 : 192735 : else if (kind == 8)
782 : : {
783 : 25975 : if (flag_real8_kind == 4)
784 : 192 : kind = 4;
785 : 25975 : if (flag_real8_kind == 10)
786 : 192 : kind = 10;
787 : 25975 : if (flag_real8_kind == 16)
788 : 384 : kind = 16;
789 : : }
790 : :
791 : 213029 : switch (exp_char)
792 : : {
793 : 29781 : case 'd':
794 : 29781 : 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 : 29781 : kind = gfc_default_double_kind;
801 : 29781 : 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 : 183248 : default:
828 : 183248 : if (kind == -2)
829 : 112931 : kind = gfc_default_real_kind;
830 : :
831 : 183248 : 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 : 213029 : e = convert_real (buffer, kind, &gfc_current_locus);
839 : 213029 : if (negate)
840 : 2882 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 : 213029 : e->ts.is_c_interop = is_iso_c;
842 : :
843 : 213029 : 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 : 213028 : 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 : 30 : 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 : 213028 : *result = e;
915 : 213028 : 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 : 587054 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 : : {
928 : 587054 : gfc_expr *start, *end;
929 : 587054 : locus old_loc;
930 : 587054 : gfc_ref *ref;
931 : 587054 : match m;
932 : :
933 : 587054 : start = NULL;
934 : 587054 : end = NULL;
935 : :
936 : 587054 : old_loc = gfc_current_locus;
937 : :
938 : 587054 : m = gfc_match_char ('(');
939 : 587054 : if (m != MATCH_YES)
940 : : return MATCH_NO;
941 : :
942 : 15146 : if (gfc_match_char (':') != MATCH_YES)
943 : : {
944 : 14268 : if (init)
945 : 0 : m = gfc_match_init_expr (&start);
946 : : else
947 : 14268 : m = gfc_match_expr (&start);
948 : :
949 : 14268 : if (m != MATCH_YES)
950 : : {
951 : 154 : m = MATCH_NO;
952 : 154 : goto cleanup;
953 : : }
954 : :
955 : 14114 : m = gfc_match_char (':');
956 : 14114 : if (m != MATCH_YES)
957 : 454 : goto cleanup;
958 : : }
959 : :
960 : 14538 : if (gfc_match_char (')') != MATCH_YES)
961 : : {
962 : 13669 : if (init)
963 : 0 : m = gfc_match_init_expr (&end);
964 : : else
965 : 13669 : m = gfc_match_expr (&end);
966 : :
967 : 13669 : if (m == MATCH_NO)
968 : 2 : goto syntax;
969 : 13667 : if (m == MATCH_ERROR)
970 : 0 : goto cleanup;
971 : :
972 : 13667 : m = gfc_match_char (')');
973 : 13667 : if (m == MATCH_NO)
974 : 3 : goto syntax;
975 : : }
976 : :
977 : : /* Optimize away the (:) reference. */
978 : 14533 : if (start == NULL && end == NULL && !deferred)
979 : : ref = NULL;
980 : : else
981 : : {
982 : 14328 : ref = gfc_get_ref ();
983 : :
984 : 14328 : ref->type = REF_SUBSTRING;
985 : 14328 : if (start == NULL)
986 : 671 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 : 14328 : ref->u.ss.start = start;
988 : 14328 : if (end == NULL && cl)
989 : 662 : end = gfc_copy_expr (cl->length);
990 : 14328 : ref->u.ss.end = end;
991 : 14328 : ref->u.ss.length = cl;
992 : : }
993 : :
994 : 14533 : *result = ref;
995 : 14533 : 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 : 613 : cleanup:
1002 : 613 : gfc_free_expr (start);
1003 : 613 : gfc_free_expr (end);
1004 : :
1005 : 613 : gfc_current_locus = old_loc;
1006 : 613 : 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 : 4048729 : next_string_char (gfc_char_t delimiter, int *ret)
1022 : : {
1023 : 4048729 : locus old_locus;
1024 : 4048729 : gfc_char_t c;
1025 : :
1026 : 4048729 : c = gfc_next_char_literal (INSTRING_WARN);
1027 : 4048729 : *ret = 0;
1028 : :
1029 : 4048729 : if (c == '\n')
1030 : : {
1031 : 4 : *ret = -2;
1032 : 4 : return 0;
1033 : : }
1034 : :
1035 : 4048725 : 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 : 4048725 : if (c != delimiter)
1047 : : return c;
1048 : :
1049 : 588176 : old_locus = gfc_current_locus;
1050 : 588176 : c = gfc_next_char_literal (NONSTRING);
1051 : :
1052 : 588176 : if (c == delimiter)
1053 : : return c;
1054 : 587358 : gfc_current_locus = old_locus;
1055 : :
1056 : 587358 : *ret = -1;
1057 : 587358 : 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 : 4322411 : match_charkind_name (char *name)
1075 : : {
1076 : 4322411 : locus old_loc;
1077 : 4322411 : char c, peek;
1078 : 4322411 : int len;
1079 : :
1080 : 4322411 : gfc_gobble_whitespace ();
1081 : 4322411 : c = gfc_next_ascii_char ();
1082 : 4322411 : if (!ISALPHA (c))
1083 : : return MATCH_NO;
1084 : :
1085 : 3930420 : *name++ = c;
1086 : 3930420 : len = 1;
1087 : :
1088 : 15908810 : for (;;)
1089 : : {
1090 : 15908810 : old_loc = gfc_current_locus;
1091 : 15908810 : c = gfc_next_ascii_char ();
1092 : :
1093 : 15908810 : if (c == '_')
1094 : : {
1095 : 511429 : peek = gfc_peek_ascii_char ();
1096 : :
1097 : 511429 : if (peek == '\'' || peek == '\"')
1098 : : {
1099 : 996 : gfc_current_locus = old_loc;
1100 : 996 : *name = '\0';
1101 : 996 : return MATCH_YES;
1102 : : }
1103 : : }
1104 : :
1105 : 15907814 : if (!ISALNUM (c)
1106 : 4439857 : && c != '_'
1107 : 3929424 : && (c != '$' || !flag_dollar_ok))
1108 : : break;
1109 : :
1110 : 11978390 : *name++ = c;
1111 : 11978390 : 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 : 6909878 : match_string_constant (gfc_expr **result)
1128 : : {
1129 : 6909878 : char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 : 6909878 : size_t length;
1131 : 6909878 : int kind,save_warn_ampersand, ret;
1132 : 6909878 : locus old_locus, start_locus;
1133 : 6909878 : gfc_symbol *sym;
1134 : 6909878 : gfc_expr *e;
1135 : 6909878 : match m;
1136 : 6909878 : gfc_char_t c, delimiter, *p;
1137 : :
1138 : 6909878 : old_locus = gfc_current_locus;
1139 : :
1140 : 6909878 : gfc_gobble_whitespace ();
1141 : :
1142 : 6909878 : c = gfc_next_char ();
1143 : 6909878 : if (c == '\'' || c == '"')
1144 : : {
1145 : 255572 : kind = gfc_default_character_kind;
1146 : 255572 : start_locus = gfc_current_locus;
1147 : 255572 : goto got_delim;
1148 : : }
1149 : :
1150 : 6654306 : if (gfc_wide_is_digit (c))
1151 : : {
1152 : 2331895 : kind = 0;
1153 : :
1154 : 5610781 : while (gfc_wide_is_digit (c))
1155 : : {
1156 : 3292327 : kind = kind * 10 + c - '0';
1157 : 3292327 : if (kind > 9999999)
1158 : 13441 : goto no_match;
1159 : 3278886 : c = gfc_next_char ();
1160 : : }
1161 : :
1162 : : }
1163 : : else
1164 : : {
1165 : 4322411 : gfc_current_locus = old_locus;
1166 : :
1167 : 4322411 : m = match_charkind_name (name);
1168 : 4322411 : if (m != MATCH_YES)
1169 : 4321415 : 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 : 2319449 : if (c != '_')
1181 : 2133307 : goto no_match;
1182 : :
1183 : 186142 : c = gfc_next_char ();
1184 : 186142 : if (c != '\'' && c != '"')
1185 : 148016 : goto no_match;
1186 : :
1187 : 38126 : start_locus = gfc_current_locus;
1188 : :
1189 : 38126 : 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 : 38126 : 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 : 38126 : 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 : 293698 : delimiter = c;
1209 : 293698 : length = 0;
1210 : :
1211 : 3755374 : for (;;)
1212 : : {
1213 : 2024536 : c = next_string_char (delimiter, &ret);
1214 : 2024536 : if (ret == -1)
1215 : : break;
1216 : 1730842 : 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 : 1730838 : 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 : 293694 : peek = gfc_peek_ascii_char ();
1229 : 293694 : if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 : 25 : goto no_match;
1231 : :
1232 : 293669 : e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233 : :
1234 : 293669 : 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 : 293669 : save_warn_ampersand = warn_ampersand;
1239 : 293669 : warn_ampersand = false;
1240 : :
1241 : 293669 : p = e->value.character.string;
1242 : 2024193 : for (size_t i = 0; i < length; i++)
1243 : : {
1244 : 1730529 : c = next_string_char (delimiter, &ret);
1245 : :
1246 : 1730529 : 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 : 1730524 : *p++ = c;
1255 : : }
1256 : :
1257 : 293664 : *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 : 293664 : warn_ampersand = save_warn_ampersand;
1259 : :
1260 : 293664 : next_string_char (delimiter, &ret);
1261 : 293664 : if (ret != -1)
1262 : 0 : gfc_internal_error ("match_string_constant(): Delimiter not found");
1263 : :
1264 : 293664 : if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
1265 : 306 : 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 : 293664 : if (e->expr_type == EXPR_SUBSTRING
1271 : 306 : && e->ref && e->ref->type == REF_SUBSTRING
1272 : 302 : && 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 : 293658 : *result = e;
1323 : :
1324 : 293658 : return MATCH_YES;
1325 : :
1326 : 6616205 : no_match:
1327 : 6616205 : gfc_current_locus = old_locus;
1328 : 6616205 : 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 : 4316247 : match_logical_constant_string (void)
1336 : : {
1337 : 4316247 : locus orig_loc = gfc_current_locus;
1338 : :
1339 : 4316247 : gfc_gobble_whitespace ();
1340 : 4316247 : if (gfc_next_ascii_char () == '.')
1341 : : {
1342 : 56279 : char ch = gfc_next_ascii_char ();
1343 : 56279 : if (ch == 'f')
1344 : : {
1345 : 28712 : if (gfc_next_ascii_char () == 'a'
1346 : 28712 : && gfc_next_ascii_char () == 'l'
1347 : 28712 : && gfc_next_ascii_char () == 's'
1348 : 28712 : && gfc_next_ascii_char () == 'e'
1349 : 57424 : && gfc_next_ascii_char () == '.')
1350 : : /* Matched ".false.". */
1351 : : return 0;
1352 : : }
1353 : 27567 : else if (ch == 't')
1354 : : {
1355 : 27566 : if (gfc_next_ascii_char () == 'r'
1356 : 27566 : && gfc_next_ascii_char () == 'u'
1357 : 27566 : && gfc_next_ascii_char () == 'e'
1358 : 55132 : && gfc_next_ascii_char () == '.')
1359 : : /* Matched ".true.". */
1360 : : return 1;
1361 : : }
1362 : : }
1363 : 4259969 : gfc_current_locus = orig_loc;
1364 : 4259969 : return -1;
1365 : : }
1366 : :
1367 : : /* Match a .true. or .false. */
1368 : :
1369 : : static match
1370 : 4316247 : match_logical_constant (gfc_expr **result)
1371 : : {
1372 : 4316247 : gfc_expr *e;
1373 : 4316247 : int i, kind, is_iso_c;
1374 : :
1375 : 4316247 : i = match_logical_constant_string ();
1376 : 4316247 : if (i == -1)
1377 : : return MATCH_NO;
1378 : :
1379 : 56278 : kind = get_kind (&is_iso_c);
1380 : 56278 : if (kind == -1)
1381 : : return MATCH_ERROR;
1382 : 56278 : if (kind == -2)
1383 : 55789 : kind = gfc_default_logical_kind;
1384 : :
1385 : 56278 : 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 : 56274 : e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 : 56274 : e->ts.is_c_interop = is_iso_c;
1393 : :
1394 : 56274 : *result = e;
1395 : 56274 : 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 : 136367 : match_sym_complex_part (gfc_expr **result)
1404 : : {
1405 : 136367 : char name[GFC_MAX_SYMBOL_LEN + 1];
1406 : 136367 : gfc_symbol *sym;
1407 : 136367 : gfc_expr *e;
1408 : 136367 : match m;
1409 : :
1410 : 136367 : m = gfc_match_name (name);
1411 : 136367 : if (m != MATCH_YES)
1412 : : return m;
1413 : :
1414 : 37209 : if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 : : return MATCH_NO;
1416 : :
1417 : 34575 : 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 : 33125 : char c;
1423 : 33125 : gfc_gobble_whitespace ();
1424 : 33125 : c = gfc_peek_ascii_char ();
1425 : 33125 : if (c == '=' || c == ',')
1426 : : {
1427 : : m = MATCH_NO;
1428 : : }
1429 : : else
1430 : : {
1431 : 30515 : gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 : 30515 : m = MATCH_ERROR;
1433 : : }
1434 : 33125 : return m;
1435 : : }
1436 : :
1437 : 1450 : if (!sym->value)
1438 : 2 : goto error;
1439 : :
1440 : 1448 : if (!gfc_numeric_ts (&sym->value->ts))
1441 : : {
1442 : 331 : gfc_error ("Numeric PARAMETER required in complex constant at %C");
1443 : 331 : return MATCH_ERROR;
1444 : : }
1445 : :
1446 : 1117 : 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 : 943 : if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1453 : : "complex constant at %C"))
1454 : : return MATCH_ERROR;
1455 : :
1456 : 940 : 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 : 869 : case BT_INTEGER:
1469 : 869 : e = gfc_int2real (sym->value, gfc_default_real_kind);
1470 : 869 : 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 : 938 : *result = e; /* e is a scalar, real, constant expression. */
1482 : 938 : 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 : 136367 : match_complex_part (gfc_expr **result)
1494 : : {
1495 : 136367 : match m;
1496 : :
1497 : 136367 : m = match_sym_complex_part (result);
1498 : 136367 : if (m != MATCH_NO)
1499 : : return m;
1500 : :
1501 : 104402 : m = match_real_constant (result, 1);
1502 : 104402 : if (m != MATCH_NO)
1503 : : return m;
1504 : :
1505 : 90992 : return match_integer_constant (result, 1);
1506 : : }
1507 : :
1508 : :
1509 : : /* Try to match a complex constant. */
1510 : :
1511 : : static match
1512 : 6919750 : match_complex_constant (gfc_expr **result)
1513 : : {
1514 : 6919750 : gfc_expr *e, *real, *imag;
1515 : 6919750 : gfc_error_buffer old_error;
1516 : 6919750 : gfc_typespec target;
1517 : 6919750 : locus old_loc;
1518 : 6919750 : int kind;
1519 : 6919750 : match m;
1520 : :
1521 : 6919750 : old_loc = gfc_current_locus;
1522 : 6919750 : real = imag = e = NULL;
1523 : :
1524 : 6919750 : m = gfc_match_char ('(');
1525 : 6919750 : if (m != MATCH_YES)
1526 : : return m;
1527 : :
1528 : 126499 : gfc_push_error (&old_error);
1529 : :
1530 : 126499 : m = match_complex_part (&real);
1531 : 126499 : if (m == MATCH_NO)
1532 : : {
1533 : 73927 : gfc_free_error (&old_error);
1534 : 73927 : goto cleanup;
1535 : : }
1536 : :
1537 : 52572 : 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 : 42700 : gfc_clear_warning ();
1543 : 42700 : gfc_pop_error (&old_error);
1544 : 42700 : m = MATCH_NO;
1545 : 42700 : 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 : 9872 : if (m == MATCH_ERROR)
1554 : : {
1555 : 4 : gfc_free_error (&old_error);
1556 : 4 : goto cleanup;
1557 : : }
1558 : 9868 : gfc_pop_error (&old_error);
1559 : :
1560 : 9868 : m = match_complex_part (&imag);
1561 : 9868 : if (m == MATCH_NO)
1562 : 3110 : goto syntax;
1563 : 6758 : if (m == MATCH_ERROR)
1564 : 133 : goto cleanup;
1565 : :
1566 : 6625 : m = gfc_match_char (')');
1567 : 6625 : 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 : 6612 : if (m == MATCH_ERROR)
1581 : 0 : goto cleanup;
1582 : :
1583 : : /* Decide on the kind of this complex number. */
1584 : 6612 : if (real->ts.type == BT_REAL)
1585 : : {
1586 : 6197 : if (imag->ts.type == BT_REAL)
1587 : 6172 : kind = gfc_kind_max (real, imag);
1588 : : else
1589 : 25 : kind = real->ts.kind;
1590 : : }
1591 : : else
1592 : : {
1593 : 415 : if (imag->ts.type == BT_REAL)
1594 : 7 : kind = imag->ts.kind;
1595 : : else
1596 : 408 : kind = gfc_default_real_kind;
1597 : : }
1598 : 6612 : gfc_clear_ts (&target);
1599 : 6612 : target.type = BT_REAL;
1600 : 6612 : target.kind = kind;
1601 : :
1602 : 6612 : if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 : 416 : gfc_convert_type (real, &target, 2);
1604 : 6612 : if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 : 471 : gfc_convert_type (imag, &target, 2);
1606 : :
1607 : 6612 : e = convert_complex (real, imag, kind);
1608 : 6612 : e->where = gfc_current_locus;
1609 : :
1610 : 6612 : gfc_free_expr (real);
1611 : 6612 : gfc_free_expr (imag);
1612 : :
1613 : 6612 : *result = e;
1614 : 6612 : return MATCH_YES;
1615 : :
1616 : 3123 : syntax:
1617 : 3123 : gfc_error ("Syntax error in COMPLEX constant at %C");
1618 : 3123 : m = MATCH_ERROR;
1619 : :
1620 : 119887 : cleanup:
1621 : 119887 : gfc_free_expr (e);
1622 : 119887 : gfc_free_expr (real);
1623 : 119887 : gfc_free_expr (imag);
1624 : 119887 : gfc_current_locus = old_loc;
1625 : :
1626 : 119887 : return m;
1627 : 6919750 : }
1628 : :
1629 : :
1630 : : /* Match constants in any of several forms. Returns nonzero for a
1631 : : match, zero for no match. */
1632 : :
1633 : : match
1634 : 6919750 : gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 : : {
1636 : 6919750 : match m;
1637 : :
1638 : 6919750 : m = match_complex_constant (result);
1639 : 6919750 : if (m != MATCH_NO)
1640 : : return m;
1641 : :
1642 : 6909878 : m = match_string_constant (result);
1643 : 6909878 : if (m != MATCH_NO)
1644 : : return m;
1645 : :
1646 : 6616205 : m = match_boz_constant (result);
1647 : 6616205 : if (m != MATCH_NO)
1648 : : return m;
1649 : :
1650 : 6612057 : m = match_real_constant (result, signflag);
1651 : 6612057 : if (m != MATCH_NO)
1652 : : return m;
1653 : :
1654 : 6412424 : m = match_hollerith_constant (result);
1655 : 6412424 : if (m != MATCH_NO)
1656 : : return m;
1657 : :
1658 : 6409788 : if (flag_unsigned)
1659 : : {
1660 : 588996 : m = match_unsigned_constant (result);
1661 : 588996 : if (m != MATCH_NO)
1662 : : return m;
1663 : : }
1664 : :
1665 : 6308416 : m = match_integer_constant (result, signflag);
1666 : 6308416 : if (m != MATCH_NO)
1667 : : return m;
1668 : :
1669 : 4316247 : m = match_logical_constant (result);
1670 : 4316247 : 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 : 762182 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 : : {
1684 : 762182 : if (!sym->attr.function || (sym->result != sym))
1685 : : return false;
1686 : 1598531 : while (ns)
1687 : : {
1688 : 905427 : if (ns->proc_name == sym)
1689 : : return true;
1690 : 893992 : 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 : 1971037 : match_actual_arg (gfc_expr **result)
1705 : : {
1706 : 1971037 : char name[GFC_MAX_SYMBOL_LEN + 1];
1707 : 1971037 : gfc_symtree *symtree;
1708 : 1971037 : locus where, w;
1709 : 1971037 : gfc_expr *e;
1710 : 1971037 : char c;
1711 : :
1712 : 1971037 : gfc_gobble_whitespace ();
1713 : 1971037 : where = gfc_current_locus;
1714 : :
1715 : 1971037 : switch (gfc_match_name (name))
1716 : : {
1717 : : case MATCH_ERROR:
1718 : : return MATCH_ERROR;
1719 : :
1720 : : case MATCH_NO:
1721 : : break;
1722 : :
1723 : 1284654 : case MATCH_YES:
1724 : 1284654 : w = gfc_current_locus;
1725 : 1284654 : gfc_gobble_whitespace ();
1726 : 1284654 : c = gfc_next_ascii_char ();
1727 : 1284654 : gfc_current_locus = w;
1728 : :
1729 : 1284654 : if (c != ',' && c != ')')
1730 : : break;
1731 : :
1732 : 677503 : 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 : 677503 : if (symtree == NULL)
1739 : : {
1740 : 13916 : gfc_get_sym_tree (name, NULL, &symtree, false);
1741 : 13916 : gfc_set_sym_referenced (symtree->n.sym);
1742 : : }
1743 : : else
1744 : : {
1745 : 663587 : gfc_symbol *sym;
1746 : :
1747 : 663587 : sym = symtree->n.sym;
1748 : 663587 : gfc_set_sym_referenced (sym);
1749 : 663587 : if (sym->attr.flavor == FL_NAMELIST)
1750 : : {
1751 : 1091 : gfc_error ("Namelist %qs cannot be an argument at %L",
1752 : : sym->name, &where);
1753 : 1091 : break;
1754 : : }
1755 : 662496 : if (sym->attr.flavor != FL_PROCEDURE
1756 : 626836 : && sym->attr.flavor != FL_UNKNOWN)
1757 : : break;
1758 : :
1759 : 185556 : 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 : 185332 : if (sym->attr.function && sym->result == sym)
1770 : : {
1771 : 3160 : if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 : : break;
1773 : :
1774 : 2514 : 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 : 198548 : e = gfc_get_expr (); /* Leave it unknown for now */
1791 : 198548 : e->symtree = symtree;
1792 : 198548 : e->expr_type = EXPR_VARIABLE;
1793 : 198548 : e->ts.type = BT_PROCEDURE;
1794 : 198548 : e->where = where;
1795 : :
1796 : 198548 : *result = e;
1797 : 198548 : return MATCH_YES;
1798 : : }
1799 : :
1800 : 1772489 : gfc_current_locus = where;
1801 : 1772489 : return gfc_match_expr (result);
1802 : : }
1803 : :
1804 : :
1805 : : /* Match a keyword argument or type parameter spec list.. */
1806 : :
1807 : : static match
1808 : 1962927 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 : : {
1810 : 1962927 : char name[GFC_MAX_SYMBOL_LEN + 1];
1811 : 1962927 : gfc_actual_arglist *a;
1812 : 1962927 : locus name_locus;
1813 : 1962927 : match m;
1814 : :
1815 : 1962927 : name_locus = gfc_current_locus;
1816 : 1962927 : m = gfc_match_name (name);
1817 : :
1818 : 1962927 : if (m != MATCH_YES)
1819 : 579013 : goto cleanup;
1820 : 1383914 : if (gfc_match_char ('=') != MATCH_YES)
1821 : : {
1822 : 1226970 : m = MATCH_NO;
1823 : 1226970 : goto cleanup;
1824 : : }
1825 : :
1826 : 156944 : if (pdt)
1827 : : {
1828 : 328 : if (gfc_match_char ('*') == MATCH_YES)
1829 : : {
1830 : 78 : actual->spec_type = SPEC_ASSUMED;
1831 : 78 : goto add_name;
1832 : : }
1833 : 250 : else if (gfc_match_char (':') == MATCH_YES)
1834 : : {
1835 : 50 : actual->spec_type = SPEC_DEFERRED;
1836 : 50 : goto add_name;
1837 : : }
1838 : : else
1839 : 200 : actual->spec_type = SPEC_EXPLICIT;
1840 : : }
1841 : :
1842 : 156816 : m = match_actual_arg (&actual->expr);
1843 : 156815 : if (m != MATCH_YES)
1844 : 10789 : goto cleanup;
1845 : :
1846 : : /* Make sure this name has not appeared yet. */
1847 : 146026 : add_name:
1848 : 146154 : if (name[0] != '\0')
1849 : : {
1850 : 470205 : for (a = base; a; a = a->next)
1851 : 324065 : 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 : 146140 : actual->name = gfc_get_string ("%s", name);
1860 : 146140 : return MATCH_YES;
1861 : :
1862 : 1816772 : cleanup:
1863 : 1816772 : gfc_current_locus = name_locus;
1864 : 1816772 : return m;
1865 : : }
1866 : :
1867 : :
1868 : : /* Match an argument list function, such as %VAL. */
1869 : :
1870 : : static match
1871 : 1924954 : match_arg_list_function (gfc_actual_arglist *result)
1872 : : {
1873 : 1924954 : char name[GFC_MAX_SYMBOL_LEN + 1];
1874 : 1924954 : locus old_locus;
1875 : 1924954 : match m;
1876 : :
1877 : 1924954 : old_locus = gfc_current_locus;
1878 : :
1879 : 1924954 : if (gfc_match_char ('%') != MATCH_YES)
1880 : : {
1881 : 1924889 : m = MATCH_NO;
1882 : 1924889 : 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 : 1924890 : cleanup:
1939 : 1924890 : gfc_current_locus = old_locus;
1940 : 1924890 : 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 : 2025260 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 : : {
1959 : 2025260 : gfc_actual_arglist *head, *tail;
1960 : 2025260 : int seen_keyword;
1961 : 2025260 : gfc_st_label *label;
1962 : 2025260 : locus old_loc;
1963 : 2025260 : match m;
1964 : :
1965 : 2025260 : *argp = tail = NULL;
1966 : 2025260 : old_loc = gfc_current_locus;
1967 : :
1968 : 2025260 : seen_keyword = 0;
1969 : :
1970 : 2025260 : if (gfc_match_char ('(') == MATCH_NO)
1971 : 1191768 : return (sub_flag) ? MATCH_YES : MATCH_NO;
1972 : :
1973 : 1413306 : if (gfc_match_char (')') == MATCH_YES)
1974 : : return MATCH_YES;
1975 : :
1976 : 1388054 : head = NULL;
1977 : :
1978 : 1388054 : matching_actual_arglist++;
1979 : :
1980 : 1962826 : for (;;)
1981 : : {
1982 : 1962826 : if (head == NULL)
1983 : 1388054 : head = tail = gfc_get_actual_arglist ();
1984 : : else
1985 : : {
1986 : 574772 : tail->next = gfc_get_actual_arglist ();
1987 : 574772 : tail = tail->next;
1988 : : }
1989 : :
1990 : 1962826 : 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 : 1962588 : if (pdt && !seen_keyword)
2007 : : {
2008 : 908 : if (gfc_match_char (':') == MATCH_YES)
2009 : : {
2010 : 52 : tail->spec_type = SPEC_DEFERRED;
2011 : 52 : goto next;
2012 : : }
2013 : 856 : else if (gfc_match_char ('*') == MATCH_YES)
2014 : : {
2015 : 92 : tail->spec_type = SPEC_ASSUMED;
2016 : 92 : goto next;
2017 : : }
2018 : : else
2019 : 764 : tail->spec_type = SPEC_EXPLICIT;
2020 : :
2021 : 764 : m = match_keyword_arg (tail, head, pdt);
2022 : 764 : if (m == MATCH_YES)
2023 : : {
2024 : 216 : seen_keyword = 1;
2025 : 216 : goto next;
2026 : : }
2027 : 548 : 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 : 1962228 : if (seen_keyword)
2034 : : {
2035 : 37274 : m = match_keyword_arg (tail, head, pdt);
2036 : :
2037 : 37274 : if (m == MATCH_ERROR)
2038 : 34 : goto cleanup;
2039 : 37240 : if (m == MATCH_NO)
2040 : : {
2041 : 1358 : gfc_error ("Missing keyword name in actual argument list at %C");
2042 : 1358 : goto cleanup;
2043 : : }
2044 : :
2045 : : }
2046 : : else
2047 : : {
2048 : : /* Try an argument list function, like %VAL. */
2049 : 1924954 : m = match_arg_list_function (tail);
2050 : 1924954 : if (m == MATCH_ERROR)
2051 : 1 : goto cleanup;
2052 : :
2053 : : /* See if we have the first keyword argument. */
2054 : 1924953 : if (m == MATCH_NO)
2055 : : {
2056 : 1924889 : m = match_keyword_arg (tail, head, false);
2057 : 1924888 : if (m == MATCH_YES)
2058 : : seen_keyword = 1;
2059 : 1814846 : if (m == MATCH_ERROR)
2060 : 689 : goto cleanup;
2061 : : }
2062 : :
2063 : 1924199 : if (m == MATCH_NO)
2064 : : {
2065 : : /* Try for a non-keyword argument. */
2066 : 1814157 : m = match_actual_arg (&tail->expr);
2067 : 1814138 : if (m == MATCH_ERROR)
2068 : 1875 : goto cleanup;
2069 : 1812263 : if (m == MATCH_NO)
2070 : 19486 : goto syntax;
2071 : : }
2072 : : }
2073 : :
2074 : :
2075 : 110042 : next:
2076 : 1939363 : if (gfc_match_char (')') == MATCH_YES)
2077 : : break;
2078 : 582961 : if (gfc_match_char (',') != MATCH_YES)
2079 : 8189 : goto syntax;
2080 : : }
2081 : :
2082 : 1356402 : *argp = head;
2083 : 1356402 : matching_actual_arglist--;
2084 : 1356402 : return MATCH_YES;
2085 : :
2086 : 27675 : syntax:
2087 : 27675 : gfc_error ("Syntax error in argument list at %C");
2088 : :
2089 : 31632 : cleanup:
2090 : 31632 : gfc_free_actual_arglist (head);
2091 : 31632 : gfc_current_locus = old_loc;
2092 : 31632 : matching_actual_arglist--;
2093 : 31632 : return MATCH_ERROR;
2094 : : }
2095 : :
2096 : :
2097 : : /* Used by gfc_match_varspec() to extend the reference list by one
2098 : : element. */
2099 : :
2100 : : static gfc_ref *
2101 : 713884 : extend_ref (gfc_expr *primary, gfc_ref *tail)
2102 : : {
2103 : 713884 : if (primary->ref == NULL)
2104 : 654948 : primary->ref = tail = gfc_get_ref ();
2105 : 58936 : else if (tail == NULL)
2106 : : {
2107 : : /* Set tail to end of reference chain. */
2108 : 23 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2109 : 23 : if (ref->next == NULL)
2110 : : {
2111 : : tail = ref;
2112 : : break;
2113 : : }
2114 : : }
2115 : : else
2116 : : {
2117 : 58922 : tail->next = gfc_get_ref ();
2118 : 58922 : tail = tail->next;
2119 : : }
2120 : :
2121 : 713884 : return tail;
2122 : : }
2123 : :
2124 : :
2125 : : /* Used by gfc_match_varspec() to match an inquiry reference. */
2126 : :
2127 : : bool
2128 : 3806 : is_inquiry_ref (const char *name, gfc_ref **ref)
2129 : : {
2130 : 3806 : inquiry_type type;
2131 : :
2132 : 3806 : if (name == NULL)
2133 : : return false;
2134 : :
2135 : 3806 : if (ref) *ref = NULL;
2136 : :
2137 : 3806 : if (strcmp (name, "re") == 0)
2138 : : type = INQUIRY_RE;
2139 : 2745 : else if (strcmp (name, "im") == 0)
2140 : : type = INQUIRY_IM;
2141 : 1863 : else if (strcmp (name, "kind") == 0)
2142 : : type = INQUIRY_KIND;
2143 : 1360 : else if (strcmp (name, "len") == 0)
2144 : : type = INQUIRY_LEN;
2145 : : else
2146 : : return false;
2147 : :
2148 : 2888 : if (ref)
2149 : : {
2150 : 1649 : *ref = gfc_get_ref ();
2151 : 1649 : (*ref)->type = REF_INQUIRY;
2152 : 1649 : (*ref)->u.i = type;
2153 : : }
2154 : :
2155 : : return true;
2156 : : }
2157 : :
2158 : :
2159 : : /* Check to see if functions in operator expressions can be resolved now. */
2160 : :
2161 : : static bool
2162 : 210 : resolvable_fcns (gfc_expr *e,
2163 : : gfc_symbol *sym ATTRIBUTE_UNUSED,
2164 : : int *f ATTRIBUTE_UNUSED)
2165 : : {
2166 : 210 : bool p;
2167 : 210 : gfc_symbol *s;
2168 : :
2169 : 210 : if (e->expr_type != EXPR_FUNCTION)
2170 : : return false;
2171 : :
2172 : 72 : s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2173 : 72 : p = s && (s->attr.use_assoc
2174 : 54 : || s->attr.host_assoc
2175 : 54 : || s->attr.if_source == IFSRC_DECL
2176 : 54 : || s->attr.proc == PROC_INTRINSIC
2177 : 24 : || gfc_is_intrinsic (s, 0, e->where));
2178 : 72 : return !p;
2179 : : }
2180 : :
2181 : :
2182 : : /* Match any additional specifications associated with the current
2183 : : variable like member references or substrings. If equiv_flag is
2184 : : set we only match stuff that is allowed inside an EQUIVALENCE
2185 : : statement. sub_flag tells whether we expect a type-bound procedure found
2186 : : to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2187 : : components, 'ppc_arg' determines whether the PPC may be called (with an
2188 : : argument list), or whether it may just be referred to as a pointer. */
2189 : :
2190 : : match
2191 : 4947204 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2192 : : bool ppc_arg)
2193 : : {
2194 : 4947204 : char name[GFC_MAX_SYMBOL_LEN + 1];
2195 : 4947204 : gfc_ref *substring, *tail, *tmp;
2196 : 4947204 : gfc_component *component = NULL;
2197 : 4947204 : gfc_component *previous = NULL;
2198 : 4947204 : gfc_symbol *sym = primary->symtree->n.sym;
2199 : 4947204 : gfc_expr *tgt_expr = NULL;
2200 : 4947204 : match m;
2201 : 4947204 : bool unknown;
2202 : 4947204 : bool inquiry;
2203 : 4947204 : bool intrinsic;
2204 : 4947204 : bool inferred_type;
2205 : 4947204 : locus old_loc;
2206 : 4947204 : char peeked_char;
2207 : :
2208 : 4947204 : tail = NULL;
2209 : :
2210 : 4947204 : gfc_gobble_whitespace ();
2211 : :
2212 : 4947204 : if (gfc_peek_ascii_char () == '[')
2213 : : {
2214 : 2849 : if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2215 : 2849 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2216 : 113 : && CLASS_DATA (sym)->attr.dimension))
2217 : : {
2218 : 0 : gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2219 : : "besides the coarray designator %<[...]%> at %C");
2220 : 0 : return MATCH_ERROR;
2221 : : }
2222 : 2849 : if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2223 : 2848 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2224 : 113 : && !CLASS_DATA (sym)->attr.codimension))
2225 : : {
2226 : 1 : gfc_error ("Coarray designator at %C but %qs is not a coarray",
2227 : : sym->name);
2228 : 1 : return MATCH_ERROR;
2229 : : }
2230 : : }
2231 : :
2232 : 4947203 : if (sym->assoc && sym->assoc->target)
2233 : 4947203 : tgt_expr = sym->assoc->target;
2234 : :
2235 : 4947203 : inferred_type = IS_INFERRED_TYPE (primary);
2236 : :
2237 : : /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
2238 : : been parsed, can generate errors with array refs.. The SELECT TYPE
2239 : : namespace is marked with 'assoc_name_inferred'. During resolution, this is
2240 : : detected and gfc_fixup_inferred_type_refs is called. */
2241 : 4946370 : if (!inferred_type
2242 : 4946370 : && sym->attr.select_type_temporary
2243 : 23228 : && sym->ns->assoc_name_inferred
2244 : 344 : && !sym->attr.select_rank_temporary)
2245 : 1177 : inferred_type = true;
2246 : :
2247 : : /* For associate names, we may not yet know whether they are arrays or not.
2248 : : If the selector expression is unambiguously an array; eg. a full array
2249 : : or an array section, then the associate name must be an array and we can
2250 : : fix it now. Otherwise, if parentheses follow and it is not a character
2251 : : type, we have to assume that it actually is one for now. The final
2252 : : decision will be made at resolution, of course. */
2253 : 4947203 : if (sym->assoc
2254 : 29060 : && gfc_peek_ascii_char () == '('
2255 : 9551 : && sym->ts.type != BT_CLASS
2256 : 4956613 : && !sym->attr.dimension)
2257 : : {
2258 : 386 : gfc_ref *ref = NULL;
2259 : :
2260 : 386 : if (!sym->assoc->dangling && tgt_expr)
2261 : : {
2262 : 326 : if (tgt_expr->expr_type == EXPR_VARIABLE)
2263 : 21 : gfc_resolve_expr (tgt_expr);
2264 : :
2265 : 326 : ref = tgt_expr->ref;
2266 : 340 : for (; ref; ref = ref->next)
2267 : 14 : if (ref->type == REF_ARRAY
2268 : 7 : && (ref->u.ar.type == AR_FULL
2269 : 7 : || ref->u.ar.type == AR_SECTION))
2270 : : break;
2271 : : }
2272 : :
2273 : 386 : if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2274 : 260 : && sym->assoc->st
2275 : 260 : && sym->assoc->st->n.sym
2276 : 260 : && sym->assoc->st->n.sym->attr.dimension == 0))
2277 : : {
2278 : 260 : sym->attr.dimension = 1;
2279 : 260 : if (sym->as == NULL
2280 : 260 : && sym->assoc->st
2281 : 260 : && sym->assoc->st->n.sym
2282 : 260 : && sym->assoc->st->n.sym->as)
2283 : 0 : sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2284 : : }
2285 : : }
2286 : 4946817 : else if (sym->ts.type == BT_CLASS
2287 : 42774 : && !(sym->assoc && sym->assoc->ar)
2288 : 42702 : && tgt_expr
2289 : 256 : && tgt_expr->expr_type == EXPR_VARIABLE
2290 : 130 : && sym->ts.u.derived != tgt_expr->ts.u.derived)
2291 : : {
2292 : 19 : gfc_resolve_expr (tgt_expr);
2293 : 19 : if (tgt_expr->rank)
2294 : 0 : sym->ts.u.derived = tgt_expr->ts.u.derived;
2295 : : }
2296 : :
2297 : 4947203 : peeked_char = gfc_peek_ascii_char ();
2298 : 1177 : if ((inferred_type && !sym->as && peeked_char == '(')
2299 : 4946982 : || (equiv_flag && peeked_char == '(') || peeked_char == '['
2300 : 4942563 : || sym->attr.codimension
2301 : 4928796 : || (sym->attr.dimension && sym->ts.type != BT_CLASS
2302 : 619993 : && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2303 : 619978 : && !(gfc_matching_procptr_assignment
2304 : 32 : && sym->attr.flavor == FL_PROCEDURE))
2305 : 9256041 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2306 : 42613 : && sym->ts.u.derived && CLASS_DATA (sym)
2307 : 42609 : && (CLASS_DATA (sym)->attr.dimension
2308 : 26424 : || CLASS_DATA (sym)->attr.codimension)))
2309 : : {
2310 : 654951 : gfc_array_spec *as;
2311 : 16502 : bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2312 : 663812 : && sym->ts.type == BT_CHARACTER;
2313 : 654951 : gfc_ref *ref, *strarr = NULL;
2314 : :
2315 : 654951 : tail = extend_ref (primary, tail);
2316 : 654951 : if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
2317 : : {
2318 : 3 : gcc_assert (sym->attr.dimension);
2319 : : /* Find array reference for substrings of character arrays. */
2320 : 3 : for (ref = primary->ref; ref && ref->next; ref = ref->next)
2321 : 3 : if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
2322 : : {
2323 : : strarr = ref;
2324 : : break;
2325 : : }
2326 : : }
2327 : : else
2328 : 654948 : tail->type = REF_ARRAY;
2329 : :
2330 : : /* In EQUIVALENCE, we don't know yet whether we are seeing
2331 : : an array, character variable or array of character
2332 : : variables. We'll leave the decision till resolve time. */
2333 : :
2334 : 654951 : if (equiv_flag)
2335 : : as = NULL;
2336 : 652950 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2337 : 16700 : as = CLASS_DATA (sym)->as;
2338 : : else
2339 : 636250 : as = sym->as;
2340 : :
2341 : 654951 : ref = strarr ? strarr : tail;
2342 : 654951 : m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
2343 : : coarray_only);
2344 : 654951 : if (m != MATCH_YES)
2345 : : return m;
2346 : :
2347 : 654876 : gfc_gobble_whitespace ();
2348 : 654876 : if (coarray_only)
2349 : : {
2350 : 1360 : primary->ts = sym->ts;
2351 : 1360 : goto check_substring;
2352 : : }
2353 : :
2354 : 653516 : if (equiv_flag && gfc_peek_ascii_char () == '(')
2355 : : {
2356 : 74 : tail = extend_ref (primary, tail);
2357 : 74 : tail->type = REF_ARRAY;
2358 : :
2359 : 74 : m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2360 : 74 : if (m != MATCH_YES)
2361 : : return m;
2362 : : }
2363 : : }
2364 : :
2365 : 4945768 : primary->ts = sym->ts;
2366 : :
2367 : 4945768 : if (equiv_flag)
2368 : : return MATCH_YES;
2369 : :
2370 : : /* With DEC extensions, member separator may be '.' or '%'. */
2371 : 4942824 : peeked_char = gfc_peek_ascii_char ();
2372 : 4942824 : m = gfc_match_member_sep (sym);
2373 : 4942824 : if (m == MATCH_ERROR)
2374 : : return MATCH_ERROR;
2375 : :
2376 : 4942823 : inquiry = false;
2377 : 4942823 : if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2378 : 124603 : && (primary->ts.type != BT_DERIVED || inferred_type))
2379 : : {
2380 : 2153 : match mm;
2381 : 2153 : old_loc = gfc_current_locus;
2382 : 2153 : mm = gfc_match_name (name);
2383 : :
2384 : : /* Check to see if this has a default complex. */
2385 : 488 : if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2386 : 2171 : && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2387 : : {
2388 : 6 : gfc_set_default_type (sym, 0, sym->ns);
2389 : 6 : primary->ts = sym->ts;
2390 : : }
2391 : :
2392 : : /* This is a usable inquiry reference, if the symbol is already known
2393 : : to have a type or no derived types with a component of this name
2394 : : can be found. If this was an inquiry reference with the same name
2395 : : as a derived component and the associate-name type is not derived
2396 : : or class, this is fixed up in 'gfc_fixup_inferred_type_refs'. */
2397 : 2153 : if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2398 : 3602 : && !(sym->ts.type == BT_UNKNOWN
2399 : 210 : && gfc_find_derived_types (sym, gfc_current_ns, name)))
2400 : : inquiry = true;
2401 : 2153 : gfc_current_locus = old_loc;
2402 : : }
2403 : :
2404 : : /* Use the default type if there is one. */
2405 : 2604607 : if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2406 : 4943305 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2407 : 0 : gfc_set_default_type (sym, 0, sym->ns);
2408 : :
2409 : : /* See if the type can be determined by resolution of the selector expression,
2410 : : if allowable now, or inferred from references. */
2411 : 4942823 : if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2412 : 2605526 : && m == MATCH_YES)
2413 : : {
2414 : 1238 : bool sym_present, resolved = false;
2415 : 1238 : gfc_symbol *tgt_sym;
2416 : :
2417 : 1238 : sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2418 : 1238 : tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
2419 : :
2420 : : /* These target expressions can be resolved at any time:
2421 : : (i) With a declared symbol or intrinsic function; or
2422 : : (ii) An operator expression,
2423 : : just as long as (iii) all the functions in the expression have been
2424 : : declared or are intrinsic. */
2425 : 1238 : if (((sym_present // (i)
2426 : 846 : && (tgt_sym->attr.use_assoc
2427 : 828 : || tgt_sym->attr.host_assoc
2428 : 828 : || tgt_sym->attr.if_source == IFSRC_DECL
2429 : 828 : || tgt_sym->attr.proc == PROC_INTRINSIC
2430 : 828 : || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2431 : 1208 : || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2432 : 54 : && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2433 : 48 : && gfc_resolve_expr (tgt_expr))
2434 : : {
2435 : 42 : sym->ts = tgt_expr->ts;
2436 : 42 : primary->ts = sym->ts;
2437 : 42 : resolved = true;
2438 : : }
2439 : :
2440 : : /* If this hasn't done the trick and the target expression is a function,
2441 : : or an unresolved operator expression, then this must be a derived type
2442 : : if 'name' matches an accessible type both in this namespace and in the
2443 : : as yet unparsed contained function. In principle, the type could have
2444 : : already been inferred to be complex and yet a derived type with a
2445 : : component name 're' or 'im' could be found. */
2446 : 42 : if (tgt_expr
2447 : 882 : && (tgt_expr->expr_type == EXPR_FUNCTION
2448 : 90 : || tgt_expr->expr_type == EXPR_ARRAY
2449 : 78 : || (!resolved && tgt_expr->expr_type == EXPR_OP))
2450 : 816 : && (sym->ts.type == BT_UNKNOWN
2451 : 388 : || (inferred_type && sym->ts.type != BT_COMPLEX))
2452 : 1940 : && gfc_find_derived_types (sym, gfc_current_ns, name, true))
2453 : : {
2454 : 552 : sym->assoc->inferred_type = 1;
2455 : : /* The first returned type is as good as any at this stage. The final
2456 : : determination is made in 'gfc_fixup_inferred_type_refs'*/
2457 : 552 : gfc_symbol **dts = &sym->assoc->derived_types;
2458 : 552 : tgt_expr->ts.type = BT_DERIVED;
2459 : 552 : tgt_expr->ts.kind = 0;
2460 : 552 : tgt_expr->ts.u.derived = *dts;
2461 : 552 : sym->ts = tgt_expr->ts;
2462 : 552 : primary->ts = sym->ts;
2463 : : /* Delete the dt list even if this process has to be done again for
2464 : : another primary expression. */
2465 : 1134 : while (*dts && (*dts)->dt_next)
2466 : : {
2467 : 582 : gfc_symbol **tmp = &(*dts)->dt_next;
2468 : 582 : *dts = NULL;
2469 : 582 : dts = tmp;
2470 : : }
2471 : : }
2472 : : /* If there is a usable inquiry reference not there are no matching
2473 : : derived types, force the inquiry reference by setting unknown the
2474 : : type of the primary expression. */
2475 : 258 : else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2476 : 734 : && !gfc_find_derived_types (sym, gfc_current_ns, name))
2477 : 48 : primary->ts.type = BT_UNKNOWN;
2478 : :
2479 : : /* An inquiry reference might determine the type, otherwise we have an
2480 : : error. */
2481 : 1238 : if (sym->ts.type == BT_UNKNOWN && !inquiry)
2482 : : {
2483 : 12 : gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2484 : 12 : return MATCH_ERROR;
2485 : : }
2486 : : }
2487 : 4941585 : else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2488 : 4722654 : && m == MATCH_YES && !inquiry)
2489 : : {
2490 : 6 : gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2491 : : peeked_char, sym->name);
2492 : 6 : return MATCH_ERROR;
2493 : : }
2494 : :
2495 : 4942805 : if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2496 : 221066 : || m != MATCH_YES)
2497 : 4799300 : goto check_substring;
2498 : :
2499 : 143505 : if (!inquiry)
2500 : 142338 : sym = sym->ts.u.derived;
2501 : : else
2502 : : sym = NULL;
2503 : :
2504 : 164859 : for (;;)
2505 : : {
2506 : 164859 : bool t;
2507 : 164859 : gfc_symtree *tbp;
2508 : 164859 : gfc_typespec *ts = &primary->ts;
2509 : :
2510 : 164859 : m = gfc_match_name (name);
2511 : 164859 : if (m == MATCH_NO)
2512 : 0 : gfc_error ("Expected structure component name at %C");
2513 : 164859 : if (m != MATCH_YES)
2514 : 131 : return MATCH_ERROR;
2515 : :
2516 : : /* For derived type components find typespec of ultimate component. */
2517 : 164859 : if (ts->type == BT_DERIVED && primary->ref)
2518 : : {
2519 : 130888 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2520 : : {
2521 : 73946 : if (ref->type == REF_COMPONENT && ref->u.c.component)
2522 : 20277 : ts = &ref->u.c.component->ts;
2523 : : }
2524 : : }
2525 : :
2526 : 164859 : intrinsic = false;
2527 : 164859 : if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
2528 : : {
2529 : 1641 : inquiry = is_inquiry_ref (name, &tmp);
2530 : 1641 : if (inquiry)
2531 : 1637 : sym = NULL;
2532 : :
2533 : 1641 : if (peeked_char == '%')
2534 : : {
2535 : 1641 : if (tmp)
2536 : : {
2537 : 1637 : gfc_symbol *s;
2538 : 1637 : switch (tmp->u.i)
2539 : : {
2540 : 1098 : case INQUIRY_RE:
2541 : 1098 : case INQUIRY_IM:
2542 : 1098 : if (!gfc_notify_std (GFC_STD_F2008,
2543 : : "RE or IM part_ref at %C"))
2544 : : return MATCH_ERROR;
2545 : : break;
2546 : :
2547 : 284 : case INQUIRY_KIND:
2548 : 284 : if (!gfc_notify_std (GFC_STD_F2003,
2549 : : "KIND part_ref at %C"))
2550 : : return MATCH_ERROR;
2551 : : break;
2552 : :
2553 : 255 : case INQUIRY_LEN:
2554 : 255 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2555 : : return MATCH_ERROR;
2556 : : break;
2557 : : }
2558 : :
2559 : : /* If necessary, infer the type of the primary expression
2560 : : and the associate-name using the the inquiry ref.. */
2561 : 1628 : s = primary->symtree ? primary->symtree->n.sym : NULL;
2562 : 1600 : if (s && s->assoc && s->assoc->target
2563 : 258 : && (s->ts.type == BT_UNKNOWN
2564 : 138 : || (primary->ts.type == BT_UNKNOWN
2565 : 48 : && s->assoc->inferred_type
2566 : 48 : && s->ts.type == BT_DERIVED)))
2567 : : {
2568 : 168 : if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2569 : : {
2570 : 72 : s->ts.type = BT_COMPLEX;
2571 : 72 : s->ts.kind = gfc_default_real_kind;;
2572 : 72 : s->assoc->inferred_type = 1;
2573 : 72 : primary->ts = s->ts;
2574 : : }
2575 : 96 : else if (tmp->u.i == INQUIRY_LEN)
2576 : : {
2577 : 48 : s->ts.type = BT_CHARACTER;
2578 : 48 : s->ts.kind = gfc_default_character_kind;;
2579 : 48 : s->assoc->inferred_type = 1;
2580 : 48 : primary->ts = s->ts;
2581 : : }
2582 : 48 : else if (s->ts.type == BT_UNKNOWN)
2583 : : {
2584 : : /* KIND inquiry gives no clue as to symbol type. */
2585 : 48 : primary->ref = tmp;
2586 : 48 : primary->ts.type = BT_INTEGER;
2587 : 48 : primary->ts.kind = gfc_default_integer_kind;
2588 : 48 : return MATCH_YES;
2589 : : }
2590 : : }
2591 : :
2592 : 1580 : if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2593 : 1094 : && primary->ts.type != BT_COMPLEX)
2594 : : {
2595 : 12 : gfc_error ("The RE or IM part_ref at %C must be "
2596 : : "applied to a COMPLEX expression");
2597 : 12 : return MATCH_ERROR;
2598 : : }
2599 : 1568 : else if (tmp->u.i == INQUIRY_LEN
2600 : 253 : && ts->type != BT_CHARACTER)
2601 : : {
2602 : 5 : gfc_error ("The LEN part_ref at %C must be applied "
2603 : : "to a CHARACTER expression");
2604 : 5 : return MATCH_ERROR;
2605 : : }
2606 : : }
2607 : 1567 : if (primary->ts.type != BT_UNKNOWN)
2608 : 164785 : intrinsic = true;
2609 : : }
2610 : : }
2611 : : else
2612 : : inquiry = false;
2613 : :
2614 : 164785 : if (sym && sym->f2k_derived)
2615 : 160418 : tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2616 : : else
2617 : : tbp = NULL;
2618 : :
2619 : 160418 : if (tbp)
2620 : : {
2621 : 3965 : gfc_symbol* tbp_sym;
2622 : :
2623 : 3965 : if (!t)
2624 : : return MATCH_ERROR;
2625 : :
2626 : 3963 : gcc_assert (!tail || !tail->next);
2627 : :
2628 : 3963 : if (!(primary->expr_type == EXPR_VARIABLE
2629 : : || (primary->expr_type == EXPR_STRUCTURE
2630 : 1 : && primary->symtree && primary->symtree->n.sym
2631 : 1 : && primary->symtree->n.sym->attr.flavor)))
2632 : : return MATCH_ERROR;
2633 : :
2634 : 3961 : if (tbp->n.tb->is_generic)
2635 : : tbp_sym = NULL;
2636 : : else
2637 : 3211 : tbp_sym = tbp->n.tb->u.specific->n.sym;
2638 : :
2639 : 3961 : primary->expr_type = EXPR_COMPCALL;
2640 : 3961 : primary->value.compcall.tbp = tbp->n.tb;
2641 : 3961 : primary->value.compcall.name = tbp->name;
2642 : 3961 : primary->value.compcall.ignore_pass = 0;
2643 : 3961 : primary->value.compcall.assign = 0;
2644 : 3961 : primary->value.compcall.base_object = NULL;
2645 : 3961 : gcc_assert (primary->symtree->n.sym->attr.referenced);
2646 : 3961 : if (tbp_sym)
2647 : 3211 : primary->ts = tbp_sym->ts;
2648 : : else
2649 : 750 : gfc_clear_ts (&primary->ts);
2650 : :
2651 : 3961 : m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2652 : : &primary->value.compcall.actual);
2653 : 3961 : if (m == MATCH_ERROR)
2654 : : return MATCH_ERROR;
2655 : 3961 : if (m == MATCH_NO)
2656 : : {
2657 : 162 : if (sub_flag)
2658 : 161 : primary->value.compcall.actual = NULL;
2659 : : else
2660 : : {
2661 : 1 : gfc_error ("Expected argument list at %C");
2662 : 1 : return MATCH_ERROR;
2663 : : }
2664 : : }
2665 : :
2666 : 143374 : break;
2667 : : }
2668 : :
2669 : 160820 : previous = component;
2670 : :
2671 : 160820 : if (!inquiry && !intrinsic)
2672 : 159254 : component = gfc_find_component (sym, name, false, false, &tmp);
2673 : : else
2674 : : component = NULL;
2675 : :
2676 : 160820 : if (intrinsic && !inquiry)
2677 : : {
2678 : 3 : if (previous)
2679 : 2 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2680 : : "type component %qs", name, previous->name);
2681 : : else
2682 : 1 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2683 : : "type component", name);
2684 : 3 : return MATCH_ERROR;
2685 : : }
2686 : 160817 : else if (component == NULL && !inquiry)
2687 : : return MATCH_ERROR;
2688 : :
2689 : : /* Extend the reference chain determined by gfc_find_component or
2690 : : is_inquiry_ref. */
2691 : 160770 : if (primary->ref == NULL)
2692 : 96675 : primary->ref = tmp;
2693 : : else
2694 : : {
2695 : : /* Find end of reference chain if inquiry reference and tail not
2696 : : set. */
2697 : 64095 : if (tail == NULL && inquiry && tmp)
2698 : 11 : tail = extend_ref (primary, tail);
2699 : :
2700 : : /* Set by the for loop below for the last component ref. */
2701 : 64095 : gcc_assert (tail != NULL);
2702 : 64095 : tail->next = tmp;
2703 : : }
2704 : :
2705 : : /* The reference chain may be longer than one hop for union
2706 : : subcomponents; find the new tail. */
2707 : 162746 : for (tail = tmp; tail->next; tail = tail->next)
2708 : : ;
2709 : :
2710 : 160770 : if (tmp && tmp->type == REF_INQUIRY)
2711 : : {
2712 : 1563 : if (!primary->where.u.lb || !primary->where.nextc)
2713 : 1379 : primary->where = gfc_current_locus;
2714 : 1563 : gfc_simplify_expr (primary, 0);
2715 : :
2716 : 1563 : if (primary->expr_type == EXPR_CONSTANT)
2717 : 354 : goto check_done;
2718 : :
2719 : 1209 : if (primary->ref == NULL)
2720 : 60 : goto check_done;
2721 : :
2722 : 1149 : switch (tmp->u.i)
2723 : : {
2724 : 938 : case INQUIRY_RE:
2725 : 938 : case INQUIRY_IM:
2726 : 938 : if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2727 : : return MATCH_ERROR;
2728 : :
2729 : 938 : if (primary->ts.type != BT_COMPLEX)
2730 : : {
2731 : 0 : gfc_error ("The RE or IM part_ref at %C must be "
2732 : : "applied to a COMPLEX expression");
2733 : 0 : return MATCH_ERROR;
2734 : : }
2735 : 938 : primary->ts.type = BT_REAL;
2736 : 938 : break;
2737 : :
2738 : 159 : case INQUIRY_LEN:
2739 : 159 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2740 : : return MATCH_ERROR;
2741 : :
2742 : 159 : if (primary->ts.type != BT_CHARACTER)
2743 : : {
2744 : 0 : gfc_error ("The LEN part_ref at %C must be applied "
2745 : : "to a CHARACTER expression");
2746 : 0 : return MATCH_ERROR;
2747 : : }
2748 : 159 : primary->ts.u.cl = NULL;
2749 : 159 : primary->ts.type = BT_INTEGER;
2750 : 159 : primary->ts.kind = gfc_default_integer_kind;
2751 : 159 : break;
2752 : :
2753 : 52 : case INQUIRY_KIND:
2754 : 52 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2755 : : return MATCH_ERROR;
2756 : :
2757 : 52 : if (primary->ts.type == BT_CLASS
2758 : 52 : || primary->ts.type == BT_DERIVED)
2759 : : {
2760 : 0 : gfc_error ("The KIND part_ref at %C must be applied "
2761 : : "to an expression of intrinsic type");
2762 : 0 : return MATCH_ERROR;
2763 : : }
2764 : 52 : primary->ts.type = BT_INTEGER;
2765 : 52 : primary->ts.kind = gfc_default_integer_kind;
2766 : 52 : break;
2767 : :
2768 : 0 : default:
2769 : 0 : gcc_unreachable ();
2770 : : }
2771 : :
2772 : 1149 : goto check_done;
2773 : : }
2774 : :
2775 : 159207 : primary->ts = component->ts;
2776 : :
2777 : 159207 : if (component->attr.proc_pointer && ppc_arg)
2778 : : {
2779 : : /* Procedure pointer component call: Look for argument list. */
2780 : 921 : m = gfc_match_actual_arglist (sub_flag,
2781 : : &primary->value.compcall.actual);
2782 : 921 : if (m == MATCH_ERROR)
2783 : : return MATCH_ERROR;
2784 : :
2785 : 921 : if (m == MATCH_NO && !gfc_matching_ptr_assignment
2786 : 263 : && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2787 : : {
2788 : 2 : gfc_error ("Procedure pointer component %qs requires an "
2789 : : "argument list at %C", component->name);
2790 : 2 : return MATCH_ERROR;
2791 : : }
2792 : :
2793 : 919 : if (m == MATCH_YES)
2794 : 657 : primary->expr_type = EXPR_PPC;
2795 : :
2796 : : break;
2797 : : }
2798 : :
2799 : 158286 : if (component->as != NULL && !component->attr.proc_pointer)
2800 : : {
2801 : 53547 : tail = extend_ref (primary, tail);
2802 : 53547 : tail->type = REF_ARRAY;
2803 : :
2804 : 107094 : m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2805 : 53547 : component->as->corank);
2806 : 53547 : if (m != MATCH_YES)
2807 : : return m;
2808 : : }
2809 : 104739 : else if (component->ts.type == BT_CLASS && component->attr.class_ok
2810 : 10933 : && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2811 : : {
2812 : 5301 : tail = extend_ref (primary, tail);
2813 : 5301 : tail->type = REF_ARRAY;
2814 : :
2815 : 10602 : m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2816 : : equiv_flag,
2817 : 5301 : CLASS_DATA (component)->as->corank);
2818 : 5301 : if (m != MATCH_YES)
2819 : : return m;
2820 : : }
2821 : :
2822 : 99438 : check_done:
2823 : : /* In principle, we could have eg. expr%re%kind so we must allow for
2824 : : this possibility. */
2825 : 159849 : if (gfc_match_char ('%') == MATCH_YES)
2826 : : {
2827 : 20984 : if (component && (component->ts.type == BT_DERIVED
2828 : 3695 : || component->ts.type == BT_CLASS))
2829 : 20513 : sym = component->ts.u.derived;
2830 : 20984 : continue;
2831 : : }
2832 : 138865 : else if (inquiry)
2833 : : break;
2834 : :
2835 : 128672 : if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2836 : 145011 : || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2837 : : break;
2838 : :
2839 : 370 : if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2840 : 370 : sym = component->ts.u.derived;
2841 : : }
2842 : :
2843 : 4944034 : check_substring:
2844 : 4944034 : unknown = false;
2845 : 4944034 : if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2846 : : {
2847 : 2604125 : if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2848 : : {
2849 : 352 : gfc_set_default_type (sym, 0, sym->ns);
2850 : 352 : primary->ts = sym->ts;
2851 : 352 : unknown = true;
2852 : : }
2853 : : }
2854 : :
2855 : 4944034 : if (primary->ts.type == BT_CHARACTER)
2856 : : {
2857 : 292016 : bool def = primary->ts.deferred == 1;
2858 : 292016 : switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2859 : : {
2860 : 13321 : case MATCH_YES:
2861 : 13321 : if (tail == NULL)
2862 : 8155 : primary->ref = substring;
2863 : : else
2864 : 5166 : tail->next = substring;
2865 : :
2866 : 13321 : if (primary->expr_type == EXPR_CONSTANT)
2867 : 755 : primary->expr_type = EXPR_SUBSTRING;
2868 : :
2869 : 13321 : if (substring)
2870 : 13141 : primary->ts.u.cl = NULL;
2871 : :
2872 : 13321 : gfc_gobble_whitespace ();
2873 : 13321 : if (gfc_peek_ascii_char () == '(')
2874 : : {
2875 : 5 : gfc_error_now ("Unexpected array/substring ref at %C");
2876 : 5 : return MATCH_ERROR;
2877 : : }
2878 : : break;
2879 : :
2880 : 278695 : case MATCH_NO:
2881 : 278695 : if (unknown)
2882 : : {
2883 : 351 : gfc_clear_ts (&primary->ts);
2884 : 351 : gfc_clear_ts (&sym->ts);
2885 : : }
2886 : : break;
2887 : :
2888 : : case MATCH_ERROR:
2889 : : return MATCH_ERROR;
2890 : : }
2891 : : }
2892 : :
2893 : : /* F08:C611. */
2894 : 4944029 : if (primary->ts.type == BT_DERIVED && primary->ref
2895 : 26449 : && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2896 : : {
2897 : 6 : gfc_error ("Nonpolymorphic reference to abstract type at %C");
2898 : 6 : return MATCH_ERROR;
2899 : : }
2900 : :
2901 : : /* F08:C727. */
2902 : 4944023 : if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2903 : : {
2904 : 3 : gfc_error ("Coindexed procedure-pointer component at %C");
2905 : 3 : return MATCH_ERROR;
2906 : : }
2907 : :
2908 : : return MATCH_YES;
2909 : : }
2910 : :
2911 : :
2912 : : /* Given an expression that is a variable, figure out what the
2913 : : ultimate variable's type and attribute is, traversing the reference
2914 : : structures if necessary.
2915 : :
2916 : : This subroutine is trickier than it looks. We start at the base
2917 : : symbol and store the attribute. Component references load a
2918 : : completely new attribute.
2919 : :
2920 : : A couple of rules come into play. Subobjects of targets are always
2921 : : targets themselves. If we see a component that goes through a
2922 : : pointer, then the expression must also be a target, since the
2923 : : pointer is associated with something (if it isn't core will soon be
2924 : : dumped). If we see a full part or section of an array, the
2925 : : expression is also an array.
2926 : :
2927 : : We can have at most one full array reference. */
2928 : :
2929 : : symbol_attribute
2930 : 4272236 : gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2931 : : {
2932 : 4272236 : int dimension, codimension, pointer, allocatable, target, optional;
2933 : 4272236 : symbol_attribute attr;
2934 : 4272236 : gfc_ref *ref;
2935 : 4272236 : gfc_symbol *sym;
2936 : 4272236 : gfc_component *comp;
2937 : 4272236 : bool has_inquiry_part;
2938 : 4272236 : bool has_substring_ref = false;
2939 : :
2940 : 4272236 : if (expr->expr_type != EXPR_VARIABLE
2941 : 26777 : && expr->expr_type != EXPR_FUNCTION
2942 : 9 : && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
2943 : 0 : gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2944 : :
2945 : 4272236 : sym = expr->symtree->n.sym;
2946 : 4272236 : attr = sym->attr;
2947 : :
2948 : 4272236 : optional = attr.optional;
2949 : 4272236 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
2950 : : {
2951 : 123334 : dimension = CLASS_DATA (sym)->attr.dimension;
2952 : 123334 : codimension = CLASS_DATA (sym)->attr.codimension;
2953 : 123334 : pointer = CLASS_DATA (sym)->attr.class_pointer;
2954 : 123334 : allocatable = CLASS_DATA (sym)->attr.allocatable;
2955 : : }
2956 : : else
2957 : : {
2958 : 4148902 : dimension = attr.dimension;
2959 : 4148902 : codimension = attr.codimension;
2960 : 4148902 : pointer = attr.pointer;
2961 : 4148902 : allocatable = attr.allocatable;
2962 : : }
2963 : :
2964 : 4272236 : target = attr.target;
2965 : 4272236 : if (pointer || attr.proc_pointer)
2966 : 172565 : target = 1;
2967 : :
2968 : : /* F2018:11.1.3.3: Other attributes of associate names
2969 : : "The associating entity does not have the ALLOCATABLE or POINTER
2970 : : attributes; it has the TARGET attribute if and only if the selector is
2971 : : a variable and has either the TARGET or POINTER attribute." */
2972 : 4272236 : if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
2973 : : {
2974 : 29022 : if (sym->assoc->target->expr_type == EXPR_VARIABLE)
2975 : : {
2976 : 26575 : symbol_attribute tgt_attr;
2977 : 26575 : tgt_attr = gfc_expr_attr (sym->assoc->target);
2978 : 34207 : target = (tgt_attr.pointer || tgt_attr.target);
2979 : : }
2980 : : else
2981 : : target = 0;
2982 : : }
2983 : :
2984 : 4272236 : if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2985 : 51360 : *ts = sym->ts;
2986 : :
2987 : : /* Catch left-overs from match_actual_arg, where an actual argument of a
2988 : : procedure is given a temporary ts.type == BT_PROCEDURE. The fixup is
2989 : : needed for structure constructors in DATA statements, where a pointer
2990 : : is associated with a data target, and the argument has not been fully
2991 : : resolved yet. Components references are dealt with further below. */
2992 : 51360 : if (ts != NULL
2993 : 1279847 : && expr->ts.type == BT_PROCEDURE
2994 : 2489 : && expr->ref == NULL
2995 : 2489 : && attr.flavor != FL_PROCEDURE
2996 : 75 : && attr.target)
2997 : 1 : *ts = sym->ts;
2998 : :
2999 : 4272236 : has_inquiry_part = false;
3000 : 5676329 : for (ref = expr->ref; ref; ref = ref->next)
3001 : 1405327 : if (ref->type == REF_SUBSTRING)
3002 : : {
3003 : : has_substring_ref = true;
3004 : : optional = false;
3005 : : }
3006 : 1391278 : else if (ref->type == REF_INQUIRY)
3007 : : {
3008 : : has_inquiry_part = true;
3009 : : optional = false;
3010 : : break;
3011 : : }
3012 : :
3013 : 5677570 : for (ref = expr->ref; ref; ref = ref->next)
3014 : 1405334 : switch (ref->type)
3015 : : {
3016 : 1083715 : case REF_ARRAY:
3017 : :
3018 : 1083715 : switch (ref->u.ar.type)
3019 : : {
3020 : : case AR_FULL:
3021 : 1405334 : dimension = 1;
3022 : : break;
3023 : :
3024 : 109678 : case AR_SECTION:
3025 : 109678 : allocatable = pointer = 0;
3026 : 109678 : dimension = 1;
3027 : 109678 : optional = false;
3028 : 109678 : break;
3029 : :
3030 : 310073 : case AR_ELEMENT:
3031 : : /* Handle coarrays. */
3032 : 310073 : if (ref->u.ar.dimen > 0)
3033 : 1405334 : allocatable = pointer = optional = false;
3034 : : break;
3035 : :
3036 : : case AR_UNKNOWN:
3037 : : /* For standard conforming code, AR_UNKNOWN should not happen.
3038 : : For nonconforming code, gfortran can end up here. Treat it
3039 : : as a no-op. */
3040 : : break;
3041 : : }
3042 : :
3043 : : break;
3044 : :
3045 : 306329 : case REF_COMPONENT:
3046 : 306329 : optional = false;
3047 : 306329 : comp = ref->u.c.component;
3048 : 306329 : attr = comp->attr;
3049 : 306329 : if (ts != NULL && !has_inquiry_part)
3050 : : {
3051 : 80525 : *ts = comp->ts;
3052 : : /* Don't set the string length if a substring reference
3053 : : follows. */
3054 : 80525 : if (ts->type == BT_CHARACTER && has_substring_ref)
3055 : 288 : ts->u.cl = NULL;
3056 : : }
3057 : :
3058 : 306329 : if (comp->ts.type == BT_CLASS)
3059 : : {
3060 : 21159 : dimension = CLASS_DATA (comp)->attr.dimension;
3061 : 21159 : codimension = CLASS_DATA (comp)->attr.codimension;
3062 : 21159 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3063 : 21159 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3064 : : }
3065 : : else
3066 : : {
3067 : 285170 : dimension = comp->attr.dimension;
3068 : 285170 : codimension = comp->attr.codimension;
3069 : 285170 : if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3070 : 13681 : pointer = comp->attr.class_pointer;
3071 : : else
3072 : 271489 : pointer = comp->attr.pointer;
3073 : 285170 : allocatable = comp->attr.allocatable;
3074 : : }
3075 : 306329 : if (pointer || attr.proc_pointer)
3076 : 52725 : target = 1;
3077 : :
3078 : : break;
3079 : :
3080 : 15290 : case REF_INQUIRY:
3081 : 15290 : case REF_SUBSTRING:
3082 : 15290 : allocatable = pointer = optional = false;
3083 : 15290 : break;
3084 : : }
3085 : :
3086 : 4272236 : attr.dimension = dimension;
3087 : 4272236 : attr.codimension = codimension;
3088 : 4272236 : attr.pointer = pointer;
3089 : 4272236 : attr.allocatable = allocatable;
3090 : 4272236 : attr.target = target;
3091 : 4272236 : attr.save = sym->attr.save;
3092 : 4272236 : attr.optional = optional;
3093 : :
3094 : 4272236 : return attr;
3095 : : }
3096 : :
3097 : :
3098 : : /* Return the attribute from a general expression. */
3099 : :
3100 : : symbol_attribute
3101 : 3653514 : gfc_expr_attr (gfc_expr *e)
3102 : : {
3103 : 3653514 : symbol_attribute attr;
3104 : :
3105 : 3653514 : switch (e->expr_type)
3106 : : {
3107 : 2957104 : case EXPR_VARIABLE:
3108 : 2957104 : attr = gfc_variable_attr (e, NULL);
3109 : 2957104 : break;
3110 : :
3111 : 41616 : case EXPR_FUNCTION:
3112 : 41616 : gfc_clear_attr (&attr);
3113 : :
3114 : 41616 : if (e->value.function.esym && e->value.function.esym->result)
3115 : : {
3116 : 14556 : gfc_symbol *sym = e->value.function.esym->result;
3117 : 14556 : attr = sym->attr;
3118 : 14556 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3119 : : {
3120 : 1706 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3121 : 1706 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3122 : 1706 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3123 : : }
3124 : : }
3125 : 27060 : else if (e->value.function.isym
3126 : 26054 : && e->value.function.isym->transformational
3127 : 16127 : && e->ts.type == BT_CLASS)
3128 : 294 : attr = CLASS_DATA (e)->attr;
3129 : 26766 : else if (e->symtree)
3130 : 26766 : attr = gfc_variable_attr (e, NULL);
3131 : :
3132 : : /* TODO: NULL() returns pointers. May have to take care of this
3133 : : here. */
3134 : :
3135 : : break;
3136 : :
3137 : 654794 : default:
3138 : 654794 : gfc_clear_attr (&attr);
3139 : 654794 : break;
3140 : : }
3141 : :
3142 : 3653514 : return attr;
3143 : : }
3144 : :
3145 : :
3146 : : /* Given an expression, figure out what the ultimate expression
3147 : : attribute is. This routine is similar to gfc_variable_attr with
3148 : : parts of gfc_expr_attr, but focuses more on the needs of
3149 : : coarrays. For coarrays a codimension attribute is kind of
3150 : : "infectious" being propagated once set and never cleared.
3151 : : The coarray_comp is only set, when the expression refs a coarray
3152 : : component. REFS_COMP is set when present to true only, when this EXPR
3153 : : refs a (non-_data) component. To check whether EXPR refs an allocatable
3154 : : component in a derived type coarray *refs_comp needs to be set and
3155 : : coarray_comp has to false. */
3156 : :
3157 : : static symbol_attribute
3158 : 12532 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3159 : : {
3160 : 12532 : int dimension, codimension, pointer, allocatable, target, coarray_comp;
3161 : 12532 : symbol_attribute attr;
3162 : 12532 : gfc_ref *ref;
3163 : 12532 : gfc_symbol *sym;
3164 : 12532 : gfc_component *comp;
3165 : :
3166 : 12532 : if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3167 : 0 : gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3168 : :
3169 : 12532 : sym = expr->symtree->n.sym;
3170 : 12532 : gfc_clear_attr (&attr);
3171 : :
3172 : 12532 : if (refs_comp)
3173 : 7780 : *refs_comp = false;
3174 : :
3175 : 12532 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3176 : : {
3177 : 369 : dimension = CLASS_DATA (sym)->attr.dimension;
3178 : 369 : codimension = CLASS_DATA (sym)->attr.codimension;
3179 : 369 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3180 : 369 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3181 : 369 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3182 : 369 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3183 : : }
3184 : : else
3185 : : {
3186 : 12163 : dimension = sym->attr.dimension;
3187 : 12163 : codimension = sym->attr.codimension;
3188 : 12163 : pointer = sym->attr.pointer;
3189 : 12163 : allocatable = sym->attr.allocatable;
3190 : 24326 : attr.alloc_comp = sym->ts.type == BT_DERIVED
3191 : 12163 : ? sym->ts.u.derived->attr.alloc_comp : 0;
3192 : 12163 : attr.pointer_comp = sym->ts.type == BT_DERIVED
3193 : 12163 : ? sym->ts.u.derived->attr.pointer_comp : 0;
3194 : : }
3195 : :
3196 : 12532 : target = coarray_comp = 0;
3197 : 12532 : if (pointer || attr.proc_pointer)
3198 : 523 : target = 1;
3199 : :
3200 : 22293 : for (ref = expr->ref; ref; ref = ref->next)
3201 : 9761 : switch (ref->type)
3202 : : {
3203 : 6361 : case REF_ARRAY:
3204 : :
3205 : 6361 : switch (ref->u.ar.type)
3206 : : {
3207 : : case AR_FULL:
3208 : : case AR_SECTION:
3209 : : dimension = 1;
3210 : 6361 : break;
3211 : :
3212 : 2852 : case AR_ELEMENT:
3213 : : /* Handle coarrays. */
3214 : 2852 : if (ref->u.ar.dimen > 0 && !in_allocate)
3215 : 6361 : allocatable = pointer = 0;
3216 : : break;
3217 : :
3218 : 0 : case AR_UNKNOWN:
3219 : : /* If any of start, end or stride is not integer, there will
3220 : : already have been an error issued. */
3221 : 0 : int errors;
3222 : 0 : gfc_get_errors (NULL, &errors);
3223 : 0 : if (errors == 0)
3224 : 0 : gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3225 : : }
3226 : :
3227 : : break;
3228 : :
3229 : 3399 : case REF_COMPONENT:
3230 : 3399 : comp = ref->u.c.component;
3231 : :
3232 : 3399 : if (comp->ts.type == BT_CLASS)
3233 : : {
3234 : : /* Set coarray_comp only, when this component introduces the
3235 : : coarray. */
3236 : 13 : coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3237 : 13 : codimension |= CLASS_DATA (comp)->attr.codimension;
3238 : 13 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3239 : 13 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3240 : : }
3241 : : else
3242 : : {
3243 : : /* Set coarray_comp only, when this component introduces the
3244 : : coarray. */
3245 : 3386 : coarray_comp = !codimension && comp->attr.codimension;
3246 : 3386 : codimension |= comp->attr.codimension;
3247 : 3386 : pointer = comp->attr.pointer;
3248 : 3386 : allocatable = comp->attr.allocatable;
3249 : : }
3250 : :
3251 : 3399 : if (refs_comp && strcmp (comp->name, "_data") != 0
3252 : 2060 : && (ref->next == NULL
3253 : 1569 : || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3254 : 1501 : *refs_comp = true;
3255 : :
3256 : 3399 : if (pointer || attr.proc_pointer)
3257 : 673 : target = 1;
3258 : :
3259 : : break;
3260 : :
3261 : : case REF_SUBSTRING:
3262 : : case REF_INQUIRY:
3263 : 9761 : allocatable = pointer = 0;
3264 : : break;
3265 : : }
3266 : :
3267 : 12532 : attr.dimension = dimension;
3268 : 12532 : attr.codimension = codimension;
3269 : 12532 : attr.pointer = pointer;
3270 : 12532 : attr.allocatable = allocatable;
3271 : 12532 : attr.target = target;
3272 : 12532 : attr.save = sym->attr.save;
3273 : 12532 : attr.coarray_comp = coarray_comp;
3274 : :
3275 : 12532 : return attr;
3276 : : }
3277 : :
3278 : :
3279 : : symbol_attribute
3280 : 14998 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3281 : : {
3282 : 14998 : symbol_attribute attr;
3283 : :
3284 : 14998 : switch (e->expr_type)
3285 : : {
3286 : 11370 : case EXPR_VARIABLE:
3287 : 11370 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3288 : 11370 : break;
3289 : :
3290 : 1166 : case EXPR_FUNCTION:
3291 : 1166 : gfc_clear_attr (&attr);
3292 : :
3293 : 1166 : if (e->value.function.esym && e->value.function.esym->result)
3294 : : {
3295 : 4 : gfc_symbol *sym = e->value.function.esym->result;
3296 : 4 : attr = sym->attr;
3297 : 4 : if (sym->ts.type == BT_CLASS)
3298 : : {
3299 : 0 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3300 : 0 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3301 : 0 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3302 : 0 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3303 : 0 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3304 : 0 : ->attr.pointer_comp;
3305 : : }
3306 : : }
3307 : 1162 : else if (e->symtree)
3308 : 1162 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3309 : : else
3310 : 0 : gfc_clear_attr (&attr);
3311 : : break;
3312 : :
3313 : 2462 : default:
3314 : 2462 : gfc_clear_attr (&attr);
3315 : 2462 : break;
3316 : : }
3317 : :
3318 : 14998 : return attr;
3319 : : }
3320 : :
3321 : :
3322 : : /* Match a structure constructor. The initial symbol has already been
3323 : : seen. */
3324 : :
3325 : : typedef struct gfc_structure_ctor_component
3326 : : {
3327 : : char* name;
3328 : : gfc_expr* val;
3329 : : locus where;
3330 : : struct gfc_structure_ctor_component* next;
3331 : : }
3332 : : gfc_structure_ctor_component;
3333 : :
3334 : : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3335 : :
3336 : : static void
3337 : 9833 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3338 : : {
3339 : 9833 : free (comp->name);
3340 : 9833 : gfc_free_expr (comp->val);
3341 : 9833 : free (comp);
3342 : 9833 : }
3343 : :
3344 : :
3345 : : /* Translate the component list into the actual constructor by sorting it in
3346 : : the order required; this also checks along the way that each and every
3347 : : component actually has an initializer and handles default initializers
3348 : : for components without explicit value given. */
3349 : : static bool
3350 : 6888 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
3351 : : gfc_constructor_base *ctor_head, gfc_symbol *sym)
3352 : : {
3353 : 6888 : gfc_structure_ctor_component *comp_iter;
3354 : 6888 : gfc_component *comp;
3355 : :
3356 : 18076 : for (comp = sym->components; comp; comp = comp->next)
3357 : : {
3358 : 11193 : gfc_structure_ctor_component **next_ptr;
3359 : 11193 : gfc_expr *value = NULL;
3360 : :
3361 : : /* Try to find the initializer for the current component by name. */
3362 : 11193 : next_ptr = comp_head;
3363 : 12327 : for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3364 : : {
3365 : 10943 : if (!strcmp (comp_iter->name, comp->name))
3366 : : break;
3367 : 1134 : next_ptr = &comp_iter->next;
3368 : : }
3369 : :
3370 : : /* If an extension, try building the parent derived type by building
3371 : : a value expression for the parent derived type and calling self. */
3372 : 11193 : if (!comp_iter && comp == sym->components && sym->attr.extension)
3373 : : {
3374 : 106 : value = gfc_get_structure_constructor_expr (comp->ts.type,
3375 : : comp->ts.kind,
3376 : : &gfc_current_locus);
3377 : 106 : value->ts = comp->ts;
3378 : :
3379 : 106 : if (!build_actual_constructor (comp_head,
3380 : : &value->value.constructor,
3381 : 106 : comp->ts.u.derived))
3382 : : {
3383 : 0 : gfc_free_expr (value);
3384 : 0 : return false;
3385 : : }
3386 : :
3387 : 106 : gfc_constructor_append_expr (ctor_head, value, NULL);
3388 : 106 : continue;
3389 : : }
3390 : :
3391 : : /* If it was not found, apply NULL expression to set the component as
3392 : : unallocated. Then try the default initializer if there's any;
3393 : : otherwise, it's an error unless this is a deferred parameter. */
3394 : 1278 : if (!comp_iter)
3395 : : {
3396 : : /* F2018 7.5.10: If an allocatable component has no corresponding
3397 : : component-data-source, then that component has an allocation
3398 : : status of unallocated.... */
3399 : 1278 : if (comp->attr.allocatable
3400 : 1143 : || (comp->ts.type == BT_CLASS
3401 : 15 : && CLASS_DATA (comp)->attr.allocatable))
3402 : : {
3403 : 144 : if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3404 : : "allocatable component %qs given in the "
3405 : : "structure constructor at %C", comp->name))
3406 : : return false;
3407 : 144 : value = gfc_get_null_expr (&gfc_current_locus);
3408 : : }
3409 : : /* ....(Preceding sentence) If a component with default
3410 : : initialization has no corresponding component-data-source, then
3411 : : the default initialization is applied to that component. */
3412 : 1134 : else if (comp->initializer)
3413 : : {
3414 : 634 : if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3415 : : "with missing optional arguments at %C"))
3416 : : return false;
3417 : 632 : value = gfc_copy_expr (comp->initializer);
3418 : : }
3419 : : /* Do not trap components such as the string length for deferred
3420 : : length character components. */
3421 : 500 : else if (!comp->attr.artificial)
3422 : : {
3423 : 3 : gfc_error ("No initializer for component %qs given in the"
3424 : : " structure constructor at %C", comp->name);
3425 : 3 : return false;
3426 : : }
3427 : : }
3428 : : else
3429 : 9809 : value = comp_iter->val;
3430 : :
3431 : : /* Add the value to the constructor chain built. */
3432 : 11082 : gfc_constructor_append_expr (ctor_head, value, NULL);
3433 : :
3434 : : /* Remove the entry from the component list. We don't want the expression
3435 : : value to be free'd, so set it to NULL. */
3436 : 11082 : if (comp_iter)
3437 : : {
3438 : 9809 : *next_ptr = comp_iter->next;
3439 : 9809 : comp_iter->val = NULL;
3440 : 9809 : gfc_free_structure_ctor_component (comp_iter);
3441 : : }
3442 : : }
3443 : : return true;
3444 : : }
3445 : :
3446 : :
3447 : : bool
3448 : 6797 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3449 : : gfc_actual_arglist **arglist,
3450 : : bool parent)
3451 : : {
3452 : 6797 : gfc_actual_arglist *actual;
3453 : 6797 : gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3454 : 6797 : gfc_constructor_base ctor_head = NULL;
3455 : 6797 : gfc_component *comp; /* Is set NULL when named component is first seen */
3456 : 6797 : const char* last_name = NULL;
3457 : 6797 : locus old_locus;
3458 : 6797 : gfc_expr *expr;
3459 : :
3460 : 6797 : expr = parent ? *cexpr : e;
3461 : 6797 : old_locus = gfc_current_locus;
3462 : 6797 : if (parent)
3463 : : ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3464 : : else
3465 : 6065 : gfc_current_locus = expr->where;
3466 : :
3467 : 6797 : comp_tail = comp_head = NULL;
3468 : :
3469 : 6797 : if (!parent && sym->attr.abstract)
3470 : : {
3471 : 1 : gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3472 : : sym->name, &expr->where);
3473 : 1 : goto cleanup;
3474 : : }
3475 : :
3476 : 6796 : comp = sym->components;
3477 : 6796 : actual = parent ? *arglist : expr->value.function.actual;
3478 : 16010 : for ( ; actual; )
3479 : : {
3480 : 9833 : gfc_component *this_comp = NULL;
3481 : :
3482 : 9833 : if (!comp_head)
3483 : 6382 : comp_tail = comp_head = gfc_get_structure_ctor_component ();
3484 : : else
3485 : : {
3486 : 3451 : comp_tail->next = gfc_get_structure_ctor_component ();
3487 : 3451 : comp_tail = comp_tail->next;
3488 : : }
3489 : 9833 : if (actual->name)
3490 : : {
3491 : 948 : if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3492 : : " constructor with named arguments at %C"))
3493 : 1 : goto cleanup;
3494 : :
3495 : 947 : comp_tail->name = xstrdup (actual->name);
3496 : 947 : last_name = comp_tail->name;
3497 : 947 : comp = NULL;
3498 : : }
3499 : : else
3500 : : {
3501 : : /* Components without name are not allowed after the first named
3502 : : component initializer! */
3503 : 8885 : if (!comp || comp->attr.artificial)
3504 : : {
3505 : 2 : if (last_name)
3506 : 0 : gfc_error ("Component initializer without name after component"
3507 : : " named %s at %L", last_name,
3508 : 0 : actual->expr ? &actual->expr->where
3509 : : : &gfc_current_locus);
3510 : : else
3511 : 2 : gfc_error ("Too many components in structure constructor at "
3512 : 2 : "%L", actual->expr ? &actual->expr->where
3513 : : : &gfc_current_locus);
3514 : 2 : goto cleanup;
3515 : : }
3516 : :
3517 : 8883 : comp_tail->name = xstrdup (comp->name);
3518 : : }
3519 : :
3520 : : /* Find the current component in the structure definition and check
3521 : : its access is not private. */
3522 : 9830 : if (comp)
3523 : 8883 : this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3524 : : else
3525 : : {
3526 : 947 : this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3527 : : false, false, NULL);
3528 : 947 : comp = NULL; /* Reset needed! */
3529 : : }
3530 : :
3531 : : /* Here we can check if a component name is given which does not
3532 : : correspond to any component of the defined structure. */
3533 : 9830 : if (!this_comp)
3534 : 8 : goto cleanup;
3535 : :
3536 : : /* For a constant string constructor, make sure the length is
3537 : : correct; truncate or fill with blanks if needed. */
3538 : 9822 : if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3539 : 983 : && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3540 : 981 : && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3541 : 969 : && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3542 : 968 : && actual->expr->ts.type == BT_CHARACTER
3543 : 956 : && actual->expr->expr_type == EXPR_CONSTANT)
3544 : : {
3545 : 733 : ptrdiff_t c, e1;
3546 : 733 : c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3547 : 733 : e1 = actual->expr->value.character.length;
3548 : :
3549 : 733 : if (c != e1)
3550 : : {
3551 : 249 : ptrdiff_t i, to;
3552 : 249 : gfc_char_t *dest;
3553 : 249 : dest = gfc_get_wide_string (c + 1);
3554 : :
3555 : 249 : to = e1 < c ? e1 : c;
3556 : 4482 : for (i = 0; i < to; i++)
3557 : 4233 : dest[i] = actual->expr->value.character.string[i];
3558 : :
3559 : 5812 : for (i = e1; i < c; i++)
3560 : 5563 : dest[i] = ' ';
3561 : :
3562 : 249 : dest[c] = '\0';
3563 : 249 : free (actual->expr->value.character.string);
3564 : :
3565 : 249 : actual->expr->value.character.length = c;
3566 : 249 : actual->expr->value.character.string = dest;
3567 : :
3568 : 249 : if (warn_line_truncation && c < e1)
3569 : 14 : gfc_warning_now (OPT_Wcharacter_truncation,
3570 : : "CHARACTER expression will be truncated "
3571 : : "in constructor (%td/%td) at %L", c,
3572 : : e1, &actual->expr->where);
3573 : : }
3574 : : }
3575 : :
3576 : 9822 : comp_tail->val = actual->expr;
3577 : 9822 : if (actual->expr != NULL)
3578 : 9822 : comp_tail->where = actual->expr->where;
3579 : 9822 : actual->expr = NULL;
3580 : :
3581 : : /* Check if this component is already given a value. */
3582 : 15662 : for (comp_iter = comp_head; comp_iter != comp_tail;
3583 : 5840 : comp_iter = comp_iter->next)
3584 : : {
3585 : 5841 : gcc_assert (comp_iter);
3586 : 5841 : if (!strcmp (comp_iter->name, comp_tail->name))
3587 : : {
3588 : 1 : gfc_error ("Component %qs is initialized twice in the structure"
3589 : : " constructor at %L", comp_tail->name,
3590 : : comp_tail->val ? &comp_tail->where
3591 : : : &gfc_current_locus);
3592 : 1 : goto cleanup;
3593 : : }
3594 : : }
3595 : :
3596 : : /* F2008, R457/C725, for PURE C1283. */
3597 : 72 : if (this_comp->attr.pointer && comp_tail->val
3598 : 9893 : && gfc_is_coindexed (comp_tail->val))
3599 : : {
3600 : 2 : gfc_error ("Coindexed expression to pointer component %qs in "
3601 : : "structure constructor at %L", comp_tail->name,
3602 : : &comp_tail->where);
3603 : 2 : goto cleanup;
3604 : : }
3605 : :
3606 : : /* If not explicitly a parent constructor, gather up the components
3607 : : and build one. */
3608 : 9819 : if (comp && comp == sym->components
3609 : 6134 : && sym->attr.extension
3610 : 780 : && comp_tail->val
3611 : 780 : && (!gfc_bt_struct (comp_tail->val->ts.type)
3612 : 78 : ||
3613 : 78 : comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3614 : : {
3615 : 732 : bool m;
3616 : 732 : gfc_actual_arglist *arg_null = NULL;
3617 : :
3618 : 732 : actual->expr = comp_tail->val;
3619 : 732 : comp_tail->val = NULL;
3620 : :
3621 : 732 : m = gfc_convert_to_structure_constructor (NULL,
3622 : : comp->ts.u.derived, &comp_tail->val,
3623 : 732 : comp->ts.u.derived->attr.zero_comp
3624 : : ? &arg_null : &actual, true);
3625 : 732 : if (!m)
3626 : 0 : goto cleanup;
3627 : :
3628 : 732 : if (comp->ts.u.derived->attr.zero_comp)
3629 : : {
3630 : 126 : comp = comp->next;
3631 : 126 : continue;
3632 : : }
3633 : : }
3634 : :
3635 : 606 : if (comp)
3636 : 8749 : comp = comp->next;
3637 : 9693 : if (parent && !comp)
3638 : : break;
3639 : :
3640 : 9088 : if (actual)
3641 : 9087 : actual = actual->next;
3642 : : }
3643 : :
3644 : 6782 : if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3645 : 5 : goto cleanup;
3646 : :
3647 : : /* No component should be left, as this should have caused an error in the
3648 : : loop constructing the component-list (name that does not correspond to any
3649 : : component in the structure definition). */
3650 : 6777 : if (comp_head && sym->attr.extension)
3651 : : {
3652 : 2 : for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3653 : : {
3654 : 1 : gfc_error ("component %qs at %L has already been set by a "
3655 : : "parent derived type constructor", comp_iter->name,
3656 : : &comp_iter->where);
3657 : : }
3658 : 1 : goto cleanup;
3659 : : }
3660 : : else
3661 : 6776 : gcc_assert (!comp_head);
3662 : :
3663 : 6776 : if (parent)
3664 : : {
3665 : 732 : expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3666 : 732 : expr->ts.u.derived = sym;
3667 : 732 : expr->value.constructor = ctor_head;
3668 : 732 : *cexpr = expr;
3669 : : }
3670 : : else
3671 : : {
3672 : 6044 : expr->ts.u.derived = sym;
3673 : 6044 : expr->ts.kind = 0;
3674 : 6044 : expr->ts.type = BT_DERIVED;
3675 : 6044 : expr->value.constructor = ctor_head;
3676 : 6044 : expr->expr_type = EXPR_STRUCTURE;
3677 : : }
3678 : :
3679 : 6776 : gfc_current_locus = old_locus;
3680 : 6776 : if (parent)
3681 : 732 : *arglist = actual;
3682 : : return true;
3683 : :
3684 : 21 : cleanup:
3685 : 21 : gfc_current_locus = old_locus;
3686 : :
3687 : 45 : for (comp_iter = comp_head; comp_iter; )
3688 : : {
3689 : 24 : gfc_structure_ctor_component *next = comp_iter->next;
3690 : 24 : gfc_free_structure_ctor_component (comp_iter);
3691 : 24 : comp_iter = next;
3692 : : }
3693 : 21 : gfc_constructor_free (ctor_head);
3694 : :
3695 : 21 : return false;
3696 : : }
3697 : :
3698 : :
3699 : : match
3700 : 60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3701 : : gfc_expr **result)
3702 : : {
3703 : 60 : match m;
3704 : 60 : gfc_expr *e;
3705 : 60 : bool t = true;
3706 : :
3707 : 60 : e = gfc_get_expr ();
3708 : 60 : e->expr_type = EXPR_FUNCTION;
3709 : 60 : e->symtree = symtree;
3710 : 60 : e->where = gfc_current_locus;
3711 : :
3712 : 60 : gcc_assert (gfc_fl_struct (sym->attr.flavor)
3713 : : && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3714 : 60 : e->value.function.esym = sym;
3715 : 60 : e->symtree->n.sym->attr.generic = 1;
3716 : :
3717 : 60 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
3718 : 60 : if (m != MATCH_YES)
3719 : : {
3720 : 0 : gfc_free_expr (e);
3721 : 0 : return m;
3722 : : }
3723 : :
3724 : 60 : if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3725 : : {
3726 : 1 : gfc_free_expr (e);
3727 : 1 : return MATCH_ERROR;
3728 : : }
3729 : :
3730 : : /* If a structure constructor is in a DATA statement, then each entity
3731 : : in the structure constructor must be a constant. Try to reduce the
3732 : : expression here. */
3733 : 59 : if (gfc_in_match_data ())
3734 : 59 : t = gfc_reduce_init_expr (e);
3735 : :
3736 : 59 : if (t)
3737 : : {
3738 : 49 : *result = e;
3739 : 49 : return MATCH_YES;
3740 : : }
3741 : : else
3742 : : {
3743 : 10 : gfc_free_expr (e);
3744 : 10 : return MATCH_ERROR;
3745 : : }
3746 : : }
3747 : :
3748 : :
3749 : : /* If the symbol is an implicit do loop index and implicitly typed,
3750 : : it should not be host associated. Provide a symtree from the
3751 : : current namespace. */
3752 : : static match
3753 : 6699944 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3754 : : {
3755 : 6699944 : if ((*sym)->attr.flavor == FL_VARIABLE
3756 : 1949821 : && (*sym)->ns != gfc_current_ns
3757 : 57932 : && (*sym)->attr.implied_index
3758 : 575 : && (*sym)->attr.implicit_type
3759 : 32 : && !(*sym)->attr.use_assoc)
3760 : : {
3761 : 32 : int i;
3762 : 32 : i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3763 : 32 : if (i)
3764 : : return MATCH_ERROR;
3765 : 32 : *sym = (*st)->n.sym;
3766 : : }
3767 : : return MATCH_YES;
3768 : : }
3769 : :
3770 : :
3771 : : /* Procedure pointer as function result: Replace the function symbol by the
3772 : : auto-generated hidden result variable named "ppr@". */
3773 : :
3774 : : static bool
3775 : 5009059 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3776 : : {
3777 : : /* Check for procedure pointer result variable. */
3778 : 5009059 : if ((*sym)->attr.function && !(*sym)->attr.external
3779 : 1369230 : && (*sym)->result && (*sym)->result != *sym
3780 : 9971 : && (*sym)->result->attr.proc_pointer
3781 : 337 : && (*sym) == gfc_current_ns->proc_name
3782 : 285 : && (*sym) == (*sym)->result->ns->proc_name
3783 : 285 : && strcmp ("ppr@", (*sym)->result->name) == 0)
3784 : : {
3785 : : /* Automatic replacement with "hidden" result variable. */
3786 : 285 : (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3787 : 285 : *sym = (*sym)->result;
3788 : 285 : *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3789 : 285 : return true;
3790 : : }
3791 : : return false;
3792 : : }
3793 : :
3794 : :
3795 : : /* Matches a variable name followed by anything that might follow it--
3796 : : array reference, argument list of a function, etc. */
3797 : :
3798 : : match
3799 : 4134070 : gfc_match_rvalue (gfc_expr **result)
3800 : : {
3801 : 4134070 : gfc_actual_arglist *actual_arglist;
3802 : 4134070 : char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3803 : 4134070 : gfc_state_data *st;
3804 : 4134070 : gfc_symbol *sym;
3805 : 4134070 : gfc_symtree *symtree;
3806 : 4134070 : locus where, old_loc;
3807 : 4134070 : gfc_expr *e;
3808 : 4134070 : match m, m2;
3809 : 4134070 : int i;
3810 : 4134070 : gfc_typespec *ts;
3811 : 4134070 : bool implicit_char;
3812 : 4134070 : gfc_ref *ref;
3813 : :
3814 : 4134070 : m = gfc_match ("%%loc");
3815 : 4134070 : if (m == MATCH_YES)
3816 : : {
3817 : 10878 : if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3818 : : return MATCH_ERROR;
3819 : 10877 : strncpy (name, "loc", 4);
3820 : : }
3821 : :
3822 : : else
3823 : : {
3824 : 4123192 : m = gfc_match_name (name);
3825 : 4123192 : if (m != MATCH_YES)
3826 : : return m;
3827 : : }
3828 : :
3829 : : /* Check if the symbol exists. */
3830 : 3936240 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3831 : : return MATCH_ERROR;
3832 : :
3833 : : /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3834 : : type. For derived types we create a generic symbol which links to the
3835 : : derived type symbol; STRUCTUREs are simpler and must not conflict with
3836 : : variables. */
3837 : 3936238 : if (!symtree)
3838 : 173368 : if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3839 : : return MATCH_ERROR;
3840 : 3936238 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3841 : : {
3842 : 3936238 : if (gfc_find_state (COMP_INTERFACE)
3843 : 3936238 : && !gfc_current_ns->has_import_set)
3844 : 91458 : i = gfc_get_sym_tree (name, NULL, &symtree, false);
3845 : : else
3846 : 3844780 : i = gfc_get_ha_sym_tree (name, &symtree);
3847 : 3936238 : if (i)
3848 : : return MATCH_ERROR;
3849 : : }
3850 : :
3851 : :
3852 : 3936238 : sym = symtree->n.sym;
3853 : 3936238 : e = NULL;
3854 : 3936238 : where = gfc_current_locus;
3855 : :
3856 : 3936238 : replace_hidden_procptr_result (&sym, &symtree);
3857 : :
3858 : : /* If this is an implicit do loop index and implicitly typed,
3859 : : it should not be host associated. */
3860 : 3936238 : m = check_for_implicit_index (&symtree, &sym);
3861 : 3936238 : if (m != MATCH_YES)
3862 : : return m;
3863 : :
3864 : 3936238 : gfc_set_sym_referenced (sym);
3865 : 3936238 : sym->attr.implied_index = 0;
3866 : :
3867 : 3936238 : if (sym->attr.function && sym->result == sym)
3868 : : {
3869 : : /* See if this is a directly recursive function call. */
3870 : 688557 : gfc_gobble_whitespace ();
3871 : 688557 : if (sym->attr.recursive
3872 : 100 : && gfc_peek_ascii_char () == '('
3873 : 93 : && gfc_current_ns->proc_name == sym
3874 : 688564 : && !sym->attr.dimension)
3875 : : {
3876 : 4 : gfc_error ("%qs at %C is the name of a recursive function "
3877 : : "and so refers to the result variable. Use an "
3878 : : "explicit RESULT variable for direct recursion "
3879 : : "(12.5.2.1)", sym->name);
3880 : 4 : return MATCH_ERROR;
3881 : : }
3882 : :
3883 : 688553 : if (gfc_is_function_return_value (sym, gfc_current_ns))
3884 : 1689 : goto variable;
3885 : :
3886 : 686864 : if (sym->attr.entry
3887 : 187 : && (sym->ns == gfc_current_ns
3888 : 27 : || sym->ns == gfc_current_ns->parent))
3889 : : {
3890 : 180 : gfc_entry_list *el = NULL;
3891 : :
3892 : 180 : for (el = sym->ns->entries; el; el = el->next)
3893 : 180 : if (sym == el->sym)
3894 : 180 : goto variable;
3895 : : }
3896 : : }
3897 : :
3898 : 3934365 : if (gfc_matching_procptr_assignment)
3899 : : {
3900 : : /* It can be a procedure or a derived-type procedure or a not-yet-known
3901 : : type. */
3902 : 1323 : if (sym->attr.flavor != FL_UNKNOWN
3903 : 983 : && sym->attr.flavor != FL_PROCEDURE
3904 : : && sym->attr.flavor != FL_PARAMETER
3905 : : && sym->attr.flavor != FL_VARIABLE)
3906 : : {
3907 : 2 : gfc_error ("Symbol at %C is not appropriate for an expression");
3908 : 2 : return MATCH_ERROR;
3909 : : }
3910 : 1321 : goto procptr0;
3911 : : }
3912 : :
3913 : 3933042 : if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3914 : 700387 : goto function0;
3915 : :
3916 : 3232655 : if (sym->attr.generic)
3917 : 67880 : goto generic_function;
3918 : :
3919 : 3164775 : switch (sym->attr.flavor)
3920 : : {
3921 : 1690285 : case FL_VARIABLE:
3922 : 1690285 : variable:
3923 : 1690285 : e = gfc_get_expr ();
3924 : :
3925 : 1690285 : e->expr_type = EXPR_VARIABLE;
3926 : 1690285 : e->symtree = symtree;
3927 : :
3928 : 1690285 : m = gfc_match_varspec (e, 0, false, true);
3929 : 1690285 : break;
3930 : :
3931 : 215104 : case FL_PARAMETER:
3932 : : /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
3933 : : end up here. Unfortunately, sym->value->expr_type is set to
3934 : : EXPR_CONSTANT, and so the if () branch would be followed without
3935 : : the !sym->as check. */
3936 : 215104 : if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
3937 : 181048 : e = gfc_copy_expr (sym->value);
3938 : : else
3939 : : {
3940 : 34056 : e = gfc_get_expr ();
3941 : 34056 : e->expr_type = EXPR_VARIABLE;
3942 : : }
3943 : :
3944 : 215104 : e->symtree = symtree;
3945 : 215104 : m = gfc_match_varspec (e, 0, false, true);
3946 : :
3947 : 215104 : if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3948 : : break;
3949 : :
3950 : : /* Variable array references to derived type parameters cause
3951 : : all sorts of headaches in simplification. Treating such
3952 : : expressions as variable works just fine for all array
3953 : : references. */
3954 : 167107 : if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
3955 : : {
3956 : 2797 : for (ref = e->ref; ref; ref = ref->next)
3957 : 2611 : if (ref->type == REF_ARRAY)
3958 : : break;
3959 : :
3960 : 2566 : if (ref == NULL || ref->u.ar.type == AR_FULL)
3961 : : break;
3962 : :
3963 : 1001 : ref = e->ref;
3964 : 1001 : e->ref = NULL;
3965 : 1001 : gfc_free_expr (e);
3966 : 1001 : e = gfc_get_expr ();
3967 : 1001 : e->expr_type = EXPR_VARIABLE;
3968 : 1001 : e->symtree = symtree;
3969 : 1001 : e->ref = ref;
3970 : : }
3971 : :
3972 : : break;
3973 : :
3974 : 0 : case FL_STRUCT:
3975 : 0 : case FL_DERIVED:
3976 : 0 : sym = gfc_use_derived (sym);
3977 : 0 : if (sym == NULL)
3978 : : m = MATCH_ERROR;
3979 : : else
3980 : 0 : goto generic_function;
3981 : : break;
3982 : :
3983 : : /* If we're here, then the name is known to be the name of a
3984 : : procedure, yet it is not sure to be the name of a function. */
3985 : 981781 : case FL_PROCEDURE:
3986 : :
3987 : : /* Procedure Pointer Assignments. */
3988 : 981781 : procptr0:
3989 : 981781 : if (gfc_matching_procptr_assignment)
3990 : : {
3991 : 1321 : gfc_gobble_whitespace ();
3992 : 1321 : if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
3993 : : /* Parse functions returning a procptr. */
3994 : 209 : goto function0;
3995 : :
3996 : 1112 : e = gfc_get_expr ();
3997 : 1112 : e->expr_type = EXPR_VARIABLE;
3998 : 1112 : e->symtree = symtree;
3999 : 1112 : m = gfc_match_varspec (e, 0, false, true);
4000 : 1044 : if (!e->ref && sym->attr.flavor == FL_UNKNOWN
4001 : 196 : && sym->ts.type == BT_UNKNOWN
4002 : 1298 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4003 : : {
4004 : : m = MATCH_ERROR;
4005 : : break;
4006 : : }
4007 : : break;
4008 : : }
4009 : :
4010 : 980460 : if (sym->attr.subroutine)
4011 : : {
4012 : 57 : gfc_error ("Unexpected use of subroutine name %qs at %C",
4013 : : sym->name);
4014 : 57 : m = MATCH_ERROR;
4015 : 57 : break;
4016 : : }
4017 : :
4018 : : /* At this point, the name has to be a non-statement function.
4019 : : If the name is the same as the current function being
4020 : : compiled, then we have a variable reference (to the function
4021 : : result) if the name is non-recursive. */
4022 : :
4023 : 980403 : st = gfc_enclosing_unit (NULL);
4024 : :
4025 : 980403 : if (st != NULL
4026 : 937332 : && st->state == COMP_FUNCTION
4027 : 81276 : && st->sym == sym
4028 : 0 : && !sym->attr.recursive)
4029 : : {
4030 : 0 : e = gfc_get_expr ();
4031 : 0 : e->symtree = symtree;
4032 : 0 : e->expr_type = EXPR_VARIABLE;
4033 : :
4034 : 0 : m = gfc_match_varspec (e, 0, false, true);
4035 : 0 : break;
4036 : : }
4037 : :
4038 : : /* Match a function reference. */
4039 : 980403 : function0:
4040 : 1680999 : m = gfc_match_actual_arglist (0, &actual_arglist);
4041 : 1680979 : if (m == MATCH_NO)
4042 : : {
4043 : 579329 : if (sym->attr.proc == PROC_ST_FUNCTION)
4044 : 1 : gfc_error ("Statement function %qs requires argument list at %C",
4045 : : sym->name);
4046 : : else
4047 : 579328 : gfc_error ("Function %qs requires an argument list at %C",
4048 : : sym->name);
4049 : :
4050 : : m = MATCH_ERROR;
4051 : : break;
4052 : : }
4053 : :
4054 : 1101650 : if (m != MATCH_YES)
4055 : : {
4056 : : m = MATCH_ERROR;
4057 : : break;
4058 : : }
4059 : :
4060 : : /* Check to see if this is a PDT constructor. The format of these
4061 : : constructors is rather unusual:
4062 : : name [(type_params)](component_values)
4063 : : where, component_values excludes the type_params. With the present
4064 : : gfortran representation this is rather awkward because the two are not
4065 : : distinguished, other than by their attributes. */
4066 : 1070158 : if (sym->attr.generic)
4067 : : {
4068 : 10092 : gfc_symtree *pdt_st;
4069 : 10092 : gfc_symbol *pdt_sym;
4070 : 10092 : gfc_actual_arglist *ctr_arglist, *tmp;
4071 : 10092 : gfc_component *c;
4072 : :
4073 : : /* Obtain the template. */
4074 : 10092 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &pdt_st);
4075 : 10092 : if (pdt_st && pdt_st->n.sym && pdt_st->n.sym->attr.pdt_template)
4076 : : {
4077 : 466 : bool type_spec_list = false;
4078 : 466 : pdt_sym = pdt_st->n.sym;
4079 : 466 : gfc_gobble_whitespace ();
4080 : : /* Look for a second actual arglist. If present, try the first
4081 : : for the type parameters. Otherwise, or if there is no match,
4082 : : depend on default values by setting the type parameters to
4083 : : NULL. */
4084 : 466 : if (gfc_peek_ascii_char() == '(')
4085 : 54 : type_spec_list = true;
4086 : :
4087 : : /* Generate this instance using the type parameters from the
4088 : : first argument list and return the parameter list in
4089 : : ctr_arglist. */
4090 : 466 : m = gfc_get_pdt_instance (actual_arglist, &pdt_sym, &ctr_arglist);
4091 : 466 : if (m != MATCH_YES)
4092 : : {
4093 : 26 : if (ctr_arglist)
4094 : 26 : gfc_free_actual_arglist (ctr_arglist);
4095 : : /* See if all the type parameters have default values. */
4096 : 0 : m = gfc_get_pdt_instance (NULL, &pdt_sym, &ctr_arglist);
4097 : 0 : if (m != MATCH_YES)
4098 : : {
4099 : : m = MATCH_NO;
4100 : 0 : break;
4101 : : }
4102 : : }
4103 : :
4104 : : /* Now match the component_values if the type parameters were
4105 : : present. */
4106 : 440 : if (type_spec_list)
4107 : : {
4108 : 54 : m = gfc_match_actual_arglist (0, &actual_arglist);
4109 : 54 : if (m != MATCH_YES)
4110 : : {
4111 : : m = MATCH_ERROR;
4112 : : break;
4113 : : }
4114 : : }
4115 : :
4116 : : /* Make sure that the component names are in place so that this
4117 : : list can be safely appended to the type parameters. */
4118 : 440 : tmp = actual_arglist;
4119 : 1571 : for (c = pdt_sym->components; c && tmp; c = c->next)
4120 : : {
4121 : 1131 : if (c->attr.pdt_kind || c->attr.pdt_len)
4122 : 630 : continue;
4123 : 501 : tmp->name = c->name;
4124 : 501 : tmp = tmp->next;
4125 : : }
4126 : :
4127 : 440 : gfc_find_sym_tree (gfc_dt_lower_string (pdt_sym->name),
4128 : : NULL, 1, &symtree);
4129 : 440 : if (!symtree)
4130 : : {
4131 : 266 : gfc_get_ha_sym_tree (gfc_dt_lower_string (pdt_sym->name) ,
4132 : : &symtree);
4133 : 266 : symtree->n.sym = pdt_sym;
4134 : 266 : symtree->n.sym->ts.u.derived = pdt_sym;
4135 : 266 : symtree->n.sym->ts.type = BT_DERIVED;
4136 : : }
4137 : :
4138 : : /* Append the type_params and the component_values. */
4139 : 706 : for (tmp = ctr_arglist; tmp && tmp->next;)
4140 : : tmp = tmp->next;
4141 : 440 : tmp->next = actual_arglist;
4142 : 440 : actual_arglist = ctr_arglist;
4143 : : }
4144 : : }
4145 : :
4146 : 1070132 : gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4147 : 1070132 : sym = symtree->n.sym;
4148 : :
4149 : 1070132 : replace_hidden_procptr_result (&sym, &symtree);
4150 : :
4151 : 1070132 : e = gfc_get_expr ();
4152 : 1070132 : e->symtree = symtree;
4153 : 1070132 : e->expr_type = EXPR_FUNCTION;
4154 : 1070132 : e->value.function.actual = actual_arglist;
4155 : 1070132 : e->where = gfc_current_locus;
4156 : :
4157 : 1070132 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4158 : 206 : && CLASS_DATA (sym)->as)
4159 : : {
4160 : 91 : e->rank = CLASS_DATA (sym)->as->rank;
4161 : 91 : e->corank = CLASS_DATA (sym)->as->corank;
4162 : : }
4163 : 1070041 : else if (sym->as != NULL)
4164 : : {
4165 : 1133 : e->rank = sym->as->rank;
4166 : 1133 : e->corank = sym->as->corank;
4167 : : }
4168 : :
4169 : 1070132 : if (!sym->attr.function
4170 : 1070132 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4171 : : {
4172 : : m = MATCH_ERROR;
4173 : : break;
4174 : : }
4175 : :
4176 : : /* Check here for the existence of at least one argument for the
4177 : : iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4178 : 1070132 : if (sym->attr.is_iso_c == 1
4179 : 2 : && (sym->from_intmod == INTMOD_ISO_C_BINDING
4180 : 2 : && (sym->intmod_sym_id == ISOCBINDING_LOC
4181 : 2 : || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4182 : 2 : || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4183 : 2 : || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4184 : : {
4185 : : /* make sure we were given a param */
4186 : 0 : if (actual_arglist == NULL)
4187 : : {
4188 : 0 : gfc_error ("Missing argument to %qs at %C", sym->name);
4189 : 0 : m = MATCH_ERROR;
4190 : 0 : break;
4191 : : }
4192 : : }
4193 : :
4194 : 1070132 : if (sym->result == NULL)
4195 : 377188 : sym->result = sym;
4196 : :
4197 : 1070132 : gfc_gobble_whitespace ();
4198 : : /* F08:C612. */
4199 : 1070132 : if (gfc_peek_ascii_char() == '%')
4200 : : {
4201 : 12 : gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4202 : : "function reference at %C");
4203 : 12 : m = MATCH_ERROR;
4204 : 12 : break;
4205 : : }
4206 : :
4207 : : m = MATCH_YES;
4208 : : break;
4209 : :
4210 : 279299 : case FL_UNKNOWN:
4211 : :
4212 : : /* Special case for derived type variables that get their types
4213 : : via an IMPLICIT statement. This can't wait for the
4214 : : resolution phase. */
4215 : :
4216 : 279299 : old_loc = gfc_current_locus;
4217 : 279299 : if (gfc_match_member_sep (sym) == MATCH_YES
4218 : 9837 : && sym->ts.type == BT_UNKNOWN
4219 : 279304 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4220 : 0 : gfc_set_default_type (sym, 0, sym->ns);
4221 : 279299 : gfc_current_locus = old_loc;
4222 : :
4223 : : /* If the symbol has a (co)dimension attribute, the expression is a
4224 : : variable. */
4225 : :
4226 : 279299 : if (sym->attr.dimension || sym->attr.codimension)
4227 : : {
4228 : 34833 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4229 : : {
4230 : : m = MATCH_ERROR;
4231 : : break;
4232 : : }
4233 : :
4234 : 34833 : e = gfc_get_expr ();
4235 : 34833 : e->symtree = symtree;
4236 : 34833 : e->expr_type = EXPR_VARIABLE;
4237 : 34833 : m = gfc_match_varspec (e, 0, false, true);
4238 : 34833 : break;
4239 : : }
4240 : :
4241 : 244466 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4242 : 4600 : && (CLASS_DATA (sym)->attr.dimension
4243 : 3212 : || CLASS_DATA (sym)->attr.codimension))
4244 : : {
4245 : 1475 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4246 : : {
4247 : : m = MATCH_ERROR;
4248 : : break;
4249 : : }
4250 : :
4251 : 1475 : e = gfc_get_expr ();
4252 : 1475 : e->symtree = symtree;
4253 : 1475 : e->expr_type = EXPR_VARIABLE;
4254 : 1475 : m = gfc_match_varspec (e, 0, false, true);
4255 : 1475 : break;
4256 : : }
4257 : :
4258 : : /* Name is not an array, so we peek to see if a '(' implies a
4259 : : function call or a substring reference. Otherwise the
4260 : : variable is just a scalar. */
4261 : :
4262 : 242991 : gfc_gobble_whitespace ();
4263 : 242991 : if (gfc_peek_ascii_char () != '(')
4264 : : {
4265 : : /* Assume a scalar variable */
4266 : 73772 : e = gfc_get_expr ();
4267 : 73772 : e->symtree = symtree;
4268 : 73772 : e->expr_type = EXPR_VARIABLE;
4269 : :
4270 : 73772 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4271 : : {
4272 : : m = MATCH_ERROR;
4273 : : break;
4274 : : }
4275 : :
4276 : : /*FIXME:??? gfc_match_varspec does set this for us: */
4277 : 73772 : e->ts = sym->ts;
4278 : 73772 : m = gfc_match_varspec (e, 0, false, true);
4279 : 73772 : break;
4280 : : }
4281 : :
4282 : : /* See if this is a function reference with a keyword argument
4283 : : as first argument. We do this because otherwise a spurious
4284 : : symbol would end up in the symbol table. */
4285 : :
4286 : 169219 : old_loc = gfc_current_locus;
4287 : 169219 : m2 = gfc_match (" ( %n =", argname);
4288 : 169219 : gfc_current_locus = old_loc;
4289 : :
4290 : 169219 : e = gfc_get_expr ();
4291 : 169219 : e->symtree = symtree;
4292 : :
4293 : 169219 : if (m2 != MATCH_YES)
4294 : : {
4295 : : /* Try to figure out whether we're dealing with a character type.
4296 : : We're peeking ahead here, because we don't want to call
4297 : : match_substring if we're dealing with an implicitly typed
4298 : : non-character variable. */
4299 : 168157 : implicit_char = false;
4300 : 168157 : if (sym->ts.type == BT_UNKNOWN)
4301 : : {
4302 : 163458 : ts = gfc_get_default_type (sym->name, NULL);
4303 : 163458 : if (ts->type == BT_CHARACTER)
4304 : : implicit_char = true;
4305 : : }
4306 : :
4307 : : /* See if this could possibly be a substring reference of a name
4308 : : that we're not sure is a variable yet. */
4309 : :
4310 : 168140 : if ((implicit_char || sym->ts.type == BT_CHARACTER)
4311 : 1374 : && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4312 : : {
4313 : :
4314 : 910 : e->expr_type = EXPR_VARIABLE;
4315 : :
4316 : 910 : if (sym->attr.flavor != FL_VARIABLE
4317 : 910 : && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4318 : : sym->name, NULL))
4319 : : {
4320 : : m = MATCH_ERROR;
4321 : : break;
4322 : : }
4323 : :
4324 : 910 : if (sym->ts.type == BT_UNKNOWN
4325 : 910 : && !gfc_set_default_type (sym, 1, NULL))
4326 : : {
4327 : : m = MATCH_ERROR;
4328 : : break;
4329 : : }
4330 : :
4331 : 910 : e->ts = sym->ts;
4332 : 910 : if (e->ref)
4333 : 885 : e->ts.u.cl = NULL;
4334 : : m = MATCH_YES;
4335 : : break;
4336 : : }
4337 : : }
4338 : :
4339 : : /* Give up, assume we have a function. */
4340 : :
4341 : 168309 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4342 : 168309 : sym = symtree->n.sym;
4343 : 168309 : e->expr_type = EXPR_FUNCTION;
4344 : :
4345 : 168309 : if (!sym->attr.function
4346 : 168309 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4347 : : {
4348 : : m = MATCH_ERROR;
4349 : : break;
4350 : : }
4351 : :
4352 : 168309 : sym->result = sym;
4353 : :
4354 : 168309 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4355 : 168309 : if (m == MATCH_NO)
4356 : 0 : gfc_error ("Missing argument list in function %qs at %C", sym->name);
4357 : :
4358 : 168309 : if (m != MATCH_YES)
4359 : : {
4360 : : m = MATCH_ERROR;
4361 : : break;
4362 : : }
4363 : :
4364 : : /* If our new function returns a character, array or structure
4365 : : type, it might have subsequent references. */
4366 : :
4367 : 168179 : m = gfc_match_varspec (e, 0, false, true);
4368 : 168179 : if (m == MATCH_NO)
4369 : : m = MATCH_YES;
4370 : :
4371 : : break;
4372 : :
4373 : 67880 : generic_function:
4374 : : /* Look for symbol first; if not found, look for STRUCTURE type symbol
4375 : : specially. Creates a generic symbol for derived types. */
4376 : 67880 : gfc_find_sym_tree (name, NULL, 1, &symtree);
4377 : 67880 : if (!symtree)
4378 : 0 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4379 : 67880 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4380 : 67880 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4381 : :
4382 : 67880 : e = gfc_get_expr ();
4383 : 67880 : e->symtree = symtree;
4384 : 67880 : e->expr_type = EXPR_FUNCTION;
4385 : :
4386 : 67880 : if (gfc_fl_struct (sym->attr.flavor))
4387 : : {
4388 : 0 : e->value.function.esym = sym;
4389 : 0 : e->symtree->n.sym->attr.generic = 1;
4390 : : }
4391 : :
4392 : 67880 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4393 : 67880 : break;
4394 : :
4395 : : case FL_NAMELIST:
4396 : : m = MATCH_ERROR;
4397 : : break;
4398 : :
4399 : 5 : default:
4400 : 5 : gfc_error ("Symbol at %C is not appropriate for an expression");
4401 : 5 : return MATCH_ERROR;
4402 : : }
4403 : :
4404 : : /* Scan for possible inquiry references. */
4405 : 69 : if (m == MATCH_YES
4406 : 3323503 : && e->expr_type == EXPR_VARIABLE
4407 : 4085654 : && gfc_peek_ascii_char () == '%')
4408 : : {
4409 : 14 : m = gfc_match_varspec (e, 0, false, false);
4410 : 14 : if (m == MATCH_NO)
4411 : : m = MATCH_YES;
4412 : : }
4413 : :
4414 : 3936181 : if (m == MATCH_YES)
4415 : : {
4416 : 3323503 : e->where = where;
4417 : 3323503 : *result = e;
4418 : : }
4419 : : else
4420 : 612678 : gfc_free_expr (e);
4421 : :
4422 : : return m;
4423 : : }
4424 : :
4425 : :
4426 : : /* Match a variable, i.e. something that can be assigned to. This
4427 : : starts as a symbol, can be a structure component or an array
4428 : : reference. It can be a function if the function doesn't have a
4429 : : separate RESULT variable. If the symbol has not been previously
4430 : : seen, we assume it is a variable.
4431 : :
4432 : : This function is called by two interface functions:
4433 : : gfc_match_variable, which has host_flag = 1, and
4434 : : gfc_match_equiv_variable, with host_flag = 0, to restrict the
4435 : : match of the symbol to the local scope. */
4436 : :
4437 : : static match
4438 : 2763732 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4439 : : {
4440 : 2763732 : gfc_symbol *sym, *dt_sym;
4441 : 2763732 : gfc_symtree *st;
4442 : 2763732 : gfc_expr *expr;
4443 : 2763732 : locus where, old_loc;
4444 : 2763732 : match m;
4445 : :
4446 : 2763732 : *result = NULL;
4447 : :
4448 : : /* Since nothing has any business being an lvalue in a module
4449 : : specification block, an interface block or a contains section,
4450 : : we force the changed_symbols mechanism to work by setting
4451 : : host_flag to 0. This prevents valid symbols that have the name
4452 : : of keywords, such as 'end', being turned into variables by
4453 : : failed matching to assignments for, e.g., END INTERFACE. */
4454 : 2763732 : if (gfc_current_state () == COMP_MODULE
4455 : 2763732 : || gfc_current_state () == COMP_SUBMODULE
4456 : : || gfc_current_state () == COMP_INTERFACE
4457 : : || gfc_current_state () == COMP_CONTAINS)
4458 : 188003 : host_flag = 0;
4459 : :
4460 : 2763732 : where = gfc_current_locus;
4461 : 2763732 : m = gfc_match_sym_tree (&st, host_flag);
4462 : 2763731 : if (m != MATCH_YES)
4463 : : return m;
4464 : :
4465 : 2763706 : sym = st->n.sym;
4466 : :
4467 : : /* If this is an implicit do loop index and implicitly typed,
4468 : : it should not be host associated. */
4469 : 2763706 : m = check_for_implicit_index (&st, &sym);
4470 : 2763706 : if (m != MATCH_YES)
4471 : : return m;
4472 : :
4473 : 2763706 : sym->attr.implied_index = 0;
4474 : :
4475 : 2763706 : gfc_set_sym_referenced (sym);
4476 : :
4477 : : /* STRUCTUREs may share names with variables, but derived types may not. */
4478 : 13866 : if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4479 : 2763771 : && (dt_sym = gfc_find_dt_in_generic (sym)))
4480 : : {
4481 : 4 : if (dt_sym->attr.flavor == FL_DERIVED)
4482 : 4 : gfc_error ("Derived type %qs cannot be used as a variable at %C",
4483 : : sym->name);
4484 : 4 : return MATCH_ERROR;
4485 : : }
4486 : :
4487 : 2763702 : switch (sym->attr.flavor)
4488 : : {
4489 : : case FL_VARIABLE:
4490 : : /* Everything is alright. */
4491 : : break;
4492 : :
4493 : 2488316 : case FL_UNKNOWN:
4494 : 2488316 : {
4495 : 2488316 : sym_flavor flavor = FL_UNKNOWN;
4496 : :
4497 : 2488316 : gfc_gobble_whitespace ();
4498 : :
4499 : 2488316 : if (sym->attr.external || sym->attr.procedure
4500 : 2488284 : || sym->attr.function || sym->attr.subroutine)
4501 : : flavor = FL_PROCEDURE;
4502 : :
4503 : : /* If it is not a procedure, is not typed and is host associated,
4504 : : we cannot give it a flavor yet. */
4505 : 2488284 : else if (sym->ns == gfc_current_ns->parent
4506 : 2678 : && sym->ts.type == BT_UNKNOWN)
4507 : : break;
4508 : :
4509 : : /* These are definitive indicators that this is a variable. */
4510 : 3313355 : else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4511 : 3295830 : || sym->attr.pointer || sym->as != NULL)
4512 : : flavor = FL_VARIABLE;
4513 : :
4514 : : if (flavor != FL_UNKNOWN
4515 : 1681374 : && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4516 : : return MATCH_ERROR;
4517 : : }
4518 : : break;
4519 : :
4520 : 17 : case FL_PARAMETER:
4521 : 17 : if (equiv_flag)
4522 : : {
4523 : 0 : gfc_error ("Named constant at %C in an EQUIVALENCE");
4524 : 0 : return MATCH_ERROR;
4525 : : }
4526 : 17 : if (gfc_in_match_data())
4527 : : {
4528 : 4 : gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4529 : : sym->name);
4530 : 4 : return MATCH_ERROR;
4531 : : }
4532 : : /* Otherwise this is checked for an error given in the
4533 : : variable definition context checks. */
4534 : : break;
4535 : :
4536 : 13862 : case FL_PROCEDURE:
4537 : : /* Check for a nonrecursive function result variable. */
4538 : 13862 : if (sym->attr.function
4539 : 12013 : && (!sym->attr.external || sym->abr_modproc_decl)
4540 : 11616 : && sym->result == sym
4541 : 25129 : && (gfc_is_function_return_value (sym, gfc_current_ns)
4542 : 2169 : || (sym->attr.entry
4543 : 467 : && sym->ns == gfc_current_ns)
4544 : 1709 : || (sym->attr.entry
4545 : 7 : && sym->ns == gfc_current_ns->parent)))
4546 : : {
4547 : : /* If a function result is a derived type, then the derived
4548 : : type may still have to be resolved. */
4549 : :
4550 : 9565 : if (sym->ts.type == BT_DERIVED
4551 : 9565 : && gfc_use_derived (sym->ts.u.derived) == NULL)
4552 : : return MATCH_ERROR;
4553 : : break;
4554 : : }
4555 : :
4556 : 4297 : if (sym->attr.proc_pointer
4557 : 4297 : || replace_hidden_procptr_result (&sym, &st))
4558 : : break;
4559 : :
4560 : : /* Fall through to error */
4561 : 2654 : gcc_fallthrough ();
4562 : :
4563 : 2654 : default:
4564 : 2654 : gfc_error ("%qs at %C is not a variable", sym->name);
4565 : 2654 : return MATCH_ERROR;
4566 : : }
4567 : :
4568 : : /* Special case for derived type variables that get their types
4569 : : via an IMPLICIT statement. This can't wait for the
4570 : : resolution phase. */
4571 : :
4572 : 2761040 : {
4573 : 2761040 : gfc_namespace * implicit_ns;
4574 : :
4575 : 2761040 : if (gfc_current_ns->proc_name == sym)
4576 : : implicit_ns = gfc_current_ns;
4577 : : else
4578 : 2752292 : implicit_ns = sym->ns;
4579 : :
4580 : 2761040 : old_loc = gfc_current_locus;
4581 : 2761040 : if (gfc_match_member_sep (sym) == MATCH_YES
4582 : 19272 : && sym->ts.type == BT_UNKNOWN
4583 : 2761052 : && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4584 : 3 : gfc_set_default_type (sym, 0, implicit_ns);
4585 : 2761040 : gfc_current_locus = old_loc;
4586 : : }
4587 : :
4588 : 2761040 : expr = gfc_get_expr ();
4589 : :
4590 : 2761040 : expr->expr_type = EXPR_VARIABLE;
4591 : 2761040 : expr->symtree = st;
4592 : 2761040 : expr->ts = sym->ts;
4593 : :
4594 : : /* Now see if we have to do more. */
4595 : 2761040 : m = gfc_match_varspec (expr, equiv_flag, false, false);
4596 : 2761040 : if (m != MATCH_YES)
4597 : : {
4598 : 79 : gfc_free_expr (expr);
4599 : 79 : return m;
4600 : : }
4601 : :
4602 : 2760961 : expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4603 : 2760961 : *result = expr;
4604 : 2760961 : return MATCH_YES;
4605 : : }
4606 : :
4607 : :
4608 : : match
4609 : 2760787 : gfc_match_variable (gfc_expr **result, int equiv_flag)
4610 : : {
4611 : 2760787 : return match_variable (result, equiv_flag, 1);
4612 : : }
4613 : :
4614 : :
4615 : : match
4616 : 2945 : gfc_match_equiv_variable (gfc_expr **result)
4617 : : {
4618 : 2945 : return match_variable (result, 1, 0);
4619 : : }
|