-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinterface.go
More file actions
158 lines (137 loc) · 4.26 KB
/
interface.go
File metadata and controls
158 lines (137 loc) · 4.26 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package goose
import (
"fmt"
"go/ast"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/goose-lang/goose/declfilter"
"github.com/goose-lang/goose/glang"
"github.com/goose-lang/goose/util"
"golang.org/x/tools/go/packages"
)
type errorCatcher struct {
errs []error
}
func (e *errorCatcher) do(f func()) {
defer func() {
if r := recover(); r != nil {
if gooseErr, ok := r.(gooseError); ok {
e.errs = append(e.errs, gooseErr.err)
} else {
// r is an error from a non-goose error, indicating a bug
panic(r)
}
}
}()
f()
}
// Decls converts an entire package (possibly multiple files) to a list of decls
func (ctx *Ctx) files(fs []*ast.File) (preDecls []glang.Decl, sortedDecls []glang.Decl, errs []error) {
var e errorCatcher
for _, f := range fs {
for _, d := range f.Decls {
e.do(func() { ctx.decl(d) })
}
}
e.do(func() { ctx.finalExtraDecls() })
return ctx.out.preHeaderDecls(), ctx.out.decls(), e.errs
}
type MultipleErrors []error
func (es MultipleErrors) Error() string {
var errs []string
for _, e := range es {
errs = append(errs, e.Error())
}
errs = append(errs, fmt.Sprintf("%d errors", len(es)))
return strings.Join(errs, "\n\n")
}
func pkgErrors(errors []packages.Error) error {
var errs []error
for _, err := range errors {
errs = append(errs, err)
}
return MultipleErrors(errs)
}
// translatePackage translates an entire package to a single Coq file.
//
// If the source directory has multiple source files, these are processed in
// alphabetical order; this must be a topological sort of the definitions or the
// Coq code will be out-of-order. Sorting ensures the results are stable
// and not dependent on map or directory iteration order.
func translatePackage(pkg *packages.Package, config declfilter.FilterConfig) (glang.File, error) {
if len(pkg.Errors) > 0 {
return glang.File{}, errors.Errorf(
"could not load package %v:\n%v", pkg.PkgPath,
pkgErrors(pkg.Errors))
}
ctx := NewPkgCtx(pkg, declfilter.New(config))
coqFile := ctx.initCoqFile(pkg, config)
preDecls, decls, errs := ctx.files(pkg.Syntax)
coqFile.PreHeaderDecls = preDecls
coqFile.Decls = decls
if len(errs) != 0 {
return coqFile, errors.Wrap(MultipleErrors(errs),
"conversion failed")
}
return coqFile, nil
}
func (ctx *Ctx) initCoqFile(pkg *packages.Package, config declfilter.FilterConfig) (f glang.File) {
f.PkgPath = pkg.PkgPath
if config.Bootstrap.Enabled {
f.Header = glang.BootstrapHeader + "\n" + strings.Join(config.Bootstrap.Prelude, "\n") + "\n"
} else {
f.Header = glang.DefaultHeader + "\n"
}
if ctx.filter.HasTrusted() {
importPath := strings.ReplaceAll(glang.ThisIsBadAndShouldBeDeprecatedGoPathToCoqPath(pkg.PkgPath), "/", ".")
f.Header += fmt.Sprintf("Require Export New.trusted_code.%s.\n", importPath) +
fmt.Sprintf("Import %s.\n", pkg.Name)
}
ffi := util.GetFfi(pkg)
if ffi != "" {
f.Header += fmt.Sprintf("From New Require Import %s_prelude.\n", ffi)
}
f.Header += "Module pkg_id.\n"
f.Header += fmt.Sprintf("Definition %s : go_string := \"%s\".\n\n", pkg.Name, pkg.PkgPath)
f.Header += "End pkg_id.\nExport pkg_id.\n"
f.Header += fmt.Sprintf("Module %s.", pkg.Name)
f.Footer = fmt.Sprintf("End %s.\n", pkg.Name)
return
}
// TranslatePackages loads packages by a list of patterns and translates them
// all, producing one file per matched package.
//
// The errs list contains errors corresponding to each package (in parallel with
// the files list). patternErr is only non-nil if the patterns themselves have
// a syntax error.
func TranslatePackages(configDir string, modDir string,
pkgPattern ...string) (files []glang.File, errs []error, patternErr error) {
pkgs, patternErr := packages.Load(util.NewPackageConfig(modDir, true), pkgPattern...)
if patternErr != nil {
return
}
if len(pkgs) == 0 {
// consider matching nothing to be an error, unlike packages.Load
return nil, nil,
errors.New("patterns matched no packages")
}
files = make([]glang.File, len(pkgs))
errs = make([]error, len(pkgs))
var wg sync.WaitGroup
wg.Add(len(pkgs))
// TODO now
for i, pkg := range pkgs {
go func() {
defer wg.Done()
config, err := util.ReadConfig(configDir, pkg.PkgPath)
if err != nil {
errs[i] = err
return
}
files[i], errs[i] = translatePackage(pkg, config)
}()
}
wg.Wait()
return
}