gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/simpler/ssa/methods.go (about) 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 // +build go1.5 6 7 package ssa 8 9 // This file defines utilities for population of method sets. 10 11 import ( 12 "fmt" 13 "go/types" 14 ) 15 16 // MethodValue returns the Function implementing method sel, building 17 // wrapper methods on demand. It returns nil if sel denotes an 18 // abstract (interface) method. 19 // 20 // Precondition: sel.Kind() == MethodVal. 21 // 22 // Thread-safe. 23 // 24 // EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) 25 // 26 func (prog *Program) MethodValue(sel *types.Selection) *Function { 27 if sel.Kind() != types.MethodVal { 28 panic(fmt.Sprintf("Method(%s) kind != MethodVal", sel)) 29 } 30 T := sel.Recv() 31 if isInterface(T) { 32 return nil // abstract method 33 } 34 if prog.mode&LogSource != 0 { 35 defer logStack("Method %s %v", T, sel)() 36 } 37 38 prog.methodsMu.Lock() 39 defer prog.methodsMu.Unlock() 40 41 return prog.addMethod(prog.createMethodSet(T), sel) 42 } 43 44 // LookupMethod returns the implementation of the method of type T 45 // identified by (pkg, name). It returns nil if the method exists but 46 // is abstract, and panics if T has no such method. 47 // 48 func (prog *Program) LookupMethod(T types.Type, pkg *types.Package, name string) *Function { 49 sel := prog.MethodSets.MethodSet(T).Lookup(pkg, name) 50 if sel == nil { 51 panic(fmt.Sprintf("%s has no method %s", T, types.Id(pkg, name))) 52 } 53 return prog.MethodValue(sel) 54 } 55 56 // methodSet contains the (concrete) methods of a non-interface type. 57 type methodSet struct { 58 mapping map[string]*Function // populated lazily 59 complete bool // mapping contains all methods 60 } 61 62 // Precondition: !isInterface(T). 63 // EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) 64 func (prog *Program) createMethodSet(T types.Type) *methodSet { 65 mset, ok := prog.methodSets.At(T).(*methodSet) 66 if !ok { 67 mset = &methodSet{mapping: make(map[string]*Function)} 68 prog.methodSets.Set(T, mset) 69 } 70 return mset 71 } 72 73 // EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) 74 func (prog *Program) addMethod(mset *methodSet, sel *types.Selection) *Function { 75 if sel.Kind() == types.MethodExpr { 76 panic(sel) 77 } 78 id := sel.Obj().Id() 79 fn := mset.mapping[id] 80 if fn == nil { 81 obj := sel.Obj().(*types.Func) 82 83 needsPromotion := len(sel.Index()) > 1 84 needsIndirection := !isPointer(recvType(obj)) && isPointer(sel.Recv()) 85 if needsPromotion || needsIndirection { 86 fn = makeWrapper(prog, sel) 87 } else { 88 fn = prog.declaredFunc(obj) 89 } 90 if fn.Signature.Recv() == nil { 91 panic(fn) // missing receiver 92 } 93 mset.mapping[id] = fn 94 } 95 return fn 96 } 97 98 // RuntimeTypes returns a new unordered slice containing all 99 // concrete types in the program for which a complete (non-empty) 100 // method set is required at run-time. 101 // 102 // Thread-safe. 103 // 104 // EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) 105 // 106 func (prog *Program) RuntimeTypes() []types.Type { 107 prog.methodsMu.Lock() 108 defer prog.methodsMu.Unlock() 109 110 var res []types.Type 111 prog.methodSets.Iterate(func(T types.Type, v interface{}) { 112 if v.(*methodSet).complete { 113 res = append(res, T) 114 } 115 }) 116 return res 117 } 118 119 // declaredFunc returns the concrete function/method denoted by obj. 120 // Panic ensues if there is none. 121 // 122 func (prog *Program) declaredFunc(obj *types.Func) *Function { 123 if v := prog.packageLevelValue(obj); v != nil { 124 return v.(*Function) 125 } 126 panic("no concrete method: " + obj.String()) 127 } 128 129 // needMethodsOf ensures that runtime type information (including the 130 // complete method set) is available for the specified type T and all 131 // its subcomponents. 132 // 133 // needMethodsOf must be called for at least every type that is an 134 // operand of some MakeInterface instruction, and for the type of 135 // every exported package member. 136 // 137 // Precondition: T is not a method signature (*Signature with Recv()!=nil). 138 // 139 // Thread-safe. (Called via emitConv from multiple builder goroutines.) 140 // 141 // TODO(adonovan): make this faster. It accounts for 20% of SSA build time. 142 // 143 // EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) 144 // 145 func (prog *Program) needMethodsOf(T types.Type) { 146 prog.methodsMu.Lock() 147 prog.needMethods(T, false) 148 prog.methodsMu.Unlock() 149 } 150 151 // Precondition: T is not a method signature (*Signature with Recv()!=nil). 152 // Recursive case: skip => don't create methods for T. 153 // 154 // EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) 155 // 156 func (prog *Program) needMethods(T types.Type, skip bool) { 157 // Each package maintains its own set of types it has visited. 158 if prevSkip, ok := prog.runtimeTypes.At(T).(bool); ok { 159 // needMethods(T) was previously called 160 if !prevSkip || skip { 161 return // already seen, with same or false 'skip' value 162 } 163 } 164 prog.runtimeTypes.Set(T, skip) 165 166 tmset := prog.MethodSets.MethodSet(T) 167 168 if !skip && !isInterface(T) && tmset.Len() > 0 { 169 // Create methods of T. 170 mset := prog.createMethodSet(T) 171 if !mset.complete { 172 mset.complete = true 173 n := tmset.Len() 174 for i := 0; i < n; i++ { 175 prog.addMethod(mset, tmset.At(i)) 176 } 177 } 178 } 179 180 // Recursion over signatures of each method. 181 for i := 0; i < tmset.Len(); i++ { 182 sig := tmset.At(i).Type().(*types.Signature) 183 prog.needMethods(sig.Params(), false) 184 prog.needMethods(sig.Results(), false) 185 } 186 187 switch t := T.(type) { 188 case *types.Basic: 189 // nop 190 191 case *types.Interface: 192 // nop---handled by recursion over method set. 193 194 case *types.Pointer: 195 prog.needMethods(t.Elem(), false) 196 197 case *types.Slice: 198 prog.needMethods(t.Elem(), false) 199 200 case *types.Chan: 201 prog.needMethods(t.Elem(), false) 202 203 case *types.Map: 204 prog.needMethods(t.Key(), false) 205 prog.needMethods(t.Elem(), false) 206 207 case *types.Signature: 208 if t.Recv() != nil { 209 panic(fmt.Sprintf("Signature %s has Recv %s", t, t.Recv())) 210 } 211 prog.needMethods(t.Params(), false) 212 prog.needMethods(t.Results(), false) 213 214 case *types.Named: 215 // A pointer-to-named type can be derived from a named 216 // type via reflection. It may have methods too. 217 prog.needMethods(types.NewPointer(T), false) 218 219 // Consider 'type T struct{S}' where S has methods. 220 // Reflection provides no way to get from T to struct{S}, 221 // only to S, so the method set of struct{S} is unwanted, 222 // so set 'skip' flag during recursion. 223 prog.needMethods(t.Underlying(), true) 224 225 case *types.Array: 226 prog.needMethods(t.Elem(), false) 227 228 case *types.Struct: 229 for i, n := 0, t.NumFields(); i < n; i++ { 230 prog.needMethods(t.Field(i).Type(), false) 231 } 232 233 case *types.Tuple: 234 for i, n := 0, t.Len(); i < n; i++ { 235 prog.needMethods(t.At(i).Type(), false) 236 } 237 238 default: 239 panic(T) 240 } 241 }