unsigned short namespace;
} stack_entry_t;
-static token_t token;
-static token_t lookahead_buffer[MAX_LOOKAHEAD];
-static int lookahead_bufpos;
-static stack_entry_t *environment_stack = NULL;
-static context_t *global_context = NULL;
-static context_t *context = NULL;
-static declaration_t *last_declaration = NULL;
-static struct obstack temp_obst;
-
-static type_t *type_int = NULL;
-static type_t *type_double = NULL;
-static type_t *type_const_char = NULL;
-static type_t *type_string = NULL;
-static type_t *type_void = NULL;
-static type_t *type_size_t = NULL;
+static token_t token;
+static token_t lookahead_buffer[MAX_LOOKAHEAD];
+static int lookahead_bufpos;
+static stack_entry_t *environment_stack = NULL;
+static context_t *global_context = NULL;
+static context_t *context = NULL;
+static declaration_t *last_declaration = NULL;
+static declaration_t *current_function = NULL;
+static struct obstack temp_obst;
+static bool found_error;
+
+static type_t *type_int = NULL;
+static type_t *type_uint = NULL;
+static type_t *type_long_double = NULL;
+static type_t *type_double = NULL;
+static type_t *type_float = NULL;
+static type_t *type_const_char = NULL;
+static type_t *type_string = NULL;
+static type_t *type_void = NULL;
+static type_t *type_size_t = NULL;
+static type_t *type_ptrdiff_t = NULL;
static statement_t *parse_compound_statement(void);
static statement_t *parse_statement(void);
#define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
-void error(void)
+static void error(void)
{
+ found_error = true;
#ifdef ABORT_ON_ERROR
abort();
#endif
}
-void parser_print_prefix_pos(const source_position_t source_position)
+static void parser_print_prefix_pos(const source_position_t source_position)
{
fputs(source_position.input_name, stderr);
fputc(':', stderr);
fputs(": ", stderr);
}
-void parser_print_error_prefix_pos(const source_position_t source_position)
+static void parser_print_error_prefix_pos(
+ const source_position_t source_position)
{
parser_print_prefix_pos(source_position);
fputs("error: ", stderr);
error();
}
-void parser_print_error_prefix(void)
+static void parser_print_error_prefix(void)
{
- parser_print_prefix_pos(token.source_position);
- error();
+ parser_print_error_prefix_pos(token.source_position);
}
static void parse_error(const char *message)
fprintf(stderr, "parse error: %s\n", message);
}
-__attribute__((unused))
static void parse_warning(const char *message)
{
parser_print_prefix_pos(token.source_position);
last_declaration = new_context->declarations;
if(last_declaration != NULL) {
- while(last_declaration->context_next != NULL) {
- last_declaration = last_declaration->context_next;
+ while(last_declaration->next != NULL) {
+ last_declaration = last_declaration->next;
}
}
}
static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
{
declaration_t *declaration = symbol->declaration;
- for( ; declaration != NULL; declaration = declaration->next) {
+ for( ; declaration != NULL; declaration = declaration->symbol_next) {
if(declaration->namespace == namespace)
return declaration;
}
symbol->declaration = declaration;
} else {
declaration_t *iter = symbol->declaration;
- for( ; iter != NULL; iter = iter->next) {
- declaration_t *next = iter->next;
- if(next == NULL) {
- iter->next = declaration;
- assert(declaration->next == NULL);
+ for( ; iter != NULL; iter = iter->symbol_next) {
+ declaration_t *symbol_next = iter->symbol_next;
+ if(symbol_next == NULL) {
+ iter->symbol_next = declaration;
+ assert(declaration->symbol_next == NULL);
break;
}
- if(next->namespace == namespace) {
- iter->next = declaration;
- declaration->next = next->next;
+ if(symbol_next->namespace == namespace) {
+ iter->symbol_next = declaration;
+ declaration->symbol_next = symbol_next->symbol_next;
break;
}
}
assert(declaration != NULL);
if(declaration->namespace == namespace) {
if(old_declaration == NULL) {
- symbol->declaration = declaration->next;
+ symbol->declaration = declaration->symbol_next;
} else {
symbol->declaration = old_declaration;
- assert(old_declaration->next == declaration->next);
+ assert(old_declaration->symbol_next ==
+ declaration->symbol_next);
}
} else {
- for( ; declaration != NULL; declaration = declaration->next) {
- declaration_t *next = declaration->next;
- if(next->namespace == namespace) {
- declaration->next = old_declaration;
- assert(old_declaration->next == next->next);
+ for(; declaration != NULL; declaration = declaration->symbol_next) {
+ declaration_t *symbol_next = declaration->symbol_next;
+ if(symbol_next->namespace == namespace) {
+ declaration->symbol_next = old_declaration;
+ assert(old_declaration->symbol_next
+ == symbol_next->symbol_next);
break;
}
}
builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
type->type.type = TYPE_BUILTIN;
type->symbol = symbol;
+ /* TODO... */
+ type->real_type = type_int;
return (type_t*) type;
}
return declaration;
}
-static declaration_t *parse_parameters(method_type_t *type)
+static declaration_t *parse_parameters(function_type_t *type)
{
if(token.type == T_IDENTIFIER) {
symbol_t *symbol = token.v.symbol;
return NULL;
}
- declaration_t *declarations = NULL;
- declaration_t *declaration;
- declaration_t *last_declaration = NULL;
- method_parameter_t *parameter;
- method_parameter_t *last_parameter = NULL;
+ declaration_t *declarations = NULL;
+ declaration_t *declaration;
+ declaration_t *last_declaration = NULL;
+ function_parameter_t *parameter;
+ function_parameter_t *last_parameter = NULL;
while(true) {
switch(token.type) {
parameter->type = declaration->type;
if(last_parameter != NULL) {
- last_declaration->context_next = declaration;
- last_parameter->next = parameter;
+ last_declaration->next = declaration;
+ last_parameter->next = parameter;
} else {
type->parameters = parameter;
declarations = declaration;
typedef enum {
CONSTRUCT_POINTER,
- CONSTRUCT_METHOD,
+ CONSTRUCT_FUNCTION,
CONSTRUCT_ARRAY
} construct_type_type_t;
type_qualifier_t type_qualifiers;
};
-typedef struct construct_method_type_t construct_method_type_t;
-struct construct_method_type_t {
- construct_type_t construct_type;
- method_type_t *method_type;
+typedef struct construct_function_type_t construct_function_type_t;
+struct construct_function_type_t {
+ construct_type_t construct_type;
+ function_type_t *function_type;
};
typedef struct parsed_array_t parsed_array_t;
return (construct_type_t*) array;
}
-static construct_type_t *parse_method_declarator(declaration_t *declaration)
+static construct_type_t *parse_function_declarator(declaration_t *declaration)
{
eat('(');
- method_type_t *method_type
- = allocate_type_zero(sizeof(method_type[0]));
- method_type->type.type = TYPE_METHOD;
+ function_type_t *type = allocate_type_zero(sizeof(type[0]));
+ type->type.type = TYPE_FUNCTION;
- declaration_t *parameters = parse_parameters(method_type);
+ declaration_t *parameters = parse_parameters(type);
if(declaration != NULL) {
declaration->context.declarations = parameters;
}
- construct_method_type_t *construct_method_type =
- obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
- memset(construct_method_type, 0, sizeof(construct_method_type[0]));
- construct_method_type->construct_type.type = CONSTRUCT_METHOD;
- construct_method_type->method_type = method_type;
+ construct_function_type_t *construct_function_type =
+ obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
+ memset(construct_function_type, 0, sizeof(construct_function_type[0]));
+ construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
+ construct_function_type->function_type = type;
expect(')');
- return (construct_type_t*) construct_method_type;
+ return (construct_type_t*) construct_function_type;
}
static construct_type_t *parse_inner_declarator(declaration_t *declaration,
construct_type_t *type;
switch(token.type) {
case '(':
- type = parse_method_declarator(declaration);
+ type = parse_function_declarator(declaration);
break;
case '[':
type = parse_array_declarator();
{
construct_type_t *iter = construct_list;
for( ; iter != NULL; iter = iter->next) {
- parsed_pointer_t *parsed_pointer;
- parsed_array_t *parsed_array;
- construct_method_type_t *construct_method_type;
- method_type_t *method_type;
- pointer_type_t *pointer_type;
- array_type_t *array_type;
+ parsed_pointer_t *parsed_pointer;
+ parsed_array_t *parsed_array;
+ construct_function_type_t *construct_function_type;
+ function_type_t *function_type;
+ pointer_type_t *pointer_type;
+ array_type_t *array_type;
switch(iter->type) {
- case CONSTRUCT_METHOD:
- construct_method_type = (construct_method_type_t*) iter;
- method_type = construct_method_type->method_type;
+ case CONSTRUCT_FUNCTION:
+ construct_function_type = (construct_function_type_t*) iter;
+ function_type = construct_function_type->function_type;
- method_type->result_type = type;
- type = (type_t*) method_type;
+ function_type->result_type = type;
+ type = (type_t*) function_type;
break;
case CONSTRUCT_POINTER:
}
if(last_declaration != NULL) {
- last_declaration->context_next = declaration;
+ last_declaration->next = declaration;
} else {
context->declarations = declaration;
}
if(token.type == '=') {
next_token();
- /* TODO: check that this is an allowed type (esp. no method type) */
+ /* TODO: check that this is an allowed type (no function type) */
if(declaration->init.initializer != NULL) {
parser_error_multiple_definition(declaration, ndeclaration);
ndeclaration->init.initializer = parse_initializer();
} else if(token.type == '{') {
- if(declaration->type->type != TYPE_METHOD) {
+ if(declaration->type->type != TYPE_FUNCTION) {
parser_print_error_prefix();
fprintf(stderr, "Declarator ");
print_type_ext(declaration->type, declaration->symbol, NULL);
- fprintf(stderr, " is not a method type.\n");
+ fprintf(stderr, " has a body but is not a function type.\n");
eat_block();
continue;
}
/* push function parameters */
declaration_t *parameter = declaration->context.declarations;
- for( ; parameter != NULL; parameter = parameter->context_next) {
+ for( ; parameter != NULL; parameter = parameter->next) {
environment_push(parameter);
}
+ declaration_t *old_current_function = current_function;
+ current_function = declaration;
statement_t *statement = parse_compound_statement();
+ assert(current_function == declaration);
+ old_current_function = current_function;
+
assert(context == &declaration->context);
set_context(last_context);
environment_pop_to(top);
static declaration_t *create_implicit_function(symbol_t *symbol,
const source_position_t source_position)
{
- method_type_t *method_type = allocate_type_zero(sizeof(method_type));
+ function_type_t *function_type = allocate_type_zero(sizeof(function_type));
- method_type->type.type = TYPE_METHOD;
- method_type->result_type = type_int;
- method_type->unspecified_parameters = true;
+ function_type->type.type = TYPE_FUNCTION;
+ function_type->result_type = type_int;
+ function_type->unspecified_parameters = true;
- type_t *type = typehash_insert((type_t*) method_type);
- if(type != (type_t*) method_type) {
- free_type(method_type);
+ type_t *type = typehash_insert((type_t*) function_type);
+ if(type != (type_t*) function_type) {
+ free_type(function_type);
}
declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
context = global_context;
environment_push(declaration);
- declaration->context_next = context->declarations;
- context->declarations = declaration;
+ declaration->next = context->declarations;
+ context->declarations = declaration;
context = last_context;
{
(void) expression;
(void) dest_type;
- /* TODO check if cast is allowed and issue warnings/errors */
+ /* TODO check if explicit cast is allowed and issue warnings/errors */
}
static expression_t *parse_cast(void)
return (expression_t*) select;
}
+static expression_t *create_cast_expression(expression_t *expression,
+ type_t *dest_type)
+{
+ unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
+
+ cast->expression.type = EXPR_UNARY;
+ cast->type = UNEXPR_CAST;
+ cast->value = expression;
+ cast->expression.datatype = dest_type;
+
+ return (expression_t*) cast;
+}
+
+static void type_error(const char *msg, const source_position_t source_position,
+ type_t *type)
+{
+ parser_print_error_prefix_pos(source_position);
+ fprintf(stderr, "%s, but found type ", msg);
+ print_type(type);
+ fputc('\n', stderr);
+ error();
+}
+
+static void type_error_incompatible(const char *msg,
+ const source_position_t source_position, type_t *type1, type_t *type2)
+{
+ parser_print_error_prefix_pos(source_position);
+ fprintf(stderr, "%s, incompatible types: ", msg);
+ print_type(type1);
+ fprintf(stderr, " - ");
+ print_type(type2);
+ fprintf(stderr, ")\n");
+ error();
+}
+
+static int get_rank(const type_t *type)
+{
+ /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
+ * and esp. footnote 108). However we can't fold constants (yet), so we
+ * can't decide wether unsigned int is possible, while int always works.
+ * (unsigned int would be preferable when possible... for stuff like
+ * struct { enum { ... } bla : 4; } ) */
+ if(type->type == TYPE_ENUM)
+ return ATOMIC_TYPE_INT;
+
+ assert(type->type == TYPE_ATOMIC);
+ atomic_type_t *atomic_type = (atomic_type_t*) type;
+ atomic_type_type_t atype = atomic_type->atype;
+ return atype;
+}
+
+static type_t *promote_integer(type_t *type)
+{
+ if(get_rank(type) < ATOMIC_TYPE_INT)
+ type = type_int;
+
+ return type;
+}
+
+static expression_t *create_implicit_cast(expression_t *expression,
+ type_t *dest_type)
+{
+ assert(expression->datatype != NULL);
+ type_t *source_type = expression->datatype;
+
+ if(expression->datatype == dest_type)
+ return expression;
+
+ if(dest_type->type == TYPE_ATOMIC) {
+ if(source_type->type != TYPE_ATOMIC)
+ panic("casting of non-atomic types not implemented yet");
+
+ if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
+ type_error_incompatible("can't cast types",
+ expression->source_position,
+ source_type, dest_type);
+ return expression;
+ }
+
+ return create_cast_expression(expression, dest_type);
+ }
+ if(dest_type->type == TYPE_POINTER) {
+ if(source_type->type == TYPE_POINTER) {
+ if(!pointers_compatible(source_type, dest_type)) {
+ type_error_incompatible("can't implicitely cast types",
+ expression->source_position,
+ source_type, dest_type);
+ } else {
+ return create_cast_expression(expression, dest_type);
+ }
+ }
+ }
+
+ panic("casting of non-atomic types not implemented yet");
+}
+
static expression_t *parse_call_expression(unsigned precedence,
expression_t *expression)
{
(void) precedence;
call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
+ call->expression.type = EXPR_CALL;
+ call->function = expression;
- call->expression.type = EXPR_CALL;
- call->method = expression;
+ function_type_t *function_type;
+ type_t *type = expression->datatype;
+ if(type->type != TYPE_FUNCTION) {
+ /* TODO calling pointers to functions is ok */
+ parser_print_error_prefix();
+ fputs("called object '", stderr);
+ print_expression(expression);
+ fputs("' (type ", stderr);
+ print_type(type);
+ fputs("is not a function\n", stderr);
+
+ function_type = NULL;
+ call->expression.datatype = NULL;
+ } else {
+ function_type = (function_type_t*) type;
+ call->expression.datatype = function_type->result_type;
+ }
/* parse arguments */
eat('(');
}
expect(')');
- type_t *type = expression->datatype;
- if(type != NULL) {
- /* we can call pointer to function */
- if(type->type == TYPE_POINTER) {
- pointer_type_t *pointer = (pointer_type_t*) type;
- type = pointer->points_to;
+ if(function_type != NULL) {
+ function_parameter_t *parameter = function_type->parameters;
+ call_argument_t *argument = call->arguments;
+ for( ; parameter != NULL && argument != NULL;
+ parameter = parameter->next, argument = argument->next) {
+ type_t *expected_type = parameter->type;
+ /* TODO report context in error messages */
+ argument->expression = create_implicit_cast(argument->expression,
+ expected_type);
}
-
- if(type == NULL || type->type != TYPE_METHOD) {
+ /* too few parameters */
+ if(parameter != NULL) {
parser_print_error_prefix();
- fprintf(stderr, "expected a method type for call but found type ");
- print_type(expression->datatype);
- fprintf(stderr, "\n");
- } else {
- method_type_t *method_type = (method_type_t*) type;
- call->expression.datatype = method_type->result_type;
+ fprintf(stderr, "too few arguments to function '");
+ print_expression(expression);
+ fprintf(stderr, "'\n");
+ } else if(argument != NULL) {
+ /* too many parameters */
+ if(!function_type->variadic
+ && !function_type->unspecified_parameters) {
+ parser_print_error_prefix();
+ fprintf(stderr, "too many arguments to function '");
+ print_expression(expression);
+ fprintf(stderr, "'\n");
+ } else {
+ /* do default promotion */
+ for( ; argument != NULL; argument = argument->next) {
+ type_t *type = argument->expression->datatype;
+
+ if(is_type_integer(type)) {
+ type = promote_integer(type);
+ } else if(type == type_float) {
+ type = type_double;
+ }
+ argument->expression
+ = create_implicit_cast(argument->expression, type);
+ }
+ }
}
}
return (expression_t*) call;
}
-static void type_error(const char *msg, const source_position_t source_position,
- type_t *type)
-{
- parser_print_error_prefix_pos(source_position);
- fprintf(stderr, "%s, but found type ", msg);
- print_type(type);
- fputc('\n', stderr);
- error();
-}
-
-static void type_error_incompatible(const char *msg,
- const source_position_t source_position, type_t *type1, type_t *type2)
-{
- parser_print_error_prefix_pos(source_position);
- fprintf(stderr, "%s, incompatible types: ", msg);
- print_type(type1);
- fprintf(stderr, " - ");
- print_type(type2);
- fprintf(stderr, ")\n");
- error();
-}
-
static type_t *get_type_after_conversion(const type_t *type1,
const type_t *type2)
{
return parse_sub_expression(precedence);
}
-#define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
-static \
-expression_t *parse_##unexpression_type(unsigned precedence) \
-{ \
- eat(token_type); \
- \
- unary_expression_t *unary_expression \
- = allocate_ast_zero(sizeof(unary_expression[0])); \
- unary_expression->expression.type = EXPR_UNARY; \
- unary_expression->type = unexpression_type; \
- unary_expression->value = parse_sub_expression(precedence); \
- \
- return (expression_t*) unary_expression; \
-}
-
-CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
-CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
-CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
-CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
-CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
-CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
-CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
-CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
-
-#define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
-static \
-expression_t *parse_##unexpression_type(unsigned precedence, \
- expression_t *left) \
+static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
+{
+ /* TODO */
+ return expression->datatype;
+}
+
+static type_t *get_unexpr_dereference_type(const expression_t *expression)
+{
+ type_t *expression_type = expression->datatype;
+
+ if(expression_type->type == TYPE_POINTER) {
+ pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
+ return pointer_type->points_to;
+ }
+ panic("deref TODO...");
+ return NULL;
+}
+
+static type_t *get_unexpr_take_addr_type(const expression_t *expression)
+{
+ type_t *type = expression->datatype;
+ return make_pointer_type(type, 0);
+}
+
+#define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
+static expression_t *parse_##unexpression_type(unsigned precedence) \
+{ \
+ eat(token_type); \
+ \
+ unary_expression_t *unary_expression \
+ = allocate_ast_zero(sizeof(unary_expression[0])); \
+ unary_expression->expression.type = EXPR_UNARY; \
+ unary_expression->type = unexpression_type; \
+ unary_expression->value = parse_sub_expression(precedence); \
+ unary_expression->expression.datatype = tfunc(unary_expression->value); \
+ \
+ return (expression_t*) unary_expression; \
+}
+
+CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
+CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
+CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
+CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
+ get_unexpr_dereference_type)
+CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
+ get_unexpr_take_addr_type)
+CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
+ get_unexpr_arithmetic_type)
+CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
+ get_unexpr_arithmetic_type)
+CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
+ get_unexpr_arithmetic_type)
+
+#define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
+ tfunc) \
+static expression_t *parse_##unexpression_type(unsigned precedence, \
+ expression_t *left) \
{ \
(void) precedence; \
eat(token_type); \
\
unary_expression_t *unary_expression \
= allocate_ast_zero(sizeof(unary_expression[0])); \
- unary_expression->expression.type = EXPR_UNARY; \
- unary_expression->type = unexpression_type; \
- unary_expression->value = left; \
+ unary_expression->expression.type = EXPR_UNARY; \
+ unary_expression->type = unexpression_type; \
+ unary_expression->value = left; \
+ unary_expression->expression.datatype = tfunc(left); \
\
return (expression_t*) unary_expression; \
}
-CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
-CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
-
-#define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
-static \
-expression_t *parse_##binexpression_type(unsigned precedence, \
- expression_t *left) \
-{ \
- eat(token_type); \
- \
- expression_t *right = parse_sub_expression(precedence); \
- \
- binary_expression_t *binexpr \
- = allocate_ast_zero(sizeof(binexpr[0])); \
- binexpr->expression.type = EXPR_BINARY; \
- binexpr->type = binexpression_type; \
- binexpr->left = left; \
- binexpr->right = right; \
- \
- return (expression_t*) binexpr; \
-}
-
-CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
-CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
-CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
-CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
-CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
-CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
-CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
-CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
-CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
-CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
-CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
-CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
-CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
-CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
-CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
-CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
-CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
-CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
-CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
-CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
-CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
-CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
-CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
-CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
-CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
-CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
-CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
-CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
-CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
-CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
+ get_unexpr_arithmetic_type)
+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
+ get_unexpr_arithmetic_type)
+
+static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
+{
+ /* TODO: handle complex + imaginary types */
+
+ /* § 6.3.1.8 Usual arithmetic conversions */
+ if(type_left == type_long_double || type_right == type_long_double) {
+ return type_long_double;
+ } else if(type_left == type_double || type_right == type_double) {
+ return type_double;
+ } else if(type_left == type_float || type_right == type_float) {
+ return type_float;
+ }
+
+ type_right = promote_integer(type_right);
+ type_left = promote_integer(type_left);
+
+ if(type_left == type_right)
+ return type_left;
+
+ bool signed_left = is_type_signed(type_left);
+ bool signed_right = is_type_signed(type_right);
+ if(get_rank(type_left) < get_rank(type_right)) {
+ if(signed_left == signed_right || !signed_right) {
+ return type_right;
+ } else {
+ return type_left;
+ }
+ } else {
+ if(signed_left == signed_right || !signed_left) {
+ return type_left;
+ } else {
+ return type_right;
+ }
+ }
+}
+
+static void semantic_binexpr_arithmetic(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = skip_typeref(left->datatype);
+ type_t *type_right = skip_typeref(right->datatype);
+
+ if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+ /* TODO: improve error message */
+ parser_print_error_prefix();
+ fprintf(stderr, "operation needs arithmetic types\n");
+ return;
+ }
+
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+}
+
+static void semantic_add(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *orig_type_left = left->datatype;
+ type_t *orig_type_right = right->datatype;
+ type_t *type_left = skip_typeref(orig_type_left);
+ type_t *type_right = skip_typeref(orig_type_right);
+
+ /* § 5.6.5 */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ return;
+ } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
+ expression->expression.datatype = type_left;
+ } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
+ expression->expression.datatype = type_right;
+ } else {
+ parser_print_error_prefix();
+ fprintf(stderr, "invalid operands to binary + (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ }
+}
+
+static void semantic_sub(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *orig_type_left = left->datatype;
+ type_t *orig_type_right = right->datatype;
+ type_t *type_left = skip_typeref(orig_type_left);
+ type_t *type_right = skip_typeref(orig_type_right);
+
+ /* § 5.6.5 */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ return;
+ } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
+ expression->expression.datatype = type_left;
+ } else if(type_left->type == TYPE_POINTER &&
+ type_right->type == TYPE_POINTER) {
+ if(!pointers_compatible(type_left, type_right)) {
+ parser_print_error_prefix();
+ fprintf(stderr, "pointers to incompatible objects to binary - (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ } else {
+ expression->expression.datatype = type_ptrdiff_t;
+ }
+ } else {
+ parser_print_error_prefix();
+ fprintf(stderr, "invalid operands to binary - (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ }
+}
+
+static void semantic_comparison(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = left->datatype;
+ type_t *type_right = right->datatype;
+
+ /* TODO non-arithmetic types */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ }
+ expression->expression.datatype = type_int;
+}
+
+static void semantic_arithmetic_assign(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = left->datatype;
+ type_t *type_right = right->datatype;
+
+ if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+ /* TODO: improve error message */
+ parser_print_error_prefix();
+ fprintf(stderr, "operation needs arithmetic types\n");
+ return;
+ }
+
+ /* combined instructions are tricky. We can't create an implicit cast on
+ * the left side, because we need the uncasted form for the store.
+ * The ast2firm pass has to know that left_type must be right_type
+ * for the arithmeitc operation and create a cast by itself */
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = type_left;
+}
+
+static void semantic_logical_op(binary_expression_t *expression)
+{
+ /* TODO */
+ expression->expression.datatype = type_int;
+}
+
+static void semantic_assign(type_t *orig_type_left, expression_t **right,
+ bool is_return)
+{
+ type_t *orig_type_right = (*right)->datatype;
+ type_t *type_left = skip_typeref(orig_type_left);
+ type_t *type_right = skip_typeref(orig_type_right);
+
+ if(type_left == type_right) {
+ /* fine */
+ } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ *right = create_implicit_cast(*right, type_left);
+ } else if(type_left->type == TYPE_POINTER
+ && type_right->type == TYPE_POINTER) {
+ /* TODO */
+ } else {
+ /* TODO: improve error message */
+ parser_print_error_prefix();
+ fprintf(stderr, "incompatible types in %s\n",
+ is_return ? "'return'" : "assignment");
+ parser_print_error_prefix();
+ print_type(type_left);
+ fputs(" <- ", stderr);
+ print_type(type_right);
+ fputs("\n", stderr);
+ }
+
+}
+
+static void semantic_binexpr_assign(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ type_t *type_left = left->datatype;
+
+ semantic_assign(type_left, &expression->right, false);
+
+ expression->expression.datatype = type_left;
+}
+
+static void semantic_comma(binary_expression_t *expression)
+{
+ expression->expression.datatype = expression->right->datatype;
+}
+
+#define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
+static expression_t *parse_##binexpression_type(unsigned precedence, \
+ expression_t *left) \
+{ \
+ eat(token_type); \
+ \
+ expression_t *right = parse_sub_expression(precedence + lr); \
+ \
+ binary_expression_t *binexpr \
+ = allocate_ast_zero(sizeof(binexpr[0])); \
+ binexpr->expression.type = EXPR_BINARY; \
+ binexpr->type = binexpression_type; \
+ binexpr->left = left; \
+ binexpr->right = right; \
+ sfunc(binexpr); \
+ \
+ return (expression_t*) binexpr; \
+}
+
+CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
+CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
+CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
+CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
+CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
+ semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
+ semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
+CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
+/* TODO shift has a bit special semantic */
+CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
+ semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
+ semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
+ semantic_arithmetic_assign, 0)
static expression_t *parse_sub_expression(unsigned precedence)
{
-void register_expression_parser(parse_expression_function parser,
- int token_type, unsigned precedence)
+static void register_expression_parser(parse_expression_function parser,
+ int token_type, unsigned precedence)
{
expression_parser_function_t *entry = &expression_parsers[token_type];
entry->precedence = precedence;
}
-void register_expression_infix_parser(parse_expression_infix_function parser,
- int token_type, unsigned precedence)
+static void register_expression_infix_parser(
+ parse_expression_infix_function parser, int token_type,
+ unsigned precedence)
{
expression_parser_function_t *entry = &expression_parsers[token_type];
statement->statement.type = STATEMENT_RETURN;
statement->statement.source_position = token.source_position;
+
+ assert(current_function->type->type == TYPE_FUNCTION);
+ function_type_t *function_type = (function_type_t*) current_function->type;
+ type_t *return_type = function_type->result_type;
+
+ expression_t *return_value;
if(token.type != ';') {
- statement->return_value = parse_expression();
+ return_value = parse_expression();
+
+ if(return_type == type_void && return_value->datatype != type_void) {
+ parse_warning("'return' with a value, in function returning void");
+ return_value = NULL;
+ } else {
+ semantic_assign(return_type, &return_value, true);
+ }
+ } else {
+ return_value = NULL;
+ if(return_type != type_void) {
+ parse_warning("'return' without value, in function returning "
+ "non-void");
+ }
}
+ statement->return_value = return_value;
+
expect(';');
return (statement_t*) statement;
if(before == NULL) {
statement->declarations_begin = context->declarations;
} else {
- statement->declarations_begin = before->context_next;
+ statement->declarations_begin = before->next;
}
statement->declarations_end = last_declaration;
translation_unit_t *parse(void)
{
environment_stack = NEW_ARR_F(stack_entry_t, 0);
+ found_error = false;
type_set_output(stderr);
+ ast_set_output(stderr);
lookahead_bufpos = 0;
for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
DEL_ARR_F(environment_stack);
+ if(found_error)
+ return NULL;
+
return unit;
}
init_expression_parsers();
obstack_init(&temp_obst);
- type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
- type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
- type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
- type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
- type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
- type_string = make_pointer_type(type_const_char, 0);
+ type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
+ type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
+ type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
+ type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
+ type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
+ type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
+ type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
+ type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
+ type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
+ type_string = make_pointer_type(type_const_char, 0);
}
void exit_parser(void)