[dev.typeparams] cmd/compile: enable parsing of generic code with new -G flag

Providing the -G flag instructs the compiler to accept type parameters.
For now, the compiler only parses such files and then exits.

Added a new test directory (test/typeparam) and initial test case.

Port from dev.go2go branch.

Change-Id: Ic11e33a9d5f012f8def0bdae205043659562ac73
Reviewed-on: https://go-review.googlesource.com/c/go/+/261660
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2020-10-12 16:19:49 -07:00
parent 7668f02dec
commit 48755e06aa
4 changed files with 77 additions and 5 deletions

View file

@ -24,7 +24,7 @@ import (
// Each declaration in every *syntax.File is converted to a syntax tree
// and its root represented by *Node is appended to xtop.
// Returns the total count of parsed lines.
func parseFiles(filenames []string) uint {
func parseFiles(filenames []string, allowGenerics bool) uint {
noders := make([]*noder, 0, len(filenames))
// Limit the number of simultaneously open files.
sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
@ -49,7 +49,11 @@ func parseFiles(filenames []string) uint {
}
defer f.Close()
p.file, _ = syntax.Parse(base, f, p.error, p.pragma, syntax.CheckBranches) // errors are tracked via p.error
mode := syntax.CheckBranches
if allowGenerics {
mode |= syntax.AllowGenerics
}
p.file, _ = syntax.Parse(base, f, p.error, p.pragma, mode) // errors are tracked via p.error
}(filename)
}
@ -59,7 +63,10 @@ func parseFiles(filenames []string) uint {
p.yyerrorpos(e.Pos, "%s", e.Msg)
}
p.node()
// noder cannot handle generic code yet
if !allowGenerics {
p.node()
}
lines += p.file.EOF.Line()
p.file = nil // release memory