mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-07 15:38:47 +00:00
find_symbols_visitor, global_info fixed
This commit is contained in:
parent
4d0b527416
commit
a512a92f92
9 changed files with 289 additions and 277 deletions
|
|
@ -1,10 +1,11 @@
|
|||
// for clangd
|
||||
#include "../include/global_info.hpp"
|
||||
#include "../include/error_handling.hpp"
|
||||
#include <variant>
|
||||
|
||||
namespace info {
|
||||
|
||||
void GlobalInfo::NamespaceVisitor::AddImport(ImportInfo&& import_info,
|
||||
void GlobalInfo::NamespaceVisitor::AddImport(definition::Import&& import_info,
|
||||
const std::optional<std::string>& name) {
|
||||
if (name.has_value()) {
|
||||
global_info_.usages_[name.value()] = std::move(import_info);
|
||||
|
|
@ -14,15 +15,13 @@ void GlobalInfo::NamespaceVisitor::AddImport(ImportInfo&& 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();
|
||||
const std::optional<definition::Namespace::Modifier>& modifier) {
|
||||
definition::Namespace* namespace_info = nullptr;
|
||||
if (modifier.has_value()) {
|
||||
namespace_info = &namespace_stack_.back()->variable_namespaces[name];
|
||||
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);
|
||||
|
|
@ -55,7 +54,7 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info) {
|
||||
definition::FunctionDeclaration&& function_declaration_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
|
|
@ -78,7 +77,7 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::st
|
|||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info) {
|
||||
definition::FunctionDefinition&& function_definition_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(name);
|
||||
|
|
@ -100,53 +99,106 @@ utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::str
|
|||
return id;
|
||||
}
|
||||
|
||||
// TODO - find constructors, etc.
|
||||
// TODO: internal types, etc.
|
||||
// TODO: extended constructor names
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||
TypeInfo&& type_info) {
|
||||
definition::Type&& type_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(type);
|
||||
auto type_id_iter = namespace_stack_.back()->types.find(type);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (type_id_iter == namespace_stack_.back()->types.end()) {
|
||||
id = global_info_.types_.size();
|
||||
namespace_stack_.back()->functions[type] = id;
|
||||
namespace_stack_.back()->types[type] = id;
|
||||
global_info_.types_.push_back(std::move(type_info));
|
||||
} else {
|
||||
// error
|
||||
error_handling::HandleTypecheckError("More then one type with the same name in namespace");
|
||||
}
|
||||
|
||||
definition::Type& moved_type_info = global_info_.types_.back();
|
||||
|
||||
if (std::holds_alternative<definition::AnyType>(moved_type_info.type)) {
|
||||
definition::AnyType& any_type_info = std::get<definition::AnyType>(moved_type_info.type);
|
||||
if (std::holds_alternative<std::unique_ptr<interpreter::tokens::VariantType>>(*any_type_info.value)) {
|
||||
interpreter::tokens::VariantType& variant_type_info = *std::get<std::unique_ptr<interpreter::tokens::VariantType>>(*any_type_info.value);
|
||||
for (size_t i = 0; i < variant_type_info.constructors.size(); ++i) {
|
||||
std::string constructor_name;
|
||||
definition::Constructor constructor_info;
|
||||
|
||||
constructor_info.type_id = id;
|
||||
constructor_info.order = i;
|
||||
|
||||
if (std::holds_alternative<interpreter::tokens::Constructor>(variant_type_info.constructors[i])) {
|
||||
constructor_name = std::get<interpreter::tokens::Constructor>(variant_type_info.constructors[i]);
|
||||
} else if (std::holds_alternative<
|
||||
std::unique_ptr<interpreter::tokens::TupleType>>(variant_type_info.constructors[i])) {
|
||||
auto maybe_constructor_name = std::get<std::unique_ptr<interpreter::tokens::TupleType>>(variant_type_info.constructors[i])->type;
|
||||
|
||||
if (maybe_constructor_name.has_value()) {
|
||||
constructor_name = maybe_constructor_name.value();
|
||||
} else {
|
||||
constructor_name = type;
|
||||
}
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
constructor_info.name = constructor_name;
|
||||
|
||||
AddConstructor(constructor_name, std::move(constructor_info));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||
TypeclassInfo&& typeclass_info) {
|
||||
definition::Typeclass&& typeclass_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto function_id_iter = namespace_stack_.back()->functions.find(typeclass);
|
||||
auto typeclass_id_iter = namespace_stack_.back()->typeclasses.find(typeclass);
|
||||
|
||||
if (function_id_iter == namespace_stack_.back()->functions.end()) {
|
||||
if (typeclass_id_iter == namespace_stack_.back()->typeclasses.end()) {
|
||||
id = global_info_.typeclasses_.size();
|
||||
namespace_stack_.back()->functions[typeclass] = id;
|
||||
namespace_stack_.back()->typeclasses[typeclass] = id;
|
||||
global_info_.typeclasses_.push_back(std::move(typeclass_info));
|
||||
} else {
|
||||
// error
|
||||
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace");
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<NamespaceInfo*>(path,
|
||||
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||
utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& constructor,
|
||||
definition::Constructor&& constructor_info) {
|
||||
size_t id = 0;
|
||||
|
||||
auto constructor_id_iter = namespace_stack_.back()->constructors.find(constructor);
|
||||
|
||||
if (constructor_id_iter == namespace_stack_.back()->constructors.end()) {
|
||||
id = global_info_.constructors_.size();
|
||||
namespace_stack_.back()->constructors[constructor] = id;
|
||||
global_info_.constructors_.push_back(std::move(constructor_info));
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace");
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||
return FindSomething<definition::Namespace*>(path,
|
||||
[] (definition::Namespace* current_namespace) -> std::optional<definition::Namespace*> {
|
||||
return current_namespace;
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindFunction(
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
[name] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto function_info_iter = current_namespace->functions.find(name);
|
||||
if (function_info_iter == current_namespace->functions.end()) {
|
||||
|
|
@ -157,39 +209,34 @@ std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindFunc
|
|||
});
|
||||
}
|
||||
|
||||
std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||
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
|
||||
|
||||
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
||||
[type, name] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type, name] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
||||
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
||||
auto variable_namespace_iter = current_namespace->variable_namespaces.find(type);
|
||||
if (variable_namespace_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;
|
||||
auto method_iter = variable_namespace_iter->second.functions.find(name);
|
||||
if (method_iter == variable_namespace_iter->second.functions.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
return method_iter->second;
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindType(
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string type) {
|
||||
const std::string& type) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[type] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
[type] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto type_info_iter = current_namespace->types.find(type);
|
||||
if (type_info_iter == current_namespace->types.end()) {
|
||||
|
|
@ -200,11 +247,11 @@ std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindType
|
|||
});
|
||||
}
|
||||
|
||||
std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string typeclass) {
|
||||
const std::string& typeclass) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<utils::IdType> {
|
||||
[typeclass] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||
|
|
@ -215,12 +262,27 @@ std::optional<std::vector<utils::IdType>> GlobalInfo::NamespaceVisitor::FindType
|
|||
});
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindConstructor(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& constructor) {
|
||||
return FindSomething<utils::IdType>(path,
|
||||
[constructor] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
|
||||
|
||||
auto constructor_info_iter = current_namespace->constructors.find(constructor);
|
||||
if (constructor_info_iter == current_namespace->constructors.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return constructor_info_iter->second;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(NamespaceInfo*)> search_func) {
|
||||
std::function<std::optional<T>(definition::Namespace*)> search_func) {
|
||||
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||
NamespaceInfo* current_namespace = nullptr;
|
||||
definition::Namespace* current_namespace = nullptr;
|
||||
if (path.has_value()) {
|
||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||
|
||||
|
|
@ -241,10 +303,10 @@ std::optional<T> GlobalInfo::NamespaceVisitor::FindSomething(
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
NamespaceInfo* current_namespace,
|
||||
std::optional<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||
definition::Namespace* current_namespace,
|
||||
const std::vector<std::string>& path) {
|
||||
NamespaceInfo* next_namespace = current_namespace;
|
||||
definition::Namespace* 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()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue