|
| 1 | +// Copyright 2023 Google LLC |
| 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 | +#ifndef THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |
| 16 | +#define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | +#include <utility> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "absl/strings/string_view.h" |
| 24 | +#include "absl/types/span.h" |
| 25 | +#include "common/kind.h" |
| 26 | + |
| 27 | +namespace cel { |
| 28 | + |
| 29 | +// Coarsely describes a function for the purpose of runtime resolution of |
| 30 | +// overloads. |
| 31 | +class FunctionDescriptor final { |
| 32 | + public: |
| 33 | + FunctionDescriptor(absl::string_view name, bool receiver_style, |
| 34 | + std::vector<Kind> types, bool is_strict = true) |
| 35 | + : impl_(std::make_shared<Impl>(name, receiver_style, std::move(types), |
| 36 | + is_strict)) {} |
| 37 | + |
| 38 | + // Function name. |
| 39 | + const std::string& name() const { return impl_->name; } |
| 40 | + |
| 41 | + // Whether function is receiver style i.e. true means arg0.name(args[1:]...). |
| 42 | + bool receiver_style() const { return impl_->receiver_style; } |
| 43 | + |
| 44 | + // The argmument types the function accepts. |
| 45 | + // |
| 46 | + // TODO: make this kinds |
| 47 | + const std::vector<Kind>& types() const { return impl_->types; } |
| 48 | + |
| 49 | + // if true (strict, default), error or unknown arguments are propagated |
| 50 | + // instead of calling the function. if false (non-strict), the function may |
| 51 | + // receive error or unknown values as arguments. |
| 52 | + bool is_strict() const { return impl_->is_strict; } |
| 53 | + |
| 54 | + // Helper for matching a descriptor. This tests that the shape is the same -- |
| 55 | + // |other| accepts the same number and types of arguments and is the same call |
| 56 | + // style). |
| 57 | + bool ShapeMatches(const FunctionDescriptor& other) const { |
| 58 | + return ShapeMatches(other.receiver_style(), other.types()); |
| 59 | + } |
| 60 | + bool ShapeMatches(bool receiver_style, absl::Span<const Kind> types) const; |
| 61 | + |
| 62 | + bool operator==(const FunctionDescriptor& other) const; |
| 63 | + |
| 64 | + bool operator<(const FunctionDescriptor& other) const; |
| 65 | + |
| 66 | + private: |
| 67 | + struct Impl final { |
| 68 | + Impl(absl::string_view name, bool receiver_style, std::vector<Kind> types, |
| 69 | + bool is_strict) |
| 70 | + : name(name), |
| 71 | + types(std::move(types)), |
| 72 | + receiver_style(receiver_style), |
| 73 | + is_strict(is_strict) {} |
| 74 | + |
| 75 | + std::string name; |
| 76 | + std::vector<Kind> types; |
| 77 | + bool receiver_style; |
| 78 | + bool is_strict; |
| 79 | + }; |
| 80 | + |
| 81 | + std::shared_ptr<const Impl> impl_; |
| 82 | +}; |
| 83 | + |
| 84 | +} // namespace cel |
| 85 | + |
| 86 | +#endif // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |
0 commit comments