docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cctype>
5#include <iomanip>
6#include <iostream>
7#include <sstream>
8#include <string_view>
9#include <type_traits>
10
11#include "sdfg/types/type.h"
12
13namespace sdfg {
14namespace codegen {
15
17public:
18 // Constructor
19 PrettyPrinter(int indent = 0, bool frozen = false);
20 PrettyPrinter(std::ostream& stream, int indent = 0, bool frozen = false);
21
22 // Set the indentation level
23 void setIndent(int indent);
24
25 int indent() const;
26
27 int changeIndent(int delta);
28
29 // Get the underlying string
30 std::string str() const;
31
32 // Clear the stringstream content
33 void clear();
34
36 size_t content() const {
37 return content_;
38 }
39
40 // Overload the insertion operator
41 template<typename T>
42 PrettyPrinter& operator<<(const T& value) {
43 if (frozen_) {
44 throw std::runtime_error("PrettyPrinter is frozen");
45 }
46 applyIndent();
47 stream << value;
48 if (has_content(value)) {
49 ++content_;
50 }
51 return *this;
52 }
53
54 // Overload for manipulators (like std::endl)
55 PrettyPrinter& operator<<(std::ostream& (*manip)(std::ostream&) );
56
57private:
58 std::unique_ptr<std::stringstream> owned_stream;
59 std::ostream& stream;
60 int indentSize;
61 bool isNewLine = true;
62 bool frozen_;
63 size_t content_ = 0;
64
65 // Apply indentation only at the beginning of a new line
66 void applyIndent();
67
68 template<typename T>
69 static bool has_content(const T& value) {
70 auto not_space = [](char c) {
71 return !std::isspace(static_cast<unsigned char>(c));
72 };
73 if constexpr (std::is_same_v<T, char>) {
74 return not_space(value);
75 } else if constexpr (std::is_convertible_v<const T&, std::string_view>) {
76 std::string_view text(value);
77 return std::any_of(text.begin(), text.end(), not_space);
78 } else {
79 // Stream manipulators such as std::hex emit nothing.
80 return !std::is_function_v<T>;
81 }
82 }
83};
84
85class Reference : public types::IType {
86private:
87 std::unique_ptr<types::IType> reference_;
88
89public:
90 Reference(const types::IType& reference_);
91
93 types::StorageType storage_type, size_t alignment, const std::string& initializer, const types::IType& reference_
94 );
95
96 std::unique_ptr<types::IType> clone() const override;
97
98 virtual types::TypeID type_id() const override;
99
101
102 bool is_symbol() const override;
103
104 bool is_pointer_like() const override {
105 return true;
106 }
107
109
110 bool operator==(const types::IType& other) const override;
111
112 std::string print() const override;
113
121 virtual void
122 replace_symbols(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
123 virtual void replace_symbols(const symbolic::ExpressionMapping& replacements) override;
124};
125
126} // namespace codegen
127} // namespace sdfg
std::string str() const
PrettyPrinter & operator<<(std::ostream &(*manip)(std::ostream &))
PrettyPrinter & operator<<(const T &value)
Definition utils.h:42
void setIndent(int indent)
PrettyPrinter(int indent=0, bool frozen=false)
PrettyPrinter(std::ostream &stream, int indent=0, bool frozen=false)
size_t content() const
Monotonic count of writes that emitted non-whitespace text; compare two readings to detect an empty r...
Definition utils.h:36
bool operator==(const types::IType &other) const override
Compares two types for equality.
virtual void replace_symbols(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
Replace symbolic expressions on this type.
Reference(types::StorageType storage_type, size_t alignment, const std::string &initializer, const types::IType &reference_)
virtual void replace_symbols(const symbolic::ExpressionMapping &replacements) override
bool is_symbol() const override
Checks if this type represents a symbolic/integer type.
const types::IType & reference_type() const
bool is_pointer_like() const override
Checks if this is a pointer or a structure behaves like a pointer.
Definition utils.h:104
std::unique_ptr< types::IType > clone() const override
Creates a deep copy of this type.
std::string print() const override
Returns a string representation of this type.
virtual types::TypeID type_id() const override
Returns the TypeID for runtime type identification.
types::PrimitiveType primitive_type() const override
Returns the primitive type for this type.
Reference(const types::IType &reference_)
Abstract base interface for all types in the sdfglib type system.
Definition type.h:625
StorageType storage_type() const
Gets the storage type (const version).
Definition type.h:651
std::string initializer() const
Gets the initializer expression.
Definition type.h:683
size_t alignment() const
Gets the alignment requirement in bytes.
Definition type.h:673
Represents the storage location and management characteristics of data.
Definition type.h:64
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Definition argument_registry.h:10
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
TypeID
Enumeration of high-level type categories.
Definition type.h:607
Definition analysis.h:7
Core type system definitions for sdfglib.