]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/primary.c
* cfganal.c: Fix a reference to Harvey's paper.
[thirdparty/gcc.git] / gcc / fortran / primary.c
CommitLineData
6de9cd9a 1/* Primary expression subroutines
da8309c6
TS
2 Copyright (C) 2000, 2001, 2002, 2004, 2005 Free Software Foundation,
3 Inc.
6de9cd9a
DN
4 Contributed by Andy Vaught
5
d1d61a00 6This file is part of GCC.
6de9cd9a 7
d1d61a00
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
6de9cd9a 12
d1d61a00
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
d1d61a00
TS
19along with GCC; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
6de9cd9a
DN
22
23
24#include "config.h"
25#include "system.h"
26#include "flags.h"
6de9cd9a
DN
27#include "gfortran.h"
28#include "arith.h"
29#include "match.h"
30#include "parse.h"
31
32/* Matches a kind-parameter expression, which is either a named
33 symbolic constant or a nonnegative integer constant. If
34 successful, sets the kind value to the correct integer. */
35
36static match
37match_kind_param (int *kind)
38{
39 char name[GFC_MAX_SYMBOL_LEN + 1];
40 gfc_symbol *sym;
41 const char *p;
42 match m;
43
44 m = gfc_match_small_literal_int (kind);
45 if (m != MATCH_NO)
46 return m;
47
48 m = gfc_match_name (name);
49 if (m != MATCH_YES)
50 return m;
51
52 if (gfc_find_symbol (name, NULL, 1, &sym))
53 return MATCH_ERROR;
54
55 if (sym == NULL)
56 return MATCH_NO;
57
58 if (sym->attr.flavor != FL_PARAMETER)
59 return MATCH_NO;
60
61 p = gfc_extract_int (sym->value, kind);
62 if (p != NULL)
63 return MATCH_NO;
64
65 if (*kind < 0)
66 return MATCH_NO;
67
68 return MATCH_YES;
69}
70
71
72/* Get a trailing kind-specification for non-character variables.
73 Returns:
74 the integer kind value or:
75 -1 if an error was generated
76 -2 if no kind was found */
77
78static int
79get_kind (void)
80{
81 int kind;
82 match m;
83
84 if (gfc_match_char ('_') != MATCH_YES)
85 return -2;
86
87 m = match_kind_param (&kind);
88 if (m == MATCH_NO)
89 gfc_error ("Missing kind-parameter at %C");
90
91 return (m == MATCH_YES) ? kind : -1;
92}
93
94
95/* Given a character and a radix, see if the character is a valid
96 digit in that radix. */
97
98static int
99check_digit (int c, int radix)
100{
101 int r;
102
103 switch (radix)
104 {
105 case 2:
106 r = ('0' <= c && c <= '1');
107 break;
108
109 case 8:
110 r = ('0' <= c && c <= '7');
111 break;
112
113 case 10:
114 r = ('0' <= c && c <= '9');
115 break;
116
117 case 16:
da8309c6 118 r = ISXDIGIT (c);
6de9cd9a
DN
119 break;
120
121 default:
122 gfc_internal_error ("check_digit(): bad radix");
123 }
124
125 return r;
126}
127
128
129/* Match the digit string part of an integer if signflag is not set,
130 the signed digit string part if signflag is set. If the buffer
131 is NULL, we just count characters for the resolution pass. Returns
132 the number of characters matched, -1 for no match. */
133
134static int
135match_digits (int signflag, int radix, char *buffer)
136{
137 locus old_loc;
138 int length, c;
139
140 length = 0;
141 c = gfc_next_char ();
142
143 if (signflag && (c == '+' || c == '-'))
144 {
145 if (buffer != NULL)
146 *buffer++ = c;
147 c = gfc_next_char ();
148 length++;
149 }
150
151 if (!check_digit (c, radix))
152 return -1;
153
154 length++;
155 if (buffer != NULL)
156 *buffer++ = c;
157
158 for (;;)
159 {
63645982 160 old_loc = gfc_current_locus;
6de9cd9a
DN
161 c = gfc_next_char ();
162
163 if (!check_digit (c, radix))
164 break;
165
166 if (buffer != NULL)
167 *buffer++ = c;
168 length++;
169 }
170
63645982 171 gfc_current_locus = old_loc;
6de9cd9a
DN
172
173 return length;
174}
175
176
177/* Match an integer (digit string and optional kind).
178 A sign will be accepted if signflag is set. */
179
180static match
181match_integer_constant (gfc_expr ** result, int signflag)
182{
183 int length, kind;
184 locus old_loc;
185 char *buffer;
186 gfc_expr *e;
187
63645982 188 old_loc = gfc_current_locus;
6de9cd9a
DN
189 gfc_gobble_whitespace ();
190
191 length = match_digits (signflag, 10, NULL);
63645982 192 gfc_current_locus = old_loc;
6de9cd9a
DN
193 if (length == -1)
194 return MATCH_NO;
195
196 buffer = alloca (length + 1);
197 memset (buffer, '\0', length + 1);
198
199 gfc_gobble_whitespace ();
200
201 match_digits (signflag, 10, buffer);
202
203 kind = get_kind ();
204 if (kind == -2)
9d64df18 205 kind = gfc_default_integer_kind;
6de9cd9a
DN
206 if (kind == -1)
207 return MATCH_ERROR;
208
e7a2d5fb 209 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
6de9cd9a
DN
210 {
211 gfc_error ("Integer kind %d at %C not available", kind);
212 return MATCH_ERROR;
213 }
214
63645982 215 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
6de9cd9a
DN
216
217 if (gfc_range_check (e) != ARITH_OK)
218 {
219 gfc_error ("Integer too big for its kind at %C");
220
221 gfc_free_expr (e);
222 return MATCH_ERROR;
223 }
224
225 *result = e;
226 return MATCH_YES;
227}
228
229
230/* Match a binary, octal or hexadecimal constant that can be found in
231 a DATA statement. */
232
233static match
234match_boz_constant (gfc_expr ** result)
235{
5d874166 236 int radix, delim, length, x_hex, kind;
6de9cd9a
DN
237 locus old_loc;
238 char *buffer;
239 gfc_expr *e;
240 const char *rname;
241
63645982 242 old_loc = gfc_current_locus;
6de9cd9a
DN
243 gfc_gobble_whitespace ();
244
88199e7c 245 x_hex = 0;
6de9cd9a
DN
246 switch (gfc_next_char ())
247 {
248 case 'b':
249 radix = 2;
250 rname = "binary";
251 break;
252 case 'o':
253 radix = 8;
254 rname = "octal";
255 break;
256 case 'x':
88199e7c 257 x_hex = 1;
6de9cd9a
DN
258 /* Fall through. */
259 case 'z':
260 radix = 16;
261 rname = "hexadecimal";
262 break;
263 default:
264 goto backup;
265 }
266
267 /* No whitespace allowed here. */
268
269 delim = gfc_next_char ();
270 if (delim != '\'' && delim != '\"')
271 goto backup;
272
5d874166
TS
273 if (x_hex && pedantic
274 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
275 "constant at %C uses non-standard syntax.")
276 == FAILURE))
277 return MATCH_ERROR;
278
63645982 279 old_loc = gfc_current_locus;
6de9cd9a
DN
280
281 length = match_digits (0, radix, NULL);
282 if (length == -1)
283 {
284 gfc_error ("Empty set of digits in %s constants at %C", rname);
285 return MATCH_ERROR;
286 }
287
288 if (gfc_next_char () != delim)
289 {
290 gfc_error ("Illegal character in %s constant at %C.", rname);
291 return MATCH_ERROR;
292 }
293
63645982 294 gfc_current_locus = old_loc;
6de9cd9a
DN
295
296 buffer = alloca (length + 1);
297 memset (buffer, '\0', length + 1);
298
299 match_digits (0, radix, buffer);
5d874166
TS
300 gfc_next_char (); /* Eat delimiter. */
301
302 kind = get_kind ();
303 if (kind == -1)
304 return MATCH_ERROR;
305 if (kind == -2)
306 kind = gfc_default_integer_kind;
307 else if (pedantic
308 && (gfc_notify_std (GFC_STD_GNU, "Extension: Kind parameter "
309 "suffix to boz literal constant at %C.")
310 == FAILURE))
311 return MATCH_ERROR;
6de9cd9a 312
5d874166 313 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
6de9cd9a
DN
314
315 if (gfc_range_check (e) != ARITH_OK)
316 {
5d874166 317 gfc_error ("Integer too big for integer kind %i at %C", kind);
6de9cd9a 318
88199e7c
TS
319 gfc_free_expr (e);
320 return MATCH_ERROR;
321 }
322
6de9cd9a
DN
323 *result = e;
324 return MATCH_YES;
325
326backup:
63645982 327 gfc_current_locus = old_loc;
6de9cd9a
DN
328 return MATCH_NO;
329}
330
331
332/* Match a real constant of some sort. */
333
334static match
335match_real_constant (gfc_expr ** result, int signflag)
336{
337 int kind, c, count, seen_dp, seen_digits, exp_char;
338 locus old_loc, temp_loc;
339 char *p, *buffer;
340 gfc_expr *e;
341
63645982 342 old_loc = gfc_current_locus;
6de9cd9a
DN
343 gfc_gobble_whitespace ();
344
345 e = NULL;
346
347 count = 0;
348 seen_dp = 0;
349 seen_digits = 0;
350 exp_char = ' ';
351
352 c = gfc_next_char ();
353 if (signflag && (c == '+' || c == '-'))
354 {
355 c = gfc_next_char ();
356 count++;
357 }
358
359 /* Scan significand. */
360 for (;; c = gfc_next_char (), count++)
361 {
362 if (c == '.')
363 {
364 if (seen_dp)
365 goto done;
366
367 /* Check to see if "." goes with a following operator like ".eq.". */
63645982 368 temp_loc = gfc_current_locus;
6de9cd9a
DN
369 c = gfc_next_char ();
370
371 if (c == 'e' || c == 'd' || c == 'q')
372 {
373 c = gfc_next_char ();
374 if (c == '.')
f7b529fa 375 goto done; /* Operator named .e. or .d. */
6de9cd9a
DN
376 }
377
378 if (ISALPHA (c))
379 goto done; /* Distinguish 1.e9 from 1.eq.2 */
380
63645982 381 gfc_current_locus = temp_loc;
6de9cd9a
DN
382 seen_dp = 1;
383 continue;
384 }
385
386 if (ISDIGIT (c))
387 {
388 seen_digits = 1;
389 continue;
390 }
391
392 break;
393 }
394
395 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
396 goto done;
397 exp_char = c;
398
399 /* Scan exponent. */
400 c = gfc_next_char ();
401 count++;
402
403 if (c == '+' || c == '-')
404 { /* optional sign */
405 c = gfc_next_char ();
406 count++;
407 }
408
409 if (!ISDIGIT (c))
410 {
411 /* TODO: seen_digits is always true at this point */
412 if (!seen_digits)
413 {
63645982 414 gfc_current_locus = old_loc;
6de9cd9a
DN
415 return MATCH_NO; /* ".e" can be something else */
416 }
417
418 gfc_error ("Missing exponent in real number at %C");
419 return MATCH_ERROR;
420 }
421
422 while (ISDIGIT (c))
423 {
424 c = gfc_next_char ();
425 count++;
426 }
427
428done:
429 /* See what we've got! */
430 if (!seen_digits || (!seen_dp && exp_char == ' '))
431 {
63645982 432 gfc_current_locus = old_loc;
6de9cd9a
DN
433 return MATCH_NO;
434 }
435
436 /* Convert the number. */
63645982 437 gfc_current_locus = old_loc;
6de9cd9a
DN
438 gfc_gobble_whitespace ();
439
440 buffer = alloca (count + 1);
441 memset (buffer, '\0', count + 1);
442
f8e566e5 443 /* Hack for mpfr_set_str(). */
6de9cd9a
DN
444 p = buffer;
445 while (count > 0)
446 {
447 *p = gfc_next_char ();
448 if (*p == 'd' || *p == 'q')
449 *p = 'e';
450 p++;
451 count--;
452 }
453
454 kind = get_kind ();
455 if (kind == -1)
456 goto cleanup;
457
458 switch (exp_char)
459 {
460 case 'd':
461 if (kind != -2)
462 {
463 gfc_error
464 ("Real number at %C has a 'd' exponent and an explicit kind");
465 goto cleanup;
466 }
9d64df18 467 kind = gfc_default_double_kind;
6de9cd9a
DN
468 break;
469
470 case 'q':
471 if (kind != -2)
472 {
473 gfc_error
474 ("Real number at %C has a 'q' exponent and an explicit kind");
475 goto cleanup;
476 }
477 kind = gfc_option.q_kind;
478 break;
479
480 default:
481 if (kind == -2)
9d64df18 482 kind = gfc_default_real_kind;
6de9cd9a 483
e7a2d5fb 484 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
6de9cd9a
DN
485 {
486 gfc_error ("Invalid real kind %d at %C", kind);
487 goto cleanup;
488 }
489 }
490
63645982 491 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
6de9cd9a
DN
492
493 switch (gfc_range_check (e))
494 {
495 case ARITH_OK:
496 break;
497 case ARITH_OVERFLOW:
498 gfc_error ("Real constant overflows its kind at %C");
499 goto cleanup;
500
501 case ARITH_UNDERFLOW:
2d8b59df
SK
502 if (gfc_option.warn_underflow)
503 gfc_warning ("Real constant underflows its kind at %C");
f8e566e5 504 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
2d8b59df 505 break;
6de9cd9a
DN
506
507 default:
508 gfc_internal_error ("gfc_range_check() returned bad value");
509 }
510
511 *result = e;
512 return MATCH_YES;
513
514cleanup:
515 gfc_free_expr (e);
516 return MATCH_ERROR;
517}
518
519
520/* Match a substring reference. */
521
522static match
523match_substring (gfc_charlen * cl, int init, gfc_ref ** result)
524{
525 gfc_expr *start, *end;
526 locus old_loc;
527 gfc_ref *ref;
528 match m;
529
530 start = NULL;
531 end = NULL;
532
63645982 533 old_loc = gfc_current_locus;
6de9cd9a
DN
534
535 m = gfc_match_char ('(');
536 if (m != MATCH_YES)
537 return MATCH_NO;
538
539 if (gfc_match_char (':') != MATCH_YES)
540 {
541 if (init)
542 m = gfc_match_init_expr (&start);
543 else
544 m = gfc_match_expr (&start);
545
546 if (m != MATCH_YES)
547 {
548 m = MATCH_NO;
549 goto cleanup;
550 }
551
552 m = gfc_match_char (':');
553 if (m != MATCH_YES)
554 goto cleanup;
555 }
556
557 if (gfc_match_char (')') != MATCH_YES)
558 {
559 if (init)
560 m = gfc_match_init_expr (&end);
561 else
562 m = gfc_match_expr (&end);
563
564 if (m == MATCH_NO)
565 goto syntax;
566 if (m == MATCH_ERROR)
567 goto cleanup;
568
569 m = gfc_match_char (')');
570 if (m == MATCH_NO)
571 goto syntax;
572 }
573
574 /* Optimize away the (:) reference. */
575 if (start == NULL && end == NULL)
576 ref = NULL;
577 else
578 {
579 ref = gfc_get_ref ();
580
581 ref->type = REF_SUBSTRING;
582 if (start == NULL)
583 start = gfc_int_expr (1);
584 ref->u.ss.start = start;
585 if (end == NULL && cl)
586 end = gfc_copy_expr (cl->length);
587 ref->u.ss.end = end;
588 ref->u.ss.length = cl;
589 }
590
591 *result = ref;
592 return MATCH_YES;
593
594syntax:
595 gfc_error ("Syntax error in SUBSTRING specification at %C");
596 m = MATCH_ERROR;
597
598cleanup:
599 gfc_free_expr (start);
600 gfc_free_expr (end);
601
63645982 602 gfc_current_locus = old_loc;
6de9cd9a
DN
603 return m;
604}
605
606
607/* Reads the next character of a string constant, taking care to
608 return doubled delimiters on the input as a single instance of
609 the delimiter.
610
611 Special return values are:
612 -1 End of the string, as determined by the delimiter
613 -2 Unterminated string detected
614
615 Backslash codes are also expanded at this time. */
616
617static int
618next_string_char (char delimiter)
619{
620 locus old_locus;
621 int c;
622
623 c = gfc_next_char_literal (1);
624
625 if (c == '\n')
626 return -2;
627
628 if (c == '\\')
629 {
63645982 630 old_locus = gfc_current_locus;
6de9cd9a
DN
631
632 switch (gfc_next_char_literal (1))
633 {
634 case 'a':
635 c = '\a';
636 break;
637 case 'b':
638 c = '\b';
639 break;
640 case 't':
641 c = '\t';
642 break;
643 case 'f':
644 c = '\f';
645 break;
646 case 'n':
647 c = '\n';
648 break;
649 case 'r':
650 c = '\r';
651 break;
652 case 'v':
653 c = '\v';
654 break;
655 case '\\':
656 c = '\\';
657 break;
658
659 default:
660 /* Unknown backslash codes are simply not expanded */
63645982 661 gfc_current_locus = old_locus;
6de9cd9a
DN
662 break;
663 }
664 }
665
666 if (c != delimiter)
667 return c;
668
63645982 669 old_locus = gfc_current_locus;
6de9cd9a
DN
670 c = gfc_next_char_literal (1);
671
672 if (c == delimiter)
673 return c;
63645982 674 gfc_current_locus = old_locus;
6de9cd9a
DN
675
676 return -1;
677}
678
679
680/* Special case of gfc_match_name() that matches a parameter kind name
681 before a string constant. This takes case of the weird but legal
682 case of: weird case of:
683
684 kind_____'string'
685
686 where kind____ is a parameter. gfc_match_name() will happily slurp
687 up all the underscores, which leads to problems. If we return
688 MATCH_YES, the parse pointer points to the final underscore, which
689 is not part of the name. We never return MATCH_ERROR-- errors in
690 the name will be detected later. */
691
692static match
693match_charkind_name (char *name)
694{
695 locus old_loc;
696 char c, peek;
697 int len;
698
699 gfc_gobble_whitespace ();
700 c = gfc_next_char ();
701 if (!ISALPHA (c))
702 return MATCH_NO;
703
704 *name++ = c;
705 len = 1;
706
707 for (;;)
708 {
63645982 709 old_loc = gfc_current_locus;
6de9cd9a
DN
710 c = gfc_next_char ();
711
712 if (c == '_')
713 {
714 peek = gfc_peek_char ();
715
716 if (peek == '\'' || peek == '\"')
717 {
63645982 718 gfc_current_locus = old_loc;
6de9cd9a
DN
719 *name = '\0';
720 return MATCH_YES;
721 }
722 }
723
724 if (!ISALNUM (c)
725 && c != '_'
726 && (gfc_option.flag_dollar_ok && c != '$'))
727 break;
728
729 *name++ = c;
730 if (++len > GFC_MAX_SYMBOL_LEN)
731 break;
732 }
733
734 return MATCH_NO;
735}
736
737
738/* See if the current input matches a character constant. Lots of
739 contortions have to be done to match the kind parameter which comes
740 before the actual string. The main consideration is that we don't
741 want to error out too quickly. For example, we don't actually do
742 any validation of the kinds until we have actually seen a legal
743 delimiter. Using match_kind_param() generates errors too quickly. */
744
745static match
746match_string_constant (gfc_expr ** result)
747{
748 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
749 int i, c, kind, length, delimiter;
750 locus old_locus, start_locus;
751 gfc_symbol *sym;
752 gfc_expr *e;
753 const char *q;
754 match m;
755
63645982 756 old_locus = gfc_current_locus;
6de9cd9a
DN
757
758 gfc_gobble_whitespace ();
759
63645982 760 start_locus = gfc_current_locus;
6de9cd9a
DN
761
762 c = gfc_next_char ();
763 if (c == '\'' || c == '"')
764 {
9d64df18 765 kind = gfc_default_character_kind;
6de9cd9a
DN
766 goto got_delim;
767 }
768
769 if (ISDIGIT (c))
770 {
771 kind = 0;
772
773 while (ISDIGIT (c))
774 {
775 kind = kind * 10 + c - '0';
776 if (kind > 9999999)
777 goto no_match;
778 c = gfc_next_char ();
779 }
780
781 }
782 else
783 {
63645982 784 gfc_current_locus = old_locus;
6de9cd9a
DN
785
786 m = match_charkind_name (name);
787 if (m != MATCH_YES)
788 goto no_match;
789
790 if (gfc_find_symbol (name, NULL, 1, &sym)
791 || sym == NULL
792 || sym->attr.flavor != FL_PARAMETER)
793 goto no_match;
794
795 kind = -1;
796 c = gfc_next_char ();
797 }
798
799 if (c == ' ')
800 {
801 gfc_gobble_whitespace ();
802 c = gfc_next_char ();
803 }
804
805 if (c != '_')
806 goto no_match;
807
808 gfc_gobble_whitespace ();
63645982 809 start_locus = gfc_current_locus;
6de9cd9a
DN
810
811 c = gfc_next_char ();
812 if (c != '\'' && c != '"')
813 goto no_match;
814
815 if (kind == -1)
816 {
817 q = gfc_extract_int (sym->value, &kind);
818 if (q != NULL)
819 {
820 gfc_error (q);
821 return MATCH_ERROR;
822 }
823 }
824
e7a2d5fb 825 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
826 {
827 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
828 return MATCH_ERROR;
829 }
830
831got_delim:
832 /* Scan the string into a block of memory by first figuring out how
833 long it is, allocating the structure, then re-reading it. This
834 isn't particularly efficient, but string constants aren't that
835 common in most code. TODO: Use obstacks? */
836
837 delimiter = c;
838 length = 0;
839
840 for (;;)
841 {
842 c = next_string_char (delimiter);
843 if (c == -1)
844 break;
845 if (c == -2)
846 {
63645982 847 gfc_current_locus = start_locus;
6de9cd9a
DN
848 gfc_error ("Unterminated character constant beginning at %C");
849 return MATCH_ERROR;
850 }
851
852 length++;
853 }
854
855 e = gfc_get_expr ();
856
857 e->expr_type = EXPR_CONSTANT;
858 e->ref = NULL;
859 e->ts.type = BT_CHARACTER;
860 e->ts.kind = kind;
861 e->where = start_locus;
862
863 e->value.character.string = p = gfc_getmem (length + 1);
864 e->value.character.length = length;
865
63645982 866 gfc_current_locus = start_locus;
6de9cd9a
DN
867 gfc_next_char (); /* Skip delimiter */
868
869 for (i = 0; i < length; i++)
870 *p++ = next_string_char (delimiter);
871
872 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
873
874 if (next_string_char (delimiter) != -1)
875 gfc_internal_error ("match_string_constant(): Delimiter not found");
876
877 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
878 e->expr_type = EXPR_SUBSTRING;
879
880 *result = e;
881
882 return MATCH_YES;
883
884no_match:
63645982 885 gfc_current_locus = old_locus;
6de9cd9a
DN
886 return MATCH_NO;
887}
888
889
890/* Match a .true. or .false. */
891
892static match
893match_logical_constant (gfc_expr ** result)
894{
895 static mstring logical_ops[] = {
896 minit (".false.", 0),
897 minit (".true.", 1),
898 minit (NULL, -1)
899 };
900
901 gfc_expr *e;
902 int i, kind;
903
904 i = gfc_match_strings (logical_ops);
905 if (i == -1)
906 return MATCH_NO;
907
908 kind = get_kind ();
909 if (kind == -1)
910 return MATCH_ERROR;
911 if (kind == -2)
9d64df18 912 kind = gfc_default_logical_kind;
6de9cd9a 913
e7a2d5fb 914 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
6de9cd9a
DN
915 gfc_error ("Bad kind for logical constant at %C");
916
917 e = gfc_get_expr ();
918
919 e->expr_type = EXPR_CONSTANT;
920 e->value.logical = i;
921 e->ts.type = BT_LOGICAL;
922 e->ts.kind = kind;
63645982 923 e->where = gfc_current_locus;
6de9cd9a
DN
924
925 *result = e;
926 return MATCH_YES;
927}
928
929
930/* Match a real or imaginary part of a complex constant that is a
931 symbolic constant. */
932
933static match
934match_sym_complex_part (gfc_expr ** result)
935{
936 char name[GFC_MAX_SYMBOL_LEN + 1];
937 gfc_symbol *sym;
938 gfc_expr *e;
939 match m;
940
941 m = gfc_match_name (name);
942 if (m != MATCH_YES)
943 return m;
944
945 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
946 return MATCH_NO;
947
948 if (sym->attr.flavor != FL_PARAMETER)
949 {
950 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
951 return MATCH_ERROR;
952 }
953
954 if (!gfc_numeric_ts (&sym->value->ts))
955 {
956 gfc_error ("Numeric PARAMETER required in complex constant at %C");
957 return MATCH_ERROR;
958 }
959
960 if (sym->value->rank != 0)
961 {
962 gfc_error ("Scalar PARAMETER required in complex constant at %C");
963 return MATCH_ERROR;
964 }
965
966 switch (sym->value->ts.type)
967 {
968 case BT_REAL:
969 e = gfc_copy_expr (sym->value);
970 break;
971
972 case BT_COMPLEX:
973 e = gfc_complex2real (sym->value, sym->value->ts.kind);
974 if (e == NULL)
975 goto error;
976 break;
977
978 case BT_INTEGER:
9d64df18 979 e = gfc_int2real (sym->value, gfc_default_real_kind);
6de9cd9a
DN
980 if (e == NULL)
981 goto error;
982 break;
983
984 default:
985 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
986 }
987
988 *result = e; /* e is a scalar, real, constant expression */
989 return MATCH_YES;
990
991error:
992 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
993 return MATCH_ERROR;
994}
995
996
997/* Match the real and imaginary parts of a complex number. This
998 subroutine is essentially match_real_constant() modified in a
999 couple of ways: A sign is always allowed and numbers that would
1000 look like an integer to match_real_constant() are automatically
1001 created as floating point numbers. The messiness involved with
1002 making sure a decimal point belongs to the number and not a
1003 trailing operator is not necessary here either (Hooray!). */
1004
1005static match
1006match_const_complex_part (gfc_expr ** result)
1007{
1008 int kind, seen_digits, seen_dp, count;
1009 char *p, c, exp_char, *buffer;
1010 locus old_loc;
1011
63645982 1012 old_loc = gfc_current_locus;
6de9cd9a
DN
1013 gfc_gobble_whitespace ();
1014
1015 seen_dp = 0;
1016 seen_digits = 0;
1017 count = 0;
1018 exp_char = ' ';
1019
1020 c = gfc_next_char ();
1021 if (c == '-' || c == '+')
1022 {
1023 c = gfc_next_char ();
1024 count++;
1025 }
1026
1027 for (;; c = gfc_next_char (), count++)
1028 {
1029 if (c == '.')
1030 {
1031 if (seen_dp)
1032 goto no_match;
1033 seen_dp = 1;
1034 continue;
1035 }
1036
1037 if (ISDIGIT (c))
1038 {
1039 seen_digits = 1;
1040 continue;
1041 }
1042
1043 break;
1044 }
1045
1046 if (!seen_digits || (c != 'd' && c != 'e'))
1047 goto done;
1048 exp_char = c;
1049
1050 /* Scan exponent. */
1051 c = gfc_next_char ();
1052 count++;
1053
1054 if (c == '+' || c == '-')
1055 { /* optional sign */
1056 c = gfc_next_char ();
1057 count++;
1058 }
1059
1060 if (!ISDIGIT (c))
1061 {
1062 gfc_error ("Missing exponent in real number at %C");
1063 return MATCH_ERROR;
1064 }
1065
1066 while (ISDIGIT (c))
1067 {
1068 c = gfc_next_char ();
1069 count++;
1070 }
1071
1072done:
1073 if (!seen_digits)
1074 goto no_match;
1075
1076 /* Convert the number. */
63645982 1077 gfc_current_locus = old_loc;
6de9cd9a
DN
1078 gfc_gobble_whitespace ();
1079
1080 buffer = alloca (count + 1);
1081 memset (buffer, '\0', count + 1);
1082
f8e566e5 1083 /* Hack for mpfr_set_str(). */
6de9cd9a
DN
1084 p = buffer;
1085 while (count > 0)
1086 {
1087 c = gfc_next_char ();
f8e566e5 1088 if (c == 'd' || c == 'q')
6de9cd9a
DN
1089 c = 'e';
1090 *p++ = c;
1091 count--;
1092 }
1093
1094 *p = '\0';
1095
1096 kind = get_kind ();
1097 if (kind == -1)
1098 return MATCH_ERROR;
1099
1100 /* If the number looked like an integer, forget about a kind we may
1101 have seen, otherwise validate the kind against real kinds. */
1102 if (seen_dp == 0 && exp_char == ' ')
1103 {
1104 if (kind == -2)
9d64df18 1105 kind = gfc_default_integer_kind;
6de9cd9a
DN
1106
1107 }
1108 else
1109 {
1110 if (exp_char == 'd')
1111 {
1112 if (kind != -2)
1113 {
1114 gfc_error
1115 ("Real number at %C has a 'd' exponent and an explicit kind");
1116 return MATCH_ERROR;
1117 }
9d64df18 1118 kind = gfc_default_double_kind;
6de9cd9a
DN
1119
1120 }
1121 else
1122 {
1123 if (kind == -2)
9d64df18 1124 kind = gfc_default_real_kind;
6de9cd9a
DN
1125 }
1126
e7a2d5fb 1127 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
6de9cd9a
DN
1128 {
1129 gfc_error ("Invalid real kind %d at %C", kind);
1130 return MATCH_ERROR;
1131 }
1132 }
1133
63645982 1134 *result = gfc_convert_real (buffer, kind, &gfc_current_locus);
6de9cd9a
DN
1135 return MATCH_YES;
1136
1137no_match:
63645982 1138 gfc_current_locus = old_loc;
6de9cd9a
DN
1139 return MATCH_NO;
1140}
1141
1142
1143/* Match a real or imaginary part of a complex number. */
1144
1145static match
1146match_complex_part (gfc_expr ** result)
1147{
1148 match m;
1149
1150 m = match_sym_complex_part (result);
1151 if (m != MATCH_NO)
1152 return m;
1153
1154 return match_const_complex_part (result);
1155}
1156
1157
1158/* Try to match a complex constant. */
1159
1160static match
1161match_complex_constant (gfc_expr ** result)
1162{
1163 gfc_expr *e, *real, *imag;
1164 gfc_error_buf old_error;
1165 gfc_typespec target;
1166 locus old_loc;
1167 int kind;
1168 match m;
1169
63645982 1170 old_loc = gfc_current_locus;
6de9cd9a
DN
1171 real = imag = e = NULL;
1172
1173 m = gfc_match_char ('(');
1174 if (m != MATCH_YES)
1175 return m;
1176
1177 gfc_push_error (&old_error);
1178
1179 m = match_complex_part (&real);
1180 if (m == MATCH_NO)
1181 goto cleanup;
1182
1183 if (gfc_match_char (',') == MATCH_NO)
1184 {
1185 gfc_pop_error (&old_error);
1186 m = MATCH_NO;
1187 goto cleanup;
1188 }
1189
1190 /* If m is error, then something was wrong with the real part and we
1191 assume we have a complex constant because we've seen the ','. An
1192 ambiguous case here is the start of an iterator list of some
1193 sort. These sort of lists are matched prior to coming here. */
1194
1195 if (m == MATCH_ERROR)
1196 goto cleanup;
1197 gfc_pop_error (&old_error);
1198
1199 m = match_complex_part (&imag);
1200 if (m == MATCH_NO)
1201 goto syntax;
1202 if (m == MATCH_ERROR)
1203 goto cleanup;
1204
1205 m = gfc_match_char (')');
1206 if (m == MATCH_NO)
1207 goto syntax;
1208
1209 if (m == MATCH_ERROR)
1210 goto cleanup;
1211
1212 /* Decide on the kind of this complex number. */
1213 kind = gfc_kind_max (real, imag);
1214 target.type = BT_REAL;
1215 target.kind = kind;
1216
1217 if (kind != real->ts.kind)
1218 gfc_convert_type (real, &target, 2);
1219 if (kind != imag->ts.kind)
1220 gfc_convert_type (imag, &target, 2);
1221
1222 e = gfc_convert_complex (real, imag, kind);
63645982 1223 e->where = gfc_current_locus;
6de9cd9a
DN
1224
1225 gfc_free_expr (real);
1226 gfc_free_expr (imag);
1227
1228 *result = e;
1229 return MATCH_YES;
1230
1231syntax:
1232 gfc_error ("Syntax error in COMPLEX constant at %C");
1233 m = MATCH_ERROR;
1234
1235cleanup:
1236 gfc_free_expr (e);
1237 gfc_free_expr (real);
1238 gfc_free_expr (imag);
63645982 1239 gfc_current_locus = old_loc;
6de9cd9a
DN
1240
1241 return m;
1242}
1243
1244
1245/* Match constants in any of several forms. Returns nonzero for a
1246 match, zero for no match. */
1247
1248match
1249gfc_match_literal_constant (gfc_expr ** result, int signflag)
1250{
1251 match m;
1252
1253 m = match_complex_constant (result);
1254 if (m != MATCH_NO)
1255 return m;
1256
1257 m = match_string_constant (result);
1258 if (m != MATCH_NO)
1259 return m;
1260
1261 m = match_boz_constant (result);
1262 if (m != MATCH_NO)
1263 return m;
1264
1265 m = match_real_constant (result, signflag);
1266 if (m != MATCH_NO)
1267 return m;
1268
1269 m = match_integer_constant (result, signflag);
1270 if (m != MATCH_NO)
1271 return m;
1272
1273 m = match_logical_constant (result);
1274 if (m != MATCH_NO)
1275 return m;
1276
1277 return MATCH_NO;
1278}
1279
1280
1281/* Match a single actual argument value. An actual argument is
1282 usually an expression, but can also be a procedure name. If the
1283 argument is a single name, it is not always possible to tell
1284 whether the name is a dummy procedure or not. We treat these cases
1285 by creating an argument that looks like a dummy procedure and
1286 fixing things later during resolution. */
1287
1288static match
1289match_actual_arg (gfc_expr ** result)
1290{
1291 char name[GFC_MAX_SYMBOL_LEN + 1];
1292 gfc_symtree *symtree;
1293 locus where, w;
1294 gfc_expr *e;
1295 int c;
1296
63645982 1297 where = gfc_current_locus;
6de9cd9a
DN
1298
1299 switch (gfc_match_name (name))
1300 {
1301 case MATCH_ERROR:
1302 return MATCH_ERROR;
1303
1304 case MATCH_NO:
1305 break;
1306
1307 case MATCH_YES:
63645982 1308 w = gfc_current_locus;
6de9cd9a
DN
1309 gfc_gobble_whitespace ();
1310 c = gfc_next_char ();
63645982 1311 gfc_current_locus = w;
6de9cd9a
DN
1312
1313 if (c != ',' && c != ')')
1314 break;
1315
1316 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1317 break;
1318 /* Handle error elsewhere. */
1319
1320 /* Eliminate a couple of common cases where we know we don't
1321 have a function argument. */
1322 if (symtree == NULL)
1323 {
1324 gfc_get_sym_tree (name, NULL, &symtree);
1325 gfc_set_sym_referenced (symtree->n.sym);
1326 }
1327 else
1328 {
1329 gfc_symbol *sym;
1330
1331 sym = symtree->n.sym;
1332 gfc_set_sym_referenced (sym);
1333 if (sym->attr.flavor != FL_PROCEDURE
1334 && sym->attr.flavor != FL_UNKNOWN)
1335 break;
1336
1337 /* If the symbol is a function with itself as the result and
1338 is being defined, then we have a variable. */
1339 if (sym->result == sym
1340 && (gfc_current_ns->proc_name == sym
1341 || (gfc_current_ns->parent != NULL
1342 && gfc_current_ns->parent->proc_name == sym)))
1343 break;
1344 }
1345
1346 e = gfc_get_expr (); /* Leave it unknown for now */
1347 e->symtree = symtree;
1348 e->expr_type = EXPR_VARIABLE;
1349 e->ts.type = BT_PROCEDURE;
1350 e->where = where;
1351
1352 *result = e;
1353 return MATCH_YES;
1354 }
1355
63645982 1356 gfc_current_locus = where;
6de9cd9a
DN
1357 return gfc_match_expr (result);
1358}
1359
1360
1361/* Match a keyword argument. */
1362
1363static match
1364match_keyword_arg (gfc_actual_arglist * actual, gfc_actual_arglist * base)
1365{
1366 char name[GFC_MAX_SYMBOL_LEN + 1];
1367 gfc_actual_arglist *a;
1368 locus name_locus;
1369 match m;
1370
63645982 1371 name_locus = gfc_current_locus;
6de9cd9a
DN
1372 m = gfc_match_name (name);
1373
1374 if (m != MATCH_YES)
1375 goto cleanup;
1376 if (gfc_match_char ('=') != MATCH_YES)
1377 {
1378 m = MATCH_NO;
1379 goto cleanup;
1380 }
1381
1382 m = match_actual_arg (&actual->expr);
1383 if (m != MATCH_YES)
1384 goto cleanup;
1385
1386 /* Make sure this name has not appeared yet. */
1387
1388 if (name[0] != '\0')
1389 {
1390 for (a = base; a; a = a->next)
1391 if (strcmp (a->name, name) == 0)
1392 {
1393 gfc_error
1394 ("Keyword '%s' at %C has already appeared in the current "
1395 "argument list", name);
1396 return MATCH_ERROR;
1397 }
1398 }
1399
1400 strcpy (actual->name, name);
1401 return MATCH_YES;
1402
1403cleanup:
63645982 1404 gfc_current_locus = name_locus;
6de9cd9a
DN
1405 return m;
1406}
1407
1408
1409/* Matches an actual argument list of a function or subroutine, from
1410 the opening parenthesis to the closing parenthesis. The argument
1411 list is assumed to allow keyword arguments because we don't know if
1412 the symbol associated with the procedure has an implicit interface
d3fcc995
TS
1413 or not. We make sure keywords are unique. If SUB_FLAG is set,
1414 we're matching the argument list of a subroutine. */
6de9cd9a
DN
1415
1416match
1417gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist ** argp)
1418{
1419 gfc_actual_arglist *head, *tail;
1420 int seen_keyword;
1421 gfc_st_label *label;
1422 locus old_loc;
1423 match m;
1424
1425 *argp = tail = NULL;
63645982 1426 old_loc = gfc_current_locus;
6de9cd9a
DN
1427
1428 seen_keyword = 0;
1429
1430 if (gfc_match_char ('(') == MATCH_NO)
1431 return (sub_flag) ? MATCH_YES : MATCH_NO;
1432
1433 if (gfc_match_char (')') == MATCH_YES)
1434 return MATCH_YES;
1435 head = NULL;
1436
1437 for (;;)
1438 {
1439 if (head == NULL)
1440 head = tail = gfc_get_actual_arglist ();
1441 else
1442 {
1443 tail->next = gfc_get_actual_arglist ();
1444 tail = tail->next;
1445 }
1446
1447 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1448 {
1449 m = gfc_match_st_label (&label, 0);
1450 if (m == MATCH_NO)
1451 gfc_error ("Expected alternate return label at %C");
1452 if (m != MATCH_YES)
1453 goto cleanup;
1454
1455 tail->label = label;
1456 goto next;
1457 }
1458
1459 /* After the first keyword argument is seen, the following
1460 arguments must also have keywords. */
1461 if (seen_keyword)
1462 {
1463 m = match_keyword_arg (tail, head);
1464
1465 if (m == MATCH_ERROR)
1466 goto cleanup;
1467 if (m == MATCH_NO)
1468 {
1469 gfc_error
1470 ("Missing keyword name in actual argument list at %C");
1471 goto cleanup;
1472 }
1473
1474 }
1475 else
1476 {
1477 /* See if we have the first keyword argument. */
1478 m = match_keyword_arg (tail, head);
1479 if (m == MATCH_YES)
1480 seen_keyword = 1;
1481 if (m == MATCH_ERROR)
1482 goto cleanup;
1483
1484 if (m == MATCH_NO)
1485 {
1486 /* Try for a non-keyword argument. */
1487 m = match_actual_arg (&tail->expr);
1488 if (m == MATCH_ERROR)
1489 goto cleanup;
1490 if (m == MATCH_NO)
1491 goto syntax;
1492 }
1493 }
1494
1495 next:
1496 if (gfc_match_char (')') == MATCH_YES)
1497 break;
1498 if (gfc_match_char (',') != MATCH_YES)
1499 goto syntax;
1500 }
1501
1502 *argp = head;
1503 return MATCH_YES;
1504
1505syntax:
1506 gfc_error ("Syntax error in argument list at %C");
1507
1508cleanup:
1509 gfc_free_actual_arglist (head);
63645982 1510 gfc_current_locus = old_loc;
6de9cd9a
DN
1511
1512 return MATCH_ERROR;
1513}
1514
1515
1516/* Used by match_varspec() to extend the reference list by one
1517 element. */
1518
1519static gfc_ref *
1520extend_ref (gfc_expr * primary, gfc_ref * tail)
1521{
1522
1523 if (primary->ref == NULL)
1524 primary->ref = tail = gfc_get_ref ();
1525 else
1526 {
1527 if (tail == NULL)
1528 gfc_internal_error ("extend_ref(): Bad tail");
1529 tail->next = gfc_get_ref ();
1530 tail = tail->next;
1531 }
1532
1533 return tail;
1534}
1535
1536
1537/* Match any additional specifications associated with the current
1538 variable like member references or substrings. If equiv_flag is
1539 set we only match stuff that is allowed inside an EQUIVALENCE
1540 statement. */
1541
1542static match
1543match_varspec (gfc_expr * primary, int equiv_flag)
1544{
1545 char name[GFC_MAX_SYMBOL_LEN + 1];
1546 gfc_ref *substring, *tail;
1547 gfc_component *component;
1548 gfc_symbol *sym;
1549 match m;
1550
1551 tail = NULL;
1552
1553 if (primary->symtree->n.sym->attr.dimension
1554 || (equiv_flag
1555 && gfc_peek_char () == '('))
1556 {
1557
1558 tail = extend_ref (primary, tail);
1559 tail->type = REF_ARRAY;
1560
1561 m = gfc_match_array_ref (&tail->u.ar, primary->symtree->n.sym->as,
1562 equiv_flag);
1563 if (m != MATCH_YES)
1564 return m;
1565 }
1566
1567 sym = primary->symtree->n.sym;
1568 primary->ts = sym->ts;
1569
1570 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1571 goto check_substring;
1572
1573 sym = sym->ts.derived;
1574
1575 for (;;)
1576 {
1577 m = gfc_match_name (name);
1578 if (m == MATCH_NO)
1579 gfc_error ("Expected structure component name at %C");
1580 if (m != MATCH_YES)
1581 return MATCH_ERROR;
1582
1583 component = gfc_find_component (sym, name);
1584 if (component == NULL)
1585 return MATCH_ERROR;
1586
1587 tail = extend_ref (primary, tail);
1588 tail->type = REF_COMPONENT;
1589
1590 tail->u.c.component = component;
1591 tail->u.c.sym = sym;
1592
1593 primary->ts = component->ts;
1594
1595 if (component->as != NULL)
1596 {
1597 tail = extend_ref (primary, tail);
1598 tail->type = REF_ARRAY;
1599
1600 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1601 if (m != MATCH_YES)
1602 return m;
1603 }
1604
1605 if (component->ts.type != BT_DERIVED
1606 || gfc_match_char ('%') != MATCH_YES)
1607 break;
1608
1609 sym = component->ts.derived;
1610 }
1611
1612check_substring:
1613 if (primary->ts.type == BT_CHARACTER)
1614 {
1615 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1616 {
1617 case MATCH_YES:
1618 if (tail == NULL)
1619 primary->ref = substring;
1620 else
1621 tail->next = substring;
1622
1623 if (primary->expr_type == EXPR_CONSTANT)
1624 primary->expr_type = EXPR_SUBSTRING;
1625
1626 break;
1627
1628 case MATCH_NO:
1629 break;
1630
1631 case MATCH_ERROR:
1632 return MATCH_ERROR;
1633 }
1634 }
1635
1636 return MATCH_YES;
1637}
1638
1639
1640/* Given an expression that is a variable, figure out what the
1641 ultimate variable's type and attribute is, traversing the reference
1642 structures if necessary.
1643
1644 This subroutine is trickier than it looks. We start at the base
1645 symbol and store the attribute. Component references load a
1646 completely new attribute.
1647
1648 A couple of rules come into play. Subobjects of targets are always
1649 targets themselves. If we see a component that goes through a
1650 pointer, then the expression must also be a target, since the
1651 pointer is associated with something (if it isn't core will soon be
1652 dumped). If we see a full part or section of an array, the
1653 expression is also an array.
1654
f7b529fa 1655 We can have at most one full array reference. */
6de9cd9a
DN
1656
1657symbol_attribute
1658gfc_variable_attr (gfc_expr * expr, gfc_typespec * ts)
1659{
1660 int dimension, pointer, target;
1661 symbol_attribute attr;
1662 gfc_ref *ref;
1663
1664 if (expr->expr_type != EXPR_VARIABLE)
1665 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1666
1667 ref = expr->ref;
1668 attr = expr->symtree->n.sym->attr;
1669
1670 dimension = attr.dimension;
1671 pointer = attr.pointer;
1672
1673 target = attr.target;
1674 if (pointer)
1675 target = 1;
1676
1677 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1678 *ts = expr->symtree->n.sym->ts;
1679
1680 for (; ref; ref = ref->next)
1681 switch (ref->type)
1682 {
1683 case REF_ARRAY:
1684
1685 switch (ref->u.ar.type)
1686 {
1687 case AR_FULL:
1688 dimension = 1;
1689 break;
1690
1691 case AR_SECTION:
1692 pointer = 0;
1693 dimension = 1;
1694 break;
1695
1696 case AR_ELEMENT:
1697 pointer = 0;
1698 break;
1699
1700 case AR_UNKNOWN:
1701 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1702 }
1703
1704 break;
1705
1706 case REF_COMPONENT:
1707 gfc_get_component_attr (&attr, ref->u.c.component);
1708 if (ts != NULL)
1709 *ts = ref->u.c.component->ts;
1710
1711 pointer = ref->u.c.component->pointer;
1712 if (pointer)
1713 target = 1;
1714
1715 break;
1716
1717 case REF_SUBSTRING:
1718 pointer = 0;
1719 break;
1720 }
1721
1722 attr.dimension = dimension;
1723 attr.pointer = pointer;
1724 attr.target = target;
1725
1726 return attr;
1727}
1728
1729
1730/* Return the attribute from a general expression. */
1731
1732symbol_attribute
1733gfc_expr_attr (gfc_expr * e)
1734{
1735 symbol_attribute attr;
1736
1737 switch (e->expr_type)
1738 {
1739 case EXPR_VARIABLE:
1740 attr = gfc_variable_attr (e, NULL);
1741 break;
1742
1743 case EXPR_FUNCTION:
1744 gfc_clear_attr (&attr);
1745
1746 if (e->value.function.esym != NULL)
1747 attr = e->value.function.esym->result->attr;
1748
1749 /* TODO: NULL() returns pointers. May have to take care of this
1750 here. */
1751
1752 break;
1753
1754 default:
1755 gfc_clear_attr (&attr);
1756 break;
1757 }
1758
1759 return attr;
1760}
1761
1762
1763/* Match a structure constructor. The initial symbol has already been
1764 seen. */
1765
d663434b
TS
1766match
1767gfc_match_structure_constructor (gfc_symbol * sym, gfc_expr ** result)
6de9cd9a
DN
1768{
1769 gfc_constructor *head, *tail;
1770 gfc_component *comp;
1771 gfc_expr *e;
1772 locus where;
1773 match m;
1774
1775 head = tail = NULL;
1776
1777 if (gfc_match_char ('(') != MATCH_YES)
1778 goto syntax;
1779
63645982 1780 where = gfc_current_locus;
6de9cd9a
DN
1781
1782 gfc_find_component (sym, NULL);
1783
1784 for (comp = sym->components; comp; comp = comp->next)
1785 {
1786 if (head == NULL)
1787 tail = head = gfc_get_constructor ();
1788 else
1789 {
1790 tail->next = gfc_get_constructor ();
1791 tail = tail->next;
1792 }
1793
1794 m = gfc_match_expr (&tail->expr);
1795 if (m == MATCH_NO)
1796 goto syntax;
1797 if (m == MATCH_ERROR)
1798 goto cleanup;
1799
1800 if (gfc_match_char (',') == MATCH_YES)
1801 {
1802 if (comp->next == NULL)
1803 {
1804 gfc_error
1805 ("Too many components in structure constructor at %C");
1806 goto cleanup;
1807 }
1808
1809 continue;
1810 }
1811
1812 break;
1813 }
1814
1815 if (gfc_match_char (')') != MATCH_YES)
1816 goto syntax;
1817
1818 if (comp->next != NULL)
1819 {
1820 gfc_error ("Too few components in structure constructor at %C");
1821 goto cleanup;
1822 }
1823
1824 e = gfc_get_expr ();
1825
1826 e->expr_type = EXPR_STRUCTURE;
1827
1828 e->ts.type = BT_DERIVED;
1829 e->ts.derived = sym;
1830 e->where = where;
1831
1832 e->value.constructor = head;
1833
1834 *result = e;
1835 return MATCH_YES;
1836
1837syntax:
1838 gfc_error ("Syntax error in structure constructor at %C");
1839
1840cleanup:
1841 gfc_free_constructor (head);
1842 return MATCH_ERROR;
1843}
1844
1845
1846/* Matches a variable name followed by anything that might follow it--
1847 array reference, argument list of a function, etc. */
1848
1849match
1850gfc_match_rvalue (gfc_expr ** result)
1851{
1852 gfc_actual_arglist *actual_arglist;
d3fcc995 1853 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
1854 gfc_state_data *st;
1855 gfc_symbol *sym;
1856 gfc_symtree *symtree;
d3fcc995 1857 locus where, old_loc;
6de9cd9a 1858 gfc_expr *e;
d3fcc995 1859 match m, m2;
6de9cd9a
DN
1860 int i;
1861
1862 m = gfc_match_name (name);
1863 if (m != MATCH_YES)
1864 return m;
1865
1866 if (gfc_find_state (COMP_INTERFACE) == SUCCESS)
1867 i = gfc_get_sym_tree (name, NULL, &symtree);
1868 else
1869 i = gfc_get_ha_sym_tree (name, &symtree);
1870
1871 if (i)
1872 return MATCH_ERROR;
1873
1874 sym = symtree->n.sym;
1875 e = NULL;
63645982 1876 where = gfc_current_locus;
6de9cd9a
DN
1877
1878 gfc_set_sym_referenced (sym);
1879
1880 if (sym->attr.function && sym->result == sym
1881 && (gfc_current_ns->proc_name == sym
1882 || (gfc_current_ns->parent != NULL
1883 && gfc_current_ns->parent->proc_name == sym)))
1884 goto variable;
1885
1886 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
1887 goto function0;
1888
1889 if (sym->attr.generic)
1890 goto generic_function;
1891
1892 switch (sym->attr.flavor)
1893 {
1894 case FL_VARIABLE:
1895 variable:
1896 if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
1897 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
1898 gfc_set_default_type (sym, 0, sym->ns);
1899
1900 e = gfc_get_expr ();
1901
1902 e->expr_type = EXPR_VARIABLE;
1903 e->symtree = symtree;
1904
1905 m = match_varspec (e, 0);
1906 break;
1907
1908 case FL_PARAMETER:
1909 if (sym->value
1910 && sym->value->expr_type != EXPR_ARRAY)
1911 e = gfc_copy_expr (sym->value);
1912 else
1913 {
1914 e = gfc_get_expr ();
1915 e->expr_type = EXPR_VARIABLE;
1916 }
1917
1918 e->symtree = symtree;
1919 m = match_varspec (e, 0);
1920 break;
1921
1922 case FL_DERIVED:
1923 sym = gfc_use_derived (sym);
1924 if (sym == NULL)
1925 m = MATCH_ERROR;
1926 else
d663434b 1927 m = gfc_match_structure_constructor (sym, &e);
6de9cd9a
DN
1928 break;
1929
1930 /* If we're here, then the name is known to be the name of a
1931 procedure, yet it is not sure to be the name of a function. */
1932 case FL_PROCEDURE:
1933 if (sym->attr.subroutine)
1934 {
1935 gfc_error ("Unexpected use of subroutine name '%s' at %C",
1936 sym->name);
1937 m = MATCH_ERROR;
1938 break;
1939 }
1940
1941 /* At this point, the name has to be a non-statement function.
1942 If the name is the same as the current function being
1943 compiled, then we have a variable reference (to the function
1944 result) if the name is non-recursive. */
1945
1946 st = gfc_enclosing_unit (NULL);
1947
1948 if (st != NULL && st->state == COMP_FUNCTION
1949 && st->sym == sym
1950 && !sym->attr.recursive)
1951 {
1952 e = gfc_get_expr ();
1953 e->symtree = symtree;
1954 e->expr_type = EXPR_VARIABLE;
1955
1956 m = match_varspec (e, 0);
1957 break;
1958 }
1959
1960 /* Match a function reference. */
1961 function0:
1962 m = gfc_match_actual_arglist (0, &actual_arglist);
1963 if (m == MATCH_NO)
1964 {
1965 if (sym->attr.proc == PROC_ST_FUNCTION)
1966 gfc_error ("Statement function '%s' requires argument list at %C",
1967 sym->name);
1968 else
1969 gfc_error ("Function '%s' requires an argument list at %C",
1970 sym->name);
1971
1972 m = MATCH_ERROR;
1973 break;
1974 }
1975
1976 if (m != MATCH_YES)
1977 {
1978 m = MATCH_ERROR;
1979 break;
1980 }
1981
1982 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
1983 sym = symtree->n.sym;
1984
1985 e = gfc_get_expr ();
1986 e->symtree = symtree;
1987 e->expr_type = EXPR_FUNCTION;
1988 e->value.function.actual = actual_arglist;
63645982 1989 e->where = gfc_current_locus;
6de9cd9a
DN
1990
1991 if (sym->as != NULL)
1992 e->rank = sym->as->rank;
1993
1994 if (!sym->attr.function
1995 && gfc_add_function (&sym->attr, NULL) == FAILURE)
1996 {
1997 m = MATCH_ERROR;
1998 break;
1999 }
2000
2001 if (sym->result == NULL)
2002 sym->result = sym;
2003
2004 m = MATCH_YES;
2005 break;
2006
2007 case FL_UNKNOWN:
2008
2009 /* Special case for derived type variables that get their types
2010 via an IMPLICIT statement. This can't wait for the
2011 resolution phase. */
2012
2013 if (gfc_peek_char () == '%'
0dd973dd 2014 && sym->ts.type == BT_UNKNOWN
6de9cd9a
DN
2015 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2016 gfc_set_default_type (sym, 0, sym->ns);
2017
2018 /* If the symbol has a dimension attribute, the expression is a
2019 variable. */
2020
2021 if (sym->attr.dimension)
2022 {
2023 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2024 {
2025 m = MATCH_ERROR;
2026 break;
2027 }
2028
2029 e = gfc_get_expr ();
2030 e->symtree = symtree;
2031 e->expr_type = EXPR_VARIABLE;
2032 m = match_varspec (e, 0);
2033 break;
2034 }
2035
2036 /* Name is not an array, so we peek to see if a '(' implies a
2037 function call or a substring reference. Otherwise the
2038 variable is just a scalar. */
2039
2040 gfc_gobble_whitespace ();
2041 if (gfc_peek_char () != '(')
2042 {
2043 /* Assume a scalar variable */
2044 e = gfc_get_expr ();
2045 e->symtree = symtree;
2046 e->expr_type = EXPR_VARIABLE;
2047
2048 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2049 {
2050 m = MATCH_ERROR;
2051 break;
2052 }
2053
2054 e->ts = sym->ts;
2055 m = match_varspec (e, 0);
2056 break;
2057 }
2058
d3fcc995
TS
2059 /* See if this is a function reference with a keyword argument
2060 as first argument. We do this because otherwise a spurious
2061 symbol would end up in the symbol table. */
2062
2063 old_loc = gfc_current_locus;
2064 m2 = gfc_match (" ( %n =", argname);
2065 gfc_current_locus = old_loc;
6de9cd9a
DN
2066
2067 e = gfc_get_expr ();
2068 e->symtree = symtree;
2069
d3fcc995 2070 if (m2 != MATCH_YES)
6de9cd9a 2071 {
d3fcc995
TS
2072 /* See if this could possibly be a substring reference of a name
2073 that we're not sure is a variable yet. */
6de9cd9a 2074
d3fcc995
TS
2075 if ((sym->ts.type == BT_UNKNOWN || sym->ts.type == BT_CHARACTER)
2076 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
6de9cd9a 2077 {
6de9cd9a 2078
d3fcc995
TS
2079 e->expr_type = EXPR_VARIABLE;
2080
2081 if (sym->attr.flavor != FL_VARIABLE
2082 && gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2083 {
2084 m = MATCH_ERROR;
2085 break;
2086 }
2087
2088 if (sym->ts.type == BT_UNKNOWN
2089 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2090 {
2091 m = MATCH_ERROR;
2092 break;
2093 }
2094
2095 e->ts = sym->ts;
2096 m = MATCH_YES;
6de9cd9a
DN
2097 break;
2098 }
6de9cd9a
DN
2099 }
2100
2101 /* Give up, assume we have a function. */
2102
2103 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2104 sym = symtree->n.sym;
2105 e->expr_type = EXPR_FUNCTION;
2106
2107 if (!sym->attr.function
2108 && gfc_add_function (&sym->attr, NULL) == FAILURE)
2109 {
2110 m = MATCH_ERROR;
2111 break;
2112 }
2113
2114 sym->result = sym;
2115
2116 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2117 if (m == MATCH_NO)
2118 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2119
2120 if (m != MATCH_YES)
2121 {
2122 m = MATCH_ERROR;
2123 break;
2124 }
2125
2126 /* If our new function returns a character, array or structure
2127 type, it might have subsequent references. */
2128
2129 m = match_varspec (e, 0);
2130 if (m == MATCH_NO)
2131 m = MATCH_YES;
2132
2133 break;
2134
2135 generic_function:
2136 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2137
2138 e = gfc_get_expr ();
2139 e->symtree = symtree;
2140 e->expr_type = EXPR_FUNCTION;
2141
2142 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2143 break;
2144
2145 default:
2146 gfc_error ("Symbol at %C is not appropriate for an expression");
2147 return MATCH_ERROR;
2148 }
2149
2150 if (m == MATCH_YES)
2151 {
2152 e->where = where;
2153 *result = e;
2154 }
2155 else
2156 gfc_free_expr (e);
2157
2158 return m;
2159}
2160
2161
2162/* Match a variable, ie something that can be assigned to. This
2163 starts as a symbol, can be a structure component or an array
2164 reference. It can be a function if the function doesn't have a
2165 separate RESULT variable. If the symbol has not been previously
2166 seen, we assume it is a variable. */
2167
2168match
2169gfc_match_variable (gfc_expr ** result, int equiv_flag)
2170{
2171 gfc_symbol *sym;
2172 gfc_symtree *st;
2173 gfc_expr *expr;
2174 locus where;
2175 match m;
2176
2177 m = gfc_match_sym_tree (&st, 1);
2178 if (m != MATCH_YES)
2179 return m;
63645982 2180 where = gfc_current_locus;
6de9cd9a
DN
2181
2182 sym = st->n.sym;
2183 gfc_set_sym_referenced (sym);
2184 switch (sym->attr.flavor)
2185 {
2186 case FL_VARIABLE:
2187 break;
2188
2189 case FL_UNKNOWN:
2190 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2191 return MATCH_ERROR;
6de9cd9a
DN
2192 break;
2193
2194 case FL_PROCEDURE:
2195 /* Check for a nonrecursive function result */
2196 if (sym->attr.function && (sym->result == sym || sym->attr.entry))
2197 {
6de9cd9a
DN
2198 /* If a function result is a derived type, then the derived
2199 type may still have to be resolved. */
2200
2201 if (sym->ts.type == BT_DERIVED
2202 && gfc_use_derived (sym->ts.derived) == NULL)
2203 return MATCH_ERROR;
6de9cd9a
DN
2204 break;
2205 }
2206
2207 /* Fall through to error */
2208
2209 default:
2210 gfc_error ("Expected VARIABLE at %C");
2211 return MATCH_ERROR;
2212 }
2213
0dd973dd
PB
2214 /* Special case for derived type variables that get their types
2215 via an IMPLICIT statement. This can't wait for the
2216 resolution phase. */
2217
2218 {
2219 gfc_namespace * implicit_ns;
2220
2221 if (gfc_current_ns->proc_name == sym)
2222 implicit_ns = gfc_current_ns;
2223 else
2224 implicit_ns = sym->ns;
2225
2226 if (gfc_peek_char () == '%'
2227 && sym->ts.type == BT_UNKNOWN
2228 && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2229 gfc_set_default_type (sym, 0, implicit_ns);
2230 }
2231
6de9cd9a
DN
2232 expr = gfc_get_expr ();
2233
2234 expr->expr_type = EXPR_VARIABLE;
2235 expr->symtree = st;
2236 expr->ts = sym->ts;
2237 expr->where = where;
2238
2239 /* Now see if we have to do more. */
2240 m = match_varspec (expr, equiv_flag);
2241 if (m != MATCH_YES)
2242 {
2243 gfc_free_expr (expr);
2244 return m;
2245 }
2246
2247 *result = expr;
2248 return MATCH_YES;
2249}