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

View as plain text