| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cel |
| 16 |
|
| 17 |
import ( |
| 18 |
"github.com/google/cel-go/common/ast" |
| 19 |
"github.com/google/cel-go/common/containers" |
| 20 |
"github.com/google/cel-go/common/operators" |
| 21 |
"github.com/google/cel-go/common/overloads" |
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
"github.com/google/cel-go/common/types/traits" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
type InlineVariable struct { |
| 29 |
name string |
| 30 |
alias string |
| 31 |
def *ast.AST |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (v *InlineVariable) Name() string { |
| 36 |
return v.name |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func (v *InlineVariable) Alias() string { |
| 41 |
return v.alias |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (v *InlineVariable) Expr() ast.Expr { |
| 46 |
return v.def.Expr() |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func (v *InlineVariable) Type() *Type { |
| 51 |
return v.def.GetType(v.def.Expr().ID()) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func NewInlineVariable(name string, definition *Ast) *InlineVariable { |
| 56 |
return NewInlineVariableWithAlias(name, name, definition) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func NewInlineVariableWithAlias(name, alias string, definition *Ast) *InlineVariable { |
| 63 |
return &InlineVariable{name: name, alias: alias, def: definition.NativeRep()} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func NewInliningOptimizer(inlineVars ...*InlineVariable) ASTOptimizer { |
| 71 |
return &inliningOptimizer{variables: inlineVars} |
| 72 |
} |
| 73 |
|
| 74 |
type inliningOptimizer struct { |
| 75 |
variables []*InlineVariable |
| 76 |
} |
| 77 |
|
| 78 |
func (opt *inliningOptimizer) Optimize(ctx *OptimizerContext, a *ast.AST) *ast.AST { |
| 79 |
root := ast.NavigateAST(a) |
| 80 |
for _, inlineVar := range opt.variables { |
| 81 |
matches := ast.MatchDescendants(root, opt.matchVariable(inlineVar.Name())) |
| 82 |
|
| 83 |
if len(matches) == 0 { |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if len(matches) == 1 || !isBindable(matches, inlineVar.Expr(), inlineVar.Type()) { |
| 89 |
for _, match := range matches { |
| 90 |
|
| 91 |
copyExpr := ctx.CopyASTAndMetadata(inlineVar.def) |
| 92 |
opt.inlineExpr(ctx, match, copyExpr, inlineVar.Type()) |
| 93 |
} |
| 94 |
continue |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
var lca ast.NavigableExpr = root |
| 100 |
lcaAncestorCount := 0 |
| 101 |
ancestors := map[int64]int{} |
| 102 |
for _, match := range matches { |
| 103 |
|
| 104 |
parent, found := match, true |
| 105 |
for found { |
| 106 |
ancestorCount, hasAncestor := ancestors[parent.ID()] |
| 107 |
if !hasAncestor { |
| 108 |
ancestors[parent.ID()] = 1 |
| 109 |
parent, found = parent.Parent() |
| 110 |
continue |
| 111 |
} |
| 112 |
if lcaAncestorCount < ancestorCount || (lcaAncestorCount == ancestorCount && lca.Depth() < parent.Depth()) { |
| 113 |
lca = parent |
| 114 |
lcaAncestorCount = ancestorCount |
| 115 |
} |
| 116 |
ancestors[parent.ID()] = ancestorCount + 1 |
| 117 |
parent, found = parent.Parent() |
| 118 |
} |
| 119 |
aliasExpr := ctx.NewIdent(inlineVar.Alias()) |
| 120 |
opt.inlineExpr(ctx, match, aliasExpr, inlineVar.Type()) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
copyExpr := ctx.CopyASTAndMetadata(inlineVar.def) |
| 125 |
|
| 126 |
inlined, bindMacro := ctx.NewBindMacro(lca.ID(), inlineVar.Alias(), copyExpr, lca) |
| 127 |
opt.inlineExpr(ctx, lca, inlined, inlineVar.Type()) |
| 128 |
ctx.SetMacroCall(lca.ID(), bindMacro) |
| 129 |
} |
| 130 |
return a |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
func (opt *inliningOptimizer) inlineExpr(ctx *OptimizerContext, prev ast.NavigableExpr, inlined ast.Expr, inlinedType *Type) { |
| 137 |
switch prev.Kind() { |
| 138 |
case ast.SelectKind: |
| 139 |
sel := prev.AsSelect() |
| 140 |
if !sel.IsTestOnly() { |
| 141 |
ctx.UpdateExpr(prev, inlined) |
| 142 |
return |
| 143 |
} |
| 144 |
opt.rewritePresenceExpr(ctx, prev, inlined, inlinedType) |
| 145 |
default: |
| 146 |
ctx.UpdateExpr(prev, inlined) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func (opt *inliningOptimizer) rewritePresenceExpr(ctx *OptimizerContext, prev, inlined ast.Expr, inlinedType *Type) { |
| 155 |
|
| 156 |
|
| 157 |
if inlined.Kind() == ast.SelectKind { |
| 158 |
presenceTest, hasMacro := ctx.NewHasMacro(prev.ID(), inlined) |
| 159 |
ctx.UpdateExpr(prev, presenceTest) |
| 160 |
ctx.SetMacroCall(prev.ID(), hasMacro) |
| 161 |
return |
| 162 |
} |
| 163 |
|
| 164 |
ctx.ClearMacroCall(prev.ID()) |
| 165 |
if inlinedType.IsAssignableType(NullType) { |
| 166 |
ctx.UpdateExpr(prev, |
| 167 |
ctx.NewCall(operators.NotEquals, |
| 168 |
inlined, |
| 169 |
ctx.NewLiteral(types.NullValue), |
| 170 |
)) |
| 171 |
return |
| 172 |
} |
| 173 |
if inlinedType.HasTrait(traits.SizerType) { |
| 174 |
ctx.UpdateExpr(prev, |
| 175 |
ctx.NewCall(operators.NotEquals, |
| 176 |
ctx.NewMemberCall(overloads.Size, inlined), |
| 177 |
ctx.NewLiteral(types.IntZero), |
| 178 |
)) |
| 179 |
return |
| 180 |
} |
| 181 |
ctx.ReportErrorAtID(prev.ID(), "unable to inline expression type %v into presence test", inlinedType) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func isBindable(matches []ast.NavigableExpr, inlined ast.Expr, inlinedType *Type) bool { |
| 192 |
if inlinedType.IsAssignableType(NullType) || |
| 193 |
inlinedType.HasTrait(traits.SizerType) { |
| 194 |
return true |
| 195 |
} |
| 196 |
for _, m := range matches { |
| 197 |
if m.Kind() != ast.SelectKind { |
| 198 |
continue |
| 199 |
} |
| 200 |
sel := m.AsSelect() |
| 201 |
if sel.IsTestOnly() { |
| 202 |
return false |
| 203 |
} |
| 204 |
} |
| 205 |
return true |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
func (opt *inliningOptimizer) matchVariable(varName string) ast.ExprMatcher { |
| 214 |
return func(e ast.NavigableExpr) bool { |
| 215 |
if e.Kind() == ast.IdentKind && e.AsIdent() == varName { |
| 216 |
return true |
| 217 |
} |
| 218 |
if e.Kind() == ast.SelectKind { |
| 219 |
sel := e.AsSelect() |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
qualName, found := containers.ToQualifiedName(sel.Operand()) |
| 224 |
return found && qualName+"."+sel.FieldName() == varName |
| 225 |
} |
| 226 |
return false |
| 227 |
} |
| 228 |
} |