forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.7 KiB
69 lines
2.7 KiB
#include "ir/IR.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
namespace ir {
|
|
|
|
Context& Module::GetContext() { return context_; }
|
|
const Context& Module::GetContext() const { return context_; }
|
|
|
|
// ─── 函数管理 ─────────────────────────────────────────────────────────────────
|
|
Function* Module::CreateFunction(const std::string& name,
|
|
std::shared_ptr<Type> ret_type) {
|
|
functions_.push_back(std::make_unique<Function>(name, std::move(ret_type)));
|
|
Function* f = functions_.back().get();
|
|
func_map_[name] = f;
|
|
return f;
|
|
}
|
|
|
|
Function* Module::GetFunction(const std::string& name) const {
|
|
auto it = func_map_.find(name);
|
|
return it == func_map_.end() ? nullptr : it->second;
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<Function>>& Module::GetFunctions() const {
|
|
return functions_;
|
|
}
|
|
|
|
// ─── 全局变量管理 ─────────────────────────────────────────────────────────────
|
|
GlobalVariable* Module::CreateGlobalVariable(const std::string& name,
|
|
bool is_const, int init_val,
|
|
int num_elements) {
|
|
globals_.push_back(
|
|
std::make_unique<GlobalVariable>(name, is_const, init_val, num_elements));
|
|
GlobalVariable* g = globals_.back().get();
|
|
global_map_[name] = g;
|
|
return g;
|
|
}
|
|
|
|
GlobalVariable* Module::GetGlobalVariable(const std::string& name) const {
|
|
auto it = global_map_.find(name);
|
|
return it == global_map_.end() ? nullptr : it->second;
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<GlobalVariable>>&
|
|
Module::GetGlobalVariables() const {
|
|
return globals_;
|
|
}
|
|
|
|
// ─── 外部函数声明 ─────────────────────────────────────────────────────────────
|
|
void Module::DeclareExternalFunc(const std::string& name,
|
|
std::shared_ptr<Type> ret_type,
|
|
std::vector<std::shared_ptr<Type>> param_types,
|
|
bool is_variadic) {
|
|
if (external_decl_index_.count(name)) return; // 已声明,幂等
|
|
external_decl_index_[name] = external_decls_.size();
|
|
external_decls_.push_back(
|
|
{name, std::move(ret_type), std::move(param_types), is_variadic});
|
|
}
|
|
|
|
bool Module::HasExternalDecl(const std::string& name) const {
|
|
return external_decl_index_.count(name) > 0;
|
|
}
|
|
|
|
const std::vector<ExternalFuncDecl>& Module::GetExternalDecls() const {
|
|
return external_decls_;
|
|
}
|
|
|
|
} // namespace ir
|