mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 13:07:13 +00:00
going to change symbol table structure
This commit is contained in:
parent
9c25cb1c6f
commit
782a48c4ff
11 changed files with 535 additions and 354 deletions
|
|
@ -6,202 +6,90 @@
|
|||
|
||||
// for clangd
|
||||
#include "symbols_info.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
// TODO: partitions
|
||||
class GlobalInfo {
|
||||
friend class NamespaceVisitor;
|
||||
public:
|
||||
GlobalInfo() {
|
||||
namespace_stack_.push_back(&global_namespace_);
|
||||
class NamespaceVisitor {
|
||||
friend GlobalInfo;
|
||||
NamespaceVisitor() = delete;
|
||||
public:
|
||||
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt);
|
||||
|
||||
void AddEnterNamespace(const std::string& name,
|
||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||
const std::optional<std::string>& variable = std::nullopt);
|
||||
|
||||
void EnterNamespace(const std::string& name);
|
||||
|
||||
void ExitNamespace();
|
||||
|
||||
void ToGlobalNamespace();
|
||||
|
||||
utils::IdType AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info);
|
||||
|
||||
utils::IdType AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info);
|
||||
|
||||
utils::IdType AddType(const std::string& type, TypeInfo&& type_info);
|
||||
|
||||
utils::IdType AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info);
|
||||
|
||||
std::optional<NamespaceInfo*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||
|
||||
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name);
|
||||
|
||||
std::optional<utils::IdType> FindMethod(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type,
|
||||
const std::string& name);
|
||||
|
||||
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string type);
|
||||
|
||||
std::optional<utils::IdType> FindTypeclass(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string typeclass);
|
||||
private:
|
||||
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
|
||||
namespace_stack_({&global_info.global_namespace_}) {}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> FindSomething(const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T*>(NamespaceInfo*)> search_func);
|
||||
|
||||
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
||||
const std::vector<std::string>& path);
|
||||
private:
|
||||
std::vector<NamespaceInfo*> namespace_stack_;
|
||||
|
||||
GlobalInfo& global_info_;
|
||||
};
|
||||
|
||||
NamespaceVisitor CreateVisitor() {
|
||||
return NamespaceVisitor(*this);
|
||||
}
|
||||
|
||||
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt) {
|
||||
if (name.has_value()) {
|
||||
usages_[name.value()] = std::move(import_info);
|
||||
} else {
|
||||
imports_.push_back(std::move(import_info));
|
||||
}
|
||||
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
||||
return functions_.GetValue(id);
|
||||
}
|
||||
|
||||
void AddEnterNamespace(const std::string& name,
|
||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||
const std::optional<std::string>& variable = std::nullopt) {
|
||||
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;
|
||||
TypeInfo* GetTypeInfo(utils::IdType id) {
|
||||
return types_.GetValue(id);
|
||||
}
|
||||
|
||||
void EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
||||
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
||||
}
|
||||
|
||||
void ExitAllNameNamespaces(const std::string& name) {
|
||||
while(namespace_stack_.size() > 1 && namespace_stack_.back()->type_name == name) {
|
||||
namespace_stack_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
void ExitNamespace() {
|
||||
if (namespace_stack_.size() <= 1) {
|
||||
// error
|
||||
return;
|
||||
}
|
||||
|
||||
namespace_stack_.pop_back();
|
||||
}
|
||||
|
||||
void ToGlobalNamespace() {
|
||||
namespace_stack_.clear();
|
||||
namespace_stack_.push_back(&global_namespace_);
|
||||
}
|
||||
|
||||
void AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
|
||||
function_info->declaration = std::move(function_declaration_info);
|
||||
}
|
||||
|
||||
void AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info) {
|
||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||
|
||||
function_info->definition = std::move(function_definition_info);
|
||||
}
|
||||
|
||||
void AddType(const std::string& type, TypeInfo&& type_info) {
|
||||
namespace_stack_.back()->types[type] = std::move(type_info);
|
||||
}
|
||||
|
||||
void AddTypeclass(const std::string& typeclass,
|
||||
TypeclassInfo&& typeclass_info) {
|
||||
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> FindNamespace(const std::vector<std::string>& path) {
|
||||
return FindSomething<NamespaceInfo>(path,
|
||||
[](NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||
return current_namespace;
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<FunctionInfo*> FindFunction(const std::vector<std::string>& path,
|
||||
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;
|
||||
});
|
||||
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
||||
return typeclasses_.GetValue(id);
|
||||
}
|
||||
|
||||
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:
|
||||
std::vector<NamespaceInfo*> namespace_stack_;
|
||||
utils::Storage<FunctionInfo*> functions_;
|
||||
utils::Storage<TypeInfo*> types_;
|
||||
utils::Storage<TypeclassInfo*> typeclasses_;
|
||||
|
||||
NamespaceInfo global_namespace_;
|
||||
std::vector<ImportInfo> imports_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue