mirror of
https://github.com/golang/go
synced 2024-11-23 00:50:05 -07:00
[dev.typeparams] cmd/compile: simplify writer.collectDecls
The previous code for walking the syntax AST to find declarations needed to know whether a declaration appeared within block scope, but syntax.Crawl (née syntax.Walk) made that somewhat awkward. This CL simplifies it a little, taking advantage of syntax.Walk's support for keeping per-subtree state. Change-Id: I03c7da8c44bec40f88e983852dc6bbab7e6ac13c Reviewed-on: https://go-review.googlesource.com/c/go/+/330549 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
ee4fc0c1bc
commit
9bdbf73c98
@ -1386,13 +1386,20 @@ type typeDeclGen struct {
|
||||
gen int
|
||||
}
|
||||
|
||||
func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
var typegen int
|
||||
type fileImports struct {
|
||||
importedEmbed, importedUnsafe bool
|
||||
}
|
||||
|
||||
for _, p := range noders {
|
||||
var importedEmbed, importedUnsafe bool
|
||||
type declCollector struct {
|
||||
pw *pkgWriter
|
||||
typegen *int
|
||||
file *fileImports
|
||||
withinFunc bool
|
||||
}
|
||||
|
||||
func (c *declCollector) Visit(n syntax.Node) syntax.Visitor {
|
||||
pw := c.pw
|
||||
|
||||
syntax.Crawl(p.file, func(n syntax.Node) bool {
|
||||
switch n := n.(type) {
|
||||
case *syntax.File:
|
||||
pw.checkPragmas(n.Pragma, ir.GoBuildPragma, false)
|
||||
@ -1402,9 +1409,9 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
|
||||
switch pkgNameOf(pw.info, n).Imported().Path() {
|
||||
case "embed":
|
||||
importedEmbed = true
|
||||
c.file.importedEmbed = true
|
||||
case "unsafe":
|
||||
importedUnsafe = true
|
||||
c.file.importedUnsafe = true
|
||||
}
|
||||
|
||||
case *syntax.ConstDecl:
|
||||
@ -1427,8 +1434,8 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
|
||||
// Assign a unique ID to function-scoped defined types.
|
||||
if !isGlobal(obj) {
|
||||
typegen++
|
||||
d.gen = typegen
|
||||
*c.typegen++
|
||||
d.gen = *c.typegen
|
||||
}
|
||||
}
|
||||
|
||||
@ -1438,11 +1445,7 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
pw.checkPragmas(n.Pragma, 0, true)
|
||||
|
||||
if p, ok := n.Pragma.(*pragmas); ok && len(p.Embeds) > 0 {
|
||||
obj := pw.info.Defs[n.NameList[0]].(*types2.Var)
|
||||
// TODO(mdempsky): isGlobal(obj) gives false positive errors
|
||||
// for //go:embed directives on package-scope blank
|
||||
// variables.
|
||||
if err := checkEmbed(n, importedEmbed, !isGlobal(obj)); err != nil {
|
||||
if err := checkEmbed(n, c.file.importedEmbed, c.withinFunc); err != nil {
|
||||
pw.errorf(p.Embeds[0].Pos, "%s", err)
|
||||
}
|
||||
}
|
||||
@ -1461,14 +1464,33 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
pw.dups.add(obj.Type(), tv.Type)
|
||||
}
|
||||
}
|
||||
|
||||
case *syntax.BlockStmt:
|
||||
if !c.withinFunc {
|
||||
copy := *c
|
||||
copy.withinFunc = true
|
||||
return ©
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (pw *pkgWriter) collectDecls(noders []*noder) {
|
||||
var typegen int
|
||||
for _, p := range noders {
|
||||
var file fileImports
|
||||
|
||||
syntax.Walk(p.file, &declCollector{
|
||||
pw: pw,
|
||||
typegen: &typegen,
|
||||
file: &file,
|
||||
})
|
||||
|
||||
pw.cgoPragmas = append(pw.cgoPragmas, p.pragcgobuf...)
|
||||
|
||||
for _, l := range p.linknames {
|
||||
if !importedUnsafe {
|
||||
if !file.importedUnsafe {
|
||||
pw.errorf(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
|
||||
continue
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user