/Users/deen/code/yugabyte-db/src/yb/bfpg/bfpg_template.h
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // This module defines a few templates to be used in file "bfpg.h". It defines the actual |
16 | | // implementation for compilation and execution of a builtin call. |
17 | | //-------------------------------------------------------------------------------------------------- |
18 | | |
19 | | #ifndef YB_BFPG_BFPG_TEMPLATE_H |
20 | | #define YB_BFPG_BFPG_TEMPLATE_H |
21 | | |
22 | | #include <fcntl.h> |
23 | | |
24 | | #include <functional> |
25 | | #include <vector> |
26 | | |
27 | | #include "yb/bfpg/gen_opcodes.h" |
28 | | #include "yb/bfpg/gen_operator.h" |
29 | | |
30 | | #include "yb/gutil/strings/substitute.h" |
31 | | |
32 | | #include "yb/util/logging.h" |
33 | | |
34 | | namespace yb { |
35 | | namespace bfpg { |
36 | | |
37 | | //-------------------------------------------------------------------------------------------------- |
38 | | // Find the builtin opcode, declaration, and return type for a builtin call. |
39 | | // Inputs: Builtin function name and parameter types. |
40 | | // Outputs: opcode and bfdecl. |
41 | | // In/Out parameter: return_type |
42 | | // If return_type is given, check if it is compatible with the declaration. |
43 | | // If not, return_type is an output parameter whose value is the return type of the builtin. |
44 | | Status FindOpcodeByType(const string& ql_name, |
45 | | const std::vector<DataType>& actual_types, |
46 | | BFOpcode *opcode, |
47 | | const BFDecl **bfdecl, |
48 | | DataType *return_type); |
49 | | |
50 | | // The effect is the same as function "FindOpcodeByType()", but it takes arguments instead types. |
51 | | // NOTE: |
52 | | // RTypePtr can be either a raw (*) or shared (const shared_ptr&) pointer. |
53 | | // PTypePtrCollection can be any standard collection of PType raw or shared pointer. |
54 | | // std::vector<PTypePtr>, std::list<PTypePtr>, std::set<PTypePtr>, ... |
55 | | template<typename PTypePtrCollection, typename RTypePtr> |
56 | | Status FindOpcode(const string& ql_name, |
57 | | const PTypePtrCollection& params, |
58 | | BFOpcode *opcode, |
59 | | const BFDecl **bfdecl, |
60 | 22 | RTypePtr result) { |
61 | | |
62 | | // Read argument types. |
63 | 22 | std::vector<DataType> actual_types(params.size(), DataType::UNKNOWN_DATA); |
64 | 22 | int pindex = 0; |
65 | 40 | for (const auto& param : params) { |
66 | 40 | actual_types[pindex] = param->ql_type_id(); |
67 | 40 | pindex++; |
68 | 40 | } |
69 | | |
70 | | // Get the opcode and declaration. |
71 | 22 | if (result == nullptr) { |
72 | 0 | return FindOpcodeByType(ql_name, actual_types, opcode, bfdecl, nullptr); |
73 | 0 | } |
74 | | |
75 | | // Get the opcode, declaration, and return type. |
76 | 22 | DataType return_type = result->ql_type_id(); |
77 | 22 | RETURN_NOT_OK(FindOpcodeByType(ql_name, actual_types, opcode, bfdecl, &return_type)); |
78 | 13 | result->set_ql_type_id(return_type); |
79 | | |
80 | 13 | return Status::OK(); |
81 | 22 | } |
82 | | } // namespace bfpg |
83 | | } // namespace yb |
84 | | |
85 | | #endif // YB_BFPG_BFPG_TEMPLATE_H |