mirror of
https://github.com/golang/go
synced 2024-11-23 11:20:05 -07:00
cmd/compile/internal/types: remove Markdcl/Pushdcl/Popdcl
Sym.Def used to be used for symbol resolution during the old (pre-types2) typechecker. But since moving to types2-based IR construction, we haven't really had a need for Sym.Def to ever refer to anything but the package-scope definition, because types2 handles symbol resolution for us. This CL finally removes the Markdcl/Pushdcl/Popdcl functions that have been a recurring source of issues in the past. Change-Id: I2b012a0f17203efdd724ebd1e9314bd128cc2d61 Reviewed-on: https://go-review.googlesource.com/c/go/+/458625 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
f0b1563535
commit
13c70b12d7
@ -19,7 +19,6 @@ type DebugFlags struct {
|
||||
Append int `help:"print information about append compilation"`
|
||||
Checkptr int `help:"instrument unsafe pointer conversions\n0: instrumentation disabled\n1: conversions involving unsafe.Pointer are instrumented\n2: conversions to unsafe.Pointer force heap allocation" concurrent:"ok"`
|
||||
Closure int `help:"print information about closure compilation"`
|
||||
DclStack int `help:"run internal dclstack check"`
|
||||
Defer int `help:"print information about defer compilation"`
|
||||
DisableNil int `help:"disable nil checks" concurrent:"ok"`
|
||||
DumpPtrs int `help:"show Node pointers values in dump output"`
|
||||
|
@ -224,10 +224,6 @@ func genhash(t *types.Type) *obj.LSym {
|
||||
typecheck.Stmts(fn.Body)
|
||||
ir.CurFunc = nil
|
||||
|
||||
if base.Debug.DclStack != 0 {
|
||||
types.CheckDclstack()
|
||||
}
|
||||
|
||||
fn.SetNilCheckDisabled(true)
|
||||
typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
|
||||
|
||||
@ -552,10 +548,6 @@ func geneq(t *types.Type) *obj.LSym {
|
||||
typecheck.Stmts(fn.Body)
|
||||
ir.CurFunc = nil
|
||||
|
||||
if base.Debug.DclStack != 0 {
|
||||
types.CheckDclstack()
|
||||
}
|
||||
|
||||
// Disable checknils while compiling this code.
|
||||
// We are comparing a struct or an array,
|
||||
// neither of which can be nil, and our comparisons
|
||||
|
@ -324,9 +324,6 @@ func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) {
|
||||
fn.Body.Append(tail)
|
||||
|
||||
typecheck.FinishFuncBody()
|
||||
if base.Debug.DclStack != 0 {
|
||||
types.CheckDclstack()
|
||||
}
|
||||
|
||||
typecheck.Func(fn)
|
||||
ir.CurFunc = fn
|
||||
|
@ -59,6 +59,7 @@ func Declare(n *ir.Name, ctxt ir.Class) {
|
||||
base.ErrorfAt(n.Pos(), "cannot declare main - must be func")
|
||||
}
|
||||
Target.Externs = append(Target.Externs, n)
|
||||
s.Def = n
|
||||
} else {
|
||||
if ir.CurFunc == nil && ctxt == ir.PAUTO {
|
||||
base.Pos = n.Pos()
|
||||
@ -67,7 +68,6 @@ func Declare(n *ir.Name, ctxt ir.Class) {
|
||||
if ir.CurFunc != nil && ctxt != ir.PFUNC && n.Op() == ir.ONAME {
|
||||
ir.CurFunc.Dcl = append(ir.CurFunc.Dcl, n)
|
||||
}
|
||||
types.Pushdcl(s)
|
||||
n.Curfn = ir.CurFunc
|
||||
}
|
||||
|
||||
@ -75,7 +75,6 @@ func Declare(n *ir.Name, ctxt ir.Class) {
|
||||
n.SetFrameOffset(0)
|
||||
}
|
||||
|
||||
s.Def = n
|
||||
n.Class = ctxt
|
||||
if ctxt == ir.PFUNC {
|
||||
n.Sym().SetFunc(true)
|
||||
@ -107,8 +106,6 @@ func StartFuncBody(fn *ir.Func) {
|
||||
funcStack = append(funcStack, funcStackEnt{ir.CurFunc, DeclContext})
|
||||
ir.CurFunc = fn
|
||||
DeclContext = ir.PAUTO
|
||||
|
||||
types.Markdcl()
|
||||
}
|
||||
|
||||
// finish the body.
|
||||
@ -116,7 +113,6 @@ func StartFuncBody(fn *ir.Func) {
|
||||
// returns in extern-declaration context.
|
||||
func FinishFuncBody() {
|
||||
// change the declaration context from auto to previous context
|
||||
types.Popdcl()
|
||||
var e funcStackEnt
|
||||
funcStack, e = funcStack[:len(funcStack)-1], funcStack[len(funcStack)-1]
|
||||
ir.CurFunc, DeclContext = e.curfn, e.dclcontext
|
||||
|
@ -4,96 +4,8 @@
|
||||
|
||||
package types
|
||||
|
||||
import (
|
||||
"cmd/compile/internal/base"
|
||||
)
|
||||
|
||||
// Declaration stack & operations
|
||||
|
||||
// A dsym stores a symbol's shadowed declaration so that it can be
|
||||
// restored once the block scope ends.
|
||||
type dsym struct {
|
||||
sym *Sym // sym == nil indicates stack mark
|
||||
def Object
|
||||
}
|
||||
|
||||
// dclstack maintains a stack of shadowed symbol declarations so that
|
||||
// Popdcl can restore their declarations when a block scope ends.
|
||||
var dclstack []dsym
|
||||
|
||||
// Pushdcl pushes the current declaration for symbol s (if any) so that
|
||||
// it can be shadowed by a new declaration within a nested block scope.
|
||||
func Pushdcl(s *Sym) {
|
||||
dclstack = append(dclstack, dsym{
|
||||
sym: s,
|
||||
def: s.Def,
|
||||
})
|
||||
}
|
||||
|
||||
// Popdcl pops the innermost block scope and restores all symbol declarations
|
||||
// to their previous state.
|
||||
func Popdcl() {
|
||||
for i := len(dclstack); i > 0; i-- {
|
||||
d := &dclstack[i-1]
|
||||
s := d.sym
|
||||
if s == nil {
|
||||
// pop stack mark
|
||||
dclstack = dclstack[:i-1]
|
||||
return
|
||||
}
|
||||
|
||||
s.Def = d.def
|
||||
|
||||
// Clear dead pointer fields.
|
||||
d.sym = nil
|
||||
d.def = nil
|
||||
}
|
||||
base.Fatalf("popdcl: no stack mark")
|
||||
}
|
||||
|
||||
// Markdcl records the start of a new block scope for declarations.
|
||||
func Markdcl() {
|
||||
dclstack = append(dclstack, dsym{
|
||||
sym: nil, // stack mark
|
||||
})
|
||||
}
|
||||
|
||||
func isDclstackValid() bool {
|
||||
for _, d := range dclstack {
|
||||
if d.sym == nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// PkgDef returns the definition associated with s at package scope.
|
||||
func (s *Sym) PkgDef() Object {
|
||||
return *s.pkgDefPtr()
|
||||
}
|
||||
func (s *Sym) PkgDef() Object { return s.Def }
|
||||
|
||||
// SetPkgDef sets the definition associated with s at package scope.
|
||||
func (s *Sym) SetPkgDef(n Object) {
|
||||
*s.pkgDefPtr() = n
|
||||
}
|
||||
|
||||
func (s *Sym) pkgDefPtr() *Object {
|
||||
// Look for outermost saved declaration, which must be the
|
||||
// package scope definition, if present.
|
||||
for i := range dclstack {
|
||||
d := &dclstack[i]
|
||||
if s == d.sym {
|
||||
return &d.def
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, the declaration hasn't been shadowed within a
|
||||
// function scope.
|
||||
return &s.Def
|
||||
}
|
||||
|
||||
func CheckDclstack() {
|
||||
if !isDclstackValid() {
|
||||
base.Fatalf("mark left on the dclstack")
|
||||
}
|
||||
}
|
||||
func (s *Sym) SetPkgDef(n Object) { s.Def = n }
|
||||
|
@ -35,8 +35,6 @@ type Sym struct {
|
||||
// bound to within the current scope. (Most parts of the compiler should
|
||||
// prefer passing the Node directly, rather than relying on this field.)
|
||||
//
|
||||
// Def is saved and restored by Pushdcl/Popdcl.
|
||||
//
|
||||
// Deprecated: New code should avoid depending on Sym.Def. Add
|
||||
// mdempsky@ as a reviewer for any CLs involving Sym.Def.
|
||||
Def Object
|
||||
|
Loading…
Reference in New Issue
Block a user