22#include "absl/strings/str_cat.h"
23#include "absl/strings/string_view.h"
24#include "absl/types/span.h"
33class NamedType :
public Type {
35 explicit NamedType(std::string name) : name_(std::move(name)) {}
37 void Print(absl::string_view, std::string* out)
const final {
38 absl::StrAppend(out, name_);
45class PointerType :
public Type {
47 explicit PointerType(std::shared_ptr<Type> pointee)
48 : pointee_(std::move(pointee)) {}
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,
"*");
56 std::shared_ptr<Type> pointee_;
59class AttrValueTypeType :
public Type {
61 void Print(absl::string_view attr_value_type, std::string* out)
const final {
62 absl::StrAppend(out, attr_value_type);
69 return std::make_shared<NamedType>(std::move(name));
73 return std::make_shared<PointerType>(std::move(pointee));
77 return std::make_shared<AttrValueTypeType>();
94template <ElementType element_type>
100template <
typename Descriptor>
104 descriptor.
name = Descriptor::kName;
106 descriptor.
symmetry = Descriptor::Symmetry::GetName();
109 for (
const auto& attr_descriptor : Descriptor::kAttrDescriptors) {
116 "Get",
"Set",
"IsNonDefault",
"NumNonDefaults",
"GetNonDefaults"};
118void CodeGenerator::EmitAttrType(
const CodegenAttrTypeDescriptor& descriptor,
119 std::string* out)
const {
122 const AttrOpFunctionInfo& op_info = attr_op_function_infos_[op];
128 absl::Span<const CodegenAttrTypeDescriptor> descriptors,
129 std::string* out)
const {
130 for (
const auto& descriptor : descriptors) {
146 std::vector<CodegenAttrTypeDescriptor> attr_type_descriptors;
148 [&attr_type_descriptors](
auto type_descriptor) {
149 attr_type_descriptors.push_back(
A strongly typed element id. Value type.
std::string GenerateCode() const
Generates code.
virtual void EmitElements(absl::Span< const absl::string_view > elements, std::string *out) const
Emits code for elements.
virtual void StartAttrType(const CodegenAttrTypeDescriptor &descriptor, std::string *out) const
Called before generating code for an attribute type.
virtual void EmitAttributes(absl::Span< const CodegenAttrTypeDescriptor > descriptors, std::string *out) const
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.
virtual void EmitHeader(std::string *out) const
Emits the header for the generated code.
Representations for types.
static std::shared_ptr< Type > Named(std::string name)
A named type, e.g. "double".
static std::shared_ptr< Type > Pointer(std::shared_ptr< Type > pointee)
A pointer type.
static std::shared_ptr< Type > AttrValueType()
Language-agnostic utilities for Elemental codegen.
CodegenAttrTypeDescriptor MakeAttrTypeDescriptor()
CodegenAttrTypeDescriptor::ValueType GetValueType(bool)
static constexpr int kNumAttrOps
constexpr absl::string_view kOpNames[static_cast< int >(AttrOp::kNumOps)]
std::tuple< BoolAttr0TypeDescriptor, BoolAttr1TypeDescriptor, IntAttr0TypeDescriptor, IntAttr1TypeDescriptor, DoubleAttr0TypeDescriptor, DoubleAttr1TypeDescriptor, DoubleAttr2TypeDescriptor, SymmetricDoubleAttr2TypeDescriptor, SymmetricDoubleAttr3TypeDescriptor, VariableAttr1TypeDescriptor > AllAttrTypeDescriptors
constexpr absl::string_view kElementNames[kNumElements]
constexpr decltype(auto) ForEach(Fn &&fn, Tuple &&tuple)
A struct to represent an attribute type descriptor during codegen.
ValueType
The value type of the attribute.
absl::string_view name
The attribute type name.
int num_key_elements
The number of key elements.
std::string symmetry
The key symmetry.
std::vector< absl::string_view > attribute_names
The names of the attributes of this type.