type.go raw

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