1
2
3
4
5
6
7 package types2
8
9 import (
10 "go/constant"
11 "strings"
12 )
13
14
15
16 var Universe *Scope
17
18
19
20 var Unsafe *Package
21
22 var (
23 universeIota Object
24 universeBool Type
25 universeByte Type
26 universeRune Type
27 universeError Type
28 universeAny Object
29 universeComparable Object
30 )
31
32
33
34
35
36
37
38 var Typ = [...]*Basic{
39 Invalid: {Invalid, 0, "invalid type"},
40
41 Bool: {Bool, IsBoolean, "bool"},
42 Int: {Int, IsInteger, "int"},
43 Int8: {Int8, IsInteger, "int8"},
44 Int16: {Int16, IsInteger, "int16"},
45 Int32: {Int32, IsInteger, "int32"},
46 Int64: {Int64, IsInteger, "int64"},
47 Uint: {Uint, IsInteger | IsUnsigned, "uint"},
48 Uint8: {Uint8, IsInteger | IsUnsigned, "uint8"},
49 Uint16: {Uint16, IsInteger | IsUnsigned, "uint16"},
50 Uint32: {Uint32, IsInteger | IsUnsigned, "uint32"},
51 Uint64: {Uint64, IsInteger | IsUnsigned, "uint64"},
52 Uintptr: {Uintptr, IsInteger | IsUnsigned, "uintptr"},
53 Float32: {Float32, IsFloat, "float32"},
54 Float64: {Float64, IsFloat, "float64"},
55 Complex64: {Complex64, IsComplex, "complex64"},
56 Complex128: {Complex128, IsComplex, "complex128"},
57 String: {String, IsString, "string"},
58 UnsafePointer: {UnsafePointer, 0, "Pointer"},
59
60 UntypedBool: {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
61 UntypedInt: {UntypedInt, IsInteger | IsUntyped, "untyped int"},
62 UntypedRune: {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
63 UntypedFloat: {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
64 UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
65 UntypedString: {UntypedString, IsString | IsUntyped, "untyped string"},
66 UntypedNil: {UntypedNil, IsUntyped, "untyped nil"},
67 }
68
69 var basicAliases = [...]*Basic{
70 {Byte, IsInteger | IsUnsigned, "byte"},
71 {Rune, IsInteger, "rune"},
72 }
73
74 func defPredeclaredTypes() {
75 for _, t := range Typ {
76 def(NewTypeName(nopos, nil, t.name, t))
77 }
78
79 for _, t := range basicAliases {
80 def(NewTypeName(nopos, nil, t.name, t))
81 }
82
83
84 {
85 obj := NewTypeName(nopos, nil, "error", nil)
86 typ := NewNamed(obj, nil, nil)
87
88
89 recv := newVar(RecvVar, nopos, nil, "", typ)
90 res := newVar(ResultVar, nopos, nil, "", Typ[String])
91 sig := NewSignatureType(recv, nil, nil, nil, NewTuple(res), false)
92 err := NewFunc(nopos, nil, "Error", sig)
93
94
95 ityp := &Interface{methods: []*Func{err}, complete: true}
96 computeInterfaceTypeSet(nil, nopos, ityp)
97
98 typ.fromRHS = ityp
99 typ.Underlying()
100 def(obj)
101 }
102
103
104 {
105 obj := NewTypeName(nopos, nil, "any", nil)
106 NewAlias(obj, &emptyInterface)
107 def(obj)
108 }
109
110
111 {
112 obj := NewTypeName(nopos, nil, "comparable", nil)
113 NewNamed(obj, &Interface{complete: true, tset: &_TypeSet{nil, allTermlist, true}}, nil)
114 def(obj)
115 }
116 }
117
118 var predeclaredConsts = [...]struct {
119 name string
120 kind BasicKind
121 val constant.Value
122 }{
123 {"true", UntypedBool, constant.MakeBool(true)},
124 {"false", UntypedBool, constant.MakeBool(false)},
125 {"iota", UntypedInt, constant.MakeInt64(0)},
126 }
127
128 func defPredeclaredConsts() {
129 for _, c := range predeclaredConsts {
130 def(NewConst(nopos, nil, c.name, Typ[c.kind], c.val))
131 }
132 }
133
134 func defPredeclaredNil() {
135 def(&Nil{object{name: "nil", typ: Typ[UntypedNil]}})
136 }
137
138
139 type builtinId int
140
141 const (
142
143 _Append builtinId = iota
144 _Cap
145 _Clear
146 _Close
147 _Complex
148 _Copy
149 _Delete
150 _Imag
151 _Len
152 _Make
153 _Max
154 _Min
155 _New
156 _Panic
157 _Print
158 _Println
159 _Real
160 _Recover
161
162
163 _Add
164 _Alignof
165 _Offsetof
166 _Sizeof
167 _Slice
168 _SliceData
169 _String
170 _StringData
171
172
173 _Assert
174 _Trace
175 )
176
177 var predeclaredFuncs = [...]struct {
178 name string
179 nargs int
180 variadic bool
181 kind exprKind
182 }{
183 _Append: {"append", 1, true, expression},
184 _Cap: {"cap", 1, false, expression},
185 _Clear: {"clear", 1, false, statement},
186 _Close: {"close", 1, false, statement},
187 _Complex: {"complex", 2, false, expression},
188 _Copy: {"copy", 2, false, statement},
189 _Delete: {"delete", 2, false, statement},
190 _Imag: {"imag", 1, false, expression},
191 _Len: {"len", 1, false, expression},
192 _Make: {"make", 1, true, expression},
193
194 _Max: {"max", 1, true, expression},
195 _Min: {"min", 1, true, expression},
196 _New: {"new", 1, false, expression},
197 _Panic: {"panic", 1, false, statement},
198 _Print: {"print", 0, true, statement},
199 _Println: {"println", 0, true, statement},
200 _Real: {"real", 1, false, expression},
201 _Recover: {"recover", 0, false, statement},
202
203 _Add: {"Add", 2, false, expression},
204 _Alignof: {"Alignof", 1, false, expression},
205 _Offsetof: {"Offsetof", 1, false, expression},
206 _Sizeof: {"Sizeof", 1, false, expression},
207 _Slice: {"Slice", 2, false, expression},
208 _SliceData: {"SliceData", 1, false, expression},
209 _String: {"String", 2, false, expression},
210 _StringData: {"StringData", 1, false, expression},
211
212 _Assert: {"assert", 1, false, statement},
213 _Trace: {"trace", 0, true, statement},
214 }
215
216 func defPredeclaredFuncs() {
217 for i := range predeclaredFuncs {
218 id := builtinId(i)
219 if id == _Assert || id == _Trace {
220 continue
221 }
222 def(newBuiltin(id))
223 }
224 }
225
226
227
228
229 func DefPredeclaredTestFuncs() {
230 if Universe.Lookup("assert") != nil {
231 return
232 }
233 def(newBuiltin(_Assert))
234 def(newBuiltin(_Trace))
235 }
236
237 func init() {
238 Universe = NewScope(nil, nopos, nopos, "universe")
239 Unsafe = NewPackage("unsafe", "unsafe")
240 Unsafe.complete = true
241
242 defPredeclaredTypes()
243 defPredeclaredConsts()
244 defPredeclaredNil()
245 defPredeclaredFuncs()
246
247 universeIota = Universe.Lookup("iota")
248 universeBool = Universe.Lookup("bool").Type()
249 universeByte = Universe.Lookup("byte").Type()
250 universeRune = Universe.Lookup("rune").Type()
251 universeError = Universe.Lookup("error").Type()
252 universeAny = Universe.Lookup("any")
253 universeComparable = Universe.Lookup("comparable")
254 }
255
256
257
258
259 func def(obj Object) {
260 assert(obj.Type() != nil)
261 name := obj.Name()
262 if strings.Contains(name, " ") {
263 return
264 }
265
266 if typ := asNamed(obj.Type()); typ != nil {
267 typ.obj = obj.(*TypeName)
268 }
269
270 scope := Universe
271 if obj.Exported() {
272 scope = Unsafe.scope
273
274 switch obj := obj.(type) {
275 case *TypeName:
276 obj.pkg = Unsafe
277 case *Builtin:
278 obj.pkg = Unsafe
279 default:
280 panic("unreachable")
281 }
282 }
283 if scope.Insert(obj) != nil {
284 panic("double declaration of predeclared identifier")
285 }
286 }
287
View as plain text