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