1// Copyright 2017 Istio 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 ast
16
17import (
18	config "istio.io/api/policy/v1beta1"
19)
20
21// FunctionMetadata contains type metadata for functions.
22type FunctionMetadata struct {
23	// Name is the name of the function.
24	Name string
25
26	// Instance indicates that this is an instance method.
27	Instance bool
28
29	// TargetType is the type of the instance method target, if this function is an instance method.
30	TargetType config.ValueType
31
32	// ReturnType is the return type of the function.
33	ReturnType config.ValueType
34
35	// ArgumentTypes is the types of the arguments in the order that is expected by the function.
36	ArgumentTypes []config.ValueType
37}
38
39func intrinsics() []FunctionMetadata {
40	return []FunctionMetadata{
41		{
42			Name:          "EQ",
43			ReturnType:    config.BOOL,
44			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
45		},
46		{
47			Name:          "NEQ",
48			ReturnType:    config.BOOL,
49			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
50		},
51		{
52			Name:          "LT",
53			ReturnType:    config.BOOL,
54			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
55		},
56		{
57			Name:          "LEQ",
58			ReturnType:    config.BOOL,
59			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
60		},
61		{
62			Name:          "GT",
63			ReturnType:    config.BOOL,
64			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
65		},
66		{
67			Name:          "GEQ",
68			ReturnType:    config.BOOL,
69			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
70		},
71		{
72			Name:          "OR",
73			ReturnType:    config.VALUE_TYPE_UNSPECIFIED,
74			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
75		},
76		{
77			Name:          "LOR",
78			ReturnType:    config.BOOL,
79			ArgumentTypes: []config.ValueType{config.BOOL, config.BOOL},
80		},
81		{
82			Name:          "LAND",
83			ReturnType:    config.BOOL,
84			ArgumentTypes: []config.ValueType{config.BOOL, config.BOOL},
85		},
86		{
87			Name:          "INDEX",
88			ReturnType:    config.STRING,
89			ArgumentTypes: []config.ValueType{config.STRING_MAP, config.STRING},
90		},
91		{
92			Name:          "conditional",
93			ReturnType:    config.VALUE_TYPE_UNSPECIFIED,
94			ArgumentTypes: []config.ValueType{config.BOOL, config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
95		},
96		{
97			Name:          "ADD",
98			ReturnType:    config.VALUE_TYPE_UNSPECIFIED,
99			ArgumentTypes: []config.ValueType{config.VALUE_TYPE_UNSPECIFIED, config.VALUE_TYPE_UNSPECIFIED},
100		},
101		{
102			Name:          "size",
103			ReturnType:    config.INT64,
104			ArgumentTypes: []config.ValueType{config.STRING},
105		},
106	}
107}
108
109// FuncMap generates a full function map, combining the intrinsic functions needed for type-checking,
110// along with external functions that are supplied as the functions parameter.
111func FuncMap(functions []FunctionMetadata) map[string]FunctionMetadata {
112	m := make(map[string]FunctionMetadata)
113	for _, fn := range intrinsics() {
114		m[fn.Name] = fn
115	}
116	for _, fn := range functions {
117		m[fn.Name] = fn
118	}
119	return m
120}
121