mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 21:17:11 +00:00
global info search methods
This commit is contained in:
parent
aa4e9fa721
commit
9c25cb1c6f
2 changed files with 124 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
// for clangd
|
// for clangd
|
||||||
|
|
@ -26,21 +27,23 @@ public:
|
||||||
void AddEnterNamespace(const std::string& name,
|
void AddEnterNamespace(const std::string& name,
|
||||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||||
const std::optional<std::string>& variable = std::nullopt) {
|
const std::optional<std::string>& variable = std::nullopt) {
|
||||||
NamespaceInfo* namespace_info = &namespace_stack_.back()->namespaces[name].emplace_back();
|
NamespaceInfo* namespace_info = nullptr;
|
||||||
namespace_stack_.push_back(namespace_info);
|
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->modifier = modifier;
|
|
||||||
namespace_info->variable = variable;
|
|
||||||
namespace_info->type_name = name;
|
namespace_info->type_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
void EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
||||||
auto& namespace_parts = namespace_stack_.back()->namespaces[name];
|
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
||||||
for (auto& part : namespace_parts) {
|
|
||||||
if (part.variable == std::nullopt) {
|
|
||||||
namespace_stack_.push_back(&part);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExitAllNameNamespaces(const std::string& name) {
|
void ExitAllNameNamespaces(const std::string& name) {
|
||||||
|
|
@ -71,14 +74,13 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddFunctionDefinition(const std::string& name,
|
void AddFunctionDefinition(const std::string& name,
|
||||||
FunctionDefinitionInfo&& function_definition_info) {
|
FunctionDefinitionInfo&& function_definition_info) {
|
||||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||||
|
|
||||||
function_info->definition = std::move(function_definition_info);
|
function_info->definition = std::move(function_definition_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddType(const std::string& type,
|
void AddType(const std::string& type, TypeInfo&& type_info) {
|
||||||
TypeInfo&& type_info) {
|
|
||||||
namespace_stack_.back()->types[type] = std::move(type_info);
|
namespace_stack_.back()->types[type] = std::move(type_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,12 +89,115 @@ public:
|
||||||
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FindFunction(const std::vector<std::string>& path) {
|
std::optional<NamespaceInfo*> FindNamespace(const std::vector<std::string>& path) {
|
||||||
// TODO
|
return FindSomething<NamespaceInfo>(path,
|
||||||
|
[](NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||||
|
return current_namespace;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FindType(const std::vector<std::string>& path) {
|
std::optional<FunctionInfo*> FindFunction(const std::vector<std::string>& path,
|
||||||
// TODO
|
const std::string& name) {
|
||||||
|
return FindSomething<FunctionInfo>(path,
|
||||||
|
[name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||||
|
|
||||||
|
auto function_info_iter = current_namespace->functions.find(name);
|
||||||
|
if (function_info_iter == current_namespace->functions.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &function_info_iter->second;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<FunctionInfo*> FindMethod(const std::vector<std::string>& path,
|
||||||
|
const std::string& type,
|
||||||
|
const std::string& name) {
|
||||||
|
// TODO: remove overhead
|
||||||
|
|
||||||
|
return FindSomething<FunctionInfo>(path,
|
||||||
|
[type, name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &method_iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<TypeInfo*> FindType(const std::vector<std::string>& path,
|
||||||
|
const std::string type) {
|
||||||
|
return FindSomething<TypeInfo>(path,
|
||||||
|
[type](NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
|
||||||
|
|
||||||
|
auto type_info_iter = current_namespace->types.find(type);
|
||||||
|
if (type_info_iter == current_namespace->types.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &type_info_iter->second;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<TypeclassInfo*> FindTypeclass(const std::vector<std::string>& path,
|
||||||
|
const std::string typeclass) {
|
||||||
|
return FindSomething<TypeclassInfo>(path,
|
||||||
|
[typeclass](NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
|
||||||
|
|
||||||
|
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||||
|
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &typeclass_info_iter->second;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename T>
|
||||||
|
std::optional<T*> FindSomething(const std::vector<std::string>& path,
|
||||||
|
std::function<std::optional<T*>(NamespaceInfo*)> search_func) {
|
||||||
|
for (ssize_t i = namespace_stack_.size(); i >= 0; --i) {
|
||||||
|
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path);
|
||||||
|
|
||||||
|
if (!maybe_namespace.has_value()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<T*> result = search_func(maybe_namespace.value());
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return result.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<NamespaceInfo*> 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,8 @@ struct NamespaceInfo {
|
||||||
std::unordered_map<std::string, TypeInfo> types;
|
std::unordered_map<std::string, TypeInfo> types;
|
||||||
std::unordered_map<std::string, TypeclassInfo> typeclasses;
|
std::unordered_map<std::string, TypeclassInfo> typeclasses;
|
||||||
std::unordered_map<std::string, FunctionInfo> functions;
|
std::unordered_map<std::string, FunctionInfo> functions;
|
||||||
std::unordered_map<std::string, std::vector<NamespaceInfo>> namespaces;
|
std::unordered_map<std::string, NamespaceInfo> namespaces;
|
||||||
|
std::unordered_map<std::string, std::vector<NamespaceInfo>> variable_namespaces;
|
||||||
|
|
||||||
std::optional<Modifier> modifier;
|
std::optional<Modifier> modifier;
|
||||||
std::optional<std::string> variable;
|
std::optional<std::string> variable;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue