...

Source file src/cuelang.org/go/internal/core/dep/mixed.go

Documentation: cuelang.org/go/internal/core/dep

     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  	for _, c := range n.Conjuncts {
    33  		if v.marked[c.Expr()] {
    34  			found = true
    35  			break
    36  		}
    37  	}
    38  
    39  	if !found {
    40  		return
    41  	}
    42  
    43  	if v.visit(n, top) != nil {
    44  		return
    45  	}
    46  
    47  	for _, a := range n.Arcs {
    48  		if !a.IsDefined(v.ctxt) || a.Label.IsLet() {
    49  			continue
    50  		}
    51  		v.dynamic(a, false)
    52  	}
    53  }
    54  
    55  type marked map[adt.Expr]bool
    56  
    57  // TODO: factor out the below logic as either a low-level dependency analyzer or
    58  // some walk functionality.
    59  
    60  // markExpr visits all nodes in an expression to mark dependencies.
    61  func (m marked) markExpr(x adt.Expr) {
    62  	m[x] = true
    63  
    64  	switch x := x.(type) {
    65  	default:
    66  
    67  	case nil:
    68  	case *adt.Vertex:
    69  		for _, c := range x.Conjuncts {
    70  			m.markExpr(c.Expr())
    71  		}
    72  
    73  	case *adt.BinaryExpr:
    74  		if x.Op == adt.AndOp {
    75  			m.markExpr(x.X)
    76  			m.markExpr(x.Y)
    77  		}
    78  
    79  	case *adt.StructLit:
    80  		for _, e := range x.Decls {
    81  			switch x := e.(type) {
    82  			case *adt.Field:
    83  				m.markExpr(x.Value)
    84  
    85  			case *adt.BulkOptionalField:
    86  				m.markExpr(x.Value)
    87  
    88  			case *adt.LetField:
    89  				m.markExpr(x.Value)
    90  
    91  			case *adt.DynamicField:
    92  				m.markExpr(x.Value)
    93  
    94  			case *adt.Ellipsis:
    95  				m.markExpr(x.Value)
    96  
    97  			case adt.Expr:
    98  				m.markExpr(x)
    99  
   100  			case *adt.Comprehension:
   101  				m.markComprehension(x)
   102  
   103  			default:
   104  				panic(fmt.Sprintf("unreachable %T", x))
   105  			}
   106  		}
   107  
   108  	case *adt.ListLit:
   109  		for _, e := range x.Elems {
   110  			switch x := e.(type) {
   111  			case adt.Expr:
   112  				m.markExpr(x)
   113  
   114  			case *adt.Comprehension:
   115  				m.markComprehension(x)
   116  
   117  			case *adt.Ellipsis:
   118  				m.markExpr(x.Value)
   119  
   120  			default:
   121  				panic(fmt.Sprintf("unreachable %T", x))
   122  			}
   123  		}
   124  
   125  	case *adt.DisjunctionExpr:
   126  		for _, d := range x.Values {
   127  			m.markExpr(d.Val)
   128  		}
   129  	}
   130  }
   131  
   132  func (m marked) markComprehension(y *adt.Comprehension) {
   133  	m.markExpr(adt.ToExpr(y.Value))
   134  }
   135  

View as plain text