| chigraph master Systems programming language written for beginners in LLVM

Public Member Functions | List of all members
chi::Context Struct Reference

The class that handles the loading, creation, storing, and compilation of modules It also stores a `LLVMContext` object to be used everywhere. More...

`#include <chi/Context.hpp>`

## Public Member Functions
| Context (const boost::filesystem::path &workPath={})
| Creates a context with just the lang module. More...
| Context (const Context &context)=delete
| Context (Context &&)=delete
| ~Context ()
| Destructor.
bool | addModule (std::unique_ptr< ChiModule > modToAdd) noexcept
| Adds a custom module to the Context. More...
Result | addModuleFromJson (const boost::filesystem::path &fullName, const nlohmann::json &json, GraphModule **toFill=nullptr)
| Load a module from JSON – avoid this use the string overload. More...
Result | compileModule (const boost::filesystem::path &fullName, Flags< CompileSettings > settings, std::unique_ptr< llvm::Module > *toFill)
| Compile a module to a `llvm::Module`. More...
Result | compileModule (ChiModule &mod, Flags< CompileSettings > settings, std::unique_ptr< llvm::Module > *toFill)
| Compile a module to a `llvm::Module`. More...
std::unique_ptr< NodeType > | createConverterNodeType (const DataType &fromType, const DataType &toType)
| Create a converter node. More...
std::vector< NodeInstance * > | findInstancesOfType (const boost::filesystem::path &moduleName, boost::string_view typeName) const
| Find all uses of a node type in all the loaded modules. More...
bool | hasWorkspace () const noexcept
| Check if this context has a workspace bound to it – same as !workspacePath().empty() More...
LangModule * | langModule () const
| Get the `LangModule`, if it has been loaded. More...
std::vector< std::string > | listModulesInWorkspace () const noexcept
| Get the list of modules in the workspace. More...
llvm::LLVMContext & | llvmContext ()
| Get the `LLVMContext` More...
Result | loadModule (const boost::filesystem::path &name, ChiModule **toFill=nullptr)
| Load a module from disk, also loads dependencies. More...
ChiModule * | moduleByFullName (const boost::filesystem::path &fullModuleName) const noexcept
| Gets the module by the full name. More...
const ModuleCache & | moduleCache () const
| Get the module cache. More...
ModuleCache & | moduleCache ()
| Get the module cache. More...
std::vector< ChiModule * > | modules () const
| Get the modules in the Context. More...
GraphModule * | newGraphModule (const boost::filesystem::path &fullName)
| Create a new GraphModule with the given full name. More...
Result | nodeTypeFromModule (const boost::filesystem::path &moduleName, boost::string_view typeName, const nlohmann::json &data, std::unique_ptr< NodeType > *toFill) noexcept
| Gets a NodeType from the JSON and name. More...
void | setModuleCache (std::unique_ptr< ModuleCache > newCache)
| Set the module cache. More...
Result | typeFromModule (const boost::filesystem::path &module, boost::string_view name, DataType *toFill) noexcept
| Gets a DataType from a module. More...
bool | unloadModule (const boost::filesystem::path &fullName)
| Unloads a module. More...
boost::filesystem::path | workspacePath () const
| Get the workspace path of the Context. More...

## Detailed Description

The class that handles the loading, creation, storing, and compilation of modules It also stores a `LLVMContext` object to be used everywhere.

It stores all the modules and allows for compilation of them. Basic use looks like this:

chi::Context ctx{"/path/to/workspace"};
chi::Reuslt res = ctx.loadModule("github.com/russelltg/hellochigraph");
if (!res) {
std::cerr << res << std::endl;
return;
}

Definition at line 55 of file Context.hpp.

## Constructor & Destructor Documentation

## ◆ Context()

chi::Context::Context | ( | const boost::filesystem::path & | workPath = `{}` | ) |

Creates a context with just the lang module.

Parameters

  workPath | Path to the workspace, or a subdirectory of the workspace

