2023-05-08 20:34:36 +03:00
|
|
|
#include <variant>
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
// for clangd
|
|
|
|
|
#include "../include/global_info.hpp"
|
2023-05-08 20:34:36 +03:00
|
|
|
#include "../include/types.hpp"
|
2023-04-22 19:30:16 +03:00
|
|
|
#include "../include/error_handling.hpp"
|
2023-05-08 20:34:36 +03:00
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
namespace info {
|
|
|
|
|
|
2023-04-29 18:42:30 +03:00
|
|
|
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,
|
2023-05-08 20:34:36 +03:00
|
|
|
std::optional<utils::IsConstModifier> modifier,
|
|
|
|
|
const interpreter::tokens::BaseNode& base_node) {
|
|
|
|
|
if (type::ToInternalType(name).has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Can't define basic type namespace", base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
size_t id = global_info_.namespaces_.size();
|
|
|
|
|
global_info_.namespaces_.emplace_back();
|
|
|
|
|
definition::Namespace* namespace_info = &global_info_.namespaces_.back();
|
|
|
|
|
|
2023-04-29 18:42:30 +03:00
|
|
|
if (modifier.has_value()) {
|
2023-05-05 16:35:13 +03:00
|
|
|
if (modifier.value() == utils::IsConstModifier::Const) {
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].const_namespaces[name] = id;
|
2023-05-05 16:35:13 +03:00
|
|
|
} else {
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].var_namespaces[name] = id;
|
2023-05-05 16:35:13 +03:00
|
|
|
}
|
2023-04-17 11:14:33 +03:00
|
|
|
|
|
|
|
|
namespace_info->modifier = modifier;
|
|
|
|
|
} else {
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].namespaces[name] = id;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-02 16:51:47 +03:00
|
|
|
namespace_info->parent_namespace = namespace_stack_.back();
|
|
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
namespace_info->type_name = name;
|
2023-05-13 18:53:54 +03:00
|
|
|
|
|
|
|
|
namespace_stack_.push_back(id);
|
2023-04-17 18:56:58 +03:00
|
|
|
current_path_.push_back(name);
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
// better code ??
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name,
|
|
|
|
|
std::optional<utils::IsConstModifier> modifier) {
|
|
|
|
|
|
|
|
|
|
if (!modifier.has_value()) {
|
|
|
|
|
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
|
|
|
|
auto namespace_iter = global_info_.namespaces_[namespace_stack_[i]].namespaces.find(name);
|
|
|
|
|
if (namespace_iter != global_info_.namespaces_[namespace_stack_[i]].namespaces.end()) {
|
|
|
|
|
namespace_stack_.push_back(namespace_iter->second);
|
|
|
|
|
current_path_.push_back(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (modifier.has_value() && modifier.value() == utils::IsConstModifier::Const) {
|
|
|
|
|
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
|
|
|
|
auto const_namespace_iter = global_info_.namespaces_[namespace_stack_[i]].const_namespaces.find(name);
|
|
|
|
|
if (const_namespace_iter != global_info_.namespaces_[namespace_stack_[i]].const_namespaces.end()) {
|
|
|
|
|
namespace_stack_.push_back(const_namespace_iter->second);
|
|
|
|
|
current_path_.push_back(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (ssize_t i = (ssize_t)namespace_stack_.size() - 1; i >= 0; --i) {
|
|
|
|
|
auto var_namespace_iter = global_info_.namespaces_[namespace_stack_[i]].var_namespaces.find(name);
|
|
|
|
|
if (var_namespace_iter != global_info_.namespaces_[namespace_stack_[i]].var_namespaces.end()) {
|
|
|
|
|
namespace_stack_.push_back(var_namespace_iter->second);
|
|
|
|
|
current_path_.push_back(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-02 15:18:08 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
error_handling::HandleInternalError("Can't find namespace " + name,
|
2023-05-07 19:52:35 +03:00
|
|
|
"GlobalInfo.NamespaceVisitor.EnterNamespace");
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
|
|
|
|
if (namespace_stack_.size() <= 1) {
|
2023-05-13 18:53:54 +03:00
|
|
|
error_handling::HandleInternalError("Can't exit from global namespace",
|
|
|
|
|
"GlobalInfo.NamespaceVisitor.ExitNamespace");
|
2023-04-17 11:14:33 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace_stack_.pop_back();
|
2023-04-17 18:56:58 +03:00
|
|
|
current_path_.pop_back();
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
|
|
|
|
namespace_stack_.clear();
|
2023-04-17 18:56:58 +03:00
|
|
|
current_path_.clear();
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
namespace_stack_.push_back(global_info_.GlobalNamespaceId);
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-07 19:52:35 +03:00
|
|
|
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-05-13 18:53:54 +03:00
|
|
|
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
2023-04-17 11:31:00 +03:00
|
|
|
id = global_info_.functions_.size();
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].functions[name] = id;
|
2023-04-17 11:31:00 +03:00
|
|
|
global_info_.functions_.emplace_back();
|
2023-05-07 19:52:35 +03:00
|
|
|
global_info_.functions_.back().argument_count = function_declaration_info.argument_types.size(); // add return type
|
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()) {
|
2023-05-07 19:52:35 +03:00
|
|
|
error_handling::HandleInternalError("Not same argument count in function definition and declaration",
|
|
|
|
|
"GlobalInfo.NamespaceVisitor. AddFunctionDeclaration");
|
2023-04-22 19:30:16 +03:00
|
|
|
}
|
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,
|
2023-04-29 18:42:30 +03:00
|
|
|
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-05-13 18:53:54 +03:00
|
|
|
auto function_id_iter = global_info_.namespaces_[namespace_stack_.back()].functions.find(name);
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
if (function_id_iter == global_info_.namespaces_[namespace_stack_.back()].functions.end()) {
|
2023-04-17 11:31:00 +03:00
|
|
|
id = global_info_.functions_.size();
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].functions[name] = id;
|
2023-04-17 11:31:00 +03:00
|
|
|
global_info_.functions_.emplace_back();
|
2023-05-07 19:52:35 +03:00
|
|
|
global_info_.functions_.back().argument_count = function_definition_info.argument_names.size() + 1;
|
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-05-07 19:52:35 +03:00
|
|
|
if (global_info_.functions_.back().argument_count != function_definition_info.argument_names.size() + 1) {
|
2023-04-22 19:30:16 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +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,
|
2023-05-05 16:35:13 +03:00
|
|
|
definition::Type&& type_info,
|
|
|
|
|
const interpreter::tokens::BaseNode& base_node) {
|
2023-05-08 20:34:36 +03:00
|
|
|
if (type::ToInternalType(type).has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Can't redefine basic type", base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
size_t id = 0;
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
2023-04-17 11:31:00 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
if (type_id_iter == global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
2023-04-17 11:31:00 +03:00
|
|
|
id = global_info_.types_.size();
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].types[type] = id;
|
2023-04-17 11:31:00 +03:00
|
|
|
global_info_.types_.push_back(std::move(type_info));
|
|
|
|
|
} else {
|
2023-05-05 16:35:13 +03:00
|
|
|
error_handling::HandleTypecheckError("More then one type with the same name in namespace", base_node);
|
|
|
|
|
} // TODO: typecheck error??
|
2023-04-29 18:42:30 +03:00
|
|
|
|
|
|
|
|
definition::Type& moved_type_info = global_info_.types_.back();
|
|
|
|
|
|
2023-05-04 16:11:25 +03:00
|
|
|
// TODO: constructors for tuple types, function types (??), array types (??), ...
|
|
|
|
|
|
2023-04-29 18:42:30 +03:00
|
|
|
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])) {
|
2023-05-04 16:11:25 +03:00
|
|
|
constructor_info.constructor_tuple_node =
|
|
|
|
|
std::get<std::unique_ptr<interpreter::tokens::TupleType>>(variant_type_info.constructors[i]).get();
|
|
|
|
|
|
|
|
|
|
auto maybe_constructor_name = constructor_info.constructor_tuple_node.value()->type;
|
2023-04-29 18:42:30 +03:00
|
|
|
|
|
|
|
|
if (maybe_constructor_name.has_value()) {
|
|
|
|
|
constructor_name = maybe_constructor_name.value();
|
|
|
|
|
} else {
|
|
|
|
|
constructor_name = type;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor_info.name = constructor_name;
|
|
|
|
|
|
2023-05-05 16:35:13 +03:00
|
|
|
AddConstructor(constructor_name, std::move(constructor_info), base_node);
|
2023-04-29 18:42:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
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,
|
2023-05-05 16:35:13 +03:00
|
|
|
definition::AbstractType&& abstract_type_info,
|
|
|
|
|
const interpreter::tokens::BaseNode& base_node) {
|
2023-05-08 20:34:36 +03:00
|
|
|
if (type::ToInternalType(abstract_type).has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Can't redefine basic type as abstract type", base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 15:18:08 +03:00
|
|
|
if (!FindAbstractType(abstract_type).has_value()) {
|
2023-05-07 19:52:35 +03:00
|
|
|
utils::IdType id = global_info_.abstract_types_.size();
|
2023-05-02 15:18:08 +03:00
|
|
|
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-07 19:52:35 +03:00
|
|
|
|
|
|
|
|
return id;
|
2023-05-02 15:18:08 +03:00
|
|
|
}
|
2023-04-17 11:31:00 +03:00
|
|
|
|
2023-05-07 19:52:35 +03:00
|
|
|
error_handling::HandleTypecheckError("More then one abstract type with the same name in namespace",
|
|
|
|
|
base_node);
|
2023-05-02 15:18:08 +03:00
|
|
|
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,
|
2023-05-05 16:35:13 +03:00
|
|
|
definition::Typeclass&& typeclass_info,
|
|
|
|
|
const interpreter::tokens::BaseNode& base_node) {
|
2023-05-08 20:34:36 +03:00
|
|
|
if (type::ToInternalType(typeclass).has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Can't redefine basic type as typeclass", base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 15:18:08 +03:00
|
|
|
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-13 18:53:54 +03:00
|
|
|
|
|
|
|
|
return id;
|
2023-04-29 18:42:30 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-02 15:18:08 +03:00
|
|
|
|
2023-05-05 16:35:13 +03:00
|
|
|
error_handling::HandleTypecheckError("More then one typeclass with the same name in namespace", base_node);
|
2023-05-02 15:18:08 +03:00
|
|
|
return 0;
|
2023-04-29 18:42:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddConstructor(const std::string& constructor,
|
2023-05-05 16:35:13 +03:00
|
|
|
definition::Constructor&& constructor_info,
|
|
|
|
|
const interpreter::tokens::BaseNode& base_node) {
|
2023-05-08 20:34:36 +03:00
|
|
|
if (type::ToInternalType(constructor).has_value()) {
|
|
|
|
|
error_handling::HandleTypecheckError("Can't redefine basic type as constructor", base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto constructor_id_iter = global_info_.namespaces_[namespace_stack_.back()].constructors.find(constructor);
|
2023-04-29 18:42:30 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
if (constructor_id_iter == global_info_.namespaces_[namespace_stack_.back()].constructors.end()) {
|
2023-05-02 15:18:08 +03:00
|
|
|
size_t id = global_info_.constructors_.size();
|
2023-05-13 18:53:54 +03:00
|
|
|
global_info_.namespaces_[namespace_stack_.back()].constructors[constructor] = id;
|
2023-04-29 18:42:30 +03:00
|
|
|
global_info_.constructors_.push_back(std::move(constructor_info));
|
2023-05-13 18:53:54 +03:00
|
|
|
|
|
|
|
|
return id;
|
2023-04-17 11:31:00 +03:00
|
|
|
}
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-05 16:35:13 +03:00
|
|
|
error_handling::HandleTypecheckError("More then one constructor with the same name in namespace", base_node);
|
2023-05-13 18:53:54 +03:00
|
|
|
exit(1);
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-07 22:58:15 +03:00
|
|
|
utils::IdType GlobalInfo::NamespaceVisitor::AddPartition(const std::vector<std::string>& path,
|
|
|
|
|
interpreter::tokens::PartitionStatement* node) {
|
|
|
|
|
PartitionInfo partition;
|
|
|
|
|
|
2023-05-11 14:56:27 +03:00
|
|
|
partition.path.reserve(current_path_.size() + path.size() - 1);
|
2023-05-07 22:58:15 +03:00
|
|
|
partition.path = current_path_;
|
|
|
|
|
|
2023-05-11 14:56:27 +03:00
|
|
|
for (size_t i = 0; i + 1 < path.size(); ++i) {
|
|
|
|
|
partition.path.push_back(path[i]);
|
2023-05-07 22:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-11 14:56:27 +03:00
|
|
|
partition.name = path.back();
|
2023-05-07 22:58:15 +03:00
|
|
|
partition.node = node;
|
|
|
|
|
|
2023-05-11 14:56:27 +03:00
|
|
|
utils::IdType id = global_info_.partitions_.size();
|
|
|
|
|
|
2023-05-07 22:58:15 +03:00
|
|
|
global_info_.partitions_.push_back(partition);
|
2023-05-11 14:56:27 +03:00
|
|
|
global_info_.partitions_trie_.Insert(partition.path, id);
|
|
|
|
|
|
|
|
|
|
return id;
|
2023-05-07 22:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
|
|
|
|
return FindSomething<utils::IdType>(path,
|
|
|
|
|
[] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
return current_namespace;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +03:00
|
|
|
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,
|
2023-05-13 18:53:54 +03:00
|
|
|
[name, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto function_info_iter = global_info_.namespaces_[current_namespace].functions.find(name);
|
|
|
|
|
if (function_info_iter == global_info_.namespaces_[current_namespace].functions.end()) {
|
2023-04-17 11:14:33 +03:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return function_info_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +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,
|
2023-05-05 16:35:13 +03:00
|
|
|
const std::string& name,
|
|
|
|
|
utils::IsConstModifier modifier) {
|
2023-04-17 11:14:33 +03:00
|
|
|
// TODO: remove overhead
|
|
|
|
|
|
2023-04-17 11:31:00 +03:00
|
|
|
return GlobalInfo::NamespaceVisitor::FindSomething<utils::IdType>(path,
|
2023-05-13 18:53:54 +03:00
|
|
|
[type, name, modifier, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
2023-05-05 16:35:13 +03:00
|
|
|
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-05 16:35:13 +03:00
|
|
|
auto variable_namespace_iter =
|
|
|
|
|
(modifier == utils::IsConstModifier::Const
|
2023-05-13 18:53:54 +03:00
|
|
|
? global_info_.namespaces_[current_namespace].const_namespaces.find(type)
|
|
|
|
|
: global_info_.namespaces_[current_namespace].var_namespaces.find(type));
|
2023-05-05 16:35:13 +03:00
|
|
|
if (variable_namespace_iter ==
|
|
|
|
|
(modifier == utils::IsConstModifier::Const
|
2023-05-13 18:53:54 +03:00
|
|
|
? global_info_.namespaces_[current_namespace].const_namespaces.end()
|
|
|
|
|
: global_info_.namespaces_[current_namespace].var_namespaces.end())) {
|
2023-04-17 11:14:33 +03:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto method_iter = global_info_.namespaces_[variable_namespace_iter->second].functions.find(name);
|
|
|
|
|
if (method_iter == global_info_.namespaces_[variable_namespace_iter->second].functions.end()) {
|
2023-04-29 18:42:30 +03:00
|
|
|
return std::nullopt;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +03:00
|
|
|
return method_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +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,
|
2023-04-29 18:42:30 +03:00
|
|
|
const std::string& type) {
|
2023-04-17 11:31:00 +03:00
|
|
|
return FindSomething<utils::IdType>(path,
|
2023-05-13 18:53:54 +03:00
|
|
|
[type, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
2023-04-17 11:14:33 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto type_info_iter = global_info_.namespaces_[current_namespace].types.find(type);
|
|
|
|
|
if (type_info_iter == global_info_.namespaces_[current_namespace].types.end()) {
|
2023-04-17 11:14:33 +03:00
|
|
|
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) {
|
2023-05-13 18:53:54 +03:00
|
|
|
auto type_id_iter = global_info_.namespaces_[namespace_stack_.back()].types.find(type);
|
2023-05-02 16:16:55 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
if (type_id_iter != global_info_.namespaces_[namespace_stack_.back()].types.end()) {
|
2023-05-02 16:16:55 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-04-29 18:42:30 +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,
|
2023-05-13 18:53:54 +03:00
|
|
|
[constructor, this] (utils::IdType current_namespace) -> std::optional<utils::IdType> {
|
2023-04-29 18:42:30 +03:00
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
auto constructor_info_iter = global_info_.namespaces_[current_namespace].constructors.find(constructor);
|
|
|
|
|
if (constructor_info_iter == global_info_.namespaces_[current_namespace].constructors.end()) {
|
2023-04-29 18:42:30 +03:00
|
|
|
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,
|
2023-05-13 18:53:54 +03:00
|
|
|
std::function<std::optional<T>(utils::IdType)> search_func) {
|
2023-04-17 11:14:33 +03:00
|
|
|
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
2023-05-13 18:53:54 +03:00
|
|
|
utils::IdType current_namespace;
|
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;
|
|
|
|
|
}
|
2023-05-07 19:52:35 +03:00
|
|
|
|
|
|
|
|
current_namespace = maybe_namespace.value();
|
2023-04-17 11:14:33 +03:00
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 18:53:54 +03:00
|
|
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
|
|
|
|
utils::IdType current_namespace,
|
2023-04-17 11:14:33 +03:00
|
|
|
const std::vector<std::string>& path) {
|
2023-05-13 18:53:54 +03:00
|
|
|
utils::IdType next_namespace = current_namespace;
|
2023-04-17 11:14:33 +03:00
|
|
|
for (auto& name : path) {
|
2023-05-13 18:53:54 +03:00
|
|
|
auto next_namespace_iter = global_info_.namespaces_[next_namespace].namespaces.find(name);
|
|
|
|
|
if (next_namespace_iter == global_info_.namespaces_[next_namespace].namespaces.end()) {
|
2023-04-17 11:14:33 +03:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
2023-05-13 18:53:54 +03:00
|
|
|
next_namespace = next_namespace_iter->second;
|
2023-04-17 11:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next_namespace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace info
|