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 : 469325 : match_kind_param (int *kind, int *is_iso_c)
41 : : {
42 : 469325 : char name[GFC_MAX_SYMBOL_LEN + 1];
43 : 469325 : gfc_symbol *sym;
44 : 469325 : match m;
45 : :
46 : 469325 : *is_iso_c = 0;
47 : :
48 : 469325 : m = gfc_match_small_literal_int (kind, NULL, false);
49 : 469325 : if (m != MATCH_NO)
50 : : return m;
51 : :
52 : 94013 : m = gfc_match_name (name, false);
53 : 94013 : if (m != MATCH_YES)
54 : : return m;
55 : :
56 : 92281 : if (gfc_find_symbol (name, NULL, 1, &sym))
57 : : return MATCH_ERROR;
58 : :
59 : 92281 : if (sym == NULL)
60 : : return MATCH_NO;
61 : :
62 : 92280 : *is_iso_c = sym->attr.is_iso_c;
63 : :
64 : 92280 : if (sym->attr.flavor != FL_PARAMETER)
65 : : return MATCH_NO;
66 : :
67 : 92280 : if (sym->value == NULL)
68 : : return MATCH_NO;
69 : :
70 : 92279 : if (gfc_extract_int (sym->value, kind))
71 : : return MATCH_NO;
72 : :
73 : 92279 : gfc_set_sym_referenced (sym);
74 : :
75 : 92279 : if (*kind < 0)
76 : : return MATCH_NO;
77 : :
78 : : return MATCH_YES;
79 : : }
80 : :
81 : :
82 : : /* Get a trailing kind-specification for non-character variables.
83 : : Returns:
84 : : * the integer kind value or
85 : : * -1 if an error was generated,
86 : : * -2 if no kind was found.
87 : : The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
88 : : symbol like e.g. 'c_int'. */
89 : :
90 : : static int
91 : 4456988 : get_kind (int *is_iso_c)
92 : : {
93 : 4456988 : int kind;
94 : 4456988 : match m;
95 : :
96 : 4456988 : *is_iso_c = 0;
97 : :
98 : 4456988 : if (gfc_match_char ('_', false) != MATCH_YES)
99 : : return -2;
100 : :
101 : 469325 : m = match_kind_param (&kind, is_iso_c);
102 : 469325 : if (m == MATCH_NO)
103 : 1734 : gfc_error ("Missing kind-parameter at %C");
104 : :
105 : 469325 : 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 : 30075310 : gfc_check_digit (char c, int radix)
114 : : {
115 : 30075310 : bool r;
116 : :
117 : 30075310 : 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 : 29976169 : case 10:
128 : 29976169 : r = ('0' <= c && c <= '9');
129 : 29976169 : 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 : 30075310 : 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 : 17530379 : match_digits (int signflag, int radix, char *buffer)
150 : : {
151 : 17530379 : locus old_loc;
152 : 17530379 : int length;
153 : 17530379 : char c;
154 : :
155 : 17530379 : length = 0;
156 : 17530379 : c = gfc_next_ascii_char ();
157 : :
158 : 17530379 : 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 : 17530379 : if (!gfc_check_digit (c, radix))
168 : : return -1;
169 : :
170 : 8401980 : length++;
171 : 8401980 : if (buffer != NULL)
172 : 4192833 : *buffer++ = c;
173 : :
174 : 16637418 : for (;;)
175 : : {
176 : 12519699 : old_loc = gfc_current_locus;
177 : 12519699 : c = gfc_next_ascii_char ();
178 : :
179 : 12519699 : if (!gfc_check_digit (c, radix))
180 : : break;
181 : :
182 : 4117719 : if (buffer != NULL)
183 : 2056588 : *buffer++ = c;
184 : 4117719 : length++;
185 : : }
186 : :
187 : 8401980 : gfc_current_locus = old_loc;
188 : :
189 : 8401980 : return length;
190 : : }
191 : :
192 : : /* Convert an integer string to an expression node. */
193 : :
194 : : static gfc_expr *
195 : 4085618 : convert_integer (const char *buffer, int kind, int radix, locus *where)
196 : : {
197 : 4085618 : gfc_expr *e;
198 : 4085618 : const char *t;
199 : :
200 : 4085618 : e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 : : /* A leading plus is allowed, but not by mpz_set_str. */
202 : 4085618 : if (buffer[0] == '+')
203 : 21 : t = buffer + 1;
204 : : else
205 : : t = buffer;
206 : 4085618 : mpz_set_str (e->value.integer, t, radix);
207 : :
208 : 4085618 : return e;
209 : : }
210 : :
211 : :
212 : : /* Convert an unsigned string to an expression node. XXX:
213 : : This needs a calculation modulo 2^n. TODO: Implement restriction
214 : : that no unary minus is permitted. */
215 : : static gfc_expr *
216 : 101372 : convert_unsigned (const char *buffer, int kind, int radix, locus *where)
217 : : {
218 : 101372 : gfc_expr *e;
219 : 101372 : const char *t;
220 : 101372 : int k;
221 : 101372 : arith rc;
222 : :
223 : 101372 : e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
224 : : /* A leading plus is allowed, but not by mpz_set_str. */
225 : 101372 : if (buffer[0] == '+')
226 : 0 : t = buffer + 1;
227 : : else
228 : : t = buffer;
229 : :
230 : 101372 : mpz_set_str (e->value.integer, t, radix);
231 : :
232 : 101372 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
233 : :
234 : : /* TODO Maybe move this somewhere else. */
235 : 101372 : rc = gfc_range_check (e);
236 : 101372 : if (rc != ARITH_OK)
237 : : {
238 : 2 : if (pedantic)
239 : 1 : gfc_error_now (gfc_arith_error (rc), &e->where);
240 : : else
241 : 1 : gfc_warning (0, gfc_arith_error (rc), &e->where);
242 : : }
243 : :
244 : 101372 : gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
245 : : false);
246 : :
247 : 101372 : return e;
248 : : }
249 : :
250 : : /* Convert a real string to an expression node. */
251 : :
252 : : static gfc_expr *
253 : 212081 : convert_real (const char *buffer, int kind, locus *where)
254 : : {
255 : 212081 : gfc_expr *e;
256 : :
257 : 212081 : e = gfc_get_constant_expr (BT_REAL, kind, where);
258 : 212081 : mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259 : :
260 : 212081 : 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 : 6480 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 : : {
270 : 6480 : gfc_expr *e;
271 : :
272 : 6480 : e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 : 6480 : mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 : : GFC_MPC_RND_MODE);
275 : :
276 : 6480 : 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 : 12744440 : match_integer_constant (gfc_expr **result, int signflag)
285 : : {
286 : 12744440 : int length, kind, is_iso_c;
287 : 12744440 : locus old_loc;
288 : 12744440 : char *buffer;
289 : 12744440 : gfc_expr *e;
290 : :
291 : 12744440 : old_loc = gfc_current_locus;
292 : 12744440 : gfc_gobble_whitespace ();
293 : :
294 : 12744440 : length = match_digits (signflag, 10, NULL);
295 : 12744440 : gfc_current_locus = old_loc;
296 : 12744440 : if (length == -1)
297 : : return MATCH_NO;
298 : :
299 : 4087352 : buffer = (char *) alloca (length + 1);
300 : 4087352 : memset (buffer, '\0', length + 1);
301 : :
302 : 4087352 : gfc_gobble_whitespace ();
303 : :
304 : 4087352 : match_digits (signflag, 10, buffer);
305 : :
306 : 4087352 : kind = get_kind (&is_iso_c);
307 : 4087352 : if (kind == -2)
308 : 3781849 : kind = gfc_default_integer_kind;
309 : 4087352 : if (kind == -1)
310 : : return MATCH_ERROR;
311 : :
312 : 4085622 : if (kind == 4 && flag_integer4_kind == 8)
313 : 0 : kind = 8;
314 : :
315 : 4085622 : 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 : 4085618 : e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 : 4085618 : e->ts.is_c_interop = is_iso_c;
323 : :
324 : 4085618 : 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 : 4076032 : *result = e;
334 : 4076032 : return MATCH_YES;
335 : : }
336 : :
337 : : /* Match an unsigned constant (an integer with suffix u). No sign
338 : : is currently accepted, in accordance with 24-116.txt, but that
339 : : could be changed later. This is very much like the integer
340 : : constant matching above, but with enough differences to put it into
341 : : its own function. */
342 : :
343 : : static match
344 : 588996 : match_unsigned_constant (gfc_expr **result)
345 : : {
346 : 588996 : int length, kind, is_iso_c;
347 : 588996 : locus old_loc;
348 : 588996 : char *buffer;
349 : 588996 : gfc_expr *e;
350 : 588996 : match m;
351 : :
352 : 588996 : old_loc = gfc_current_locus;
353 : 588996 : gfc_gobble_whitespace ();
354 : :
355 : 588996 : length = match_digits (/* signflag = */ false, 10, NULL);
356 : :
357 : 588996 : if (length == -1)
358 : 471311 : goto fail;
359 : :
360 : 117685 : m = gfc_match_char ('u');
361 : 117685 : if (m == MATCH_NO)
362 : 16313 : goto fail;
363 : :
364 : 101372 : gfc_current_locus = old_loc;
365 : :
366 : 101372 : buffer = (char *) alloca (length + 1);
367 : 101372 : memset (buffer, '\0', length + 1);
368 : :
369 : 101372 : gfc_gobble_whitespace ();
370 : :
371 : 101372 : match_digits (false, 10, buffer);
372 : :
373 : 101372 : m = gfc_match_char ('u');
374 : 101372 : if (m == MATCH_NO)
375 : 0 : goto fail;
376 : :
377 : 101372 : kind = get_kind (&is_iso_c);
378 : 101372 : if (kind == -2)
379 : 8357 : kind = gfc_default_unsigned_kind;
380 : 101372 : if (kind == -1)
381 : : return MATCH_ERROR;
382 : :
383 : 101372 : if (kind == 4 && flag_integer4_kind == 8)
384 : 0 : kind = 8;
385 : :
386 : 101372 : if (gfc_validate_kind (BT_UNSIGNED, kind, true) < 0)
387 : : {
388 : 0 : gfc_error ("Unsigned kind %d at %C not available", kind);
389 : 0 : return MATCH_ERROR;
390 : : }
391 : :
392 : 101372 : e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
393 : 101372 : e->ts.is_c_interop = is_iso_c;
394 : :
395 : 101372 : *result = e;
396 : 101372 : return MATCH_YES;
397 : :
398 : 487624 : fail:
399 : 487624 : gfc_current_locus = old_loc;
400 : 487624 : return MATCH_NO;
401 : : }
402 : :
403 : : /* Match a Hollerith constant. */
404 : :
405 : : static match
406 : 6378775 : match_hollerith_constant (gfc_expr **result)
407 : : {
408 : 6378775 : locus old_loc;
409 : 6378775 : gfc_expr *e = NULL;
410 : 6378775 : int num, pad;
411 : 6378775 : int i;
412 : :
413 : 6378775 : old_loc = gfc_current_locus;
414 : 6378775 : gfc_gobble_whitespace ();
415 : :
416 : 6378775 : if (match_integer_constant (&e, 0) == MATCH_YES
417 : 6378775 : && 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 : 6376136 : gfc_free_expr (e);
474 : 6376136 : gfc_current_locus = old_loc;
475 : 6376136 : 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 : 6581832 : match_boz_constant (gfc_expr **result)
491 : : {
492 : 6581832 : int radix, length, x_hex;
493 : 6581832 : locus old_loc, start_loc;
494 : 6581832 : char *buffer, post, delim;
495 : 6581832 : gfc_expr *e;
496 : :
497 : 6581832 : start_loc = old_loc = gfc_current_locus;
498 : 6581832 : gfc_gobble_whitespace ();
499 : :
500 : 6581832 : x_hex = 0;
501 : 6581832 : switch (post = gfc_next_ascii_char ())
502 : : {
503 : : case 'b':
504 : : radix = 2;
505 : : post = 0;
506 : : break;
507 : 53338 : case 'o':
508 : 53338 : radix = 8;
509 : 53338 : post = 0;
510 : 53338 : break;
511 : 88827 : case 'x':
512 : 88827 : 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 : 6303869 : default:
526 : 6303869 : goto backup;
527 : : }
528 : :
529 : : /* No whitespace allowed here. */
530 : :
531 : 53338 : if (post == 0)
532 : 277938 : delim = gfc_next_ascii_char ();
533 : :
534 : 277963 : if (delim != '\'' && delim != '\"')
535 : 273851 : 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 : 6577720 : backup:
609 : 6577720 : gfc_current_locus = start_loc;
610 : 6577720 : 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 : 6681771 : match_real_constant (gfc_expr **result, int signflag)
619 : : {
620 : 6681771 : int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 : 6681771 : locus old_loc, temp_loc;
622 : 6681771 : char *p, *buffer, c, exp_char;
623 : 6681771 : gfc_expr *e;
624 : 6681771 : bool negate;
625 : :
626 : 6681771 : old_loc = gfc_current_locus;
627 : 6681771 : gfc_gobble_whitespace ();
628 : :
629 : 6681771 : e = NULL;
630 : :
631 : 6681771 : default_exponent = 0;
632 : 6681771 : count = 0;
633 : 6681771 : seen_dp = 0;
634 : 6681771 : seen_digits = 0;
635 : 6681771 : exp_char = ' ';
636 : 6681771 : negate = false;
637 : :
638 : 6681771 : c = gfc_next_ascii_char ();
639 : 6681771 : if (signflag && (c == '+' || c == '-'))
640 : : {
641 : 5825 : if (c == '-')
642 : 5689 : negate = true;
643 : :
644 : 5825 : gfc_gobble_whitespace ();
645 : 5825 : c = gfc_next_ascii_char ();
646 : : }
647 : :
648 : : /* Scan significand. */
649 : 3830316 : for (;; c = gfc_next_ascii_char (), count++)
650 : : {
651 : 10512087 : if (c == '.')
652 : : {
653 : 272469 : if (seen_dp)
654 : 204 : goto done;
655 : :
656 : : /* Check to see if "." goes with a following operator like
657 : : ".eq.". */
658 : 272265 : temp_loc = gfc_current_locus;
659 : 272265 : c = gfc_next_ascii_char ();
660 : :
661 : 272265 : 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 : 272265 : if (ISALPHA (c))
669 : 66443 : goto done; /* Distinguish 1.e9 from 1.eq.2 */
670 : :
671 : 205822 : gfc_current_locus = temp_loc;
672 : 205822 : seen_dp = 1;
673 : 205822 : continue;
674 : : }
675 : :
676 : 10239618 : if (ISDIGIT (c))
677 : : {
678 : 3624494 : seen_digits = 1;
679 : 3624494 : continue;
680 : : }
681 : :
682 : 6615124 : break;
683 : : }
684 : :
685 : 6615124 : if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 : 2272342 : goto done;
687 : 36928 : exp_char = c;
688 : :
689 : :
690 : 36928 : 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 : 36928 : c = gfc_next_ascii_char ();
703 : 36928 : count++;
704 : :
705 : 36928 : if (c == '+' || c == '-')
706 : : { /* optional sign */
707 : 6642 : c = gfc_next_ascii_char ();
708 : 6642 : count++;
709 : : }
710 : :
711 : 36928 : if (!ISDIGIT (c))
712 : : {
713 : : /* With -fdec, default exponent to 0 instead of complaining. */
714 : 40 : if (flag_dec)
715 : 36918 : 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 : 76579 : while (ISDIGIT (c))
724 : : {
725 : 39661 : c = gfc_next_ascii_char ();
726 : 39661 : count++;
727 : : }
728 : :
729 : 6681761 : done:
730 : : /* Check that we have a numeric constant. */
731 : 6681761 : if (!seen_digits || (!seen_dp && exp_char == ' '))
732 : : {
733 : 6469676 : gfc_current_locus = old_loc;
734 : 6469676 : return MATCH_NO;
735 : : }
736 : :
737 : : /* Convert the number. */
738 : 212085 : gfc_current_locus = old_loc;
739 : 212085 : gfc_gobble_whitespace ();
740 : :
741 : 212085 : buffer = (char *) alloca (count + default_exponent + 1);
742 : 212085 : memset (buffer, '\0', count + default_exponent + 1);
743 : :
744 : 212085 : p = buffer;
745 : 212085 : c = gfc_next_ascii_char ();
746 : 212085 : if (c == '+' || c == '-')
747 : : {
748 : 2927 : gfc_gobble_whitespace ();
749 : 2927 : c = gfc_next_ascii_char ();
750 : : }
751 : :
752 : : /* Hack for mpfr_set_str(). */
753 : 1375713 : for (;;)
754 : : {
755 : 793899 : if (c == 'd' || c == 'q')
756 : 29619 : *p = 'e';
757 : : else
758 : 764280 : *p = c;
759 : 793899 : p++;
760 : 793899 : if (--count == 0)
761 : : break;
762 : :
763 : 581814 : c = gfc_next_ascii_char ();
764 : : }
765 : 212085 : if (default_exponent)
766 : 30 : *p++ = '0';
767 : :
768 : 212085 : kind = get_kind (&is_iso_c);
769 : 212085 : if (kind == -1)
770 : 4 : goto cleanup;
771 : :
772 : 212081 : if (kind == 4)
773 : : {
774 : 20294 : if (flag_real4_kind == 8)
775 : 192 : kind = 8;
776 : 20294 : if (flag_real4_kind == 10)
777 : 192 : kind = 10;
778 : 20294 : if (flag_real4_kind == 16)
779 : 384 : kind = 16;
780 : : }
781 : 191787 : else if (kind == 8)
782 : : {
783 : 25996 : if (flag_real8_kind == 4)
784 : 192 : kind = 4;
785 : 25996 : if (flag_real8_kind == 10)
786 : 192 : kind = 10;
787 : 25996 : if (flag_real8_kind == 16)
788 : 384 : kind = 16;
789 : : }
790 : :
791 : 212081 : 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 : 182462 : default:
828 : 182462 : if (kind == -2)
829 : 112148 : kind = gfc_default_real_kind;
830 : :
831 : 182462 : 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 : 212081 : e = convert_real (buffer, kind, &gfc_current_locus);
839 : 212081 : if (negate)
840 : 2822 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 : 212081 : e->ts.is_c_interop = is_iso_c;
842 : :
843 : 212081 : 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 : 212080 : 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 : 212080 : *result = e;
915 : 212080 : 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 : 585648 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 : : {
928 : 585648 : gfc_expr *start, *end;
929 : 585648 : locus old_loc;
930 : 585648 : gfc_ref *ref;
931 : 585648 : match m;
932 : :
933 : 585648 : start = NULL;
934 : 585648 : end = NULL;
935 : :
936 : 585648 : old_loc = gfc_current_locus;
937 : :
938 : 585648 : m = gfc_match_char ('(');
939 : 585648 : if (m != MATCH_YES)
940 : : return MATCH_NO;
941 : :
942 : 15140 : if (gfc_match_char (':') != MATCH_YES)
943 : : {
944 : 14262 : if (init)
945 : 0 : m = gfc_match_init_expr (&start);
946 : : else
947 : 14262 : m = gfc_match_expr (&start);
948 : :
949 : 14262 : if (m != MATCH_YES)
950 : : {
951 : 154 : m = MATCH_NO;
952 : 154 : goto cleanup;
953 : : }
954 : :
955 : 14108 : m = gfc_match_char (':');
956 : 14108 : if (m != MATCH_YES)
957 : 454 : goto cleanup;
958 : : }
959 : :
960 : 14532 : if (gfc_match_char (')') != MATCH_YES)
961 : : {
962 : 13664 : if (init)
963 : 0 : m = gfc_match_init_expr (&end);
964 : : else
965 : 13664 : m = gfc_match_expr (&end);
966 : :
967 : 13664 : if (m == MATCH_NO)
968 : 2 : goto syntax;
969 : 13662 : if (m == MATCH_ERROR)
970 : 0 : goto cleanup;
971 : :
972 : 13662 : m = gfc_match_char (')');
973 : 13662 : if (m == MATCH_NO)
974 : 3 : goto syntax;
975 : : }
976 : :
977 : : /* Optimize away the (:) reference. */
978 : 14527 : if (start == NULL && end == NULL && !deferred)
979 : : ref = NULL;
980 : : else
981 : : {
982 : 14322 : ref = gfc_get_ref ();
983 : :
984 : 14322 : ref->type = REF_SUBSTRING;
985 : 14322 : if (start == NULL)
986 : 671 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 : 14322 : ref->u.ss.start = start;
988 : 14322 : if (end == NULL && cl)
989 : 661 : end = gfc_copy_expr (cl->length);
990 : 14322 : ref->u.ss.end = end;
991 : 14322 : ref->u.ss.length = cl;
992 : : }
993 : :
994 : 14527 : *result = ref;
995 : 14527 : 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 : 4038269 : next_string_char (gfc_char_t delimiter, int *ret)
1022 : : {
1023 : 4038269 : locus old_locus;
1024 : 4038269 : gfc_char_t c;
1025 : :
1026 : 4038269 : c = gfc_next_char_literal (INSTRING_WARN);
1027 : 4038269 : *ret = 0;
1028 : :
1029 : 4038269 : if (c == '\n')
1030 : : {
1031 : 4 : *ret = -2;
1032 : 4 : return 0;
1033 : : }
1034 : :
1035 : 4038265 : 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 : 4038265 : if (c != delimiter)
1047 : : return c;
1048 : :
1049 : 586742 : old_locus = gfc_current_locus;
1050 : 586742 : c = gfc_next_char_literal (NONSTRING);
1051 : :
1052 : 586742 : if (c == delimiter)
1053 : : return c;
1054 : 585924 : gfc_current_locus = old_locus;
1055 : :
1056 : 585924 : *ret = -1;
1057 : 585924 : 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 : 4296144 : match_charkind_name (char *name)
1075 : : {
1076 : 4296144 : locus old_loc;
1077 : 4296144 : char c, peek;
1078 : 4296144 : int len;
1079 : :
1080 : 4296144 : gfc_gobble_whitespace ();
1081 : 4296144 : c = gfc_next_ascii_char ();
1082 : 4296144 : if (!ISALPHA (c))
1083 : : return MATCH_NO;
1084 : :
1085 : 3906403 : *name++ = c;
1086 : 3906403 : len = 1;
1087 : :
1088 : 15765787 : for (;;)
1089 : : {
1090 : 15765787 : old_loc = gfc_current_locus;
1091 : 15765787 : c = gfc_next_ascii_char ();
1092 : :
1093 : 15765787 : if (c == '_')
1094 : : {
1095 : 504577 : peek = gfc_peek_ascii_char ();
1096 : :
1097 : 504577 : if (peek == '\'' || peek == '\"')
1098 : : {
1099 : 996 : gfc_current_locus = old_loc;
1100 : 996 : *name = '\0';
1101 : 996 : return MATCH_YES;
1102 : : }
1103 : : }
1104 : :
1105 : 15764791 : if (!ISALNUM (c)
1106 : 4408988 : && c != '_'
1107 : 3905407 : && (c != '$' || !flag_dollar_ok))
1108 : : break;
1109 : :
1110 : 11859384 : *name++ = c;
1111 : 11859384 : 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 : 6874788 : match_string_constant (gfc_expr **result)
1128 : : {
1129 : 6874788 : char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 : 6874788 : size_t length;
1131 : 6874788 : int kind,save_warn_ampersand, ret;
1132 : 6874788 : locus old_locus, start_locus;
1133 : 6874788 : gfc_symbol *sym;
1134 : 6874788 : gfc_expr *e;
1135 : 6874788 : match m;
1136 : 6874788 : gfc_char_t c, delimiter, *p;
1137 : :
1138 : 6874788 : old_locus = gfc_current_locus;
1139 : :
1140 : 6874788 : gfc_gobble_whitespace ();
1141 : :
1142 : 6874788 : c = gfc_next_char ();
1143 : 6874788 : if (c == '\'' || c == '"')
1144 : : {
1145 : 254908 : kind = gfc_default_character_kind;
1146 : 254908 : start_locus = gfc_current_locus;
1147 : 254908 : goto got_delim;
1148 : : }
1149 : :
1150 : 6619880 : if (gfc_wide_is_digit (c))
1151 : : {
1152 : 2323736 : kind = 0;
1153 : :
1154 : 5591930 : while (gfc_wide_is_digit (c))
1155 : : {
1156 : 3281635 : kind = kind * 10 + c - '0';
1157 : 3281635 : if (kind > 9999999)
1158 : 13441 : goto no_match;
1159 : 3268194 : c = gfc_next_char ();
1160 : : }
1161 : :
1162 : : }
1163 : : else
1164 : : {
1165 : 4296144 : gfc_current_locus = old_locus;
1166 : :
1167 : 4296144 : m = match_charkind_name (name);
1168 : 4296144 : if (m != MATCH_YES)
1169 : 4295148 : goto no_match;
1170 : :
1171 : 996 : if (gfc_find_symbol (name, NULL, 1, &sym)
1172 : 996 : || sym == NULL
1173 : 1991 : || sym->attr.flavor != FL_PARAMETER)
1174 : 1 : goto no_match;
1175 : :
1176 : 995 : kind = -1;
1177 : 995 : c = gfc_next_char ();
1178 : : }
1179 : :
1180 : 2311290 : if (c != '_')
1181 : 2125203 : goto no_match;
1182 : :
1183 : 186087 : c = gfc_next_char ();
1184 : 186087 : if (c != '\'' && c != '"')
1185 : 148014 : goto no_match;
1186 : :
1187 : 38073 : start_locus = gfc_current_locus;
1188 : :
1189 : 38073 : if (kind == -1)
1190 : : {
1191 : 995 : if (gfc_extract_int (sym->value, &kind, 1))
1192 : : return MATCH_ERROR;
1193 : 995 : gfc_set_sym_referenced (sym);
1194 : : }
1195 : :
1196 : 38073 : 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 : 38073 : 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 : 292981 : delimiter = c;
1209 : 292981 : length = 0;
1210 : :
1211 : 3745631 : for (;;)
1212 : : {
1213 : 2019306 : c = next_string_char (delimiter, &ret);
1214 : 2019306 : if (ret == -1)
1215 : : break;
1216 : 1726329 : 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 : 1726325 : 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 : 292977 : peek = gfc_peek_ascii_char ();
1229 : 292977 : if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 : 25 : goto no_match;
1231 : :
1232 : 292952 : e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233 : :
1234 : 292952 : 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 : 292952 : save_warn_ampersand = warn_ampersand;
1239 : 292952 : warn_ampersand = false;
1240 : :
1241 : 292952 : p = e->value.character.string;
1242 : 2018963 : for (size_t i = 0; i < length; i++)
1243 : : {
1244 : 1726016 : c = next_string_char (delimiter, &ret);
1245 : :
1246 : 1726016 : 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 : 1726011 : *p++ = c;
1255 : : }
1256 : :
1257 : 292947 : *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 : 292947 : warn_ampersand = save_warn_ampersand;
1259 : :
1260 : 292947 : next_string_char (delimiter, &ret);
1261 : 292947 : if (ret != -1)
1262 : 0 : gfc_internal_error ("match_string_constant(): Delimiter not found");
1263 : :
1264 : 292947 : 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 : 292947 : 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 : 292941 : *result = e;
1323 : :
1324 : 292941 : return MATCH_YES;
1325 : :
1326 : 6581832 : no_match:
1327 : 6581832 : gfc_current_locus = old_locus;
1328 : 6581832 : 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 : 4290016 : match_logical_constant_string (void)
1336 : : {
1337 : 4290016 : locus orig_loc = gfc_current_locus;
1338 : :
1339 : 4290016 : gfc_gobble_whitespace ();
1340 : 4290016 : if (gfc_next_ascii_char () == '.')
1341 : : {
1342 : 56180 : char ch = gfc_next_ascii_char ();
1343 : 56180 : if (ch == 'f')
1344 : : {
1345 : 28697 : if (gfc_next_ascii_char () == 'a'
1346 : 28697 : && gfc_next_ascii_char () == 'l'
1347 : 28697 : && gfc_next_ascii_char () == 's'
1348 : 28697 : && gfc_next_ascii_char () == 'e'
1349 : 57394 : && gfc_next_ascii_char () == '.')
1350 : : /* Matched ".false.". */
1351 : : return 0;
1352 : : }
1353 : 27483 : else if (ch == 't')
1354 : : {
1355 : 27482 : if (gfc_next_ascii_char () == 'r'
1356 : 27482 : && gfc_next_ascii_char () == 'u'
1357 : 27482 : && gfc_next_ascii_char () == 'e'
1358 : 54964 : && gfc_next_ascii_char () == '.')
1359 : : /* Matched ".true.". */
1360 : : return 1;
1361 : : }
1362 : : }
1363 : 4233837 : gfc_current_locus = orig_loc;
1364 : 4233837 : return -1;
1365 : : }
1366 : :
1367 : : /* Match a .true. or .false. */
1368 : :
1369 : : static match
1370 : 4290016 : match_logical_constant (gfc_expr **result)
1371 : : {
1372 : 4290016 : gfc_expr *e;
1373 : 4290016 : int i, kind, is_iso_c;
1374 : :
1375 : 4290016 : i = match_logical_constant_string ();
1376 : 4290016 : if (i == -1)
1377 : : return MATCH_NO;
1378 : :
1379 : 56179 : kind = get_kind (&is_iso_c);
1380 : 56179 : if (kind == -1)
1381 : : return MATCH_ERROR;
1382 : 56179 : if (kind == -2)
1383 : 55690 : kind = gfc_default_logical_kind;
1384 : :
1385 : 56179 : 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 : 56175 : e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 : 56175 : e->ts.is_c_interop = is_iso_c;
1393 : :
1394 : 56175 : *result = e;
1395 : 56175 : 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 : 135796 : match_sym_complex_part (gfc_expr **result)
1404 : : {
1405 : 135796 : char name[GFC_MAX_SYMBOL_LEN + 1];
1406 : 135796 : gfc_symbol *sym;
1407 : 135796 : gfc_expr *e;
1408 : 135796 : match m;
1409 : :
1410 : 135796 : m = gfc_match_name (name);
1411 : 135796 : if (m != MATCH_YES)
1412 : : return m;
1413 : :
1414 : 36947 : if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 : : return MATCH_NO;
1416 : :
1417 : 34313 : 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 : 32863 : char c;
1423 : 32863 : gfc_gobble_whitespace ();
1424 : 32863 : c = gfc_peek_ascii_char ();
1425 : 32863 : if (c == '=' || c == ',')
1426 : : {
1427 : : m = MATCH_NO;
1428 : : }
1429 : : else
1430 : : {
1431 : 30295 : gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 : 30295 : m = MATCH_ERROR;
1433 : : }
1434 : 32863 : return m;
1435 : : }
1436 : :
1437 : 1450 : if (!sym->value)
1438 : 2 : goto error;
1439 : :
1440 : 1448 : if (!gfc_numeric_ts (&sym->value->ts))
1441 : : {
1442 : 331 : gfc_error ("Numeric PARAMETER required in complex constant at %C");
1443 : 331 : return MATCH_ERROR;
1444 : : }
1445 : :
1446 : 1117 : if (sym->value->rank != 0)
1447 : : {
1448 : 174 : gfc_error ("Scalar PARAMETER required in complex constant at %C");
1449 : 174 : return MATCH_ERROR;
1450 : : }
1451 : :
1452 : 943 : if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1453 : : "complex constant at %C"))
1454 : : return MATCH_ERROR;
1455 : :
1456 : 940 : switch (sym->value->ts.type)
1457 : : {
1458 : 68 : case BT_REAL:
1459 : 68 : e = gfc_copy_expr (sym->value);
1460 : 68 : break;
1461 : :
1462 : 1 : case BT_COMPLEX:
1463 : 1 : e = gfc_complex2real (sym->value, sym->value->ts.kind);
1464 : 1 : if (e == NULL)
1465 : 0 : goto error;
1466 : : break;
1467 : :
1468 : 869 : case BT_INTEGER:
1469 : 869 : e = gfc_int2real (sym->value, gfc_default_real_kind);
1470 : 869 : if (e == NULL)
1471 : 0 : goto error;
1472 : : break;
1473 : :
1474 : 2 : case BT_UNSIGNED:
1475 : 2 : goto error;
1476 : :
1477 : 0 : default:
1478 : 0 : gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1479 : : }
1480 : :
1481 : 938 : *result = e; /* e is a scalar, real, constant expression. */
1482 : 938 : return MATCH_YES;
1483 : :
1484 : 4 : error:
1485 : 4 : gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1486 : 4 : return MATCH_ERROR;
1487 : : }
1488 : :
1489 : :
1490 : : /* Match a real or imaginary part of a complex number. */
1491 : :
1492 : : static match
1493 : 135796 : match_complex_part (gfc_expr **result)
1494 : : {
1495 : 135796 : match m;
1496 : :
1497 : 135796 : m = match_sym_complex_part (result);
1498 : 135796 : if (m != MATCH_NO)
1499 : : return m;
1500 : :
1501 : 104051 : m = match_real_constant (result, 1);
1502 : 104051 : if (m != MATCH_NO)
1503 : : return m;
1504 : :
1505 : 90901 : return match_integer_constant (result, 1);
1506 : : }
1507 : :
1508 : :
1509 : : /* Try to match a complex constant. */
1510 : :
1511 : : static match
1512 : 6884522 : match_complex_constant (gfc_expr **result)
1513 : : {
1514 : 6884522 : gfc_expr *e, *real, *imag;
1515 : 6884522 : gfc_error_buffer old_error;
1516 : 6884522 : gfc_typespec target;
1517 : 6884522 : locus old_loc;
1518 : 6884522 : int kind;
1519 : 6884522 : match m;
1520 : :
1521 : 6884522 : old_loc = gfc_current_locus;
1522 : 6884522 : real = imag = e = NULL;
1523 : :
1524 : 6884522 : m = gfc_match_char ('(');
1525 : 6884522 : if (m != MATCH_YES)
1526 : : return m;
1527 : :
1528 : 126066 : gfc_push_error (&old_error);
1529 : :
1530 : 126066 : m = match_complex_part (&real);
1531 : 126066 : if (m == MATCH_NO)
1532 : : {
1533 : 73874 : gfc_free_error (&old_error);
1534 : 73874 : goto cleanup;
1535 : : }
1536 : :
1537 : 52192 : 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 : 42458 : gfc_clear_warning ();
1543 : 42458 : gfc_pop_error (&old_error);
1544 : 42458 : m = MATCH_NO;
1545 : 42458 : 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 : 9734 : if (m == MATCH_ERROR)
1554 : : {
1555 : 4 : gfc_free_error (&old_error);
1556 : 4 : goto cleanup;
1557 : : }
1558 : 9730 : gfc_pop_error (&old_error);
1559 : :
1560 : 9730 : m = match_complex_part (&imag);
1561 : 9730 : if (m == MATCH_NO)
1562 : 3104 : goto syntax;
1563 : 6626 : if (m == MATCH_ERROR)
1564 : 133 : goto cleanup;
1565 : :
1566 : 6493 : m = gfc_match_char (')');
1567 : 6493 : 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 : 6480 : if (m == MATCH_ERROR)
1581 : 0 : goto cleanup;
1582 : :
1583 : : /* Decide on the kind of this complex number. */
1584 : 6480 : if (real->ts.type == BT_REAL)
1585 : : {
1586 : 6065 : if (imag->ts.type == BT_REAL)
1587 : 6040 : kind = gfc_kind_max (real, imag);
1588 : : else
1589 : 25 : kind = real->ts.kind;
1590 : : }
1591 : : else
1592 : : {
1593 : 415 : if (imag->ts.type == BT_REAL)
1594 : 7 : kind = imag->ts.kind;
1595 : : else
1596 : 408 : kind = gfc_default_real_kind;
1597 : : }
1598 : 6480 : gfc_clear_ts (&target);
1599 : 6480 : target.type = BT_REAL;
1600 : 6480 : target.kind = kind;
1601 : :
1602 : 6480 : if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 : 416 : gfc_convert_type (real, &target, 2);
1604 : 6480 : if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 : 471 : gfc_convert_type (imag, &target, 2);
1606 : :
1607 : 6480 : e = convert_complex (real, imag, kind);
1608 : 6480 : e->where = gfc_current_locus;
1609 : :
1610 : 6480 : gfc_free_expr (real);
1611 : 6480 : gfc_free_expr (imag);
1612 : :
1613 : 6480 : *result = e;
1614 : 6480 : 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 : 119586 : cleanup:
1621 : 119586 : gfc_free_expr (e);
1622 : 119586 : gfc_free_expr (real);
1623 : 119586 : gfc_free_expr (imag);
1624 : 119586 : gfc_current_locus = old_loc;
1625 : :
1626 : 119586 : return m;
1627 : 6884522 : }
1628 : :
1629 : :
1630 : : /* Match constants in any of several forms. Returns nonzero for a
1631 : : match, zero for no match. */
1632 : :
1633 : : match
1634 : 6884522 : gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 : : {
1636 : 6884522 : match m;
1637 : :
1638 : 6884522 : m = match_complex_constant (result);
1639 : 6884522 : if (m != MATCH_NO)
1640 : : return m;
1641 : :
1642 : 6874788 : m = match_string_constant (result);
1643 : 6874788 : if (m != MATCH_NO)
1644 : : return m;
1645 : :
1646 : 6581832 : m = match_boz_constant (result);
1647 : 6581832 : if (m != MATCH_NO)
1648 : : return m;
1649 : :
1650 : 6577720 : m = match_real_constant (result, signflag);
1651 : 6577720 : if (m != MATCH_NO)
1652 : : return m;
1653 : :
1654 : 6378775 : m = match_hollerith_constant (result);
1655 : 6378775 : if (m != MATCH_NO)
1656 : : return m;
1657 : :
1658 : 6376136 : if (flag_unsigned)
1659 : : {
1660 : 588996 : m = match_unsigned_constant (result);
1661 : 588996 : if (m != MATCH_NO)
1662 : : return m;
1663 : : }
1664 : :
1665 : 6274764 : m = match_integer_constant (result, signflag);
1666 : 6274764 : if (m != MATCH_NO)
1667 : : return m;
1668 : :
1669 : 4290016 : m = match_logical_constant (result);
1670 : 4290016 : 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 : 759624 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 : : {
1684 : 759624 : if (!sym->attr.function || (sym->result != sym))
1685 : : return false;
1686 : 1593261 : while (ns)
1687 : : {
1688 : 902200 : if (ns->proc_name == sym)
1689 : : return true;
1690 : 890922 : 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 : 1961020 : match_actual_arg (gfc_expr **result)
1705 : : {
1706 : 1961020 : char name[GFC_MAX_SYMBOL_LEN + 1];
1707 : 1961020 : gfc_symtree *symtree;
1708 : 1961020 : locus where, w;
1709 : 1961020 : gfc_expr *e;
1710 : 1961020 : char c;
1711 : :
1712 : 1961020 : gfc_gobble_whitespace ();
1713 : 1961020 : where = gfc_current_locus;
1714 : :
1715 : 1961020 : switch (gfc_match_name (name))
1716 : : {
1717 : : case MATCH_ERROR:
1718 : : return MATCH_ERROR;
1719 : :
1720 : : case MATCH_NO:
1721 : : break;
1722 : :
1723 : 1277238 : case MATCH_YES:
1724 : 1277238 : w = gfc_current_locus;
1725 : 1277238 : gfc_gobble_whitespace ();
1726 : 1277238 : c = gfc_next_ascii_char ();
1727 : 1277238 : gfc_current_locus = w;
1728 : :
1729 : 1277238 : if (c != ',' && c != ')')
1730 : : break;
1731 : :
1732 : 672758 : 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 : 672758 : if (symtree == NULL)
1739 : : {
1740 : 13001 : gfc_get_sym_tree (name, NULL, &symtree, false);
1741 : 13001 : gfc_set_sym_referenced (symtree->n.sym);
1742 : : }
1743 : : else
1744 : : {
1745 : 659757 : gfc_symbol *sym;
1746 : :
1747 : 659757 : sym = symtree->n.sym;
1748 : 659757 : gfc_set_sym_referenced (sym);
1749 : 659757 : 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 : 658666 : if (sym->attr.flavor != FL_PROCEDURE
1756 : 623288 : && sym->attr.flavor != FL_UNKNOWN)
1757 : : break;
1758 : :
1759 : 184279 : 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 : 184055 : if (sym->attr.function && sym->result == sym)
1770 : : {
1771 : 3123 : if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 : : break;
1773 : :
1774 : 2477 : 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 : 196356 : e = gfc_get_expr (); /* Leave it unknown for now */
1791 : 196356 : e->symtree = symtree;
1792 : 196356 : e->expr_type = EXPR_VARIABLE;
1793 : 196356 : e->ts.type = BT_PROCEDURE;
1794 : 196356 : e->where = where;
1795 : :
1796 : 196356 : *result = e;
1797 : 196356 : return MATCH_YES;
1798 : : }
1799 : :
1800 : 1764664 : gfc_current_locus = where;
1801 : 1764664 : return gfc_match_expr (result);
1802 : : }
1803 : :
1804 : :
1805 : : /* Match a keyword argument or type parameter spec list.. */
1806 : :
1807 : : static match
1808 : 1952808 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 : : {
1810 : 1952808 : char name[GFC_MAX_SYMBOL_LEN + 1];
1811 : 1952808 : gfc_actual_arglist *a;
1812 : 1952808 : locus name_locus;
1813 : 1952808 : match m;
1814 : :
1815 : 1952808 : name_locus = gfc_current_locus;
1816 : 1952808 : m = gfc_match_name (name);
1817 : :
1818 : 1952808 : if (m != MATCH_YES)
1819 : 576696 : goto cleanup;
1820 : 1376112 : if (gfc_match_char ('=') != MATCH_YES)
1821 : : {
1822 : 1219742 : m = MATCH_NO;
1823 : 1219742 : goto cleanup;
1824 : : }
1825 : :
1826 : 156370 : 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 : 156338 : m = match_actual_arg (&actual->expr);
1843 : 156338 : if (m != MATCH_YES)
1844 : 10679 : goto cleanup;
1845 : :
1846 : : /* Make sure this name has not appeared yet. */
1847 : 145659 : add_name:
1848 : 145691 : if (name[0] != '\0')
1849 : : {
1850 : 468844 : for (a = base; a; a = a->next)
1851 : 323167 : if (a->name != NULL && strcmp (a->name, name) == 0)
1852 : : {
1853 : 14 : gfc_error ("Keyword %qs at %C has already appeared in the "
1854 : : "current argument list", name);
1855 : 14 : return MATCH_ERROR;
1856 : : }
1857 : : }
1858 : :
1859 : 145677 : actual->name = gfc_get_string ("%s", name);
1860 : 145677 : return MATCH_YES;
1861 : :
1862 : 1807117 : cleanup:
1863 : 1807117 : gfc_current_locus = name_locus;
1864 : 1807117 : return m;
1865 : : }
1866 : :
1867 : :
1868 : : /* Match an argument list function, such as %VAL. */
1869 : :
1870 : : static match
1871 : 1915173 : match_arg_list_function (gfc_actual_arglist *result)
1872 : : {
1873 : 1915173 : char name[GFC_MAX_SYMBOL_LEN + 1];
1874 : 1915173 : locus old_locus;
1875 : 1915173 : match m;
1876 : :
1877 : 1915173 : old_locus = gfc_current_locus;
1878 : :
1879 : 1915173 : if (gfc_match_char ('%') != MATCH_YES)
1880 : : {
1881 : 1915108 : m = MATCH_NO;
1882 : 1915108 : 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 : 1915109 : cleanup:
1939 : 1915109 : gfc_current_locus = old_locus;
1940 : 1915109 : 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 : 2009586 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 : : {
1959 : 2009586 : gfc_actual_arglist *head, *tail;
1960 : 2009586 : int seen_keyword;
1961 : 2009586 : gfc_st_label *label;
1962 : 2009586 : locus old_loc;
1963 : 2009586 : match m;
1964 : :
1965 : 2009586 : *argp = tail = NULL;
1966 : 2009586 : old_loc = gfc_current_locus;
1967 : :
1968 : 2009586 : seen_keyword = 0;
1969 : :
1970 : 2009586 : if (gfc_match_char ('(') == MATCH_NO)
1971 : 1176600 : return (sub_flag) ? MATCH_YES : MATCH_NO;
1972 : :
1973 : 1405357 : if (gfc_match_char (')') == MATCH_YES)
1974 : : return MATCH_YES;
1975 : :
1976 : 1380198 : head = NULL;
1977 : :
1978 : 1380198 : matching_actual_arglist++;
1979 : :
1980 : 1952792 : for (;;)
1981 : : {
1982 : 1952792 : if (head == NULL)
1983 : 1380198 : head = tail = gfc_get_actual_arglist ();
1984 : : else
1985 : : {
1986 : 572594 : tail->next = gfc_get_actual_arglist ();
1987 : 572594 : tail = tail->next;
1988 : : }
1989 : :
1990 : 1952792 : 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 : 1952554 : 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 : 1952204 : if (seen_keyword)
2034 : : {
2035 : 37031 : m = match_keyword_arg (tail, head, pdt);
2036 : :
2037 : 37031 : if (m == MATCH_ERROR)
2038 : 34 : goto cleanup;
2039 : 36997 : 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 : 1915173 : m = match_arg_list_function (tail);
2050 : 1915173 : if (m == MATCH_ERROR)
2051 : 1 : goto cleanup;
2052 : :
2053 : : /* See if we have the first keyword argument. */
2054 : 1915172 : if (m == MATCH_NO)
2055 : : {
2056 : 1915108 : m = match_keyword_arg (tail, head, false);
2057 : 1915108 : if (m == MATCH_YES)
2058 : : seen_keyword = 1;
2059 : 1805309 : if (m == MATCH_ERROR)
2060 : 691 : goto cleanup;
2061 : : }
2062 : :
2063 : 1914417 : if (m == MATCH_NO)
2064 : : {
2065 : : /* Try for a non-keyword argument. */
2066 : 1804618 : m = match_actual_arg (&tail->expr);
2067 : 1804618 : if (m == MATCH_ERROR)
2068 : 1812 : goto cleanup;
2069 : 1802806 : if (m == MATCH_NO)
2070 : 18483 : goto syntax;
2071 : : }
2072 : : }
2073 : :
2074 : :
2075 : 109799 : next:
2076 : 1930455 : if (gfc_match_char (')') == MATCH_YES)
2077 : : break;
2078 : 580708 : if (gfc_match_char (',') != MATCH_YES)
2079 : 8114 : goto syntax;
2080 : : }
2081 : :
2082 : 1349747 : *argp = head;
2083 : 1349747 : matching_actual_arglist--;
2084 : 1349747 : return MATCH_YES;
2085 : :
2086 : 26597 : syntax:
2087 : 26597 : gfc_error ("Syntax error in argument list at %C");
2088 : :
2089 : 30451 : cleanup:
2090 : 30451 : gfc_free_actual_arglist (head);
2091 : 30451 : gfc_current_locus = old_loc;
2092 : 30451 : matching_actual_arglist--;
2093 : 30451 : 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 : 710892 : extend_ref (gfc_expr *primary, gfc_ref *tail)
2102 : : {
2103 : 710892 : if (primary->ref == NULL)
2104 : 652804 : primary->ref = tail = gfc_get_ref ();
2105 : 58088 : else if (tail == NULL)
2106 : : {
2107 : : /* Set tail to end of reference chain. */
2108 : 23 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2109 : 23 : if (ref->next == NULL)
2110 : : {
2111 : : tail = ref;
2112 : : break;
2113 : : }
2114 : : }
2115 : : else
2116 : : {
2117 : 58074 : tail->next = gfc_get_ref ();
2118 : 58074 : tail = tail->next;
2119 : : }
2120 : :
2121 : 710892 : return tail;
2122 : : }
2123 : :
2124 : :
2125 : : /* Used by gfc_match_varspec() to match an inquiry reference. */
2126 : :
2127 : : bool
2128 : 3800 : is_inquiry_ref (const char *name, gfc_ref **ref)
2129 : : {
2130 : 3800 : inquiry_type type;
2131 : :
2132 : 3800 : if (name == NULL)
2133 : : return false;
2134 : :
2135 : 3800 : if (ref) *ref = NULL;
2136 : :
2137 : 3800 : if (strcmp (name, "re") == 0)
2138 : : type = INQUIRY_RE;
2139 : 2739 : else if (strcmp (name, "im") == 0)
2140 : : type = INQUIRY_IM;
2141 : 1857 : else if (strcmp (name, "kind") == 0)
2142 : : type = INQUIRY_KIND;
2143 : 1354 : else if (strcmp (name, "len") == 0)
2144 : : type = INQUIRY_LEN;
2145 : : else
2146 : : return false;
2147 : :
2148 : 2888 : if (ref)
2149 : : {
2150 : 1649 : *ref = gfc_get_ref ();
2151 : 1649 : (*ref)->type = REF_INQUIRY;
2152 : 1649 : (*ref)->u.i = type;
2153 : : }
2154 : :
2155 : : return true;
2156 : : }
2157 : :
2158 : :
2159 : : /* Check to see if functions in operator expressions can be resolved now. */
2160 : :
2161 : : static bool
2162 : 198 : resolvable_fcns (gfc_expr *e,
2163 : : gfc_symbol *sym ATTRIBUTE_UNUSED,
2164 : : int *f ATTRIBUTE_UNUSED)
2165 : : {
2166 : 198 : bool p;
2167 : 198 : gfc_symbol *s;
2168 : :
2169 : 198 : if (e->expr_type != EXPR_FUNCTION)
2170 : : return false;
2171 : :
2172 : 72 : s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2173 : 72 : p = s && (s->attr.use_assoc
2174 : 54 : || s->attr.host_assoc
2175 : 54 : || s->attr.if_source == IFSRC_DECL
2176 : 54 : || s->attr.proc == PROC_INTRINSIC
2177 : 24 : || gfc_is_intrinsic (s, 0, e->where));
2178 : 72 : return !p;
2179 : : }
2180 : :
2181 : :
2182 : : /* Match any additional specifications associated with the current
2183 : : variable like member references or substrings. If equiv_flag is
2184 : : set we only match stuff that is allowed inside an EQUIVALENCE
2185 : : statement. sub_flag tells whether we expect a type-bound procedure found
2186 : : to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2187 : : components, 'ppc_arg' determines whether the PPC may be called (with an
2188 : : argument list), or whether it may just be referred to as a pointer. */
2189 : :
2190 : : match
2191 : 4911123 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2192 : : bool ppc_arg)
2193 : : {
2194 : 4911123 : char name[GFC_MAX_SYMBOL_LEN + 1];
2195 : 4911123 : gfc_ref *substring, *tail, *tmp;
2196 : 4911123 : gfc_component *component = NULL;
2197 : 4911123 : gfc_component *previous = NULL;
2198 : 4911123 : gfc_symbol *sym = primary->symtree->n.sym;
2199 : 4911123 : gfc_expr *tgt_expr = NULL;
2200 : 4911123 : match m;
2201 : 4911123 : bool unknown;
2202 : 4911123 : bool inquiry;
2203 : 4911123 : bool intrinsic;
2204 : 4911123 : bool inferred_type;
2205 : 4911123 : locus old_loc;
2206 : 4911123 : char peeked_char;
2207 : :
2208 : 4911123 : tail = NULL;
2209 : :
2210 : 4911123 : gfc_gobble_whitespace ();
2211 : :
2212 : 4911123 : if (gfc_peek_ascii_char () == '[')
2213 : : {
2214 : 2849 : if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2215 : 2849 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2216 : 113 : && CLASS_DATA (sym)->attr.dimension))
2217 : : {
2218 : 0 : gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2219 : : "besides the coarray designator %<[...]%> at %C");
2220 : 0 : return MATCH_ERROR;
2221 : : }
2222 : 2849 : if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2223 : 2848 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2224 : 113 : && !CLASS_DATA (sym)->attr.codimension))
2225 : : {
2226 : 1 : gfc_error ("Coarray designator at %C but %qs is not a coarray",
2227 : : sym->name);
2228 : 1 : return MATCH_ERROR;
2229 : : }
2230 : : }
2231 : :
2232 : 4911122 : if (sym->assoc && sym->assoc->target)
2233 : 4911122 : tgt_expr = sym->assoc->target;
2234 : :
2235 : 4911122 : inferred_type = IS_INFERRED_TYPE (primary);
2236 : :
2237 : : /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
2238 : : been parsed, can generate errors with array refs.. The SELECT TYPE
2239 : : namespace is marked with 'assoc_name_inferred'. During resolution, this is
2240 : : detected and gfc_fixup_inferred_type_refs is called. */
2241 : 4910289 : if (!inferred_type
2242 : 4910289 : && sym->attr.select_type_temporary
2243 : 23196 : && sym->ns->assoc_name_inferred
2244 : 344 : && !sym->attr.select_rank_temporary)
2245 : 1177 : inferred_type = true;
2246 : :
2247 : : /* For associate names, we may not yet know whether they are arrays or not.
2248 : : If the selector expression is unambiguously an array; eg. a full array
2249 : : or an array section, then the associate name must be an array and we can
2250 : : fix it now. Otherwise, if parentheses follow and it is not a character
2251 : : type, we have to assume that it actually is one for now. The final
2252 : : decision will be made at resolution, of course. */
2253 : 4911122 : if (sym->assoc
2254 : 29021 : && gfc_peek_ascii_char () == '('
2255 : 9551 : && sym->ts.type != BT_CLASS
2256 : 4920532 : && !sym->attr.dimension)
2257 : : {
2258 : 386 : gfc_ref *ref = NULL;
2259 : :
2260 : 386 : if (!sym->assoc->dangling && tgt_expr)
2261 : : {
2262 : 326 : if (tgt_expr->expr_type == EXPR_VARIABLE)
2263 : 21 : gfc_resolve_expr (tgt_expr);
2264 : :
2265 : 326 : ref = tgt_expr->ref;
2266 : 340 : for (; ref; ref = ref->next)
2267 : 14 : if (ref->type == REF_ARRAY
2268 : 7 : && (ref->u.ar.type == AR_FULL
2269 : 7 : || ref->u.ar.type == AR_SECTION))
2270 : : break;
2271 : : }
2272 : :
2273 : 386 : if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2274 : 260 : && sym->assoc->st
2275 : 260 : && sym->assoc->st->n.sym
2276 : 260 : && sym->assoc->st->n.sym->attr.dimension == 0))
2277 : : {
2278 : 260 : sym->attr.dimension = 1;
2279 : 260 : if (sym->as == NULL
2280 : 260 : && sym->assoc->st
2281 : 260 : && sym->assoc->st->n.sym
2282 : 260 : && sym->assoc->st->n.sym->as)
2283 : 0 : sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2284 : : }
2285 : : }
2286 : 4910736 : else if (sym->ts.type == BT_CLASS
2287 : 42432 : && !(sym->assoc && sym->assoc->ar)
2288 : 42360 : && tgt_expr
2289 : 256 : && tgt_expr->expr_type == EXPR_VARIABLE
2290 : 130 : && sym->ts.u.derived != tgt_expr->ts.u.derived)
2291 : : {
2292 : 19 : gfc_resolve_expr (tgt_expr);
2293 : 19 : if (tgt_expr->rank)
2294 : 0 : sym->ts.u.derived = tgt_expr->ts.u.derived;
2295 : : }
2296 : :
2297 : 4911122 : peeked_char = gfc_peek_ascii_char ();
2298 : 1177 : if ((inferred_type && !sym->as && peeked_char == '(')
2299 : 4910901 : || (equiv_flag && peeked_char == '(') || peeked_char == '['
2300 : 4906482 : || sym->attr.codimension
2301 : 4892749 : || (sym->attr.dimension && sym->ts.type != BT_CLASS
2302 : 617955 : && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2303 : 617940 : && !(gfc_matching_procptr_assignment
2304 : 32 : && sym->attr.flavor == FL_PROCEDURE))
2305 : 9185951 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2306 : 42271 : && sym->ts.u.derived && CLASS_DATA (sym)
2307 : 42267 : && (CLASS_DATA (sym)->attr.dimension
2308 : 26154 : || CLASS_DATA (sym)->attr.codimension)))
2309 : : {
2310 : 652807 : gfc_array_spec *as;
2311 : 16468 : bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2312 : 661646 : && sym->ts.type == BT_CHARACTER;
2313 : 652807 : gfc_ref *ref, *strarr = NULL;
2314 : :
2315 : 652807 : tail = extend_ref (primary, tail);
2316 : 652807 : if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
2317 : : {
2318 : 3 : gcc_assert (sym->attr.dimension);
2319 : : /* Find array reference for substrings of character arrays. */
2320 : 3 : for (ref = primary->ref; ref && ref->next; ref = ref->next)
2321 : 3 : if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
2322 : : {
2323 : : strarr = ref;
2324 : : break;
2325 : : }
2326 : : }
2327 : : else
2328 : 652804 : tail->type = REF_ARRAY;
2329 : :
2330 : : /* In EQUIVALENCE, we don't know yet whether we are seeing
2331 : : an array, character variable or array of character
2332 : : variables. We'll leave the decision till resolve time. */
2333 : :
2334 : 652807 : if (equiv_flag)
2335 : : as = NULL;
2336 : 650806 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2337 : 16628 : as = CLASS_DATA (sym)->as;
2338 : : else
2339 : 634178 : as = sym->as;
2340 : :
2341 : 652807 : ref = strarr ? strarr : tail;
2342 : 652807 : m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
2343 : : coarray_only);
2344 : 652807 : if (m != MATCH_YES)
2345 : : return m;
2346 : :
2347 : 652732 : gfc_gobble_whitespace ();
2348 : 652732 : if (coarray_only)
2349 : : {
2350 : 1360 : primary->ts = sym->ts;
2351 : 1360 : goto check_substring;
2352 : : }
2353 : :
2354 : 651372 : if (equiv_flag && gfc_peek_ascii_char () == '(')
2355 : : {
2356 : 74 : tail = extend_ref (primary, tail);
2357 : 74 : tail->type = REF_ARRAY;
2358 : :
2359 : 74 : m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2360 : 74 : if (m != MATCH_YES)
2361 : : return m;
2362 : : }
2363 : : }
2364 : :
2365 : 4909687 : primary->ts = sym->ts;
2366 : :
2367 : 4909687 : if (equiv_flag)
2368 : : return MATCH_YES;
2369 : :
2370 : : /* With DEC extensions, member separator may be '.' or '%'. */
2371 : 4906743 : peeked_char = gfc_peek_ascii_char ();
2372 : 4906743 : m = gfc_match_member_sep (sym);
2373 : 4906743 : if (m == MATCH_ERROR)
2374 : : return MATCH_ERROR;
2375 : :
2376 : 4906742 : inquiry = false;
2377 : 4906742 : if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2378 : 122732 : && (primary->ts.type != BT_DERIVED || inferred_type))
2379 : : {
2380 : 2147 : match mm;
2381 : 2147 : old_loc = gfc_current_locus;
2382 : 2147 : mm = gfc_match_name (name);
2383 : :
2384 : : /* Check to see if this has a default complex. */
2385 : 482 : if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2386 : 2165 : && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2387 : : {
2388 : 6 : gfc_set_default_type (sym, 0, sym->ns);
2389 : 6 : primary->ts = sym->ts;
2390 : : }
2391 : :
2392 : : /* This is a usable inquiry reference, if the symbol is already known
2393 : : to have a type or no derived types with a component of this name
2394 : : can be found. If this was an inquiry reference with the same name
2395 : : as a derived component and the associate-name type is not derived
2396 : : or class, this is fixed up in 'gfc_fixup_inferred_type_refs'. */
2397 : 2147 : if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2398 : 3596 : && !(sym->ts.type == BT_UNKNOWN
2399 : 210 : && gfc_find_derived_types (sym, gfc_current_ns, name)))
2400 : : inquiry = true;
2401 : 2147 : gfc_current_locus = old_loc;
2402 : : }
2403 : :
2404 : : /* Use the default type if there is one. */
2405 : 2578970 : if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2406 : 4907218 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2407 : 0 : gfc_set_default_type (sym, 0, sym->ns);
2408 : :
2409 : : /* See if the type can be determined by resolution of the selector expression,
2410 : : if allowable now, or inferred from references. */
2411 : 4906742 : if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2412 : 2579889 : && m == MATCH_YES)
2413 : : {
2414 : 1232 : bool sym_present, resolved = false;
2415 : 1232 : gfc_symbol *tgt_sym;
2416 : :
2417 : 1232 : sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2418 : 1232 : tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
2419 : :
2420 : : /* These target expressions can be resolved at any time:
2421 : : (i) With a declared symbol or intrinsic function; or
2422 : : (ii) An operator expression,
2423 : : just as long as (iii) all the functions in the expression have been
2424 : : declared or are intrinsic. */
2425 : 1232 : if (((sym_present // (i)
2426 : 846 : && (tgt_sym->attr.use_assoc
2427 : 828 : || tgt_sym->attr.host_assoc
2428 : 828 : || tgt_sym->attr.if_source == IFSRC_DECL
2429 : 828 : || tgt_sym->attr.proc == PROC_INTRINSIC
2430 : 828 : || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2431 : 1202 : || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2432 : 48 : && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2433 : 42 : && gfc_resolve_expr (tgt_expr))
2434 : : {
2435 : 42 : sym->ts = tgt_expr->ts;
2436 : 42 : primary->ts = sym->ts;
2437 : 42 : resolved = true;
2438 : : }
2439 : :
2440 : : /* If this hasn't done the trick and the target expression is a function,
2441 : : or an unresolved operator expression, then this must be a derived type
2442 : : if 'name' matches an accessible type both in this namespace and in the
2443 : : as yet unparsed contained function. In principle, the type could have
2444 : : already been inferred to be complex and yet a derived type with a
2445 : : component name 're' or 'im' could be found. */
2446 : 42 : if (tgt_expr
2447 : 876 : && (tgt_expr->expr_type == EXPR_FUNCTION
2448 : 84 : || tgt_expr->expr_type == EXPR_ARRAY
2449 : 72 : || (!resolved && tgt_expr->expr_type == EXPR_OP))
2450 : 810 : && (sym->ts.type == BT_UNKNOWN
2451 : 388 : || (inferred_type && sym->ts.type != BT_COMPLEX))
2452 : 1928 : && gfc_find_derived_types (sym, gfc_current_ns, name, true))
2453 : : {
2454 : 546 : sym->assoc->inferred_type = 1;
2455 : : /* The first returned type is as good as any at this stage. The final
2456 : : determination is made in 'gfc_fixup_inferred_type_refs'*/
2457 : 546 : gfc_symbol **dts = &sym->assoc->derived_types;
2458 : 546 : tgt_expr->ts.type = BT_DERIVED;
2459 : 546 : tgt_expr->ts.kind = 0;
2460 : 546 : tgt_expr->ts.u.derived = *dts;
2461 : 546 : sym->ts = tgt_expr->ts;
2462 : 546 : primary->ts = sym->ts;
2463 : : /* Delete the dt list even if this process has to be done again for
2464 : : another primary expression. */
2465 : 1122 : while (*dts && (*dts)->dt_next)
2466 : : {
2467 : 576 : gfc_symbol **tmp = &(*dts)->dt_next;
2468 : 576 : *dts = NULL;
2469 : 576 : dts = tmp;
2470 : : }
2471 : : }
2472 : : /* If there is a usable inquiry reference not there are no matching
2473 : : derived types, force the inquiry reference by setting unknown the
2474 : : type of the primary expression. */
2475 : 258 : else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2476 : 734 : && !gfc_find_derived_types (sym, gfc_current_ns, name))
2477 : 48 : primary->ts.type = BT_UNKNOWN;
2478 : :
2479 : : /* An inquiry reference might determine the type, otherwise we have an
2480 : : error. */
2481 : 1232 : if (sym->ts.type == BT_UNKNOWN && !inquiry)
2482 : : {
2483 : 12 : gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2484 : 12 : return MATCH_ERROR;
2485 : : }
2486 : : }
2487 : 4905510 : else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2488 : 4688854 : && m == MATCH_YES && !inquiry)
2489 : : {
2490 : 6 : gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2491 : : peeked_char, sym->name);
2492 : 6 : return MATCH_ERROR;
2493 : : }
2494 : :
2495 : 4906724 : if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2496 : 218785 : || m != MATCH_YES)
2497 : 4765261 : goto check_substring;
2498 : :
2499 : 141463 : if (!inquiry)
2500 : 140296 : sym = sym->ts.u.derived;
2501 : : else
2502 : : sym = NULL;
2503 : :
2504 : 162714 : for (;;)
2505 : : {
2506 : 162714 : bool t;
2507 : 162714 : gfc_symtree *tbp;
2508 : 162714 : gfc_typespec *ts = &primary->ts;
2509 : :
2510 : 162714 : m = gfc_match_name (name);
2511 : 162714 : if (m == MATCH_NO)
2512 : 0 : gfc_error ("Expected structure component name at %C");
2513 : 162714 : if (m != MATCH_YES)
2514 : 131 : return MATCH_ERROR;
2515 : :
2516 : : /* For derived type components find typespec of ultimate component. */
2517 : 162714 : if (ts->type == BT_DERIVED && primary->ref)
2518 : : {
2519 : 129635 : for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2520 : : {
2521 : 73306 : if (ref->type == REF_COMPONENT && ref->u.c.component)
2522 : 20147 : ts = &ref->u.c.component->ts;
2523 : : }
2524 : : }
2525 : :
2526 : 162714 : intrinsic = false;
2527 : 162714 : if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
2528 : : {
2529 : 1641 : inquiry = is_inquiry_ref (name, &tmp);
2530 : 1641 : if (inquiry)
2531 : 1637 : sym = NULL;
2532 : :
2533 : 1641 : if (peeked_char == '%')
2534 : : {
2535 : 1641 : if (tmp)
2536 : : {
2537 : 1637 : gfc_symbol *s;
2538 : 1637 : switch (tmp->u.i)
2539 : : {
2540 : 1098 : case INQUIRY_RE:
2541 : 1098 : case INQUIRY_IM:
2542 : 1098 : if (!gfc_notify_std (GFC_STD_F2008,
2543 : : "RE or IM part_ref at %C"))
2544 : : return MATCH_ERROR;
2545 : : break;
2546 : :
2547 : 284 : case INQUIRY_KIND:
2548 : 284 : if (!gfc_notify_std (GFC_STD_F2003,
2549 : : "KIND part_ref at %C"))
2550 : : return MATCH_ERROR;
2551 : : break;
2552 : :
2553 : 255 : case INQUIRY_LEN:
2554 : 255 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2555 : : return MATCH_ERROR;
2556 : : break;
2557 : : }
2558 : :
2559 : : /* If necessary, infer the type of the primary expression
2560 : : and the associate-name using the the inquiry ref.. */
2561 : 1628 : s = primary->symtree ? primary->symtree->n.sym : NULL;
2562 : 1600 : if (s && s->assoc && s->assoc->target
2563 : 258 : && (s->ts.type == BT_UNKNOWN
2564 : 138 : || (primary->ts.type == BT_UNKNOWN
2565 : 48 : && s->assoc->inferred_type
2566 : 48 : && s->ts.type == BT_DERIVED)))
2567 : : {
2568 : 168 : if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2569 : : {
2570 : 72 : s->ts.type = BT_COMPLEX;
2571 : 72 : s->ts.kind = gfc_default_real_kind;;
2572 : 72 : s->assoc->inferred_type = 1;
2573 : 72 : primary->ts = s->ts;
2574 : : }
2575 : 96 : else if (tmp->u.i == INQUIRY_LEN)
2576 : : {
2577 : 48 : s->ts.type = BT_CHARACTER;
2578 : 48 : s->ts.kind = gfc_default_character_kind;;
2579 : 48 : s->assoc->inferred_type = 1;
2580 : 48 : primary->ts = s->ts;
2581 : : }
2582 : 48 : else if (s->ts.type == BT_UNKNOWN)
2583 : : {
2584 : : /* KIND inquiry gives no clue as to symbol type. */
2585 : 48 : primary->ref = tmp;
2586 : 48 : primary->ts.type = BT_INTEGER;
2587 : 48 : primary->ts.kind = gfc_default_integer_kind;
2588 : 48 : return MATCH_YES;
2589 : : }
2590 : : }
2591 : :
2592 : 1580 : if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2593 : 1094 : && primary->ts.type != BT_COMPLEX)
2594 : : {
2595 : 12 : gfc_error ("The RE or IM part_ref at %C must be "
2596 : : "applied to a COMPLEX expression");
2597 : 12 : return MATCH_ERROR;
2598 : : }
2599 : 1568 : else if (tmp->u.i == INQUIRY_LEN
2600 : 253 : && ts->type != BT_CHARACTER)
2601 : : {
2602 : 5 : gfc_error ("The LEN part_ref at %C must be applied "
2603 : : "to a CHARACTER expression");
2604 : 5 : return MATCH_ERROR;
2605 : : }
2606 : : }
2607 : 1567 : if (primary->ts.type != BT_UNKNOWN)
2608 : 162640 : intrinsic = true;
2609 : : }
2610 : : }
2611 : : else
2612 : : inquiry = false;
2613 : :
2614 : 162640 : if (sym && sym->f2k_derived)
2615 : 158435 : tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2616 : : else
2617 : : tbp = NULL;
2618 : :
2619 : 158435 : if (tbp)
2620 : : {
2621 : 3866 : gfc_symbol* tbp_sym;
2622 : :
2623 : 3866 : if (!t)
2624 : : return MATCH_ERROR;
2625 : :
2626 : 3864 : gcc_assert (!tail || !tail->next);
2627 : :
2628 : 3864 : if (!(primary->expr_type == EXPR_VARIABLE
2629 : : || (primary->expr_type == EXPR_STRUCTURE
2630 : 1 : && primary->symtree && primary->symtree->n.sym
2631 : 1 : && primary->symtree->n.sym->attr.flavor)))
2632 : : return MATCH_ERROR;
2633 : :
2634 : 3862 : if (tbp->n.tb->is_generic)
2635 : : tbp_sym = NULL;
2636 : : else
2637 : 3208 : tbp_sym = tbp->n.tb->u.specific->n.sym;
2638 : :
2639 : 3862 : primary->expr_type = EXPR_COMPCALL;
2640 : 3862 : primary->value.compcall.tbp = tbp->n.tb;
2641 : 3862 : primary->value.compcall.name = tbp->name;
2642 : 3862 : primary->value.compcall.ignore_pass = 0;
2643 : 3862 : primary->value.compcall.assign = 0;
2644 : 3862 : primary->value.compcall.base_object = NULL;
2645 : 3862 : gcc_assert (primary->symtree->n.sym->attr.referenced);
2646 : 3862 : if (tbp_sym)
2647 : 3208 : primary->ts = tbp_sym->ts;
2648 : : else
2649 : 654 : gfc_clear_ts (&primary->ts);
2650 : :
2651 : 3862 : m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2652 : : &primary->value.compcall.actual);
2653 : 3862 : if (m == MATCH_ERROR)
2654 : : return MATCH_ERROR;
2655 : 3862 : if (m == MATCH_NO)
2656 : : {
2657 : 162 : if (sub_flag)
2658 : 161 : primary->value.compcall.actual = NULL;
2659 : : else
2660 : : {
2661 : 1 : gfc_error ("Expected argument list at %C");
2662 : 1 : return MATCH_ERROR;
2663 : : }
2664 : : }
2665 : :
2666 : 141332 : break;
2667 : : }
2668 : :
2669 : 158774 : previous = component;
2670 : :
2671 : 158774 : if (!inquiry && !intrinsic)
2672 : 157208 : component = gfc_find_component (sym, name, false, false, &tmp);
2673 : : else
2674 : : component = NULL;
2675 : :
2676 : 158774 : if (intrinsic && !inquiry)
2677 : : {
2678 : 3 : if (previous)
2679 : 2 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2680 : : "type component %qs", name, previous->name);
2681 : : else
2682 : 1 : gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2683 : : "type component", name);
2684 : 3 : return MATCH_ERROR;
2685 : : }
2686 : 158771 : else if (component == NULL && !inquiry)
2687 : : return MATCH_ERROR;
2688 : :
2689 : : /* Extend the reference chain determined by gfc_find_component or
2690 : : is_inquiry_ref. */
2691 : 158724 : if (primary->ref == NULL)
2692 : 95248 : primary->ref = tmp;
2693 : : else
2694 : : {
2695 : : /* Find end of reference chain if inquiry reference and tail not
2696 : : set. */
2697 : 63476 : if (tail == NULL && inquiry && tmp)
2698 : 11 : tail = extend_ref (primary, tail);
2699 : :
2700 : : /* Set by the for loop below for the last component ref. */
2701 : 63476 : gcc_assert (tail != NULL);
2702 : 63476 : tail->next = tmp;
2703 : : }
2704 : :
2705 : : /* The reference chain may be longer than one hop for union
2706 : : subcomponents; find the new tail. */
2707 : 160700 : for (tail = tmp; tail->next; tail = tail->next)
2708 : : ;
2709 : :
2710 : 158724 : if (tmp && tmp->type == REF_INQUIRY)
2711 : : {
2712 : 1563 : if (!primary->where.u.lb || !primary->where.nextc)
2713 : 1379 : primary->where = gfc_current_locus;
2714 : 1563 : gfc_simplify_expr (primary, 0);
2715 : :
2716 : 1563 : if (primary->expr_type == EXPR_CONSTANT)
2717 : 354 : goto check_done;
2718 : :
2719 : 1209 : if (primary->ref == NULL)
2720 : 60 : goto check_done;
2721 : :
2722 : 1149 : switch (tmp->u.i)
2723 : : {
2724 : 938 : case INQUIRY_RE:
2725 : 938 : case INQUIRY_IM:
2726 : 938 : if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2727 : : return MATCH_ERROR;
2728 : :
2729 : 938 : if (primary->ts.type != BT_COMPLEX)
2730 : : {
2731 : 0 : gfc_error ("The RE or IM part_ref at %C must be "
2732 : : "applied to a COMPLEX expression");
2733 : 0 : return MATCH_ERROR;
2734 : : }
2735 : 938 : primary->ts.type = BT_REAL;
2736 : 938 : break;
2737 : :
2738 : 159 : case INQUIRY_LEN:
2739 : 159 : if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2740 : : return MATCH_ERROR;
2741 : :
2742 : 159 : if (primary->ts.type != BT_CHARACTER)
2743 : : {
2744 : 0 : gfc_error ("The LEN part_ref at %C must be applied "
2745 : : "to a CHARACTER expression");
2746 : 0 : return MATCH_ERROR;
2747 : : }
2748 : 159 : primary->ts.u.cl = NULL;
2749 : 159 : primary->ts.type = BT_INTEGER;
2750 : 159 : primary->ts.kind = gfc_default_integer_kind;
2751 : 159 : break;
2752 : :
2753 : 52 : case INQUIRY_KIND:
2754 : 52 : if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2755 : : return MATCH_ERROR;
2756 : :
2757 : 52 : if (primary->ts.type == BT_CLASS
2758 : 52 : || primary->ts.type == BT_DERIVED)
2759 : : {
2760 : 0 : gfc_error ("The KIND part_ref at %C must be applied "
2761 : : "to an expression of intrinsic type");
2762 : 0 : return MATCH_ERROR;
2763 : : }
2764 : 52 : primary->ts.type = BT_INTEGER;
2765 : 52 : primary->ts.kind = gfc_default_integer_kind;
2766 : 52 : break;
2767 : :
2768 : 0 : default:
2769 : 0 : gcc_unreachable ();
2770 : : }
2771 : :
2772 : 1149 : goto check_done;
2773 : : }
2774 : :
2775 : 157161 : primary->ts = component->ts;
2776 : :
2777 : 157161 : if (component->attr.proc_pointer && ppc_arg)
2778 : : {
2779 : : /* Procedure pointer component call: Look for argument list. */
2780 : 873 : m = gfc_match_actual_arglist (sub_flag,
2781 : : &primary->value.compcall.actual);
2782 : 873 : if (m == MATCH_ERROR)
2783 : : return MATCH_ERROR;
2784 : :
2785 : 873 : if (m == MATCH_NO && !gfc_matching_ptr_assignment
2786 : 263 : && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2787 : : {
2788 : 2 : gfc_error ("Procedure pointer component %qs requires an "
2789 : : "argument list at %C", component->name);
2790 : 2 : return MATCH_ERROR;
2791 : : }
2792 : :
2793 : 871 : if (m == MATCH_YES)
2794 : 609 : primary->expr_type = EXPR_PPC;
2795 : :
2796 : : break;
2797 : : }
2798 : :
2799 : 156288 : if (component->as != NULL && !component->attr.proc_pointer)
2800 : : {
2801 : 52699 : tail = extend_ref (primary, tail);
2802 : 52699 : tail->type = REF_ARRAY;
2803 : :
2804 : 105398 : m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2805 : 52699 : component->as->corank);
2806 : 52699 : if (m != MATCH_YES)
2807 : : return m;
2808 : : }
2809 : 103589 : else if (component->ts.type == BT_CLASS && component->attr.class_ok
2810 : 10807 : && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2811 : : {
2812 : 5301 : tail = extend_ref (primary, tail);
2813 : 5301 : tail->type = REF_ARRAY;
2814 : :
2815 : 10602 : m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2816 : : equiv_flag,
2817 : 5301 : CLASS_DATA (component)->as->corank);
2818 : 5301 : if (m != MATCH_YES)
2819 : : return m;
2820 : : }
2821 : :
2822 : 98288 : check_done:
2823 : : /* In principle, we could have eg. expr%re%kind so we must allow for
2824 : : this possibility. */
2825 : 157851 : if (gfc_match_char ('%') == MATCH_YES)
2826 : : {
2827 : 20881 : if (component && (component->ts.type == BT_DERIVED
2828 : 3695 : || component->ts.type == BT_CLASS))
2829 : 20410 : sym = component->ts.u.derived;
2830 : 20881 : continue;
2831 : : }
2832 : 136970 : else if (inquiry)
2833 : : break;
2834 : :
2835 : 126653 : if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2836 : 142990 : || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2837 : : break;
2838 : :
2839 : 370 : if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2840 : 370 : sym = component->ts.u.derived;
2841 : : }
2842 : :
2843 : 4907953 : check_substring:
2844 : 4907953 : unknown = false;
2845 : 4907953 : if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2846 : : {
2847 : 2578494 : if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2848 : : {
2849 : 352 : gfc_set_default_type (sym, 0, sym->ns);
2850 : 352 : primary->ts = sym->ts;
2851 : 352 : unknown = true;
2852 : : }
2853 : : }
2854 : :
2855 : 4907953 : if (primary->ts.type == BT_CHARACTER)
2856 : : {
2857 : 291328 : bool def = primary->ts.deferred == 1;
2858 : 291328 : switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2859 : : {
2860 : 13319 : case MATCH_YES:
2861 : 13319 : if (tail == NULL)
2862 : 8153 : primary->ref = substring;
2863 : : else
2864 : 5166 : tail->next = substring;
2865 : :
2866 : 13319 : if (primary->expr_type == EXPR_CONSTANT)
2867 : 755 : primary->expr_type = EXPR_SUBSTRING;
2868 : :
2869 : 13319 : if (substring)
2870 : 13139 : primary->ts.u.cl = NULL;
2871 : :
2872 : 13319 : gfc_gobble_whitespace ();
2873 : 13319 : if (gfc_peek_ascii_char () == '(')
2874 : : {
2875 : 5 : gfc_error_now ("Unexpected array/substring ref at %C");
2876 : 5 : return MATCH_ERROR;
2877 : : }
2878 : : break;
2879 : :
2880 : 278009 : case MATCH_NO:
2881 : 278009 : if (unknown)
2882 : : {
2883 : 351 : gfc_clear_ts (&primary->ts);
2884 : 351 : gfc_clear_ts (&sym->ts);
2885 : : }
2886 : : break;
2887 : :
2888 : : case MATCH_ERROR:
2889 : : return MATCH_ERROR;
2890 : : }
2891 : : }
2892 : :
2893 : : /* F08:C611. */
2894 : 4907948 : if (primary->ts.type == BT_DERIVED && primary->ref
2895 : 26465 : && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2896 : : {
2897 : 6 : gfc_error ("Nonpolymorphic reference to abstract type at %C");
2898 : 6 : return MATCH_ERROR;
2899 : : }
2900 : :
2901 : : /* F08:C727. */
2902 : 4907942 : if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2903 : : {
2904 : 3 : gfc_error ("Coindexed procedure-pointer component at %C");
2905 : 3 : return MATCH_ERROR;
2906 : : }
2907 : :
2908 : : return MATCH_YES;
2909 : : }
2910 : :
2911 : :
2912 : : /* Given an expression that is a variable, figure out what the
2913 : : ultimate variable's type and attribute is, traversing the reference
2914 : : structures if necessary.
2915 : :
2916 : : This subroutine is trickier than it looks. We start at the base
2917 : : symbol and store the attribute. Component references load a
2918 : : completely new attribute.
2919 : :
2920 : : A couple of rules come into play. Subobjects of targets are always
2921 : : targets themselves. If we see a component that goes through a
2922 : : pointer, then the expression must also be a target, since the
2923 : : pointer is associated with something (if it isn't core will soon be
2924 : : dumped). If we see a full part or section of an array, the
2925 : : expression is also an array.
2926 : :
2927 : : We can have at most one full array reference. */
2928 : :
2929 : : symbol_attribute
2930 : 4252870 : gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2931 : : {
2932 : 4252870 : int dimension, codimension, pointer, allocatable, target, optional;
2933 : 4252870 : symbol_attribute attr;
2934 : 4252870 : gfc_ref *ref;
2935 : 4252870 : gfc_symbol *sym;
2936 : 4252870 : gfc_component *comp;
2937 : 4252870 : bool has_inquiry_part;
2938 : 4252870 : bool has_substring_ref = false;
2939 : :
2940 : 4252870 : if (expr->expr_type != EXPR_VARIABLE
2941 : 26691 : && expr->expr_type != EXPR_FUNCTION
2942 : 9 : && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
2943 : 0 : gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2944 : :
2945 : 4252870 : sym = expr->symtree->n.sym;
2946 : 4252870 : attr = sym->attr;
2947 : :
2948 : 4252870 : optional = attr.optional;
2949 : 4252870 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
2950 : : {
2951 : 122070 : dimension = CLASS_DATA (sym)->attr.dimension;
2952 : 122070 : codimension = CLASS_DATA (sym)->attr.codimension;
2953 : 122070 : pointer = CLASS_DATA (sym)->attr.class_pointer;
2954 : 122070 : allocatable = CLASS_DATA (sym)->attr.allocatable;
2955 : : }
2956 : : else
2957 : : {
2958 : 4130800 : dimension = attr.dimension;
2959 : 4130800 : codimension = attr.codimension;
2960 : 4130800 : pointer = attr.pointer;
2961 : 4130800 : allocatable = attr.allocatable;
2962 : : }
2963 : :
2964 : 4252870 : target = attr.target;
2965 : 4252870 : if (pointer || attr.proc_pointer)
2966 : 173459 : target = 1;
2967 : :
2968 : : /* F2018:11.1.3.3: Other attributes of associate names
2969 : : "The associating entity does not have the ALLOCATABLE or POINTER
2970 : : attributes; it has the TARGET attribute if and only if the selector is
2971 : : a variable and has either the TARGET or POINTER attribute." */
2972 : 4252870 : if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
2973 : : {
2974 : 28843 : if (sym->assoc->target->expr_type == EXPR_VARIABLE)
2975 : : {
2976 : 26426 : symbol_attribute tgt_attr;
2977 : 26426 : tgt_attr = gfc_expr_attr (sym->assoc->target);
2978 : 33992 : target = (tgt_attr.pointer || tgt_attr.target);
2979 : : }
2980 : : else
2981 : : target = 0;
2982 : : }
2983 : :
2984 : 4252870 : if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2985 : 51153 : *ts = sym->ts;
2986 : :
2987 : : /* Catch left-overs from match_actual_arg, where an actual argument of a
2988 : : procedure is given a temporary ts.type == BT_PROCEDURE. The fixup is
2989 : : needed for structure constructors in DATA statements, where a pointer
2990 : : is associated with a data target, and the argument has not been fully
2991 : : resolved yet. Components references are dealt with further below. */
2992 : 51153 : if (ts != NULL
2993 : 1273581 : && expr->ts.type == BT_PROCEDURE
2994 : 2437 : && expr->ref == NULL
2995 : 2437 : && attr.flavor != FL_PROCEDURE
2996 : 25 : && attr.target)
2997 : 1 : *ts = sym->ts;
2998 : :
2999 : 4252870 : has_inquiry_part = false;
3000 : 5649146 : for (ref = expr->ref; ref; ref = ref->next)
3001 : 1397510 : if (ref->type == REF_SUBSTRING)
3002 : : {
3003 : : has_substring_ref = true;
3004 : : optional = false;
3005 : : }
3006 : 1383470 : else if (ref->type == REF_INQUIRY)
3007 : : {
3008 : : has_inquiry_part = true;
3009 : : optional = false;
3010 : : break;
3011 : : }
3012 : :
3013 : 5650387 : for (ref = expr->ref; ref; ref = ref->next)
3014 : 1397517 : switch (ref->type)
3015 : : {
3016 : 1078932 : case REF_ARRAY:
3017 : :
3018 : 1078932 : switch (ref->u.ar.type)
3019 : : {
3020 : : case AR_FULL:
3021 : 1397517 : dimension = 1;
3022 : : break;
3023 : :
3024 : 109577 : case AR_SECTION:
3025 : 109577 : allocatable = pointer = 0;
3026 : 109577 : dimension = 1;
3027 : 109577 : optional = false;
3028 : 109577 : break;
3029 : :
3030 : 308526 : case AR_ELEMENT:
3031 : : /* Handle coarrays. */
3032 : 308526 : if (ref->u.ar.dimen > 0)
3033 : 1397517 : allocatable = pointer = optional = false;
3034 : : break;
3035 : :
3036 : : case AR_UNKNOWN:
3037 : : /* For standard conforming code, AR_UNKNOWN should not happen.
3038 : : For nonconforming code, gfortran can end up here. Treat it
3039 : : as a no-op. */
3040 : : break;
3041 : : }
3042 : :
3043 : : break;
3044 : :
3045 : 303304 : case REF_COMPONENT:
3046 : 303304 : optional = false;
3047 : 303304 : comp = ref->u.c.component;
3048 : 303304 : attr = comp->attr;
3049 : 303304 : if (ts != NULL && !has_inquiry_part)
3050 : : {
3051 : 79664 : *ts = comp->ts;
3052 : : /* Don't set the string length if a substring reference
3053 : : follows. */
3054 : 79664 : if (ts->type == BT_CHARACTER && has_substring_ref)
3055 : 288 : ts->u.cl = NULL;
3056 : : }
3057 : :
3058 : 303304 : if (comp->ts.type == BT_CLASS)
3059 : : {
3060 : 20787 : codimension = CLASS_DATA (comp)->attr.codimension;
3061 : 20787 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3062 : 20787 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3063 : : }
3064 : : else
3065 : : {
3066 : 282517 : codimension = comp->attr.codimension;
3067 : 282517 : if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3068 : 13657 : pointer = comp->attr.class_pointer;
3069 : : else
3070 : 268860 : pointer = comp->attr.pointer;
3071 : 282517 : allocatable = comp->attr.allocatable;
3072 : : }
3073 : 303304 : if (pointer || attr.proc_pointer)
3074 : 52475 : target = 1;
3075 : :
3076 : : break;
3077 : :
3078 : 15281 : case REF_INQUIRY:
3079 : 15281 : case REF_SUBSTRING:
3080 : 15281 : allocatable = pointer = optional = false;
3081 : 15281 : break;
3082 : : }
3083 : :
3084 : 4252870 : attr.dimension = dimension;
3085 : 4252870 : attr.codimension = codimension;
3086 : 4252870 : attr.pointer = pointer;
3087 : 4252870 : attr.allocatable = allocatable;
3088 : 4252870 : attr.target = target;
3089 : 4252870 : attr.save = sym->attr.save;
3090 : 4252870 : attr.optional = optional;
3091 : :
3092 : 4252870 : return attr;
3093 : : }
3094 : :
3095 : :
3096 : : /* Return the attribute from a general expression. */
3097 : :
3098 : : symbol_attribute
3099 : 3637436 : gfc_expr_attr (gfc_expr *e)
3100 : : {
3101 : 3637436 : symbol_attribute attr;
3102 : :
3103 : 3637436 : switch (e->expr_type)
3104 : : {
3105 : 2944229 : case EXPR_VARIABLE:
3106 : 2944229 : attr = gfc_variable_attr (e, NULL);
3107 : 2944229 : break;
3108 : :
3109 : 41536 : case EXPR_FUNCTION:
3110 : 41536 : gfc_clear_attr (&attr);
3111 : :
3112 : 41536 : if (e->value.function.esym && e->value.function.esym->result)
3113 : : {
3114 : 14562 : gfc_symbol *sym = e->value.function.esym->result;
3115 : 14562 : attr = sym->attr;
3116 : 14562 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3117 : : {
3118 : 1694 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3119 : 1694 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3120 : 1694 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3121 : : }
3122 : : }
3123 : 26974 : else if (e->value.function.isym
3124 : 25968 : && e->value.function.isym->transformational
3125 : 16089 : && e->ts.type == BT_CLASS)
3126 : 294 : attr = CLASS_DATA (e)->attr;
3127 : 26680 : else if (e->symtree)
3128 : 26680 : attr = gfc_variable_attr (e, NULL);
3129 : :
3130 : : /* TODO: NULL() returns pointers. May have to take care of this
3131 : : here. */
3132 : :
3133 : : break;
3134 : :
3135 : 651671 : default:
3136 : 651671 : gfc_clear_attr (&attr);
3137 : 651671 : break;
3138 : : }
3139 : :
3140 : 3637436 : return attr;
3141 : : }
3142 : :
3143 : :
3144 : : /* Given an expression, figure out what the ultimate expression
3145 : : attribute is. This routine is similar to gfc_variable_attr with
3146 : : parts of gfc_expr_attr, but focuses more on the needs of
3147 : : coarrays. For coarrays a codimension attribute is kind of
3148 : : "infectious" being propagated once set and never cleared.
3149 : : The coarray_comp is only set, when the expression refs a coarray
3150 : : component. REFS_COMP is set when present to true only, when this EXPR
3151 : : refs a (non-_data) component. To check whether EXPR refs an allocatable
3152 : : component in a derived type coarray *refs_comp needs to be set and
3153 : : coarray_comp has to false. */
3154 : :
3155 : : static symbol_attribute
3156 : 12448 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3157 : : {
3158 : 12448 : int dimension, codimension, pointer, allocatable, target, coarray_comp;
3159 : 12448 : symbol_attribute attr;
3160 : 12448 : gfc_ref *ref;
3161 : 12448 : gfc_symbol *sym;
3162 : 12448 : gfc_component *comp;
3163 : :
3164 : 12448 : if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3165 : 0 : gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3166 : :
3167 : 12448 : sym = expr->symtree->n.sym;
3168 : 12448 : gfc_clear_attr (&attr);
3169 : :
3170 : 12448 : if (refs_comp)
3171 : 7766 : *refs_comp = false;
3172 : :
3173 : 12448 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3174 : : {
3175 : 369 : dimension = CLASS_DATA (sym)->attr.dimension;
3176 : 369 : codimension = CLASS_DATA (sym)->attr.codimension;
3177 : 369 : pointer = CLASS_DATA (sym)->attr.class_pointer;
3178 : 369 : allocatable = CLASS_DATA (sym)->attr.allocatable;
3179 : 369 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3180 : 369 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3181 : : }
3182 : : else
3183 : : {
3184 : 12079 : dimension = sym->attr.dimension;
3185 : 12079 : codimension = sym->attr.codimension;
3186 : 12079 : pointer = sym->attr.pointer;
3187 : 12079 : allocatable = sym->attr.allocatable;
3188 : 24158 : attr.alloc_comp = sym->ts.type == BT_DERIVED
3189 : 12079 : ? sym->ts.u.derived->attr.alloc_comp : 0;
3190 : 12079 : attr.pointer_comp = sym->ts.type == BT_DERIVED
3191 : 12079 : ? sym->ts.u.derived->attr.pointer_comp : 0;
3192 : : }
3193 : :
3194 : 12448 : target = coarray_comp = 0;
3195 : 12448 : if (pointer || attr.proc_pointer)
3196 : 522 : target = 1;
3197 : :
3198 : 22180 : for (ref = expr->ref; ref; ref = ref->next)
3199 : 9732 : switch (ref->type)
3200 : : {
3201 : 6348 : case REF_ARRAY:
3202 : :
3203 : 6348 : switch (ref->u.ar.type)
3204 : : {
3205 : : case AR_FULL:
3206 : : case AR_SECTION:
3207 : : dimension = 1;
3208 : 6348 : break;
3209 : :
3210 : 2843 : case AR_ELEMENT:
3211 : : /* Handle coarrays. */
3212 : 2843 : if (ref->u.ar.dimen > 0 && !in_allocate)
3213 : 6348 : allocatable = pointer = 0;
3214 : : break;
3215 : :
3216 : 0 : case AR_UNKNOWN:
3217 : : /* If any of start, end or stride is not integer, there will
3218 : : already have been an error issued. */
3219 : 0 : int errors;
3220 : 0 : gfc_get_errors (NULL, &errors);
3221 : 0 : if (errors == 0)
3222 : 0 : gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3223 : : }
3224 : :
3225 : : break;
3226 : :
3227 : 3383 : case REF_COMPONENT:
3228 : 3383 : comp = ref->u.c.component;
3229 : :
3230 : 3383 : if (comp->ts.type == BT_CLASS)
3231 : : {
3232 : : /* Set coarray_comp only, when this component introduces the
3233 : : coarray. */
3234 : 13 : coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3235 : 13 : codimension |= CLASS_DATA (comp)->attr.codimension;
3236 : 13 : pointer = CLASS_DATA (comp)->attr.class_pointer;
3237 : 13 : allocatable = CLASS_DATA (comp)->attr.allocatable;
3238 : : }
3239 : : else
3240 : : {
3241 : : /* Set coarray_comp only, when this component introduces the
3242 : : coarray. */
3243 : 3370 : coarray_comp = !codimension && comp->attr.codimension;
3244 : 3370 : codimension |= comp->attr.codimension;
3245 : 3370 : pointer = comp->attr.pointer;
3246 : 3370 : allocatable = comp->attr.allocatable;
3247 : : }
3248 : :
3249 : 3383 : if (refs_comp && strcmp (comp->name, "_data") != 0
3250 : 2054 : && (ref->next == NULL
3251 : 1563 : || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3252 : 1495 : *refs_comp = true;
3253 : :
3254 : 3383 : if (pointer || attr.proc_pointer)
3255 : 673 : target = 1;
3256 : :
3257 : : break;
3258 : :
3259 : : case REF_SUBSTRING:
3260 : : case REF_INQUIRY:
3261 : 9732 : allocatable = pointer = 0;
3262 : : break;
3263 : : }
3264 : :
3265 : 12448 : attr.dimension = dimension;
3266 : 12448 : attr.codimension = codimension;
3267 : 12448 : attr.pointer = pointer;
3268 : 12448 : attr.allocatable = allocatable;
3269 : 12448 : attr.target = target;
3270 : 12448 : attr.save = sym->attr.save;
3271 : 12448 : attr.coarray_comp = coarray_comp;
3272 : :
3273 : 12448 : return attr;
3274 : : }
3275 : :
3276 : :
3277 : : symbol_attribute
3278 : 14908 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3279 : : {
3280 : 14908 : symbol_attribute attr;
3281 : :
3282 : 14908 : switch (e->expr_type)
3283 : : {
3284 : 11287 : case EXPR_VARIABLE:
3285 : 11287 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3286 : 11287 : break;
3287 : :
3288 : 1165 : case EXPR_FUNCTION:
3289 : 1165 : gfc_clear_attr (&attr);
3290 : :
3291 : 1165 : if (e->value.function.esym && e->value.function.esym->result)
3292 : : {
3293 : 4 : gfc_symbol *sym = e->value.function.esym->result;
3294 : 4 : attr = sym->attr;
3295 : 4 : if (sym->ts.type == BT_CLASS)
3296 : : {
3297 : 0 : attr.dimension = CLASS_DATA (sym)->attr.dimension;
3298 : 0 : attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3299 : 0 : attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3300 : 0 : attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3301 : 0 : attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3302 : 0 : ->attr.pointer_comp;
3303 : : }
3304 : : }
3305 : 1161 : else if (e->symtree)
3306 : 1161 : attr = caf_variable_attr (e, in_allocate, refs_comp);
3307 : : else
3308 : 0 : gfc_clear_attr (&attr);
3309 : : break;
3310 : :
3311 : 2456 : default:
3312 : 2456 : gfc_clear_attr (&attr);
3313 : 2456 : break;
3314 : : }
3315 : :
3316 : 14908 : return attr;
3317 : : }
3318 : :
3319 : :
3320 : : /* Match a structure constructor. The initial symbol has already been
3321 : : seen. */
3322 : :
3323 : : typedef struct gfc_structure_ctor_component
3324 : : {
3325 : : char* name;
3326 : : gfc_expr* val;
3327 : : locus where;
3328 : : struct gfc_structure_ctor_component* next;
3329 : : }
3330 : : gfc_structure_ctor_component;
3331 : :
3332 : : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3333 : :
3334 : : static void
3335 : 9591 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3336 : : {
3337 : 9591 : free (comp->name);
3338 : 9591 : gfc_free_expr (comp->val);
3339 : 9591 : free (comp);
3340 : 9591 : }
3341 : :
3342 : :
3343 : : /* Translate the component list into the actual constructor by sorting it in
3344 : : the order required; this also checks along the way that each and every
3345 : : component actually has an initializer and handles default initializers
3346 : : for components without explicit value given. */
3347 : : static bool
3348 : 6705 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
3349 : : gfc_constructor_base *ctor_head, gfc_symbol *sym)
3350 : : {
3351 : 6705 : gfc_structure_ctor_component *comp_iter;
3352 : 6705 : gfc_component *comp;
3353 : :
3354 : 17650 : for (comp = sym->components; comp; comp = comp->next)
3355 : : {
3356 : 10950 : gfc_structure_ctor_component **next_ptr;
3357 : 10950 : gfc_expr *value = NULL;
3358 : :
3359 : : /* Try to find the initializer for the current component by name. */
3360 : 10950 : next_ptr = comp_head;
3361 : 12084 : for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3362 : : {
3363 : 10701 : if (!strcmp (comp_iter->name, comp->name))
3364 : : break;
3365 : 1134 : next_ptr = &comp_iter->next;
3366 : : }
3367 : :
3368 : : /* If an extension, try building the parent derived type by building
3369 : : a value expression for the parent derived type and calling self. */
3370 : 10950 : if (!comp_iter && comp == sym->components && sym->attr.extension)
3371 : : {
3372 : 106 : value = gfc_get_structure_constructor_expr (comp->ts.type,
3373 : : comp->ts.kind,
3374 : : &gfc_current_locus);
3375 : 106 : value->ts = comp->ts;
3376 : :
3377 : 106 : if (!build_actual_constructor (comp_head,
3378 : : &value->value.constructor,
3379 : 106 : comp->ts.u.derived))
3380 : : {
3381 : 0 : gfc_free_expr (value);
3382 : 0 : return false;
3383 : : }
3384 : :
3385 : 106 : gfc_constructor_append_expr (ctor_head, value, NULL);
3386 : 106 : continue;
3387 : : }
3388 : :
3389 : : /* If it was not found, apply NULL expression to set the component as
3390 : : unallocated. Then try the default initializer if there's any;
3391 : : otherwise, it's an error unless this is a deferred parameter. */
3392 : 1277 : if (!comp_iter)
3393 : : {
3394 : : /* F2018 7.5.10: If an allocatable component has no corresponding
3395 : : component-data-source, then that component has an allocation
3396 : : status of unallocated.... */
3397 : 1277 : if (comp->attr.allocatable
3398 : 1144 : || (comp->ts.type == BT_CLASS
3399 : 15 : && CLASS_DATA (comp)->attr.allocatable))
3400 : : {
3401 : 142 : if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3402 : : "allocatable component %qs given in the "
3403 : : "structure constructor at %C", comp->name))
3404 : : return false;
3405 : 142 : value = gfc_get_null_expr (&gfc_current_locus);
3406 : : }
3407 : : /* ....(Preceding sentence) If a component with default
3408 : : initialization has no corresponding component-data-source, then
3409 : : the default initialization is applied to that component. */
3410 : 1135 : else if (comp->initializer)
3411 : : {
3412 : 634 : if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3413 : : "with missing optional arguments at %C"))
3414 : : return false;
3415 : 632 : value = gfc_copy_expr (comp->initializer);
3416 : : }
3417 : : /* Do not trap components such as the string length for deferred
3418 : : length character components. */
3419 : 501 : else if (!comp->attr.artificial)
3420 : : {
3421 : 3 : gfc_error ("No initializer for component %qs given in the"
3422 : : " structure constructor at %C", comp->name);
3423 : 3 : return false;
3424 : : }
3425 : : }
3426 : : else
3427 : 9567 : value = comp_iter->val;
3428 : :
3429 : : /* Add the value to the constructor chain built. */
3430 : 10839 : gfc_constructor_append_expr (ctor_head, value, NULL);
3431 : :
3432 : : /* Remove the entry from the component list. We don't want the expression
3433 : : value to be free'd, so set it to NULL. */
3434 : 10839 : if (comp_iter)
3435 : : {
3436 : 9567 : *next_ptr = comp_iter->next;
3437 : 9567 : comp_iter->val = NULL;
3438 : 9567 : gfc_free_structure_ctor_component (comp_iter);
3439 : : }
3440 : : }
3441 : : return true;
3442 : : }
3443 : :
3444 : :
3445 : : bool
3446 : 6614 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3447 : : gfc_actual_arglist **arglist,
3448 : : bool parent)
3449 : : {
3450 : 6614 : gfc_actual_arglist *actual;
3451 : 6614 : gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3452 : 6614 : gfc_constructor_base ctor_head = NULL;
3453 : 6614 : gfc_component *comp; /* Is set NULL when named component is first seen */
3454 : 6614 : const char* last_name = NULL;
3455 : 6614 : locus old_locus;
3456 : 6614 : gfc_expr *expr;
3457 : :
3458 : 6614 : expr = parent ? *cexpr : e;
3459 : 6614 : old_locus = gfc_current_locus;
3460 : 6614 : if (parent)
3461 : : ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3462 : : else
3463 : 5912 : gfc_current_locus = expr->where;
3464 : :
3465 : 6614 : comp_tail = comp_head = NULL;
3466 : :
3467 : 6614 : if (!parent && sym->attr.abstract)
3468 : : {
3469 : 1 : gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3470 : : sym->name, &expr->where);
3471 : 1 : goto cleanup;
3472 : : }
3473 : :
3474 : 6613 : comp = sym->components;
3475 : 6613 : actual = parent ? *arglist : expr->value.function.actual;
3476 : 15615 : for ( ; actual; )
3477 : : {
3478 : 9591 : gfc_component *this_comp = NULL;
3479 : :
3480 : 9591 : if (!comp_head)
3481 : 6200 : comp_tail = comp_head = gfc_get_structure_ctor_component ();
3482 : : else
3483 : : {
3484 : 3391 : comp_tail->next = gfc_get_structure_ctor_component ();
3485 : 3391 : comp_tail = comp_tail->next;
3486 : : }
3487 : 9591 : if (actual->name)
3488 : : {
3489 : 840 : if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3490 : : " constructor with named arguments at %C"))
3491 : 1 : goto cleanup;
3492 : :
3493 : 839 : comp_tail->name = xstrdup (actual->name);
3494 : 839 : last_name = comp_tail->name;
3495 : 839 : comp = NULL;
3496 : : }
3497 : : else
3498 : : {
3499 : : /* Components without name are not allowed after the first named
3500 : : component initializer! */
3501 : 8751 : if (!comp || comp->attr.artificial)
3502 : : {
3503 : 2 : if (last_name)
3504 : 0 : gfc_error ("Component initializer without name after component"
3505 : : " named %s at %L", last_name,
3506 : 0 : actual->expr ? &actual->expr->where
3507 : : : &gfc_current_locus);
3508 : : else
3509 : 2 : gfc_error ("Too many components in structure constructor at "
3510 : 2 : "%L", actual->expr ? &actual->expr->where
3511 : : : &gfc_current_locus);
3512 : 2 : goto cleanup;
3513 : : }
3514 : :
3515 : 8749 : comp_tail->name = xstrdup (comp->name);
3516 : : }
3517 : :
3518 : : /* Find the current component in the structure definition and check
3519 : : its access is not private. */
3520 : 9588 : if (comp)
3521 : 8749 : this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3522 : : else
3523 : : {
3524 : 839 : this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3525 : : false, false, NULL);
3526 : 839 : comp = NULL; /* Reset needed! */
3527 : : }
3528 : :
3529 : : /* Here we can check if a component name is given which does not
3530 : : correspond to any component of the defined structure. */
3531 : 9588 : if (!this_comp)
3532 : 8 : goto cleanup;
3533 : :
3534 : : /* For a constant string constructor, make sure the length is
3535 : : correct; truncate or fill with blanks if needed. */
3536 : 9580 : if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3537 : 983 : && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3538 : 981 : && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3539 : 969 : && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3540 : 968 : && actual->expr->ts.type == BT_CHARACTER
3541 : 956 : && actual->expr->expr_type == EXPR_CONSTANT)
3542 : : {
3543 : 733 : ptrdiff_t c, e1;
3544 : 733 : c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3545 : 733 : e1 = actual->expr->value.character.length;
3546 : :
3547 : 733 : if (c != e1)
3548 : : {
3549 : 249 : ptrdiff_t i, to;
3550 : 249 : gfc_char_t *dest;
3551 : 249 : dest = gfc_get_wide_string (c + 1);
3552 : :
3553 : 249 : to = e1 < c ? e1 : c;
3554 : 4482 : for (i = 0; i < to; i++)
3555 : 4233 : dest[i] = actual->expr->value.character.string[i];
3556 : :
3557 : 5812 : for (i = e1; i < c; i++)
3558 : 5563 : dest[i] = ' ';
3559 : :
3560 : 249 : dest[c] = '\0';
3561 : 249 : free (actual->expr->value.character.string);
3562 : :
3563 : 249 : actual->expr->value.character.length = c;
3564 : 249 : actual->expr->value.character.string = dest;
3565 : :
3566 : 249 : if (warn_line_truncation && c < e1)
3567 : 14 : gfc_warning_now (OPT_Wcharacter_truncation,
3568 : : "CHARACTER expression will be truncated "
3569 : : "in constructor (%td/%td) at %L", c,
3570 : : e1, &actual->expr->where);
3571 : : }
3572 : : }
3573 : :
3574 : 9580 : comp_tail->val = actual->expr;
3575 : 9580 : if (actual->expr != NULL)
3576 : 9580 : comp_tail->where = actual->expr->where;
3577 : 9580 : actual->expr = NULL;
3578 : :
3579 : : /* Check if this component is already given a value. */
3580 : 15360 : for (comp_iter = comp_head; comp_iter != comp_tail;
3581 : 5780 : comp_iter = comp_iter->next)
3582 : : {
3583 : 5781 : gcc_assert (comp_iter);
3584 : 5781 : if (!strcmp (comp_iter->name, comp_tail->name))
3585 : : {
3586 : 1 : gfc_error ("Component %qs is initialized twice in the structure"
3587 : : " constructor at %L", comp_tail->name,
3588 : : comp_tail->val ? &comp_tail->where
3589 : : : &gfc_current_locus);
3590 : 1 : goto cleanup;
3591 : : }
3592 : : }
3593 : :
3594 : : /* F2008, R457/C725, for PURE C1283. */
3595 : 72 : if (this_comp->attr.pointer && comp_tail->val
3596 : 9651 : && gfc_is_coindexed (comp_tail->val))
3597 : : {
3598 : 2 : gfc_error ("Coindexed expression to pointer component %qs in "
3599 : : "structure constructor at %L", comp_tail->name,
3600 : : &comp_tail->where);
3601 : 2 : goto cleanup;
3602 : : }
3603 : :
3604 : : /* If not explicitly a parent constructor, gather up the components
3605 : : and build one. */
3606 : 9577 : if (comp && comp == sym->components
3607 : 6000 : && sym->attr.extension
3608 : 750 : && comp_tail->val
3609 : 750 : && (!gfc_bt_struct (comp_tail->val->ts.type)
3610 : 78 : ||
3611 : 78 : comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3612 : : {
3613 : 702 : bool m;
3614 : 702 : gfc_actual_arglist *arg_null = NULL;
3615 : :
3616 : 702 : actual->expr = comp_tail->val;
3617 : 702 : comp_tail->val = NULL;
3618 : :
3619 : 702 : m = gfc_convert_to_structure_constructor (NULL,
3620 : : comp->ts.u.derived, &comp_tail->val,
3621 : 702 : comp->ts.u.derived->attr.zero_comp
3622 : : ? &arg_null : &actual, true);
3623 : 702 : if (!m)
3624 : 0 : goto cleanup;
3625 : :
3626 : 702 : if (comp->ts.u.derived->attr.zero_comp)
3627 : : {
3628 : 126 : comp = comp->next;
3629 : 126 : continue;
3630 : : }
3631 : : }
3632 : :
3633 : 576 : if (comp)
3634 : 8615 : comp = comp->next;
3635 : 9451 : if (parent && !comp)
3636 : : break;
3637 : :
3638 : 8876 : if (actual)
3639 : 8875 : actual = actual->next;
3640 : : }
3641 : :
3642 : 6599 : if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3643 : 5 : goto cleanup;
3644 : :
3645 : : /* No component should be left, as this should have caused an error in the
3646 : : loop constructing the component-list (name that does not correspond to any
3647 : : component in the structure definition). */
3648 : 6594 : if (comp_head && sym->attr.extension)
3649 : : {
3650 : 2 : for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3651 : : {
3652 : 1 : gfc_error ("component %qs at %L has already been set by a "
3653 : : "parent derived type constructor", comp_iter->name,
3654 : : &comp_iter->where);
3655 : : }
3656 : 1 : goto cleanup;
3657 : : }
3658 : : else
3659 : 6593 : gcc_assert (!comp_head);
3660 : :
3661 : 6593 : if (parent)
3662 : : {
3663 : 702 : expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3664 : 702 : expr->ts.u.derived = sym;
3665 : 702 : expr->value.constructor = ctor_head;
3666 : 702 : *cexpr = expr;
3667 : : }
3668 : : else
3669 : : {
3670 : 5891 : expr->ts.u.derived = sym;
3671 : 5891 : expr->ts.kind = 0;
3672 : 5891 : expr->ts.type = BT_DERIVED;
3673 : 5891 : expr->value.constructor = ctor_head;
3674 : 5891 : expr->expr_type = EXPR_STRUCTURE;
3675 : : }
3676 : :
3677 : 6593 : gfc_current_locus = old_locus;
3678 : 6593 : if (parent)
3679 : 702 : *arglist = actual;
3680 : : return true;
3681 : :
3682 : 21 : cleanup:
3683 : 21 : gfc_current_locus = old_locus;
3684 : :
3685 : 45 : for (comp_iter = comp_head; comp_iter; )
3686 : : {
3687 : 24 : gfc_structure_ctor_component *next = comp_iter->next;
3688 : 24 : gfc_free_structure_ctor_component (comp_iter);
3689 : 24 : comp_iter = next;
3690 : : }
3691 : 21 : gfc_constructor_free (ctor_head);
3692 : :
3693 : 21 : return false;
3694 : : }
3695 : :
3696 : :
3697 : : match
3698 : 60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3699 : : gfc_expr **result)
3700 : : {
3701 : 60 : match m;
3702 : 60 : gfc_expr *e;
3703 : 60 : bool t = true;
3704 : :
3705 : 60 : e = gfc_get_expr ();
3706 : 60 : e->expr_type = EXPR_FUNCTION;
3707 : 60 : e->symtree = symtree;
3708 : 60 : e->where = gfc_current_locus;
3709 : :
3710 : 60 : gcc_assert (gfc_fl_struct (sym->attr.flavor)
3711 : : && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3712 : 60 : e->value.function.esym = sym;
3713 : 60 : e->symtree->n.sym->attr.generic = 1;
3714 : :
3715 : 60 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
3716 : 60 : if (m != MATCH_YES)
3717 : : {
3718 : 0 : gfc_free_expr (e);
3719 : 0 : return m;
3720 : : }
3721 : :
3722 : 60 : if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3723 : : {
3724 : 1 : gfc_free_expr (e);
3725 : 1 : return MATCH_ERROR;
3726 : : }
3727 : :
3728 : : /* If a structure constructor is in a DATA statement, then each entity
3729 : : in the structure constructor must be a constant. Try to reduce the
3730 : : expression here. */
3731 : 59 : if (gfc_in_match_data ())
3732 : 59 : t = gfc_reduce_init_expr (e);
3733 : :
3734 : 59 : if (t)
3735 : : {
3736 : 49 : *result = e;
3737 : 49 : return MATCH_YES;
3738 : : }
3739 : : else
3740 : : {
3741 : 10 : gfc_free_expr (e);
3742 : 10 : return MATCH_ERROR;
3743 : : }
3744 : : }
3745 : :
3746 : :
3747 : : /* If the symbol is an implicit do loop index and implicitly typed,
3748 : : it should not be host associated. Provide a symtree from the
3749 : : current namespace. */
3750 : : static match
3751 : 6650012 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3752 : : {
3753 : 6650012 : if ((*sym)->attr.flavor == FL_VARIABLE
3754 : 1943764 : && (*sym)->ns != gfc_current_ns
3755 : 57329 : && (*sym)->attr.implied_index
3756 : 569 : && (*sym)->attr.implicit_type
3757 : 32 : && !(*sym)->attr.use_assoc)
3758 : : {
3759 : 32 : int i;
3760 : 32 : i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3761 : 32 : if (i)
3762 : : return MATCH_ERROR;
3763 : 32 : *sym = (*st)->n.sym;
3764 : : }
3765 : : return MATCH_YES;
3766 : : }
3767 : :
3768 : :
3769 : : /* Procedure pointer as function result: Replace the function symbol by the
3770 : : auto-generated hidden result variable named "ppr@". */
3771 : :
3772 : : static bool
3773 : 4980097 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3774 : : {
3775 : : /* Check for procedure pointer result variable. */
3776 : 4980097 : if ((*sym)->attr.function && !(*sym)->attr.external
3777 : 1365484 : && (*sym)->result && (*sym)->result != *sym
3778 : 10365 : && (*sym)->result->attr.proc_pointer
3779 : 337 : && (*sym) == gfc_current_ns->proc_name
3780 : 285 : && (*sym) == (*sym)->result->ns->proc_name
3781 : 285 : && strcmp ("ppr@", (*sym)->result->name) == 0)
3782 : : {
3783 : : /* Automatic replacement with "hidden" result variable. */
3784 : 285 : (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3785 : 285 : *sym = (*sym)->result;
3786 : 285 : *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3787 : 285 : return true;
3788 : : }
3789 : : return false;
3790 : : }
3791 : :
3792 : :
3793 : : /* Matches a variable name followed by anything that might follow it--
3794 : : array reference, argument list of a function, etc. */
3795 : :
3796 : : match
3797 : 4108769 : gfc_match_rvalue (gfc_expr **result)
3798 : : {
3799 : 4108769 : gfc_actual_arglist *actual_arglist;
3800 : 4108769 : char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3801 : 4108769 : gfc_state_data *st;
3802 : 4108769 : gfc_symbol *sym;
3803 : 4108769 : gfc_symtree *symtree;
3804 : 4108769 : locus where, old_loc;
3805 : 4108769 : gfc_expr *e;
3806 : 4108769 : match m, m2;
3807 : 4108769 : int i;
3808 : 4108769 : gfc_typespec *ts;
3809 : 4108769 : bool implicit_char;
3810 : 4108769 : gfc_ref *ref;
3811 : :
3812 : 4108769 : m = gfc_match ("%%loc");
3813 : 4108769 : if (m == MATCH_YES)
3814 : : {
3815 : 10878 : if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3816 : : return MATCH_ERROR;
3817 : 10877 : strncpy (name, "loc", 4);
3818 : : }
3819 : :
3820 : : else
3821 : : {
3822 : 4097891 : m = gfc_match_name (name);
3823 : 4097891 : if (m != MATCH_YES)
3824 : : return m;
3825 : : }
3826 : :
3827 : : /* Check if the symbol exists. */
3828 : 3912259 : if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3829 : : return MATCH_ERROR;
3830 : :
3831 : : /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3832 : : type. For derived types we create a generic symbol which links to the
3833 : : derived type symbol; STRUCTUREs are simpler and must not conflict with
3834 : : variables. */
3835 : 3912257 : if (!symtree)
3836 : 172408 : if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3837 : : return MATCH_ERROR;
3838 : 3912257 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3839 : : {
3840 : 3912257 : if (gfc_find_state (COMP_INTERFACE)
3841 : 3912257 : && !gfc_current_ns->has_import_set)
3842 : 84817 : i = gfc_get_sym_tree (name, NULL, &symtree, false);
3843 : : else
3844 : 3827440 : i = gfc_get_ha_sym_tree (name, &symtree);
3845 : 3912257 : if (i)
3846 : : return MATCH_ERROR;
3847 : : }
3848 : :
3849 : :
3850 : 3912257 : sym = symtree->n.sym;
3851 : 3912257 : e = NULL;
3852 : 3912257 : where = gfc_current_locus;
3853 : :
3854 : 3912257 : replace_hidden_procptr_result (&sym, &symtree);
3855 : :
3856 : : /* If this is an implicit do loop index and implicitly typed,
3857 : : it should not be host associated. */
3858 : 3912257 : m = check_for_implicit_index (&symtree, &sym);
3859 : 3912257 : if (m != MATCH_YES)
3860 : : return m;
3861 : :
3862 : 3912257 : gfc_set_sym_referenced (sym);
3863 : 3912257 : sym->attr.implied_index = 0;
3864 : :
3865 : 3912257 : if (sym->attr.function && sym->result == sym)
3866 : : {
3867 : : /* See if this is a directly recursive function call. */
3868 : 686545 : gfc_gobble_whitespace ();
3869 : 686545 : if (sym->attr.recursive
3870 : 100 : && gfc_peek_ascii_char () == '('
3871 : 93 : && gfc_current_ns->proc_name == sym
3872 : 686552 : && !sym->attr.dimension)
3873 : : {
3874 : 4 : gfc_error ("%qs at %C is the name of a recursive function "
3875 : : "and so refers to the result variable. Use an "
3876 : : "explicit RESULT variable for direct recursion "
3877 : : "(12.5.2.1)", sym->name);
3878 : 4 : return MATCH_ERROR;
3879 : : }
3880 : :
3881 : 686541 : if (gfc_is_function_return_value (sym, gfc_current_ns))
3882 : 1683 : goto variable;
3883 : :
3884 : 684858 : if (sym->attr.entry
3885 : 187 : && (sym->ns == gfc_current_ns
3886 : 27 : || sym->ns == gfc_current_ns->parent))
3887 : : {
3888 : 180 : gfc_entry_list *el = NULL;
3889 : :
3890 : 180 : for (el = sym->ns->entries; el; el = el->next)
3891 : 180 : if (sym == el->sym)
3892 : 180 : goto variable;
3893 : : }
3894 : : }
3895 : :
3896 : 3910390 : if (gfc_matching_procptr_assignment)
3897 : : {
3898 : : /* It can be a procedure or a derived-type procedure or a not-yet-known
3899 : : type. */
3900 : 1311 : if (sym->attr.flavor != FL_UNKNOWN
3901 : 983 : && sym->attr.flavor != FL_PROCEDURE
3902 : : && sym->attr.flavor != FL_PARAMETER
3903 : : && sym->attr.flavor != FL_VARIABLE)
3904 : : {
3905 : 2 : gfc_error ("Symbol at %C is not appropriate for an expression");
3906 : 2 : return MATCH_ERROR;
3907 : : }
3908 : 1309 : goto procptr0;
3909 : : }
3910 : :
3911 : 3909079 : if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3912 : 698417 : goto function0;
3913 : :
3914 : 3210662 : if (sym->attr.generic)
3915 : 67679 : goto generic_function;
3916 : :
3917 : 3142983 : switch (sym->attr.flavor)
3918 : : {
3919 : 1684706 : case FL_VARIABLE:
3920 : 1684706 : variable:
3921 : 1684706 : e = gfc_get_expr ();
3922 : :
3923 : 1684706 : e->expr_type = EXPR_VARIABLE;
3924 : 1684706 : e->symtree = symtree;
3925 : :
3926 : 1684706 : m = gfc_match_varspec (e, 0, false, true);
3927 : 1684706 : break;
3928 : :
3929 : 212544 : case FL_PARAMETER:
3930 : : /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
3931 : : end up here. Unfortunately, sym->value->expr_type is set to
3932 : : EXPR_CONSTANT, and so the if () branch would be followed without
3933 : : the !sym->as check. */
3934 : 212544 : if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
3935 : 178592 : e = gfc_copy_expr (sym->value);
3936 : : else
3937 : : {
3938 : 33952 : e = gfc_get_expr ();
3939 : 33952 : e->expr_type = EXPR_VARIABLE;
3940 : : }
3941 : :
3942 : 212544 : e->symtree = symtree;
3943 : 212544 : m = gfc_match_varspec (e, 0, false, true);
3944 : :
3945 : 212544 : if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3946 : : break;
3947 : :
3948 : : /* Variable array references to derived type parameters cause
3949 : : all sorts of headaches in simplification. Treating such
3950 : : expressions as variable works just fine for all array
3951 : : references. */
3952 : 165223 : if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
3953 : : {
3954 : 2797 : for (ref = e->ref; ref; ref = ref->next)
3955 : 2611 : if (ref->type == REF_ARRAY)
3956 : : break;
3957 : :
3958 : 2566 : if (ref == NULL || ref->u.ar.type == AR_FULL)
3959 : : break;
3960 : :
3961 : 1001 : ref = e->ref;
3962 : 1001 : e->ref = NULL;
3963 : 1001 : gfc_free_expr (e);
3964 : 1001 : e = gfc_get_expr ();
3965 : 1001 : e->expr_type = EXPR_VARIABLE;
3966 : 1001 : e->symtree = symtree;
3967 : 1001 : e->ref = ref;
3968 : : }
3969 : :
3970 : : break;
3971 : :
3972 : 0 : case FL_STRUCT:
3973 : 0 : case FL_DERIVED:
3974 : 0 : sym = gfc_use_derived (sym);
3975 : 0 : if (sym == NULL)
3976 : : m = MATCH_ERROR;
3977 : : else
3978 : 0 : goto generic_function;
3979 : : break;
3980 : :
3981 : : /* If we're here, then the name is known to be the name of a
3982 : : procedure, yet it is not sure to be the name of a function. */
3983 : 970300 : case FL_PROCEDURE:
3984 : :
3985 : : /* Procedure Pointer Assignments. */
3986 : 970300 : procptr0:
3987 : 970300 : if (gfc_matching_procptr_assignment)
3988 : : {
3989 : 1309 : gfc_gobble_whitespace ();
3990 : 1309 : if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
3991 : : /* Parse functions returning a procptr. */
3992 : 209 : goto function0;
3993 : :
3994 : 1100 : e = gfc_get_expr ();
3995 : 1100 : e->expr_type = EXPR_VARIABLE;
3996 : 1100 : e->symtree = symtree;
3997 : 1100 : m = gfc_match_varspec (e, 0, false, true);
3998 : 1032 : if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3999 : 184 : && sym->ts.type == BT_UNKNOWN
4000 : 1274 : && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4001 : : {
4002 : : m = MATCH_ERROR;
4003 : : break;
4004 : : }
4005 : : break;
4006 : : }
4007 : :
4008 : 968991 : if (sym->attr.subroutine)
4009 : : {
4010 : 57 : gfc_error ("Unexpected use of subroutine name %qs at %C",
4011 : : sym->name);
4012 : 57 : m = MATCH_ERROR;
4013 : 57 : break;
4014 : : }
4015 : :
4016 : : /* At this point, the name has to be a non-statement function.
4017 : : If the name is the same as the current function being
4018 : : compiled, then we have a variable reference (to the function
4019 : : result) if the name is non-recursive. */
4020 : :
4021 : 968934 : st = gfc_enclosing_unit (NULL);
4022 : :
4023 : 968934 : if (st != NULL
4024 : 926219 : && st->state == COMP_FUNCTION
4025 : 80889 : && st->sym == sym
4026 : 0 : && !sym->attr.recursive)
4027 : : {
4028 : 0 : e = gfc_get_expr ();
4029 : 0 : e->symtree = symtree;
4030 : 0 : e->expr_type = EXPR_VARIABLE;
4031 : :
4032 : 0 : m = gfc_match_varspec (e, 0, false, true);
4033 : 0 : break;
4034 : : }
4035 : :
4036 : : /* Match a function reference. */
4037 : 968934 : function0:
4038 : 1667560 : m = gfc_match_actual_arglist (0, &actual_arglist);
4039 : 1667560 : if (m == MATCH_NO)
4040 : : {
4041 : 571886 : if (sym->attr.proc == PROC_ST_FUNCTION)
4042 : 1 : gfc_error ("Statement function %qs requires argument list at %C",
4043 : : sym->name);
4044 : : else
4045 : 571885 : gfc_error ("Function %qs requires an argument list at %C",
4046 : : sym->name);
4047 : :
4048 : : m = MATCH_ERROR;
4049 : : break;
4050 : : }
4051 : :
4052 : 1095674 : if (m != MATCH_YES)
4053 : : {
4054 : : m = MATCH_ERROR;
4055 : : break;
4056 : : }
4057 : :
4058 : 1065363 : gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4059 : 1065363 : sym = symtree->n.sym;
4060 : :
4061 : 1065363 : replace_hidden_procptr_result (&sym, &symtree);
4062 : :
4063 : 1065363 : e = gfc_get_expr ();
4064 : 1065363 : e->symtree = symtree;
4065 : 1065363 : e->expr_type = EXPR_FUNCTION;
4066 : 1065363 : e->value.function.actual = actual_arglist;
4067 : 1065363 : e->where = gfc_current_locus;
4068 : :
4069 : 1065363 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4070 : 206 : && CLASS_DATA (sym)->as)
4071 : : {
4072 : 91 : e->rank = CLASS_DATA (sym)->as->rank;
4073 : 91 : e->corank = CLASS_DATA (sym)->as->corank;
4074 : : }
4075 : 1065272 : else if (sym->as != NULL)
4076 : : {
4077 : 1133 : e->rank = sym->as->rank;
4078 : 1133 : e->corank = sym->as->corank;
4079 : : }
4080 : :
4081 : 1065363 : if (!sym->attr.function
4082 : 1065363 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4083 : : {
4084 : : m = MATCH_ERROR;
4085 : : break;
4086 : : }
4087 : :
4088 : : /* Check here for the existence of at least one argument for the
4089 : : iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4090 : 1065363 : if (sym->attr.is_iso_c == 1
4091 : 2 : && (sym->from_intmod == INTMOD_ISO_C_BINDING
4092 : 2 : && (sym->intmod_sym_id == ISOCBINDING_LOC
4093 : 2 : || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4094 : 2 : || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4095 : 2 : || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4096 : : {
4097 : : /* make sure we were given a param */
4098 : 0 : if (actual_arglist == NULL)
4099 : : {
4100 : 0 : gfc_error ("Missing argument to %qs at %C", sym->name);
4101 : 0 : m = MATCH_ERROR;
4102 : 0 : break;
4103 : : }
4104 : : }
4105 : :
4106 : 1065363 : if (sym->result == NULL)
4107 : 374293 : sym->result = sym;
4108 : :
4109 : 1065363 : gfc_gobble_whitespace ();
4110 : : /* F08:C612. */
4111 : 1065363 : if (gfc_peek_ascii_char() == '%')
4112 : : {
4113 : 12 : gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4114 : : "function reference at %C");
4115 : 12 : m = MATCH_ERROR;
4116 : 12 : break;
4117 : : }
4118 : :
4119 : : m = MATCH_YES;
4120 : : break;
4121 : :
4122 : 277109 : case FL_UNKNOWN:
4123 : :
4124 : : /* Special case for derived type variables that get their types
4125 : : via an IMPLICIT statement. This can't wait for the
4126 : : resolution phase. */
4127 : :
4128 : 277109 : old_loc = gfc_current_locus;
4129 : 277109 : if (gfc_match_member_sep (sym) == MATCH_YES
4130 : 9555 : && sym->ts.type == BT_UNKNOWN
4131 : 277114 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4132 : 0 : gfc_set_default_type (sym, 0, sym->ns);
4133 : 277109 : gfc_current_locus = old_loc;
4134 : :
4135 : : /* If the symbol has a (co)dimension attribute, the expression is a
4136 : : variable. */
4137 : :
4138 : 277109 : if (sym->attr.dimension || sym->attr.codimension)
4139 : : {
4140 : 34558 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4141 : : {
4142 : : m = MATCH_ERROR;
4143 : : break;
4144 : : }
4145 : :
4146 : 34558 : e = gfc_get_expr ();
4147 : 34558 : e->symtree = symtree;
4148 : 34558 : e->expr_type = EXPR_VARIABLE;
4149 : 34558 : m = gfc_match_varspec (e, 0, false, true);
4150 : 34558 : break;
4151 : : }
4152 : :
4153 : 242551 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4154 : 4520 : && (CLASS_DATA (sym)->attr.dimension
4155 : 3138 : || CLASS_DATA (sym)->attr.codimension))
4156 : : {
4157 : 1469 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4158 : : {
4159 : : m = MATCH_ERROR;
4160 : : break;
4161 : : }
4162 : :
4163 : 1469 : e = gfc_get_expr ();
4164 : 1469 : e->symtree = symtree;
4165 : 1469 : e->expr_type = EXPR_VARIABLE;
4166 : 1469 : m = gfc_match_varspec (e, 0, false, true);
4167 : 1469 : break;
4168 : : }
4169 : :
4170 : : /* Name is not an array, so we peek to see if a '(' implies a
4171 : : function call or a substring reference. Otherwise the
4172 : : variable is just a scalar. */
4173 : :
4174 : 241082 : gfc_gobble_whitespace ();
4175 : 241082 : if (gfc_peek_ascii_char () != '(')
4176 : : {
4177 : : /* Assume a scalar variable */
4178 : 72855 : e = gfc_get_expr ();
4179 : 72855 : e->symtree = symtree;
4180 : 72855 : e->expr_type = EXPR_VARIABLE;
4181 : :
4182 : 72855 : if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4183 : : {
4184 : : m = MATCH_ERROR;
4185 : : break;
4186 : : }
4187 : :
4188 : : /*FIXME:??? gfc_match_varspec does set this for us: */
4189 : 72855 : e->ts = sym->ts;
4190 : 72855 : m = gfc_match_varspec (e, 0, false, true);
4191 : 72855 : break;
4192 : : }
4193 : :
4194 : : /* See if this is a function reference with a keyword argument
4195 : : as first argument. We do this because otherwise a spurious
4196 : : symbol would end up in the symbol table. */
4197 : :
4198 : 168227 : old_loc = gfc_current_locus;
4199 : 168227 : m2 = gfc_match (" ( %n =", argname);
4200 : 168227 : gfc_current_locus = old_loc;
4201 : :
4202 : 168227 : e = gfc_get_expr ();
4203 : 168227 : e->symtree = symtree;
4204 : :
4205 : 168227 : if (m2 != MATCH_YES)
4206 : : {
4207 : : /* Try to figure out whether we're dealing with a character type.
4208 : : We're peeking ahead here, because we don't want to call
4209 : : match_substring if we're dealing with an implicitly typed
4210 : : non-character variable. */
4211 : 167171 : implicit_char = false;
4212 : 167171 : if (sym->ts.type == BT_UNKNOWN)
4213 : : {
4214 : 162473 : ts = gfc_get_default_type (sym->name, NULL);
4215 : 162473 : if (ts->type == BT_CHARACTER)
4216 : : implicit_char = true;
4217 : : }
4218 : :
4219 : : /* See if this could possibly be a substring reference of a name
4220 : : that we're not sure is a variable yet. */
4221 : :
4222 : 167154 : if ((implicit_char || sym->ts.type == BT_CHARACTER)
4223 : 1373 : && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4224 : : {
4225 : :
4226 : 909 : e->expr_type = EXPR_VARIABLE;
4227 : :
4228 : 909 : if (sym->attr.flavor != FL_VARIABLE
4229 : 909 : && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4230 : : sym->name, NULL))
4231 : : {
4232 : : m = MATCH_ERROR;
4233 : : break;
4234 : : }
4235 : :
4236 : 909 : if (sym->ts.type == BT_UNKNOWN
4237 : 909 : && !gfc_set_default_type (sym, 1, NULL))
4238 : : {
4239 : : m = MATCH_ERROR;
4240 : : break;
4241 : : }
4242 : :
4243 : 909 : e->ts = sym->ts;
4244 : 909 : if (e->ref)
4245 : 884 : e->ts.u.cl = NULL;
4246 : : m = MATCH_YES;
4247 : : break;
4248 : : }
4249 : : }
4250 : :
4251 : : /* Give up, assume we have a function. */
4252 : :
4253 : 167318 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4254 : 167318 : sym = symtree->n.sym;
4255 : 167318 : e->expr_type = EXPR_FUNCTION;
4256 : :
4257 : 167318 : if (!sym->attr.function
4258 : 167318 : && !gfc_add_function (&sym->attr, sym->name, NULL))
4259 : : {
4260 : : m = MATCH_ERROR;
4261 : : break;
4262 : : }
4263 : :
4264 : 167318 : sym->result = sym;
4265 : :
4266 : 167318 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4267 : 167318 : if (m == MATCH_NO)
4268 : 0 : gfc_error ("Missing argument list in function %qs at %C", sym->name);
4269 : :
4270 : 167318 : if (m != MATCH_YES)
4271 : : {
4272 : : m = MATCH_ERROR;
4273 : : break;
4274 : : }
4275 : :
4276 : : /* If our new function returns a character, array or structure
4277 : : type, it might have subsequent references. */
4278 : :
4279 : 167188 : m = gfc_match_varspec (e, 0, false, true);
4280 : 167188 : if (m == MATCH_NO)
4281 : : m = MATCH_YES;
4282 : :
4283 : : break;
4284 : :
4285 : 67679 : generic_function:
4286 : : /* Look for symbol first; if not found, look for STRUCTURE type symbol
4287 : : specially. Creates a generic symbol for derived types. */
4288 : 67679 : gfc_find_sym_tree (name, NULL, 1, &symtree);
4289 : 67679 : if (!symtree)
4290 : 0 : gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4291 : 67679 : if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4292 : 67679 : gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4293 : :
4294 : 67679 : e = gfc_get_expr ();
4295 : 67679 : e->symtree = symtree;
4296 : 67679 : e->expr_type = EXPR_FUNCTION;
4297 : :
4298 : 67679 : if (gfc_fl_struct (sym->attr.flavor))
4299 : : {
4300 : 0 : e->value.function.esym = sym;
4301 : 0 : e->symtree->n.sym->attr.generic = 1;
4302 : : }
4303 : :
4304 : 67679 : m = gfc_match_actual_arglist (0, &e->value.function.actual);
4305 : 67679 : break;
4306 : :
4307 : : case FL_NAMELIST:
4308 : : m = MATCH_ERROR;
4309 : : break;
4310 : :
4311 : 5 : default:
4312 : 5 : gfc_error ("Symbol at %C is not appropriate for an expression");
4313 : 5 : return MATCH_ERROR;
4314 : : }
4315 : :
4316 : : /* Scan for possible inquiry references. */
4317 : 69 : if (m == MATCH_YES
4318 : 3308192 : && e->expr_type == EXPR_VARIABLE
4319 : 4068365 : && gfc_peek_ascii_char () == '%')
4320 : : {
4321 : 14 : m = gfc_match_varspec (e, 0, false, false);
4322 : 14 : if (m == MATCH_NO)
4323 : : m = MATCH_YES;
4324 : : }
4325 : :
4326 : 3912246 : if (m == MATCH_YES)
4327 : : {
4328 : 3308192 : e->where = where;
4329 : 3308192 : *result = e;
4330 : : }
4331 : : else
4332 : 604054 : gfc_free_expr (e);
4333 : :
4334 : : return m;
4335 : : }
4336 : :
4337 : :
4338 : : /* Match a variable, i.e. something that can be assigned to. This
4339 : : starts as a symbol, can be a structure component or an array
4340 : : reference. It can be a function if the function doesn't have a
4341 : : separate RESULT variable. If the symbol has not been previously
4342 : : seen, we assume it is a variable.
4343 : :
4344 : : This function is called by two interface functions:
4345 : : gfc_match_variable, which has host_flag = 1, and
4346 : : gfc_match_equiv_variable, with host_flag = 0, to restrict the
4347 : : match of the symbol to the local scope. */
4348 : :
4349 : : static match
4350 : 2737781 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4351 : : {
4352 : 2737781 : gfc_symbol *sym, *dt_sym;
4353 : 2737781 : gfc_symtree *st;
4354 : 2737781 : gfc_expr *expr;
4355 : 2737781 : locus where, old_loc;
4356 : 2737781 : match m;
4357 : :
4358 : 2737781 : *result = NULL;
4359 : :
4360 : : /* Since nothing has any business being an lvalue in a module
4361 : : specification block, an interface block or a contains section,
4362 : : we force the changed_symbols mechanism to work by setting
4363 : : host_flag to 0. This prevents valid symbols that have the name
4364 : : of keywords, such as 'end', being turned into variables by
4365 : : failed matching to assignments for, e.g., END INTERFACE. */
4366 : 2737781 : if (gfc_current_state () == COMP_MODULE
4367 : 2737781 : || gfc_current_state () == COMP_SUBMODULE
4368 : : || gfc_current_state () == COMP_INTERFACE
4369 : : || gfc_current_state () == COMP_CONTAINS)
4370 : 183032 : host_flag = 0;
4371 : :
4372 : 2737781 : where = gfc_current_locus;
4373 : 2737781 : m = gfc_match_sym_tree (&st, host_flag);
4374 : 2737780 : if (m != MATCH_YES)
4375 : : return m;
4376 : :
4377 : 2737755 : sym = st->n.sym;
4378 : :
4379 : : /* If this is an implicit do loop index and implicitly typed,
4380 : : it should not be host associated. */
4381 : 2737755 : m = check_for_implicit_index (&st, &sym);
4382 : 2737755 : if (m != MATCH_YES)
4383 : : return m;
4384 : :
4385 : 2737755 : sym->attr.implied_index = 0;
4386 : :
4387 : 2737755 : gfc_set_sym_referenced (sym);
4388 : :
4389 : : /* STRUCTUREs may share names with variables, but derived types may not. */
4390 : 13504 : if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4391 : 2737821 : && (dt_sym = gfc_find_dt_in_generic (sym)))
4392 : : {
4393 : 5 : if (dt_sym->attr.flavor == FL_DERIVED)
4394 : 5 : gfc_error ("Derived type %qs cannot be used as a variable at %C",
4395 : : sym->name);
4396 : 5 : return MATCH_ERROR;
4397 : : }
4398 : :
4399 : 2737750 : switch (sym->attr.flavor)
4400 : : {
4401 : : case FL_VARIABLE:
4402 : : /* Everything is alright. */
4403 : : break;
4404 : :
4405 : 2463211 : case FL_UNKNOWN:
4406 : 2463211 : {
4407 : 2463211 : sym_flavor flavor = FL_UNKNOWN;
4408 : :
4409 : 2463211 : gfc_gobble_whitespace ();
4410 : :
4411 : 2463211 : if (sym->attr.external || sym->attr.procedure
4412 : 2463179 : || sym->attr.function || sym->attr.subroutine)
4413 : : flavor = FL_PROCEDURE;
4414 : :
4415 : : /* If it is not a procedure, is not typed and is host associated,
4416 : : we cannot give it a flavor yet. */
4417 : 2463179 : else if (sym->ns == gfc_current_ns->parent
4418 : 2659 : && sym->ts.type == BT_UNKNOWN)
4419 : : break;
4420 : :
4421 : : /* These are definitive indicators that this is a variable. */
4422 : 3280309 : else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4423 : 3262811 : || sym->attr.pointer || sym->as != NULL)
4424 : : flavor = FL_VARIABLE;
4425 : :
4426 : : if (flavor != FL_UNKNOWN
4427 : 1664183 : && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4428 : : return MATCH_ERROR;
4429 : : }
4430 : : break;
4431 : :
4432 : 17 : case FL_PARAMETER:
4433 : 17 : if (equiv_flag)
4434 : : {
4435 : 0 : gfc_error ("Named constant at %C in an EQUIVALENCE");
4436 : 0 : return MATCH_ERROR;
4437 : : }
4438 : 17 : if (gfc_in_match_data())
4439 : : {
4440 : 4 : gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4441 : : sym->name);
4442 : 4 : return MATCH_ERROR;
4443 : : }
4444 : : /* Otherwise this is checked for an error given in the
4445 : : variable definition context checks. */
4446 : : break;
4447 : :
4448 : 13499 : case FL_PROCEDURE:
4449 : : /* Check for a nonrecursive function result variable. */
4450 : 13499 : if (sym->attr.function
4451 : 11862 : && (!sym->attr.external || sym->abr_modproc_decl)
4452 : 11465 : && sym->result == sym
4453 : 24615 : && (gfc_is_function_return_value (sym, gfc_current_ns)
4454 : 2169 : || (sym->attr.entry
4455 : 467 : && sym->ns == gfc_current_ns)
4456 : 1709 : || (sym->attr.entry
4457 : 7 : && sym->ns == gfc_current_ns->parent)))
4458 : : {
4459 : : /* If a function result is a derived type, then the derived
4460 : : type may still have to be resolved. */
4461 : :
4462 : 9414 : if (sym->ts.type == BT_DERIVED
4463 : 9414 : && gfc_use_derived (sym->ts.u.derived) == NULL)
4464 : : return MATCH_ERROR;
4465 : : break;
4466 : : }
4467 : :
4468 : 4085 : if (sym->attr.proc_pointer
4469 : 4085 : || replace_hidden_procptr_result (&sym, &st))
4470 : : break;
4471 : :
4472 : : /* Fall through to error */
4473 : 2442 : gcc_fallthrough ();
4474 : :
4475 : 2442 : default:
4476 : 2442 : gfc_error ("%qs at %C is not a variable", sym->name);
4477 : 2442 : return MATCH_ERROR;
4478 : : }
4479 : :
4480 : : /* Special case for derived type variables that get their types
4481 : : via an IMPLICIT statement. This can't wait for the
4482 : : resolution phase. */
4483 : :
4484 : 2735300 : {
4485 : 2735300 : gfc_namespace * implicit_ns;
4486 : :
4487 : 2735300 : if (gfc_current_ns->proc_name == sym)
4488 : : implicit_ns = gfc_current_ns;
4489 : : else
4490 : 2726697 : implicit_ns = sym->ns;
4491 : :
4492 : 2735300 : old_loc = gfc_current_locus;
4493 : 2735300 : if (gfc_match_member_sep (sym) == MATCH_YES
4494 : 19032 : && sym->ts.type == BT_UNKNOWN
4495 : 2735312 : && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4496 : 3 : gfc_set_default_type (sym, 0, implicit_ns);
4497 : 2735300 : gfc_current_locus = old_loc;
4498 : : }
4499 : :
4500 : 2735300 : expr = gfc_get_expr ();
4501 : :
4502 : 2735300 : expr->expr_type = EXPR_VARIABLE;
4503 : 2735300 : expr->symtree = st;
4504 : 2735300 : expr->ts = sym->ts;
4505 : :
4506 : : /* Now see if we have to do more. */
4507 : 2735300 : m = gfc_match_varspec (expr, equiv_flag, false, false);
4508 : 2735300 : if (m != MATCH_YES)
4509 : : {
4510 : 79 : gfc_free_expr (expr);
4511 : 79 : return m;
4512 : : }
4513 : :
4514 : 2735221 : expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4515 : 2735221 : *result = expr;
4516 : 2735221 : return MATCH_YES;
4517 : : }
4518 : :
4519 : :
4520 : : match
4521 : 2734836 : gfc_match_variable (gfc_expr **result, int equiv_flag)
4522 : : {
4523 : 2734836 : return match_variable (result, equiv_flag, 1);
4524 : : }
4525 : :
4526 : :
4527 : : match
4528 : 2945 : gfc_match_equiv_variable (gfc_expr **result)
4529 : : {
4530 : 2945 : return match_variable (result, 1, 0);
4531 : : }
|