From 28989b9fcb9970000cf1c5cc7e44b7c5d573e33b Mon Sep 17 00:00:00 2001 From: Teddy Date: Sat, 5 Apr 2014 14:12:35 +0800 Subject: add a dirty hack to `semantic_func` --- semantics.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/semantics.c b/semantics.c index cc7bd5e..aa97c6b 100644 --- a/semantics.c +++ b/semantics.c @@ -455,8 +455,8 @@ CVar_t semantics_declr(CNode *, CType_t, CScope_t); CVar_t semantics_p_decl(CNode *p, CScope_t scope) { CHECK_TYPE(p, PLAIN_DECL); CVar_t var = semantics_declr(p->chd->next, - semantics_type_spec(p->chd, scope), - scope); + semantics_type_spec(p->chd, scope), + scope); if (!type_is_complete(var->type)) { sprintf(err_buff, "parameter '%s' has incomplete type", var->name); @@ -1368,12 +1368,20 @@ CVar_t semantics_func(CNode *p, CScope_t scope) { } scope->func = func; + cscope_enter(scope); /* enter function local scope */ func->rec.func.params = semantics_params(chd, scope); /* check params */ - if (cscope_push_var(scope, res)) - old = res; - cscope_enter(scope); /* enter into function local scope */ - { + { /* Note: here is a dirty hack to forcibly push function definition to + the global scope, while all the types specified in parameters retain in local scope. + The key point is to make sure semantics_params does not push any var */ + CSNode *ntop = scope->top; CVar_t p; + scope->top = ntop->next; + scope->lvl--; + if (cscope_push_var(scope, res)) + old = res; + scope->top = ntop; + scope->lvl++; + for (p = func->rec.func.params; p; p = p->next) cscope_push_var(scope, p); } -- cgit v1.2.3