-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
97 lines (87 loc) · 2.54 KB
/
Copy pathmodule.go
File metadata and controls
97 lines (87 loc) · 2.54 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package analysis
import (
"fmt"
"path/filepath"
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/module/imports"
"github.com/ProCode-Software/klar/internal/target"
"github.com/ProCode-Software/klar/internal/version"
)
// A Module describes a Klar module.
type Module struct {
Name, Path string // Base name, dir/file path
Programs map[string]*ast.Program
fileID map[FileID]string // File ID to key in Programs
fileContext map[FileID]*Context // File ID to fctx
ImportPath imports.ImportPath
Imports []*Module
Targets []target.Target // Targets the module was compiled for
KlarVersion version.Version // Minimum required Klar version
Context *Context // Root non-builtin context
Flags Flag
Info *Info
}
// NewModule returns a new Module. The module is not complete.
func NewModule(
name, path string,
importPath imports.ImportPath,
programs map[string]*ast.Program,
klarVersion version.Version,
targets []target.Target,
) *Module {
ctx := NewContext(BuiltInContext, 0)
return &Module{
Name: name,
Path: path,
ImportPath: importPath,
Programs: programs,
Targets: targets,
KlarVersion: klarVersion,
Context: ctx,
}
}
// ImportPathString returns m's import path as a dot-separated path.
func (m *Module) ImportPathString() string {
return m.ImportPath.String()
}
// JoinFilePath joins the module's directory location with the given basename.
// If m is a single-file module, it returns the file's path. JoinFilePath does not
// validate that basename is a valid file in the module.
func (m *Module) JoinFilePath(basename string) string {
if m.Flags.Has(SingleFileModule) {
return m.Path
} else {
return filepath.Join(m.Path, basename)
}
}
// ResolveFile returns the base name of the file represented by id.
// It panics if the file is not found.
func (m *Module) ResolveFile(id FileID) string {
if id == 0 {
return "top-level"
}
path, ok := m.fileID[id]
if !ok {
panic(fmt.Sprintf("file with id %d not found", id))
}
return path
}
// ResolveFilePath returns the full path of the file represented by id,
// or an empty string if not found.
func (m *Module) ResolveFilePath(id FileID) string {
if file := m.fileID[id]; file != "" {
return m.JoinFilePath(file)
}
return ""
}
func (m *Module) String() string {
return fmt.Sprintf("module %s (%s)", m.ImportPathString(), m.Path)
}
func (m *Module) HasExports() bool {
for _, obj := range m.Context.Declarations {
if obj.Public {
return true
}
}
return false
}