Merge from mainline
[official-gcc.git] / gcc / fortran / matchexp.c
blob008214940f024dfc505c082ca684a1176e214b57
1 /* Expression parser.
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006 Free Software Foundation,
3 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
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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "gfortran.h"
27 #include "arith.h"
28 #include "match.h"
30 static char expression_syntax[] = N_("Syntax error in expression at %C");
33 /* Match a user-defined operator name. This is a normal name with a
34 few restrictions. The error_flag controls whether an error is
35 raised if 'true' or 'false' are used or not. */
37 match
38 gfc_match_defined_op_name (char *result, int error_flag)
40 static const char * const badops[] = {
41 "and", "or", "not", "eqv", "neqv", "eq", "ne", "ge", "le", "lt", "gt",
42 NULL
45 char name[GFC_MAX_SYMBOL_LEN + 1];
46 locus old_loc;
47 match m;
48 int i;
50 old_loc = gfc_current_locus;
52 m = gfc_match (" . %n .", name);
53 if (m != MATCH_YES)
54 return m;
56 /* .true. and .false. have interpretations as constants. Trying to
57 use these as operators will fail at a later time. */
59 if (strcmp (name, "true") == 0 || strcmp (name, "false") == 0)
61 if (error_flag)
62 goto error;
63 gfc_current_locus = old_loc;
64 return MATCH_NO;
67 for (i = 0; badops[i]; i++)
68 if (strcmp (badops[i], name) == 0)
69 goto error;
71 for (i = 0; name[i]; i++)
72 if (!ISALPHA (name[i]))
74 gfc_error ("Bad character '%c' in OPERATOR name at %C", name[i]);
75 return MATCH_ERROR;
78 strcpy (result, name);
79 return MATCH_YES;
81 error:
82 gfc_error ("The name '%s' cannot be used as a defined operator at %C",
83 name);
85 gfc_current_locus = old_loc;
86 return MATCH_ERROR;
90 /* Match a user defined operator. The symbol found must be an
91 operator already. */
93 static match
94 match_defined_operator (gfc_user_op ** result)
96 char name[GFC_MAX_SYMBOL_LEN + 1];
97 match m;
99 m = gfc_match_defined_op_name (name, 0);
100 if (m != MATCH_YES)
101 return m;
103 *result = gfc_get_uop (name);
104 return MATCH_YES;
108 /* Check to see if the given operator is next on the input. If this
109 is not the case, the parse pointer remains where it was. */
111 static int
112 next_operator (gfc_intrinsic_op t)
114 gfc_intrinsic_op u;
115 locus old_loc;
117 old_loc = gfc_current_locus;
118 if (gfc_match_intrinsic_op (&u) == MATCH_YES && t == u)
119 return 1;
121 gfc_current_locus = old_loc;
122 return 0;
126 /* Match a primary expression. */
128 static match
129 match_primary (gfc_expr ** result)
131 match m;
132 gfc_expr *e;
133 locus where;
135 m = gfc_match_literal_constant (result, 0);
136 if (m != MATCH_NO)
137 return m;
139 m = gfc_match_array_constructor (result);
140 if (m != MATCH_NO)
141 return m;
143 m = gfc_match_rvalue (result);
144 if (m != MATCH_NO)
145 return m;
147 /* Match an expression in parentheses. */
148 where = gfc_current_locus;
150 if (gfc_match_char ('(') != MATCH_YES)
151 return MATCH_NO;
153 m = gfc_match_expr (&e);
154 if (m == MATCH_NO)
155 goto syntax;
156 if (m == MATCH_ERROR)
157 return m;
159 m = gfc_match_char (')');
160 if (m == MATCH_NO)
161 gfc_error ("Expected a right parenthesis in expression at %C");
163 /* Now we have the expression inside the parentheses, build the
164 expression pointing to it. By 7.1.7.2 the integrity of
165 parentheses is only conserved in numerical calculations, so we
166 don't bother to keep the parentheses otherwise. */
167 if(!gfc_numeric_ts(&e->ts))
168 *result = e;
169 else
171 gfc_expr *e2 = gfc_get_expr();
173 e2->expr_type = EXPR_OP;
174 e2->ts = e->ts;
175 e2->rank = e->rank;
176 e2->where = where;
177 e2->value.op.operator = INTRINSIC_PARENTHESES;
178 e2->value.op.op1 = e;
179 e2->value.op.op2 = NULL;
180 *result = e2;
183 if (m != MATCH_YES)
185 gfc_free_expr (*result);
186 return MATCH_ERROR;
189 return MATCH_YES;
191 syntax:
192 gfc_error (expression_syntax);
193 return MATCH_ERROR;
197 /* Build an operator expression node. */
199 static gfc_expr *
200 build_node (gfc_intrinsic_op operator, locus * where,
201 gfc_expr * op1, gfc_expr * op2)
203 gfc_expr *new;
205 new = gfc_get_expr ();
206 new->expr_type = EXPR_OP;
207 new->value.op.operator = operator;
208 new->where = *where;
210 new->value.op.op1 = op1;
211 new->value.op.op2 = op2;
213 return new;
217 /* Match a level 1 expression. */
219 static match
220 match_level_1 (gfc_expr ** result)
222 gfc_user_op *uop;
223 gfc_expr *e, *f;
224 locus where;
225 match m;
227 where = gfc_current_locus;
228 uop = NULL;
229 m = match_defined_operator (&uop);
230 if (m == MATCH_ERROR)
231 return m;
233 m = match_primary (&e);
234 if (m != MATCH_YES)
235 return m;
237 if (uop == NULL)
238 *result = e;
239 else
241 f = build_node (INTRINSIC_USER, &where, e, NULL);
242 f->value.op.uop = uop;
243 *result = f;
246 return MATCH_YES;
250 /* As a GNU extension we support an expanded level-2 expression syntax.
251 Via this extension we support (arbitrary) nesting of unary plus and
252 minus operations following unary and binary operators, such as **.
253 The grammar of section 7.1.1.3 is effectively rewitten as:
255 R704 mult-operand is level-1-expr [ power-op ext-mult-operand ]
256 R704' ext-mult-operand is add-op ext-mult-operand
257 or mult-operand
258 R705 add-operand is add-operand mult-op ext-mult-operand
259 or mult-operand
260 R705' ext-add-operand is add-op ext-add-operand
261 or add-operand
262 R706 level-2-expr is [ level-2-expr ] add-op ext-add-operand
263 or add-operand
266 static match match_ext_mult_operand (gfc_expr ** result);
267 static match match_ext_add_operand (gfc_expr ** result);
270 static int
271 match_add_op (void)
274 if (next_operator (INTRINSIC_MINUS))
275 return -1;
276 if (next_operator (INTRINSIC_PLUS))
277 return 1;
278 return 0;
282 static match
283 match_mult_operand (gfc_expr ** result)
285 gfc_expr *e, *exp, *r;
286 locus where;
287 match m;
289 m = match_level_1 (&e);
290 if (m != MATCH_YES)
291 return m;
293 if (!next_operator (INTRINSIC_POWER))
295 *result = e;
296 return MATCH_YES;
299 where = gfc_current_locus;
301 m = match_ext_mult_operand (&exp);
302 if (m == MATCH_NO)
303 gfc_error ("Expected exponent in expression at %C");
304 if (m != MATCH_YES)
306 gfc_free_expr (e);
307 return MATCH_ERROR;
310 r = gfc_power (e, exp);
311 if (r == NULL)
313 gfc_free_expr (e);
314 gfc_free_expr (exp);
315 return MATCH_ERROR;
318 r->where = where;
319 *result = r;
321 return MATCH_YES;
325 static match
326 match_ext_mult_operand (gfc_expr ** result)
328 gfc_expr *all, *e;
329 locus where;
330 match m;
331 int i;
333 where = gfc_current_locus;
334 i = match_add_op ();
336 if (i == 0)
337 return match_mult_operand (result);
339 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
340 " arithmetic operator (use parentheses) at %C")
341 == FAILURE)
342 return MATCH_ERROR;
344 m = match_ext_mult_operand (&e);
345 if (m != MATCH_YES)
346 return m;
348 if (i == -1)
349 all = gfc_uminus (e);
350 else
351 all = gfc_uplus (e);
353 if (all == NULL)
355 gfc_free_expr (e);
356 return MATCH_ERROR;
359 all->where = where;
360 *result = all;
361 return MATCH_YES;
365 static match
366 match_add_operand (gfc_expr ** result)
368 gfc_expr *all, *e, *total;
369 locus where, old_loc;
370 match m;
371 gfc_intrinsic_op i;
373 m = match_mult_operand (&all);
374 if (m != MATCH_YES)
375 return m;
377 for (;;)
379 /* Build up a string of products or quotients. */
381 old_loc = gfc_current_locus;
383 if (next_operator (INTRINSIC_TIMES))
384 i = INTRINSIC_TIMES;
385 else
387 if (next_operator (INTRINSIC_DIVIDE))
388 i = INTRINSIC_DIVIDE;
389 else
390 break;
393 where = gfc_current_locus;
395 m = match_ext_mult_operand (&e);
396 if (m == MATCH_NO)
398 gfc_current_locus = old_loc;
399 break;
402 if (m == MATCH_ERROR)
404 gfc_free_expr (all);
405 return MATCH_ERROR;
408 if (i == INTRINSIC_TIMES)
409 total = gfc_multiply (all, e);
410 else
411 total = gfc_divide (all, e);
413 if (total == NULL)
415 gfc_free_expr (all);
416 gfc_free_expr (e);
417 return MATCH_ERROR;
420 all = total;
421 all->where = where;
424 *result = all;
425 return MATCH_YES;
429 static match
430 match_ext_add_operand (gfc_expr ** result)
432 gfc_expr *all, *e;
433 locus where;
434 match m;
435 int i;
437 where = gfc_current_locus;
438 i = match_add_op ();
440 if (i == 0)
441 return match_add_operand (result);
443 if (gfc_notify_std (GFC_STD_GNU, "Extension: Unary operator following"
444 " arithmetic operator (use parentheses) at %C")
445 == FAILURE)
446 return MATCH_ERROR;
448 m = match_ext_add_operand (&e);
449 if (m != MATCH_YES)
450 return m;
452 if (i == -1)
453 all = gfc_uminus (e);
454 else
455 all = gfc_uplus (e);
457 if (all == NULL)
459 gfc_free_expr (e);
460 return MATCH_ERROR;
463 all->where = where;
464 *result = all;
465 return MATCH_YES;
469 /* Match a level 2 expression. */
471 static match
472 match_level_2 (gfc_expr ** result)
474 gfc_expr *all, *e, *total;
475 locus where;
476 match m;
477 int i;
479 where = gfc_current_locus;
480 i = match_add_op ();
482 if (i != 0)
484 m = match_ext_add_operand (&e);
485 if (m == MATCH_NO)
487 gfc_error (expression_syntax);
488 m = MATCH_ERROR;
491 else
492 m = match_add_operand (&e);
494 if (m != MATCH_YES)
495 return m;
497 if (i == 0)
498 all = e;
499 else
501 if (i == -1)
502 all = gfc_uminus (e);
503 else
504 all = gfc_uplus (e);
506 if (all == NULL)
508 gfc_free_expr (e);
509 return MATCH_ERROR;
513 all->where = where;
515 /* Append add-operands to the sum */
517 for (;;)
519 where = gfc_current_locus;
520 i = match_add_op ();
521 if (i == 0)
522 break;
524 m = match_ext_add_operand (&e);
525 if (m == MATCH_NO)
526 gfc_error (expression_syntax);
527 if (m != MATCH_YES)
529 gfc_free_expr (all);
530 return MATCH_ERROR;
533 if (i == -1)
534 total = gfc_subtract (all, e);
535 else
536 total = gfc_add (all, e);
538 if (total == NULL)
540 gfc_free_expr (all);
541 gfc_free_expr (e);
542 return MATCH_ERROR;
545 all = total;
546 all->where = where;
549 *result = all;
550 return MATCH_YES;
554 /* Match a level three expression. */
556 static match
557 match_level_3 (gfc_expr ** result)
559 gfc_expr *all, *e, *total;
560 locus where;
561 match m;
563 m = match_level_2 (&all);
564 if (m != MATCH_YES)
565 return m;
567 for (;;)
569 if (!next_operator (INTRINSIC_CONCAT))
570 break;
572 where = gfc_current_locus;
574 m = match_level_2 (&e);
575 if (m == MATCH_NO)
577 gfc_error (expression_syntax);
578 gfc_free_expr (all);
580 if (m != MATCH_YES)
581 return MATCH_ERROR;
583 total = gfc_concat (all, e);
584 if (total == NULL)
586 gfc_free_expr (all);
587 gfc_free_expr (e);
588 return MATCH_ERROR;
591 all = total;
592 all->where = where;
595 *result = all;
596 return MATCH_YES;
600 /* Match a level 4 expression. */
602 static match
603 match_level_4 (gfc_expr ** result)
605 gfc_expr *left, *right, *r;
606 gfc_intrinsic_op i;
607 locus old_loc;
608 locus where;
609 match m;
611 m = match_level_3 (&left);
612 if (m != MATCH_YES)
613 return m;
615 old_loc = gfc_current_locus;
617 if (gfc_match_intrinsic_op (&i) != MATCH_YES)
619 *result = left;
620 return MATCH_YES;
623 if (i != INTRINSIC_EQ && i != INTRINSIC_NE && i != INTRINSIC_GE
624 && i != INTRINSIC_LE && i != INTRINSIC_LT && i != INTRINSIC_GT)
626 gfc_current_locus = old_loc;
627 *result = left;
628 return MATCH_YES;
631 where = gfc_current_locus;
633 m = match_level_3 (&right);
634 if (m == MATCH_NO)
635 gfc_error (expression_syntax);
636 if (m != MATCH_YES)
638 gfc_free_expr (left);
639 return MATCH_ERROR;
642 switch (i)
644 case INTRINSIC_EQ:
645 r = gfc_eq (left, right);
646 break;
648 case INTRINSIC_NE:
649 r = gfc_ne (left, right);
650 break;
652 case INTRINSIC_LT:
653 r = gfc_lt (left, right);
654 break;
656 case INTRINSIC_LE:
657 r = gfc_le (left, right);
658 break;
660 case INTRINSIC_GT:
661 r = gfc_gt (left, right);
662 break;
664 case INTRINSIC_GE:
665 r = gfc_ge (left, right);
666 break;
668 default:
669 gfc_internal_error ("match_level_4(): Bad operator");
672 if (r == NULL)
674 gfc_free_expr (left);
675 gfc_free_expr (right);
676 return MATCH_ERROR;
679 r->where = where;
680 *result = r;
682 return MATCH_YES;
686 static match
687 match_and_operand (gfc_expr ** result)
689 gfc_expr *e, *r;
690 locus where;
691 match m;
692 int i;
694 i = next_operator (INTRINSIC_NOT);
695 where = gfc_current_locus;
697 m = match_level_4 (&e);
698 if (m != MATCH_YES)
699 return m;
701 r = e;
702 if (i)
704 r = gfc_not (e);
705 if (r == NULL)
707 gfc_free_expr (e);
708 return MATCH_ERROR;
712 r->where = where;
713 *result = r;
715 return MATCH_YES;
719 static match
720 match_or_operand (gfc_expr ** result)
722 gfc_expr *all, *e, *total;
723 locus where;
724 match m;
726 m = match_and_operand (&all);
727 if (m != MATCH_YES)
728 return m;
730 for (;;)
732 if (!next_operator (INTRINSIC_AND))
733 break;
734 where = gfc_current_locus;
736 m = match_and_operand (&e);
737 if (m == MATCH_NO)
738 gfc_error (expression_syntax);
739 if (m != MATCH_YES)
741 gfc_free_expr (all);
742 return MATCH_ERROR;
745 total = gfc_and (all, e);
746 if (total == NULL)
748 gfc_free_expr (all);
749 gfc_free_expr (e);
750 return MATCH_ERROR;
753 all = total;
754 all->where = where;
757 *result = all;
758 return MATCH_YES;
762 static match
763 match_equiv_operand (gfc_expr ** result)
765 gfc_expr *all, *e, *total;
766 locus where;
767 match m;
769 m = match_or_operand (&all);
770 if (m != MATCH_YES)
771 return m;
773 for (;;)
775 if (!next_operator (INTRINSIC_OR))
776 break;
777 where = gfc_current_locus;
779 m = match_or_operand (&e);
780 if (m == MATCH_NO)
781 gfc_error (expression_syntax);
782 if (m != MATCH_YES)
784 gfc_free_expr (all);
785 return MATCH_ERROR;
788 total = gfc_or (all, e);
789 if (total == NULL)
791 gfc_free_expr (all);
792 gfc_free_expr (e);
793 return MATCH_ERROR;
796 all = total;
797 all->where = where;
800 *result = all;
801 return MATCH_YES;
805 /* Match a level 5 expression. */
807 static match
808 match_level_5 (gfc_expr ** result)
810 gfc_expr *all, *e, *total;
811 locus where;
812 match m;
813 gfc_intrinsic_op i;
815 m = match_equiv_operand (&all);
816 if (m != MATCH_YES)
817 return m;
819 for (;;)
821 if (next_operator (INTRINSIC_EQV))
822 i = INTRINSIC_EQV;
823 else
825 if (next_operator (INTRINSIC_NEQV))
826 i = INTRINSIC_NEQV;
827 else
828 break;
831 where = gfc_current_locus;
833 m = match_equiv_operand (&e);
834 if (m == MATCH_NO)
835 gfc_error (expression_syntax);
836 if (m != MATCH_YES)
838 gfc_free_expr (all);
839 return MATCH_ERROR;
842 if (i == INTRINSIC_EQV)
843 total = gfc_eqv (all, e);
844 else
845 total = gfc_neqv (all, e);
847 if (total == NULL)
849 gfc_free_expr (all);
850 gfc_free_expr (e);
851 return MATCH_ERROR;
854 all = total;
855 all->where = where;
858 *result = all;
859 return MATCH_YES;
863 /* Match an expression. At this level, we are stringing together
864 level 5 expressions separated by binary operators. */
866 match
867 gfc_match_expr (gfc_expr ** result)
869 gfc_expr *all, *e;
870 gfc_user_op *uop;
871 locus where;
872 match m;
874 m = match_level_5 (&all);
875 if (m != MATCH_YES)
876 return m;
878 for (;;)
880 uop = NULL;
881 m = match_defined_operator (&uop);
882 if (m == MATCH_NO)
883 break;
884 if (m == MATCH_ERROR)
886 gfc_free_expr (all);
887 return MATCH_ERROR;
890 where = gfc_current_locus;
892 m = match_level_5 (&e);
893 if (m == MATCH_NO)
894 gfc_error (expression_syntax);
895 if (m != MATCH_YES)
897 gfc_free_expr (all);
898 return MATCH_ERROR;
901 all = build_node (INTRINSIC_USER, &where, all, e);
902 all->value.op.uop = uop;
905 *result = all;
906 return MATCH_YES;