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 "Extention: 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;
312 old_loc = gfc_current_locus;
313 gfc_gobble_whitespace ();
316 switch (gfc_next_char ())
331 rname = "hexadecimal";
337 /* No whitespace allowed here. */
339 delim = gfc_next_char ();
340 if (delim != '\'' && delim != '\"')
343 if (x_hex && pedantic
344 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
345 "constant at %C uses non-standard syntax.")
349 old_loc = gfc_current_locus;
351 length = match_digits (0, radix, NULL);
354 gfc_error ("Empty set of digits in %s constants at %C", rname);
358 if (gfc_next_char () != delim)
360 gfc_error ("Illegal character in %s constant at %C.", rname);
364 gfc_current_locus = old_loc;
366 buffer = alloca (length + 1);
367 memset (buffer, '\0', length + 1);
369 match_digits (0, radix, buffer);
370 gfc_next_char (); /* Eat delimiter. */
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." */
380 kind = gfc_max_integer_kind;
381 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
383 if (gfc_range_check (e) != ARITH_OK)
385 gfc_error ("Integer too big for integer kind %i at %C", kind);
395 gfc_current_locus = old_loc;
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. */
404 match_real_constant (gfc_expr ** result, int signflag)
406 int kind, c, count, seen_dp, seen_digits, exp_char;
407 locus old_loc, temp_loc;
412 old_loc = gfc_current_locus;
413 gfc_gobble_whitespace ();
423 c = gfc_next_char ();
424 if (signflag && (c == '+' || c == '-'))
429 gfc_gobble_whitespace ();
430 c = gfc_next_char ();
433 /* Scan significand. */
434 for (;; c = gfc_next_char (), count++)
441 /* Check to see if "." goes with a following operator like ".eq.". */
442 temp_loc = gfc_current_locus;
443 c = gfc_next_char ();
445 if (c == 'e' || c == 'd' || c == 'q')
447 c = gfc_next_char ();
449 goto done; /* Operator named .e. or .d. */
453 goto done; /* Distinguish 1.e9 from 1.eq.2 */
455 gfc_current_locus = temp_loc;
470 || (c != 'e' && c != 'd' && c != 'q'))
475 c = gfc_next_char ();
478 if (c == '+' || c == '-')
479 { /* optional sign */
480 c = gfc_next_char ();
486 gfc_error ("Missing exponent in real number at %C");
492 c = gfc_next_char ();
497 /* Check that we have a numeric constant. */
498 if (!seen_digits || (!seen_dp && exp_char == ' '))
500 gfc_current_locus = old_loc;
504 /* Convert the number. */
505 gfc_current_locus = old_loc;
506 gfc_gobble_whitespace ();
508 buffer = alloca (count + 1);
509 memset (buffer, '\0', count + 1);
512 c = gfc_next_char ();
513 if (c == '+' || c == '-')
515 gfc_gobble_whitespace ();
516 c = gfc_next_char ();
519 /* Hack for mpfr_set_str(). */
522 if (c == 'd' || c == 'q')
530 c = gfc_next_char ();
543 ("Real number at %C has a 'd' exponent and an explicit kind");
546 kind = gfc_default_double_kind;
553 ("Real number at %C has a 'q' exponent and an explicit kind");
556 kind = gfc_option.q_kind;
561 kind = gfc_default_real_kind;
563 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
565 gfc_error ("Invalid real kind %d at %C", kind);
570 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
572 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
574 switch (gfc_range_check (e))
579 gfc_error ("Real constant overflows its kind at %C");
582 case ARITH_UNDERFLOW:
583 if (gfc_option.warn_underflow)
584 gfc_warning ("Real constant underflows its kind at %C");
585 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
589 gfc_internal_error ("gfc_range_check() returned bad value");
601 /* Match a substring reference. */
604 match_substring (gfc_charlen * cl, int init, gfc_ref ** result)
606 gfc_expr *start, *end;
614 old_loc = gfc_current_locus;
616 m = gfc_match_char ('(');
620 if (gfc_match_char (':') != MATCH_YES)
623 m = gfc_match_init_expr (&start);
625 m = gfc_match_expr (&start);
633 m = gfc_match_char (':');
638 if (gfc_match_char (')') != MATCH_YES)
641 m = gfc_match_init_expr (&end);
643 m = gfc_match_expr (&end);
647 if (m == MATCH_ERROR)
650 m = gfc_match_char (')');
655 /* Optimize away the (:) reference. */
656 if (start == NULL && end == NULL)
660 ref = gfc_get_ref ();
662 ref->type = REF_SUBSTRING;
664 start = gfc_int_expr (1);
665 ref->u.ss.start = start;
666 if (end == NULL && cl)
667 end = gfc_copy_expr (cl->length);
669 ref->u.ss.length = cl;
676 gfc_error ("Syntax error in SUBSTRING specification at %C");
680 gfc_free_expr (start);
683 gfc_current_locus = old_loc;
688 /* Reads the next character of a string constant, taking care to
689 return doubled delimiters on the input as a single instance of
692 Special return values are:
693 -1 End of the string, as determined by the delimiter
694 -2 Unterminated string detected
696 Backslash codes are also expanded at this time. */
699 next_string_char (char delimiter)
704 c = gfc_next_char_literal (1);
709 if (gfc_option.flag_backslash && c == '\\')
711 old_locus = gfc_current_locus;
713 switch (gfc_next_char_literal (1))
741 /* Unknown backslash codes are simply not expanded */
742 gfc_current_locus = old_locus;
750 old_locus = gfc_current_locus;
751 c = gfc_next_char_literal (1);
755 gfc_current_locus = old_locus;
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:
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. */
774 match_charkind_name (char *name)
780 gfc_gobble_whitespace ();
781 c = gfc_next_char ();
790 old_loc = gfc_current_locus;
791 c = gfc_next_char ();
795 peek = gfc_peek_char ();
797 if (peek == '\'' || peek == '\"')
799 gfc_current_locus = old_loc;
807 && (gfc_option.flag_dollar_ok && c != '$'))
811 if (++len > GFC_MAX_SYMBOL_LEN)
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. */
827 match_string_constant (gfc_expr ** result)
829 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
830 int i, c, kind, length, delimiter;
831 locus old_locus, start_locus;
837 old_locus = gfc_current_locus;
839 gfc_gobble_whitespace ();
841 start_locus = gfc_current_locus;
843 c = gfc_next_char ();
844 if (c == '\'' || c == '"')
846 kind = gfc_default_character_kind;
856 kind = kind * 10 + c - '0';
859 c = gfc_next_char ();
865 gfc_current_locus = old_locus;
867 m = match_charkind_name (name);
871 if (gfc_find_symbol (name, NULL, 1, &sym)
873 || sym->attr.flavor != FL_PARAMETER)
877 c = gfc_next_char ();
882 gfc_gobble_whitespace ();
883 c = gfc_next_char ();
889 gfc_gobble_whitespace ();
890 start_locus = gfc_current_locus;
892 c = gfc_next_char ();
893 if (c != '\'' && c != '"')
898 q = gfc_extract_int (sym->value, &kind);
906 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
908 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
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? */
923 c = next_string_char (delimiter);
928 gfc_current_locus = start_locus;
929 gfc_error ("Unterminated character constant beginning at %C");
938 e->expr_type = EXPR_CONSTANT;
940 e->ts.type = BT_CHARACTER;
942 e->where = start_locus;
944 e->value.character.string = p = gfc_getmem (length + 1);
945 e->value.character.length = length;
947 gfc_current_locus = start_locus;
948 gfc_next_char (); /* Skip delimiter */
950 for (i = 0; i < length; i++)
951 *p++ = next_string_char (delimiter);
953 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
955 if (next_string_char (delimiter) != -1)
956 gfc_internal_error ("match_string_constant(): Delimiter not found");
958 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
959 e->expr_type = EXPR_SUBSTRING;
966 gfc_current_locus = old_locus;
971 /* Match a .true. or .false. */
974 match_logical_constant (gfc_expr ** result)
976 static mstring logical_ops[] = {
977 minit (".false.", 0),
985 i = gfc_match_strings (logical_ops);
993 kind = gfc_default_logical_kind;
995 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
996 gfc_error ("Bad kind for logical constant at %C");
1000 e->expr_type = EXPR_CONSTANT;
1001 e->value.logical = i;
1002 e->ts.type = BT_LOGICAL;
1004 e->where = gfc_current_locus;
1011 /* Match a real or imaginary part of a complex constant that is a
1012 symbolic constant. */
1015 match_sym_complex_part (gfc_expr ** result)
1017 char name[GFC_MAX_SYMBOL_LEN + 1];
1022 m = gfc_match_name (name);
1026 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1029 if (sym->attr.flavor != FL_PARAMETER)
1031 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1035 if (!gfc_numeric_ts (&sym->value->ts))
1037 gfc_error ("Numeric PARAMETER required in complex constant at %C");
1041 if (sym->value->rank != 0)
1043 gfc_error ("Scalar PARAMETER required in complex constant at %C");
1047 switch (sym->value->ts.type)
1050 e = gfc_copy_expr (sym->value);
1054 e = gfc_complex2real (sym->value, sym->value->ts.kind);
1060 e = gfc_int2real (sym->value, gfc_default_real_kind);
1066 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1069 *result = e; /* e is a scalar, real, constant expression */
1073 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1078 /* Match a real or imaginary part of a complex number. */
1081 match_complex_part (gfc_expr ** result)
1085 m = match_sym_complex_part (result);
1089 m = match_real_constant (result, 1);
1093 return match_integer_constant (result, 1);
1097 /* Try to match a complex constant. */
1100 match_complex_constant (gfc_expr ** result)
1102 gfc_expr *e, *real, *imag;
1103 gfc_error_buf old_error;
1104 gfc_typespec target;
1109 old_loc = gfc_current_locus;
1110 real = imag = e = NULL;
1112 m = gfc_match_char ('(');
1116 gfc_push_error (&old_error);
1118 m = match_complex_part (&real);
1121 gfc_free_error (&old_error);
1125 if (gfc_match_char (',') == MATCH_NO)
1127 gfc_pop_error (&old_error);
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. */
1137 if (m == MATCH_ERROR)
1139 gfc_free_error (&old_error);
1142 gfc_pop_error (&old_error);
1144 m = match_complex_part (&imag);
1147 if (m == MATCH_ERROR)
1150 m = gfc_match_char (')');
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 () == '=')
1164 if (m == MATCH_ERROR)
1167 /* Decide on the kind of this complex number. */
1168 if (real->ts.type == BT_REAL)
1170 if (imag->ts.type == BT_REAL)
1171 kind = gfc_kind_max (real, imag);
1173 kind = real->ts.kind;
1177 if (imag->ts.type == BT_REAL)
1178 kind = imag->ts.kind;
1180 kind = gfc_default_real_kind;
1182 target.type = BT_REAL;
1185 if (real->ts.type != BT_REAL || kind != real->ts.kind)
1186 gfc_convert_type (real, &target, 2);
1187 if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1188 gfc_convert_type (imag, &target, 2);
1190 e = gfc_convert_complex (real, imag, kind);
1191 e->where = gfc_current_locus;
1193 gfc_free_expr (real);
1194 gfc_free_expr (imag);
1200 gfc_error ("Syntax error in COMPLEX constant at %C");
1205 gfc_free_expr (real);
1206 gfc_free_expr (imag);
1207 gfc_current_locus = old_loc;
1213 /* Match constants in any of several forms. Returns nonzero for a
1214 match, zero for no match. */
1217 gfc_match_literal_constant (gfc_expr ** result, int signflag)
1221 m = match_complex_constant (result);
1225 m = match_string_constant (result);
1229 m = match_boz_constant (result);
1233 m = match_real_constant (result, signflag);
1237 m = match_hollerith_constant (result);
1241 m = match_integer_constant (result, signflag);
1245 m = match_logical_constant (result);
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. */
1261 match_actual_arg (gfc_expr ** result)
1263 char name[GFC_MAX_SYMBOL_LEN + 1];
1264 gfc_symtree *symtree;
1269 where = gfc_current_locus;
1271 switch (gfc_match_name (name))
1280 w = gfc_current_locus;
1281 gfc_gobble_whitespace ();
1282 c = gfc_next_char ();
1283 gfc_current_locus = w;
1285 if (c != ',' && c != ')')
1288 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1290 /* Handle error elsewhere. */
1292 /* Eliminate a couple of common cases where we know we don't
1293 have a function argument. */
1294 if (symtree == NULL)
1296 gfc_get_sym_tree (name, NULL, &symtree);
1297 gfc_set_sym_referenced (symtree->n.sym);
1303 sym = symtree->n.sym;
1304 gfc_set_sym_referenced (sym);
1305 if (sym->attr.flavor != FL_PROCEDURE
1306 && sym->attr.flavor != FL_UNKNOWN)
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)))
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;
1328 gfc_current_locus = where;
1329 return gfc_match_expr (result);
1333 /* Match a keyword argument. */
1336 match_keyword_arg (gfc_actual_arglist * actual, gfc_actual_arglist * base)
1338 char name[GFC_MAX_SYMBOL_LEN + 1];
1339 gfc_actual_arglist *a;
1343 name_locus = gfc_current_locus;
1344 m = gfc_match_name (name);
1348 if (gfc_match_char ('=') != MATCH_YES)
1354 m = match_actual_arg (&actual->expr);
1358 /* Make sure this name has not appeared yet. */
1360 if (name[0] != '\0')
1362 for (a = base; a; a = a->next)
1363 if (a->name != NULL && strcmp (a->name, name) == 0)
1366 ("Keyword '%s' at %C has already appeared in the current "
1367 "argument list", name);
1372 actual->name = gfc_get_string (name);
1376 gfc_current_locus = name_locus;
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
1385 or not. We make sure keywords are unique. If SUB_FLAG is set,
1386 we're matching the argument list of a subroutine. */
1389 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist ** argp)
1391 gfc_actual_arglist *head, *tail;
1393 gfc_st_label *label;
1397 *argp = tail = NULL;
1398 old_loc = gfc_current_locus;
1402 if (gfc_match_char ('(') == MATCH_NO)
1403 return (sub_flag) ? MATCH_YES : MATCH_NO;
1405 if (gfc_match_char (')') == MATCH_YES)
1412 head = tail = gfc_get_actual_arglist ();
1415 tail->next = gfc_get_actual_arglist ();
1419 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1421 m = gfc_match_st_label (&label, 0);
1423 gfc_error ("Expected alternate return label at %C");
1427 tail->label = label;
1431 /* After the first keyword argument is seen, the following
1432 arguments must also have keywords. */
1435 m = match_keyword_arg (tail, head);
1437 if (m == MATCH_ERROR)
1442 ("Missing keyword name in actual argument list at %C");
1449 /* See if we have the first keyword argument. */
1450 m = match_keyword_arg (tail, head);
1453 if (m == MATCH_ERROR)
1458 /* Try for a non-keyword argument. */
1459 m = match_actual_arg (&tail->expr);
1460 if (m == MATCH_ERROR)
1468 if (gfc_match_char (')') == MATCH_YES)
1470 if (gfc_match_char (',') != MATCH_YES)
1478 gfc_error ("Syntax error in argument list at %C");
1481 gfc_free_actual_arglist (head);
1482 gfc_current_locus = old_loc;
1488 /* Used by match_varspec() to extend the reference list by one
1492 extend_ref (gfc_expr * primary, gfc_ref * tail)
1495 if (primary->ref == NULL)
1496 primary->ref = tail = gfc_get_ref ();
1500 gfc_internal_error ("extend_ref(): Bad tail");
1501 tail->next = gfc_get_ref ();
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
1515 match_varspec (gfc_expr * primary, int equiv_flag)
1517 char name[GFC_MAX_SYMBOL_LEN + 1];
1518 gfc_ref *substring, *tail;
1519 gfc_component *component;
1520 gfc_symbol *sym = primary->symtree->n.sym;
1525 if ((equiv_flag && gfc_peek_char () == '(')
1526 || sym->attr.dimension)
1528 /* In EQUIVALENCE, we don't know yet whether we are seeing
1529 an array, character variable or array of character
1530 variables. We'll leave the decision till resolve
1532 tail = extend_ref (primary, tail);
1533 tail->type = REF_ARRAY;
1535 m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1540 if (equiv_flag && gfc_peek_char () == '(')
1542 tail = extend_ref (primary, tail);
1543 tail->type = REF_ARRAY;
1545 m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1551 primary->ts = sym->ts;
1556 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1557 goto check_substring;
1559 sym = sym->ts.derived;
1563 m = gfc_match_name (name);
1565 gfc_error ("Expected structure component name at %C");
1569 component = gfc_find_component (sym, name);
1570 if (component == NULL)
1573 tail = extend_ref (primary, tail);
1574 tail->type = REF_COMPONENT;
1576 tail->u.c.component = component;
1577 tail->u.c.sym = sym;
1579 primary->ts = component->ts;
1581 if (component->as != NULL)
1583 tail = extend_ref (primary, tail);
1584 tail->type = REF_ARRAY;
1586 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1591 if (component->ts.type != BT_DERIVED
1592 || gfc_match_char ('%') != MATCH_YES)
1595 sym = component->ts.derived;
1599 if (primary->ts.type == BT_CHARACTER)
1601 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1605 primary->ref = substring;
1607 tail->next = substring;
1609 if (primary->expr_type == EXPR_CONSTANT)
1610 primary->expr_type = EXPR_SUBSTRING;
1613 primary->ts.cl = NULL;
1629 /* Given an expression that is a variable, figure out what the
1630 ultimate variable's type and attribute is, traversing the reference
1631 structures if necessary.
1633 This subroutine is trickier than it looks. We start at the base
1634 symbol and store the attribute. Component references load a
1635 completely new attribute.
1637 A couple of rules come into play. Subobjects of targets are always
1638 targets themselves. If we see a component that goes through a
1639 pointer, then the expression must also be a target, since the
1640 pointer is associated with something (if it isn't core will soon be
1641 dumped). If we see a full part or section of an array, the
1642 expression is also an array.
1644 We can have at most one full array reference. */
1647 gfc_variable_attr (gfc_expr * expr, gfc_typespec * ts)
1649 int dimension, pointer, target;
1650 symbol_attribute attr;
1653 if (expr->expr_type != EXPR_VARIABLE)
1654 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1657 attr = expr->symtree->n.sym->attr;
1659 dimension = attr.dimension;
1660 pointer = attr.pointer;
1662 target = attr.target;
1666 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1667 *ts = expr->symtree->n.sym->ts;
1669 for (; ref; ref = ref->next)
1674 switch (ref->u.ar.type)
1690 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1696 gfc_get_component_attr (&attr, ref->u.c.component);
1698 *ts = ref->u.c.component->ts;
1700 pointer = ref->u.c.component->pointer;
1711 attr.dimension = dimension;
1712 attr.pointer = pointer;
1713 attr.target = target;
1719 /* Return the attribute from a general expression. */
1722 gfc_expr_attr (gfc_expr * e)
1724 symbol_attribute attr;
1726 switch (e->expr_type)
1729 attr = gfc_variable_attr (e, NULL);
1733 gfc_clear_attr (&attr);
1735 if (e->value.function.esym != NULL)
1736 attr = e->value.function.esym->result->attr;
1738 /* TODO: NULL() returns pointers. May have to take care of this
1744 gfc_clear_attr (&attr);
1752 /* Match a structure constructor. The initial symbol has already been
1756 gfc_match_structure_constructor (gfc_symbol * sym, gfc_expr ** result)
1758 gfc_constructor *head, *tail;
1759 gfc_component *comp;
1766 if (gfc_match_char ('(') != MATCH_YES)
1769 where = gfc_current_locus;
1771 gfc_find_component (sym, NULL);
1773 for (comp = sym->components; comp; comp = comp->next)
1776 tail = head = gfc_get_constructor ();
1779 tail->next = gfc_get_constructor ();
1783 m = gfc_match_expr (&tail->expr);
1786 if (m == MATCH_ERROR)
1789 if (gfc_match_char (',') == MATCH_YES)
1791 if (comp->next == NULL)
1794 ("Too many components in structure constructor at %C");
1804 if (gfc_match_char (')') != MATCH_YES)
1807 if (comp->next != NULL)
1809 gfc_error ("Too few components in structure constructor at %C");
1813 e = gfc_get_expr ();
1815 e->expr_type = EXPR_STRUCTURE;
1817 e->ts.type = BT_DERIVED;
1818 e->ts.derived = sym;
1821 e->value.constructor = head;
1827 gfc_error ("Syntax error in structure constructor at %C");
1830 gfc_free_constructor (head);
1835 /* Matches a variable name followed by anything that might follow it--
1836 array reference, argument list of a function, etc. */
1839 gfc_match_rvalue (gfc_expr ** result)
1841 gfc_actual_arglist *actual_arglist;
1842 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
1845 gfc_symtree *symtree;
1846 locus where, old_loc;
1851 m = gfc_match_name (name);
1855 if (gfc_find_state (COMP_INTERFACE) == SUCCESS)
1856 i = gfc_get_sym_tree (name, NULL, &symtree);
1858 i = gfc_get_ha_sym_tree (name, &symtree);
1863 sym = symtree->n.sym;
1865 where = gfc_current_locus;
1867 gfc_set_sym_referenced (sym);
1869 if (sym->attr.function && sym->result == sym)
1871 if (gfc_current_ns->proc_name == sym
1872 || (gfc_current_ns->parent != NULL
1873 && gfc_current_ns->parent->proc_name == sym))
1877 && (sym->ns == gfc_current_ns
1878 || sym->ns == gfc_current_ns->parent))
1880 gfc_entry_list *el = NULL;
1882 for (el = sym->ns->entries; el; el = el->next)
1888 if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
1891 if (sym->attr.generic)
1892 goto generic_function;
1894 switch (sym->attr.flavor)
1898 if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
1899 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
1900 gfc_set_default_type (sym, 0, sym->ns);
1902 e = gfc_get_expr ();
1904 e->expr_type = EXPR_VARIABLE;
1905 e->symtree = symtree;
1907 m = match_varspec (e, 0);
1911 /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
1912 end up here. Unfortunately, sym->value->expr_type is set to
1913 EXPR_CONSTANT, and so the if () branch would be followed without
1914 the !sym->as check. */
1915 if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
1916 e = gfc_copy_expr (sym->value);
1919 e = gfc_get_expr ();
1920 e->expr_type = EXPR_VARIABLE;
1923 e->symtree = symtree;
1924 m = match_varspec (e, 0);
1928 sym = gfc_use_derived (sym);
1932 m = gfc_match_structure_constructor (sym, &e);
1935 /* If we're here, then the name is known to be the name of a
1936 procedure, yet it is not sure to be the name of a function. */
1938 if (sym->attr.subroutine)
1940 gfc_error ("Unexpected use of subroutine name '%s' at %C",
1946 /* At this point, the name has to be a non-statement function.
1947 If the name is the same as the current function being
1948 compiled, then we have a variable reference (to the function
1949 result) if the name is non-recursive. */
1951 st = gfc_enclosing_unit (NULL);
1953 if (st != NULL && st->state == COMP_FUNCTION
1955 && !sym->attr.recursive)
1957 e = gfc_get_expr ();
1958 e->symtree = symtree;
1959 e->expr_type = EXPR_VARIABLE;
1961 m = match_varspec (e, 0);
1965 /* Match a function reference. */
1967 m = gfc_match_actual_arglist (0, &actual_arglist);
1970 if (sym->attr.proc == PROC_ST_FUNCTION)
1971 gfc_error ("Statement function '%s' requires argument list at %C",
1974 gfc_error ("Function '%s' requires an argument list at %C",
1987 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
1988 sym = symtree->n.sym;
1990 e = gfc_get_expr ();
1991 e->symtree = symtree;
1992 e->expr_type = EXPR_FUNCTION;
1993 e->value.function.actual = actual_arglist;
1994 e->where = gfc_current_locus;
1996 if (sym->as != NULL)
1997 e->rank = sym->as->rank;
1999 if (!sym->attr.function
2000 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2006 if (sym->result == NULL)
2014 /* Special case for derived type variables that get their types
2015 via an IMPLICIT statement. This can't wait for the
2016 resolution phase. */
2018 if (gfc_peek_char () == '%'
2019 && sym->ts.type == BT_UNKNOWN
2020 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2021 gfc_set_default_type (sym, 0, sym->ns);
2023 /* If the symbol has a dimension attribute, the expression is a
2026 if (sym->attr.dimension)
2028 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2029 sym->name, NULL) == FAILURE)
2035 e = gfc_get_expr ();
2036 e->symtree = symtree;
2037 e->expr_type = EXPR_VARIABLE;
2038 m = match_varspec (e, 0);
2042 /* Name is not an array, so we peek to see if a '(' implies a
2043 function call or a substring reference. Otherwise the
2044 variable is just a scalar. */
2046 gfc_gobble_whitespace ();
2047 if (gfc_peek_char () != '(')
2049 /* Assume a scalar variable */
2050 e = gfc_get_expr ();
2051 e->symtree = symtree;
2052 e->expr_type = EXPR_VARIABLE;
2054 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2055 sym->name, NULL) == FAILURE)
2062 m = match_varspec (e, 0);
2066 /* See if this is a function reference with a keyword argument
2067 as first argument. We do this because otherwise a spurious
2068 symbol would end up in the symbol table. */
2070 old_loc = gfc_current_locus;
2071 m2 = gfc_match (" ( %n =", argname);
2072 gfc_current_locus = old_loc;
2074 e = gfc_get_expr ();
2075 e->symtree = symtree;
2077 if (m2 != MATCH_YES)
2079 /* See if this could possibly be a substring reference of a name
2080 that we're not sure is a variable yet. */
2082 if ((sym->ts.type == BT_UNKNOWN || sym->ts.type == BT_CHARACTER)
2083 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2086 e->expr_type = EXPR_VARIABLE;
2088 if (sym->attr.flavor != FL_VARIABLE
2089 && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2090 sym->name, NULL) == FAILURE)
2096 if (sym->ts.type == BT_UNKNOWN
2097 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2111 /* Give up, assume we have a function. */
2113 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2114 sym = symtree->n.sym;
2115 e->expr_type = EXPR_FUNCTION;
2117 if (!sym->attr.function
2118 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2126 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2128 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2136 /* If our new function returns a character, array or structure
2137 type, it might have subsequent references. */
2139 m = match_varspec (e, 0);
2146 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2148 e = gfc_get_expr ();
2149 e->symtree = symtree;
2150 e->expr_type = EXPR_FUNCTION;
2152 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2156 gfc_error ("Symbol at %C is not appropriate for an expression");
2172 /* Match a variable, ie something that can be assigned to. This
2173 starts as a symbol, can be a structure component or an array
2174 reference. It can be a function if the function doesn't have a
2175 separate RESULT variable. If the symbol has not been previously
2176 seen, we assume it is a variable. */
2179 gfc_match_variable (gfc_expr ** result, int equiv_flag)
2187 m = gfc_match_sym_tree (&st, 1);
2190 where = gfc_current_locus;
2193 gfc_set_sym_referenced (sym);
2194 switch (sym->attr.flavor)
2200 if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2201 sym->name, NULL) == FAILURE)
2206 /* Check for a nonrecursive function result */
2207 if (sym->attr.function && (sym->result == sym || sym->attr.entry))
2209 /* If a function result is a derived type, then the derived
2210 type may still have to be resolved. */
2212 if (sym->ts.type == BT_DERIVED
2213 && gfc_use_derived (sym->ts.derived) == NULL)
2218 /* Fall through to error */
2221 gfc_error ("Expected VARIABLE at %C");
2225 /* Special case for derived type variables that get their types
2226 via an IMPLICIT statement. This can't wait for the
2227 resolution phase. */
2230 gfc_namespace * implicit_ns;
2232 if (gfc_current_ns->proc_name == sym)
2233 implicit_ns = gfc_current_ns;
2235 implicit_ns = sym->ns;
2237 if (gfc_peek_char () == '%'
2238 && sym->ts.type == BT_UNKNOWN
2239 && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2240 gfc_set_default_type (sym, 0, implicit_ns);
2243 expr = gfc_get_expr ();
2245 expr->expr_type = EXPR_VARIABLE;
2248 expr->where = where;
2250 /* Now see if we have to do more. */
2251 m = match_varspec (expr, equiv_flag);
2254 gfc_free_expr (expr);