2023-04-17 11:14:33 +03:00
|
|
|
// for clangd
|
|
|
|
|
#include "../include/global_info.hpp"
|
|
|
|
|
|
|
|
|
|
namespace info {
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::AddImport(ImportInfo&& import_info,
|
|
|
|
|
const std::optional<std::string>& name) {
|
|
|
|
|
if (name.has_value()) {
|
|
|
|
|
global_info_.usages_[name.value()] = std::move(import_info);
|
|
|
|
|
} else {
|
|
|
|
|
global_info_.imports_.push_back(std::move(import_info));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
|
|
|
|
const std::optional<NamespaceInfo::Modifier>& modifier,
|
|
|
|
|
const std::optional<std::string>& variable) {
|
|
|
|
|
NamespaceInfo* namespace_info = nullptr;
|
|
|
|
|
if (variable.has_value()) {
|
|
|
|
|
namespace_info = &namespace_stack_.back()->variable_namespaces[name].emplace_back();
|
|
|
|
|
namespace_stack_.push_back(namespace_info);
|
|
|
|
|
|
|
|
|
|
namespace_info->modifier = modifier;
|
|
|
|
|
namespace_info->variable = variable;
|
|
|
|
|
} else {
|
|
|
|
|
namespace_info = &namespace_stack_.back()->namespaces[name];
|
|
|
|
|
namespace_stack_.push_back(namespace_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace_info->type_name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
|
|
|
|
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
|
|
|
|
if (namespace_stack_.size() <= 1) {
|
|
|
|
|
// error
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace_stack_.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|
|
|
|
namespace_stack_.clear();
|
|
|
|
|
namespace_stack_.push_back(&global_info_.global_namespace_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
|
|
|
|
FunctionDeclarationInfo&& function_declaration_info) {
|
2023-04-17 11:31:00 +03:00
|
|
|
size_t id = 0;
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
|
|
|
|
id = global_info_.functions_.size();
|
|
|
|
|
namespace_stack_.back()->functions[name] = id;
|
|
|
|
|
global_info_.functions_.emplace_back();
|
|
|
|
|
global_info_.functions_.back().declaration = std::move(function_declaration_info);
|
|
|
|
|
} else {
|
|
|
|
|
id = function_id_iter->second;
|
|
|
|
|
global_info_.functions_[id].declaration = std::move(function_declaration_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
|
|
|
|
FunctionDefinitionInfo&& function_definition_info) {
|
2023-04-17 11:31:00 +03:00
|
|
|
size_t id = 0;
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
|
|
|
|
id = global_info_.functions_.size();
|
|
|
|
|
namespace_stack_.back()->functions[name] = id;
|
|
|
|
|
global_info_.functions_.emplace_back();
|
|
|
|
|
global_info_.functions_.back().definition = std::move(function_definition_info);
|
|
|
|
|
} else {
|
|
|
|
|
id = function_id_iter->second;
|
|
|
|
|
global_info_.functions_[id].definition = std::move(function_definition_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
|
|
|
|
TypeInfo&& type_info) {
|
2023-04-17 11:31:00 +03:00
|
|
|
size_t id = 0;
|
|
|
|
|
|
|
|
|
|
auto function_id_iter = namespace_stack_.back()->functions.find(type);
|
|
|
|
|
|
|
|
|
|
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
|
|
|
|
id = global_info_.types_.size();
|
|
|
|
|
namespace_stack_.back()->functions[type] = id;
|
|
|
|
|
global_info_.types_.push_back(std::move(type_info));
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
|
|
|
|
TypeclassInfo&& typeclass_info) {
|
2023-04-17 11:31:00 +03:00
|
|
|
size_t id = 0;
|
|
|
|
|
|
|
|
|
|
auto function_id_iter = namespace_stack_.back()->functions.find(typeclass);
|
|
|
|
|
|
|
|
|
|
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
|
|
|
|
id = global_info_.typeclasses_.size();
|
|
|
|
|
namespace_stack_.back()->functions[typeclass] = id;
|
|
|
|
|
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return id;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
2023-04-17 11:31:00 +03:00
|
|
|
return FindSomething<NamespaceInfo*>(path,
|
2023-04-17 11:14:33 +03:00
|
|
|
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
|
|
|
|
return current_namespace;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
|
|
|
|
const std::optional<std::vector<std::string>>& path,
|
|
|
|
|
const std::string& name) {
|
2023-04-17 11:31:00 +03:00
|
|
|
return FindSomething<utils::IdType>(path,
|
|
|
|
|
[name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
auto function_info_iter = current_namespace->functions.find(name);
|
|
|
|
|
if (function_info_iter == current_namespace->functions.end()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return function_info_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
|
|
|
|
const std::optional<std::vector<std::string>>& path,
|
|
|
|
|
const std::string& type,
|
|
|
|
|
const std::string& name) {
|
|
|
|
|
// TODO: remove overhead
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
|
|
|
|
[type, name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
|
|
|
|
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& variable_namespace : variable_namespaces_iter->second) {
|
|
|
|
|
auto method_iter = variable_namespace.functions.find(name);
|
|
|
|
|
|
|
|
|
|
if (method_iter == variable_namespace.functions.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return method_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
|
|
|
|
const std::optional<std::vector<std::string>>& path,
|
|
|
|
|
const std::string type) {
|
2023-04-17 11:31:00 +03:00
|
|
|
return FindSomething<utils::IdType>(path,
|
|
|
|
|
[type] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
auto type_info_iter = current_namespace->types.find(type);
|
|
|
|
|
if (type_info_iter == current_namespace->types.end()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return type_info_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
|
|
|
|
const std::optional<std::vector<std::string>>& path,
|
|
|
|
|
const std::string typeclass) {
|
2023-04-17 11:31:00 +03:00
|
|
|
return FindSomething<utils::IdType>(path,
|
|
|
|
|
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
|
|
|
|
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return typeclass_info_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2023-04-17 11:31:00 +03:00
|
|
|
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
2023-04-17 11:14:33 +03:00
|
|
|
const std::optional<std::vector<std::string>>& path,
|
2023-04-17 11:31:00 +03:00
|
|
|
std::function<std::optional<T>(NamespaceInfo*)> search_func) {
|
2023-04-17 11:14:33 +03:00
|
|
|
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
|
|
|
|
NamespaceInfo* current_namespace = nullptr;
|
|
|
|
|
if (path.has_value()) {
|
|
|
|
|
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
|
|
|
|
|
|
|
|
|
if (!maybe_namespace.has_value()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
current_namespace = namespace_stack_[i];
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
std::optional<T> result = search_func(current_namespace);
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
if (result.has_value()) {
|
|
|
|
|
return result.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
|
|
|
|
NamespaceInfo* current_namespace,
|
|
|
|
|
const std::vector<std::string>& path) {
|
|
|
|
|
NamespaceInfo* next_namespace = current_namespace;
|
|
|
|
|
for (auto& name : path) {
|
|
|
|
|
auto next_namespace_iter = next_namespace->namespaces.find(name);
|
|
|
|
|
if (next_namespace_iter == next_namespace->namespaces.end()) {
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
next_namespace = &next_namespace_iter->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next_namespace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace info
|