|
| 1 | +const builtin = @import("builtin"); |
| 2 | +const Scope = @import("scope.zig").Scope; |
| 3 | +const Module = @import("module.zig").Module; |
| 4 | +const Value = @import("value.zig").Value; |
| 5 | + |
| 6 | +pub const Type = struct { |
| 7 | + base: Value, |
| 8 | + id: Id, |
| 9 | + |
| 10 | + pub const Id = builtin.TypeId; |
| 11 | + |
| 12 | + pub fn destroy(base: *Type, module: *Module) void { |
| 13 | + switch (base.id) { |
| 14 | + Id.Struct => @fieldParentPtr(Struct, "base", base).destroy(module), |
| 15 | + Id.Fn => @fieldParentPtr(Fn, "base", base).destroy(module), |
| 16 | + Id.Type => @fieldParentPtr(MetaType, "base", base).destroy(module), |
| 17 | + Id.Void => @fieldParentPtr(Void, "base", base).destroy(module), |
| 18 | + Id.Bool => @fieldParentPtr(Bool, "base", base).destroy(module), |
| 19 | + Id.NoReturn => @fieldParentPtr(NoReturn, "base", base).destroy(module), |
| 20 | + Id.Int => @fieldParentPtr(Int, "base", base).destroy(module), |
| 21 | + Id.Float => @fieldParentPtr(Float, "base", base).destroy(module), |
| 22 | + Id.Pointer => @fieldParentPtr(Pointer, "base", base).destroy(module), |
| 23 | + Id.Array => @fieldParentPtr(Array, "base", base).destroy(module), |
| 24 | + Id.ComptimeFloat => @fieldParentPtr(ComptimeFloat, "base", base).destroy(module), |
| 25 | + Id.ComptimeInt => @fieldParentPtr(ComptimeInt, "base", base).destroy(module), |
| 26 | + Id.Undefined => @fieldParentPtr(Undefined, "base", base).destroy(module), |
| 27 | + Id.Null => @fieldParentPtr(Null, "base", base).destroy(module), |
| 28 | + Id.Optional => @fieldParentPtr(Optional, "base", base).destroy(module), |
| 29 | + Id.ErrorUnion => @fieldParentPtr(ErrorUnion, "base", base).destroy(module), |
| 30 | + Id.ErrorSet => @fieldParentPtr(ErrorSet, "base", base).destroy(module), |
| 31 | + Id.Enum => @fieldParentPtr(Enum, "base", base).destroy(module), |
| 32 | + Id.Union => @fieldParentPtr(Union, "base", base).destroy(module), |
| 33 | + Id.Namespace => @fieldParentPtr(Namespace, "base", base).destroy(module), |
| 34 | + Id.Block => @fieldParentPtr(Block, "base", base).destroy(module), |
| 35 | + Id.BoundFn => @fieldParentPtr(BoundFn, "base", base).destroy(module), |
| 36 | + Id.ArgTuple => @fieldParentPtr(ArgTuple, "base", base).destroy(module), |
| 37 | + Id.Opaque => @fieldParentPtr(Opaque, "base", base).destroy(module), |
| 38 | + Id.Promise => @fieldParentPtr(Promise, "base", base).destroy(module), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub const Struct = struct { |
| 43 | + base: Type, |
| 44 | + decls: *Scope.Decls, |
| 45 | + |
| 46 | + pub fn destroy(self: *Struct, module: *Module) void { |
| 47 | + module.a().destroy(self); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + pub const Fn = struct { |
| 52 | + base: Type, |
| 53 | + |
| 54 | + pub fn create(module: *Module) !*Fn { |
| 55 | + return module.a().create(Fn{ |
| 56 | + .base = Type{ |
| 57 | + .base = Value{ |
| 58 | + .id = Value.Id.Type, |
| 59 | + .typeof = &MetaType.get(module).base, |
| 60 | + .ref_count = 1, |
| 61 | + }, |
| 62 | + .id = builtin.TypeId.Fn, |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + pub fn destroy(self: *Fn, module: *Module) void { |
| 68 | + module.a().destroy(self); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + pub const MetaType = struct { |
| 73 | + base: Type, |
| 74 | + value: *Type, |
| 75 | + |
| 76 | + /// Adds 1 reference to the resulting type |
| 77 | + pub fn get(module: *Module) *MetaType { |
| 78 | + module.meta_type.base.base.ref(); |
| 79 | + return module.meta_type; |
| 80 | + } |
| 81 | + |
| 82 | + pub fn destroy(self: *MetaType, module: *Module) void { |
| 83 | + module.a().destroy(self); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + pub const Void = struct { |
| 88 | + base: Type, |
| 89 | + |
| 90 | + /// Adds 1 reference to the resulting type |
| 91 | + pub fn get(module: *Module) *Void { |
| 92 | + module.void_type.base.base.ref(); |
| 93 | + return module.void_type; |
| 94 | + } |
| 95 | + |
| 96 | + pub fn destroy(self: *Void, module: *Module) void { |
| 97 | + module.a().destroy(self); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + pub const Bool = struct { |
| 102 | + base: Type, |
| 103 | + |
| 104 | + /// Adds 1 reference to the resulting type |
| 105 | + pub fn get(module: *Module) *Bool { |
| 106 | + module.bool_type.base.base.ref(); |
| 107 | + return module.bool_type; |
| 108 | + } |
| 109 | + |
| 110 | + pub fn destroy(self: *Bool, module: *Module) void { |
| 111 | + module.a().destroy(self); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + pub const NoReturn = struct { |
| 116 | + base: Type, |
| 117 | + |
| 118 | + /// Adds 1 reference to the resulting type |
| 119 | + pub fn get(module: *Module) *NoReturn { |
| 120 | + module.noreturn_type.base.base.ref(); |
| 121 | + return module.noreturn_type; |
| 122 | + } |
| 123 | + |
| 124 | + pub fn destroy(self: *NoReturn, module: *Module) void { |
| 125 | + module.a().destroy(self); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + pub const Int = struct { |
| 130 | + base: Type, |
| 131 | + |
| 132 | + pub fn destroy(self: *Int, module: *Module) void { |
| 133 | + module.a().destroy(self); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + pub const Float = struct { |
| 138 | + base: Type, |
| 139 | + |
| 140 | + pub fn destroy(self: *Float, module: *Module) void { |
| 141 | + module.a().destroy(self); |
| 142 | + } |
| 143 | + }; |
| 144 | + pub const Pointer = struct { |
| 145 | + base: Type, |
| 146 | + |
| 147 | + pub fn destroy(self: *Pointer, module: *Module) void { |
| 148 | + module.a().destroy(self); |
| 149 | + } |
| 150 | + }; |
| 151 | + pub const Array = struct { |
| 152 | + base: Type, |
| 153 | + |
| 154 | + pub fn destroy(self: *Array, module: *Module) void { |
| 155 | + module.a().destroy(self); |
| 156 | + } |
| 157 | + }; |
| 158 | + pub const ComptimeFloat = struct { |
| 159 | + base: Type, |
| 160 | + |
| 161 | + pub fn destroy(self: *ComptimeFloat, module: *Module) void { |
| 162 | + module.a().destroy(self); |
| 163 | + } |
| 164 | + }; |
| 165 | + pub const ComptimeInt = struct { |
| 166 | + base: Type, |
| 167 | + |
| 168 | + pub fn destroy(self: *ComptimeInt, module: *Module) void { |
| 169 | + module.a().destroy(self); |
| 170 | + } |
| 171 | + }; |
| 172 | + pub const Undefined = struct { |
| 173 | + base: Type, |
| 174 | + |
| 175 | + pub fn destroy(self: *Undefined, module: *Module) void { |
| 176 | + module.a().destroy(self); |
| 177 | + } |
| 178 | + }; |
| 179 | + pub const Null = struct { |
| 180 | + base: Type, |
| 181 | + |
| 182 | + pub fn destroy(self: *Null, module: *Module) void { |
| 183 | + module.a().destroy(self); |
| 184 | + } |
| 185 | + }; |
| 186 | + pub const Optional = struct { |
| 187 | + base: Type, |
| 188 | + |
| 189 | + pub fn destroy(self: *Optional, module: *Module) void { |
| 190 | + module.a().destroy(self); |
| 191 | + } |
| 192 | + }; |
| 193 | + pub const ErrorUnion = struct { |
| 194 | + base: Type, |
| 195 | + |
| 196 | + pub fn destroy(self: *ErrorUnion, module: *Module) void { |
| 197 | + module.a().destroy(self); |
| 198 | + } |
| 199 | + }; |
| 200 | + pub const ErrorSet = struct { |
| 201 | + base: Type, |
| 202 | + |
| 203 | + pub fn destroy(self: *ErrorSet, module: *Module) void { |
| 204 | + module.a().destroy(self); |
| 205 | + } |
| 206 | + }; |
| 207 | + pub const Enum = struct { |
| 208 | + base: Type, |
| 209 | + |
| 210 | + pub fn destroy(self: *Enum, module: *Module) void { |
| 211 | + module.a().destroy(self); |
| 212 | + } |
| 213 | + }; |
| 214 | + pub const Union = struct { |
| 215 | + base: Type, |
| 216 | + |
| 217 | + pub fn destroy(self: *Union, module: *Module) void { |
| 218 | + module.a().destroy(self); |
| 219 | + } |
| 220 | + }; |
| 221 | + pub const Namespace = struct { |
| 222 | + base: Type, |
| 223 | + |
| 224 | + pub fn destroy(self: *Namespace, module: *Module) void { |
| 225 | + module.a().destroy(self); |
| 226 | + } |
| 227 | + }; |
| 228 | + |
| 229 | + pub const Block = struct { |
| 230 | + base: Type, |
| 231 | + |
| 232 | + pub fn destroy(self: *Block, module: *Module) void { |
| 233 | + module.a().destroy(self); |
| 234 | + } |
| 235 | + }; |
| 236 | + |
| 237 | + pub const BoundFn = struct { |
| 238 | + base: Type, |
| 239 | + |
| 240 | + pub fn destroy(self: *BoundFn, module: *Module) void { |
| 241 | + module.a().destroy(self); |
| 242 | + } |
| 243 | + }; |
| 244 | + |
| 245 | + pub const ArgTuple = struct { |
| 246 | + base: Type, |
| 247 | + |
| 248 | + pub fn destroy(self: *ArgTuple, module: *Module) void { |
| 249 | + module.a().destroy(self); |
| 250 | + } |
| 251 | + }; |
| 252 | + |
| 253 | + pub const Opaque = struct { |
| 254 | + base: Type, |
| 255 | + |
| 256 | + pub fn destroy(self: *Opaque, module: *Module) void { |
| 257 | + module.a().destroy(self); |
| 258 | + } |
| 259 | + }; |
| 260 | + |
| 261 | + pub const Promise = struct { |
| 262 | + base: Type, |
| 263 | + |
| 264 | + pub fn destroy(self: *Promise, module: *Module) void { |
| 265 | + module.a().destroy(self); |
| 266 | + } |
| 267 | + }; |
| 268 | +}; |
0 commit comments