| 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 a number of miscellaneous utility functions. |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"go/ast" |
| 12 |
"go/token" |
| 13 |
"go/types" |
| 14 |
"io" |
| 15 |
"os" |
| 16 |
|
| 17 |
"honnef.co/go/tools/go/ast/astutil" |
| 18 |
"honnef.co/go/tools/go/types/typeutil" |
| 19 |
|
| 20 |
"golang.org/x/exp/typeparams" |
| 21 |
) |
| 22 |
|
| 23 |
//// AST utilities |
| 24 |
|
| 25 |
func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } |
| 26 |
|
| 27 |
// isBlankIdent returns true iff e is an Ident with name "_". |
| 28 |
// They have no associated types.Object, and thus no type. |
| 29 |
func isBlankIdent(e ast.Expr) bool { |
| 30 |
id, ok := e.(*ast.Ident) |
| 31 |
return ok && id.Name == "_" |
| 32 |
} |
| 33 |
|
| 34 |
//// Type utilities. Some of these belong in go/types. |
| 35 |
|
| 36 |
// isPointer returns true for types whose underlying type is a pointer, |
| 37 |
// and for type parameters whose core type is a pointer. |
| 38 |
func isPointer(typ types.Type) bool { |
| 39 |
if ctyp := typeutil.CoreType(typ); ctyp != nil { |
| 40 |
_, ok := ctyp.(*types.Pointer) |
| 41 |
return ok |
| 42 |
} |
| 43 |
_, ok := typ.Underlying().(*types.Pointer) |
| 44 |
return ok |
| 45 |
} |
| 46 |
|
| 47 |
// deref returns a pointer's element type; otherwise it returns typ. |
| 48 |
func deref(typ types.Type) types.Type { |
| 49 |
orig := typ |
| 50 |
typ = types.Unalias(typ) |
| 51 |
|
| 52 |
if t, ok := typ.(*types.TypeParam); ok { |
| 53 |
if ctyp := typeutil.CoreType(t); ctyp != nil { |
| 54 |
// This can happen, for example, with len(T) where T is a |
| 55 |
// type parameter whose core type is a pointer to array. |
| 56 |
typ = ctyp |
| 57 |
} |
| 58 |
} |
| 59 |
if p, ok := typ.Underlying().(*types.Pointer); ok { |
| 60 |
return p.Elem() |
| 61 |
} |
| 62 |
return orig |
| 63 |
} |
| 64 |
|
| 65 |
// recvType returns the receiver type of method obj. |
| 66 |
func recvType(obj *types.Func) types.Type { |
| 67 |
return obj.Type().(*types.Signature).Recv().Type() |
| 68 |
} |
| 69 |
|
| 70 |
// logStack prints the formatted "start" message to stderr and |
| 71 |
// returns a closure that prints the corresponding "end" message. |
| 72 |
// Call using 'defer logStack(...)()' to show builder stack on panic. |
| 73 |
// Don't forget trailing parens! |
| 74 |
func logStack(format string, args ...any) func() { |
| 75 |
msg := fmt.Sprintf(format, args...) |
| 76 |
io.WriteString(os.Stderr, msg) |
| 77 |
io.WriteString(os.Stderr, "\n") |
| 78 |
return func() { |
| 79 |
io.WriteString(os.Stderr, msg) |
| 80 |
io.WriteString(os.Stderr, " end\n") |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// newVar creates a 'var' for use in a types.Tuple. |
| 85 |
func newVar(name string, typ types.Type) *types.Var { |
| 86 |
return types.NewParam(token.NoPos, nil, name, typ) |
| 87 |
} |
| 88 |
|
| 89 |
// anonVar creates an anonymous 'var' for use in a types.Tuple. |
| 90 |
func anonVar(typ types.Type) *types.Var { |
| 91 |
return newVar("", typ) |
| 92 |
} |
| 93 |
|
| 94 |
var lenResults = types.NewTuple(anonVar(tInt)) |
| 95 |
|
| 96 |
// makeLen returns the len builtin specialized to type func(T)int. |
| 97 |
func makeLen(T types.Type) *Builtin { |
| 98 |
lenParams := types.NewTuple(anonVar(T)) |
| 99 |
return &Builtin{ |
| 100 |
name: "len", |
| 101 |
sig: types.NewSignatureType(nil, nil, nil, lenParams, lenResults, false), |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
type StackMap struct { |
| 106 |
m []map[Value]Value |
| 107 |
} |
| 108 |
|
| 109 |
func (m *StackMap) Push() { |
| 110 |
m.m = append(m.m, map[Value]Value{}) |
| 111 |
} |
| 112 |
|
| 113 |
func (m *StackMap) Pop() { |
| 114 |
m.m = m.m[:len(m.m)-1] |
| 115 |
} |
| 116 |
|
| 117 |
func (m *StackMap) Get(key Value) (Value, bool) { |
| 118 |
for i := len(m.m) - 1; i >= 0; i-- { |
| 119 |
if v, ok := m.m[i][key]; ok { |
| 120 |
return v, true |
| 121 |
} |
| 122 |
} |
| 123 |
return nil, false |
| 124 |
} |
| 125 |
|
| 126 |
func (m *StackMap) Set(k Value, v Value) { |
| 127 |
m.m[len(m.m)-1][k] = v |
| 128 |
} |
| 129 |
|
| 130 |
// Unwrap recursively unwraps Sigma and Copy nodes. |
| 131 |
func Unwrap(v Value) Value { |
| 132 |
for { |
| 133 |
switch vv := v.(type) { |
| 134 |
case *Sigma: |
| 135 |
v = vv.X |
| 136 |
case *Copy: |
| 137 |
v = vv.X |
| 138 |
default: |
| 139 |
return v |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func assert(x bool) { |
| 145 |
if !x { |
| 146 |
panic("failed assertion") |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// BlockMap is a mapping from basic blocks (identified by their indices) to values. |
| 151 |
type BlockMap[T any] []T |
| 152 |
|
| 153 |
// isBasic reports whether t is a basic type. |
| 154 |
func isBasic(t types.Type) bool { |
| 155 |
_, ok := t.(*types.Basic) |
| 156 |
return ok |
| 157 |
} |
| 158 |
|
| 159 |
// isNonTypeParamInterface reports whether t is an interface type but not a type parameter. |
| 160 |
func isNonTypeParamInterface(t types.Type) bool { |
| 161 |
return !typeparams.IsTypeParam(t) && types.IsInterface(t) |
| 162 |
} |