cuelang.org/go@v0.13.0/internal/core/dep/mixed.go (about)

     1  // Copyright 2020 CUE Authors
     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 dep
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"cuelang.org/go/internal/core/adt"
    21  )
    22  
    23  // dynamic visits conjuncts of structs that are defined by the root for all
    24  // of its fields, recursively.
    25  //
    26  // The current algorithm visits all known conjuncts and descends into the
    27  // evaluated Vertex. A more correct and more performant algorithm would be to
    28  // descend into the conjuncts and evaluate the necessary values, like fields
    29  // and comprehension sources.
    30  func (v *visitor) dynamic(n *adt.Vertex, top bool) {
    31  	found := false
    32  	// TODO: Consider if we should only visit the conjuncts of the disjunction
    33  	// for dynamic mode.
    34  	n.VisitLeafConjuncts(func(c adt.Conjunct) bool {
    35  		if v.marked[c.Expr()] {
    36  			found = true
    37  			return false
    38  		}
    39  		return true
    40  	})
    41  
    42  	if !found {
    43  		return
    44  	}
    45  
    46  	if v.visit(n, top) != nil {
    47  		return
    48  	}
    49  
    50  	n = n.DerefValue()
    51  	for _, a := range n.Arcs {
    52  		if !a.IsDefined(v.ctxt) || a.Label.IsLet() {
    53  			continue
    54  		}
    55  		v.dynamic(a, false)
    56  	}
    57  }
    58  
    59  type marked map[adt.Expr]bool
    60  
    61  // TODO: factor out the below logic as either a low-level dependency analyzer or
    62  // some walk functionality.
    63  
    64  // markExpr visits all nodes in an expression to mark dependencies.
    65  func (m marked) markExpr(x adt.Expr) {
    66  	m[x] = true
    67  
    68  	switch x := x.(type) {
    69  	default:
    70  
    71  	case nil:
    72  	case *adt.Vertex:
    73  		x.VisitLeafConjuncts(func(c adt.Conjunct) bool {
    74  			m.markExpr(c.Expr())
    75  			return true
    76  		})
    77  
    78  	case *adt.BinaryExpr:
    79  		if x.Op == adt.AndOp {
    80  			m.markExpr(x.X)
    81  			m.markExpr(x.Y)
    82  		}
    83  
    84  	case *adt.StructLit:
    85  		for _, e := range x.Decls {
    86  			switch x := e.(type) {
    87  			case *adt.Field:
    88  				m.markExpr(x.Value)
    89  
    90  			case *adt.BulkOptionalField:
    91  				m.markExpr(x.Value)
    92  
    93  			case *adt.LetField:
    94  				m.markExpr(x.Value)
    95  
    96  			case *adt.DynamicField:
    97  				m.markExpr(x.Value)
    98  
    99  			case *adt.Ellipsis:
   100  				m.markExpr(x.Value)
   101  
   102  			case adt.Expr:
   103  				m.markExpr(x)
   104  
   105  			case *adt.Comprehension:
   106  				m.markComprehension(x)
   107  
   108  			default:
   109  				panic(fmt.Sprintf("unreachable %T", x))
   110  			}
   111  		}
   112  
   113  	case *adt.ListLit:
   114  		for _, e := range x.Elems {
   115  			switch x := e.(type) {
   116  			case adt.Expr:
   117  				m.markExpr(x)
   118  
   119  			case *adt.Comprehension:
   120  				m.markComprehension(x)
   121  
   122  			case *adt.Ellipsis:
   123  				m.markExpr(x.Value)
   124  
   125  			default:
   126  				panic(fmt.Sprintf("unreachable %T", x))
   127  			}
   128  		}
   129  
   130  	case *adt.DisjunctionExpr:
   131  		for _, d := range x.Values {
   132  			m.markExpr(d.Val)
   133  		}
   134  	}
   135  }
   136  
   137  func (m marked) markComprehension(y *adt.Comprehension) {
   138  	m.markExpr(adt.ToExpr(y.Value))
   139  }