package codegen
import (
"git.sr.ht/~mna/snow/pkg/semantic"
)
type _import struct {
Path string
Pkg string
Symbol string
AppliedTo *semantic.Fn
}
func imports(unit *semantic.Unit) map[*semantic.File][]_import {
imps := make(map[*semantic.File][]_import)
var curFile *semantic.File
semantic.Inspect(unit, func(n semantic.Node) bool {
switch n := n.(type) {
case *semantic.File:
curFile = n
case *semantic.Fn:
for _, attr := range n.Attrs {
// only care about extern attributes
if attr.InitOf == nil || attr.InitOf.Ident() != semantic.ExternAttrName {
continue
}
vals := attr.Values()
imp := _import{
Path: vals["import"].(string),
Symbol: vals["symbol"].(string),
AppliedTo: n,
}
if pkg, _ := vals["pkg"].(string); pkg != "" {
imp.Pkg = pkg
}
imps[curFile] = append(imps[curFile], imp)
}
case semantic.Expr:
return false
}
return true
})
return imps
}