-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeAnn.swift
197 lines (161 loc) · 3.96 KB
/
TypeAnn.swift
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
//
// TypeAnn.swift
//
// SwiSwi - a tiny Swift-like language
//
// Created for the Budapest Swift Meetup
// by Árpád Goretity (H2CO3)
// on 15/12/2015
//
// There's no warranty whatsoever.
//
class TypeAnn {
init() {}
func toString() -> String {
return "Type"
}
func equals(rhs: TypeAnn) -> Bool {
return false
}
func isNumeric() -> Bool {
return false
}
// For code generation.
// Returns the LLVM representation of the type.
func llvmType() -> LLVMTypeRef {
assert(false, "cannot represent generic TypeAnn in LLVM")
return nil
}
}
final class VoidType : TypeAnn {
override func toString() -> String {
return "Void"
}
override func equals(rhs: TypeAnn) -> Bool {
return rhs is VoidType // assumes final
}
override func llvmType() -> LLVMTypeRef {
return LLVMVoidType()
}
}
final class BoolType : TypeAnn {
override func toString() -> String {
return "Bool"
}
override func equals(rhs: TypeAnn) -> Bool {
return rhs is BoolType // assumes final
}
override func llvmType() -> LLVMTypeRef {
return LLVMInt1Type()
}
}
final class IntType : TypeAnn {
override func toString() -> String {
return "Int";
}
override func equals(rhs: TypeAnn) -> Bool {
return rhs is IntType // assumes final
}
override func isNumeric() -> Bool {
return true
}
override func llvmType() -> LLVMTypeRef {
return LLVMInt64Type()
}
}
final class DoubleType : TypeAnn {
override func toString() -> String {
return "Double";
}
override func equals(rhs: TypeAnn) -> Bool {
return rhs is DoubleType // assumes final
}
override func isNumeric() -> Bool {
return true
}
override func llvmType() -> LLVMTypeRef {
return LLVMDoubleType()
}
}
final class StringType : TypeAnn {
static var storedLlvmType: LLVMTypeRef? = nil
override func toString() -> String {
return "String";
}
override func equals(rhs: TypeAnn) -> Bool {
return rhs is StringType // assumes final
}
// Strings are represented by:
// struct string {
// char *begin;
// uint64_t length;
// }
override func llvmType() -> LLVMTypeRef {
if let storedLlvmType = StringType.storedLlvmType {
return storedLlvmType
}
var elems = [
LLVMPointerType(LLVMInt8Type(), 0), // char * in addr. space #0
LLVMInt64Type()
]
// 0: not packed
StringType.storedLlvmType = LLVMStructType(&elems, UInt32(elems.count), 0)
print("Generating String type \(StringType.storedLlvmType!)")
return StringType.storedLlvmType!
}
}
class FunctionType : TypeAnn {
let argType: TypeAnn
let retType: TypeAnn
init(_ argType: TypeAnn, _ retType: TypeAnn) {
self.argType = argType
self.retType = retType
super.init()
}
override func toString() -> String {
// TODO: may need parentheses in the future (precedence!)
return "\(self.argType.toString()) -> \(self.retType.toString())";
}
override func equals(rhs: TypeAnn) -> Bool {
guard let rt = rhs as? FunctionType else {
return false
}
return argType.equals(rt.argType) && retType.equals(rt.retType)
}
override func llvmType() -> LLVMTypeRef {
// a function with a 'Void' argument is a
// special case: it means '0 arguments'.
var args: [LLVMTypeRef] = []
if self.argType != VoidType() {
args.append(self.argType.llvmType())
}
return LLVMFunctionType(
self.retType.llvmType(),
&args,
UInt32(args.count),
0 // 0: not variadic
)
}
}
// Convenience equality operators
func ==(lhs: TypeAnn, rhs: TypeAnn) -> Bool {
return lhs.equals(rhs)
}
func !=(lhs: TypeAnn, rhs: TypeAnn) -> Bool {
return !(lhs == rhs)
}
func TypeFromTypeName(oname: String?) -> TypeAnn {
guard let name = oname else {
return VoidType()
}
switch name {
case "Void": return VoidType()
case "Bool": return BoolType()
case "Int": return IntType()
case "Double": return DoubleType()
case "String": return StringType()
default:
assert(false, "unknown type name: \(name)")
return VoidType()
}
}