Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gen.cc
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
15
16#include <cstdint>
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/strings/str_cat.h"
23#include "absl/strings/string_view.h"
24#include "absl/types/span.h"
28
30
31namespace {
32
33class NamedType : public Type {
34 public:
35 explicit NamedType(std::string name) : name_(std::move(name)) {}
36
37 void Print(absl::string_view, std::string* out) const final {
38 absl::StrAppend(out, name_);
39 }
40
41 private:
42 std::string name_;
43};
44
45class PointerType : public Type {
46 public:
47 explicit PointerType(std::shared_ptr<Type> pointee)
48 : pointee_(std::move(pointee)) {}
49
50 void Print(absl::string_view attr_value_type, std::string* out) const final {
51 pointee_->Print(attr_value_type, out);
52 absl::StrAppend(out, "*");
53 }
54
55 private:
56 std::shared_ptr<Type> pointee_;
57};
58
59class AttrValueTypeType : public Type {
60 public:
61 void Print(absl::string_view attr_value_type, std::string* out) const final {
62 absl::StrAppend(out, attr_value_type);
63 }
64};
65
66} // namespace
67
68std::shared_ptr<Type> Type::Named(std::string name) {
69 return std::make_shared<NamedType>(std::move(name));
70}
71
72std::shared_ptr<Type> Type::Pointer(std::shared_ptr<Type> pointee) {
73 return std::make_shared<PointerType>(std::move(pointee));
74}
75
76std::shared_ptr<Type> Type::AttrValueType() {
77 return std::make_shared<AttrValueTypeType>();
78}
79
80Type::~Type() = default;
81
85
89
93
94template <ElementType element_type>
99
100template <typename Descriptor>
102 CodegenAttrTypeDescriptor descriptor;
103 descriptor.value_type = GetValueType(typename Descriptor::ValueType{});
104 descriptor.name = Descriptor::kName;
105 descriptor.num_key_elements = Descriptor::kNumKeyElements;
106 descriptor.symmetry = Descriptor::Symmetry::GetName();
107
108 descriptor.attribute_names.reserve(Descriptor::NumAttrs());
109 for (const auto& attr_descriptor : Descriptor::kAttrDescriptors) {
110 descriptor.attribute_names.push_back(attr_descriptor.name);
111 }
112 return descriptor;
113}
114
115constexpr absl::string_view kOpNames[static_cast<int>(AttrOp::kNumOps)] = {
116 "Get", "Set", "IsNonDefault", "NumNonDefaults", "GetNonDefaults"};
117
118void CodeGenerator::EmitAttrType(const CodegenAttrTypeDescriptor& descriptor,
119 std::string* out) const {
120 StartAttrType(descriptor, out);
121 for (int op = 0; op < kNumAttrOps; ++op) {
122 const AttrOpFunctionInfo& op_info = attr_op_function_infos_[op];
123 EmitAttrOp(kOpNames[op], descriptor, op_info, out);
124 }
125}
126
128 absl::Span<const CodegenAttrTypeDescriptor> descriptors,
129 std::string* out) const {
130 for (const auto& descriptor : descriptors) {
131 StartAttrType(descriptor, out);
132 for (int i = 0; i < kNumAttrOps; ++i) {
133 EmitAttrOp(kOpNames[i], descriptor, attr_op_function_infos_[i], out);
134 }
135 }
136}
137
138std::string CodeGenerator::GenerateCode() const {
139 std::string out;
140 EmitHeader(&out);
141
142 // Generate elements.
144
145 // Generate attributes.
146 std::vector<CodegenAttrTypeDescriptor> attr_type_descriptors;
147 ForEach(
148 [&attr_type_descriptors](auto type_descriptor) {
149 attr_type_descriptors.push_back(
150 MakeAttrTypeDescriptor<decltype(type_descriptor)>());
151 },
153 EmitAttributes(attr_type_descriptors, &out);
154
155 return out;
156}
157
158} // namespace operations_research::math_opt::codegen
A strongly typed element id. Value type.
Definition elements.h:64
std::string GenerateCode() const
Generates code.
Definition gen.cc:138
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
static std::shared_ptr< Type > AttrValueType()
Definition gen.cc:76
Language-agnostic utilities for Elemental codegen.
Definition codegen.cc:29
CodegenAttrTypeDescriptor MakeAttrTypeDescriptor()
Definition gen.cc:101
CodegenAttrTypeDescriptor::ValueType GetValueType(bool)
Definition gen.cc:82
static constexpr int kNumAttrOps
Definition gen.h:39
constexpr absl::string_view kOpNames[static_cast< int >(AttrOp::kNumOps)]
Definition gen.cc:115
std::tuple< BoolAttr0TypeDescriptor, BoolAttr1TypeDescriptor, IntAttr0TypeDescriptor, IntAttr1TypeDescriptor, DoubleAttr0TypeDescriptor, DoubleAttr1TypeDescriptor, DoubleAttr2TypeDescriptor, SymmetricDoubleAttr2TypeDescriptor, SymmetricDoubleAttr3TypeDescriptor, VariableAttr1TypeDescriptor > AllAttrTypeDescriptors
Definition attributes.h:291
constexpr absl::string_view kElementNames[kNumElements]
Definition elements.h:46
constexpr decltype(auto) ForEach(Fn &&fn, Tuple &&tuple)
Definition arrays.h:65
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