| 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 ir |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/types" |
| 9 |
|
| 10 |
"honnef.co/go/tools/go/types/typeutil" |
| 11 |
) |
| 12 |
|
| 13 |
// Utilities for dealing with type sets. |
| 14 |
|
| 15 |
const debug = false |
| 16 |
|
| 17 |
// typeset is an iterator over the (type/underlying type) pairs of the |
| 18 |
// specific type terms of the type set implied by t. |
| 19 |
// If t is a type parameter, the implied type set is the type set of t's constraint. |
| 20 |
// In that case, if there are no specific terms, typeset calls yield with (nil, nil). |
| 21 |
// If t is not a type parameter, the implied type set consists of just t. |
| 22 |
// In any case, typeset is guaranteed to call yield at least once. |
| 23 |
func typeset(typ types.Type, yield func(t, u types.Type) bool) { |
| 24 |
switch typ := types.Unalias(typ).(type) { |
| 25 |
case *types.TypeParam, *types.Interface: |
| 26 |
terms := termListOf(typ) |
| 27 |
if len(terms) == 0 { |
| 28 |
yield(nil, nil) |
| 29 |
return |
| 30 |
} |
| 31 |
for _, term := range terms { |
| 32 |
u := types.Unalias(term.Type()) |
| 33 |
if !term.Tilde() { |
| 34 |
u = u.Underlying() |
| 35 |
} |
| 36 |
if debug { |
| 37 |
assert(types.Identical(u, u.Underlying()), "Unalias(x) == under(x) for ~x terms") |
| 38 |
} |
| 39 |
if !yield(term.Type(), u) { |
| 40 |
break |
| 41 |
} |
| 42 |
} |
| 43 |
return |
| 44 |
default: |
| 45 |
yield(typ, typ.Underlying()) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// termListOf returns the type set of typ as a normalized term set. Returns an empty set on an error. |
| 50 |
func termListOf(typ types.Type) []*types.Term { |
| 51 |
return typeutil.NewTypeSet(typ).Terms |
| 52 |
} |
| 53 |
|
| 54 |
// typeSetIsEmpty returns true if a typeset is empty. |
| 55 |
func typeSetIsEmpty(typ types.Type) bool { |
| 56 |
var empty bool |
| 57 |
typeset(typ, func(t, _ types.Type) bool { |
| 58 |
empty = t == nil |
| 59 |
return false |
| 60 |
}) |
| 61 |
return empty |
| 62 |
} |
| 63 |
|
| 64 |
// isBytestring returns true if T has the same terms as interface{[]byte | string}. |
| 65 |
// These act like a core type for some operations: slice expressions, append and copy. |
| 66 |
// |
| 67 |
// See https://go.dev/ref/spec#Core_types for the details on bytestring. |
| 68 |
func isBytestring(T types.Type) bool { |
| 69 |
U := T.Underlying() |
| 70 |
if _, ok := U.(*types.Interface); !ok { |
| 71 |
return false |
| 72 |
} |
| 73 |
|
| 74 |
hasBytes, hasString := false, false |
| 75 |
ok := underIs(U, func(t types.Type) bool { |
| 76 |
switch { |
| 77 |
case isString(t): |
| 78 |
hasString = true |
| 79 |
return true |
| 80 |
case isByteSlice(t): |
| 81 |
hasBytes = true |
| 82 |
return true |
| 83 |
default: |
| 84 |
return false |
| 85 |
} |
| 86 |
}) |
| 87 |
return ok && hasBytes && hasString |
| 88 |
} |
| 89 |
|
| 90 |
// underIs calls f with the underlying types of the type terms |
| 91 |
// of the type set of typ and reports whether all calls to f returned true. |
| 92 |
// If there are no specific terms, underIs returns the result of f(nil). |
| 93 |
func underIs(typ types.Type, f func(types.Type) bool) bool { |
| 94 |
var ok bool |
| 95 |
typeset(typ, func(t, u types.Type) bool { |
| 96 |
ok = f(u) |
| 97 |
return ok |
| 98 |
}) |
| 99 |
return ok |
| 100 |
} |
| 101 |
|
| 102 |
// indexType returns the element type and index mode of a IndexExpr over a type. |
| 103 |
// It returns an invalid mode if the type is not indexable; this should never occur in a well-typed program. |
| 104 |
func indexType(typ types.Type) (types.Type, indexMode) { |
| 105 |
switch U := typ.Underlying().(type) { |
| 106 |
case *types.Array: |
| 107 |
return U.Elem(), ixArrVar |
| 108 |
case *types.Pointer: |
| 109 |
if arr, ok := U.Elem().Underlying().(*types.Array); ok { |
| 110 |
return arr.Elem(), ixVar |
| 111 |
} |
| 112 |
case *types.Slice: |
| 113 |
return U.Elem(), ixVar |
| 114 |
case *types.Map: |
| 115 |
return U.Elem(), ixMap |
| 116 |
case *types.Basic: |
| 117 |
return tByte, ixValue // must be a string |
| 118 |
case *types.Interface: |
| 119 |
var elem types.Type |
| 120 |
mode := ixInvalid |
| 121 |
typeset(typ, func(t, _ types.Type) bool { |
| 122 |
if t == nil { |
| 123 |
return false // empty set |
| 124 |
} |
| 125 |
e, m := indexType(t) |
| 126 |
if elem == nil { |
| 127 |
elem, mode = e, m |
| 128 |
} |
| 129 |
if debug && !types.Identical(elem, e) { // if type checked, just a sanity check |
| 130 |
mode = ixInvalid |
| 131 |
return false |
| 132 |
} |
| 133 |
// Update the mode to the most constrained address type. |
| 134 |
mode = mode.meet(m) |
| 135 |
return mode != ixInvalid |
| 136 |
}) |
| 137 |
return elem, mode |
| 138 |
} |
| 139 |
return nil, ixInvalid |
| 140 |
} |
| 141 |
|
| 142 |
// An indexMode specifies the (addressing) mode of an index operand. |
| 143 |
// |
| 144 |
// Addressing mode of an index operation is based on the set of |
| 145 |
// underlying types. |
| 146 |
// Hasse diagram of the indexMode meet semi-lattice: |
| 147 |
// |
| 148 |
// ixVar ixMap |
| 149 |
// | | |
| 150 |
// ixArrVar | |
| 151 |
// | | |
| 152 |
// ixValue | |
| 153 |
// \ / |
| 154 |
// ixInvalid |
| 155 |
type indexMode byte |
| 156 |
|
| 157 |
const ( |
| 158 |
ixInvalid indexMode = iota // index is invalid |
| 159 |
ixValue // index is a computed value (not addressable) |
| 160 |
ixArrVar // like ixVar, but index operand contains an array |
| 161 |
ixVar // index is an addressable variable |
| 162 |
ixMap // index is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment) |
| 163 |
) |
| 164 |
|
| 165 |
// meet is the address type that is constrained by both x and y. |
| 166 |
func (x indexMode) meet(y indexMode) indexMode { |
| 167 |
if (x == ixMap || y == ixMap) && x != y { |
| 168 |
return ixInvalid |
| 169 |
} |
| 170 |
// Use int representation and return min. |
| 171 |
if x < y { |
| 172 |
return y |
| 173 |
} |
| 174 |
return x |
| 175 |
} |