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
|
// Copyright 2020 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.
package subsume
// TODO: structural subsumption has not yet been implemented.
import "cuelang.org/go/internal/core/adt"
func (s *subsumer) subsumes(gt, lt adt.Conjunct) bool {
if gt == lt {
return true
}
// First try evaluating at the value level.
x, _ := gt.Expr().(adt.Value)
y, _ := lt.Expr().(adt.Value)
if x == nil {
// Fall back to structural.
return s.structural(gt, lt)
}
if y == nil {
return false
}
return s.values(x, y)
}
func (s *subsumer) conjunct(gt, lt adt.Conjunct) bool {
return false
}
func (s *subsumer) c(env *adt.Environment, x adt.Expr) adt.Conjunct {
return adt.MakeRootConjunct(env, x)
}
func isBottomConjunct(c adt.Conjunct) bool {
b, _ := c.Expr().(*adt.Bottom)
return b != nil
}
func (s *subsumer) node(env *adt.Environment, up int32) *adt.Vertex {
for ; up != 0; up-- {
env = env.Up
}
return env.Vertex
}
func (s *subsumer) structural(a, b adt.Conjunct) bool {
if isBottomConjunct(b) {
return true
}
switch x := a.Expr().(type) {
case *adt.DisjunctionExpr:
case *adt.StructLit:
case *adt.ListLit:
case *adt.FieldReference:
if y, ok := b.Elem().(*adt.FieldReference); ok && x.Label == y.Label {
if s.node(a.Env, x.UpCount) == s.node(b.Env, y.UpCount) {
return true
}
}
case *adt.LabelReference:
if y, ok := b.Elem().(*adt.LabelReference); ok {
if s.node(a.Env, x.UpCount) == s.node(b.Env, y.UpCount) {
return true
}
}
case *adt.DynamicReference:
if y, ok := b.Elem().(*adt.FieldReference); ok {
if s.node(a.Env, x.UpCount) == s.node(b.Env, y.UpCount) {
return true
}
}
case *adt.ImportReference:
if y, ok := b.Elem().(*adt.ImportReference); ok &&
x.ImportPath == y.ImportPath {
return true
}
case *adt.LetReference:
if y, ok := b.Elem().(*adt.LetReference); ok && x.Label == y.Label {
if s.node(a.Env, x.UpCount) == s.node(b.Env, y.UpCount) {
return true
}
}
case *adt.SelectorExpr:
if y, ok := a.Elem().(*adt.SelectorExpr); ok &&
x.Sel == y.Sel &&
s.conjunct(s.c(a.Env, x.X), s.c(b.Env, y.X)) {
return true
}
case *adt.IndexExpr:
if y, ok := b.Elem().(*adt.IndexExpr); ok &&
s.conjunct(s.c(a.Env, x.X), s.c(b.Env, y.X)) &&
s.conjunct(s.c(a.Env, x.Index), s.c(b.Env, y.Index)) {
return true
}
case *adt.SliceExpr:
if r, ok := b.Elem().(*adt.SliceExpr); ok &&
s.conjunct(s.c(a.Env, x.X), s.c(b.Env, r.X)) &&
s.conjunct(s.c(a.Env, x.Lo), s.c(b.Env, r.Lo)) &&
s.conjunct(s.c(a.Env, x.Hi), s.c(b.Env, r.Hi)) {
return true
}
case *adt.Interpolation:
switch y := b.Elem().(type) {
case *adt.String:
// Be conservative if not ground.
s.inexact = true
case *adt.Interpolation:
// structural equivalence
if len(x.Parts) != len(y.Parts) {
return false
}
for i, p := range x.Parts {
if !s.conjunct(s.c(a.Env, p), s.c(b.Env, y.Parts[i])) {
return false
}
}
return true
}
case *adt.BoundExpr:
if y, ok := b.Elem().(*adt.BoundExpr); ok && x.Op == y.Op {
return s.conjunct(s.c(a.Env, x.Expr), s.c(b.Env, y.Expr))
}
case *adt.UnaryExpr:
if y, ok := b.Elem().(*adt.UnaryExpr); ok && x.Op == y.Op {
return s.conjunct(s.c(a.Env, x.X), s.c(b.Env, y.X))
}
case *adt.BinaryExpr:
if y, ok := b.Elem().(*adt.BinaryExpr); ok && x.Op == y.Op {
return s.conjunct(s.c(a.Env, x.X), s.c(b.Env, y.X)) &&
s.conjunct(s.c(a.Env, x.Y), s.c(b.Env, y.Y))
}
case *adt.CallExpr:
if y, ok := b.Elem().(*adt.CallExpr); ok {
if len(x.Args) != len(y.Args) {
return false
}
for i, arg := range x.Args {
if !s.conjunct(s.c(a.Env, arg), s.c(b.Env, y.Args[i])) {
return false
}
}
return s.conjunct(s.c(a.Env, x.Fun), s.c(b.Env, y.Fun))
}
}
return false
}
func (s *subsumer) structLit(
ea *adt.Environment, sa *adt.StructLit,
eb *adt.Environment, sb *adt.StructLit) bool {
// Create index of instance fields.
ca := newCollatedDecls()
ca.collate(ea, sa)
if ca.yielders != nil || ca.dynamic != nil {
// TODO: we could do structural comparison of comprehensions
// in many cases. For instance, an if clause would subsume
// structurally if it subsumes any of the if clauses in sb.
s.inexact = true
return false
}
cb := newCollatedDecls()
cb.collate(eb, sb)
if ca.hasOptional && !s.IgnoreOptional {
// TODO: same argument here as for comprehensions. This could
// be made to work.
if ca.pattern != nil || ca.dynamic != nil {
s.inexact = true
return false
}
// for f, b := range cb.fields {
// if !b.required || f.IsDef() {
// continue
// }
// name := ctx.LabelStr(b.Label)
// arg := &stringLit{x.baseValue, name, nil}
// u, _ := x.optionals.constraint(ctx, arg)
// if u != nil && !s.subsumes(u, b.v) {
// return false
// }
// }
}
return false
}
// collatedDecls is used to compute the structural subsumption of two
// struct literals.
type collatedDecls struct {
fields map[adt.Feature]field
yielders []adt.Yielder
pattern []*adt.BulkOptionalField
dynamic []*adt.DynamicField
values []adt.Expr
additional []*adt.Ellipsis
isOpen bool
hasOptional bool
}
func newCollatedDecls() *collatedDecls {
return &collatedDecls{fields: map[adt.Feature]field{}}
}
type field struct {
required bool
conjuncts []adt.Conjunct
}
func (c *collatedDecls) collate(env *adt.Environment, s *adt.StructLit) {
for _, d := range s.Decls {
switch x := d.(type) {
case *adt.Field:
e := c.fields[x.Label]
e.required = true
e.conjuncts = append(e.conjuncts, adt.MakeRootConjunct(env, x))
c.fields[x.Label] = e
if x.ArcType != adt.ArcMember {
c.hasOptional = true
}
case *adt.BulkOptionalField:
c.pattern = append(c.pattern, x)
c.hasOptional = true
case *adt.DynamicField:
c.dynamic = append(c.dynamic, x)
c.hasOptional = true
case *adt.Ellipsis:
c.isOpen = true
c.additional = append(c.additional, x)
case *adt.Comprehension:
c.yielders = append(c.yielders, x.Clauses...)
case *adt.LetClause:
c.yielders = append(c.yielders, x)
}
}
}
|