scope.c (4112B)
1 #include <stdbool.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include "util.h" 5 #include "cc.h" 6 7 struct scope filescope; 8 9 void 10 scopeinit(void) 11 { 12 static struct decl builtins[] = { 13 {.name = "__builtin_alloca", .kind = DECLBUILTIN, .u.builtin = BUILTINALLOCA}, 14 {.name = "__builtin_constant_p", .kind = DECLBUILTIN, .u.builtin = BUILTINCONSTANTP}, 15 {.name = "__builtin_expect", .kind = DECLBUILTIN, .u.builtin = BUILTINEXPECT}, 16 {.name = "__builtin_inff", .kind = DECLBUILTIN, .u.builtin = BUILTININFF}, 17 {.name = "__builtin_nanf", .kind = DECLBUILTIN, .u.builtin = BUILTINNANF}, 18 {.name = "__builtin_offsetof", .kind = DECLBUILTIN, .u.builtin = BUILTINOFFSETOF}, 19 {.name = "__builtin_types_compatible_p", 20 .kind = DECLBUILTIN, 21 .u.builtin = BUILTINTYPESCOMPATIBLEP}, 22 {.name = "__builtin_unreachable", .kind = DECLBUILTIN, .u.builtin = BUILTINUNREACHABLE}, 23 {.name = "__builtin_va_arg", .kind = DECLBUILTIN, .u.builtin = BUILTINVAARG}, 24 {.name = "__builtin_va_copy", .kind = DECLBUILTIN, .u.builtin = BUILTINVACOPY}, 25 {.name = "__builtin_va_end", .kind = DECLBUILTIN, .u.builtin = BUILTINVAEND}, 26 {.name = "__builtin_va_start", .kind = DECLBUILTIN, .u.builtin = BUILTINVASTART}, 27 {.name = "__builtin_c23_va_start", .kind = DECLBUILTIN, .u.builtin = BUILTINVASTART}, 28 {.name = "__atomic_load_n", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICLOAD}, 29 {.name = "__atomic_store_n", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICSTORE}, 30 {.name = "__atomic_exchange_n", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICEXCHANGE}, 31 {.name = "__atomic_compare_exchange_n", 32 .kind = DECLBUILTIN, 33 .u.builtin = BUILTINATOMICCOMPAREEXCHANGE}, 34 {.name = "__atomic_fetch_add", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICFETCHADD}, 35 {.name = "__atomic_fetch_sub", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICFETCHSUB}, 36 {.name = "__atomic_fetch_and", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICFETCHAND}, 37 {.name = "__atomic_fetch_or", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICFETCHOR}, 38 {.name = "__atomic_fetch_xor", .kind = DECLBUILTIN, .u.builtin = BUILTINATOMICFETCHXOR}, 39 }; 40 static struct decl valist; 41 struct decl *d; 42 43 for (d = builtins; d < builtins + countof(builtins); ++d) 44 scopeputdecl(&filescope, d); 45 valist.name = "__builtin_va_list"; 46 valist.kind = DECLTYPE; 47 valist.type = targ->typevalist; 48 scopeputdecl(&filescope, &valist); 49 } 50 51 struct scope * 52 mkscope(struct scope *parent) 53 { 54 struct scope *s; 55 56 s = xmalloc(sizeof(*s)); 57 s->decls.len = 0; 58 s->tags.len = 0; 59 s->breaklabel = parent->breaklabel; 60 s->continuelabel = parent->continuelabel; 61 s->switchcases = parent->switchcases; 62 s->parent = parent; 63 64 return s; 65 } 66 67 struct scope * 68 delscope(struct scope *s) 69 { 70 struct scope *parent = s->parent; 71 72 if (s->decls.len) 73 mapfree(&s->decls, NULL); 74 if (s->tags.len) 75 mapfree(&s->tags, NULL); 76 free(s); 77 78 return parent; 79 } 80 81 struct decl * 82 scopegetdecl(struct scope *s, const char *name, bool recurse) 83 { 84 struct decl *d; 85 struct mapkey k; 86 87 mapkey(&k, name, strlen(name)); 88 do { 89 d = s->decls.len ? mapget(&s->decls, &k, NULL) : NULL; 90 s = s->parent; 91 } while (!d && s && recurse); 92 93 return d; 94 } 95 96 struct type * 97 scopegettag(struct scope *s, const char *name, bool recurse) 98 { 99 struct type *t; 100 struct mapkey k; 101 102 mapkey(&k, name, strlen(name)); 103 do { 104 t = s->tags.len ? mapget(&s->tags, &k, NULL) : NULL; 105 s = s->parent; 106 } while (!t && s && recurse); 107 108 return t; 109 } 110 111 void 112 scopeputdecl(struct scope *s, struct decl *d) 113 { 114 struct mapkey k; 115 size_t i; 116 117 if (!s->decls.len) 118 mapinit(&s->decls, 32); 119 mapkey(&k, d->name, strlen(d->name)); 120 mapput(&s->decls, &k, &i); 121 s->decls.vals[i].p = d; 122 } 123 124 void 125 scopeputtag(struct scope *s, const char *name, struct type *t) 126 { 127 struct mapkey k; 128 size_t i; 129 130 if (!s->tags.len) 131 mapinit(&s->tags, 32); 132 mapkey(&k, name, strlen(name)); 133 mapput(&s->tags, &k, &i); 134 s->tags.vals[i].p = t; 135 }