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