2 Copyright (C) 2000, 2001, 2002, 2004, 2005 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, 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 /* Match a primary expression. */
128 match_primary (gfc_expr ** result)
134 m = gfc_match_literal_constant (result, 0);
138 m = gfc_match_array_constructor (result);
142 m = gfc_match_rvalue (result);
146 /* Match an expression in parentheses. */
147 where = gfc_current_locus;
149 if (gfc_match_char ('(') != MATCH_YES)
152 m = gfc_match_expr (&e);
155 if (m == MATCH_ERROR)
158 m = gfc_match_char (')');
160 gfc_error ("Expected a right parenthesis in expression at %C");
162 /* Now we have the expression inside the parentheses, build the
163 expression pointing to it. By 7.1.7.2 the integrity of
164 parentheses is only conserved in numerical calculations, so we
165 don't bother to keep the parentheses otherwise. */
166 if(!gfc_numeric_ts(&e->ts))
170 gfc_expr *e2 = gfc_get_expr();
172 e2->expr_type = EXPR_OP;
176 e2->value.op.operator = INTRINSIC_PARENTHESES;
177 e2->value.op.op1 = e;
178 e2->value.op.op2 = NULL;
184 gfc_free_expr (*result);
191 gfc_error (expression_syntax);
196 /* Build an operator expression node. */
199 build_node (gfc_intrinsic_op operator, locus * where,
200 gfc_expr * op1, gfc_expr * op2)
204 new = gfc_get_expr ();
205 new->expr_type = EXPR_OP;
206 new->value.op.operator = operator;
209 new->value.op.op1 = op1;
210 new->value.op.op2 = op2;
216 /* Match a level 1 expression. */
219 match_level_1 (gfc_expr ** result)
226 where = gfc_current_locus;
228 m = match_defined_operator (&uop);
229 if (m == MATCH_ERROR)
232 m = match_primary (&e);
240 f = build_node (INTRINSIC_USER, &where, e, NULL);
241 f->value.op.uop = uop;
249 /* As a GNU extension we support an expanded level-2 expression syntax.
250 Via this extension we support (arbitrary) nesting of unary plus and
251 minus operations following unary and binary operators, such as **.
252 The grammar of section 7.1.1.3 is effectively rewitten as:
254 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
255 R704' ext-mult-operand is add-op ext-mult-operand
257 R705 add-operand is add-operand mult-op ext-mult-operand
259 R705' ext-add-operand is add-op ext-add-operand
261 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
265 static match match_ext_mult_operand (gfc_expr ** result);
266 static match match_ext_add_operand (gfc_expr ** result);
273 if (next_operator (INTRINSIC_MINUS))
275 if (next_operator (INTRINSIC_PLUS))
282 match_mult_operand (gfc_expr ** result)
284 gfc_expr *e, *exp, *r;
288 m = match_level_1 (&e);
292 if (!next_operator (INTRINSIC_POWER))
298 where = gfc_current_locus;
300 m = match_ext_mult_operand (&exp);
302 gfc_error ("Expected exponent in expression at %C");
309 r = gfc_power (e, exp);
325 match_ext_mult_operand (gfc_expr ** result)
332 where = gfc_current_locus;
336 return match_mult_operand (result);
338 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
339 " arithmetic operator (use parentheses) at %C")
343 m = match_ext_mult_operand (&e);
348 all = gfc_uminus (e);
365 match_add_operand (gfc_expr ** result)
367 gfc_expr *all, *e, *total;
368 locus where, old_loc;
372 m = match_mult_operand (&all);
378 /* Build up a string of products or quotients. */
380 old_loc = gfc_current_locus;
382 if (next_operator (INTRINSIC_TIMES))
386 if (next_operator (INTRINSIC_DIVIDE))
387 i = INTRINSIC_DIVIDE;
392 where = gfc_current_locus;
394 m = match_ext_mult_operand (&e);
397 gfc_current_locus = old_loc;
401 if (m == MATCH_ERROR)
407 if (i == INTRINSIC_TIMES)
408 total = gfc_multiply (all, e);
410 total = gfc_divide (all, e);
429 match_ext_add_operand (gfc_expr ** result)
436 where = gfc_current_locus;
440 return match_add_operand (result);
442 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
443 " arithmetic operator (use parentheses) at %C")
447 m = match_ext_add_operand (&e);
452 all = gfc_uminus (e);
468 /* Match a level 2 expression. */
471 match_level_2 (gfc_expr ** result)
473 gfc_expr *all, *e, *total;
478 where = gfc_current_locus;
483 m = match_ext_add_operand (&e);
486 gfc_error (expression_syntax);
491 m = match_add_operand (&e);
501 all = gfc_uminus (e);
514 /* Append add-operands to the sum */
518 where = gfc_current_locus;
523 m = match_ext_add_operand (&e);
525 gfc_error (expression_syntax);
533 total = gfc_subtract (all, e);
535 total = gfc_add (all, e);
553 /* Match a level three expression. */
556 match_level_3 (gfc_expr ** result)
558 gfc_expr *all, *e, *total;
562 m = match_level_2 (&all);
568 if (!next_operator (INTRINSIC_CONCAT))
571 where = gfc_current_locus;
573 m = match_level_2 (&e);
576 gfc_error (expression_syntax);
582 total = gfc_concat (all, e);
599 /* Match a level 4 expression. */
602 match_level_4 (gfc_expr ** result)
604 gfc_expr *left, *right, *r;
610 m = match_level_3 (&left);
614 old_loc = gfc_current_locus;
616 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
622 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
623 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT)
625 gfc_current_locus = old_loc;
630 where = gfc_current_locus;
632 m = match_level_3 (&right);
634 gfc_error (expression_syntax);
637 gfc_free_expr (left);
644 r = gfc_eq (left, right);
648 r = gfc_ne (left, right);
652 r = gfc_lt (left, right);
656 r = gfc_le (left, right);
660 r = gfc_gt (left, right);
664 r = gfc_ge (left, right);
668 gfc_internal_error ("match_level_4(): Bad operator");
673 gfc_free_expr (left);
674 gfc_free_expr (right);
686 match_and_operand (gfc_expr ** result)
693 i = next_operator (INTRINSIC_NOT);
694 where = gfc_current_locus;
696 m = match_level_4 (&e);
719 match_or_operand (gfc_expr ** result)
721 gfc_expr *all, *e, *total;
725 m = match_and_operand (&all);
731 if (!next_operator (INTRINSIC_AND))
733 where = gfc_current_locus;
735 m = match_and_operand (&e);
737 gfc_error (expression_syntax);
744 total = gfc_and (all, e);
762 match_equiv_operand (gfc_expr ** result)
764 gfc_expr *all, *e, *total;
768 m = match_or_operand (&all);
774 if (!next_operator (INTRINSIC_OR))
776 where = gfc_current_locus;
778 m = match_or_operand (&e);
780 gfc_error (expression_syntax);
787 total = gfc_or (all, e);
804 /* Match a level 5 expression. */
807 match_level_5 (gfc_expr ** result)
809 gfc_expr *all, *e, *total;
814 m = match_equiv_operand (&all);
820 if (next_operator (INTRINSIC_EQV))
824 if (next_operator (INTRINSIC_NEQV))
830 where = gfc_current_locus;
832 m = match_equiv_operand (&e);
834 gfc_error (expression_syntax);
841 if (i == INTRINSIC_EQV)
842 total = gfc_eqv (all, e);
844 total = gfc_neqv (all, e);
862 /* Match an expression. At this level, we are stringing together
863 level 5 expressions separated by binary operators. */
866 gfc_match_expr (gfc_expr ** result)
873 m = match_level_5 (&all);
880 m = match_defined_operator (&uop);
883 if (m == MATCH_ERROR)
889 where = gfc_current_locus;
891 m = match_level_5 (&e);
893 gfc_error (expression_syntax);
900 all = build_node (INTRINSIC_USER, &where, all, e);
901 all->value.op.uop = uop;