From b13cfc525531d30a42eadd30a908210bbe92047a Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 30 Aug 2018 22:47:34 +0200 Subject: [PATCH] Be able to deal with anonymous functions --- ir.go | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/ir.go b/ir.go index d2e5c813..f414ad37 100644 --- a/ir.go +++ b/ir.go @@ -119,28 +119,10 @@ func (p *Program) addFunction(ssaFn *ssa.Function) { f := &Function{fn: ssaFn} p.Functions = append(p.Functions, f) p.functionMap[ssaFn] = f + f.parsePragmas() - // Parse compiler directives in the preceding comments. - if f.fn.Syntax() != nil && f.fn.Syntax().(*ast.FuncDecl).Doc != nil { - for _, comment := range f.fn.Syntax().(*ast.FuncDecl).Doc.List { - if !strings.HasPrefix(comment.Text, "//go:") { - continue - } - parts := strings.Fields(comment.Text) - switch parts[0] { - case "//go:linkname": - if len(parts) != 3 || parts[1] != ssaFn.Name() { - continue - } - // Only enable go:linkname when the package imports "unsafe". - // This is a slightly looser requirement than what gc uses: gc - // requires the file to import "unsafe", not the package as a - // whole. - if hasUnsafeImport(ssaFn.Pkg.Pkg) { - f.linkName = parts[2] - } - } - } + for _, anon := range ssaFn.AnonFuncs { + p.addFunction(anon) } } @@ -162,6 +144,34 @@ func (p *Program) GetGlobal(ssaGlobal *ssa.Global) *Global { return p.globalMap[ssaGlobal] } +// Parse compiler directives in the preceding comments. +func (f *Function) parsePragmas() { + if f.fn.Syntax() == nil { + return + } + if decl, ok := f.fn.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil { + for _, comment := range decl.Doc.List { + if !strings.HasPrefix(comment.Text, "//go:") { + continue + } + parts := strings.Fields(comment.Text) + switch parts[0] { + case "//go:linkname": + if len(parts) != 3 || parts[1] != f.fn.Name() { + continue + } + // Only enable go:linkname when the package imports "unsafe". + // This is a slightly looser requirement than what gc uses: gc + // requires the file to import "unsafe", not the package as a + // whole. + if hasUnsafeImport(f.fn.Pkg.Pkg) { + f.linkName = parts[2] + } + } + } + } +} + // Return the link name for this function. func (f *Function) LinkName() string { if f.linkName != "" {