chigraph  master
Systems programming language written for beginners in LLVM
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< NodeTypecreateConverterNodeType (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...
 
LangModulelangModule () 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...
 
ChiModulemoduleByFullName (const boost::filesystem::path &fullModuleName) const noexcept
 Gets the module by the full name. More...
 
const ModuleCachemoduleCache () const
 Get the module cache. More...
 
ModuleCachemoduleCache ()
 Get the module cache. More...
 
std::vector< ChiModule * > modules () const
 Get the modules in the Context. More...
 
GraphModulenewGraphModule (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
workPathPath 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
modToAddThe 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]fullNameThe full path of the module, including URL
[in]jsonThe JSON data
[out]toFillThe 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]fullNameThe full name of the module to compile. If moduleByFullName(fullName) == nullptr, this function has no side-effects
[in]settingsThe settings. See CompileSettings for more info
[out]toFillThe 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]modThe module to compile
[in]settingsThe settings. See CompileSettings for more details
[out]toFillThe 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]fromTypeThe type to convert from
[in]toTypeThe 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
moduleNameThe name of the module that the type being search for is in
typeNameThe name of the type in module to search for
Returns
All the NodeInstances 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
inlinenoexcept

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]nameThe name of the moudle
Precondition
!name.empty() to fetch all dependencies as well. Leave as default to only use local modules.
Parameters
[out]toFillThe 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
fullModuleNameThe 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
fullNameThe 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]moduleNameThe full module name.
[in]typeNameThe name of the node type
[in]dataThe JSON data that is used to construct the NodeType.
[out]toFillThe 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
newCacheThe 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]moduleThe full name of the module
[in]nameThe name of the type, required
[out]toFillThe 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
fullNameThe 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: