Commit b873512b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent c92cd912
...@@ -91,6 +91,21 @@ func (v byPkgPath) Less(i, j int) bool { return v[i].PkgPath < v[j].PkgPath } ...@@ -91,6 +91,21 @@ func (v byPkgPath) Less(i, j int) bool { return v[i].PkgPath < v[j].PkgPath }
func (v byPkgPath) Swap(i, j int) { v[i], v[j] = v[j], v[i] } func (v byPkgPath) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v byPkgPath) Len() int { return len(v) } func (v byPkgPath) Len() int { return len(v) }
// progImporter is types.Importer that imports packages from loaded loader.Program
type progImporter {
prog *loader.Program
}
func (pi *progImporter) Import(path string) (*types.Package, error) {
pkgi := pi.prog.Package(path)
if pkgi == nil {
return nil, fmt.Errorf("package %q not found", path)
}
return pkgi.Pkg, nil
}
// parseTraceEvent parses trace event definition into traceEvent // parseTraceEvent parses trace event definition into traceEvent
// text is text argument after "//trace:event " // text is text argument after "//trace:event "
func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error) { func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error) {
...@@ -98,17 +113,17 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error) ...@@ -98,17 +113,17 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error)
return nil, fmt.Errorf("trace event must start with \"trace\"") // XXX pos return nil, fmt.Errorf("trace event must start with \"trace\"") // XXX pos
} }
// trace event definition as func declaration // prepare artificial package with trace event definition as func declaration
buf := &Buffer{} // XXX package name must be from trace definition context buf := &Buffer{} // XXX package name must be from trace definition context ?
buf.emit("package xxx") buf.emit("package xxx")
// add // add
// 1. all imports from original source file TODO // 1. all imports from original source file
// 2. add dot-import of original package TODO // 2. dot-import of original package
// so inside it all looks like as if it was in original source context // so that inside it all looks like as if it was in original source context
buf.emit("\nimport (") buf.emit("\nimport (")
for _, imp := range file.Imports { for _, imp := range srcfile.Imports {
impline := "" impline := ""
if imp.Name != nil { if imp.Name != nil {
impline += imp.Name.Name + " " impline += imp.Name.Name + " "
...@@ -120,7 +135,9 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error) ...@@ -120,7 +135,9 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error)
buf.emit("\t. %q", pkgi.Pkg.Path) buf.emit("\t. %q", pkgi.Pkg.Path)
buf.emit(")") buf.emit(")")
text = "\nfunc " + text // func itself
buf.emit("\nfunc " + text)
// XXX add all imports from file of trace event definition context // XXX add all imports from file of trace event definition context
fset := token.NewFileSet() // XXX fset := token.NewFileSet() // XXX
filename := "tracefunc.go" // XXX -> original_file.go:<lineno> ? filename := "tracefunc.go" // XXX -> original_file.go:<lineno> ?
...@@ -142,6 +159,10 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error) ...@@ -142,6 +159,10 @@ func parseTraceEvent(pkgi *loader.PackageInfo, text string) (*traceEvent, error)
return nil, fmt.Errorf("trace event must not return results") return nil, fmt.Errorf("trace event must not return results")
} }
// typecheck prepared package to get trace func argument types
Importer: &progImporter{lprog}
// XXX +pos // XXX +pos
return &traceEvent{pkgi, declf}, nil return &traceEvent{pkgi, declf}, nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment