1 /* Primary expression subroutines
2 Copyright (C) 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 /* Matches a kind-parameter expression, which is either a named
35 symbolic constant or a nonnegative integer constant. If
36 successful, sets the kind value to the correct integer. */
39 match_kind_param (int *kind)
41 char name[GFC_MAX_SYMBOL_LEN + 1];
46 m = gfc_match_small_literal_int (kind);
50 m = gfc_match_name (name);
54 if (gfc_find_symbol (name, NULL, 1, &sym))
60 if (sym->attr.flavor != FL_PARAMETER)
63 p = gfc_extract_int (sym->value, kind);
74 /* Get a trailing kind-specification for non-character variables.
76 the integer kind value or:
77 -1 if an error was generated
78 -2 if no kind was found */
86 if (gfc_match_char ('_') != MATCH_YES)
89 m = match_kind_param (&kind);
91 gfc_error ("Missing kind-parameter at %C");
93 return (m == MATCH_YES) ? kind : -1;
97 /* Given a character and a radix, see if the character is a valid
98 digit in that radix. */
101 check_digit (int c, int radix)
108 r = ('0' <= c && c <= '1');
112 r = ('0' <= c && c <= '7');
116 r = ('0' <= c && c <= '9');
120 r = ('0' <= c && c <= '9') || ('a' <= c && c <= 'f');
124 gfc_internal_error ("check_digit(): bad radix");
131 /* Match the digit string part of an integer if signflag is not set,
132 the signed digit string part if signflag is set. If the buffer
133 is NULL, we just count characters for the resolution pass. Returns
134 the number of characters matched, -1 for no match. */
137 match_digits (int signflag, int radix, char *buffer)
143 c = gfc_next_char ();
145 if (signflag && (c == '+' || c == '-'))
149 c = gfc_next_char ();
153 if (!check_digit (c, radix))
162 old_loc = gfc_current_locus;
163 c = gfc_next_char ();
165 if (!check_digit (c, radix))
173 gfc_current_locus = old_loc;
179 /* Match an integer (digit string and optional kind).
180 A sign will be accepted if signflag is set. */
183 match_integer_constant (gfc_expr ** result, int signflag)
190 old_loc = gfc_current_locus;
191 gfc_gobble_whitespace ();
193 length = match_digits (signflag, 10, NULL);
194 gfc_current_locus = old_loc;
198 buffer = alloca (length + 1);
199 memset (buffer, '\0', length + 1);
201 gfc_gobble_whitespace ();
203 match_digits (signflag, 10, buffer);
207 kind = gfc_default_integer_kind;
211 if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
213 gfc_error ("Integer kind %d at %C not available", kind);
217 e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
219 if (gfc_range_check (e) != ARITH_OK)
221 gfc_error ("Integer too big for its kind at %C");
232 /* Match a binary, octal or hexadecimal constant that can be found in
236 match_boz_constant (gfc_expr ** result)
238 int radix, delim, length, x_hex, kind;
244 old_loc = gfc_current_locus;
245 gfc_gobble_whitespace ();
248 switch (gfc_next_char ())
263 rname = "hexadecimal";
269 /* No whitespace allowed here. */
271 delim = gfc_next_char ();
272 if (delim != '\'' && delim != '\"')
275 if (x_hex && pedantic
276 && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
277 "constant at %C uses non-standard syntax.")
281 old_loc = gfc_current_locus;
283 length = match_digits (0, radix, NULL);
286 gfc_error ("Empty set of digits in %s constants at %C", rname);
290 if (gfc_next_char () != delim)
292 gfc_error ("Illegal character in %s constant at %C.", rname);
296 gfc_current_locus = old_loc;
298 buffer = alloca (length + 1);
299 memset (buffer, '\0', length + 1);
301 match_digits (0, radix, buffer);
302 gfc_next_char (); /* Eat delimiter. */
308 kind = gfc_default_integer_kind;
310 && (gfc_notify_std (GFC_STD_GNU, "Extension: Kind parameter "
311 "suffix to boz literal constant at %C.")
315 e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
317 if (gfc_range_check (e) != ARITH_OK)
319 gfc_error ("Integer too big for integer kind %i at %C", kind);
329 gfc_current_locus = old_loc;
334 /* Match a real constant of some sort. */
337 match_real_constant (gfc_expr ** result, int signflag)
339 int kind, c, count, seen_dp, seen_digits, exp_char;
340 locus old_loc, temp_loc;
344 old_loc = gfc_current_locus;
345 gfc_gobble_whitespace ();
354 c = gfc_next_char ();
355 if (signflag && (c == '+' || c == '-'))
357 c = gfc_next_char ();
361 /* Scan significand. */
362 for (;; c = gfc_next_char (), count++)
369 /* Check to see if "." goes with a following operator like ".eq.". */
370 temp_loc = gfc_current_locus;
371 c = gfc_next_char ();
373 if (c == 'e' || c == 'd' || c == 'q')
375 c = gfc_next_char ();
377 goto done; /* Operator named .e. or .d. */
381 goto done; /* Distinguish 1.e9 from 1.eq.2 */
383 gfc_current_locus = temp_loc;
397 if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
402 c = gfc_next_char ();
405 if (c == '+' || c == '-')
406 { /* optional sign */
407 c = gfc_next_char ();
413 /* TODO: seen_digits is always true at this point */
416 gfc_current_locus = old_loc;
417 return MATCH_NO; /* ".e" can be something else */
420 gfc_error ("Missing exponent in real number at %C");
426 c = gfc_next_char ();
431 /* See what we've got! */
432 if (!seen_digits || (!seen_dp && exp_char == ' '))
434 gfc_current_locus = old_loc;
438 /* Convert the number. */
439 gfc_current_locus = old_loc;
440 gfc_gobble_whitespace ();
442 buffer = alloca (count + 1);
443 memset (buffer, '\0', count + 1);
445 /* Hack for mpfr_set_str(). */
449 *p = gfc_next_char ();
450 if (*p == 'd' || *p == 'q')
466 ("Real number at %C has a 'd' exponent and an explicit kind");
469 kind = gfc_default_double_kind;
476 ("Real number at %C has a 'q' exponent and an explicit kind");
479 kind = gfc_option.q_kind;
484 kind = gfc_default_real_kind;
486 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
488 gfc_error ("Invalid real kind %d at %C", kind);
493 e = gfc_convert_real (buffer, kind, &gfc_current_locus);
495 switch (gfc_range_check (e))
500 gfc_error ("Real constant overflows its kind at %C");
503 case ARITH_UNDERFLOW:
504 if (gfc_option.warn_underflow)
505 gfc_warning ("Real constant underflows its kind at %C");
506 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
510 gfc_internal_error ("gfc_range_check() returned bad value");
522 /* Match a substring reference. */
525 match_substring (gfc_charlen * cl, int init, gfc_ref ** result)
527 gfc_expr *start, *end;
535 old_loc = gfc_current_locus;
537 m = gfc_match_char ('(');
541 if (gfc_match_char (':') != MATCH_YES)
544 m = gfc_match_init_expr (&start);
546 m = gfc_match_expr (&start);
554 m = gfc_match_char (':');
559 if (gfc_match_char (')') != MATCH_YES)
562 m = gfc_match_init_expr (&end);
564 m = gfc_match_expr (&end);
568 if (m == MATCH_ERROR)
571 m = gfc_match_char (')');
576 /* Optimize away the (:) reference. */
577 if (start == NULL && end == NULL)
581 ref = gfc_get_ref ();
583 ref->type = REF_SUBSTRING;
585 start = gfc_int_expr (1);
586 ref->u.ss.start = start;
587 if (end == NULL && cl)
588 end = gfc_copy_expr (cl->length);
590 ref->u.ss.length = cl;
597 gfc_error ("Syntax error in SUBSTRING specification at %C");
601 gfc_free_expr (start);
604 gfc_current_locus = old_loc;
609 /* Reads the next character of a string constant, taking care to
610 return doubled delimiters on the input as a single instance of
613 Special return values are:
614 -1 End of the string, as determined by the delimiter
615 -2 Unterminated string detected
617 Backslash codes are also expanded at this time. */
620 next_string_char (char delimiter)
625 c = gfc_next_char_literal (1);
632 old_locus = gfc_current_locus;
634 switch (gfc_next_char_literal (1))
662 /* Unknown backslash codes are simply not expanded */
663 gfc_current_locus = old_locus;
671 old_locus = gfc_current_locus;
672 c = gfc_next_char_literal (1);
676 gfc_current_locus = old_locus;
682 /* Special case of gfc_match_name() that matches a parameter kind name
683 before a string constant. This takes case of the weird but legal
684 case of: weird case of:
688 where kind____ is a parameter. gfc_match_name() will happily slurp
689 up all the underscores, which leads to problems. If we return
690 MATCH_YES, the parse pointer points to the final underscore, which
691 is not part of the name. We never return MATCH_ERROR-- errors in
692 the name will be detected later. */
695 match_charkind_name (char *name)
701 gfc_gobble_whitespace ();
702 c = gfc_next_char ();
711 old_loc = gfc_current_locus;
712 c = gfc_next_char ();
716 peek = gfc_peek_char ();
718 if (peek == '\'' || peek == '\"')
720 gfc_current_locus = old_loc;
728 && (gfc_option.flag_dollar_ok && c != '$'))
732 if (++len > GFC_MAX_SYMBOL_LEN)
740 /* See if the current input matches a character constant. Lots of
741 contortions have to be done to match the kind parameter which comes
742 before the actual string. The main consideration is that we don't
743 want to error out too quickly. For example, we don't actually do
744 any validation of the kinds until we have actually seen a legal
745 delimiter. Using match_kind_param() generates errors too quickly. */
748 match_string_constant (gfc_expr ** result)
750 char *p, name[GFC_MAX_SYMBOL_LEN + 1];
751 int i, c, kind, length, delimiter;
752 locus old_locus, start_locus;
758 old_locus = gfc_current_locus;
760 gfc_gobble_whitespace ();
762 start_locus = gfc_current_locus;
764 c = gfc_next_char ();
765 if (c == '\'' || c == '"')
767 kind = gfc_default_character_kind;
777 kind = kind * 10 + c - '0';
780 c = gfc_next_char ();
786 gfc_current_locus = old_locus;
788 m = match_charkind_name (name);
792 if (gfc_find_symbol (name, NULL, 1, &sym)
794 || sym->attr.flavor != FL_PARAMETER)
798 c = gfc_next_char ();
803 gfc_gobble_whitespace ();
804 c = gfc_next_char ();
810 gfc_gobble_whitespace ();
811 start_locus = gfc_current_locus;
813 c = gfc_next_char ();
814 if (c != '\'' && c != '"')
819 q = gfc_extract_int (sym->value, &kind);
827 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
829 gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
834 /* Scan the string into a block of memory by first figuring out how
835 long it is, allocating the structure, then re-reading it. This
836 isn't particularly efficient, but string constants aren't that
837 common in most code. TODO: Use obstacks? */
844 c = next_string_char (delimiter);
849 gfc_current_locus = start_locus;
850 gfc_error ("Unterminated character constant beginning at %C");
859 e->expr_type = EXPR_CONSTANT;
861 e->ts.type = BT_CHARACTER;
863 e->where = start_locus;
865 e->value.character.string = p = gfc_getmem (length + 1);
866 e->value.character.length = length;
868 gfc_current_locus = start_locus;
869 gfc_next_char (); /* Skip delimiter */
871 for (i = 0; i < length; i++)
872 *p++ = next_string_char (delimiter);
874 *p = '\0'; /* TODO: C-style string is for development/debug purposes. */
876 if (next_string_char (delimiter) != -1)
877 gfc_internal_error ("match_string_constant(): Delimiter not found");
879 if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
880 e->expr_type = EXPR_SUBSTRING;
887 gfc_current_locus = old_locus;
892 /* Match a .true. or .false. */
895 match_logical_constant (gfc_expr ** result)
897 static mstring logical_ops[] = {
898 minit (".false.", 0),
906 i = gfc_match_strings (logical_ops);
914 kind = gfc_default_logical_kind;
916 if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
917 gfc_error ("Bad kind for logical constant at %C");
921 e->expr_type = EXPR_CONSTANT;
922 e->value.logical = i;
923 e->ts.type = BT_LOGICAL;
925 e->where = gfc_current_locus;
932 /* Match a real or imaginary part of a complex constant that is a
933 symbolic constant. */
936 match_sym_complex_part (gfc_expr ** result)
938 char name[GFC_MAX_SYMBOL_LEN + 1];
943 m = gfc_match_name (name);
947 if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
950 if (sym->attr.flavor != FL_PARAMETER)
952 gfc_error ("Expected PARAMETER symbol in complex constant at %C");
956 if (!gfc_numeric_ts (&sym->value->ts))
958 gfc_error ("Numeric PARAMETER required in complex constant at %C");
962 if (sym->value->rank != 0)
964 gfc_error ("Scalar PARAMETER required in complex constant at %C");
968 switch (sym->value->ts.type)
971 e = gfc_copy_expr (sym->value);
975 e = gfc_complex2real (sym->value, sym->value->ts.kind);
981 e = gfc_int2real (sym->value, gfc_default_real_kind);
987 gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
990 *result = e; /* e is a scalar, real, constant expression */
994 gfc_error ("Error converting PARAMETER constant in complex constant at %C");
999 /* Match the real and imaginary parts of a complex number. This
1000 subroutine is essentially match_real_constant() modified in a
1001 couple of ways: A sign is always allowed and numbers that would
1002 look like an integer to match_real_constant() are automatically
1003 created as floating point numbers. The messiness involved with
1004 making sure a decimal point belongs to the number and not a
1005 trailing operator is not necessary here either (Hooray!). */
1008 match_const_complex_part (gfc_expr ** result)
1010 int kind, seen_digits, seen_dp, count;
1011 char *p, c, exp_char, *buffer;
1014 old_loc = gfc_current_locus;
1015 gfc_gobble_whitespace ();
1022 c = gfc_next_char ();
1023 if (c == '-' || c == '+')
1025 c = gfc_next_char ();
1029 for (;; c = gfc_next_char (), count++)
1048 if (!seen_digits || (c != 'd' && c != 'e'))
1052 /* Scan exponent. */
1053 c = gfc_next_char ();
1056 if (c == '+' || c == '-')
1057 { /* optional sign */
1058 c = gfc_next_char ();
1064 gfc_error ("Missing exponent in real number at %C");
1070 c = gfc_next_char ();
1078 /* Convert the number. */
1079 gfc_current_locus = old_loc;
1080 gfc_gobble_whitespace ();
1082 buffer = alloca (count + 1);
1083 memset (buffer, '\0', count + 1);
1085 /* Hack for mpfr_set_str(). */
1089 c = gfc_next_char ();
1090 if (c == 'd' || c == 'q')
1102 /* If the number looked like an integer, forget about a kind we may
1103 have seen, otherwise validate the kind against real kinds. */
1104 if (seen_dp == 0 && exp_char == ' ')
1107 kind = gfc_default_integer_kind;
1112 if (exp_char == 'd')
1117 ("Real number at %C has a 'd' exponent and an explicit kind");
1120 kind = gfc_default_double_kind;
1126 kind = gfc_default_real_kind;
1129 if (gfc_validate_kind (BT_REAL, kind, true) < 0)
1131 gfc_error ("Invalid real kind %d at %C", kind);
1136 *result = gfc_convert_real (buffer, kind, &gfc_current_locus);
1140 gfc_current_locus = old_loc;
1145 /* Match a real or imaginary part of a complex number. */
1148 match_complex_part (gfc_expr ** result)
1152 m = match_sym_complex_part (result);
1156 return match_const_complex_part (result);
1160 /* Try to match a complex constant. */
1163 match_complex_constant (gfc_expr ** result)
1165 gfc_expr *e, *real, *imag;
1166 gfc_error_buf old_error;
1167 gfc_typespec target;
1172 old_loc = gfc_current_locus;
1173 real = imag = e = NULL;
1175 m = gfc_match_char ('(');
1179 gfc_push_error (&old_error);
1181 m = match_complex_part (&real);
1185 if (gfc_match_char (',') == MATCH_NO)
1187 gfc_pop_error (&old_error);
1192 /* If m is error, then something was wrong with the real part and we
1193 assume we have a complex constant because we've seen the ','. An
1194 ambiguous case here is the start of an iterator list of some
1195 sort. These sort of lists are matched prior to coming here. */
1197 if (m == MATCH_ERROR)
1199 gfc_pop_error (&old_error);
1201 m = match_complex_part (&imag);
1204 if (m == MATCH_ERROR)
1207 m = gfc_match_char (')');
1211 if (m == MATCH_ERROR)
1214 /* Decide on the kind of this complex number. */
1215 kind = gfc_kind_max (real, imag);
1216 target.type = BT_REAL;
1219 if (kind != real->ts.kind)
1220 gfc_convert_type (real, &target, 2);
1221 if (kind != imag->ts.kind)
1222 gfc_convert_type (imag, &target, 2);
1224 e = gfc_convert_complex (real, imag, kind);
1225 e->where = gfc_current_locus;
1227 gfc_free_expr (real);
1228 gfc_free_expr (imag);
1234 gfc_error ("Syntax error in COMPLEX constant at %C");
1239 gfc_free_expr (real);
1240 gfc_free_expr (imag);
1241 gfc_current_locus = old_loc;
1247 /* Match constants in any of several forms. Returns nonzero for a
1248 match, zero for no match. */
1251 gfc_match_literal_constant (gfc_expr ** result, int signflag)
1255 m = match_complex_constant (result);
1259 m = match_string_constant (result);
1263 m = match_boz_constant (result);
1267 m = match_real_constant (result, signflag);
1271 m = match_integer_constant (result, signflag);
1275 m = match_logical_constant (result);
1283 /* Match a single actual argument value. An actual argument is
1284 usually an expression, but can also be a procedure name. If the
1285 argument is a single name, it is not always possible to tell
1286 whether the name is a dummy procedure or not. We treat these cases
1287 by creating an argument that looks like a dummy procedure and
1288 fixing things later during resolution. */
1291 match_actual_arg (gfc_expr ** result)
1293 char name[GFC_MAX_SYMBOL_LEN + 1];
1294 gfc_symtree *symtree;
1299 where = gfc_current_locus;
1301 switch (gfc_match_name (name))
1310 w = gfc_current_locus;
1311 gfc_gobble_whitespace ();
1312 c = gfc_next_char ();
1313 gfc_current_locus = w;
1315 if (c != ',' && c != ')')
1318 if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1320 /* Handle error elsewhere. */
1322 /* Eliminate a couple of common cases where we know we don't
1323 have a function argument. */
1324 if (symtree == NULL)
1326 gfc_get_sym_tree (name, NULL, &symtree);
1327 gfc_set_sym_referenced (symtree->n.sym);
1333 sym = symtree->n.sym;
1334 gfc_set_sym_referenced (sym);
1335 if (sym->attr.flavor != FL_PROCEDURE
1336 && sym->attr.flavor != FL_UNKNOWN)
1339 /* If the symbol is a function with itself as the result and
1340 is being defined, then we have a variable. */
1341 if (sym->result == sym
1342 && (gfc_current_ns->proc_name == sym
1343 || (gfc_current_ns->parent != NULL
1344 && gfc_current_ns->parent->proc_name == sym)))
1348 e = gfc_get_expr (); /* Leave it unknown for now */
1349 e->symtree = symtree;
1350 e->expr_type = EXPR_VARIABLE;
1351 e->ts.type = BT_PROCEDURE;
1358 gfc_current_locus = where;
1359 return gfc_match_expr (result);
1363 /* Match a keyword argument. */
1366 match_keyword_arg (gfc_actual_arglist * actual, gfc_actual_arglist * base)
1368 char name[GFC_MAX_SYMBOL_LEN + 1];
1369 gfc_actual_arglist *a;
1373 name_locus = gfc_current_locus;
1374 m = gfc_match_name (name);
1378 if (gfc_match_char ('=') != MATCH_YES)
1384 m = match_actual_arg (&actual->expr);
1388 /* Make sure this name has not appeared yet. */
1390 if (name[0] != '\0')
1392 for (a = base; a; a = a->next)
1393 if (strcmp (a->name, name) == 0)
1396 ("Keyword '%s' at %C has already appeared in the current "
1397 "argument list", name);
1402 strcpy (actual->name, name);
1406 gfc_current_locus = name_locus;
1411 /* Matches an actual argument list of a function or subroutine, from
1412 the opening parenthesis to the closing parenthesis. The argument
1413 list is assumed to allow keyword arguments because we don't know if
1414 the symbol associated with the procedure has an implicit interface
1415 or not. We make sure keywords are unique. If SUB_FLAG is set,
1416 we're matching the argument list of a subroutine. */
1419 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist ** argp)
1421 gfc_actual_arglist *head, *tail;
1423 gfc_st_label *label;
1427 *argp = tail = NULL;
1428 old_loc = gfc_current_locus;
1432 if (gfc_match_char ('(') == MATCH_NO)
1433 return (sub_flag) ? MATCH_YES : MATCH_NO;
1435 if (gfc_match_char (')') == MATCH_YES)
1442 head = tail = gfc_get_actual_arglist ();
1445 tail->next = gfc_get_actual_arglist ();
1449 if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1451 m = gfc_match_st_label (&label, 0);
1453 gfc_error ("Expected alternate return label at %C");
1457 tail->label = label;
1461 /* After the first keyword argument is seen, the following
1462 arguments must also have keywords. */
1465 m = match_keyword_arg (tail, head);
1467 if (m == MATCH_ERROR)
1472 ("Missing keyword name in actual argument list at %C");
1479 /* See if we have the first keyword argument. */
1480 m = match_keyword_arg (tail, head);
1483 if (m == MATCH_ERROR)
1488 /* Try for a non-keyword argument. */
1489 m = match_actual_arg (&tail->expr);
1490 if (m == MATCH_ERROR)
1498 if (gfc_match_char (')') == MATCH_YES)
1500 if (gfc_match_char (',') != MATCH_YES)
1508 gfc_error ("Syntax error in argument list at %C");
1511 gfc_free_actual_arglist (head);
1512 gfc_current_locus = old_loc;
1518 /* Used by match_varspec() to extend the reference list by one
1522 extend_ref (gfc_expr * primary, gfc_ref * tail)
1525 if (primary->ref == NULL)
1526 primary->ref = tail = gfc_get_ref ();
1530 gfc_internal_error ("extend_ref(): Bad tail");
1531 tail->next = gfc_get_ref ();
1539 /* Match any additional specifications associated with the current
1540 variable like member references or substrings. If equiv_flag is
1541 set we only match stuff that is allowed inside an EQUIVALENCE
1545 match_varspec (gfc_expr * primary, int equiv_flag)
1547 char name[GFC_MAX_SYMBOL_LEN + 1];
1548 gfc_ref *substring, *tail;
1549 gfc_component *component;
1555 if (primary->symtree->n.sym->attr.dimension
1557 && gfc_peek_char () == '('))
1560 tail = extend_ref (primary, tail);
1561 tail->type = REF_ARRAY;
1563 m = gfc_match_array_ref (&tail->u.ar, primary->symtree->n.sym->as,
1569 sym = primary->symtree->n.sym;
1570 primary->ts = sym->ts;
1572 if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1573 goto check_substring;
1575 sym = sym->ts.derived;
1579 m = gfc_match_name (name);
1581 gfc_error ("Expected structure component name at %C");
1585 component = gfc_find_component (sym, name);
1586 if (component == NULL)
1589 tail = extend_ref (primary, tail);
1590 tail->type = REF_COMPONENT;
1592 tail->u.c.component = component;
1593 tail->u.c.sym = sym;
1595 primary->ts = component->ts;
1597 if (component->as != NULL)
1599 tail = extend_ref (primary, tail);
1600 tail->type = REF_ARRAY;
1602 m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1607 if (component->ts.type != BT_DERIVED
1608 || gfc_match_char ('%') != MATCH_YES)
1611 sym = component->ts.derived;
1615 if (primary->ts.type == BT_CHARACTER)
1617 switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1621 primary->ref = substring;
1623 tail->next = substring;
1625 if (primary->expr_type == EXPR_CONSTANT)
1626 primary->expr_type = EXPR_SUBSTRING;
1642 /* Given an expression that is a variable, figure out what the
1643 ultimate variable's type and attribute is, traversing the reference
1644 structures if necessary.
1646 This subroutine is trickier than it looks. We start at the base
1647 symbol and store the attribute. Component references load a
1648 completely new attribute.
1650 A couple of rules come into play. Subobjects of targets are always
1651 targets themselves. If we see a component that goes through a
1652 pointer, then the expression must also be a target, since the
1653 pointer is associated with something (if it isn't core will soon be
1654 dumped). If we see a full part or section of an array, the
1655 expression is also an array.
1657 We can have at most one full array reference. */
1660 gfc_variable_attr (gfc_expr * expr, gfc_typespec * ts)
1662 int dimension, pointer, target;
1663 symbol_attribute attr;
1666 if (expr->expr_type != EXPR_VARIABLE)
1667 gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1670 attr = expr->symtree->n.sym->attr;
1672 dimension = attr.dimension;
1673 pointer = attr.pointer;
1675 target = attr.target;
1679 if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1680 *ts = expr->symtree->n.sym->ts;
1682 for (; ref; ref = ref->next)
1687 switch (ref->u.ar.type)
1703 gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1709 gfc_get_component_attr (&attr, ref->u.c.component);
1711 *ts = ref->u.c.component->ts;
1713 pointer = ref->u.c.component->pointer;
1724 attr.dimension = dimension;
1725 attr.pointer = pointer;
1726 attr.target = target;
1732 /* Return the attribute from a general expression. */
1735 gfc_expr_attr (gfc_expr * e)
1737 symbol_attribute attr;
1739 switch (e->expr_type)
1742 attr = gfc_variable_attr (e, NULL);
1746 gfc_clear_attr (&attr);
1748 if (e->value.function.esym != NULL)
1749 attr = e->value.function.esym->result->attr;
1751 /* TODO: NULL() returns pointers. May have to take care of this
1757 gfc_clear_attr (&attr);
1765 /* Match a structure constructor. The initial symbol has already been
1769 gfc_match_structure_constructor (gfc_symbol * sym, gfc_expr ** result)
1771 gfc_constructor *head, *tail;
1772 gfc_component *comp;
1779 if (gfc_match_char ('(') != MATCH_YES)
1782 where = gfc_current_locus;
1784 gfc_find_component (sym, NULL);
1786 for (comp = sym->components; comp; comp = comp->next)
1789 tail = head = gfc_get_constructor ();
1792 tail->next = gfc_get_constructor ();
1796 m = gfc_match_expr (&tail->expr);
1799 if (m == MATCH_ERROR)
1802 if (gfc_match_char (',') == MATCH_YES)
1804 if (comp->next == NULL)
1807 ("Too many components in structure constructor at %C");
1817 if (gfc_match_char (')') != MATCH_YES)
1820 if (comp->next != NULL)
1822 gfc_error ("Too few components in structure constructor at %C");
1826 e = gfc_get_expr ();
1828 e->expr_type = EXPR_STRUCTURE;
1830 e->ts.type = BT_DERIVED;
1831 e->ts.derived = sym;
1834 e->value.constructor = head;
1840 gfc_error ("Syntax error in structure constructor at %C");
1843 gfc_free_constructor (head);
1848 /* Matches a variable name followed by anything that might follow it--
1849 array reference, argument list of a function, etc. */
1852 gfc_match_rvalue (gfc_expr ** result)
1854 gfc_actual_arglist *actual_arglist;
1855 char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
1858 gfc_symtree *symtree;
1859 locus where, old_loc;
1864 m = gfc_match_name (name);
1868 if (gfc_find_state (COMP_INTERFACE) == SUCCESS)
1869 i = gfc_get_sym_tree (name, NULL, &symtree);
1871 i = gfc_get_ha_sym_tree (name, &symtree);
1876 sym = symtree->n.sym;
1878 where = gfc_current_locus;
1880 gfc_set_sym_referenced (sym);
1882 if (sym->attr.function && sym->result == sym
1883 && (gfc_current_ns->proc_name == sym
1884 || (gfc_current_ns->parent != NULL
1885 && gfc_current_ns->parent->proc_name == sym)))
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);
1912 && sym->value->expr_type != EXPR_ARRAY)
1913 e = gfc_copy_expr (sym->value);
1916 e = gfc_get_expr ();
1917 e->expr_type = EXPR_VARIABLE;
1920 e->symtree = symtree;
1921 m = match_varspec (e, 0);
1925 sym = gfc_use_derived (sym);
1929 m = gfc_match_structure_constructor (sym, &e);
1932 /* If we're here, then the name is known to be the name of a
1933 procedure, yet it is not sure to be the name of a function. */
1935 if (sym->attr.subroutine)
1937 gfc_error ("Unexpected use of subroutine name '%s' at %C",
1943 /* At this point, the name has to be a non-statement function.
1944 If the name is the same as the current function being
1945 compiled, then we have a variable reference (to the function
1946 result) if the name is non-recursive. */
1948 st = gfc_enclosing_unit (NULL);
1950 if (st != NULL && st->state == COMP_FUNCTION
1952 && !sym->attr.recursive)
1954 e = gfc_get_expr ();
1955 e->symtree = symtree;
1956 e->expr_type = EXPR_VARIABLE;
1958 m = match_varspec (e, 0);
1962 /* Match a function reference. */
1964 m = gfc_match_actual_arglist (0, &actual_arglist);
1967 if (sym->attr.proc == PROC_ST_FUNCTION)
1968 gfc_error ("Statement function '%s' requires argument list at %C",
1971 gfc_error ("Function '%s' requires an argument list at %C",
1984 gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
1985 sym = symtree->n.sym;
1987 e = gfc_get_expr ();
1988 e->symtree = symtree;
1989 e->expr_type = EXPR_FUNCTION;
1990 e->value.function.actual = actual_arglist;
1991 e->where = gfc_current_locus;
1993 if (sym->as != NULL)
1994 e->rank = sym->as->rank;
1996 if (!sym->attr.function
1997 && gfc_add_function (&sym->attr, NULL) == FAILURE)
2003 if (sym->result == NULL)
2011 /* Special case for derived type variables that get their types
2012 via an IMPLICIT statement. This can't wait for the
2013 resolution phase. */
2015 if (gfc_peek_char () == '%'
2016 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2017 gfc_set_default_type (sym, 0, sym->ns);
2019 /* If the symbol has a dimension attribute, the expression is a
2022 if (sym->attr.dimension)
2024 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2030 e = gfc_get_expr ();
2031 e->symtree = symtree;
2032 e->expr_type = EXPR_VARIABLE;
2033 m = match_varspec (e, 0);
2037 /* Name is not an array, so we peek to see if a '(' implies a
2038 function call or a substring reference. Otherwise the
2039 variable is just a scalar. */
2041 gfc_gobble_whitespace ();
2042 if (gfc_peek_char () != '(')
2044 /* Assume a scalar variable */
2045 e = gfc_get_expr ();
2046 e->symtree = symtree;
2047 e->expr_type = EXPR_VARIABLE;
2049 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2056 m = match_varspec (e, 0);
2060 /* See if this is a function reference with a keyword argument
2061 as first argument. We do this because otherwise a spurious
2062 symbol would end up in the symbol table. */
2064 old_loc = gfc_current_locus;
2065 m2 = gfc_match (" ( %n =", argname);
2066 gfc_current_locus = old_loc;
2068 e = gfc_get_expr ();
2069 e->symtree = symtree;
2071 if (m2 != MATCH_YES)
2073 /* See if this could possibly be a substring reference of a name
2074 that we're not sure is a variable yet. */
2076 if ((sym->ts.type == BT_UNKNOWN || sym->ts.type == BT_CHARACTER)
2077 && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2080 e->expr_type = EXPR_VARIABLE;
2082 if (sym->attr.flavor != FL_VARIABLE
2083 && gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2089 if (sym->ts.type == BT_UNKNOWN
2090 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2102 /* Give up, assume we have a function. */
2104 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2105 sym = symtree->n.sym;
2106 e->expr_type = EXPR_FUNCTION;
2108 if (!sym->attr.function
2109 && gfc_add_function (&sym->attr, NULL) == FAILURE)
2117 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2119 gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2127 /* If our new function returns a character, array or structure
2128 type, it might have subsequent references. */
2130 m = match_varspec (e, 0);
2137 gfc_get_sym_tree (name, NULL, &symtree); /* Can't fail */
2139 e = gfc_get_expr ();
2140 e->symtree = symtree;
2141 e->expr_type = EXPR_FUNCTION;
2143 m = gfc_match_actual_arglist (0, &e->value.function.actual);
2147 gfc_error ("Symbol at %C is not appropriate for an expression");
2163 /* Match a variable, ie something that can be assigned to. This
2164 starts as a symbol, can be a structure component or an array
2165 reference. It can be a function if the function doesn't have a
2166 separate RESULT variable. If the symbol has not been previously
2167 seen, we assume it is a variable. */
2170 gfc_match_variable (gfc_expr ** result, int equiv_flag)
2178 m = gfc_match_sym_tree (&st, 1);
2181 where = gfc_current_locus;
2184 gfc_set_sym_referenced (sym);
2185 switch (sym->attr.flavor)
2191 if (gfc_add_flavor (&sym->attr, FL_VARIABLE, NULL) == FAILURE)
2194 /* Special case for derived type variables that get their types
2195 via an IMPLICIT statement. This can't wait for the
2196 resolution phase. */
2198 if (gfc_peek_char () == '%'
2199 && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2200 gfc_set_default_type (sym, 0, sym->ns);
2205 /* Check for a nonrecursive function result */
2206 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)
2219 /* Fall through to error */
2222 gfc_error ("Expected VARIABLE at %C");
2226 expr = gfc_get_expr ();
2228 expr->expr_type = EXPR_VARIABLE;
2231 expr->where = where;
2233 /* Now see if we have to do more. */
2234 m = match_varspec (expr, equiv_flag);
2237 gfc_free_expr (expr);