~mna/snow unlisted

snow/pkg/codegen/imports.go -rw-r--r-- 974 bytes
424066c5Martin Angers doc: v0.0.5 1 year, 7 months ago
                                                                                
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
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
}