![]() |
chigraph
master
Systems programming language written for beginners in LLVM
|
this is an AST-like representation of a function in a graph It is used for IDE-like behavior, codegen, and JSON generation. More...
#include <chi/GraphFunction.hpp>
Public Member Functions | |
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't call this directly use GraphModule::getorCreateFunction. More... | |
GraphFunction (const GraphFunction &)=delete | |
GraphFunction (GraphFunction &&)=delete | |
~GraphFunction ()=default | |
Destructor. | |
Context & | context () const |
Get the context. More... | |
Result | createEntryNodeType (std::unique_ptr< NodeType > *toFill) const |
Create a fresh NodeType for an entry. More... | |
Result | createExitNodeType (std::unique_ptr< NodeType > *toFill) const |
Create a fresh NodeType for an exit. More... | |
const std::string & | description () const |
Get the description of the function. More... | |
llvm::FunctionType * | functionType () const |
Get the LLVM function type for the function. More... | |
GraphModule & | module () const |
Get the GraphModule that contains this GraphFunction. More... | |
std::string | name () const |
Get the name of the function. More... | |
GraphFunction & | operator= (const GraphFunction &)=delete |
GraphFunction & | operator= (GraphFunction &&)=delete |
std::string | qualifiedName () const |
Get the qualified name of the function Same as module().fullName() + ":" + name();. More... | |
void | setDescription (std::string newDesc) |
Set the description of the function. More... | |
std::vector< NodeInstance * > | setName (boost::string_view newName, bool updateReferences=true) |
Set the name of the function. More... | |
Node Manipulation | |
Functions for mainpulating nodes; getting, adding | |
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. More... | |
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. More... | |
NodeInstance * | nodeByID (const boost::uuids::uuid &id) const |
Get a node with a given ID. More... | |
NodeInstance * | entryNode () const noexcept |
Gets the node with type lang:entry returns nullptr on failure Also returns nullptr if there are two entry nodes, which is illegal. More... | |
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. More... | |
std::vector< NodeInstance * > | nodesWithType (const boost::filesystem::path &module, boost::string_view name) const noexcept |
Gets the nodes with a given type. More... | |
Result | insertNode (const boost::filesystem::path &moduleName, boost::string_view typeName, const nlohmann::json &typeJSON, float x, float y, boost::uuids::uuid id=boost::uuids::random_generator()(), NodeInstance **toFill=nullptr) |
Add a node to the graph using module, type, and json. More... | |
Result | removeNode (NodeInstance &nodeToRemove) |
Remove a node from the function. More... | |
Result | getOrInsertEntryNode (float x, float y, boost::uuids::uuid id=boost::uuids::random_generator()(), NodeInstance **toFill=nullptr) |
Creates an entry node if it doesn't already exist, else just return it. More... | |
Data input modifiers | |
const std::vector< NamedDataType > & | dataInputs () const |
Get the function data inputs in the format {type, docstring}. More... | |
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. More... | |
void | removeDataInput (size_t idx) |
Remove an input from the argument list Also removes invalid connections If idx is out of range, this function does nothing. More... | |
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 nothing. More... | |
void | retypeDataInput (size_t idx, DataType newType) |
Change the type of a data input This also updates the entry node and disconnects invalid connections. More... | |
Data output modifiers | |
const std::vector< NamedDataType > & | dataOutputs () const |
Get the function data outputs in the format {type, docstring}. More... | |
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. More... | |
void | removeDataOutput (size_t idx) |
Remove an data output from the argument list Also removes invalid connections If idx is out of range, this function does nothing. More... | |
void | renameDataOutput (size_t idx, std::string newName) |
Modify an data output (change it's type and docstring) More... | |
void | retypeDataOutput (size_t idx, DataType newType) |
Change the type of a data output This also updates all exit nodes and disconnects invalid connections. More... | |
Exec input modifiers | |
const std::vector< std::string > & | execInputs () const |
Get the function exec inputs. More... | |
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. More... | |
void | removeExecInput (size_t idx) |
Remove an exec input from the argument list If idx is out of range, this function does nothing. More... | |
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. More... | |
Exec output modifiers | |
Getters/modifers for exec output | |
const std::vector< std::string > & | execOutputs () const |
Get the function exec outputs. More... | |
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. More... | |
void | removeExecOutput (size_t idx) |
Remove an exec output from the argument list If idx is out of range, this function does nothing. More... | |
void | renameExecOutput (size_t idx, std::string name) |
Rename an exec output If idx is out of range, this function does nothing. More... | |
Local Variable Manipulation functions | |
const std::vector< NamedDataType > & | localVariables () const |
Get the local variables. More... | |
NamedDataType | localVariableFromName (boost::string_view name) const |
Get a local varaible by name. More... | |
NamedDataType | getOrCreateLocalVariable (std::string name, DataType type, bool *inserted=nullptr) |
Create a new local varaible in the module. More... | |
bool | removeLocalVariable (boost::string_view name) |
Remove a local variable from the function by name. More... | |
void | renameLocalVariable (std::string oldName, std::string newName) |
Rename a local variable. More... | |
void | retypeLocalVariable (boost::string_view name, DataType newType) |
Set a new type to a local variable. More... | |
this is an AST-like representation of a function in a graph It is used for IDE-like behavior, codegen, and JSON generation.
Definition at line 23 of file GraphFunction.hpp.
chi::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't call this directly use GraphModule::getorCreateFunction.
mod | The owning module |
name | The name of the function |
dataIns | The data inputs to the function |
dataOuts | The data outputs of the function |
execIns | The exec inputs to the function |
execOuts | The exec outputs to the function |
Definition at line 25 of file GraphFunction.cpp.
References name().
void chi::GraphFunction::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.
type | The new input type |
name | The name of the input (just for documentation) |
addBefore | The input ID to add before, default is end |
Definition at line 212 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataInputs().
void chi::GraphFunction::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.
type | The new output type |
name | The name of the output (just for documentation) |
addBefore | The output to add before, default is end |
Definition at line 248 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataOutputs().
void chi::GraphFunction::addExecInput | ( | std::string | name, |
size_t | addBefore = (std::numeric_limits<size_t>::max)() |
||
) |
Add an exec input to the end of the argument list.
name | The name of the input (just for documentation) |
addBefore | the input to add after |
Definition at line 299 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by execInputs().
void chi::GraphFunction::addExecOutput | ( | std::string | name, |
size_t | addBefore = (std::numeric_limits<size_t>::max)() |
||
) |
Add an exec output to the end of the argument list.
name | The name of the output (just for documentation) |
addBefore | The output to add after |
Definition at line 328 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by execOutputs().
|
inline |
Get the context.
Definition at line 310 of file GraphFunction.hpp.
Referenced by createEntryNodeType(), createExitNodeType(), functionType(), insertNode(), chi::jsonToGraphFunction(), and setName().
Create a fresh NodeType for an entry.
[out] | toFill | The NodeType pointer to fill |
Definition at line 156 of file GraphFunction.cpp.
References context(), dataInputs(), execInputs(), and chi::Context::nodeTypeFromModule().
Referenced by getOrInsertEntryNode(), nodes(), and renameExecOutput().
Create a fresh NodeType for an exit.
[out] | toFill | The NodeType pointer to fill |
Definition at line 174 of file GraphFunction.cpp.
References context(), dataOutputs(), execOutputs(), and chi::Context::nodeTypeFromModule().
Referenced by nodes(), and renameExecOutput().
|
inline |
Get the function data inputs in the format {type, docstring}.
Definition at line 145 of file GraphFunction.hpp.
References addDataInput(), removeDataInput(), renameDataInput(), and retypeDataInput().
Referenced by createEntryNodeType(), entryNode(), functionType(), chi::graphFunctionToJson(), chi::validateFunctionEntryType(), and chi::validateFunctionMainSignature().
|
inline |
Get the function data outputs in the format {type, docstring}.
Definition at line 180 of file GraphFunction.hpp.
References addDataOutput(), removeDataOutput(), renameDataOutput(), and retypeDataOutput().
Referenced by createExitNodeType(), functionType(), chi::graphFunctionToJson(), chi::validateFunctionExitTypes(), and chi::validateFunctionMainSignature().
|
inline |
Get the description of the function.
Definition at line 303 of file GraphFunction.hpp.
Referenced by chi::graphFunctionToJson().
|
noexcept |
Gets the node with type lang:entry returns nullptr on failure Also returns nullptr if there are two entry nodes, which is illegal.
Definition at line 44 of file GraphFunction.cpp.
References dataInputs(), execInputs(), and nodesWithType().
Referenced by getOrInsertEntryNode(), nodes(), chi::validateFunctionEntryType(), and chi::validateFunctionNodeInputs().
|
inline |
Get the function exec inputs.
Definition at line 216 of file GraphFunction.hpp.
References addExecInput(), removeExecInput(), and renameExecInput().
Referenced by createEntryNodeType(), entryNode(), chi::graphFunctionToJson(), and chi::validateFunctionMainSignature().
|
inline |
Get the function exec outputs.
Definition at line 241 of file GraphFunction.hpp.
References addExecOutput(), removeExecOutput(), and renameExecOutput().
Referenced by createExitNodeType(), chi::graphFunctionToJson(), and chi::validateFunctionMainSignature().
llvm::FunctionType * chi::GraphFunction::functionType | ( | ) | const |
Get the LLVM function type for the function.
Definition at line 281 of file GraphFunction.cpp.
References context(), dataInputs(), and dataOutputs().
Referenced by nodes().
NamedDataType chi::GraphFunction::getOrCreateLocalVariable | ( | std::string | name, |
DataType | type, | ||
bool * | inserted = nullptr |
||
) |
Create a new local varaible in the module.
name | The name of the local variable |
type | The type of the local |
inserted | True if the local varaiable was new, optional |
Definition at line 387 of file GraphFunction.cpp.
References localVariableFromName(), module(), and chi::ChiModule::updateLastEditTime().
Referenced by chi::jsonToGraphFunction(), and localVariables().
Result chi::GraphFunction::getOrInsertEntryNode | ( | float | x, |
float | y, | ||
boost::uuids::uuid | id = boost::uuids::random_generator()() , |
||
NodeInstance ** | toFill = nullptr |
||
) |
Creates an entry node if it doesn't already exist, else just return it.
x | The x coordinate of the new entry, or changes the existing entry node to be at this X location |
y | The y coordinate of the new entry, or changes the existing entry node to be at this Y location |
id | The ID of the node, disregarded if there is already an entry |
toFill | The NodeInstance* to fill, optional |
Definition at line 192 of file GraphFunction.cpp.
References createEntryNodeType(), entryNode(), insertNode(), module(), and chi::ChiModule::updateLastEditTime().
Referenced by nodes().
Result chi::GraphFunction::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.
[in] | type | The type of the node |
[in] | x | The x location of the node |
[in] | y | The y location of the node |
[in] | id | The node ID |
[out] | toFill | The nodeInstance to fill to, optional. |
Definition at line 64 of file GraphFunction.cpp.
References chi::Result::addEntry(), module(), nodes(), and chi::ChiModule::updateLastEditTime().
Referenced by getOrInsertEntryNode(), insertNode(), chi::jsonToGraphFunction(), and nodes().
Result chi::GraphFunction::insertNode | ( | const boost::filesystem::path & | moduleName, |
boost::string_view | typeName, | ||
const nlohmann::json & | typeJSON, | ||
float | x, | ||
float | y, | ||
boost::uuids::uuid | id = boost::uuids::random_generator()() , |
||
NodeInstance ** | toFill = nullptr |
||
) |
Add a node to the graph using module, type, and json.
moduleName | The name of the module that typeName is in |
typeName | The name of the node type in the module with the name moduleName |
typeJSON | The JSON to be passed to create the type |
x | The x location of the node |
y | The y location of the node |
id | The node ID |
toFill | The NodeInstance* to fill to, optional |
Definition at line 100 of file GraphFunction.cpp.
References context(), insertNode(), module(), chi::Context::nodeTypeFromModule(), and chi::ChiModule::updateLastEditTime().
NamedDataType chi::GraphFunction::localVariableFromName | ( | boost::string_view | name | ) | const |
Get a local varaible by name.
name | The name of the variable |
Definition at line 379 of file GraphFunction.cpp.
Referenced by getOrCreateLocalVariable(), and localVariables().
|
inline |
Get the local variables.
Definition at line 265 of file GraphFunction.hpp.
References getOrCreateLocalVariable(), localVariableFromName(), removeLocalVariable(), renameLocalVariable(), and retypeLocalVariable().
Referenced by chi::graphFunctionToJson().
|
inline |
Get the GraphModule that contains this GraphFunction.
Definition at line 328 of file GraphFunction.hpp.
Referenced by addDataInput(), addDataOutput(), addExecInput(), addExecOutput(), getOrCreateLocalVariable(), getOrInsertEntryNode(), insertNode(), chi::Debugger::nodeFromFrame(), nodes(), nodesWithType(), chi::parseColonPair(), qualifiedName(), removeDataInput(), removeDataOutput(), removeExecInput(), removeExecOutput(), removeLocalVariable(), removeNode(), renameDataInput(), renameDataOutput(), renameExecInput(), renameExecOutput(), renameLocalVariable(), retypeDataInput(), retypeLocalVariable(), setName(), chi::validateFunction(), chi::validateFunctionConnectionsAreTwoWay(), chi::validateFunctionEntryType(), chi::validateFunctionExecOutputs(), and chi::validateFunctionNodeInputs().
|
inline |
Get the name of the function.
Definition at line 313 of file GraphFunction.hpp.
References qualifiedName(), and setName().
Referenced by GraphFunction(), chi::graphFunctionToJson(), nodesWithType(), chi::parseColonPair(), qualifiedName(), chi::GraphModule::removeFunction(), removeLocalVariable(), setName(), chi::validateFunction(), chi::validateFunctionConnectionsAreTwoWay(), chi::validateFunctionEntryType(), chi::validateFunctionExecOutputs(), and chi::validateFunctionNodeInputs().
NodeInstance * chi::GraphFunction::nodeByID | ( | const boost::uuids::uuid & | id | ) | const |
Get a node with a given ID.
id | The ID of the node |
Definition at line 38 of file GraphFunction.cpp.
References nodes().
Referenced by nodes().
|
inline |
Get the nodes in the function Usually called by connectData or connectExec or GraphFunction.
Definition at line 51 of file GraphFunction.hpp.
Referenced by chi::graphFunctionToJson(), insertNode(), chi::jsonToGraphFunction(), nodeByID(), removeNode(), chi::validateFunctionConnectionsAreTwoWay(), and chi::validateFunctionExecOutputs().
|
inline |
Get the nodes in the function Usually called by connectData or connectExec or GraphFunction.
Definition at line 55 of file GraphFunction.hpp.
References createEntryNodeType(), createExitNodeType(), entryNode(), functionType(), getOrInsertEntryNode(), insertNode(), module(), nodeByID(), nodesWithType(), and removeNode().
|
noexcept |
Gets the nodes with a given type.
module | The module the type is in |
name | The name of the type |
Definition at line 87 of file GraphFunction.cpp.
References module(), and name().
Referenced by entryNode(), nodes(), removeLocalVariable(), renameExecOutput(), renameLocalVariable(), retypeLocalVariable(), and chi::validateFunctionExitTypes().
std::string chi::GraphFunction::qualifiedName | ( | ) | const |
Get the qualified name of the function Same as module().fullName() + ":" + name();.
Definition at line 530 of file GraphFunction.cpp.
References chi::ChiModule::fullName(), module(), and name().
Referenced by name(), and retypeLocalVariable().
void chi::GraphFunction::removeDataInput | ( | size_t | idx | ) |
Remove an input from the argument list Also removes invalid connections If idx is out of range, this function does nothing.
idx | The index to delete |
Definition at line 224 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataInputs().
void chi::GraphFunction::removeDataOutput | ( | size_t | idx | ) |
Remove an data output from the argument list Also removes invalid connections If idx is out of range, this function does nothing.
idx | The index to delete |
Definition at line 260 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataOutputs().
void chi::GraphFunction::removeExecInput | ( | size_t | idx | ) |
Remove an exec input from the argument list If idx is out of range, this function does nothing.
idx | The index to delete |
Definition at line 312 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by execInputs().
void chi::GraphFunction::removeExecOutput | ( | size_t | idx | ) |
Remove an exec output from the argument list If idx is out of range, this function does nothing.
idx | The index to delete |
Definition at line 341 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by execOutputs().
bool chi::GraphFunction::removeLocalVariable | ( | boost::string_view | name | ) |
Remove a local variable from the function by name.
name | The name of the local variable to remove |
Definition at line 405 of file GraphFunction.cpp.
References module(), name(), nodesWithType(), removeNode(), and chi::ChiModule::updateLastEditTime().
Referenced by localVariables().
Result chi::GraphFunction::removeNode | ( | NodeInstance & | nodeToRemove | ) |
Remove a node from the function.
Also disconnect it's connections.
nodeToRemove | The node to remove |
Definition at line 114 of file GraphFunction.cpp.
References chi::disconnectData(), chi::disconnectExec(), chi::NodeInstance::id(), chi::NodeInstance::inputDataConnections, chi::NodeInstance::inputExecConnections, module(), nodes(), chi::NodeInstance::outputDataConnections, chi::NodeInstance::outputExecConnections, and chi::ChiModule::updateLastEditTime().
Referenced by nodes(), and removeLocalVariable().
void chi::GraphFunction::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 nothing.
idx | The index to rename |
newName | The new name |
Definition at line 232 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataInputs().
void chi::GraphFunction::renameDataOutput | ( | size_t | idx, |
std::string | newName | ||
) |
Modify an data output (change it's type and docstring)
Rename a data output This also updates all exit nodes If idx is out of range, this function does nothing.
idx | The index to rename |
newName | The new name |
Definition at line 268 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataOutputs().
void chi::GraphFunction::renameExecInput | ( | size_t | idx, |
std::string | name | ||
) |
Change the name for an exec input If idx is out of range, this function does nothing.
idx | The index to change |
name | The new name. |
Definition at line 320 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by execInputs().
void chi::GraphFunction::renameExecOutput | ( | size_t | idx, |
std::string | name | ||
) |
Rename an exec output If idx is out of range, this function does nothing.
idx | The index to change |
name | The new name. |
Definition at line 349 of file GraphFunction.cpp.
References createEntryNodeType(), createExitNodeType(), module(), nodesWithType(), and chi::ChiModule::updateLastEditTime().
Referenced by execOutputs().
void chi::GraphFunction::renameLocalVariable | ( | std::string | oldName, |
std::string | newName | ||
) |
Rename a local variable.
oldName | The name of the existing local to change |
newName | The new name of the local |
Definition at line 428 of file GraphFunction.cpp.
References module(), nodesWithType(), chi::GraphModule::nodeTypeFromName(), and chi::ChiModule::updateLastEditTime().
Referenced by localVariables().
void chi::GraphFunction::retypeDataInput | ( | size_t | idx, |
DataType | newType | ||
) |
Change the type of a data input This also updates the entry node and disconnects invalid connections.
If idx is out of range, this function does nothing.
idx | The index to retype |
newType | The new type |
Definition at line 240 of file GraphFunction.cpp.
References module(), and chi::ChiModule::updateLastEditTime().
Referenced by dataInputs().
void chi::GraphFunction::retypeDataOutput | ( | size_t | idx, |
DataType | newType | ||
) |
Change the type of a data output This also updates all exit nodes and disconnects invalid connections.
If idx is out of range, this function does nothing.
idx | The index to retype |
newType | The new type |
Definition at line 276 of file GraphFunction.cpp.
Referenced by dataOutputs().
void chi::GraphFunction::retypeLocalVariable | ( | boost::string_view | name, |
DataType | newType | ||
) |
Set a new type to a local variable.
name | The name of the local to change |
newType | The new type |
Definition at line 469 of file GraphFunction.cpp.
References module(), nodesWithType(), chi::GraphModule::nodeTypeFromName(), chi::DataType::qualifiedName(), qualifiedName(), and chi::ChiModule::updateLastEditTime().
Referenced by localVariables().
|
inline |
Set the description of the function.
newDesc | The new description |
Definition at line 299 of file GraphFunction.hpp.
Referenced by chi::createGraphFunctionDeclarationFromJson().
std::vector< NodeInstance * > chi::GraphFunction::setName | ( | boost::string_view | newName, |
bool | updateReferences = true |
||
) |
Set the name of the function.
newName | the new name for the function |
updateReferences | should the references be updated? If true, all modules in the context will be scanned and updated |
Definition at line 506 of file GraphFunction.cpp.
References context(), chi::Context::findInstancesOfType(), module(), name(), chi::Context::nodeTypeFromModule(), and chi::ChiModule::updateLastEditTime().
Referenced by name().