-
Notifications
You must be signed in to change notification settings - Fork 8
/
types.oc
297 lines (258 loc) · 7.41 KB
/
types.oc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//* Contains the structs for the type system.
import std::buffer::{ Buffer }
import std::vector::{ Vector }
import std::map::{ Map }
import std::mem
import std::span::{ Span }
import @ast::nodes::{ AST, ASTType, Structure, Variable, Function, Enum }
import @ast::scopes::{ Symbol, TemplateInstance }
import @ast::program::Namespace
import @tokens::{ Token, TokenType }
enum BaseType {
Char
Bool
Void
I8
I16
I32
I64
U8
U16
U32
U64
F32
F64
//* For convenience, to iterate over all the base types
NUM_BASE_TYPES
FunctionPtr // C-style function pointer
Closure
Pointer
Structure
Unresolved
Array
Alias
UnresolvedTemplate
Enum
Error
}
def BaseType::str(this): str => match this {
Char => "char"
Bool => "bool"
Void => "void"
I8 => "i8"
I16 => "i16"
I32 => "i32"
I64 => "i64"
U8 => "u8"
U16 => "u16"
U32 => "u32"
U64 => "u64"
F32 => "f32"
F64 => "f64"
else => .dbg()
}
def BaseType::is_callable(this): bool => this == FunctionPtr or this == Closure
// This is used for both Function and Closure types.
struct FunctionType {
//* `null` if this is a function pointer
orig: &Function
params: &Vector<&Variable>
return_type: &Type
is_variadic: bool
}
struct ArrayType {
elem_type: &Type
size_expr: &AST
size_known: bool
size: u32
}
//* Stores an unresolved template specialization.
//*
//* We have this here for the cases where we can't resolve a template
//* (e.g. when we're parsing a template definition). This is not actually
//* used in the compiler for generating any code, but is helpful for outputting
//* documentation.
struct UnresolvedTemplate {
base: &Type, // FIXME: Should this be a symbol?
args: &Vector<&Type>
}
struct TypeUnion {
ptr: &Type
struc: &Structure
enom: &Enum
unresolved: &AST
func: FunctionType
arr: ArrayType
unresolved_spec: UnresolvedTemplate
}
struct Type {
name: str
base: BaseType
span: Span
u: TypeUnion
methods: &Map<str, &Function>
sym: &Symbol
//* This is used for specializations, so that they can point to the original
//* template instance and the template arguments.
template_instance: &TemplateInstance
}
def Type::shallow_copy(old: &Type): &Type {
let new = mem::alloc<Type>()
*new = *old
return new
}
def Type::new_resolved(base: BaseType, span: Span): &Type {
let type = mem::alloc<Type>()
type.base = base
type.span = span
type.name = base.str()
type.methods = Map<str, &Function>::new()
return type
}
def Type::new_unresolved(name: str, span: Span): &Type {
let type = Type::new_resolved(BaseType::Unresolved, span)
type.name = name
return type
}
def Type::new_unresolved_base(base: BaseType, span: Span): &Type {
let ident = AST::new(ASTType::Identifier, span)
ident.u.ident.name = base.str()
let type = Type::new_resolved(BaseType::Unresolved, span)
type.u.unresolved = ident
return type
}
def Type::is_integer(&this): bool => match .base {
I8 | I16 | I32 | I64 |
U8 | U16 | U32 | U64 => true
else => false
}
def Type::is_float(&this): bool => .base == BaseType::F32 or .base == BaseType::F64
def Type::is_numeric(&this): bool => match .base {
I8 | I16 | I32 | I64 |
U8 | U16 | U32 | U64 |
F32 | F64 => true
else => false
}
def Type::is_numeric_or_char(&this): bool => .is_numeric() or .base == BaseType::Char
def Type::can_have_methods(&this): bool => match .base {
Char | Bool | Void | I8 | I16 | I32 | I64 |
U8 | U16 | U32 | U64 | F32 | F64 | Structure |
Alias | Enum => true
else => false
}
def Type::is_resolved(&this): bool => match .base {
Unresolved => false
Alias => .u.ptr.is_resolved()
Pointer => .u.ptr.is_resolved()
FunctionPtr => {
let resolved = .u.func.return_type.is_resolved()
for param in .u.func.params.iter() {
resolved = resolved and param.type.is_resolved()
}
yield resolved
}
else => true
}
def Type::eq(&this, other: &Type, strict: bool = false): bool {
if (this == null and other == null) return true
if (this == null or other == null) return false
// Aliases are equal to their original type
if .base == Alias return .u.ptr.eq(other, strict)
if other.base == Alias return .eq(other.u.ptr, strict)
if .base != other.base return false
match .base {
Error | Unresolved | UnresolvedTemplate => return false
// Closures should already be canonicalized
Closure => return .sym.full_name.eq(other.sym.full_name)
FunctionPtr => {
let af = .u.func
let bf = other.u.func
if not af.return_type.eq(bf.return_type, strict: true) return false
if af.params.size != bf.params.size return false
for let i = 0; i < af.params.size; i += 1 {
let a = af.params.at(i)
let b = bf.params.at(i)
if not a.type.eq(b.type, strict: true) return false
}
// FIXME: What about variadic functions?
return true
}
Pointer => {
if not strict {
if .u.ptr.base == BaseType::Void or other.u.ptr.base == BaseType::Void {
return true
}
}
return .u.ptr.eq(other.u.ptr, strict: true)
}
Structure => return .u.struc == other.u.struc
Enum => return .u.enom == other.u.enom
Array => {
if not .u.arr.elem_type.eq(other.u.arr.elem_type, strict: true) return false
if .u.arr.size_known and other.u.arr.size_known {
return .u.arr.size == other.u.arr.size
}
return true
}
else => {
if .base as u32 < BaseType::NUM_BASE_TYPES as u32 {
return true
}
assert false, `Unhandled case in Type::eq(), base = {.base}`
}
}
}
def Type::is_str(&this): bool {
return .base == Alias and .sym.full_name.eq("str")
}
//* If this type is an alias, return the underlying type.
def Type::unaliased(&this): &Type {
if .base != Alias return this
return .u.ptr.unaliased()
}
def Type::decay_array(&this): &Type {
if .base != BaseType::Array return this
let res = Type::new_resolved(BaseType::Pointer, .span)
res.u.ptr = .u.arr.elem_type
return res
}
def Type::str(&this): str => match .base {
Pointer => `&{.u.ptr.str()}`
Closure | FunctionPtr => {
let buf = Buffer::make()
if .base == Closure {
buf += "@"
}
buf += "fn("
for let i = 0; i < .u.func.params.size; i += 1 {
let param = .u.func.params.at(i)
if param.type? {
buf += param.type.str()
} else {
buf += "null"
}
if i < .u.func.params.size - 1 {
buf += ", "
}
}
buf += "): "
buf += .u.func.return_type.str()
return buf.str()
}
Array => {
let buf = Buffer::make()
buf += .u.arr.elem_type.str()
buf += "["
if .u.arr.size_known {
buf += `{.u.arr.size}`
} else {
buf += `?{this:p}`
}
buf += "]"
return buf.str()
}
Structure => .u.struc.sym.display
Enum => .u.enom.sym.display
Alias => .name
else => .base.str()
}