2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 static char expression_syntax[] = N_("Syntax error in expression at %C");
31 /* Match a user-defined operator name. This is a normal name with a
32 few restrictions. The error_flag controls whether an error is
33 raised if 'true' or 'false' are used or not. */
36 gfc_match_defined_op_name (char *result, int error_flag)
38 static const char * const badops[] = {
39 "and", "or", "not", "eqv", "neqv", "eq", "ne", "ge", "le", "lt", "gt",
43 char name[GFC_MAX_SYMBOL_LEN + 1];
48 old_loc = gfc_current_locus;
50 m = gfc_match (" . %n .", name);
54 /* .true. and .false. have interpretations as constants. Trying to
55 use these as operators will fail at a later time. */
57 if (strcmp (name, "true") == 0 || strcmp (name, "false") == 0)
61 gfc_current_locus = old_loc;
65 for (i = 0; badops[i]; i++)
66 if (strcmp (badops[i], name) == 0)
69 for (i = 0; name[i]; i++)
70 if (!ISALPHA (name[i]))
72 gfc_error ("Bad character '%c' in OPERATOR name at %C", name[i]);
76 strcpy (result, name);
80 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
83 gfc_current_locus = old_loc;
88 /* Match a user defined operator. The symbol found must be an
92 match_defined_operator (gfc_user_op **result)
94 char name[GFC_MAX_SYMBOL_LEN + 1];
97 m = gfc_match_defined_op_name (name, 0);
101 *result = gfc_get_uop (name);
106 /* Check to see if the given operator is next on the input. If this
107 is not the case, the parse pointer remains where it was. */
110 next_operator (gfc_intrinsic_op t)
115 old_loc = gfc_current_locus;
116 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
119 gfc_current_locus = old_loc;
124 /* Call the INTRINSIC_PARENTHESES function. This is both
125 used explicitly, as below, or by resolve.c to generate
129 gfc_get_parentheses (gfc_expr *e)
133 e2 = gfc_get_operator_expr (&e->where, INTRINSIC_PARENTHESES, e, NULL);
141 /* Match a primary expression. */
144 match_primary (gfc_expr **result)
149 m = gfc_match_literal_constant (result, 0);
153 m = gfc_match_array_constructor (result);
157 m = gfc_match_rvalue (result);
161 /* Match an expression in parentheses. */
162 if (gfc_match_char ('(') != MATCH_YES)
165 m = gfc_match_expr (&e);
168 if (m == MATCH_ERROR)
171 m = gfc_match_char (')');
173 gfc_error ("Expected a right parenthesis in expression at %C");
175 /* Now we have the expression inside the parentheses, build the
176 expression pointing to it. By 7.1.7.2, any expression in
177 parentheses shall be treated as a data entity. */
178 *result = gfc_get_parentheses (e);
182 gfc_free_expr (*result);
189 gfc_error (expression_syntax);
194 /* Match a level 1 expression. */
197 match_level_1 (gfc_expr **result)
204 where = gfc_current_locus;
206 m = match_defined_operator (&uop);
207 if (m == MATCH_ERROR)
210 m = match_primary (&e);
218 f = gfc_get_operator_expr (&where, INTRINSIC_USER, e, NULL);
219 f->value.op.uop = uop;
227 /* As a GNU extension we support an expanded level-2 expression syntax.
228 Via this extension we support (arbitrary) nesting of unary plus and
229 minus operations following unary and binary operators, such as **.
230 The grammar of section 7.1.1.3 is effectively rewritten as:
232 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
233 R704' ext-mult-operand is add-op ext-mult-operand
235 R705 add-operand is add-operand mult-op ext-mult-operand
237 R705' ext-add-operand is add-op ext-add-operand
239 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
243 static match match_ext_mult_operand (gfc_expr **result);
244 static match match_ext_add_operand (gfc_expr **result);
249 if (next_operator (INTRINSIC_MINUS))
251 if (next_operator (INTRINSIC_PLUS))
258 match_mult_operand (gfc_expr **result)
260 gfc_expr *e, *exp, *r;
264 m = match_level_1 (&e);
268 if (!next_operator (INTRINSIC_POWER))
274 where = gfc_current_locus;
276 m = match_ext_mult_operand (&exp);
278 gfc_error ("Expected exponent in expression at %C");
285 r = gfc_power (e, exp);
301 match_ext_mult_operand (gfc_expr **result)
308 where = gfc_current_locus;
312 return match_mult_operand (result);
314 if (gfc_notification_std (GFC_STD_GNU) == ERROR)
316 gfc_error ("Extension: Unary operator following "
317 "arithmetic operator (use parentheses) at %C");
321 gfc_warning ("Extension: Unary operator following "
322 "arithmetic operator (use parentheses) at %C");
324 m = match_ext_mult_operand (&e);
329 all = gfc_uminus (e);
346 match_add_operand (gfc_expr **result)
348 gfc_expr *all, *e, *total;
349 locus where, old_loc;
353 m = match_mult_operand (&all);
359 /* Build up a string of products or quotients. */
361 old_loc = gfc_current_locus;
363 if (next_operator (INTRINSIC_TIMES))
367 if (next_operator (INTRINSIC_DIVIDE))
368 i = INTRINSIC_DIVIDE;
373 where = gfc_current_locus;
375 m = match_ext_mult_operand (&e);
378 gfc_current_locus = old_loc;
382 if (m == MATCH_ERROR)
388 if (i == INTRINSIC_TIMES)
389 total = gfc_multiply (all, e);
391 total = gfc_divide (all, e);
410 match_ext_add_operand (gfc_expr **result)
417 where = gfc_current_locus;
421 return match_add_operand (result);
423 if (gfc_notification_std (GFC_STD_GNU) == ERROR)
425 gfc_error ("Extension: Unary operator following "
426 "arithmetic operator (use parentheses) at %C");
430 gfc_warning ("Extension: Unary operator following "
431 "arithmetic operator (use parentheses) at %C");
433 m = match_ext_add_operand (&e);
438 all = gfc_uminus (e);
454 /* Match a level 2 expression. */
457 match_level_2 (gfc_expr **result)
459 gfc_expr *all, *e, *total;
464 where = gfc_current_locus;
469 m = match_ext_add_operand (&e);
472 gfc_error (expression_syntax);
477 m = match_add_operand (&e);
487 all = gfc_uminus (e);
500 /* Append add-operands to the sum. */
504 where = gfc_current_locus;
509 m = match_ext_add_operand (&e);
511 gfc_error (expression_syntax);
519 total = gfc_subtract (all, e);
521 total = gfc_add (all, e);
539 /* Match a level three expression. */
542 match_level_3 (gfc_expr **result)
544 gfc_expr *all, *e, *total;
548 m = match_level_2 (&all);
554 if (!next_operator (INTRINSIC_CONCAT))
557 where = gfc_current_locus;
559 m = match_level_2 (&e);
562 gfc_error (expression_syntax);
568 total = gfc_concat (all, e);
585 /* Match a level 4 expression. */
588 match_level_4 (gfc_expr **result)
590 gfc_expr *left, *right, *r;
596 m = match_level_3 (&left);
600 old_loc = gfc_current_locus;
602 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
608 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
609 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT
610 && i != INTRINSIC_EQ_OS && i != INTRINSIC_NE_OS && i != INTRINSIC_GE_OS
611 && i != INTRINSIC_LE_OS && i != INTRINSIC_LT_OS && i != INTRINSIC_GT_OS)
613 gfc_current_locus = old_loc;
618 where = gfc_current_locus;
620 m = match_level_3 (&right);
622 gfc_error (expression_syntax);
625 gfc_free_expr (left);
632 case INTRINSIC_EQ_OS:
633 r = gfc_eq (left, right, i);
637 case INTRINSIC_NE_OS:
638 r = gfc_ne (left, right, i);
642 case INTRINSIC_LT_OS:
643 r = gfc_lt (left, right, i);
647 case INTRINSIC_LE_OS:
648 r = gfc_le (left, right, i);
652 case INTRINSIC_GT_OS:
653 r = gfc_gt (left, right, i);
657 case INTRINSIC_GE_OS:
658 r = gfc_ge (left, right, i);
662 gfc_internal_error ("match_level_4(): Bad operator");
667 gfc_free_expr (left);
668 gfc_free_expr (right);
680 match_and_operand (gfc_expr **result)
687 i = next_operator (INTRINSIC_NOT);
688 where = gfc_current_locus;
690 m = match_level_4 (&e);
713 match_or_operand (gfc_expr **result)
715 gfc_expr *all, *e, *total;
719 m = match_and_operand (&all);
725 if (!next_operator (INTRINSIC_AND))
727 where = gfc_current_locus;
729 m = match_and_operand (&e);
731 gfc_error (expression_syntax);
738 total = gfc_and (all, e);
756 match_equiv_operand (gfc_expr **result)
758 gfc_expr *all, *e, *total;
762 m = match_or_operand (&all);
768 if (!next_operator (INTRINSIC_OR))
770 where = gfc_current_locus;
772 m = match_or_operand (&e);
774 gfc_error (expression_syntax);
781 total = gfc_or (all, e);
798 /* Match a level 5 expression. */
801 match_level_5 (gfc_expr **result)
803 gfc_expr *all, *e, *total;
808 m = match_equiv_operand (&all);
814 if (next_operator (INTRINSIC_EQV))
818 if (next_operator (INTRINSIC_NEQV))
824 where = gfc_current_locus;
826 m = match_equiv_operand (&e);
828 gfc_error (expression_syntax);
835 if (i == INTRINSIC_EQV)
836 total = gfc_eqv (all, e);
838 total = gfc_neqv (all, e);
856 /* Match an expression. At this level, we are stringing together
857 level 5 expressions separated by binary operators. */
860 gfc_match_expr (gfc_expr **result)
867 m = match_level_5 (&all);
874 m = match_defined_operator (&uop);
877 if (m == MATCH_ERROR)
883 where = gfc_current_locus;
885 m = match_level_5 (&e);
887 gfc_error (expression_syntax);
894 all = gfc_get_operator_expr (&where, INTRINSIC_USER, all, e);
895 all->value.op.uop = uop;