lang_2023/src/global_info.cpp

351 lines
13 KiB
C++
Raw Normal View History

2023-04-17 11:14:33 +03:00
// for clangd
#include "../include/global_info.hpp"
2023-04-22 19:30:16 +03:00
#include "../include/error_handling.hpp"
#include <variant>
2023-04-17 11:14:33 +03:00
namespace info {
void GlobalInfo::NamespaceVisitor::AddImport(definition::Import&& import_info,
2023-04-17 11:14:33 +03:00
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<definition::Namespace::Modifier>& modifier) {
definition::Namespace* namespace_info = nullptr;
if (modifier.has_value()) {
namespace_info = &namespace_stack_.back()->variable_namespaces[name];
2023-04-17 11:14:33 +03:00
namespace_stack_.push_back(namespace_info);
namespace_info->modifier = modifier;
} else {
namespace_info = &namespace_stack_.back()->namespaces[name];
namespace_stack_.push_back(namespace_info);
}
namespace_info->type_name = name;
current_path_.push_back(name);
2023-04-17 11:14:33 +03:00
}
2023-05-02 15:18:08 +03:00
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) {
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
auto namespace_iter = namespace_stack_[i]->namespaces.find(name);
if (namespace_iter != namespace_stack_[i]->namespaces.end()) {
namespace_stack_.push_back(&namespace_iter->second);
current_path_.push_back(name);
return;
}
}
error_handling::HandleInternalError("Can't find namespace", "GlobalInfo.NamespaceVisitor.EnterNamespace");
2023-04-17 11:14:33 +03:00
}
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
if (namespace_stack_.size() <= 1) {
// error
return;
}
namespace_stack_.pop_back();
current_path_.pop_back();
2023-04-17 11:14:33 +03:00
}
void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
namespace_stack_.clear();
current_path_.clear();
2023-04-17 11:14:33 +03:00
namespace_stack_.push_back(&global_info_.global_namespace_);
}
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
definition::FunctionDeclaration&& 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();
2023-04-22 19:30:16 +03:00
global_info_.functions_.back().argument_count = function_declaration_info.argument_types.size();
2023-04-17 11:31:00 +03:00
global_info_.functions_.back().declaration = std::move(function_declaration_info);
} else {
id = function_id_iter->second;
2023-04-22 19:30:16 +03:00
if (global_info_.functions_.back().argument_count != function_declaration_info.argument_types.size()) {
error_handling::HandleInternalError("Not same argument count in function definition and declaration", "GlobalInfo");
}
2023-04-17 11:31:00 +03:00
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,
definition::FunctionDefinition&& 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();
2023-04-22 19:30:16 +03:00
global_info_.functions_.back().argument_count = function_definition_info.argument_names.size();
2023-04-17 11:31:00 +03:00
global_info_.functions_.back().definition = std::move(function_definition_info);
} else {
id = function_id_iter->second;
2023-04-22 19:30:16 +03:00
if (global_info_.functions_.back().argument_count != function_definition_info.argument_names.size()) {
error_handling::HandleInternalError("Not same argument count in function definition and declaration", "GlobalInfo");
}
2023-04-17 11:31:00 +03:00
global_info_.functions_[id].definition = std::move(function_definition_info);
}
return id;
2023-04-17 11:14:33 +03:00
}
// TODO: internal types, etc.
2023-05-02 15:18:08 +03:00
// TODO: extended constructor names (point separated names)
2023-04-17 11:14:33 +03:00
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
definition::Type&& type_info) {
2023-04-17 11:31:00 +03:00
size_t id = 0;
auto type_id_iter = namespace_stack_.back()->types.find(type);
2023-04-17 11:31:00 +03:00
if (type_id_iter == namespace_stack_.back()->types.end()) {
2023-04-17 11:31:00 +03:00
id = global_info_.types_.size();
namespace_stack_.back()->types[type] = id;
2023-04-17 11:31:00 +03:00
global_info_.types_.push_back(std::move(type_info));
} else {
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));
}
}
2023-04-17 11:31:00 +03:00
}
return id;
2023-04-17 11:14:33 +03:00
}
2023-05-02 15:18:08 +03:00
// TODO: link abstract type with let definitions
utils::IdType GlobalInfo::NamespaceVisitor::AddAbstractType(const std::string& abstract_type,
definition::AbstractType&& abstract_type_info) {
if (!FindAbstractType(abstract_type).has_value()) {
size_t id = global_info_.abstract_types_.size();
global_info_.name_to_abstract_type_[abstract_type] = id;
2023-05-02 16:16:55 +03:00
global_info_.abstract_types_.push_back(std::move(abstract_type_info));
2023-05-02 15:18:08 +03:00
}
2023-04-17 11:31:00 +03:00
2023-05-02 15:18:08 +03:00
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace");
return 0;
}
2023-04-17 11:31:00 +03:00
2023-05-02 15:18:08 +03:00
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
definition::Typeclass&& typeclass_info) {
if (!FindTypeclass(typeclass).has_value()) {
size_t id = global_info_.typeclasses_.size();
global_info_.name_to_typeclass_[typeclass] = id;
2023-04-17 11:31:00 +03:00
global_info_.typeclasses_.push_back(std::move(typeclass_info));
}
2023-05-02 15:18:08 +03:00
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace");
return 0;
}
utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& constructor,
definition::Constructor&& constructor_info) {
auto constructor_id_iter = namespace_stack_.back()->constructors.find(constructor);
if (constructor_id_iter == namespace_stack_.back()->constructors.end()) {
2023-05-02 15:18:08 +03:00
size_t id = global_info_.constructors_.size();
namespace_stack_.back()->constructors[constructor] = id;
global_info_.constructors_.push_back(std::move(constructor_info));
2023-04-17 11:31:00 +03:00
}
2023-04-17 11:14:33 +03:00
2023-05-02 15:18:08 +03:00
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace");
return 0;
2023-04-17 11:14:33 +03:00
}
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*> {
2023-04-17 11:14:33 +03:00
return current_namespace;
});
}
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
2023-04-17 11:14:33 +03:00
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] (definition::Namespace* 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(
2023-04-17 11:14:33 +03:00
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] (definition::Namespace* current_namespace) -> std::optional<utils::IdType> {
2023-04-17 11:14:33 +03:00
auto variable_namespace_iter = current_namespace->variable_namespaces.find(type);
if (variable_namespace_iter == current_namespace->variable_namespaces.end()) {
2023-04-17 11:14:33 +03:00
return std::nullopt;
}
auto method_iter = variable_namespace_iter->second.functions.find(name);
if (method_iter == variable_namespace_iter->second.functions.end()) {
return std::nullopt;
2023-04-17 11:14:33 +03:00
}
return method_iter->second;
2023-04-17 11:14:33 +03:00
});
}
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
2023-04-17 11:14:33 +03:00
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] (definition::Namespace* 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
});
}
2023-05-02 16:16:55 +03:00
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindLocalType(const std::string& type) {
auto type_id_iter = namespace_stack_.back()->types.find(type);
if (type_id_iter != namespace_stack_.back()->types.end()) {
return type_id_iter->second;
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindAbstractType(const std::string& abstract_type) {
auto abstract_type_id_iter = global_info_.name_to_abstract_type_.find(abstract_type);
2023-04-17 11:14:33 +03:00
2023-05-02 15:18:08 +03:00
if (abstract_type_id_iter != global_info_.name_to_abstract_type_.end()) {
return abstract_type_id_iter->second;
}
2023-04-17 11:14:33 +03:00
2023-05-02 15:18:08 +03:00
return std::nullopt;
}
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(const std::string& typeclass) {
auto typeclass_id_iter = global_info_.name_to_typeclass_.find(typeclass);
if (typeclass_id_iter != global_info_.name_to_typeclass_.end()) {
return typeclass_id_iter->second;
}
return std::nullopt;
2023-04-17 11:14:33 +03:00
}
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;
});
}
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,
std::function<std::optional<T>(definition::Namespace*)> search_func) {
2023-04-17 11:14:33 +03:00
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
definition::Namespace* current_namespace = nullptr;
2023-04-17 11:14:33 +03:00
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<definition::Namespace*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
definition::Namespace* current_namespace,
2023-04-17 11:14:33 +03:00
const std::vector<std::string>& path) {
definition::Namespace* next_namespace = current_namespace;
2023-04-17 11:14:33 +03:00
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