Debian Codegraph @

go/ir/const.go

raw
1 // Copyright 2013 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 ir
6
7 // This file defines the Const SSA value type.
8
9 import (
10 "bytes"
11 "fmt"
12 "go/ast"
13 "go/constant"
14 "go/types"
15 "strconv"
16
17 "golang.org/x/exp/typeparams"
18 "honnef.co/go/tools/internal/xtools-internal/typesinternal"
19 )
20
21 // soleTypeKind returns a BasicInfo for which constant.Value can
22 // represent all zero values for the types in the type set.
23 //
24 // types.IsBoolean for false is a representative.
25 // types.IsInteger for 0
26 // types.IsString for ""
27 // 0 otherwise.
28 func soleTypeKind(typ types.Type) types.BasicInfo {
29 // State records the set of possible zero values (false, 0, "").
30 // Candidates (perhaps all) are eliminated during the type-set
31 // iteration, which executes at least once.
32 state := types.IsBoolean | types.IsInteger | types.IsString
33 underIs(typ, func(ut types.Type) bool {
34 var c types.BasicInfo
35 if t, ok := ut.(*types.Basic); ok {
36 c = t.Info()
37 }
38 if c&types.IsNumeric != 0 { // int/float/complex
39 c = types.IsInteger
40 }
41 state = state & c
42 return state != 0
43 })
44 return state
45 }
46
47 // NewConst returns a new constant of the specified value and type.
48 // val must be valid according to the specification of Const.Value.
49 func NewConst(val constant.Value, typ types.Type, source ast.Node) *Const {
50 if val == nil {
51 switch soleTypeKind(typ) {
52 case types.IsBoolean:
53 val = constant.MakeBool(false)
54 case types.IsInteger:
55 val = constant.MakeInt64(0)
56 case types.IsString:
57 val = constant.MakeString("")
58 }
59 }
60 c := &Const{
61 typ: typ,
62 Value: val,
63 }
64 c.setSource(source)
65 return c
66 }
67
68 // intConst returns an 'int' constant that evaluates to i.
69 // (i is an int64 in case the host is narrower than the target.)
70 func intConst(i int64, source ast.Node) *Const {
71 return NewConst(constant.MakeInt64(i), tInt, source)
72 }
73
74 // nilConst returns a nil constant of the specified type, which may
75 // be any reference type, including interfaces.
76 func nilConst(typ types.Type, source ast.Node) *Const {
77 return NewConst(nil, typ, source)
78 }
79
80 // stringConst returns a 'string' constant that evaluates to s.
81 func stringConst(s string, source ast.Node) *Const {
82 return NewConst(constant.MakeString(s), tString, source)
83 }
84
85 // zeroConst returns a new "zero" constant of the specified type.
86 func zeroConst(t types.Type, source ast.Node) Constant {
87 return NewConst(nil, t, source)
88 }
89
90 func (c *Const) RelString(from *types.Package) string {
91 var p string
92 if c.Value == nil {
93 switch c.typ.(type) {
94 case *types.Array, *types.Struct:
95 p = "{}"
96 case *types.Alias:
97 switch c.typ.Underlying().(type) {
98 case *types.Array, *types.Struct:
99 p = "{}"
100 default:
101 p, _ = typesinternal.ZeroString(types.Unalias(c.typ), types.RelativeTo(from))
102 }
103 default:
104 p, _ = typesinternal.ZeroString(c.typ, types.RelativeTo(from))
105 }
106 } else if c.Value.Kind() == constant.String {
107 v := constant.StringVal(c.Value)
108 const max = 20
109 // TODO(adonovan): don't cut a rune in half.
110 if len(v) > max {
111 v = v[:max-3] + "..." // abbreviate
112 }
113 p = strconv.Quote(v)
114 } else {
115 p = c.Value.String()
116 }
117 return p + ":" + relType(c.Type(), from)
118 }
119
120 func (c *Const) Name() string {
121 return c.RelString(nil)
122 }
123
124 func (c *Const) String() string {
125 return c.RelString(nil)
126 }
127
128 func (c *Const) Type() types.Type {
129 return c.typ
130 }
131
132 func (c *Const) Referrers() *[]Instruction {
133 return nil
134 }
135
136 func (c *Const) Parent() *Function { return nil }
137
138 func (v *AggregateConst) RelString(pkg *types.Package) string {
139 var b bytes.Buffer
140 fmt.Fprint(&b, "const {")
141 for i, vv := range v.Values {
142 if i > 0 {
143 fmt.Fprint(&b, ", ")
144 }
145 fmt.Fprint(&b, relName(vv, v))
146 }
147 fmt.Fprint(&b, "}")
148 return b.String()
149 }
150
151 func (v *AggregateConst) Name() string {
152 return v.RelString(nil)
153 }
154
155 func (v *AggregateConst) String() string {
156 return v.RelString(nil)
157 }
158
159 func (v *AggregateConst) Type() types.Type {
160 return v.typ
161 }
162
163 func (v *AggregateConst) Referrers() *[]Instruction {
164 return nil
165 }
166
167 func (v *AggregateConst) Parent() *Function { return nil }
168
169 // IsNil returns true if this constant represents a typed or untyped nil value.
170 func (c *Const) IsNil() bool {
171 return c.Value == nil && nillable(c.typ)
172 }
173
174 // nillable reports whether *new(T) == nil is legal for type T.
175 func nillable(t types.Type) bool {
176 if typeparams.IsTypeParam(t) {
177 return underIs(t, func(u types.Type) bool {
178 // empty type set (u==nil) => any underlying types => not nillable
179 return u != nil && nillable(u)
180 })
181 }
182 switch t.Underlying().(type) {
183 case *types.Pointer, *types.Slice, *types.Chan, *types.Map, *types.Signature:
184 return true
185 case *types.Interface:
186 return true // basic interface.
187 default:
188 return false
189 }
190 }
191
192 // TODO(adonovan): move everything below into honnef.co/go/tools/go/ir/interp.
193
194 // Int64 returns the numeric value of this constant truncated to fit
195 // a signed 64-bit integer.
196 func (c *Const) Int64() int64 {
197 switch x := constant.ToInt(c.Value); x.Kind() {
198 case constant.Int:
199 if i, ok := constant.Int64Val(x); ok {
200 return i
201 }
202 return 0
203 case constant.Float:
204 f, _ := constant.Float64Val(x)
205 return int64(f)
206 }
207 panic(fmt.Sprintf("unexpected constant value: %T", c.Value))
208 }
209
210 // Uint64 returns the numeric value of this constant truncated to fit
211 // an unsigned 64-bit integer.
212 func (c *Const) Uint64() uint64 {
213 switch x := constant.ToInt(c.Value); x.Kind() {
214 case constant.Int:
215 if u, ok := constant.Uint64Val(x); ok {
216 return u
217 }
218 return 0
219 case constant.Float:
220 f, _ := constant.Float64Val(x)
221 return uint64(f)
222 }
223 panic(fmt.Sprintf("unexpected constant value: %T", c.Value))
224 }
225
226 // Float64 returns the numeric value of this constant truncated to fit
227 // a float64.
228 func (c *Const) Float64() float64 {
229 x := constant.ToFloat(c.Value) // (c.Value == nil) => x.Kind() == Unknown
230 f, _ := constant.Float64Val(x)
231 return f
232 }
233
234 // Complex128 returns the complex value of this constant truncated to
235 // fit a complex128.
236 func (c *Const) Complex128() complex128 {
237 x := constant.ToComplex(c.Value) // (c.Value == nil) => x.Kind() == Unknown
238 re, _ := constant.Float64Val(constant.Real(x))
239 im, _ := constant.Float64Val(constant.Imag(x))
240 return complex(re, im)
241 }