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
5package syntax
6
7import "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.)
14type 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.
29type 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.
39type TypeAndValue struct {
40	Type  Type
41	Value constant.Value
42	exprFlags
43}
44
45type exprFlags uint16
46
47func (f exprFlags) IsVoid() bool          { return f&1 != 0 }
48func (f exprFlags) IsType() bool          { return f&2 != 0 }
49func (f exprFlags) IsBuiltin() bool       { return f&4 != 0 } // a language builtin that resembles a function call, e.g., "make, append, new"
50func (f exprFlags) IsValue() bool         { return f&8 != 0 }
51func (f exprFlags) IsNil() bool           { return f&16 != 0 }
52func (f exprFlags) Addressable() bool     { return f&32 != 0 }
53func (f exprFlags) Assignable() bool      { return f&64 != 0 }
54func (f exprFlags) HasOk() bool           { return f&128 != 0 }
55func (f exprFlags) IsRuntimeHelper() bool { return f&256 != 0 } // a runtime function called from transformed syntax
56
57func (f *exprFlags) SetIsVoid()          { *f |= 1 }
58func (f *exprFlags) SetIsType()          { *f |= 2 }
59func (f *exprFlags) SetIsBuiltin()       { *f |= 4 }
60func (f *exprFlags) SetIsValue()         { *f |= 8 }
61func (f *exprFlags) SetIsNil()           { *f |= 16 }
62func (f *exprFlags) SetAddressable()     { *f |= 32 }
63func (f *exprFlags) SetAssignable()      { *f |= 64 }
64func (f *exprFlags) SetHasOk()           { *f |= 128 }
65func (f *exprFlags) SetIsRuntimeHelper() { *f |= 256 }
66
67// a typeAndValue contains the results of typechecking an expression.
68// It is embedded in expression nodes.
69type typeAndValue struct {
70	tv TypeAndValue
71}
72
73func (x *typeAndValue) SetTypeInfo(tv TypeAndValue) {
74	x.tv = tv
75}
76func (x *typeAndValue) GetTypeInfo() TypeAndValue {
77	return x.tv
78}
79