cuelang.org/go@v0.13.0/internal/core/adt/call.go (about)

     1  // Copyright 2025 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  //     https://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 adt
    16  
    17  import (
    18  	"cuelang.org/go/cue/ast"
    19  	"cuelang.org/go/cue/token"
    20  )
    21  
    22  // A CallContext holds all relevant information for a function call to
    23  // be executed.
    24  type CallContext struct {
    25  	ctx         *OpContext
    26  	call        *CallExpr
    27  	builtin     *Builtin
    28  	args        []Value
    29  	isValidator bool
    30  }
    31  
    32  func (c *CallContext) OpContext() *OpContext {
    33  	return c.ctx
    34  }
    35  
    36  func (c *CallContext) Pos() token.Pos {
    37  	var src ast.Node
    38  	switch {
    39  	case c.call != nil:
    40  		src = c.call.Source()
    41  	case c.builtin != nil:
    42  		src = c.builtin.Source()
    43  	}
    44  	if src != nil {
    45  		return src.Pos()
    46  	}
    47  	return token.NoPos
    48  }
    49  
    50  func (c *CallContext) Value(i int) Value {
    51  	return c.args[i]
    52  }
    53  
    54  // NumParams returns the total number of parameters to this function.
    55  func (c *CallContext) NumParams() int {
    56  	return len(c.args)
    57  }
    58  
    59  func (c *CallContext) AddPositions(err *ValueError) {
    60  	for _, v := range c.args {
    61  		err.AddPosition(v)
    62  	}
    63  }
    64  
    65  // Args return the pre-evaluated arguments. This function is only used for
    66  // transitioning and will be removed at some point. Use [CallContext.Value]
    67  // instead.
    68  func (c *CallContext) Args() []Value {
    69  	return c.args
    70  }
    71  
    72  // Expr returns the nth argument expression. The value is evaluated and any
    73  // cycle information is accumulated in the context. This allows cycles in
    74  // arguments to be detected.
    75  //
    76  // This method of getting an argument should be used when the argument is used
    77  // as a schema and may contain cycles.
    78  func (c *CallContext) Expr(i int) Value {
    79  	// If the call context represents a validator call, the argument will be
    80  	// offset by 1.
    81  	if c.isValidator {
    82  		if i == 0 {
    83  			c.Errf("Expr may not be called for 0th argument of validator")
    84  			return nil
    85  		}
    86  		i--
    87  	}
    88  	x := c.call.Args[i]
    89  
    90  	// Evaluated while keeping any cycle information in the context.
    91  	return c.ctx.EvaluateKeepState(x)
    92  }
    93  
    94  func (c *CallContext) Errf(format string, args ...interface{}) *Bottom {
    95  	return c.ctx.NewErrf(format, args...)
    96  }