Source File
alias.go
Belonging Package
go/types
// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
// Source: ../../cmd/compile/internal/types2/alias.go
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
)
// An Alias represents an alias type.
// Whether or not Alias types are created is controlled by the
// gotypesalias setting with the GODEBUG environment variable.
// For gotypesalias=1, alias declarations produce an Alias type.
// Otherwise, the alias information is only in the type name,
// which points directly to the actual (aliased) type.
type Alias struct {
obj *TypeName // corresponding declared alias object
orig *Alias // original, uninstantiated alias
tparams *TypeParamList // type parameters, or nil
targs *TypeList // type arguments, or nil
fromRHS Type // RHS of type alias declaration; may be an alias
actual Type // actual (aliased) type; never an alias
}
// NewAlias creates a new Alias type with the given type name and rhs.
// rhs must not be nil.
func ( *TypeName, Type) *Alias {
:= (*Checker)(nil).newAlias(, )
// Ensure that alias.actual is set (#65455).
.cleanup()
return
}
// Obj returns the type name for the declaration defining the alias type a.
// For instantiated types, this is same as the type name of the origin type.
func ( *Alias) () *TypeName { return .orig.obj }
func ( *Alias) () string { return TypeString(, nil) }
// Underlying returns the [underlying type] of the alias type a, which is the
// underlying type of the aliased type. Underlying types are never Named,
// TypeParam, or Alias types.
//
// [underlying type]: https://go.dev/ref/spec#Underlying_types.
func ( *Alias) () Type { return unalias().Underlying() }
// Origin returns the generic Alias type of which a is an instance.
// If a is not an instance of a generic alias, Origin returns a.
func ( *Alias) () *Alias { return .orig }
// TypeParams returns the type parameters of the alias type a, or nil.
// A generic Alias and its instances have the same type parameters.
func ( *Alias) () *TypeParamList { return .tparams }
// SetTypeParams sets the type parameters of the alias type a.
// The alias a must not have type arguments.
func ( *Alias) ( []*TypeParam) {
assert(.targs == nil)
.tparams = bindTParams()
}
// TypeArgs returns the type arguments used to instantiate the Alias type.
// If a is not an instance of a generic alias, the result is nil.
func ( *Alias) () *TypeList { return .targs }
// Rhs returns the type R on the right-hand side of an alias
// declaration "type A = R", which may be another alias.
func ( *Alias) () Type { return .fromRHS }
// Unalias returns t if it is not an alias type;
// otherwise it follows t's alias chain until it
// reaches a non-alias type which is then returned.
// Consequently, the result is never an alias type.
func ( Type) Type {
if , := .(*Alias); != nil {
return unalias()
}
return
}
func unalias( *Alias) Type {
if .actual != nil {
return .actual
}
var Type
for := ; != nil; , _ = .(*Alias) {
= .fromRHS
}
if == nil {
panic(fmt.Sprintf("non-terminated alias %s", .obj.name))
}
// Memoize the type only if valid.
// In the presence of unfinished cyclic declarations, Unalias
// would otherwise latch the invalid value (#66704).
// TODO(adonovan): rethink, along with checker.typeDecl's use
// of Invalid to mark unfinished aliases.
if != Typ[Invalid] {
.actual =
}
return
}
// asNamed returns t as *Named if that is t's
// actual type. It returns nil otherwise.
func asNamed( Type) *Named {
, := Unalias().(*Named)
return
}
// newAlias creates a new Alias type with the given type name and rhs.
// rhs must not be nil.
func ( *Checker) ( *TypeName, Type) *Alias {
assert( != nil)
:= new(Alias)
.obj =
.orig =
.fromRHS =
if .typ == nil {
.typ =
}
// Ensure that a.actual is set at the end of type checking.
if != nil {
.needsCleanup()
}
return
}
// newAliasInstance creates a new alias instance for the given origin and type
// arguments, recording pos as the position of its synthetic object (for error
// reporting).
func ( *Checker) ( token.Pos, *Alias, []Type, *Context) *Alias {
assert(len() > 0)
:= NewTypeName(, .obj.pkg, .obj.name, nil)
:= .subst(, .fromRHS, makeSubstMap(.TypeParams().list(), ), nil, )
:= .newAlias(, )
.orig =
.tparams = .tparams
.targs = newTypeList()
return
}
func ( *Alias) () {
// Ensure a.actual is set before types are published,
// so Unalias is a pure "getter", not a "setter".
:= Unalias()
if == Typ[Invalid] {
// We don't set a.actual to Typ[Invalid] during type checking,
// as it may indicate that the RHS is not fully set up.
.actual =
}
}
The pages are generated with Golds v0.7.0-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |