self->ir_values = NULL;
self->ir_value_count = 0;
+ self->setter = NULL;
+ self->getter = NULL;
+
return self;
}
* on all the globals.
*/
if (!self->ir_v) {
- asterror(ast_ctx(self), "ast_value used before generated (%s)", self->name);
+ char typename[1024];
+ ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
+ asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
return false;
}
*out = self->ir_v;
bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
{
ir_value *v = NULL;
+
if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
{
ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
ast_value *fval = NULL;
ast_block *body;
ast_value *index, *value;
- varentry_t entry;
if (!ast_istype(array->expression.next, ast_value)) {
parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
if (!ast_function_blocks_add(func, body))
goto cleanup2;
- entry.name = util_strdup(funcname);
- entry.var = (ast_expression*)fval;
- if (!parser_t_globals_add(parser, entry)) {
- mem_d(entry.name);
- goto cleanup2;
- }
- if (!parser_t_functions_add(parser, func))
- goto cleanup2;
+ array->setter = fval;
return true;
cleanup:
ast_value *fval = NULL;
ast_block *body;
ast_value *index;
- varentry_t entry;
if (!ast_istype(array->expression.next, ast_value)) {
parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
if (!ast_function_blocks_add(func, body))
goto cleanup2;
- entry.name = util_strdup(funcname);
- entry.var = (ast_expression*)fval;
- if (!parser_t_globals_add(parser, entry)) {
- mem_d(entry.name);
- goto cleanup2;
- }
- if (!parser_t_functions_add(parser, func))
- goto cleanup2;
+ array->getter = fval;
return true;
cleanup:
return false;
}
}
+ for (i = 0; i < parser->globals_count; ++i) {
+ ast_value *asvalue;
+ if (!ast_istype(parser->globals[i].var, ast_value))
+ continue;
+ asvalue = (ast_value*)(parser->globals[i].var);
+ if (asvalue->setter) {
+ if (!ast_global_codegen(asvalue->setter, ir, false) ||
+ !ast_function_codegen(asvalue->setter->constval.vfunc, ir))
+ {
+ printf("failed to generate setter for %s\n", parser->globals[i].name);
+ ir_builder_delete(ir);
+ return false;
+ }
+ }
+ if (asvalue->getter) {
+ if (!ast_global_codegen(asvalue->getter, ir, false) ||
+ !ast_function_codegen(asvalue->getter->constval.vfunc, ir))
+ {
+ printf("failed to generate getter for %s\n", parser->globals[i].name);
+ ir_builder_delete(ir);
+ return false;
+ }
+ }
+ }
for (i = 0; i < parser->functions_count; ++i) {
if (!ast_function_codegen(parser->functions[i], ir)) {
printf("failed to generate function %s\n", parser->functions[i]->name);