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 : 469037 : match_kind_param (int *kind, int *is_iso_c)
41 : : {
42 : 469037 : char name[GFC_MAX_SYMBOL_LEN + 1];
43 : 469037 : gfc_symbol *sym;
44 : 469037 : match m;
45 : :
46 : 469037 : *is_iso_c = 0;
47 : :
48 : 469037 : m = gfc_match_small_literal_int (kind, NULL, false);
49 : 469037 : if (m != MATCH_NO)
50 : : return m;
51 : :
52 : 93933 : m = gfc_match_name (name, false);
53 : 93933 : if (m != MATCH_YES)
54 : : return m;
55 : :
56 : 92201 : if (gfc_find_symbol (name, NULL, 1, &sym))
57 : : return MATCH_ERROR;
58 : :
59 : 92201 : if (sym == NULL)
60 : : return MATCH_NO;
61 : :
62 : 92200 : *is_iso_c = sym->attr.is_iso_c;
63 : :
64 : 92200 : if (sym->attr.flavor != FL_PARAMETER)
65 : : return MATCH_NO;
66 : :
67 : 92200 : if (sym->value == NULL)
68 : : return MATCH_NO;
69 : :
70 : 92199 : if (gfc_extract_int (sym->value, kind))
71 : : return MATCH_NO;
72 : :
73 : 92199 : gfc_set_sym_referenced (sym);
74 : :
75 : 92199 : 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 : 4335516 : get_kind (int *is_iso_c)
92 : : {
93 : 4335516 : int kind;
94 : 4335516 : match m;
95 : :
96 : 4335516 : *is_iso_c = 0;
97 : :
98 : 4335516 : if (gfc_match_char ('_', false) != MATCH_YES)
99 : : return -2;
100 : :
101 : 469037 : m = match_kind_param (&kind, is_iso_c);
102 : 469037 : if (m == MATCH_NO)
103 : 1734 : gfc_error ("Missing kind-parameter at %C");
104 : :
105 : 469037 : 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 : 29402360 : gfc_check_digit (char c, int radix)
114 : : {
115 : 29402360 : bool r;
116 : :
117 : 29402360 : 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 : 29303219 : case 10:
128 : 29303219 : r = ('0' <= c && c <= '9');
129 : 29303219 : break;
130 : :
131 : 64321 : case 16:
132 : 64321 : r = ISXDIGIT (c);
133 : 64321 : break;
134 : :
135 : 0 : default:
136 : 0 : gfc_internal_error ("gfc_check_digit(): bad radix");
137 : : }
138 : :
139 : 29402360 : 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 : 17141835 : match_digits (int signflag, int radix, char *buffer)
150 : : {
151 : 17141835 : locus old_loc;
152 : 17141835 : int length;
153 : 17141835 : char c;
154 : :
155 : 17141835 : length = 0;
156 : 17141835 : c = gfc_next_ascii_char ();
157 : :
158 : 17141835 : if (signflag && (c == '+' || c == '-'))
159 : : {
160 : 4761 : if (buffer != NULL)
161 : 1863 : *buffer++ = c;
162 : 4761 : gfc_gobble_whitespace ();
163 : 4761 : c = gfc_next_ascii_char ();
164 : 4761 : length++;
165 : : }
166 : :
167 : 17141835 : if (!gfc_check_digit (c, radix))
168 : : return -1;
169 : :
170 : 8183206 : length++;
171 : 8183206 : if (buffer != NULL)
172 : 4083679 : *buffer++ = c;
173 : :
174 : 16287380 : for (;;)
175 : : {
176 : 12235293 : old_loc = gfc_current_locus;
177 : 12235293 : c = gfc_next_ascii_char ();
178 : :
179 : 12235293 : if (!gfc_check_digit (c, radix))
180 : : break;
181 : :
182 : 4052087 : if (buffer != NULL)
183 : 2023910 : *buffer++ = c;
184 : 4052087 : length++;
185 : : }
186 : :
187 : 8183206 : gfc_current_locus = old_loc;
188 : :
189 : 8183206 : return length;
190 : : }
191 : :
192 : : /* Convert an integer string to an expression node. */
193 : :
194 : : static gfc_expr *
195 : 3976544 : convert_integer (const char *buffer, int kind, int radix, locus *where)
196 : : {
197 : 3976544 : gfc_expr *e;
198 : 3976544 : const char *t;
199 : :
200 : 3976544 : e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 : : /* A leading plus is allowed, but not by mpz_set_str. */
202 : 3976544 : if (buffer[0] == '+')
203 : 21 : t = buffer + 1;
204 : : else
205 : : t = buffer;
206 : 3976544 : mpz_set_str (e->value.integer, t, radix);
207 : :
208 : 3976544 : 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 : 101292 : convert_unsigned (const char *buffer, int kind, int radix, locus *where)
217 : : {
218 : 101292 : gfc_expr *e;
219 : 101292 : const char *t;
220 : 101292 : int k;
221 : 101292 : arith rc;
222 : :
223 : 101292 : e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
224 : : /* A leading plus is allowed, but not by mpz_set_str. */
225 : 101292 : if (buffer[0] == '+')
226 : 0 : t = buffer + 1;
227 : : else
228 : : t = buffer;
229 : :
230 : 101292 : mpz_set_str (e->value.integer, t, radix);
231 : :
232 : 101292 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
233 : :
234 : : /* TODO Maybe move this somewhere else. */
235 : 101292 : rc = gfc_range_check (e);
236 : 101292 : 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 : 101292 : gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
245 : : false);
246 : :
247 : 101292 : return e;
248 : : }
249 : :
250 : : /* Convert a real string to an expression node. */
251 : :
252 : : static gfc_expr *
253 : 210972 : convert_real (const char *buffer, int kind, locus *where)
254 : : {
255 : 210972 : gfc_expr *e;
256 : :
257 : 210972 : e = gfc_get_constant_expr (BT_REAL, kind, where);
258 : 210972 : mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259 : :
260 : 210972 : 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 : 6477 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 : : {
270 : 6477 : gfc_expr *e;
271 : :
272 : 6477 : e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 : 6477 : mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 : : GFC_MPC_RND_MODE);
275 : :
276 : 6477 : 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 : 12466104 : match_integer_constant (gfc_expr **result, int signflag)
285 : : {
286 : 12466104 : int length, kind, is_iso_c;
287 : 12466104 : locus old_loc;
288 : 12466104 : char *buffer;
289 : 12466104 : gfc_expr *e;
290 : :
291 : 12466104 : old_loc = gfc_current_locus;
292 : 12466104 : gfc_gobble_whitespace ();
293 : :
294 : 12466104 : length = match_digits (signflag, 10, NULL);
295 : 12466104 : gfc_current_locus = old_loc;
296 : 12466104 : if (length == -1)
297 : : return MATCH_NO;
298 : :
299 : 3978278 : buffer = (char *) alloca (length + 1);
300 : 3978278 : memset (buffer, '\0', length + 1);
301 : :
302 : 3978278 : gfc_gobble_whitespace ();
303 : :
304 : 3978278 : match_digits (signflag, 10, buffer);
305 : :
306 : 3978278 : kind = get_kind (&is_iso_c);
307 : 3978278 : if (kind == -2)
308 : 3672779 : kind = gfc_default_integer_kind;
309 : 3978278 : if (kind == -1)
310 : : return MATCH_ERROR;
311 : :
312 : 3976548 : if (kind == 4 && flag_integer4_kind == 8)
313 : 0 : kind = 8;
314 : :
315 : 3976548 : 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 : 3976544 : e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 : 3976544 : e->ts.is_c_interop = is_iso_c;
323 : :
324 : 3976544 : 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 : 3966958 : *result = e;
334 : 3966958 : 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 : 587942 : match_unsigned_constant (gfc_expr **result)
345 : : {
346 : 587942 : int length, kind, is_iso_c;
347 : 587942 : locus old_loc;
348 : 587942 : char *buffer;
349 : 587942 : gfc_expr *e;
350 : 587942 : match m;
351 : :
352 : 587942 : old_loc = gfc_current_locus;
353 : 587942 : gfc_gobble_whitespace ();
354 : :
355 : 587942 : length = match_digits (/* signflag = */ false, 10, NULL);
356 : :
357 : 587942 : if (length == -1)
358 : 470803 : goto fail;
359 : :
360 : 117139 : m = gfc_match_char ('u');
361 : 117139 : if (m == MATCH_NO)
362 : 15847 : goto fail;
363 : :
364 : 101292 : gfc_current_locus = old_loc;
365 : :
366 : 101292 : buffer = (char *) alloca (length + 1);
367 : 101292 : memset (buffer, '\0', length + 1);
368 : :
369 : 101292 : gfc_gobble_whitespace ();
370 : :
371 : 101292 : match_digits (false, 10, buffer);
372 : :
373 : 101292 : m = gfc_match_char ('u');
374 : 101292 : if (m == MATCH_NO)
375 : 0 : goto fail;
376 : :
377 : 101292 : kind = get_kind (&is_iso_c);
378 : 101292 : if (kind == -2)
379 : 8357 : kind = gfc_default_unsigned_kind;
380 : 101292 : if (kind == -1)
381 : : return MATCH_ERROR;
382 : :
383 : 101292 : if (kind == 4 && flag_integer4_kind == 8)
384 : 0 : kind = 8;
385 : :
386 : 101292 : 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 : 101292 : e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
393 : 101292 : e->ts.is_c_interop = is_iso_c;
394 : :
395 : 101292 : *result = e;
396 : 101292 : return MATCH_YES;
397 : :
398 : 486650 : fail:
399 : 486650 : gfc_current_locus = old_loc;
400 : 486650 : return MATCH_NO;
401 : : }
402 : :
403 : : /* Match a Hollerith constant. */
404 : :
405 : : static match
406 : 6239569 : match_hollerith_constant (gfc_expr **result)
407 : : {
408 : 6239569 : locus old_loc;
409 : 6239569 : gfc_expr *e = NULL;
410 : 6239569 : int num, pad;
411 : 6239569 : int i;
412 : :
413 : 6239569 : old_loc = gfc_current_locus;
414 : 6239569 : gfc_gobble_whitespace ();
415 : :
416 : 6239569 : if (match_integer_constant (&e, 0) == MATCH_YES
417 : 6239569 : && gfc_match_char ('h') == MATCH_YES)
418 : : {
419 : 2639 : if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
420 : 17 : 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 : 6236930 : gfc_free_expr (e);
474 : 6236930 : gfc_current_locus = old_loc;
475 : 6236930 : return MATCH_NO;
476 : :
477 : 19 : cleanup:
478 : 19 : gfc_free_expr (e);
479 : 19 : 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 : 6441519 : match_boz_constant (gfc_expr **result)
491 : : {
492 : 6441519 : int radix, length, x_hex;
493 : 6441519 : locus old_loc, start_loc;
494 : 6441519 : char *buffer, post, delim;
495 : 6441519 : gfc_expr *e;
496 : :
497 : 6441519 : start_loc = old_loc = gfc_current_locus;
498 : 6441519 : gfc_gobble_whitespace ();
499 : :
500 : 6441519 : x_hex = 0;
501 : 6441519 : switch (post = gfc_next_ascii_char ())
502 : : {
503 : : case 'b':
504 : : radix = 2;
505 : : post = 0;
506 : : break;
507 : 53093 : case 'o':
508 : 53093 : radix = 8;
509 : 53093 : post = 0;
510 : 53093 : break;
511 : 88612 : case 'x':
512 : 88612 : 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 : 6169943 : default:
526 : 6169943 : goto backup;
527 : : }
528 : :
529 : : /* No whitespace allowed here. */
530 : :
531 : 53093 : if (post == 0)
532 : 271551 : delim = gfc_next_ascii_char ();
533 : :
534 : 271576 : if (delim != '\'' && delim != '\"')
535 : 267464 : goto backup;
536 : :
537 : 4112 : if (x_hex
538 : 4112 : && gfc_invalid_boz (G_("Hexadecimal constant at %L uses "
539 : : "nonstandard X instead of Z"), &gfc_current_locus))
540 : : return MATCH_ERROR;
541 : :
542 : 4110 : old_loc = gfc_current_locus;
543 : :
544 : 4110 : length = match_digits (0, radix, NULL);
545 : 4110 : 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 : 4110 : 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 : 4110 : 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 : 4109 : gfc_current_locus = old_loc;
582 : :
583 : 4109 : buffer = (char *) alloca (length + 1);
584 : 4109 : memset (buffer, '\0', length + 1);
585 : :
586 : 4109 : match_digits (0, radix, buffer);
587 : 4109 : gfc_next_ascii_char (); /* Eat delimiter. */
588 : 4109 : if (post == 1)
589 : 24 : gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
590 : :
591 : 4109 : e = gfc_get_expr ();
592 : 4109 : e->expr_type = EXPR_CONSTANT;
593 : 4109 : e->ts.type = BT_BOZ;
594 : 4109 : e->where = gfc_current_locus;
595 : 4109 : e->boz.rdx = radix;
596 : 4109 : e->boz.len = length;
597 : 4109 : e->boz.str = XCNEWVEC (char, length + 1);
598 : 4109 : strncpy (e->boz.str, buffer, length);
599 : :
600 : 4109 : if (!gfc_in_match_data ()
601 : 4109 : && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
602 : : "statement at %L", &e->where)))
603 : : return MATCH_ERROR;
604 : :
605 : 4104 : *result = e;
606 : 4104 : return MATCH_YES;
607 : :
608 : 6437407 : backup:
609 : 6437407 : gfc_current_locus = start_loc;
610 : 6437407 : 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 : 6541452 : match_real_constant (gfc_expr **result, int signflag)
619 : : {
620 : 6541452 : int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 : 6541452 : locus old_loc, temp_loc;
622 : 6541452 : char *p, *buffer, c, exp_char;
623 : 6541452 : gfc_expr *e;
624 : 6541452 : bool negate;
625 : :
626 : 6541452 : old_loc = gfc_current_locus;
627 : 6541452 : gfc_gobble_whitespace ();
628 : :
629 : 6541452 : e = NULL;
630 : :
631 : 6541452 : default_exponent = 0;
632 : 6541452 : count = 0;
633 : 6541452 : seen_dp = 0;
634 : 6541452 : seen_digits = 0;
635 : 6541452 : exp_char = ' ';
636 : 6541452 : negate = false;
637 : :
638 : 6541452 : c = gfc_next_ascii_char ();
639 : 6541452 : if (signflag && (c == '+' || c == '-'))
640 : : {
641 : 5824 : if (c == '-')
642 : 5688 : negate = true;
643 : :
644 : 5824 : gfc_gobble_whitespace ();
645 : 5824 : c = gfc_next_ascii_char ();
646 : : }
647 : :
648 : : /* Scan significand. */
649 : 3755957 : for (;; c = gfc_next_ascii_char (), count++)
650 : : {
651 : 10297409 : if (c == '.')
652 : : {
653 : 260179 : if (seen_dp)
654 : 204 : goto done;
655 : :
656 : : /* Check to see if "." goes with a following operator like
657 : : ".eq.". */
658 : 259975 : temp_loc = gfc_current_locus;
659 : 259975 : c = gfc_next_ascii_char ();
660 : :
661 : 259975 : if (c == 'e' || c == 'd' || c == 'q')
662 : : {
663 : 17646 : c = gfc_next_ascii_char ();
664 : 17646 : if (c == '.')
665 : 0 : goto done; /* Operator named .e. or .d. */
666 : : }
667 : :
668 : 259975 : if (ISALPHA (c))
669 : 55234 : goto done; /* Distinguish 1.e9 from 1.eq.2 */
670 : :
671 : 204741 : gfc_current_locus = temp_loc;
672 : 204741 : seen_dp = 1;
673 : 204741 : continue;
674 : : }
675 : :
676 : 10037230 : if (ISDIGIT (c))
677 : : {
678 : 3551216 : seen_digits = 1;
679 : 3551216 : continue;
680 : : }
681 : :
682 : 6486014 : break;
683 : : }
684 : :
685 : 6486014 : if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 : 2216682 : goto done;
687 : 36900 : exp_char = c;
688 : :
689 : :
690 : 36900 : 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 : 36900 : c = gfc_next_ascii_char ();
703 : 36900 : count++;
704 : :
705 : 36900 : if (c == '+' || c == '-')
706 : : { /* optional sign */
707 : 6638 : c = gfc_next_ascii_char ();
708 : 6638 : count++;
709 : : }
710 : :
711 : 36900 : if (!ISDIGIT (c))
712 : : {
713 : : /* With -fdec, default exponent to 0 instead of complaining. */
714 : 40 : if (flag_dec)
715 : 36890 : 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 : 76523 : while (ISDIGIT (c))
724 : : {
725 : 39633 : c = gfc_next_ascii_char ();
726 : 39633 : count++;
727 : : }
728 : :
729 : 6541442 : done:
730 : : /* Check that we have a numeric constant. */
731 : 6541442 : if (!seen_digits || (!seen_dp && exp_char == ' '))
732 : : {
733 : 6330466 : gfc_current_locus = old_loc;
734 : 6330466 : return MATCH_NO;
735 : : }
736 : :
737 : : /* Convert the number. */
738 : 210976 : gfc_current_locus = old_loc;
739 : 210976 : gfc_gobble_whitespace ();
740 : :
741 : 210976 : buffer = (char *) alloca (count + default_exponent + 1);
742 : 210976 : memset (buffer, '\0', count + default_exponent + 1);
743 : :
744 : 210976 : p = buffer;
745 : 210976 : c = gfc_next_ascii_char ();
746 : 210976 : if (c == '+' || c == '-')
747 : : {
748 : 2926 : gfc_gobble_whitespace ();
749 : 2926 : c = gfc_next_ascii_char ();
750 : : }
751 : :
752 : : /* Hack for mpfr_set_str(). */
753 : 1369820 : for (;;)
754 : : {
755 : 790398 : if (c == 'd' || c == 'q')
756 : 29619 : *p = 'e';
757 : : else
758 : 760779 : *p = c;
759 : 790398 : p++;
760 : 790398 : if (--count == 0)
761 : : break;
762 : :
763 : 579422 : c = gfc_next_ascii_char ();
764 : : }
765 : 210976 : if (default_exponent)
766 : 30 : *p++ = '0';
767 : :
768 : 210976 : kind = get_kind (&is_iso_c);
769 : 210976 : if (kind == -1)
770 : 4 : goto cleanup;
771 : :
772 : 210972 : if (kind == 4)
773 : : {
774 : 20258 : if (flag_real4_kind == 8)
775 : 192 : kind = 8;
776 : 20258 : if (flag_real4_kind == 10)
777 : 192 : kind = 10;
778 : 20258 : if (flag_real4_kind == 16)
779 : 384 : kind = 16;
780 : : }
781 : 190714 : else if (kind == 8)
782 : : {
783 : 25900 : if (flag_real8_kind == 4)
784 : 192 : kind = 4;
785 : 25900 : if (flag_real8_kind == 10)
786 : 192 : kind = 10;
787 : 25900 : if (flag_real8_kind == 16)
788 : 384 : kind = 16;
789 : : }
790 : :
791 : 210972 : switch (exp_char)
792 : : {
793 : 29619 : case 'd':
794 : 29619 : 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 : 29619 : kind = gfc_default_double_kind;
801 : 29619 : 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 : 181353 : default:
828 : 181353 : if (kind == -2)
829 : 111243 : kind = gfc_default_real_kind;
830 : :
831 : 181353 : 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 : 210972 : e = convert_real (buffer, kind, &gfc_current_locus);
839 : 210972 : if (negate)
840 : 2821 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 : 210972 : e->ts.is_c_interop = is_iso_c;
842 : :
843 : 210972 : 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 : 210971 : 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 : 210971 : *result = e;
915 : 210971 : 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 : 581598 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 : : {
928 : 581598 : gfc_expr *start, *end;
929 : 581598 : locus old_loc;
930 : 581598 : gfc_ref *ref;
931 : 581598 : match m;
932 : :
933 : 581598 : start = NULL;
934 : 581598 : end = NULL;
935 : :
936 : 581598 : old_loc = gfc_current_locus;
937 : :
938 : 581598 : m = gfc_match_char ('(');
939 : 581598 : if (m != MATCH_YES)
940 : : return MATCH_NO;
941 : :
942 : 15086 : if (gfc_match_char (':') != MATCH_YES)
943 : : {
944 : 14208 : if (init)
945 : 0 : m = gfc_match_init_expr (&start);
946 : : else
947 : 14208 : m = gfc_match_expr (&start);
948 : :
949 : 14208 : if (m != MATCH_YES)
950 : : {
951 : 154 : m = MATCH_NO;
952 : 154 : goto cleanup;
953 : : }
954 : :
955 : 14054 : m = gfc_match_char (':');
956 : 14054 : if (m != MATCH_YES)
957 : 454 : goto cleanup;
958 : : }
959 : :
960 : 14478 : if (gfc_match_char (')') != MATCH_YES)
961 : : {
962 : 13610 : if (init)
963 : 0 : m = gfc_match_init_expr (&end);
964 : : else
965 : 13610 : m = gfc_match_expr (&end);
966 : :
967 : 13610 : if (m == MATCH_NO)
968 : 2 : goto syntax;
969 : 13608 : if (m == MATCH_ERROR)
970 : 0 : goto cleanup;
971 : :
972 : 13608 : m = gfc_match_char (')');
973 : 13608 : if (m == MATCH_NO)
974 : 3 : goto syntax;
975 : : }
976 : :
977 : : /* Optimize away the (:) reference. */
978 : 14473 : if (start == NULL && end == NULL && !deferred)
979 : : ref = NULL;
980 : : else
981 : : {
982 : 14268 : ref = gfc_get_ref ();
983 : :
984 : 14268 : ref->type = REF_SUBSTRING;
985 : 14268 : if (start == NULL)
986 : 671 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 : 14268 : ref->u.ss.start = start;
988 : 14268 : if (end == NULL && cl)
989 : 661 : end = gfc_copy_expr (cl->length);
990 : 14268 : ref->u.ss.end = end;
991 : 14268 : ref->u.ss.length = cl;
992 : : }
993 : :
994 : 14473 : *result = ref;
995 : 14473 : 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 : 4027951 : next_string_char (gfc_char_t delimiter, int *ret)
1022 : : {
1023 : 4027951 : locus old_locus;
1024 : 4027951 : gfc_char_t c;
1025 : :
1026 : 4027951 : c = gfc_next_char_literal (INSTRING_WARN);
1027 : 4027951 : *ret = 0;
1028 : :
1029 : 4027951 : if (c == '\n')
1030 : : {
1031 : 4 : *ret = -2;
1032 : 4 : return 0;
1033 : : }
1034 : :
1035 : 4027947 : 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 : 4027947 : if (c != delimiter)
1047 : : return c;
1048 : :
1049 : 585094 : old_locus = gfc_current_locus;
1050 : 585094 : c = gfc_next_char_literal (NONSTRING);
1051 : :
1052 : 585094 : if (c == delimiter)
1053 : : return c;
1054 : 584276 : gfc_current_locus = old_locus;
1055 : :
1056 : 584276 : *ret = -1;
1057 : 584276 : 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 : 4211509 : match_charkind_name (char *name)
1075 : : {
1076 : 4211509 : locus old_loc;
1077 : 4211509 : char c, peek;
1078 : 4211509 : int len;
1079 : :
1080 : 4211509 : gfc_gobble_whitespace ();
1081 : 4211509 : c = gfc_next_ascii_char ();
1082 : 4211509 : if (!ISALPHA (c))
1083 : : return MATCH_NO;
1084 : :
1085 : 3834240 : *name++ = c;
1086 : 3834240 : len = 1;
1087 : :
1088 : 15499574 : for (;;)
1089 : : {
1090 : 15499574 : old_loc = gfc_current_locus;
1091 : 15499574 : c = gfc_next_ascii_char ();
1092 : :
1093 : 15499574 : if (c == '_')
1094 : : {
1095 : 500831 : peek = gfc_peek_ascii_char ();
1096 : :
1097 : 500831 : if (peek == '\'' || peek == '\"')
1098 : : {
1099 : 995 : gfc_current_locus = old_loc;
1100 : 995 : *name = '\0';
1101 : 995 : return MATCH_YES;
1102 : : }
1103 : : }
1104 : :
1105 : 15498579 : if (!ISALNUM (c)
1106 : 4333081 : && c != '_'
1107 : 3833245 : && (c != '$' || !flag_dollar_ok))
1108 : : break;
1109 : :
1110 : 11665334 : *name++ = c;
1111 : 11665334 : 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 : 6733651 : match_string_constant (gfc_expr **result)
1128 : : {
1129 : 6733651 : char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 : 6733651 : size_t length;
1131 : 6733651 : int kind,save_warn_ampersand, ret;
1132 : 6733651 : locus old_locus, start_locus;
1133 : 6733651 : gfc_symbol *sym;
1134 : 6733651 : gfc_expr *e;
1135 : 6733651 : match m;
1136 : 6733651 : gfc_char_t c, delimiter, *p;
1137 : :
1138 : 6733651 : old_locus = gfc_current_locus;
1139 : :
1140 : 6733651 : gfc_gobble_whitespace ();
1141 : :
1142 : 6733651 : c = gfc_next_char ();
1143 : 6733651 : if (c == '\'' || c == '"')
1144 : : {
1145 : 254185 : kind = gfc_default_character_kind;
1146 : 254185 : start_locus = gfc_current_locus;
1147 : 254185 : goto got_delim;
1148 : : }
1149 : :
1150 : 6479466 : if (gfc_wide_is_digit (c))
1151 : : {
1152 : 2267957 : kind = 0;
1153 : :
1154 : 5464032 : while (gfc_wide_is_digit (c))
1155 : : {
1156 : 3209516 : kind = kind * 10 + c - '0';
1157 : 3209516 : if (kind > 9999999)
1158 : 13441 : goto no_match;
1159 : 3196075 : c = gfc_next_char ();
1160 : : }
1161 : :
1162 : : }
1163 : : else
1164 : : {
1165 : 4211509 : gfc_current_locus = old_locus;
1166 : :
1167 : 4211509 : m = match_charkind_name (name);
1168 : 4211509 : if (m != MATCH_YES)
1169 : 4210514 : goto no_match;
1170 : :
1171 : 995 : if (gfc_find_symbol (name, NULL, 1, &sym)
1172 : 995 : || sym == NULL
1173 : 1989 : || sym->attr.flavor != FL_PARAMETER)
1174 : 1 : goto no_match;
1175 : :
1176 : 994 : kind = -1;
1177 : 994 : c = gfc_next_char ();
1178 : : }
1179 : :
1180 : 2255510 : if (c != '_')
1181 : 2069526 : goto no_match;
1182 : :
1183 : 185984 : c = gfc_next_char ();
1184 : 185984 : if (c != '\'' && c != '"')
1185 : 148012 : goto no_match;
1186 : :
1187 : 37972 : start_locus = gfc_current_locus;
1188 : :
1189 : 37972 : if (kind == -1)
1190 : : {
1191 : 994 : if (gfc_extract_int (sym->value, &kind, 1))
1192 : : return MATCH_ERROR;
1193 : 994 : gfc_set_sym_referenced (sym);
1194 : : }
1195 : :
1196 : 37972 : 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 : 37972 : 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 : 292157 : delimiter = c;
1209 : 292157 : length = 0;
1210 : :
1211 : 3736137 : for (;;)
1212 : : {
1213 : 2014147 : c = next_string_char (delimiter, &ret);
1214 : 2014147 : if (ret == -1)
1215 : : break;
1216 : 1721994 : 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 : 1721990 : 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 : 292153 : peek = gfc_peek_ascii_char ();
1229 : 292153 : if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 : 25 : goto no_match;
1231 : :
1232 : 292128 : e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233 : :
1234 : 292128 : 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 : 292128 : save_warn_ampersand = warn_ampersand;
1239 : 292128 : warn_ampersand = false;
1240 : :
1241 : 292128 : p = e->value.character.string;
1242 : 2013804 : for (size_t i = 0; i < length; i++)
1243 : : {
1244 : 1721681 : c = next_string_char (delimiter, &ret);
1245 : :
1246 : 1721681 : 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 : 1721676 : *p++ = c;
1255 : : }
1256 : :
1257 : 292123 : *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 : 292123 : warn_ampersand = save_warn_ampersand;
1259 : :
1260 : 292123 : next_string_char (delimiter, &ret);
1261 : 292123 : if (ret != -1)
1262 : 0 : gfc_internal_error ("match_string_constant(): Delimiter not found");
1263 : :
1264 : 292123 : if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
1265 : 303 : 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 : 292123 : if (e->expr_type == EXPR_SUBSTRING
1271 : 303 : && e->ref && e->ref->type == REF_SUBSTRING
1272 : 299 : && e->ref->u.ss.start->expr_type == EXPR_CONSTANT
1273 : 73 : && (e->ref->u.ss.end == NULL
1274 : 71 : || e->ref->u.ss.end->expr_type == EXPR_CONSTANT))
1275 : : {
1276 : 71 : gfc_expr *res;
1277 : 71 : ptrdiff_t istart, iend;
1278 : 71 : size_t length;
1279 : 71 : bool equal_length = false;
1280 : :
1281 : : /* Basic checks on substring starting and ending indices. */
1282 : 71 : if (!gfc_resolve_substring (e->ref, &equal_length))
1283 : 6 : return MATCH_ERROR;
1284 : :
1285 : 68 : length = e->value.character.length;
1286 : 68 : istart = gfc_mpz_get_hwi (e->ref->u.ss.start->value.integer);
1287 : 68 : if (e->ref->u.ss.end == NULL)
1288 : : iend = length;
1289 : : else
1290 : 66 : iend = gfc_mpz_get_hwi (e->ref->u.ss.end->value.integer);
1291 : :
1292 : 68 : if (istart <= iend)
1293 : : {
1294 : 65 : 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 : 63 : 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 : 62 : length = iend - istart + 1;
1307 : : }
1308 : : else
1309 : : length = 0;
1310 : :
1311 : 65 : res = gfc_get_constant_expr (BT_CHARACTER, e->ts.kind, &e->where);
1312 : 65 : res->value.character.string = gfc_get_wide_string (length + 1);
1313 : 65 : res->value.character.length = length;
1314 : 65 : if (length > 0)
1315 : 62 : memcpy (res->value.character.string,
1316 : 62 : &e->value.character.string[istart - 1],
1317 : : length * sizeof (gfc_char_t));
1318 : 65 : res->value.character.string[length] = '\0';
1319 : 65 : e = res;
1320 : : }
1321 : :
1322 : 292117 : *result = e;
1323 : :
1324 : 292117 : return MATCH_YES;
1325 : :
1326 : 6441519 : no_match:
1327 : 6441519 : gfc_current_locus = old_locus;
1328 : 6441519 : 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 : 4205385 : match_logical_constant_string (void)
1336 : : {
1337 : 4205385 : locus orig_loc = gfc_current_locus;
1338 : :
1339 : 4205385 : gfc_gobble_whitespace ();
1340 : 4205385 : if (gfc_next_ascii_char () == '.')
1341 : : {
1342 : 44971 : char ch = gfc_next_ascii_char ();
1343 : 44971 : if (ch == 'f')
1344 : : {
1345 : 23229 : if (gfc_next_ascii_char () == 'a'
1346 : 23229 : && gfc_next_ascii_char () == 'l'
1347 : 23229 : && gfc_next_ascii_char () == 's'
1348 : 23229 : && gfc_next_ascii_char () == 'e'
1349 : 46458 : && gfc_next_ascii_char () == '.')
1350 : : /* Matched ".false.". */
1351 : : return 0;
1352 : : }
1353 : 21742 : else if (ch == 't')
1354 : : {
1355 : 21741 : if (gfc_next_ascii_char () == 'r'
1356 : 21741 : && gfc_next_ascii_char () == 'u'
1357 : 21741 : && gfc_next_ascii_char () == 'e'
1358 : 43482 : && gfc_next_ascii_char () == '.')
1359 : : /* Matched ".true.". */
1360 : : return 1;
1361 : : }
1362 : : }
1363 : 4160415 : gfc_current_locus = orig_loc;
1364 : 4160415 : return -1;
1365 : : }
1366 : :
1367 : : /* Match a .true. or .false. */
1368 : :
1369 : : static match
1370 : 4205385 : match_logical_constant (gfc_expr **result)
1371 : : {
1372 : 4205385 : gfc_expr *e;
1373 : 4205385 : int i, kind, is_iso_c;
1374 : :
1375 : 4205385 : i = match_logical_constant_string ();
1376 : 4205385 : if (i == -1)
1377 : : return MATCH_NO;
1378 : :
1379 : 44970 : kind = get_kind (&is_iso_c);
1380 : 44970 : if (kind == -1)
1381 : : return MATCH_ERROR;
1382 : 44970 : if (kind == -2)
1383 : 44481 : kind = gfc_default_logical_kind;
1384 : :
1385 : 44970 : 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 : 44966 : e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 : 44966 : e->ts.is_c_interop = is_iso_c;
1393 : :
1394 : 44966 : *result = e;
1395 : 44966 : 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 : 135749 : match_sym_complex_part (gfc_expr **result)
1404 : : {
1405 : 135749 : char name[GFC_MAX_SYMBOL_LEN + 1];
1406 : 135749 : gfc_symbol *sym;
1407 : 135749 : gfc_expr *e;
1408 : 135749 : match m;
1409 : :
1410 : 135749 : m = gfc_match_name (name);
1411 : 135749 : if (m != MATCH_YES)
1412 : : return m;
1413 : :
1414 : 36906 : if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 : : return MATCH_NO;
1416 : :
1417 : 34272 : 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 : 32823 : char c;
1423 : 32823 : gfc_gobble_whitespace ();
1424 : 32823 : c = gfc_peek_ascii_char ();
1425 : 32823 : if (c == '=' || c == ',')
1426 : : {
1427 : : m = MATCH_NO;
1428 : : }
1429 : : else
1430 : : {
1431 : 30255 : gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 : 30255 : m = MATCH_ERROR;
1433 : : }
1434 : 32823 : return m;
1435 : : }
1436 : :
1437 : 1449 : if (!sym->value)
1438 : 2 : goto error;
1439 : :
1440 : 1447 : if (!gfc_numeric_ts (&sym->value->ts))
1441 : : {
1442 : 330 : gfc_error ("Numeric PARAMETER required in complex constant at %C");
1443 : 330 : 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 : 135749 : match_complex_part (gfc_expr **result)
1494 : : {
1495 : 135749 : match m;
1496 : :
1497 : 135749 : m = match_sym_complex_part (result);
1498 : 135749 : if (m != MATCH_NO)
1499 : : return m;
1500 : :
1501 : 104045 : m = match_real_constant (result, 1);
1502 : 104045 : if (m != MATCH_NO)
1503 : : return m;
1504 : :
1505 : 90897 : return match_integer_constant (result, 1);
1506 : : }
1507 : :
1508 : :
1509 : : /* Try to match a complex constant. */
1510 : :
1511 : : static match
1512 : 6743382 : match_complex_constant (gfc_expr **result)
1513 : : {
1514 : 6743382 : gfc_expr *e, *real, *imag;
1515 : 6743382 : gfc_error_buffer old_error;
1516 : 6743382 : gfc_typespec target;
1517 : 6743382 : locus old_loc;
1518 : 6743382 : int kind;
1519 : 6743382 : match m;
1520 : :
1521 : 6743382 : old_loc = gfc_current_locus;
1522 : 6743382 : real = imag = e = NULL;
1523 : :
1524 : 6743382 : m = gfc_match_char ('(');
1525 : 6743382 : if (m != MATCH_YES)
1526 : : return m;
1527 : :
1528 : 126022 : gfc_push_error (&old_error);
1529 : :
1530 : 126022 : m = match_complex_part (&real);
1531 : 126022 : if (m == MATCH_NO)
1532 : : {
1533 : 73874 : gfc_free_error (&old_error);
1534 : 73874 : goto cleanup;
1535 : : }
1536 : :
1537 : 52148 : 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 : 42417 : gfc_clear_warning ();
1543 : 42417 : gfc_pop_error (&old_error);
1544 : 42417 : m = MATCH_NO;
1545 : 42417 : 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 : 9731 : if (m == MATCH_ERROR)
1554 : : {
1555 : 4 : gfc_free_error (&old_error);
1556 : 4 : goto cleanup;
1557 : : }
1558 : 9727 : gfc_pop_error (&old_error);
1559 : :
1560 : 9727 : m = match_complex_part (&imag);
1561 : 9727 : if (m == MATCH_NO)
1562 : 3104 : goto syntax;
1563 : 6623 : if (m == MATCH_ERROR)
1564 : 133 : goto cleanup;
1565 : :
1566 : 6490 : m = gfc_match_char (')');
1567 : 6490 : 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 : 6477 : if (m == MATCH_ERROR)
1581 : 0 : goto cleanup;
1582 : :
1583 : : /* Decide on the kind of this complex number. */
1584 : 6477 : if (real->ts.type == BT_REAL)
1585 : : {
1586 : 6064 : if (imag->ts.type == BT_REAL)
1587 : 6039 : kind = gfc_kind_max (real, imag);
1588 : : else
1589 : 25 : kind = real->ts.kind;
1590 : : }
1591 : : else
1592 : : {
1593 : 413 : if (imag->ts.type == BT_REAL)
1594 : 7 : kind = imag->ts.kind;
1595 : : else
1596 : 406 : kind = gfc_default_real_kind;
1597 : : }
1598 : 6477 : gfc_clear_ts (&target);
1599 : 6477 : target.type = BT_REAL;
1600 : 6477 : target.kind = kind;
1601 : :
1602 : 6477 : if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 : 414 : gfc_convert_type (real, &target, 2);
1604 : 6477 : if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 : 469 : gfc_convert_type (imag, &target, 2);
1606 : :
1607 : 6477 : e = convert_complex (real, imag, kind);
1608 : 6477 : e->where = gfc_current_locus;
1609 : :
1610 : 6477 : gfc_free_expr (real);
1611 : 6477 : gfc_free_expr (imag);
1612 : :
1613 : 6477 : *result = e;
1614 : 6477 : return MATCH_YES;
1615 : :
1616 : 3117 : syntax:
1617 : 3117 : gfc_error ("Syntax error in COMPLEX constant at %C");
1618 : 3117 : m = MATCH_ERROR;
1619 : :
1620 : 119545 : cleanup:
1621 : 119545 : gfc_free_expr (e);
1622 : 119545 : gfc_free_expr (real);
1623 : 119545 : gfc_free_expr (imag);
1624 : 119545 : gfc_current_locus = old_loc;
1625 : :
1626 : 119545 : return m;
1627 : 6743382 : }
1628 : :
1629 : :
1630 : : /* Match constants in any of several forms. Returns nonzero for a
1631 : : match, zero for no match. */
1632 : :
1633 : : match
1634 : 6743382 : gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 : : {
1636 : 6743382 : match m;
1637 : :
1638 : 6743382 : m = match_complex_constant (result);
1639 : 6743382 : if (m != MATCH_NO)
1640 : : return m;
1641 : :
1642 : 6733651 : m = match_string_constant (result);
1643 : 6733651 : if (m != MATCH_NO)
1644 : : return m;
1645 : :
1646 : 6441519 : m = match_boz_constant (result);
1647 : 6441519 : if (m != MATCH_NO)
1648 : : return m;
1649 : :
1650 : 6437407 : m = match_real_constant (result, signflag);
1651 : 6437407 : if (m != MATCH_NO)
1652 : : return m;
1653 : :
1654 : 6239569 : m = match_hollerith_constant (result);
1655 : 6239569 : if (m != MATCH_NO)
1656 : : return m;
1657 : :
1658 : 6236930 : if (flag_unsigned)
1659 : : {
1660 : 587942 : m = match_unsigned_constant (result);
1661 : 587942 : if (m != MATCH_NO)
1662 : : return m;
1663 : : }
1664 : :
1665 : 6135638 : m = match_integer_constant (result, signflag);
1666 : 6135638 : if (m != MATCH_NO)
1667 : : return m;
1668 : :
1669 : 4205385 : m = match_logical_constant (result);
1670 : 4205385 : 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 : 730943 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 : : {
1684 : 730943 : if (!sym->attr.function || (sym->result != sym))
1685 : : return false;
1686 : 1535749 : while (ns)
1687 : : {
1688 : 873186 : if (ns->proc_name == sym)
1689 : : return true;
1690 : 861935 : 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 : 1882644 : match_actual_arg (gfc_expr **result)
1705 : : {
1706 : 1882644 : char name[GFC_MAX_SYMBOL_LEN + 1];
1707 : 1882644 : gfc_symtree *symtree;
1708 : 1882644 : locus where, w;
1709 : 1882644 : gfc_expr *e;
1710 : 1882644 : char c;
1711 : :
1712 : 1882644 : gfc_gobble_whitespace ();
1713 : 1882644 : where = gfc_current_locus;
1714 : :
1715 : 1882644 : switch (gfc_match_name (name))
1716 : : {
1717 : : case MATCH_ERROR:
1718 : : return MATCH_ERROR;
1719 : :
1720 : : case MATCH_NO:
1721 : : break;
1722 : :
1723 : 1232330 : case MATCH_YES:
1724 : 1232330 : w = gfc_current_locus;
1725 : 1232330 : gfc_gobble_whitespace ();
1726 : 1232330 : c = gfc_next_ascii_char ();
1727 : 1232330 : gfc_current_locus = w;
1728 : :
1729 : 1232330 : if (c != ',' && c != ')')
1730 : : break;
1731 : :
1732 : 644729 : 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 : 644729 : if (symtree == NULL)
1739 : : {
1740 : 12743 : gfc_get_sym_tree (name, NULL, &symtree, false);
1741 : 12743 : gfc_set_sym_referenced (symtree->n.sym);
1742 : : }
1743 : : else
1744 : : {
1745 : 631986 : gfc_symbol *sym;
1746 : :
1747 : 631986 : sym = symtree->n.sym;
1748 : 631986 : gfc_set_sym_referenced (sym);
1749 : 631986 : 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 : 630895 : if (sym->attr.flavor != FL_PROCEDURE
1756 : 596202 : && sym->attr.flavor != FL_UNKNOWN)
1757 : : break;
1758 : :
1759 : 183277 : 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 : 183053 : if (sym->attr.function && sym->result == sym)
1770 : : {
1771 : 3120 : if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 : : break;
1773 : :
1774 : 2474 : 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 : 195096 : e = gfc_get_expr (); /* Leave it unknown for now */
1791 : 195096 : e->symtree = symtree;
1792 : 195096 : e->expr_type = EXPR_VARIABLE;
1793 : 195096 : e->ts.type = BT_PROCEDURE;
1794 : 195096 : e->where = where;
1795 : :
1796 : 195096 : *result = e;
1797 : 195096 : return MATCH_YES;
1798 : : }
1799 : :
1800 : 1687548 : gfc_current_locus = where;
1801 : 1687548 : return gfc_match_expr (result);
1802 : : }
1803 : :
1804 : :
1805 : : /* Match a keyword argument or type parameter spec list.. */
1806 : :
1807 : : static match
1808 : 1874535 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 : : {
1810 : 1874535 : char name[GFC_MAX_SYMBOL_LEN + 1];
1811 : 1874535 : gfc_actual_arglist *a;
1812 : 1874535 : locus name_locus;
1813 : 1874535 : match m;
1814 : :
1815 : 1874535 : name_locus = gfc_current_locus;
1816 : 1874535 : m = gfc_match_name (name);
1817 : :
1818 : 1874535 : if (m != MATCH_YES)
1819 : 558008 : goto cleanup;
1820 : 1316527 : if (gfc_match_char ('=') != MATCH_YES)
1821 : : {
1822 : 1175939 : m = MATCH_NO;
1823 : 1175939 : goto cleanup;
1824 : : }
1825 : :
1826 : 140588 : if (pdt)
1827 : : {
1828 : 214 : if (gfc_match_char ('*') == MATCH_YES)
1829 : : {
1830 : 18 : actual->spec_type = SPEC_ASSUMED;
1831 : 18 : goto add_name;
1832 : : }
1833 : 196 : else if (gfc_match_char (':') == MATCH_YES)
1834 : : {
1835 : 14 : actual->spec_type = SPEC_DEFERRED;
1836 : 14 : goto add_name;
1837 : : }
1838 : : else
1839 : 182 : actual->spec_type = SPEC_EXPLICIT;
1840 : : }
1841 : :
1842 : 140556 : m = match_actual_arg (&actual->expr);
1843 : 140556 : if (m != MATCH_YES)
1844 : 10576 : goto cleanup;
1845 : :
1846 : : /* Make sure this name has not appeared yet. */
1847 : 129980 : add_name:
1848 : 130012 : if (name[0] != '\0')
1849 : : {
1850 : 403451 : for (a = base; a; a = a->next)
1851 : 273449 : if (a->name != NULL && strcmp (a->name, name) == 0)
1852 : : {
1853 : 10 : gfc_error ("Keyword %qs at %C has already appeared in the "
1854 : : "current argument list", name);
1855 : 10 : return MATCH_ERROR;
1856 : : }
1857 : : }
1858 : :
1859 : 130002 : actual->name = gfc_get_string ("%s", name);
1860 : 130002 : return MATCH_YES;
1861 : :
1862 : 1744523 : cleanup:
1863 : 1744523 : gfc_current_locus = name_locus;
1864 : 1744523 : return m;
1865 : : }
1866 : :
1867 : :
1868 : : /* Match an argument list function, such as %VAL. */
1869 : :
1870 : : static match
1871 : 1841853 : match_arg_list_function (gfc_actual_arglist *result)
1872 : : {
1873 : 1841853 : char name[GFC_MAX_SYMBOL_LEN + 1];
1874 : 1841853 : locus old_locus;
1875 : 1841853 : match m;
1876 : :
1877 : 1841853 : old_locus = gfc_current_locus;
1878 : :
1879 : 1841853 : if (gfc_match_char ('%') != MATCH_YES)
1880 : : {
1881 : 1841788 : m = MATCH_NO;
1882 : 1841788 : 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 : 1841789 : cleanup:
1939 : 1841789 : gfc_current_locus = old_locus;
1940 : 1841789 : 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 : 1969363 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 : : {
1959 : 1969363 : gfc_actual_arglist *head, *tail;
1960 : 1969363 : int seen_keyword;
1961 : 1969363 : gfc_st_label *label;
1962 : 1969363 : locus old_loc;
1963 : 1969363 : match m;
1964 : :
1965 : 1969363 : *argp = tail = NULL;
1966 : 1969363 : old_loc = gfc_current_locus;
1967 : :
1968 : 1969363 : seen_keyword = 0;
1969 : :
1970 : 1969363 : if (gfc_match_char ('(') == MATCH_NO)
1971 : 1169501 : return (sub_flag) ? MATCH_YES : MATCH_NO;
1972 : :
1973 : 1369009 : if (gfc_match_char (')') == MATCH_YES)
1974 : : return MATCH_YES;
1975 : :
1976 : 1344097 : head = NULL;
1977 : :
1978 : 1344097 : matching_actual_arglist++;
1979 : :
1980 : 1874519 : for (;;)
1981 : : {
1982 : 1874519 : if (head == NULL)
1983 : 1344097 : head = tail = gfc_get_actual_arglist ();
1984 : : else
1985 : : {
1986 : 530422 : tail->next = gfc_get_actual_arglist ();
1987 : 530422 : tail = tail->next;
1988 : : }
1989 : :
1990 : 1874519 : 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 : 1874281 : if (pdt && !seen_keyword)
2007 : : {
2008 : 822 : if (gfc_match_char (':') == MATCH_YES)
2009 : : {
2010 : 55 : tail->spec_type = SPEC_DEFERRED;
2011 : 55 : goto next;
2012 : : }
2013 : 767 : else if (gfc_match_char ('*') == MATCH_YES)
2014 : : {
2015 : 98 : tail->spec_type = SPEC_ASSUMED;
2016 : 98 : goto next;
2017 : : }
2018 : : else
2019 : 669 : tail->spec_type = SPEC_EXPLICIT;
2020 : :
2021 : 669 : m = match_keyword_arg (tail, head, pdt);
2022 : 669 : if (m == MATCH_YES)
2023 : : {
2024 : 197 : seen_keyword = 1;
2025 : 197 : goto next;
2026 : : }
2027 : 472 : 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 : 1873931 : if (seen_keyword)
2034 : : {
2035 : 32078 : m = match_keyword_arg (tail, head, pdt);
2036 : :
2037 : 32078 : if (m == MATCH_ERROR)
2038 : 30 : goto cleanup;
2039 : 32048 : if (m == MATCH_NO)
2040 : : {
2041 : 1316 : gfc_error ("Missing keyword name in actual argument list at %C");
2042 : 1316 : goto cleanup;
2043 : : }
2044 : :
2045 : : }
2046 : : else
2047 : : {
2048 : : /* Try an argument list function, like %VAL. */
2049 : 1841853 : m = match_arg_list_function (tail);
2050 : 1841853 : if (m == MATCH_ERROR)
2051 : 1 : goto cleanup;
2052 : :
2053 : : /* See if we have the first keyword argument. */
2054 : 1841852 : if (m == MATCH_NO)
2055 : : {
2056 : 1841788 : m = match_keyword_arg (tail, head, false);
2057 : 1841788 : if (m == MATCH_YES)
2058 : : seen_keyword = 1;
2059 : 1742715 : if (m == MATCH_ERROR)
2060 : 691 : goto cleanup;
2061 : : }
2062 : :
2063 : 1841097 : if (m == MATCH_NO)
2064 : : {
2065 : : /* Try for a non-keyword argument. */
2066 : 1742024 : m = match_actual_arg (&tail->expr);
2067 : 1742024 : if (m == MATCH_ERROR)
2068 : 1800 : goto cleanup;
2069 : 1740224 : if (m == MATCH_NO)
2070 : 18434 : goto syntax;
2071 : : }
2072 : : }
2073 : :
2074 : :
2075 : 99073 : next:
2076 : 1852247 : if (gfc_match_char (')') == MATCH_YES)
2077 : : break;
2078 : 538470 : if (gfc_match_char (',') != MATCH_YES)
2079 : 8048 : goto syntax;
2080 : : }
2081 : :
2082 : 1313777 : *argp = head;
2083 : 1313777 : matching_actual_arglist--;
2084 : 1313777 : return MATCH_YES;
2085 : :
2086 : 26482 : syntax:
2087 : 26482 : gfc_error ("Syntax error in argument list at %C");
2088 : :
2089 : 30320 : cleanup:
2090 : 30320 : gfc_free_actual_arglist (head);
2091 : 30320 : gfc_current_locus = old_loc;
2092 : 30320 : matching_actual_arglist--;
2093 : 30320 : 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 : 686783 : extend_ref (gfc_expr *primary, gfc_ref *tail)
2102 : : {
2103 : 686783 : if (primary->ref == NULL)
2104 : 630496 : primary->ref = tail = gfc_get_ref ();
2105 : : else
2106 : : {
2107 : 56287 : if (tail == NULL)
2108 : 0 : gfc_internal_error ("extend_ref(): Bad tail");
2109 : 56287 : tail->next = gfc_get_ref ();
2110 : 56287 : tail = tail->next;
2111 : : }
2112 : :
2113 : 686783 : return tail;
2114 : : }
2115 : :
2116 : :
2117 : : /* Used by gfc_match_varspec() to match an inquiry reference. */
2118 : :
2119 : : bool
2120 : 3553 : is_inquiry_ref (const char *name, gfc_ref **ref)
2121 : : {
2122 : 3553 : inquiry_type type;
2123 : :
2124 : 3553 : if (name == NULL)
2125 : : return false;
2126 : :
2127 : 3553 : if (ref) *ref = NULL;
2128 : :
2129 : 3553 : if (strcmp (name, "re") == 0)
2130 : : type = INQUIRY_RE;
2131 : 2589 : else if (strcmp (name, "im") == 0)
2132 : : type = INQUIRY_IM;
2133 : 1804 : else if (strcmp (name, "kind") == 0)
2134 : : type = INQUIRY_KIND;
2135 : 1340 : else if (strcmp (name, "len") == 0)
2136 : : type = INQUIRY_LEN;
2137 : : else
2138 : : return false;
2139 : :
2140 : 2641 : if (ref)
2141 : : {
2142 : 1463 : *ref = gfc_get_ref ();
2143 : 1463 : (*ref)->type = REF_INQUIRY;
2144 : 1463 : (*ref)->u.i = type;
2145 : : }
2146 : :
2147 : : return true;
2148 : : }
2149 : :
2150 : :
2151 : : /* Check to see if functions in operator expressions can be resolved now. */
2152 : :
2153 : : static bool
2154 : 198 : resolvable_fcns (gfc_expr *e,
2155 : : gfc_symbol *sym ATTRIBUTE_UNUSED,
2156 : : int *f ATTRIBUTE_UNUSED)
2157 : : {
2158 : 198 : bool p;
2159 : 198 : gfc_symbol *s;
2160 : :
2161 : 198 : if (e->expr_type != EXPR_FUNCTION)
2162 : : return false;
2163 : :
2164 : 72 : s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2165 : 144 : p = s && (s->attr.use_assoc
2166 : 72 : || s->attr.host_assoc
2167 : 54 : || s->attr.if_source == IFSRC_DECL
2168 : 54 : || s->attr.proc == PROC_INTRINSIC
2169 : 24 : || gfc_is_intrinsic (s, 0, e->where));
2170 : 72 : return !p;
2171 : : }
2172 : :
2173 : :
2174 : : /* Match any additional specifications associated with the current
2175 : : variable like member references or substrings. If equiv_flag is
2176 : : set we only match stuff that is allowed inside an EQUIVALENCE
2177 : : statement. sub_flag tells whether we expect a type-bound procedure found
2178 : : to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2179 : : components, 'ppc_arg' determines whether the PPC may be called (with an
2180 : : argument list), or whether it may just be referred to as a pointer. */
2181 : :
2182 : : match
2183 : 4845678 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2184 : : bool ppc_arg)
2185 : : {
2186 : 4845678 : char name[GFC_MAX_SYMBOL_LEN + 1];
2187 : 4845678 : gfc_ref *substring, *tail, *tmp;
2188 : 4845678 : gfc_component *component = NULL;
2189 : 4845678 : gfc_component *previous = NULL;
2190 : 4845678 : gfc_symbol *sym = primary->symtree->n.sym;
2191 : 4845678 : gfc_expr *tgt_expr = NULL;
2192 : 4845678 : match m;
2193 : 4845678 : bool unknown;
2194 : 4845678 : bool inquiry;
2195 : 4845678 : bool intrinsic;
2196 : 4845678 : bool inferred_type;
2197 : 4845678 : locus old_loc;
2198 : 4845678 : char peeked_char;
2199 : :
2200 : 4845678 : tail = NULL;
2201 : :
2202 : 4845678 : gfc_gobble_whitespace ();
2203 : :
2204 : 4845678 : if (gfc_peek_ascii_char () == '[')
2205 : : {
2206 : 2785 : if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2207 : 2785 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2208 : 113 : && CLASS_DATA (sym)->attr.dimension))
2209 : : {
2210 : 0 : gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2211 : : "besides the coarray designator %<[...]%> at %C");
2212 : 0 : return MATCH_ERROR;
2213 : : }
2214 : 2785 : if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2215 : 2784 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2216 : 113 : && !CLASS_DATA (sym)->attr.codimension))
2217 : : {
2218 : 1 : gfc_error ("Coarray designator at %C but %qs is not a coarray",
2219 : : sym->name);
2220 : 1 : return MATCH_ERROR;
2221 : : }
2222 : : }
2223 : :
2224 : 4845677 : if (sym->assoc && sym->assoc->target)
2225 : 4845677 : tgt_expr = sym->assoc->target;
2226 : :
2227 : 4845677 : inferred_type = IS_INFERRED_TYPE (primary);
2228 : :
2229 : : /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
2230 : : been parsed, can generate errors with array refs.. The SELECT TYPE
2231 : : namespace is marked with 'assoc_name_inferred'. During resolution, this is
2232 : : detected and gfc_fixup_inferred_type_refs is called. */
2233 : 4844844 : if (!inferred_type
2234 : 4844844 : && sym->attr.select_type_temporary
2235 : 23184 : && sym->ns->assoc_name_inferred
2236 : 344 : && !sym->attr.select_rank_temporary)
2237 : 1177 : inferred_type = true;
2238 : :
2239 : : /* For associate names, we may not yet know whether they are arrays or not.
2240 : : If the selector expression is unambiguously an array; eg. a full array
2241 : : or an array section, then the associate name must be an array and we can
2242 : : fix it now. Otherwise, if parentheses follow and it is not a character
2243 : : type, we have to assume that it actually is one for now. The final
2244 : : decision will be made at resolution, of course. */
2245 : 4845677 : if (sym->assoc
2246 : 28873 : && gfc_peek_ascii_char () == '('
2247 : 9551 : && sym->ts.type != BT_CLASS
2248 : 4855087 : && !sym->attr.dimension)
2249 : : {
2250 : 386 : gfc_ref *ref = NULL;
2251 : :
2252 : 386 : if (!sym->assoc->dangling && tgt_expr)
2253 : : {
2254 : 326 : if (tgt_expr->expr_type == EXPR_VARIABLE)
2255 : 21 : gfc_resolve_expr (tgt_expr);
2256 : :
2257 : 326 : ref = tgt_expr->ref;
2258 : 340 : for (; ref; ref = ref->next)
2259 : 14 : if (ref->type == REF_ARRAY
2260 : 7 : && (ref->u.ar.type == AR_FULL
2261 : 7 : || ref->u.ar.type == AR_SECTION))
2262 : : break;
2263 : : }
2264 : :
2265 : 386 : if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2266 : 260 : && sym->assoc->st
2267 : 260 : && sym->assoc->st->n.sym
2268 : 260 : && sym->assoc->st->n.sym->attr.dimension == 0))
2269 : : {
2270 : 260 : sym->attr.dimension = 1;
2271 : 260 : if (sym->as == NULL
2272 : 260 : && sym->assoc->st
2273 : 260 : && sym->assoc->st->n.sym
2274 : 260 : && sym->assoc->st->n.sym->as)
2275 : 0 : sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2276 : : }
2277 : : }
2278 : 4845291 : else if (sym->ts.type == BT_CLASS
2279 : 42281 : && !(sym->assoc && sym->assoc->ar)
2280 : 42209 : && tgt_expr
2281 : 256 : && tgt_expr->expr_type == EXPR_VARIABLE
2282 : 130 : && sym->ts.u.derived != tgt_expr->ts.u.derived)
2283 : : {
2284 : 19 : gfc_resolve_expr (tgt_expr);
2285 : 19 : if (tgt_expr->rank)
2286 : 0 : sym->ts.u.derived = tgt_expr->ts.u.derived;
2287 : : }
2288 : :
2289 : 4845677 : peeked_char = gfc_peek_ascii_char ();
2290 : 1177 : if ((inferred_type && !sym->as && peeked_char == '(')
2291 : 4845456 : || (equiv_flag && peeked_char == '(') || peeked_char == '['
2292 : 4841101 : || sym->attr.codimension
2293 : 4827564 : || (sym->attr.dimension && sym->ts.type != BT_CLASS
2294 : 595956 : && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2295 : 595941 : && !(gfc_matching_procptr_assignment
2296 : 32 : && sym->attr.flavor == FL_PROCEDURE))
2297 : 9077320 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2298 : 42120 : && sym->ts.u.derived && CLASS_DATA (sym)
2299 : 42116 : && (CLASS_DATA (sym)->attr.dimension
2300 : 42116 : || CLASS_DATA (sym)->attr.codimension)))
2301 : : {
2302 : 630496 : gfc_array_spec *as;
2303 : 1260992 : bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2304 : 630496 : && sym->ts.type == BT_CHARACTER;
2305 : :
2306 : 630496 : tail = extend_ref (primary, tail);
2307 : 630496 : tail->type = REF_ARRAY;
2308 : :
2309 : : /* In EQUIVALENCE, we don't know yet whether we are seeing
2310 : : an array, character variable or array of character
2311 : : variables. We'll leave the decision till resolve time. */
2312 : :
2313 : 630496 : if (equiv_flag)
2314 : : as = NULL;
2315 : 628495 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2316 : 16576 : as = CLASS_DATA (sym)->as;
2317 : : else
2318 : 611919 : as = sym->as;
2319 : :
2320 : 630496 : m = gfc_match_array_ref (&tail->u.ar, as, equiv_flag, as ? as->corank : 0,
2321 : : coarray_only);
2322 : 630496 : if (m != MATCH_YES)
2323 : : return m;
2324 : :
2325 : 630421 : gfc_gobble_whitespace ();
2326 : 630421 : if (coarray_only)
2327 : : {
2328 : 1360 : primary->ts = sym->ts;
2329 : 1360 : goto check_substring;
2330 : : }
2331 : :
2332 : 629061 : if (equiv_flag && gfc_peek_ascii_char () == '(')
2333 : : {
2334 : 74 : tail = extend_ref (primary, tail);
2335 : 74 : tail->type = REF_ARRAY;
2336 : :
2337 : 74 : m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2338 : 74 : if (m != MATCH_YES)
2339 : : return m;
2340 : : }
2341 : : }
2342 : :
2343 : 4844242 : primary->ts = sym->ts;
2344 : :
2345 : 4844242 : if (equiv_flag)
2346 : : return MATCH_YES;
2347 : :
2348 : : /* With DEC extensions, member separator may be '.' or '%'. */
2349 : 4841298 : peeked_char = gfc_peek_ascii_char ();
2350 : 4841298 : m = gfc_match_member_sep (sym);
2351 : 4841298 : if (m == MATCH_ERROR)
2352 : : return MATCH_ERROR;
2353 : :
2354 : 4841297 : inquiry = false;
2355 : 4841297 : if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2356 : 120986 : && (primary->ts.type != BT_DERIVED || inferred_type))
2357 : : {
2358 : 2086 : match mm;
2359 : 2086 : old_loc = gfc_current_locus;
2360 : 2086 : mm = gfc_match_name (name);
2361 : :
2362 : : /* Check to see if this has a default complex. */
2363 : 482 : if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2364 : 2104 : && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2365 : : {
2366 : 6 : gfc_set_default_type (sym, 0, sym->ns);
2367 : 6 : primary->ts = sym->ts;
2368 : : }
2369 : :
2370 : : /* This is a usable inquiry reference, if the symbol is already known
2371 : : to have a type or no derived types with a component of this name
2372 : : can be found. If this was an inquiry reference with the same name
2373 : : as a derived component and the associate-name type is not derived
2374 : : or class, this is fixed up in 'gfc_fixup_inferred_type_refs'. */
2375 : 2086 : if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2376 : 3474 : && !(sym->ts.type == BT_UNKNOWN
2377 : 210 : && gfc_find_derived_types (sym, gfc_current_ns, name)))
2378 : : inquiry = true;
2379 : 2086 : gfc_current_locus = old_loc;
2380 : : }
2381 : :
2382 : : /* Use the default type if there is one. */
2383 : 2548030 : if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2384 : 4841773 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2385 : 0 : gfc_set_default_type (sym, 0, sym->ns);
2386 : :
2387 : : /* See if the type can be determined by resolution of the selector expression,
2388 : : if allowable now, or inferred from references. */
2389 : 4841297 : if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2390 : 2548949 : && m == MATCH_YES)
2391 : : {
2392 : 1232 : bool sym_present, resolved = false;
2393 : 1232 : gfc_symbol *tgt_sym;
2394 : :
2395 : 1232 : sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2396 : 1232 : tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
2397 : :
2398 : : /* These target expressions can be resolved at any time:
2399 : : (i) With a declared symbol or intrinsic function; or
2400 : : (ii) An operator expression,
2401 : : just as long as (iii) all the functions in the expression have been
2402 : : declared or are intrinsic. */
2403 : 1232 : if (((sym_present // (i)
2404 : 846 : && (tgt_sym->attr.use_assoc
2405 : 846 : || tgt_sym->attr.host_assoc
2406 : 828 : || tgt_sym->attr.if_source == IFSRC_DECL
2407 : 828 : || tgt_sym->attr.proc == PROC_INTRINSIC
2408 : 828 : || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2409 : 1202 : || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2410 : 48 : && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2411 : 42 : && gfc_resolve_expr (tgt_expr))
2412 : : {
2413 : 42 : sym->ts = tgt_expr->ts;
2414 : 42 : primary->ts = sym->ts;
2415 : 42 : resolved = true;
2416 : : }
2417 : :
2418 : : /* If this hasn't done the trick and the target expression is a function,
2419 : : or an unresolved operator expression, then this must be a derived type
2420 : : if 'name' matches an accessible type both in this namespace and in the
2421 : : as yet unparsed contained function. In principle, the type could have
2422 : : already been inferred to be complex and yet a derived type with a
2423 : : component name 're' or 'im' could be found. */
2424 : 42 : if (tgt_expr
2425 : 876 : && (tgt_expr->expr_type == EXPR_FUNCTION
2426 : 84 : || tgt_expr->expr_type == EXPR_ARRAY
2427 : 72 : || (!resolved && tgt_expr->expr_type == EXPR_OP))
2428 : 810 : && (sym->ts.type == BT_UNKNOWN
2429 : 388 : || (inferred_type && sym->ts.type != BT_COMPLEX))
2430 : 1928 : && gfc_find_derived_types (sym, gfc_current_ns, name, true))
2431 : : {
2432 : 546 : sym->assoc->inferred_type = 1;
2433 : : /* The first returned type is as good as any at this stage. The final
2434 : : determination is made in 'gfc_fixup_inferred_type_refs'*/
2435 : 546 : gfc_symbol **dts = &sym->assoc->derived_types;
2436 : 546 : tgt_expr->ts.type = BT_DERIVED;
2437 : 546 : tgt_expr->ts.kind = 0;
2438 : 546 : tgt_expr->ts.u.derived = *dts;
2439 : 546 : sym->ts = tgt_expr->ts;
2440 : 546 : primary->ts = sym->ts;
2441 : : /* Delete the dt list even if this process has to be done again for
2442 : : another primary expression. */
2443 : 1122 : while (*dts && (*dts)->dt_next)
2444 : : {
2445 : 576 : gfc_symbol **tmp = &(*dts)->dt_next;
2446 : 576 : *dts = NULL;
2447 : 576 : dts = tmp;
2448 : : }
2449 : : }
2450 : : /* If there is a usable inquiry reference not there are no matching
2451 : : derived types, force the inquiry reference by setting unknown the
2452 : : type of the primary expression. */
2453 : 258 : else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2454 : 734 : && !gfc_find_derived_types (sym, gfc_current_ns, name))
2455 : 48 : primary->ts.type = BT_UNKNOWN;
2456 : :
2457 : : /* An inquiry reference might determine the type, otherwise we have an
2458 : : error. */
2459 : 1232 : if (sym->ts.type == BT_UNKNOWN && !inquiry)
2460 : : {
2461 : 12 : gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2462 : 12 : return MATCH_ERROR;
2463 : : }
2464 : : }
2465 : 4840065 : else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2466 : 4625688 : && m == MATCH_YES && !inquiry)
2467 : : {
2468 : 6 : gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2469 : : peeked_char, sym->name);
2470 : 6 : return MATCH_ERROR;
2471 : : }
2472 : :
2473 : 4841279 : if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2474 : 216445 : || m != MATCH_YES)
2475 : 4701606 : goto check_substring;
2476 : :
2477 : 139673 : if (!inquiry)
2478 : 138567 : sym = sym->ts.u.derived;
2479 : : else
2480 : : sym = NULL;
2481 : :
2482 : 159422 : for (;;)
2483 : : {
2484 : 159422 : bool t;
2485 : 159422 : gfc_symtree *tbp;
2486 : :
2487 : 159422 : m = gfc_match_name (name);
2488 : 159422 : if (m == MATCH_NO)
2489 : 0 : gfc_error ("Expected structure component name at %C");
2490 : 159422 : if (m != MATCH_YES)
2491 : 131 : return MATCH_ERROR;
2492 : :
2493 : 159422 : intrinsic = false;
2494 : 159422 : if (primary->ts.type != BT_CLASS && primary->ts.type != BT_DERIVED)
2495 : : {
2496 : 1455 : inquiry = is_inquiry_ref (name, &tmp);
2497 : 1455 : if (inquiry)
2498 : 1451 : sym = NULL;
2499 : :
2500 : 1455 : if (peeked_char == '%')
2501 : : {
2502 : 1455 : if (tmp)
2503 : : {
2504 : 1451 : gfc_symbol *s;
2505 : 1451 : switch (tmp->u.i)
2506 : : {
2507 : 952 : case INQUIRY_RE:
2508 : 952 : case INQUIRY_IM:
2509 : 952 : if (!gfc_notify_std (GFC_STD_F2008,
2510 : : "RE or IM part_ref at %C"))
2511 : : return MATCH_ERROR;
2512 : : break;
2513 : :
2514 : 252 : case INQUIRY_KIND:
2515 : 252 : if (!gfc_notify_std (GFC_STD_F2003,
2516 : : "KIND part_ref at %C"))
2517 : : return MATCH_ERROR;
2518 : : break;
2519 : :
2520 : 247 : case INQUIRY_LEN:
2521 : 247 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2522 : : return MATCH_ERROR;
2523 : : break;
2524 : : }
2525 : :
2526 : : /* If necessary, infer the type of the primary expression
2527 : : and the associate-name using the the inquiry ref.. */
2528 : 1442 : s = primary->symtree ? primary->symtree->n.sym : NULL;
2529 : 1438 : if (s && s->assoc && s->assoc->target
2530 : 258 : && (s->ts.type == BT_UNKNOWN
2531 : 138 : || (primary->ts.type == BT_UNKNOWN
2532 : 48 : && s->assoc->inferred_type
2533 : 48 : && s->ts.type == BT_DERIVED)))
2534 : : {
2535 : 168 : if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2536 : : {
2537 : 72 : s->ts.type = BT_COMPLEX;
2538 : 72 : s->ts.kind = gfc_default_real_kind;;
2539 : 72 : s->assoc->inferred_type = 1;
2540 : 72 : primary->ts = s->ts;
2541 : : }
2542 : 96 : else if (tmp->u.i == INQUIRY_LEN)
2543 : : {
2544 : 48 : s->ts.type = BT_CHARACTER;
2545 : 48 : s->ts.kind = gfc_default_character_kind;;
2546 : 48 : s->assoc->inferred_type = 1;
2547 : 48 : primary->ts = s->ts;
2548 : : }
2549 : 48 : else if (s->ts.type == BT_UNKNOWN)
2550 : : {
2551 : : /* KIND inquiry gives no clue as to symbol type. */
2552 : 48 : primary->ref = tmp;
2553 : 48 : primary->ts.type = BT_INTEGER;
2554 : 48 : primary->ts.kind = gfc_default_integer_kind;
2555 : 48 : return MATCH_YES;
2556 : : }
2557 : : }
2558 : :
2559 : 1394 : if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2560 : 948 : && primary->ts.type != BT_COMPLEX)
2561 : : {
2562 : 12 : gfc_error ("The RE or IM part_ref at %C must be "
2563 : : "applied to a COMPLEX expression");
2564 : 12 : return MATCH_ERROR;
2565 : : }
2566 : 1382 : else if (tmp->u.i == INQUIRY_LEN
2567 : 245 : && primary->ts.type != BT_CHARACTER)
2568 : : {
2569 : 5 : gfc_error ("The LEN part_ref at %C must be applied "
2570 : : "to a CHARACTER expression");
2571 : 5 : return MATCH_ERROR;
2572 : : }
2573 : : }
2574 : 1381 : if (primary->ts.type != BT_UNKNOWN)
2575 : 159348 : intrinsic = true;
2576 : : }
2577 : : }
2578 : : else
2579 : : inquiry = false;
2580 : :
2581 : 159348 : if (sym && sym->f2k_derived)
2582 : 155329 : tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2583 : : else
2584 : : tbp = NULL;
2585 : :
2586 : 155329 : if (tbp)
2587 : : {
2588 : 3866 : gfc_symbol* tbp_sym;
2589 : :
2590 : 3866 : if (!t)
2591 : : return MATCH_ERROR;
2592 : :
2593 : 3864 : gcc_assert (!tail || !tail->next);
2594 : :
2595 : 3864 : if (!(primary->expr_type == EXPR_VARIABLE
2596 : : || (primary->expr_type == EXPR_STRUCTURE
2597 : 1 : && primary->symtree && primary->symtree->n.sym
2598 : 1 : && primary->symtree->n.sym->attr.flavor)))
2599 : : return MATCH_ERROR;
2600 : :
2601 : 3862 : if (tbp->n.tb->is_generic)
2602 : : tbp_sym = NULL;
2603 : : else
2604 : 3208 : tbp_sym = tbp->n.tb->u.specific->n.sym;
2605 : :
2606 : 3862 : primary->expr_type = EXPR_COMPCALL;
2607 : 3862 : primary->value.compcall.tbp = tbp->n.tb;
2608 : 3862 : primary->value.compcall.name = tbp->name;
2609 : 3862 : primary->value.compcall.ignore_pass = 0;
2610 : 3862 : primary->value.compcall.assign = 0;
2611 : 3862 : primary->value.compcall.base_object = NULL;
2612 : 3862 : gcc_assert (primary->symtree->n.sym->attr.referenced);
2613 : 3862 : if (tbp_sym)
2614 : 3208 : primary->ts = tbp_sym->ts;
2615 : : else
2616 : 654 : gfc_clear_ts (&primary->ts);
2617 : :
2618 : 3862 : m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2619 : : &primary->value.compcall.actual);
2620 : 3862 : if (m == MATCH_ERROR)
2621 : : return MATCH_ERROR;
2622 : 3862 : if (m == MATCH_NO)
2623 : : {
2624 : 162 : if (sub_flag)
2625 : 161 : primary->value.compcall.actual = NULL;
2626 : : else
2627 : : {
2628 : 1 : gfc_error ("Expected argument list at %C");
2629 : 1 : return MATCH_ERROR;
2630 : : }
2631 : : }
2632 : :
2633 : 139542 : break;
2634 : : }
2635 : :
2636 : 155482 : previous = component;
2637 : :
2638 : 155482 : if (!inquiry && !intrinsic)
2639 : 154102 : component = gfc_find_component (sym, name, false, false, &tmp);
2640 : : else
2641 : : component = NULL;
2642 : :
2643 : 155482 : if (intrinsic && !inquiry)
2644 : : {
2645 : 3 : if (previous)
2646 : 2 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2647 : : "type component %qs", name, previous->name);
2648 : : else
2649 : 1 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2650 : : "type component", name);
2651 : 3 : return MATCH_ERROR;
2652 : : }
2653 : 155479 : else if (component == NULL && !inquiry)
2654 : : return MATCH_ERROR;
2655 : :
2656 : : /* Extend the reference chain determined by gfc_find_component or
2657 : : is_inquiry_ref. */
2658 : 155432 : if (primary->ref == NULL)
2659 : 93623 : primary->ref = tmp;
2660 : : else
2661 : : {
2662 : : /* Set by the for loop below for the last component ref. */
2663 : 61809 : gcc_assert (tail != NULL);
2664 : 61809 : tail->next = tmp;
2665 : : }
2666 : :
2667 : : /* The reference chain may be longer than one hop for union
2668 : : subcomponents; find the new tail. */
2669 : 157408 : for (tail = tmp; tail->next; tail = tail->next)
2670 : : ;
2671 : :
2672 : 155432 : if (tmp && tmp->type == REF_INQUIRY)
2673 : : {
2674 : 1377 : if (!primary->where.u.lb || !primary->where.nextc)
2675 : 1229 : primary->where = gfc_current_locus;
2676 : 1377 : gfc_simplify_expr (primary, 0);
2677 : :
2678 : 1377 : if (primary->expr_type == EXPR_CONSTANT)
2679 : 314 : goto check_done;
2680 : :
2681 : 1063 : switch (tmp->u.i)
2682 : : {
2683 : 852 : case INQUIRY_RE:
2684 : 852 : case INQUIRY_IM:
2685 : 852 : if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2686 : : return MATCH_ERROR;
2687 : :
2688 : 852 : if (primary->ts.type != BT_COMPLEX)
2689 : : {
2690 : 0 : gfc_error ("The RE or IM part_ref at %C must be "
2691 : : "applied to a COMPLEX expression");
2692 : 0 : return MATCH_ERROR;
2693 : : }
2694 : 852 : primary->ts.type = BT_REAL;
2695 : 852 : break;
2696 : :
2697 : 159 : case INQUIRY_LEN:
2698 : 159 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2699 : : return MATCH_ERROR;
2700 : :
2701 : 159 : if (primary->ts.type != BT_CHARACTER)
2702 : : {
2703 : 0 : gfc_error ("The LEN part_ref at %C must be applied "
2704 : : "to a CHARACTER expression");
2705 : 0 : return MATCH_ERROR;
2706 : : }
2707 : 159 : primary->ts.u.cl = NULL;
2708 : 159 : primary->ts.type = BT_INTEGER;
2709 : 159 : primary->ts.kind = gfc_default_integer_kind;
2710 : 159 : break;
2711 : :
2712 : 52 : case INQUIRY_KIND:
2713 : 52 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2714 : : return MATCH_ERROR;
2715 : :
2716 : 52 : if (primary->ts.type == BT_CLASS
2717 : 52 : || primary->ts.type == BT_DERIVED)
2718 : : {
2719 : 0 : gfc_error ("The KIND part_ref at %C must be applied "
2720 : : "to an expression of intrinsic type");
2721 : 0 : return MATCH_ERROR;
2722 : : }
2723 : 52 : primary->ts.type = BT_INTEGER;
2724 : 52 : primary->ts.kind = gfc_default_integer_kind;
2725 : 52 : break;
2726 : :
2727 : 0 : default:
2728 : 0 : gcc_unreachable ();
2729 : : }
2730 : :
2731 : 1063 : goto check_done;
2732 : : }
2733 : :
2734 : 154055 : primary->ts = component->ts;
2735 : :
2736 : 154055 : if (component->attr.proc_pointer && ppc_arg)
2737 : : {
2738 : : /* Procedure pointer component call: Look for argument list. */
2739 : 873 : m = gfc_match_actual_arglist (sub_flag,
2740 : : &primary->value.compcall.actual);
2741 : 873 : if (m == MATCH_ERROR)
2742 : : return MATCH_ERROR;
2743 : :
2744 : 873 : if (m == MATCH_NO && !gfc_matching_ptr_assignment
2745 : 263 : && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2746 : : {
2747 : 2 : gfc_error ("Procedure pointer component %qs requires an "
2748 : : "argument list at %C", component->name);
2749 : 2 : return MATCH_ERROR;
2750 : : }
2751 : :
2752 : 871 : if (m == MATCH_YES)
2753 : 609 : primary->expr_type = EXPR_PPC;
2754 : :
2755 : : break;
2756 : : }
2757 : :
2758 : 153182 : if (component->as != NULL && !component->attr.proc_pointer)
2759 : : {
2760 : 51300 : tail = extend_ref (primary, tail);
2761 : 51300 : tail->type = REF_ARRAY;
2762 : :
2763 : 102600 : m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2764 : 51300 : component->as->corank);
2765 : 51300 : if (m != MATCH_YES)
2766 : : return m;
2767 : : }
2768 : 101882 : else if (component->ts.type == BT_CLASS && component->attr.class_ok
2769 : 10227 : && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2770 : : {
2771 : 4913 : tail = extend_ref (primary, tail);
2772 : 4913 : tail->type = REF_ARRAY;
2773 : :
2774 : 9826 : m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2775 : : equiv_flag,
2776 : 4913 : CLASS_DATA (component)->as->corank);
2777 : 4913 : if (m != MATCH_YES)
2778 : : return m;
2779 : : }
2780 : :
2781 : 96969 : check_done:
2782 : : /* In principle, we could have eg. expr%re%kind so we must allow for
2783 : : this possibility. */
2784 : 154559 : if (gfc_match_char ('%') == MATCH_YES)
2785 : : {
2786 : 19379 : if (component && (component->ts.type == BT_DERIVED
2787 : 3081 : || component->ts.type == BT_CLASS))
2788 : 19030 : sym = component->ts.u.derived;
2789 : 19379 : continue;
2790 : : }
2791 : 135180 : else if (inquiry)
2792 : : break;
2793 : :
2794 : 125049 : if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2795 : 141298 : || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2796 : : break;
2797 : :
2798 : 370 : if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2799 : 370 : sym = component->ts.u.derived;
2800 : : }
2801 : :
2802 : 4842508 : check_substring:
2803 : 4842508 : unknown = false;
2804 : 4842508 : if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2805 : : {
2806 : 2547554 : if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2807 : : {
2808 : 352 : gfc_set_default_type (sym, 0, sym->ns);
2809 : 352 : primary->ts = sym->ts;
2810 : 352 : unknown = true;
2811 : : }
2812 : : }
2813 : :
2814 : 4842508 : if (primary->ts.type == BT_CHARACTER)
2815 : : {
2816 : 288104 : bool def = primary->ts.deferred == 1;
2817 : 288104 : switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2818 : : {
2819 : 13267 : case MATCH_YES:
2820 : 13267 : if (tail == NULL)
2821 : 8120 : primary->ref = substring;
2822 : : else
2823 : 5147 : tail->next = substring;
2824 : :
2825 : 13267 : if (primary->expr_type == EXPR_CONSTANT)
2826 : 755 : primary->expr_type = EXPR_SUBSTRING;
2827 : :
2828 : 13267 : if (substring)
2829 : 13087 : primary->ts.u.cl = NULL;
2830 : :
2831 : 13267 : if (gfc_peek_ascii_char () == '(')
2832 : : {
2833 : 5 : gfc_error_now ("Unexpected array/substring ref at %C");
2834 : 5 : return MATCH_ERROR;
2835 : : }
2836 : : break;
2837 : :
2838 : 274837 : case MATCH_NO:
2839 : 274837 : if (unknown)
2840 : : {
2841 : 351 : gfc_clear_ts (&primary->ts);
2842 : 351 : gfc_clear_ts (&sym->ts);
2843 : : }
2844 : : break;
2845 : :
2846 : : case MATCH_ERROR:
2847 : : return MATCH_ERROR;
2848 : : }
2849 : : }
2850 : :
2851 : : /* F08:C611. */
2852 : 4842503 : if (primary->ts.type == BT_DERIVED && primary->ref
2853 : 26411 : && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2854 : : {
2855 : 6 : gfc_error ("Nonpolymorphic reference to abstract type at %C");
2856 : 6 : return MATCH_ERROR;
2857 : : }
2858 : :
2859 : : /* F08:C727. */
2860 : 4842497 : if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2861 : : {
2862 : 3 : gfc_error ("Coindexed procedure-pointer component at %C");
2863 : 3 : return MATCH_ERROR;
2864 : : }
2865 : :
2866 : : return MATCH_YES;
2867 : : }
2868 : :
2869 : :
2870 : : /* Given an expression that is a variable, figure out what the
2871 : : ultimate variable's type and attribute is, traversing the reference
2872 : : structures if necessary.
2873 : :
2874 : : This subroutine is trickier than it looks. We start at the base
2875 : : symbol and store the attribute. Component references load a
2876 : : completely new attribute.
2877 : :
2878 : : A couple of rules come into play. Subobjects of targets are always
2879 : : targets themselves. If we see a component that goes through a
2880 : : pointer, then the expression must also be a target, since the
2881 : : pointer is associated with something (if it isn't core will soon be
2882 : : dumped). If we see a full part or section of an array, the
2883 : : expression is also an array.
2884 : :
2885 : : We can have at most one full array reference. */
2886 : :
2887 : : symbol_attribute
2888 : 4222110 : gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2889 : : {
2890 : 4222110 : int dimension, codimension, pointer, allocatable, target, optional;
2891 : 4222110 : symbol_attribute attr;
2892 : 4222110 : gfc_ref *ref;
2893 : 4222110 : gfc_symbol *sym;
2894 : 4222110 : gfc_component *comp;
2895 : 4222110 : bool has_inquiry_part;
2896 : :
2897 : 4222110 : if (expr->expr_type != EXPR_VARIABLE
2898 : 26587 : && expr->expr_type != EXPR_FUNCTION
2899 : 9 : && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
2900 : 0 : gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2901 : :
2902 : 4222110 : sym = expr->symtree->n.sym;
2903 : 4222110 : attr = sym->attr;
2904 : :
2905 : 4222110 : optional = attr.optional;
2906 : 4222110 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
2907 : : {
2908 : 121666 : dimension = CLASS_DATA (sym)->attr.dimension;
2909 : 121666 : codimension = CLASS_DATA (sym)->attr.codimension;
2910 : 121666 : pointer = CLASS_DATA (sym)->attr.class_pointer;
2911 : 121666 : allocatable = CLASS_DATA (sym)->attr.allocatable;
2912 : : }
2913 : : else
2914 : : {
2915 : 4100444 : dimension = attr.dimension;
2916 : 4100444 : codimension = attr.codimension;
2917 : 4100444 : pointer = attr.pointer;
2918 : 4100444 : allocatable = attr.allocatable;
2919 : : }
2920 : :
2921 : 4222110 : target = attr.target;
2922 : 4222110 : if (pointer || attr.proc_pointer)
2923 : 172917 : target = 1;
2924 : :
2925 : : /* F2018:11.1.3.3: Other attributes of associate names
2926 : : "The associating entity does not have the ALLOCATABLE or POINTER
2927 : : attributes; it has the TARGET attribute if and only if the selector is
2928 : : a variable and has either the TARGET or POINTER attribute." */
2929 : 4222110 : if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
2930 : : {
2931 : 28683 : if (sym->assoc->target->expr_type == EXPR_VARIABLE)
2932 : : {
2933 : 26320 : symbol_attribute tgt_attr;
2934 : 26320 : tgt_attr = gfc_expr_attr (sym->assoc->target);
2935 : 26320 : target = (tgt_attr.pointer || tgt_attr.target);
2936 : : }
2937 : : else
2938 : : target = 0;
2939 : : }
2940 : :
2941 : 4222110 : if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2942 : 49646 : *ts = sym->ts;
2943 : :
2944 : : /* Catch left-overs from match_actual_arg, where an actual argument of a
2945 : : procedure is given a temporary ts.type == BT_PROCEDURE. The fixup is
2946 : : needed for structure constructors in DATA statements, where a pointer
2947 : : is associated with a data target, and the argument has not been fully
2948 : : resolved yet. Components references are dealt with further below. */
2949 : 49646 : if (ts != NULL
2950 : 1259858 : && expr->ts.type == BT_PROCEDURE
2951 : 2424 : && expr->ref == NULL
2952 : 2424 : && attr.flavor != FL_PROCEDURE
2953 : 25 : && attr.target)
2954 : 1 : *ts = sym->ts;
2955 : :
2956 : 4222110 : has_inquiry_part = false;
2957 : 5599012 : for (ref = expr->ref; ref; ref = ref->next)
2958 : 1378108 : if (ref->type == REF_INQUIRY)
2959 : : {
2960 : : has_inquiry_part = true;
2961 : : optional = false;
2962 : : break;
2963 : : }
2964 : :
2965 : 5600225 : for (ref = expr->ref; ref; ref = ref->next)
2966 : 1378115 : switch (ref->type)
2967 : : {
2968 : 1065409 : case REF_ARRAY:
2969 : :
2970 : 1065409 : switch (ref->u.ar.type)
2971 : : {
2972 : : case AR_FULL:
2973 : 1378115 : dimension = 1;
2974 : : break;
2975 : :
2976 : 108966 : case AR_SECTION:
2977 : 108966 : allocatable = pointer = 0;
2978 : 108966 : dimension = 1;
2979 : 108966 : optional = false;
2980 : 108966 : break;
2981 : :
2982 : 306782 : case AR_ELEMENT:
2983 : : /* Handle coarrays. */
2984 : 306782 : if (ref->u.ar.dimen > 0)
2985 : 1378115 : allocatable = pointer = optional = false;
2986 : : break;
2987 : :
2988 : : case AR_UNKNOWN:
2989 : : /* For standard conforming code, AR_UNKNOWN should not happen.
2990 : : For nonconforming code, gfortran can end up here. Treat it
2991 : : as a no-op. */
2992 : : break;
2993 : : }
2994 : :
2995 : : break;
2996 : :
2997 : 297494 : case REF_COMPONENT:
2998 : 297494 : optional = false;
2999 : 297494 : comp = ref->u.c.component;
3000 : 297494 : attr = comp->attr;
3001 : 297494 : if (ts != NULL && !has_inquiry_part)
3002 : : {
3003 : 76988 : *ts = comp->ts;
3004 : : /* Don't set the string length if a substring reference
3005 : : follows. */
3006 : 76988 : if (ts->type == BT_CHARACTER
3007 : 8414 : && ref->next && ref->next->type == REF_SUBSTRING)
3008 : 208 : ts->u.cl = NULL;
3009 : : }
3010 : :
3011 : 297494 : if (comp->ts.type == BT_CLASS)
3012 : : {
3013 : 19972 : codimension = CLASS_DATA (comp)->attr.codimension;
3014 : 19972 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3015 : 19972 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3016 : : }
3017 : : else
3018 : : {
3019 : 277522 : codimension = comp->attr.codimension;
3020 : 277522 : if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3021 : 13657 : pointer = comp->attr.class_pointer;
3022 : : else
3023 : 263865 : pointer = comp->attr.pointer;
3024 : 277522 : allocatable = comp->attr.allocatable;
3025 : : }
3026 : 297494 : if (pointer || attr.proc_pointer)
3027 : 51994 : target = 1;
3028 : :
3029 : : break;
3030 : :
3031 : 15212 : case REF_INQUIRY:
3032 : 15212 : case REF_SUBSTRING:
3033 : 15212 : allocatable = pointer = optional = false;
3034 : 15212 : break;
3035 : : }
3036 : :
3037 : 4222110 : attr.dimension = dimension;
3038 : 4222110 : attr.codimension = codimension;
3039 : 4222110 : attr.pointer = pointer;
3040 : 4222110 : attr.allocatable = allocatable;
3041 : 4222110 : attr.target = target;
3042 : 4222110 : attr.save = sym->attr.save;
3043 : 4222110 : attr.optional = optional;
3044 : :
3045 : 4222110 : return attr;
3046 : : }
3047 : :
3048 : :
3049 : : /* Return the attribute from a general expression. */
3050 : :
3051 : : symbol_attribute
3052 : 3612133 : gfc_expr_attr (gfc_expr *e)
3053 : : {
3054 : 3612133 : symbol_attribute attr;
3055 : :
3056 : 3612133 : switch (e->expr_type)
3057 : : {
3058 : 2927348 : case EXPR_VARIABLE:
3059 : 2927348 : attr = gfc_variable_attr (e, NULL);
3060 : 2927348 : break;
3061 : :
3062 : 41383 : case EXPR_FUNCTION:
3063 : 41383 : gfc_clear_attr (&attr);
3064 : :
3065 : 41383 : if (e->value.function.esym && e->value.function.esym->result)
3066 : : {
3067 : 14513 : gfc_symbol *sym = e->value.function.esym->result;
3068 : 14513 : attr = sym->attr;
3069 : 14513 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3070 : : {
3071 : 1670 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3072 : 1670 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3073 : 1670 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3074 : : }
3075 : : }
3076 : 26870 : else if (e->value.function.isym
3077 : 25864 : && e->value.function.isym->transformational
3078 : 15991 : && e->ts.type == BT_CLASS)
3079 : 294 : attr = CLASS_DATA (e)->attr;
3080 : 26576 : else if (e->symtree)
3081 : 26576 : attr = gfc_variable_attr (e, NULL);
3082 : :
3083 : : /* TODO: NULL() returns pointers. May have to take care of this
3084 : : here. */
3085 : :
3086 : : break;
3087 : :
3088 : 643402 : default:
3089 : 643402 : gfc_clear_attr (&attr);
3090 : 643402 : break;
3091 : : }
3092 : :
3093 : 3612133 : return attr;
3094 : : }
3095 : :
3096 : :
3097 : : /* Given an expression, figure out what the ultimate expression
3098 : : attribute is. This routine is similar to gfc_variable_attr with
3099 : : parts of gfc_expr_attr, but focuses more on the needs of
3100 : : coarrays. For coarrays a codimension attribute is kind of
3101 : : "infectious" being propagated once set and never cleared.
3102 : : The coarray_comp is only set, when the expression refs a coarray
3103 : : component. REFS_COMP is set when present to true only, when this EXPR
3104 : : refs a (non-_data) component. To check whether EXPR refs an allocatable
3105 : : component in a derived type coarray *refs_comp needs to be set and
3106 : : coarray_comp has to false. */
3107 : :
3108 : : static symbol_attribute
3109 : 11974 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3110 : : {
3111 : 11974 : int dimension, codimension, pointer, allocatable, target, coarray_comp;
3112 : 11974 : symbol_attribute attr;
3113 : 11974 : gfc_ref *ref;
3114 : 11974 : gfc_symbol *sym;
3115 : 11974 : gfc_component *comp;
3116 : :
3117 : 11974 : if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3118 : 0 : gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3119 : :
3120 : 11974 : sym = expr->symtree->n.sym;
3121 : 11974 : gfc_clear_attr (&attr);
3122 : :
3123 : 11974 : if (refs_comp)
3124 : 7368 : *refs_comp = false;
3125 : :
3126 : 11974 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3127 : : {
3128 : 369 : dimension = CLASS_DATA (sym)->attr.dimension;
3129 : 369 : codimension = CLASS_DATA (sym)->attr.codimension;
3130 : 369 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3131 : 369 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3132 : 369 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3133 : 369 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3134 : : }
3135 : : else
3136 : : {
3137 : 11605 : dimension = sym->attr.dimension;
3138 : 11605 : codimension = sym->attr.codimension;
3139 : 11605 : pointer = sym->attr.pointer;
3140 : 11605 : allocatable = sym->attr.allocatable;
3141 : 23210 : attr.alloc_comp = sym->ts.type == BT_DERIVED
3142 : 11605 : ? sym->ts.u.derived->attr.alloc_comp : 0;
3143 : 11605 : attr.pointer_comp = sym->ts.type == BT_DERIVED
3144 : 11605 : ? sym->ts.u.derived->attr.pointer_comp : 0;
3145 : : }
3146 : :
3147 : 11974 : target = coarray_comp = 0;
3148 : 11974 : if (pointer || attr.proc_pointer)
3149 : 509 : target = 1;
3150 : :
3151 : 21622 : for (ref = expr->ref; ref; ref = ref->next)
3152 : 9648 : switch (ref->type)
3153 : : {
3154 : 6288 : case REF_ARRAY:
3155 : :
3156 : 6288 : switch (ref->u.ar.type)
3157 : : {
3158 : : case AR_FULL:
3159 : : case AR_SECTION:
3160 : : dimension = 1;
3161 : 6288 : break;
3162 : :
3163 : 2819 : case AR_ELEMENT:
3164 : : /* Handle coarrays. */
3165 : 2819 : if (ref->u.ar.dimen > 0 && !in_allocate)
3166 : 6288 : allocatable = pointer = 0;
3167 : : break;
3168 : :
3169 : 0 : case AR_UNKNOWN:
3170 : : /* If any of start, end or stride is not integer, there will
3171 : : already have been an error issued. */
3172 : 0 : int errors;
3173 : 0 : gfc_get_errors (NULL, &errors);
3174 : 0 : if (errors == 0)
3175 : 0 : gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3176 : : }
3177 : :
3178 : : break;
3179 : :
3180 : 3359 : case REF_COMPONENT:
3181 : 3359 : comp = ref->u.c.component;
3182 : :
3183 : 3359 : if (comp->ts.type == BT_CLASS)
3184 : : {
3185 : : /* Set coarray_comp only, when this component introduces the
3186 : : coarray. */
3187 : 13 : coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3188 : 13 : codimension |= CLASS_DATA (comp)->attr.codimension;
3189 : 13 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3190 : 13 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3191 : : }
3192 : : else
3193 : : {
3194 : : /* Set coarray_comp only, when this component introduces the
3195 : : coarray. */
3196 : 3346 : coarray_comp = !codimension && comp->attr.codimension;
3197 : 3346 : codimension |= comp->attr.codimension;
3198 : 3346 : pointer = comp->attr.pointer;
3199 : 3346 : allocatable = comp->attr.allocatable;
3200 : : }
3201 : :
3202 : 3359 : if (refs_comp && strcmp (comp->name, "_data") != 0
3203 : 2054 : && (ref->next == NULL
3204 : 1563 : || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3205 : 1495 : *refs_comp = true;
3206 : :
3207 : 3359 : if (pointer || attr.proc_pointer)
3208 : 673 : target = 1;
3209 : :
3210 : : break;
3211 : :
3212 : : case REF_SUBSTRING:
3213 : : case REF_INQUIRY:
3214 : 9648 : allocatable = pointer = 0;
3215 : : break;
3216 : : }
3217 : :
3218 : 11974 : attr.dimension = dimension;
3219 : 11974 : attr.codimension = codimension;
3220 : 11974 : attr.pointer = pointer;
3221 : 11974 : attr.allocatable = allocatable;
3222 : 11974 : attr.target = target;
3223 : 11974 : attr.save = sym->attr.save;
3224 : 11974 : attr.coarray_comp = coarray_comp;
3225 : :
3226 : 11974 : return attr;
3227 : : }
3228 : :
3229 : :
3230 : : symbol_attribute
3231 : 14358 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3232 : : {
3233 : 14358 : symbol_attribute attr;
3234 : :
3235 : 14358 : switch (e->expr_type)
3236 : : {
3237 : 10947 : case EXPR_VARIABLE:
3238 : 10947 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3239 : 10947 : break;
3240 : :
3241 : 1031 : case EXPR_FUNCTION:
3242 : 1031 : gfc_clear_attr (&attr);
3243 : :
3244 : 1031 : if (e->value.function.esym && e->value.function.esym->result)
3245 : : {
3246 : 4 : gfc_symbol *sym = e->value.function.esym->result;
3247 : 4 : attr = sym->attr;
3248 : 4 : if (sym->ts.type == BT_CLASS)
3249 : : {
3250 : 0 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3251 : 0 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3252 : 0 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3253 : 0 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3254 : 0 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3255 : 0 : ->attr.pointer_comp;
3256 : : }
3257 : : }
3258 : 1027 : else if (e->symtree)
3259 : 1027 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3260 : : else
3261 : 0 : gfc_clear_attr (&attr);
3262 : : break;
3263 : :
3264 : 2380 : default:
3265 : 2380 : gfc_clear_attr (&attr);
3266 : 2380 : break;
3267 : : }
3268 : :
3269 : 14358 : return attr;
3270 : : }
3271 : :
3272 : :
3273 : : /* Match a structure constructor. The initial symbol has already been
3274 : : seen. */
3275 : :
3276 : : typedef struct gfc_structure_ctor_component
3277 : : {
3278 : : char* name;
3279 : : gfc_expr* val;
3280 : : locus where;
3281 : : struct gfc_structure_ctor_component* next;
3282 : : }
3283 : : gfc_structure_ctor_component;
3284 : :
3285 : : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3286 : :
3287 : : static void
3288 : 9575 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3289 : : {
3290 : 9575 : free (comp->name);
3291 : 9575 : gfc_free_expr (comp->val);
3292 : 9575 : free (comp);
3293 : 9575 : }
3294 : :
3295 : :
3296 : : /* Translate the component list into the actual constructor by sorting it in
3297 : : the order required; this also checks along the way that each and every
3298 : : component actually has an initializer and handles default initializers
3299 : : for components without explicit value given. */
3300 : : static bool
3301 : 6691 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
3302 : : gfc_constructor_base *ctor_head, gfc_symbol *sym)
3303 : : {
3304 : 6691 : gfc_structure_ctor_component *comp_iter;
3305 : 6691 : gfc_component *comp;
3306 : :
3307 : 17613 : for (comp = sym->components; comp; comp = comp->next)
3308 : : {
3309 : 10927 : gfc_structure_ctor_component **next_ptr;
3310 : 10927 : gfc_expr *value = NULL;
3311 : :
3312 : : /* Try to find the initializer for the current component by name. */
3313 : 10927 : next_ptr = comp_head;
3314 : 12059 : for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3315 : : {
3316 : 10683 : if (!strcmp (comp_iter->name, comp->name))
3317 : : break;
3318 : 1132 : next_ptr = &comp_iter->next;
3319 : : }
3320 : :
3321 : : /* If an extension, try building the parent derived type by building
3322 : : a value expression for the parent derived type and calling self. */
3323 : 10927 : if (!comp_iter && comp == sym->components && sym->attr.extension)
3324 : : {
3325 : 106 : value = gfc_get_structure_constructor_expr (comp->ts.type,
3326 : : comp->ts.kind,
3327 : : &gfc_current_locus);
3328 : 106 : value->ts = comp->ts;
3329 : :
3330 : 106 : if (!build_actual_constructor (comp_head,
3331 : : &value->value.constructor,
3332 : : comp->ts.u.derived))
3333 : : {
3334 : 0 : gfc_free_expr (value);
3335 : 0 : return false;
3336 : : }
3337 : :
3338 : 106 : gfc_constructor_append_expr (ctor_head, value, NULL);
3339 : 106 : continue;
3340 : : }
3341 : :
3342 : : /* If it was not found, apply NULL expression to set the component as
3343 : : unallocated. Then try the default initializer if there's any;
3344 : : otherwise, it's an error unless this is a deferred parameter. */
3345 : 1270 : if (!comp_iter)
3346 : : {
3347 : : /* F2018 7.5.10: If an allocatable component has no corresponding
3348 : : component-data-source, then that component has an allocation
3349 : : status of unallocated.... */
3350 : 1270 : if (comp->attr.allocatable
3351 : 1139 : || (comp->ts.type == BT_CLASS
3352 : 15 : && CLASS_DATA (comp)->attr.allocatable))
3353 : : {
3354 : 140 : if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3355 : : "allocatable component %qs given in the "
3356 : : "structure constructor at %C", comp->name))
3357 : : return false;
3358 : 140 : value = gfc_get_null_expr (&gfc_current_locus);
3359 : : }
3360 : : /* ....(Preceding sentence) If a component with default
3361 : : initialization has no corresponding component-data-source, then
3362 : : the default initialization is applied to that component. */
3363 : 1130 : else if (comp->initializer)
3364 : : {
3365 : 629 : if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3366 : : "with missing optional arguments at %C"))
3367 : : return false;
3368 : 627 : value = gfc_copy_expr (comp->initializer);
3369 : : }
3370 : : /* Do not trap components such as the string length for deferred
3371 : : length character components. */
3372 : 501 : else if (!comp->attr.artificial)
3373 : : {
3374 : 3 : gfc_error ("No initializer for component %qs given in the"
3375 : : " structure constructor at %C", comp->name);
3376 : 3 : return false;
3377 : : }
3378 : : }
3379 : : else
3380 : 9551 : value = comp_iter->val;
3381 : :
3382 : : /* Add the value to the constructor chain built. */
3383 : 10816 : gfc_constructor_append_expr (ctor_head, value, NULL);
3384 : :
3385 : : /* Remove the entry from the component list. We don't want the expression
3386 : : value to be free'd, so set it to NULL. */
3387 : 10816 : if (comp_iter)
3388 : : {
3389 : 9551 : *next_ptr = comp_iter->next;
3390 : 9551 : comp_iter->val = NULL;
3391 : 9551 : gfc_free_structure_ctor_component (comp_iter);
3392 : : }
3393 : : }
3394 : : return true;
3395 : : }
3396 : :
3397 : :
3398 : : bool
3399 : 6600 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3400 : : gfc_actual_arglist **arglist,
3401 : : bool parent)
3402 : : {
3403 : 6600 : gfc_actual_arglist *actual;
3404 : 6600 : gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3405 : 6600 : gfc_constructor_base ctor_head = NULL;
3406 : 6600 : gfc_component *comp; /* Is set NULL when named component is first seen */
3407 : 6600 : const char* last_name = NULL;
3408 : 6600 : locus old_locus;
3409 : 6600 : gfc_expr *expr;
3410 : :
3411 : 6600 : expr = parent ? *cexpr : e;
3412 : 6600 : old_locus = gfc_current_locus;
3413 : 6600 : if (parent)
3414 : : ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3415 : : else
3416 : 5898 : gfc_current_locus = expr->where;
3417 : :
3418 : 6600 : comp_tail = comp_head = NULL;
3419 : :
3420 : 6600 : if (!parent && sym->attr.abstract)
3421 : : {
3422 : 1 : gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3423 : : sym->name, &expr->where);
3424 : 1 : goto cleanup;
3425 : : }
3426 : :
3427 : 6599 : comp = sym->components;
3428 : 6599 : actual = parent ? *arglist : expr->value.function.actual;
3429 : 15585 : for ( ; actual; )
3430 : : {
3431 : 9575 : gfc_component *this_comp = NULL;
3432 : :
3433 : 9575 : if (!comp_head)
3434 : 6187 : comp_tail = comp_head = gfc_get_structure_ctor_component ();
3435 : : else
3436 : : {
3437 : 3388 : comp_tail->next = gfc_get_structure_ctor_component ();
3438 : 3388 : comp_tail = comp_tail->next;
3439 : : }
3440 : 9575 : if (actual->name)
3441 : : {
3442 : 835 : if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3443 : : " constructor with named arguments at %C"))
3444 : 1 : goto cleanup;
3445 : :
3446 : 834 : comp_tail->name = xstrdup (actual->name);
3447 : 834 : last_name = comp_tail->name;
3448 : 834 : comp = NULL;
3449 : : }
3450 : : else
3451 : : {
3452 : : /* Components without name are not allowed after the first named
3453 : : component initializer! */
3454 : 8740 : if (!comp || comp->attr.artificial)
3455 : : {
3456 : 2 : if (last_name)
3457 : 0 : gfc_error ("Component initializer without name after component"
3458 : : " named %s at %L", last_name,
3459 : 0 : actual->expr ? &actual->expr->where
3460 : : : &gfc_current_locus);
3461 : : else
3462 : 2 : gfc_error ("Too many components in structure constructor at "
3463 : 2 : "%L", actual->expr ? &actual->expr->where
3464 : : : &gfc_current_locus);
3465 : 2 : goto cleanup;
3466 : : }
3467 : :
3468 : 8738 : comp_tail->name = xstrdup (comp->name);
3469 : : }
3470 : :
3471 : : /* Find the current component in the structure definition and check
3472 : : its access is not private. */
3473 : 9572 : if (comp)
3474 : 8738 : this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3475 : : else
3476 : : {
3477 : 834 : this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3478 : : false, false, NULL);
3479 : 834 : comp = NULL; /* Reset needed! */
3480 : : }
3481 : :
3482 : : /* Here we can check if a component name is given which does not
3483 : : correspond to any component of the defined structure. */
3484 : 9572 : if (!this_comp)
3485 : 8 : goto cleanup;
3486 : :
3487 : : /* For a constant string constructor, make sure the length is
3488 : : correct; truncate or fill with blanks if needed. */
3489 : 9564 : if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3490 : 983 : && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3491 : 981 : && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3492 : 969 : && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3493 : 968 : && actual->expr->ts.type == BT_CHARACTER
3494 : 956 : && actual->expr->expr_type == EXPR_CONSTANT)
3495 : : {
3496 : 733 : ptrdiff_t c, e1;
3497 : 733 : c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3498 : 733 : e1 = actual->expr->value.character.length;
3499 : :
3500 : 733 : if (c != e1)
3501 : : {
3502 : 249 : ptrdiff_t i, to;
3503 : 249 : gfc_char_t *dest;
3504 : 249 : dest = gfc_get_wide_string (c + 1);
3505 : :
3506 : 249 : to = e1 < c ? e1 : c;
3507 : 4482 : for (i = 0; i < to; i++)
3508 : 4233 : dest[i] = actual->expr->value.character.string[i];
3509 : :
3510 : 5812 : for (i = e1; i < c; i++)
3511 : 5563 : dest[i] = ' ';
3512 : :
3513 : 249 : dest[c] = '\0';
3514 : 249 : free (actual->expr->value.character.string);
3515 : :
3516 : 249 : actual->expr->value.character.length = c;
3517 : 249 : actual->expr->value.character.string = dest;
3518 : :
3519 : 249 : if (warn_line_truncation && c < e1)
3520 : 14 : gfc_warning_now (OPT_Wcharacter_truncation,
3521 : : "CHARACTER expression will be truncated "
3522 : : "in constructor (%td/%td) at %L", c,
3523 : : e1, &actual->expr->where);
3524 : : }
3525 : : }
3526 : :
3527 : 9564 : comp_tail->val = actual->expr;
3528 : 9564 : if (actual->expr != NULL)
3529 : 9564 : comp_tail->where = actual->expr->where;
3530 : 9564 : actual->expr = NULL;
3531 : :
3532 : : /* Check if this component is already given a value. */
3533 : 15341 : for (comp_iter = comp_head; comp_iter != comp_tail;
3534 : 5777 : comp_iter = comp_iter->next)
3535 : : {
3536 : 5778 : gcc_assert (comp_iter);
3537 : 5778 : if (!strcmp (comp_iter->name, comp_tail->name))
3538 : : {
3539 : 1 : gfc_error ("Component %qs is initialized twice in the structure"
3540 : : " constructor at %L", comp_tail->name,
3541 : : comp_tail->val ? &comp_tail->where
3542 : : : &gfc_current_locus);
3543 : 1 : goto cleanup;
3544 : : }
3545 : : }
3546 : :
3547 : : /* F2008, R457/C725, for PURE C1283. */
3548 : 72 : if (this_comp->attr.pointer && comp_tail->val
3549 : 9635 : && gfc_is_coindexed (comp_tail->val))
3550 : : {
3551 : 2 : gfc_error ("Coindexed expression to pointer component %qs in "
3552 : : "structure constructor at %L", comp_tail->name,
3553 : : &comp_tail->where);
3554 : 2 : goto cleanup;
3555 : : }
3556 : :
3557 : : /* If not explicitly a parent constructor, gather up the components
3558 : : and build one. */
3559 : 9561 : if (comp && comp == sym->components
3560 : 5992 : && sym->attr.extension
3561 : 750 : && comp_tail->val
3562 : 750 : && (!gfc_bt_struct (comp_tail->val->ts.type)
3563 : 78 : ||
3564 : 78 : comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3565 : : {
3566 : 702 : bool m;
3567 : 702 : gfc_actual_arglist *arg_null = NULL;
3568 : :
3569 : 702 : actual->expr = comp_tail->val;
3570 : 702 : comp_tail->val = NULL;
3571 : :
3572 : 702 : m = gfc_convert_to_structure_constructor (NULL,
3573 : : comp->ts.u.derived, &comp_tail->val,
3574 : 702 : comp->ts.u.derived->attr.zero_comp
3575 : : ? &arg_null : &actual, true);
3576 : 702 : if (!m)
3577 : 0 : goto cleanup;
3578 : :
3579 : 702 : if (comp->ts.u.derived->attr.zero_comp)
3580 : : {
3581 : 126 : comp = comp->next;
3582 : 126 : continue;
3583 : : }
3584 : : }
3585 : :
3586 : 576 : if (comp)
3587 : 8604 : comp = comp->next;
3588 : 9435 : if (parent && !comp)
3589 : : break;
3590 : :
3591 : 8860 : if (actual)
3592 : 8859 : actual = actual->next;
3593 : : }
3594 : :
3595 : 6585 : if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3596 : 5 : goto cleanup;
3597 : :
3598 : : /* No component should be left, as this should have caused an error in the
3599 : : loop constructing the component-list (name that does not correspond to any
3600 : : component in the structure definition). */
3601 : 6580 : if (comp_head && sym->attr.extension)
3602 : : {
3603 : 2 : for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3604 : : {
3605 : 1 : gfc_error ("component %qs at %L has already been set by a "
3606 : : "parent derived type constructor", comp_iter->name,
3607 : : &comp_iter->where);
3608 : : }
3609 : 1 : goto cleanup;
3610 : : }
3611 : : else
3612 : 6579 : gcc_assert (!comp_head);
3613 : :
3614 : 6579 : if (parent)
3615 : : {
3616 : 702 : expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3617 : 702 : expr->ts.u.derived = sym;
3618 : 702 : expr->value.constructor = ctor_head;
3619 : 702 : *cexpr = expr;
3620 : : }
3621 : : else
3622 : : {
3623 : 5877 : expr->ts.u.derived = sym;
3624 : 5877 : expr->ts.kind = 0;
3625 : 5877 : expr->ts.type = BT_DERIVED;
3626 : 5877 : expr->value.constructor = ctor_head;
3627 : 5877 : expr->expr_type = EXPR_STRUCTURE;
3628 : : }
3629 : :
3630 : 6579 : gfc_current_locus = old_locus;
3631 : 6579 : if (parent)
3632 : 702 : *arglist = actual;
3633 : : return true;
3634 : :
3635 : 21 : cleanup:
3636 : 21 : gfc_current_locus = old_locus;
3637 : :
3638 : 45 : for (comp_iter = comp_head; comp_iter; )
3639 : : {
3640 : 24 : gfc_structure_ctor_component *next = comp_iter->next;
3641 : 24 : gfc_free_structure_ctor_component (comp_iter);
3642 : 24 : comp_iter = next;
3643 : : }
3644 : 21 : gfc_constructor_free (ctor_head);
3645 : :
3646 : 21 : return false;
3647 : : }
3648 : :
3649 : :
3650 : : match
3651 : 60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3652 : : gfc_expr **result)
3653 : : {
3654 : 60 : match m;
3655 : 60 : gfc_expr *e;
3656 : 60 : bool t = true;
3657 : :
3658 : 60 : e = gfc_get_expr ();
3659 : 60 : e->expr_type = EXPR_FUNCTION;
3660 : 60 : e->symtree = symtree;
3661 : 60 : e->where = gfc_current_locus;
3662 : :
3663 : 60 : gcc_assert (gfc_fl_struct (sym->attr.flavor)
3664 : : && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3665 : 60 : e->value.function.esym = sym;
3666 : 60 : e->symtree->n.sym->attr.generic = 1;
3667 : :
3668 : 60 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
3669 : 60 : if (m != MATCH_YES)
3670 : : {
3671 : 0 : gfc_free_expr (e);
3672 : 0 : return m;
3673 : : }
3674 : :
3675 : 60 : if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3676 : : {
3677 : 1 : gfc_free_expr (e);
3678 : 1 : return MATCH_ERROR;
3679 : : }
3680 : :
3681 : : /* If a structure constructor is in a DATA statement, then each entity
3682 : : in the structure constructor must be a constant. Try to reduce the
3683 : : expression here. */
3684 : 59 : if (gfc_in_match_data ())
3685 : 59 : t = gfc_reduce_init_expr (e);
3686 : :
3687 : 59 : if (t)
3688 : : {
3689 : 49 : *result = e;
3690 : 49 : return MATCH_YES;
3691 : : }
3692 : : else
3693 : : {
3694 : 10 : gfc_free_expr (e);
3695 : 10 : return MATCH_ERROR;
3696 : : }
3697 : : }
3698 : :
3699 : :
3700 : : /* If the symbol is an implicit do loop index and implicitly typed,
3701 : : it should not be host associated. Provide a symtree from the
3702 : : current namespace. */
3703 : : static match
3704 : 6546014 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3705 : : {
3706 : 6546014 : if ((*sym)->attr.flavor == FL_VARIABLE
3707 : 1912090 : && (*sym)->ns != gfc_current_ns
3708 : : && (*sym)->attr.implied_index
3709 : : && (*sym)->attr.implicit_type
3710 : 56749 : && !(*sym)->attr.use_assoc)
3711 : : {
3712 : 32 : int i;
3713 : 32 : i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3714 : 32 : if (i)
3715 : : return MATCH_ERROR;
3716 : 32 : *sym = (*st)->n.sym;
3717 : : }
3718 : : return MATCH_YES;
3719 : : }
3720 : :
3721 : :
3722 : : /* Procedure pointer as function result: Replace the function symbol by the
3723 : : auto-generated hidden result variable named "ppr@". */
3724 : :
3725 : : static bool
3726 : 4872719 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3727 : : {
3728 : : /* Check for procedure pointer result variable. */
3729 : 4872719 : if ((*sym)->attr.function && !(*sym)->attr.external
3730 : 1308688 : && (*sym)->result && (*sym)->result != *sym
3731 : 10299 : && (*sym)->result->attr.proc_pointer
3732 : 323 : && (*sym) == gfc_current_ns->proc_name
3733 : 283 : && (*sym) == (*sym)->result->ns->proc_name
3734 : 283 : && strcmp ("ppr@", (*sym)->result->name) == 0)
3735 : : {
3736 : : /* Automatic replacement with "hidden" result variable. */
3737 : 283 : (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3738 : 283 : *sym = (*sym)->result;
3739 : 283 : *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3740 : 283 : return true;
3741 : : }
3742 : : return false;
3743 : : }
3744 : :
3745 : :
3746 : : /* Matches a variable name followed by anything that might follow it--
3747 : : array reference, argument list of a function, etc. */
3748 : :
3749 : : match
3750 : 4036294 : gfc_match_rvalue (gfc_expr **result)
3751 : : {
3752 : 4036294 : gfc_actual_arglist *actual_arglist;
3753 : 4036294 : char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3754 : 4036294 : gfc_state_data *st;
3755 : 4036294 : gfc_symbol *sym;
3756 : 4036294 : gfc_symtree *symtree;
3757 : 4036294 : locus where, old_loc;
3758 : 4036294 : gfc_expr *e;
3759 : 4036294 : match m, m2;
3760 : 4036294 : int i;
3761 : 4036294 : gfc_typespec *ts;
3762 : 4036294 : bool implicit_char;
3763 : 4036294 : gfc_ref *ref;
3764 : :
3765 : 4036294 : m = gfc_match ("%%loc");
3766 : 4036294 : if (m == MATCH_YES)
3767 : : {
3768 : 10878 : if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3769 : : return MATCH_ERROR;
3770 : 10877 : strncpy (name, "loc", 4);
3771 : : }
3772 : :
3773 : : else
3774 : : {
3775 : 4025416 : m = gfc_match_name (name);
3776 : 4025416 : if (m != MATCH_YES)
3777 : : return m;
3778 : : }
3779 : :
3780 : : /* Check if the symbol exists. */
3781 : 3840095 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3782 : : return MATCH_ERROR;
3783 : :
3784 : : /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3785 : : type. For derived types we create a generic symbol which links to the
3786 : : derived type symbol; STRUCTUREs are simpler and must not conflict with
3787 : : variables. */
3788 : 3840093 : if (!symtree)
3789 : 171404 : if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3790 : : return MATCH_ERROR;
3791 : 3840093 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3792 : : {
3793 : 3840093 : if (gfc_find_state (COMP_INTERFACE)
3794 : 3840093 : && !gfc_current_ns->has_import_set)
3795 : 82034 : i = gfc_get_sym_tree (name, NULL, &symtree, false);
3796 : : else
3797 : 3758059 : i = gfc_get_ha_sym_tree (name, &symtree);
3798 : 3840093 : if (i)
3799 : : return MATCH_ERROR;
3800 : : }
3801 : :
3802 : :
3803 : 3840093 : sym = symtree->n.sym;
3804 : 3840093 : e = NULL;
3805 : 3840093 : where = gfc_current_locus;
3806 : :
3807 : 3840093 : replace_hidden_procptr_result (&sym, &symtree);
3808 : :
3809 : : /* If this is an implicit do loop index and implicitly typed,
3810 : : it should not be host associated. */
3811 : 3840093 : m = check_for_implicit_index (&symtree, &sym);
3812 : 3840093 : if (m != MATCH_YES)
3813 : : return m;
3814 : :
3815 : 3840093 : gfc_set_sym_referenced (sym);
3816 : 3840093 : sym->attr.implied_index = 0;
3817 : :
3818 : 3840093 : if (sym->attr.function && sym->result == sym)
3819 : : {
3820 : : /* See if this is a directly recursive function call. */
3821 : 658182 : gfc_gobble_whitespace ();
3822 : 658182 : if (sym->attr.recursive
3823 : 100 : && gfc_peek_ascii_char () == '('
3824 : 93 : && gfc_current_ns->proc_name == sym
3825 : 658189 : && !sym->attr.dimension)
3826 : : {
3827 : 4 : gfc_error ("%qs at %C is the name of a recursive function "
3828 : : "and so refers to the result variable. Use an "
3829 : : "explicit RESULT variable for direct recursion "
3830 : : "(12.5.2.1)", sym->name);
3831 : 4 : return MATCH_ERROR;
3832 : : }
3833 : :
3834 : 658178 : if (gfc_is_function_return_value (sym, gfc_current_ns))
3835 : 1677 : goto variable;
3836 : :
3837 : 656501 : if (sym->attr.entry
3838 : 187 : && (sym->ns == gfc_current_ns
3839 : 27 : || sym->ns == gfc_current_ns->parent))
3840 : : {
3841 : 180 : gfc_entry_list *el = NULL;
3842 : :
3843 : 180 : for (el = sym->ns->entries; el; el = el->next)
3844 : 180 : if (sym == el->sym)
3845 : 180 : goto variable;
3846 : : }
3847 : : }
3848 : :
3849 : 3838232 : if (gfc_matching_procptr_assignment)
3850 : : {
3851 : : /* It can be a procedure or a derived-type procedure or a not-yet-known
3852 : : type. */
3853 : 1297 : if (sym->attr.flavor != FL_UNKNOWN
3854 : 971 : && sym->attr.flavor != FL_PROCEDURE
3855 : : && sym->attr.flavor != FL_PARAMETER
3856 : : && sym->attr.flavor != FL_VARIABLE)
3857 : : {
3858 : 2 : gfc_error ("Symbol at %C is not appropriate for an expression");
3859 : 2 : return MATCH_ERROR;
3860 : : }
3861 : 1295 : goto procptr0;
3862 : : }
3863 : :
3864 : 3836935 : if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3865 : 669996 : goto function0;
3866 : :
3867 : 3166939 : if (sym->attr.generic)
3868 : 67679 : goto generic_function;
3869 : :
3870 : 3099260 : switch (sym->attr.flavor)
3871 : : {
3872 : 1654187 : case FL_VARIABLE:
3873 : 1654187 : variable:
3874 : 1654187 : e = gfc_get_expr ();
3875 : :
3876 : 1654187 : e->expr_type = EXPR_VARIABLE;
3877 : 1654187 : e->symtree = symtree;
3878 : :
3879 : 1654187 : m = gfc_match_varspec (e, 0, false, true);
3880 : 1654187 : break;
3881 : :
3882 : 210548 : case FL_PARAMETER:
3883 : : /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
3884 : : end up here. Unfortunately, sym->value->expr_type is set to
3885 : : EXPR_CONSTANT, and so the if () branch would be followed without
3886 : : the !sym->as check. */
3887 : 210548 : if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
3888 : 176877 : e = gfc_copy_expr (sym->value);
3889 : : else
3890 : : {
3891 : 33671 : e = gfc_get_expr ();
3892 : 33671 : e->expr_type = EXPR_VARIABLE;
3893 : : }
3894 : :
3895 : 210548 : e->symtree = symtree;
3896 : 210548 : m = gfc_match_varspec (e, 0, false, true);
3897 : :
3898 : 210548 : if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3899 : : break;
3900 : :
3901 : : /* Variable array references to derived type parameters cause
3902 : : all sorts of headaches in simplification. Treating such
3903 : : expressions as variable works just fine for all array
3904 : : references. */
3905 : 164009 : if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
3906 : : {
3907 : 2743 : for (ref = e->ref; ref; ref = ref->next)
3908 : 2557 : if (ref->type == REF_ARRAY)
3909 : : break;
3910 : :
3911 : 2524 : if (ref == NULL || ref->u.ar.type == AR_FULL)
3912 : : break;
3913 : :
3914 : 985 : ref = e->ref;
3915 : 985 : e->ref = NULL;
3916 : 985 : gfc_free_expr (e);
3917 : 985 : e = gfc_get_expr ();
3918 : 985 : e->expr_type = EXPR_VARIABLE;
3919 : 985 : e->symtree = symtree;
3920 : 985 : e->ref = ref;
3921 : : }
3922 : :
3923 : : break;
3924 : :
3925 : 0 : case FL_STRUCT:
3926 : 0 : case FL_DERIVED:
3927 : 0 : sym = gfc_use_derived (sym);
3928 : 0 : if (sym == NULL)
3929 : : m = MATCH_ERROR;
3930 : : else
3931 : 0 : goto generic_function;
3932 : : break;
3933 : :
3934 : : /* If we're here, then the name is known to be the name of a
3935 : : procedure, yet it is not sure to be the name of a function. */
3936 : 960270 : case FL_PROCEDURE:
3937 : :
3938 : : /* Procedure Pointer Assignments. */
3939 : 960270 : procptr0:
3940 : 960270 : if (gfc_matching_procptr_assignment)
3941 : : {
3942 : 1295 : gfc_gobble_whitespace ();
3943 : 1295 : if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
3944 : : /* Parse functions returning a procptr. */
3945 : 201 : goto function0;
3946 : :
3947 : 1094 : e = gfc_get_expr ();
3948 : 1094 : e->expr_type = EXPR_VARIABLE;
3949 : 1094 : e->symtree = symtree;
3950 : 1094 : m = gfc_match_varspec (e, 0, false, true);
3951 : 1026 : if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3952 : 184 : && sym->ts.type == BT_UNKNOWN
3953 : 1268 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
3954 : : {
3955 : : m = MATCH_ERROR;
3956 : : break;
3957 : : }
3958 : : break;
3959 : : }
3960 : :
3961 : 958975 : if (sym->attr.subroutine)
3962 : : {
3963 : 57 : gfc_error ("Unexpected use of subroutine name %qs at %C",
3964 : : sym->name);
3965 : 57 : m = MATCH_ERROR;
3966 : 57 : break;
3967 : : }
3968 : :
3969 : : /* At this point, the name has to be a non-statement function.
3970 : : If the name is the same as the current function being
3971 : : compiled, then we have a variable reference (to the function
3972 : : result) if the name is non-recursive. */
3973 : :
3974 : 958918 : st = gfc_enclosing_unit (NULL);
3975 : :
3976 : 958918 : if (st != NULL
3977 : 916432 : && st->state == COMP_FUNCTION
3978 : 79897 : && st->sym == sym
3979 : 0 : && !sym->attr.recursive)
3980 : : {
3981 : 0 : e = gfc_get_expr ();
3982 : 0 : e->symtree = symtree;
3983 : 0 : e->expr_type = EXPR_VARIABLE;
3984 : :
3985 : 0 : m = gfc_match_varspec (e, 0, false, true);
3986 : 0 : break;
3987 : : }
3988 : :
3989 : : /* Match a function reference. */
3990 : 958918 : function0:
3991 : 1629115 : m = gfc_match_actual_arglist (0, &actual_arglist);
3992 : 1629115 : if (m == MATCH_NO)
3993 : : {
3994 : 568662 : if (sym->attr.proc == PROC_ST_FUNCTION)
3995 : 1 : gfc_error ("Statement function %qs requires argument list at %C",
3996 : : sym->name);
3997 : : else
3998 : 568661 : gfc_error ("Function %qs requires an argument list at %C",
3999 : : sym->name);
4000 : :
4001 : : m = MATCH_ERROR;
4002 : : break;
4003 : : }
4004 : :
4005 : 1060453 : if (m != MATCH_YES)
4006 : : {
4007 : : m = MATCH_ERROR;
4008 : : break;
4009 : : }
4010 : :
4011 : 1030271 : gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4012 : 1030271 : sym = symtree->n.sym;
4013 : :
4014 : 1030271 : replace_hidden_procptr_result (&sym, &symtree);
4015 : :
4016 : 1030271 : e = gfc_get_expr ();
4017 : 1030271 : e->symtree = symtree;
4018 : 1030271 : e->expr_type = EXPR_FUNCTION;
4019 : 1030271 : e->value.function.actual = actual_arglist;
4020 : 1030271 : e->where = gfc_current_locus;
4021 : :
4022 : 1030271 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4023 : 206 : && CLASS_DATA (sym)->as)
4024 : : {
4025 : 91 : e->rank = CLASS_DATA (sym)->as->rank;
4026 : 91 : e->corank = CLASS_DATA (sym)->as->corank;
4027 : : }
4028 : 1030180 : else if (sym->as != NULL)
4029 : : {
4030 : 1113 : e->rank = sym->as->rank;
4031 : 1113 : e->corank = sym->as->corank;
4032 : : }
4033 : :
4034 : 1030271 : if (!sym->attr.function
4035 : 1030271 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4036 : : {
4037 : : m = MATCH_ERROR;
4038 : : break;
4039 : : }
4040 : :
4041 : : /* Check here for the existence of at least one argument for the
4042 : : iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4043 : 1030271 : if (sym->attr.is_iso_c == 1
4044 : 2 : && (sym->from_intmod == INTMOD_ISO_C_BINDING
4045 : 2 : && (sym->intmod_sym_id == ISOCBINDING_LOC
4046 : 2 : || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4047 : 2 : || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4048 : 2 : || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4049 : : {
4050 : : /* make sure we were given a param */
4051 : 0 : if (actual_arglist == NULL)
4052 : : {
4053 : 0 : gfc_error ("Missing argument to %qs at %C", sym->name);
4054 : 0 : m = MATCH_ERROR;
4055 : 0 : break;
4056 : : }
4057 : : }
4058 : :
4059 : 1030271 : if (sym->result == NULL)
4060 : 367591 : sym->result = sym;
4061 : :
4062 : 1030271 : gfc_gobble_whitespace ();
4063 : : /* F08:C612. */
4064 : 1030271 : if (gfc_peek_ascii_char() == '%')
4065 : : {
4066 : 12 : gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4067 : : "function reference at %C");
4068 : 12 : m = MATCH_ERROR;
4069 : 12 : break;
4070 : : }
4071 : :
4072 : : m = MATCH_YES;
4073 : : break;
4074 : :
4075 : 275911 : case FL_UNKNOWN:
4076 : :
4077 : : /* Special case for derived type variables that get their types
4078 : : via an IMPLICIT statement. This can't wait for the
4079 : : resolution phase. */
4080 : :
4081 : 275911 : old_loc = gfc_current_locus;
4082 : 275911 : if (gfc_match_member_sep (sym) == MATCH_YES
4083 : 9518 : && sym->ts.type == BT_UNKNOWN
4084 : 275916 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4085 : 0 : gfc_set_default_type (sym, 0, sym->ns);
4086 : 275911 : gfc_current_locus = old_loc;
4087 : :
4088 : : /* If the symbol has a (co)dimension attribute, the expression is a
4089 : : variable. */
4090 : :
4091 : 275911 : if (sym->attr.dimension || sym->attr.codimension)
4092 : : {
4093 : 34496 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4094 : : {
4095 : : m = MATCH_ERROR;
4096 : : break;
4097 : : }
4098 : :
4099 : 34496 : e = gfc_get_expr ();
4100 : 34496 : e->symtree = symtree;
4101 : 34496 : e->expr_type = EXPR_VARIABLE;
4102 : 34496 : m = gfc_match_varspec (e, 0, false, true);
4103 : 34496 : break;
4104 : : }
4105 : :
4106 : 241415 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4107 : 4511 : && (CLASS_DATA (sym)->attr.dimension
4108 : 4511 : || CLASS_DATA (sym)->attr.codimension))
4109 : : {
4110 : 1467 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4111 : : {
4112 : : m = MATCH_ERROR;
4113 : : break;
4114 : : }
4115 : :
4116 : 1467 : e = gfc_get_expr ();
4117 : 1467 : e->symtree = symtree;
4118 : 1467 : e->expr_type = EXPR_VARIABLE;
4119 : 1467 : m = gfc_match_varspec (e, 0, false, true);
4120 : 1467 : break;
4121 : : }
4122 : :
4123 : : /* Name is not an array, so we peek to see if a '(' implies a
4124 : : function call or a substring reference. Otherwise the
4125 : : variable is just a scalar. */
4126 : :
4127 : 239948 : gfc_gobble_whitespace ();
4128 : 239948 : if (gfc_peek_ascii_char () != '(')
4129 : : {
4130 : : /* Assume a scalar variable */
4131 : 72678 : e = gfc_get_expr ();
4132 : 72678 : e->symtree = symtree;
4133 : 72678 : e->expr_type = EXPR_VARIABLE;
4134 : :
4135 : 72678 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4136 : : {
4137 : : m = MATCH_ERROR;
4138 : : break;
4139 : : }
4140 : :
4141 : : /*FIXME:??? gfc_match_varspec does set this for us: */
4142 : 72678 : e->ts = sym->ts;
4143 : 72678 : m = gfc_match_varspec (e, 0, false, true);
4144 : 72678 : break;
4145 : : }
4146 : :
4147 : : /* See if this is a function reference with a keyword argument
4148 : : as first argument. We do this because otherwise a spurious
4149 : : symbol would end up in the symbol table. */
4150 : :
4151 : 167270 : old_loc = gfc_current_locus;
4152 : 167270 : m2 = gfc_match (" ( %n =", argname);
4153 : 167270 : gfc_current_locus = old_loc;
4154 : :
4155 : 167270 : e = gfc_get_expr ();
4156 : 167270 : e->symtree = symtree;
4157 : :
4158 : 167270 : if (m2 != MATCH_YES)
4159 : : {
4160 : : /* Try to figure out whether we're dealing with a character type.
4161 : : We're peeking ahead here, because we don't want to call
4162 : : match_substring if we're dealing with an implicitly typed
4163 : : non-character variable. */
4164 : 166238 : implicit_char = false;
4165 : 166238 : if (sym->ts.type == BT_UNKNOWN)
4166 : : {
4167 : 161542 : ts = gfc_get_default_type (sym->name, NULL);
4168 : 161542 : if (ts->type == BT_CHARACTER)
4169 : : implicit_char = true;
4170 : : }
4171 : :
4172 : : /* See if this could possibly be a substring reference of a name
4173 : : that we're not sure is a variable yet. */
4174 : :
4175 : 166221 : if ((implicit_char || sym->ts.type == BT_CHARACTER)
4176 : 1371 : && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4177 : : {
4178 : :
4179 : 907 : e->expr_type = EXPR_VARIABLE;
4180 : :
4181 : 907 : if (sym->attr.flavor != FL_VARIABLE
4182 : 907 : && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4183 : : sym->name, NULL))
4184 : : {
4185 : : m = MATCH_ERROR;
4186 : : break;
4187 : : }
4188 : :
4189 : 907 : if (sym->ts.type == BT_UNKNOWN
4190 : 907 : && !gfc_set_default_type (sym, 1, NULL))
4191 : : {
4192 : : m = MATCH_ERROR;
4193 : : break;
4194 : : }
4195 : :
4196 : 907 : e->ts = sym->ts;
4197 : 907 : if (e->ref)
4198 : 882 : e->ts.u.cl = NULL;
4199 : : m = MATCH_YES;
4200 : : break;
4201 : : }
4202 : : }
4203 : :
4204 : : /* Give up, assume we have a function. */
4205 : :
4206 : 166363 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4207 : 166363 : sym = symtree->n.sym;
4208 : 166363 : e->expr_type = EXPR_FUNCTION;
4209 : :
4210 : 166363 : if (!sym->attr.function
4211 : 166363 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4212 : : {
4213 : : m = MATCH_ERROR;
4214 : : break;
4215 : : }
4216 : :
4217 : 166363 : sym->result = sym;
4218 : :
4219 : 166363 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4220 : 166363 : if (m == MATCH_NO)
4221 : 0 : gfc_error ("Missing argument list in function %qs at %C", sym->name);
4222 : :
4223 : 166363 : if (m != MATCH_YES)
4224 : : {
4225 : : m = MATCH_ERROR;
4226 : : break;
4227 : : }
4228 : :
4229 : : /* If our new function returns a character, array or structure
4230 : : type, it might have subsequent references. */
4231 : :
4232 : 166233 : m = gfc_match_varspec (e, 0, false, true);
4233 : 166233 : if (m == MATCH_NO)
4234 : : m = MATCH_YES;
4235 : :
4236 : : break;
4237 : :
4238 : 67679 : generic_function:
4239 : : /* Look for symbol first; if not found, look for STRUCTURE type symbol
4240 : : specially. Creates a generic symbol for derived types. */
4241 : 67679 : gfc_find_sym_tree (name, NULL, 1, &symtree);
4242 : 67679 : if (!symtree)
4243 : 0 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4244 : 67679 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4245 : 67679 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4246 : :
4247 : 67679 : e = gfc_get_expr ();
4248 : 67679 : e->symtree = symtree;
4249 : 67679 : e->expr_type = EXPR_FUNCTION;
4250 : :
4251 : 67679 : if (gfc_fl_struct (sym->attr.flavor))
4252 : : {
4253 : 0 : e->value.function.esym = sym;
4254 : 0 : e->symtree->n.sym->attr.generic = 1;
4255 : : }
4256 : :
4257 : 67679 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4258 : 67679 : break;
4259 : :
4260 : : case FL_NAMELIST:
4261 : : m = MATCH_ERROR;
4262 : : break;
4263 : :
4264 : 5 : default:
4265 : 5 : gfc_error ("Symbol at %C is not appropriate for an expression");
4266 : 5 : return MATCH_ERROR;
4267 : : }
4268 : :
4269 : 2208451 : if (m == MATCH_YES)
4270 : : {
4271 : 3239381 : e->where = where;
4272 : 3239381 : *result = e;
4273 : : }
4274 : : else
4275 : 600701 : gfc_free_expr (e);
4276 : :
4277 : : return m;
4278 : : }
4279 : :
4280 : :
4281 : : /* Match a variable, i.e. something that can be assigned to. This
4282 : : starts as a symbol, can be a structure component or an array
4283 : : reference. It can be a function if the function doesn't have a
4284 : : separate RESULT variable. If the symbol has not been previously
4285 : : seen, we assume it is a variable.
4286 : :
4287 : : This function is called by two interface functions:
4288 : : gfc_match_variable, which has host_flag = 1, and
4289 : : gfc_match_equiv_variable, with host_flag = 0, to restrict the
4290 : : match of the symbol to the local scope. */
4291 : :
4292 : : static match
4293 : 2705946 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4294 : : {
4295 : 2705946 : gfc_symbol *sym, *dt_sym;
4296 : 2705946 : gfc_symtree *st;
4297 : 2705946 : gfc_expr *expr;
4298 : 2705946 : locus where, old_loc;
4299 : 2705946 : match m;
4300 : :
4301 : 2705946 : *result = NULL;
4302 : :
4303 : : /* Since nothing has any business being an lvalue in a module
4304 : : specification block, an interface block or a contains section,
4305 : : we force the changed_symbols mechanism to work by setting
4306 : : host_flag to 0. This prevents valid symbols that have the name
4307 : : of keywords, such as 'end', being turned into variables by
4308 : : failed matching to assignments for, e.g., END INTERFACE. */
4309 : 2705946 : if (gfc_current_state () == COMP_MODULE
4310 : 2705946 : || gfc_current_state () == COMP_SUBMODULE
4311 : : || gfc_current_state () == COMP_INTERFACE
4312 : : || gfc_current_state () == COMP_CONTAINS)
4313 : 181850 : host_flag = 0;
4314 : :
4315 : 2705946 : where = gfc_current_locus;
4316 : 2705946 : m = gfc_match_sym_tree (&st, host_flag);
4317 : 2705945 : if (m != MATCH_YES)
4318 : : return m;
4319 : :
4320 : 2705921 : sym = st->n.sym;
4321 : :
4322 : : /* If this is an implicit do loop index and implicitly typed,
4323 : : it should not be host associated. */
4324 : 2705921 : m = check_for_implicit_index (&st, &sym);
4325 : 2705921 : if (m != MATCH_YES)
4326 : : return m;
4327 : :
4328 : 2705921 : sym->attr.implied_index = 0;
4329 : :
4330 : 2705921 : gfc_set_sym_referenced (sym);
4331 : :
4332 : : /* STRUCTUREs may share names with variables, but derived types may not. */
4333 : 13335 : if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4334 : 2705987 : && (dt_sym = gfc_find_dt_in_generic (sym)))
4335 : : {
4336 : 5 : if (dt_sym->attr.flavor == FL_DERIVED)
4337 : 5 : gfc_error ("Derived type %qs cannot be used as a variable at %C",
4338 : : sym->name);
4339 : 5 : return MATCH_ERROR;
4340 : : }
4341 : :
4342 : 2705916 : switch (sym->attr.flavor)
4343 : : {
4344 : : case FL_VARIABLE:
4345 : : /* Everything is alright. */
4346 : : break;
4347 : :
4348 : 2432707 : case FL_UNKNOWN:
4349 : 2432707 : {
4350 : 2432707 : sym_flavor flavor = FL_UNKNOWN;
4351 : :
4352 : 2432707 : gfc_gobble_whitespace ();
4353 : :
4354 : 2432707 : if (sym->attr.external || sym->attr.procedure
4355 : 2432707 : || sym->attr.function || sym->attr.subroutine)
4356 : : flavor = FL_PROCEDURE;
4357 : :
4358 : : /* If it is not a procedure, is not typed and is host associated,
4359 : : we cannot give it a flavor yet. */
4360 : 2432675 : else if (sym->ns == gfc_current_ns->parent
4361 : 2615 : && sym->ts.type == BT_UNKNOWN)
4362 : : break;
4363 : :
4364 : : /* These are definitive indicators that this is a variable. */
4365 : 3236164 : else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4366 : 3218699 : || sym->attr.pointer || sym->as != NULL)
4367 : : flavor = FL_VARIABLE;
4368 : :
4369 : : if (flavor != FL_UNKNOWN
4370 : 1647287 : && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4371 : : return MATCH_ERROR;
4372 : : }
4373 : : break;
4374 : :
4375 : 17 : case FL_PARAMETER:
4376 : 17 : if (equiv_flag)
4377 : : {
4378 : 0 : gfc_error ("Named constant at %C in an EQUIVALENCE");
4379 : 0 : return MATCH_ERROR;
4380 : : }
4381 : 17 : if (gfc_in_match_data())
4382 : : {
4383 : 4 : gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4384 : : sym->name);
4385 : 4 : return MATCH_ERROR;
4386 : : }
4387 : : /* Otherwise this is checked for an error given in the
4388 : : variable definition context checks. */
4389 : : break;
4390 : :
4391 : 13330 : case FL_PROCEDURE:
4392 : : /* Check for a nonrecursive function result variable. */
4393 : 13330 : if (sym->attr.function
4394 : 13330 : && !sym->attr.external
4395 : 11310 : && sym->result == sym
4396 : 24293 : && (gfc_is_function_return_value (sym, gfc_current_ns)
4397 : 2037 : || (sym->attr.entry
4398 : 467 : && sym->ns == gfc_current_ns)
4399 : 1577 : || (sym->attr.entry
4400 : 7 : && sym->ns == gfc_current_ns->parent)))
4401 : : {
4402 : : /* If a function result is a derived type, then the derived
4403 : : type may still have to be resolved. */
4404 : :
4405 : 9393 : if (sym->ts.type == BT_DERIVED
4406 : 9393 : && gfc_use_derived (sym->ts.u.derived) == NULL)
4407 : : return MATCH_ERROR;
4408 : : break;
4409 : : }
4410 : :
4411 : 3937 : if (sym->attr.proc_pointer
4412 : 3937 : || replace_hidden_procptr_result (&sym, &st))
4413 : : break;
4414 : :
4415 : : /* Fall through to error */
4416 : 2322 : gcc_fallthrough ();
4417 : :
4418 : 2322 : default:
4419 : 2322 : gfc_error ("%qs at %C is not a variable", sym->name);
4420 : 2322 : return MATCH_ERROR;
4421 : : }
4422 : :
4423 : : /* Special case for derived type variables that get their types
4424 : : via an IMPLICIT statement. This can't wait for the
4425 : : resolution phase. */
4426 : :
4427 : 2703586 : {
4428 : 2703586 : gfc_namespace * implicit_ns;
4429 : :
4430 : 2703586 : if (gfc_current_ns->proc_name == sym)
4431 : : implicit_ns = gfc_current_ns;
4432 : : else
4433 : 2695004 : implicit_ns = sym->ns;
4434 : :
4435 : 2703586 : old_loc = gfc_current_locus;
4436 : 2703586 : if (gfc_match_member_sep (sym) == MATCH_YES
4437 : 18772 : && sym->ts.type == BT_UNKNOWN
4438 : 2703598 : && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4439 : 3 : gfc_set_default_type (sym, 0, implicit_ns);
4440 : 2703586 : gfc_current_locus = old_loc;
4441 : : }
4442 : :
4443 : 2703586 : expr = gfc_get_expr ();
4444 : :
4445 : 2703586 : expr->expr_type = EXPR_VARIABLE;
4446 : 2703586 : expr->symtree = st;
4447 : 2703586 : expr->ts = sym->ts;
4448 : :
4449 : : /* Now see if we have to do more. */
4450 : 2703586 : m = gfc_match_varspec (expr, equiv_flag, false, false);
4451 : 2703586 : if (m != MATCH_YES)
4452 : : {
4453 : 79 : gfc_free_expr (expr);
4454 : 79 : return m;
4455 : : }
4456 : :
4457 : 2703507 : expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4458 : 2703507 : *result = expr;
4459 : 2703507 : return MATCH_YES;
4460 : : }
4461 : :
4462 : :
4463 : : match
4464 : 2703001 : gfc_match_variable (gfc_expr **result, int equiv_flag)
4465 : : {
4466 : 2703001 : return match_variable (result, equiv_flag, 1);
4467 : : }
4468 : :
4469 : :
4470 : : match
4471 : 2945 : gfc_match_equiv_variable (gfc_expr **result)
4472 : : {
4473 : 2945 : return match_variable (result, 1, 0);
4474 : : }
|