func()

in tools/go-agent/instrument/plugins/rewrite/func.go [220:267]


func (c *Context) rewriteVarIfExistingMapping(exp, parent dst.Expr) bool {
	switch n := exp.(type) {
	case *dst.Ident:
		if v := c.rewriteMapping.findVarMappingName(n.Name); v != "" {
			n.Name = v
			return true
		}
	case *dst.SelectorExpr:
		if pkg, ok := n.X.(*dst.Ident); ok {
			if imp := c.packageImport[pkg.Name]; imp != nil {
				tools.RemovePackageRef(parent, n, -1)
				return true
			}
		}
		return c.rewriteVarIfExistingMapping(n.X, n)
	case *dst.CompositeLit:
		c.enhanceTypeNameWhenRewrite(n.Type, n, -1)
		for _, elt := range n.Elts {
			// for struct data, ex: "&xxx{k: v}"
			if kv, ok := elt.(*dst.KeyValueExpr); ok {
				c.rewriteVarIfExistingMapping(kv.Value, elt)
			}
		}
	case *dst.UnaryExpr:
		c.enhanceTypeNameWhenRewrite(n.X, n, -1)
	case *dst.IndexExpr:
		c.rewriteVarIfExistingMapping(n.Index, n)
		c.rewriteVarIfExistingMapping(n.X, n)
	case *dst.CallExpr:
		c.enhanceTypeNameWhenRewrite(n.Fun, n, -1)
		for _, arg := range n.Args {
			c.rewriteVarIfExistingMapping(arg, n)
		}
	case *dst.StarExpr:
		c.enhanceTypeNameWhenRewrite(n.X, n, -1)
	case *dst.FuncLit:
		c.rewriteMapping.pushBlockStack()
		c.enhanceFuncParameter(n.Type.Params)
		c.enhanceFuncParameter(n.Type.Results)
		if n.Body != nil && len(n.Body.List) > 0 {
			for _, stmt := range n.Body.List {
				c.enhanceFuncStmt(stmt)
			}
		}
		c.rewriteMapping.popBlockStack()
	}
	return false
}