#include "ir/IR.h" #include namespace ir { Context& Module::GetContext() { return context_; } const Context& Module::GetContext() const { return context_; } // ─── 函数管理 ───────────────────────────────────────────────────────────────── Function* Module::CreateFunction(const std::string& name, std::shared_ptr ret_type) { functions_.push_back(std::make_unique(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>& 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(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>& Module::GetGlobalVariables() const { return globals_; } // ─── 外部函数声明 ───────────────────────────────────────────────────────────── void Module::DeclareExternalFunc(const std::string& name, std::shared_ptr ret_type, std::vector> 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& Module::GetExternalDecls() const { return external_decls_; } } // namespace ir