ast2firm: Implement casting from complex to real types.
[cparser] / ast.c
diff --git a/ast.c b/ast.c
index d08da8c..f852a9d 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1,21 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
  */
 #include <config.h>
 
@@ -124,7 +109,7 @@ static unsigned get_expression_precedence(expression_kind_t kind)
 
                [EXPR_UNARY_NEGATE]               = PREC_UNARY,
                [EXPR_UNARY_PLUS]                 = PREC_UNARY,
-               [EXPR_UNARY_BITWISE_NEGATE]       = PREC_UNARY,
+               [EXPR_UNARY_COMPLEMENT]           = PREC_UNARY,
                [EXPR_UNARY_NOT]                  = PREC_UNARY,
                [EXPR_UNARY_DEREFERENCE]          = PREC_UNARY,
                [EXPR_UNARY_TAKE_ADDRESS]         = PREC_UNARY,
@@ -137,6 +122,8 @@ static unsigned get_expression_precedence(expression_kind_t kind)
                [EXPR_UNARY_DELETE]               = PREC_UNARY,
                [EXPR_UNARY_DELETE_ARRAY]         = PREC_UNARY,
                [EXPR_UNARY_THROW]                = PREC_ASSIGNMENT,
+               [EXPR_UNARY_IMAG]                 = PREC_UNARY,
+               [EXPR_UNARY_REAL]                 = PREC_UNARY,
 
                [EXPR_BINARY_ADD]                 = PREC_ADDITIVE,
                [EXPR_BINARY_SUB]                 = PREC_ADDITIVE,
@@ -356,16 +343,18 @@ static void print_unary_expression(const unary_expression_t *unexpr)
 {
        unsigned prec = get_expression_precedence(unexpr->base.kind);
        switch (unexpr->base.kind) {
-       case EXPR_UNARY_NEGATE:           print_char  ('-' ); break;
-       case EXPR_UNARY_PLUS:             print_char  ('+' ); break;
-       case EXPR_UNARY_NOT:              print_char  ('!' ); break;
-       case EXPR_UNARY_BITWISE_NEGATE:   print_char  ('~' ); break;
-       case EXPR_UNARY_PREFIX_INCREMENT: print_string("++"); break;
-       case EXPR_UNARY_PREFIX_DECREMENT: print_string("--"); break;
-       case EXPR_UNARY_DEREFERENCE:      print_char  ('*' ); break;
-       case EXPR_UNARY_TAKE_ADDRESS:     print_char  ('&' ); break;
-       case EXPR_UNARY_DELETE:           print_string("delete "); break;
+       case EXPR_UNARY_NEGATE:           print_char  ('-' );         break;
+       case EXPR_UNARY_PLUS:             print_char  ('+' );         break;
+       case EXPR_UNARY_NOT:              print_char  ('!' );         break;
+       case EXPR_UNARY_COMPLEMENT:       print_char  ('~' );         break;
+       case EXPR_UNARY_PREFIX_INCREMENT: print_string("++");         break;
+       case EXPR_UNARY_PREFIX_DECREMENT: print_string("--");         break;
+       case EXPR_UNARY_DEREFERENCE:      print_char  ('*' );         break;
+       case EXPR_UNARY_TAKE_ADDRESS:     print_char  ('&' );         break;
+       case EXPR_UNARY_DELETE:           print_string("delete ");    break;
        case EXPR_UNARY_DELETE_ARRAY:     print_string("delete [] "); break;
+       case EXPR_UNARY_REAL:             print_string("__real__ ");  break;
+       case EXPR_UNARY_IMAG:             print_string("__imag__ ");  break;
 
        case EXPR_UNARY_POSTFIX_INCREMENT:
                print_expression_prec(unexpr->value, prec);
@@ -385,7 +374,6 @@ static void print_unary_expression(const unary_expression_t *unexpr)
                print_assignment_expression(unexpr->value);
                print_char(')');
                return;
-
        case EXPR_UNARY_THROW:
                if (unexpr->value == NULL) {
                        print_string("throw");
@@ -596,6 +584,10 @@ static void print_designator(const designator_t *designator)
                if (designator->symbol == NULL) {
                        print_char('[');
                        print_expression(designator->array_index);
+                       if (designator->range_last) {
+                               print_string(" ... ");
+                               print_expression(designator->range_last);
+                       }
                        print_char(']');
                } else {
                        print_char('.');
@@ -1835,10 +1827,18 @@ check_type:
 
        case EXPR_UNARY_NEGATE:
        case EXPR_UNARY_PLUS:
-       case EXPR_UNARY_BITWISE_NEGATE:
+       case EXPR_UNARY_COMPLEMENT:
        case EXPR_UNARY_NOT:
                return is_constant_expression(expression->unary.value);
 
+       case EXPR_UNARY_IMAG:
+       case EXPR_UNARY_REAL: {
+               type_t *type = skip_typeref(expression->base.type);
+               if (!is_type_valid(type))
+                       return EXPR_CLASS_ERROR;
+               return is_constant_expression(expression->unary.value);
+       }
+
        case EXPR_UNARY_CAST: {
                type_t *const type = skip_typeref(expression->base.type);
                if (is_type_scalar(type))