| chigraph master Systems programming language written for beginners in LLVM

GraphFunction.hpp
Go to the documentation of this file.
1
4 #ifndef CHI_GRAPH_FUNCTION_HPP
5 #define CHI_GRAPH_FUNCTION_HPP
6
7 #pragma once
8
9 #include "chi/Fwd.hpp"
10 #include "chi/Support/HashUuid.hpp"
11 #include "chi/Support/json.hpp"
12
13 #include <unordered_map>
14
15 #include <boost/filesystem.hpp>
16 #include <boost/utility/string_view.hpp>
17 #include <boost/uuid/random_generator.hpp>
18 #include <boost/uuid/uuid.hpp>
19
20 namespace chi {
23 struct GraphFunction {
31 GraphFunction(GraphModule& mod, std::string name, std::vector<NamedDataType> dataIns,
32 std::vector<NamedDataType> dataOuts, std::vector<std::string> execIns,
33 std::vector<std::string> execOuts);
34
35 GraphFunction(const GraphFunction&) = delete;
36 GraphFunction(GraphFunction&&) = delete;
37
38 GraphFunction& operator=(const GraphFunction&) = delete;
39 GraphFunction& operator=(GraphFunction&&) = delete;
40
42 ~GraphFunction() = default;
43
47
51 std::unordered_map<boost::uuids::uuid, std::unique_ptr<NodeInstance>>& nodes() {
52 return mNodes;
53 }
55 const std::unordered_map<boost::uuids::uuid, std::unique_ptr<NodeInstance>>& nodes() const {
56 return mNodes;
57 }
58
62 NodeInstance* nodeByID(const boost::uuids::uuid& id) const;
63
68 NodeInstance* entryNode() const noexcept;
69
77 Result insertNode(std::unique_ptr<NodeType> type, float x, float y,
78 boost::uuids::uuid id = boost::uuids::random_generator()(),
79 NodeInstance** toFill = nullptr);
80
85 std::vector<NodeInstance*> nodesWithType(const boost::filesystem::path& module,
86 boost::string_view name) const noexcept;
87
97 Result insertNode(const boost::filesystem::path& moduleName, boost::string_view typeName,
98 const nlohmann::json& typeJSON, float x, float y,
99 boost::uuids::uuid id = boost::uuids::random_generator()(),
100 NodeInstance** toFill = nullptr);
101
105 Result removeNode(NodeInstance& nodeToRemove);
106
115 Result getOrInsertEntryNode(float x, float y,
116 boost::uuids::uuid id = boost::uuids::random_generator()(),
117 NodeInstance** toFill = nullptr);
118
120
126 Result createEntryNodeType(std::unique_ptr<NodeType>* toFill) const;
127
133 Result createExitNodeType(std::unique_ptr<NodeType>* toFill) const;
134
137 llvm::FunctionType* functionType() const;
138
139 // TODO: check uses and replace to avoid errors
142
145 const std::vector<NamedDataType>& dataInputs() const { return mDataInputs; }
150 void addDataInput(const DataType& type, std::string name,
151 size_t addBefore = (std::numeric_limits<size_t>::max)());
152
157 void removeDataInput(size_t idx);
158
164 void renameDataInput(size_t idx, std::string newName);
165
171 void retypeDataInput(size_t idx, DataType newType);
172
174
177
180 const std::vector<NamedDataType>& dataOutputs() const { return mDataOutputs; }
185 void addDataOutput(const DataType& type, std::string name,
186 size_t addBefore = (std::numeric_limits<size_t>::max)());
187
192 void removeDataOutput(size_t idx);
194
200 void renameDataOutput(size_t idx, std::string newName);
201
207 void retypeDataOutput(size_t idx, DataType newType);
208
210
213
216 const std::vector<std::string>& execInputs() const { return mExecInputs; }
220 void addExecInput(std::string name, size_t addBefore = (std::numeric_limits<size_t>::max)());
221
225 void removeExecInput(size_t idx);
226
231 void renameExecInput(size_t idx, std::string name);
232
234
238
241 const std::vector<std::string>& execOutputs() const { return mExecOutputs; }
245 void addExecOutput(std::string name, size_t addBefore = (std::numeric_limits<size_t>::max)());
246
250 void removeExecOutput(size_t idx);
251
256 void renameExecOutput(size_t idx, std::string name);
257
259
262
265 const std::vector<NamedDataType>& localVariables() const { return mLocalVariables; }
266
270 NamedDataType localVariableFromName(boost::string_view name) const;
271
277 NamedDataType getOrCreateLocalVariable(std::string name, DataType type,
278 bool* inserted = nullptr);
279
283 bool removeLocalVariable(boost::string_view name);
284
288 void renameLocalVariable(std::string oldName, std::string newName);
289
293 void retypeLocalVariable(boost::string_view name, DataType newType);
294
296
299 void setDescription(std::string newDesc) { mDescription = std::move(newDesc); }
300
303 const std::string& description() const { return mDescription; };
304
305 // Various getters
307
310 Context& context() const { return *mContext; }
313 std::string name() const { return mName; }
317 std::string qualifiedName() const;
318
324 std::vector<NodeInstance*> setName(boost::string_view newName, bool updateReferences = true);
325
328 GraphModule& module() const { return *mModule; }
329
330 private:
331 void updateEntries(); // update the entry node to work with
332 void updateExits();
333
334 GraphModule* mModule;
335 Context* mContext;
336 std::string mName;
337 std::string mDescription;
338
339 std::vector<NamedDataType> mDataInputs;
340 std::vector<NamedDataType> mDataOutputs;
341
342 std::vector<std::string> mExecInputs;
343 std::vector<std::string> mExecOutputs;
344
345 std::vector<NamedDataType> mLocalVariables;
346
347 std::unordered_map<boost::uuids::uuid, std::unique_ptr<NodeInstance>>
348 mNodes;
349 };
350
355 inline std::pair<std::string, std::string> parseColonPair(const std::string& in) {
356 size_t colonID = in.find(':');
357 if (colonID == std::string::npos) { return {}; }
358 std::string module = in.substr(0, in.find(':'));
359 std::string name = in.substr(in.find(':') + 1);
360
361 return {module, name};
362 }
363 } // namespace chi
364
365 #endif // CHI_GRAPH_FUNCTION_HPP
chi::GraphFunction::addDataOutput
void addDataOutput(const DataType &type, std::string name, size_t addBefore=(std::numeric_limits< size_t >::max)())
Add an data output to the end of the argument list.
Definition: GraphFunction.cpp:248
chi::GraphFunction::GraphFunction
GraphFunction(GraphModule &mod, std::string name, std::vector< NamedDataType > dataIns, std::vector< NamedDataType > dataOuts, std::vector< std::string > execIns, std::vector< std::string > execOuts)
Construct a graph–don&#39;t call this directly use GraphModule::getorCreateFunction. ...
Definition: GraphFunction.cpp:25
HashUuid.hpp
chi::GraphFunction::addDataInput
void addDataInput(const DataType &type, std::string name, size_t addBefore=(std::numeric_limits< size_t >::max)())
Add an input to the end of the argument list.
Definition: GraphFunction.cpp:212
chi::GraphFunction::getOrInsertEntryNode
Result getOrInsertEntryNode(float x, float y, boost::uuids::uuid id=boost::uuids::random_generator()(), NodeInstance **toFill=nullptr)
Creates an entry node if it doesn&#39;t already exist, else just return it.
Definition: GraphFunction.cpp:192
chi::GraphFunction::dataOutputs
const std::vector< NamedDataType > & dataOutputs() const
Get the function data outputs in the format {type, docstring}.
Definition: GraphFunction.hpp:180
chi::GraphFunction
this is an AST-like representation of a function in a graph It is used for IDE-like behavior...
Definition: GraphFunction.hpp:23
Fwd.hpp
Forward declares all the chigraph data types.
chi::GraphFunction::removeNode
Result removeNode(NodeInstance &nodeToRemove)
Remove a node from the function.
Definition: GraphFunction.cpp:114
chi::GraphFunction::createEntryNodeType
Result createEntryNodeType(std::unique_ptr< NodeType > *toFill) const
Create a fresh NodeType for an entry.
Definition: GraphFunction.cpp:156
chi::GraphFunction::retypeDataInput
void retypeDataInput(size_t idx, DataType newType)
Change the type of a data input This also updates the entry node and disconnects invalid connections...
Definition: GraphFunction.cpp:240
chi::GraphFunction::execInputs
const std::vector< std::string > & execInputs() const
Get the function exec inputs.
Definition: GraphFunction.hpp:216
chi::parseColonPair
std::pair< std::string, std::string > parseColonPair(const std::string &in)
Parse a colonated pair Example: lang:i32 would turn into {lang, i32}.
Definition: GraphFunction.hpp:355
chi::GraphFunction::insertNode
Result insertNode(std::unique_ptr< NodeType > type, float x, float y, boost::uuids::uuid id=boost::uuids::random_generator()(), NodeInstance **toFill=nullptr)
Add a node to the graph.
Definition: GraphFunction.cpp:64
chi::GraphFunction::localVariableFromName
NamedDataType localVariableFromName(boost::string_view name) const
Get a local varaible by name.
Definition: GraphFunction.cpp:379
chi::GraphFunction::execOutputs
const std::vector< std::string > & execOutputs() const
Get the function exec outputs.
Definition: GraphFunction.hpp:241
chi::GraphFunction::~GraphFunction
~GraphFunction()=default
Destructor.
chi::GraphFunction::removeDataInput
void removeDataInput(size_t idx)
Remove an input from the argument list Also removes invalid connections If idx is out of range...
Definition: GraphFunction.cpp:224
chi::GraphFunction::context
Context & context() const
Get the context.
Definition: GraphFunction.hpp:310
chi::GraphFunction::retypeLocalVariable
void retypeLocalVariable(boost::string_view name, DataType newType)
Set a new type to a local variable.
Definition: GraphFunction.cpp:469
chi::GraphFunction::qualifiedName
std::string qualifiedName() const
Get the qualified name of the function Same as module().fullName() + ":" + name();.
Definition: GraphFunction.cpp:530
chi::NodeInstance
An instance of a node.
Definition: NodeInstance.hpp:19
chi::GraphFunction::removeExecOutput
void removeExecOutput(size_t idx)
Remove an exec output from the argument list If idx is out of range, this function does nothing...
Definition: GraphFunction.cpp:341
chi::GraphFunction::setDescription
void setDescription(std::string newDesc)
Set the description of the function.
Definition: GraphFunction.hpp:299
chi::GraphFunction::module
GraphModule & module() const
Get the GraphModule that contains this GraphFunction.
Definition: GraphFunction.hpp:328
chi::GraphFunction::removeDataOutput
void removeDataOutput(size_t idx)
Remove an data output from the argument list Also removes invalid connections If idx is out of range...
Definition: GraphFunction.cpp:260
chi::GraphFunction::createExitNodeType
Result createExitNodeType(std::unique_ptr< NodeType > *toFill) const
Create a fresh NodeType for an exit.
Definition: GraphFunction.cpp:174
chi::GraphFunction::name
std::string name() const
Get the name of the function.
Definition: GraphFunction.hpp:313
chi::NamedDataType
Basicaly a std::pair<std::string, DataType>, except it has nicer names.
Definition: DataType.hpp:72
chi::GraphFunction::nodes
const std::unordered_map< boost::uuids::uuid, std::unique_ptr< NodeInstance > > & nodes() const
Get the nodes in the function Usually called by connectData or connectExec or GraphFunction.
Definition: GraphFunction.hpp:55
chi::Context
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
chi::GraphFunction::removeExecInput
void removeExecInput(size_t idx)
Remove an exec input from the argument list If idx is out of range, this function does nothing...
Definition: GraphFunction.cpp:312
chi::GraphFunction::renameExecInput
void renameExecInput(size_t idx, std::string name)
Change the name for an exec input If idx is out of range, this function does nothing.
Definition: GraphFunction.cpp:320
chi::GraphFunction::nodeByID
NodeInstance * nodeByID(const boost::uuids::uuid &id) const
Get a node with a given ID.
Definition: GraphFunction.cpp:38
chi::GraphFunction::setName
std::vector< NodeInstance * > setName(boost::string_view newName, bool updateReferences=true)
Set the name of the function.
Definition: GraphFunction.cpp:506
chi::GraphFunction::entryNode
NodeInstance * entryNode() const noexcept
Gets the node with type lang:entry returns nullptr on failure Also returns nullptr if there are two e...
Definition: GraphFunction.cpp:44
chi::GraphFunction::renameExecOutput
void renameExecOutput(size_t idx, std::string name)
Rename an exec output If idx is out of range, this function does nothing.
Definition: GraphFunction.cpp:349
chi::GraphFunction::nodes
std::unordered_map< boost::uuids::uuid, std::unique_ptr< NodeInstance > > & nodes()
Get the nodes in the function Usually called by connectData or connectExec or GraphFunction.
Definition: GraphFunction.hpp:51
chi::GraphFunction::retypeDataOutput
void retypeDataOutput(size_t idx, DataType newType)
Change the type of a data output This also updates all exit nodes and disconnects invalid connections...
Definition: GraphFunction.cpp:276
chi::GraphFunction::renameLocalVariable
void renameLocalVariable(std::string oldName, std::string newName)
Rename a local variable.
Definition: GraphFunction.cpp:428
chi::GraphFunction::removeLocalVariable
bool removeLocalVariable(boost::string_view name)
Remove a local variable from the function by name.
Definition: GraphFunction.cpp:405
chi::GraphFunction::nodesWithType
std::vector< NodeInstance * > nodesWithType(const boost::filesystem::path &module, boost::string_view name) const noexcept
Gets the nodes with a given type.
Definition: GraphFunction.cpp:87
chi::GraphFunction::localVariables
const std::vector< NamedDataType > & localVariables() const
Get the local variables.
Definition: GraphFunction.hpp:265
chi::GraphModule
Module that holds graph functions.
Definition: GraphModule.hpp:16
chi::GraphFunction::renameDataOutput
void renameDataOutput(size_t idx, std::string newName)
Modify an data output (change it&#39;s type and docstring)
Definition: GraphFunction.cpp:268
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12
chi::GraphFunction::addExecOutput
void addExecOutput(std::string name, size_t addBefore=(std::numeric_limits< size_t >::max)())
Add an exec output to the end of the argument list.
Definition: GraphFunction.cpp:328
chi::GraphFunction::addExecInput
void addExecInput(std::string name, size_t addBefore=(std::numeric_limits< size_t >::max)())
Add an exec input to the end of the argument list.
Definition: GraphFunction.cpp:299
chi::DataType
A type of data Loose wrapper around llvm::Type*, except it knows which ChiModule it&#39;s in and it embed...
Definition: DataType.hpp:17
chi::GraphFunction::dataInputs
const std::vector< NamedDataType > & dataInputs() const
Get the function data inputs in the format {type, docstring}.
Definition: GraphFunction.hpp:145
chi::GraphFunction::functionType
llvm::FunctionType * functionType() const
Get the LLVM function type for the function.
Definition: GraphFunction.cpp:281
chi::Result
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
chi::GraphFunction::getOrCreateLocalVariable
NamedDataType getOrCreateLocalVariable(std::string name, DataType type, bool *inserted=nullptr)
Create a new local varaible in the module.
Definition: GraphFunction.cpp:387
chi::GraphFunction::renameDataInput
void renameDataInput(size_t idx, std::string newName)
Rename a data input This also updates the entry node If idx is out of range, this function does nothi...
Definition: GraphFunction.cpp:232
chi::GraphFunction::description
const std::string & description() const
Get the description of the function.
Definition: GraphFunction.hpp:303