kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

decl.h (3201B)


      1 #ifndef KIT_DECL_H
      2 #define KIT_DECL_H
      3 
      4 #include <kit/cg.h>
      5 
      6 #include "c_support.h"
      7 #include "type/type.h"
      8 
      9 #ifndef KIT_OBJ_H
     10 typedef KitCgSym ObjSymId;
     11 typedef KitSym ObjSecId;
     12 
     13 #define OBJ_SYM_NONE KIT_CG_SYM_NONE
     14 #define OBJ_SEC_NONE 0u
     15 
     16 enum {
     17   SV_DEFAULT = KIT_CG_VIS_DEFAULT,
     18   SV_HIDDEN = KIT_CG_VIS_HIDDEN,
     19   SV_PROTECTED = KIT_CG_VIS_PROTECTED,
     20   SV_INTERNAL = KIT_CG_VIS_HIDDEN,
     21 };
     22 #endif
     23 
     24 /* C declaration semantics. This layer is deliberately above ObjBuilder:
     25  * ObjBuilder stores object-format facts, while DeclTable owns C linkage,
     26  * storage duration, tentative-definition, static-local, and initializer rules.
     27  */
     28 typedef struct DeclTable DeclTable;
     29 
     30 typedef u32 DeclId;
     31 #define DECL_NONE 0u
     32 
     33 typedef enum DeclStorage {
     34   DS_EXTERN,
     35   DS_STATIC,
     36   DS_AUTO,
     37   DS_REGISTER,
     38   DS_TYPEDEF,
     39 } DeclStorage;
     40 
     41 typedef enum DeclLinkage {
     42   DL_NONE,
     43   DL_INTERNAL,
     44   DL_EXTERNAL,
     45 } DeclLinkage;
     46 
     47 typedef enum DeclState {
     48   DSTATE_NONE,
     49   DSTATE_DECLARED,
     50   DSTATE_TENTATIVE,
     51   DSTATE_DEFINED,
     52   DSTATE_FUNC_DEFINED,
     53 } DeclState;
     54 
     55 typedef enum DeclFlag {
     56   DF_NONE = 0,
     57   DF_THREAD = 1u << 0,
     58   DF_INLINE = 1u << 1,
     59   DF_TENTATIVE = 1u << 2,
     60   DF_USED = 1u << 3,
     61   DF_WEAK = 1u << 4,
     62   DF_STATIC_LOCAL = 1u << 5,
     63   /* Phase 2 attribute-honoring flags. DF_NORETURN is the unified bit for
     64    * _Noreturn and __attribute__((noreturn)); the inline-policy flags flow to
     65    * CG/opt as function and direct-call inline policy. */
     66   DF_NORETURN = 1u << 6,
     67   DF_ALWAYS_INLINE = 1u << 7,
     68   DF_NOINLINE = 1u << 8,
     69   DF_GNU_INLINE = 1u << 9,
     70 } DeclFlag;
     71 
     72 typedef struct Decl {
     73   DeclId id;
     74   Sym name;
     75   const Type* type;
     76   ObjSymId obj_sym;
     77   ObjSecId section_id; /* optional explicit section; OBJ_SEC_NONE => default */
     78   SrcLoc loc;
     79   u8 storage;    /* DeclStorage */
     80   u8 linkage;    /* DeclLinkage */
     81   u8 visibility; /* SymVis */
     82   u8 pad;
     83   u32 flags; /* DeclFlag */
     84   /* Phase 2 attribute carriers — populated by attr_list_to_decl. */
     85   u32 align; /* explicit alignment from _Alignas or aligned(N); 0=natural */
     86   Sym alias_target; /* target name for __attribute__((alias("..."))); 0=none */
     87   /* GCC asm-label rename (`extern T f(...) __asm__("altname")`): when nonzero,
     88    * the emitted/referenced linkage symbol uses this name (run through the
     89    * target's linkage-name rule) instead of `name`. `name` stays the C source
     90    * identifier for diagnostics/debug. mingw redirects e.g. time -> _time64. */
     91   Sym asm_name;
     92   /* Wasm import overrides from __attribute__((import_module("M"),
     93    * import_name("F"))). Honored only by the wasm backend; ignored by other
     94    * targets. Both 0 = backend default (module="env", name=<sym name>). */
     95   Sym wasm_import_module;
     96   Sym wasm_import_name;
     97 } Decl;
     98 
     99 DeclTable* decl_new(Compiler*, Pool*, KitCg*);
    100 void decl_free(DeclTable*);
    101 
    102 DeclId decl_declare(DeclTable*, const Decl*);
    103 const Decl* decl_get(const DeclTable*, DeclId);
    104 ObjSymId decl_obj_sym(const DeclTable*, DeclId);
    105 
    106 /* Re-apply linkage-affecting attributes gathered from a later redeclaration or
    107  * definition onto an already-declared symbol. Currently handles `weak`, which
    108  * C allows on any declaration of a symbol. */
    109 void decl_apply_redecl_flags(DeclTable*, DeclId, u32 new_flags);
    110 
    111 #endif