Source file src/go/types/alias.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  
     3  // Copyright 2023 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package types
     8  
     9  import "fmt"
    10  
    11  // An Alias represents an alias type.
    12  // Whether or not Alias types are created is controlled by the
    13  // gotypesalias setting with the GODEBUG environment variable.
    14  // For gotypesalias=1, alias declarations produce an Alias type.
    15  // Otherwise, the alias information is only in the type name,
    16  // which points directly to the actual (aliased) type.
    17  type Alias struct {
    18  	obj     *TypeName // corresponding declared alias object
    19  	fromRHS Type      // RHS of type alias declaration; may be an alias
    20  	actual  Type      // actual (aliased) type; never an alias
    21  }
    22  
    23  // NewAlias creates a new Alias type with the given type name and rhs.
    24  // rhs must not be nil.
    25  func NewAlias(obj *TypeName, rhs Type) *Alias {
    26  	alias := (*Checker)(nil).newAlias(obj, rhs)
    27  	// Ensure that alias.actual is set (#65455).
    28  	unalias(alias)
    29  	return alias
    30  }
    31  
    32  func (a *Alias) Obj() *TypeName   { return a.obj }
    33  func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
    34  func (a *Alias) String() string   { return TypeString(a, nil) }
    35  
    36  // Type accessors
    37  
    38  // Unalias returns t if it is not an alias type;
    39  // otherwise it follows t's alias chain until it
    40  // reaches a non-alias type which is then returned.
    41  // Consequently, the result is never an alias type.
    42  func Unalias(t Type) Type {
    43  	if a0, _ := t.(*Alias); a0 != nil {
    44  		return unalias(a0)
    45  	}
    46  	return t
    47  }
    48  
    49  func unalias(a0 *Alias) Type {
    50  	if a0.actual != nil {
    51  		return a0.actual
    52  	}
    53  	var t Type
    54  	for a := a0; a != nil; a, _ = t.(*Alias) {
    55  		t = a.fromRHS
    56  	}
    57  	if t == nil {
    58  		panic(fmt.Sprintf("non-terminated alias %s", a0.obj.name))
    59  	}
    60  	a0.actual = t
    61  	return t
    62  }
    63  
    64  // asNamed returns t as *Named if that is t's
    65  // actual type. It returns nil otherwise.
    66  func asNamed(t Type) *Named {
    67  	n, _ := Unalias(t).(*Named)
    68  	return n
    69  }
    70  
    71  // newAlias creates a new Alias type with the given type name and rhs.
    72  // rhs must not be nil.
    73  func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
    74  	assert(rhs != nil)
    75  	a := &Alias{obj, rhs, nil}
    76  	if obj.typ == nil {
    77  		obj.typ = a
    78  	}
    79  
    80  	// Ensure that a.actual is set at the end of type checking.
    81  	if check != nil {
    82  		check.needsCleanup(a)
    83  	}
    84  
    85  	return a
    86  }
    87  
    88  func (a *Alias) cleanup() {
    89  	Unalias(a)
    90  }
    91  

View as plain text