From e48919bcde90d283e01e903eae8f92da4a3c1103 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 11 Jan 2017 15:20:38 -0800 Subject: [PATCH] [dev.inline] cmd/compile: split mkpackage into separate functions Previously, mkpackage jumbled together three unrelated tasks: handling package declarations, clearing imports from processing previous source files, and assigning a default value to outfile. Change-Id: I1e124335768aeabfd1a6d9cc2499fbb980d951cf Reviewed-on: https://go-review.googlesource.com/35124 Reviewed-by: Robert Griesemer Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/go.go | 2 - src/cmd/compile/internal/gc/main.go | 104 ++++++++++++++------------- src/cmd/compile/internal/gc/noder.go | 2 + 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/cmd/compile/internal/gc/go.go b/src/cmd/compile/internal/gc/go.go index 3d4b511109b..26b11a27b6f 100644 --- a/src/cmd/compile/internal/gc/go.go +++ b/src/cmd/compile/internal/gc/go.go @@ -113,8 +113,6 @@ var sizeof_String int // runtime sizeof(String) var pragcgobuf string -var infile string - var outfile string var linkobj string diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index 3ce9994eb1f..46c02b46e59 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -218,6 +218,26 @@ func Main() { usage() } + if outfile == "" { + p := flag.Arg(0) + if i := strings.LastIndex(p, "/"); i >= 0 { + p = p[i+1:] + } + if runtime.GOOS == "windows" { + if i := strings.LastIndex(p, `\`); i >= 0 { + p = p[i+1:] + } + } + if i := strings.LastIndex(p, "."); i >= 0 { + p = p[:i] + } + suffix := ".o" + if writearchive { + suffix = ".a" + } + outfile = p + suffix + } + startProfile() if flag_race { @@ -306,7 +326,7 @@ func Main() { timings.Start("fe", "parse") var lines uint - for _, infile = range flag.Args() { + for _, infile := range flag.Args() { block = 1 iota_ = -1000000 imported_unsafe = false @@ -319,7 +339,6 @@ func Main() { timings.AddEvent(int64(lines), "lines") testdclstack() - mkpackage(localpkg.Name) // final import not used checks finishUniverse() typecheckok = true @@ -900,54 +919,37 @@ func mkpackage(pkgname string) { if pkgname != localpkg.Name { yyerror("package %s; expected %s", pkgname, localpkg.Name) } - for _, s := range localpkg.Syms { - if s.Def == nil { - continue - } - if s.Def.Op == OPACK { - // throw away top-level package name leftover - // from previous file. - // leave s->block set to cause redeclaration - // errors if a conflicting top-level name is - // introduced by a different file. - if !s.Def.Used && nsyntaxerrors == 0 { - pkgnotused(s.Def.Pos, s.Def.Name.Pkg.Path, s.Name) - } - s.Def = nil - continue - } - - if s.Def.Sym != s && s.Flags&SymAlias == 0 { - // throw away top-level name left over - // from previous import . "x" - if s.Def.Name != nil && s.Def.Name.Pack != nil && !s.Def.Name.Pack.Used && nsyntaxerrors == 0 { - pkgnotused(s.Def.Name.Pack.Pos, s.Def.Name.Pack.Name.Pkg.Path, "") - s.Def.Name.Pack.Used = true - } - - s.Def = nil - continue - } - } - } - - if outfile == "" { - p := infile - if i := strings.LastIndex(p, "/"); i >= 0 { - p = p[i+1:] - } - if runtime.GOOS == "windows" { - if i := strings.LastIndex(p, `\`); i >= 0 { - p = p[i+1:] - } - } - if i := strings.LastIndex(p, "."); i >= 0 { - p = p[:i] - } - suffix := ".o" - if writearchive { - suffix = ".a" - } - outfile = p + suffix + } +} + +func clearImports() { + for _, s := range localpkg.Syms { + if s.Def == nil { + continue + } + if s.Def.Op == OPACK { + // throw away top-level package name leftover + // from previous file. + // leave s->block set to cause redeclaration + // errors if a conflicting top-level name is + // introduced by a different file. + if !s.Def.Used && nsyntaxerrors == 0 { + pkgnotused(s.Def.Pos, s.Def.Name.Pkg.Path, s.Name) + } + s.Def = nil + continue + } + + if s.Def.Sym != s && s.Flags&SymAlias == 0 { + // throw away top-level name left over + // from previous import . "x" + if s.Def.Name != nil && s.Def.Name.Pack != nil && !s.Def.Name.Pack.Used && nsyntaxerrors == 0 { + pkgnotused(s.Def.Name.Pack.Pos, s.Def.Name.Pack.Name.Pkg.Path, "") + s.Def.Name.Pack.Used = true + } + + s.Def = nil + continue + } } } diff --git a/src/cmd/compile/internal/gc/noder.go b/src/cmd/compile/internal/gc/noder.go index d7e3023102a..27edffea339 100644 --- a/src/cmd/compile/internal/gc/noder.go +++ b/src/cmd/compile/internal/gc/noder.go @@ -70,6 +70,8 @@ func (p *noder) file(file *syntax.File) { // TODO(gri) fix this once we switched permanently to the new // position information. lineno = MakePos(file.Pos().Base(), uint(file.Lines), 0) + + clearImports() } func (p *noder) decls(decls []syntax.Decl) (l []*Node) {