github.com/bir3/gocompiler@v0.9.2202/src/go/types/universe.go (about) 1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. 2 3 // Copyright 2011 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file sets up the universe scope and the unsafe package. 8 9 package types 10 11 import ( 12 "github.com/bir3/gocompiler/src/go/constant" 13 "strings" 14 ) 15 16 // The Universe scope contains all predeclared objects of Go. 17 // It is the outermost scope of any chain of nested scopes. 18 var Universe *Scope 19 20 // The Unsafe package is the package returned by an importer 21 // for the import path "unsafe". 22 var Unsafe *Package 23 24 var ( 25 universeIota Object 26 universeByte Type // uint8 alias, but has name "byte" 27 universeRune Type // int32 alias, but has name "rune" 28 universeAny Object 29 universeError Type 30 universeComparable Object 31 ) 32 33 // Typ contains the predeclared *Basic types indexed by their 34 // corresponding BasicKind. 35 // 36 // The *Basic type for Typ[Byte] will have the name "uint8". 37 // Use Universe.Lookup("byte").Type() to obtain the specific 38 // alias basic type named "byte" (and analogous for "rune"). 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 // type any = interface{} 84 // Note: don't use &emptyInterface for the type of any. Using a unique 85 // pointer allows us to detect any and format it as "any" rather than 86 // interface{}, which clarifies user-facing error messages significantly. 87 def(NewTypeName(nopos, nil, "any", &Interface{complete: true, tset: &topTypeSet})) 88 89 // type error interface{ Error() string } 90 { 91 obj := NewTypeName(nopos, nil, "error", nil) 92 obj.setColor(black) 93 typ := NewNamed(obj, nil, nil) 94 95 // error.Error() string 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 // interface{ Error() string } 102 ityp := &Interface{methods: []*Func{err}, complete: true} 103 computeInterfaceTypeSet(nil, nopos, ityp) // prevent races due to lazy computation of tset 104 105 typ.SetUnderlying(ityp) 106 def(obj) 107 } 108 109 // type comparable interface{} // marked as comparable 110 { 111 obj := NewTypeName(nopos, nil, "comparable", nil) 112 obj.setColor(black) 113 typ := NewNamed(obj, nil, nil) 114 115 // interface{} // marked as comparable 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 // A builtinId is the id of a builtin function. 144 type builtinId int 145 146 const ( 147 // universe scope 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 // package unsafe 168 _Add 169 _Alignof 170 _Offsetof 171 _Sizeof 172 _Slice 173 _SliceData 174 _String 175 _StringData 176 177 // testing support 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 // To disable max/min, remove the next two lines. 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 // only define these in testing environment 226 } 227 def(newBuiltin(id)) 228 } 229 } 230 231 // DefPredeclaredTestFuncs defines the assert and trace built-ins. 232 // These built-ins are intended for debugging and testing of this 233 // package only. 234 func DefPredeclaredTestFuncs() { 235 if Universe.Lookup("assert") != nil { 236 return // already defined 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 // Objects with names containing blanks are internal and not entered into 261 // a scope. Objects with exported names are inserted in the unsafe package 262 // scope; other objects are inserted in the universe scope. 263 func def(obj Object) { 264 assert(obj.color() == black) 265 name := obj.Name() 266 if strings.Contains(name, " ") { 267 return // nothing to do 268 } 269 // fix Obj link for named types 270 if typ := asNamed(obj.Type()); typ != nil { 271 typ.obj = obj.(*TypeName) 272 } 273 // exported identifiers go into package unsafe 274 scope := Universe 275 if obj.Exported() { 276 scope = Unsafe.scope 277 // set Pkg field 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 }