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  	fromRHS Type      // RHS of type alias declaration; may be an alias
    18  	actual  Type      // actual (aliased) type; never an alias
    19  }
    20  
    21  // NewAlias creates a new Alias type with the given type name and rhs.
    22  // rhs must not be nil.
    23  func NewAlias(obj *TypeName, rhs Type) *Alias {
    24  	alias := (*Checker)(nil).newAlias(obj, rhs)
    25  	// Ensure that alias.actual is set (#65455).
    26  	unalias(alias)
    27  	return alias
    28  }
    29  
    30  func (a *Alias) Obj() *TypeName   { return a.obj }
    31  func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
    32  func (a *Alias) String() string   { return TypeString(a, nil) }
    33  
    34  // Type accessors
    35  
    36  // Unalias returns t if it is not an alias type;
    37  // otherwise it follows t's alias chain until it
    38  // reaches a non-alias type which is then returned.
    39  // Consequently, the result is never an alias type.
    40  func Unalias(t Type) Type {
    41  	if a0, _ := t.(*Alias); a0 != nil {
    42  		return unalias(a0)
    43  	}
    44  	return t
    45  }
    46  
    47  func unalias(a0 *Alias) Type {
    48  	if a0.actual != nil {
    49  		return a0.actual
    50  	}
    51  	var t Type
    52  	for a := a0; a != nil; a, _ = t.(*Alias) {
    53  		t = a.fromRHS
    54  	}
    55  	if t == nil {
    56  		panic(fmt.Sprintf("non-terminated alias %s", a0.obj.name))
    57  	}
    58  	a0.actual = t
    59  	return t
    60  }
    61  
    62  // asNamed returns t as *Named if that is t's
    63  // actual type. It returns nil otherwise.
    64  func asNamed(t Type) *Named {
    65  	n, _ := Unalias(t).(*Named)
    66  	return n
    67  }
    68  
    69  // newAlias creates a new Alias type with the given type name and rhs.
    70  // rhs must not be nil.
    71  func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
    72  	assert(rhs != nil)
    73  	a := &Alias{obj, rhs, nil}
    74  	if obj.typ == nil {
    75  		obj.typ = a
    76  	}
    77  
    78  	// Ensure that a.actual is set at the end of type checking.
    79  	if check != nil {
    80  		check.needsCleanup(a)
    81  	}
    82  
    83  	return a
    84  }
    85  
    86  func (a *Alias) cleanup() {
    87  	Unalias(a)
    88  }
    89  

View as plain text