github.com/bir3/gocompiler@v0.9.2202/src/go/types/alias.go (about)

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