chigraph  master
Systems programming language written for beginners in LLVM
Context.hpp
Go to the documentation of this file.
1 
4 #ifndef CHI_CONTEXT_HPP
5 #define CHI_CONTEXT_HPP
6 
7 #pragma once
8 
9 #include <memory>
10 #include <unordered_map>
11 
12 #include "chi/Fwd.hpp"
13 #include "chi/ModuleCache.hpp"
14 #include "chi/Support/Flags.hpp"
15 #include "chi/Support/json.hpp"
16 
17 #include <llvm/IR/LLVMContext.h>
18 #include <llvm/Support/CodeGen.h> // for CodeGenOpt
19 
20 #include <boost/filesystem/path.hpp>
21 #include <boost/utility/string_view.hpp>
22 
23 namespace chi {
24 
26 enum class CompileSettings {
27 
29  UseCache = 1u,
30 
35  LinkDependencies = 1u << 1,
36 
38  Default = UseCache | LinkDependencies
39 };
40 
55 struct Context {
58  Context(const boost::filesystem::path& workPath = {});
59 
61  ~Context();
62 
63  // no move or copy, doesn't make sense
64  Context(const Context& context) = delete;
65  Context(Context&&) = delete;
66 
71  ChiModule* moduleByFullName(const boost::filesystem::path& fullModuleName) const noexcept;
72 
76  GraphModule* newGraphModule(const boost::filesystem::path& fullName);
77 
80  // init it (pretty sure it inits windows networking stuff)
81  std::vector<std::string> listModulesInWorkspace() const noexcept;
82 
89  Result loadModule(const boost::filesystem::path& name, ChiModule** toFill = nullptr);
90 
96  Result addModuleFromJson(const boost::filesystem::path& fullName, const nlohmann::json& json,
97  GraphModule** toFill = nullptr);
98 
102  bool addModule(std::unique_ptr<ChiModule> modToAdd) noexcept;
103 
107  bool unloadModule(const boost::filesystem::path& fullName);
108 
115  Result typeFromModule(const boost::filesystem::path& module, boost::string_view name,
116  DataType* toFill) noexcept;
117 
126  Result nodeTypeFromModule(const boost::filesystem::path& moduleName,
127  boost::string_view typeName, const nlohmann::json& data,
128  std::unique_ptr<NodeType>* toFill) noexcept;
129 
134  std::unique_ptr<NodeType> createConverterNodeType(const DataType& fromType, const DataType& toType);
135 
138  boost::filesystem::path workspacePath() const { return mWorkspacePath; }
141  bool hasWorkspace() const noexcept { return !workspacePath().empty(); }
142 
151  Result compileModule(const boost::filesystem::path& fullName, Flags<CompileSettings> settings,
152  std::unique_ptr<llvm::Module>* toFill);
153 
161  Result compileModule(ChiModule& mod, Flags<CompileSettings> settings,
162  std::unique_ptr<llvm::Module>* toFill);
163 
168  std::vector<NodeInstance*> findInstancesOfType(const boost::filesystem::path& moduleName,
169  boost::string_view typeName) const;
170 
173  llvm::LLVMContext& llvmContext() { return mLLVMContext; }
174 
177  LangModule* langModule() const { return mLangModule; }
178 
181  std::vector<ChiModule*> modules() const {
182  std::vector<ChiModule*> ret;
183 
184  for (const auto& mod : mModules) { ret.push_back(mod.get()); }
185 
186  return ret;
187  }
188 
191  const ModuleCache& moduleCache() const { return *mModuleCache; }
192 
194  ModuleCache& moduleCache() { return *mModuleCache; }
195 
199  void setModuleCache(std::unique_ptr<ModuleCache> newCache);
200 
201 private:
202  boost::filesystem::path mWorkspacePath;
203 
204  llvm::LLVMContext mLLVMContext;
205 
206  std::vector<std::unique_ptr<ChiModule>> mModules;
207 
208  // This cache is only for use during compilation to not duplicate modules
209  std::unordered_map<std::string /*full name*/, llvm::Module* /*the compiled module*/>
210  mCompileCache;
211 
212  LangModule* mLangModule = nullptr;
213 
214  std::unique_ptr<ModuleCache> mModuleCache;
215 
216  std::unordered_map<std::string /*from Type*/, std::unordered_map<std::string /*to type*/, std::unique_ptr<NodeType>>> mTypeConverters;
217 };
218 
224 boost::filesystem::path workspaceFromChildPath(const boost::filesystem::path& path);
225 
229 std::string stringifyLLVMType(llvm::Type* ty);
230 
239 Result interpretLLVMIR(std::unique_ptr<llvm::Module> mod,
240  llvm::CodeGenOpt::Level optLevel = llvm::CodeGenOpt::Default,
241  const std::vector<llvm::GenericValue>& args = {},
242  llvm::Function* funcToRun = nullptr, llvm::GenericValue* ret = nullptr);
243 
251 Result interpretLLVMIRAsMain(std::unique_ptr<llvm::Module> mod,
252  llvm::CodeGenOpt::Level optLevel = llvm::CodeGenOpt::Default,
253  const std::vector<std::string>& args = {},
254  llvm::Function* funcToRun = nullptr, int* ret = nullptr);
255 } // namespace chi
256 
257 #endif // CHI_CONTEXT_HPP
LangModule * langModule() const
Get the LangModule, if it has been loaded.
Definition: Context.hpp:177
Forward declares all the chigraph data types.
The module that provides built-in operations like literals, math operations, etc. ...
Definition: LangModule.hpp:16
Default, which is both.
boost::filesystem::path workspacePath() const
Get the workspace path of the Context.
Definition: Context.hpp:138
std::vector< ChiModule * > modules() const
Get the modules in the Context.
Definition: Context.hpp:181
CompileSettings
Settings for compiling modules.
Definition: Context.hpp:26
boost::filesystem::path workspaceFromChildPath(const boost::filesystem::path &path)
Get the workspace directory from a child of the workspace directory Example: say you have a workspace...
Definition: Context.cpp:462
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
An abstract class that represents a module of code in Chigraph Can be compiled to a llvm::Module...
Definition: ChiModule.hpp:24
Result interpretLLVMIRAsMain(std::unique_ptr< llvm::Module > mod, llvm::CodeGenOpt::Level optLevel=llvm::CodeGenOpt::Default, const std::vector< std::string > &args={}, llvm::Function *funcToRun=nullptr, int *ret=nullptr)
Interpret LLVM IR as if it were the main function.
Definition: Context.cpp:564
ModuleCache & moduleCache()
Get the module cache.
Definition: Context.hpp:194
Result interpretLLVMIR(std::unique_ptr< llvm::Module > mod, llvm::CodeGenOpt::Level optLevel=llvm::CodeGenOpt::Default, const std::vector< llvm::GenericValue > &args={}, llvm::Function *funcToRun=nullptr, llvm::GenericValue *ret=nullptr)
Interpret LLVM IR, just a convenience function.
Definition: Context.cpp:528
A template class for type-safe flags.
Definition: Flags.hpp:26
llvm::LLVMContext & llvmContext()
Get the LLVMContext
Definition: Context.hpp:173
const ModuleCache & moduleCache() const
Get the module cache.
Definition: Context.hpp:191
This class provides an interface for creating module caches.
Definition: ModuleCache.hpp:16
Module that holds graph functions.
Definition: GraphModule.hpp:16
bool hasWorkspace() const noexcept
Check if this context has a workspace bound to it – same as !workspacePath().empty() ...
Definition: Context.hpp:141
The namespace where chigraph lives.
Use the cache in lib.
std::string stringifyLLVMType(llvm::Type *ty)
Turns a type into a string.
Definition: Context.cpp:474
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
Link in dependencies If this is set, it will be a ready to run module If not, it&#39;ll contain forward d...
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72