ast_expression_codegen *codegen)
{
self->expression.codegen = codegen;
+ self->expression.vtype = TYPE_VOID;
+ self->expression.next = NULL;
+}
+
+static void ast_expression_delete(ast_expression *self)
+{
+ if (self->expression.next)
+ ast_delete(self->expression.next);
+}
+
+static void ast_expression_delete_full(ast_expression *self)
+{
+ ast_expression_delete(self);
+ mem_d(self);
+}
+
+static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
+{
+ const ast_expression_common *cpex;
+ ast_expression_common *selfex;
+
+ if (!ex)
+ return NULL;
+ else
+ {
+ ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
+
+ cpex = &ex->expression;
+ selfex = &self->expression;
+
+ selfex->vtype = cpex->vtype;
+ if (cpex->next)
+ {
+ selfex->next = ast_type_copy(ctx, cpex->next);
+ if (!selfex->next) {
+ mem_d(self);
+ return NULL;
+ }
+ }
+ else
+ selfex->next = NULL;
+
+ /* This may never be codegen()d */
+ selfex->codegen = NULL;
+ return self;
+ }
}
ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
self->expression.node.keep = true; /* keep */
self->name = name ? util_strdup(name) : NULL;
- self->vtype = t;
- self->next = NULL;
+ self->expression.vtype = t;
+ self->expression.next = NULL;
MEM_VECTOR_INIT(self, params);
self->isconst = false;
memset(&self->constval, 0, sizeof(self->constval));
for (i = 0; i < self->params_count; ++i)
ast_value_delete(self->params[i]); /* delete, the ast_function is expected to die first */
MEM_VECTOR_CLEAR(self, params);
- if (self->next) /* delete, not unref, types are always copied */
- ast_delete(self->next);
if (self->isconst) {
- switch (self->vtype)
+ switch (self->expression.vtype)
{
case TYPE_STRING:
mem_d((void*)self->constval.vstring);
break;
}
}
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
{
ast_unref(self->left);
ast_unref(self->right);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
{
+ const ast_expression *outtype;
+
ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
+
+ if (field->expression.vtype != TYPE_FIELD) {
+ mem_d(self);
+ return NULL;
+ }
+
+ outtype = field->expression.next;
+ if (!outtype) {
+ mem_d(self);
+ /* Error: field has no type... */
+ return NULL;
+ }
+
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
+ self->expression.vtype = outtype->expression.vtype;
+ self->expression.next = ast_type_copy(ctx, outtype->expression.next);
+
self->entity = entity;
self->field = field;
{
ast_unref(self->entity);
ast_unref(self->field);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
ast_unref(self->cond);
ast_unref(self->on_true);
ast_unref(self->on_false);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
ast_unref(self->cond);
ast_unref(self->on_true);
ast_unref(self->on_false);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
{
ast_unref(self->dest);
ast_unref(self->source);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
for (i = 0; i < self->locals_count; ++i)
ast_delete(self->locals[i]);
MEM_VECTOR_CLEAR(self, locals);
+ ast_expression_delete((ast_expression*)self);
mem_d(self);
}
if (!vtype ||
vtype->isconst ||
- vtype->vtype != TYPE_FUNCTION)
+ vtype->expression.vtype != TYPE_FUNCTION)
{
mem_d(self);
return NULL;
bool ast_global_codegen(ast_value *self, ir_builder *ir)
{
ir_value *v = NULL;
- if (self->isconst && self->vtype == TYPE_FUNCTION)
+ if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
{
ir_function *func = ir_builder_create_function(ir, self->name);
if (!func)
return true;
}
- v = ir_builder_create_global(ir, self->name, self->vtype);
+ v = ir_builder_create_global(ir, self->name, self->expression.vtype);
if (!v)
return false;
if (self->isconst) {
- switch (self->vtype)
+ switch (self->expression.vtype)
{
case TYPE_FLOAT:
if (!ir_value_set_float(v, self->constval.vfloat))
*/
goto error;
default:
- printf("TODO: global constant type %i\n", self->vtype);
+ printf("TODO: global constant type %i\n", self->expression.vtype);
break;
}
}
bool ast_local_codegen(ast_value *self, ir_function *func)
{
ir_value *v = NULL;
- if (self->isconst && self->vtype == TYPE_FUNCTION)
+ if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
{
/* Do we allow local functions? I think not...
* this is NOT a function pointer atm.
return false;
}
- v = ir_function_create_local(func, self->name, self->vtype);
+ v = ir_function_create_local(func, self->name, self->expression.vtype);
if (!v)
return false;
* I suppose the IR will have to deal with this
*/
if (self->isconst) {
- switch (self->vtype)
+ switch (self->expression.vtype)
{
case TYPE_FLOAT:
if (!ir_value_set_float(v, self->constval.vfloat))
goto error;
break;
default:
- printf("TODO: global constant type %i\n", self->vtype);
+ printf("TODO: global constant type %i\n", self->expression.vtype);
break;
}
}