chigraph  master
Systems programming language written for beginners in LLVM
NodeType.hpp
Go to the documentation of this file.
1 
4 #ifndef CHI_NODE_TYPE_HPP
5 #define CHI_NODE_TYPE_HPP
6 
7 #pragma once
8 
9 #include <memory>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include "chi/Fwd.hpp"
14 #include "chi/Support/json.hpp"
15 
16 namespace chi {
19 struct NodeType {
20 private:
21  friend NodeInstance;
22 
23 public:
28  NodeType(ChiModule& mod, std::string name = {}, std::string description = {});
30  virtual ~NodeType();
31 
34  std::string qualifiedName() const;
35 
47  virtual Result codegen(NodeCompiler& compiler, llvm::BasicBlock& codegenInto,
48  size_t execInputID, const llvm::DebugLoc& nodeLocation,
49  const std::vector<llvm::Value*>& io,
50  const std::vector<llvm::BasicBlock*>& outputBlocks) = 0;
51 
54  virtual nlohmann::json toJSON() const { return {}; }
57  virtual std::unique_ptr<NodeType> clone() const = 0;
58 
61  std::string name() const { return mName; }
64  std::string description() const { return mDescription; }
67  ChiModule& module() const { return *mModule; }
70  Context& context() const { return *mContext; }
73  const std::vector<NamedDataType>& dataInputs() const { return mDataInputs; }
76  const std::vector<NamedDataType>& dataOutputs() const { return mDataOutputs; }
79  const std::vector<std::string>& execInputs() const { return mExecInputs; }
82  const std::vector<std::string>& execOutputs() const { return mExecOutputs; }
83 
86  bool pure() { return mPure; }
87 
89  bool converter() { return mConverter; }
90 
91 protected:
94  void setDataInputs(std::vector<NamedDataType> newInputs);
97  void setDataOutputs(std::vector<NamedDataType> newOutputs);
100  void setExecInputs(std::vector<std::string> newInputs);
103  void setExecOutputs(std::vector<std::string> newOutputs);
104 
107  void setName(std::string newName);
108 
111  void setDescription(std::string newDesc);
112 
120  void makePure();
121 
125  void makeConverter();
126 
129  NodeInstance* nodeInstance() const;
130 
131 private:
132  ChiModule* mModule;
133  Context* mContext;
134  std::string mName, mDescription;
135 
136  NodeInstance* mNodeInstance = nullptr;
137 
138  std::vector<NamedDataType> mDataInputs;
139  std::vector<NamedDataType> mDataOutputs;
140 
141  std::vector<std::string> mExecInputs;
142  std::vector<std::string> mExecOutputs;
143 
144  bool mPure = false;
145  bool mConverter = false;
146 };
147 } // namespace chi
148 
149 #endif // CHI_NODE_TYPE_HPP
const std::vector< std::string > & execInputs() const
Get the execution inputs for the node.
Definition: NodeType.hpp:79
ChiModule & module() const
Get the ChiModule this NodeType belongs to.
Definition: NodeType.hpp:67
Forward declares all the chigraph data types.
Context & context() const
Get the Context this NodeType belongs to.
Definition: NodeType.hpp:70
const std::vector< NamedDataType > & dataOutputs() const
Get the data outputs for the node.
Definition: NodeType.hpp:76
An instance of a node.
void makeConverter()
Make this a converter node.
Definition: NodeType.cpp:44
void setDescription(std::string newDesc)
Set the description of the node.
Definition: NodeType.cpp:56
bool converter()
Get if this node is a converter.
Definition: NodeType.hpp:89
std::string name() const
Get the name of the NodeType in the ChiModule.
Definition: NodeType.hpp:61
virtual nlohmann::json toJSON() const
Create the JSON necessary to store the object.
Definition: NodeType.hpp:54
const std::vector< std::string > & execOutputs() const
Get the execution outputs for the node.
Definition: NodeType.hpp:82
void makePure()
Make this node pure For more info on what this means, see https://en.wikipedia.org/wiki/Pure_function...
Definition: NodeType.cpp:37
std::string description() const
Get the description of the NodeType.
Definition: NodeType.hpp:64
Helper to compile nodes.
A generic node type.
Definition: NodeType.hpp:19
NodeInstance * nodeInstance() const
Get the node instance.
Definition: NodeType.cpp:52
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
NodeType(ChiModule &mod, std::string name={}, std::string description={})
Constructor.
Definition: NodeType.cpp:9
An abstract class that represents a module of code in Chigraph Can be compiled to a llvm::Module...
Definition: ChiModule.hpp:24
void setExecOutputs(std::vector< std::string > newOutputs)
Set the exec outputs for the NodeType.
Definition: NodeType.cpp:33
void setDataOutputs(std::vector< NamedDataType > newOutputs)
Set the data outputs for the NodeType.
Definition: NodeType.cpp:24
void setExecInputs(std::vector< std::string > newInputs)
Set the exec inputs for the NodeType.
Definition: NodeType.cpp:29
bool pure()
Get if this node is pure.
Definition: NodeType.hpp:86
void setName(std::string newName)
Set the name of the type.
Definition: NodeType.cpp:54
virtual ~NodeType()
Destructor.
const std::vector< NamedDataType > & dataInputs() const
Get the data inputs for the node.
Definition: NodeType.hpp:73
The namespace where chigraph lives.
std::string qualifiedName() const
Get the qualified name of the node type, like module.name():name()
Definition: NodeType.cpp:17
virtual std::unique_ptr< NodeType > clone() const =0
Clones the type.
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
virtual Result codegen(NodeCompiler &compiler, llvm::BasicBlock &codegenInto, size_t execInputID, const llvm::DebugLoc &nodeLocation, const std::vector< llvm::Value *> &io, const std::vector< llvm::BasicBlock *> &outputBlocks)=0
A virtual function that is called when this node needs to be called.
void setDataInputs(std::vector< NamedDataType > newInputs)
Set the data inputs for the NodeType.
Definition: NodeType.cpp:19