1 /* Primary expression subroutines
2 Copyright (C) 2000, 2001, 2002, 2004, 2005 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
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. */
37 match_kind_param (int *kind)
39 char name[GFC_MAX_SYMBOL_LEN + 1];
44 m = gfc_match_small_literal_int (kind);
48 m = gfc_match_name (name);
52 if (gfc_find_symbol (name, NULL, 1, &sym))
58 if (sym->attr.flavor != FL_PARAMETER)
61 p = gfc_extract_int (sym->value, kind);
72 /* Get a trailing kind-specification for non-character variables.
74 the integer kind value or:
75 -1 if an error was generated
76 -2 if no kind was found */
84 if (gfc_match_char ('_') != MATCH_YES)
87 m = match_kind_param (&kind);
89 gfc_error ("Missing kind-parameter at %C");
91 return (m == MATCH_YES) ? kind : -1;
95 /* Given a character and a radix, see if the character is a valid
96 digit in that radix. */
99 check_digit (int c, int radix)
106 r = ('0' <= c && c <= '1');
110 r = ('0' <= c && c <= '7');
114 r = ('0' <= c && c <= '9');
122 gfc_internal_error ("check_digit(): bad radix");
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. */
135 match_digits (int signflag, int radix, char *buffer)
141 c = gfc_next_char ();
143 if (signflag && (c == '+' || c == '-'))
147 gfc_gobble_whitespace ();
148 c = gfc_next_char ();
152 if (!check_digit (c, radix))
161 old_loc = gfc_current_locus;
162 c = gfc_next_char ();
164 if (!check_digit (c, radix))
172 gfc_current_locus = old_loc;
178 /* Match an integer (digit string and optional kind).
179 A sign will be accepted if signflag is set. */
182 match_integer_constant (gfc_expr ** result, int signflag)
189 old_loc = gfc_current_locus;
190 gfc_gobble_whitespace ();
192 length = match_digits (signflag, 10, NULL);
193 gfc_current_locus = old_loc;
197 buffer = alloca (length + 1);
198 memset (buffer, '\0', length + 1);
200 gfc_gobble_whitespace ();
202 match_digits (signflag, 10, buffer);
206 kind = gfc_default_integer_kind;
210 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
212 gfc_error ("Integer kind %d at %C not available", kind);
216 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
218 if (gfc_range_check (e) != ARITH_OK)
220 gfc_error ("Integer too big for its kind at %C");
231 /* Match a Hollerith constant. */
234 match_hollerith_constant (gfc_expr ** result)
243 old_loc = gfc_current_locus;
244 gfc_gobble_whitespace ();
246 if (match_integer_constant (&e, 0) == MATCH_YES
247 && gfc_match_char ('h') == MATCH_YES)
249 if (gfc_notify_std (GFC_STD_LEGACY,
250 "Extension: Hollerith constant at %C")
254 msg = gfc_extract_int (e, &num);
262 gfc_error ("Invalid Hollerith constant: %L must contain at least one "
263 "character", &old_loc);
266 if (e->ts.kind != gfc_default_integer_kind)
268 gfc_error ("Invalid Hollerith constant: Interger kind at %L "
269 "should be default", &old_loc);
274 buffer = (char *) gfc_getmem (sizeof(char) * num + 1);
275 for (i = 0; i < num; i++)
277 buffer[i] = gfc_next_char_literal (1);
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;
291 gfc_current_locus = old_loc;
300 /* Match a binary, octal or hexadecimal constant that can be found in
304 match_boz_constant (gfc_expr ** result)
306 int radix, delim, length, x_hex, kind;
311 old_loc = gfc_current_locus;
312 gfc_gobble_whitespace ();
315 switch (gfc_next_char ())
333 /* No whitespace allowed here. */
335 delim = gfc_next_char ();
336 if (delim != '\'' && delim != '\"')
339 if (x_hex && pedantic
340 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
341 "constant at %C uses non-standard syntax.")
345 old_loc = gfc_current_locus;
347 length = match_digits (0, radix, NULL);
353 gfc_error ("Empty set of digits in binary constant at %C");
355 gfc_error ("Empty set of digits in octal constant at %C");
357 gfc_error ("Empty set of digits in hexadecimal constant at %C");
364 if (gfc_next_char () != delim)
369 gfc_error ("Illegal character in binary constant at %C");
371 gfc_error ("Illegal character in octal constant at %C");
373 gfc_error ("Illegal character in hexadecimal constant at %C");
380 gfc_current_locus = old_loc;
382 buffer = alloca (length + 1);
383 memset (buffer, '\0', length + 1);
385 match_digits (0, radix, buffer);
386 gfc_next_char (); /* Eat delimiter. */
389 /* In section 5.2.5 and following C567 in the Fortran 2003 standard, we find
390 "If a data-stmt-constant is a boz-literal-constant, the corresponding
391 variable shall be of type integer. The boz-literal-constant is treated
392 as if it were an int-literal-constant with a kind-param that specifies
393 the representation method with the largest decimal exponent range
394 supported by the processor." */
396 kind = gfc_max_integer_kind;
397 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
399 if (gfc_range_check (e) != ARITH_OK)
401 gfc_error ("Integer too big for integer kind %i at %C", kind);
411 gfc_current_locus = old_loc;
416 /* Match a real constant of some sort. Allow a signed constant if signflag
417 is nonzero. Allow integer constants if allow_int is true. */
420 match_real_constant (gfc_expr ** result, int signflag)
422 int kind, c, count, seen_dp, seen_digits, exp_char;
423 locus old_loc, temp_loc;
428 old_loc = gfc_current_locus;
429 gfc_gobble_whitespace ();
439 c = gfc_next_char ();
440 if (signflag && (c == '+' || c == '-'))
445 gfc_gobble_whitespace ();
446 c = gfc_next_char ();
449 /* Scan significand. */
450 for (;; c = gfc_next_char (), count++)
457 /* Check to see if "." goes with a following operator like ".eq.". */
458 temp_loc = gfc_current_locus;
459 c = gfc_next_char ();
461 if (c == 'e' || c == 'd' || c == 'q')
463 c = gfc_next_char ();
465 goto done; /* Operator named .e. or .d. */
469 goto done; /* Distinguish 1.e9 from 1.eq.2 */
471 gfc_current_locus = temp_loc;
486 || (c != 'e' && c != 'd' && c != 'q'))
491 c = gfc_next_char ();
494 if (c == '+' || c == '-')
495 { /* optional sign */
496 c = gfc_next_char ();
502 gfc_error ("Missing exponent in real number at %C");
508 c = gfc_next_char ();
513 /* Check that we have a numeric constant. */
514 if (!seen_digits || (!seen_dp && exp_char == ' '))
516 gfc_current_locus = old_loc;
520 /* Convert the number. */
521 gfc_current_locus = old_loc;
522 gfc_gobble_whitespace ();
524 buffer = alloca (count + 1);
525 memset (buffer, '\0', count + 1);
528 c = gfc_next_char ();
529 if (c == '+' || c == '-')
531 gfc_gobble_whitespace ();
532 c = gfc_next_char ();
535 /* Hack for mpfr_set_str(). */
538 if (c == 'd' || c == 'q')
546 c = gfc_next_char ();
559 ("Real number at %C has a 'd' exponent and an explicit kind");
562 kind = gfc_default_double_kind;
569 ("Real number at %C has a 'q' exponent and an explicit kind");
572 kind = gfc_option.q_kind;
577 kind = gfc_default_real_kind;
579 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
581 gfc_error ("Invalid real kind %d at %C", kind);
586 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
588 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
590 switch (gfc_range_check (e))
595 gfc_error ("Real constant overflows its kind at %C");
598 case ARITH_UNDERFLOW:
599 if (gfc_option.warn_underflow)
600 gfc_warning ("Real constant underflows its kind at %C");
601 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
605 gfc_internal_error ("gfc_range_check() returned bad value");
617 /* Match a substring reference. */
620 match_substring (gfc_charlen * cl, int init, gfc_ref ** result)
622 gfc_expr *start, *end;
630 old_loc = gfc_current_locus;
632 m = gfc_match_char ('(');
636 if (gfc_match_char (':') != MATCH_YES)
639 m = gfc_match_init_expr (&start);
641 m = gfc_match_expr (&start);
649 m = gfc_match_char (':');
654 if (gfc_match_char (')') != MATCH_YES)
657 m = gfc_match_init_expr (&end);
659 m = gfc_match_expr (&end);
663 if (m == MATCH_ERROR)
666 m = gfc_match_char (')');
671 /* Optimize away the (:) reference. */
672 if (start == NULL && end == NULL)
676 ref = gfc_get_ref ();
678 ref->type = REF_SUBSTRING;
680 start = gfc_int_expr (1);
681 ref->u.ss.start = start;
682 if (end == NULL && cl)
683 end = gfc_copy_expr (cl->length);
685 ref->u.ss.length = cl;
692 gfc_error ("Syntax error in SUBSTRING specification at %C");
696 gfc_free_expr (start);
699 gfc_current_locus = old_loc;
704 /* Reads the next character of a string constant, taking care to
705 return doubled delimiters on the input as a single instance of
708 Special return values are:
709 -1 End of the string, as determined by the delimiter
710 -2 Unterminated string detected
712 Backslash codes are also expanded at this time. */
715 next_string_char (char delimiter)
720 c = gfc_next_char_literal (1);
725 if (gfc_option.flag_backslash && c == '\\')
727 old_locus = gfc_current_locus;
729 switch (gfc_next_char_literal (1))
757 /* Unknown backslash codes are simply not expanded */
758 gfc_current_locus = old_locus;
766 old_locus = gfc_current_locus;
767 c = gfc_next_char_literal (1);
771 gfc_current_locus = old_locus;
777 /* Special case of gfc_match_name() that matches a parameter kind name
778 before a string constant. This takes case of the weird but legal
783 where kind____ is a parameter. gfc_match_name() will happily slurp
784 up all the underscores, which leads to problems. If we return
785 MATCH_YES, the parse pointer points to the final underscore, which
786 is not part of the name. We never return MATCH_ERROR-- errors in
787 the name will be detected later. */
790 match_charkind_name (char *name)
796 gfc_gobble_whitespace ();
797 c = gfc_next_char ();
806 old_loc = gfc_current_locus;
807 c = gfc_next_char ();
811 peek = gfc_peek_char ();
813 if (peek == '\'' || peek == '\"')
815 gfc_current_locus = old_loc;
823 && (gfc_option.flag_dollar_ok && c != '$'))
827 if (++len > GFC_MAX_SYMBOL_LEN)
835 /* See if the current input matches a character constant. Lots of
836 contortions have to be done to match the kind parameter which comes
837 before the actual string. The main consideration is that we don't
838 want to error out too quickly. For example, we don't actually do
839 any validation of the kinds until we have actually seen a legal
840 delimiter. Using match_kind_param() generates errors too quickly. */
843 match_string_constant (gfc_expr ** result)
845 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
846 int i, c, kind, length, delimiter;
847 locus old_locus, start_locus;
853 old_locus = gfc_current_locus;
855 gfc_gobble_whitespace ();
857 start_locus = gfc_current_locus;
859 c = gfc_next_char ();
860 if (c == '\'' || c == '"')
862 kind = gfc_default_character_kind;
872 kind = kind * 10 + c - '0';
875 c = gfc_next_char ();
881 gfc_current_locus = old_locus;
883 m = match_charkind_name (name);
887 if (gfc_find_symbol (name, NULL, 1, &sym)
889 || sym->attr.flavor != FL_PARAMETER)
893 c = gfc_next_char ();
898 gfc_gobble_whitespace ();
899 c = gfc_next_char ();
905 gfc_gobble_whitespace ();
906 start_locus = gfc_current_locus;
908 c = gfc_next_char ();
909 if (c != '\'' && c != '"')
914 q = gfc_extract_int (sym->value, &kind);
922 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
924 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
929 /* Scan the string into a block of memory by first figuring out how
930 long it is, allocating the structure, then re-reading it. This
931 isn't particularly efficient, but string constants aren't that
932 common in most code. TODO: Use obstacks? */
939 c = next_string_char (delimiter);
944 gfc_current_locus = start_locus;
945 gfc_error ("Unterminated character constant beginning at %C");
954 e->expr_type = EXPR_CONSTANT;
956 e->ts.type = BT_CHARACTER;
958 e->where = start_locus;
960 e->value.character.string = p = gfc_getmem (length + 1);
961 e->value.character.length = length;
963 gfc_current_locus = start_locus;
964 gfc_next_char (); /* Skip delimiter */
966 for (i = 0; i < length; i++)
967 *p++ = next_string_char (delimiter);
969 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
971 if (next_string_char (delimiter) != -1)
972 gfc_internal_error ("match_string_constant(): Delimiter not found");
974 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
975 e->expr_type = EXPR_SUBSTRING;
982 gfc_current_locus = old_locus;
987 /* Match a .true. or .false. */
990 match_logical_constant (gfc_expr ** result)
992 static mstring logical_ops[] = {
993 minit (".false.", 0),
1001 i = gfc_match_strings (logical_ops);
1009 kind = gfc_default_logical_kind;
1011 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1012 gfc_error ("Bad kind for logical constant at %C");
1014 e = gfc_get_expr ();
1016 e->expr_type = EXPR_CONSTANT;
1017 e->value.logical = i;
1018 e->ts.type = BT_LOGICAL;
1020 e->where = gfc_current_locus;
1027 /* Match a real or imaginary part of a complex constant that is a
1028 symbolic constant. */
1031 match_sym_complex_part (gfc_expr ** result)
1033 char name[GFC_MAX_SYMBOL_LEN + 1];
1038 m = gfc_match_name (name);
1042 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1045 if (sym->attr.flavor != FL_PARAMETER)
1047 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1051 if (!gfc_numeric_ts (&sym->value->ts))
1053 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1057 if (sym->value->rank != 0)
1059 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1063 switch (sym->value->ts.type)
1066 e = gfc_copy_expr (sym->value);
1070 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1076 e = gfc_int2real (sym->value, gfc_default_real_kind);
1082 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1085 *result = e; /* e is a scalar, real, constant expression */
1089 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1094 /* Match a real or imaginary part of a complex number. */
1097 match_complex_part (gfc_expr ** result)
1101 m = match_sym_complex_part (result);
1105 m = match_real_constant (result, 1);
1109 return match_integer_constant (result, 1);
1113 /* Try to match a complex constant. */
1116 match_complex_constant (gfc_expr ** result)
1118 gfc_expr *e, *real, *imag;
1119 gfc_error_buf old_error;
1120 gfc_typespec target;
1125 old_loc = gfc_current_locus;
1126 real = imag = e = NULL;
1128 m = gfc_match_char ('(');
1132 gfc_push_error (&old_error);
1134 m = match_complex_part (&real);
1137 gfc_free_error (&old_error);
1141 if (gfc_match_char (',') == MATCH_NO)
1143 gfc_pop_error (&old_error);
1148 /* If m is error, then something was wrong with the real part and we
1149 assume we have a complex constant because we've seen the ','. An
1150 ambiguous case here is the start of an iterator list of some
1151 sort. These sort of lists are matched prior to coming here. */
1153 if (m == MATCH_ERROR)
1155 gfc_free_error (&old_error);
1158 gfc_pop_error (&old_error);
1160 m = match_complex_part (&imag);
1163 if (m == MATCH_ERROR)
1166 m = gfc_match_char (')');
1169 /* Give the matcher for implied do-loops a chance to run. This
1170 yields a much saner error message for (/ (i, 4=i, 6) /). */
1171 if (gfc_peek_char () == '=')
1180 if (m == MATCH_ERROR)
1183 /* Decide on the kind of this complex number. */
1184 if (real->ts.type == BT_REAL)
1186 if (imag->ts.type == BT_REAL)
1187 kind = gfc_kind_max (real, imag);
1189 kind = real->ts.kind;
1193 if (imag->ts.type == BT_REAL)
1194 kind = imag->ts.kind;
1196 kind = gfc_default_real_kind;
1198 target.type = BT_REAL;
1201 if (real->ts.type != BT_REAL || kind != real->ts.kind)
1202 gfc_convert_type (real, &target, 2);
1203 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1204 gfc_convert_type (imag, &target, 2);
1206 e = gfc_convert_complex (real, imag, kind);
1207 e->where = gfc_current_locus;
1209 gfc_free_expr (real);
1210 gfc_free_expr (imag);
1216 gfc_error ("Syntax error in COMPLEX constant at %C");
1221 gfc_free_expr (real);
1222 gfc_free_expr (imag);
1223 gfc_current_locus = old_loc;
1229 /* Match constants in any of several forms. Returns nonzero for a
1230 match, zero for no match. */
1233 gfc_match_literal_constant (gfc_expr ** result, int signflag)
1237 m = match_complex_constant (result);
1241 m = match_string_constant (result);
1245 m = match_boz_constant (result);
1249 m = match_real_constant (result, signflag);
1253 m = match_hollerith_constant (result);
1257 m = match_integer_constant (result, signflag);
1261 m = match_logical_constant (result);
1269 /* Match a single actual argument value. An actual argument is
1270 usually an expression, but can also be a procedure name. If the
1271 argument is a single name, it is not always possible to tell
1272 whether the name is a dummy procedure or not. We treat these cases
1273 by creating an argument that looks like a dummy procedure and
1274 fixing things later during resolution. */
1277 match_actual_arg (gfc_expr ** result)
1279 char name[GFC_MAX_SYMBOL_LEN + 1];
1280 gfc_symtree *symtree;
1285 where = gfc_current_locus;
1287 switch (gfc_match_name (name))
1296 w = gfc_current_locus;
1297 gfc_gobble_whitespace ();
1298 c = gfc_next_char ();
1299 gfc_current_locus = w;
1301 if (c != ',' && c != ')')
1304 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1306 /* Handle error elsewhere. */
1308 /* Eliminate a couple of common cases where we know we don't
1309 have a function argument. */
1310 if (symtree == NULL)
1312 gfc_get_sym_tree (name, NULL, &symtree);
1313 gfc_set_sym_referenced (symtree->n.sym);
1319 sym = symtree->n.sym;
1320 gfc_set_sym_referenced (sym);
1321 if (sym->attr.flavor != FL_PROCEDURE
1322 && sym->attr.flavor != FL_UNKNOWN)
1325 /* If the symbol is a function with itself as the result and
1326 is being defined, then we have a variable. */
1327 if (sym->attr.function && sym->result == sym)
1329 if (gfc_current_ns->proc_name == sym
1330 || (gfc_current_ns->parent != NULL
1331 && gfc_current_ns->parent->proc_name == sym))
1335 && (sym->ns == gfc_current_ns
1336 || sym->ns == gfc_current_ns->parent))
1338 gfc_entry_list *el = NULL;
1340 for (el = sym->ns->entries; el; el = el->next)
1350 e = gfc_get_expr (); /* Leave it unknown for now */
1351 e->symtree = symtree;
1352 e->expr_type = EXPR_VARIABLE;
1353 e->ts.type = BT_PROCEDURE;
1360 gfc_current_locus = where;
1361 return gfc_match_expr (result);
1365 /* Match a keyword argument. */
1368 match_keyword_arg (gfc_actual_arglist * actual, gfc_actual_arglist * base)
1370 char name[GFC_MAX_SYMBOL_LEN + 1];
1371 gfc_actual_arglist *a;
1375 name_locus = gfc_current_locus;
1376 m = gfc_match_name (name);
1380 if (gfc_match_char ('=') != MATCH_YES)
1386 m = match_actual_arg (&actual->expr);
1390 /* Make sure this name has not appeared yet. */
1392 if (name[0] != '\0')
1394 for (a = base; a; a = a->next)
1395 if (a->name != NULL && strcmp (a->name, name) == 0)
1398 ("Keyword '%s' at %C has already appeared in the current "
1399 "argument list", name);
1404 actual->name = gfc_get_string (name);
1408 gfc_current_locus = name_locus;
1413 /* Matches an actual argument list of a function or subroutine, from
1414 the opening parenthesis to the closing parenthesis. The argument
1415 list is assumed to allow keyword arguments because we don't know if
1416 the symbol associated with the procedure has an implicit interface
1417 or not. We make sure keywords are unique. If SUB_FLAG is set,
1418 we're matching the argument list of a subroutine. */
1421 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist ** argp)
1423 gfc_actual_arglist *head, *tail;
1425 gfc_st_label *label;
1429 *argp = tail = NULL;
1430 old_loc = gfc_current_locus;
1434 if (gfc_match_char ('(') == MATCH_NO)
1435 return (sub_flag) ? MATCH_YES : MATCH_NO;
1437 if (gfc_match_char (')') == MATCH_YES)
1444 head = tail = gfc_get_actual_arglist ();
1447 tail->next = gfc_get_actual_arglist ();
1451 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1453 m = gfc_match_st_label (&label, 0);
1455 gfc_error ("Expected alternate return label at %C");
1459 tail->label = label;
1463 /* After the first keyword argument is seen, the following
1464 arguments must also have keywords. */
1467 m = match_keyword_arg (tail, head);
1469 if (m == MATCH_ERROR)
1474 ("Missing keyword name in actual argument list at %C");
1481 /* See if we have the first keyword argument. */
1482 m = match_keyword_arg (tail, head);
1485 if (m == MATCH_ERROR)
1490 /* Try for a non-keyword argument. */
1491 m = match_actual_arg (&tail->expr);
1492 if (m == MATCH_ERROR)
1500 if (gfc_match_char (')') == MATCH_YES)
1502 if (gfc_match_char (',') != MATCH_YES)
1510 gfc_error ("Syntax error in argument list at %C");
1513 gfc_free_actual_arglist (head);
1514 gfc_current_locus = old_loc;
1520 /* Used by match_varspec() to extend the reference list by one
1524 extend_ref (gfc_expr * primary, gfc_ref * tail)
1527 if (primary->ref == NULL)
1528 primary->ref = tail = gfc_get_ref ();
1532 gfc_internal_error ("extend_ref(): Bad tail");
1533 tail->next = gfc_get_ref ();
1541 /* Match any additional specifications associated with the current
1542 variable like member references or substrings. If equiv_flag is
1543 set we only match stuff that is allowed inside an EQUIVALENCE
1547 match_varspec (gfc_expr * primary, int equiv_flag)
1549 char name[GFC_MAX_SYMBOL_LEN + 1];
1550 gfc_ref *substring, *tail;
1551 gfc_component *component;
1552 gfc_symbol *sym = primary->symtree->n.sym;
1557 if ((equiv_flag && gfc_peek_char () == '(')
1558 || sym->attr.dimension)
1560 /* In EQUIVALENCE, we don't know yet whether we are seeing
1561 an array, character variable or array of character
1562 variables. We'll leave the decision till resolve
1564 tail = extend_ref (primary, tail);
1565 tail->type = REF_ARRAY;
1567 m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1572 if (equiv_flag && gfc_peek_char () == '(')
1574 tail = extend_ref (primary, tail);
1575 tail->type = REF_ARRAY;
1577 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1583 primary->ts = sym->ts;
1588 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1589 goto check_substring;
1591 sym = sym->ts.derived;
1595 m = gfc_match_name (name);
1597 gfc_error ("Expected structure component name at %C");
1601 component = gfc_find_component (sym, name);
1602 if (component == NULL)
1605 tail = extend_ref (primary, tail);
1606 tail->type = REF_COMPONENT;
1608 tail->u.c.component = component;
1609 tail->u.c.sym = sym;
1611 primary->ts = component->ts;
1613 if (component->as != NULL)
1615 tail = extend_ref (primary, tail);
1616 tail->type = REF_ARRAY;
1618 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1623 if (component->ts.type != BT_DERIVED
1624 || gfc_match_char ('%') != MATCH_YES)
1627 sym = component->ts.derived;
1631 if (primary->ts.type == BT_CHARACTER)
1633 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1637 primary->ref = substring;
1639 tail->next = substring;
1641 if (primary->expr_type == EXPR_CONSTANT)
1642 primary->expr_type = EXPR_SUBSTRING;
1645 primary->ts.cl = NULL;
1661 /* Given an expression that is a variable, figure out what the
1662 ultimate variable's type and attribute is, traversing the reference
1663 structures if necessary.
1665 This subroutine is trickier than it looks. We start at the base
1666 symbol and store the attribute. Component references load a
1667 completely new attribute.
1669 A couple of rules come into play. Subobjects of targets are always
1670 targets themselves. If we see a component that goes through a
1671 pointer, then the expression must also be a target, since the
1672 pointer is associated with something (if it isn't core will soon be
1673 dumped). If we see a full part or section of an array, the
1674 expression is also an array.
1676 We can have at most one full array reference. */
1679 gfc_variable_attr (gfc_expr * expr, gfc_typespec * ts)
1681 int dimension, pointer, target;
1682 symbol_attribute attr;
1685 if (expr->expr_type != EXPR_VARIABLE)
1686 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1689 attr = expr->symtree->n.sym->attr;
1691 dimension = attr.dimension;
1692 pointer = attr.pointer;
1694 target = attr.target;
1698 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1699 *ts = expr->symtree->n.sym->ts;
1701 for (; ref; ref = ref->next)
1706 switch (ref->u.ar.type)
1722 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1728 gfc_get_component_attr (&attr, ref->u.c.component);
1730 *ts = ref->u.c.component->ts;
1732 pointer = ref->u.c.component->pointer;
1743 attr.dimension = dimension;
1744 attr.pointer = pointer;
1745 attr.target = target;
1751 /* Return the attribute from a general expression. */
1754 gfc_expr_attr (gfc_expr * e)
1756 symbol_attribute attr;
1758 switch (e->expr_type)
1761 attr = gfc_variable_attr (e, NULL);
1765 gfc_clear_attr (&attr);
1767 if (e->value.function.esym != NULL)
1768 attr = e->value.function.esym->result->attr;
1770 /* TODO: NULL() returns pointers. May have to take care of this
1776 gfc_clear_attr (&attr);
1784 /* Match a structure constructor. The initial symbol has already been
1788 gfc_match_structure_constructor (gfc_symbol * sym, gfc_expr ** result)
1790 gfc_constructor *head, *tail;
1791 gfc_component *comp;
1798 if (gfc_match_char ('(') != MATCH_YES)
1801 where = gfc_current_locus;
1803 gfc_find_component (sym, NULL);
1805 for (comp = sym->components; comp; comp = comp->next)
1808 tail = head = gfc_get_constructor ();
1811 tail->next = gfc_get_constructor ();
1815 m = gfc_match_expr (&tail->expr);
1818 if (m == MATCH_ERROR)
1821 if (gfc_match_char (',') == MATCH_YES)
1823 if (comp->next == NULL)
1826 ("Too many components in structure constructor at %C");
1836 if (gfc_match_char (')') != MATCH_YES)
1839 if (comp->next != NULL)
1841 gfc_error ("Too few components in structure constructor at %C");
1845 e = gfc_get_expr ();
1847 e->expr_type = EXPR_STRUCTURE;
1849 e->ts.type = BT_DERIVED;
1850 e->ts.derived = sym;
1853 e->value.constructor = head;
1859 gfc_error ("Syntax error in structure constructor at %C");
1862 gfc_free_constructor (head);
1867 /* Matches a variable name followed by anything that might follow it--
1868 array reference, argument list of a function, etc. */
1871 gfc_match_rvalue (gfc_expr ** result)
1873 gfc_actual_arglist *actual_arglist;
1874 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
1877 gfc_symtree *symtree;
1878 locus where, old_loc;
1883 m = gfc_match_name (name);
1887 if (gfc_find_state (COMP_INTERFACE) == SUCCESS)
1888 i = gfc_get_sym_tree (name, NULL, &symtree);
1890 i = gfc_get_ha_sym_tree (name, &symtree);
1895 sym = symtree->n.sym;
1897 where = gfc_current_locus;
1899 gfc_set_sym_referenced (sym);
1901 if (sym->attr.function && sym->result == sym)
1903 if (gfc_current_ns->proc_name == sym
1904 || (gfc_current_ns->parent != NULL
1905 && gfc_current_ns->parent->proc_name == sym))
1909 && (sym->ns == gfc_current_ns
1910 || sym->ns == gfc_current_ns->parent))
1912 gfc_entry_list *el = NULL;
1914 for (el = sym->ns->entries; el; el = el->next)
1920 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
1923 if (sym->attr.generic)
1924 goto generic_function;
1926 switch (sym->attr.flavor)
1930 if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
1931 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
1932 gfc_set_default_type (sym, 0, sym->ns);
1934 e = gfc_get_expr ();
1936 e->expr_type = EXPR_VARIABLE;
1937 e->symtree = symtree;
1939 m = match_varspec (e, 0);
1943 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
1944 end up here. Unfortunately, sym->value->expr_type is set to
1945 EXPR_CONSTANT, and so the if () branch would be followed without
1946 the !sym->as check. */
1947 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
1948 e = gfc_copy_expr (sym->value);
1951 e = gfc_get_expr ();
1952 e->expr_type = EXPR_VARIABLE;
1955 e->symtree = symtree;
1956 m = match_varspec (e, 0);
1960 sym = gfc_use_derived (sym);
1964 m = gfc_match_structure_constructor (sym, &e);
1967 /* If we're here, then the name is known to be the name of a
1968 procedure, yet it is not sure to be the name of a function. */
1970 if (sym->attr.subroutine)
1972 gfc_error ("Unexpected use of subroutine name '%s' at %C",
1978 /* At this point, the name has to be a non-statement function.
1979 If the name is the same as the current function being
1980 compiled, then we have a variable reference (to the function
1981 result) if the name is non-recursive. */
1983 st = gfc_enclosing_unit (NULL);
1985 if (st != NULL && st->state == COMP_FUNCTION
1987 && !sym->attr.recursive)
1989 e = gfc_get_expr ();
1990 e->symtree = symtree;
1991 e->expr_type = EXPR_VARIABLE;
1993 m = match_varspec (e, 0);
1997 /* Match a function reference. */
1999 m = gfc_match_actual_arglist (0, &actual_arglist);
2002 if (sym->attr.proc == PROC_ST_FUNCTION)
2003 gfc_error ("Statement function '%s' requires argument list at %C",
2006 gfc_error ("Function '%s' requires an argument list at %C",
2019 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
2020 sym = symtree->n.sym;
2022 e = gfc_get_expr ();
2023 e->symtree = symtree;
2024 e->expr_type = EXPR_FUNCTION;
2025 e->value.function.actual = actual_arglist;
2026 e->where = gfc_current_locus;
2028 if (sym->as != NULL)
2029 e->rank = sym->as->rank;
2031 if (!sym->attr.function
2032 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2038 if (sym->result == NULL)
2046 /* Special case for derived type variables that get their types
2047 via an IMPLICIT statement. This can't wait for the
2048 resolution phase. */
2050 if (gfc_peek_char () == '%'
2051 && sym->ts.type == BT_UNKNOWN
2052 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2053 gfc_set_default_type (sym, 0, sym->ns);
2055 /* If the symbol has a dimension attribute, the expression is a
2058 if (sym->attr.dimension)
2060 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2061 sym->name, NULL) == FAILURE)
2067 e = gfc_get_expr ();
2068 e->symtree = symtree;
2069 e->expr_type = EXPR_VARIABLE;
2070 m = match_varspec (e, 0);
2074 /* Name is not an array, so we peek to see if a '(' implies a
2075 function call or a substring reference. Otherwise the
2076 variable is just a scalar. */
2078 gfc_gobble_whitespace ();
2079 if (gfc_peek_char () != '(')
2081 /* Assume a scalar variable */
2082 e = gfc_get_expr ();
2083 e->symtree = symtree;
2084 e->expr_type = EXPR_VARIABLE;
2086 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2087 sym->name, NULL) == FAILURE)
2094 m = match_varspec (e, 0);
2098 /* See if this is a function reference with a keyword argument
2099 as first argument. We do this because otherwise a spurious
2100 symbol would end up in the symbol table. */
2102 old_loc = gfc_current_locus;
2103 m2 = gfc_match (" ( %n =", argname);
2104 gfc_current_locus = old_loc;
2106 e = gfc_get_expr ();
2107 e->symtree = symtree;
2109 if (m2 != MATCH_YES)
2111 /* See if this could possibly be a substring reference of a name
2112 that we're not sure is a variable yet. */
2114 if ((sym->ts.type == BT_UNKNOWN || sym->ts.type == BT_CHARACTER)
2115 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2118 e->expr_type = EXPR_VARIABLE;
2120 if (sym->attr.flavor != FL_VARIABLE
2121 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2122 sym->name, NULL) == FAILURE)
2128 if (sym->ts.type == BT_UNKNOWN
2129 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2143 /* Give up, assume we have a function. */
2145 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2146 sym = symtree->n.sym;
2147 e->expr_type = EXPR_FUNCTION;
2149 if (!sym->attr.function
2150 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2158 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2160 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2168 /* If our new function returns a character, array or structure
2169 type, it might have subsequent references. */
2171 m = match_varspec (e, 0);
2178 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2180 e = gfc_get_expr ();
2181 e->symtree = symtree;
2182 e->expr_type = EXPR_FUNCTION;
2184 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2188 gfc_error ("Symbol at %C is not appropriate for an expression");
2204 /* Match a variable, ie something that can be assigned to. This
2205 starts as a symbol, can be a structure component or an array
2206 reference. It can be a function if the function doesn't have a
2207 separate RESULT variable. If the symbol has not been previously
2208 seen, we assume it is a variable.
2210 This function is called by two interface functions:
2211 gfc_match_variable, which has host_flag = 1, and
2212 gfc_match_equiv_variable, with host_flag = 0, to restrict the
2213 match of the symbol to the local scope. */
2216 match_variable (gfc_expr ** result, int equiv_flag, int host_flag)
2224 m = gfc_match_sym_tree (&st, host_flag);
2227 where = gfc_current_locus;
2230 gfc_set_sym_referenced (sym);
2231 switch (sym->attr.flavor)
2237 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2238 sym->name, NULL) == FAILURE)
2243 /* Check for a nonrecursive function result */
2244 if (sym->attr.function && (sym->result == sym || sym->attr.entry))
2246 /* If a function result is a derived type, then the derived
2247 type may still have to be resolved. */
2249 if (sym->ts.type == BT_DERIVED
2250 && gfc_use_derived (sym->ts.derived) == NULL)
2255 /* Fall through to error */
2258 gfc_error ("Expected VARIABLE at %C");
2262 /* Special case for derived type variables that get their types
2263 via an IMPLICIT statement. This can't wait for the
2264 resolution phase. */
2267 gfc_namespace * implicit_ns;
2269 if (gfc_current_ns->proc_name == sym)
2270 implicit_ns = gfc_current_ns;
2272 implicit_ns = sym->ns;
2274 if (gfc_peek_char () == '%'
2275 && sym->ts.type == BT_UNKNOWN
2276 && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2277 gfc_set_default_type (sym, 0, implicit_ns);
2280 expr = gfc_get_expr ();
2282 expr->expr_type = EXPR_VARIABLE;
2285 expr->where = where;
2287 /* Now see if we have to do more. */
2288 m = match_varspec (expr, equiv_flag);
2291 gfc_free_expr (expr);
2300 gfc_match_variable (gfc_expr ** result, int equiv_flag)
2302 return match_variable (result, equiv_flag, 1);
2306 gfc_match_equiv_variable (gfc_expr ** result)
2308 return match_variable (result, 1, 0);