decl_attrs.c (2857B)
1 #include "decl/decl_attrs.h" 2 3 #include <string.h> 4 5 /* Bare `__attribute__((aligned))` (no argument) means "biggest scalar 6 * alignment". kit's targets all have `_Alignof(long double) == 16` 7 * (x86_64 SysV, AArch64 AAPCS, RISC-V LP64D), so 16 is a valid v1 8 * stand-in across the board. */ 9 #define ATTR_ALIGNED_DEFAULT 16u 10 11 static void apply_section(Compiler* c, DeclTable* t, const Attr* a, Decl* out) { 12 (void)c; 13 if (!t || a->v.sym == 0) return; 14 out->section_id = a->v.sym; 15 } 16 17 static void apply_visibility(Compiler* c, const Attr* a, Decl* out) { 18 if (a->v.sym == 0) { 19 compiler_panic(c, a->loc, "visibility attribute missing argument"); 20 } 21 KitSlice ss = kit_sym_str(c, a->v.sym); 22 const char* s = ss.s; 23 if (s && kit_slice_eq_cstr(ss, "default")) { 24 out->visibility = SV_DEFAULT; 25 } else if (s && kit_slice_eq_cstr(ss, "hidden")) { 26 out->visibility = SV_HIDDEN; 27 } else if (s && kit_slice_eq_cstr(ss, "protected")) { 28 out->visibility = SV_PROTECTED; 29 } else if (s && kit_slice_eq_cstr(ss, "internal")) { 30 out->visibility = SV_INTERNAL; 31 } else { 32 compiler_panic(c, a->loc, 33 "unknown visibility '%.*s' (expected default|hidden|" 34 "protected|internal)", 35 KIT_SLICE_ARG(kit_slice_cstr(s ? s : ""))); 36 } 37 } 38 39 void attr_list_to_decl(Compiler* c, DeclTable* t, const Attr* attrs, 40 Decl* out) { 41 for (const Attr* a = attrs; a; a = a->next) { 42 switch ((AttrKind)a->kind) { 43 case ATTR_ALIGNED: { 44 u32 v = (a->nargs == 0) ? ATTR_ALIGNED_DEFAULT : (u32)a->v.i; 45 if (v > out->align) out->align = v; 46 break; 47 } 48 case ATTR_SECTION: 49 apply_section(c, t, a, out); 50 break; 51 case ATTR_USED: 52 out->flags |= DF_USED; 53 break; 54 case ATTR_WEAK: 55 out->flags |= DF_WEAK; 56 break; 57 case ATTR_NORETURN: 58 out->flags |= DF_NORETURN; 59 break; 60 case ATTR_ALWAYS_INLINE: 61 out->flags |= DF_ALWAYS_INLINE; 62 break; 63 case ATTR_NOINLINE: 64 out->flags |= DF_NOINLINE; 65 break; 66 case ATTR_GNU_INLINE: 67 out->flags |= DF_GNU_INLINE; 68 break; 69 case ATTR_VISIBILITY: 70 apply_visibility(c, a, out); 71 break; 72 case ATTR_ALIAS: 73 out->alias_target = a->v.sym; 74 break; 75 case ATTR_IMPORT_MODULE: 76 if (a->v.sym == 0) { 77 compiler_panic(c, a->loc, 78 "import_module attribute requires a string argument"); 79 } 80 out->wasm_import_module = a->v.sym; 81 break; 82 case ATTR_IMPORT_NAME: 83 if (a->v.sym == 0) { 84 compiler_panic(c, a->loc, 85 "import_name attribute requires a string argument"); 86 } 87 out->wasm_import_name = a->v.sym; 88 break; 89 default: 90 break; 91 } 92 } 93 }