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 "go/token" 12 ) 13 14 // An Alias represents an alias type. 15 // 16 // Alias types are created by alias declarations such as: 17 // 18 // type A = int 19 // 20 // The type on the right-hand side of the declaration can be accessed 21 // using [Alias.Rhs]. This type may itself be an alias. 22 // Call [Unalias] to obtain the first non-alias type in a chain of 23 // alias type declarations. 24 // 25 // Like a defined ([Named]) type, an alias type has a name. 26 // Use the [Alias.Obj] method to access its [TypeName] object. 27 type Alias struct { 28 obj *TypeName // corresponding declared alias object 29 orig *Alias // original, uninstantiated alias 30 tparams *TypeParamList // type parameters, or nil 31 targs *TypeList // type arguments, or nil 32 fromRHS Type // RHS of type alias declaration; may be an alias 33 actual Type // actual (aliased) type; never an alias 34 } 35 36 // NewAlias creates a new Alias type with the given type name and rhs. 37 // If rhs is nil, the alias is incomplete. 38 func NewAlias(obj *TypeName, rhs Type) *Alias { 39 alias := (*Checker)(nil).newAlias(obj, rhs) 40 // Ensure that alias.actual is set (#65455). 41 alias.cleanup() 42 return alias 43 } 44 45 // Obj returns the type name for the declaration defining the alias type a. 46 // For instantiated types, this is same as the type name of the origin type. 47 func (a *Alias) Obj() *TypeName { return a.orig.obj } 48 49 func (a *Alias) String() string { return TypeString(a, nil) } 50 51 // Underlying returns the [underlying type] of the alias type a, which is the 52 // underlying type of the aliased type. Underlying types are never Named, 53 // TypeParam, or Alias types. 54 // 55 // [underlying type]: https://go.dev/ref/spec#Underlying_types. 56 func (a *Alias) Underlying() Type { return unalias(a).Underlying() } 57 58 // Origin returns the generic Alias type of which a is an instance. 59 // If a is not an instance of a generic alias, Origin returns a. 60 func (a *Alias) Origin() *Alias { return a.orig } 61 62 // TypeParams returns the type parameters of the alias type a, or nil. 63 // A generic Alias and its instances have the same type parameters. 64 func (a *Alias) TypeParams() *TypeParamList { return a.tparams } 65 66 // SetTypeParams sets the type parameters of the alias type a. 67 // The alias a must not have type arguments. 68 func (a *Alias) SetTypeParams(tparams []*TypeParam) { 69 assert(a.targs == nil) 70 a.tparams = bindTParams(tparams) 71 } 72 73 // TypeArgs returns the type arguments used to instantiate the Alias type. 74 // If a is not an instance of a generic alias, the result is nil. 75 func (a *Alias) TypeArgs() *TypeList { return a.targs } 76 77 // Rhs returns the type R on the right-hand side of an alias 78 // declaration "type A = R", which may be another alias. 79 func (a *Alias) Rhs() Type { return a.fromRHS } 80 81 // Unalias returns t if it is not an alias type; 82 // otherwise it follows t's alias chain until it 83 // reaches a non-alias type which is then returned. 84 // Consequently, the result is never an alias type. 85 // Returns nil if the alias is incomplete. 86 func Unalias(t Type) Type { 87 if a0, _ := t.(*Alias); a0 != nil { 88 return unalias(a0) 89 } 90 return t 91 } 92 93 func unalias(a0 *Alias) Type { 94 if a0.actual != nil { 95 return a0.actual 96 } 97 var t Type 98 for a := a0; a != nil; a, _ = t.(*Alias) { 99 t = a.fromRHS 100 } 101 // It's fine to memoize nil types since it's the zero value for actual. 102 // It accomplishes nothing. 103 a0.actual = t 104 return t 105 } 106 107 // asNamed returns t as *Named if that is t's 108 // actual type. It returns nil otherwise. 109 func asNamed(t Type) *Named { 110 n, _ := Unalias(t).(*Named) 111 return n 112 } 113 114 // newAlias creates a new Alias type with the given type name and rhs. 115 // If rhs is nil, the alias is incomplete. 116 func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias { 117 a := new(Alias) 118 a.obj = obj 119 a.orig = a 120 a.fromRHS = rhs 121 if obj.typ == nil { 122 obj.typ = a 123 } 124 125 // Ensure that a.actual is set at the end of type checking. 126 if check != nil { 127 check.needsCleanup(a) 128 } 129 130 return a 131 } 132 133 // newAliasInstance creates a new alias instance for the given origin and type 134 // arguments, recording pos as the position of its synthetic object (for error 135 // reporting). 136 func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias { 137 assert(len(targs) > 0) 138 obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil) 139 rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt) 140 res := check.newAlias(obj, rhs) 141 res.orig = orig 142 res.tparams = orig.tparams 143 res.targs = newTypeList(targs) 144 return res 145 } 146 147 func (a *Alias) cleanup() { 148 // Ensure a.actual is set before types are published, 149 // so unalias is a pure "getter", not a "setter". 150 unalias(a) 151 } 152