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.
nudt-compiler-cpp/src/ir/Module.cpp

46 lines
1.5 KiB

#include "ir/IR.h"
namespace ir {
Function* Module::CreateFunction(
const std::string& name, std::shared_ptr<Type> ret_type,
const std::vector<std::shared_ptr<Type>>& param_types,
const std::vector<std::string>& param_names, bool is_external) {
if (auto* existing = GetFunction(name)) {
existing->SetExternal(existing->IsExternal() && is_external);
return existing;
}
auto func = std::make_unique<Function>(name, std::move(ret_type), param_types,
param_names, is_external);
auto* ptr = func.get();
functions_.push_back(std::move(func));
function_map_[name] = ptr;
return ptr;
}
Function* Module::GetFunction(const std::string& name) const {
auto it = function_map_.find(name);
return it == function_map_.end() ? nullptr : it->second;
}
GlobalValue* Module::CreateGlobalValue(const std::string& name,
std::shared_ptr<Type> object_type,
bool is_const, Value* init) {
if (auto* existing = GetGlobalValue(name)) {
return existing;
}
auto global =
std::make_unique<GlobalValue>(std::move(object_type), name, is_const, init);
auto* ptr = global.get();
globals_.push_back(std::move(global));
global_map_[name] = ptr;
return ptr;
}
GlobalValue* Module::GetGlobalValue(const std::string& name) const {
auto it = global_map_.find(name);
return it == global_map_.end() ? nullptr : it->second;
}
} // namespace ir