static unsigned char token_anchor_set[T_LAST_TOKEN];
/** The current source position. */
-#define HERE &token.source_position
+#define HERE (&token.source_position)
static type_t *type_valist;
++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).
*/
/**
* 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) {
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; \
} \
[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",
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.
*/
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)
len * sizeof(initializers[0]));
DEL_ARR_F(initializers);
- ascend_to(path, top_path_level);
+ ascend_to(path, top_path_level+1);
return result;
end_error:
skip_initializers();
DEL_ARR_F(initializers);
- ascend_to(path, top_path_level);
+ ascend_to(path, top_path_level+1);
return NULL;
}
(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);
}
}
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);
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);
}
last_declaration = declaration;
- if(token.type != ',')
+ if (token.type != ',') {
break;
+ }
next_token();
} while(token.type == T_IDENTIFIER);
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;
case T_DOTDOTDOT:
next_token();
type->variadic = 1;
- return declarations;
+ goto parameters_finished;
case T_IDENTIFIER:
case T___extension__:
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 {
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);
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;
}
}
}
}
+
+ if (declaration->is_inline)
+ previous_declaration->is_inline = true;
return previous_declaration;
}
} else if (is_function_definition) {
}
static bool is_declaration_specifier(const token_t *token,
- bool only_type_specifiers)
+ bool only_specifiers_qualifiers)
{
switch(token->type) {
TYPE_SPECIFIERS
+ TYPE_QUALIFIERS
return true;
case T_IDENTIFIER:
return is_typedef_symbol(token->v.symbol);
case T___extension__:
STORAGE_CLASSES
- TYPE_QUALIFIERS
- return !only_type_specifiers;
+ return !only_specifiers_qualifiers;
default:
return false;
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 */
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);
}
/* 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;
}
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) {
/* § 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;
declaration->type = type;
declaration->symbol = symbol;
declaration->source_position = *source_position;
- declaration->parent_scope = global_scope;
- scope_t *old_scope = scope;
- set_scope(global_scope);
-
- environment_push(declaration);
- /* prepends the declaration to the global declarations list */
- declaration->next = scope->declarations;
- scope->declarations = declaration;
-
- assert(scope == global_scope);
- set_scope(old_scope);
+ bool strict_prototypes_old = warning.strict_prototypes;
+ warning.strict_prototypes = false;
+ record_declaration(declaration);
+ warning.strict_prototypes = strict_prototypes_old;
return declaration;
}
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.
*
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:
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:
return expression;
}
-static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
+static expression_t *parse_typeprop(expression_kind_t const kind,
+ source_position_t const pos,
+ unsigned const precedence)
{
expression_t *tp_expression = allocate_expression_zero(kind);
- tp_expression->base.type = type_size_t;
+ tp_expression->base.type = type_size_t;
+ tp_expression->base.source_position = pos;
- if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
+ char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
+
+ if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
next_token();
add_anchor_token(')');
- tp_expression->typeprop.type = parse_typename();
+ type_t* const orig_type = parse_typename();
+ tp_expression->typeprop.type = orig_type;
+
+ type_t const* const type = skip_typeref(orig_type);
+ char const* const wrong_type =
+ is_type_incomplete(type) ? "incomplete" :
+ type->kind == TYPE_FUNCTION ? "function designator" :
+ type->kind == TYPE_BITFIELD ? "bitfield" :
+ NULL;
+ if (wrong_type != NULL) {
+ errorf(&pos, "operand of %s expression must not be %s type '%T'",
+ what, wrong_type, type);
+ }
+
rem_anchor_token(')');
expect(')');
} else {
expression_t *expression = parse_sub_expression(precedence);
- expression->base.type = revert_automatic_type_conversion(expression);
+
+ type_t* const orig_type = revert_automatic_type_conversion(expression);
+ expression->base.type = orig_type;
+
+ type_t const* const type = skip_typeref(orig_type);
+ char const* const wrong_type =
+ is_type_incomplete(type) ? "incomplete" :
+ type->kind == TYPE_FUNCTION ? "function designator" :
+ type->kind == TYPE_BITFIELD ? "bitfield" :
+ NULL;
+ if (wrong_type != NULL) {
+ errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
+ }
tp_expression->typeprop.type = expression->base.type;
tp_expression->typeprop.tp_expression = expression;
static expression_t *parse_sizeof(unsigned precedence)
{
+ source_position_t pos = *HERE;
eat(T_sizeof);
- return parse_typeprop(EXPR_SIZEOF, precedence);
+ return parse_typeprop(EXPR_SIZEOF, pos, precedence);
}
static expression_t *parse_alignof(unsigned precedence)
{
+ source_position_t pos = *HERE;
eat(T___alignof__);
- return parse_typeprop(EXPR_SIZEOF, precedence);
+ return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
}
static expression_t *parse_select_expression(unsigned precedence,
expression_t *select = allocate_expression_zero(EXPR_SELECT);
select->select.compound = compound;
- if(token.type != T_IDENTIFIER) {
+ if (token.type != T_IDENTIFIER) {
parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
return select;
}
type_t *const type = skip_typeref(orig_type);
type_t *type_left = type;
- if(is_pointer) {
+ if (is_pointer) {
if (!is_type_pointer(type)) {
if (is_type_valid(type)) {
errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
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();
}
declaration_t *iter = find_compound_entry(declaration, symbol);
- if(iter == NULL) {
+ if (iter == NULL) {
errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
return create_invalid_expression();
}
select->select.compound_entry = iter;
select->base.type = expression_type;
- if(expression_type->kind == TYPE_BITFIELD) {
- expression_t *extract
- = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
- extract->unary.value = select;
- extract->base.type = expression_type->bitfield.base_type;
-
- return extract;
+ type_t *skipped = skip_typeref(iter->type);
+ if (skipped->kind == TYPE_BITFIELD) {
+ select->base.type = skipped->bitfield.base_type;
}
return select;
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;
expression->base.type = type_int;
}
+/**
+ * Checks if a compound type has constant fields.
+ */
+static bool has_const_fields(const compound_type_t *type)
+{
+ const scope_t *scope = &type->declaration->scope;
+ const declaration_t *declaration = scope->declarations;
+
+ for (; declaration != NULL; declaration = declaration->next) {
+ if (declaration->namespc != NAMESPACE_NORMAL)
+ continue;
+
+ const type_t *decl_type = skip_typeref(declaration->type);
+ if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
+ return true;
+ }
+ /* TODO */
+ return false;
+}
+
+static bool is_valid_assignment_lhs(expression_t const* const left)
+{
+ type_t *const orig_type_left = revert_automatic_type_conversion(left);
+ type_t *const type_left = skip_typeref(orig_type_left);
+
+ switch (left->kind) {
+ case EXPR_REFERENCE:
+ case EXPR_ARRAY_ACCESS:
+ case EXPR_SELECT:
+ case EXPR_UNARY_DEREFERENCE:
+ break;
+
+ default:
+ errorf(HERE, "left hand side '%E' of assignment is not an lvalue", left);
+ return false;
+ }
+
+ if (is_type_array(type_left)) {
+ errorf(HERE, "cannot assign to arrays ('%E')", left);
+ return false;
+ }
+ if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
+ errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
+ orig_type_left);
+ return false;
+ }
+ if (is_type_incomplete(type_left)) {
+ errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
+ left, orig_type_left);
+ return false;
+ }
+ if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
+ errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
+ left, orig_type_left);
+ return false;
+ }
+
+ return true;
+}
+
static void semantic_arithmetic_assign(binary_expression_t *expression)
{
expression_t *left = expression->left;
type_t *orig_type_left = left->base.type;
type_t *orig_type_right = right->base.type;
+ if (!is_valid_assignment_lhs(left))
+ return;
+
type_t *type_left = skip_typeref(orig_type_left);
type_t *type_right = skip_typeref(orig_type_right);
type_t *const type_left = skip_typeref(orig_type_left);
type_t *const type_right = skip_typeref(orig_type_right);
+ if (!is_valid_assignment_lhs(left))
+ return;
+
if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
/* combined instructions are tricky. We can't create an implicit cast on
* the left side, because we need the uncasted form for the store.
expression->base.type = type_int;
}
-/**
- * Checks if a compound type has constant fields.
- */
-static bool has_const_fields(const compound_type_t *type)
-{
- const scope_t *scope = &type->declaration->scope;
- const declaration_t *declaration = scope->declarations;
-
- for (; declaration != NULL; declaration = declaration->next) {
- if (declaration->namespc != NAMESPACE_NORMAL)
- continue;
-
- const type_t *decl_type = skip_typeref(declaration->type);
- if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
- return true;
- }
- /* TODO */
- return false;
-}
-
/**
* Check the semantic restrictions of a binary assign expression.
*/
type_t *type_left = revert_automatic_type_conversion(left);
type_left = skip_typeref(orig_type_left);
- /* must be a modifiable lvalue */
- if (is_type_array(type_left)) {
- errorf(HERE, "cannot assign to arrays ('%E')", left);
+ if (!is_valid_assignment_lhs(left))
return;
- }
- if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
- errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
- orig_type_left);
- return;
- }
- if(is_type_incomplete(type_left)) {
- errorf(HERE,
- "left-hand side of assignment '%E' has incomplete type '%T'",
- left, orig_type_left);
- return;
- }
- if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
- errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
- left, orig_type_left);
- return;
- }
type_t *const res_type = semantic_assign(orig_type_left, expression->right,
"assignment", &left->base.source_position);
case EXPR_UNARY_CAST_IMPLICIT: return true;
case EXPR_UNARY_ASSUME: return true;
- case EXPR_UNARY_BITFIELD_EXTRACT: return false;
case EXPR_BINARY_ADD: return false;
case EXPR_BINARY_SUB: return false;