diff --git a/include/global_info.hpp b/include/global_info.hpp index e0047a8..92ed2f3 100644 --- a/include/global_info.hpp +++ b/include/global_info.hpp @@ -59,8 +59,8 @@ public: namespace_stack_({&global_info.global_namespace_}) {} template - std::optional FindSomething(const std::optional>& path, - std::function(NamespaceInfo*)> search_func); + std::optional FindSomething(const std::optional>& path, + std::function(NamespaceInfo*)> search_func); std::optional FindNamespaceIn(NamespaceInfo* current_namespace, const std::vector& path); @@ -74,22 +74,25 @@ public: return NamespaceVisitor(*this); } + // TODO: remember about vector realloc FunctionInfo* GetFunctionInfo(utils::IdType id) { - return functions_.GetValue(id); + return &functions_[id]; } + // TODO: remember about vector realloc TypeInfo* GetTypeInfo(utils::IdType id) { - return types_.GetValue(id); + return &types_[id]; } + // TODO: remember about vector realloc TypeclassInfo* GetTypeclassInfo(utils::IdType id) { - return typeclasses_.GetValue(id); + return &typeclasses_[id]; } private: - utils::Storage functions_; - utils::Storage types_; - utils::Storage typeclasses_; + std::vector functions_; + std::vector types_; + std::vector typeclasses_; NamespaceInfo global_namespace_; std::vector imports_; diff --git a/include/type_graph.hpp b/include/type_graph.hpp index ea5b7a6..78e8937 100644 --- a/include/type_graph.hpp +++ b/include/type_graph.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/include/utils.hpp b/include/utils.hpp index 5313b3b..c8135b9 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/src/.global_info.cpp.kate-swp b/src/.global_info.cpp.kate-swp deleted file mode 100644 index a2a7ac1..0000000 Binary files a/src/.global_info.cpp.kate-swp and /dev/null differ diff --git a/src/global_info.cpp b/src/global_info.cpp index edf7847..dbf1a42 100644 --- a/src/global_info.cpp +++ b/src/global_info.cpp @@ -50,36 +50,78 @@ void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() { utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name, FunctionDeclarationInfo&& function_declaration_info) { - FunctionInfo* function_info = &namespace_stack_.back()->functions[name]; + size_t id = 0; - function_info->declaration = std::move(function_declaration_info); + auto function_id_iter = namespace_stack_.back()->functions.find(name); - return global_info_.functions_.GetId(function_info); + 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(); + global_info_.functions_.back().declaration = std::move(function_declaration_info); + } else { + id = function_id_iter->second; + global_info_.functions_[id].declaration = std::move(function_declaration_info); + } + + return id; } utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name, FunctionDefinitionInfo&& function_definition_info) { - FunctionInfo* function_info = &namespace_stack_.back()->functions[name]; + size_t id = 0; - function_info->definition = std::move(function_definition_info); + auto function_id_iter = namespace_stack_.back()->functions.find(name); - return global_info_.functions_.GetId(function_info); + 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(); + global_info_.functions_.back().definition = std::move(function_definition_info); + } else { + id = function_id_iter->second; + global_info_.functions_[id].definition = std::move(function_definition_info); + } + + return id; } utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type, TypeInfo&& type_info) { - namespace_stack_.back()->types[type] = std::move(type_info); + size_t id = 0; + + auto function_id_iter = namespace_stack_.back()->functions.find(type); + + if (function_id_iter == namespace_stack_.back()->functions.end()) { + id = global_info_.types_.size(); + namespace_stack_.back()->functions[type] = id; + global_info_.types_.push_back(std::move(type_info)); + } else { + // error + } + + return id; } utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info) { - namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info); + size_t id = 0; - return global_info_.functions_.GetId(typeclass_info); + auto function_id_iter = namespace_stack_.back()->functions.find(typeclass); + + if (function_id_iter == namespace_stack_.back()->functions.end()) { + id = global_info_.typeclasses_.size(); + namespace_stack_.back()->functions[typeclass] = id; + global_info_.typeclasses_.push_back(std::move(typeclass_info)); + } else { + // error + } + + return id; } std::optional GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional>& path) { - return FindSomething(path, + return FindSomething(path, [] (NamespaceInfo* current_namespace) -> std::optional { return current_namespace; }); @@ -88,22 +130,16 @@ std::optional GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional GlobalInfo::NamespaceVisitor::FindFunction( const std::optional>& path, const std::string& name) { - auto result = FindSomething(path, - [name] (NamespaceInfo* current_namespace) -> std::optional { + return FindSomething(path, + [name] (NamespaceInfo* current_namespace) -> std::optional { 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; + return function_info_iter->second; }); - - if (result.has_value()) { - return global_info_.functions_.GetId(result.value()); - } else { - return std::nullopt; - } } std::optional GlobalInfo::NamespaceVisitor::FindMethod( @@ -112,8 +148,8 @@ std::optional GlobalInfo::NamespaceVisitor::FindMethod( const std::string& name) { // TODO: remove overhead - auto result = GlobalInfo::NamespaceVisitor::FindSomething(path, - [type, name] (NamespaceInfo* current_namespace) -> std::optional { + return GlobalInfo::NamespaceVisitor::FindSomething(path, + [type, name] (NamespaceInfo* current_namespace) -> std::optional { auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type); if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) { @@ -127,65 +163,47 @@ std::optional GlobalInfo::NamespaceVisitor::FindMethod( continue; } - return &method_iter->second; + return method_iter->second; } return std::nullopt; }); - - if (result.has_value()) { - return global_info_.functions_.GetId(result.value()); - } else { - return std::nullopt; - } } std::optional GlobalInfo::NamespaceVisitor::FindType( const std::optional>& path, const std::string type) { - auto result = FindSomething(path, - [type] (NamespaceInfo* current_namespace) -> std::optional { + return FindSomething(path, + [type] (NamespaceInfo* current_namespace) -> std::optional { 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; + return type_info_iter->second; }); - - if (result.has_value()) { - return global_info_.types_.GetId(result.value()); - } else { - return std::nullopt; - } } std::optional GlobalInfo::NamespaceVisitor::FindTypeclass( const std::optional>& path, const std::string typeclass) { - auto result = FindSomething(path, - [typeclass] (NamespaceInfo* current_namespace) -> std::optional { + return FindSomething(path, + [typeclass] (NamespaceInfo* current_namespace) -> std::optional { 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; + return typeclass_info_iter->second; }); - - if (result.has_value()) { - return global_info_.typeclasses_.GetId(result.value()); - } else { - return std::nullopt; - } } template -std::optional GlobalInfo::NamespaceVisitor::FindSomething( +std::optional GlobalInfo::NamespaceVisitor::FindSomething( const std::optional>& path, - std::function(NamespaceInfo*)> search_func) { + std::function(NamespaceInfo*)> search_func) { for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) { NamespaceInfo* current_namespace = nullptr; if (path.has_value()) { @@ -198,7 +216,7 @@ std::optional GlobalInfo::NamespaceVisitor::FindSomething( current_namespace = namespace_stack_[i]; } - std::optional result = search_func(current_namespace); + std::optional result = search_func(current_namespace); if (result.has_value()) { return result.value();