Parse C++ bool, false and true.
authorChristoph Mallon <christoph.mallon@gmx.de>
Mon, 10 Nov 2008 09:23:31 +0000 (09:23 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Mon, 10 Nov 2008 09:23:31 +0000 (09:23 +0000)
[r23538]

ast.c
parser.c
tokens.inc
type.c
types.c
types.h

diff --git a/ast.c b/ast.c
index 5c70076..201039e 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -196,7 +196,9 @@ static void print_const(const const_expression_t *cnst)
 
        const type_t *const type = skip_typeref(cnst->base.type);
 
-       if (is_type_integer(type)) {
+       if (is_type_atomic(type, ATOMIC_TYPE_BOOL)) {
+               fputs(cnst->v.int_value ? "true" : "false", out);
+       } else if (is_type_integer(type)) {
                fprintf(out, "%lld", cnst->v.int_value);
        } else if (is_type_float(type)) {
                long double const val = cnst->v.float_value;
index 68b997c..3865da7 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -221,22 +221,23 @@ static void semantic_comparison(binary_expression_t *expression);
 #endif
 
 #define TYPE_SPECIFIERS       \
-       case T_void:              \
+       case T__Bool:             \
+       case T___builtin_va_list: \
+       case T___typeof__:        \
+       case T__declspec:         \
+       case T_bool:              \
        case T_char:              \
-       case T_short:             \
+       case T_double:            \
+       case T_enum:              \
+       case T_float:             \
        case T_int:               \
        case T_long:              \
-       case T_float:             \
-       case T_double:            \
+       case T_short:             \
        case T_signed:            \
-       case T_unsigned:          \
-       case T__Bool:             \
        case T_struct:            \
        case T_union:             \
-       case T_enum:              \
-       case T___typeof__:        \
-       case T___builtin_va_list: \
-       case T__declspec:         \
+       case T_unsigned:          \
+       case T_void:              \
        COMPLEX_SPECIFIERS        \
        IMAGINARY_SPECIFIERS
 
@@ -296,9 +297,11 @@ static void semantic_comparison(binary_expression_t *expression);
        case T___func__:                 \
        case T___noop:                   \
        case T__assume:                  \
-       case T_sizeof:                   \
        case T_delete:                   \
-       case T_throw:
+       case T_false:                    \
+       case T_sizeof:                   \
+       case T_throw:                    \
+       case T_true:
 
 /**
  * Allocate an AST node with given size and
@@ -3647,22 +3650,23 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                        }                                                           \
                        break
 
-               MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void");
-               MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char");
-               MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short");
-               MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int");
-               MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float");
-               MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double");
-               MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed");
-               MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned");
                MATCH_SPECIFIER(T__Bool,      SPECIFIER_BOOL,      "_Bool");
-               MATCH_SPECIFIER(T__int8,      SPECIFIER_INT8,      "_int8");
+               MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex");
+               MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
+               MATCH_SPECIFIER(T__int128,    SPECIFIER_INT128,    "_int128");
                MATCH_SPECIFIER(T__int16,     SPECIFIER_INT16,     "_int16");
                MATCH_SPECIFIER(T__int32,     SPECIFIER_INT32,     "_int32");
                MATCH_SPECIFIER(T__int64,     SPECIFIER_INT64,     "_int64");
-               MATCH_SPECIFIER(T__int128,    SPECIFIER_INT128,    "_int128");
-               MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex");
-               MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
+               MATCH_SPECIFIER(T__int8,      SPECIFIER_INT8,      "_int8");
+               MATCH_SPECIFIER(T_bool,       SPECIFIER_BOOL,      "bool");
+               MATCH_SPECIFIER(T_char,       SPECIFIER_CHAR,      "char");
+               MATCH_SPECIFIER(T_double,     SPECIFIER_DOUBLE,    "double");
+               MATCH_SPECIFIER(T_float,      SPECIFIER_FLOAT,     "float");
+               MATCH_SPECIFIER(T_int,        SPECIFIER_INT,       "int");
+               MATCH_SPECIFIER(T_short,      SPECIFIER_SHORT,     "short");
+               MATCH_SPECIFIER(T_signed,     SPECIFIER_SIGNED,    "signed");
+               MATCH_SPECIFIER(T_unsigned,   SPECIFIER_UNSIGNED,  "unsigned");
+               MATCH_SPECIFIER(T_void,       SPECIFIER_VOID,      "void");
 
                case T__forceinline:
                        /* only in microsoft mode */
@@ -6291,6 +6295,20 @@ static expression_t *parse_string_const(void)
        }
 }
 
+/**
+ * Parse a boolean constant.
+ */
+static expression_t *parse_bool_const(bool value)
+{
+       expression_t *cnst       = allocate_expression_zero(EXPR_CONST);
+       cnst->base.type          = type_bool;
+       cnst->conste.v.int_value = value;
+
+       next_token();
+
+       return cnst;
+}
+
 /**
  * Parse an integer constant.
  */
@@ -7325,6 +7343,8 @@ end_error:
 static expression_t *parse_primary_expression(void)
 {
        switch (token.type) {
+               case T_false:                    return parse_bool_const(false);
+               case T_true:                     return parse_bool_const(true);
                case T_INTEGER:                  return parse_int_const();
                case T_CHARACTER_CONSTANT:       return parse_character_constant();
                case T_WIDE_CHARACTER_CONSTANT:  return parse_wide_character_constant();
index e649437..11f4a7f 100644 (file)
@@ -58,7 +58,9 @@ S(_CXX, delete)
 S(_CXX, dynamic_cast)
 S(_CXX, explicit)
 S(_CXX, export)
+#undef false
 S(_CXX, false)
+#define false 0
 S(_CXX, friend)
 S(_CXX, mutable)
 S(_CXX, namespace)
@@ -72,7 +74,9 @@ S(_CXX, static_cast)
 S(_CXX, template)
 S(_CXX, this)
 S(_CXX, throw)
+#undef true
 S(_CXX, true)
+#define true 1
 S(_CXX, try)
 S(_CXX, typeid)
 S(_CXX, typename)
diff --git a/type.c b/type.c
index 9ea8d41..e3a2941 100644 (file)
--- a/type.c
+++ b/type.c
@@ -221,23 +221,23 @@ void print_atomic_kinds(atomic_type_kind_t kind)
 {
        const char *s = "INVALIDATOMIC";
        switch(kind) {
-       case ATOMIC_TYPE_INVALID:                               break;
-       case ATOMIC_TYPE_VOID:        s = "void";               break;
-       case ATOMIC_TYPE_BOOL:        s = "_Bool";              break;
-       case ATOMIC_TYPE_CHAR:        s = "char";               break;
-       case ATOMIC_TYPE_SCHAR:       s = "signed char";        break;
-       case ATOMIC_TYPE_UCHAR:       s = "unsigned char";      break;
-       case ATOMIC_TYPE_INT:         s = "int";                break;
-       case ATOMIC_TYPE_UINT:        s = "unsigned int";       break;
-       case ATOMIC_TYPE_SHORT:       s = "short";              break;
-       case ATOMIC_TYPE_USHORT:      s = "unsigned short";     break;
-       case ATOMIC_TYPE_LONG:        s = "long";               break;
-       case ATOMIC_TYPE_ULONG:       s = "unsigned long";      break;
-       case ATOMIC_TYPE_LONGLONG:    s = "long long";          break;
-       case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long"; break;
-       case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";        break;
-       case ATOMIC_TYPE_FLOAT:       s = "float";              break;
-       case ATOMIC_TYPE_DOUBLE:      s = "double";             break;
+       case ATOMIC_TYPE_INVALID:                                           break;
+       case ATOMIC_TYPE_VOID:        s = "void";                           break;
+       case ATOMIC_TYPE_BOOL:        s = c_mode & _CXX ? "bool" : "_Bool"; break;
+       case ATOMIC_TYPE_CHAR:        s = "char";                           break;
+       case ATOMIC_TYPE_SCHAR:       s = "signed char";                    break;
+       case ATOMIC_TYPE_UCHAR:       s = "unsigned char";                  break;
+       case ATOMIC_TYPE_INT:         s = "int";                            break;
+       case ATOMIC_TYPE_UINT:        s = "unsigned int";                   break;
+       case ATOMIC_TYPE_SHORT:       s = "short";                          break;
+       case ATOMIC_TYPE_USHORT:      s = "unsigned short";                 break;
+       case ATOMIC_TYPE_LONG:        s = "long";                           break;
+       case ATOMIC_TYPE_ULONG:       s = "unsigned long";                  break;
+       case ATOMIC_TYPE_LONGLONG:    s = "long long";                      break;
+       case ATOMIC_TYPE_ULONGLONG:   s = "unsigned long long";             break;
+       case ATOMIC_TYPE_LONG_DOUBLE: s = "long double";                    break;
+       case ATOMIC_TYPE_FLOAT:       s = "float";                          break;
+       case ATOMIC_TYPE_DOUBLE:      s = "double";                         break;
        }
        fputs(s, out);
 }
diff --git a/types.c b/types.c
index 578b342..1f34936 100644 (file)
--- a/types.c
+++ b/types.c
@@ -24,6 +24,7 @@
 /** The error type. */
 type_t *type_error_type;
 
+type_t *type_bool;
 type_t *type_char;
 type_t *type_const_char;
 type_t *type_double;
@@ -95,6 +96,7 @@ void init_basic_types(void)
                                           TYPE_MODIFIER_NONE, 0, NULL };
 
        type_error_type         = (type_t*)&error;
+       type_bool               = make_atomic_type(ATOMIC_TYPE_BOOL,        TYPE_QUALIFIER_NONE);
        type_signed_char        = make_atomic_type(ATOMIC_TYPE_SCHAR,       TYPE_QUALIFIER_NONE);
        type_short              = make_atomic_type(ATOMIC_TYPE_SHORT,       TYPE_QUALIFIER_NONE);
        type_unsigned_short     = make_atomic_type(ATOMIC_TYPE_USHORT,      TYPE_QUALIFIER_NONE);
diff --git a/types.h b/types.h
index b7ebfd3..622cd1e 100644 (file)
--- a/types.h
+++ b/types.h
@@ -24,6 +24,7 @@
 
 extern type_t *type_error_type;
 
+extern type_t *type_bool;
 extern type_t *type_char;
 extern type_t *type_const_char;
 extern type_t *type_double;