lang_2023/include/global_info.hpp

127 lines
3.9 KiB
C++
Raw Normal View History

2023-03-26 15:20:53 +03:00
#pragma once
#include <string>
2023-04-14 16:43:56 +03:00
#include <functional>
2023-03-26 15:20:53 +03:00
#include <unordered_map>
// for clangd
#include "symbols_info.hpp"
#include "type_manager.hpp"
2023-04-17 11:14:33 +03:00
#include "utils.hpp"
2023-03-26 15:20:53 +03:00
namespace info {
2023-04-07 12:13:31 +03:00
// TODO: partitions
2023-03-26 15:20:53 +03:00
class GlobalInfo {
2023-04-17 11:14:33 +03:00
friend class NamespaceVisitor;
2023-03-26 15:20:53 +03:00
public:
2023-04-17 11:14:33 +03:00
class NamespaceVisitor {
friend GlobalInfo;
NamespaceVisitor() = delete;
public:
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
void AddEnterNamespace(const std::string& name,
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
const std::optional<std::string>& variable = std::nullopt);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
void EnterNamespace(const std::string& name);
2023-04-14 14:37:46 +03:00
2023-04-17 11:14:33 +03:00
void ExitNamespace();
2023-04-14 14:37:46 +03:00
2023-04-17 11:14:33 +03:00
void ToGlobalNamespace();
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
utils::IdType AddFunctionDeclaration(const std::string& name,
FunctionDeclarationInfo&& function_declaration_info);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
utils::IdType AddFunctionDefinition(const std::string& name,
FunctionDefinitionInfo&& function_definition_info);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
utils::IdType AddType(const std::string& type, TypeInfo&& type_info);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
utils::IdType AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
std::optional<NamespaceInfo*> FindNamespace(const std::optional<std::vector<std::string>>& path);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
const std::string& name);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
std::optional<utils::IdType> FindMethod(const std::optional<std::vector<std::string>>& path,
const std::string& type,
const std::string& name);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
const std::string type);
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
std::optional<utils::IdType> FindTypeclass(const std::optional<std::vector<std::string>>& path,
const std::string typeclass);
NamespaceVisitor CreateVisitor() {
return global_info_.CreateVisitor();
}
type::TypeManager* GetTypeManager() {
return global_info_.GetTypeManager();
}
2023-04-22 19:30:16 +03:00
GlobalInfo* GetGlobalInfo() {
return &global_info_;
}
const std::vector<std::string>& GetCurrentPath() {
return current_path_;
}
2023-04-17 11:14:33 +03:00
private:
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
namespace_stack_({&global_info.global_namespace_}) {}
2023-04-07 12:13:31 +03:00
2023-04-17 11:14:33 +03:00
template<typename T>
2023-04-17 11:31:00 +03:00
std::optional<T> FindSomething(const std::optional<std::vector<std::string>>& path,
std::function<std::optional<T>(NamespaceInfo*)> search_func);
2023-04-14 16:43:56 +03:00
2023-04-17 11:14:33 +03:00
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
const std::vector<std::string>& path);
private:
std::vector<NamespaceInfo*> namespace_stack_;
std::vector<std::string> current_path_;
2023-04-14 16:43:56 +03:00
2023-04-17 11:14:33 +03:00
GlobalInfo& global_info_;
};
2023-04-14 16:43:56 +03:00
2023-04-17 11:14:33 +03:00
NamespaceVisitor CreateVisitor() {
return NamespaceVisitor(*this);
2023-04-14 16:43:56 +03:00
}
type::TypeManager* GetTypeManager() {
return &type_manager_;
}
2023-04-17 11:31:00 +03:00
// TODO: remember about vector realloc
2023-04-17 11:14:33 +03:00
FunctionInfo* GetFunctionInfo(utils::IdType id) {
2023-04-17 11:31:00 +03:00
return &functions_[id];
2023-04-14 14:37:46 +03:00
}
2023-04-07 12:13:31 +03:00
2023-04-17 11:31:00 +03:00
// TODO: remember about vector realloc
2023-04-17 11:14:33 +03:00
TypeInfo* GetTypeInfo(utils::IdType id) {
2023-04-17 11:31:00 +03:00
return &types_[id];
2023-04-14 16:43:56 +03:00
}
2023-04-17 11:31:00 +03:00
// TODO: remember about vector realloc
2023-04-17 11:14:33 +03:00
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
2023-04-17 11:31:00 +03:00
return &typeclasses_[id];
2023-04-14 14:37:46 +03:00
}
2023-03-26 15:20:53 +03:00
private:
2023-04-17 11:31:00 +03:00
std::vector<FunctionInfo> functions_;
std::vector<TypeInfo> types_;
std::vector<TypeclassInfo> typeclasses_;
2023-04-07 12:13:31 +03:00
type::TypeManager type_manager_;
2023-03-26 15:20:53 +03:00
NamespaceInfo global_namespace_;
2023-04-07 12:13:31 +03:00
std::vector<ImportInfo> imports_;
std::unordered_map<std::string, ImportInfo> usages_;
2023-03-26 15:20:53 +03:00
};
} // namespace info