K&R style parameter lists are not a prototype...
[cparser] / parser.c
index d83c261..3ca86ca 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -366,6 +366,8 @@ static size_t get_type_struct_size(type_kind_t kind)
 {
        static const size_t sizes[] = {
                [TYPE_ATOMIC]          = sizeof(atomic_type_t),
+               [TYPE_COMPLEX]         = sizeof(complex_type_t),
+               [TYPE_IMAGINARY]       = sizeof(imaginary_type_t),
                [TYPE_BITFIELD]        = sizeof(bitfield_type_t),
                [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
                [TYPE_COMPOUND_UNION]  = sizeof(compound_type_t),
@@ -488,6 +490,18 @@ static void add_anchor_token(int token_type) {
        ++token_anchor_set[token_type];
 }
 
+static int save_and_reset_anchor_state(int token_type) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       int count = token_anchor_set[token_type];
+       token_anchor_set[token_type] = 0;
+       return count;
+}
+
+static void restore_anchor_state(int token_type, int count) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       token_anchor_set[token_type] = count;
+}
+
 /**
  * Remove a token from the token anchor set (a multi-set).
  */
@@ -580,7 +594,10 @@ static void eat_statement(void) {
 /**
  * Report a parse error because an expected token was not found.
  */
-static __attribute__((sentinel))
+static
+#if defined __GNUC__ && __GNUC__ >= 4
+__attribute__((sentinel))
+#endif
 void parse_error_expected(const char *message, ...)
 {
        if(message != NULL) {
@@ -622,6 +639,8 @@ static void type_error_incompatible(const char *msg,
         parse_error_expected(NULL, (expected), NULL); \
                add_anchor_token(expected);                   \
         eat_until_anchor();                           \
+        if (token.type == expected)                   \
+               next_token();                             \
                rem_anchor_token(expected);                   \
         goto end_error;                               \
     }                                                 \
@@ -1043,6 +1062,7 @@ static const char *gnu_attribute_names[GNU_AK_LAST] = {
        [GNU_AK_TLS_MODEL]              = "tls_model",
        [GNU_AK_VISIBILITY]             = "visibility",
        [GNU_AK_REGPARM]                = "regparm",
+       [GNU_AK_MODE]                   = "mode",
        [GNU_AK_MODEL]                  = "model",
        [GNU_AK_TRAP_EXIT]              = "trap_exit",
        [GNU_AK_SP_SWITCH]              = "sp_switch",
@@ -1207,6 +1227,21 @@ static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
        attribute->invalid = true;
 }
 
+static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
+{
+       /* TODO: find out what is allowed here... */
+
+       /* at least: byte, word, pointer, list of machine modes
+        * __XXX___ is interpreted as XXX */
+       add_anchor_token(')');
+       expect(T_IDENTIFIER);
+       rem_anchor_token(')');
+       expect(')');
+       return;
+end_error:
+       attribute->invalid = true;
+}
+
 /**
  * parse one interrupt argument.
  */
@@ -1531,8 +1566,18 @@ static void parse_gnu_attribute(gnu_attribute_t **attributes)
                                        if(!attribute->have_arguments) {
                                                /* should have arguments */
                                                errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
-                                       } else
+                                       } else {
                                                parse_gnu_attribute_model_arg(attribute);
+                                       }
+                                       break;
+                               case GNU_AK_MODE:
+                                       if(!attribute->have_arguments) {
+                                               /* should have arguments */
+                                               errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
+                                       } else {
+                                               parse_gnu_attribute_mode_arg(attribute);
+                                       }
+                                       break;
                                case GNU_AK_INTERRUPT:
                                        /* may have one string argument */
                                        if(attribute->have_arguments)
@@ -2071,7 +2116,7 @@ static void skip_until(int type) {
 }
 
 /**
- * skip any {...} blocks until a closing braket is reached.
+ * skip any {...} blocks until a closing bracket is reached.
  */
 static void skip_initializers(void)
 {
@@ -2411,7 +2456,7 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
                        (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
                declaration->source_position = token.source_position;
                declaration->symbol          = symbol;
-               declaration->parent_scope  = scope;
+               declaration->parent_scope    = scope;
                if (symbol != NULL) {
                        environment_push(declaration);
                }
@@ -2419,13 +2464,13 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
        }
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if (declaration->init.complete) {
                        assert(symbol != NULL);
                        errorf(HERE, "multiple definitions of '%s %Y'",
                               is_struct ? "struct" : "union", symbol);
                        declaration->scope.declarations = NULL;
                }
-               declaration->init.is_defined = true;
+               declaration->init.complete = true;
 
                parse_compound_type_entries(declaration);
                parse_attributes(&attributes);
@@ -2517,14 +2562,14 @@ static type_t *parse_enum_specifier(void)
        type->enumt.declaration = declaration;
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if(declaration->init.complete) {
                        errorf(HERE, "multiple definitions of enum %Y", symbol);
                }
                if (symbol != NULL) {
                        environment_push(declaration);
                }
                append_declaration(declaration);
-               declaration->init.is_defined = 1;
+               declaration->init.complete = true;
 
                parse_enum_entries(type);
                parse_attributes(&attributes);
@@ -2614,10 +2659,8 @@ typedef enum {
        SPECIFIER_INT32     = 1 << 13,
        SPECIFIER_INT64     = 1 << 14,
        SPECIFIER_INT128    = 1 << 15,
-#ifdef PROVIDE_COMPLEX
        SPECIFIER_COMPLEX   = 1 << 16,
        SPECIFIER_IMAGINARY = 1 << 17,
-#endif
 } specifiers_t;
 
 static type_t *create_builtin_type(symbol_t *const symbol,
@@ -2628,7 +2671,7 @@ static type_t *create_builtin_type(symbol_t *const symbol,
        type->builtin.real_type = real_type;
 
        type_t *result = typehash_insert(type);
-       if (type != result) {
+       if(type != result) {
                free_type(type);
        }
 
@@ -2638,8 +2681,8 @@ static type_t *create_builtin_type(symbol_t *const symbol,
 static type_t *get_typedef_type(symbol_t *symbol)
 {
        declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
-       if(declaration == NULL
-                       || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
+       if(declaration == NULL ||
+          declaration->storage_class != STORAGE_CLASS_TYPEDEF)
                return NULL;
 
        type_t *type               = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
@@ -2757,7 +2800,7 @@ static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *spe
                                        }
                                }
                                next_token();
-                           if(token.type == ',') {
+                               if(token.type == ',') {
                                        next_token();
                                        continue;
                                }
@@ -2910,10 +2953,9 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                MATCH_SPECIFIER(T__int32,     SPECIFIER_INT32,     "_int32")
                MATCH_SPECIFIER(T__int64,     SPECIFIER_INT64,     "_int64")
                MATCH_SPECIFIER(T__int128,    SPECIFIER_INT128,    "_int128")
-#ifdef PROVIDE_COMPLEX
                MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
                MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
-#endif
+
                case T__forceinline:
                        /* only in microsoft mode */
                        specifiers->decl_modifiers |= DM_FORCEINLINE;
@@ -3100,26 +3142,18 @@ finish_specifiers:
                case SPECIFIER_BOOL:
                        atomic_type = ATOMIC_TYPE_BOOL;
                        break;
-#ifdef PROVIDE_COMPLEX
                case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
-                       break;
-               case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
-                       break;
-               case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
-                       break;
                case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_FLOAT;
                        break;
+               case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
                case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_DOUBLE;
                        break;
+               case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
                case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
                        break;
-#endif
                default:
                        /* invalid specifier combination, give an error message */
                        if(type_specifiers == 0) {
@@ -3143,9 +3177,19 @@ finish_specifiers:
                        atomic_type = ATOMIC_TYPE_INVALID;
                }
 
-               type               = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
-               type->atomic.akind = atomic_type;
-               newtype            = 1;
+               if(type_specifiers & SPECIFIER_COMPLEX &&
+                  atomic_type != ATOMIC_TYPE_INVALID) {
+                       type                = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
+                       type->complex.akind = atomic_type;
+               } else if(type_specifiers & SPECIFIER_IMAGINARY &&
+                         atomic_type != ATOMIC_TYPE_INVALID) {
+                       type                  = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
+                       type->imaginary.akind = atomic_type;
+               } else {
+                       type               = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
+                       type->atomic.akind = atomic_type;
+               }
+               newtype = 1;
        } else {
                if(type_specifiers != 0) {
                        errorf(HERE, "multiple datatypes in declaration");
@@ -3206,8 +3250,9 @@ static declaration_t *parse_identifier_list(void)
                }
                last_declaration = declaration;
 
-               if(token.type != ',')
+               if (token.type != ',') {
                        break;
+               }
                next_token();
        } while(token.type == T_IDENTIFIER);
 
@@ -3260,24 +3305,30 @@ static declaration_t *parse_parameter(void)
 
 static declaration_t *parse_parameters(function_type_t *type)
 {
+       declaration_t *declarations = NULL;
+
+       eat('(');
+       add_anchor_token(')');
+       int saved_comma_state = save_and_reset_anchor_state(',');
+
        if(token.type == T_IDENTIFIER) {
                symbol_t *symbol = token.v.symbol;
                if(!is_typedef_symbol(symbol)) {
                        type->kr_style_parameters = true;
-                       return parse_identifier_list();
+                       declarations = parse_identifier_list();
+                       goto parameters_finished;
                }
        }
 
        if(token.type == ')') {
                type->unspecified_parameters = 1;
-               return NULL;
+               goto parameters_finished;
        }
        if(token.type == T_void && look_ahead(1)->type == ')') {
                next_token();
-               return NULL;
+               goto parameters_finished;
        }
 
-       declaration_t        *declarations = NULL;
        declaration_t        *declaration;
        declaration_t        *last_declaration = NULL;
        function_parameter_t *parameter;
@@ -3288,7 +3339,7 @@ static declaration_t *parse_parameters(function_type_t *type)
                case T_DOTDOTDOT:
                        next_token();
                        type->variadic = 1;
-                       return declarations;
+                       goto parameters_finished;
 
                case T_IDENTIFIER:
                case T___extension__:
@@ -3311,12 +3362,25 @@ static declaration_t *parse_parameters(function_type_t *type)
                        break;
 
                default:
-                       return declarations;
+                       goto parameters_finished;
+               }
+               if (token.type != ',') {
+                       goto parameters_finished;
                }
-               if(token.type != ',')
-                       return declarations;
                next_token();
        }
+
+
+parameters_finished:
+       rem_anchor_token(')');
+       expect(')');
+
+       restore_anchor_state(',', saved_comma_state);
+       return declarations;
+
+end_error:
+       restore_anchor_state(',', saved_comma_state);
+       return NULL;
 }
 
 typedef enum {
@@ -3411,9 +3475,6 @@ end_error:
 
 static construct_type_t *parse_function_declarator(declaration_t *declaration)
 {
-       eat('(');
-       add_anchor_token(')');
-
        type_t *type;
        if(declaration != NULL) {
                type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
@@ -3432,10 +3493,6 @@ static construct_type_t *parse_function_declarator(declaration_t *declaration)
        construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
        construct_function_type->function_type       = type;
 
-       rem_anchor_token(')');
-       expect(')');
-
-end_error:
        return (construct_type_t*) construct_function_type;
 }
 
@@ -3866,6 +3923,9 @@ warn_redundant_declaration:
                                        }
                                }
                        }
+
+                       if (declaration->is_inline)
+                               previous_declaration->is_inline = true;
                        return previous_declaration;
                }
        } else if (is_function_definition) {
@@ -4101,10 +4161,10 @@ static void parse_declaration(parsed_declaration_func finished_declaration)
 static void parse_kr_declaration_list(declaration_t *declaration)
 {
        type_t *type = skip_typeref(declaration->type);
-       if(!is_type_function(type))
+       if (!is_type_function(type))
                return;
 
-       if(!type->function.kr_style_parameters)
+       if (!type->function.kr_style_parameters)
                return;
 
        /* push function parameters */
@@ -4113,14 +4173,14 @@ static void parse_kr_declaration_list(declaration_t *declaration)
        set_scope(&declaration->scope);
 
        declaration_t *parameter = declaration->scope.declarations;
-       for( ; parameter != NULL; parameter = parameter->next) {
+       for ( ; parameter != NULL; parameter = parameter->next) {
                assert(parameter->parent_scope == NULL);
                parameter->parent_scope = scope;
                environment_push(parameter);
        }
 
        /* parse declaration list */
-       while(is_declaration_specifier(&token, false)) {
+       while (is_declaration_specifier(&token, false)) {
                parse_declaration(finished_kr_declaration);
        }
 
@@ -4131,7 +4191,6 @@ static void parse_kr_declaration_list(declaration_t *declaration)
 
        /* update function type */
        type_t *new_type = duplicate_type(type);
-       new_type->function.kr_style_parameters = false;
 
        function_parameter_t *parameters     = NULL;
        function_parameter_t *last_parameter = NULL;
@@ -4169,7 +4228,11 @@ static void parse_kr_declaration_list(declaration_t *declaration)
                }
                last_parameter = function_parameter;
        }
+
+       /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
+        * prototype */
        new_type->function.parameters = parameters;
+       new_type->function.unspecified_parameters = true;
 
        type = typehash_insert(new_type);
        if(type != new_type) {
@@ -4309,7 +4372,9 @@ static void parse_external_declaration(void)
 
        /* § 6.7.5.3 (14) a function definition with () means no
         * parameters (and not unspecified parameters) */
-       if(type->function.unspecified_parameters) {
+       if(type->function.unspecified_parameters
+                       && type->function.parameters == NULL
+                       && !type->function.kr_style_parameters) {
                type_t *duplicate = duplicate_type(type);
                duplicate->function.unspecified_parameters = false;
 
@@ -4759,6 +4824,20 @@ static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
        return result;
 }
 
+static type_t *make_function_0_type(type_t *return_type)
+{
+       type_t *type               = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
+       type->function.return_type = return_type;
+       type->function.parameters  = NULL;
+
+       type_t *result = typehash_insert(type);
+       if(result != type) {
+               free_type(type);
+       }
+
+       return result;
+}
+
 /**
  * Creates a function type for some function like builtins.
  *
@@ -4769,6 +4848,8 @@ static type_t *get_builtin_symbol_type(symbol_t *symbol)
        switch(symbol->ID) {
        case T___builtin_alloca:
                return make_function_1_type(type_void_ptr, type_size_t);
+       case T___builtin_huge_val:
+               return make_function_0_type(type_double);
        case T___builtin_nan:
                return make_function_1_type(type_double, type_char_ptr);
        case T___builtin_nanf:
@@ -5475,6 +5556,7 @@ static expression_t *parse_primary_expression(void)
                case T___builtin_nan:
                case T___builtin_nand:
                case T___builtin_nanf:
+               case T___builtin_huge_val:
                case T___builtin_va_end:         return parse_builtin_symbol();
                case T___builtin_isgreater:
                case T___builtin_isgreaterequal:
@@ -5645,7 +5727,7 @@ static expression_t *parse_select_expression(unsigned precedence,
 
        declaration_t *const declaration = type_left->compound.declaration;
 
-       if(!declaration->init.is_defined) {
+       if(!declaration->init.complete) {
                errorf(HERE, "request for member '%Y' of incomplete type '%T'",
                       symbol, type_left);
                return create_invalid_expression();
@@ -6287,9 +6369,9 @@ static void semantic_comparison(binary_expression_t *expression)
                                other_expr = left;
                        }
 
-                       type_t *other_type = skip_typeref(other_expr->base.type);
                        if(const_expr != NULL) {
-                               long val = fold_constant(const_expr);
+                               type_t *other_type = skip_typeref(other_expr->base.type);
+                               long    val        = fold_constant(const_expr);
                                /* TODO: check if val can be represented by other_type */
                                (void) other_type;
                                (void) val;