forked from go-llvm/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.go
54 lines (51 loc) · 1.4 KB
/
annotations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2013 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package llgo
import (
"code.google.com/p/go.tools/go/types"
goimporter "code.google.com/p/go.tools/importer"
"code.google.com/p/go.tools/ssa"
"go/ast"
"go/token"
)
// processAnnotations takes an *ssa.Package and a
// *importer.PackageInfo, and processes all of the
// llgo source annotations attached to each top-level
// function and global variable.
func (c *compiler) processAnnotations(u *unit, pkginfo *goimporter.PackageInfo) {
members := make(map[types.Object]*LLVMValue, len(u.globals))
for k, v := range u.globals {
members[k.(ssa.Member).Object()] = v
}
applyAttributes := func(attrs []Attribute, idents ...*ast.Ident) {
if len(attrs) == 0 {
return
}
for _, ident := range idents {
if v := members[pkginfo.ObjectOf(ident)]; v != nil {
for _, attr := range attrs {
attr.Apply(v)
}
}
}
}
for _, f := range pkginfo.Files {
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
attrs := parseAttributes(decl.Doc)
applyAttributes(attrs, decl.Name)
case *ast.GenDecl:
if decl.Tok != token.VAR {
continue
}
for _, spec := range decl.Specs {
varspec := spec.(*ast.ValueSpec)
attrs := parseAttributes(decl.Doc)
applyAttributes(attrs, varspec.Names...)
}
}
}
}
}