| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package checker |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/google/cel-go/common/decls" |
| 21 | ) |
| 22 | |
| 23 | // Scopes represents nested Decl sets where the Scopes value contains a Groups containing all |
| 24 | // identifiers in scope and an optional parent representing outer scopes. |
| 25 | // Each Groups value is a mapping of names to Decls in the ident and function namespaces. |
| 26 | // Lookups are performed such that bindings in inner scopes shadow those in outer scopes. |
| 27 | type Scopes struct { |
| 28 | parent *Scopes |
| 29 | scopes *Group |
| 30 | } |
| 31 | |
| 32 | // newScopes creates a new, empty Scopes. |
| 33 | // Some operations can't be safely performed until a Group is added with Push. |
| 34 | func newScopes() *Scopes { |
| 35 | return &Scopes{ |
| 36 | scopes: newGroup(), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Copy creates a copy of the current Scopes values, including a copy of its parent if non-nil. |
| 41 | func (s *Scopes) Copy() *Scopes { |
| 42 | cpy := newScopes() |
| 43 | if s == nil { |
| 44 | return cpy |
| 45 | } |
| 46 | if s.parent != nil { |
| 47 | cpy.parent = s.parent.Copy() |
| 48 | } |
| 49 | cpy.scopes = s.scopes.copy() |
| 50 | return cpy |
| 51 | } |
| 52 | |
| 53 | // Push creates a new Scopes value which references the current Scope as its parent. |
| 54 | func (s *Scopes) Push() *Scopes { |
| 55 | return &Scopes{ |
| 56 | parent: s, |
| 57 | scopes: newGroup(), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Pop returns the parent Scopes value for the current scope, or the current scope if the parent |
| 62 | // is nil. |
| 63 | func (s *Scopes) Pop() *Scopes { |
| 64 | if s.parent != nil { |
| 65 | return s.parent |
| 66 | } |
| 67 | // TODO: Consider whether this should be an error / panic. |
| 68 | return s |
| 69 | } |
| 70 | |
| 71 | // AddIdent adds the ident Decl in the current scope. |
| 72 | // Note: If the name collides with an existing identifier in the scope, the Decl is overwritten. |
| 73 | func (s *Scopes) AddIdent(decl *decls.VariableDecl) { |
| 74 | s.scopes.idents[decl.Name()] = decl |
| 75 | } |
| 76 | |
| 77 | // FindIdent finds the first ident Decl with a matching name in Scopes, or nil if one cannot be |
| 78 | // found. |
| 79 | // Note: The search is performed from innermost to outermost. |
| 80 | func (s *Scopes) FindIdent(name string) *decls.VariableDecl { |
| 81 | name = strings.TrimPrefix(name, ".") |
| 82 | if ident, found := s.scopes.idents[name]; found { |
| 83 | return ident |
| 84 | } |
| 85 | if s.parent != nil { |
| 86 | return s.parent.FindIdent(name) |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // FindIdentInScope finds the first ident Decl with a matching name in the current Scopes value, or |
| 92 | // nil if one does not exist. |
| 93 | // Note: The search is only performed on the current scope and does not search outer scopes. |
| 94 | func (s *Scopes) FindIdentInScope(name string) *decls.VariableDecl { |
| 95 | name = strings.TrimPrefix(name, ".") |
| 96 | if ident, found := s.scopes.idents[name]; found { |
| 97 | return ident |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // FindLocalIdent finds a locally scoped variable with a given name, ignoring the root scope. |
| 103 | func (s *Scopes) FindLocalIdent(name string) *decls.VariableDecl { |
| 104 | if s == nil || s.parent == nil { |
| 105 | return nil |
| 106 | } |
| 107 | if ident := s.FindIdentInScope(name); ident != nil { |
| 108 | return ident |
| 109 | } |
| 110 | return s.parent.FindLocalIdent(name) |
| 111 | } |
| 112 | |
| 113 | // FindGlobalIdent finds an identifier in the global scope, ignoring all local scopes. |
| 114 | func (s *Scopes) FindGlobalIdent(name string) *decls.VariableDecl { |
| 115 | scope := s |
| 116 | for scope.parent != nil { |
| 117 | scope = scope.parent |
| 118 | } |
| 119 | return scope.FindIdentInScope(name) |
| 120 | } |
| 121 | |
| 122 | // SetFunction adds the function Decl to the current scope. |
| 123 | // Note: Any previous entry for a function in the current scope with the same name is overwritten. |
| 124 | func (s *Scopes) SetFunction(fn *decls.FunctionDecl) { |
| 125 | s.scopes.functions[fn.Name()] = fn |
| 126 | } |
| 127 | |
| 128 | // FindFunction finds the first function Decl with a matching name in Scopes. |
| 129 | // The search is performed from innermost to outermost. |
| 130 | // Returns nil if no such function in Scopes. |
| 131 | func (s *Scopes) FindFunction(name string) *decls.FunctionDecl { |
| 132 | name = strings.TrimPrefix(name, ".") |
| 133 | if fn, found := s.scopes.functions[name]; found { |
| 134 | return fn |
| 135 | } |
| 136 | if s.parent != nil { |
| 137 | return s.parent.FindFunction(name) |
| 138 | } |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // Group is a set of Decls that is pushed on or popped off a Scopes as a unit. |
| 143 | // Contains separate namespaces for identifier and function Decls. |
| 144 | // (Should be named "Scope" perhaps?) |
| 145 | type Group struct { |
| 146 | idents map[string]*decls.VariableDecl |
| 147 | functions map[string]*decls.FunctionDecl |
| 148 | } |
| 149 | |
| 150 | // copy creates a new Group instance with a shallow copy of the variables and functions. |
| 151 | // If callers need to mutate the exprpb.Decl definitions for a Function, they should copy-on-write. |
| 152 | func (g *Group) copy() *Group { |
| 153 | cpy := &Group{ |
| 154 | idents: make(map[string]*decls.VariableDecl, len(g.idents)), |
| 155 | functions: make(map[string]*decls.FunctionDecl, len(g.functions)), |
| 156 | } |
| 157 | for n, id := range g.idents { |
| 158 | cpy.idents[n] = id |
| 159 | } |
| 160 | for n, fn := range g.functions { |
| 161 | cpy.functions[n] = fn |
| 162 | } |
| 163 | return cpy |
| 164 | } |
| 165 | |
| 166 | // newGroup creates a new Group with empty maps for identifiers and functions. |
| 167 | func newGroup() *Group { |
| 168 | return &Group{ |
| 169 | idents: make(map[string]*decls.VariableDecl), |
| 170 | functions: make(map[string]*decls.FunctionDecl), |
| 171 | } |
| 172 | } |