From: Wolfgang (Blub) Bumiller Date: Sun, 2 Dec 2012 16:57:08 +0000 (+0100) Subject: Get rid of some code duplication X-Git-Tag: 0.1.9~124^2~2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=02ec45363e86b5ab0f74535be979d2e9f9942748;p=xonotic%2Fgmqcc.git Get rid of some code duplication --- diff --git a/ast.c b/ast.c index fc82940..69a16fe 100644 --- a/ast.c +++ b/ast.c @@ -1391,18 +1391,8 @@ bool ast_local_codegen(ast_value *self, ir_function *func, bool param) v->cvq = self->cvq; self->ir_v = v; - if (self->setter) { - if (!ast_global_codegen(self->setter, func->owner, false) || - !ast_function_codegen(self->setter->constval.vfunc, func->owner) || - !ir_function_finalize(self->setter->constval.vfunc->ir_func)) - return false; - } - if (self->getter) { - if (!ast_global_codegen(self->getter, func->owner, false) || - !ast_function_codegen(self->getter->constval.vfunc, func->owner) || - !ir_function_finalize(self->getter->constval.vfunc->ir_func)) - return false; - } + if (!ast_generate_accessors(self, func->owner)) + return false; return true; error: /* clean up */ @@ -1410,6 +1400,29 @@ error: /* clean up */ return false; } +bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir) +{ + if (asvalue->setter) { + if (!ast_global_codegen (asvalue->setter, ir, false) || + !ast_function_codegen(asvalue->setter->constval.vfunc, ir) || + !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func)) + { + compile_error(ast_ctx(asvalue), "internal error: failed to generate setter for `%s`", asvalue->name); + return false; + } + } + if (asvalue->getter) { + if (!ast_global_codegen (asvalue->getter, ir, false) || + !ast_function_codegen(asvalue->getter->constval.vfunc, ir) || + !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func)) + { + compile_error(ast_ctx(asvalue), "internal error: failed to generate getter for `%s`", asvalue->name); + return false; + } + } + return true; +} + bool ast_function_codegen(ast_function *self, ir_builder *ir) { ir_function *irf; diff --git a/ast.h b/ast.h index 004243f..41ef318 100644 --- a/ast.h +++ b/ast.h @@ -626,6 +626,7 @@ void ast_function_delete(ast_function*); const char* ast_function_label(ast_function*, const char *prefix); bool ast_function_codegen(ast_function *self, ir_builder *builder); +bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir); /* Expression union */ diff --git a/parser.c b/parser.c index d2c8bff..c3c4fa2 100644 --- a/parser.c +++ b/parser.c @@ -4591,25 +4591,9 @@ bool parser_finish(const char *output) if (!ast_istype(parser->globals[i], ast_value)) continue; asvalue = (ast_value*)(parser->globals[i]); - if (asvalue->setter) { - if (!ast_global_codegen(asvalue->setter, ir, false) || - !ast_function_codegen(asvalue->setter->constval.vfunc, ir) || - !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func)) - { - printf("failed to generate setter for %s\n", asvalue->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) || - !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func)) - { - printf("failed to generate getter for %s\n", asvalue->name); - ir_builder_delete(ir); - return false; - } + if (!ast_generate_accessors(asvalue, ir)) { + ir_builder_delete(ir); + return false; } } for (i = 0; i < vec_size(parser->fields); ++i) { @@ -4620,25 +4604,9 @@ bool parser_finish(const char *output) continue; if (asvalue->expression.vtype != TYPE_ARRAY) continue; - if (asvalue->setter) { - if (!ast_global_codegen(asvalue->setter, ir, false) || - !ast_function_codegen(asvalue->setter->constval.vfunc, ir) || - !ir_function_finalize(asvalue->setter->constval.vfunc->ir_func)) - { - printf("failed to generate setter for %s\n", asvalue->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) || - !ir_function_finalize(asvalue->getter->constval.vfunc->ir_func)) - { - printf("failed to generate getter for %s\n", asvalue->name); - ir_builder_delete(ir); - return false; - } + if (!ast_generate_accessors(asvalue, ir)) { + ir_builder_delete(ir); + return false; } } for (i = 0; i < vec_size(parser->functions); ++i) {