Source file src/go/types/alias.go
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. 2 // Source: ../../cmd/compile/internal/types2/alias.go 3 4 // Copyright 2023 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package types 9 10 import ( 11 "fmt" 12 "go/token" 13 ) 14 15 // An Alias represents an alias type. 16 // Whether or not Alias types are created is controlled by the 17 // gotypesalias setting with the GODEBUG environment variable. 18 // For gotypesalias=1, alias declarations produce an Alias type. 19 // Otherwise, the alias information is only in the type name, 20 // which points directly to the actual (aliased) type. 21 type Alias struct { 22 obj *TypeName // corresponding declared alias object 23 orig *Alias // original, uninstantiated alias 24 tparams *TypeParamList // type parameters, or nil 25 targs *TypeList // type arguments, or nil 26 fromRHS Type // RHS of type alias declaration; may be an alias 27 actual Type // actual (aliased) type; never an alias 28 } 29 30 // NewAlias creates a new Alias type with the given type name and rhs. 31 // rhs must not be nil. 32 func NewAlias(obj *TypeName, rhs Type) *Alias { 33 alias := (*Checker)(nil).newAlias(obj, rhs) 34 // Ensure that alias.actual is set (#65455). 35 alias.cleanup() 36 return alias 37 } 38 39 // Obj returns the type name for the declaration defining the alias type a. 40 // For instantiated types, this is same as the type name of the origin type. 41 func (a *Alias) Obj() *TypeName { return a.orig.obj } 42 43 func (a *Alias) String() string { return TypeString(a, nil) } 44 45 // Underlying returns the [underlying type] of the alias type a, which is the 46 // underlying type of the aliased type. Underlying types are never Named, 47 // TypeParam, or Alias types. 48 // 49 // [underlying type]: https://go.dev/ref/spec#Underlying_types. 50 func (a *Alias) Underlying() Type { return unalias(a).Underlying() } 51 52 // Origin returns the generic Alias type of which a is an instance. 53 // If a is not an instance of a generic alias, Origin returns a. 54 func (a *Alias) Origin() *Alias { return a.orig } 55 56 // TypeParams returns the type parameters of the alias type a, or nil. 57 // A generic Alias and its instances have the same type parameters. 58 func (a *Alias) TypeParams() *TypeParamList { return a.tparams } 59 60 // SetTypeParams sets the type parameters of the alias type a. 61 // The alias a must not have type arguments. 62 func (a *Alias) SetTypeParams(tparams []*TypeParam) { 63 assert(a.targs == nil) 64 a.tparams = bindTParams(tparams) 65 } 66 67 // TypeArgs returns the type arguments used to instantiate the Alias type. 68 // If a is not an instance of a generic alias, the result is nil. 69 func (a *Alias) TypeArgs() *TypeList { return a.targs } 70 71 // Rhs returns the type R on the right-hand side of an alias 72 // declaration "type A = R", which may be another alias. 73 func (a *Alias) Rhs() Type { return a.fromRHS } 74 75 // Unalias returns t if it is not an alias type; 76 // otherwise it follows t's alias chain until it 77 // reaches a non-alias type which is then returned. 78 // Consequently, the result is never an alias type. 79 func Unalias(t Type) Type { 80 if a0, _ := t.(*Alias); a0 != nil { 81 return unalias(a0) 82 } 83 return t 84 } 85 86 func unalias(a0 *Alias) Type { 87 if a0.actual != nil { 88 return a0.actual 89 } 90 var t Type 91 for a := a0; a != nil; a, _ = t.(*Alias) { 92 t = a.fromRHS 93 } 94 if t == nil { 95 panic(fmt.Sprintf("non-terminated alias %s", a0.obj.name)) 96 } 97 98 // Memoize the type only if valid. 99 // In the presence of unfinished cyclic declarations, Unalias 100 // would otherwise latch the invalid value (#66704). 101 // TODO(adonovan): rethink, along with checker.typeDecl's use 102 // of Invalid to mark unfinished aliases. 103 if t != Typ[Invalid] { 104 a0.actual = t 105 } 106 107 return t 108 } 109 110 // asNamed returns t as *Named if that is t's 111 // actual type. It returns nil otherwise. 112 func asNamed(t Type) *Named { 113 n, _ := Unalias(t).(*Named) 114 return n 115 } 116 117 // newAlias creates a new Alias type with the given type name and rhs. 118 // rhs must not be nil. 119 func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias { 120 assert(rhs != nil) 121 a := new(Alias) 122 a.obj = obj 123 a.orig = a 124 a.fromRHS = rhs 125 if obj.typ == nil { 126 obj.typ = a 127 } 128 129 // Ensure that a.actual is set at the end of type checking. 130 if check != nil { 131 check.needsCleanup(a) 132 } 133 134 return a 135 } 136 137 // newAliasInstance creates a new alias instance for the given origin and type 138 // arguments, recording pos as the position of its synthetic object (for error 139 // reporting). 140 func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias { 141 assert(len(targs) > 0) 142 obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil) 143 rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt) 144 res := check.newAlias(obj, rhs) 145 res.orig = orig 146 res.tparams = orig.tparams 147 res.targs = newTypeList(targs) 148 return res 149 } 150 151 func (a *Alias) cleanup() { 152 // Ensure a.actual is set before types are published, 153 // so Unalias is a pure "getter", not a "setter". 154 actual := Unalias(a) 155 156 if actual == Typ[Invalid] { 157 // We don't set a.actual to Typ[Invalid] during type checking, 158 // as it may indicate that the RHS is not fully set up. 159 a.actual = actual 160 } 161 } 162