go/src/cmd/compile/internal/noder/validate.go

116 lines
2.8 KiB
Go
Raw Normal View History

// 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. Must match either, since we
// sometimes delay the transformation to the ir form.
if tuple.Len() == 2 && types.Identical(t1, g.typ(tuple.At(0).Type())) {
return true
}
return types.Identical(t1, g.typ(t2))
}
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)
cmd/compile: get instantiated generic types working with interfaces Get instantiatiated generic types working with interfaces, including typechecking assignments to interfaces and instantiating all the methods properly. To get it all working, this change includes: - Add support for substituting in interfaces in subster.typ() - Fill in the info for the methods for all instantiated generic types, so those methods will be available for later typechecking (by the old typechecker) when assigning an instantiated generic type to an interface. We also want those methods available so we have the list when we want to instantiate all methods of an instantiated type. We have both for instantiated types encountered during the initial noder phase, and for instantiated types created during stenciling of a function/method. - When we first create a fully-instantiated generic type (whether during initial noder2 pass or while instantiating a method/function), add it to a list so that all of its methods will also be instantiated. This is needed so that an instantiated type can be assigned to an interface. - Properly substitute type names in the names of instantiated methods. - New accessor methods for types.Type.RParam. - To deal with generic types which are empty structs (or just don't use their type params anywhere), we want to set HasTParam if a named type has any type params that are not fully instantiated, even if the type param is not used in the type. - In subst.typ() and elsewhere, always set sym.Def for a new forwarding type we are creating, so we always create a single unique type for each generic type instantiation. This handles recursion within a type, and also recursive relationships across many types or methods. We remove the seen[] hashtable, which was serving the same purpose, but for subst.typ() only. We now handle all kinds of recursive types. - We don't seem to need to force types.CheckSize() on created/substituted generic types anymore, so commented out for now. - Add an RParams accessor to types2.Signature, and also a new exported types2.AsSignature() function. Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632 Reviewed-on: https://go-review.googlesource.com/c/go/+/298449 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dan Scales <danscales@google.com> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
typ = deref(typ)
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
}