in tool/instrument/trampoline.go [585:653]
func (rp *RuleProcessor) rewriteCallContextImpl() {
util.Assert(len(rp.callCtxMethods) > 4, "sanity check")
var (
methodSetParam *dst.FuncDecl
methodGetParam *dst.FuncDecl
methodGetRetVal *dst.FuncDecl
methodSetRetVal *dst.FuncDecl
)
for _, decl := range rp.callCtxMethods {
switch decl.Name.Name {
case TrampolineSetParamName:
methodSetParam = decl
case TrampolineGetParamName:
methodGetParam = decl
case TrampolineGetReturnValName:
methodGetRetVal = decl
case TrampolineSetReturnValName:
methodSetRetVal = decl
}
}
// Rewrite SetParam and GetParam methods
// Dont believe what you see in template.go, we will null out it and rewrite
// the whole switch statement
methodSetParamBody := methodSetParam.Body.List[1].(*dst.SwitchStmt).Body
methodGetParamBody := methodGetParam.Body.List[0].(*dst.SwitchStmt).Body
methodSetRetValBody := methodSetRetVal.Body.List[1].(*dst.SwitchStmt).Body
methodGetRetValBody := methodGetRetVal.Body.List[0].(*dst.SwitchStmt).Body
methodGetParamBody.List = nil
methodSetParamBody.List = nil
methodGetRetValBody.List = nil
methodSetRetValBody.List = nil
idx := 0
if util.HasReceiver(rp.rawFunc) {
recvType := rp.rawFunc.Recv.List[0].Type
clause := setParamClause(idx, recvType)
methodSetParamBody.List = append(methodSetParamBody.List, clause)
clause = getParamClause(idx, recvType)
methodGetParamBody.List = append(methodGetParamBody.List, clause)
idx++
}
for _, param := range rp.rawFunc.Type.Params.List {
paramType := desugarType(param)
for range param.Names {
clause := setParamClause(idx, paramType)
methodSetParamBody.List =
append(methodSetParamBody.List, clause)
clause = getParamClause(idx, paramType)
methodGetParamBody.List =
append(methodGetParamBody.List, clause)
idx++
}
}
// Rewrite GetReturnVal and SetReturnVal methods
if rp.rawFunc.Type.Results != nil {
idx = 0
for _, retval := range rp.rawFunc.Type.Results.List {
retType := desugarType(retval)
for range retval.Names {
clause := getReturnValClause(idx, retType)
methodGetRetValBody.List =
append(methodGetRetValBody.List, clause)
clause = setReturnValClause(idx, retType)
methodSetRetValBody.List =
append(methodSetRetValBody.List, clause)
idx++
}
}
}
}