/*
- * Copyright (C) 2012
+ * Copyright (C) 2012, 2013
* Wolfgang Bumiller
* Dale Weiler
*
self->expression.params = NULL;
self->expression.count = 0;
self->expression.flags = 0;
+ self->expression.varparam = NULL;
}
static void ast_expression_delete(ast_expression *self)
self->getter = NULL;
self->desc = NULL;
+ self->argcounter = NULL;
+
return self;
}
{
if (self->name)
mem_d((void*)self->name);
+ if (self->argcounter)
+ mem_d((void*)self->argcounter);
if (self->hasvalue) {
switch (self->expression.vtype)
{
void ast_ternary_delete(ast_ternary *self)
{
- ast_unref(self->cond);
- ast_unref(self->on_true);
- ast_unref(self->on_false);
+ /* the if()s are only there because computed-gotos can set them
+ * to NULL
+ */
+ if (self->cond) ast_unref(self->cond);
+ if (self->on_true) ast_unref(self->on_true);
+ if (self->on_false) ast_unref(self->on_false);
ast_expression_delete((ast_expression*)self);
mem_d(self);
}
mem_d(self);
}
-ast_label* ast_label_new(lex_ctx ctx, const char *name)
+ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
{
ast_instantiate(ast_label, ctx, ast_label_delete);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
- self->name = util_strdup(name);
- self->irblock = NULL;
- self->gotos = NULL;
+ self->expression.vtype = TYPE_NOEXPR;
+
+ self->name = util_strdup(name);
+ self->irblock = NULL;
+ self->gotos = NULL;
+ self->undefined = undefined;
return self;
}
ast_side_effects(self) = true;
- self->params = NULL;
- self->func = funcexpr;
+ self->params = NULL;
+ self->func = funcexpr;
+ self->va_count = NULL;
ast_type_adopt(self, funcexpr->expression.next);
if (self->func)
ast_unref(self->func);
+ if (self->va_count)
+ ast_unref(self->va_count);
+
ast_expression_delete((ast_expression*)self);
mem_d(self);
}
bool ast_call_check_types(ast_call *self)
{
+ char texp[1024];
+ char tgot[1024];
size_t i;
bool retval = true;
const ast_expression *func = self->func;
for (i = 0; i < count; ++i) {
if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
{
- char texp[1024];
- char tgot[1024];
ast_type_to_string(self->params[i], tgot, sizeof(tgot));
ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
retval = false;
}
}
+ count = vec_size(self->params);
+ if (count > vec_size(func->expression.params) && func->expression.varparam) {
+ for (; i < count; ++i) {
+ if (!ast_compare_type(self->params[i], func->expression.varparam))
+ {
+ ast_type_to_string(self->params[i], tgot, sizeof(tgot));
+ ast_type_to_string(func->expression.varparam, texp, sizeof(texp));
+ compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
+ (unsigned int)(i+1), texp, tgot);
+ /* we don't immediately return */
+ retval = false;
+ }
+ }
+ }
return retval;
}
vtype->hasvalue = true;
vtype->constval.vfunc = self;
+ self->varargs = NULL;
+ self->argc = NULL;
+
return self;
}
vec_free(self->blocks);
vec_free(self->breakblocks);
vec_free(self->continueblocks);
+ if (self->varargs)
+ ast_delete(self->varargs);
+ if (self->argc)
+ ast_delete(self->argc);
mem_d(self);
}
self->constval.vfunc->ir_func = func;
self->ir_v = func->value;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
/* The function is filled later on ast_function_codegen... */
return true;
}
v->unique_life = true;
v->locked = true;
array->ir_v = self->ir_v = v;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
namelen = strlen(self->name);
name = (char*)mem_a(namelen + 16);
array->ir_values[ai]->context = ast_ctx(self);
array->ir_values[ai]->unique_life = true;
array->ir_values[ai]->locked = true;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
}
mem_d(name);
}
return false;
v->context = ast_ctx(self);
self->ir_v = v;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
}
return true;
}
v->context = ast_ctx(self);
v->unique_life = true;
v->locked = true;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ v->flags |= IR_FLAG_INCLUDE_DEF;
namelen = strlen(self->name);
name = (char*)mem_a(namelen + 16);
self->ir_values[ai]->context = ast_ctx(self);
self->ir_values[ai]->unique_life = true;
self->ir_values[ai]->locked = true;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
}
mem_d(name);
}
/* link us to the ir_value */
v->cvq = self->cvq;
self->ir_v = v;
+ if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
+ self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
return true;
error: /* clean up */
func->flags |= IR_FLAG_HAS_ARRAYS;
- if (param) {
+ if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
compile_error(ast_ctx(self), "array-parameters are not supported");
return false;
}
{
ir_function *irf;
ir_value *dummy;
- ast_expression_common *ec;
+ ast_expression_common *ec;
+ ast_expression_codegen *cgen;
size_t i;
(void)ir;
}
}
+ if (self->varargs) {
+ if (!ast_local_codegen(self->varargs, self->ir_func, true))
+ return false;
+ irf->max_varargs = self->varargs->expression.count;
+ }
+
if (self->builtin) {
irf->builtin = self->builtin;
return true;
return false;
}
+ if (self->argc) {
+ ir_value *va_count;
+ if (!ast_local_codegen(self->argc, self->ir_func, true))
+ return false;
+ cgen = self->argc->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
+ return false;
+ if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
+ va_count, ir_builder_get_va_count(ir)))
+ {
+ return false;
+ }
+ }
+
for (i = 0; i < vec_size(self->blocks); ++i) {
- ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
- if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
+ cgen = self->blocks[i]->expression.codegen;
+ if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
return false;
}
*out = ir_call_value(call);
self->expression.outr = *out;
+ (*out)->vtype = self->expression.vtype;
+ codegen_output_type(self, *out);
return true;
}
compile_error(ast_ctx(self), "array indexing here needs an integer constant");
return false;
}
+ (*out)->vtype = self->expression.vtype;
+ codegen_output_type(self, *out);
return true;
}
size_t i;
ir_value *dummy;
+ if (self->undefined) {
+ compile_error(ast_ctx(self), "internal error: ast_label never defined");
+ return false;
+ }
+
*out = NULL;
if (lvalue) {
compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
vec_push(params, param);
}
+ /* varargs counter */
+ if (self->va_count) {
+ ir_value *va_count;
+ ir_builder *builder = func->curblock->owner->owner;
+ cgen = self->va_count->expression.codegen;
+ if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
+ return false;
+ if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
+ ir_builder_get_va_count(builder), va_count))
+ {
+ return false;
+ }
+ }
+
callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
ast_function_label(func, "call"),
funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));