10#include <llvm/IR/DIBuilder.h>
11#include <llvm/IR/GlobalVariable.h>
12#include <llvm/IR/IRBuilder.h>
13#include <llvm/IR/LLVMContext.h>
14#include <llvm/IR/Module.h>
15#include <llvm/Target/TargetMachine.h>
46 vector<SourceFile> m_sources;
48 std::deque<Fn> m_fns{};
49 std::deque<Struct> m_structs{};
50 std::deque<Fn> m_ctors{};
51 std::deque<Const> m_consts{};
52 std::queue<DeclLike> m_decl_queue{};
53 unique_ptr<semantic::TypeWalker> m_walker;
59 optional<InScope> m_scope_ctor{};
63 llvm::Function* m_global_ctor_fn;
64 llvm::Function* m_global_dtor_fn;
68 std::map<ast::Program*, llvm::DICompileUnit*> m_source_mapping{};
70 unique_ptr<llvm::LLVMContext> m_context;
71 unique_ptr<llvm::IRBuilder<>> m_builder;
72 unique_ptr<llvm::Module> m_module;
73 unique_ptr<llvm::TargetMachine> m_target_machine;
74 unique_ptr<llvm::DIBuilder> m_debug;
80 [[nodiscard]]
auto module() const -> const auto& {
return m_module; }
81 [[nodiscard]]
auto context() const -> const auto& {
return m_context; }
82 [[nodiscard]]
auto builder() const -> const auto& {
return m_builder; }
118 template <
typename T>
119 requires (!std::is_const_v<T>)
120 void statement(T& stat) {
121 throw std::runtime_error(
"Unknown statement "s + stat.kind_name());
124 template <
typename T>
125 requires (!std::is_const_v<T>)
126 auto expression(T& expr) -> Val {
127 throw std::runtime_error(
"Unknown expression "s + expr.kind_name());
132 auto entrypoint_builder() -> llvm::IRBuilder<>;
133 auto entrypoint_dtor_builder() -> llvm::IRBuilder<>;
136 void setup_fn_base(Fn&);
139 void destruct_last_scope();
142 void destruct_all_scopes();
145 void make_temporary_in_scope(Val& val,
const ast::AST& ast,
const string& name =
"tmp"s);
147 void expose_parameter_as_local(ty::Type type,
const string& name,
const ast::AST& ast, Val val);
149 auto create_malloc(llvm::Type* base_type, Val slice_size, string_view name =
""sv) -> Val;
150 auto create_malloc(llvm::Type* base_type, uint64_t slice_size, string_view name =
""sv) -> Val;
151 auto create_free(Val ptr) -> Val;
154 auto primitive(Fn* fn,
const vector<Val>& args,
const vector<ty::Type>& types, vector<ast::AnyExpr*>& ast_args)
157 auto int_bin_primitive(
const string& primitive,
const vector<Val>& args) -> Val;
160 void walk_types(DeclLike);
164 auto get_vtable(Struct& st,
const Struct& iface)
noexcept(
false) -> nonnull<llvm::GlobalVariable*>;
166 void declare_default_ctor(Struct&);
The Compiler the the primary top-level type during compilation. A single instance is created during t...
auto source_files() -> const auto &
auto builder() const -> const auto &
auto body_expression(ast::Expr &expr) -> Val
void body_statement(ast::Stmt &)
auto ptr_bitsize() -> unsigned int
auto llvm_type(ty::Type type, bool erase_opaque=false) -> llvm::Type *
Convert a type into its corresponding LLVM type.
auto direct_call_operator(ast::CallExpr &expr) -> Val
void run()
Begin compilation!
void define(Fn &)
Compile the body of a function or constructor.
void write_object(const char *filename, bool binary)
auto declare(Fn &) -> llvm::Function *
Declare a function/constructor in bytecode, or get an existing declaration.
Compiler(const optional< string > &target_triple, vector< SourceFile > source_files)
auto module() const -> const auto &
auto decl_statement(ast::Stmt &, optional< ty::Type > parent=std::nullopt, ast::Program *member=nullptr, nullable< Substitutions * > parent_subs=nullptr) -> DeclLike
void destruct(Val val, ty::Type type)
Destructs an object val of specified type type.
auto context() const -> const auto &
auto default_init(ty::Type type) -> Val
Default-constructs an object of specified type type.
auto create_struct(Struct &) -> bool
All nodes in the AST tree of the program inherit from this class.
Expressions have an associated value and type.
Statements make up most things in source code.
Represents a type in the type system. NOTE: that this isn't the class to use for type introspection,...
An user-defined struct type with associated fields.
A "qualified" type, with a non-stackable qualifier, i.e. mut.
A helper template to walk the Abstract Syntax Tree (AST), utilizing the Curiously Recurring Template ...
A constant declaration in the compiler.
A common base between declarations in the compiler: Fn, Struct and Const. Its value may also be absen...
A function declaration in the compiler.
A struct declaration in the compiler.
A value of a complied expression.
A function call or operator.
The top level structure of a file of source code.
Determine the type information of AST nodes. This makes up most of the "semantic" phase of the compil...