67105cc9ab1f2d02a59a983a19b5072c71a00270
[gcc.git] / gcc / fortran / primary.c
1 /* Primary expression subroutines
2 Copyright (C) 2000-2020 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);
49 if (m != MATCH_NO)
50 return m;
51
52 m = gfc_match_name (name);
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 ('_') != 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 int
113 gfc_check_digit (char c, int radix)
114 {
115 int 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 a real string to an expression node. */
213
214 static gfc_expr *
215 convert_real (const char *buffer, int kind, locus *where)
216 {
217 gfc_expr *e;
218
219 e = gfc_get_constant_expr (BT_REAL, kind, where);
220 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
221
222 return e;
223 }
224
225
226 /* Convert a pair of real, constant expression nodes to a single
227 complex expression node. */
228
229 static gfc_expr *
230 convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
231 {
232 gfc_expr *e;
233
234 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
235 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
236 GFC_MPC_RND_MODE);
237
238 return e;
239 }
240
241
242 /* Match an integer (digit string and optional kind).
243 A sign will be accepted if signflag is set. */
244
245 static match
246 match_integer_constant (gfc_expr **result, int signflag)
247 {
248 int length, kind, is_iso_c;
249 locus old_loc;
250 char *buffer;
251 gfc_expr *e;
252
253 old_loc = gfc_current_locus;
254 gfc_gobble_whitespace ();
255
256 length = match_digits (signflag, 10, NULL);
257 gfc_current_locus = old_loc;
258 if (length == -1)
259 return MATCH_NO;
260
261 buffer = (char *) alloca (length + 1);
262 memset (buffer, '\0', length + 1);
263
264 gfc_gobble_whitespace ();
265
266 match_digits (signflag, 10, buffer);
267
268 kind = get_kind (&is_iso_c);
269 if (kind == -2)
270 kind = gfc_default_integer_kind;
271 if (kind == -1)
272 return MATCH_ERROR;
273
274 if (kind == 4 && flag_integer4_kind == 8)
275 kind = 8;
276
277 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
278 {
279 gfc_error ("Integer kind %d at %C not available", kind);
280 return MATCH_ERROR;
281 }
282
283 e = convert_integer (buffer, kind, 10, &gfc_current_locus);
284 e->ts.is_c_interop = is_iso_c;
285
286 if (gfc_range_check (e) != ARITH_OK)
287 {
288 gfc_error ("Integer too big for its kind at %C. This check can be "
289 "disabled with the option %<-fno-range-check%>");
290
291 gfc_free_expr (e);
292 return MATCH_ERROR;
293 }
294
295 *result = e;
296 return MATCH_YES;
297 }
298
299
300 /* Match a Hollerith constant. */
301
302 static match
303 match_hollerith_constant (gfc_expr **result)
304 {
305 locus old_loc;
306 gfc_expr *e = NULL;
307 int num, pad;
308 int i;
309
310 old_loc = gfc_current_locus;
311 gfc_gobble_whitespace ();
312
313 if (match_integer_constant (&e, 0) == MATCH_YES
314 && gfc_match_char ('h') == MATCH_YES)
315 {
316 if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
317 goto cleanup;
318
319 if (gfc_extract_int (e, &num, 1))
320 goto cleanup;
321 if (num == 0)
322 {
323 gfc_error ("Invalid Hollerith constant: %L must contain at least "
324 "one character", &old_loc);
325 goto cleanup;
326 }
327 if (e->ts.kind != gfc_default_integer_kind)
328 {
329 gfc_error ("Invalid Hollerith constant: Integer kind at %L "
330 "should be default", &old_loc);
331 goto cleanup;
332 }
333 else
334 {
335 gfc_free_expr (e);
336 e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
337 &gfc_current_locus);
338
339 /* Calculate padding needed to fit default integer memory. */
340 pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
341
342 e->representation.string = XCNEWVEC (char, num + pad + 1);
343
344 for (i = 0; i < num; i++)
345 {
346 gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
347 if (! gfc_wide_fits_in_byte (c))
348 {
349 gfc_error ("Invalid Hollerith constant at %L contains a "
350 "wide character", &old_loc);
351 goto cleanup;
352 }
353
354 e->representation.string[i] = (unsigned char) c;
355 }
356
357 /* Now pad with blanks and end with a null char. */
358 for (i = 0; i < pad; i++)
359 e->representation.string[num + i] = ' ';
360
361 e->representation.string[num + i] = '\0';
362 e->representation.length = num + pad;
363 e->ts.u.pad = pad;
364
365 *result = e;
366 return MATCH_YES;
367 }
368 }
369
370 gfc_free_expr (e);
371 gfc_current_locus = old_loc;
372 return MATCH_NO;
373
374 cleanup:
375 gfc_free_expr (e);
376 return MATCH_ERROR;
377 }
378
379
380 /* Match a binary, octal or hexadecimal constant that can be found in
381 a DATA statement. The standard permits b'010...', o'73...', and
382 z'a1...' where b, o, and z can be capital letters. This function
383 also accepts postfixed forms of the constants: '01...'b, '73...'o,
384 and 'a1...'z. An additional extension is the use of x for z. */
385
386 static match
387 match_boz_constant (gfc_expr **result)
388 {
389 int radix, length, x_hex;
390 locus old_loc, start_loc;
391 char *buffer, post, delim;
392 gfc_expr *e;
393
394 start_loc = old_loc = gfc_current_locus;
395 gfc_gobble_whitespace ();
396
397 x_hex = 0;
398 switch (post = gfc_next_ascii_char ())
399 {
400 case 'b':
401 radix = 2;
402 post = 0;
403 break;
404 case 'o':
405 radix = 8;
406 post = 0;
407 break;
408 case 'x':
409 x_hex = 1;
410 /* Fall through. */
411 case 'z':
412 radix = 16;
413 post = 0;
414 break;
415 case '\'':
416 /* Fall through. */
417 case '\"':
418 delim = post;
419 post = 1;
420 radix = 16; /* Set to accept any valid digit string. */
421 break;
422 default:
423 goto backup;
424 }
425
426 /* No whitespace allowed here. */
427
428 if (post == 0)
429 delim = gfc_next_ascii_char ();
430
431 if (delim != '\'' && delim != '\"')
432 goto backup;
433
434 if (x_hex
435 && gfc_invalid_boz ("Hexadecimal constant at %L uses "
436 "nonstandard X instead of Z", &gfc_current_locus))
437 return MATCH_ERROR;
438
439 old_loc = gfc_current_locus;
440
441 length = match_digits (0, radix, NULL);
442 if (length == -1)
443 {
444 gfc_error ("Empty set of digits in BOZ constant at %C");
445 return MATCH_ERROR;
446 }
447
448 if (gfc_next_ascii_char () != delim)
449 {
450 gfc_error ("Illegal character in BOZ constant at %C");
451 return MATCH_ERROR;
452 }
453
454 if (post == 1)
455 {
456 switch (gfc_next_ascii_char ())
457 {
458 case 'b':
459 radix = 2;
460 break;
461 case 'o':
462 radix = 8;
463 break;
464 case 'x':
465 /* Fall through. */
466 case 'z':
467 radix = 16;
468 break;
469 default:
470 goto backup;
471 }
472
473 if (gfc_invalid_boz ("BOZ constant at %C uses nonstandard postfix "
474 "syntax", &gfc_current_locus))
475 return MATCH_ERROR;
476 }
477
478 gfc_current_locus = old_loc;
479
480 buffer = (char *) alloca (length + 1);
481 memset (buffer, '\0', length + 1);
482
483 match_digits (0, radix, buffer);
484 gfc_next_ascii_char (); /* Eat delimiter. */
485 if (post == 1)
486 gfc_next_ascii_char (); /* Eat postfixed b, o, z, or x. */
487
488 e = gfc_get_expr ();
489 e->expr_type = EXPR_CONSTANT;
490 e->ts.type = BT_BOZ;
491 e->where = gfc_current_locus;
492 e->boz.rdx = radix;
493 e->boz.len = length;
494 e->boz.str = XCNEWVEC (char, length + 1);
495 strncpy (e->boz.str, buffer, length);
496
497 if (!gfc_in_match_data ()
498 && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
499 "statement at %L", &e->where)))
500 return MATCH_ERROR;
501
502 *result = e;
503 return MATCH_YES;
504
505 backup:
506 gfc_current_locus = start_loc;
507 return MATCH_NO;
508 }
509
510
511 /* Match a real constant of some sort. Allow a signed constant if signflag
512 is nonzero. */
513
514 static match
515 match_real_constant (gfc_expr **result, int signflag)
516 {
517 int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
518 locus old_loc, temp_loc;
519 char *p, *buffer, c, exp_char;
520 gfc_expr *e;
521 bool negate;
522
523 old_loc = gfc_current_locus;
524 gfc_gobble_whitespace ();
525
526 e = NULL;
527
528 default_exponent = 0;
529 count = 0;
530 seen_dp = 0;
531 seen_digits = 0;
532 exp_char = ' ';
533 negate = FALSE;
534
535 c = gfc_next_ascii_char ();
536 if (signflag && (c == '+' || c == '-'))
537 {
538 if (c == '-')
539 negate = TRUE;
540
541 gfc_gobble_whitespace ();
542 c = gfc_next_ascii_char ();
543 }
544
545 /* Scan significand. */
546 for (;; c = gfc_next_ascii_char (), count++)
547 {
548 if (c == '.')
549 {
550 if (seen_dp)
551 goto done;
552
553 /* Check to see if "." goes with a following operator like
554 ".eq.". */
555 temp_loc = gfc_current_locus;
556 c = gfc_next_ascii_char ();
557
558 if (c == 'e' || c == 'd' || c == 'q')
559 {
560 c = gfc_next_ascii_char ();
561 if (c == '.')
562 goto done; /* Operator named .e. or .d. */
563 }
564
565 if (ISALPHA (c))
566 goto done; /* Distinguish 1.e9 from 1.eq.2 */
567
568 gfc_current_locus = temp_loc;
569 seen_dp = 1;
570 continue;
571 }
572
573 if (ISDIGIT (c))
574 {
575 seen_digits = 1;
576 continue;
577 }
578
579 break;
580 }
581
582 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
583 goto done;
584 exp_char = c;
585
586
587 if (c == 'q')
588 {
589 if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter 'q' in "
590 "real-literal-constant at %C"))
591 return MATCH_ERROR;
592 else if (warn_real_q_constant)
593 gfc_warning (OPT_Wreal_q_constant,
594 "Extension: exponent-letter %<q%> in real-literal-constant "
595 "at %C");
596 }
597
598 /* Scan exponent. */
599 c = gfc_next_ascii_char ();
600 count++;
601
602 if (c == '+' || c == '-')
603 { /* optional sign */
604 c = gfc_next_ascii_char ();
605 count++;
606 }
607
608 if (!ISDIGIT (c))
609 {
610 /* With -fdec, default exponent to 0 instead of complaining. */
611 if (flag_dec)
612 default_exponent = 1;
613 else
614 {
615 gfc_error ("Missing exponent in real number at %C");
616 return MATCH_ERROR;
617 }
618 }
619
620 while (ISDIGIT (c))
621 {
622 c = gfc_next_ascii_char ();
623 count++;
624 }
625
626 done:
627 /* Check that we have a numeric constant. */
628 if (!seen_digits || (!seen_dp && exp_char == ' '))
629 {
630 gfc_current_locus = old_loc;
631 return MATCH_NO;
632 }
633
634 /* Convert the number. */
635 gfc_current_locus = old_loc;
636 gfc_gobble_whitespace ();
637
638 buffer = (char *) alloca (count + default_exponent + 1);
639 memset (buffer, '\0', count + default_exponent + 1);
640
641 p = buffer;
642 c = gfc_next_ascii_char ();
643 if (c == '+' || c == '-')
644 {
645 gfc_gobble_whitespace ();
646 c = gfc_next_ascii_char ();
647 }
648
649 /* Hack for mpfr_set_str(). */
650 for (;;)
651 {
652 if (c == 'd' || c == 'q')
653 *p = 'e';
654 else
655 *p = c;
656 p++;
657 if (--count == 0)
658 break;
659
660 c = gfc_next_ascii_char ();
661 }
662 if (default_exponent)
663 *p++ = '0';
664
665 kind = get_kind (&is_iso_c);
666 if (kind == -1)
667 goto cleanup;
668
669 switch (exp_char)
670 {
671 case 'd':
672 if (kind != -2)
673 {
674 gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
675 "kind");
676 goto cleanup;
677 }
678 kind = gfc_default_double_kind;
679
680 if (kind == 4)
681 {
682 if (flag_real4_kind == 8)
683 kind = 8;
684 if (flag_real4_kind == 10)
685 kind = 10;
686 if (flag_real4_kind == 16)
687 kind = 16;
688 }
689
690 if (kind == 8)
691 {
692 if (flag_real8_kind == 4)
693 kind = 4;
694 if (flag_real8_kind == 10)
695 kind = 10;
696 if (flag_real8_kind == 16)
697 kind = 16;
698 }
699 break;
700
701 case 'q':
702 if (kind != -2)
703 {
704 gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
705 "kind");
706 goto cleanup;
707 }
708
709 /* The maximum possible real kind type parameter is 16. First, try
710 that for the kind, then fallback to trying kind=10 (Intel 80 bit)
711 extended precision. If neither value works, just given up. */
712 kind = 16;
713 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
714 {
715 kind = 10;
716 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
717 {
718 gfc_error ("Invalid exponent-letter %<q%> in "
719 "real-literal-constant at %C");
720 goto cleanup;
721 }
722 }
723 break;
724
725 default:
726 if (kind == -2)
727 kind = gfc_default_real_kind;
728
729 if (kind == 4)
730 {
731 if (flag_real4_kind == 8)
732 kind = 8;
733 if (flag_real4_kind == 10)
734 kind = 10;
735 if (flag_real4_kind == 16)
736 kind = 16;
737 }
738
739 if (kind == 8)
740 {
741 if (flag_real8_kind == 4)
742 kind = 4;
743 if (flag_real8_kind == 10)
744 kind = 10;
745 if (flag_real8_kind == 16)
746 kind = 16;
747 }
748
749 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
750 {
751 gfc_error ("Invalid real kind %d at %C", kind);
752 goto cleanup;
753 }
754 }
755
756 e = convert_real (buffer, kind, &gfc_current_locus);
757 if (negate)
758 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
759 e->ts.is_c_interop = is_iso_c;
760
761 switch (gfc_range_check (e))
762 {
763 case ARITH_OK:
764 break;
765 case ARITH_OVERFLOW:
766 gfc_error ("Real constant overflows its kind at %C");
767 goto cleanup;
768
769 case ARITH_UNDERFLOW:
770 if (warn_underflow)
771 gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
772 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
773 break;
774
775 default:
776 gfc_internal_error ("gfc_range_check() returned bad value");
777 }
778
779 /* Warn about trailing digits which suggest the user added too many
780 trailing digits, which may cause the appearance of higher pecision
781 than the kind kan support.
782
783 This is done by replacing the rightmost non-zero digit with zero
784 and comparing with the original value. If these are equal, we
785 assume the user supplied more digits than intended (or forgot to
786 convert to the correct kind).
787 */
788
789 if (warn_conversion_extra)
790 {
791 mpfr_t r;
792 char *c1;
793 bool did_break;
794
795 c1 = strchr (buffer, 'e');
796 if (c1 == NULL)
797 c1 = buffer + strlen(buffer);
798
799 did_break = false;
800 for (p = c1; p > buffer;)
801 {
802 p--;
803 if (*p == '.')
804 continue;
805
806 if (*p != '0')
807 {
808 *p = '0';
809 did_break = true;
810 break;
811 }
812 }
813
814 if (did_break)
815 {
816 mpfr_init (r);
817 mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
818 if (negate)
819 mpfr_neg (r, r, GFC_RND_MODE);
820
821 mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
822
823 if (mpfr_cmp_ui (r, 0) == 0)
824 gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
825 "in %qs number at %C, maybe incorrect KIND",
826 gfc_typename (&e->ts));
827
828 mpfr_clear (r);
829 }
830 }
831
832 *result = e;
833 return MATCH_YES;
834
835 cleanup:
836 gfc_free_expr (e);
837 return MATCH_ERROR;
838 }
839
840
841 /* Match a substring reference. */
842
843 static match
844 match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
845 {
846 gfc_expr *start, *end;
847 locus old_loc;
848 gfc_ref *ref;
849 match m;
850
851 start = NULL;
852 end = NULL;
853
854 old_loc = gfc_current_locus;
855
856 m = gfc_match_char ('(');
857 if (m != MATCH_YES)
858 return MATCH_NO;
859
860 if (gfc_match_char (':') != MATCH_YES)
861 {
862 if (init)
863 m = gfc_match_init_expr (&start);
864 else
865 m = gfc_match_expr (&start);
866
867 if (m != MATCH_YES)
868 {
869 m = MATCH_NO;
870 goto cleanup;
871 }
872
873 m = gfc_match_char (':');
874 if (m != MATCH_YES)
875 goto cleanup;
876 }
877
878 if (gfc_match_char (')') != MATCH_YES)
879 {
880 if (init)
881 m = gfc_match_init_expr (&end);
882 else
883 m = gfc_match_expr (&end);
884
885 if (m == MATCH_NO)
886 goto syntax;
887 if (m == MATCH_ERROR)
888 goto cleanup;
889
890 m = gfc_match_char (')');
891 if (m == MATCH_NO)
892 goto syntax;
893 }
894
895 /* Optimize away the (:) reference. */
896 if (start == NULL && end == NULL && !deferred)
897 ref = NULL;
898 else
899 {
900 ref = gfc_get_ref ();
901
902 ref->type = REF_SUBSTRING;
903 if (start == NULL)
904 start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
905 ref->u.ss.start = start;
906 if (end == NULL && cl)
907 end = gfc_copy_expr (cl->length);
908 ref->u.ss.end = end;
909 ref->u.ss.length = cl;
910 }
911
912 *result = ref;
913 return MATCH_YES;
914
915 syntax:
916 gfc_error ("Syntax error in SUBSTRING specification at %C");
917 m = MATCH_ERROR;
918
919 cleanup:
920 gfc_free_expr (start);
921 gfc_free_expr (end);
922
923 gfc_current_locus = old_loc;
924 return m;
925 }
926
927
928 /* Reads the next character of a string constant, taking care to
929 return doubled delimiters on the input as a single instance of
930 the delimiter.
931
932 Special return values for "ret" argument are:
933 -1 End of the string, as determined by the delimiter
934 -2 Unterminated string detected
935
936 Backslash codes are also expanded at this time. */
937
938 static gfc_char_t
939 next_string_char (gfc_char_t delimiter, int *ret)
940 {
941 locus old_locus;
942 gfc_char_t c;
943
944 c = gfc_next_char_literal (INSTRING_WARN);
945 *ret = 0;
946
947 if (c == '\n')
948 {
949 *ret = -2;
950 return 0;
951 }
952
953 if (flag_backslash && c == '\\')
954 {
955 old_locus = gfc_current_locus;
956
957 if (gfc_match_special_char (&c) == MATCH_NO)
958 gfc_current_locus = old_locus;
959
960 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
961 gfc_warning (0, "Extension: backslash character at %C");
962 }
963
964 if (c != delimiter)
965 return c;
966
967 old_locus = gfc_current_locus;
968 c = gfc_next_char_literal (NONSTRING);
969
970 if (c == delimiter)
971 return c;
972 gfc_current_locus = old_locus;
973
974 *ret = -1;
975 return 0;
976 }
977
978
979 /* Special case of gfc_match_name() that matches a parameter kind name
980 before a string constant. This takes case of the weird but legal
981 case of:
982
983 kind_____'string'
984
985 where kind____ is a parameter. gfc_match_name() will happily slurp
986 up all the underscores, which leads to problems. If we return
987 MATCH_YES, the parse pointer points to the final underscore, which
988 is not part of the name. We never return MATCH_ERROR-- errors in
989 the name will be detected later. */
990
991 static match
992 match_charkind_name (char *name)
993 {
994 locus old_loc;
995 char c, peek;
996 int len;
997
998 gfc_gobble_whitespace ();
999 c = gfc_next_ascii_char ();
1000 if (!ISALPHA (c))
1001 return MATCH_NO;
1002
1003 *name++ = c;
1004 len = 1;
1005
1006 for (;;)
1007 {
1008 old_loc = gfc_current_locus;
1009 c = gfc_next_ascii_char ();
1010
1011 if (c == '_')
1012 {
1013 peek = gfc_peek_ascii_char ();
1014
1015 if (peek == '\'' || peek == '\"')
1016 {
1017 gfc_current_locus = old_loc;
1018 *name = '\0';
1019 return MATCH_YES;
1020 }
1021 }
1022
1023 if (!ISALNUM (c)
1024 && c != '_'
1025 && (c != '$' || !flag_dollar_ok))
1026 break;
1027
1028 *name++ = c;
1029 if (++len > GFC_MAX_SYMBOL_LEN)
1030 break;
1031 }
1032
1033 return MATCH_NO;
1034 }
1035
1036
1037 /* See if the current input matches a character constant. Lots of
1038 contortions have to be done to match the kind parameter which comes
1039 before the actual string. The main consideration is that we don't
1040 want to error out too quickly. For example, we don't actually do
1041 any validation of the kinds until we have actually seen a legal
1042 delimiter. Using match_kind_param() generates errors too quickly. */
1043
1044 static match
1045 match_string_constant (gfc_expr **result)
1046 {
1047 char name[GFC_MAX_SYMBOL_LEN + 1], peek;
1048 size_t length;
1049 int kind,save_warn_ampersand, ret;
1050 locus old_locus, start_locus;
1051 gfc_symbol *sym;
1052 gfc_expr *e;
1053 match m;
1054 gfc_char_t c, delimiter, *p;
1055
1056 old_locus = gfc_current_locus;
1057
1058 gfc_gobble_whitespace ();
1059
1060 c = gfc_next_char ();
1061 if (c == '\'' || c == '"')
1062 {
1063 kind = gfc_default_character_kind;
1064 start_locus = gfc_current_locus;
1065 goto got_delim;
1066 }
1067
1068 if (gfc_wide_is_digit (c))
1069 {
1070 kind = 0;
1071
1072 while (gfc_wide_is_digit (c))
1073 {
1074 kind = kind * 10 + c - '0';
1075 if (kind > 9999999)
1076 goto no_match;
1077 c = gfc_next_char ();
1078 }
1079
1080 }
1081 else
1082 {
1083 gfc_current_locus = old_locus;
1084
1085 m = match_charkind_name (name);
1086 if (m != MATCH_YES)
1087 goto no_match;
1088
1089 if (gfc_find_symbol (name, NULL, 1, &sym)
1090 || sym == NULL
1091 || sym->attr.flavor != FL_PARAMETER)
1092 goto no_match;
1093
1094 kind = -1;
1095 c = gfc_next_char ();
1096 }
1097
1098 if (c == ' ')
1099 {
1100 gfc_gobble_whitespace ();
1101 c = gfc_next_char ();
1102 }
1103
1104 if (c != '_')
1105 goto no_match;
1106
1107 gfc_gobble_whitespace ();
1108
1109 c = gfc_next_char ();
1110 if (c != '\'' && c != '"')
1111 goto no_match;
1112
1113 start_locus = gfc_current_locus;
1114
1115 if (kind == -1)
1116 {
1117 if (gfc_extract_int (sym->value, &kind, 1))
1118 return MATCH_ERROR;
1119 gfc_set_sym_referenced (sym);
1120 }
1121
1122 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1123 {
1124 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
1125 return MATCH_ERROR;
1126 }
1127
1128 got_delim:
1129 /* Scan the string into a block of memory by first figuring out how
1130 long it is, allocating the structure, then re-reading it. This
1131 isn't particularly efficient, but string constants aren't that
1132 common in most code. TODO: Use obstacks? */
1133
1134 delimiter = c;
1135 length = 0;
1136
1137 for (;;)
1138 {
1139 c = next_string_char (delimiter, &ret);
1140 if (ret == -1)
1141 break;
1142 if (ret == -2)
1143 {
1144 gfc_current_locus = start_locus;
1145 gfc_error ("Unterminated character constant beginning at %C");
1146 return MATCH_ERROR;
1147 }
1148
1149 length++;
1150 }
1151
1152 /* Peek at the next character to see if it is a b, o, z, or x for the
1153 postfixed BOZ literal constants. */
1154 peek = gfc_peek_ascii_char ();
1155 if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1156 goto no_match;
1157
1158 e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1159
1160 gfc_current_locus = start_locus;
1161
1162 /* We disable the warning for the following loop as the warning has already
1163 been printed in the loop above. */
1164 save_warn_ampersand = warn_ampersand;
1165 warn_ampersand = false;
1166
1167 p = e->value.character.string;
1168 for (size_t i = 0; i < length; i++)
1169 {
1170 c = next_string_char (delimiter, &ret);
1171
1172 if (!gfc_check_character_range (c, kind))
1173 {
1174 gfc_free_expr (e);
1175 gfc_error ("Character %qs in string at %C is not representable "
1176 "in character kind %d", gfc_print_wide_char (c), kind);
1177 return MATCH_ERROR;
1178 }
1179
1180 *p++ = c;
1181 }
1182
1183 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
1184 warn_ampersand = save_warn_ampersand;
1185
1186 next_string_char (delimiter, &ret);
1187 if (ret != -1)
1188 gfc_internal_error ("match_string_constant(): Delimiter not found");
1189
1190 if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
1191 e->expr_type = EXPR_SUBSTRING;
1192
1193 *result = e;
1194
1195 return MATCH_YES;
1196
1197 no_match:
1198 gfc_current_locus = old_locus;
1199 return MATCH_NO;
1200 }
1201
1202
1203 /* Match a .true. or .false. Returns 1 if a .true. was found,
1204 0 if a .false. was found, and -1 otherwise. */
1205 static int
1206 match_logical_constant_string (void)
1207 {
1208 locus orig_loc = gfc_current_locus;
1209
1210 gfc_gobble_whitespace ();
1211 if (gfc_next_ascii_char () == '.')
1212 {
1213 char ch = gfc_next_ascii_char ();
1214 if (ch == 'f')
1215 {
1216 if (gfc_next_ascii_char () == 'a'
1217 && gfc_next_ascii_char () == 'l'
1218 && gfc_next_ascii_char () == 's'
1219 && gfc_next_ascii_char () == 'e'
1220 && gfc_next_ascii_char () == '.')
1221 /* Matched ".false.". */
1222 return 0;
1223 }
1224 else if (ch == 't')
1225 {
1226 if (gfc_next_ascii_char () == 'r'
1227 && gfc_next_ascii_char () == 'u'
1228 && gfc_next_ascii_char () == 'e'
1229 && gfc_next_ascii_char () == '.')
1230 /* Matched ".true.". */
1231 return 1;
1232 }
1233 }
1234 gfc_current_locus = orig_loc;
1235 return -1;
1236 }
1237
1238 /* Match a .true. or .false. */
1239
1240 static match
1241 match_logical_constant (gfc_expr **result)
1242 {
1243 gfc_expr *e;
1244 int i, kind, is_iso_c;
1245
1246 i = match_logical_constant_string ();
1247 if (i == -1)
1248 return MATCH_NO;
1249
1250 kind = get_kind (&is_iso_c);
1251 if (kind == -1)
1252 return MATCH_ERROR;
1253 if (kind == -2)
1254 kind = gfc_default_logical_kind;
1255
1256 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1257 {
1258 gfc_error ("Bad kind for logical constant at %C");
1259 return MATCH_ERROR;
1260 }
1261
1262 e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1263 e->ts.is_c_interop = is_iso_c;
1264
1265 *result = e;
1266 return MATCH_YES;
1267 }
1268
1269
1270 /* Match a real or imaginary part of a complex constant that is a
1271 symbolic constant. */
1272
1273 static match
1274 match_sym_complex_part (gfc_expr **result)
1275 {
1276 char name[GFC_MAX_SYMBOL_LEN + 1];
1277 gfc_symbol *sym;
1278 gfc_expr *e;
1279 match m;
1280
1281 m = gfc_match_name (name);
1282 if (m != MATCH_YES)
1283 return m;
1284
1285 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1286 return MATCH_NO;
1287
1288 if (sym->attr.flavor != FL_PARAMETER)
1289 {
1290 /* Give the matcher for implied do-loops a chance to run. This yields
1291 a much saner error message for "write(*,*) (i, i=1, 6" where the
1292 right parenthesis is missing. */
1293 char c;
1294 gfc_gobble_whitespace ();
1295 c = gfc_peek_ascii_char ();
1296 if (c == '=' || c == ',')
1297 {
1298 m = MATCH_NO;
1299 }
1300 else
1301 {
1302 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1303 m = MATCH_ERROR;
1304 }
1305 return m;
1306 }
1307
1308 if (!sym->value)
1309 goto error;
1310
1311 if (!gfc_numeric_ts (&sym->value->ts))
1312 {
1313 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1314 return MATCH_ERROR;
1315 }
1316
1317 if (sym->value->rank != 0)
1318 {
1319 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1320 return MATCH_ERROR;
1321 }
1322
1323 if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1324 "complex constant at %C"))
1325 return MATCH_ERROR;
1326
1327 switch (sym->value->ts.type)
1328 {
1329 case BT_REAL:
1330 e = gfc_copy_expr (sym->value);
1331 break;
1332
1333 case BT_COMPLEX:
1334 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1335 if (e == NULL)
1336 goto error;
1337 break;
1338
1339 case BT_INTEGER:
1340 e = gfc_int2real (sym->value, gfc_default_real_kind);
1341 if (e == NULL)
1342 goto error;
1343 break;
1344
1345 default:
1346 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1347 }
1348
1349 *result = e; /* e is a scalar, real, constant expression. */
1350 return MATCH_YES;
1351
1352 error:
1353 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1354 return MATCH_ERROR;
1355 }
1356
1357
1358 /* Match a real or imaginary part of a complex number. */
1359
1360 static match
1361 match_complex_part (gfc_expr **result)
1362 {
1363 match m;
1364
1365 m = match_sym_complex_part (result);
1366 if (m != MATCH_NO)
1367 return m;
1368
1369 m = match_real_constant (result, 1);
1370 if (m != MATCH_NO)
1371 return m;
1372
1373 return match_integer_constant (result, 1);
1374 }
1375
1376
1377 /* Try to match a complex constant. */
1378
1379 static match
1380 match_complex_constant (gfc_expr **result)
1381 {
1382 gfc_expr *e, *real, *imag;
1383 gfc_error_buffer old_error;
1384 gfc_typespec target;
1385 locus old_loc;
1386 int kind;
1387 match m;
1388
1389 old_loc = gfc_current_locus;
1390 real = imag = e = NULL;
1391
1392 m = gfc_match_char ('(');
1393 if (m != MATCH_YES)
1394 return m;
1395
1396 gfc_push_error (&old_error);
1397
1398 m = match_complex_part (&real);
1399 if (m == MATCH_NO)
1400 {
1401 gfc_free_error (&old_error);
1402 goto cleanup;
1403 }
1404
1405 if (gfc_match_char (',') == MATCH_NO)
1406 {
1407 /* It is possible that gfc_int2real issued a warning when
1408 converting an integer to real. Throw this away here. */
1409
1410 gfc_clear_warning ();
1411 gfc_pop_error (&old_error);
1412 m = MATCH_NO;
1413 goto cleanup;
1414 }
1415
1416 /* If m is error, then something was wrong with the real part and we
1417 assume we have a complex constant because we've seen the ','. An
1418 ambiguous case here is the start of an iterator list of some
1419 sort. These sort of lists are matched prior to coming here. */
1420
1421 if (m == MATCH_ERROR)
1422 {
1423 gfc_free_error (&old_error);
1424 goto cleanup;
1425 }
1426 gfc_pop_error (&old_error);
1427
1428 m = match_complex_part (&imag);
1429 if (m == MATCH_NO)
1430 goto syntax;
1431 if (m == MATCH_ERROR)
1432 goto cleanup;
1433
1434 m = gfc_match_char (')');
1435 if (m == MATCH_NO)
1436 {
1437 /* Give the matcher for implied do-loops a chance to run. This
1438 yields a much saner error message for (/ (i, 4=i, 6) /). */
1439 if (gfc_peek_ascii_char () == '=')
1440 {
1441 m = MATCH_ERROR;
1442 goto cleanup;
1443 }
1444 else
1445 goto syntax;
1446 }
1447
1448 if (m == MATCH_ERROR)
1449 goto cleanup;
1450
1451 /* Decide on the kind of this complex number. */
1452 if (real->ts.type == BT_REAL)
1453 {
1454 if (imag->ts.type == BT_REAL)
1455 kind = gfc_kind_max (real, imag);
1456 else
1457 kind = real->ts.kind;
1458 }
1459 else
1460 {
1461 if (imag->ts.type == BT_REAL)
1462 kind = imag->ts.kind;
1463 else
1464 kind = gfc_default_real_kind;
1465 }
1466 gfc_clear_ts (&target);
1467 target.type = BT_REAL;
1468 target.kind = kind;
1469
1470 if (real->ts.type != BT_REAL || kind != real->ts.kind)
1471 gfc_convert_type (real, &target, 2);
1472 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1473 gfc_convert_type (imag, &target, 2);
1474
1475 e = convert_complex (real, imag, kind);
1476 e->where = gfc_current_locus;
1477
1478 gfc_free_expr (real);
1479 gfc_free_expr (imag);
1480
1481 *result = e;
1482 return MATCH_YES;
1483
1484 syntax:
1485 gfc_error ("Syntax error in COMPLEX constant at %C");
1486 m = MATCH_ERROR;
1487
1488 cleanup:
1489 gfc_free_expr (e);
1490 gfc_free_expr (real);
1491 gfc_free_expr (imag);
1492 gfc_current_locus = old_loc;
1493
1494 return m;
1495 }
1496
1497
1498 /* Match constants in any of several forms. Returns nonzero for a
1499 match, zero for no match. */
1500
1501 match
1502 gfc_match_literal_constant (gfc_expr **result, int signflag)
1503 {
1504 match m;
1505
1506 m = match_complex_constant (result);
1507 if (m != MATCH_NO)
1508 return m;
1509
1510 m = match_string_constant (result);
1511 if (m != MATCH_NO)
1512 return m;
1513
1514 m = match_boz_constant (result);
1515 if (m != MATCH_NO)
1516 return m;
1517
1518 m = match_real_constant (result, signflag);
1519 if (m != MATCH_NO)
1520 return m;
1521
1522 m = match_hollerith_constant (result);
1523 if (m != MATCH_NO)
1524 return m;
1525
1526 m = match_integer_constant (result, signflag);
1527 if (m != MATCH_NO)
1528 return m;
1529
1530 m = match_logical_constant (result);
1531 if (m != MATCH_NO)
1532 return m;
1533
1534 return MATCH_NO;
1535 }
1536
1537
1538 /* This checks if a symbol is the return value of an encompassing function.
1539 Function nesting can be maximally two levels deep, but we may have
1540 additional local namespaces like BLOCK etc. */
1541
1542 bool
1543 gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1544 {
1545 if (!sym->attr.function || (sym->result != sym))
1546 return false;
1547 while (ns)
1548 {
1549 if (ns->proc_name == sym)
1550 return true;
1551 ns = ns->parent;
1552 }
1553 return false;
1554 }
1555
1556
1557 /* Match a single actual argument value. An actual argument is
1558 usually an expression, but can also be a procedure name. If the
1559 argument is a single name, it is not always possible to tell
1560 whether the name is a dummy procedure or not. We treat these cases
1561 by creating an argument that looks like a dummy procedure and
1562 fixing things later during resolution. */
1563
1564 static match
1565 match_actual_arg (gfc_expr **result)
1566 {
1567 char name[GFC_MAX_SYMBOL_LEN + 1];
1568 gfc_symtree *symtree;
1569 locus where, w;
1570 gfc_expr *e;
1571 char c;
1572
1573 gfc_gobble_whitespace ();
1574 where = gfc_current_locus;
1575
1576 switch (gfc_match_name (name))
1577 {
1578 case MATCH_ERROR:
1579 return MATCH_ERROR;
1580
1581 case MATCH_NO:
1582 break;
1583
1584 case MATCH_YES:
1585 w = gfc_current_locus;
1586 gfc_gobble_whitespace ();
1587 c = gfc_next_ascii_char ();
1588 gfc_current_locus = w;
1589
1590 if (c != ',' && c != ')')
1591 break;
1592
1593 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1594 break;
1595 /* Handle error elsewhere. */
1596
1597 /* Eliminate a couple of common cases where we know we don't
1598 have a function argument. */
1599 if (symtree == NULL)
1600 {
1601 gfc_get_sym_tree (name, NULL, &symtree, false);
1602 gfc_set_sym_referenced (symtree->n.sym);
1603 }
1604 else
1605 {
1606 gfc_symbol *sym;
1607
1608 sym = symtree->n.sym;
1609 gfc_set_sym_referenced (sym);
1610 if (sym->attr.flavor == FL_NAMELIST)
1611 {
1612 gfc_error ("Namelist %qs cannot be an argument at %L",
1613 sym->name, &where);
1614 break;
1615 }
1616 if (sym->attr.flavor != FL_PROCEDURE
1617 && sym->attr.flavor != FL_UNKNOWN)
1618 break;
1619
1620 if (sym->attr.in_common && !sym->attr.proc_pointer)
1621 {
1622 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE,
1623 sym->name, &sym->declared_at))
1624 return MATCH_ERROR;
1625 break;
1626 }
1627
1628 /* If the symbol is a function with itself as the result and
1629 is being defined, then we have a variable. */
1630 if (sym->attr.function && sym->result == sym)
1631 {
1632 if (gfc_is_function_return_value (sym, gfc_current_ns))
1633 break;
1634
1635 if (sym->attr.entry
1636 && (sym->ns == gfc_current_ns
1637 || sym->ns == gfc_current_ns->parent))
1638 {
1639 gfc_entry_list *el = NULL;
1640
1641 for (el = sym->ns->entries; el; el = el->next)
1642 if (sym == el->sym)
1643 break;
1644
1645 if (el)
1646 break;
1647 }
1648 }
1649 }
1650
1651 e = gfc_get_expr (); /* Leave it unknown for now */
1652 e->symtree = symtree;
1653 e->expr_type = EXPR_VARIABLE;
1654 e->ts.type = BT_PROCEDURE;
1655 e->where = where;
1656
1657 *result = e;
1658 return MATCH_YES;
1659 }
1660
1661 gfc_current_locus = where;
1662 return gfc_match_expr (result);
1663 }
1664
1665
1666 /* Match a keyword argument or type parameter spec list.. */
1667
1668 static match
1669 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
1670 {
1671 char name[GFC_MAX_SYMBOL_LEN + 1];
1672 gfc_actual_arglist *a;
1673 locus name_locus;
1674 match m;
1675
1676 name_locus = gfc_current_locus;
1677 m = gfc_match_name (name);
1678
1679 if (m != MATCH_YES)
1680 goto cleanup;
1681 if (gfc_match_char ('=') != MATCH_YES)
1682 {
1683 m = MATCH_NO;
1684 goto cleanup;
1685 }
1686
1687 if (pdt)
1688 {
1689 if (gfc_match_char ('*') == MATCH_YES)
1690 {
1691 actual->spec_type = SPEC_ASSUMED;
1692 goto add_name;
1693 }
1694 else if (gfc_match_char (':') == MATCH_YES)
1695 {
1696 actual->spec_type = SPEC_DEFERRED;
1697 goto add_name;
1698 }
1699 else
1700 actual->spec_type = SPEC_EXPLICIT;
1701 }
1702
1703 m = match_actual_arg (&actual->expr);
1704 if (m != MATCH_YES)
1705 goto cleanup;
1706
1707 /* Make sure this name has not appeared yet. */
1708 add_name:
1709 if (name[0] != '\0')
1710 {
1711 for (a = base; a; a = a->next)
1712 if (a->name != NULL && strcmp (a->name, name) == 0)
1713 {
1714 gfc_error ("Keyword %qs at %C has already appeared in the "
1715 "current argument list", name);
1716 return MATCH_ERROR;
1717 }
1718 }
1719
1720 actual->name = gfc_get_string ("%s", name);
1721 return MATCH_YES;
1722
1723 cleanup:
1724 gfc_current_locus = name_locus;
1725 return m;
1726 }
1727
1728
1729 /* Match an argument list function, such as %VAL. */
1730
1731 static match
1732 match_arg_list_function (gfc_actual_arglist *result)
1733 {
1734 char name[GFC_MAX_SYMBOL_LEN + 1];
1735 locus old_locus;
1736 match m;
1737
1738 old_locus = gfc_current_locus;
1739
1740 if (gfc_match_char ('%') != MATCH_YES)
1741 {
1742 m = MATCH_NO;
1743 goto cleanup;
1744 }
1745
1746 m = gfc_match ("%n (", name);
1747 if (m != MATCH_YES)
1748 goto cleanup;
1749
1750 if (name[0] != '\0')
1751 {
1752 switch (name[0])
1753 {
1754 case 'l':
1755 if (gfc_str_startswith (name, "loc"))
1756 {
1757 result->name = "%LOC";
1758 break;
1759 }
1760 /* FALLTHRU */
1761 case 'r':
1762 if (gfc_str_startswith (name, "ref"))
1763 {
1764 result->name = "%REF";
1765 break;
1766 }
1767 /* FALLTHRU */
1768 case 'v':
1769 if (gfc_str_startswith (name, "val"))
1770 {
1771 result->name = "%VAL";
1772 break;
1773 }
1774 /* FALLTHRU */
1775 default:
1776 m = MATCH_ERROR;
1777 goto cleanup;
1778 }
1779 }
1780
1781 if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
1782 {
1783 m = MATCH_ERROR;
1784 goto cleanup;
1785 }
1786
1787 m = match_actual_arg (&result->expr);
1788 if (m != MATCH_YES)
1789 goto cleanup;
1790
1791 if (gfc_match_char (')') != MATCH_YES)
1792 {
1793 m = MATCH_NO;
1794 goto cleanup;
1795 }
1796
1797 return MATCH_YES;
1798
1799 cleanup:
1800 gfc_current_locus = old_locus;
1801 return m;
1802 }
1803
1804
1805 /* Matches an actual argument list of a function or subroutine, from
1806 the opening parenthesis to the closing parenthesis. The argument
1807 list is assumed to allow keyword arguments because we don't know if
1808 the symbol associated with the procedure has an implicit interface
1809 or not. We make sure keywords are unique. If sub_flag is set,
1810 we're matching the argument list of a subroutine.
1811
1812 NOTE: An alternative use for this function is to match type parameter
1813 spec lists, which are so similar to actual argument lists that the
1814 machinery can be reused. This use is flagged by the optional argument
1815 'pdt'. */
1816
1817 match
1818 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
1819 {
1820 gfc_actual_arglist *head, *tail;
1821 int seen_keyword;
1822 gfc_st_label *label;
1823 locus old_loc;
1824 match m;
1825
1826 *argp = tail = NULL;
1827 old_loc = gfc_current_locus;
1828
1829 seen_keyword = 0;
1830
1831 if (gfc_match_char ('(') == MATCH_NO)
1832 return (sub_flag) ? MATCH_YES : MATCH_NO;
1833
1834 if (gfc_match_char (')') == MATCH_YES)
1835 return MATCH_YES;
1836
1837 head = NULL;
1838
1839 matching_actual_arglist++;
1840
1841 for (;;)
1842 {
1843 if (head == NULL)
1844 head = tail = gfc_get_actual_arglist ();
1845 else
1846 {
1847 tail->next = gfc_get_actual_arglist ();
1848 tail = tail->next;
1849 }
1850
1851 if (sub_flag && !pdt && gfc_match_char ('*') == MATCH_YES)
1852 {
1853 m = gfc_match_st_label (&label);
1854 if (m == MATCH_NO)
1855 gfc_error ("Expected alternate return label at %C");
1856 if (m != MATCH_YES)
1857 goto cleanup;
1858
1859 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
1860 "at %C"))
1861 goto cleanup;
1862
1863 tail->label = label;
1864 goto next;
1865 }
1866
1867 if (pdt && !seen_keyword)
1868 {
1869 if (gfc_match_char (':') == MATCH_YES)
1870 {
1871 tail->spec_type = SPEC_DEFERRED;
1872 goto next;
1873 }
1874 else if (gfc_match_char ('*') == MATCH_YES)
1875 {
1876 tail->spec_type = SPEC_ASSUMED;
1877 goto next;
1878 }
1879 else
1880 tail->spec_type = SPEC_EXPLICIT;
1881
1882 m = match_keyword_arg (tail, head, pdt);
1883 if (m == MATCH_YES)
1884 {
1885 seen_keyword = 1;
1886 goto next;
1887 }
1888 if (m == MATCH_ERROR)
1889 goto cleanup;
1890 }
1891
1892 /* After the first keyword argument is seen, the following
1893 arguments must also have keywords. */
1894 if (seen_keyword)
1895 {
1896 m = match_keyword_arg (tail, head, pdt);
1897
1898 if (m == MATCH_ERROR)
1899 goto cleanup;
1900 if (m == MATCH_NO)
1901 {
1902 gfc_error ("Missing keyword name in actual argument list at %C");
1903 goto cleanup;
1904 }
1905
1906 }
1907 else
1908 {
1909 /* Try an argument list function, like %VAL. */
1910 m = match_arg_list_function (tail);
1911 if (m == MATCH_ERROR)
1912 goto cleanup;
1913
1914 /* See if we have the first keyword argument. */
1915 if (m == MATCH_NO)
1916 {
1917 m = match_keyword_arg (tail, head, false);
1918 if (m == MATCH_YES)
1919 seen_keyword = 1;
1920 if (m == MATCH_ERROR)
1921 goto cleanup;
1922 }
1923
1924 if (m == MATCH_NO)
1925 {
1926 /* Try for a non-keyword argument. */
1927 m = match_actual_arg (&tail->expr);
1928 if (m == MATCH_ERROR)
1929 goto cleanup;
1930 if (m == MATCH_NO)
1931 goto syntax;
1932 }
1933 }
1934
1935
1936 next:
1937 if (gfc_match_char (')') == MATCH_YES)
1938 break;
1939 if (gfc_match_char (',') != MATCH_YES)
1940 goto syntax;
1941 }
1942
1943 *argp = head;
1944 matching_actual_arglist--;
1945 return MATCH_YES;
1946
1947 syntax:
1948 gfc_error ("Syntax error in argument list at %C");
1949
1950 cleanup:
1951 gfc_free_actual_arglist (head);
1952 gfc_current_locus = old_loc;
1953 matching_actual_arglist--;
1954 return MATCH_ERROR;
1955 }
1956
1957
1958 /* Used by gfc_match_varspec() to extend the reference list by one
1959 element. */
1960
1961 static gfc_ref *
1962 extend_ref (gfc_expr *primary, gfc_ref *tail)
1963 {
1964 if (primary->ref == NULL)
1965 primary->ref = tail = gfc_get_ref ();
1966 else
1967 {
1968 if (tail == NULL)
1969 gfc_internal_error ("extend_ref(): Bad tail");
1970 tail->next = gfc_get_ref ();
1971 tail = tail->next;
1972 }
1973
1974 return tail;
1975 }
1976
1977
1978 /* Used by gfc_match_varspec() to match an inquiry reference. */
1979
1980 static bool
1981 is_inquiry_ref (const char *name, gfc_ref **ref)
1982 {
1983 inquiry_type type;
1984
1985 if (name == NULL)
1986 return false;
1987
1988 if (ref) *ref = NULL;
1989
1990 if (strcmp (name, "re") == 0)
1991 type = INQUIRY_RE;
1992 else if (strcmp (name, "im") == 0)
1993 type = INQUIRY_IM;
1994 else if (strcmp (name, "kind") == 0)
1995 type = INQUIRY_KIND;
1996 else if (strcmp (name, "len") == 0)
1997 type = INQUIRY_LEN;
1998 else
1999 return false;
2000
2001 switch (type)
2002 {
2003 case INQUIRY_RE:
2004 case INQUIRY_IM:
2005 if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2006 return false;
2007 break;
2008
2009 case INQUIRY_KIND:
2010 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2011 return false;
2012 break;
2013
2014 case INQUIRY_LEN:
2015 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2016 return false;
2017 break;
2018
2019 default:
2020 gcc_unreachable ();
2021 }
2022
2023 if (ref)
2024 {
2025 *ref = gfc_get_ref ();
2026 (*ref)->type = REF_INQUIRY;
2027 (*ref)->u.i = type;
2028 }
2029
2030 return true;
2031 }
2032
2033
2034 /* Match any additional specifications associated with the current
2035 variable like member references or substrings. If equiv_flag is
2036 set we only match stuff that is allowed inside an EQUIVALENCE
2037 statement. sub_flag tells whether we expect a type-bound procedure found
2038 to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
2039 components, 'ppc_arg' determines whether the PPC may be called (with an
2040 argument list), or whether it may just be referred to as a pointer. */
2041
2042 match
2043 gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
2044 bool ppc_arg)
2045 {
2046 char name[GFC_MAX_SYMBOL_LEN + 1];
2047 gfc_ref *substring, *tail, *tmp;
2048 gfc_component *component;
2049 gfc_symbol *sym = primary->symtree->n.sym;
2050 gfc_expr *tgt_expr = NULL;
2051 match m;
2052 bool unknown;
2053 bool inquiry;
2054 bool intrinsic;
2055 locus old_loc;
2056 char sep;
2057
2058 tail = NULL;
2059
2060 gfc_gobble_whitespace ();
2061
2062 if (gfc_peek_ascii_char () == '[')
2063 {
2064 if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
2065 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2066 && CLASS_DATA (sym)->attr.dimension))
2067 {
2068 gfc_error ("Array section designator, e.g. '(:)', is required "
2069 "besides the coarray designator '[...]' at %C");
2070 return MATCH_ERROR;
2071 }
2072 if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
2073 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
2074 && !CLASS_DATA (sym)->attr.codimension))
2075 {
2076 gfc_error ("Coarray designator at %C but %qs is not a coarray",
2077 sym->name);
2078 return MATCH_ERROR;
2079 }
2080 }
2081
2082 if (sym->assoc && sym->assoc->target)
2083 tgt_expr = sym->assoc->target;
2084
2085 /* For associate names, we may not yet know whether they are arrays or not.
2086 If the selector expression is unambiguously an array; eg. a full array
2087 or an array section, then the associate name must be an array and we can
2088 fix it now. Otherwise, if parentheses follow and it is not a character
2089 type, we have to assume that it actually is one for now. The final
2090 decision will be made at resolution, of course. */
2091 if (sym->assoc
2092 && gfc_peek_ascii_char () == '('
2093 && sym->ts.type != BT_CLASS
2094 && !sym->attr.dimension)
2095 {
2096 gfc_ref *ref = NULL;
2097
2098 if (!sym->assoc->dangling && tgt_expr)
2099 {
2100 if (tgt_expr->expr_type == EXPR_VARIABLE)
2101 gfc_resolve_expr (tgt_expr);
2102
2103 ref = tgt_expr->ref;
2104 for (; ref; ref = ref->next)
2105 if (ref->type == REF_ARRAY
2106 && (ref->u.ar.type == AR_FULL
2107 || ref->u.ar.type == AR_SECTION))
2108 break;
2109 }
2110
2111 if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
2112 && sym->assoc->st
2113 && sym->assoc->st->n.sym
2114 && sym->assoc->st->n.sym->attr.dimension == 0))
2115 {
2116 sym->attr.dimension = 1;
2117 if (sym->as == NULL
2118 && sym->assoc->st
2119 && sym->assoc->st->n.sym
2120 && sym->assoc->st->n.sym->as)
2121 sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
2122 }
2123 }
2124 else if (sym->ts.type == BT_CLASS
2125 && tgt_expr
2126 && tgt_expr->expr_type == EXPR_VARIABLE
2127 && sym->ts.u.derived != tgt_expr->ts.u.derived)
2128 {
2129 gfc_resolve_expr (tgt_expr);
2130 if (tgt_expr->rank)
2131 sym->ts.u.derived = tgt_expr->ts.u.derived;
2132 }
2133
2134 if ((equiv_flag && gfc_peek_ascii_char () == '(')
2135 || gfc_peek_ascii_char () == '[' || sym->attr.codimension
2136 || (sym->attr.dimension && sym->ts.type != BT_CLASS
2137 && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
2138 && !(gfc_matching_procptr_assignment
2139 && sym->attr.flavor == FL_PROCEDURE))
2140 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2141 && (CLASS_DATA (sym)->attr.dimension
2142 || CLASS_DATA (sym)->attr.codimension)))
2143 {
2144 gfc_array_spec *as;
2145
2146 tail = extend_ref (primary, tail);
2147 tail->type = REF_ARRAY;
2148
2149 /* In EQUIVALENCE, we don't know yet whether we are seeing
2150 an array, character variable or array of character
2151 variables. We'll leave the decision till resolve time. */
2152
2153 if (equiv_flag)
2154 as = NULL;
2155 else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
2156 as = CLASS_DATA (sym)->as;
2157 else
2158 as = sym->as;
2159
2160 m = gfc_match_array_ref (&tail->u.ar, as, equiv_flag,
2161 as ? as->corank : 0);
2162 if (m != MATCH_YES)
2163 return m;
2164
2165 gfc_gobble_whitespace ();
2166 if (equiv_flag && gfc_peek_ascii_char () == '(')
2167 {
2168 tail = extend_ref (primary, tail);
2169 tail->type = REF_ARRAY;
2170
2171 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
2172 if (m != MATCH_YES)
2173 return m;
2174 }
2175 }
2176
2177 primary->ts = sym->ts;
2178
2179 if (equiv_flag)
2180 return MATCH_YES;
2181
2182 /* With DEC extensions, member separator may be '.' or '%'. */
2183 sep = gfc_peek_ascii_char ();
2184 m = gfc_match_member_sep (sym);
2185 if (m == MATCH_ERROR)
2186 return MATCH_ERROR;
2187
2188 inquiry = false;
2189 if (m == MATCH_YES && sep == '%'
2190 && primary->ts.type != BT_CLASS
2191 && primary->ts.type != BT_DERIVED)
2192 {
2193 match mm;
2194 old_loc = gfc_current_locus;
2195 mm = gfc_match_name (name);
2196 if (mm == MATCH_YES && is_inquiry_ref (name, &tmp))
2197 inquiry = true;
2198 gfc_current_locus = old_loc;
2199 }
2200
2201 if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
2202 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2203 gfc_set_default_type (sym, 0, sym->ns);
2204
2205 /* See if there is a usable typespec in the "no IMPLICIT type" error. */
2206 if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES)
2207 {
2208 bool permissible;
2209
2210 /* These target expressions can be resolved at any time. */
2211 permissible = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym
2212 && (tgt_expr->symtree->n.sym->attr.use_assoc
2213 || tgt_expr->symtree->n.sym->attr.host_assoc
2214 || tgt_expr->symtree->n.sym->attr.if_source
2215 == IFSRC_DECL);
2216 permissible = permissible
2217 || (tgt_expr && tgt_expr->expr_type == EXPR_OP);
2218
2219 if (permissible)
2220 {
2221 gfc_resolve_expr (tgt_expr);
2222 sym->ts = tgt_expr->ts;
2223 }
2224
2225 if (sym->ts.type == BT_UNKNOWN)
2226 {
2227 gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
2228 return MATCH_ERROR;
2229 }
2230 }
2231 else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
2232 && m == MATCH_YES && !inquiry)
2233 {
2234 gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
2235 sep, sym->name);
2236 return MATCH_ERROR;
2237 }
2238
2239 if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
2240 || m != MATCH_YES)
2241 goto check_substring;
2242
2243 if (!inquiry)
2244 sym = sym->ts.u.derived;
2245 else
2246 sym = NULL;
2247
2248 for (;;)
2249 {
2250 bool t;
2251 gfc_symtree *tbp;
2252
2253 m = gfc_match_name (name);
2254 if (m == MATCH_NO)
2255 gfc_error ("Expected structure component name at %C");
2256 if (m != MATCH_YES)
2257 return MATCH_ERROR;
2258
2259 intrinsic = false;
2260 if (primary->ts.type != BT_CLASS && primary->ts.type != BT_DERIVED)
2261 {
2262 inquiry = is_inquiry_ref (name, &tmp);
2263 if (inquiry)
2264 sym = NULL;
2265
2266 if (sep == '%')
2267 {
2268 if (tmp)
2269 {
2270 if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
2271 && primary->ts.type != BT_COMPLEX)
2272 {
2273 gfc_error ("The RE or IM part_ref at %C must be "
2274 "applied to a COMPLEX expression");
2275 return MATCH_ERROR;
2276 }
2277 else if (tmp->u.i == INQUIRY_LEN
2278 && primary->ts.type != BT_CHARACTER)
2279 {
2280 gfc_error ("The LEN part_ref at %C must be applied "
2281 "to a CHARACTER expression");
2282 return MATCH_ERROR;
2283 }
2284 }
2285 if (primary->ts.type != BT_UNKNOWN)
2286 intrinsic = true;
2287 }
2288 }
2289 else
2290 inquiry = false;
2291
2292 if (sym && sym->f2k_derived)
2293 tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
2294 else
2295 tbp = NULL;
2296
2297 if (tbp)
2298 {
2299 gfc_symbol* tbp_sym;
2300
2301 if (!t)
2302 return MATCH_ERROR;
2303
2304 gcc_assert (!tail || !tail->next);
2305
2306 if (!(primary->expr_type == EXPR_VARIABLE
2307 || (primary->expr_type == EXPR_STRUCTURE
2308 && primary->symtree && primary->symtree->n.sym
2309 && primary->symtree->n.sym->attr.flavor)))
2310 return MATCH_ERROR;
2311
2312 if (tbp->n.tb->is_generic)
2313 tbp_sym = NULL;
2314 else
2315 tbp_sym = tbp->n.tb->u.specific->n.sym;
2316
2317 primary->expr_type = EXPR_COMPCALL;
2318 primary->value.compcall.tbp = tbp->n.tb;
2319 primary->value.compcall.name = tbp->name;
2320 primary->value.compcall.ignore_pass = 0;
2321 primary->value.compcall.assign = 0;
2322 primary->value.compcall.base_object = NULL;
2323 gcc_assert (primary->symtree->n.sym->attr.referenced);
2324 if (tbp_sym)
2325 primary->ts = tbp_sym->ts;
2326 else
2327 gfc_clear_ts (&primary->ts);
2328
2329 m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
2330 &primary->value.compcall.actual);
2331 if (m == MATCH_ERROR)
2332 return MATCH_ERROR;
2333 if (m == MATCH_NO)
2334 {
2335 if (sub_flag)
2336 primary->value.compcall.actual = NULL;
2337 else
2338 {
2339 gfc_error ("Expected argument list at %C");
2340 return MATCH_ERROR;
2341 }
2342 }
2343
2344 break;
2345 }
2346
2347 if (!inquiry && !intrinsic)
2348 component = gfc_find_component (sym, name, false, false, &tmp);
2349 else
2350 component = NULL;
2351
2352 /* In some cases, returning MATCH_NO gives a better error message. Most
2353 cases return "Unclassifiable statement at..." */
2354 if (intrinsic && !inquiry)
2355 return MATCH_NO;
2356 else if (component == NULL && !inquiry)
2357 return MATCH_ERROR;
2358
2359 /* Extend the reference chain determined by gfc_find_component or
2360 is_inquiry_ref. */
2361 if (primary->ref == NULL)
2362 primary->ref = tmp;
2363 else
2364 {
2365 /* Set by the for loop below for the last component ref. */
2366 gcc_assert (tail != NULL);
2367 tail->next = tmp;
2368 }
2369
2370 /* The reference chain may be longer than one hop for union
2371 subcomponents; find the new tail. */
2372 for (tail = tmp; tail->next; tail = tail->next)
2373 ;
2374
2375 if (tmp && tmp->type == REF_INQUIRY)
2376 {
2377 if (!primary->where.lb || !primary->where.nextc)
2378 primary->where = gfc_current_locus;
2379 gfc_simplify_expr (primary, 0);
2380
2381 if (primary->expr_type == EXPR_CONSTANT)
2382 goto check_done;
2383
2384 switch (tmp->u.i)
2385 {
2386 case INQUIRY_RE:
2387 case INQUIRY_IM:
2388 if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
2389 return MATCH_ERROR;
2390
2391 if (primary->ts.type != BT_COMPLEX)
2392 {
2393 gfc_error ("The RE or IM part_ref at %C must be "
2394 "applied to a COMPLEX expression");
2395 return MATCH_ERROR;
2396 }
2397 primary->ts.type = BT_REAL;
2398 break;
2399
2400 case INQUIRY_LEN:
2401 if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
2402 return MATCH_ERROR;
2403
2404 if (primary->ts.type != BT_CHARACTER)
2405 {
2406 gfc_error ("The LEN part_ref at %C must be applied "
2407 "to a CHARACTER expression");
2408 return MATCH_ERROR;
2409 }
2410 primary->ts.u.cl = NULL;
2411 primary->ts.type = BT_INTEGER;
2412 primary->ts.kind = gfc_default_integer_kind;
2413 break;
2414
2415 case INQUIRY_KIND:
2416 if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
2417 return MATCH_ERROR;
2418
2419 if (primary->ts.type == BT_CLASS
2420 || primary->ts.type == BT_DERIVED)
2421 {
2422 gfc_error ("The KIND part_ref at %C must be applied "
2423 "to an expression of intrinsic type");
2424 return MATCH_ERROR;
2425 }
2426 primary->ts.type = BT_INTEGER;
2427 primary->ts.kind = gfc_default_integer_kind;
2428 break;
2429
2430 default:
2431 gcc_unreachable ();
2432 }
2433
2434 goto check_done;
2435 }
2436
2437 primary->ts = component->ts;
2438
2439 if (component->attr.proc_pointer && ppc_arg)
2440 {
2441 /* Procedure pointer component call: Look for argument list. */
2442 m = gfc_match_actual_arglist (sub_flag,
2443 &primary->value.compcall.actual);
2444 if (m == MATCH_ERROR)
2445 return MATCH_ERROR;
2446
2447 if (m == MATCH_NO && !gfc_matching_ptr_assignment
2448 && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2449 {
2450 gfc_error ("Procedure pointer component %qs requires an "
2451 "argument list at %C", component->name);
2452 return MATCH_ERROR;
2453 }
2454
2455 if (m == MATCH_YES)
2456 primary->expr_type = EXPR_PPC;
2457
2458 break;
2459 }
2460
2461 if (component->as != NULL && !component->attr.proc_pointer)
2462 {
2463 tail = extend_ref (primary, tail);
2464 tail->type = REF_ARRAY;
2465
2466 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2467 component->as->corank);
2468 if (m != MATCH_YES)
2469 return m;
2470 }
2471 else if (component->ts.type == BT_CLASS && component->attr.class_ok
2472 && CLASS_DATA (component)->as && !component->attr.proc_pointer)
2473 {
2474 tail = extend_ref (primary, tail);
2475 tail->type = REF_ARRAY;
2476
2477 m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2478 equiv_flag,
2479 CLASS_DATA (component)->as->corank);
2480 if (m != MATCH_YES)
2481 return m;
2482 }
2483
2484 check_done:
2485 /* In principle, we could have eg. expr%re%kind so we must allow for
2486 this possibility. */
2487 if (gfc_match_char ('%') == MATCH_YES)
2488 {
2489 if (component && (component->ts.type == BT_DERIVED
2490 || component->ts.type == BT_CLASS))
2491 sym = component->ts.u.derived;
2492 continue;
2493 }
2494 else if (inquiry)
2495 break;
2496
2497 if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2498 || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
2499 break;
2500
2501 if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
2502 sym = component->ts.u.derived;
2503 }
2504
2505 check_substring:
2506 unknown = false;
2507 if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
2508 {
2509 if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2510 {
2511 gfc_set_default_type (sym, 0, sym->ns);
2512 primary->ts = sym->ts;
2513 unknown = true;
2514 }
2515 }
2516
2517 if (primary->ts.type == BT_CHARACTER)
2518 {
2519 bool def = primary->ts.deferred == 1;
2520 switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
2521 {
2522 case MATCH_YES:
2523 if (tail == NULL)
2524 primary->ref = substring;
2525 else
2526 tail->next = substring;
2527
2528 if (primary->expr_type == EXPR_CONSTANT)
2529 primary->expr_type = EXPR_SUBSTRING;
2530
2531 if (substring)
2532 primary->ts.u.cl = NULL;
2533
2534 break;
2535
2536 case MATCH_NO:
2537 if (unknown)
2538 {
2539 gfc_clear_ts (&primary->ts);
2540 gfc_clear_ts (&sym->ts);
2541 }
2542 break;
2543
2544 case MATCH_ERROR:
2545 return MATCH_ERROR;
2546 }
2547 }
2548
2549 /* F08:C611. */
2550 if (primary->ts.type == BT_DERIVED && primary->ref
2551 && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
2552 {
2553 gfc_error ("Nonpolymorphic reference to abstract type at %C");
2554 return MATCH_ERROR;
2555 }
2556
2557 /* F08:C727. */
2558 if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2559 {
2560 gfc_error ("Coindexed procedure-pointer component at %C");
2561 return MATCH_ERROR;
2562 }
2563
2564 return MATCH_YES;
2565 }
2566
2567
2568 /* Given an expression that is a variable, figure out what the
2569 ultimate variable's type and attribute is, traversing the reference
2570 structures if necessary.
2571
2572 This subroutine is trickier than it looks. We start at the base
2573 symbol and store the attribute. Component references load a
2574 completely new attribute.
2575
2576 A couple of rules come into play. Subobjects of targets are always
2577 targets themselves. If we see a component that goes through a
2578 pointer, then the expression must also be a target, since the
2579 pointer is associated with something (if it isn't core will soon be
2580 dumped). If we see a full part or section of an array, the
2581 expression is also an array.
2582
2583 We can have at most one full array reference. */
2584
2585 symbol_attribute
2586 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2587 {
2588 int dimension, codimension, pointer, allocatable, target;
2589 symbol_attribute attr;
2590 gfc_ref *ref;
2591 gfc_symbol *sym;
2592 gfc_component *comp;
2593 bool has_inquiry_part;
2594
2595 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2596 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2597
2598 sym = expr->symtree->n.sym;
2599 attr = sym->attr;
2600
2601 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2602 {
2603 dimension = CLASS_DATA (sym)->attr.dimension;
2604 codimension = CLASS_DATA (sym)->attr.codimension;
2605 pointer = CLASS_DATA (sym)->attr.class_pointer;
2606 allocatable = CLASS_DATA (sym)->attr.allocatable;
2607 }
2608 else
2609 {
2610 dimension = attr.dimension;
2611 codimension = attr.codimension;
2612 pointer = attr.pointer;
2613 allocatable = attr.allocatable;
2614 }
2615
2616 target = attr.target;
2617 if (pointer || attr.proc_pointer)
2618 target = 1;
2619
2620 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2621 *ts = sym->ts;
2622
2623 has_inquiry_part = false;
2624 for (ref = expr->ref; ref; ref = ref->next)
2625 if (ref->type == REF_INQUIRY)
2626 {
2627 has_inquiry_part = true;
2628 break;
2629 }
2630
2631 for (ref = expr->ref; ref; ref = ref->next)
2632 switch (ref->type)
2633 {
2634 case REF_ARRAY:
2635
2636 switch (ref->u.ar.type)
2637 {
2638 case AR_FULL:
2639 dimension = 1;
2640 break;
2641
2642 case AR_SECTION:
2643 allocatable = pointer = 0;
2644 dimension = 1;
2645 break;
2646
2647 case AR_ELEMENT:
2648 /* Handle coarrays. */
2649 if (ref->u.ar.dimen > 0)
2650 allocatable = pointer = 0;
2651 break;
2652
2653 case AR_UNKNOWN:
2654 /* For standard conforming code, AR_UNKNOWN should not happen.
2655 For nonconforming code, gfortran can end up here. Treat it
2656 as a no-op. */
2657 break;
2658 }
2659
2660 break;
2661
2662 case REF_COMPONENT:
2663 comp = ref->u.c.component;
2664 attr = comp->attr;
2665 if (ts != NULL && !has_inquiry_part)
2666 {
2667 *ts = comp->ts;
2668 /* Don't set the string length if a substring reference
2669 follows. */
2670 if (ts->type == BT_CHARACTER
2671 && ref->next && ref->next->type == REF_SUBSTRING)
2672 ts->u.cl = NULL;
2673 }
2674
2675 if (comp->ts.type == BT_CLASS)
2676 {
2677 codimension = CLASS_DATA (comp)->attr.codimension;
2678 pointer = CLASS_DATA (comp)->attr.class_pointer;
2679 allocatable = CLASS_DATA (comp)->attr.allocatable;
2680 }
2681 else
2682 {
2683 codimension = comp->attr.codimension;
2684 pointer = comp->attr.pointer;
2685 allocatable = comp->attr.allocatable;
2686 }
2687 if (pointer || attr.proc_pointer)
2688 target = 1;
2689
2690 break;
2691
2692 case REF_INQUIRY:
2693 case REF_SUBSTRING:
2694 allocatable = pointer = 0;
2695 break;
2696 }
2697
2698 attr.dimension = dimension;
2699 attr.codimension = codimension;
2700 attr.pointer = pointer;
2701 attr.allocatable = allocatable;
2702 attr.target = target;
2703 attr.save = sym->attr.save;
2704
2705 return attr;
2706 }
2707
2708
2709 /* Return the attribute from a general expression. */
2710
2711 symbol_attribute
2712 gfc_expr_attr (gfc_expr *e)
2713 {
2714 symbol_attribute attr;
2715
2716 switch (e->expr_type)
2717 {
2718 case EXPR_VARIABLE:
2719 attr = gfc_variable_attr (e, NULL);
2720 break;
2721
2722 case EXPR_FUNCTION:
2723 gfc_clear_attr (&attr);
2724
2725 if (e->value.function.esym && e->value.function.esym->result)
2726 {
2727 gfc_symbol *sym = e->value.function.esym->result;
2728 attr = sym->attr;
2729 if (sym->ts.type == BT_CLASS)
2730 {
2731 attr.dimension = CLASS_DATA (sym)->attr.dimension;
2732 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2733 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2734 }
2735 }
2736 else if (e->value.function.isym
2737 && e->value.function.isym->transformational
2738 && e->ts.type == BT_CLASS)
2739 attr = CLASS_DATA (e)->attr;
2740 else
2741 attr = gfc_variable_attr (e, NULL);
2742
2743 /* TODO: NULL() returns pointers. May have to take care of this
2744 here. */
2745
2746 break;
2747
2748 default:
2749 gfc_clear_attr (&attr);
2750 break;
2751 }
2752
2753 return attr;
2754 }
2755
2756
2757 /* Given an expression, figure out what the ultimate expression
2758 attribute is. This routine is similar to gfc_variable_attr with
2759 parts of gfc_expr_attr, but focuses more on the needs of
2760 coarrays. For coarrays a codimension attribute is kind of
2761 "infectious" being propagated once set and never cleared.
2762 The coarray_comp is only set, when the expression refs a coarray
2763 component. REFS_COMP is set when present to true only, when this EXPR
2764 refs a (non-_data) component. To check whether EXPR refs an allocatable
2765 component in a derived type coarray *refs_comp needs to be set and
2766 coarray_comp has to false. */
2767
2768 static symbol_attribute
2769 caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
2770 {
2771 int dimension, codimension, pointer, allocatable, target, coarray_comp;
2772 symbol_attribute attr;
2773 gfc_ref *ref;
2774 gfc_symbol *sym;
2775 gfc_component *comp;
2776
2777 if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2778 gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
2779
2780 sym = expr->symtree->n.sym;
2781 gfc_clear_attr (&attr);
2782
2783 if (refs_comp)
2784 *refs_comp = false;
2785
2786 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2787 {
2788 dimension = CLASS_DATA (sym)->attr.dimension;
2789 codimension = CLASS_DATA (sym)->attr.codimension;
2790 pointer = CLASS_DATA (sym)->attr.class_pointer;
2791 allocatable = CLASS_DATA (sym)->attr.allocatable;
2792 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2793 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
2794 }
2795 else
2796 {
2797 dimension = sym->attr.dimension;
2798 codimension = sym->attr.codimension;
2799 pointer = sym->attr.pointer;
2800 allocatable = sym->attr.allocatable;
2801 attr.alloc_comp = sym->ts.type == BT_DERIVED
2802 ? sym->ts.u.derived->attr.alloc_comp : 0;
2803 attr.pointer_comp = sym->ts.type == BT_DERIVED
2804 ? sym->ts.u.derived->attr.pointer_comp : 0;
2805 }
2806
2807 target = coarray_comp = 0;
2808 if (pointer || attr.proc_pointer)
2809 target = 1;
2810
2811 for (ref = expr->ref; ref; ref = ref->next)
2812 switch (ref->type)
2813 {
2814 case REF_ARRAY:
2815
2816 switch (ref->u.ar.type)
2817 {
2818 case AR_FULL:
2819 case AR_SECTION:
2820 dimension = 1;
2821 break;
2822
2823 case AR_ELEMENT:
2824 /* Handle coarrays. */
2825 if (ref->u.ar.dimen > 0 && !in_allocate)
2826 allocatable = pointer = 0;
2827 break;
2828
2829 case AR_UNKNOWN:
2830 /* If any of start, end or stride is not integer, there will
2831 already have been an error issued. */
2832 int errors;
2833 gfc_get_errors (NULL, &errors);
2834 if (errors == 0)
2835 gfc_internal_error ("gfc_caf_attr(): Bad array reference");
2836 }
2837
2838 break;
2839
2840 case REF_COMPONENT:
2841 comp = ref->u.c.component;
2842
2843 if (comp->ts.type == BT_CLASS)
2844 {
2845 /* Set coarray_comp only, when this component introduces the
2846 coarray. */
2847 coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
2848 codimension |= CLASS_DATA (comp)->attr.codimension;
2849 pointer = CLASS_DATA (comp)->attr.class_pointer;
2850 allocatable = CLASS_DATA (comp)->attr.allocatable;
2851 }
2852 else
2853 {
2854 /* Set coarray_comp only, when this component introduces the
2855 coarray. */
2856 coarray_comp = !codimension && comp->attr.codimension;
2857 codimension |= comp->attr.codimension;
2858 pointer = comp->attr.pointer;
2859 allocatable = comp->attr.allocatable;
2860 }
2861
2862 if (refs_comp && strcmp (comp->name, "_data") != 0
2863 && (ref->next == NULL
2864 || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
2865 *refs_comp = true;
2866
2867 if (pointer || attr.proc_pointer)
2868 target = 1;
2869
2870 break;
2871
2872 case REF_SUBSTRING:
2873 case REF_INQUIRY:
2874 allocatable = pointer = 0;
2875 break;
2876 }
2877
2878 attr.dimension = dimension;
2879 attr.codimension = codimension;
2880 attr.pointer = pointer;
2881 attr.allocatable = allocatable;
2882 attr.target = target;
2883 attr.save = sym->attr.save;
2884 attr.coarray_comp = coarray_comp;
2885
2886 return attr;
2887 }
2888
2889
2890 symbol_attribute
2891 gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
2892 {
2893 symbol_attribute attr;
2894
2895 switch (e->expr_type)
2896 {
2897 case EXPR_VARIABLE:
2898 attr = caf_variable_attr (e, in_allocate, refs_comp);
2899 break;
2900
2901 case EXPR_FUNCTION:
2902 gfc_clear_attr (&attr);
2903
2904 if (e->value.function.esym && e->value.function.esym->result)
2905 {
2906 gfc_symbol *sym = e->value.function.esym->result;
2907 attr = sym->attr;
2908 if (sym->ts.type == BT_CLASS)
2909 {
2910 attr.dimension = CLASS_DATA (sym)->attr.dimension;
2911 attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2912 attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2913 attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
2914 attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
2915 ->attr.pointer_comp;
2916 }
2917 }
2918 else if (e->symtree)
2919 attr = caf_variable_attr (e, in_allocate, refs_comp);
2920 else
2921 gfc_clear_attr (&attr);
2922 break;
2923
2924 default:
2925 gfc_clear_attr (&attr);
2926 break;
2927 }
2928
2929 return attr;
2930 }
2931
2932
2933 /* Match a structure constructor. The initial symbol has already been
2934 seen. */
2935
2936 typedef struct gfc_structure_ctor_component
2937 {
2938 char* name;
2939 gfc_expr* val;
2940 locus where;
2941 struct gfc_structure_ctor_component* next;
2942 }
2943 gfc_structure_ctor_component;
2944
2945 #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
2946
2947 static void
2948 gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2949 {
2950 free (comp->name);
2951 gfc_free_expr (comp->val);
2952 free (comp);
2953 }
2954
2955
2956 /* Translate the component list into the actual constructor by sorting it in
2957 the order required; this also checks along the way that each and every
2958 component actually has an initializer and handles default initializers
2959 for components without explicit value given. */
2960 static bool
2961 build_actual_constructor (gfc_structure_ctor_component **comp_head,
2962 gfc_constructor_base *ctor_head, gfc_symbol *sym)
2963 {
2964 gfc_structure_ctor_component *comp_iter;
2965 gfc_component *comp;
2966
2967 for (comp = sym->components; comp; comp = comp->next)
2968 {
2969 gfc_structure_ctor_component **next_ptr;
2970 gfc_expr *value = NULL;
2971
2972 /* Try to find the initializer for the current component by name. */
2973 next_ptr = comp_head;
2974 for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2975 {
2976 if (!strcmp (comp_iter->name, comp->name))
2977 break;
2978 next_ptr = &comp_iter->next;
2979 }
2980
2981 /* If an extension, try building the parent derived type by building
2982 a value expression for the parent derived type and calling self. */
2983 if (!comp_iter && comp == sym->components && sym->attr.extension)
2984 {
2985 value = gfc_get_structure_constructor_expr (comp->ts.type,
2986 comp->ts.kind,
2987 &gfc_current_locus);
2988 value->ts = comp->ts;
2989
2990 if (!build_actual_constructor (comp_head,
2991 &value->value.constructor,
2992 comp->ts.u.derived))
2993 {
2994 gfc_free_expr (value);
2995 return false;
2996 }
2997
2998 gfc_constructor_append_expr (ctor_head, value, NULL);
2999 continue;
3000 }
3001
3002 /* If it was not found, try the default initializer if there's any;
3003 otherwise, it's an error unless this is a deferred parameter. */
3004 if (!comp_iter)
3005 {
3006 if (comp->initializer)
3007 {
3008 if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
3009 "with missing optional arguments at %C"))
3010 return false;
3011 value = gfc_copy_expr (comp->initializer);
3012 }
3013 else if (comp->attr.allocatable
3014 || (comp->ts.type == BT_CLASS
3015 && CLASS_DATA (comp)->attr.allocatable))
3016 {
3017 if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
3018 "allocatable component %qs given in the "
3019 "structure constructor at %C", comp->name))
3020 return false;
3021 }
3022 else if (!comp->attr.artificial)
3023 {
3024 gfc_error ("No initializer for component %qs given in the"
3025 " structure constructor at %C", comp->name);
3026 return false;
3027 }
3028 }
3029 else
3030 value = comp_iter->val;
3031
3032 /* Add the value to the constructor chain built. */
3033 gfc_constructor_append_expr (ctor_head, value, NULL);
3034
3035 /* Remove the entry from the component list. We don't want the expression
3036 value to be free'd, so set it to NULL. */
3037 if (comp_iter)
3038 {
3039 *next_ptr = comp_iter->next;
3040 comp_iter->val = NULL;
3041 gfc_free_structure_ctor_component (comp_iter);
3042 }
3043 }
3044 return true;
3045 }
3046
3047
3048 bool
3049 gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
3050 gfc_actual_arglist **arglist,
3051 bool parent)
3052 {
3053 gfc_actual_arglist *actual;
3054 gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
3055 gfc_constructor_base ctor_head = NULL;
3056 gfc_component *comp; /* Is set NULL when named component is first seen */
3057 const char* last_name = NULL;
3058 locus old_locus;
3059 gfc_expr *expr;
3060
3061 expr = parent ? *cexpr : e;
3062 old_locus = gfc_current_locus;
3063 if (parent)
3064 ; /* gfc_current_locus = *arglist->expr ? ->where;*/
3065 else
3066 gfc_current_locus = expr->where;
3067
3068 comp_tail = comp_head = NULL;
3069
3070 if (!parent && sym->attr.abstract)
3071 {
3072 gfc_error ("Cannot construct ABSTRACT type %qs at %L",
3073 sym->name, &expr->where);
3074 goto cleanup;
3075 }
3076
3077 comp = sym->components;
3078 actual = parent ? *arglist : expr->value.function.actual;
3079 for ( ; actual; )
3080 {
3081 gfc_component *this_comp = NULL;
3082
3083 if (!comp_head)
3084 comp_tail = comp_head = gfc_get_structure_ctor_component ();
3085 else
3086 {
3087 comp_tail->next = gfc_get_structure_ctor_component ();
3088 comp_tail = comp_tail->next;
3089 }
3090 if (actual->name)
3091 {
3092 if (!gfc_notify_std (GFC_STD_F2003, "Structure"
3093 " constructor with named arguments at %C"))
3094 goto cleanup;
3095
3096 comp_tail->name = xstrdup (actual->name);
3097 last_name = comp_tail->name;
3098 comp = NULL;
3099 }
3100 else
3101 {
3102 /* Components without name are not allowed after the first named
3103 component initializer! */
3104 if (!comp || comp->attr.artificial)
3105 {
3106 if (last_name)
3107 gfc_error ("Component initializer without name after component"
3108 " named %s at %L", last_name,
3109 actual->expr ? &actual->expr->where
3110 : &gfc_current_locus);
3111 else
3112 gfc_error ("Too many components in structure constructor at "
3113 "%L", actual->expr ? &actual->expr->where
3114 : &gfc_current_locus);
3115 goto cleanup;
3116 }
3117
3118 comp_tail->name = xstrdup (comp->name);
3119 }
3120
3121 /* Find the current component in the structure definition and check
3122 its access is not private. */
3123 if (comp)
3124 this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
3125 else
3126 {
3127 this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
3128 false, false, NULL);
3129 comp = NULL; /* Reset needed! */
3130 }
3131
3132 /* Here we can check if a component name is given which does not
3133 correspond to any component of the defined structure. */
3134 if (!this_comp)
3135 goto cleanup;
3136
3137 /* For a constant string constructor, make sure the length is
3138 correct; truncate of fill with blanks if needed. */
3139 if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
3140 && this_comp->ts.u.cl && this_comp->ts.u.cl->length
3141 && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
3142 && actual->expr->ts.type == BT_CHARACTER
3143 && actual->expr->expr_type == EXPR_CONSTANT)
3144 {
3145 ptrdiff_t c, e1;
3146 c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
3147 e1 = actual->expr->value.character.length;
3148
3149 if (c != e1)
3150 {
3151 ptrdiff_t i, to;
3152 gfc_char_t *dest;
3153 dest = gfc_get_wide_string (c + 1);
3154
3155 to = e1 < c ? e1 : c;
3156 for (i = 0; i < to; i++)
3157 dest[i] = actual->expr->value.character.string[i];
3158
3159 for (i = e1; i < c; i++)
3160 dest[i] = ' ';
3161
3162 dest[c] = '\0';
3163 free (actual->expr->value.character.string);
3164
3165 actual->expr->value.character.length = c;
3166 actual->expr->value.character.string = dest;
3167
3168 if (warn_line_truncation && c < e1)
3169 gfc_warning_now (OPT_Wcharacter_truncation,
3170 "CHARACTER expression will be truncated "
3171 "in constructor (%ld/%ld) at %L", (long int) c,
3172 (long int) e1, &actual->expr->where);
3173 }
3174 }
3175
3176 comp_tail->val = actual->expr;
3177 if (actual->expr != NULL)
3178 comp_tail->where = actual->expr->where;
3179 actual->expr = NULL;
3180
3181 /* Check if this component is already given a value. */
3182 for (comp_iter = comp_head; comp_iter != comp_tail;
3183 comp_iter = comp_iter->next)
3184 {
3185 gcc_assert (comp_iter);
3186 if (!strcmp (comp_iter->name, comp_tail->name))
3187 {
3188 gfc_error ("Component %qs is initialized twice in the structure"
3189 " constructor at %L", comp_tail->name,
3190 comp_tail->val ? &comp_tail->where
3191 : &gfc_current_locus);
3192 goto cleanup;
3193 }
3194 }
3195
3196 /* F2008, R457/C725, for PURE C1283. */
3197 if (this_comp->attr.pointer && comp_tail->val
3198 && gfc_is_coindexed (comp_tail->val))
3199 {
3200 gfc_error ("Coindexed expression to pointer component %qs in "
3201 "structure constructor at %L", comp_tail->name,
3202 &comp_tail->where);
3203 goto cleanup;
3204 }
3205
3206 /* If not explicitly a parent constructor, gather up the components
3207 and build one. */
3208 if (comp && comp == sym->components
3209 && sym->attr.extension
3210 && comp_tail->val
3211 && (!gfc_bt_struct (comp_tail->val->ts.type)
3212 ||
3213 comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
3214 {
3215 bool m;
3216 gfc_actual_arglist *arg_null = NULL;
3217
3218 actual->expr = comp_tail->val;
3219 comp_tail->val = NULL;
3220
3221 m = gfc_convert_to_structure_constructor (NULL,
3222 comp->ts.u.derived, &comp_tail->val,
3223 comp->ts.u.derived->attr.zero_comp
3224 ? &arg_null : &actual, true);
3225 if (!m)
3226 goto cleanup;
3227
3228 if (comp->ts.u.derived->attr.zero_comp)
3229 {
3230 comp = comp->next;
3231 continue;
3232 }
3233 }
3234
3235 if (comp)
3236 comp = comp->next;
3237 if (parent && !comp)
3238 break;
3239
3240 if (actual)
3241 actual = actual->next;
3242 }
3243
3244 if (!build_actual_constructor (&comp_head, &ctor_head, sym))
3245 goto cleanup;
3246
3247 /* No component should be left, as this should have caused an error in the
3248 loop constructing the component-list (name that does not correspond to any
3249 component in the structure definition). */
3250 if (comp_head && sym->attr.extension)
3251 {
3252 for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
3253 {
3254 gfc_error ("component %qs at %L has already been set by a "
3255 "parent derived type constructor", comp_iter->name,
3256 &comp_iter->where);
3257 }
3258 goto cleanup;
3259 }
3260 else
3261 gcc_assert (!comp_head);
3262
3263 if (parent)
3264 {
3265 expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
3266 expr->ts.u.derived = sym;
3267 expr->value.constructor = ctor_head;
3268 *cexpr = expr;
3269 }
3270 else
3271 {
3272 expr->ts.u.derived = sym;
3273 expr->ts.kind = 0;
3274 expr->ts.type = BT_DERIVED;
3275 expr->value.constructor = ctor_head;
3276 expr->expr_type = EXPR_STRUCTURE;
3277 }
3278
3279 gfc_current_locus = old_locus;
3280 if (parent)
3281 *arglist = actual;
3282 return true;
3283
3284 cleanup:
3285 gfc_current_locus = old_locus;
3286
3287 for (comp_iter = comp_head; comp_iter; )
3288 {
3289 gfc_structure_ctor_component *next = comp_iter->next;
3290 gfc_free_structure_ctor_component (comp_iter);
3291 comp_iter = next;
3292 }
3293 gfc_constructor_free (ctor_head);
3294
3295 return false;
3296 }
3297
3298
3299 match
3300 gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
3301 {
3302 match m;
3303 gfc_expr *e;
3304 gfc_symtree *symtree;
3305
3306 gfc_get_ha_sym_tree (sym->name, &symtree);
3307
3308 e = gfc_get_expr ();
3309 e->symtree = symtree;
3310 e->expr_type = EXPR_FUNCTION;
3311 e->where = gfc_current_locus;
3312
3313 gcc_assert (gfc_fl_struct (sym->attr.flavor)
3314 && symtree->n.sym->attr.flavor == FL_PROCEDURE);
3315 e->value.function.esym = sym;
3316 e->symtree->n.sym->attr.generic = 1;
3317
3318 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3319 if (m != MATCH_YES)
3320 {
3321 gfc_free_expr (e);
3322 return m;
3323 }
3324
3325 if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
3326 {
3327 gfc_free_expr (e);
3328 return MATCH_ERROR;
3329 }
3330
3331 /* If a structure constructor is in a DATA statement, then each entity
3332 in the structure constructor must be a constant. Try to reduce the
3333 expression here. */
3334 if (gfc_in_match_data ())
3335 gfc_reduce_init_expr (e);
3336
3337 *result = e;
3338 return MATCH_YES;
3339 }
3340
3341
3342 /* If the symbol is an implicit do loop index and implicitly typed,
3343 it should not be host associated. Provide a symtree from the
3344 current namespace. */
3345 static match
3346 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
3347 {
3348 if ((*sym)->attr.flavor == FL_VARIABLE
3349 && (*sym)->ns != gfc_current_ns
3350 && (*sym)->attr.implied_index
3351 && (*sym)->attr.implicit_type
3352 && !(*sym)->attr.use_assoc)
3353 {
3354 int i;
3355 i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
3356 if (i)
3357 return MATCH_ERROR;
3358 *sym = (*st)->n.sym;
3359 }
3360 return MATCH_YES;
3361 }
3362
3363
3364 /* Procedure pointer as function result: Replace the function symbol by the
3365 auto-generated hidden result variable named "ppr@". */
3366
3367 static bool
3368 replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
3369 {
3370 /* Check for procedure pointer result variable. */
3371 if ((*sym)->attr.function && !(*sym)->attr.external
3372 && (*sym)->result && (*sym)->result != *sym
3373 && (*sym)->result->attr.proc_pointer
3374 && (*sym) == gfc_current_ns->proc_name
3375 && (*sym) == (*sym)->result->ns->proc_name
3376 && strcmp ("ppr@", (*sym)->result->name) == 0)
3377 {
3378 /* Automatic replacement with "hidden" result variable. */
3379 (*sym)->result->attr.referenced = (*sym)->attr.referenced;
3380 *sym = (*sym)->result;
3381 *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
3382 return true;
3383 }
3384 return false;
3385 }
3386
3387
3388 /* Matches a variable name followed by anything that might follow it--
3389 array reference, argument list of a function, etc. */
3390
3391 match
3392 gfc_match_rvalue (gfc_expr **result)
3393 {
3394 gfc_actual_arglist *actual_arglist;
3395 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
3396 gfc_state_data *st;
3397 gfc_symbol *sym;
3398 gfc_symtree *symtree;
3399 locus where, old_loc;
3400 gfc_expr *e;
3401 match m, m2;
3402 int i;
3403 gfc_typespec *ts;
3404 bool implicit_char;
3405 gfc_ref *ref;
3406
3407 m = gfc_match ("%%loc");
3408 if (m == MATCH_YES)
3409 {
3410 if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
3411 return MATCH_ERROR;
3412 strncpy (name, "loc", 4);
3413 }
3414
3415 else
3416 {
3417 m = gfc_match_name (name);
3418 if (m != MATCH_YES)
3419 return m;
3420 }
3421
3422 /* Check if the symbol exists. */
3423 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
3424 return MATCH_ERROR;
3425
3426 /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
3427 type. For derived types we create a generic symbol which links to the
3428 derived type symbol; STRUCTUREs are simpler and must not conflict with
3429 variables. */
3430 if (!symtree)
3431 if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
3432 return MATCH_ERROR;
3433 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3434 {
3435 if (gfc_find_state (COMP_INTERFACE)
3436 && !gfc_current_ns->has_import_set)
3437 i = gfc_get_sym_tree (name, NULL, &symtree, false);
3438 else
3439 i = gfc_get_ha_sym_tree (name, &symtree);
3440 if (i)
3441 return MATCH_ERROR;
3442 }
3443
3444
3445 sym = symtree->n.sym;
3446 e = NULL;
3447 where = gfc_current_locus;
3448
3449 replace_hidden_procptr_result (&sym, &symtree);
3450
3451 /* If this is an implicit do loop index and implicitly typed,
3452 it should not be host associated. */
3453 m = check_for_implicit_index (&symtree, &sym);
3454 if (m != MATCH_YES)
3455 return m;
3456
3457 gfc_set_sym_referenced (sym);
3458 sym->attr.implied_index = 0;
3459
3460 if (sym->attr.function && sym->result == sym)
3461 {
3462 /* See if this is a directly recursive function call. */
3463 gfc_gobble_whitespace ();
3464 if (sym->attr.recursive
3465 && gfc_peek_ascii_char () == '('
3466 && gfc_current_ns->proc_name == sym
3467 && !sym->attr.dimension)
3468 {
3469 gfc_error ("%qs at %C is the name of a recursive function "
3470 "and so refers to the result variable. Use an "
3471 "explicit RESULT variable for direct recursion "
3472 "(12.5.2.1)", sym->name);
3473 return MATCH_ERROR;
3474 }
3475
3476 if (gfc_is_function_return_value (sym, gfc_current_ns))
3477 goto variable;
3478
3479 if (sym->attr.entry
3480 && (sym->ns == gfc_current_ns
3481 || sym->ns == gfc_current_ns->parent))
3482 {
3483 gfc_entry_list *el = NULL;
3484
3485 for (el = sym->ns->entries; el; el = el->next)
3486 if (sym == el->sym)
3487 goto variable;
3488 }
3489 }
3490
3491 if (gfc_matching_procptr_assignment)
3492 {
3493 /* It can be a procedure or a derived-type procedure or a not-yet-known
3494 type. */
3495 if (sym->attr.flavor != FL_UNKNOWN
3496 && sym->attr.flavor != FL_PROCEDURE
3497 && sym->attr.flavor != FL_PARAMETER
3498 && sym->attr.flavor != FL_VARIABLE)
3499 {
3500 gfc_error ("Symbol at %C is not appropriate for an expression");
3501 return MATCH_ERROR;
3502 }
3503 goto procptr0;
3504 }
3505
3506 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
3507 goto function0;
3508
3509 if (sym->attr.generic)
3510 goto generic_function;
3511
3512 switch (sym->attr.flavor)
3513 {
3514 case FL_VARIABLE:
3515 variable:
3516 e = gfc_get_expr ();
3517
3518 e->expr_type = EXPR_VARIABLE;
3519 e->symtree = symtree;
3520
3521 m = gfc_match_varspec (e, 0, false, true);
3522 break;
3523
3524 case FL_PARAMETER:
3525 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
3526 end up here. Unfortunately, sym->value->expr_type is set to
3527 EXPR_CONSTANT, and so the if () branch would be followed without
3528 the !sym->as check. */
3529 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
3530 e = gfc_copy_expr (sym->value);
3531 else
3532 {
3533 e = gfc_get_expr ();
3534 e->expr_type = EXPR_VARIABLE;
3535 }
3536
3537 e->symtree = symtree;
3538 m = gfc_match_varspec (e, 0, false, true);
3539
3540 if (sym->ts.is_c_interop || sym->ts.is_iso_c)
3541 break;
3542
3543 /* Variable array references to derived type parameters cause
3544 all sorts of headaches in simplification. Treating such
3545 expressions as variable works just fine for all array
3546 references. */
3547 if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
3548 {
3549 for (ref = e->ref; ref; ref = ref->next)
3550 if (ref->type == REF_ARRAY)
3551 break;
3552
3553 if (ref == NULL || ref->u.ar.type == AR_FULL)
3554 break;
3555
3556 ref = e->ref;
3557 e->ref = NULL;
3558 gfc_free_expr (e);
3559 e = gfc_get_expr ();
3560 e->expr_type = EXPR_VARIABLE;
3561 e->symtree = symtree;
3562 e->ref = ref;
3563 }
3564
3565 break;
3566
3567 case FL_STRUCT:
3568 case FL_DERIVED:
3569 sym = gfc_use_derived (sym);
3570 if (sym == NULL)
3571 m = MATCH_ERROR;
3572 else
3573 goto generic_function;
3574 break;
3575
3576 /* If we're here, then the name is known to be the name of a
3577 procedure, yet it is not sure to be the name of a function. */
3578 case FL_PROCEDURE:
3579
3580 /* Procedure Pointer Assignments. */
3581 procptr0:
3582 if (gfc_matching_procptr_assignment)
3583 {
3584 gfc_gobble_whitespace ();
3585 if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
3586 /* Parse functions returning a procptr. */
3587 goto function0;
3588
3589 e = gfc_get_expr ();
3590 e->expr_type = EXPR_VARIABLE;
3591 e->symtree = symtree;
3592 m = gfc_match_varspec (e, 0, false, true);
3593 if (!e->ref && sym->attr.flavor == FL_UNKNOWN
3594 && sym->ts.type == BT_UNKNOWN
3595 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
3596 {
3597 m = MATCH_ERROR;
3598 break;
3599 }
3600 break;
3601 }
3602
3603 if (sym->attr.subroutine)
3604 {
3605 gfc_error ("Unexpected use of subroutine name %qs at %C",
3606 sym->name);
3607 m = MATCH_ERROR;
3608 break;
3609 }
3610
3611 /* At this point, the name has to be a non-statement function.
3612 If the name is the same as the current function being
3613 compiled, then we have a variable reference (to the function
3614 result) if the name is non-recursive. */
3615
3616 st = gfc_enclosing_unit (NULL);
3617
3618 if (st != NULL
3619 && st->state == COMP_FUNCTION
3620 && st->sym == sym
3621 && !sym->attr.recursive)
3622 {
3623 e = gfc_get_expr ();
3624 e->symtree = symtree;
3625 e->expr_type = EXPR_VARIABLE;
3626
3627 m = gfc_match_varspec (e, 0, false, true);
3628 break;
3629 }
3630
3631 /* Match a function reference. */
3632 function0:
3633 m = gfc_match_actual_arglist (0, &actual_arglist);
3634 if (m == MATCH_NO)
3635 {
3636 if (sym->attr.proc == PROC_ST_FUNCTION)
3637 gfc_error ("Statement function %qs requires argument list at %C",
3638 sym->name);
3639 else
3640 gfc_error ("Function %qs requires an argument list at %C",
3641 sym->name);
3642
3643 m = MATCH_ERROR;
3644 break;
3645 }
3646
3647 if (m != MATCH_YES)
3648 {
3649 m = MATCH_ERROR;
3650 break;
3651 }
3652
3653 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
3654 sym = symtree->n.sym;
3655
3656 replace_hidden_procptr_result (&sym, &symtree);
3657
3658 e = gfc_get_expr ();
3659 e->symtree = symtree;
3660 e->expr_type = EXPR_FUNCTION;
3661 e->value.function.actual = actual_arglist;
3662 e->where = gfc_current_locus;
3663
3664 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
3665 && CLASS_DATA (sym)->as)
3666 e->rank = CLASS_DATA (sym)->as->rank;
3667 else if (sym->as != NULL)
3668 e->rank = sym->as->rank;
3669
3670 if (!sym->attr.function
3671 && !gfc_add_function (&sym->attr, sym->name, NULL))
3672 {
3673 m = MATCH_ERROR;
3674 break;
3675 }
3676
3677 /* Check here for the existence of at least one argument for the
3678 iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED. The
3679 argument(s) given will be checked in gfc_iso_c_func_interface,
3680 during resolution of the function call. */
3681 if (sym->attr.is_iso_c == 1
3682 && (sym->from_intmod == INTMOD_ISO_C_BINDING
3683 && (sym->intmod_sym_id == ISOCBINDING_LOC
3684 || sym->intmod_sym_id == ISOCBINDING_FUNLOC
3685 || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
3686 {
3687 /* make sure we were given a param */
3688 if (actual_arglist == NULL)
3689 {
3690 gfc_error ("Missing argument to %qs at %C", sym->name);
3691 m = MATCH_ERROR;
3692 break;
3693 }
3694 }
3695
3696 if (sym->result == NULL)
3697 sym->result = sym;
3698
3699 gfc_gobble_whitespace ();
3700 /* F08:C612. */
3701 if (gfc_peek_ascii_char() == '%')
3702 {
3703 gfc_error ("The leftmost part-ref in a data-ref cannot be a "
3704 "function reference at %C");
3705 m = MATCH_ERROR;
3706 break;
3707 }
3708
3709 m = MATCH_YES;
3710 break;
3711
3712 case FL_UNKNOWN:
3713
3714 /* Special case for derived type variables that get their types
3715 via an IMPLICIT statement. This can't wait for the
3716 resolution phase. */
3717
3718 old_loc = gfc_current_locus;
3719 if (gfc_match_member_sep (sym) == MATCH_YES
3720 && sym->ts.type == BT_UNKNOWN
3721 && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
3722 gfc_set_default_type (sym, 0, sym->ns);
3723 gfc_current_locus = old_loc;
3724
3725 /* If the symbol has a (co)dimension attribute, the expression is a
3726 variable. */
3727
3728 if (sym->attr.dimension || sym->attr.codimension)
3729 {
3730 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
3731 {
3732 m = MATCH_ERROR;
3733 break;
3734 }
3735
3736 e = gfc_get_expr ();
3737 e->symtree = symtree;
3738 e->expr_type = EXPR_VARIABLE;
3739 m = gfc_match_varspec (e, 0, false, true);
3740 break;
3741 }
3742
3743 if (sym->ts.type == BT_CLASS && sym->attr.class_ok
3744 && (CLASS_DATA (sym)->attr.dimension
3745 || CLASS_DATA (sym)->attr.codimension))
3746 {
3747 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
3748 {
3749 m = MATCH_ERROR;
3750 break;
3751 }
3752
3753 e = gfc_get_expr ();
3754 e->symtree = symtree;
3755 e->expr_type = EXPR_VARIABLE;
3756 m = gfc_match_varspec (e, 0, false, true);
3757 break;
3758 }
3759
3760 /* Name is not an array, so we peek to see if a '(' implies a
3761 function call or a substring reference. Otherwise the
3762 variable is just a scalar. */
3763
3764 gfc_gobble_whitespace ();
3765 if (gfc_peek_ascii_char () != '(')
3766 {
3767 /* Assume a scalar variable */
3768 e = gfc_get_expr ();
3769 e->symtree = symtree;
3770 e->expr_type = EXPR_VARIABLE;
3771
3772 if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
3773 {
3774 m = MATCH_ERROR;
3775 break;
3776 }
3777
3778 /*FIXME:??? gfc_match_varspec does set this for us: */
3779 e->ts = sym->ts;
3780 m = gfc_match_varspec (e, 0, false, true);
3781 break;
3782 }
3783
3784 /* See if this is a function reference with a keyword argument
3785 as first argument. We do this because otherwise a spurious
3786 symbol would end up in the symbol table. */
3787
3788 old_loc = gfc_current_locus;
3789 m2 = gfc_match (" ( %n =", argname);
3790 gfc_current_locus = old_loc;
3791
3792 e = gfc_get_expr ();
3793 e->symtree = symtree;
3794
3795 if (m2 != MATCH_YES)
3796 {
3797 /* Try to figure out whether we're dealing with a character type.
3798 We're peeking ahead here, because we don't want to call
3799 match_substring if we're dealing with an implicitly typed
3800 non-character variable. */
3801 implicit_char = false;
3802 if (sym->ts.type == BT_UNKNOWN)
3803 {
3804 ts = gfc_get_default_type (sym->name, NULL);
3805 if (ts->type == BT_CHARACTER)
3806 implicit_char = true;
3807 }
3808
3809 /* See if this could possibly be a substring reference of a name
3810 that we're not sure is a variable yet. */
3811
3812 if ((implicit_char || sym->ts.type == BT_CHARACTER)
3813 && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
3814 {
3815
3816 e->expr_type = EXPR_VARIABLE;
3817
3818 if (sym->attr.flavor != FL_VARIABLE
3819 && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
3820 sym->name, NULL))
3821 {
3822 m = MATCH_ERROR;
3823 break;
3824 }
3825
3826 if (sym->ts.type == BT_UNKNOWN
3827 && !gfc_set_default_type (sym, 1, NULL))
3828 {
3829 m = MATCH_ERROR;
3830 break;
3831 }
3832
3833 e->ts = sym->ts;
3834 if (e->ref)
3835 e->ts.u.cl = NULL;
3836 m = MATCH_YES;
3837 break;
3838 }
3839 }
3840
3841 /* Give up, assume we have a function. */
3842
3843 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
3844 sym = symtree->n.sym;
3845 e->expr_type = EXPR_FUNCTION;
3846
3847 if (!sym->attr.function
3848 && !gfc_add_function (&sym->attr, sym->name, NULL))
3849 {
3850 m = MATCH_ERROR;
3851 break;
3852 }
3853
3854 sym->result = sym;
3855
3856 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3857 if (m == MATCH_NO)
3858 gfc_error ("Missing argument list in function %qs at %C", sym->name);
3859
3860 if (m != MATCH_YES)
3861 {
3862 m = MATCH_ERROR;
3863 break;
3864 }
3865
3866 /* If our new function returns a character, array or structure
3867 type, it might have subsequent references. */
3868
3869 m = gfc_match_varspec (e, 0, false, true);
3870 if (m == MATCH_NO)
3871 m = MATCH_YES;
3872
3873 break;
3874
3875 generic_function:
3876 /* Look for symbol first; if not found, look for STRUCTURE type symbol
3877 specially. Creates a generic symbol for derived types. */
3878 gfc_find_sym_tree (name, NULL, 1, &symtree);
3879 if (!symtree)
3880 gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
3881 if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
3882 gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
3883
3884 e = gfc_get_expr ();
3885 e->symtree = symtree;
3886 e->expr_type = EXPR_FUNCTION;
3887
3888 if (gfc_fl_struct (sym->attr.flavor))
3889 {
3890 e->value.function.esym = sym;
3891 e->symtree->n.sym->attr.generic = 1;
3892 }
3893
3894 m = gfc_match_actual_arglist (0, &e->value.function.actual);
3895 break;
3896
3897 case FL_NAMELIST:
3898 m = MATCH_ERROR;
3899 break;
3900
3901 default:
3902 gfc_error ("Symbol at %C is not appropriate for an expression");
3903 return MATCH_ERROR;
3904 }
3905
3906 if (m == MATCH_YES)
3907 {
3908 e->where = where;
3909 *result = e;
3910 }
3911 else
3912 gfc_free_expr (e);
3913
3914 return m;
3915 }
3916
3917
3918 /* Match a variable, i.e. something that can be assigned to. This
3919 starts as a symbol, can be a structure component or an array
3920 reference. It can be a function if the function doesn't have a
3921 separate RESULT variable. If the symbol has not been previously
3922 seen, we assume it is a variable.
3923
3924 This function is called by two interface functions:
3925 gfc_match_variable, which has host_flag = 1, and
3926 gfc_match_equiv_variable, with host_flag = 0, to restrict the
3927 match of the symbol to the local scope. */
3928
3929 static match
3930 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
3931 {
3932 gfc_symbol *sym, *dt_sym;
3933 gfc_symtree *st;
3934 gfc_expr *expr;
3935 locus where, old_loc;
3936 match m;
3937
3938 /* Since nothing has any business being an lvalue in a module
3939 specification block, an interface block or a contains section,
3940 we force the changed_symbols mechanism to work by setting
3941 host_flag to 0. This prevents valid symbols that have the name
3942 of keywords, such as 'end', being turned into variables by
3943 failed matching to assignments for, e.g., END INTERFACE. */
3944 if (gfc_current_state () == COMP_MODULE
3945 || gfc_current_state () == COMP_SUBMODULE
3946 || gfc_current_state () == COMP_INTERFACE
3947 || gfc_current_state () == COMP_CONTAINS)
3948 host_flag = 0;
3949
3950 where = gfc_current_locus;
3951 m = gfc_match_sym_tree (&st, host_flag);
3952 if (m != MATCH_YES)
3953 return m;
3954
3955 sym = st->n.sym;
3956
3957 /* If this is an implicit do loop index and implicitly typed,
3958 it should not be host associated. */
3959 m = check_for_implicit_index (&st, &sym);
3960 if (m != MATCH_YES)
3961 return m;
3962
3963 sym->attr.implied_index = 0;
3964
3965 gfc_set_sym_referenced (sym);
3966
3967 /* STRUCTUREs may share names with variables, but derived types may not. */
3968 if (sym->attr.flavor == FL_PROCEDURE && sym->generic
3969 && (dt_sym = gfc_find_dt_in_generic (sym)))
3970 {
3971 if (dt_sym->attr.flavor == FL_DERIVED)
3972 gfc_error ("Derived type %qs cannot be used as a variable at %C",
3973 sym->name);
3974 return MATCH_ERROR;
3975 }
3976
3977 switch (sym->attr.flavor)
3978 {
3979 case FL_VARIABLE:
3980 /* Everything is alright. */
3981 break;
3982
3983 case FL_UNKNOWN:
3984 {
3985 sym_flavor flavor = FL_UNKNOWN;
3986
3987 gfc_gobble_whitespace ();
3988
3989 if (sym->attr.external || sym->attr.procedure
3990 || sym->attr.function || sym->attr.subroutine)
3991 flavor = FL_PROCEDURE;
3992
3993 /* If it is not a procedure, is not typed and is host associated,
3994 we cannot give it a flavor yet. */
3995 else if (sym->ns == gfc_current_ns->parent
3996 && sym->ts.type == BT_UNKNOWN)
3997 break;
3998
3999 /* These are definitive indicators that this is a variable. */
4000 else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
4001 || sym->attr.pointer || sym->as != NULL)
4002 flavor = FL_VARIABLE;
4003
4004 if (flavor != FL_UNKNOWN
4005 && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
4006 return MATCH_ERROR;
4007 }
4008 break;
4009
4010 case FL_PARAMETER:
4011 if (equiv_flag)
4012 {
4013 gfc_error ("Named constant at %C in an EQUIVALENCE");
4014 return MATCH_ERROR;
4015 }
4016 /* Otherwise this is checked for and an error given in the
4017 variable definition context checks. */
4018 break;
4019
4020 case FL_PROCEDURE:
4021 /* Check for a nonrecursive function result variable. */
4022 if (sym->attr.function
4023 && !sym->attr.external
4024 && sym->result == sym
4025 && (gfc_is_function_return_value (sym, gfc_current_ns)
4026 || (sym->attr.entry
4027 && sym->ns == gfc_current_ns)
4028 || (sym->attr.entry
4029 && sym->ns == gfc_current_ns->parent)))
4030 {
4031 /* If a function result is a derived type, then the derived
4032 type may still have to be resolved. */
4033
4034 if (sym->ts.type == BT_DERIVED
4035 && gfc_use_derived (sym->ts.u.derived) == NULL)
4036 return MATCH_ERROR;
4037 break;
4038 }
4039
4040 if (sym->attr.proc_pointer
4041 || replace_hidden_procptr_result (&sym, &st))
4042 break;
4043
4044 /* Fall through to error */
4045 gcc_fallthrough ();
4046
4047 default:
4048 gfc_error ("%qs at %C is not a variable", sym->name);
4049 return MATCH_ERROR;
4050 }
4051
4052 /* Special case for derived type variables that get their types
4053 via an IMPLICIT statement. This can't wait for the
4054 resolution phase. */
4055
4056 {
4057 gfc_namespace * implicit_ns;
4058
4059 if (gfc_current_ns->proc_name == sym)
4060 implicit_ns = gfc_current_ns;
4061 else
4062 implicit_ns = sym->ns;
4063
4064 old_loc = gfc_current_locus;
4065 if (gfc_match_member_sep (sym) == MATCH_YES
4066 && sym->ts.type == BT_UNKNOWN
4067 && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
4068 gfc_set_default_type (sym, 0, implicit_ns);
4069 gfc_current_locus = old_loc;
4070 }
4071
4072 expr = gfc_get_expr ();
4073
4074 expr->expr_type = EXPR_VARIABLE;
4075 expr->symtree = st;
4076 expr->ts = sym->ts;
4077 expr->where = where;
4078
4079 /* Now see if we have to do more. */
4080 m = gfc_match_varspec (expr, equiv_flag, false, false);
4081 if (m != MATCH_YES)
4082 {
4083 gfc_free_expr (expr);
4084 return m;
4085 }
4086
4087 *result = expr;
4088 return MATCH_YES;
4089 }
4090
4091
4092 match
4093 gfc_match_variable (gfc_expr **result, int equiv_flag)
4094 {
4095 return match_variable (result, equiv_flag, 1);
4096 }
4097
4098
4099 match
4100 gfc_match_equiv_variable (gfc_expr **result)
4101 {
4102 return match_variable (result, 1, 0);
4103 }
4104