2023-05-29 21:03:29 +00:00
|
|
|
// Copyright 2018 Google LLC
|
|
|
|
//
|
|
|
|
// 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 checker defines functions to type-checked a parsed expression
|
|
|
|
// against a set of identifier and function declarations.
|
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-19 08:01:33 +00:00
|
|
|
"reflect"
|
2023-05-29 21:03:29 +00:00
|
|
|
|
|
|
|
"github.com/google/cel-go/common"
|
2023-12-18 20:31:00 +00:00
|
|
|
"github.com/google/cel-go/common/ast"
|
2023-05-29 21:03:29 +00:00
|
|
|
"github.com/google/cel-go/common/containers"
|
2023-12-18 20:31:00 +00:00
|
|
|
"github.com/google/cel-go/common/decls"
|
2023-08-17 05:15:28 +00:00
|
|
|
"github.com/google/cel-go/common/operators"
|
2023-12-18 20:31:00 +00:00
|
|
|
"github.com/google/cel-go/common/types"
|
2024-08-19 08:01:33 +00:00
|
|
|
"github.com/google/cel-go/common/types/ref"
|
2023-05-29 21:03:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type checker struct {
|
2024-08-19 08:01:33 +00:00
|
|
|
*ast.AST
|
|
|
|
ast.ExprFactory
|
2023-05-29 21:03:29 +00:00
|
|
|
env *Env
|
|
|
|
errors *typeErrors
|
|
|
|
mappings *mapping
|
|
|
|
freeTypeVarCounter int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check performs type checking, giving a typed AST.
|
2024-08-19 08:01:33 +00:00
|
|
|
//
|
|
|
|
// The input is a parsed AST and an env which encapsulates type binding of variables,
|
|
|
|
// declarations of built-in functions, descriptions of protocol buffers, and a registry for
|
|
|
|
// errors.
|
|
|
|
//
|
|
|
|
// Returns a type-checked AST, which might not be usable if there are errors in the error
|
|
|
|
// registry.
|
|
|
|
func Check(parsed *ast.AST, source common.Source, env *Env) (*ast.AST, *common.Errors) {
|
2023-12-18 20:31:00 +00:00
|
|
|
errs := common.NewErrors(source)
|
2024-08-19 08:01:33 +00:00
|
|
|
typeMap := make(map[int64]*types.Type)
|
|
|
|
refMap := make(map[int64]*ast.ReferenceInfo)
|
2023-05-29 21:03:29 +00:00
|
|
|
c := checker{
|
2024-08-19 08:01:33 +00:00
|
|
|
AST: ast.NewCheckedAST(parsed, typeMap, refMap),
|
|
|
|
ExprFactory: ast.NewExprFactory(),
|
2023-05-29 21:03:29 +00:00
|
|
|
env: env,
|
2023-12-18 20:31:00 +00:00
|
|
|
errors: &typeErrors{errs: errs},
|
2023-05-29 21:03:29 +00:00
|
|
|
mappings: newMapping(),
|
|
|
|
freeTypeVarCounter: 0,
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
c.check(c.Expr())
|
2023-05-29 21:03:29 +00:00
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
// Walk over the final type map substituting any type parameters either by their bound value
|
|
|
|
// or by DYN.
|
|
|
|
for id, t := range c.TypeMap() {
|
|
|
|
c.SetType(id, substitute(c.mappings, t, true))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
return c.AST, errs
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) check(e ast.Expr) {
|
2023-05-29 21:03:29 +00:00
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
switch e.Kind() {
|
|
|
|
case ast.LiteralKind:
|
|
|
|
literal := ref.Val(e.AsLiteral())
|
|
|
|
switch literal.Type() {
|
|
|
|
case types.BoolType, types.BytesType, types.DoubleType, types.IntType,
|
|
|
|
types.NullType, types.StringType, types.UintType:
|
|
|
|
c.setType(e, literal.Type().(*types.Type))
|
|
|
|
default:
|
|
|
|
c.errors.unexpectedASTType(e.ID(), c.location(e), "literal", literal.Type().TypeName())
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.IdentKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkIdent(e)
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.SelectKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkSelect(e)
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.CallKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkCall(e)
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.ListKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkCreateList(e)
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.MapKind:
|
|
|
|
c.checkCreateMap(e)
|
|
|
|
case ast.StructKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkCreateStruct(e)
|
2024-08-19 08:01:33 +00:00
|
|
|
case ast.ComprehensionKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
c.checkComprehension(e)
|
|
|
|
default:
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.unexpectedASTType(e.ID(), c.location(e), "unspecified", reflect.TypeOf(e).Name())
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkIdent(e ast.Expr) {
|
|
|
|
identName := e.AsIdent()
|
2023-05-29 21:03:29 +00:00
|
|
|
// Check to see if the identifier is declared.
|
2024-08-19 08:01:33 +00:00
|
|
|
if ident := c.env.LookupIdent(identName); ident != nil {
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, ident.Type())
|
|
|
|
c.setReference(e, ast.NewIdentReference(ident.Name(), ident.Value()))
|
2023-05-29 21:03:29 +00:00
|
|
|
// Overwrite the identifier with its fully qualified name.
|
2024-08-19 08:01:33 +00:00
|
|
|
e.SetKindCase(c.NewIdent(e.ID(), ident.Name()))
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.ErrorType)
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.undeclaredReference(e.ID(), c.location(e), c.env.container.Name(), identName)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkSelect(e ast.Expr) {
|
|
|
|
sel := e.AsSelect()
|
2023-05-29 21:03:29 +00:00
|
|
|
// Before traversing down the tree, try to interpret as qualified name.
|
|
|
|
qname, found := containers.ToQualifiedName(e)
|
|
|
|
if found {
|
|
|
|
ident := c.env.LookupIdent(qname)
|
|
|
|
if ident != nil {
|
|
|
|
// We don't check for a TestOnly expression here since the `found` result is
|
|
|
|
// always going to be false for TestOnly expressions.
|
|
|
|
|
|
|
|
// Rewrite the node to be a variable reference to the resolved fully-qualified
|
|
|
|
// variable name.
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, ident.Type())
|
|
|
|
c.setReference(e, ast.NewIdentReference(ident.Name(), ident.Value()))
|
2024-08-19 08:01:33 +00:00
|
|
|
e.SetKindCase(c.NewIdent(e.ID(), ident.Name()))
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
resultType := c.checkSelectField(e, sel.Operand(), sel.FieldName(), false)
|
|
|
|
if sel.IsTestOnly() {
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = types.BoolType
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
|
|
|
c.setType(e, substitute(c.mappings, resultType, false))
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkOptSelect(e ast.Expr) {
|
2023-08-17 05:15:28 +00:00
|
|
|
// Collect metadata related to the opt select call packaged by the parser.
|
2024-08-19 08:01:33 +00:00
|
|
|
call := e.AsCall()
|
|
|
|
operand := call.Args()[0]
|
|
|
|
field := call.Args()[1]
|
2023-08-17 05:15:28 +00:00
|
|
|
fieldName, isString := maybeUnwrapString(field)
|
|
|
|
if !isString {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.notAnOptionalFieldSelection(field.ID(), c.location(field), field)
|
2023-08-17 05:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform type-checking using the field selection logic.
|
|
|
|
resultType := c.checkSelectField(e, operand, fieldName, true)
|
|
|
|
c.setType(e, substitute(c.mappings, resultType, false))
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setReference(e, ast.NewFunctionReference("select_optional_field"))
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkSelectField(e, operand ast.Expr, field string, optional bool) *types.Type {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Interpret as field selection, first traversing down the operand.
|
2023-08-17 05:15:28 +00:00
|
|
|
c.check(operand)
|
|
|
|
operandType := substitute(c.mappings, c.getType(operand), false)
|
|
|
|
|
|
|
|
// If the target type is 'optional', unwrap it for the sake of this check.
|
|
|
|
targetType, isOpt := maybeUnwrapOptional(operandType)
|
|
|
|
|
2023-05-29 21:03:29 +00:00
|
|
|
// Assume error type by default as most types do not support field selection.
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType := types.ErrorType
|
|
|
|
switch targetType.Kind() {
|
|
|
|
case types.MapKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
// Maps yield their value type as the selection result type.
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = targetType.Parameters()[1]
|
|
|
|
case types.StructKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
// Objects yield their field type declaration as the selection result type, but only if
|
|
|
|
// the field is defined.
|
|
|
|
messageType := targetType
|
2024-08-19 08:01:33 +00:00
|
|
|
if fieldType, found := c.lookupFieldType(e.ID(), messageType.TypeName(), field); found {
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = fieldType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
case types.TypeParamKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
// Set the operand type to DYN to prevent assignment to a potentially incorrect type
|
|
|
|
// at a later point in type-checking. The isAssignable call will update the type
|
|
|
|
// substitutions for the type param under the covers.
|
2023-12-18 20:31:00 +00:00
|
|
|
c.isAssignable(types.DynType, targetType)
|
2023-05-29 21:03:29 +00:00
|
|
|
// Also, set the result type to DYN.
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = types.DynType
|
2023-05-29 21:03:29 +00:00
|
|
|
default:
|
|
|
|
// Dynamic / error values are treated as DYN type. Errors are handled this way as well
|
|
|
|
// in order to allow forward progress on the check.
|
2023-08-17 05:15:28 +00:00
|
|
|
if !isDynOrError(targetType) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeDoesNotSupportFieldSelection(e.ID(), c.location(e), targetType)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = types.DynType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-08-17 05:15:28 +00:00
|
|
|
|
|
|
|
// If the target type was optional coming in, then the result must be optional going out.
|
|
|
|
if isOpt || optional {
|
2023-12-18 20:31:00 +00:00
|
|
|
return types.NewOptionalType(resultType)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-08-17 05:15:28 +00:00
|
|
|
return resultType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkCall(e ast.Expr) {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Note: similar logic exists within the `interpreter/planner.go`. If making changes here
|
|
|
|
// please consider the impact on planner.go and consolidate implementations or mirror code
|
|
|
|
// as appropriate.
|
2024-08-19 08:01:33 +00:00
|
|
|
call := e.AsCall()
|
|
|
|
fnName := call.FunctionName()
|
2023-08-17 05:15:28 +00:00
|
|
|
if fnName == operators.OptSelect {
|
|
|
|
c.checkOptSelect(e)
|
|
|
|
return
|
|
|
|
}
|
2023-05-29 21:03:29 +00:00
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
args := call.Args()
|
2023-05-29 21:03:29 +00:00
|
|
|
// Traverse arguments.
|
|
|
|
for _, arg := range args {
|
|
|
|
c.check(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular static call with simple name.
|
2024-08-19 08:01:33 +00:00
|
|
|
if !call.IsMemberFunction() {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Check for the existence of the function.
|
|
|
|
fn := c.env.LookupFunction(fnName)
|
|
|
|
if fn == nil {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.undeclaredReference(e.ID(), c.location(e), c.env.container.Name(), fnName)
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.ErrorType)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Overwrite the function name with its fully qualified resolved name.
|
2024-08-19 08:01:33 +00:00
|
|
|
e.SetKindCase(c.NewCall(e.ID(), fn.Name(), args...))
|
2023-05-29 21:03:29 +00:00
|
|
|
// Check to see whether the overload resolves.
|
2023-12-18 20:31:00 +00:00
|
|
|
c.resolveOverloadOrError(e, fn, nil, args)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a receiver 'target' is present, it may either be a receiver function, or a namespaced
|
|
|
|
// function, but not both. Given a.b.c() either a.b.c is a function or c is a function with
|
|
|
|
// target a.b.
|
|
|
|
//
|
|
|
|
// Check whether the target is a namespaced function name.
|
2024-08-19 08:01:33 +00:00
|
|
|
target := call.Target()
|
2023-05-29 21:03:29 +00:00
|
|
|
qualifiedPrefix, maybeQualified := containers.ToQualifiedName(target)
|
|
|
|
if maybeQualified {
|
|
|
|
maybeQualifiedName := qualifiedPrefix + "." + fnName
|
|
|
|
fn := c.env.LookupFunction(maybeQualifiedName)
|
|
|
|
if fn != nil {
|
|
|
|
// The function name is namespaced and so preserving the target operand would
|
|
|
|
// be an inaccurate representation of the desired evaluation behavior.
|
|
|
|
// Overwrite with fully-qualified resolved function name sans receiver target.
|
2024-08-19 08:01:33 +00:00
|
|
|
e.SetKindCase(c.NewCall(e.ID(), fn.Name(), args...))
|
2023-12-18 20:31:00 +00:00
|
|
|
c.resolveOverloadOrError(e, fn, nil, args)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular instance call.
|
2024-08-19 08:01:33 +00:00
|
|
|
c.check(target)
|
2023-05-29 21:03:29 +00:00
|
|
|
fn := c.env.LookupFunction(fnName)
|
|
|
|
// Function found, attempt overload resolution.
|
|
|
|
if fn != nil {
|
2023-12-18 20:31:00 +00:00
|
|
|
c.resolveOverloadOrError(e, fn, target, args)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Function name not declared, record error.
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.ErrorType)
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.undeclaredReference(e.ID(), c.location(e), c.env.container.Name(), fnName)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) resolveOverloadOrError(
|
2024-08-19 08:01:33 +00:00
|
|
|
e ast.Expr, fn *decls.FunctionDecl, target ast.Expr, args []ast.Expr) {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Attempt to resolve the overload.
|
2023-12-18 20:31:00 +00:00
|
|
|
resolution := c.resolveOverload(e, fn, target, args)
|
2023-05-29 21:03:29 +00:00
|
|
|
// No such overload, error noted in the resolveOverload call, type recorded here.
|
|
|
|
if resolution == nil {
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.ErrorType)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Overload found.
|
|
|
|
c.setType(e, resolution.Type)
|
|
|
|
c.setReference(e, resolution.Reference)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) resolveOverload(
|
2024-08-19 08:01:33 +00:00
|
|
|
call ast.Expr, fn *decls.FunctionDecl, target ast.Expr, args []ast.Expr) *overloadResolution {
|
2023-05-29 21:03:29 +00:00
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
var argTypes []*types.Type
|
2023-05-29 21:03:29 +00:00
|
|
|
if target != nil {
|
|
|
|
argTypes = append(argTypes, c.getType(target))
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
argTypes = append(argTypes, c.getType(arg))
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
var resultType *types.Type
|
|
|
|
var checkedRef *ast.ReferenceInfo
|
|
|
|
for _, overload := range fn.OverloadDecls() {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Determine whether the overload is currently considered.
|
2023-12-18 20:31:00 +00:00
|
|
|
if c.env.isOverloadDisabled(overload.ID()) {
|
2023-05-29 21:03:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the call style for the overload matches.
|
2023-12-18 20:31:00 +00:00
|
|
|
if (target == nil && overload.IsMemberFunction()) ||
|
|
|
|
(target != nil && !overload.IsMemberFunction()) {
|
2023-05-29 21:03:29 +00:00
|
|
|
// not a compatible call style.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
// Alternative type-checking behavior when the logical operators are compacted into
|
|
|
|
// variadic AST representations.
|
|
|
|
if fn.Name() == operators.LogicalAnd || fn.Name() == operators.LogicalOr {
|
|
|
|
checkedRef = ast.NewFunctionReference(overload.ID())
|
|
|
|
for i, argType := range argTypes {
|
|
|
|
if !c.isAssignable(argType, types.BoolType) {
|
|
|
|
c.errors.typeMismatch(
|
2024-08-19 08:01:33 +00:00
|
|
|
args[i].ID(),
|
|
|
|
c.locationByID(args[i].ID()),
|
2023-12-18 20:31:00 +00:00
|
|
|
types.BoolType,
|
|
|
|
argType)
|
|
|
|
resultType = types.ErrorType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isError(resultType) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newResolution(checkedRef, types.BoolType)
|
|
|
|
}
|
|
|
|
|
|
|
|
overloadType := newFunctionType(overload.ResultType(), overload.ArgTypes()...)
|
|
|
|
typeParams := overload.TypeParams()
|
|
|
|
if len(typeParams) != 0 {
|
2023-05-29 21:03:29 +00:00
|
|
|
// Instantiate overload's type with fresh type variables.
|
|
|
|
substitutions := newMapping()
|
2023-12-18 20:31:00 +00:00
|
|
|
for _, typePar := range typeParams {
|
|
|
|
substitutions.add(types.NewTypeParamType(typePar), c.newTypeVar())
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
overloadType = substitute(substitutions, overloadType, false)
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
candidateArgTypes := overloadType.Parameters()[1:]
|
2023-05-29 21:03:29 +00:00
|
|
|
if c.isAssignableList(argTypes, candidateArgTypes) {
|
|
|
|
if checkedRef == nil {
|
2023-12-18 20:31:00 +00:00
|
|
|
checkedRef = ast.NewFunctionReference(overload.ID())
|
2023-05-29 21:03:29 +00:00
|
|
|
} else {
|
2023-12-18 20:31:00 +00:00
|
|
|
checkedRef.AddOverload(overload.ID())
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// First matching overload, determines result type.
|
2023-12-18 20:31:00 +00:00
|
|
|
fnResultType := substitute(c.mappings, overloadType.Parameters()[0], false)
|
2023-05-29 21:03:29 +00:00
|
|
|
if resultType == nil {
|
|
|
|
resultType = fnResultType
|
2023-12-18 20:31:00 +00:00
|
|
|
} else if !isDyn(resultType) && !fnResultType.IsExactType(resultType) {
|
|
|
|
resultType = types.DynType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resultType == nil {
|
2023-12-18 20:31:00 +00:00
|
|
|
for i, argType := range argTypes {
|
|
|
|
argTypes[i] = substitute(c.mappings, argType, true)
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.noMatchingOverload(call.ID(), c.location(call), fn.Name(), argTypes, target != nil)
|
2023-05-29 21:03:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return newResolution(checkedRef, resultType)
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkCreateList(e ast.Expr) {
|
|
|
|
create := e.AsList()
|
2023-12-18 20:31:00 +00:00
|
|
|
var elemsType *types.Type
|
2024-08-19 08:01:33 +00:00
|
|
|
optionalIndices := create.OptionalIndices()
|
2023-08-17 05:15:28 +00:00
|
|
|
optionals := make(map[int32]bool, len(optionalIndices))
|
|
|
|
for _, optInd := range optionalIndices {
|
|
|
|
optionals[optInd] = true
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
for i, e := range create.Elements() {
|
2023-05-29 21:03:29 +00:00
|
|
|
c.check(e)
|
2023-08-17 05:15:28 +00:00
|
|
|
elemType := c.getType(e)
|
|
|
|
if optionals[int32(i)] {
|
|
|
|
var isOptional bool
|
|
|
|
elemType, isOptional = maybeUnwrapOptional(elemType)
|
|
|
|
if !isOptional && !isDyn(elemType) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeMismatch(e.ID(), c.location(e), types.NewOptionalType(elemType), elemType)
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
elemsType = c.joinTypes(e, elemsType, elemType)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-08-17 05:15:28 +00:00
|
|
|
if elemsType == nil {
|
2023-05-29 21:03:29 +00:00
|
|
|
// If the list is empty, assign free type var to elem type.
|
2023-08-17 05:15:28 +00:00
|
|
|
elemsType = c.newTypeVar()
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.NewListType(elemsType))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkCreateMap(e ast.Expr) {
|
|
|
|
mapVal := e.AsMap()
|
2023-12-18 20:31:00 +00:00
|
|
|
var mapKeyType *types.Type
|
|
|
|
var mapValueType *types.Type
|
2024-08-19 08:01:33 +00:00
|
|
|
for _, e := range mapVal.Entries() {
|
|
|
|
entry := e.AsMapEntry()
|
|
|
|
key := entry.Key()
|
2023-05-29 21:03:29 +00:00
|
|
|
c.check(key)
|
2023-12-18 20:31:00 +00:00
|
|
|
mapKeyType = c.joinTypes(key, mapKeyType, c.getType(key))
|
2023-08-17 05:15:28 +00:00
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
val := entry.Value()
|
2023-08-17 05:15:28 +00:00
|
|
|
c.check(val)
|
|
|
|
valType := c.getType(val)
|
2024-08-19 08:01:33 +00:00
|
|
|
if entry.IsOptional() {
|
2023-08-17 05:15:28 +00:00
|
|
|
var isOptional bool
|
|
|
|
valType, isOptional = maybeUnwrapOptional(valType)
|
|
|
|
if !isOptional && !isDyn(valType) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeMismatch(val.ID(), c.location(val), types.NewOptionalType(valType), valType)
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
mapValueType = c.joinTypes(val, mapValueType, valType)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-08-17 05:15:28 +00:00
|
|
|
if mapKeyType == nil {
|
2023-05-29 21:03:29 +00:00
|
|
|
// If the map is empty, assign free type variables to typeKey and value type.
|
2023-08-17 05:15:28 +00:00
|
|
|
mapKeyType = c.newTypeVar()
|
|
|
|
mapValueType = c.newTypeVar()
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.NewMapType(mapKeyType, mapValueType))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkCreateStruct(e ast.Expr) {
|
|
|
|
msgVal := e.AsStruct()
|
2023-05-29 21:03:29 +00:00
|
|
|
// Determine the type of the message.
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType := types.ErrorType
|
2024-08-19 08:01:33 +00:00
|
|
|
ident := c.env.LookupIdent(msgVal.TypeName())
|
2023-12-18 20:31:00 +00:00
|
|
|
if ident == nil {
|
2023-05-29 21:03:29 +00:00
|
|
|
c.errors.undeclaredReference(
|
2024-08-19 08:01:33 +00:00
|
|
|
e.ID(), c.location(e), c.env.container.Name(), msgVal.TypeName())
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, types.ErrorType)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Ensure the type name is fully qualified in the AST.
|
2023-12-18 20:31:00 +00:00
|
|
|
typeName := ident.Name()
|
2024-08-19 08:01:33 +00:00
|
|
|
if msgVal.TypeName() != typeName {
|
|
|
|
e.SetKindCase(c.NewStruct(e.ID(), typeName, msgVal.Fields()))
|
|
|
|
msgVal = e.AsStruct()
|
|
|
|
}
|
|
|
|
c.setReference(e, ast.NewIdentReference(typeName, nil))
|
2023-12-18 20:31:00 +00:00
|
|
|
identKind := ident.Type().Kind()
|
|
|
|
if identKind != types.ErrorKind {
|
|
|
|
if identKind != types.TypeKind {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.notAType(e.ID(), c.location(e), ident.Type().DeclaredTypeName())
|
2023-05-29 21:03:29 +00:00
|
|
|
} else {
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = ident.Type().Parameters()[0]
|
|
|
|
// Backwards compatibility test between well-known types and message types
|
|
|
|
// In this context, the type is being instantiated by its protobuf name which
|
|
|
|
// is not ideal or recommended, but some users expect this to work.
|
|
|
|
if isWellKnownType(resultType) {
|
|
|
|
typeName = getWellKnownTypeName(resultType)
|
|
|
|
} else if resultType.Kind() == types.StructKind {
|
|
|
|
typeName = resultType.DeclaredTypeName()
|
|
|
|
} else {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.notAMessageType(e.ID(), c.location(e), resultType.DeclaredTypeName())
|
2023-12-18 20:31:00 +00:00
|
|
|
resultType = types.ErrorType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
c.setType(e, resultType)
|
2023-05-29 21:03:29 +00:00
|
|
|
|
|
|
|
// Check the field initializers.
|
2024-08-19 08:01:33 +00:00
|
|
|
for _, f := range msgVal.Fields() {
|
|
|
|
field := f.AsStructField()
|
|
|
|
fieldName := field.Name()
|
|
|
|
value := field.Value()
|
2023-05-29 21:03:29 +00:00
|
|
|
c.check(value)
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
fieldType := types.ErrorType
|
2024-08-19 08:01:33 +00:00
|
|
|
ft, found := c.lookupFieldType(f.ID(), typeName, fieldName)
|
2023-08-17 05:15:28 +00:00
|
|
|
if found {
|
2023-12-18 20:31:00 +00:00
|
|
|
fieldType = ft
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valType := c.getType(value)
|
2024-08-19 08:01:33 +00:00
|
|
|
if field.IsOptional() {
|
2023-08-17 05:15:28 +00:00
|
|
|
var isOptional bool
|
|
|
|
valType, isOptional = maybeUnwrapOptional(valType)
|
|
|
|
if !isOptional && !isDyn(valType) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeMismatch(value.ID(), c.location(value), types.NewOptionalType(valType), valType)
|
2023-08-17 05:15:28 +00:00
|
|
|
}
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-08-17 05:15:28 +00:00
|
|
|
if !c.isAssignable(fieldType, valType) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.fieldTypeMismatch(f.ID(), c.locationByID(f.ID()), fieldName, fieldType, valType)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) checkComprehension(e ast.Expr) {
|
|
|
|
comp := e.AsComprehension()
|
|
|
|
c.check(comp.IterRange())
|
|
|
|
c.check(comp.AccuInit())
|
|
|
|
accuType := c.getType(comp.AccuInit())
|
|
|
|
rangeType := substitute(c.mappings, c.getType(comp.IterRange()), false)
|
2023-12-18 20:31:00 +00:00
|
|
|
var varType *types.Type
|
2023-05-29 21:03:29 +00:00
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
switch rangeType.Kind() {
|
|
|
|
case types.ListKind:
|
|
|
|
varType = rangeType.Parameters()[0]
|
|
|
|
case types.MapKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
// Ranges over the keys.
|
2023-12-18 20:31:00 +00:00
|
|
|
varType = rangeType.Parameters()[0]
|
|
|
|
case types.DynKind, types.ErrorKind, types.TypeParamKind:
|
2023-05-29 21:03:29 +00:00
|
|
|
// Set the range type to DYN to prevent assignment to a potentially incorrect type
|
|
|
|
// at a later point in type-checking. The isAssignable call will update the type
|
|
|
|
// substitutions for the type param under the covers.
|
2023-12-18 20:31:00 +00:00
|
|
|
c.isAssignable(types.DynType, rangeType)
|
2023-05-29 21:03:29 +00:00
|
|
|
// Set the range iteration variable to type DYN as well.
|
2023-12-18 20:31:00 +00:00
|
|
|
varType = types.DynType
|
2023-05-29 21:03:29 +00:00
|
|
|
default:
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.notAComprehensionRange(comp.IterRange().ID(), c.location(comp.IterRange()), rangeType)
|
2023-12-18 20:31:00 +00:00
|
|
|
varType = types.ErrorType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a scope for the comprehension since it has a local accumulation variable.
|
|
|
|
// This scope will contain the accumulation variable used to compute the result.
|
|
|
|
c.env = c.env.enterScope()
|
2024-08-19 08:01:33 +00:00
|
|
|
c.env.AddIdents(decls.NewVariable(comp.AccuVar(), accuType))
|
2023-05-29 21:03:29 +00:00
|
|
|
// Create a block scope for the loop.
|
|
|
|
c.env = c.env.enterScope()
|
2024-08-19 08:01:33 +00:00
|
|
|
c.env.AddIdents(decls.NewVariable(comp.IterVar(), varType))
|
2023-05-29 21:03:29 +00:00
|
|
|
// Check the variable references in the condition and step.
|
2024-08-19 08:01:33 +00:00
|
|
|
c.check(comp.LoopCondition())
|
|
|
|
c.assertType(comp.LoopCondition(), types.BoolType)
|
|
|
|
c.check(comp.LoopStep())
|
|
|
|
c.assertType(comp.LoopStep(), accuType)
|
2023-05-29 21:03:29 +00:00
|
|
|
// Exit the loop's block scope before checking the result.
|
|
|
|
c.env = c.env.exitScope()
|
2024-08-19 08:01:33 +00:00
|
|
|
c.check(comp.Result())
|
2023-05-29 21:03:29 +00:00
|
|
|
// Exit the comprehension scope.
|
|
|
|
c.env = c.env.exitScope()
|
2024-08-19 08:01:33 +00:00
|
|
|
c.setType(e, substitute(c.mappings, c.getType(comp.Result()), false))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks compatibility of joined types, and returns the most general common type.
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) joinTypes(e ast.Expr, previous, current *types.Type) *types.Type {
|
2023-05-29 21:03:29 +00:00
|
|
|
if previous == nil {
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
if c.isAssignable(previous, current) {
|
|
|
|
return mostGeneral(previous, current)
|
|
|
|
}
|
|
|
|
if c.dynAggregateLiteralElementTypesEnabled() {
|
2023-12-18 20:31:00 +00:00
|
|
|
return types.DynType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeMismatch(e.ID(), c.location(e), previous, current)
|
2023-12-18 20:31:00 +00:00
|
|
|
return types.ErrorType
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) dynAggregateLiteralElementTypesEnabled() bool {
|
|
|
|
return c.env.aggLitElemType == dynElementType
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func (c *checker) newTypeVar() *types.Type {
|
2023-05-29 21:03:29 +00:00
|
|
|
id := c.freeTypeVarCounter
|
|
|
|
c.freeTypeVarCounter++
|
2023-12-18 20:31:00 +00:00
|
|
|
return types.NewTypeParamType(fmt.Sprintf("_var%d", id))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func (c *checker) isAssignable(t1, t2 *types.Type) bool {
|
2023-05-29 21:03:29 +00:00
|
|
|
subs := isAssignable(c.mappings, t1, t2)
|
|
|
|
if subs != nil {
|
|
|
|
c.mappings = subs
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func (c *checker) isAssignableList(l1, l2 []*types.Type) bool {
|
2023-05-29 21:03:29 +00:00
|
|
|
subs := isAssignableList(c.mappings, l1, l2)
|
|
|
|
if subs != nil {
|
|
|
|
c.mappings = subs
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func maybeUnwrapString(e ast.Expr) (string, bool) {
|
|
|
|
switch e.Kind() {
|
|
|
|
case ast.LiteralKind:
|
|
|
|
literal := e.AsLiteral()
|
|
|
|
switch v := literal.(type) {
|
|
|
|
case types.String:
|
|
|
|
return string(v), true
|
2023-12-18 20:31:00 +00:00
|
|
|
}
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
return "", false
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) setType(e ast.Expr, t *types.Type) {
|
|
|
|
if old, found := c.TypeMap()[e.ID()]; found && !old.IsExactType(t) {
|
|
|
|
c.errors.incompatibleType(e.ID(), c.location(e), e, old, t)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
c.SetType(e.ID(), t)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) getType(e ast.Expr) *types.Type {
|
|
|
|
return c.TypeMap()[e.ID()]
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) setReference(e ast.Expr, r *ast.ReferenceInfo) {
|
|
|
|
if old, found := c.ReferenceMap()[e.ID()]; found && !old.Equals(r) {
|
|
|
|
c.errors.referenceRedefinition(e.ID(), c.location(e), e, old, r)
|
2023-05-29 21:03:29 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-19 08:01:33 +00:00
|
|
|
c.SetReference(e.ID(), r)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) assertType(e ast.Expr, t *types.Type) {
|
2023-05-29 21:03:29 +00:00
|
|
|
if !c.isAssignable(t, c.getType(e)) {
|
2024-08-19 08:01:33 +00:00
|
|
|
c.errors.typeMismatch(e.ID(), c.location(e), t, c.getType(e))
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type overloadResolution struct {
|
2023-12-18 20:31:00 +00:00
|
|
|
Type *types.Type
|
|
|
|
Reference *ast.ReferenceInfo
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func newResolution(r *ast.ReferenceInfo, t *types.Type) *overloadResolution {
|
2023-05-29 21:03:29 +00:00
|
|
|
return &overloadResolution{
|
2023-12-18 20:31:00 +00:00
|
|
|
Reference: r,
|
2023-05-29 21:03:29 +00:00
|
|
|
Type: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 08:01:33 +00:00
|
|
|
func (c *checker) location(e ast.Expr) common.Location {
|
|
|
|
return c.locationByID(e.ID())
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) locationByID(id int64) common.Location {
|
2024-08-19 08:01:33 +00:00
|
|
|
return c.SourceInfo().GetStartLocation(id)
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func (c *checker) lookupFieldType(exprID int64, structType, fieldName string) (*types.Type, bool) {
|
|
|
|
if _, found := c.env.provider.FindStructType(structType); !found {
|
|
|
|
// This should not happen, anyway, report an error.
|
|
|
|
c.errors.unexpectedFailedResolution(exprID, c.locationByID(exprID), structType)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ft, found := c.env.provider.FindStructFieldType(structType, fieldName); found {
|
|
|
|
return ft.Type, found
|
|
|
|
}
|
|
|
|
|
|
|
|
c.errors.undefinedField(exprID, c.locationByID(exprID), fieldName)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWellKnownType(t *types.Type) bool {
|
|
|
|
switch t.Kind() {
|
|
|
|
case types.AnyKind, types.TimestampKind, types.DurationKind, types.DynKind, types.NullTypeKind:
|
|
|
|
return true
|
|
|
|
case types.BoolKind, types.BytesKind, types.DoubleKind, types.IntKind, types.StringKind, types.UintKind:
|
|
|
|
return t.IsAssignableType(types.NullType)
|
|
|
|
case types.ListKind:
|
|
|
|
return t.Parameters()[0] == types.DynType
|
|
|
|
case types.MapKind:
|
|
|
|
return t.Parameters()[0] == types.StringType && t.Parameters()[1] == types.DynType
|
|
|
|
}
|
|
|
|
return false
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 20:31:00 +00:00
|
|
|
func getWellKnownTypeName(t *types.Type) string {
|
|
|
|
if name, found := wellKnownTypes[t.Kind()]; found {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return ""
|
2023-05-29 21:03:29 +00:00
|
|
|
}
|
2023-12-18 20:31:00 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
wellKnownTypes = map[types.Kind]string{
|
|
|
|
types.AnyKind: "google.protobuf.Any",
|
|
|
|
types.BoolKind: "google.protobuf.BoolValue",
|
|
|
|
types.BytesKind: "google.protobuf.BytesValue",
|
|
|
|
types.DoubleKind: "google.protobuf.DoubleValue",
|
|
|
|
types.DurationKind: "google.protobuf.Duration",
|
|
|
|
types.DynKind: "google.protobuf.Value",
|
|
|
|
types.IntKind: "google.protobuf.Int64Value",
|
|
|
|
types.ListKind: "google.protobuf.ListValue",
|
|
|
|
types.NullTypeKind: "google.protobuf.NullValue",
|
|
|
|
types.MapKind: "google.protobuf.Struct",
|
|
|
|
types.StringKind: "google.protobuf.StringValue",
|
|
|
|
types.TimestampKind: "google.protobuf.Timestamp",
|
|
|
|
types.UintKind: "google.protobuf.UInt64Value",
|
|
|
|
}
|
|
|
|
)
|