predeclared.go (2116B)
1 // Copyright 2024 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gcimporter 6 7 import ( 8 "go/types" 9 "sync" 10 ) 11 12 // predecl is a cache for the predeclared types in types.Universe. 13 // 14 // Cache a distinct result based on the runtime value of any. 15 // The pointer value of the any type varies based on GODEBUG settings. 16 var predeclMu sync.Mutex 17 var predecl map[types.Type][]types.Type 18 19 func predeclared() []types.Type { 20 anyt := types.Universe.Lookup("any").Type() 21 22 predeclMu.Lock() 23 defer predeclMu.Unlock() 24 25 if pre, ok := predecl[anyt]; ok { 26 return pre 27 } 28 29 if predecl == nil { 30 predecl = make(map[types.Type][]types.Type) 31 } 32 33 decls := []types.Type{ // basic types 34 types.Typ[types.Bool], 35 types.Typ[types.Int], 36 types.Typ[types.Int8], 37 types.Typ[types.Int16], 38 types.Typ[types.Int32], 39 types.Typ[types.Int64], 40 types.Typ[types.Uint], 41 types.Typ[types.Uint8], 42 types.Typ[types.Uint16], 43 types.Typ[types.Uint32], 44 types.Typ[types.Uint64], 45 types.Typ[types.Uintptr], 46 types.Typ[types.Float32], 47 types.Typ[types.Float64], 48 types.Typ[types.Complex64], 49 types.Typ[types.Complex128], 50 types.Typ[types.String], 51 52 // basic type aliases 53 types.Universe.Lookup("byte").Type(), 54 types.Universe.Lookup("rune").Type(), 55 56 // error 57 types.Universe.Lookup("error").Type(), 58 59 // untyped types 60 types.Typ[types.UntypedBool], 61 types.Typ[types.UntypedInt], 62 types.Typ[types.UntypedRune], 63 types.Typ[types.UntypedFloat], 64 types.Typ[types.UntypedComplex], 65 types.Typ[types.UntypedString], 66 types.Typ[types.UntypedNil], 67 68 // package unsafe 69 types.Typ[types.UnsafePointer], 70 71 // invalid type 72 types.Typ[types.Invalid], // only appears in packages with errors 73 74 // used internally by gc; never used by this package or in .a files 75 anyType{}, 76 77 // comparable 78 types.Universe.Lookup("comparable").Type(), 79 80 // any 81 anyt, 82 } 83 84 predecl[anyt] = decls 85 return decls 86 } 87 88 type anyType struct{} 89 90 func (t anyType) Underlying() types.Type { return t } 91 func (t anyType) String() string { return "any" }