blob: e77df49e42e5d3680bb1ca35b20a77e88d7845a1 [file] [log] [blame]
Marcel van Lohuizen6a088302020-07-03 14:42:05 +02001// 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
15package adt
16
Marcel van Lohuizen1d29ac32020-07-15 15:02:14 +020017import (
18 "cuelang.org/go/cue/ast"
19 "cuelang.org/go/cue/token"
20)
21
22func Resolve(ctx *OpContext, c Conjunct) *Vertex {
23 env := c.Env
24 // TODO: also allow resolution in parent scopes. The following will set up
25 // the environments. But the compiler also needs to resolve accordingly.
26 //
27 // // Set up environments for parent scopes, if any.
28 // root := env
29 // for p := scope; p != nil; p = p.Parent {
30 // root.Up = &Environment{Vertex: p.Parent}
31 // root = root.Up
32 // }
33
34 var v Value
35
36 expr := c.Expr()
37 switch x := expr.(type) {
38 case Value:
39 v = x
40
41 case Resolver:
42 r, err := ctx.Resolve(env, x)
43 if err != nil {
44 v = err
45 break
46 }
Marcel van Lohuizenbedb0472021-02-01 14:17:19 +010047 // r.Finalize(ctx) // TODO: Finalize here?
Marcel van Lohuizen1d29ac32020-07-15 15:02:14 +020048 return r
49
50 case Evaluator:
51 // TODO: have a way to evaluate, but not strip down to the value.
52 v, _ = ctx.Evaluate(env, expr)
53
54 default:
55 // Unknown type.
56 v = ctx.NewErrf(
Marcel van Lohuizen276ce262021-04-20 10:05:35 +020057 "could not evaluate expression %s of type %T", c.Expr(), c)
Marcel van Lohuizen1d29ac32020-07-15 15:02:14 +020058 }
59
60 return ToVertex(v)
61}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +020062
63// A Node is any abstract data type representing an value or expression.
64type Node interface {
65 Source() ast.Node
66 node() // enforce internal.
67}
68
69// A Decl represents all valid StructLit elements.
70type Decl interface {
71 Node
72 declNode()
73}
74
75// An Elem represents all value ListLit elements.
76//
77// All Elem values can be used as a Decl.
78type Elem interface {
79 Decl
80 elemNode()
81}
82
83// An Expr corresponds to an ast.Expr.
84//
85// All Expr values can be used as an Elem or Decl.
86type Expr interface {
87 Elem
88 expr()
89}
90
Marcel van Lohuizen08a16522020-11-26 19:00:03 +010091// A BaseValue is any Value or a *Marker. It indicates the type of a Vertex.
92type BaseValue interface {
93 Kind() Kind
94}
95
Marcel van Lohuizen6a088302020-07-03 14:42:05 +020096// A Value represents a node in the evaluated data graph.
97//
98// All Values values can also be used as a Expr.
99type Value interface {
100 Expr
101 Concreteness() Concreteness
102 Kind() Kind
103}
104
105// An Evaluator provides a method to convert to a value.
106type Evaluator interface {
107 Node
Marcel van Lohuizen08a16522020-11-26 19:00:03 +0100108
109 // evaluate evaluates the underlying expression. If the expression
110 // is incomplete, it may record the error in ctx and return nil.
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200111 evaluate(ctx *OpContext) Value
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200112}
113
114// A Resolver represents a reference somewhere else within a tree that resolves
115// a value.
116type Resolver interface {
117 Node
Marcel van Lohuizend0124e22021-01-26 11:57:45 +0100118 resolve(ctx *OpContext, state VertexStatus) *Vertex
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200119}
120
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200121type YieldFunc func(env *Environment, s *StructLit)
122
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200123// A Yielder represents 0 or more labeled values of structs or lists.
124type Yielder interface {
125 Node
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200126 yield(ctx *OpContext, fn YieldFunc)
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200127}
128
129// A Validator validates a Value. All Validators are Values.
130type Validator interface {
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200131 Value
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200132 validate(c *OpContext, v Value) *Bottom
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200133}
134
Marcel van Lohuizen1d29ac32020-07-15 15:02:14 +0200135// Pos returns the file position of n, or token.NoPos if it is unknown.
136func Pos(n Node) token.Pos {
137 src := n.Source()
138 if src == nil {
139 return token.NoPos
140 }
141 return src.Pos()
142}
143
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200144// Value
145
146func (x *Vertex) Concreteness() Concreteness {
147 // Depends on concreteness of value.
Marcel van Lohuizen41afc872020-11-26 20:20:58 +0100148 switch v := x.BaseValue.(type) {
Marcel van Lohuizen37293f92020-11-26 17:06:18 +0100149 case nil:
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200150 return Concrete // Should be indetermined.
Marcel van Lohuizen37293f92020-11-26 17:06:18 +0100151
152 case Value:
153 return v.Concreteness()
154
155 default: // *StructMarker, *ListMarker:
156 return Concrete
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200157 }
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200158}
159
Marcel van Lohuizen08a16522020-11-26 19:00:03 +0100160func (x *NodeLink) Concreteness() Concreteness { return Concrete }
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200161
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200162func (*Conjunction) Concreteness() Concreteness { return Constraint }
163func (*Disjunction) Concreteness() Concreteness { return Constraint }
164func (*BoundValue) Concreteness() Concreteness { return Constraint }
165
Marcel van Lohuizenf62bfed2020-12-04 14:17:34 +0100166func (*Builtin) Concreteness() Concreteness { return Concrete }
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200167func (*BuiltinValidator) Concreteness() Concreteness { return Constraint }
168
169// Value and Expr
170
171func (*Bottom) Concreteness() Concreteness { return BottomLevel }
172func (*Null) Concreteness() Concreteness { return Concrete }
173func (*Bool) Concreteness() Concreteness { return Concrete }
174func (*Num) Concreteness() Concreteness { return Concrete }
175func (*String) Concreteness() Concreteness { return Concrete }
176func (*Bytes) Concreteness() Concreteness { return Concrete }
177func (*Top) Concreteness() Concreteness { return Any }
178func (*BasicType) Concreteness() Concreteness { return Type }
179
180// Expr
181
182func (*StructLit) expr() {}
183func (*ListLit) expr() {}
184func (*DisjunctionExpr) expr() {}
185
186// Expr and Value
187
188func (*Bottom) expr() {}
189func (*Null) expr() {}
190func (*Bool) expr() {}
191func (*Num) expr() {}
192func (*String) expr() {}
193func (*Bytes) expr() {}
194func (*Top) expr() {}
195func (*BasicType) expr() {}
196func (*Vertex) expr() {}
197func (*ListMarker) expr() {}
198func (*StructMarker) expr() {}
199func (*Conjunction) expr() {}
200func (*Disjunction) expr() {}
201func (*BoundValue) expr() {}
202func (*BuiltinValidator) expr() {}
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200203func (*Builtin) expr() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200204
205// Expr and Resolver
206
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200207func (*NodeLink) expr() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200208func (*FieldReference) expr() {}
209func (*LabelReference) expr() {}
210func (*DynamicReference) expr() {}
211func (*ImportReference) expr() {}
212func (*LetReference) expr() {}
213
214// Expr and Evaluator
215
216func (*BoundExpr) expr() {}
217func (*SelectorExpr) expr() {}
218func (*IndexExpr) expr() {}
219func (*SliceExpr) expr() {}
220func (*Interpolation) expr() {}
221func (*UnaryExpr) expr() {}
222func (*BinaryExpr) expr() {}
223func (*CallExpr) expr() {}
224
225// Decl and Expr (so allow attaching original source in Conjunct)
226
227func (*Field) declNode() {}
228func (x *Field) expr() Expr { return x.Value }
229func (*OptionalField) declNode() {}
230func (x *OptionalField) expr() Expr { return x.Value }
231func (*BulkOptionalField) declNode() {}
232func (x *BulkOptionalField) expr() Expr { return x.Value }
233func (*DynamicField) declNode() {}
234func (x *DynamicField) expr() Expr { return x.Value }
235
236// Decl and Yielder
237
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200238func (*LetClause) declNode() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200239
240// Decl and Elem
241
242func (*StructLit) declNode() {}
243func (*StructLit) elemNode() {}
244func (*ListLit) declNode() {}
245func (*ListLit) elemNode() {}
246func (*Ellipsis) elemNode() {}
247func (*Ellipsis) declNode() {}
248func (*Bottom) declNode() {}
249func (*Bottom) elemNode() {}
250func (*Null) declNode() {}
251func (*Null) elemNode() {}
252func (*Bool) declNode() {}
253func (*Bool) elemNode() {}
254func (*Num) declNode() {}
255func (*Num) elemNode() {}
256func (*String) declNode() {}
257func (*String) elemNode() {}
258func (*Bytes) declNode() {}
259func (*Bytes) elemNode() {}
260func (*Top) declNode() {}
261func (*Top) elemNode() {}
262func (*BasicType) declNode() {}
263func (*BasicType) elemNode() {}
264func (*BoundExpr) declNode() {}
265func (*BoundExpr) elemNode() {}
266func (*Vertex) declNode() {}
267func (*Vertex) elemNode() {}
268func (*ListMarker) declNode() {}
269func (*ListMarker) elemNode() {}
270func (*StructMarker) declNode() {}
271func (*StructMarker) elemNode() {}
272func (*Conjunction) declNode() {}
273func (*Conjunction) elemNode() {}
274func (*Disjunction) declNode() {}
275func (*Disjunction) elemNode() {}
276func (*BoundValue) declNode() {}
277func (*BoundValue) elemNode() {}
278func (*BuiltinValidator) declNode() {}
279func (*BuiltinValidator) elemNode() {}
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200280func (*NodeLink) declNode() {}
281func (*NodeLink) elemNode() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200282func (*FieldReference) declNode() {}
283func (*FieldReference) elemNode() {}
284func (*LabelReference) declNode() {}
285func (*LabelReference) elemNode() {}
286func (*DynamicReference) declNode() {}
287func (*DynamicReference) elemNode() {}
288func (*ImportReference) declNode() {}
289func (*ImportReference) elemNode() {}
290func (*LetReference) declNode() {}
291func (*LetReference) elemNode() {}
292func (*SelectorExpr) declNode() {}
293func (*SelectorExpr) elemNode() {}
294func (*IndexExpr) declNode() {}
295func (*IndexExpr) elemNode() {}
296func (*SliceExpr) declNode() {}
297func (*SliceExpr) elemNode() {}
298func (*Interpolation) declNode() {}
299func (*Interpolation) elemNode() {}
300func (*UnaryExpr) declNode() {}
301func (*UnaryExpr) elemNode() {}
302func (*BinaryExpr) declNode() {}
303func (*BinaryExpr) elemNode() {}
304func (*CallExpr) declNode() {}
305func (*CallExpr) elemNode() {}
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200306func (*Builtin) declNode() {}
307func (*Builtin) elemNode() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200308func (*DisjunctionExpr) declNode() {}
309func (*DisjunctionExpr) elemNode() {}
310
311// Decl, Elem, and Yielder
312
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200313func (*ForClause) declNode() {}
314func (*ForClause) elemNode() {}
315func (*IfClause) declNode() {}
316func (*IfClause) elemNode() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200317
Marcel van Lohuizen0f935f82020-06-09 09:13:01 +0200318// Yielder only: ValueClause
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200319
320// Node
321
322func (*Vertex) node() {}
323func (*Conjunction) node() {}
324func (*Disjunction) node() {}
325func (*BoundValue) node() {}
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200326func (*Builtin) node() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200327func (*BuiltinValidator) node() {}
328func (*Bottom) node() {}
329func (*Null) node() {}
330func (*Bool) node() {}
331func (*Num) node() {}
332func (*String) node() {}
333func (*Bytes) node() {}
334func (*Top) node() {}
335func (*BasicType) node() {}
336func (*StructLit) node() {}
337func (*ListLit) node() {}
338func (*BoundExpr) node() {}
Marcel van Lohuizen0cfe4112020-06-27 15:01:34 +0200339func (*NodeLink) node() {}
Marcel van Lohuizen6a088302020-07-03 14:42:05 +0200340func (*FieldReference) node() {}
341func (*LabelReference) node() {}
342func (*DynamicReference) node() {}
343func (*ImportReference) node() {}
344func (*LetReference) node() {}
345func (*SelectorExpr) node() {}
346func (*IndexExpr) node() {}
347func (*SliceExpr) node() {}
348func (*Interpolation) node() {}
349func (*UnaryExpr) node() {}
350func (*BinaryExpr) node() {}
351func (*CallExpr) node() {}
352func (*DisjunctionExpr) node() {}
353func (*Field) node() {}
354func (*OptionalField) node() {}
355func (*BulkOptionalField) node() {}
356func (*DynamicField) node() {}
357func (*Ellipsis) node() {}
358func (*ForClause) node() {}
359func (*IfClause) node() {}
360func (*LetClause) node() {}
361func (*ValueClause) node() {}