Index: llgo/trunk/CMakeLists.txt =================================================================== --- llgo/trunk/CMakeLists.txt +++ llgo/trunk/CMakeLists.txt @@ -11,6 +11,7 @@ build/context.go cmd/gllgo/gllgo.go debug/debug.go + driver/parser.go irgen/annotations.go irgen/attribute.go irgen/builtins.go @@ -23,7 +24,6 @@ irgen/indirect.go irgen/interfaces.go irgen/maps.go - irgen/parser.go irgen/predicates.go irgen/println.go irgen/runtime.go Index: llgo/trunk/cmd/gllgo/gllgo.go =================================================================== --- llgo/trunk/cmd/gllgo/gllgo.go +++ llgo/trunk/cmd/gllgo/gllgo.go @@ -23,6 +23,7 @@ "errors" "fmt" "go/scanner" + "go/token" "io/ioutil" "log" "os" @@ -31,6 +32,7 @@ "strings" "llvm.org/llgo/debug" + "llvm.org/llgo/driver" "llvm.org/llgo/irgen" "llvm.org/llvm/bindings/go/llvm" ) @@ -580,7 +582,13 @@ return err } - module, err := compiler.Compile(inputs, opts.pkgpath) + fset := token.NewFileSet() + files, err := driver.ParseFiles(fset, inputs) + if err != nil { + return err + } + + module, err := compiler.Compile(fset, files, opts.pkgpath) if err != nil { return err } Index: llgo/trunk/driver/parser.go =================================================================== --- llgo/trunk/driver/parser.go +++ llgo/trunk/driver/parser.go @@ -0,0 +1,43 @@ +//===- parser.go - parser wrapper -----------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains functions for calling the parser in an appropriate way for +// llgo. +// +//===----------------------------------------------------------------------===// + +package driver + +import ( + "fmt" + "go/ast" + "go/parser" + "go/scanner" + "go/token" +) + +func parseFile(fset *token.FileSet, filename string) (*ast.File, error) { + // Retain comments; this is important for annotation processing. + mode := parser.DeclarationErrors | parser.ParseComments + return parser.ParseFile(fset, filename, nil, mode) +} + +func ParseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) { + files := make([]*ast.File, len(filenames)) + for i, filename := range filenames { + file, err := parseFile(fset, filename) + if _, ok := err.(scanner.ErrorList); ok { + return nil, err + } else if err != nil { + return nil, fmt.Errorf("%q: %v", filename, err) + } + files[i] = file + } + return files, nil +} Index: llgo/trunk/irgen/compiler.go =================================================================== --- llgo/trunk/irgen/compiler.go +++ llgo/trunk/irgen/compiler.go @@ -16,6 +16,7 @@ import ( "bytes" "fmt" + "go/ast" "go/token" "log" "sort" @@ -102,7 +103,7 @@ return compiler, nil } -func (c *Compiler) Compile(filenames []string, importpath string) (m *Module, err error) { +func (c *Compiler) Compile(fset *token.FileSet, astFiles []*ast.File, importpath string) (m *Module, err error) { target := llvm.NewTargetData(c.dataLayout) compiler := &compiler{ CompilerOptions: c.opts, @@ -111,7 +112,7 @@ pnacl: c.pnacl, llvmtypes: NewLLVMTypeMap(llvm.GlobalContext(), target), } - return compiler.compile(filenames, importpath) + return compiler.compile(fset, astFiles, importpath) } type compiler struct { @@ -149,7 +150,7 @@ } } -func (compiler *compiler) compile(filenames []string, importpath string) (m *Module, err error) { +func (compiler *compiler) compile(fset *token.FileSet, astFiles []*ast.File, importpath string) (m *Module, err error) { buildctx, err := llgobuild.ContextFromTriple(compiler.TargetTriple) if err != nil { return nil, err @@ -170,19 +171,13 @@ } impcfg := &loader.Config{ - Fset: token.NewFileSet(), + Fset: fset, TypeChecker: types.Config{ Import: importer, Sizes: compiler.llvmtypes, }, Build: &buildctx.Context, } - // Must use parseFiles, so we retain comments; - // this is important for annotation processing. - astFiles, err := parseFiles(impcfg.Fset, filenames) - if err != nil { - return nil, err - } // If no import path is specified, then set the import // path to be the same as the package's name. if importpath == "" { Index: llgo/trunk/irgen/parser.go =================================================================== --- llgo/trunk/irgen/parser.go +++ llgo/trunk/irgen/parser.go @@ -1,42 +0,0 @@ -//===- parser.go - parser wrapper -----------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains functions for calling the parser in an appropriate way for -// llgo. -// -//===----------------------------------------------------------------------===// - -package irgen - -import ( - "fmt" - "go/ast" - "go/parser" - "go/scanner" - "go/token" -) - -func parseFile(fset *token.FileSet, filename string) (*ast.File, error) { - mode := parser.DeclarationErrors | parser.ParseComments - return parser.ParseFile(fset, filename, nil, mode) -} - -func parseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) { - files := make([]*ast.File, len(filenames)) - for i, filename := range filenames { - file, err := parseFile(fset, filename) - if _, ok := err.(scanner.ErrorList); ok { - return nil, err - } else if err != nil { - return nil, fmt.Errorf("%q: %v", filename, err) - } - files[i] = file - } - return files, nil -}