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;
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;
}
const symbol_t *const symbol = declaration->symbol;
const namespace_t namespc = (namespace_t)declaration->namespc;
+ assert(declaration->symbol != NULL);
+ declaration_t *previous_declaration = get_declaration(symbol, namespc);
+
type_t *const orig_type = declaration->type;
type_t *const type = skip_typeref(orig_type);
if (is_type_function(type) &&
type->function.unspecified_parameters &&
- warning.strict_prototypes) {
+ warning.strict_prototypes &&
+ previous_declaration == NULL) {
warningf(&declaration->source_position,
"function declaration '%#T' is not a prototype",
orig_type, declaration->symbol);
check_type_of_main(declaration, &type->function);
}
- assert(declaration->symbol != NULL);
- declaration_t *previous_declaration = get_declaration(symbol, namespc);
-
assert(declaration != previous_declaration);
if (previous_declaration != NULL) {
if (previous_declaration->parent_scope == scope) {
}
}
+static type_t *get_default_promoted_type(type_t *orig_type)
+{
+ type_t *result = orig_type;
+
+ type_t *type = skip_typeref(orig_type);
+ if(is_type_integer(type)) {
+ result = promote_integer(type);
+ } else if(type == type_float) {
+ result = type_double;
+ }
+
+ return result;
+}
+
static void parse_kr_declaration_list(declaration_t *declaration)
{
type_t *type = skip_typeref(declaration->type);
semantic_parameter(parameter_declaration);
parameter_type = parameter_declaration->type;
+ /*
+ * we need the default promoted types for the function type
+ */
+ parameter_type = get_default_promoted_type(parameter_type);
+
function_parameter_t *function_parameter
= obstack_alloc(type_obst, sizeof(function_parameter[0]));
memset(function_parameter, 0, sizeof(function_parameter[0]));
/* ยง 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.parameters = parameters;
new_type->function.unspecified_parameters = true;
type = typehash_insert(new_type);
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 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.complete) {
+ 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;
rem_anchor_token(')');
expect(')');
- if(function_type != NULL) {
- function_parameter_t *parameter = function_type->parameters;
- call_argument_t *argument = call->arguments;
+ if(function_type == NULL)
+ return result;
+
+ function_parameter_t *parameter = function_type->parameters;
+ call_argument_t *argument = call->arguments;
+ if (!function_type->unspecified_parameters) {
for( ; parameter != NULL && argument != NULL;
parameter = parameter->next, argument = argument->next) {
type_t *expected_type = parameter->type;
/* TODO report scope in error messages */
expression_t *const arg_expr = argument->expression;
type_t *const res_type = semantic_assign(expected_type, arg_expr,
- "function call",
- &arg_expr->base.source_position);
+ "function call",
+ &arg_expr->base.source_position);
if (res_type == NULL) {
/* TODO improve error message */
errorf(&arg_expr->base.source_position,
argument->expression = create_implicit_cast(argument->expression, expected_type);
}
}
- /* too few parameters */
- if(parameter != NULL) {
+
+ if (parameter != NULL) {
errorf(HERE, "too few arguments to function '%E'", expression);
- } else if(argument != NULL) {
- /* too many parameters */
- if(!function_type->variadic
- && !function_type->unspecified_parameters) {
- errorf(HERE, "too many arguments to function '%E'", expression);
- } else {
- /* do default promotion */
- for( ; argument != NULL; argument = argument->next) {
- type_t *type = argument->expression->base.type;
-
- type = skip_typeref(type);
- if(is_type_integer(type)) {
- type = promote_integer(type);
- } else if(type == type_float) {
- type = type_double;
- }
+ } else if (argument != NULL && !function_type->variadic) {
+ errorf(HERE, "too many arguments to function '%E'", expression);
+ }
+ }
- argument->expression
- = create_implicit_cast(argument->expression, type);
- }
+ /* do default promotion */
+ for( ; argument != NULL; argument = argument->next) {
+ type_t *type = argument->expression->base.type;
- check_format(&result->call);
- }
- } else {
- check_format(&result->call);
- }
+ type = get_default_promoted_type(type);
+
+ argument->expression
+ = create_implicit_cast(argument->expression, type);
}
+ check_format(&result->call);
+
return result;
end_error:
return create_invalid_expression();
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);
- return;
- }
- if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
- errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
- orig_type_left);
+ if (!is_valid_assignment_lhs(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;
register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
T_MINUSMINUS, 30);
- register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
- register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
- register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
- register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
- register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
- register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
- register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
+ register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
+ register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
+ register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
+ register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
+ register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
+ register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
+ register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);