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