Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gen.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Language-agnostic utilities for `Elemental` codegen.
15#ifndef OR_TOOLS_MATH_OPT_ELEMENTAL_CODEGEN_GEN_H_
16#define OR_TOOLS_MATH_OPT_ELEMENTAL_CODEGEN_GEN_H_
17
18#include <array>
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "absl/strings/string_view.h"
24#include "absl/types/span.h"
25
27
28// The list of attribute operations supported by `Elemental`.
38
39static constexpr int kNumAttrOps = static_cast<int>(AttrOp::kNumOps);
40
41// A struct to represent an attribute type descriptor during codegen.
43 // The attribute type name.
44 absl::string_view name;
45 // The value type of the attribute.
46 enum class ValueType {
50 };
52 // The number of key elements.
54 // The key symmetry.
55 std::string symmetry;
56
57 // The names of the attributes of this type.
58 std::vector<absl::string_view> attribute_names;
59};
60
61// Representations for types.
62class Type {
63 public:
64 // A named type, e.g. "double".
65 static std::shared_ptr<Type> Named(std::string name);
66 // A pointer type.
67 static std::shared_ptr<Type> Pointer(std::shared_ptr<Type> pointee);
68 // A placeholder for the attribute value type, which is yet unknown when types
69 // are defined. This gets replaced by `attr_value_type` when calling `Print`.
70 static std::shared_ptr<Type> AttrValueType();
71
72 virtual ~Type();
73
74 // Prints the type to `out`, replacing `AttrValueType` placeholders with
75 // `attr_value_type`.
76 virtual void Print(absl::string_view attr_value_type,
77 std::string* out) const = 0;
78};
79
80// Information about how to codegen a given `AttrOp` in a given language.
82 // The return type of the function.
83 std::shared_ptr<Type> return_type;
84
85 // If true, the function has an `AttrKey` parameter.
87
88 // Extra parameters (e.g. {"double", "value"} for `Set` operations).
90 std::shared_ptr<Type> type;
91 std::string name;
92 };
93 std::vector<ExtraParameter> extra_parameters;
94};
95
96using AttrOpFunctionInfos = std::array<AttrOpFunctionInfo, kNumAttrOps>;
97
98// The code generator interface.
100 public:
101 explicit CodeGenerator(const AttrOpFunctionInfos* attr_op_function_infos)
102 : attr_op_function_infos_(*attr_op_function_infos) {}
103
104 virtual ~CodeGenerator() = default;
105
106 // Generates code.
107 std::string GenerateCode() const;
108
109 // Emits the header for the generated code.
110 virtual void EmitHeader(std::string* out) const {}
111
112 // Emits code for elements.
113 virtual void EmitElements(absl::Span<const absl::string_view> elements,
114 std::string* out) const {}
115
116 // Emits code for attributes. By default, this iterates attributes and for
117 // each attribute:
118 // - calls `StartAttrType`, and
119 // - calls `EmitAttrOp` for each operation.
120 virtual void EmitAttributes(
121 absl::Span<const CodegenAttrTypeDescriptor> descriptors,
122 std::string* out) const;
123
124 // Called before generating code for an attribute type.
125 virtual void StartAttrType(const CodegenAttrTypeDescriptor& descriptor,
126 std::string* out) const {}
127
128 // Emits code for operation `info` for attribute described by `descriptor`.
129 virtual void EmitAttrOp(absl::string_view op_name,
130 const CodegenAttrTypeDescriptor& descriptor,
131 const AttrOpFunctionInfo& info,
132 std::string* out) const {}
133
134 private:
135 void EmitAttrType(const CodegenAttrTypeDescriptor& descriptor,
136 std::string* out) const;
137
138 const AttrOpFunctionInfos& attr_op_function_infos_;
139};
140
141} // namespace operations_research::math_opt::codegen
142
143#endif // OR_TOOLS_MATH_OPT_ELEMENTAL_CODEGEN_GEN_H_
std::string GenerateCode() const
Generates code.
Definition gen.cc:138
CodeGenerator(const AttrOpFunctionInfos *attr_op_function_infos)
Definition gen.h:101
virtual void EmitElements(absl::Span< const absl::string_view > elements, std::string *out) const
Emits code for elements.
Definition gen.h:113
virtual void StartAttrType(const CodegenAttrTypeDescriptor &descriptor, std::string *out) const
Called before generating code for an attribute type.
Definition gen.h:125
virtual void EmitAttributes(absl::Span< const CodegenAttrTypeDescriptor > descriptors, std::string *out) const
Definition gen.cc:127
virtual void EmitAttrOp(absl::string_view op_name, const CodegenAttrTypeDescriptor &descriptor, const AttrOpFunctionInfo &info, std::string *out) const
Emits code for operation info for attribute described by descriptor.
Definition gen.h:129
virtual void EmitHeader(std::string *out) const
Emits the header for the generated code.
Definition gen.h:110
Representations for types.
Definition gen.h:62
static std::shared_ptr< Type > Named(std::string name)
A named type, e.g. "double".
Definition gen.cc:68
static std::shared_ptr< Type > Pointer(std::shared_ptr< Type > pointee)
A pointer type.
Definition gen.cc:72
virtual void Print(absl::string_view attr_value_type, std::string *out) const =0
static std::shared_ptr< Type > AttrValueType()
Definition gen.cc:76
Language-agnostic utilities for Elemental codegen.
Definition codegen.cc:29
static constexpr int kNumAttrOps
Definition gen.h:39
AttrOp
The list of attribute operations supported by Elemental.
Definition gen.h:29
std::array< AttrOpFunctionInfo, kNumAttrOps > AttrOpFunctionInfos
Definition gen.h:96
Extra parameters (e.g. {"double", "value"} for Set operations).
Definition gen.h:89
Information about how to codegen a given AttrOp in a given language.
Definition gen.h:81
bool has_key_parameter
If true, the function has an AttrKey parameter.
Definition gen.h:86
std::shared_ptr< Type > return_type
The return type of the function.
Definition gen.h:83
std::vector< ExtraParameter > extra_parameters
Definition gen.h:93
A struct to represent an attribute type descriptor during codegen.
Definition gen.h:42
absl::string_view name
The attribute type name.
Definition gen.h:44
std::vector< absl::string_view > attribute_names
The names of the attributes of this type.
Definition gen.h:58