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 := (*Checker)(nil).newNamed(obj, nil, nil)
123
124
125 recv := newVar(RecvVar, nopos, nil, "", typ)
126 res := newVar(ResultVar, 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.fromRHS = ityp
135 typ.Underlying()
136 def(obj)
137 }
138
139
140 {
141 obj := NewTypeName(nopos, nil, "comparable", nil)
142 obj.setColor(black)
143 typ := (*Checker)(nil).newNamed(obj, nil, nil)
144
145
146 ityp := &Interface{complete: true, tset: &_TypeSet{nil, allTermlist, true}}
147
148 typ.fromRHS = ityp
149 typ.Underlying()
150 def(obj)
151 }
152 }
153
154 var predeclaredConsts = [...]struct {
155 name string
156 kind BasicKind
157 val constant.Value
158 }{
159 {"true", UntypedBool, constant.MakeBool(true)},
160 {"false", UntypedBool, constant.MakeBool(false)},
161 {"iota", UntypedInt, constant.MakeInt64(0)},
162 }
163
164 func defPredeclaredConsts() {
165 for _, c := range predeclaredConsts {
166 def(NewConst(nopos, nil, c.name, Typ[c.kind], c.val))
167 }
168 }
169
170 func defPredeclaredNil() {
171 def(&Nil{object{name: "nil", typ: Typ[UntypedNil], color_: black}})
172 }
173
174
175 type builtinId int
176
177 const (
178
179 _Append builtinId = iota
180 _Cap
181 _Clear
182 _Close
183 _Complex
184 _Copy
185 _Delete
186 _Imag
187 _Len
188 _Make
189 _Max
190 _Min
191 _New
192 _Panic
193 _Print
194 _Println
195 _Real
196 _Recover
197
198
199 _Add
200 _Alignof
201 _Offsetof
202 _Sizeof
203 _Slice
204 _SliceData
205 _String
206 _StringData
207
208
209 _Assert
210 _Trace
211 )
212
213 var predeclaredFuncs = [...]struct {
214 name string
215 nargs int
216 variadic bool
217 kind exprKind
218 }{
219 _Append: {"append", 1, true, expression},
220 _Cap: {"cap", 1, false, expression},
221 _Clear: {"clear", 1, false, statement},
222 _Close: {"close", 1, false, statement},
223 _Complex: {"complex", 2, false, expression},
224 _Copy: {"copy", 2, false, statement},
225 _Delete: {"delete", 2, false, statement},
226 _Imag: {"imag", 1, false, expression},
227 _Len: {"len", 1, false, expression},
228 _Make: {"make", 1, true, expression},
229
230 _Max: {"max", 1, true, expression},
231 _Min: {"min", 1, true, expression},
232 _New: {"new", 1, false, expression},
233 _Panic: {"panic", 1, false, statement},
234 _Print: {"print", 0, true, statement},
235 _Println: {"println", 0, true, statement},
236 _Real: {"real", 1, false, expression},
237 _Recover: {"recover", 0, false, statement},
238
239 _Add: {"Add", 2, false, expression},
240 _Alignof: {"Alignof", 1, false, expression},
241 _Offsetof: {"Offsetof", 1, false, expression},
242 _Sizeof: {"Sizeof", 1, false, expression},
243 _Slice: {"Slice", 2, false, expression},
244 _SliceData: {"SliceData", 1, false, expression},
245 _String: {"String", 2, false, expression},
246 _StringData: {"StringData", 1, false, expression},
247
248 _Assert: {"assert", 1, false, statement},
249 _Trace: {"trace", 0, true, statement},
250 }
251
252 func defPredeclaredFuncs() {
253 for i := range predeclaredFuncs {
254 id := builtinId(i)
255 if id == _Assert || id == _Trace {
256 continue
257 }
258 def(newBuiltin(id))
259 }
260 }
261
262
263
264
265 func DefPredeclaredTestFuncs() {
266 if Universe.Lookup("assert") != nil {
267 return
268 }
269 def(newBuiltin(_Assert))
270 def(newBuiltin(_Trace))
271 }
272
273 func init() {
274 Universe = NewScope(nil, nopos, nopos, "universe")
275 Unsafe = NewPackage("unsafe", "unsafe")
276 Unsafe.complete = true
277
278 defPredeclaredTypes()
279 defPredeclaredConsts()
280 defPredeclaredNil()
281 defPredeclaredFuncs()
282
283 universeIota = Universe.Lookup("iota")
284 universeBool = Universe.Lookup("bool").Type()
285 universeByte = Universe.Lookup("byte").Type()
286 universeRune = Universe.Lookup("rune").Type()
287 universeError = Universe.Lookup("error").Type()
288 universeComparable = Universe.Lookup("comparable")
289 }
290
291
292
293
294 func def(obj Object) {
295 assert(obj.color() == black)
296 name := obj.Name()
297 if strings.Contains(name, " ") {
298 return
299 }
300
301 if typ := asNamed(obj.Type()); typ != nil {
302 typ.obj = obj.(*TypeName)
303 }
304
305 scope := Universe
306 if obj.Exported() {
307 scope = Unsafe.scope
308
309 switch obj := obj.(type) {
310 case *TypeName:
311 obj.pkg = Unsafe
312 case *Builtin:
313 obj.pkg = Unsafe
314 default:
315 panic("unreachable")
316 }
317 }
318 if scope.Insert(obj) != nil {
319 panic("double declaration of predeclared identifier")
320 }
321 }
322
View as plain text