Source file src/cmd/compile/internal/syntax/type.go

     1  // Copyright 2022 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 syntax
     6  
     7  import "go/constant"
     8  
     9  // A Type represents a type of Go.
    10  // All types implement the Type interface.
    11  // (This type originally lived in types2. We moved it here
    12  // so we could depend on it from other packages without
    13  // introducing a circularity.)
    14  type Type interface {
    15  	// Underlying returns the underlying type of a type.
    16  	Underlying() Type
    17  
    18  	// String returns a string representation of a type.
    19  	String() string
    20  }
    21  
    22  // Expressions in the syntax package provide storage for
    23  // the typechecker to record its results. This interface
    24  // is the mechanism the typechecker uses to record results,
    25  // and clients use to retrieve those results.
    26  type typeInfo interface {
    27  	SetTypeInfo(TypeAndValue)
    28  	GetTypeInfo() TypeAndValue
    29  }
    30  
    31  // A TypeAndValue records the type information, constant
    32  // value if known, and various other flags associated with
    33  // an expression.
    34  // This type is similar to types2.TypeAndValue, but exposes
    35  // none of types2's internals.
    36  type TypeAndValue struct {
    37  	Type  Type
    38  	Value constant.Value
    39  	exprFlags
    40  }
    41  
    42  type exprFlags uint16
    43  
    44  func (f exprFlags) IsVoid() bool          { return f&1 != 0 }
    45  func (f exprFlags) IsType() bool          { return f&2 != 0 }
    46  func (f exprFlags) IsBuiltin() bool       { return f&4 != 0 } // a language builtin that resembles a function call, e.g., "make, append, new"
    47  func (f exprFlags) IsValue() bool         { return f&8 != 0 }
    48  func (f exprFlags) IsNil() bool           { return f&16 != 0 }
    49  func (f exprFlags) Addressable() bool     { return f&32 != 0 }
    50  func (f exprFlags) Assignable() bool      { return f&64 != 0 }
    51  func (f exprFlags) HasOk() bool           { return f&128 != 0 }
    52  func (f exprFlags) IsRuntimeHelper() bool { return f&256 != 0 } // a runtime function called from transformed syntax
    53  
    54  func (f *exprFlags) SetIsVoid()          { *f |= 1 }
    55  func (f *exprFlags) SetIsType()          { *f |= 2 }
    56  func (f *exprFlags) SetIsBuiltin()       { *f |= 4 }
    57  func (f *exprFlags) SetIsValue()         { *f |= 8 }
    58  func (f *exprFlags) SetIsNil()           { *f |= 16 }
    59  func (f *exprFlags) SetAddressable()     { *f |= 32 }
    60  func (f *exprFlags) SetAssignable()      { *f |= 64 }
    61  func (f *exprFlags) SetHasOk()           { *f |= 128 }
    62  func (f *exprFlags) SetIsRuntimeHelper() { *f |= 256 }
    63  
    64  // a typeAndValue contains the results of typechecking an expression.
    65  // It is embedded in expression nodes.
    66  type typeAndValue struct {
    67  	tv TypeAndValue
    68  }
    69  
    70  func (x *typeAndValue) SetTypeInfo(tv TypeAndValue) {
    71  	x.tv = tv
    72  }
    73  func (x *typeAndValue) GetTypeInfo() TypeAndValue {
    74  	return x.tv
    75  }
    76  

View as plain text