| 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 |
// |
| 28 |
// Historically, Alias types were not materialized so that, in the example |
| 29 |
// above, A's type was represented by a Basic (int), not an Alias |
| 30 |
// whose [Alias.Rhs] is int. But Go 1.24 allows you to declare an |
| 31 |
// alias type with type parameters or arguments: |
| 32 |
// |
| 33 |
// type Set[K comparable] = map[K]bool |
| 34 |
// s := make(Set[String]) |
| 35 |
// |
| 36 |
// and this requires that Alias types be materialized. Use the |
| 37 |
// [Alias.TypeParams] and [Alias.TypeArgs] methods to access them. |
| 38 |
// |
| 39 |
// To ease the transition, the Alias type was introduced in go1.22, |
| 40 |
// but the type-checker would not construct values of this type unless |
| 41 |
// the GODEBUG=gotypesalias=1 environment variable was provided. |
| 42 |
// Starting in go1.23, this variable is enabled by default. |
| 43 |
// This setting also causes the predeclared type "any" to be |
| 44 |
// represented as an Alias, not a bare [Interface]. |
| 45 |
type Alias struct { |
| 46 |
obj *TypeName // corresponding declared alias object |
| 47 |
orig *Alias // original, uninstantiated alias |
| 48 |
tparams *TypeParamList // type parameters, or nil |
| 49 |
targs *TypeList // type arguments, or nil |
| 50 |
fromRHS Type // RHS of type alias declaration; may be an alias |
| 51 |
actual Type // actual (aliased) type; never an alias |
| 52 |
} |
| 53 |
|
| 54 |
// NewAlias creates a new Alias type with the given type name and rhs. |
| 55 |
// If rhs is nil, the alias is incomplete. |
| 56 |
func NewAlias(obj *TypeName, rhs Type) *Alias { |
| 57 |
alias := (*Checker)(nil).newAlias(obj, rhs) |
| 58 |
// Ensure that alias.actual is set (#65455). |
| 59 |
alias.cleanup() |
| 60 |
return alias |
| 61 |
} |
| 62 |
|
| 63 |
// Obj returns the type name for the declaration defining the alias type a. |
| 64 |
// For instantiated types, this is same as the type name of the origin type. |
| 65 |
func (a *Alias) Obj() *TypeName { return a.orig.obj } |
| 66 |
|
| 67 |
func (a *Alias) String() string { return TypeString(a, nil) } |
| 68 |
|
| 69 |
// Underlying returns the [underlying type] of the alias type a, which is the |
| 70 |
// underlying type of the aliased type. Underlying types are never Named, |
| 71 |
// TypeParam, or Alias types. |
| 72 |
// |
| 73 |
// [underlying type]: https://go.dev/ref/spec#Underlying_types. |
| 74 |
func (a *Alias) Underlying() Type { return unalias(a).Underlying() } |
| 75 |
|
| 76 |
// Origin returns the generic Alias type of which a is an instance. |
| 77 |
// If a is not an instance of a generic alias, Origin returns a. |
| 78 |
func (a *Alias) Origin() *Alias { return a.orig } |
| 79 |
|
| 80 |
// TypeParams returns the type parameters of the alias type a, or nil. |
| 81 |
// A generic Alias and its instances have the same type parameters. |
| 82 |
func (a *Alias) TypeParams() *TypeParamList { return a.tparams } |
| 83 |
|
| 84 |
// SetTypeParams sets the type parameters of the alias type a. |
| 85 |
// The alias a must not have type arguments. |
| 86 |
func (a *Alias) SetTypeParams(tparams []*TypeParam) { |
| 87 |
assert(a.targs == nil) |
| 88 |
a.tparams = bindTParams(tparams) |
| 89 |
} |
| 90 |
|
| 91 |
// TypeArgs returns the type arguments used to instantiate the Alias type. |
| 92 |
// If a is not an instance of a generic alias, the result is nil. |
| 93 |
func (a *Alias) TypeArgs() *TypeList { return a.targs } |
| 94 |
|
| 95 |
// Rhs returns the type R on the right-hand side of an alias |
| 96 |
// declaration "type A = R", which may be another alias. |
| 97 |
func (a *Alias) Rhs() Type { return a.fromRHS } |
| 98 |
|
| 99 |
// Unalias returns t if it is not an alias type; |
| 100 |
// otherwise it follows t's alias chain until it |
| 101 |
// reaches a non-alias type which is then returned. |
| 102 |
// Consequently, the result is never an alias type. |
| 103 |
// Returns nil if the alias is incomplete. |
| 104 |
func Unalias(t Type) Type { |
| 105 |
if a0, _ := t.(*Alias); a0 != nil { |
| 106 |
return unalias(a0) |
| 107 |
} |
| 108 |
return t |
| 109 |
} |
| 110 |
|
| 111 |
func unalias(a0 *Alias) Type { |
| 112 |
if a0.actual != nil { |
| 113 |
return a0.actual |
| 114 |
} |
| 115 |
var t Type |
| 116 |
for a := a0; a != nil; a, _ = t.(*Alias) { |
| 117 |
t = a.fromRHS |
| 118 |
} |
| 119 |
// It's fine to memoize nil types since it's the zero value for actual. |
| 120 |
// It accomplishes nothing. |
| 121 |
a0.actual = t |
| 122 |
return t |
| 123 |
} |
| 124 |
|
| 125 |
// asNamed returns t as *Named if that is t's |
| 126 |
// actual type. It returns nil otherwise. |
| 127 |
func asNamed(t Type) *Named { |
| 128 |
n, _ := Unalias(t).(*Named) |
| 129 |
return n |
| 130 |
} |
| 131 |
|
| 132 |
// newAlias creates a new Alias type with the given type name and rhs. |
| 133 |
// If rhs is nil, the alias is incomplete. |
| 134 |
func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias { |
| 135 |
a := new(Alias) |
| 136 |
a.obj = obj |
| 137 |
a.orig = a |
| 138 |
a.fromRHS = rhs |
| 139 |
if obj.typ == nil { |
| 140 |
obj.typ = a |
| 141 |
} |
| 142 |
|
| 143 |
// Ensure that a.actual is set at the end of type checking. |
| 144 |
if check != nil { |
| 145 |
check.needsCleanup(a) |
| 146 |
} |
| 147 |
|
| 148 |
return a |
| 149 |
} |
| 150 |
|
| 151 |
// newAliasInstance creates a new alias instance for the given origin and type |
| 152 |
// arguments, recording pos as the position of its synthetic object (for error |
| 153 |
// reporting). |
| 154 |
func (check *Checker) newAliasInstance(pos token.Pos, orig *Alias, targs []Type, expanding *Named, ctxt *Context) *Alias { |
| 155 |
assert(len(targs) > 0) |
| 156 |
obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil) |
| 157 |
rhs := check.subst(pos, orig.fromRHS, makeSubstMap(orig.TypeParams().list(), targs), expanding, ctxt) |
| 158 |
res := check.newAlias(obj, rhs) |
| 159 |
res.orig = orig |
| 160 |
res.tparams = orig.tparams |
| 161 |
res.targs = newTypeList(targs) |
| 162 |
return res |
| 163 |
} |
| 164 |
|
| 165 |
func (a *Alias) cleanup() { |
| 166 |
// Ensure a.actual is set before types are published, |
| 167 |
// so unalias is a pure "getter", not a "setter". |
| 168 |
unalias(a) |
| 169 |
} |