mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
126 lines
3.9 KiB
C++
126 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <functional>
|
|
#include <unordered_map>
|
|
|
|
// for clangd
|
|
#include "symbols_info.hpp"
|
|
#include "type_manager.hpp"
|
|
#include "utils.hpp"
|
|
|
|
namespace info {
|
|
|
|
// TODO: partitions
|
|
class GlobalInfo {
|
|
friend class NamespaceVisitor;
|
|
public:
|
|
class NamespaceVisitor {
|
|
friend GlobalInfo;
|
|
NamespaceVisitor() = delete;
|
|
public:
|
|
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt);
|
|
|
|
void AddEnterNamespace(const std::string& name,
|
|
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
|
const std::optional<std::string>& variable = std::nullopt);
|
|
|
|
void EnterNamespace(const std::string& name);
|
|
|
|
void ExitNamespace();
|
|
|
|
void ToGlobalNamespace();
|
|
|
|
utils::IdType AddFunctionDeclaration(const std::string& name,
|
|
FunctionDeclarationInfo&& function_declaration_info);
|
|
|
|
utils::IdType AddFunctionDefinition(const std::string& name,
|
|
FunctionDefinitionInfo&& function_definition_info);
|
|
|
|
utils::IdType AddType(const std::string& type, TypeInfo&& type_info);
|
|
|
|
utils::IdType AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info);
|
|
|
|
std::optional<NamespaceInfo*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
|
|
|
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
|
|
const std::string& name);
|
|
|
|
std::optional<utils::IdType> FindMethod(const std::optional<std::vector<std::string>>& path,
|
|
const std::string& type,
|
|
const std::string& name);
|
|
|
|
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
|
const std::string type);
|
|
|
|
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();
|
|
}
|
|
|
|
GlobalInfo* GetGlobalInfo() {
|
|
return &global_info_;
|
|
}
|
|
|
|
const std::vector<std::string>& GetCurrentPath() {
|
|
return current_path_;
|
|
}
|
|
private:
|
|
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
|
|
namespace_stack_({&global_info.global_namespace_}) {}
|
|
|
|
template<typename T>
|
|
std::optional<T> FindSomething(const std::optional<std::vector<std::string>>& path,
|
|
std::function<std::optional<T>(NamespaceInfo*)> search_func);
|
|
|
|
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_;
|
|
|
|
GlobalInfo& global_info_;
|
|
};
|
|
|
|
NamespaceVisitor CreateVisitor() {
|
|
return NamespaceVisitor(*this);
|
|
}
|
|
|
|
type::TypeManager* GetTypeManager() {
|
|
return &type_manager_;
|
|
}
|
|
|
|
// TODO: remember about vector realloc
|
|
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
|
return &functions_[id];
|
|
}
|
|
|
|
// TODO: remember about vector realloc
|
|
TypeInfo* GetTypeInfo(utils::IdType id) {
|
|
return &types_[id];
|
|
}
|
|
|
|
// TODO: remember about vector realloc
|
|
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
|
return &typeclasses_[id];
|
|
}
|
|
|
|
private:
|
|
std::vector<FunctionInfo> functions_;
|
|
std::vector<TypeInfo> types_;
|
|
std::vector<TypeclassInfo> typeclasses_;
|
|
|
|
type::TypeManager type_manager_;
|
|
|
|
NamespaceInfo global_namespace_;
|
|
std::vector<ImportInfo> imports_;
|
|
std::unordered_map<std::string, ImportInfo> usages_;
|
|
};
|
|
|
|
} // namespace info
|