1 /* Routines for manipulation of expression nodes.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GNU G95.
7 GNU G95 is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU G95 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU G95; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
31 /* Get a new expr node. */
38 e = gfc_getmem (sizeof (gfc_expr));
40 gfc_clear_ts (&e->ts);
52 /* Free an argument list and everything below it. */
55 gfc_free_actual_arglist (gfc_actual_arglist * a1)
57 gfc_actual_arglist *a2;
62 gfc_free_expr (a1->expr);
69 /* Copy an arglist structure and all of the arguments. */
72 gfc_copy_actual_arglist (gfc_actual_arglist * p)
74 gfc_actual_arglist *head, *tail, *new;
78 for (; p; p = p->next)
80 new = gfc_get_actual_arglist ();
83 new->expr = gfc_copy_expr (p->expr);
98 /* Free a list of reference structures. */
101 gfc_free_ref_list (gfc_ref * p)
113 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
115 gfc_free_expr (p->u.ar.start[i]);
116 gfc_free_expr (p->u.ar.end[i]);
117 gfc_free_expr (p->u.ar.stride[i]);
123 gfc_free_expr (p->u.ss.start);
124 gfc_free_expr (p->u.ss.end);
136 /* Workhorse function for gfc_free_expr() that frees everything
137 beneath an expression node, but not the node itself. This is
138 useful when we want to simplify a node and replace it with
139 something else or the expression node belongs to another structure. */
142 free_expr0 (gfc_expr * e)
146 switch (e->expr_type)
152 mpz_clear (e->value.integer);
156 mpf_clear (e->value.real);
160 gfc_free (e->value.character.string);
164 mpf_clear (e->value.complex.r);
165 mpf_clear (e->value.complex.i);
176 gfc_free_expr (e->op1);
178 gfc_free_expr (e->op2);
182 gfc_free_actual_arglist (e->value.function.actual);
190 gfc_free_constructor (e->value.constructor);
194 gfc_free (e->value.character.string);
201 gfc_internal_error ("free_expr0(): Bad expr type");
204 /* Free a shape array. */
205 if (e->shape != NULL)
207 for (n = 0; n < e->rank; n++)
208 mpz_clear (e->shape[n]);
213 gfc_free_ref_list (e->ref);
215 memset (e, '\0', sizeof (gfc_expr));
219 /* Free an expression node and everything beneath it. */
222 gfc_free_expr (gfc_expr * e)
233 /* Graft the *src expression onto the *dest subexpression. */
236 gfc_replace_expr (gfc_expr * dest, gfc_expr * src)
246 /* Try to extract an integer constant from the passed expression node.
247 Returns an error message or NULL if the result is set. It is
248 tempting to generate an error and return SUCCESS or FAILURE, but
249 failure is OK for some callers. */
252 gfc_extract_int (gfc_expr * expr, int *result)
255 if (expr->expr_type != EXPR_CONSTANT)
256 return "Constant expression required at %C";
258 if (expr->ts.type != BT_INTEGER)
259 return "Integer expression required at %C";
261 if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
262 || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
264 return "Integer value too large in expression at %C";
267 *result = (int) mpz_get_si (expr->value.integer);
273 /* Recursively copy a list of reference structures. */
276 copy_ref (gfc_ref * src)
284 dest = gfc_get_ref ();
285 dest->type = src->type;
290 ar = gfc_copy_array_ref (&src->u.ar);
296 dest->u.c = src->u.c;
300 dest->u.ss = src->u.ss;
301 dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
302 dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
306 dest->next = copy_ref (src->next);
312 /* Copy a shape array. */
315 gfc_copy_shape (mpz_t * shape, int rank)
323 new_shape = gfc_get_shape (rank);
325 for (n = 0; n < rank; n++)
326 mpz_init_set (new_shape[n], shape[n]);
332 /* Given an expression pointer, return a copy of the expression. This
333 subroutine is recursive. */
336 gfc_copy_expr (gfc_expr * p)
347 switch (q->expr_type)
350 s = gfc_getmem (p->value.character.length + 1);
351 q->value.character.string = s;
353 memcpy (s, p->value.character.string, p->value.character.length + 1);
355 q->op1 = gfc_copy_expr (p->op1);
356 q->op2 = gfc_copy_expr (p->op2);
363 mpz_init_set (q->value.integer, p->value.integer);
367 mpf_init_set (q->value.real, p->value.real);
371 mpf_init_set (q->value.complex.r, p->value.complex.r);
372 mpf_init_set (q->value.complex.i, p->value.complex.i);
376 s = gfc_getmem (p->value.character.length + 1);
377 q->value.character.string = s;
379 memcpy (s, p->value.character.string,
380 p->value.character.length + 1);
385 break; /* Already done */
389 gfc_internal_error ("gfc_copy_expr(): Bad expr node");
399 case INTRINSIC_UPLUS:
400 case INTRINSIC_UMINUS:
401 q->op1 = gfc_copy_expr (p->op1);
404 default: /* Binary operators */
405 q->op1 = gfc_copy_expr (p->op1);
406 q->op2 = gfc_copy_expr (p->op2);
413 q->value.function.actual =
414 gfc_copy_actual_arglist (p->value.function.actual);
419 q->value.constructor = gfc_copy_constructor (p->value.constructor);
427 q->shape = gfc_copy_shape (p->shape, p->rank);
429 q->ref = copy_ref (p->ref);
435 /* Return the maximum kind of two expressions. In general, higher
436 kind numbers mean more precision for numeric types. */
439 gfc_kind_max (gfc_expr * e1, gfc_expr * e2)
442 return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
446 /* Returns nonzero if the type is numeric, zero otherwise. */
449 numeric_type (bt type)
452 return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
456 /* Returns nonzero if the typespec is a numeric type, zero otherwise. */
459 gfc_numeric_ts (gfc_typespec * ts)
462 return numeric_type (ts->type);
466 /* Returns an expression node that is an integer constant. */
475 p->expr_type = EXPR_CONSTANT;
476 p->ts.type = BT_INTEGER;
477 p->ts.kind = gfc_default_integer_kind ();
479 p->where = *gfc_current_locus ();
480 mpz_init_set_si (p->value.integer, i);
486 /* Returns an expression node that is a logical constant. */
489 gfc_logical_expr (int i, locus * where)
495 p->expr_type = EXPR_CONSTANT;
496 p->ts.type = BT_LOGICAL;
497 p->ts.kind = gfc_default_logical_kind ();
500 where = gfc_current_locus ();
502 p->value.logical = i;
508 /* Return an expression node with an optional argument list attached.
509 A variable number of gfc_expr pointers are strung together in an
510 argument list with a NULL pointer terminating the list. */
513 gfc_build_conversion (gfc_expr * e)
518 p->expr_type = EXPR_FUNCTION;
520 p->value.function.actual = NULL;
522 p->value.function.actual = gfc_get_actual_arglist ();
523 p->value.function.actual->expr = e;
529 /* Given an expression node with some sort of numeric binary
530 expression, insert type conversions required to make the operands
533 The exception is that the operands of an exponential don't have to
534 have the same type. If possible, the base is promoted to the type
535 of the exponent. For example, 1**2.3 becomes 1.0**2.3, but
536 1.0**2 stays as it is. */
539 gfc_type_convert_binary (gfc_expr * e)
546 if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
548 gfc_clear_ts (&e->ts);
552 /* Kind conversions of same type. */
553 if (op1->ts.type == op2->ts.type)
556 if (op1->ts.kind == op2->ts.kind)
558 /* No type conversions. */
563 if (op1->ts.kind > op2->ts.kind)
564 gfc_convert_type (op2, &op1->ts, 2);
566 gfc_convert_type (op1, &op2->ts, 2);
572 /* Integer combined with real or complex. */
573 if (op2->ts.type == BT_INTEGER)
577 /* Special cose for ** operator. */
578 if (e->operator == INTRINSIC_POWER)
581 gfc_convert_type (e->op2, &e->ts, 2);
585 if (op1->ts.type == BT_INTEGER)
588 gfc_convert_type (e->op1, &e->ts, 2);
592 /* Real combined with complex. */
593 e->ts.type = BT_COMPLEX;
594 if (op1->ts.kind > op2->ts.kind)
595 e->ts.kind = op1->ts.kind;
597 e->ts.kind = op2->ts.kind;
598 if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
599 gfc_convert_type (e->op1, &e->ts, 2);
600 if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
601 gfc_convert_type (e->op2, &e->ts, 2);
608 /* Function to determine if an expression is constant or not. This
609 function expects that the expression has already been simplified. */
612 gfc_is_constant_expr (gfc_expr * e)
615 gfc_actual_arglist *arg;
621 switch (e->expr_type)
624 rv = (gfc_is_constant_expr (e->op1)
626 || gfc_is_constant_expr (e->op2)));
635 /* Call to intrinsic with at least one argument. */
637 if (e->value.function.isym && e->value.function.actual)
639 for (arg = e->value.function.actual; arg; arg = arg->next)
641 if (!gfc_is_constant_expr (arg->expr))
655 rv = gfc_is_constant_expr (e->op1) && gfc_is_constant_expr (e->op2);
660 for (c = e->value.constructor; c; c = c->next)
661 if (!gfc_is_constant_expr (c->expr))
669 rv = gfc_constant_ac (e);
673 gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
680 /* Try to collapse intrinsic expressions. */
683 simplify_intrinsic_op (gfc_expr * p, int type)
685 gfc_expr *op1, *op2, *result;
687 if (p->operator == INTRINSIC_USER)
693 if (gfc_simplify_expr (op1, type) == FAILURE)
695 if (gfc_simplify_expr (op2, type) == FAILURE)
698 if (!gfc_is_constant_expr (op1)
699 || (op2 != NULL && !gfc_is_constant_expr (op2)))
708 case INTRINSIC_UPLUS:
709 result = gfc_uplus (op1);
712 case INTRINSIC_UMINUS:
713 result = gfc_uminus (op1);
717 result = gfc_add (op1, op2);
720 case INTRINSIC_MINUS:
721 result = gfc_subtract (op1, op2);
724 case INTRINSIC_TIMES:
725 result = gfc_multiply (op1, op2);
728 case INTRINSIC_DIVIDE:
729 result = gfc_divide (op1, op2);
732 case INTRINSIC_POWER:
733 result = gfc_power (op1, op2);
736 case INTRINSIC_CONCAT:
737 result = gfc_concat (op1, op2);
741 result = gfc_eq (op1, op2);
745 result = gfc_ne (op1, op2);
749 result = gfc_gt (op1, op2);
753 result = gfc_ge (op1, op2);
757 result = gfc_lt (op1, op2);
761 result = gfc_le (op1, op2);
765 result = gfc_not (op1);
769 result = gfc_and (op1, op2);
773 result = gfc_or (op1, op2);
777 result = gfc_eqv (op1, op2);
781 result = gfc_neqv (op1, op2);
785 gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
795 gfc_replace_expr (p, result);
801 /* Subroutine to simplify constructor expressions. Mutually recursive
802 with gfc_simplify_expr(). */
805 simplify_constructor (gfc_constructor * c, int type)
808 for (; c; c = c->next)
811 && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
812 || gfc_simplify_expr (c->iterator->end, type) == FAILURE
813 || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
816 if (c->expr && gfc_simplify_expr (c->expr, type) == FAILURE)
824 /* Pull a single array element out of an array constructor. */
826 static gfc_constructor *
827 find_array_element (gfc_constructor * cons, gfc_array_ref * ar)
829 unsigned long nelemen;
834 mpz_init_set_ui (offset, 0);
836 for (i = 0; i < ar->dimen; i++)
838 if (ar->start[i]->expr_type != EXPR_CONSTANT)
843 mpz_sub (delta, ar->start[i]->value.integer,
844 ar->as->lower[i]->value.integer);
845 mpz_add (offset, offset, delta);
850 if (mpz_fits_ulong_p (offset))
852 for (nelemen = mpz_get_ui (offset); nelemen > 0; nelemen--)
873 /* Find a component of a structure constructor. */
875 static gfc_constructor *
876 find_component_ref (gfc_constructor * cons, gfc_ref * ref)
881 comp = ref->u.c.sym->components;
882 pick = ref->u.c.component;
893 /* Replace an expression with the contents of a constructor, removing
894 the subobject reference in the process. */
897 remove_subobject_ref (gfc_expr * p, gfc_constructor * cons)
903 e->ref = p->ref->next;
905 gfc_replace_expr (p, e);
909 /* Simplify a subobject reference of a constructor. This occurs when
910 parameter variable values are substituted. */
913 simplify_const_ref (gfc_expr * p)
915 gfc_constructor *cons;
919 switch (p->ref->type)
922 switch (p->ref->u.ar.type)
925 cons = find_array_element (p->value.constructor, &p->ref->u.ar);
928 remove_subobject_ref (p, cons);
932 if (p->ref->next != NULL)
934 /* TODO: Simplify array subobject references. */
937 gfc_free_ref_list (p->ref);
942 /* TODO: Simplify array subsections. */
949 cons = find_component_ref (p->value.constructor, p->ref);
950 remove_subobject_ref (p, cons);
954 /* TODO: Constant substrings. */
963 /* Simplify a chain of references. */
966 simplify_ref_chain (gfc_ref * ref, int type)
970 for (; ref; ref = ref->next)
975 for (n = 0; n < ref->u.ar.dimen; n++)
977 if (gfc_simplify_expr (ref->u.ar.start[n], type)
980 if (gfc_simplify_expr (ref->u.ar.end[n], type)
983 if (gfc_simplify_expr (ref->u.ar.stride[n], type)
990 if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
992 if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
1004 /* Try to substitute the value of a parameter variable. */
1006 simplify_parameter_variable (gfc_expr * p, int type)
1011 e = gfc_copy_expr (p->symtree->n.sym->value);
1013 e->ref = copy_ref (p->ref);
1014 t = gfc_simplify_expr (e, type);
1016 /* Only use the simplification if it eliminated all subobject
1018 if (t == SUCCESS && ! e->ref)
1019 gfc_replace_expr (p, e);
1026 /* Given an expression, simplify it by collapsing constant
1027 expressions. Most simplification takes place when the expression
1028 tree is being constructed. If an intrinsic function is simplified
1029 at some point, we get called again to collapse the result against
1032 We work by recursively simplifying expression nodes, simplifying
1033 intrinsic functions where possible, which can lead to further
1034 constant collapsing. If an operator has constant operand(s), we
1035 rip the expression apart, and rebuild it, hoping that it becomes
1038 The expression type is defined for:
1039 0 Basic expression parsing
1040 1 Simplifying array constructors -- will substitute
1042 Returns FAILURE on error, SUCCESS otherwise.
1043 NOTE: Will return SUCCESS even if the expression can not be simplified. */
1046 gfc_simplify_expr (gfc_expr * p, int type)
1048 gfc_actual_arglist *ap;
1053 switch (p->expr_type)
1060 for (ap = p->value.function.actual; ap; ap = ap->next)
1061 if (gfc_simplify_expr (ap->expr, type) == FAILURE)
1064 if (p->value.function.isym != NULL
1065 && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
1070 case EXPR_SUBSTRING:
1071 if (gfc_simplify_expr (p->op1, type) == FAILURE
1072 || gfc_simplify_expr (p->op2, type) == FAILURE)
1075 /* TODO: evaluate constant substrings. */
1080 if (simplify_intrinsic_op (p, type) == FAILURE)
1085 /* Only substitute array parameter variables if we are in an
1086 initialization expression, or we want a subsection. */
1087 if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1088 && (gfc_init_expr || p->ref
1089 || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1091 if (simplify_parameter_variable (p, type) == FAILURE)
1098 gfc_simplify_iterator_var (p);
1101 /* Simplify subcomponent references. */
1102 if (simplify_ref_chain (p->ref, type) == FAILURE)
1107 case EXPR_STRUCTURE:
1109 if (simplify_ref_chain (p->ref, type) == FAILURE)
1112 if (simplify_constructor (p->value.constructor, type) == FAILURE)
1115 if (p->expr_type == EXPR_ARRAY)
1116 gfc_expand_constructor (p);
1118 if (simplify_const_ref (p) == FAILURE)
1128 /* Returns the type of an expression with the exception that iterator
1129 variables are automatically integers no matter what else they may
1136 if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
1143 /* Check an intrinsic arithmetic operation to see if it is consistent
1144 with some type of expression. */
1146 static try check_init_expr (gfc_expr *);
1149 check_intrinsic_op (gfc_expr * e, try (*check_function) (gfc_expr *))
1152 if ((*check_function) (e->op1) == FAILURE)
1155 switch (e->operator)
1157 case INTRINSIC_UPLUS:
1158 case INTRINSIC_UMINUS:
1159 if (!numeric_type (et0 (e->op1)))
1170 case INTRINSIC_PLUS:
1171 case INTRINSIC_MINUS:
1172 case INTRINSIC_TIMES:
1173 case INTRINSIC_DIVIDE:
1174 case INTRINSIC_POWER:
1175 if ((*check_function) (e->op2) == FAILURE)
1178 if (!numeric_type (et0 (e->op1)) || !numeric_type (et0 (e->op2)))
1181 if (e->operator != INTRINSIC_POWER)
1184 if (check_function == check_init_expr && et0 (e->op2) != BT_INTEGER)
1186 gfc_error ("Exponent at %L must be INTEGER for an initialization "
1187 "expression", &e->op2->where);
1193 case INTRINSIC_CONCAT:
1194 if ((*check_function) (e->op2) == FAILURE)
1197 if (et0 (e->op1) != BT_CHARACTER || et0 (e->op2) != BT_CHARACTER)
1199 gfc_error ("Concatenation operator in expression at %L "
1200 "must have two CHARACTER operands", &e->op1->where);
1204 if (e->op1->ts.kind != e->op2->ts.kind)
1206 gfc_error ("Concat operator at %L must concatenate strings of the "
1207 "same kind", &e->where);
1214 if (et0 (e->op1) != BT_LOGICAL)
1216 gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
1217 "operand", &e->op1->where);
1226 case INTRINSIC_NEQV:
1227 if ((*check_function) (e->op2) == FAILURE)
1230 if (et0 (e->op1) != BT_LOGICAL || et0 (e->op2) != BT_LOGICAL)
1232 gfc_error ("LOGICAL operands are required in expression at %L",
1240 gfc_error ("Only intrinsic operators can be used in expression at %L",
1248 gfc_error ("Numeric operands are required in expression at %L", &e->where);
1255 /* Certain inquiry functions are specifically allowed to have variable
1256 arguments, which is an exception to the normal requirement that an
1257 initialization function have initialization arguments. We head off
1258 this problem here. */
1261 check_inquiry (gfc_expr * e)
1265 /* FIXME: This should be moved into the intrinsic definitions,
1266 to eliminate this ugly hack. */
1267 static const char * const inquiry_function[] = {
1268 "digits", "epsilon", "huge", "kind", "maxexponent", "minexponent",
1269 "precision", "radix", "range", "tiny", "bit_size", "size", "shape",
1270 "lbound", "ubound", NULL
1275 /* These functions must have exactly one argument. */
1276 if (e->value.function.actual == NULL
1277 || e->value.function.actual->next != NULL)
1280 if (e->value.function.name != NULL
1281 && e->value.function.name[0] != '\0')
1284 name = e->symtree->n.sym->name;
1286 for (i = 0; inquiry_function[i]; i++)
1287 if (strcmp (inquiry_function[i], name) == 0)
1290 if (inquiry_function[i] == NULL)
1293 e = e->value.function.actual->expr;
1295 if (e == NULL || e->expr_type != EXPR_VARIABLE)
1298 /* At this point we have a numeric inquiry function with a variable
1299 argument. The type of the variable might be undefined, but we
1300 need it now, because the arguments of these functions are allowed
1303 if (e->ts.type == BT_UNKNOWN)
1305 if (e->symtree->n.sym->ts.type == BT_UNKNOWN
1306 && gfc_set_default_type (e->symtree->n.sym, 0, gfc_current_ns)
1310 e->ts = e->symtree->n.sym->ts;
1317 /* Verify that an expression is an initialization expression. A side
1318 effect is that the expression tree is reduced to a single constant
1319 node if all goes well. This would normally happen when the
1320 expression is constructed but function references are assumed to be
1321 intrinsics in the context of initialization expressions. If
1322 FAILURE is returned an error message has been generated. */
1325 check_init_expr (gfc_expr * e)
1327 gfc_actual_arglist *ap;
1334 switch (e->expr_type)
1337 t = check_intrinsic_op (e, check_init_expr);
1339 t = gfc_simplify_expr (e, 0);
1346 if (check_inquiry (e) != SUCCESS)
1349 for (ap = e->value.function.actual; ap; ap = ap->next)
1350 if (check_init_expr (ap->expr) == FAILURE)
1359 m = gfc_intrinsic_func_interface (e, 0);
1362 gfc_error ("Function '%s' in initialization expression at %L "
1363 "must be an intrinsic function",
1364 e->symtree->n.sym->name, &e->where);
1375 if (gfc_check_iter_variable (e) == SUCCESS)
1378 if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
1380 t = simplify_parameter_variable (e, 0);
1384 gfc_error ("Variable '%s' at %L cannot appear in an initialization "
1385 "expression", e->symtree->n.sym->name, &e->where);
1394 case EXPR_SUBSTRING:
1395 t = check_init_expr (e->op1);
1399 t = check_init_expr (e->op2);
1401 t = gfc_simplify_expr (e, 0);
1405 case EXPR_STRUCTURE:
1406 t = gfc_check_constructor (e, check_init_expr);
1410 t = gfc_check_constructor (e, check_init_expr);
1414 t = gfc_expand_constructor (e);
1418 t = gfc_check_constructor_type (e);
1422 gfc_internal_error ("check_init_expr(): Unknown expression type");
1429 /* Match an initialization expression. We work by first matching an
1430 expression, then reducing it to a constant. */
1433 gfc_match_init_expr (gfc_expr ** result)
1439 m = gfc_match_expr (&expr);
1444 t = gfc_resolve_expr (expr);
1446 t = check_init_expr (expr);
1451 gfc_free_expr (expr);
1455 if (expr->expr_type == EXPR_ARRAY
1456 && (gfc_check_constructor_type (expr) == FAILURE
1457 || gfc_expand_constructor (expr) == FAILURE))
1459 gfc_free_expr (expr);
1463 if (!gfc_is_constant_expr (expr))
1464 gfc_internal_error ("Initialization expression didn't reduce %C");
1473 static try check_restricted (gfc_expr *);
1475 /* Given an actual argument list, test to see that each argument is a
1476 restricted expression and optionally if the expression type is
1477 integer or character. */
1480 restricted_args (gfc_actual_arglist * a, int check_type)
1484 for (; a; a = a->next)
1486 if (check_restricted (a->expr) == FAILURE)
1492 type = a->expr->ts.type;
1493 if (type != BT_CHARACTER && type != BT_INTEGER)
1496 ("Function argument at %L must be of type INTEGER or CHARACTER",
1506 /************* Restricted/specification expressions *************/
1509 /* Make sure a non-intrinsic function is a specification function. */
1512 external_spec_function (gfc_expr * e)
1516 f = e->value.function.esym;
1518 if (f->attr.proc == PROC_ST_FUNCTION)
1520 gfc_error ("Specification function '%s' at %L cannot be a statement "
1521 "function", f->name, &e->where);
1525 if (f->attr.proc == PROC_INTERNAL)
1527 gfc_error ("Specification function '%s' at %L cannot be an internal "
1528 "function", f->name, &e->where);
1534 gfc_error ("Specification function '%s' at %L must be PURE", f->name,
1539 if (f->attr.recursive)
1541 gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
1542 f->name, &e->where);
1546 return restricted_args (e->value.function.actual, 0);
1550 /* Check to see that a function reference to an intrinsic is a
1551 restricted expression. Some functions required by the standard are
1552 omitted because references to them have already been simplified.
1553 Strictly speaking, a lot of these checks are redundant with other
1554 checks. If a function is indeed a particular intrinsic, then the
1555 type of its argument have already been checked and passed. */
1558 restricted_intrinsic (gfc_expr * e)
1560 gfc_intrinsic_sym *sym;
1567 const *cp, cases[] =
1571 {"selected_int_kind", 0},
1572 {"selected_real_kind", 0},
1580 /* bit_size() has already been reduced */
1582 /* kind() has already been reduced */
1583 /* Numeric inquiry functions have been reduced */
1589 sym = e->value.function.isym;
1594 return restricted_args (e->value.function.actual, 1);
1596 for (cp = cases; cp->name; cp++)
1597 if (strcmp (cp->name, sym->name) == 0)
1600 if (cp->name == NULL)
1602 gfc_error ("Intrinsic function '%s' at %L is not a restricted function",
1603 sym->name, &e->where);
1607 switch (cp->case_number)
1610 /* Functions that are restricted if they have character/integer args. */
1611 t = restricted_args (e->value.function.actual, 1);
1614 case 1: /* NULL() */
1619 /* Functions that could be checking the bounds of an assumed-size array. */
1621 /* TODO: implement checks from 7.1.6.2 (10) */
1625 gfc_internal_error ("restricted_intrinsic(): Bad case");
1632 /* Verify that an expression is a restricted expression. Like its
1633 cousin check_init_expr(), an error message is generated if we
1637 check_restricted (gfc_expr * e)
1645 switch (e->expr_type)
1648 t = check_intrinsic_op (e, check_restricted);
1650 t = gfc_simplify_expr (e, 0);
1655 t = e->value.function.esym ?
1656 external_spec_function (e) : restricted_intrinsic (e);
1661 sym = e->symtree->n.sym;
1664 if (sym->attr.optional)
1666 gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
1667 sym->name, &e->where);
1671 if (sym->attr.intent == INTENT_OUT)
1673 gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
1674 sym->name, &e->where);
1678 if (sym->attr.in_common
1679 || sym->attr.use_assoc
1681 || sym->ns != gfc_current_ns
1682 || (sym->ns->proc_name != NULL
1683 && sym->ns->proc_name->attr.flavor == FL_MODULE))
1689 gfc_error ("Variable '%s' cannot appear in the expression at %L",
1690 sym->name, &e->where);
1699 case EXPR_SUBSTRING:
1700 t = gfc_specification_expr (e->op1);
1704 t = gfc_specification_expr (e->op2);
1706 t = gfc_simplify_expr (e, 0);
1710 case EXPR_STRUCTURE:
1711 t = gfc_check_constructor (e, check_restricted);
1715 t = gfc_check_constructor (e, check_restricted);
1719 gfc_internal_error ("check_restricted(): Unknown expression type");
1726 /* Check to see that an expression is a specification expression. If
1727 we return FAILURE, an error has been generated. */
1730 gfc_specification_expr (gfc_expr * e)
1733 if (e->ts.type != BT_INTEGER)
1735 gfc_error ("Expression at %L must be of INTEGER type", &e->where);
1741 gfc_error ("Expression at %L must be scalar", &e->where);
1745 if (gfc_simplify_expr (e, 0) == FAILURE)
1748 return check_restricted (e);
1752 /************** Expression conformance checks. *************/
1754 /* Given two expressions, make sure that the arrays are conformable. */
1757 gfc_check_conformance (const char *optype, gfc_expr * op1, gfc_expr * op2)
1759 int op1_flag, op2_flag, d;
1760 mpz_t op1_size, op2_size;
1763 if (op1->rank == 0 || op2->rank == 0)
1766 if (op1->rank != op2->rank)
1768 gfc_error ("Incompatible ranks in %s at %L", optype, &op1->where);
1774 for (d = 0; d < op1->rank; d++)
1776 op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
1777 op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;
1779 if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
1781 gfc_error ("%s at %L has different shape on dimension %d (%d/%d)",
1782 optype, &op1->where, d + 1, (int) mpz_get_si (op1_size),
1783 (int) mpz_get_si (op2_size));
1789 mpz_clear (op1_size);
1791 mpz_clear (op2_size);
1801 /* Given an assignable expression and an arbitrary expression, make
1802 sure that the assignment can take place. */
1805 gfc_check_assign (gfc_expr * lvalue, gfc_expr * rvalue, int conform)
1809 sym = lvalue->symtree->n.sym;
1811 if (sym->attr.intent == INTENT_IN)
1813 gfc_error ("Can't assign to INTENT(IN) variable '%s' at %L",
1814 sym->name, &lvalue->where);
1818 if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
1820 gfc_error ("Incompatible ranks in assignment at %L", &lvalue->where);
1824 if (lvalue->ts.type == BT_UNKNOWN)
1826 gfc_error ("Variable type is UNKNOWN in assignment at %L",
1831 /* Check size of array assignments. */
1832 if (lvalue->rank != 0 && rvalue->rank != 0
1833 && gfc_check_conformance ("Array assignment", lvalue, rvalue) != SUCCESS)
1836 if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
1841 if (gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
1844 gfc_error ("Incompatible types in assignment at %L, %s to %s",
1845 &rvalue->where, gfc_typename (&rvalue->ts),
1846 gfc_typename (&lvalue->ts));
1851 return gfc_convert_type (rvalue, &lvalue->ts, 1);
1855 /* Check that a pointer assignment is OK. We first check lvalue, and
1856 we only check rvalue if it's not an assignment to NULL() or a
1857 NULLIFY statement. */
1860 gfc_check_pointer_assign (gfc_expr * lvalue, gfc_expr * rvalue)
1862 symbol_attribute attr;
1865 if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN)
1867 gfc_error ("Pointer assignment target is not a POINTER at %L",
1872 attr = gfc_variable_attr (lvalue, NULL);
1875 gfc_error ("Pointer assignment to non-POINTER at %L", &lvalue->where);
1879 is_pure = gfc_pure (NULL);
1881 if (is_pure && gfc_impure_variable (lvalue->symtree->n.sym))
1883 gfc_error ("Bad pointer object in PURE procedure at %L",
1888 /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
1889 kind, etc for lvalue and rvalue must match, and rvalue must be a
1890 pure variable if we're in a pure function. */
1891 if (rvalue->expr_type != EXPR_NULL)
1894 if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
1896 gfc_error ("Different types in pointer assignment at %L",
1901 if (lvalue->ts.kind != rvalue->ts.kind)
1904 ("Different kind type parameters in pointer assignment at %L",
1909 attr = gfc_expr_attr (rvalue);
1910 if (!attr.target && !attr.pointer)
1913 ("Pointer assignment target is neither TARGET nor POINTER at "
1914 "%L", &rvalue->where);
1918 if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
1921 ("Bad target in pointer assignment in PURE procedure at %L",
1930 /* Relative of gfc_check_assign() except that the lvalue is a single
1934 gfc_check_assign_symbol (gfc_symbol * sym, gfc_expr * rvalue)
1939 memset (&lvalue, '\0', sizeof (gfc_expr));
1941 lvalue.expr_type = EXPR_VARIABLE;
1942 lvalue.ts = sym->ts;
1944 lvalue.rank = sym->as->rank;
1945 lvalue.symtree = (gfc_symtree *)gfc_getmem (sizeof (gfc_symtree));
1946 lvalue.symtree->n.sym = sym;
1947 lvalue.where = sym->declared_at;
1949 r = gfc_check_assign (&lvalue, rvalue, 1);
1951 gfc_free (lvalue.symtree);