Joos1W Compiler Framework
All Classes Functions Typedefs Pages
tir::CompilationUnit Class Reference

Public Member Functions

 CompilationUnit (Context &ctx)
 
 CompilationUnit (const CompilationUnit &)=delete
 
 CompilationUnit (CompilationUnit &&)=delete
 
CompilationUnitoperator= (const CompilationUnit &)=delete
 
CompilationUnitoperator= (CompilationUnit &&)=delete
 
FunctionCreateFunction (FunctionType *type, const std::string_view name)
 Create a new function with the given type and name. More...
 
GlobalVariableCreateGlobalVariable (Type *type, const std::string_view name)
 Create a new global variable with the given type and name. More...
 
FunctionfindFunction (const std::string_view name)
 Get the function with the given name. More...
 
GlobalVariablefindGlobalVariable (const std::string_view name)
 Find the global variable with the given name. More...
 
std::ostream & print (std::ostream &os) const
 
void dump () const
 
utils::Generator< Function * > functions () const
 Filters through the global objects and yields just the functions.
 
utils::Generator< GlobalObject * > global_objects () const
 Yields all global objects in the compilation unit.
 
utils::Generator< std::pair< std::string_view, GlobalObject * > > global_objects_kv () const
 Yields all global objects in the compilation unit.
 
auto & ctx ()
 Get the context associated with this compilation unit.
 
utils::Generator< GlobalVariable * > global_variables () const
 Filters through the global objects and yields just the variables.
 
void removeGlobalObject (std::string const &name)
 Remove the global object with the given name.
 
FunctionbuiltinMalloc ()
 
FunctionbuiltinException ()
 

Detailed Description

Definition at line 12 of file CompilationUnit.h.

Member Function Documentation

◆ CreateFunction()

Function* tir::CompilationUnit::CreateFunction ( FunctionType type,
const std::string_view  name 
)
inline

Create a new function with the given type and name.

Parameters
typeThe type of the function
nameThe name of the function
Returns
Function* The function, or nullptr if it already exists

Definition at line 27 of file CompilationUnit.h.

27  {
28  if(findFunction(std::string{name})) return nullptr;
29  auto* buf = ctx_.alloc().allocate_bytes(sizeof(Function), alignof(Function));
30  auto* func = new(buf) Function{ctx_, this, type, name};
31  globals_.emplace(name, func);
32  return func;
33  }
Function * findFunction(const std::string_view name)
Get the function with the given name.

References findFunction().

◆ CreateGlobalVariable()

GlobalVariable* tir::CompilationUnit::CreateGlobalVariable ( Type type,
const std::string_view  name 
)
inline

Create a new global variable with the given type and name.

Parameters
type
name
Returns
GlobalVariable*

Definition at line 42 of file CompilationUnit.h.

42  {
43  if(findGlobalVariable(std::string{name})) return nullptr;
44  auto* buf = ctx_.alloc().allocate_bytes(sizeof(GlobalVariable),
45  alignof(GlobalVariable));
46  auto* gv = new(buf) GlobalVariable{ctx_, type};
47  globals_.emplace(name, gv);
48  return gv;
49  }
GlobalVariable * findGlobalVariable(const std::string_view name)
Find the global variable with the given name.

References findGlobalVariable().

◆ findFunction()

Function* tir::CompilationUnit::findFunction ( const std::string_view  name)
inline

Get the function with the given name.

Parameters
nameThe name of the function
Returns
Function* The function, or nullptr if it does not exist

Definition at line 57 of file CompilationUnit.h.

57  {
58  auto it = globals_.find(std::string{name});
59  if(it == globals_.end()) return nullptr;
60  auto go = it->second;
61  return dyn_cast<Function>(go);
62  }

Referenced by CreateFunction().

◆ findGlobalVariable()

GlobalVariable* tir::CompilationUnit::findGlobalVariable ( const std::string_view  name)
inline

Find the global variable with the given name.

Parameters
nameThe name of the global variable
Returns
GlobalVariable* The global variable, or nullptr if it does not exist

Definition at line 70 of file CompilationUnit.h.

70  {
71  auto it = globals_.find(std::string{name});
72  if(it == globals_.end()) return nullptr;
73  auto go = it->second;
74  return dyn_cast<GlobalVariable>(go);
75  }

Referenced by CreateGlobalVariable().


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