[dev.typeparams] cmd/compile: add types2-based noder

This CL adds "irgen", a new noding implementation that utilizes types2
to guide IR construction. Notably, it completely skips dealing with
constant and type expressions (aside from using ir.TypeNode to
interoperate with the types1 typechecker), because types2 already
handled those. It also omits any syntax checking, trusting that types2
already rejected any errors.

It currently still utilizes the types1 typechecker for the desugaring
operations it handles (e.g., turning OAS2 into OAS2FUNC/etc, inserting
implicit conversions, rewriting f(g()) functions, and so on). However,
the IR is constructed in a fully incremental fashion, so it should be
easy to now piecemeal replace those dependencies as needed.

Nearly all of "go test std cmd" passes with -G=3 enabled by
default. The main remaining blocker is the number of test/run.go
failures. There also appear to be cases where types2 does not provide
us with position information. These will be iterated upon.

Portions and ideas from Dan Scales's CL 276653.

Change-Id: Ic99e8f2d0267b0312d30c10d5d043f5817a59c9d
Reviewed-on: https://go-review.googlesource.com/c/go/+/281932
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2021-01-09 00:57:55 -08:00
parent f065ff221b
commit ef5285fbd0
16 changed files with 1688 additions and 161 deletions

View file

@ -0,0 +1,113 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package noder
import (
"go/constant"
"cmd/compile/internal/base"
"cmd/compile/internal/syntax"
"cmd/compile/internal/types"
"cmd/compile/internal/types2"
)
// match reports whether types t1 and t2 are consistent
// representations for a given expression's type.
func (g *irgen) match(t1 *types.Type, t2 types2.Type, hasOK bool) bool {
tuple, ok := t2.(*types2.Tuple)
if !ok {
// Not a tuple; can use simple type identity comparison.
return types.Identical(t1, g.typ(t2))
}
if hasOK {
// For has-ok values, types2 represents the expression's type as
// a 2-element tuple, whereas ir just uses the first type and
// infers that the second type is boolean.
return tuple.Len() == 2 && types.Identical(t1, g.typ(tuple.At(0).Type()))
}
if t1 == nil || tuple == nil {
return t1 == nil && tuple == nil
}
if !t1.IsFuncArgStruct() {
return false
}
if t1.NumFields() != tuple.Len() {
return false
}
for i, result := range t1.FieldSlice() {
if !types.Identical(result.Type, g.typ(tuple.At(i).Type())) {
return false
}
}
return true
}
func (g *irgen) validate(n syntax.Node) {
switch n := n.(type) {
case *syntax.CallExpr:
tv := g.info.Types[n.Fun]
if tv.IsBuiltin() {
switch builtin := n.Fun.(type) {
case *syntax.Name:
g.validateBuiltin(builtin.Value, n)
case *syntax.SelectorExpr:
g.validateBuiltin(builtin.Sel.Value, n)
default:
g.unhandled("builtin", n)
}
}
}
}
func (g *irgen) validateBuiltin(name string, call *syntax.CallExpr) {
switch name {
case "Alignof", "Offsetof", "Sizeof":
// Check that types2+gcSizes calculates sizes the same
// as cmd/compile does.
got, ok := constant.Int64Val(g.info.Types[call].Value)
if !ok {
base.FatalfAt(g.pos(call), "expected int64 constant value")
}
want := g.unsafeExpr(name, call.ArgList[0])
if got != want {
base.FatalfAt(g.pos(call), "got %v from types2, but want %v", got, want)
}
}
}
// unsafeExpr evaluates the given unsafe builtin function on arg.
func (g *irgen) unsafeExpr(name string, arg syntax.Expr) int64 {
switch name {
case "Alignof":
return g.typ(g.info.Types[arg].Type).Alignment()
case "Sizeof":
return g.typ(g.info.Types[arg].Type).Size()
}
// Offsetof
sel := arg.(*syntax.SelectorExpr)
selection := g.info.Selections[sel]
typ := g.typ(g.info.Types[sel.X].Type)
if typ.IsPtr() {
typ = typ.Elem()
}
var offset int64
for _, i := range selection.Index() {
// Ensure field offsets have been calculated.
types.CalcSize(typ)
f := typ.Field(i)
offset += f.Offset
typ = f.Type
}
return offset
}