| 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 ssa |
| 6 | |
| 7 | import ( |
| 8 | "go/types" |
| 9 | |
| 10 | "golang.org/x/tools/internal/typeparams" |
| 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 | terms, err := typeparams.NormalTerms(typ) |
| 52 | if err != nil { |
| 53 | return nil |
| 54 | } |
| 55 | return terms |
| 56 | } |
| 57 | |
| 58 | // typeSetIsEmpty returns true if a typeset is empty. |
| 59 | func typeSetIsEmpty(typ types.Type) bool { |
| 60 | var empty bool |
| 61 | typeset(typ, func(t, _ types.Type) bool { |
| 62 | empty = t == nil |
| 63 | return false |
| 64 | }) |
| 65 | return empty |
| 66 | } |
| 67 | |
| 68 | // isBytestring returns true if T has the same terms as interface{[]byte | string}. |
| 69 | // These act like a core type for some operations: slice expressions, append and copy. |
| 70 | // |
| 71 | // See https://go.dev/ref/spec#Core_types for the details on bytestring. |
| 72 | func isBytestring(T types.Type) bool { |
| 73 | U := T.Underlying() |
| 74 | if _, ok := U.(*types.Interface); !ok { |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | hasBytes, hasString := false, false |
| 79 | ok := underIs(U, func(t types.Type) bool { |
| 80 | switch { |
| 81 | case isString(t): |
| 82 | hasString = true |
| 83 | return true |
| 84 | case isByteSlice(t): |
| 85 | hasBytes = true |
| 86 | return true |
| 87 | default: |
| 88 | return false |
| 89 | } |
| 90 | }) |
| 91 | return ok && hasBytes && hasString |
| 92 | } |
| 93 | |
| 94 | // underIs calls f with the underlying types of the type terms |
| 95 | // of the type set of typ and reports whether all calls to f returned true. |
| 96 | // If there are no specific terms, underIs returns the result of f(nil). |
| 97 | func underIs(typ types.Type, f func(types.Type) bool) bool { |
| 98 | var ok bool |
| 99 | typeset(typ, func(t, u types.Type) bool { |
| 100 | ok = f(u) |
| 101 | return ok |
| 102 | }) |
| 103 | return ok |
| 104 | } |
| 105 | |
| 106 | // indexType returns the element type and index mode of a IndexExpr over a type. |
| 107 | // It returns an invalid mode if the type is not indexable; this should never occur in a well-typed program. |
| 108 | func indexType(typ types.Type) (types.Type, indexMode) { |
| 109 | switch U := typ.Underlying().(type) { |
| 110 | case *types.Array: |
| 111 | return U.Elem(), ixArrVar |
| 112 | case *types.Pointer: |
| 113 | if arr, ok := U.Elem().Underlying().(*types.Array); ok { |
| 114 | return arr.Elem(), ixVar |
| 115 | } |
| 116 | case *types.Slice: |
| 117 | return U.Elem(), ixVar |
| 118 | case *types.Map: |
| 119 | return U.Elem(), ixMap |
| 120 | case *types.Basic: |
| 121 | return tByte, ixValue // must be a string |
| 122 | case *types.Interface: |
| 123 | var elem types.Type |
| 124 | mode := ixInvalid |
| 125 | typeset(typ, func(t, _ types.Type) bool { |
| 126 | if t == nil { |
| 127 | return false // empty set |
| 128 | } |
| 129 | e, m := indexType(t) |
| 130 | if elem == nil { |
| 131 | elem, mode = e, m |
| 132 | } |
| 133 | if debug && !types.Identical(elem, e) { // if type checked, just a sanity check |
| 134 | mode = ixInvalid |
| 135 | return false |
| 136 | } |
| 137 | // Update the mode to the most constrained address type. |
| 138 | mode = mode.meet(m) |
| 139 | return mode != ixInvalid |
| 140 | }) |
| 141 | return elem, mode |
| 142 | } |
| 143 | return nil, ixInvalid |
| 144 | } |
| 145 | |
| 146 | // An indexMode specifies the (addressing) mode of an index operand. |
| 147 | // |
| 148 | // Addressing mode of an index operation is based on the set of |
| 149 | // underlying types. |
| 150 | // Hasse diagram of the indexMode meet semi-lattice: |
| 151 | // |
| 152 | // ixVar ixMap |
| 153 | // | | |
| 154 | // ixArrVar | |
| 155 | // | | |
| 156 | // ixValue | |
| 157 | // \ / |
| 158 | // ixInvalid |
| 159 | type indexMode byte |
| 160 | |
| 161 | const ( |
| 162 | ixInvalid indexMode = iota // index is invalid |
| 163 | ixValue // index is a computed value (not addressable) |
| 164 | ixArrVar // like ixVar, but index operand contains an array |
| 165 | ixVar // index is an addressable variable |
| 166 | ixMap // index is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment) |
| 167 | ) |
| 168 | |
| 169 | // meet is the address type that is constrained by both x and y. |
| 170 | func (x indexMode) meet(y indexMode) indexMode { |
| 171 | if (x == ixMap || y == ixMap) && x != y { |
| 172 | return ixInvalid |
| 173 | } |
| 174 | // Use int representation and return min. |
| 175 | if x < y { |
| 176 | return y |
| 177 | } |
| 178 | return x |
| 179 | } |