1
0
mirror of https://github.com/golang/go synced 2024-11-22 16:14:56 -07:00

[dev.typeparams] cmd/compile/internal/types2: review of scopes.go

This file has a few changes compared to the go/types version:
1) syntax.Pos is used instead of token.Pos.
2) The cmpPos helper function (defined elsewhere) is used to
   compare positions (syntax.Pos positions cannot be compared
   directly with <=).
3) A new method Scope.Squash was added (primary difference).

f=scope.go; diff $f ../../../../go/types/$f
7c7
< package types2
---
> package types
11d10
< 	"cmd/compile/internal/syntax"
12a12
> 	"go/token"
26c26
< 	pos, end syntax.Pos        // scope extent; may be invalid
---
> 	pos, end token.Pos         // scope extent; may be invalid
33c33
< func NewScope(parent *Scope, pos, end syntax.Pos, comment string) *Scope {
---
> func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope {
82c82
< func (s *Scope) LookupParent(name string, pos syntax.Pos) (*Scope, Object) {
---
> func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object) {
84c84
< 		if obj := s.elems[name]; obj != nil && (!pos.IsKnown() || cmpPos(obj.scopePos(), pos) <= 0) {
---
> 		if obj := s.elems[name]; obj != nil && (!pos.IsValid() || obj.scopePos() <= pos) {
111,144d110
< // Squash merges s with its parent scope p by adding all
< // objects of s to p, adding all children of s to the
< // children of p, and removing s from p's children.
< // The function f is called for each object obj in s which
< // has an object alt in p. s should be discarded after
< // having been squashed.
< func (s *Scope) Squash(err func(obj, alt Object)) {
< 	p := s.parent
< 	assert(p != nil)
< 	for _, obj := range s.elems {
< 		obj.setParent(nil)
< 		if alt := p.Insert(obj); alt != nil {
< 			err(obj, alt)
< 		}
< 	}
<
< 	j := -1 // index of s in p.children
< 	for i, ch := range p.children {
< 		if ch == s {
< 			j = i
< 			break
< 		}
< 	}
< 	assert(j >= 0)
< 	k := len(p.children) - 1
< 	p.children[j] = p.children[k]
< 	p.children = p.children[:k]
<
< 	p.children = append(p.children, s.children...)
<
< 	s.children = nil
< 	s.elems = nil
< }
<
149,150c115,116
< func (s *Scope) Pos() syntax.Pos { return s.pos }
< func (s *Scope) End() syntax.Pos { return s.end }
---
> func (s *Scope) Pos() token.Pos { return s.pos }
> func (s *Scope) End() token.Pos { return s.end }
155,156c121,122
< func (s *Scope) Contains(pos syntax.Pos) bool {
< 	return cmpPos(s.pos, pos) <= 0 && cmpPos(pos, s.end) < 0
---
> func (s *Scope) Contains(pos token.Pos) bool {
> 	return s.pos <= pos && pos < s.end
164c130
< func (s *Scope) Innermost(pos syntax.Pos) *Scope {
---
> func (s *Scope) Innermost(pos token.Pos) *Scope {

Change-Id: If6c459f45dae8980ffb3a902a46b1700e9b55dc7
Reviewed-on: https://go-review.googlesource.com/c/go/+/265700
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2020-10-27 11:50:46 -07:00
parent 9392b82919
commit 41ff51ae00

View File

@ -1,4 +1,3 @@
// UNREVIEWED
// Copyright 2013 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.