| 1 | // Copyright 2023 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 ast |
| 16 | |
| 17 | import "github.com/google/cel-go/common/types/ref" |
| 18 | |
| 19 | // ExprFactory interfaces defines a set of methods necessary for building native expression values. |
| 20 | type ExprFactory interface { |
| 21 | // CopyExpr creates a deep copy of the input Expr value. |
| 22 | CopyExpr(Expr) Expr |
| 23 | |
| 24 | // CopyEntryExpr creates a deep copy of the input EntryExpr value. |
| 25 | CopyEntryExpr(EntryExpr) EntryExpr |
| 26 | |
| 27 | // NewCall creates an Expr value representing a global function call. |
| 28 | NewCall(id int64, function string, args ...Expr) Expr |
| 29 | |
| 30 | // NewComprehension creates an Expr value representing a one-variable comprehension over a value range. |
| 31 | NewComprehension(id int64, iterRange Expr, iterVar, accuVar string, accuInit, loopCondition, loopStep, result Expr) Expr |
| 32 | |
| 33 | // NewComprehensionTwoVar creates an Expr value representing a two-variable comprehension over a value range. |
| 34 | NewComprehensionTwoVar(id int64, iterRange Expr, iterVar, iterVar2, accuVar string, accuInit, loopCondition, loopStep, result Expr) Expr |
| 35 | |
| 36 | // NewMemberCall creates an Expr value representing a member function call. |
| 37 | NewMemberCall(id int64, function string, receiver Expr, args ...Expr) Expr |
| 38 | |
| 39 | // NewIdent creates an Expr value representing an identifier. |
| 40 | NewIdent(id int64, name string) Expr |
| 41 | |
| 42 | // NewAccuIdent creates an Expr value representing an accumulator identifier within a |
| 43 | // comprehension. |
| 44 | NewAccuIdent(id int64) Expr |
| 45 | |
| 46 | // AccuIdentName reports the name of the accumulator variable to be used within a comprehension. |
| 47 | AccuIdentName() string |
| 48 | |
| 49 | // NewLiteral creates an Expr value representing a literal value, such as a string or integer. |
| 50 | NewLiteral(id int64, value ref.Val) Expr |
| 51 | |
| 52 | // NewList creates an Expr value representing a list literal expression with optional indices. |
| 53 | // |
| 54 | // Optional indices will typically be empty unless the CEL optional types are enabled. |
| 55 | NewList(id int64, elems []Expr, optIndices []int32) Expr |
| 56 | |
| 57 | // NewMap creates an Expr value representing a map literal expression |
| 58 | NewMap(id int64, entries []EntryExpr) Expr |
| 59 | |
| 60 | // NewMapEntry creates a MapEntry with a given key, value, and a flag indicating whether |
| 61 | // the key is optionally set. |
| 62 | NewMapEntry(id int64, key, value Expr, isOptional bool) EntryExpr |
| 63 | |
| 64 | // NewPresenceTest creates an Expr representing a field presence test on an operand expression. |
| 65 | NewPresenceTest(id int64, operand Expr, field string) Expr |
| 66 | |
| 67 | // NewSelect creates an Expr representing a field selection on an operand expression. |
| 68 | NewSelect(id int64, operand Expr, field string) Expr |
| 69 | |
| 70 | // NewStruct creates an Expr value representing a struct literal with a given type name and a |
| 71 | // set of field initializers. |
| 72 | NewStruct(id int64, typeName string, fields []EntryExpr) Expr |
| 73 | |
| 74 | // NewStructField creates a StructField with a given field name, value, and a flag indicating |
| 75 | // whether the field is optionally set. |
| 76 | NewStructField(id int64, field string, value Expr, isOptional bool) EntryExpr |
| 77 | |
| 78 | // NewUnspecifiedExpr creates an empty expression node. |
| 79 | NewUnspecifiedExpr(id int64) Expr |
| 80 | |
| 81 | isExprFactory() |
| 82 | } |
| 83 | |
| 84 | type baseExprFactory struct { |
| 85 | accumulatorName string |
| 86 | } |
| 87 | |
| 88 | // NewExprFactory creates an ExprFactory instance. |
| 89 | func NewExprFactory() ExprFactory { |
| 90 | return &baseExprFactory{ |
| 91 | "@result", |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // NewExprFactoryWithAccumulator creates an ExprFactory instance with a custom |
| 96 | // accumulator identifier name. |
| 97 | func NewExprFactoryWithAccumulator(id string) ExprFactory { |
| 98 | return &baseExprFactory{ |
| 99 | id, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (fac *baseExprFactory) NewCall(id int64, function string, args ...Expr) Expr { |
| 104 | if len(args) == 0 { |
| 105 | args = []Expr{} |
| 106 | } |
| 107 | return fac.newExpr( |
| 108 | id, |
| 109 | &baseCallExpr{ |
| 110 | function: function, |
| 111 | target: nilExpr, |
| 112 | args: args, |
| 113 | isMember: false, |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | func (fac *baseExprFactory) NewMemberCall(id int64, function string, target Expr, args ...Expr) Expr { |
| 118 | if len(args) == 0 { |
| 119 | args = []Expr{} |
| 120 | } |
| 121 | return fac.newExpr( |
| 122 | id, |
| 123 | &baseCallExpr{ |
| 124 | function: function, |
| 125 | target: target, |
| 126 | args: args, |
| 127 | isMember: true, |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | func (fac *baseExprFactory) NewComprehension(id int64, iterRange Expr, iterVar, accuVar string, accuInit, loopCond, loopStep, result Expr) Expr { |
| 132 | // Set the iter_var2 to empty string to indicate the second variable is omitted |
| 133 | return fac.NewComprehensionTwoVar(id, iterRange, iterVar, "", accuVar, accuInit, loopCond, loopStep, result) |
| 134 | } |
| 135 | |
| 136 | func (fac *baseExprFactory) NewComprehensionTwoVar(id int64, iterRange Expr, iterVar, iterVar2, accuVar string, accuInit, loopCond, loopStep, result Expr) Expr { |
| 137 | return fac.newExpr( |
| 138 | id, |
| 139 | &baseComprehensionExpr{ |
| 140 | iterRange: iterRange, |
| 141 | iterVar: iterVar, |
| 142 | iterVar2: iterVar2, |
| 143 | accuVar: accuVar, |
| 144 | accuInit: accuInit, |
| 145 | loopCond: loopCond, |
| 146 | loopStep: loopStep, |
| 147 | result: result, |
| 148 | }) |
| 149 | } |
| 150 | |
| 151 | func (fac *baseExprFactory) NewIdent(id int64, name string) Expr { |
| 152 | return fac.newExpr(id, baseIdentExpr(name)) |
| 153 | } |
| 154 | |
| 155 | func (fac *baseExprFactory) NewAccuIdent(id int64) Expr { |
| 156 | return fac.NewIdent(id, fac.AccuIdentName()) |
| 157 | } |
| 158 | |
| 159 | func (fac *baseExprFactory) AccuIdentName() string { |
| 160 | return fac.accumulatorName |
| 161 | } |
| 162 | |
| 163 | func (fac *baseExprFactory) NewLiteral(id int64, value ref.Val) Expr { |
| 164 | return fac.newExpr(id, &baseLiteral{Val: value}) |
| 165 | } |
| 166 | |
| 167 | func (fac *baseExprFactory) NewList(id int64, elems []Expr, optIndices []int32) Expr { |
| 168 | optIndexMap := make(map[int32]struct{}, len(optIndices)) |
| 169 | for _, idx := range optIndices { |
| 170 | optIndexMap[idx] = struct{}{} |
| 171 | } |
| 172 | return fac.newExpr(id, |
| 173 | &baseListExpr{ |
| 174 | elements: elems, |
| 175 | optIndices: optIndices, |
| 176 | optIndexMap: optIndexMap, |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | func (fac *baseExprFactory) NewMap(id int64, entries []EntryExpr) Expr { |
| 181 | return fac.newExpr(id, &baseMapExpr{entries: entries}) |
| 182 | } |
| 183 | |
| 184 | func (fac *baseExprFactory) NewMapEntry(id int64, key, value Expr, isOptional bool) EntryExpr { |
| 185 | return fac.newEntryExpr( |
| 186 | id, |
| 187 | &baseMapEntry{ |
| 188 | key: key, |
| 189 | value: value, |
| 190 | isOptional: isOptional, |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | func (fac *baseExprFactory) NewPresenceTest(id int64, operand Expr, field string) Expr { |
| 195 | return fac.newExpr( |
| 196 | id, |
| 197 | &baseSelectExpr{ |
| 198 | operand: operand, |
| 199 | field: field, |
| 200 | testOnly: true, |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | func (fac *baseExprFactory) NewSelect(id int64, operand Expr, field string) Expr { |
| 205 | return fac.newExpr( |
| 206 | id, |
| 207 | &baseSelectExpr{ |
| 208 | operand: operand, |
| 209 | field: field, |
| 210 | }) |
| 211 | } |
| 212 | |
| 213 | func (fac *baseExprFactory) NewStruct(id int64, typeName string, fields []EntryExpr) Expr { |
| 214 | return fac.newExpr( |
| 215 | id, |
| 216 | &baseStructExpr{ |
| 217 | typeName: typeName, |
| 218 | fields: fields, |
| 219 | }) |
| 220 | } |
| 221 | |
| 222 | func (fac *baseExprFactory) NewStructField(id int64, field string, value Expr, isOptional bool) EntryExpr { |
| 223 | return fac.newEntryExpr( |
| 224 | id, |
| 225 | &baseStructField{ |
| 226 | field: field, |
| 227 | value: value, |
| 228 | isOptional: isOptional, |
| 229 | }) |
| 230 | } |
| 231 | |
| 232 | func (fac *baseExprFactory) NewUnspecifiedExpr(id int64) Expr { |
| 233 | return fac.newExpr(id, nil) |
| 234 | } |
| 235 | |
| 236 | func (fac *baseExprFactory) CopyExpr(e Expr) Expr { |
| 237 | // unwrap navigable expressions to avoid unnecessary allocations during copying. |
| 238 | if nav, ok := e.(*navigableExprImpl); ok { |
| 239 | e = nav.Expr |
| 240 | } |
| 241 | switch e.Kind() { |
| 242 | case CallKind: |
| 243 | c := e.AsCall() |
| 244 | argsCopy := make([]Expr, len(c.Args())) |
| 245 | for i, arg := range c.Args() { |
| 246 | argsCopy[i] = fac.CopyExpr(arg) |
| 247 | } |
| 248 | if !c.IsMemberFunction() { |
| 249 | return fac.NewCall(e.ID(), c.FunctionName(), argsCopy...) |
| 250 | } |
| 251 | return fac.NewMemberCall(e.ID(), c.FunctionName(), fac.CopyExpr(c.Target()), argsCopy...) |
| 252 | case ComprehensionKind: |
| 253 | compre := e.AsComprehension() |
| 254 | return fac.NewComprehensionTwoVar(e.ID(), |
| 255 | fac.CopyExpr(compre.IterRange()), |
| 256 | compre.IterVar(), |
| 257 | compre.IterVar2(), |
| 258 | compre.AccuVar(), |
| 259 | fac.CopyExpr(compre.AccuInit()), |
| 260 | fac.CopyExpr(compre.LoopCondition()), |
| 261 | fac.CopyExpr(compre.LoopStep()), |
| 262 | fac.CopyExpr(compre.Result())) |
| 263 | case IdentKind: |
| 264 | return fac.NewIdent(e.ID(), e.AsIdent()) |
| 265 | case ListKind: |
| 266 | l := e.AsList() |
| 267 | elemsCopy := make([]Expr, l.Size()) |
| 268 | for i, elem := range l.Elements() { |
| 269 | elemsCopy[i] = fac.CopyExpr(elem) |
| 270 | } |
| 271 | return fac.NewList(e.ID(), elemsCopy, l.OptionalIndices()) |
| 272 | case LiteralKind: |
| 273 | return fac.NewLiteral(e.ID(), e.AsLiteral()) |
| 274 | case MapKind: |
| 275 | m := e.AsMap() |
| 276 | entriesCopy := make([]EntryExpr, m.Size()) |
| 277 | for i, entry := range m.Entries() { |
| 278 | entriesCopy[i] = fac.CopyEntryExpr(entry) |
| 279 | } |
| 280 | return fac.NewMap(e.ID(), entriesCopy) |
| 281 | case SelectKind: |
| 282 | s := e.AsSelect() |
| 283 | if s.IsTestOnly() { |
| 284 | return fac.NewPresenceTest(e.ID(), fac.CopyExpr(s.Operand()), s.FieldName()) |
| 285 | } |
| 286 | return fac.NewSelect(e.ID(), fac.CopyExpr(s.Operand()), s.FieldName()) |
| 287 | case StructKind: |
| 288 | s := e.AsStruct() |
| 289 | fieldsCopy := make([]EntryExpr, len(s.Fields())) |
| 290 | for i, field := range s.Fields() { |
| 291 | fieldsCopy[i] = fac.CopyEntryExpr(field) |
| 292 | } |
| 293 | return fac.NewStruct(e.ID(), s.TypeName(), fieldsCopy) |
| 294 | default: |
| 295 | return fac.NewUnspecifiedExpr(e.ID()) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func (fac *baseExprFactory) CopyEntryExpr(e EntryExpr) EntryExpr { |
| 300 | switch e.Kind() { |
| 301 | case MapEntryKind: |
| 302 | entry := e.AsMapEntry() |
| 303 | return fac.NewMapEntry(e.ID(), |
| 304 | fac.CopyExpr(entry.Key()), fac.CopyExpr(entry.Value()), entry.IsOptional()) |
| 305 | case StructFieldKind: |
| 306 | field := e.AsStructField() |
| 307 | return fac.NewStructField(e.ID(), |
| 308 | field.Name(), fac.CopyExpr(field.Value()), field.IsOptional()) |
| 309 | default: |
| 310 | return fac.newEntryExpr(e.ID(), nil) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | func (*baseExprFactory) isExprFactory() {} |
| 315 | |
| 316 | func (fac *baseExprFactory) newExpr(id int64, e exprKindCase) Expr { |
| 317 | return &expr{ |
| 318 | id: id, |
| 319 | exprKindCase: e, |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func (fac *baseExprFactory) newEntryExpr(id int64, e entryExprKindCase) EntryExpr { |
| 324 | return &entryExpr{ |
| 325 | id: id, |
| 326 | entryExprKindCase: e, |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | var ( |
| 331 | defaultFactory = &baseExprFactory{} |
| 332 | ) |