Definition at line 40 of file Context.cpp.

References chi::workspaceFromChildPath(), and ~Context().

Here is the call graph for this function:

## Member Function Documentation

## ◆ addModule()

bool chi::Context::addModule | ( | std::unique_ptr< ChiModule > | modToAdd | ) |
| noexcept

Adds a custom module to the Context.

Parameters

  modToAdd | The module to add. The context will take excluseive ownership of it.

Returns
  True if the module was added (it didn't exist before)

Definition at line 185 of file Context.cpp.

References moduleByFullName(), and chi::LangModule::nodeTypeFromName().

Referenced by loadModule(), and newGraphModule().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ addModuleFromJson()

Result chi::Context::addModuleFromJson | ( | const boost::filesystem::path & | fullName,
|  | const nlohmann::json & | json,
|  | GraphModule ** | toFill = `nullptr`
| ) |  |

Load a module from JSON – avoid this use the string overload.

Parameters

  [in] | fullName | The full path of the module, including URL
  [in] | json | The JSON data
  [out] | toFill | The GraphModule* to fill into, optional

Returns
  The Result

Definition at line 156 of file Context.cpp.

References chi::Result::addScopedContext(), chi::ChiModule::fullName(), chi::jsonToGraphModule(), moduleByFullName(), and unloadModule().

Referenced by loadModule().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ compileModule() [1/2]

Result chi::Context::compileModule | ( | const boost::filesystem::path & | fullName,
|  | Flags< CompileSettings > | settings,
|  | std::unique_ptr< llvm::Module > * | toFill
| ) |  |

Compile a module to a `llvm::Module`.

Parameters

  [in] | fullName | The full name of the module to compile. If `moduleByFullName(fullName) == nullptr`, this function has no side-effects
  [in] | settings | The settings. See CompileSettings for more info
  [out] | toFill | The `llvm::Module` to fill – this can be nullptr it will be replaced

Precondition
  toFill isn't null (the value the unique_ptr points to be can be null, but not the pointer to the unique_ptr)

Returns
  The `Result`

Definition at line 284 of file Context.cpp.

References chi::Result::addEntry(), and moduleByFullName().

Referenced by compileModule(), and chi::Debugger::start().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ compileModule() [2/2]

Result chi::Context::compileModule | ( | ChiModule & | mod,
|  | Flags< CompileSettings > | settings,
|  | std::unique_ptr< llvm::Module > * | toFill
| ) |  |

Compile a module to a `llvm::Module`.

Parameters

  [in] | mod | The module to compile
  [in] | settings | The settings. See CompileSettings for more details
  [out] | toFill | The `llvm::Module` to fill – this can be nullptr it will be replaced

Precondition
  `toFill != nullptr` (the value the `unique_ptr` points to be can be null, but not the pointer to the `unique_ptr`)

Returns
  The `Result`

Definition at line 298 of file Context.cpp.

References chi::Result::addEntry(), chi::Result::addScopedContext(), chi::ModuleCache::cacheModule(), compileModule(), chi::ChiModule::dependencies(), chi::executablePath(), chi::ChiModule::fullName(), chi::ChiModule::fullNamePath(), chi::ChiModule::generateModule(), chi::ChiModule::lastEditTime(), chi::LinkDependencies, llvmContext(), moduleByFullName(), moduleCache(), chi::parseBitcodeFile(), chi::ModuleCache::retrieveFromCache(), chi::ChiModule::shortName(), and chi::UseCache.

Here is the call graph for this function:

## ◆ createConverterNodeType()

std::unique_ptr< NodeType > chi::Context::createConverterNodeType | ( | const DataType & | fromType,
|  | const DataType & | toType
| ) |  |

Create a converter node.

Parameters

  [in] | fromType | The type to convert from
  [in] | toType | The type to convert to

Returns
  The node type, or nullptr

Definition at line 269 of file Context.cpp.

References chi::DataType::qualifiedName().

Here is the call graph for this function:

## ◆ findInstancesOfType()

std::vector< NodeInstance * > chi::Context::findInstancesOfType | ( | const boost::filesystem::path & | moduleName,
|  | boost::string_view | typeName
| ) |  | const

Find all uses of a node type in all the loaded modules.

Parameters

  moduleName | The name of the module that the type being search for is in
  typeName | The name of the type in `module` to search for

Returns
  All the `NodeInstance`s that are of that type

Definition at line 444 of file Context.cpp.

Referenced by chi::GraphStruct::dataType(), chi::GraphModule::removeFunction(), chi::GraphStruct::setName(), and chi::GraphFunction::setName().

Here is the caller graph for this function:

## ◆ hasWorkspace()

bool chi::Context::hasWorkspace | ( |  | ) | const
| inline noexcept

Check if this context has a workspace bound to it – same as !workspacePath().empty()

Returns
  If it has a workspace

Definition at line 141 of file Context.hpp.

## ◆ langModule()

LangModule* chi::Context::langModule | ( |  | ) | const
| inline

Get the `LangModule`, if it has been loaded.

Returns
  The `LangModule`

Definition at line 177 of file Context.hpp.

Referenced by chi::FunctionCompiler::createSubroutineType(), and loadModule().

Here is the caller graph for this function:

## ◆ listModulesInWorkspace()

std::vector< std::string > chi::Context::listModulesInWorkspace | ( |  | ) | const
| noexcept

Get the list of modules in the workspace.

Returns
  The module list

Definition at line 68 of file Context.cpp.

References workspacePath().

Here is the call graph for this function:

## ◆ llvmContext()

llvm::LLVMContext& chi::Context::llvmContext | ( |  | ) |
| inline

Get the `LLVMContext`

Returns
  The `LLVMContext`

Definition at line 173 of file Context.hpp.

Referenced by compileModule(), chi::GraphStruct::dataType(), chi::LangModule::LangModule(), and chi::LangModule::typeFromName().

Here is the caller graph for this function:

## ◆ loadModule()

Result chi::Context::loadModule | ( | const boost::filesystem::path & | name,
|  | ChiModule ** | toFill = `nullptr`
| ) |  |

Load a module from disk, also loads dependencies.

Parameters

  [in] | name | The name of the moudle

Precondition
  `!name.empty()` to fetch all dependencies as well. Leave as default to only use local modules.

Parameters

  [out] | toFill | The module that was loaded, optional

Returns
  The result

Definition at line 91 of file Context.cpp.

References addModule(), addModuleFromJson(), chi::Result::addScopedContext(), langModule(), moduleByFullName(), chi::ChiModule::updateLastEditTime(), and workspacePath().

Referenced by chi::ChiModule::addDependency().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ moduleByFullName()

ChiModule * chi::Context::moduleByFullName | ( | const boost::filesystem::path & | fullModuleName | ) | const
| noexcept

Gets the module by the full name.

Parameters

  fullModuleName | The name of the module to find

Returns
  ret_module The module that has the full name `fullModuleName`, nullptr if none were found

Definition at line 48 of file Context.cpp.

Referenced by addModule(), addModuleFromJson(), compileModule(), loadModule(), chi::Debugger::nodeFromFrame(), nodeTypeFromModule(), and typeFromModule().

Here is the caller graph for this function:

## ◆ moduleCache() [1/2]

const ModuleCache& chi::Context::moduleCache | ( |  | ) | const
| inline

Get the module cache.

Returns
  The ModuleCache

Definition at line 191 of file Context.hpp.

Referenced by compileModule().

Here is the caller graph for this function:

## ◆ moduleCache() [2/2]

ModuleCache& chi::Context::moduleCache | ( |  | ) |
| inline

Get the module cache.

Returns
  The ModuleCache

Definition at line 194 of file Context.hpp.

References chi::interpretLLVMIR(), chi::interpretLLVMIRAsMain(), chi::stringifyLLVMType(), and chi::workspaceFromChildPath().

Here is the call graph for this function:

## ◆ modules()

std::vector<ChiModule*> chi::Context::modules | ( |  | ) | const
| inline

Get the modules in the Context.

Returns
  The modules

Definition at line 181 of file Context.hpp.

## ◆ newGraphModule()

GraphModule * chi::Context::newGraphModule | ( | const boost::filesystem::path & | fullName | ) |

Create a new GraphModule with the given full name.

Parameters

  fullName | The new name

Returns
  The created GraphModule

Definition at line 55 of file Context.cpp.

References addModule().

Referenced by chi::jsonToGraphModule().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ nodeTypeFromModule()

Result chi::Context::nodeTypeFromModule | ( | const boost::filesystem::path & | moduleName,
|  | boost::string_view | typeName,
|  | const nlohmann::json & | data,
|  | std::unique_ptr< NodeType > * | toFill
| ) |  |
| noexcept

Gets a NodeType from the JSON and name.

Parameters

  [in] | moduleName | The full module name.
  [in] | typeName | The name of the node type
  [in] | data | The JSON data that is used to construct the NodeType.
  [out] | toFill | The point to fill

Precondition
  `toFill != nullptr` (the value the `unique_ptr` points to be can be null, but not the pointer to the `unique_ptr`)

Returns
  The Result

Definition at line 253 of file Context.cpp.

References chi::Result::addEntry(), and moduleByFullName().

Referenced by chi::GraphFunction::createEntryNodeType(), chi::GraphFunction::createExitNodeType(), chi::GraphFunction::insertNode(), chi::jsonToGraphFunction(), and chi::GraphFunction::setName().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ setModuleCache()

void chi::Context::setModuleCache | ( | std::unique_ptr< ModuleCache > | newCache | ) |

Set the module cache.

Parameters

  newCache | The new module cache

Precondition
  `newCache != nullptr`

Definition at line 599 of file Context.cpp.

## ◆ typeFromModule()

Result chi::Context::typeFromModule | ( | const boost::filesystem::path & | module,
|  | boost::string_view | name,
|  | DataType * | toFill
| ) |  |
| noexcept

Gets a DataType from a module.

Parameters

  [in] | module | The full name of the module
  [in] | name | The name of the type, required
  [out] | toFill | The type to fill

Precondition
  `toFill != nullptr`

Returns
  The result

Definition at line 232 of file Context.cpp.

References chi::Result::addEntry(), moduleByFullName(), and chi::ChiModule::typeFromName().

Referenced by chi::createGraphFunctionDeclarationFromJson(), chi::GraphModule::createNodeTypeFromCCode(), chi::jsonToGraphFunction(), chi::jsonToGraphStruct(), chi::LangModule::LangModule(), and chi::GraphModule::nodeTypeFromName().

Here is the call graph for this function:
Here is the caller graph for this function:

## ◆ unloadModule()

bool chi::Context::unloadModule | ( | const boost::filesystem::path & | fullName | ) |

Unloads a module.

Parameters

  fullName | The full name of the module to unload

Returns
  True if a module was unloaded

Definition at line 219 of file Context.cpp.

Referenced by addModuleFromJson().

Here is the caller graph for this function:

## ◆ workspacePath()

boost::filesystem::path chi::Context::workspacePath | ( |  | ) | const
| inline

Get the workspace path of the Context.

Returns
  The workspace path

Definition at line 138 of file Context.hpp.

Referenced by chi::DefaultModuleCache::cachePathForModule(), chi::jsonToGraphModule(), listModulesInWorkspace(), loadModule(), and chi::GraphModule::sourceFilePath().

Here is the caller graph for this function:

The documentation for this struct was generated from the following files:

- lib/core/include/chi/Context.hpp
- lib/core/src/Context.cpp