cleanup: Add and use macro MIN().
[cparser] / ast.c
diff --git a/ast.c b/ast.c
index 3f258cb..baa472d 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>
 
 #include "type_t.h"
 #include "parser.h"
 #include "lang_features.h"
-#include "entity_t.h"
 #include "printer.h"
 #include "separator_t.h"
 #include "types.h"
 
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <ctype.h>
 
-#if defined(__INTEL_COMPILER)
-#include <mathimf.h>
-#elif defined(__CYGWIN__)
-#include "win32/cygwin_math_ext.h"
-#else
-#include <math.h>
-#endif
-
 #include "adt/error.h"
 #include "adt/util.h"
 
@@ -124,7 +97,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 +110,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 +331,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 +362,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 +572,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('.');
@@ -1005,18 +985,17 @@ static void print_for_statement(const for_statement_t *statement)
  *
  * @param arguments   the arguments
  */
-static void print_asm_arguments(asm_argument_t *arguments)
+static void print_asm_arguments(asm_argument_t const *const arguments)
 {
-       separator_t     sep      = { "", ", " };
-       asm_argument_t *argument = arguments;
-       for (; argument != NULL; argument = argument->next) {
+       print_string(" :");
+       separator_t sep = { " ", ", " };
+       for (asm_argument_t const *i = arguments; i; i = i->next) {
                print_string(sep_next(&sep));
-               if (argument->symbol) {
-                       print_format("[%s] ", argument->symbol->string);
-               }
-               print_quoted_string(&argument->constraints, '"');
+               if (i->symbol)
+                       print_format("[%s] ", i->symbol->string);
+               print_quoted_string(&i->constraints, '"');
                print_string(" (");
-               print_expression(argument->expression);
+               print_expression(i->expression);
                print_char(')');
        }
 }
@@ -1026,48 +1005,50 @@ static void print_asm_arguments(asm_argument_t *arguments)
  *
  * @param clobbers   the clobbers
  */
-static void print_asm_clobbers(asm_clobber_t *clobbers)
+static void print_asm_clobbers(asm_clobber_t const *const clobbers)
+{
+       print_string(" :");
+       separator_t sep = { " ", ", " };
+       for (asm_clobber_t const *i = clobbers; i; i = i->next) {
+               print_string(sep_next(&sep));
+               print_quoted_string(&i->clobber, '"');
+       }
+}
+
+static void print_asm_labels(asm_label_t const *const labels)
 {
-       separator_t    sep     = { "", ", " };
-       asm_clobber_t *clobber = clobbers;
-       for (; clobber != NULL; clobber = clobber->next) {
+       print_string(" :");
+       separator_t sep = { " ", ", " };
+       for (asm_label_t const *i = labels; i; i = i->next) {
                print_string(sep_next(&sep));
-               print_quoted_string(&clobber->clobber, '"');
+               print_string(i->label->base.symbol->string);
        }
 }
 
 /**
  * Print an assembler statement.
  *
- * @param statement   the statement
+ * @param stmt   the statement
  */
-static void print_asm_statement(const asm_statement_t *statement)
+static void print_asm_statement(asm_statement_t const *const stmt)
 {
-       print_string("asm ");
-       if (statement->is_volatile) {
-               print_string("volatile ");
-       }
+       print_string("asm");
+       if (stmt->is_volatile) print_string(" volatile");
+       if (stmt->labels)      print_string(" goto");
        print_char('(');
-       print_quoted_string(&statement->asm_text, '"');
-       if (statement->outputs  == NULL &&
-           statement->inputs   == NULL &&
-           statement->clobbers == NULL)
-               goto end_of_print_asm_statement;
-
-       print_string(" : ");
-       print_asm_arguments(statement->outputs);
-       if (statement->inputs == NULL && statement->clobbers == NULL)
-               goto end_of_print_asm_statement;
-
-       print_string(" : ");
-       print_asm_arguments(statement->inputs);
-       if (statement->clobbers == NULL)
-               goto end_of_print_asm_statement;
-
-       print_string(" : ");
-       print_asm_clobbers(statement->clobbers);
-
-end_of_print_asm_statement:
+       print_quoted_string(&stmt->asm_text, '"');
+
+       unsigned const n =
+               stmt->labels   ? 4 :
+               stmt->clobbers ? 3 :
+               stmt->inputs   ? 2 :
+               stmt->outputs  ? 1 :
+               0;
+       if (n >= 1) print_asm_arguments(stmt->outputs);
+       if (n >= 2) print_asm_arguments(stmt->inputs);
+       if (n >= 3) print_asm_clobbers( stmt->clobbers);
+       if (n >= 4) print_asm_labels(   stmt->labels);
+
        print_string(");");
 }
 
@@ -1473,9 +1454,7 @@ expression_classification_t is_constant_initializer(const initializer_t *initial
                for (size_t i = 0; i < initializer->list.len; ++i) {
                        initializer_t *sub_initializer = initializer->list.initializers[i];
                        expression_classification_t const cur = is_constant_initializer(sub_initializer);
-                       if (all > cur) {
-                               all = cur;
-                       }
+                       all = MIN(all, cur);
                }
                return all;
        }
@@ -1497,6 +1476,14 @@ static expression_classification_t is_object_with_linker_constant_address(
        case EXPR_UNARY_DEREFERENCE:
                return is_linker_constant(expression->unary.value);
 
+       case EXPR_COMPOUND_LITERAL: {
+               const compound_literal_expression_t *literal
+                       = &expression->compound_literal;
+               return literal->global_scope ||
+                      ((literal->type->base.qualifiers & TYPE_QUALIFIER_CONST)
+                       && is_constant_initializer(literal->initializer));
+       }
+
        case EXPR_SELECT: {
                type_t *base_type = skip_typeref(expression->select.compound->base.type);
                if (is_type_pointer(base_type)) {
@@ -1510,7 +1497,7 @@ static expression_classification_t is_object_with_linker_constant_address(
        case EXPR_ARRAY_ACCESS: {
                expression_classification_t const ref = is_linker_constant(expression->array_access.array_ref);
                expression_classification_t const idx = is_constant_expression(expression->array_access.index);
-               return ref < idx ? ref : idx;
+               return MIN(ref, idx);
        }
 
        case EXPR_REFERENCE: {
@@ -1589,11 +1576,11 @@ expression_classification_t is_linker_constant(const expression_t *expression)
                if (is_type_pointer(ltype)) {
                        expression_classification_t const l = is_linker_constant(left);
                        expression_classification_t const r = is_constant_expression(right);
-                       return l < r ? l : r;
+                       return MIN(l, r);
                } else if (is_type_pointer(rtype)) {
                        expression_classification_t const l = is_constant_expression(left);
                        expression_classification_t const r = is_linker_constant(right);
-                       return l < r ? l : r;
+                       return MIN(l, r);
                } else if (!is_type_valid(ltype) || !is_type_valid(rtype)) {
                        return EXPR_CLASS_ERROR;
                } else {
@@ -1629,7 +1616,7 @@ expression_classification_t is_linker_constant(const expression_t *expression)
        case EXPR_CONDITIONAL: {
                expression_t *const c = expression->conditional.condition;
                expression_classification_t const cclass = is_constant_expression(c);
-               if (cclass != EXPR_CLASS_CONSTANT)
+               if (cclass < EXPR_CLASS_CONSTANT)
                        return cclass;
 
                if (fold_constant_to_bool(c)) {
@@ -1719,11 +1706,11 @@ static expression_classification_t is_object_with_constant_address(const express
                array_access_expression_t const* const array_access =
                        &expression->array_access;
                expression_classification_t const idx_class = is_constant_expression(array_access->index);
-               if (idx_class != EXPR_CLASS_CONSTANT)
+               if (idx_class < EXPR_CLASS_CONSTANT)
                        return idx_class;
                expression_classification_t const ref_addr = is_object_with_constant_address(array_access->array_ref);
                expression_classification_t const ref_ptr  = is_constant_pointer(array_access->array_ref);
-               return ref_addr > ref_ptr ? ref_addr : ref_ptr;
+               return MAX(ref_addr, ref_ptr);
        }
 
        case EXPR_UNARY_DEREFERENCE:
@@ -1745,7 +1732,7 @@ expression_classification_t is_constant_expression(const expression_t *expressio
        case EXPR_ENUM_CONSTANT:
        case EXPR_LITERAL_BOOLEAN:
        case EXPR_LITERAL_MS_NOOP:
-               return EXPR_CLASS_CONSTANT;
+               return EXPR_CLASS_INTEGER_CONSTANT;
 
        {
                type_t *type;
@@ -1758,7 +1745,6 @@ expression_classification_t is_constant_expression(const expression_t *expressio
                goto check_type;
 
        case EXPR_LITERAL_INTEGER:
-       case EXPR_LITERAL_FLOATINGPOINT:
                type = skip_typeref(expression->base.type);
                goto check_type;
 
@@ -1773,12 +1759,17 @@ expression_classification_t is_constant_expression(const expression_t *expressio
                goto check_type;
 
 check_type:
+               return is_type_valid(type) ? EXPR_CLASS_INTEGER_CONSTANT : EXPR_CLASS_ERROR;
+       }
+
+       case EXPR_LITERAL_FLOATINGPOINT: {
+               type_t *const type = skip_typeref(expression->base.type);
                return is_type_valid(type) ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
        }
 
        case EXPR_BUILTIN_CONSTANT_P: {
                expression_classification_t const c = is_constant_expression(expression->builtin_constant.value);
-               return c != EXPR_CLASS_ERROR ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
+               return c != EXPR_CLASS_ERROR ? EXPR_CLASS_INTEGER_CONSTANT : EXPR_CLASS_ERROR;
        }
 
        case EXPR_STRING_LITERAL:
@@ -1826,12 +1817,28 @@ 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_integer(type)) {
+                       expression_t *const val = expression->unary.value;
+                       if (is_type_arithmetic(skip_typeref(val->base.type))) {
+                               return val->kind == EXPR_LITERAL_FLOATINGPOINT
+                                       ? EXPR_CLASS_INTEGER_CONSTANT
+                                       : is_constant_expression(val);
+                       }
+               }
                if (is_type_scalar(type))
                        return is_constant_expression(expression->unary.value);
                if (!is_type_valid(type))
@@ -1863,44 +1870,52 @@ check_type:
        case EXPR_BINARY_ISUNORDERED: {
                expression_classification_t const l = is_constant_expression(expression->binary.left);
                expression_classification_t const r = is_constant_expression(expression->binary.right);
-               return l < r ? l : r;
+               return MIN(l, r);
        }
 
        case EXPR_BINARY_LOGICAL_AND: {
-               expression_t const         *const left   = expression->binary.left;
-               expression_classification_t const lclass = is_constant_expression(left);
-               if (lclass != EXPR_CLASS_CONSTANT)
-                       return lclass;
+               expression_t const         *const left = expression->binary.left;
+               expression_classification_t const lcls = is_constant_expression(left);
+               if (lcls < EXPR_CLASS_CONSTANT)
+                       return lcls;
+               expression_classification_t const rcls = is_constant_expression(expression->binary.right);
+               if (lcls == EXPR_CLASS_INTEGER_CONSTANT && rcls == EXPR_CLASS_INTEGER_CONSTANT)
+                       return EXPR_CLASS_INTEGER_CONSTANT;
                if (!fold_constant_to_bool(left))
                        return EXPR_CLASS_CONSTANT;
-               return is_constant_expression(expression->binary.right);
+               return MIN(rcls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_BINARY_LOGICAL_OR: {
-               expression_t const         *const left   = expression->binary.left;
-               expression_classification_t const lclass = is_constant_expression(left);
-               if (lclass != EXPR_CLASS_CONSTANT)
-                       return lclass;
+               expression_t const         *const left = expression->binary.left;
+               expression_classification_t const lcls = is_constant_expression(left);
+               if (lcls < EXPR_CLASS_CONSTANT)
+                       return lcls;
+               expression_classification_t const rcls = is_constant_expression(expression->binary.right);
+               if (lcls == EXPR_CLASS_INTEGER_CONSTANT && rcls == EXPR_CLASS_INTEGER_CONSTANT)
+                       return EXPR_CLASS_INTEGER_CONSTANT;
                if (fold_constant_to_bool(left))
                        return EXPR_CLASS_CONSTANT;
-               return is_constant_expression(expression->binary.right);
+               return MIN(rcls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_COMPOUND_LITERAL:
                return is_constant_initializer(expression->compound_literal.initializer);
 
        case EXPR_CONDITIONAL: {
-               expression_t               *const condition = expression->conditional.condition;
-               expression_classification_t const cclass    = is_constant_expression(condition);
-               if (cclass != EXPR_CLASS_CONSTANT)
-                       return cclass;
-
-               if (fold_constant_to_bool(condition)) {
-                       expression_t const *const t = expression->conditional.true_expression;
-                       return t == NULL ? EXPR_CLASS_CONSTANT : is_constant_expression(t);
-               } else {
-                       return is_constant_expression(expression->conditional.false_expression);
-               }
+               expression_t               *const cond = expression->conditional.condition;
+               expression_classification_t const ccls = is_constant_expression(cond);
+               if (ccls < EXPR_CLASS_CONSTANT)
+                       return ccls;
+               expression_t         const *const t    = expression->conditional.true_expression;
+               expression_classification_t const tcls = t == NULL ? ccls : is_constant_expression(t);
+               expression_classification_t const fcls = is_constant_expression(expression->conditional.false_expression);
+               if (ccls == EXPR_CLASS_INTEGER_CONSTANT &&
+                   tcls == EXPR_CLASS_INTEGER_CONSTANT &&
+                   fcls == EXPR_CLASS_INTEGER_CONSTANT)
+                       return EXPR_CLASS_INTEGER_CONSTANT;
+               expression_classification_t const cls = fold_constant_to_bool(cond) ? tcls : fcls;
+               return MIN(cls, EXPR_CLASS_CONSTANT);
        }
 
        case EXPR_ERROR: