1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file implements scopes and the objects they contain.
package astutil
import (
"fmt"
"strings"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
)
// An ErrFunc processes errors.
type ErrFunc func(pos token.Pos, msg string, args ...interface{})
// TODO: future development
//
// Resolution currently assigns values along the table below. This is based on
// Go's resolver and is not quite convenient for CUE's purposes. For one, CUE
// allows manually setting resolution and than call astutil.Sanitize to
// normalize the ast.File. Manually assigning resolutions according to the
// below table is rather tedious though.
//
// Instead of using the Scope and Node fields in identifiers, we suggest the
// following assignments:
//
// Reference Node // an Decl or Clause
// Ident *Ident // The identifier in References (optional)
//
// References always refers to the direct element in the scope in which the
// identifier occurs, not the final value, so: *Field, *LetClause, *ForClause,
// etc. In case Ident is defined, it must be the same pointer as the
// referencing identifier. In case it is not defined, the Name of the
// referencing identifier can be used to locate the proper identifier in the
// referenced node.
//
// The Scope field in the original design then loses its function.
//
// Type of reference Scope Node
// Let Clause File/Struct LetClause
// Alias declaration File/Struct Alias (deprecated)
// Illegal Reference File/Struct
// Value
// X in a: X=y Field Alias
// Fields
// X in X: y File/Struct Expr (y)
// X in X=x: y File/Struct Field
// X in X=(x): y File/Struct Field
// X in X="\(x)": y File/Struct Field
// X in [X=x]: y Field Expr (x)
// X in X=[x]: y Field Field
//
// for k, v in ForClause Ident
// let x = y LetClause Ident
//
// Fields inside lambda
// Label Field Expr
// Value Field Field
// Pkg nil ImportSpec
// Resolve resolves all identifiers in a file. Unresolved identifiers are
// recorded in Unresolved. It will not overwrite already resolved values.
func Resolve(f *ast.File, errFn ErrFunc) {
visitor := &scope{errFn: errFn, identFn: resolveIdent}
ast.Walk(f, visitor.Before, nil)
}
// Resolve resolves all identifiers in an expression.
// It will not overwrite already resolved values.
func ResolveExpr(e ast.Expr, errFn ErrFunc) {
f := &ast.File{}
visitor := &scope{file: f, errFn: errFn, identFn: resolveIdent}
ast.Walk(e, visitor.Before, nil)
}
// A Scope maintains the set of named language entities declared
// in the scope and a link to the immediately surrounding (outer)
// scope.
type scope struct {
file *ast.File
outer *scope
node ast.Node
index map[string]entry
inField bool
identFn func(s *scope, n *ast.Ident) bool
nameFn func(name string)
errFn func(p token.Pos, msg string, args ...interface{})
}
type entry struct {
node ast.Node
link ast.Node // Alias, LetClause, or Field
}
func newScope(f *ast.File, outer *scope, node ast.Node, decls []ast.Decl) *scope {
const n = 4 // initial scope capacity
s := &scope{
file: f,
outer: outer,
node: node,
index: make(map[string]entry, n),
identFn: outer.identFn,
nameFn: outer.nameFn,
errFn: outer.errFn,
}
for _, d := range decls {
switch x := d.(type) {
case *ast.Field:
label := x.Label
if a, ok := x.Label.(*ast.Alias); ok {
name := a.Ident.Name
if _, ok := a.Expr.(*ast.ListLit); !ok {
s.insert(name, x, a)
}
}
// default:
name, isIdent, _ := ast.LabelName(label)
if isIdent {
v := x.Value
// Avoid interpreting value aliases at this point.
if a, ok := v.(*ast.Alias); ok {
v = a.Expr
}
s.insert(name, v, x)
}
case *ast.LetClause:
name, isIdent, _ := ast.LabelName(x.Ident)
if isIdent {
s.insert(name, x, x)
}
case *ast.Alias:
name, isIdent, _ := ast.LabelName(x.Ident)
if isIdent {
s.insert(name, x, x)
}
case *ast.ImportDecl:
for _, spec := range x.Specs {
info, _ := ParseImportSpec(spec)
s.insert(info.Ident, spec, spec)
}
}
}
return s
}
func (s *scope) isLet(n ast.Node) bool {
if _, ok := s.node.(*ast.Field); ok {
return true
}
switch n.(type) {
case *ast.LetClause, *ast.Alias, *ast.Field:
return true
}
return false
}
func (s *scope) mustBeUnique(n ast.Node) bool {
if _, ok := s.node.(*ast.Field); ok {
return true
}
switch n.(type) {
// TODO: add *ast.ImportSpec when some implementations are moved over to
// Sanitize.
case *ast.ImportSpec, *ast.LetClause, *ast.Alias, *ast.Field:
return true
}
return false
}
func (s *scope) insert(name string, n, link ast.Node) {
if name == "" {
return
}
if s.nameFn != nil {
s.nameFn(name)
}
// TODO: record both positions.
if outer, _, existing := s.lookup(name); existing.node != nil {
if s.isLet(n) != outer.isLet(existing.node) {
s.errFn(n.Pos(), "cannot have both alias and field with name %q in same scope", name)
return
} else if s.mustBeUnique(n) || outer.mustBeUnique(existing.node) {
if outer == s {
if _, ok := existing.node.(*ast.ImportSpec); ok {
return
// TODO:
// s.errFn(n.Pos(), "conflicting declaration %s\n"+
// "\tprevious declaration at %s",
// name, existing.node.Pos())
} else {
s.errFn(n.Pos(), "alias %q redeclared in same scope", name)
}
return
}
// TODO: Should we disallow shadowing of aliases?
// This was the case, but it complicates the transition to
// square brackets. The spec says allow it.
// s.errFn(n.Pos(), "alias %q already declared in enclosing scope", name)
}
}
s.index[name] = entry{node: n, link: link}
}
func (s *scope) resolveScope(name string, node ast.Node) (scope ast.Node, e entry, ok bool) {
last := s
for s != nil {
if n, ok := s.index[name]; ok && node == n.node {
if last.node == n.node {
return nil, n, true
}
return s.node, n, true
}
s, last = s.outer, s
}
return nil, entry{}, false
}
func (s *scope) lookup(name string) (p *scope, obj ast.Node, node entry) {
// TODO(#152): consider returning nil for obj if it is a reference to root.
// last := s
if name == "_" {
return nil, nil, entry{}
}
for s != nil {
if n, ok := s.index[name]; ok {
if _, ok := n.node.(*ast.ImportSpec); ok {
return s, nil, n
}
return s, s.node, n
}
// s, last = s.outer, s
s = s.outer
}
return nil, nil, entry{}
}
func (s *scope) Before(n ast.Node) bool {
switch x := n.(type) {
case *ast.File:
s := newScope(x, s, x, x.Decls)
// Support imports.
for _, d := range x.Decls {
ast.Walk(d, s.Before, nil)
}
return false
case *ast.StructLit:
s = newScope(s.file, s, x, x.Elts)
for _, elt := range x.Elts {
ast.Walk(elt, s.Before, nil)
}
return false
case *ast.Comprehension:
s = scopeClauses(s, x.Clauses)
ast.Walk(x.Value, s.Before, nil)
return false
case *ast.Field:
var n ast.Node = x.Label
alias, ok := x.Label.(*ast.Alias)
if ok {
n = alias.Expr
}
switch label := n.(type) {
case *ast.ParenExpr:
ast.Walk(label, s.Before, nil)
case *ast.Interpolation:
ast.Walk(label, s.Before, nil)
case *ast.ListLit:
if len(label.Elts) != 1 {
break
}
s = newScope(s.file, s, x, nil)
if alias != nil {
if name, _, _ := ast.LabelName(alias.Ident); name != "" {
s.insert(name, x, alias)
}
}
expr := label.Elts[0]
if a, ok := expr.(*ast.Alias); ok {
expr = a.Expr
// Add to current scope, instead of the value's, and allow
// references to bind to these illegally.
// We need this kind of administration anyway to detect
// illegal name clashes, and it allows giving better error
// messages. This puts the burden on clients of this library
// to detect illegal usage, though.
s.insert(a.Ident.Name, a.Expr, a)
}
ast.Walk(expr, nil, func(n ast.Node) {
if x, ok := n.(*ast.Ident); ok {
for s := s; s != nil && !s.inField; s = s.outer {
if _, ok := s.index[x.Name]; ok {
s.errFn(n.Pos(),
"reference %q in label expression refers to field against which it would be matched", x.Name)
}
}
}
})
ast.Walk(expr, s.Before, nil)
}
if n := x.Value; n != nil {
if alias, ok := x.Value.(*ast.Alias); ok {
// TODO: this should move into Before once decl attributes
// have been fully deprecated and embed attributes are introduced.
s = newScope(s.file, s, x, nil)
s.insert(alias.Ident.Name, alias, x)
n = alias.Expr
}
s.inField = true
ast.Walk(n, s.Before, nil)
s.inField = false
}
return false
case *ast.LetClause:
// Disallow referring to the current LHS name.
name := x.Ident.Name
saved := s.index[name]
delete(s.index, name) // The same name may still appear in another scope
if x.Expr != nil {
ast.Walk(x.Expr, s.Before, nil)
}
s.index[name] = saved
return false
case *ast.Alias:
// Disallow referring to the current LHS name.
name := x.Ident.Name
saved := s.index[name]
delete(s.index, name) // The same name may still appear in another scope
if x.Expr != nil {
ast.Walk(x.Expr, s.Before, nil)
}
s.index[name] = saved
return false
case *ast.ImportSpec:
return false
case *ast.Attribute:
// TODO: tokenize attributes, resolve identifiers and store the ones
// that resolve in a list.
case *ast.SelectorExpr:
ast.Walk(x.X, s.Before, nil)
return false
case *ast.Ident:
if s.identFn(s, x) {
return false
}
}
return true
}
func resolveIdent(s *scope, x *ast.Ident) bool {
name, ok, _ := ast.LabelName(x)
if !ok {
// TODO: generate error
return false
}
if _, obj, node := s.lookup(name); node.node != nil {
switch x.Node {
case nil:
x.Node = node.node
x.Scope = obj
case node.node:
x.Scope = obj
default: // x.Node != node
scope, _, ok := s.resolveScope(name, x.Node)
if !ok {
s.file.Unresolved = append(s.file.Unresolved, x)
}
x.Scope = scope
}
} else {
s.file.Unresolved = append(s.file.Unresolved, x)
}
return true
}
func scopeClauses(s *scope, clauses []ast.Clause) *scope {
for _, c := range clauses {
switch x := c.(type) {
case *ast.ForClause:
ast.Walk(x.Source, s.Before, nil)
s = newScope(s.file, s, x, nil)
if x.Key != nil {
s.insert(x.Key.Name, x.Key, x)
}
s.insert(x.Value.Name, x.Value, x)
case *ast.LetClause:
ast.Walk(x.Expr, s.Before, nil)
s = newScope(s.file, s, x, nil)
s.insert(x.Ident.Name, x.Ident, x)
default:
ast.Walk(c, s.Before, nil)
}
}
return s
}
// Debugging support
func (s *scope) String() string {
var b strings.Builder
fmt.Fprintf(&b, "scope %p {", s)
if s != nil && len(s.index) > 0 {
fmt.Fprintln(&b)
for name := range s.index {
fmt.Fprintf(&b, "\t%v\n", name)
}
}
fmt.Fprintf(&b, "}\n")
return b.String()
}
|