📄 symtab.c
字号:
printf("%u, ", that->children[i]); printf("\n"); #endif if (!that->htab) { that->htab = new_hashtab(); if (!that->htab) return 0; } return hashtab_insert(that->htab,entry);}static voidpo_deltabs(scopetab_t *that){ int j; if (!that) return; for (j = 0; j < that->nchild; j++) po_deltabs(that->children[j]); free_scopetab(that);}static intchild_insert(scopetab_t *mom, scopetab_t *kid){ int j; if (0) printf(" child insert - level %d\n", mom->level); if (mom->nchild >= mom->size) { scopetab_t **oldkids = mom->children; mom->size += mom->size; mom->children = (scopetab_t **) emalloc( sizeof(scopetab_t*) * mom->size ); for (j=0; j < mom->nchild; j++) mom->children[j] = oldkids[j]; } mom->children[mom->nchild] = kid; (mom->nchild)++; return 1;}intfind_symbol(scopetab_t *st, symentry_t *se){ int i; hashtab_t *h; symentry_t *e; if (!st || !se) { printf("<%u,%u> ",st,se); return 0; } h = st->htab; if (h) for (i = 0; i < h->tsize; i++) for (e = h->tab[i]; e; e = e->next) if (e == se) {same: printf("%s:%d: (scopetab %u symentry %u)", se->fn, se->ln, st, se); if (se->container) printf("cont:<%s> ", x_stmnt(se->container)); if (Verbose&2) { switch (st->level) { case 0: printf("Top"); break; case 1: printf("Global"); break; case 2: printf("File"); break; case 3: printf("Struct/FctParams"); break; default: printf("Block"); break; } } return 1; } else if (strcmp(e->nme->str, se->nme->str) == 0) { if (1 || (Verbose&2)) { printf("<rogue match> "); goto same; } return 0; } if (find_symbol(st->parent, se)) return 1; printf("\n===couldn't find it:===\n"); vis = 1; show_scopetab(st, 0, stdout); vis = 0; printf("===\n\t"); return 0;}voidcheck_struct_use(FILE *fd, scopetab_t *that){ scopetab_t *z; symentry_t *x; int mustcheck, j; vis = 1; if (that->htab) { mustcheck = 0; for (z = that; z; z = z->parent) if (z->owner && strcmp(z->owner, "-") != 0) { if (z->owner_t == TN_OBJ_DEF) mustcheck = 1; break; } if (mustcheck) for (j = 0; j < that->htab->tsize; j++) for (x = that->htab->tab[j]; x; x = x->next) {#if DBG printf("%s (%u) is %sused, owner: %s type: %d\n", x->nme->str, x, x->used?"":"un", that->owner?that->owner:"--", that->owner_t);#endif if (!uno_ignore(x)) /* unused field */ { fprintf(fd, "%s\t%s\t%s\t%d\n", (x->used)?"y":"z", /* set in dflow.c */ x->nme->str, x->node->hdr.fnm, x->node->hdr.line); } } } for (j = 0; j < that->nchild; j++) check_struct_use(fd, that->children[j]);}voidstruct_fields(FILE *fd){ extern context_t *contxt; check_struct_use(fd, contxt->syms->root); /* uno cannot tell that contxt has a value here */}static voidshow_scopetab(scopetab_t *that, int tabs, FILE *fp){ int j;#if DBG fprintf(fp, "scopetab %u, level: %d nsyms: %d nchild: %d\n", that, that->level, that->nsyms, that->nchild);#endif if (that->htab) { scopetab_t *z; char prepped[512]; switch (that->level) { case 0: strcpy(prepped, "scope level 0?"); break; case 1: strcpy(prepped, "Global scope:"); break; case 2: strcpy(prepped, "File scope:"); break; case 3: strcpy(prepped, "Struct/FctParams:"); break; default: strcpy(prepped, "Block:"); break; } for (z = that; z; z = z->parent) if (z->owner && strcmp(z->owner, "-") != 0) { strcat(prepped, "\t"); strcat(prepped, z->owner); switch (z->owner_t) { case TN_OBJ_DEF: strcat(prepped, " [struct/union]"); break; case TN_FUNC_DEF: strcat(prepped, " [fct]"); break; case 0: strcat(prepped, " [0]"); break; default: strcat(prepped, " [?]"); break; } break; }#if DBG else strcat(prepped, "-u-");#endif dotabs(tabs, fp); fprintf(fp, "%s\n", prepped); show_hashtab(that->htab, tabs, fp); fprintf(fp, "\n"); } for (j = 0; j < that->nchild; j++) show_scopetab(that->children[j], tabs+1, fp);}voidset_owner(scopetab_t *p, char *s, int tp){ int i; if (0) printf(" setowner for %u %s %d (%s)\n", p, s, tp, p->owner?p->owner:"not yet set"); if (p->owner) return; /* already named */ p->owner = s; p->owner_t = tp; for (i = 0; i < p->nchild; i++) /* propagate to nested unnamed scopes */ set_owner(p->children[i], s, tp);}voidname_scope(context_t *q, char *s, int tp){ symtab_t *p = q->syms; if (DBG) printf("name_scope %u -- owner=%s (owner_t=%d) clevel %d, level %d\n", p->current, s, tp, p->clevel, p->current->level); set_owner(p->current, s, tp); if (Verbose&2) printf("SET level %d,%d, %s\n", p->clevel, p->current->level, s);}symtab_t*new_symtab(void){ symtab_t *that; if (freesymt) { that = freesymt; freesymt = that->nxt; memset(that, 0, sizeof(symtab_t)); } else that = (symtab_t *) emalloc(sizeof(symtab_t)); that->root = new_scopetab(NULL); that->clevel = EXTERN_SCOPE; that->current = that->root; return that;}symentry_t*symtab_lookup(symtab_t *that, str_t *nme){ symentry_t *ret = NULL; if (that->current) ret = scopetab_find(that->current, nme); return ret;}static voidfree_symtab(symtab_t *that){ if (!that) return; /* post-order traversal */ po_deltabs(that->root); that->nxt = freesymt; freesymt = that;}symentry_t*symtab_insert(symtab_t *that, symentry_t *entry){ symentry_t *t;#if DBG printf(" symtab insert %s (clevel %d currentlevel %d) %u\n", entry->nme->str, that->clevel, that->current->level, entry);#endif while (that->clevel > that->current->level) { scopetab_t *child = new_scopetab(that->current); if (!child || !child_insert(that->current,child)) return NULL; that->current = child; } t = scopetab_insert(that->current,entry);#if DBG printf(" inserted at %u\n", t);#endif return t;}symentry_t *symtab_insert_at(symtab_t *that, symentry_t *entry, int level){ scopetab_t *scp; scopetab_t *child;#ifdef MORE_VERBOSE printf("scope level %d; ", that->current->level); printf("current level %d; ", that->clevel); printf("request level %d\n", level);#endif while ((that->clevel > that->current->level) && (that->clevel >= level)) { child = new_scopetab(that->current); if (!child || !child_insert(that->current,child)) return (symentry_t *) 0; that->current = child; } scp = that->current; while (scp && (scp->level > level)) scp = scp->parent; if (scp) return scopetab_insert(scp,entry); return (symentry_t *) 0;}intst_enter_scope(symtab_t *that){ return ++(that->clevel);}voidst_exit_scope(symtab_t *that){ if (!that) return; (that->clevel)--; if (that->current->level > that->clevel) that->current = that->current->parent;}voidshow_symtab(symtab_t *that, FILE *fp){ vis = 1; show_scopetab(that->root, 0, fp);}context_t*new_context(void){ context_t *that = (context_t *) emalloc( sizeof(context_t) ); that->labels = new_symtab(); that->tags = new_symtab(); that->syms = new_symtab(); return that;}voidfree_context(context_t *that){ if (!that) return; free_symtab(that->labels); free_symtab(that->tags); free_symtab(that->syms); efree(that);}intenter_scope(context_t *that){ if (!that) return 0;#ifdef MORE_VERBOSE printf(" Enter Scope: %d\n", that->syms->clevel + 1);#endif st_enter_scope(that->labels); st_enter_scope(that->tags); return st_enter_scope(that->syms);}voidexit_scope(context_t *that){#ifdef MORE_VERBOSE printf(" Exit Scope: %d\n", that->syms->clevel);#endif if (!that) return; st_exit_scope(that->labels); st_exit_scope(that->tags); st_exit_scope(that->syms);}voidexit_scopes(context_t *that, int newlev){ if (newlev < EXTERN_SCOPE) newlev = EXTERN_SCOPE; while (newlev < that->syms->current->level) exit_scope(that);}typedef struct Cname { char *vn; struct Cname *nxt;} Cname;static Cname *cnames;voidadd_constant(char *p){ Cname *s; for (s = cnames; s; s = s->nxt) if (strcmp(p, s->vn) == 0) return; s = (Cname *) emalloc(sizeof(Cname)); s->vn = (char *) emalloc(strlen(p)+1); strcpy(s->vn, p); s->nxt = cnames; cnames = s;}intis_constant(char *p){ Cname *s; for (s = cnames; s; s = s->nxt) if (strcmp(p, s->vn) == 0) return 1; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -