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
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.
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. ...
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.
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.
const std::vector< NamedDataType > & dataOutputs() const
Get the function data outputs in the format {type, docstring}.
this is an AST-like representation of a function in a graph It is used for IDE-like behavior...
Forward declares all the chigraph data types.
Result removeNode(NodeInstance &nodeToRemove)
Remove a node from the function.
Result createEntryNodeType(std::unique_ptr< NodeType > *toFill) const
Create a fresh NodeType for an entry.
void retypeDataInput(size_t idx, DataType newType)
Change the type of a data input This also updates the entry node and disconnects invalid connections...
const std::vector< std::string > & execInputs() const
Get the function exec inputs.
std::pair< std::string, std::string > parseColonPair(const std::string &in)
Parse a colonated pair Example: lang:i32 would turn into {lang, i32}.
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.
NamedDataType localVariableFromName(boost::string_view name) const
Get a local varaible by name.
const std::vector< std::string > & execOutputs() const
Get the function exec outputs.
~GraphFunction()=default
Destructor.
void removeDataInput(size_t idx)
Remove an input from the argument list Also removes invalid connections If idx is out of range...
Context & context() const
Get the context.
void retypeLocalVariable(boost::string_view name, DataType newType)
Set a new type to a local variable.
std::string qualifiedName() const
Get the qualified name of the function Same as module().fullName() + ":" + name();.
An instance of a node.
void removeExecOutput(size_t idx)
Remove an exec output from the argument list If idx is out of range, this function does nothing...
void setDescription(std::string newDesc)
Set the description of the function.
GraphModule & module() const
Get the GraphModule that contains this GraphFunction.
void removeDataOutput(size_t idx)
Remove an data output from the argument list Also removes invalid connections If idx is out of range...
Result createExitNodeType(std::unique_ptr< NodeType > *toFill) const
Create a fresh NodeType for an exit.
std::string name() const
Get the name of the function.
Basicaly a std::pair<std::string, DataType>, except it has nicer names.
Definition: DataType.hpp:72
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.
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
void removeExecInput(size_t idx)
Remove an exec input from the argument list If idx is out of range, this function does nothing...
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.
NodeInstance * nodeByID(const boost::uuids::uuid &id) const
Get a node with a given ID.
std::vector< NodeInstance * > setName(boost::string_view newName, bool updateReferences=true)
Set the name of the function.
NodeInstance * entryNode() const noexcept
Gets the node with type lang:entry returns nullptr on failure Also returns nullptr if there are two e...
void renameExecOutput(size_t idx, std::string name)
Rename an exec output If idx is out of range, this function does nothing.
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.
void retypeDataOutput(size_t idx, DataType newType)
Change the type of a data output This also updates all exit nodes and disconnects invalid connections...
void renameLocalVariable(std::string oldName, std::string newName)
Rename a local variable.
bool removeLocalVariable(boost::string_view name)
Remove a local variable from the function by name.
std::vector< NodeInstance * > nodesWithType(const boost::filesystem::path &module, boost::string_view name) const noexcept
Gets the nodes with a given type.
const std::vector< NamedDataType > & localVariables() const
Get the local variables.
Module that holds graph functions.
Definition: GraphModule.hpp:16
void renameDataOutput(size_t idx, std::string newName)
Modify an data output (change it&#39;s type and docstring)
The namespace where chigraph lives.
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.
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.
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
const std::vector< NamedDataType > & dataInputs() const
Get the function data inputs in the format {type, docstring}.
llvm::FunctionType * functionType() const
Get the LLVM function type for the function.
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
NamedDataType getOrCreateLocalVariable(std::string name, DataType type, bool *inserted=nullptr)
Create a new local varaible in the module.
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...
const std::string & description() const
Get the description of the function.