declarations_and_types.odin (4187B)
1 // In here we put types that can be used within any file. They are said to be independent, because 2 // they do not depend on libclang. 3 // 4 // It is FORBIDDEN to import libclang or use anything from libclang in here, as the outputter 5 // will use these types. The outputter does not, and should not, have any knowledge of clang. 6 package bindgen2 7 8 // A type identifier is either a string or an index that points to another type. The string used to 9 // refer to a type just by its name (for example, when a struct field refers to some other type). 10 // The index is often used when a struct contains a field of anonymous type. 11 Definition :: union { 12 Type_Name, 13 Fixed_Value, 14 Type_Index, 15 Macro_Name, 16 } 17 18 Type_Name :: distinct string 19 20 Fixed_Value :: distinct string 21 22 Macro_Name :: distinct string 23 // Just an index into an array of types. Use to point out the definition of another type. 24 Type_Index :: distinct int 25 26 27 28 TYPE_INDEX_NONE :: Type_Index(0) 29 30 Decl_List :: ^[dynamic]Decl 31 Type_List :: ^[dynamic]Type 32 33 add_type :: proc(array: Type_List, t: Type) -> Type_Index { 34 idx := len(array) 35 append(array, t) 36 return Type_Index(idx) 37 } 38 39 add_decl :: proc(decls: Decl_List, d: Decl) { 40 append(decls, d) 41 } 42 43 Decl :: struct { 44 name: string, 45 46 def: Definition, 47 comment_before: string, 48 side_comment: string, // rename to comment_on_right 49 50 invalid: bool, 51 52 is_forward_declare: bool, 53 54 original_line: int, 55 56 explicitly_created: bool, 57 58 // TODO can we get these two for all fields 59 60 // Only used for macros. 61 explicit_whitespace_before_side_comment: int, 62 63 // Only used for macros. 64 explicit_whitespace_after_name: int, 65 66 // This declaration originates from a C macro. 67 // 68 // TODO: We currently have three "categories": types, procs and macros. Should this be enumified 69 // perhaps? The proc info comes from 'def' currently 70 from_macro: bool, 71 } 72 73 Type :: union #no_nil { 74 Type_Unknown, 75 Type_Pointer, 76 Type_Multipointer, 77 Type_Pointer_By_Ptr, 78 Type_Raw_Pointer, 79 Type_CString, 80 Type_Struct, 81 Type_Enum, 82 Type_Bit_Set, 83 Type_Bit_Set_Constant, 84 Type_Alias, 85 Type_Fixed_Array, 86 Type_Procedure, 87 } 88 89 Type_Pointer :: struct { 90 pointed_to_type: Definition, 91 } 92 93 Type_Multipointer :: struct { 94 pointed_to_type: Definition, 95 } 96 97 Type_Pointer_By_Ptr :: struct { 98 pointed_to_type: Definition, 99 } 100 101 Type_Alias :: struct { 102 aliased_type: Definition, 103 } 104 105 Type_Struct_Field :: struct { 106 names: [dynamic]string, 107 anonymous: bool, 108 type: Definition, 109 type_overrride: string, 110 comment_before: string, 111 comment_on_right: string, 112 113 tag: string, 114 is_using: bool, 115 116 // internal 117 line: int, 118 } 119 120 Type_Struct :: struct { 121 fields: []Type_Struct_Field, 122 raw_union: bool, 123 } 124 125 Type_Enum_Member :: struct { 126 name: string, 127 value: int, 128 comment_before: string, 129 comment_on_right: string, 130 } 131 132 Type_Enum :: struct { 133 storage_type: typeid, 134 members: []Type_Enum_Member, 135 } 136 137 Type_Unknown :: struct {} 138 139 Type_Raw_Pointer :: struct {} 140 141 Type_Bit_Set :: struct { 142 enum_decl_name: Definition, 143 enum_type: Type_Index, 144 } 145 146 Type_Bit_Set_Constant :: struct { 147 bit_set_type: Type_Index, 148 bit_set_type_name: Type_Name, 149 value: int, 150 } 151 152 Type_Fixed_Array :: struct { 153 element_type: Definition, 154 size: int, 155 } 156 157 Type_Procedure_Parameter :: struct { 158 name: string, 159 type: Definition, 160 default: string, 161 any_int: bool, 162 } 163 164 Type_Procedure :: struct { 165 parameters: []Type_Procedure_Parameter, 166 result_type: Definition, 167 calling_convention: Calling_Convention, 168 is_variadic: bool, 169 } 170 171 Calling_Convention :: enum { 172 C, 173 Std_Call, 174 Fast_Call, 175 } 176 177 Type_CString :: struct {} 178 179 // Hard-coded override containing Odin type text 180 Type_Override :: struct { 181 definition_text: string, 182 } 183 184 check_type_definition :: proc(types: Type_List, def: Definition, $T: typeid) -> (bool) { 185 if idx, is_idx := def.(Type_Index); is_idx { 186 _, is_type := types[idx].(T) 187 188 return is_type 189 } 190 191 return false 192 } 193 194 resolve_type_definition :: proc(types: Type_List, def: Definition, $T: typeid) -> (T, bool) { 195 if idx, is_idx := def.(Type_Index); is_idx { 196 return types[idx].(T) 197 } 198 199 return {}, false 200 } 201 202 resolve_type_definition_ptr :: proc(types: Type_List, def: Definition, $T: typeid) -> ^T { 203 if idx, is_idx := def.(Type_Index); is_idx { 204 if t, is_t := &types[idx].(T); is_t { 205 return t 206 } 207 } 208 209 return nil 210 } 211