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