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.
47 lines
1.3 KiB
47 lines
1.3 KiB
// 保存函数列表并提供模块级上下文访问。
|
|
|
|
#include "ir/IR.h"
|
|
|
|
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,
|
|
std::vector<std::shared_ptr<Type>> param_types) {
|
|
functions_.push_back(std::make_unique<Function>(
|
|
name, std::move(ret_type), std::move(param_types)));
|
|
return functions_.back().get();
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<Function>>& Module::GetFunctions() const {
|
|
return functions_;
|
|
}
|
|
|
|
Function* Module::FindFunction(const std::string& name) const {
|
|
for (const auto& f : functions_) {
|
|
if (f && f->GetName() == name) return f.get();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
GlobalVariable* Module::CreateGlobalVar(const std::string& name, int init_val, int count) {
|
|
global_vars_.push_back(std::make_unique<GlobalVariable>(name, init_val, count));
|
|
return global_vars_.back().get();
|
|
}
|
|
|
|
GlobalVariable* Module::FindGlobalVar(const std::string& name) const {
|
|
for (const auto& gv : global_vars_) {
|
|
if (gv && gv->GetName() == name) return gv.get();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
const std::vector<std::unique_ptr<GlobalVariable>>& Module::GetGlobalVars() const {
|
|
return global_vars_;
|
|
}
|
|
|
|
} // namespace ir
|