Merge from trunk @ 138209
[official-gcc.git] / gcc / fortran / matchexp.c
blobf7573655316d50aa7f7514336a4ba25aed4b56c7
1 /* Expression parser.
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "arith.h"
26 #include "match.h"
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. */
35 match
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",
40 NULL
43 char name[GFC_MAX_SYMBOL_LEN + 1];
44 locus old_loc;
45 match m;
46 int i;
48 old_loc = gfc_current_locus;
50 m = gfc_match (" . %n .", name);
51 if (m != MATCH_YES)
52 return m;
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)
59 if (error_flag)
60 goto error;
61 gfc_current_locus = old_loc;
62 return MATCH_NO;
65 for (i = 0; badops[i]; i++)
66 if (strcmp (badops[i], name) == 0)
67 goto error;
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]);
73 return MATCH_ERROR;
76 strcpy (result, name);
77 return MATCH_YES;
79 error:
80 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
81 name);
83 gfc_current_locus = old_loc;
84 return MATCH_ERROR;
88 /* Match a user defined operator. The symbol found must be an
89 operator already. */
91 static match
92 match_defined_operator (gfc_user_op **result)
94 char name[GFC_MAX_SYMBOL_LEN + 1];
95 match m;
97 m = gfc_match_defined_op_name (name, 0);
98 if (m != MATCH_YES)
99 return m;
101 *result = gfc_get_uop (name);
102 return MATCH_YES;
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. */
109 static int
110 next_operator (gfc_intrinsic_op t)
112 gfc_intrinsic_op u;
113 locus old_loc;
115 old_loc = gfc_current_locus;
116 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
117 return 1;
119 gfc_current_locus = old_loc;
120 return 0;
124 /* Call the INTRINSIC_PARENTHESES function. This is both
125 used explicitly, as below, or by resolve.c to generate
126 temporaries. */
128 gfc_expr *
129 gfc_get_parentheses (gfc_expr *e)
131 gfc_expr *e2;
133 e2 = gfc_get_expr();
134 e2->expr_type = EXPR_OP;
135 e2->ts = e->ts;
136 e2->rank = e->rank;
137 e2->where = e->where;
138 e2->value.op.op = INTRINSIC_PARENTHESES;
139 e2->value.op.op1 = e;
140 e2->value.op.op2 = NULL;
141 return e2;
145 /* Match a primary expression. */
147 static match
148 match_primary (gfc_expr **result)
150 match m;
151 gfc_expr *e;
152 locus where;
154 m = gfc_match_literal_constant (result, 0);
155 if (m != MATCH_NO)
156 return m;
158 m = gfc_match_array_constructor (result);
159 if (m != MATCH_NO)
160 return m;
162 m = gfc_match_rvalue (result);
163 if (m != MATCH_NO)
164 return m;
166 /* Match an expression in parentheses. */
167 where = gfc_current_locus;
169 if (gfc_match_char ('(') != MATCH_YES)
170 return MATCH_NO;
172 m = gfc_match_expr (&e);
173 if (m == MATCH_NO)
174 goto syntax;
175 if (m == MATCH_ERROR)
176 return m;
178 m = gfc_match_char (')');
179 if (m == MATCH_NO)
180 gfc_error ("Expected a right parenthesis in expression at %C");
182 /* Now we have the expression inside the parentheses, build the
183 expression pointing to it. By 7.1.7.2, any expression in
184 parentheses shall be treated as a data entity. */
185 *result = gfc_get_parentheses (e);
187 if (m != MATCH_YES)
189 gfc_free_expr (*result);
190 return MATCH_ERROR;
193 return MATCH_YES;
195 syntax:
196 gfc_error (expression_syntax);
197 return MATCH_ERROR;
201 /* Build an operator expression node. */
203 static gfc_expr *
204 build_node (gfc_intrinsic_op op, locus *where,
205 gfc_expr *op1, gfc_expr *op2)
207 gfc_expr *new_expr;
209 new_expr = gfc_get_expr ();
210 new_expr->expr_type = EXPR_OP;
211 new_expr->value.op.op = op;
212 new_expr->where = *where;
214 new_expr->value.op.op1 = op1;
215 new_expr->value.op.op2 = op2;
217 return new_expr;
221 /* Match a level 1 expression. */
223 static match
224 match_level_1 (gfc_expr **result)
226 gfc_user_op *uop;
227 gfc_expr *e, *f;
228 locus where;
229 match m;
231 where = gfc_current_locus;
232 uop = NULL;
233 m = match_defined_operator (&uop);
234 if (m == MATCH_ERROR)
235 return m;
237 m = match_primary (&e);
238 if (m != MATCH_YES)
239 return m;
241 if (uop == NULL)
242 *result = e;
243 else
245 f = build_node (INTRINSIC_USER, &where, e, NULL);
246 f->value.op.uop = uop;
247 *result = f;
250 return MATCH_YES;
254 /* As a GNU extension we support an expanded level-2 expression syntax.
255 Via this extension we support (arbitrary) nesting of unary plus and
256 minus operations following unary and binary operators, such as **.
257 The grammar of section 7.1.1.3 is effectively rewritten as:
259 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
260 R704' ext-mult-operand is add-op ext-mult-operand
261 or mult-operand
262 R705 add-operand is add-operand mult-op ext-mult-operand
263 or mult-operand
264 R705' ext-add-operand is add-op ext-add-operand
265 or add-operand
266 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
267 or add-operand
270 static match match_ext_mult_operand (gfc_expr **result);
271 static match match_ext_add_operand (gfc_expr **result);
273 static int
274 match_add_op (void)
276 if (next_operator (INTRINSIC_MINUS))
277 return -1;
278 if (next_operator (INTRINSIC_PLUS))
279 return 1;
280 return 0;
284 static match
285 match_mult_operand (gfc_expr **result)
287 gfc_expr *e, *exp, *r;
288 locus where;
289 match m;
291 m = match_level_1 (&e);
292 if (m != MATCH_YES)
293 return m;
295 if (!next_operator (INTRINSIC_POWER))
297 *result = e;
298 return MATCH_YES;
301 where = gfc_current_locus;
303 m = match_ext_mult_operand (&exp);
304 if (m == MATCH_NO)
305 gfc_error ("Expected exponent in expression at %C");
306 if (m != MATCH_YES)
308 gfc_free_expr (e);
309 return MATCH_ERROR;
312 r = gfc_power (e, exp);
313 if (r == NULL)
315 gfc_free_expr (e);
316 gfc_free_expr (exp);
317 return MATCH_ERROR;
320 r->where = where;
321 *result = r;
323 return MATCH_YES;
327 static match
328 match_ext_mult_operand (gfc_expr **result)
330 gfc_expr *all, *e;
331 locus where;
332 match m;
333 int i;
335 where = gfc_current_locus;
336 i = match_add_op ();
338 if (i == 0)
339 return match_mult_operand (result);
341 if (gfc_notification_std (GFC_STD_GNU) == ERROR)
343 gfc_error ("Extension: Unary operator following "
344 "arithmetic operator (use parentheses) at %C");
345 return MATCH_ERROR;
347 else
348 gfc_warning ("Extension: Unary operator following "
349 "arithmetic operator (use parentheses) at %C");
351 m = match_ext_mult_operand (&e);
352 if (m != MATCH_YES)
353 return m;
355 if (i == -1)
356 all = gfc_uminus (e);
357 else
358 all = gfc_uplus (e);
360 if (all == NULL)
362 gfc_free_expr (e);
363 return MATCH_ERROR;
366 all->where = where;
367 *result = all;
368 return MATCH_YES;
372 static match
373 match_add_operand (gfc_expr **result)
375 gfc_expr *all, *e, *total;
376 locus where, old_loc;
377 match m;
378 gfc_intrinsic_op i;
380 m = match_mult_operand (&all);
381 if (m != MATCH_YES)
382 return m;
384 for (;;)
386 /* Build up a string of products or quotients. */
388 old_loc = gfc_current_locus;
390 if (next_operator (INTRINSIC_TIMES))
391 i = INTRINSIC_TIMES;
392 else
394 if (next_operator (INTRINSIC_DIVIDE))
395 i = INTRINSIC_DIVIDE;
396 else
397 break;
400 where = gfc_current_locus;
402 m = match_ext_mult_operand (&e);
403 if (m == MATCH_NO)
405 gfc_current_locus = old_loc;
406 break;
409 if (m == MATCH_ERROR)
411 gfc_free_expr (all);
412 return MATCH_ERROR;
415 if (i == INTRINSIC_TIMES)
416 total = gfc_multiply (all, e);
417 else
418 total = gfc_divide (all, e);
420 if (total == NULL)
422 gfc_free_expr (all);
423 gfc_free_expr (e);
424 return MATCH_ERROR;
427 all = total;
428 all->where = where;
431 *result = all;
432 return MATCH_YES;
436 static match
437 match_ext_add_operand (gfc_expr **result)
439 gfc_expr *all, *e;
440 locus where;
441 match m;
442 int i;
444 where = gfc_current_locus;
445 i = match_add_op ();
447 if (i == 0)
448 return match_add_operand (result);
450 if (gfc_notification_std (GFC_STD_GNU) == ERROR)
452 gfc_error ("Extension: Unary operator following "
453 "arithmetic operator (use parentheses) at %C");
454 return MATCH_ERROR;
456 else
457 gfc_warning ("Extension: Unary operator following "
458 "arithmetic operator (use parentheses) at %C");
460 m = match_ext_add_operand (&e);
461 if (m != MATCH_YES)
462 return m;
464 if (i == -1)
465 all = gfc_uminus (e);
466 else
467 all = gfc_uplus (e);
469 if (all == NULL)
471 gfc_free_expr (e);
472 return MATCH_ERROR;
475 all->where = where;
476 *result = all;
477 return MATCH_YES;
481 /* Match a level 2 expression. */
483 static match
484 match_level_2 (gfc_expr **result)
486 gfc_expr *all, *e, *total;
487 locus where;
488 match m;
489 int i;
491 where = gfc_current_locus;
492 i = match_add_op ();
494 if (i != 0)
496 m = match_ext_add_operand (&e);
497 if (m == MATCH_NO)
499 gfc_error (expression_syntax);
500 m = MATCH_ERROR;
503 else
504 m = match_add_operand (&e);
506 if (m != MATCH_YES)
507 return m;
509 if (i == 0)
510 all = e;
511 else
513 if (i == -1)
514 all = gfc_uminus (e);
515 else
516 all = gfc_uplus (e);
518 if (all == NULL)
520 gfc_free_expr (e);
521 return MATCH_ERROR;
525 all->where = where;
527 /* Append add-operands to the sum. */
529 for (;;)
531 where = gfc_current_locus;
532 i = match_add_op ();
533 if (i == 0)
534 break;
536 m = match_ext_add_operand (&e);
537 if (m == MATCH_NO)
538 gfc_error (expression_syntax);
539 if (m != MATCH_YES)
541 gfc_free_expr (all);
542 return MATCH_ERROR;
545 if (i == -1)
546 total = gfc_subtract (all, e);
547 else
548 total = gfc_add (all, e);
550 if (total == NULL)
552 gfc_free_expr (all);
553 gfc_free_expr (e);
554 return MATCH_ERROR;
557 all = total;
558 all->where = where;
561 *result = all;
562 return MATCH_YES;
566 /* Match a level three expression. */
568 static match
569 match_level_3 (gfc_expr **result)
571 gfc_expr *all, *e, *total;
572 locus where;
573 match m;
575 m = match_level_2 (&all);
576 if (m != MATCH_YES)
577 return m;
579 for (;;)
581 if (!next_operator (INTRINSIC_CONCAT))
582 break;
584 where = gfc_current_locus;
586 m = match_level_2 (&e);
587 if (m == MATCH_NO)
589 gfc_error (expression_syntax);
590 gfc_free_expr (all);
592 if (m != MATCH_YES)
593 return MATCH_ERROR;
595 total = gfc_concat (all, e);
596 if (total == NULL)
598 gfc_free_expr (all);
599 gfc_free_expr (e);
600 return MATCH_ERROR;
603 all = total;
604 all->where = where;
607 *result = all;
608 return MATCH_YES;
612 /* Match a level 4 expression. */
614 static match
615 match_level_4 (gfc_expr **result)
617 gfc_expr *left, *right, *r;
618 gfc_intrinsic_op i;
619 locus old_loc;
620 locus where;
621 match m;
623 m = match_level_3 (&left);
624 if (m != MATCH_YES)
625 return m;
627 old_loc = gfc_current_locus;
629 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
631 *result = left;
632 return MATCH_YES;
635 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
636 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT
637 && i != INTRINSIC_EQ_OS && i != INTRINSIC_NE_OS && i != INTRINSIC_GE_OS
638 && i != INTRINSIC_LE_OS && i != INTRINSIC_LT_OS && i != INTRINSIC_GT_OS)
640 gfc_current_locus = old_loc;
641 *result = left;
642 return MATCH_YES;
645 where = gfc_current_locus;
647 m = match_level_3 (&right);
648 if (m == MATCH_NO)
649 gfc_error (expression_syntax);
650 if (m != MATCH_YES)
652 gfc_free_expr (left);
653 return MATCH_ERROR;
656 switch (i)
658 case INTRINSIC_EQ:
659 case INTRINSIC_EQ_OS:
660 r = gfc_eq (left, right, i);
661 break;
663 case INTRINSIC_NE:
664 case INTRINSIC_NE_OS:
665 r = gfc_ne (left, right, i);
666 break;
668 case INTRINSIC_LT:
669 case INTRINSIC_LT_OS:
670 r = gfc_lt (left, right, i);
671 break;
673 case INTRINSIC_LE:
674 case INTRINSIC_LE_OS:
675 r = gfc_le (left, right, i);
676 break;
678 case INTRINSIC_GT:
679 case INTRINSIC_GT_OS:
680 r = gfc_gt (left, right, i);
681 break;
683 case INTRINSIC_GE:
684 case INTRINSIC_GE_OS:
685 r = gfc_ge (left, right, i);
686 break;
688 default:
689 gfc_internal_error ("match_level_4(): Bad operator");
692 if (r == NULL)
694 gfc_free_expr (left);
695 gfc_free_expr (right);
696 return MATCH_ERROR;
699 r->where = where;
700 *result = r;
702 return MATCH_YES;
706 static match
707 match_and_operand (gfc_expr **result)
709 gfc_expr *e, *r;
710 locus where;
711 match m;
712 int i;
714 i = next_operator (INTRINSIC_NOT);
715 where = gfc_current_locus;
717 m = match_level_4 (&e);
718 if (m != MATCH_YES)
719 return m;
721 r = e;
722 if (i)
724 r = gfc_not (e);
725 if (r == NULL)
727 gfc_free_expr (e);
728 return MATCH_ERROR;
732 r->where = where;
733 *result = r;
735 return MATCH_YES;
739 static match
740 match_or_operand (gfc_expr **result)
742 gfc_expr *all, *e, *total;
743 locus where;
744 match m;
746 m = match_and_operand (&all);
747 if (m != MATCH_YES)
748 return m;
750 for (;;)
752 if (!next_operator (INTRINSIC_AND))
753 break;
754 where = gfc_current_locus;
756 m = match_and_operand (&e);
757 if (m == MATCH_NO)
758 gfc_error (expression_syntax);
759 if (m != MATCH_YES)
761 gfc_free_expr (all);
762 return MATCH_ERROR;
765 total = gfc_and (all, e);
766 if (total == NULL)
768 gfc_free_expr (all);
769 gfc_free_expr (e);
770 return MATCH_ERROR;
773 all = total;
774 all->where = where;
777 *result = all;
778 return MATCH_YES;
782 static match
783 match_equiv_operand (gfc_expr **result)
785 gfc_expr *all, *e, *total;
786 locus where;
787 match m;
789 m = match_or_operand (&all);
790 if (m != MATCH_YES)
791 return m;
793 for (;;)
795 if (!next_operator (INTRINSIC_OR))
796 break;
797 where = gfc_current_locus;
799 m = match_or_operand (&e);
800 if (m == MATCH_NO)
801 gfc_error (expression_syntax);
802 if (m != MATCH_YES)
804 gfc_free_expr (all);
805 return MATCH_ERROR;
808 total = gfc_or (all, e);
809 if (total == NULL)
811 gfc_free_expr (all);
812 gfc_free_expr (e);
813 return MATCH_ERROR;
816 all = total;
817 all->where = where;
820 *result = all;
821 return MATCH_YES;
825 /* Match a level 5 expression. */
827 static match
828 match_level_5 (gfc_expr **result)
830 gfc_expr *all, *e, *total;
831 locus where;
832 match m;
833 gfc_intrinsic_op i;
835 m = match_equiv_operand (&all);
836 if (m != MATCH_YES)
837 return m;
839 for (;;)
841 if (next_operator (INTRINSIC_EQV))
842 i = INTRINSIC_EQV;
843 else
845 if (next_operator (INTRINSIC_NEQV))
846 i = INTRINSIC_NEQV;
847 else
848 break;
851 where = gfc_current_locus;
853 m = match_equiv_operand (&e);
854 if (m == MATCH_NO)
855 gfc_error (expression_syntax);
856 if (m != MATCH_YES)
858 gfc_free_expr (all);
859 return MATCH_ERROR;
862 if (i == INTRINSIC_EQV)
863 total = gfc_eqv (all, e);
864 else
865 total = gfc_neqv (all, e);
867 if (total == NULL)
869 gfc_free_expr (all);
870 gfc_free_expr (e);
871 return MATCH_ERROR;
874 all = total;
875 all->where = where;
878 *result = all;
879 return MATCH_YES;
883 /* Match an expression. At this level, we are stringing together
884 level 5 expressions separated by binary operators. */
886 match
887 gfc_match_expr (gfc_expr **result)
889 gfc_expr *all, *e;
890 gfc_user_op *uop;
891 locus where;
892 match m;
894 m = match_level_5 (&all);
895 if (m != MATCH_YES)
896 return m;
898 for (;;)
900 uop = NULL;
901 m = match_defined_operator (&uop);
902 if (m == MATCH_NO)
903 break;
904 if (m == MATCH_ERROR)
906 gfc_free_expr (all);
907 return MATCH_ERROR;
910 where = gfc_current_locus;
912 m = match_level_5 (&e);
913 if (m == MATCH_NO)
914 gfc_error (expression_syntax);
915 if (m != MATCH_YES)
917 gfc_free_expr (all);
918 return MATCH_ERROR;
921 all = build_node (INTRINSIC_USER, &where, all, e);
922 all->value.op.uop = uop;
925 *result = all;
926 return MATCH_YES;