Cerata
A library to generate structural hardware designs
literal.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // 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
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <utility>
18 #include <optional>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <unordered_map>
23 
24 #include "cerata/object.h"
25 #include "cerata/type.h"
26 #include "cerata/node.h"
27 
28 namespace cerata {
29 
36 class Literal : public MultiOutputNode {
37  public:
39  enum class StorageType { INT, UINT, STRING, BOOL };
40  protected:
42  Literal(std::string name,
43  const std::shared_ptr<Type> &type,
44  StorageType st,
45  std::string str_val,
46  int64_t int_val,
47  bool bool_val)
48  : MultiOutputNode(std::move(name), Node::NodeID::LITERAL, type),
49  storage_type_(st),
50  Bool_val_(bool_val),
51  Int_val_(int_val),
52  String_val_(std::move(str_val)) {}
53 
56 
57  // Macros to generate Literal functions for different storage types.
58 #define LITERAL_DECL_FACTORY(NAME, TYPENAME) \
59  public: \
60  explicit Literal(std::string name, const std::shared_ptr<Type> &type, TYPENAME value); \
61  static std::shared_ptr<Literal> Make##NAME(TYPENAME value); \
62  static std::shared_ptr<Literal> Make##NAME(const std::shared_ptr<Type> &type, TYPENAME value); \
63  static std::shared_ptr<Literal> Make##NAME(std::string name, const std::shared_ptr<Type> &type, TYPENAME value); \
64  TYPENAME NAME##Value() const { return NAME##_val_; } \
65  \
66  protected: \
67  TYPENAME NAME##_val_ {}
68 
70  LITERAL_DECL_FACTORY(Bool, bool); //NOLINT
72  LITERAL_DECL_FACTORY(Int, int64_t); //NOLINT
74  LITERAL_DECL_FACTORY(String, std::string); //NOLINT
75 
76  public:
78  void SetParent(Graph *graph) override;
80  static std::shared_ptr<Literal> Make(bool value) { return MakeBool(value); }
82  static std::shared_ptr<Literal> Make(int64_t value) { return MakeInt(value); }
84  static std::shared_ptr<Literal> Make(std::string value) { return MakeString(std::move(value)); }
85 
87  std::shared_ptr<Object> Copy() const override;
88 
90  inline std::vector<Edge *> sources() const override { return {}; }
92  inline std::vector<Edge *> sinks() const override { return ToRawPointers(outputs_); }
93 
95  std::string ToString() const override;
96 
99 };
100 
107 template<typename T>
108 T RawValueOf(const Literal &node) { throw std::runtime_error("Can not obtain raw value for type."); }
109 
111 template<>
112 inline bool RawValueOf(const Literal &node) { return node.BoolValue(); }
113 
115 template<>
116 inline int64_t RawValueOf(const Literal &node) { return node.IntValue(); }
117 
119 template<>
120 inline std::string RawValueOf(const Literal &node) { return node.StringValue(); }
121 
123 template<typename T>
124 Literal::StorageType StorageTypeOf() { throw std::runtime_error("Type has no known StorageType."); }
125 
127 template<>
128 inline Literal::StorageType StorageTypeOf<bool>() { return Literal::StorageType::BOOL; }
129 
131 template<>
132 inline Literal::StorageType StorageTypeOf<int64_t>() { return Literal::StorageType::INT; }
133 
135 template<>
136 inline Literal::StorageType StorageTypeOf<uint64_t>() { return Literal::StorageType::UINT; }
137 
139 template<>
140 inline Literal::StorageType StorageTypeOf<std::string>() { return Literal::StorageType::STRING; }
141 
142 } // namespace cerata
cerata::Literal::LITERAL_DECL_FACTORY
LITERAL_DECL_FACTORY(String, std::string)
Strings.
cerata::Literal::SetParent
void SetParent(Graph *graph) override
Literal nodes are only owned by the literal pool, hence never have a parent graph.
Definition: literal.cc:57
cerata::Literal::Copy
std::shared_ptr< Object > Copy() const override
Create a copy of this Literal.
Definition: literal.cc:49
cerata::Literal::Make
static std::shared_ptr< Literal > Make(bool value)
Create a boolean literal.
Definition: literal.h:80
cerata::Literal
A Literal Node.
Definition: literal.h:36
cerata::StorageTypeOf< uint64_t >
Literal::StorageType StorageTypeOf< uint64_t >()
Template specialization for StorageTypeOf<int>
Definition: literal.h:136
cerata::MultiOutputNode::outputs_
std::vector< std::shared_ptr< Edge > > outputs_
The outgoing Edges that sink this Node.
Definition: node.h:142
cerata::Graph
A graph representing a hardware structure.
Definition: graph.h:37
cerata::ToRawPointers
std::vector< T * > ToRawPointers(const std::vector< std::shared_ptr< T >> &list)
Convert a list of shared pointers to raw pointers.
Definition: utils.h:125
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::Literal::Literal
Literal(std::string name, const std::shared_ptr< Type > &type, StorageType st, std::string str_val, int64_t int_val, bool bool_val)
Literal constructor.
Definition: literal.h:42
cerata::Node
A node.
Definition: node.h:42
cerata::RawValueOf
T RawValueOf(const Literal &node)
Obtain the raw value of a literal node.
Definition: literal.h:108
cerata::Literal::sources
std::vector< Edge * > sources() const override
A literal node has no inputs. This function returns an empty list.
Definition: literal.h:90
cerata::Named::name
std::string name() const
Return the name of the object.
Definition: utils.h:45
cerata::Node::NodeID
NodeID
Node type IDs with different properties.
Definition: node.h:45
cerata::Literal::ToString
std::string ToString() const override
Convert the Literal value to a human-readable string.
Definition: literal.cc:20
cerata::Literal::LITERAL_DECL_FACTORY
LITERAL_DECL_FACTORY(Int, int64_t)
Ints.
cerata::Literal::storage_type
StorageType storage_type() const
Return the storage type of the literal.
Definition: literal.h:98
cerata::Literal::Make
static std::shared_ptr< Literal > Make(int64_t value)
Create an integer literal.
Definition: literal.h:82
cerata::Literal::sinks
std::vector< Edge * > sinks() const override
Get the output edges of this Node.
Definition: literal.h:92
cerata::StorageTypeOf< int64_t >
Literal::StorageType StorageTypeOf< int64_t >()
Template specialization for StorageTypeOf<int>
Definition: literal.h:132
cerata::StorageTypeOf
Literal::StorageType StorageTypeOf()
Obtain the Literal::StorageType enum value of a C++ type T.
Definition: literal.h:124
cerata::Literal::LITERAL_DECL_FACTORY
LITERAL_DECL_FACTORY(Bool, bool)
Bools.
cerata::Literal::StorageType
StorageType
The storage type of the literal value.
Definition: literal.h:39
cerata::Literal::storage_type_
StorageType storage_type_
The raw storage type of the literal node.
Definition: literal.h:55
cerata::Literal::Make
static std::shared_ptr< Literal > Make(std::string value)
Create a string literal.
Definition: literal.h:84
cerata::StorageTypeOf< bool >
Literal::StorageType StorageTypeOf< bool >()
Template specialization for StorageTypeOf<bool>
Definition: literal.h:128
cerata::String
String type.
Definition: type.h:212
cerata::Node::type
Type * type() const
Return the node Type.
Definition: node.h:57
cerata::MultiOutputNode
A no-input, multiple-outputs node.
Definition: node.h:140