Debian Codegraph @

gcc/fortran/primary.cc

raw
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 match_kind_param (int *kind, int *is_iso_c)
41 {
42 char name[GFC_MAX_SYMBOL_LEN + 1];
43 gfc_symbol *sym;
44 match m;
45
46 *is_iso_c = 0;
47
48 m = gfc_match_small_literal_int (kind, NULL, false);
49 if (m != MATCH_NO)
50 return m;
51
52 m = gfc_match_name (name, false);
53 if (m != MATCH_YES)
54 return m;
55
56 if (gfc_find_symbol (name, NULL, 1, &sym))
57 return MATCH_ERROR;
58
59 if (sym == NULL)
60 return MATCH_NO;
61
62 *is_iso_c = sym->attr.is_iso_c;
63
64 if (sym->attr.flavor != FL_PARAMETER)
65 return MATCH_NO;
66
67 if (sym->value == NULL)
68 return MATCH_NO;
69
70 if (gfc_extract_int (sym->value, kind))
71 return MATCH_NO;
72
73 gfc_set_sym_referenced (sym);
74
75 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 get_kind (int *is_iso_c)
92 {
93 int kind;
94 match m;
95
96 *is_iso_c = 0;
97
98 if (gfc_match_char ('_', false) != MATCH_YES)
99 return -2;
100
101 m = match_kind_param (&kind, is_iso_c);
102 if (m == MATCH_NO)
103 gfc_error ("Missing kind-parameter at %C");
104
105 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 gfc_check_digit (char c, int radix)
114 {
115 bool r;
116
117 switch (radix)
118 {
119 case 2:
120 r = ('0' <= c && c <= '1');
121 break;
122
123 case 8:
124 r = ('0' <= c && c <= '7');
125 break;
126
127 case 10:
128 r = ('0' <= c && c <= '9');
129 break;
130
131 case 16:
132 r = ISXDIGIT (c);
133 break;
134
135 default:
136 gfc_internal_error ("gfc_check_digit(): bad radix");
137 }
138
139 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 match_digits (int signflag, int radix, char *buffer)
150 {
151 locus old_loc;
152 int length;
153 char c;
154
155 length = 0;
156 c = gfc_next_ascii_char ();
157
158 if (signflag && (c == '+' || c == '-'))
159 {
160 if (buffer != NULL)
161 *buffer++ = c;
162 gfc_gobble_whitespace ();
163 c = gfc_next_ascii_char ();
164 length++;
165 }
166
167 if (!gfc_check_digit (c, radix))
168 return -1;
169
170 length++;
171 if (buffer != NULL)
172 *buffer++ = c;
173
174 for (;;)
175 {
176 old_loc = gfc_current_locus;
177 c = gfc_next_ascii_char ();
178
179 if (!gfc_check_digit (c, radix))
180 break;
181
182 if (buffer != NULL)
183 *buffer++ = c;
184 length++;
185 }
186
187 gfc_current_locus = old_loc;
188
189 return length;
190 }
191
192 /* Convert an integer string to an expression node. */
193
194 static gfc_expr *
195 convert_integer (const char *buffer, int kind, int radix, locus *where)
196 {
197 gfc_expr *e;
198 const char *t;
199
200 e = gfc_get_constant_expr (BT_INTEGER, kind, where);
201 /* A leading plus is allowed, but not by mpz_set_str. */
202 if (buffer[0] == '+')
203 t = buffer + 1;
204 else
205 t = buffer;
206 mpz_set_str (e->value.integer, t, radix);
207
208 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 convert_unsigned (const char *buffer, int kind, int radix, locus *where)
217 {
218 gfc_expr *e;
219 const char *t;
220 int k;
221 arith rc;
222
223 e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
224 /* A leading plus is allowed, but not by mpz_set_str. */
225 if (buffer[0] == '+')
226 t = buffer + 1;
227 else
228 t = buffer;
229
230 mpz_set_str (e->value.integer, t, radix);
231
232 k = gfc_validate_kind (BT_UNSIGNED, kind, false);
233
234 /* TODO Maybe move this somewhere else. */
235 rc = gfc_range_check (e);
236 if (rc != ARITH_OK)
237 {
238 if (pedantic)
239 gfc_error_now (gfc_arith_error (rc), &e->where);
240 else
241 gfc_warning (0, gfc_arith_error (rc), &e->where);
242 }
243
244 gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
245 false);
246
247 return e;
248 }
249
250 /* Convert a real string to an expression node. */
251
252 static gfc_expr *
253 convert_real (const char *buffer, int kind, locus *where)
254 {
255 gfc_expr *e;
256
257 e = gfc_get_constant_expr (BT_REAL, kind, where);
258 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
259
260 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 convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
269 {
270 gfc_expr *e;
271
272 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
273 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
274 GFC_MPC_RND_MODE);
275
276 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 match_integer_constant (gfc_expr **result, int signflag)
285 {
286 int length, kind, is_iso_c;
287 locus old_loc;
288 char *buffer;
289 gfc_expr *e;
290
291 old_loc = gfc_current_locus;
292 gfc_gobble_whitespace ();
293
294 length = match_digits (signflag, 10, NULL);
295 gfc_current_locus = old_loc;
296 if (length == -1)
297 return MATCH_NO;
298
299 buffer = (char *) alloca (length + 1);
300 memset (buffer, '\0', length + 1);
301
302 gfc_gobble_whitespace ();
303
304 match_digits (signflag, 10, buffer);
305
306 kind = get_kind (&is_iso_c);
307 if (kind == -2)
308 kind = gfc_default_integer_kind;
309 if (kind == -1)
310 return MATCH_ERROR;
311
312 if (kind == 4 && flag_integer4_kind == 8)
313 kind = 8;
314
315 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
316 {
317 gfc_error ("Integer kind %d at %C not available", kind);
318 return MATCH_ERROR;
319 }
320
321 e = convert_integer (buffer, kind, 10, &gfc_current_locus);
322 e->ts.is_c_interop = is_iso_c;
323
324 if (gfc_range_check (e) != ARITH_OK)
325 {
326 gfc_error ("Integer too big for its kind at %C. This check can be "
327 "disabled with the option %<-fno-range-check%>");
328
329 gfc_free_expr (e);
330 return MATCH_ERROR;
331 }
332
333 *result = e;
334 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 match_unsigned_constant (gfc_expr **result)
345 {
346 int length, kind, is_iso_c;
347 locus old_loc;
348 char *buffer;
349 gfc_expr *e;
350 match m;
351
352 old_loc = gfc_current_locus;
353 gfc_gobble_whitespace ();
354
355 length = match_digits (/* signflag = */ false, 10, NULL);
356
357 if (length == -1)
358 goto fail;
359
360 m = gfc_match_char ('u');
361 if (m == MATCH_NO)
362 goto fail;
363
364 gfc_current_locus = old_loc;
365
366 buffer = (char *) alloca (length + 1);
367 memset (buffer, '\0', length + 1);
368
369 gfc_gobble_whitespace ();
370
371 match_digits (false, 10, buffer);
372
373 m = gfc_match_char ('u');
374 if (m == MATCH_NO)
375 goto fail;
376
377 kind = get_kind (&is_iso_c);
378 if (kind == -2)
379 kind = gfc_default_unsigned_kind;
380 if (kind == -1)
381 return MATCH_ERROR;
382
383 if (kind == 4 && flag_integer4_kind == 8)
384 kind = 8;
385
386 if (gfc_validate_kind (BT_UNSIGNED, kind, true) < 0)
387 {
388 gfc_error ("Unsigned kind %d at %C not available", kind);
389 return MATCH_ERROR;
390 }
391
392 e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
393 e->ts.is_c_interop = is_iso_c;
394
395 *result = e;
396 return MATCH_YES;
397
398 fail:
399 gfc_current_locus = old_loc;
400 return MATCH_NO;
401 }
402
403 /* Match a Hollerith constant. */
404
405 static match
406 match_hollerith_constant (gfc_expr **result)
407 {
408 locus old_loc;
409 gfc_expr *e = NULL;
410 int num, pad;
411 int i;
412
413 old_loc = gfc_current_locus;
414 gfc_gobble_whitespace ();
415
416 if (match_integer_constant (&e, 0) == MATCH_YES
417 && gfc_match_char ('h') == MATCH_YES)
418 {
419 if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
420 goto cleanup;
421
422 if (gfc_extract_int (e, &num, 1))
423 goto cleanup;
424 if (num == 0)
425 {
426 gfc_error ("Invalid Hollerith constant: %L must contain at least "
427 "one character", &old_loc);
428 goto cleanup;
429 }
430 if (e->ts.kind != gfc_default_integer_kind)
431 {
432 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
433 "should be default", &old_loc);
434 goto cleanup;
435 }
436 else
437 {
438 gfc_free_expr (e);
439 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 pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
444
445 e->representation.string = XCNEWVEC (char, num + pad + 1);
446
447 for (i = 0; i < num; i++)
448 {
449 gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
450 if (! gfc_wide_fits_in_byte (c))
451 {
452 gfc_error ("Invalid Hollerith constant at %L contains a "
453 "wide character", &old_loc);
454 goto cleanup;
455 }
456
457 e->representation.string[i] = (unsigned char) c;
458 }
459
460 /* Now pad with blanks and end with a null char. */
461 for (i = 0; i < pad; i++)
462 e->representation.string[num + i] = ' ';
463
464 e->representation.string[num + i] = '\0';
465 e->representation.length = num + pad;
466 e->ts.u.pad = pad;
467
468 *result = e;
469 return MATCH_YES;
470 }
471 }
472
473 gfc_free_expr (e);
474 gfc_current_locus = old_loc;
475 return MATCH_NO;
476
477 cleanup:
478 gfc_free_expr (e);
479 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 match_boz_constant (gfc_expr **result)
491 {
492 int radix, length, x_hex;
493 locus old_loc, start_loc;
494 char *buffer, post, delim;
495 gfc_expr *e;
496
497 start_loc = old_loc = gfc_current_locus;
498 gfc_gobble_whitespace ();
499
500 x_hex = 0;
501 switch (post = gfc_next_ascii_char ())
502 {
503 case 'b':
504 radix = 2;
505 post = 0;
506 break;
507 case 'o':
508 radix = 8;
509 post = 0;
510 break;
511 case 'x':
512 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 default:
526 goto backup;
527 }
528
529 /* No whitespace allowed here. */
530
531 if (post == 0)
532 delim = gfc_next_ascii_char ();
533
534 if (delim != '\'' && delim != '\"')
535 goto backup;
536
537 if (x_hex
538 && gfc_invalid_boz (G_("Hexadecimal constant at %L uses "
539 "nonstandard X instead of Z"), &gfc_current_locus))
540 return MATCH_ERROR;
541
542 old_loc = gfc_current_locus;
543
544 length = match_digits (0, radix, NULL);
545 if (length == -1)
546 {
547 gfc_error ("Empty set of digits in BOZ constant at %C");
548 return MATCH_ERROR;
549 }
550
551 if (gfc_next_ascii_char () != delim)
552 {
553 gfc_error ("Illegal character in BOZ constant at %C");
554 return MATCH_ERROR;
555 }
556
557 if (post == 1)
558 {
559 switch (gfc_next_ascii_char ())
560 {
561 case 'b':
562 radix = 2;
563 break;
564 case 'o':
565 radix = 8;
566 break;
567 case 'x':
568 /* Fall through. */
569 case 'z':
570 radix = 16;
571 break;
572 default:
573 goto backup;
574 }
575
576 if (gfc_invalid_boz (G_("BOZ constant at %C uses nonstandard postfix "
577 "syntax"), &gfc_current_locus))
578 return MATCH_ERROR;
579 }
580
581 gfc_current_locus = old_loc;
582
583 buffer = (char *) alloca (length + 1);
584 memset (buffer, '\0', length + 1);
585
586 match_digits (0, radix, buffer);
587 gfc_next_ascii_char (); /* Eat delimiter. */
588 if (post == 1)
589 gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
590
591 e = gfc_get_expr ();
592 e->expr_type = EXPR_CONSTANT;
593 e->ts.type = BT_BOZ;
594 e->where = gfc_current_locus;
595 e->boz.rdx = radix;
596 e->boz.len = length;
597 e->boz.str = XCNEWVEC (char, length + 1);
598 strncpy (e->boz.str, buffer, length);
599
600 if (!gfc_in_match_data ()
601 && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
602 "statement at %L", &e->where)))
603 return MATCH_ERROR;
604
605 *result = e;
606 return MATCH_YES;
607
608 backup:
609 gfc_current_locus = start_loc;
610 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 match_real_constant (gfc_expr **result, int signflag)
619 {
620 int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
621 locus old_loc, temp_loc;
622 char *p, *buffer, c, exp_char;
623 gfc_expr *e;
624 bool negate;
625
626 old_loc = gfc_current_locus;
627 gfc_gobble_whitespace ();
628
629 e = NULL;
630
631 default_exponent = 0;
632 count = 0;
633 seen_dp = 0;
634 seen_digits = 0;
635 exp_char = ' ';
636 negate = false;
637
638 c = gfc_next_ascii_char ();
639 if (signflag && (c == '+' || c == '-'))
640 {
641 if (c == '-')
642 negate = true;
643
644 gfc_gobble_whitespace ();
645 c = gfc_next_ascii_char ();
646 }
647
648 /* Scan significand. */
649 for (;; c = gfc_next_ascii_char (), count++)
650 {
651 if (c == '.')
652 {
653 if (seen_dp)
654 goto done;
655
656 /* Check to see if "." goes with a following operator like
657 ".eq.". */
658 temp_loc = gfc_current_locus;
659 c = gfc_next_ascii_char ();
660
661 if (c == 'e' || c == 'd' || c == 'q')
662 {
663 c = gfc_next_ascii_char ();
664 if (c == '.')
665 goto done; /* Operator named .e. or .d. */
666 }
667
668 if (ISALPHA (c))
669 goto done; /* Distinguish 1.e9 from 1.eq.2 */
670
671 gfc_current_locus = temp_loc;
672 seen_dp = 1;
673 continue;
674 }
675
676 if (ISDIGIT (c))
677 {
678 seen_digits = 1;
679 continue;
680 }
681
682 break;
683 }
684
685 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
686 goto done;
687 exp_char = c;
688
689
690 if (c == 'q')
691 {
692 if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter %<q%> in "
693 "real-literal-constant at %C"))
694 return MATCH_ERROR;
695 else if (warn_real_q_constant)
696 gfc_warning (OPT_Wreal_q_constant,
697 "Extension: exponent-letter %<q%> in real-literal-constant "
698 "at %C");
699 }
700
701 /* Scan exponent. */
702 c = gfc_next_ascii_char ();
703 count++;
704
705 if (c == '+' || c == '-')
706 { /* optional sign */
707 c = gfc_next_ascii_char ();
708 count++;
709 }
710
711 if (!ISDIGIT (c))
712 {
713 /* With -fdec, default exponent to 0 instead of complaining. */
714 if (flag_dec)
715 default_exponent = 1;
716 else
717 {
718 gfc_error ("Missing exponent in real number at %C");
719 return MATCH_ERROR;
720 }
721 }
722
723 while (ISDIGIT (c))
724 {
725 c = gfc_next_ascii_char ();
726 count++;
727 }
728
729 done:
730 /* Check that we have a numeric constant. */
731 if (!seen_digits || (!seen_dp && exp_char == ' '))
732 {
733 gfc_current_locus = old_loc;
734 return MATCH_NO;
735 }
736
737 /* Convert the number. */
738 gfc_current_locus = old_loc;
739 gfc_gobble_whitespace ();
740
741 buffer = (char *) alloca (count + default_exponent + 1);
742 memset (buffer, '\0', count + default_exponent + 1);
743
744 p = buffer;
745 c = gfc_next_ascii_char ();
746 if (c == '+' || c == '-')
747 {
748 gfc_gobble_whitespace ();
749 c = gfc_next_ascii_char ();
750 }
751
752 /* Hack for mpfr_set_str(). */
753 for (;;)
754 {
755 if (c == 'd' || c == 'q')
756 *p = 'e';
757 else
758 *p = c;
759 p++;
760 if (--count == 0)
761 break;
762
763 c = gfc_next_ascii_char ();
764 }
765 if (default_exponent)
766 *p++ = '0';
767
768 kind = get_kind (&is_iso_c);
769 if (kind == -1)
770 goto cleanup;
771
772 if (kind == 4)
773 {
774 if (flag_real4_kind == 8)
775 kind = 8;
776 if (flag_real4_kind == 10)
777 kind = 10;
778 if (flag_real4_kind == 16)
779 kind = 16;
780 }
781 else if (kind == 8)
782 {
783 if (flag_real8_kind == 4)
784 kind = 4;
785 if (flag_real8_kind == 10)
786 kind = 10;
787 if (flag_real8_kind == 16)
788 kind = 16;
789 }
790
791 switch (exp_char)
792 {
793 case 'd':
794 if (kind != -2)
795 {
796 gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
797 "kind");
798 goto cleanup;
799 }
800 kind = gfc_default_double_kind;
801 break;
802
803 case 'q':
804 if (kind != -2)
805 {
806 gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
807 "kind");
808 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 kind = 16;
815 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
816 {
817 kind = 10;
818 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
819 {
820 gfc_error ("Invalid exponent-letter %<q%> in "
821 "real-literal-constant at %C");
822 goto cleanup;
823 }
824 }
825 break;
826
827 default:
828 if (kind == -2)
829 kind = gfc_default_real_kind;
830
831 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
832 {
833 gfc_error ("Invalid real kind %d at %C", kind);
834 goto cleanup;
835 }
836 }
837
838 e = convert_real (buffer, kind, &gfc_current_locus);
839 if (negate)
840 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
841 e->ts.is_c_interop = is_iso_c;
842
843 switch (gfc_range_check (e))
844 {
845 case ARITH_OK:
846 break;
847 case ARITH_OVERFLOW:
848 gfc_error ("Real constant overflows its kind at %C");
849 goto cleanup;
850
851 case ARITH_UNDERFLOW:
852 if (warn_underflow)
853 gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
854 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
855 break;
856
857 default:
858 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 if (warn_conversion_extra)
872 {
873 mpfr_t r;
874 char *c1;
875 bool did_break;
876
877 c1 = strchr (buffer, 'e');
878 if (c1 == NULL)
879 c1 = buffer + strlen(buffer);
880
881 did_break = false;
882 for (p = c1; p > buffer;)
883 {
884 p--;
885 if (*p == '.')
886 continue;
887
888 if (*p != '0')
889 {
890 *p = '0';
891 did_break = true;
892 break;
893 }
894 }
895
896 if (did_break)
897 {
898 mpfr_init (r);
899 mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
900 if (negate)
901 mpfr_neg (r, r, GFC_RND_MODE);
902
903 mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
904
905 if (mpfr_cmp_ui (r, 0) == 0)
906 gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
907 "in %qs number at %C, maybe incorrect KIND",
908 gfc_typename (&e->ts));
909
910 mpfr_clear (r);
911 }
912 }
913
914 *result = e;
915 return MATCH_YES;
916
917 cleanup:
918 gfc_free_expr (e);
919 return MATCH_ERROR;
920 }
921
922
923 /* Match a substring reference. */
924
925 static match
926 match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
927 {
928 gfc_expr *start, *end;
929 locus old_loc;
930 gfc_ref *ref;
931 match m;
932
933 start = NULL;
934 end = NULL;
935
936 old_loc = gfc_current_locus;
937
938 m = gfc_match_char ('(');
939 if (m != MATCH_YES)
940 return MATCH_NO;
941
942 if (gfc_match_char (':') != MATCH_YES)
943 {
944 if (init)
945 m = gfc_match_init_expr (&start);
946 else
947 m = gfc_match_expr (&start);
948
949 if (m != MATCH_YES)
950 {
951 m = MATCH_NO;
952 goto cleanup;
953 }
954
955 m = gfc_match_char (':');
956 if (m != MATCH_YES)
957 goto cleanup;
958 }
959
960 if (gfc_match_char (')') != MATCH_YES)
961 {
962 if (init)
963 m = gfc_match_init_expr (&end);
964 else
965 m = gfc_match_expr (&end);
966
967 if (m == MATCH_NO)
968 goto syntax;
969 if (m == MATCH_ERROR)
970 goto cleanup;
971
972 m = gfc_match_char (')');
973 if (m == MATCH_NO)
974 goto syntax;
975 }
976
977 /* Optimize away the (:) reference. */
978 if (start == NULL && end == NULL && !deferred)
979 ref = NULL;
980 else
981 {
982 ref = gfc_get_ref ();
983
984 ref->type = REF_SUBSTRING;
985 if (start == NULL)
986 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
987 ref->u.ss.start = start;
988 if (end == NULL && cl)
989 end = gfc_copy_expr (cl->length);
990 ref->u.ss.end = end;
991 ref->u.ss.length = cl;
992 }
993
994 *result = ref;
995 return MATCH_YES;
996
997 syntax:
998 gfc_error ("Syntax error in SUBSTRING specification at %C");
999 m = MATCH_ERROR;
1000
1001 cleanup:
1002 gfc_free_expr (start);
1003 gfc_free_expr (end);
1004
1005 gfc_current_locus = old_loc;
1006 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 next_string_char (gfc_char_t delimiter, int *ret)
1022 {
1023 locus old_locus;
1024 gfc_char_t c;
1025
1026 c = gfc_next_char_literal (INSTRING_WARN);
1027 *ret = 0;
1028
1029 if (c == '\n')
1030 {
1031 *ret = -2;
1032 return 0;
1033 }
1034
1035 if (flag_backslash && c == '\\')
1036 {
1037 old_locus = gfc_current_locus;
1038
1039 if (gfc_match_special_char (&c) == MATCH_NO)
1040 gfc_current_locus = old_locus;
1041
1042 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
1043 gfc_warning (0, "Extension: backslash character at %C");
1044 }
1045
1046 if (c != delimiter)
1047 return c;
1048
1049 old_locus = gfc_current_locus;
1050 c = gfc_next_char_literal (NONSTRING);
1051
1052 if (c == delimiter)
1053 return c;
1054 gfc_current_locus = old_locus;
1055
1056 *ret = -1;
1057 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 match_charkind_name (char *name)
1075 {
1076 locus old_loc;
1077 char c, peek;
1078 int len;
1079
1080 gfc_gobble_whitespace ();
1081 c = gfc_next_ascii_char ();
1082 if (!ISALPHA (c))
1083 return MATCH_NO;
1084
1085 *name++ = c;
1086 len = 1;
1087
1088 for (;;)
1089 {
1090 old_loc = gfc_current_locus;
1091 c = gfc_next_ascii_char ();
1092
1093 if (c == '_')
1094 {
1095 peek = gfc_peek_ascii_char ();
1096
1097 if (peek == '\'' || peek == '\"')
1098 {
1099 gfc_current_locus = old_loc;
1100 *name = '\0';
1101 return MATCH_YES;
1102 }
1103 }
1104
1105 if (!ISALNUM (c)
1106 && c != '_'
1107 && (c != '$' || !flag_dollar_ok))
1108 break;
1109
1110 *name++ = c;
1111 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 match_string_constant (gfc_expr **result)
1128 {
1129 char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1130 size_t length;
1131 int kind,save_warn_ampersand, ret;
1132 locus old_locus, start_locus;
1133 gfc_symbol *sym;
1134 gfc_expr *e;
1135 match m;
1136 gfc_char_t c, delimiter, *p;
1137
1138 old_locus = gfc_current_locus;
1139
1140 gfc_gobble_whitespace ();
1141
1142 c = gfc_next_char ();
1143 if (c == '\'' || c == '"')
1144 {
1145 kind = gfc_default_character_kind;
1146 start_locus = gfc_current_locus;
1147 goto got_delim;
1148 }
1149
1150 if (gfc_wide_is_digit (c))
1151 {
1152 kind = 0;
1153
1154 while (gfc_wide_is_digit (c))
1155 {
1156 kind = kind * 10 + c - '0';
1157 if (kind > 9999999)
1158 goto no_match;
1159 c = gfc_next_char ();
1160 }
1161
1162 }
1163 else
1164 {
1165 gfc_current_locus = old_locus;
1166
1167 m = match_charkind_name (name);
1168 if (m != MATCH_YES)
1169 goto no_match;
1170
1171 if (gfc_find_symbol (name, NULL, 1, &sym)
1172 || sym == NULL
1173 || sym->attr.flavor != FL_PARAMETER)
1174 goto no_match;
1175
1176 kind = -1;
1177 c = gfc_next_char ();
1178 }
1179
1180 if (c != '_')
1181 goto no_match;
1182
1183 c = gfc_next_char ();
1184 if (c != '\'' && c != '"')
1185 goto no_match;
1186
1187 start_locus = gfc_current_locus;
1188
1189 if (kind == -1)
1190 {
1191 if (gfc_extract_int (sym->value, &kind, 1))
1192 return MATCH_ERROR;
1193 gfc_set_sym_referenced (sym);
1194 }
1195
1196 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1197 {
1198 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
1199 return MATCH_ERROR;
1200 }
1201
1202 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 delimiter = c;
1209 length = 0;
1210
1211 for (;;)
1212 {
1213 c = next_string_char (delimiter, &ret);
1214 if (ret == -1)
1215 break;
1216 if (ret == -2)
1217 {
1218 gfc_current_locus = start_locus;
1219 gfc_error ("Unterminated character constant beginning at %C");
1220 return MATCH_ERROR;
1221 }
1222
1223 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 peek = gfc_peek_ascii_char ();
1229 if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1230 goto no_match;
1231
1232 e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1233
1234 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 save_warn_ampersand = warn_ampersand;
1239 warn_ampersand = false;
1240
1241 p = e->value.character.string;
1242 for (size_t i = 0; i < length; i++)
1243 {
1244 c = next_string_char (delimiter, &ret);
1245
1246 if (!gfc_check_character_range (c, kind))
1247 {
1248 gfc_free_expr (e);
1249 gfc_error ("Character %qs in string at %C is not representable "
1250 "in character kind %d", gfc_print_wide_char (c), kind);
1251 return MATCH_ERROR;
1252 }
1253
1254 *p++ = c;
1255 }
1256
1257 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1258 warn_ampersand = save_warn_ampersand;
1259
1260 next_string_char (delimiter, &ret);
1261 if (ret != -1)
1262 gfc_internal_error ("match_string_constant(): Delimiter not found");
1263
1264 if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
1265 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 if (e->expr_type == EXPR_SUBSTRING
1271 && e->ref && e->ref->type == REF_SUBSTRING
1272 && e->ref->u.ss.start->expr_type == EXPR_CONSTANT
1273 && (e->ref->u.ss.end == NULL
1274 || e->ref->u.ss.end->expr_type == EXPR_CONSTANT))
1275 {
1276 gfc_expr *res;
1277 ptrdiff_t istart, iend;
1278 size_t length;
1279 bool equal_length = false;
1280
1281 /* Basic checks on substring starting and ending indices. */
1282 if (!gfc_resolve_substring (e->ref, &equal_length))
1283 return MATCH_ERROR;
1284
1285 length = e->value.character.length;
1286 istart = gfc_mpz_get_hwi (e->ref->u.ss.start->value.integer);
1287 if (e->ref->u.ss.end == NULL)
1288 iend = length;
1289 else
1290 iend = gfc_mpz_get_hwi (e->ref->u.ss.end->value.integer);
1291
1292 if (istart <= iend)
1293 {
1294 if (istart < 1)
1295 {
1296 gfc_error ("Substring start index (%td) at %L below 1",
1297 istart, &e->ref->u.ss.start->where);
1298 return MATCH_ERROR;
1299 }
1300 if (iend > (ssize_t) length)
1301 {
1302 gfc_error ("Substring end index (%td) at %L exceeds string "
1303 "length", iend, &e->ref->u.ss.end->where);
1304 return MATCH_ERROR;
1305 }
1306 length = iend - istart + 1;
1307 }
1308 else
1309 length = 0;
1310
1311 res = gfc_get_constant_expr (BT_CHARACTER, e->ts.kind, &e->where);
1312 res->value.character.string = gfc_get_wide_string (length + 1);
1313 res->value.character.length = length;
1314 if (length > 0)
1315 memcpy (res->value.character.string,
1316 &e->value.character.string[istart - 1],
1317 length * sizeof (gfc_char_t));
1318 res->value.character.string[length] = '\0';
1319 e = res;
1320 }
1321
1322 *result = e;
1323
1324 return MATCH_YES;
1325
1326 no_match:
1327 gfc_current_locus = old_locus;
1328 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 match_logical_constant_string (void)
1336 {
1337 locus orig_loc = gfc_current_locus;
1338
1339 gfc_gobble_whitespace ();
1340 if (gfc_next_ascii_char () == '.')
1341 {
1342 char ch = gfc_next_ascii_char ();
1343 if (ch == 'f')
1344 {
1345 if (gfc_next_ascii_char () == 'a'
1346 && gfc_next_ascii_char () == 'l'
1347 && gfc_next_ascii_char () == 's'
1348 && gfc_next_ascii_char () == 'e'
1349 && gfc_next_ascii_char () == '.')
1350 /* Matched ".false.". */
1351 return 0;
1352 }
1353 else if (ch == 't')
1354 {
1355 if (gfc_next_ascii_char () == 'r'
1356 && gfc_next_ascii_char () == 'u'
1357 && gfc_next_ascii_char () == 'e'
1358 && gfc_next_ascii_char () == '.')
1359 /* Matched ".true.". */
1360 return 1;
1361 }
1362 }
1363 gfc_current_locus = orig_loc;
1364 return -1;
1365 }
1366
1367 /* Match a .true. or .false. */
1368
1369 static match
1370 match_logical_constant (gfc_expr **result)
1371 {
1372 gfc_expr *e;
1373 int i, kind, is_iso_c;
1374
1375 i = match_logical_constant_string ();
1376 if (i == -1)
1377 return MATCH_NO;
1378
1379 kind = get_kind (&is_iso_c);
1380 if (kind == -1)
1381 return MATCH_ERROR;
1382 if (kind == -2)
1383 kind = gfc_default_logical_kind;
1384
1385 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1386 {
1387 gfc_error ("Bad kind for logical constant at %C");
1388 return MATCH_ERROR;
1389 }
1390
1391 e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1392 e->ts.is_c_interop = is_iso_c;
1393
1394 *result = e;
1395 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 match_sym_complex_part (gfc_expr **result)
1404 {
1405 char name[GFC_MAX_SYMBOL_LEN + 1];
1406 gfc_symbol *sym;
1407 gfc_expr *e;
1408 match m;
1409
1410 m = gfc_match_name (name);
1411 if (m != MATCH_YES)
1412 return m;
1413
1414 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1415 return MATCH_NO;
1416
1417 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 char c;
1423 gfc_gobble_whitespace ();
1424 c = gfc_peek_ascii_char ();
1425 if (c == '=' || c == ',')
1426 {
1427 m = MATCH_NO;
1428 }
1429 else
1430 {
1431 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1432 m = MATCH_ERROR;
1433 }
1434 return m;
1435 }
1436
1437 if (!sym->value)
1438 goto error;
1439
1440 if (!gfc_numeric_ts (&sym->value->ts))
1441 {
1442 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1443 return MATCH_ERROR;
1444 }
1445
1446 if (sym->value->rank != 0)
1447 {
1448 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1449 return MATCH_ERROR;
1450 }
1451
1452 if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1453 "complex constant at %C"))
1454 return MATCH_ERROR;
1455
1456 switch (sym->value->ts.type)
1457 {
1458 case BT_REAL:
1459 e = gfc_copy_expr (sym->value);
1460 break;
1461
1462 case BT_COMPLEX:
1463 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1464 if (e == NULL)
1465 goto error;
1466 break;
1467
1468 case BT_INTEGER:
1469 e = gfc_int2real (sym->value, gfc_default_real_kind);
1470 if (e == NULL)
1471 goto error;
1472 break;
1473
1474 case BT_UNSIGNED:
1475 goto error;
1476
1477 default:
1478 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1479 }
1480
1481 *result = e; /* e is a scalar, real, constant expression. */
1482 return MATCH_YES;
1483
1484 error:
1485 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1486 return MATCH_ERROR;
1487 }
1488
1489
1490 /* Match a real or imaginary part of a complex number. */
1491
1492 static match
1493 match_complex_part (gfc_expr **result)
1494 {
1495 match m;
1496
1497 m = match_sym_complex_part (result);
1498 if (m != MATCH_NO)
1499 return m;
1500
1501 m = match_real_constant (result, 1);
1502 if (m != MATCH_NO)
1503 return m;
1504
1505 return match_integer_constant (result, 1);
1506 }
1507
1508
1509 /* Try to match a complex constant. */
1510
1511 static match
1512 match_complex_constant (gfc_expr **result)
1513 {
1514 gfc_expr *e, *real, *imag;
1515 gfc_error_buffer old_error;
1516 gfc_typespec target;
1517 locus old_loc;
1518 int kind;
1519 match m;
1520
1521 old_loc = gfc_current_locus;
1522 real = imag = e = NULL;
1523
1524 m = gfc_match_char ('(');
1525 if (m != MATCH_YES)
1526 return m;
1527
1528 gfc_push_error (&old_error);
1529
1530 m = match_complex_part (&real);
1531 if (m == MATCH_NO)
1532 {
1533 gfc_free_error (&old_error);
1534 goto cleanup;
1535 }
1536
1537 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 gfc_clear_warning ();
1543 gfc_pop_error (&old_error);
1544 m = MATCH_NO;
1545 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 if (m == MATCH_ERROR)
1554 {
1555 gfc_free_error (&old_error);
1556 goto cleanup;
1557 }
1558 gfc_pop_error (&old_error);
1559
1560 m = match_complex_part (&imag);
1561 if (m == MATCH_NO)
1562 goto syntax;
1563 if (m == MATCH_ERROR)
1564 goto cleanup;
1565
1566 m = gfc_match_char (')');
1567 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 if (gfc_peek_ascii_char () == '=')
1572 {
1573 m = MATCH_ERROR;
1574 goto cleanup;
1575 }
1576 else
1577 goto syntax;
1578 }
1579
1580 if (m == MATCH_ERROR)
1581 goto cleanup;
1582
1583 /* Decide on the kind of this complex number. */
1584 if (real->ts.type == BT_REAL)
1585 {
1586 if (imag->ts.type == BT_REAL)
1587 kind = gfc_kind_max (real, imag);
1588 else
1589 kind = real->ts.kind;
1590 }
1591 else
1592 {
1593 if (imag->ts.type == BT_REAL)
1594 kind = imag->ts.kind;
1595 else
1596 kind = gfc_default_real_kind;
1597 }
1598 gfc_clear_ts (&target);
1599 target.type = BT_REAL;
1600 target.kind = kind;
1601
1602 if (real->ts.type != BT_REAL || kind != real->ts.kind)
1603 gfc_convert_type (real, &target, 2);
1604 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1605 gfc_convert_type (imag, &target, 2);
1606
1607 e = convert_complex (real, imag, kind);
1608 e->where = gfc_current_locus;
1609
1610 gfc_free_expr (real);
1611 gfc_free_expr (imag);
1612
1613 *result = e;
1614 return MATCH_YES;
1615
1616 syntax:
1617 gfc_error ("Syntax error in COMPLEX constant at %C");
1618 m = MATCH_ERROR;
1619
1620 cleanup:
1621 gfc_free_expr (e);
1622 gfc_free_expr (real);
1623 gfc_free_expr (imag);
1624 gfc_current_locus = old_loc;
1625
1626 return m;
1627 }
1628
1629
1630 /* Match constants in any of several forms. Returns nonzero for a
1631 match, zero for no match. */
1632
1633 match
1634 gfc_match_literal_constant (gfc_expr **result, int signflag)
1635 {
1636 match m;
1637
1638 m = match_complex_constant (result);
1639 if (m != MATCH_NO)
1640 return m;
1641
1642 m = match_string_constant (result);
1643 if (m != MATCH_NO)
1644 return m;
1645
1646 m = match_boz_constant (result);
1647 if (m != MATCH_NO)
1648 return m;
1649
1650 m = match_real_constant (result, signflag);
1651 if (m != MATCH_NO)
1652 return m;
1653
1654 m = match_hollerith_constant (result);
1655 if (m != MATCH_NO)
1656 return m;
1657
1658 if (flag_unsigned)
1659 {
1660 m = match_unsigned_constant (result);
1661 if (m != MATCH_NO)
1662 return m;
1663 }
1664
1665 m = match_integer_constant (result, signflag);
1666 if (m != MATCH_NO)
1667 return m;
1668
1669 m = match_logical_constant (result);
1670 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 gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1683 {
1684 if (!sym->attr.function || (sym->result != sym))
1685 return false;
1686 while (ns)
1687 {
1688 if (ns->proc_name == sym)
1689 return true;
1690 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 match_actual_arg (gfc_expr **result)
1705 {
1706 char name[GFC_MAX_SYMBOL_LEN + 1];
1707 gfc_symtree *symtree;
1708 locus where, w;
1709 gfc_expr *e;
1710 char c;
1711
1712 gfc_gobble_whitespace ();
1713 where = gfc_current_locus;
1714
1715 switch (gfc_match_name (name))
1716 {
1717 case MATCH_ERROR:
1718 return MATCH_ERROR;
1719
1720 case MATCH_NO:
1721 break;
1722
1723 case MATCH_YES:
1724 w = gfc_current_locus;
1725 gfc_gobble_whitespace ();
1726 c = gfc_next_ascii_char ();
1727 gfc_current_locus = w;
1728
1729 if (c != ',' && c != ')')
1730 break;
1731
1732 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 if (symtree == NULL)
1739 {
1740 gfc_get_sym_tree (name, NULL, &symtree, false);
1741 gfc_set_sym_referenced (symtree->n.sym);
1742 }
1743 else
1744 {
1745 gfc_symbol *sym;
1746
1747 sym = symtree->n.sym;
1748 gfc_set_sym_referenced (sym);
1749 if (sym->attr.flavor == FL_NAMELIST)
1750 {
1751 gfc_error ("Namelist %qs cannot be an argument at %L",
1752 sym->name, &where);
1753 break;
1754 }
1755 if (sym->attr.flavor != FL_PROCEDURE
1756 && sym->attr.flavor != FL_UNKNOWN)
1757 break;
1758
1759 if (sym->attr.in_common && !sym->attr.proc_pointer)
1760 {
1761 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 if (sym->attr.function && sym->result == sym)
1770 {
1771 if (gfc_is_function_return_value (sym, gfc_current_ns))
1772 break;
1773
1774 if (sym->attr.entry
1775 && (sym->ns == gfc_current_ns
1776 || sym->ns == gfc_current_ns->parent))
1777 {
1778 gfc_entry_list *el = NULL;
1779
1780 for (el = sym->ns->entries; el; el = el->next)
1781 if (sym == el->sym)
1782 break;
1783
1784 if (el)
1785 break;
1786 }
1787 }
1788 }
1789
1790 e = gfc_get_expr (); /* Leave it unknown for now */
1791 e->symtree = symtree;
1792 e->expr_type = EXPR_VARIABLE;
1793 e->ts.type = BT_PROCEDURE;
1794 e->where = where;
1795
1796 *result = e;
1797 return MATCH_YES;
1798 }
1799
1800 gfc_current_locus = where;
1801 return gfc_match_expr (result);
1802 }
1803
1804
1805 /* Match a keyword argument or type parameter spec list.. */
1806
1807 static match
1808 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1809 {
1810 char name[GFC_MAX_SYMBOL_LEN + 1];
1811 gfc_actual_arglist *a;
1812 locus name_locus;
1813 match m;
1814
1815 name_locus = gfc_current_locus;
1816 m = gfc_match_name (name);
1817
1818 if (m != MATCH_YES)
1819 goto cleanup;
1820 if (gfc_match_char ('=') != MATCH_YES)
1821 {
1822 m = MATCH_NO;
1823 goto cleanup;
1824 }
1825
1826 if (pdt)
1827 {
1828 if (gfc_match_char ('*') == MATCH_YES)
1829 {
1830 actual->spec_type = SPEC_ASSUMED;
1831 goto add_name;
1832 }
1833 else if (gfc_match_char (':') == MATCH_YES)
1834 {
1835 actual->spec_type = SPEC_DEFERRED;
1836 goto add_name;
1837 }
1838 else
1839 actual->spec_type = SPEC_EXPLICIT;
1840 }
1841
1842 m = match_actual_arg (&actual->expr);
1843 if (m != MATCH_YES)
1844 goto cleanup;
1845
1846 /* Make sure this name has not appeared yet. */
1847 add_name:
1848 if (name[0] != '\0')
1849 {
1850 for (a = base; a; a = a->next)
1851 if (a->name != NULL && strcmp (a->name, name) == 0)
1852 {
1853 gfc_error ("Keyword %qs at %C has already appeared in the "
1854 "current argument list", name);
1855 return MATCH_ERROR;
1856 }
1857 }
1858
1859 actual->name = gfc_get_string ("%s", name);
1860 return MATCH_YES;
1861
1862 cleanup:
1863 gfc_current_locus = name_locus;
1864 return m;
1865 }
1866
1867
1868 /* Match an argument list function, such as %VAL. */
1869
1870 static match
1871 match_arg_list_function (gfc_actual_arglist *result)
1872 {
1873 char name[GFC_MAX_SYMBOL_LEN + 1];
1874 locus old_locus;
1875 match m;
1876
1877 old_locus = gfc_current_locus;
1878
1879 if (gfc_match_char ('%') != MATCH_YES)
1880 {
1881 m = MATCH_NO;
1882 goto cleanup;
1883 }
1884
1885 m = gfc_match ("%n (", name);
1886 if (m != MATCH_YES)
1887 goto cleanup;
1888
1889 if (name[0] != '\0')
1890 {
1891 switch (name[0])
1892 {
1893 case 'l':
1894 if (startswith (name, "loc"))
1895 {
1896 result->name = "%LOC";
1897 break;
1898 }
1899 /* FALLTHRU */
1900 case 'r':
1901 if (startswith (name, "ref"))
1902 {
1903 result->name = "%REF";
1904 break;
1905 }
1906 /* FALLTHRU */
1907 case 'v':
1908 if (startswith (name, "val"))
1909 {
1910 result->name = "%VAL";
1911 break;
1912 }
1913 /* FALLTHRU */
1914 default:
1915 m = MATCH_ERROR;
1916 goto cleanup;
1917 }
1918 }
1919
1920 if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
1921 {
1922 m = MATCH_ERROR;
1923 goto cleanup;
1924 }
1925
1926 m = match_actual_arg (&result->expr);
1927 if (m != MATCH_YES)
1928 goto cleanup;
1929
1930 if (gfc_match_char (')') != MATCH_YES)
1931 {
1932 m = MATCH_NO;
1933 goto cleanup;
1934 }
1935
1936 return MATCH_YES;
1937
1938 cleanup:
1939 gfc_current_locus = old_locus;
1940 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 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1958 {
1959 gfc_actual_arglist *head, *tail;
1960 int seen_keyword;
1961 gfc_st_label *label;
1962 locus old_loc;
1963 match m;
1964
1965 *argp = tail = NULL;
1966 old_loc = gfc_current_locus;
1967
1968 seen_keyword = 0;
1969
1970 if (gfc_match_char ('(') == MATCH_NO)
1971 return (sub_flag) ? MATCH_YES : MATCH_NO;
1972
1973 if (gfc_match_char (')') == MATCH_YES)
1974 return MATCH_YES;
1975
1976 head = NULL;
1977
1978 matching_actual_arglist++;
1979
1980 for (;;)
1981 {
1982 if (head == NULL)
1983 head = tail = gfc_get_actual_arglist ();
1984 else
1985 {
1986 tail->next = gfc_get_actual_arglist ();
1987 tail = tail->next;
1988 }
1989
1990 if (sub_flag && !pdt && gfc_match_char ('*') == MATCH_YES)
1991 {
1992 m = gfc_match_st_label (&label);
1993 if (m == MATCH_NO)
1994 gfc_error ("Expected alternate return label at %C");
1995 if (m != MATCH_YES)
1996 goto cleanup;
1997
1998 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
1999 "at %C"))
2000 goto cleanup;
2001
2002 tail->label = label;
2003 goto next;
2004 }
2005
2006 if (pdt && !seen_keyword)
2007 {
2008 if (gfc_match_char (':') == MATCH_YES)
2009 {
2010 tail->spec_type = SPEC_DEFERRED;
2011 goto next;
2012 }
2013 else if (gfc_match_char ('*') == MATCH_YES)
2014 {
2015 tail->spec_type = SPEC_ASSUMED;
2016 goto next;
2017 }
2018 else
2019 tail->spec_type = SPEC_EXPLICIT;
2020
2021 m = match_keyword_arg (tail, head, pdt);
2022 if (m == MATCH_YES)
2023 {
2024 seen_keyword = 1;
2025 goto next;
2026 }
2027 if (m == MATCH_ERROR)
2028 goto cleanup;
2029 }
2030
2031 /* After the first keyword argument is seen, the following
2032 arguments must also have keywords. */
2033 if (seen_keyword)
2034 {
2035 m = match_keyword_arg (tail, head, pdt);
2036
2037 if (m == MATCH_ERROR)
2038 goto cleanup;
2039 if (m == MATCH_NO)
2040 {
2041 gfc_error ("Missing keyword name in actual argument list at %C");
2042 goto cleanup;
2043 }
2044
2045 }
2046 else
2047 {
2048 /* Try an argument list function, like %VAL. */
2049 m = match_arg_list_function (tail);
2050 if (m == MATCH_ERROR)
2051 goto cleanup;
2052
2053 /* See if we have the first keyword argument. */
2054 if (m == MATCH_NO)
2055 {
2056 m = match_keyword_arg (tail, head, false);
2057 if (m == MATCH_YES)
2058 seen_keyword = 1;
2059 if (m == MATCH_ERROR)
2060 goto cleanup;
2061 }
2062
2063 if (m == MATCH_NO)
2064 {
2065 /* Try for a non-keyword argument. */
2066 m = match_actual_arg (&tail->expr);
2067 if (m == MATCH_ERROR)
2068 goto cleanup;
2069 if (m == MATCH_NO)
2070 goto syntax;
2071 }
2072 }
2073
2074
2075 next:
2076 if (gfc_match_char (')') == MATCH_YES)
2077 break;
2078 if (gfc_match_char (',') != MATCH_YES)
2079 goto syntax;
2080 }
2081
2082 *argp = head;
2083 matching_actual_arglist--;
2084 return MATCH_YES;
2085
2086 syntax:
2087 gfc_error ("Syntax error in argument list at %C");
2088
2089 cleanup:
2090 gfc_free_actual_arglist (head);
2091 gfc_current_locus = old_loc;
2092 matching_actual_arglist--;
2093 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 extend_ref (gfc_expr *primary, gfc_ref *tail)
2102 {
2103 if (primary->ref == NULL)
2104 primary->ref = tail = gfc_get_ref ();
2105 else if (tail == NULL)
2106 {
2107 /* Set tail to end of reference chain. */
2108 for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2109 if (ref->next == NULL)
2110 {
2111 tail = ref;
2112 break;
2113 }
2114 }
2115 else
2116 {
2117 tail->next = gfc_get_ref ();
2118 tail = tail->next;
2119 }
2120
2121 return tail;
2122 }
2123
2124
2125 /* Used by gfc_match_varspec() to match an inquiry reference. */
2126
2127 bool
2128 is_inquiry_ref (const char *name, gfc_ref **ref)
2129 {
2130 inquiry_type type;
2131
2132 if (name == NULL)
2133 return false;
2134
2135 if (ref) *ref = NULL;
2136
2137 if (strcmp (name, "re") == 0)
2138 type = INQUIRY_RE;
2139 else if (strcmp (name, "im") == 0)
2140 type = INQUIRY_IM;
2141 else if (strcmp (name, "kind") == 0)
2142 type = INQUIRY_KIND;
2143 else if (strcmp (name, "len") == 0)
2144 type = INQUIRY_LEN;
2145 else
2146 return false;
2147
2148 if (ref)
2149 {
2150 *ref = gfc_get_ref ();
2151 (*ref)->type = REF_INQUIRY;
2152 (*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 resolvable_fcns (gfc_expr *e,
2163 gfc_symbol *sym ATTRIBUTE_UNUSED,
2164 int *f ATTRIBUTE_UNUSED)
2165 {
2166 bool p;
2167 gfc_symbol *s;
2168
2169 if (e->expr_type != EXPR_FUNCTION)
2170 return false;
2171
2172 s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
2173 p = s && (s->attr.use_assoc
2174 || s->attr.host_assoc
2175 || s->attr.if_source == IFSRC_DECL
2176 || s->attr.proc == PROC_INTRINSIC
2177 || gfc_is_intrinsic (s, 0, e->where));
2178 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 gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2192 bool ppc_arg)
2193 {
2194 char name[GFC_MAX_SYMBOL_LEN + 1];
2195 gfc_ref *substring, *tail, *tmp;
2196 gfc_component *component = NULL;
2197 gfc_component *previous = NULL;
2198 gfc_symbol *sym = primary->symtree->n.sym;
2199 gfc_expr *tgt_expr = NULL;
2200 match m;
2201 bool unknown;
2202 bool inquiry;
2203 bool intrinsic;
2204 bool inferred_type;
2205 locus old_loc;
2206 char peeked_char;
2207
2208 tail = NULL;
2209
2210 gfc_gobble_whitespace ();
2211
2212 if (gfc_peek_ascii_char () == '[')
2213 {
2214 if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2215 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2216 && CLASS_DATA (sym)->attr.dimension))
2217 {
2218 gfc_error ("Array section designator, e.g. %<(:)%>, is required "
2219 "besides the coarray designator %<[...]%> at %C");
2220 return MATCH_ERROR;
2221 }
2222 if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2223 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2224 && !CLASS_DATA (sym)->attr.codimension))
2225 {
2226 gfc_error ("Coarray designator at %C but %qs is not a coarray",
2227 sym->name);
2228 return MATCH_ERROR;
2229 }
2230 }
2231
2232 if (sym->assoc && sym->assoc->target)
2233 tgt_expr = sym->assoc->target;
2234
2235 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 if (!inferred_type
2242 && sym->attr.select_type_temporary
2243 && sym->ns->assoc_name_inferred
2244 && !sym->attr.select_rank_temporary)
2245 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 if (sym->assoc
2254 && gfc_peek_ascii_char () == '('
2255 && sym->ts.type != BT_CLASS
2256 && !sym->attr.dimension)
2257 {
2258 gfc_ref *ref = NULL;
2259
2260 if (!sym->assoc->dangling && tgt_expr)
2261 {
2262 if (tgt_expr->expr_type == EXPR_VARIABLE)
2263 gfc_resolve_expr (tgt_expr);
2264
2265 ref = tgt_expr->ref;
2266 for (; ref; ref = ref->next)
2267 if (ref->type == REF_ARRAY
2268 && (ref->u.ar.type == AR_FULL
2269 || ref->u.ar.type == AR_SECTION))
2270 break;
2271 }
2272
2273 if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2274 && sym->assoc->st
2275 && sym->assoc->st->n.sym
2276 && sym->assoc->st->n.sym->attr.dimension == 0))
2277 {
2278 sym->attr.dimension = 1;
2279 if (sym->as == NULL
2280 && sym->assoc->st
2281 && sym->assoc->st->n.sym
2282 && sym->assoc->st->n.sym->as)
2283 sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2284 }
2285 }
2286 else if (sym->ts.type == BT_CLASS
2287 && !(sym->assoc && sym->assoc->ar)
2288 && tgt_expr
2289 && tgt_expr->expr_type == EXPR_VARIABLE
2290 && sym->ts.u.derived != tgt_expr->ts.u.derived)
2291 {
2292 gfc_resolve_expr (tgt_expr);
2293 if (tgt_expr->rank)
2294 sym->ts.u.derived = tgt_expr->ts.u.derived;
2295 }
2296
2297 peeked_char = gfc_peek_ascii_char ();
2298 if ((inferred_type && !sym->as && peeked_char == '(')
2299 || (equiv_flag && peeked_char == '(') || peeked_char == '['
2300 || sym->attr.codimension
2301 || (sym->attr.dimension && sym->ts.type != BT_CLASS
2302 && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2303 && !(gfc_matching_procptr_assignment
2304 && sym->attr.flavor == FL_PROCEDURE))
2305 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2306 && sym->ts.u.derived && CLASS_DATA (sym)
2307 && (CLASS_DATA (sym)->attr.dimension
2308 || CLASS_DATA (sym)->attr.codimension)))
2309 {
2310 gfc_array_spec *as;
2311 bool coarray_only = sym->attr.codimension && !sym->attr.dimension
2312 && sym->ts.type == BT_CHARACTER;
2313 gfc_ref *ref, *strarr = NULL;
2314
2315 tail = extend_ref (primary, tail);
2316 if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
2317 {
2318 gcc_assert (sym->attr.dimension);
2319 /* Find array reference for substrings of character arrays. */
2320 for (ref = primary->ref; ref && ref->next; ref = ref->next)
2321 if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
2322 {
2323 strarr = ref;
2324 break;
2325 }
2326 }
2327 else
2328 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 if (equiv_flag)
2335 as = NULL;
2336 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2337 as = CLASS_DATA (sym)->as;
2338 else
2339 as = sym->as;
2340
2341 ref = strarr ? strarr : tail;
2342 m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
2343 coarray_only);
2344 if (m != MATCH_YES)
2345 return m;
2346
2347 gfc_gobble_whitespace ();
2348 if (coarray_only)
2349 {
2350 primary->ts = sym->ts;
2351 goto check_substring;
2352 }
2353
2354 if (equiv_flag && gfc_peek_ascii_char () == '(')
2355 {
2356 tail = extend_ref (primary, tail);
2357 tail->type = REF_ARRAY;
2358
2359 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2360 if (m != MATCH_YES)
2361 return m;
2362 }
2363 }
2364
2365 primary->ts = sym->ts;
2366
2367 if (equiv_flag)
2368 return MATCH_YES;
2369
2370 /* With DEC extensions, member separator may be '.' or '%'. */
2371 peeked_char = gfc_peek_ascii_char ();
2372 m = gfc_match_member_sep (sym);
2373 if (m == MATCH_ERROR)
2374 return MATCH_ERROR;
2375
2376 inquiry = false;
2377 if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
2378 && (primary->ts.type != BT_DERIVED || inferred_type))
2379 {
2380 match mm;
2381 old_loc = gfc_current_locus;
2382 mm = gfc_match_name (name);
2383
2384 /* Check to see if this has a default complex. */
2385 if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
2386 && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
2387 {
2388 gfc_set_default_type (sym, 0, sym->ns);
2389 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 if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
2398 && !(sym->ts.type == BT_UNKNOWN
2399 && gfc_find_derived_types (sym, gfc_current_ns, name)))
2400 inquiry = true;
2401 gfc_current_locus = old_loc;
2402 }
2403
2404 /* Use the default type if there is one. */
2405 if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2406 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2407 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 if ((sym->ts.type == BT_UNKNOWN || inferred_type)
2412 && m == MATCH_YES)
2413 {
2414 bool sym_present, resolved = false;
2415 gfc_symbol *tgt_sym;
2416
2417 sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
2418 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 if (((sym_present // (i)
2426 && (tgt_sym->attr.use_assoc
2427 || tgt_sym->attr.host_assoc
2428 || tgt_sym->attr.if_source == IFSRC_DECL
2429 || tgt_sym->attr.proc == PROC_INTRINSIC
2430 || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
2431 || (tgt_expr && tgt_expr->expr_type == EXPR_OP)) // (ii)
2432 && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0) // (iii)
2433 && gfc_resolve_expr (tgt_expr))
2434 {
2435 sym->ts = tgt_expr->ts;
2436 primary->ts = sym->ts;
2437 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 if (tgt_expr
2447 && (tgt_expr->expr_type == EXPR_FUNCTION
2448 || tgt_expr->expr_type == EXPR_ARRAY
2449 || (!resolved && tgt_expr->expr_type == EXPR_OP))
2450 && (sym->ts.type == BT_UNKNOWN
2451 || (inferred_type && sym->ts.type != BT_COMPLEX))
2452 && gfc_find_derived_types (sym, gfc_current_ns, name, true))
2453 {
2454 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 gfc_symbol **dts = &sym->assoc->derived_types;
2458 tgt_expr->ts.type = BT_DERIVED;
2459 tgt_expr->ts.kind = 0;
2460 tgt_expr->ts.u.derived = *dts;
2461 sym->ts = tgt_expr->ts;
2462 primary->ts = sym->ts;
2463 /* Delete the dt list even if this process has to be done again for
2464 another primary expression. */
2465 while (*dts && (*dts)->dt_next)
2466 {
2467 gfc_symbol **tmp = &(*dts)->dt_next;
2468 *dts = NULL;
2469 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 else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
2476 && !gfc_find_derived_types (sym, gfc_current_ns, name))
2477 primary->ts.type = BT_UNKNOWN;
2478
2479 /* An inquiry reference might determine the type, otherwise we have an
2480 error. */
2481 if (sym->ts.type == BT_UNKNOWN && !inquiry)
2482 {
2483 gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2484 return MATCH_ERROR;
2485 }
2486 }
2487 else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2488 && m == MATCH_YES && !inquiry)
2489 {
2490 gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2491 peeked_char, sym->name);
2492 return MATCH_ERROR;
2493 }
2494
2495 if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2496 || m != MATCH_YES)
2497 goto check_substring;
2498
2499 if (!inquiry)
2500 sym = sym->ts.u.derived;
2501 else
2502 sym = NULL;
2503
2504 for (;;)
2505 {
2506 bool t;
2507 gfc_symtree *tbp;
2508 gfc_typespec *ts = &primary->ts;
2509
2510 m = gfc_match_name (name);
2511 if (m == MATCH_NO)
2512 gfc_error ("Expected structure component name at %C");
2513 if (m != MATCH_YES)
2514 return MATCH_ERROR;
2515
2516 /* For derived type components find typespec of ultimate component. */
2517 if (ts->type == BT_DERIVED && primary->ref)
2518 {
2519 for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
2520 {
2521 if (ref->type == REF_COMPONENT && ref->u.c.component)
2522 ts = &ref->u.c.component->ts;
2523 }
2524 }
2525
2526 intrinsic = false;
2527 if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
2528 {
2529 inquiry = is_inquiry_ref (name, &tmp);
2530 if (inquiry)
2531 sym = NULL;
2532
2533 if (peeked_char == '%')
2534 {
2535 if (tmp)
2536 {
2537 gfc_symbol *s;
2538 switch (tmp->u.i)
2539 {
2540 case INQUIRY_RE:
2541 case INQUIRY_IM:
2542 if (!gfc_notify_std (GFC_STD_F2008,
2543 "RE or IM part_ref at %C"))
2544 return MATCH_ERROR;
2545 break;
2546
2547 case INQUIRY_KIND:
2548 if (!gfc_notify_std (GFC_STD_F2003,
2549 "KIND part_ref at %C"))
2550 return MATCH_ERROR;
2551 break;
2552
2553 case INQUIRY_LEN:
2554 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 s = primary->symtree ? primary->symtree->n.sym : NULL;
2562 if (s && s->assoc && s->assoc->target
2563 && (s->ts.type == BT_UNKNOWN
2564 || (primary->ts.type == BT_UNKNOWN
2565 && s->assoc->inferred_type
2566 && s->ts.type == BT_DERIVED)))
2567 {
2568 if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2569 {
2570 s->ts.type = BT_COMPLEX;
2571 s->ts.kind = gfc_default_real_kind;;
2572 s->assoc->inferred_type = 1;
2573 primary->ts = s->ts;
2574 }
2575 else if (tmp->u.i == INQUIRY_LEN)
2576 {
2577 s->ts.type = BT_CHARACTER;
2578 s->ts.kind = gfc_default_character_kind;;
2579 s->assoc->inferred_type = 1;
2580 primary->ts = s->ts;
2581 }
2582 else if (s->ts.type == BT_UNKNOWN)
2583 {
2584 /* KIND inquiry gives no clue as to symbol type. */
2585 primary->ref = tmp;
2586 primary->ts.type = BT_INTEGER;
2587 primary->ts.kind = gfc_default_integer_kind;
2588 return MATCH_YES;
2589 }
2590 }
2591
2592 if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2593 && primary->ts.type != BT_COMPLEX)
2594 {
2595 gfc_error ("The RE or IM part_ref at %C must be "
2596 "applied to a COMPLEX expression");
2597 return MATCH_ERROR;
2598 }
2599 else if (tmp->u.i == INQUIRY_LEN
2600 && ts->type != BT_CHARACTER)
2601 {
2602 gfc_error ("The LEN part_ref at %C must be applied "
2603 "to a CHARACTER expression");
2604 return MATCH_ERROR;
2605 }
2606 }
2607 if (primary->ts.type != BT_UNKNOWN)
2608 intrinsic = true;
2609 }
2610 }
2611 else
2612 inquiry = false;
2613
2614 if (sym && sym->f2k_derived)
2615 tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2616 else
2617 tbp = NULL;
2618
2619 if (tbp)
2620 {
2621 gfc_symbol* tbp_sym;
2622
2623 if (!t)
2624 return MATCH_ERROR;
2625
2626 gcc_assert (!tail || !tail->next);
2627
2628 if (!(primary->expr_type == EXPR_VARIABLE
2629 || (primary->expr_type == EXPR_STRUCTURE
2630 && primary->symtree && primary->symtree->n.sym
2631 && primary->symtree->n.sym->attr.flavor)))
2632 return MATCH_ERROR;
2633
2634 if (tbp->n.tb->is_generic)
2635 tbp_sym = NULL;
2636 else
2637 tbp_sym = tbp->n.tb->u.specific->n.sym;
2638
2639 primary->expr_type = EXPR_COMPCALL;
2640 primary->value.compcall.tbp = tbp->n.tb;
2641 primary->value.compcall.name = tbp->name;
2642 primary->value.compcall.ignore_pass = 0;
2643 primary->value.compcall.assign = 0;
2644 primary->value.compcall.base_object = NULL;
2645 gcc_assert (primary->symtree->n.sym->attr.referenced);
2646 if (tbp_sym)
2647 primary->ts = tbp_sym->ts;
2648 else
2649 gfc_clear_ts (&primary->ts);
2650
2651 m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2652 &primary->value.compcall.actual);
2653 if (m == MATCH_ERROR)
2654 return MATCH_ERROR;
2655 if (m == MATCH_NO)
2656 {
2657 if (sub_flag)
2658 primary->value.compcall.actual = NULL;
2659 else
2660 {
2661 gfc_error ("Expected argument list at %C");
2662 return MATCH_ERROR;
2663 }
2664 }
2665
2666 break;
2667 }
2668
2669 previous = component;
2670
2671 if (!inquiry && !intrinsic)
2672 component = gfc_find_component (sym, name, false, false, &tmp);
2673 else
2674 component = NULL;
2675
2676 if (intrinsic && !inquiry)
2677 {
2678 if (previous)
2679 gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2680 "type component %qs", name, previous->name);
2681 else
2682 gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
2683 "type component", name);
2684 return MATCH_ERROR;
2685 }
2686 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 if (primary->ref == NULL)
2692 primary->ref = tmp;
2693 else
2694 {
2695 /* Find end of reference chain if inquiry reference and tail not
2696 set. */
2697 if (tail == NULL && inquiry && tmp)
2698 tail = extend_ref (primary, tail);
2699
2700 /* Set by the for loop below for the last component ref. */
2701 gcc_assert (tail != NULL);
2702 tail->next = tmp;
2703 }
2704
2705 /* The reference chain may be longer than one hop for union
2706 subcomponents; find the new tail. */
2707 for (tail = tmp; tail->next; tail = tail->next)
2708 ;
2709
2710 if (tmp && tmp->type == REF_INQUIRY)
2711 {
2712 if (!primary->where.u.lb || !primary->where.nextc)
2713 primary->where = gfc_current_locus;
2714 gfc_simplify_expr (primary, 0);
2715
2716 if (primary->expr_type == EXPR_CONSTANT)
2717 goto check_done;
2718
2719 if (primary->ref == NULL)
2720 goto check_done;
2721
2722 switch (tmp->u.i)
2723 {
2724 case INQUIRY_RE:
2725 case INQUIRY_IM:
2726 if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2727 return MATCH_ERROR;
2728
2729 if (primary->ts.type != BT_COMPLEX)
2730 {
2731 gfc_error ("The RE or IM part_ref at %C must be "
2732 "applied to a COMPLEX expression");
2733 return MATCH_ERROR;
2734 }
2735 primary->ts.type = BT_REAL;
2736 break;
2737
2738 case INQUIRY_LEN:
2739 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2740 return MATCH_ERROR;
2741
2742 if (primary->ts.type != BT_CHARACTER)
2743 {
2744 gfc_error ("The LEN part_ref at %C must be applied "
2745 "to a CHARACTER expression");
2746 return MATCH_ERROR;
2747 }
2748 primary->ts.u.cl = NULL;
2749 primary->ts.type = BT_INTEGER;
2750 primary->ts.kind = gfc_default_integer_kind;
2751 break;
2752
2753 case INQUIRY_KIND:
2754 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2755 return MATCH_ERROR;
2756
2757 if (primary->ts.type == BT_CLASS
2758 || primary->ts.type == BT_DERIVED)
2759 {
2760 gfc_error ("The KIND part_ref at %C must be applied "
2761 "to an expression of intrinsic type");
2762 return MATCH_ERROR;
2763 }
2764 primary->ts.type = BT_INTEGER;
2765 primary->ts.kind = gfc_default_integer_kind;
2766 break;
2767
2768 default:
2769 gcc_unreachable ();
2770 }
2771
2772 goto check_done;
2773 }
2774
2775 primary->ts = component->ts;
2776
2777 if (component->attr.proc_pointer && ppc_arg)
2778 {
2779 /* Procedure pointer component call: Look for argument list. */
2780 m = gfc_match_actual_arglist (sub_flag,
2781 &primary->value.compcall.actual);
2782 if (m == MATCH_ERROR)
2783 return MATCH_ERROR;
2784
2785 if (m == MATCH_NO && !gfc_matching_ptr_assignment
2786 && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2787 {
2788 gfc_error ("Procedure pointer component %qs requires an "
2789 "argument list at %C", component->name);
2790 return MATCH_ERROR;
2791 }
2792
2793 if (m == MATCH_YES)
2794 primary->expr_type = EXPR_PPC;
2795
2796 break;
2797 }
2798
2799 if (component->as != NULL && !component->attr.proc_pointer)
2800 {
2801 tail = extend_ref (primary, tail);
2802 tail->type = REF_ARRAY;
2803
2804 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2805 component->as->corank);
2806 if (m != MATCH_YES)
2807 return m;
2808 }
2809 else if (component->ts.type == BT_CLASS && component->attr.class_ok
2810 && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2811 {
2812 tail = extend_ref (primary, tail);
2813 tail->type = REF_ARRAY;
2814
2815 m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2816 equiv_flag,
2817 CLASS_DATA (component)->as->corank);
2818 if (m != MATCH_YES)
2819 return m;
2820 }
2821
2822 check_done:
2823 /* In principle, we could have eg. expr%re%kind so we must allow for
2824 this possibility. */
2825 if (gfc_match_char ('%') == MATCH_YES)
2826 {
2827 if (component && (component->ts.type == BT_DERIVED
2828 || component->ts.type == BT_CLASS))
2829 sym = component->ts.u.derived;
2830 continue;
2831 }
2832 else if (inquiry)
2833 break;
2834
2835 if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2836 || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2837 break;
2838
2839 if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2840 sym = component->ts.u.derived;
2841 }
2842
2843 check_substring:
2844 unknown = false;
2845 if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2846 {
2847 if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2848 {
2849 gfc_set_default_type (sym, 0, sym->ns);
2850 primary->ts = sym->ts;
2851 unknown = true;
2852 }
2853 }
2854
2855 if (primary->ts.type == BT_CHARACTER)
2856 {
2857 bool def = primary->ts.deferred == 1;
2858 switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2859 {
2860 case MATCH_YES:
2861 if (tail == NULL)
2862 primary->ref = substring;
2863 else
2864 tail->next = substring;
2865
2866 if (primary->expr_type == EXPR_CONSTANT)
2867 primary->expr_type = EXPR_SUBSTRING;
2868
2869 if (substring)
2870 primary->ts.u.cl = NULL;
2871
2872 gfc_gobble_whitespace ();
2873 if (gfc_peek_ascii_char () == '(')
2874 {
2875 gfc_error_now ("Unexpected array/substring ref at %C");
2876 return MATCH_ERROR;
2877 }
2878 break;
2879
2880 case MATCH_NO:
2881 if (unknown)
2882 {
2883 gfc_clear_ts (&primary->ts);
2884 gfc_clear_ts (&sym->ts);
2885 }
2886 break;
2887
2888 case MATCH_ERROR:
2889 return MATCH_ERROR;
2890 }
2891 }
2892
2893 /* F08:C611. */
2894 if (primary->ts.type == BT_DERIVED && primary->ref
2895 && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2896 {
2897 gfc_error ("Nonpolymorphic reference to abstract type at %C");
2898 return MATCH_ERROR;
2899 }
2900
2901 /* F08:C727. */
2902 if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2903 {
2904 gfc_error ("Coindexed procedure-pointer component at %C");
2905 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 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2931 {
2932 int dimension, codimension, pointer, allocatable, target, optional;
2933 symbol_attribute attr;
2934 gfc_ref *ref;
2935 gfc_symbol *sym;
2936 gfc_component *comp;
2937 bool has_inquiry_part;
2938 bool has_substring_ref = false;
2939
2940 if (expr->expr_type != EXPR_VARIABLE
2941 && expr->expr_type != EXPR_FUNCTION
2942 && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
2943 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2944
2945 sym = expr->symtree->n.sym;
2946 attr = sym->attr;
2947
2948 optional = attr.optional;
2949 if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
2950 {
2951 dimension = CLASS_DATA (sym)->attr.dimension;
2952 codimension = CLASS_DATA (sym)->attr.codimension;
2953 pointer = CLASS_DATA (sym)->attr.class_pointer;
2954 allocatable = CLASS_DATA (sym)->attr.allocatable;
2955 }
2956 else
2957 {
2958 dimension = attr.dimension;
2959 codimension = attr.codimension;
2960 pointer = attr.pointer;
2961 allocatable = attr.allocatable;
2962 }
2963
2964 target = attr.target;
2965 if (pointer || attr.proc_pointer)
2966 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 if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
2973 {
2974 if (sym->assoc->target->expr_type == EXPR_VARIABLE)
2975 {
2976 symbol_attribute tgt_attr;
2977 tgt_attr = gfc_expr_attr (sym->assoc->target);
2978 target = (tgt_attr.pointer || tgt_attr.target);
2979 }
2980 else
2981 target = 0;
2982 }
2983
2984 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2985 *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 if (ts != NULL
2993 && expr->ts.type == BT_PROCEDURE
2994 && expr->ref == NULL
2995 && attr.flavor != FL_PROCEDURE
2996 && attr.target)
2997 *ts = sym->ts;
2998
2999 has_inquiry_part = false;
3000 for (ref = expr->ref; ref; ref = ref->next)
3001 if (ref->type == REF_SUBSTRING)
3002 {
3003 has_substring_ref = true;
3004 optional = false;
3005 }
3006 else if (ref->type == REF_INQUIRY)
3007 {
3008 has_inquiry_part = true;
3009 optional = false;
3010 break;
3011 }
3012
3013 for (ref = expr->ref; ref; ref = ref->next)
3014 switch (ref->type)
3015 {
3016 case REF_ARRAY:
3017
3018 switch (ref->u.ar.type)
3019 {
3020 case AR_FULL:
3021 dimension = 1;
3022 break;
3023
3024 case AR_SECTION:
3025 allocatable = pointer = 0;
3026 dimension = 1;
3027 optional = false;
3028 break;
3029
3030 case AR_ELEMENT:
3031 /* Handle coarrays. */
3032 if (ref->u.ar.dimen > 0)
3033 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 case REF_COMPONENT:
3046 optional = false;
3047 comp = ref->u.c.component;
3048 attr = comp->attr;
3049 if (ts != NULL && !has_inquiry_part)
3050 {
3051 *ts = comp->ts;
3052 /* Don't set the string length if a substring reference
3053 follows. */
3054 if (ts->type == BT_CHARACTER && has_substring_ref)
3055 ts->u.cl = NULL;
3056 }
3057
3058 if (comp->ts.type == BT_CLASS)
3059 {
3060 dimension = CLASS_DATA (comp)->attr.dimension;
3061 codimension = CLASS_DATA (comp)->attr.codimension;
3062 pointer = CLASS_DATA (comp)->attr.class_pointer;
3063 allocatable = CLASS_DATA (comp)->attr.allocatable;
3064 }
3065 else
3066 {
3067 dimension = comp->attr.dimension;
3068 codimension = comp->attr.codimension;
3069 if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
3070 pointer = comp->attr.class_pointer;
3071 else
3072 pointer = comp->attr.pointer;
3073 allocatable = comp->attr.allocatable;
3074 }
3075 if (pointer || attr.proc_pointer)
3076 target = 1;
3077
3078 break;
3079
3080 case REF_INQUIRY:
3081 case REF_SUBSTRING:
3082 allocatable = pointer = optional = false;
3083 break;
3084 }
3085
3086 attr.dimension = dimension;
3087 attr.codimension = codimension;
3088 attr.pointer = pointer;
3089 attr.allocatable = allocatable;
3090 attr.target = target;
3091 attr.save = sym->attr.save;
3092 attr.optional = optional;
3093
3094 return attr;
3095 }
3096
3097
3098 /* Return the attribute from a general expression. */
3099
3100 symbol_attribute
3101 gfc_expr_attr (gfc_expr *e)
3102 {
3103 symbol_attribute attr;
3104
3105 switch (e->expr_type)
3106 {
3107 case EXPR_VARIABLE:
3108 attr = gfc_variable_attr (e, NULL);
3109 break;
3110
3111 case EXPR_FUNCTION:
3112 gfc_clear_attr (&attr);
3113
3114 if (e->value.function.esym && e->value.function.esym->result)
3115 {
3116 gfc_symbol *sym = e->value.function.esym->result;
3117 attr = sym->attr;
3118 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3119 {
3120 attr.dimension = CLASS_DATA (sym)->attr.dimension;
3121 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3122 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3123 }
3124 }
3125 else if (e->value.function.isym
3126 && e->value.function.isym->transformational
3127 && e->ts.type == BT_CLASS)
3128 attr = CLASS_DATA (e)->attr;
3129 else if (e->symtree)
3130 attr = gfc_variable_attr (e, NULL);
3131
3132 /* TODO: NULL() returns pointers. May have to take care of this
3133 here. */
3134
3135 break;
3136
3137 default:
3138 gfc_clear_attr (&attr);
3139 break;
3140 }
3141
3142 return attr;
3143 }
3144
3145
3146 /* Given an expression, figure out what the ultimate expression
3147 attribute is. This routine is similar to gfc_variable_attr with
3148 parts of gfc_expr_attr, but focuses more on the needs of
3149 coarrays. For coarrays a codimension attribute is kind of
3150 "infectious" being propagated once set and never cleared.
3151 The coarray_comp is only set, when the expression refs a coarray
3152 component. REFS_COMP is set when present to true only, when this EXPR
3153 refs a (non-_data) component. To check whether EXPR refs an allocatable
3154 component in a derived type coarray *refs_comp needs to be set and
3155 coarray_comp has to false. */
3156
3157 static symbol_attribute
3158 caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
3159 {
3160 int dimension, codimension, pointer, allocatable, target, coarray_comp;
3161 symbol_attribute attr;
3162 gfc_ref *ref;
3163 gfc_symbol *sym;
3164 gfc_component *comp;
3165
3166 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
3167 gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
3168
3169 sym = expr->symtree->n.sym;
3170 gfc_clear_attr (&attr);
3171
3172 if (refs_comp)
3173 *refs_comp = false;
3174
3175 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
3176 {
3177 dimension = CLASS_DATA (sym)->attr.dimension;
3178 codimension = CLASS_DATA (sym)->attr.codimension;
3179 pointer = CLASS_DATA (sym)->attr.class_pointer;
3180 allocatable = CLASS_DATA (sym)->attr.allocatable;
3181 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3182 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
3183 }
3184 else
3185 {
3186 dimension = sym->attr.dimension;
3187 codimension = sym->attr.codimension;
3188 pointer = sym->attr.pointer;
3189 allocatable = sym->attr.allocatable;
3190 attr.alloc_comp = sym->ts.type == BT_DERIVED
3191 ? sym->ts.u.derived->attr.alloc_comp : 0;
3192 attr.pointer_comp = sym->ts.type == BT_DERIVED
3193 ? sym->ts.u.derived->attr.pointer_comp : 0;
3194 }
3195
3196 target = coarray_comp = 0;
3197 if (pointer || attr.proc_pointer)
3198 target = 1;
3199
3200 for (ref = expr->ref; ref; ref = ref->next)
3201 switch (ref->type)
3202 {
3203 case REF_ARRAY:
3204
3205 switch (ref->u.ar.type)
3206 {
3207 case AR_FULL:
3208 case AR_SECTION:
3209 dimension = 1;
3210 break;
3211
3212 case AR_ELEMENT:
3213 /* Handle coarrays. */
3214 if (ref->u.ar.dimen > 0 && !in_allocate)
3215 allocatable = pointer = 0;
3216 break;
3217
3218 case AR_UNKNOWN:
3219 /* If any of start, end or stride is not integer, there will
3220 already have been an error issued. */
3221 int errors;
3222 gfc_get_errors (NULL, &errors);
3223 if (errors == 0)
3224 gfc_internal_error ("gfc_caf_attr(): Bad array reference");
3225 }
3226
3227 break;
3228
3229 case REF_COMPONENT:
3230 comp = ref->u.c.component;
3231
3232 if (comp->ts.type == BT_CLASS)
3233 {
3234 /* Set coarray_comp only, when this component introduces the
3235 coarray. */
3236 coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
3237 codimension |= CLASS_DATA (comp)->attr.codimension;
3238 pointer = CLASS_DATA (comp)->attr.class_pointer;
3239 allocatable = CLASS_DATA (comp)->attr.allocatable;
3240 }
3241 else
3242 {
3243 /* Set coarray_comp only, when this component introduces the
3244 coarray. */
3245 coarray_comp = !codimension && comp->attr.codimension;
3246 codimension |= comp->attr.codimension;
3247 pointer = comp->attr.pointer;
3248 allocatable = comp->attr.allocatable;
3249 }
3250
3251 if (refs_comp && strcmp (comp->name, "_data") != 0
3252 && (ref->next == NULL
3253 || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
3254 *refs_comp = true;
3255
3256 if (pointer || attr.proc_pointer)
3257 target = 1;
3258
3259 break;
3260
3261 case REF_SUBSTRING:
3262 case REF_INQUIRY:
3263 allocatable = pointer = 0;
3264 break;
3265 }
3266
3267 attr.dimension = dimension;
3268 attr.codimension = codimension;
3269 attr.pointer = pointer;
3270 attr.allocatable = allocatable;
3271 attr.target = target;
3272 attr.save = sym->attr.save;
3273 attr.coarray_comp = coarray_comp;
3274
3275 return attr;
3276 }
3277
3278
3279 symbol_attribute
3280 gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
3281 {
3282 symbol_attribute attr;
3283
3284 switch (e->expr_type)
3285 {
3286 case EXPR_VARIABLE:
3287 attr = caf_variable_attr (e, in_allocate, refs_comp);
3288 break;
3289
3290 case EXPR_FUNCTION:
3291 gfc_clear_attr (&attr);
3292
3293 if (e->value.function.esym && e->value.function.esym->result)
3294 {
3295 gfc_symbol *sym = e->value.function.esym->result;
3296 attr = sym->attr;
3297 if (sym->ts.type == BT_CLASS)
3298 {
3299 attr.dimension = CLASS_DATA (sym)->attr.dimension;
3300 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
3301 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
3302 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
3303 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
3304 ->attr.pointer_comp;
3305 }
3306 }
3307 else if (e->symtree)
3308 attr = caf_variable_attr (e, in_allocate, refs_comp);
3309 else
3310 gfc_clear_attr (&attr);
3311 break;
3312
3313 default:
3314 gfc_clear_attr (&attr);
3315 break;
3316 }
3317
3318 return attr;
3319 }
3320
3321
3322 /* Match a structure constructor. The initial symbol has already been
3323 seen. */
3324
3325 typedef struct gfc_structure_ctor_component
3326 {
3327 char* name;
3328 gfc_expr* val;
3329 locus where;
3330 struct gfc_structure_ctor_component* next;
3331 }
3332 gfc_structure_ctor_component;
3333
3334 #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
3335
3336 static void
3337 gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
3338 {
3339 free (comp->name);
3340 gfc_free_expr (comp->val);
3341 free (comp);
3342 }
3343
3344
3345 /* Translate the component list into the actual constructor by sorting it in
3346 the order required; this also checks along the way that each and every
3347 component actually has an initializer and handles default initializers
3348 for components without explicit value given. */
3349 static bool
3350 build_actual_constructor (gfc_structure_ctor_component **comp_head,
3351 gfc_constructor_base *ctor_head, gfc_symbol *sym)
3352 {
3353 gfc_structure_ctor_component *comp_iter;
3354 gfc_component *comp;
3355
3356 for (comp = sym->components; comp; comp = comp->next)
3357 {
3358 gfc_structure_ctor_component **next_ptr;
3359 gfc_expr *value = NULL;
3360
3361 /* Try to find the initializer for the current component by name. */
3362 next_ptr = comp_head;
3363 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
3364 {
3365 if (!strcmp (comp_iter->name, comp->name))
3366 break;
3367 next_ptr = &comp_iter->next;
3368 }
3369
3370 /* If an extension, try building the parent derived type by building
3371 a value expression for the parent derived type and calling self. */
3372 if (!comp_iter && comp == sym->components && sym->attr.extension)
3373 {
3374 value = gfc_get_structure_constructor_expr (comp->ts.type,
3375 comp->ts.kind,
3376 &gfc_current_locus);
3377 value->ts = comp->ts;
3378
3379 if (!build_actual_constructor (comp_head,
3380 &value->value.constructor,
3381 comp->ts.u.derived))
3382 {
3383 gfc_free_expr (value);
3384 return false;
3385 }
3386
3387 gfc_constructor_append_expr (ctor_head, value, NULL);
3388 continue;
3389 }
3390
3391 /* If it was not found, apply NULL expression to set the component as
3392 unallocated. Then try the default initializer if there's any;
3393 otherwise, it's an error unless this is a deferred parameter. */
3394 if (!comp_iter)
3395 {
3396 /* F2018 7.5.10: If an allocatable component has no corresponding
3397 component-data-source, then that component has an allocation
3398 status of unallocated.... */
3399 if (comp->attr.allocatable
3400 || (comp->ts.type == BT_CLASS
3401 && CLASS_DATA (comp)->attr.allocatable))
3402 {
3403 if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3404 "allocatable component %qs given in the "
3405 "structure constructor at %C", comp->name))
3406 return false;
3407 value = gfc_get_null_expr (&gfc_current_locus);
3408 }
3409 /* ....(Preceding sentence) If a component with default
3410 initialization has no corresponding component-data-source, then
3411 the default initialization is applied to that component. */
3412 else if (comp->initializer)
3413 {
3414 if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3415 "with missing optional arguments at %C"))
3416 return false;
3417 value = gfc_copy_expr (comp->initializer);
3418 }
3419 /* Do not trap components such as the string length for deferred
3420 length character components. */
3421 else if (!comp->attr.artificial)
3422 {
3423 gfc_error ("No initializer for component %qs given in the"
3424 " structure constructor at %C", comp->name);
3425 return false;
3426 }
3427 }
3428 else
3429 value = comp_iter->val;
3430
3431 /* Add the value to the constructor chain built. */
3432 gfc_constructor_append_expr (ctor_head, value, NULL);
3433
3434 /* Remove the entry from the component list. We don't want the expression
3435 value to be free'd, so set it to NULL. */
3436 if (comp_iter)
3437 {
3438 *next_ptr = comp_iter->next;
3439 comp_iter->val = NULL;
3440 gfc_free_structure_ctor_component (comp_iter);
3441 }
3442 }
3443 return true;
3444 }
3445
3446
3447 bool
3448 gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3449 gfc_actual_arglist **arglist,
3450 bool parent)
3451 {
3452 gfc_actual_arglist *actual;
3453 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3454 gfc_constructor_base ctor_head = NULL;
3455 gfc_component *comp; /* Is set NULL when named component is first seen */
3456 const char* last_name = NULL;
3457 locus old_locus;
3458 gfc_expr *expr;
3459
3460 expr = parent ? *cexpr : e;
3461 old_locus = gfc_current_locus;
3462 if (parent)
3463 ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3464 else
3465 gfc_current_locus = expr->where;
3466
3467 comp_tail = comp_head = NULL;
3468
3469 if (!parent && sym->attr.abstract)
3470 {
3471 gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3472 sym->name, &expr->where);
3473 goto cleanup;
3474 }
3475
3476 comp = sym->components;
3477 actual = parent ? *arglist : expr->value.function.actual;
3478 for ( ; actual; )
3479 {
3480 gfc_component *this_comp = NULL;
3481
3482 if (!comp_head)
3483 comp_tail = comp_head = gfc_get_structure_ctor_component ();
3484 else
3485 {
3486 comp_tail->next = gfc_get_structure_ctor_component ();
3487 comp_tail = comp_tail->next;
3488 }
3489 if (actual->name)
3490 {
3491 if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3492 " constructor with named arguments at %C"))
3493 goto cleanup;
3494
3495 comp_tail->name = xstrdup (actual->name);
3496 last_name = comp_tail->name;
3497 comp = NULL;
3498 }
3499 else
3500 {
3501 /* Components without name are not allowed after the first named
3502 component initializer! */
3503 if (!comp || comp->attr.artificial)
3504 {
3505 if (last_name)
3506 gfc_error ("Component initializer without name after component"
3507 " named %s at %L", last_name,
3508 actual->expr ? &actual->expr->where
3509 : &gfc_current_locus);
3510 else
3511 gfc_error ("Too many components in structure constructor at "
3512 "%L", actual->expr ? &actual->expr->where
3513 : &gfc_current_locus);
3514 goto cleanup;
3515 }
3516
3517 comp_tail->name = xstrdup (comp->name);
3518 }
3519
3520 /* Find the current component in the structure definition and check
3521 its access is not private. */
3522 if (comp)
3523 this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3524 else
3525 {
3526 this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3527 false, false, NULL);
3528 comp = NULL; /* Reset needed! */
3529 }
3530
3531 /* Here we can check if a component name is given which does not
3532 correspond to any component of the defined structure. */
3533 if (!this_comp)
3534 goto cleanup;
3535
3536 /* For a constant string constructor, make sure the length is
3537 correct; truncate or fill with blanks if needed. */
3538 if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3539 && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3540 && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3541 && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
3542 && actual->expr
3543 && actual->expr->ts.type == BT_CHARACTER
3544 && actual->expr->expr_type == EXPR_CONSTANT)
3545 {
3546 ptrdiff_t c, e1;
3547 c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3548 e1 = actual->expr->value.character.length;
3549
3550 if (c != e1)
3551 {
3552 ptrdiff_t i, to;
3553 gfc_char_t *dest;
3554 dest = gfc_get_wide_string (c + 1);
3555
3556 to = e1 < c ? e1 : c;
3557 for (i = 0; i < to; i++)
3558 dest[i] = actual->expr->value.character.string[i];
3559
3560 for (i = e1; i < c; i++)
3561 dest[i] = ' ';
3562
3563 dest[c] = '\0';
3564 free (actual->expr->value.character.string);
3565
3566 actual->expr->value.character.length = c;
3567 actual->expr->value.character.string = dest;
3568
3569 if (warn_line_truncation && c < e1)
3570 gfc_warning_now (OPT_Wcharacter_truncation,
3571 "CHARACTER expression will be truncated "
3572 "in constructor (%td/%td) at %L", c,
3573 e1, &actual->expr->where);
3574 }
3575 }
3576
3577 comp_tail->val = actual->expr;
3578 if (actual->expr != NULL)
3579 comp_tail->where = actual->expr->where;
3580 actual->expr = NULL;
3581
3582 /* Check if this component is already given a value. */
3583 for (comp_iter = comp_head; comp_iter != comp_tail;
3584 comp_iter = comp_iter->next)
3585 {
3586 gcc_assert (comp_iter);
3587 if (!strcmp (comp_iter->name, comp_tail->name))
3588 {
3589 gfc_error ("Component %qs is initialized twice in the structure"
3590 " constructor at %L", comp_tail->name,
3591 comp_tail->val ? &comp_tail->where
3592 : &gfc_current_locus);
3593 goto cleanup;
3594 }
3595 }
3596
3597 /* F2008, R457/C725, for PURE C1283. */
3598 if (this_comp->attr.pointer && comp_tail->val
3599 && gfc_is_coindexed (comp_tail->val))
3600 {
3601 gfc_error ("Coindexed expression to pointer component %qs in "
3602 "structure constructor at %L", comp_tail->name,
3603 &comp_tail->where);
3604 goto cleanup;
3605 }
3606
3607 /* If not explicitly a parent constructor, gather up the components
3608 and build one. */
3609 if (comp && comp == sym->components
3610 && sym->attr.extension
3611 && comp_tail->val
3612 && (!gfc_bt_struct (comp_tail->val->ts.type)
3613 || comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3614 {
3615 bool m;
3616 gfc_actual_arglist *arg_null = NULL;
3617
3618 actual->expr = comp_tail->val;
3619 comp_tail->val = NULL;
3620 #define shorter gfc_convert_to_structure_constructor
3621 m = shorter (NULL, comp->ts.u.derived, &comp_tail->val,
3622 comp->ts.u.derived->attr.zero_comp ? &arg_null :
3623 &actual, true);
3624 #undef shorter
3625
3626 if (!m)
3627 goto cleanup;
3628
3629 if (comp->ts.u.derived->attr.zero_comp)
3630 {
3631 comp = comp->next;
3632 continue;
3633 }
3634 }
3635
3636 if (comp)
3637 comp = comp->next;
3638 if (parent && !comp)
3639 break;
3640
3641 if (actual)
3642 actual = actual->next;
3643 }
3644
3645 if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3646 goto cleanup;
3647
3648 /* No component should be left, as this should have caused an error in the
3649 loop constructing the component-list (name that does not correspond to any
3650 component in the structure definition). */
3651 if (comp_head && sym->attr.extension)
3652 {
3653 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3654 {
3655 gfc_error ("component %qs at %L has already been set by a "
3656 "parent derived type constructor", comp_iter->name,
3657 &comp_iter->where);
3658 }
3659 goto cleanup;
3660 }
3661 else
3662 gcc_assert (!comp_head);
3663
3664 if (parent)
3665 {
3666 expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3667 expr->ts.u.derived = sym;
3668 expr->value.constructor = ctor_head;
3669 *cexpr = expr;
3670 }
3671 else
3672 {
3673 expr->ts.u.derived = sym;
3674 expr->ts.kind = 0;
3675 expr->ts.type = BT_DERIVED;
3676 expr->value.constructor = ctor_head;
3677 expr->expr_type = EXPR_STRUCTURE;
3678 }
3679
3680 gfc_current_locus = old_locus;
3681 if (parent)
3682 *arglist = actual;
3683 return true;
3684
3685 cleanup:
3686 gfc_current_locus = old_locus;
3687
3688 for (comp_iter = comp_head; comp_iter; )
3689 {
3690 gfc_structure_ctor_component *next = comp_iter->next;
3691 gfc_free_structure_ctor_component (comp_iter);
3692 comp_iter = next;
3693 }
3694 gfc_constructor_free (ctor_head);
3695
3696 return false;
3697 }
3698
3699
3700 match
3701 gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
3702 gfc_expr **result)
3703 {
3704 match m;
3705 gfc_expr *e;
3706 bool t = true;
3707
3708 e = gfc_get_expr ();
3709 e->expr_type = EXPR_FUNCTION;
3710 e->symtree = symtree;
3711 e->where = gfc_current_locus;
3712
3713 gcc_assert (gfc_fl_struct (sym->attr.flavor)
3714 && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3715 e->value.function.esym = sym;
3716 e->symtree->n.sym->attr.generic = 1;
3717
3718 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3719 if (m != MATCH_YES)
3720 {
3721 gfc_free_expr (e);
3722 return m;
3723 }
3724
3725 if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3726 {
3727 gfc_free_expr (e);
3728 return MATCH_ERROR;
3729 }
3730
3731 /* If a structure constructor is in a DATA statement, then each entity
3732 in the structure constructor must be a constant. Try to reduce the
3733 expression here. */
3734 if (gfc_in_match_data ())
3735 t = gfc_reduce_init_expr (e);
3736
3737 if (t)
3738 {
3739 *result = e;
3740 return MATCH_YES;
3741 }
3742 else
3743 {
3744 gfc_free_expr (e);
3745 return MATCH_ERROR;
3746 }
3747 }
3748
3749
3750 /* If the symbol is an implicit do loop index and implicitly typed,
3751 it should not be host associated. Provide a symtree from the
3752 current namespace. */
3753 static match
3754 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3755 {
3756 if ((*sym)->attr.flavor == FL_VARIABLE
3757 && (*sym)->ns != gfc_current_ns
3758 && (*sym)->attr.implied_index
3759 && (*sym)->attr.implicit_type
3760 && !(*sym)->attr.use_assoc)
3761 {
3762 int i;
3763 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3764 if (i)
3765 return MATCH_ERROR;
3766 *sym = (*st)->n.sym;
3767 }
3768 return MATCH_YES;
3769 }
3770
3771
3772 /* Procedure pointer as function result: Replace the function symbol by the
3773 auto-generated hidden result variable named "ppr@". */
3774
3775 static bool
3776 replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3777 {
3778 /* Check for procedure pointer result variable. */
3779 if ((*sym)->attr.function && !(*sym)->attr.external
3780 && (*sym)->result && (*sym)->result != *sym
3781 && (*sym)->result->attr.proc_pointer
3782 && (*sym) == gfc_current_ns->proc_name
3783 && (*sym) == (*sym)->result->ns->proc_name
3784 && strcmp ("ppr@", (*sym)->result->name) == 0)
3785 {
3786 /* Automatic replacement with "hidden" result variable. */
3787 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3788 *sym = (*sym)->result;
3789 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3790 return true;
3791 }
3792 return false;
3793 }
3794
3795
3796 /* Matches a variable name followed by anything that might follow it--
3797 array reference, argument list of a function, etc. */
3798
3799 match
3800 gfc_match_rvalue (gfc_expr **result)
3801 {
3802 gfc_actual_arglist *actual_arglist;
3803 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3804 gfc_state_data *st;
3805 gfc_symbol *sym;
3806 gfc_symtree *symtree;
3807 locus where, old_loc;
3808 gfc_expr *e;
3809 match m, m2;
3810 int i;
3811 gfc_typespec *ts;
3812 bool implicit_char;
3813 gfc_ref *ref;
3814
3815 m = gfc_match ("%%loc");
3816 if (m == MATCH_YES)
3817 {
3818 if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3819 return MATCH_ERROR;
3820 strncpy (name, "loc", 4);
3821 }
3822
3823 else
3824 {
3825 m = gfc_match_name (name);
3826 if (m != MATCH_YES)
3827 return m;
3828 }
3829
3830 /* Check if the symbol exists. */
3831 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3832 return MATCH_ERROR;
3833
3834 /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3835 type. For derived types we create a generic symbol which links to the
3836 derived type symbol; STRUCTUREs are simpler and must not conflict with
3837 variables. */
3838 if (!symtree)
3839 if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3840 return MATCH_ERROR;
3841 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3842 {
3843 if (gfc_find_state (COMP_INTERFACE)
3844 && !gfc_current_ns->has_import_set)
3845 i = gfc_get_sym_tree (name, NULL, &symtree, false);
3846 else
3847 i = gfc_get_ha_sym_tree (name, &symtree);
3848 if (i)
3849 return MATCH_ERROR;
3850 }
3851
3852
3853 sym = symtree->n.sym;
3854 e = NULL;
3855 where = gfc_current_locus;
3856
3857 replace_hidden_procptr_result (&sym, &symtree);
3858
3859 /* If this is an implicit do loop index and implicitly typed,
3860 it should not be host associated. */
3861 m = check_for_implicit_index (&symtree, &sym);
3862 if (m != MATCH_YES)
3863 return m;
3864
3865 gfc_set_sym_referenced (sym);
3866 sym->attr.implied_index = 0;
3867
3868 if (sym->attr.function && sym->result == sym)
3869 {
3870 /* See if this is a directly recursive function call. */
3871 gfc_gobble_whitespace ();
3872 if (sym->attr.recursive
3873 && gfc_peek_ascii_char () == '('
3874 && gfc_current_ns->proc_name == sym
3875 && !sym->attr.dimension)
3876 {
3877 gfc_error ("%qs at %C is the name of a recursive function "
3878 "and so refers to the result variable. Use an "
3879 "explicit RESULT variable for direct recursion "
3880 "(12.5.2.1)", sym->name);
3881 return MATCH_ERROR;
3882 }
3883
3884 if (gfc_is_function_return_value (sym, gfc_current_ns))
3885 goto variable;
3886
3887 if (sym->attr.entry
3888 && (sym->ns == gfc_current_ns
3889 || sym->ns == gfc_current_ns->parent))
3890 {
3891 gfc_entry_list *el = NULL;
3892
3893 for (el = sym->ns->entries; el; el = el->next)
3894 if (sym == el->sym)
3895 goto variable;
3896 }
3897 }
3898
3899 if (gfc_matching_procptr_assignment)
3900 {
3901 /* It can be a procedure or a derived-type procedure or a not-yet-known
3902 type. */
3903 if (sym->attr.flavor != FL_UNKNOWN
3904 && sym->attr.flavor != FL_PROCEDURE
3905 && sym->attr.flavor != FL_PARAMETER
3906 && sym->attr.flavor != FL_VARIABLE)
3907 {
3908 gfc_error ("Symbol at %C is not appropriate for an expression");
3909 return MATCH_ERROR;
3910 }
3911 goto procptr0;
3912 }
3913
3914 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3915 goto function0;
3916
3917 if (sym->attr.generic)
3918 goto generic_function;
3919
3920 switch (sym->attr.flavor)
3921 {
3922 case FL_VARIABLE:
3923 variable:
3924 e = gfc_get_expr ();
3925
3926 e->expr_type = EXPR_VARIABLE;
3927 e->symtree = symtree;
3928
3929 m = gfc_match_varspec (e, 0, false, true);
3930 break;
3931
3932 case FL_PARAMETER:
3933 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
3934 end up here. Unfortunately, sym->value->expr_type is set to
3935 EXPR_CONSTANT, and so the if () branch would be followed without
3936 the !sym->as check. */
3937 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
3938 e = gfc_copy_expr (sym->value);
3939 else
3940 {
3941 e = gfc_get_expr ();
3942 e->expr_type = EXPR_VARIABLE;
3943 }
3944
3945 e->symtree = symtree;
3946 m = gfc_match_varspec (e, 0, false, true);
3947
3948 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3949 break;
3950
3951 /* Variable array references to derived type parameters cause
3952 all sorts of headaches in simplification. Treating such
3953 expressions as variable works just fine for all array
3954 references. */
3955 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
3956 {
3957 for (ref = e->ref; ref; ref = ref->next)
3958 if (ref->type == REF_ARRAY)
3959 break;
3960
3961 if (ref == NULL || ref->u.ar.type == AR_FULL)
3962 break;
3963
3964 ref = e->ref;
3965 e->ref = NULL;
3966 gfc_free_expr (e);
3967 e = gfc_get_expr ();
3968 e->expr_type = EXPR_VARIABLE;
3969 e->symtree = symtree;
3970 e->ref = ref;
3971 }
3972
3973 break;
3974
3975 case FL_STRUCT:
3976 case FL_DERIVED:
3977 sym = gfc_use_derived (sym);
3978 if (sym == NULL)
3979 m = MATCH_ERROR;
3980 else
3981 goto generic_function;
3982 break;
3983
3984 /* If we're here, then the name is known to be the name of a
3985 procedure, yet it is not sure to be the name of a function. */
3986 case FL_PROCEDURE:
3987
3988 /* Procedure Pointer Assignments. */
3989 procptr0:
3990 if (gfc_matching_procptr_assignment)
3991 {
3992 gfc_gobble_whitespace ();
3993 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
3994 /* Parse functions returning a procptr. */
3995 goto function0;
3996
3997 e = gfc_get_expr ();
3998 e->expr_type = EXPR_VARIABLE;
3999 e->symtree = symtree;
4000 m = gfc_match_varspec (e, 0, false, true);
4001 if (!e->ref && sym->attr.flavor == FL_UNKNOWN
4002 && sym->ts.type == BT_UNKNOWN
4003 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4004 {
4005 m = MATCH_ERROR;
4006 break;
4007 }
4008 break;
4009 }
4010
4011 if (sym->attr.subroutine)
4012 {
4013 gfc_error ("Unexpected use of subroutine name %qs at %C",
4014 sym->name);
4015 m = MATCH_ERROR;
4016 break;
4017 }
4018
4019 /* At this point, the name has to be a non-statement function.
4020 If the name is the same as the current function being
4021 compiled, then we have a variable reference (to the function
4022 result) if the name is non-recursive. */
4023
4024 st = gfc_enclosing_unit (NULL);
4025
4026 if (st != NULL
4027 && st->state == COMP_FUNCTION
4028 && st->sym == sym
4029 && !sym->attr.recursive)
4030 {
4031 e = gfc_get_expr ();
4032 e->symtree = symtree;
4033 e->expr_type = EXPR_VARIABLE;
4034
4035 m = gfc_match_varspec (e, 0, false, true);
4036 break;
4037 }
4038
4039 /* Match a function reference. */
4040 function0:
4041 m = gfc_match_actual_arglist (0, &actual_arglist);
4042 if (m == MATCH_NO)
4043 {
4044 if (sym->attr.proc == PROC_ST_FUNCTION)
4045 gfc_error ("Statement function %qs requires argument list at %C",
4046 sym->name);
4047 else
4048 gfc_error ("Function %qs requires an argument list at %C",
4049 sym->name);
4050
4051 m = MATCH_ERROR;
4052 break;
4053 }
4054
4055 if (m != MATCH_YES)
4056 {
4057 m = MATCH_ERROR;
4058 break;
4059 }
4060
4061 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
4062 sym = symtree->n.sym;
4063
4064 replace_hidden_procptr_result (&sym, &symtree);
4065
4066 e = gfc_get_expr ();
4067 e->symtree = symtree;
4068 e->expr_type = EXPR_FUNCTION;
4069 e->value.function.actual = actual_arglist;
4070 e->where = gfc_current_locus;
4071
4072 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4073 && CLASS_DATA (sym)->as)
4074 {
4075 e->rank = CLASS_DATA (sym)->as->rank;
4076 e->corank = CLASS_DATA (sym)->as->corank;
4077 }
4078 else if (sym->as != NULL)
4079 {
4080 e->rank = sym->as->rank;
4081 e->corank = sym->as->corank;
4082 }
4083
4084 if (!sym->attr.function
4085 && !gfc_add_function (&sym->attr, sym->name, NULL))
4086 {
4087 m = MATCH_ERROR;
4088 break;
4089 }
4090
4091 /* Check here for the existence of at least one argument for the
4092 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. */
4093 if (sym->attr.is_iso_c == 1
4094 && (sym->from_intmod == INTMOD_ISO_C_BINDING
4095 && (sym->intmod_sym_id == ISOCBINDING_LOC
4096 || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
4097 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
4098 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
4099 {
4100 /* make sure we were given a param */
4101 if (actual_arglist == NULL)
4102 {
4103 gfc_error ("Missing argument to %qs at %C", sym->name);
4104 m = MATCH_ERROR;
4105 break;
4106 }
4107 }
4108
4109 if (sym->result == NULL)
4110 sym->result = sym;
4111
4112 gfc_gobble_whitespace ();
4113 /* F08:C612. */
4114 if (gfc_peek_ascii_char() == '%')
4115 {
4116 gfc_error ("The leftmost part-ref in a data-ref cannot be a "
4117 "function reference at %C");
4118 m = MATCH_ERROR;
4119 break;
4120 }
4121
4122 m = MATCH_YES;
4123 break;
4124
4125 case FL_UNKNOWN:
4126
4127 /* Special case for derived type variables that get their types
4128 via an IMPLICIT statement. This can't wait for the
4129 resolution phase. */
4130
4131 old_loc = gfc_current_locus;
4132 if (gfc_match_member_sep (sym) == MATCH_YES
4133 && sym->ts.type == BT_UNKNOWN
4134 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
4135 gfc_set_default_type (sym, 0, sym->ns);
4136 gfc_current_locus = old_loc;
4137
4138 /* If the symbol has a (co)dimension attribute, the expression is a
4139 variable. */
4140
4141 if (sym->attr.dimension || sym->attr.codimension)
4142 {
4143 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4144 {
4145 m = MATCH_ERROR;
4146 break;
4147 }
4148
4149 e = gfc_get_expr ();
4150 e->symtree = symtree;
4151 e->expr_type = EXPR_VARIABLE;
4152 m = gfc_match_varspec (e, 0, false, true);
4153 break;
4154 }
4155
4156 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
4157 && (CLASS_DATA (sym)->attr.dimension
4158 || CLASS_DATA (sym)->attr.codimension))
4159 {
4160 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4161 {
4162 m = MATCH_ERROR;
4163 break;
4164 }
4165
4166 e = gfc_get_expr ();
4167 e->symtree = symtree;
4168 e->expr_type = EXPR_VARIABLE;
4169 m = gfc_match_varspec (e, 0, false, true);
4170 break;
4171 }
4172
4173 /* Name is not an array, so we peek to see if a '(' implies a
4174 function call or a substring reference. Otherwise the
4175 variable is just a scalar. */
4176
4177 gfc_gobble_whitespace ();
4178 if (gfc_peek_ascii_char () != '(')
4179 {
4180 /* Assume a scalar variable */
4181 e = gfc_get_expr ();
4182 e->symtree = symtree;
4183 e->expr_type = EXPR_VARIABLE;
4184
4185 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
4186 {
4187 m = MATCH_ERROR;
4188 break;
4189 }
4190
4191 /*FIXME:??? gfc_match_varspec does set this for us: */
4192 e->ts = sym->ts;
4193 m = gfc_match_varspec (e, 0, false, true);
4194 break;
4195 }
4196
4197 /* See if this is a function reference with a keyword argument
4198 as first argument. We do this because otherwise a spurious
4199 symbol would end up in the symbol table. */
4200
4201 old_loc = gfc_current_locus;
4202 m2 = gfc_match (" ( %n =", argname);
4203 gfc_current_locus = old_loc;
4204
4205 e = gfc_get_expr ();
4206 e->symtree = symtree;
4207
4208 if (m2 != MATCH_YES)
4209 {
4210 /* Try to figure out whether we're dealing with a character type.
4211 We're peeking ahead here, because we don't want to call
4212 match_substring if we're dealing with an implicitly typed
4213 non-character variable. */
4214 implicit_char = false;
4215 if (sym->ts.type == BT_UNKNOWN)
4216 {
4217 ts = gfc_get_default_type (sym->name, NULL);
4218 if (ts->type == BT_CHARACTER)
4219 implicit_char = true;
4220 }
4221
4222 /* See if this could possibly be a substring reference of a name
4223 that we're not sure is a variable yet. */
4224
4225 if ((implicit_char || sym->ts.type == BT_CHARACTER)
4226 && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
4227 {
4228
4229 e->expr_type = EXPR_VARIABLE;
4230
4231 if (sym->attr.flavor != FL_VARIABLE
4232 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
4233 sym->name, NULL))
4234 {
4235 m = MATCH_ERROR;
4236 break;
4237 }
4238
4239 if (sym->ts.type == BT_UNKNOWN
4240 && !gfc_set_default_type (sym, 1, NULL))
4241 {
4242 m = MATCH_ERROR;
4243 break;
4244 }
4245
4246 e->ts = sym->ts;
4247 if (e->ref)
4248 e->ts.u.cl = NULL;
4249 m = MATCH_YES;
4250 break;
4251 }
4252 }
4253
4254 /* Give up, assume we have a function. */
4255
4256 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4257 sym = symtree->n.sym;
4258 e->expr_type = EXPR_FUNCTION;
4259
4260 if (!sym->attr.function
4261 && !gfc_add_function (&sym->attr, sym->name, NULL))
4262 {
4263 m = MATCH_ERROR;
4264 break;
4265 }
4266
4267 sym->result = sym;
4268
4269 m = gfc_match_actual_arglist (0, &e->value.function.actual);
4270 if (m == MATCH_NO)
4271 gfc_error ("Missing argument list in function %qs at %C", sym->name);
4272
4273 if (m != MATCH_YES)
4274 {
4275 m = MATCH_ERROR;
4276 break;
4277 }
4278
4279 /* If our new function returns a character, array or structure
4280 type, it might have subsequent references. */
4281
4282 m = gfc_match_varspec (e, 0, false, true);
4283 if (m == MATCH_NO)
4284 m = MATCH_YES;
4285
4286 break;
4287
4288 generic_function:
4289 /* Look for symbol first; if not found, look for STRUCTURE type symbol
4290 specially. Creates a generic symbol for derived types. */
4291 gfc_find_sym_tree (name, NULL, 1, &symtree);
4292 if (!symtree)
4293 gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
4294 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
4295 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
4296
4297 e = gfc_get_expr ();
4298 e->symtree = symtree;
4299 e->expr_type = EXPR_FUNCTION;
4300
4301 if (gfc_fl_struct (sym->attr.flavor))
4302 {
4303 e->value.function.esym = sym;
4304 e->symtree->n.sym->attr.generic = 1;
4305 }
4306
4307 m = gfc_match_actual_arglist (0, &e->value.function.actual);
4308 break;
4309
4310 case FL_NAMELIST:
4311 m = MATCH_ERROR;
4312 break;
4313
4314 default:
4315 gfc_error ("Symbol at %C is not appropriate for an expression");
4316 return MATCH_ERROR;
4317 }
4318
4319 /* Scan for possible inquiry references. */
4320 if (m == MATCH_YES
4321 && e->expr_type == EXPR_VARIABLE
4322 && gfc_peek_ascii_char () == '%')
4323 {
4324 m = gfc_match_varspec (e, 0, false, false);
4325 if (m == MATCH_NO)
4326 m = MATCH_YES;
4327 }
4328
4329 if (m == MATCH_YES)
4330 {
4331 e->where = where;
4332 *result = e;
4333 }
4334 else
4335 gfc_free_expr (e);
4336
4337 return m;
4338 }
4339
4340
4341 /* Match a variable, i.e. something that can be assigned to. This
4342 starts as a symbol, can be a structure component or an array
4343 reference. It can be a function if the function doesn't have a
4344 separate RESULT variable. If the symbol has not been previously
4345 seen, we assume it is a variable.
4346
4347 This function is called by two interface functions:
4348 gfc_match_variable, which has host_flag = 1, and
4349 gfc_match_equiv_variable, with host_flag = 0, to restrict the
4350 match of the symbol to the local scope. */
4351
4352 static match
4353 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
4354 {
4355 gfc_symbol *sym, *dt_sym;
4356 gfc_symtree *st;
4357 gfc_expr *expr;
4358 locus where, old_loc;
4359 match m;
4360
4361 *result = NULL;
4362
4363 /* Since nothing has any business being an lvalue in a module
4364 specification block, an interface block or a contains section,
4365 we force the changed_symbols mechanism to work by setting
4366 host_flag to 0. This prevents valid symbols that have the name
4367 of keywords, such as 'end', being turned into variables by
4368 failed matching to assignments for, e.g., END INTERFACE. */
4369 if (gfc_current_state () == COMP_MODULE
4370 || gfc_current_state () == COMP_SUBMODULE
4371 || gfc_current_state () == COMP_INTERFACE
4372 || gfc_current_state () == COMP_CONTAINS)
4373 host_flag = 0;
4374
4375 where = gfc_current_locus;
4376 m = gfc_match_sym_tree (&st, host_flag);
4377 if (m != MATCH_YES)
4378 return m;
4379
4380 sym = st->n.sym;
4381
4382 /* If this is an implicit do loop index and implicitly typed,
4383 it should not be host associated. */
4384 m = check_for_implicit_index (&st, &sym);
4385 if (m != MATCH_YES)
4386 return m;
4387
4388 sym->attr.implied_index = 0;
4389
4390 gfc_set_sym_referenced (sym);
4391
4392 /* STRUCTUREs may share names with variables, but derived types may not. */
4393 if (sym->attr.flavor == FL_PROCEDURE && sym->generic
4394 && (dt_sym = gfc_find_dt_in_generic (sym)))
4395 {
4396 if (dt_sym->attr.flavor == FL_DERIVED)
4397 gfc_error ("Derived type %qs cannot be used as a variable at %C",
4398 sym->name);
4399 return MATCH_ERROR;
4400 }
4401
4402 switch (sym->attr.flavor)
4403 {
4404 case FL_VARIABLE:
4405 /* Everything is alright. */
4406 break;
4407
4408 case FL_UNKNOWN:
4409 {
4410 sym_flavor flavor = FL_UNKNOWN;
4411
4412 gfc_gobble_whitespace ();
4413
4414 if (sym->attr.external || sym->attr.procedure
4415 || sym->attr.function || sym->attr.subroutine)
4416 flavor = FL_PROCEDURE;
4417
4418 /* If it is not a procedure, is not typed and is host associated,
4419 we cannot give it a flavor yet. */
4420 else if (sym->ns == gfc_current_ns->parent
4421 && sym->ts.type == BT_UNKNOWN)
4422 break;
4423
4424 /* These are definitive indicators that this is a variable. */
4425 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4426 || sym->attr.pointer || sym->as != NULL)
4427 flavor = FL_VARIABLE;
4428
4429 if (flavor != FL_UNKNOWN
4430 && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4431 return MATCH_ERROR;
4432 }
4433 break;
4434
4435 case FL_PARAMETER:
4436 if (equiv_flag)
4437 {
4438 gfc_error ("Named constant at %C in an EQUIVALENCE");
4439 return MATCH_ERROR;
4440 }
4441 if (gfc_in_match_data())
4442 {
4443 gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
4444 sym->name);
4445 return MATCH_ERROR;
4446 }
4447 /* Otherwise this is checked for an error given in the
4448 variable definition context checks. */
4449 break;
4450
4451 case FL_PROCEDURE:
4452 /* Check for a nonrecursive function result variable. */
4453 if (sym->attr.function
4454 && (!sym->attr.external || sym->abr_modproc_decl)
4455 && sym->result == sym
4456 && (gfc_is_function_return_value (sym, gfc_current_ns)
4457 || (sym->attr.entry
4458 && sym->ns == gfc_current_ns)
4459 || (sym->attr.entry
4460 && sym->ns == gfc_current_ns->parent)))
4461 {
4462 /* If a function result is a derived type, then the derived
4463 type may still have to be resolved. */
4464
4465 if (sym->ts.type == BT_DERIVED
4466 && gfc_use_derived (sym->ts.u.derived) == NULL)
4467 return MATCH_ERROR;
4468 break;
4469 }
4470
4471 if (sym->attr.proc_pointer
4472 || replace_hidden_procptr_result (&sym, &st))
4473 break;
4474
4475 /* Fall through to error */
4476 gcc_fallthrough ();
4477
4478 default:
4479 gfc_error ("%qs at %C is not a variable", sym->name);
4480 return MATCH_ERROR;
4481 }
4482
4483 /* Special case for derived type variables that get their types
4484 via an IMPLICIT statement. This can't wait for the
4485 resolution phase. */
4486
4487 {
4488 gfc_namespace * implicit_ns;
4489
4490 if (gfc_current_ns->proc_name == sym)
4491 implicit_ns = gfc_current_ns;
4492 else
4493 implicit_ns = sym->ns;
4494
4495 old_loc = gfc_current_locus;
4496 if (gfc_match_member_sep (sym) == MATCH_YES
4497 && sym->ts.type == BT_UNKNOWN
4498 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4499 gfc_set_default_type (sym, 0, implicit_ns);
4500 gfc_current_locus = old_loc;
4501 }
4502
4503 expr = gfc_get_expr ();
4504
4505 expr->expr_type = EXPR_VARIABLE;
4506 expr->symtree = st;
4507 expr->ts = sym->ts;
4508
4509 /* Now see if we have to do more. */
4510 m = gfc_match_varspec (expr, equiv_flag, false, false);
4511 if (m != MATCH_YES)
4512 {
4513 gfc_free_expr (expr);
4514 return m;
4515 }
4516
4517 expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
4518 *result = expr;
4519 return MATCH_YES;
4520 }
4521
4522
4523 match
4524 gfc_match_variable (gfc_expr **result, int equiv_flag)
4525 {
4526 return match_variable (result, equiv_flag, 1);
4527 }
4528
4529
4530 match
4531 gfc_match_equiv_variable (gfc_expr **result)
4532 {
4533 return match_variable (result, 1, 0);
4534 }