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