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 universeError Type
31 universeAny Object
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
82 for _, t := range basicAliases {
83 def(NewTypeName(nopos, nil, t.name, t))
84 }
85
86
87 {
88 obj := NewTypeName(nopos, nil, "error", nil)
89 typ := NewNamed(obj, nil, nil)
90
91
92 recv := newVar(RecvVar, nopos, nil, "", typ)
93 res := newVar(ResultVar, nopos, nil, "", Typ[String])
94 sig := NewSignatureType(recv, nil, nil, nil, NewTuple(res), false)
95 err := NewFunc(nopos, nil, "Error", sig)
96
97
98 ityp := &Interface{methods: []*Func{err}, complete: true}
99 computeInterfaceTypeSet(nil, nopos, ityp)
100
101 typ.fromRHS = ityp
102 typ.Underlying()
103 def(obj)
104 }
105
106
107 {
108 obj := NewTypeName(nopos, nil, "any", nil)
109 NewAlias(obj, &emptyInterface)
110 def(obj)
111 }
112
113
114 {
115 obj := NewTypeName(nopos, nil, "comparable", nil)
116 NewNamed(obj, &Interface{complete: true, tset: &_TypeSet{nil, allTermlist, true}}, nil)
117 def(obj)
118 }
119 }
120
121 var predeclaredConsts = [...]struct {
122 name string
123 kind BasicKind
124 val constant.Value
125 }{
126 {"true", UntypedBool, constant.MakeBool(true)},
127 {"false", UntypedBool, constant.MakeBool(false)},
128 {"iota", UntypedInt, constant.MakeInt64(0)},
129 }
130
131 func defPredeclaredConsts() {
132 for _, c := range predeclaredConsts {
133 def(NewConst(nopos, nil, c.name, Typ[c.kind], c.val))
134 }
135 }
136
137 func defPredeclaredNil() {
138 def(&Nil{object{name: "nil", typ: Typ[UntypedNil]}})
139 }
140
141
142 type builtinId int
143
144 const (
145
146 _Append builtinId = iota
147 _Cap
148 _Clear
149 _Close
150 _Complex
151 _Copy
152 _Delete
153 _Imag
154 _Len
155 _Make
156 _Max
157 _Min
158 _New
159 _Panic
160 _Print
161 _Println
162 _Real
163 _Recover
164
165
166 _Add
167 _Alignof
168 _Offsetof
169 _Sizeof
170 _Slice
171 _SliceData
172 _String
173 _StringData
174
175
176 _Assert
177 _Trace
178 )
179
180 var predeclaredFuncs = [...]struct {
181 name string
182 nargs int
183 variadic bool
184 kind exprKind
185 }{
186 _Append: {"append", 1, true, expression},
187 _Cap: {"cap", 1, false, expression},
188 _Clear: {"clear", 1, false, statement},
189 _Close: {"close", 1, false, statement},
190 _Complex: {"complex", 2, false, expression},
191 _Copy: {"copy", 2, false, statement},
192 _Delete: {"delete", 2, false, statement},
193 _Imag: {"imag", 1, false, expression},
194 _Len: {"len", 1, false, expression},
195 _Make: {"make", 1, true, expression},
196
197 _Max: {"max", 1, true, expression},
198 _Min: {"min", 1, true, expression},
199 _New: {"new", 1, false, expression},
200 _Panic: {"panic", 1, false, statement},
201 _Print: {"print", 0, true, statement},
202 _Println: {"println", 0, true, statement},
203 _Real: {"real", 1, false, expression},
204 _Recover: {"recover", 0, false, statement},
205
206 _Add: {"Add", 2, false, expression},
207 _Alignof: {"Alignof", 1, false, expression},
208 _Offsetof: {"Offsetof", 1, false, expression},
209 _Sizeof: {"Sizeof", 1, false, expression},
210 _Slice: {"Slice", 2, false, expression},
211 _SliceData: {"SliceData", 1, false, expression},
212 _String: {"String", 2, false, expression},
213 _StringData: {"StringData", 1, false, expression},
214
215 _Assert: {"assert", 1, false, statement},
216 _Trace: {"trace", 0, true, statement},
217 }
218
219 func defPredeclaredFuncs() {
220 for i := range predeclaredFuncs {
221 id := builtinId(i)
222 if id == _Assert || id == _Trace {
223 continue
224 }
225 def(newBuiltin(id))
226 }
227 }
228
229
230
231
232 func DefPredeclaredTestFuncs() {
233 if Universe.Lookup("assert") != nil {
234 return
235 }
236 def(newBuiltin(_Assert))
237 def(newBuiltin(_Trace))
238 }
239
240 func init() {
241 Universe = NewScope(nil, nopos, nopos, "universe")
242 Unsafe = NewPackage("unsafe", "unsafe")
243 Unsafe.complete = true
244
245 defPredeclaredTypes()
246 defPredeclaredConsts()
247 defPredeclaredNil()
248 defPredeclaredFuncs()
249
250 universeIota = Universe.Lookup("iota")
251 universeBool = Universe.Lookup("bool").Type()
252 universeByte = Universe.Lookup("byte").Type()
253 universeRune = Universe.Lookup("rune").Type()
254 universeError = Universe.Lookup("error").Type()
255 universeAny = Universe.Lookup("any")
256 universeComparable = Universe.Lookup("comparable")
257 }
258
259
260
261
262 func def(obj Object) {
263 assert(obj.Type() != nil)
264 name := obj.Name()
265 if strings.Contains(name, " ") {
266 return
267 }
268
269 if typ := asNamed(obj.Type()); typ != nil {
270 typ.obj = obj.(*TypeName)
271 }
272
273 scope := Universe
274 if obj.Exported() {
275 scope = Unsafe.scope
276
277 switch obj := obj.(type) {
278 case *TypeName:
279 obj.pkg = Unsafe
280 case *Builtin:
281 obj.pkg = Unsafe
282 default:
283 panic("unreachable")
284 }
285 }
286 if scope.Insert(obj) != nil {
287 panic("double declaration of predeclared identifier")
288 }
289 }
290
View as plain text