type StaticOptimizer … // NewStaticOptimizer creates a StaticOptimizer with a sequence of ASTOptimizer's to be applied // to a checked expression. func NewStaticOptimizer(optimizers ...ASTOptimizer) *StaticOptimizer { … } // Optimize applies a sequence of optimizations to an Ast within a given environment. // // If issues are encountered, the Issues.Err() return value will be non-nil. func (opt *StaticOptimizer) Optimize(env *Env, a *Ast) (*Ast, *Issues) { … } // normalizeIDs ensures that the metadata present with an AST is reset in a manner such // that the ids within the expression correspond to the ids within macros. func normalizeIDs(idGen ast.IDGenerator, optimized ast.Expr, info *ast.SourceInfo) { … } func cleanupMacroRefs(expr ast.Expr, info *ast.SourceInfo) { … } // newIDGenerator ensures that new ids are only created the first time they are encountered. func newIDGenerator(seed int64) *idGenerator { … } type idGenerator … func (gen *idGenerator) nextID() int64 { … } func (gen *idGenerator) renumberStable(id int64) int64 { … } type OptimizerContext … type ASTOptimizer … type optimizerExprFactory … // NewAST creates an AST from the current expression using the tracked source info which // is modified and managed by the OptimizerContext. func (opt *optimizerExprFactory) NewAST(expr ast.Expr) *ast.AST { … } // CopyAST creates a renumbered copy of `Expr` and `SourceInfo` values of the input AST, where the // renumbering uses the same scheme as the core optimizer logic ensuring there are no collisions // between copies. // // Use this method before attempting to merge the expression from AST into another. func (opt *optimizerExprFactory) CopyAST(a *ast.AST) (ast.Expr, *ast.SourceInfo) { … } // CopyASTAndMetadata copies the input AST and propagates the macro metadata into the AST being // optimized. func (opt *optimizerExprFactory) CopyASTAndMetadata(a *ast.AST) ast.Expr { … } // ClearMacroCall clears the macro at the given expression id. func (opt *optimizerExprFactory) ClearMacroCall(id int64) { … } // SetMacroCall sets the macro call metadata for the given macro id within the tracked source info // metadata. func (opt *optimizerExprFactory) SetMacroCall(id int64, expr ast.Expr) { … } // MacroCalls returns the map of macro calls currently in the context. func (opt *optimizerExprFactory) MacroCalls() map[int64]ast.Expr { … } // NewBindMacro creates an AST expression representing the expanded bind() macro, and a macro expression // representing the unexpanded call signature to be inserted into the source info macro call metadata. func (opt *optimizerExprFactory) NewBindMacro(macroID int64, varName string, varInit, remaining ast.Expr) (astExpr, macroExpr ast.Expr) { … } // NewCall creates a global function call invocation expression. // // Example: // // countByField(list, fieldName) // - function: countByField // - args: [list, fieldName] func (opt *optimizerExprFactory) NewCall(function string, args ...ast.Expr) ast.Expr { … } // NewMemberCall creates a member function call invocation expression where 'target' is the receiver of the call. // // Example: // // list.countByField(fieldName) // - function: countByField // - target: list // - args: [fieldName] func (opt *optimizerExprFactory) NewMemberCall(function string, target ast.Expr, args ...ast.Expr) ast.Expr { … } // NewIdent creates a new identifier expression. // // Examples: // // - simple_var_name // - qualified.subpackage.var_name func (opt *optimizerExprFactory) NewIdent(name string) ast.Expr { … } // NewLiteral creates a new literal expression value. // // The range of valid values for a literal generated during optimization is different than for expressions // generated via parsing / type-checking, as the ref.Val may be _any_ CEL value so long as the value can // be converted back to a literal-like form. func (opt *optimizerExprFactory) NewLiteral(value ref.Val) ast.Expr { … } // NewList creates a list expression with a set of optional indices. // // Examples: // // [a, b] // - elems: [a, b] // - optIndices: [] // // [a, ?b, ?c] // - elems: [a, b, c] // - optIndices: [1, 2] func (opt *optimizerExprFactory) NewList(elems []ast.Expr, optIndices []int32) ast.Expr { … } // NewMap creates a map from a set of entry expressions which contain a key and value expression. func (opt *optimizerExprFactory) NewMap(entries []ast.EntryExpr) ast.Expr { … } // NewMapEntry creates a map entry with a key and value expression and a flag to indicate whether the // entry is optional. // // Examples: // // {a: b} // - key: a // - value: b // - optional: false // // {?a: ?b} // - key: a // - value: b // - optional: true func (opt *optimizerExprFactory) NewMapEntry(key, value ast.Expr, isOptional bool) ast.EntryExpr { … } // NewHasMacro generates a test-only select expression to be included within an AST and an unexpanded // has() macro call signature to be inserted into the source info macro call metadata. func (opt *optimizerExprFactory) NewHasMacro(macroID int64, s ast.Expr) (astExpr, macroExpr ast.Expr) { … } // NewSelect creates a select expression where a field value is selected from an operand. // // Example: // // msg.field_name // - operand: msg // - field: field_name func (opt *optimizerExprFactory) NewSelect(operand ast.Expr, field string) ast.Expr { … } // NewStruct creates a new typed struct value with an set of field initializations. // // Example: // // pkg.TypeName{field: value} // - typeName: pkg.TypeName // - fields: [{field: value}] func (opt *optimizerExprFactory) NewStruct(typeName string, fields []ast.EntryExpr) ast.Expr { … } // NewStructField creates a struct field initialization. // // Examples: // // {count: 3u} // - field: count // - value: 3u // - optional: false // // {?count: x} // - field: count // - value: x // - optional: true func (opt *optimizerExprFactory) NewStructField(field string, value ast.Expr, isOptional bool) ast.EntryExpr { … } // UpdateExpr updates the target expression with the updated content while preserving macro metadata. // // There are four scenarios during the update to consider: // 1. target is not macro, updated is not macro // 2. target is macro, updated is not macro // 3. target is macro, updated is macro // 4. target is not macro, updated is macro // // When the target is a macro already, it may either be updated to a new macro function // body if the update is also a macro, or it may be removed altogether if the update is // a macro. // // When the update is a macro, then the target references within other macros must be // updated to point to the new updated macro. Otherwise, other macros which pointed to // the target body must be replaced with copies of the updated expression body. func (opt *optimizerExprFactory) UpdateExpr(target, updated ast.Expr) { … } func (opt *optimizerExprFactory) sanitizeMacro(macroID int64, macroExpr ast.Expr) { … }