2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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
29 static char expression_syntax[] = N_("Syntax error in expression at %C");
32 /* Match a user-defined operator name. This is a normal name with a
33 few restrictions. The error_flag controls whether an error is
34 raised if 'true' or 'false' are used or not. */
37 gfc_match_defined_op_name (char *result, int error_flag)
39 static const char * const badops[] = {
40 "and", "or", "not", "eqv", "neqv", "eq", "ne", "ge", "le", "lt", "gt",
44 char name[GFC_MAX_SYMBOL_LEN + 1];
49 old_loc = gfc_current_locus;
51 m = gfc_match (" . %n .", name);
55 /* .true. and .false. have interpretations as constants. Trying to
56 use these as operators will fail at a later time. */
58 if (strcmp (name, "true") == 0 || strcmp (name, "false") == 0)
62 gfc_current_locus = old_loc;
66 for (i = 0; badops[i]; i++)
67 if (strcmp (badops[i], name) == 0)
70 for (i = 0; name[i]; i++)
71 if (!ISALPHA (name[i]))
73 gfc_error ("Bad character '%c' in OPERATOR name at %C", name[i]);
77 strcpy (result, name);
81 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
84 gfc_current_locus = old_loc;
89 /* Match a user defined operator. The symbol found must be an
93 match_defined_operator (gfc_user_op **result)
95 char name[GFC_MAX_SYMBOL_LEN + 1];
98 m = gfc_match_defined_op_name (name, 0);
102 *result = gfc_get_uop (name);
107 /* Check to see if the given operator is next on the input. If this
108 is not the case, the parse pointer remains where it was. */
111 next_operator (gfc_intrinsic_op t)
116 old_loc = gfc_current_locus;
117 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
120 gfc_current_locus = old_loc;
125 /* Call the INTRINSIC_PARENTHESES function. This is both
126 used explicitly, as below, or by resolve.c to generate
130 gfc_get_parentheses (gfc_expr *e)
134 /* This is a temporary fix, awaiting the patch for various
135 other character problems. The resolution and translation
136 of substrings and concatenations are so kludged up that
137 putting parentheses around them breaks everything. */
138 if (e->ts.type == BT_CHARACTER && e->ref)
142 e2->expr_type = EXPR_OP;
145 e2->where = e->where;
146 e2->value.op.operator = INTRINSIC_PARENTHESES;
147 e2->value.op.op1 = e;
148 e2->value.op.op2 = NULL;
153 /* Match a primary expression. */
156 match_primary (gfc_expr **result)
162 m = gfc_match_literal_constant (result, 0);
166 m = gfc_match_array_constructor (result);
170 m = gfc_match_rvalue (result);
174 /* Match an expression in parentheses. */
175 where = gfc_current_locus;
177 if (gfc_match_char ('(') != MATCH_YES)
180 m = gfc_match_expr (&e);
183 if (m == MATCH_ERROR)
186 m = gfc_match_char (')');
188 gfc_error ("Expected a right parenthesis in expression at %C");
190 /* Now we have the expression inside the parentheses, build the
191 expression pointing to it. By 7.1.7.2, any expression in
192 parentheses shall be treated as a data entity. */
193 *result = gfc_get_parentheses (e);
197 gfc_free_expr (*result);
204 gfc_error (expression_syntax);
209 /* Build an operator expression node. */
212 build_node (gfc_intrinsic_op operator, locus *where,
213 gfc_expr *op1, gfc_expr *op2)
217 new = gfc_get_expr ();
218 new->expr_type = EXPR_OP;
219 new->value.op.operator = operator;
222 new->value.op.op1 = op1;
223 new->value.op.op2 = op2;
229 /* Match a level 1 expression. */
232 match_level_1 (gfc_expr **result)
239 where = gfc_current_locus;
241 m = match_defined_operator (&uop);
242 if (m == MATCH_ERROR)
245 m = match_primary (&e);
253 f = build_node (INTRINSIC_USER, &where, e, NULL);
254 f->value.op.uop = uop;
262 /* As a GNU extension we support an expanded level-2 expression syntax.
263 Via this extension we support (arbitrary) nesting of unary plus and
264 minus operations following unary and binary operators, such as **.
265 The grammar of section 7.1.1.3 is effectively rewitten as:
267 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
268 R704' ext-mult-operand is add-op ext-mult-operand
270 R705 add-operand is add-operand mult-op ext-mult-operand
272 R705' ext-add-operand is add-op ext-add-operand
274 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
278 static match match_ext_mult_operand (gfc_expr **result);
279 static match match_ext_add_operand (gfc_expr **result);
284 if (next_operator (INTRINSIC_MINUS))
286 if (next_operator (INTRINSIC_PLUS))
293 match_mult_operand (gfc_expr **result)
295 gfc_expr *e, *exp, *r;
299 m = match_level_1 (&e);
303 if (!next_operator (INTRINSIC_POWER))
309 where = gfc_current_locus;
311 m = match_ext_mult_operand (&exp);
313 gfc_error ("Expected exponent in expression at %C");
320 r = gfc_power (e, exp);
336 match_ext_mult_operand (gfc_expr **result)
343 where = gfc_current_locus;
347 return match_mult_operand (result);
349 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following "
350 "arithmetic operator (use parentheses) at %C")
354 m = match_ext_mult_operand (&e);
359 all = gfc_uminus (e);
376 match_add_operand (gfc_expr **result)
378 gfc_expr *all, *e, *total;
379 locus where, old_loc;
383 m = match_mult_operand (&all);
389 /* Build up a string of products or quotients. */
391 old_loc = gfc_current_locus;
393 if (next_operator (INTRINSIC_TIMES))
397 if (next_operator (INTRINSIC_DIVIDE))
398 i = INTRINSIC_DIVIDE;
403 where = gfc_current_locus;
405 m = match_ext_mult_operand (&e);
408 gfc_current_locus = old_loc;
412 if (m == MATCH_ERROR)
418 if (i == INTRINSIC_TIMES)
419 total = gfc_multiply (all, e);
421 total = gfc_divide (all, e);
440 match_ext_add_operand (gfc_expr **result)
447 where = gfc_current_locus;
451 return match_add_operand (result);
453 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following "
454 "arithmetic operator (use parentheses) at %C")
458 m = match_ext_add_operand (&e);
463 all = gfc_uminus (e);
479 /* Match a level 2 expression. */
482 match_level_2 (gfc_expr **result)
484 gfc_expr *all, *e, *total;
489 where = gfc_current_locus;
494 m = match_ext_add_operand (&e);
497 gfc_error (expression_syntax);
502 m = match_add_operand (&e);
512 all = gfc_uminus (e);
525 /* Append add-operands to the sum. */
529 where = gfc_current_locus;
534 m = match_ext_add_operand (&e);
536 gfc_error (expression_syntax);
544 total = gfc_subtract (all, e);
546 total = gfc_add (all, e);
564 /* Match a level three expression. */
567 match_level_3 (gfc_expr **result)
569 gfc_expr *all, *e, *total;
573 m = match_level_2 (&all);
579 if (!next_operator (INTRINSIC_CONCAT))
582 where = gfc_current_locus;
584 m = match_level_2 (&e);
587 gfc_error (expression_syntax);
593 total = gfc_concat (all, e);
610 /* Match a level 4 expression. */
613 match_level_4 (gfc_expr **result)
615 gfc_expr *left, *right, *r;
621 m = match_level_3 (&left);
625 old_loc = gfc_current_locus;
627 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
633 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
634 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT
635 && i != INTRINSIC_EQ_OS && i != INTRINSIC_NE_OS && i != INTRINSIC_GE_OS
636 && i != INTRINSIC_LE_OS && i != INTRINSIC_LT_OS && i != INTRINSIC_GT_OS)
638 gfc_current_locus = old_loc;
643 where = gfc_current_locus;
645 m = match_level_3 (&right);
647 gfc_error (expression_syntax);
650 gfc_free_expr (left);
657 case INTRINSIC_EQ_OS:
658 r = gfc_eq (left, right, i);
662 case INTRINSIC_NE_OS:
663 r = gfc_ne (left, right, i);
667 case INTRINSIC_LT_OS:
668 r = gfc_lt (left, right, i);
672 case INTRINSIC_LE_OS:
673 r = gfc_le (left, right, i);
677 case INTRINSIC_GT_OS:
678 r = gfc_gt (left, right, i);
682 case INTRINSIC_GE_OS:
683 r = gfc_ge (left, right, i);
687 gfc_internal_error ("match_level_4(): Bad operator");
692 gfc_free_expr (left);
693 gfc_free_expr (right);
705 match_and_operand (gfc_expr **result)
712 i = next_operator (INTRINSIC_NOT);
713 where = gfc_current_locus;
715 m = match_level_4 (&e);
738 match_or_operand (gfc_expr **result)
740 gfc_expr *all, *e, *total;
744 m = match_and_operand (&all);
750 if (!next_operator (INTRINSIC_AND))
752 where = gfc_current_locus;
754 m = match_and_operand (&e);
756 gfc_error (expression_syntax);
763 total = gfc_and (all, e);
781 match_equiv_operand (gfc_expr **result)
783 gfc_expr *all, *e, *total;
787 m = match_or_operand (&all);
793 if (!next_operator (INTRINSIC_OR))
795 where = gfc_current_locus;
797 m = match_or_operand (&e);
799 gfc_error (expression_syntax);
806 total = gfc_or (all, e);
823 /* Match a level 5 expression. */
826 match_level_5 (gfc_expr **result)
828 gfc_expr *all, *e, *total;
833 m = match_equiv_operand (&all);
839 if (next_operator (INTRINSIC_EQV))
843 if (next_operator (INTRINSIC_NEQV))
849 where = gfc_current_locus;
851 m = match_equiv_operand (&e);
853 gfc_error (expression_syntax);
860 if (i == INTRINSIC_EQV)
861 total = gfc_eqv (all, e);
863 total = gfc_neqv (all, e);
881 /* Match an expression. At this level, we are stringing together
882 level 5 expressions separated by binary operators. */
885 gfc_match_expr (gfc_expr **result)
892 m = match_level_5 (&all);
899 m = match_defined_operator (&uop);
902 if (m == MATCH_ERROR)
908 where = gfc_current_locus;
910 m = match_level_5 (&e);
912 gfc_error (expression_syntax);
919 all = build_node (INTRINSIC_USER, &where, all, e);
920 all->value.op.uop = uop;