going to change symbol table structure

This commit is contained in:
ProgramSnail 2023-04-17 11:14:33 +03:00
parent 9c25cb1c6f
commit 782a48c4ff
11 changed files with 535 additions and 354 deletions

View file

@ -33,10 +33,7 @@ private:
std::unordered_map<std::string, VariableInfo> variables_;
};
// TODO handle current namespace (for class names, function names, etc.)
// TODO ?? are global variables forbidden ??
Context global_context_;
// Context global_context_; // ??
std::vector<Context> contexts_;
};

View file

@ -6,202 +6,90 @@
// for clangd
#include "symbols_info.hpp"
#include "utils.hpp"
namespace info {
// TODO: partitions
class GlobalInfo {
friend class NamespaceVisitor;
public:
GlobalInfo() {
namespace_stack_.push_back(&global_namespace_);
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);
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_;
GlobalInfo& global_info_;
};
NamespaceVisitor CreateVisitor() {
return NamespaceVisitor(*this);
}
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt) {
if (name.has_value()) {
usages_[name.value()] = std::move(import_info);
} else {
imports_.push_back(std::move(import_info));
}
FunctionInfo* GetFunctionInfo(utils::IdType id) {
return functions_.GetValue(id);
}
void AddEnterNamespace(const std::string& name,
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
const std::optional<std::string>& variable = std::nullopt) {
NamespaceInfo* namespace_info = nullptr;
if (variable.has_value()) {
namespace_info = &namespace_stack_.back()->variable_namespaces[name].emplace_back();
namespace_stack_.push_back(namespace_info);
namespace_info->modifier = modifier;
namespace_info->variable = variable;
} else {
namespace_info = &namespace_stack_.back()->namespaces[name];
namespace_stack_.push_back(namespace_info);
}
namespace_info->type_name = name;
TypeInfo* GetTypeInfo(utils::IdType id) {
return types_.GetValue(id);
}
void EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
}
void ExitAllNameNamespaces(const std::string& name) {
while(namespace_stack_.size() > 1 && namespace_stack_.back()->type_name == name) {
namespace_stack_.pop_back();
}
}
void ExitNamespace() {
if (namespace_stack_.size() <= 1) {
// error
return;
}
namespace_stack_.pop_back();
}
void ToGlobalNamespace() {
namespace_stack_.clear();
namespace_stack_.push_back(&global_namespace_);
}
void AddFunctionDeclaration(const std::string& name,
FunctionDeclarationInfo&& function_declaration_info) {
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
function_info->declaration = std::move(function_declaration_info);
}
void AddFunctionDefinition(const std::string& name,
FunctionDefinitionInfo&& function_definition_info) {
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
function_info->definition = std::move(function_definition_info);
}
void AddType(const std::string& type, TypeInfo&& type_info) {
namespace_stack_.back()->types[type] = std::move(type_info);
}
void AddTypeclass(const std::string& typeclass,
TypeclassInfo&& typeclass_info) {
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
}
std::optional<NamespaceInfo*> FindNamespace(const std::vector<std::string>& path) {
return FindSomething<NamespaceInfo>(path,
[](NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
return current_namespace;
});
}
std::optional<FunctionInfo*> FindFunction(const std::vector<std::string>& path,
const std::string& name) {
return FindSomething<FunctionInfo>(path,
[name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
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;
});
}
std::optional<FunctionInfo*> FindMethod(const std::vector<std::string>& path,
const std::string& type,
const std::string& name) {
// TODO: remove overhead
return FindSomething<FunctionInfo>(path,
[type, name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
return std::nullopt;
}
for (auto& variable_namespace : variable_namespaces_iter->second) {
auto method_iter = variable_namespace.functions.find(name);
if (method_iter == variable_namespace.functions.end()) {
continue;
}
return &method_iter->second;
}
return std::nullopt;
});
}
std::optional<TypeInfo*> FindType(const std::vector<std::string>& path,
const std::string type) {
return FindSomething<TypeInfo>(path,
[type](NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
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;
});
}
std::optional<TypeclassInfo*> FindTypeclass(const std::vector<std::string>& path,
const std::string typeclass) {
return FindSomething<TypeclassInfo>(path,
[typeclass](NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
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;
});
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
return typeclasses_.GetValue(id);
}
private:
template<typename T>
std::optional<T*> FindSomething(const std::vector<std::string>& path,
std::function<std::optional<T*>(NamespaceInfo*)> search_func) {
for (ssize_t i = namespace_stack_.size(); i >= 0; --i) {
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path);
if (!maybe_namespace.has_value()) {
continue;
}
std::optional<T*> result = search_func(maybe_namespace.value());
if (result.has_value()) {
return result.value();
}
}
return std::nullopt;
}
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
const std::vector<std::string>& path) {
NamespaceInfo* next_namespace = current_namespace;
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;
}
private:
std::vector<NamespaceInfo*> namespace_stack_;
utils::Storage<FunctionInfo*> functions_;
utils::Storage<TypeInfo*> types_;
utils::Storage<TypeclassInfo*> typeclasses_;
NamespaceInfo global_namespace_;
std::vector<ImportInfo> imports_;

View file

@ -9,6 +9,7 @@
#include <memory>
// for clangd
#include "utils.hpp"
namespace interpreter {
class Node;
@ -110,9 +111,9 @@ struct ImportInfo {
struct NamespaceInfo {
enum Modifier { Const, Var };
std::unordered_map<std::string, TypeInfo> types;
std::unordered_map<std::string, TypeclassInfo> typeclasses;
std::unordered_map<std::string, FunctionInfo> functions;
std::unordered_map<std::string, utils::IdType> types;
std::unordered_map<std::string, utils::IdType> typeclasses;
std::unordered_map<std::string, utils::IdType> functions;
std::unordered_map<std::string, NamespaceInfo> namespaces;
std::unordered_map<std::string, std::vector<NamespaceInfo>> variable_namespaces;

View file

@ -1,14 +1,19 @@
#pragma once
// for clangd
#include "type_info_contexts.hpp"
#include "symbols_info.hpp"
#include "visitor.hpp"
#include "global_info.hpp"
#include "type_graph.hpp"
namespace interpreter {
class TypeCheckVisitor : public Visitor {
public:
explicit TypeCheckVisitor(info::GlobalInfo& global_info) : global_info_(global_info) {}
explicit TypeCheckVisitor(info::GlobalInfo& global_info,
info::TypeGraph& type_graph)
: global_info_(global_info), type_graph_(type_graph) {}
private:
// Sources -----------------
@ -99,9 +104,9 @@ private:
// Identifiers, constants, etc. -----------------
void Visit(ExtendedName* node) override;
// // void Visit(ExtendedName* node) override;
void Visit(std::string* node) override; // std::string
// // void Visit(std::string* node) override; // std::string
void Visit(FloatNumberLiteral* node) override;
void Visit(NumberLiteral* node) override;
@ -110,6 +115,10 @@ private:
private:
info::GlobalInfo& global_info_;
info::TypeGraph type_graph_;
info::TypeInfoContextManager context_manager_;
info::TypeInfo current_type_;
};
} // namespace interpreter

View file

@ -4,6 +4,11 @@
#include <optional>
#include <string>
// for clangd
#include "utils.hpp"
namespace info {
// TODO: optimize recalc
class TypeGraph {
@ -17,11 +22,11 @@ public:
vertex.path = path;
for (auto& method : methods) {
vertex.new_requirements.methods.insert(GetHash(method));
vertex.new_requirements.methods.insert(storage_.GetId(method));
}
for (auto& typeclass : typeclasses) {
vertex.new_requirements.typeclasses.insert(GetHash(typeclass));
vertex.new_requirements.typeclasses.insert(storage_.GetId(typeclass));
}
verticles_.push_back(vertex);
@ -49,13 +54,13 @@ public:
methods.reserve(cluster_requirements_[cluster_id].methods.size());
for (auto& method : cluster_requirements_[cluster_id].methods) {
methods.push_back(std::move(GetString(method)));
methods.push_back(std::move(storage_.GetValue(method)));
}
return methods;
}
std::vector<std::string> VertexTypeclasses(size_t id) {
std::vector<utils::IdType> VertexTypeclasses(size_t id) {
if (!is_calculated_) {
// error
return {};
@ -63,27 +68,27 @@ public:
size_t cluster_id = verticles_[id].cluster.value();
std::vector<std::string> typeclasses;
std::vector<utils::IdType> typeclasses;
typeclasses.reserve(cluster_requirements_[cluster_id].typeclasses.size());
for (auto& typeclass : cluster_requirements_[cluster_id].typeclasses) {
typeclasses.push_back(std::move(GetString(typeclass)));
typeclasses.push_back(typeclass);
}
return typeclasses;
}
void AddTypeclass(size_t id, const std::string& method) {
void AddMethod(size_t id, const std::string& method) {
is_calculated_ = false;
verticles_[id].new_requirements.methods.insert(GetHash(method));
verticles_[id].new_requirements.methods.insert(storage_.GetId(method));
}
void AddMethod(size_t id, const std::string& typeclass) {
void AddTypeclass(size_t id, utils::IdType typeclass) {
is_calculated_ = false;
verticles_[id].new_requirements.typeclasses.insert(GetHash(typeclass));
verticles_[id].new_requirements.typeclasses.insert(typeclass);
}
void AddEdge(size_t from, size_t to) {
@ -150,8 +155,8 @@ public:
private:
struct RequirementsData {
std::unordered_set<size_t> methods;
std::unordered_set<size_t> typeclasses;
std::unordered_set<utils::IdType> methods;
std::unordered_set<utils::IdType> typeclasses;
};
struct Vertex {
@ -204,27 +209,8 @@ private:
return sorted_verticles;
}
size_t GetHash(const std::string& str) {
size_t hash = 0;
auto str_position = string_to_hash_.find(str);
if (str_position == string_to_hash_.end()) {
hash = hash_to_string_.size();
string_to_hash_[str] = hash;
hash_to_string_.push_back(str);
} else {
hash = str_position->second;
}
return hash;
}
std::string GetString(size_t hash) {
return hash_to_string_[hash];
}
private:
std::unordered_map<std::vector<std::string>, size_t> verticle_ids_;
std::unordered_map<std::vector<std::string>, size_t> verticle_ids_;
std::vector<std::vector<size_t>> edges_;
std::vector<std::vector<size_t>> back_edges_;
std::vector<Vertex> verticles_;
@ -232,8 +218,9 @@ private:
std::vector<std::vector<size_t>> clusters_;
std::vector<RequirementsData> cluster_requirements_;
std::vector<std::string> hash_to_string_;
std::unordered_map<std::string, size_t> string_to_hash_;
utils::Storage<std::string> storage_;
bool is_calculated_ = false;
};
} // namespace info

View file

@ -0,0 +1,96 @@
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
// for clangd
#include "symbols_info.hpp"
namespace info {
class TypeInfoContextManager {
public:
void CallFunction(const std::vector<std::string>& names,
const std::vector<TypeInfo*>& types) {
if (names.size() != types.size()) {
// error
}
contexts_.emplace_back(true);
for (size_t i = 0; i < names.size(); ++i) {
DefineVariable(names[i], types[i]);
}
}
void EnterContext() {
contexts_.emplace_back(false);
}
void ExitContext() {
if (contexts_.empty()) {
// error
}
contexts_.pop_back();
}
void ExitFromAllContexts() {
contexts_.clear();
}
// TODO Variable modifiers
bool DefineVariable(const std::string& name, TypeInfo* type) {
return contexts_.back().DefineVariable(name, type);
}
void EnterVariableContext(const std::string& name, TypeInfo* type) { // for variable namespaces, for loops
contexts_.push_back(false);
DefineVariable(name, type);
}
std::optional<TypeInfo*> GetVariableType(const std::string& name) {
for (ssize_t i = contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetVariableType(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
private:
class Context {
public:
Context(bool hide_previous) : hide_previous_(hide_previous) {}
bool DefineVariable(const std::string& name, TypeInfo* type) {
if (variables_.count(name) > 0) {
return false;
}
variables_[name] = type;
return true;
}
std::optional<TypeInfo*> GetVariableType(const std::string& name) {
auto variable_iter = variables_.find(name);
if (variable_iter == variables_.end()) {
return std::nullopt;
}
return variable_iter->second;
}
bool IsFirst() { return hide_previous_; }
private:
bool hide_previous_;
std::unordered_map<std::string, TypeInfo*> variables_;
};
std::vector<Context> contexts_;
};
} // namespace info

48
include/types_info.hpp Normal file
View file

@ -0,0 +1,48 @@
// for clangd
#include "symbols_info.hpp"
namespace info {
struct BasicType {
std::string name;
std::vector<size_t> methods; // ??
std::vector<size_t> typeclasses;
};
struct AbstractType {
size_t graph_id;
};
struct TupleType;
struct VariantType;
struct ReferenceType;
using AnyType = std::variant<std::unique_ptr<BasicType>,
std::unique_ptr<AbstractType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>,
std::unique_ptr<ReferenceType>>;
struct TupleType {
std::vector<std::pair<std::optional<std::string>, AnyType>> fields;
};
struct VariantType {
std::vector<std::pair<std::string, TupleType>> constructors;
};
struct ReferenceType {
enum { Reference, UniqueReference } reference_type;
AnyType type;
};
struct FunctionalType {
};
struct Type {
size_t graph_id_;
TypeInfo* info = nullptr;
std::vector<std::string> requrements_;
};
} // namespace info

36
include/utils.hpp Normal file
View file

@ -0,0 +1,36 @@
#include <endian.h>
#include <vector>
#include <unordered_map>
namespace utils {
using IdType = std::size_t;
template<typename T>
class Storage {
public:
IdType GetId(const T& value) {
IdType id = 0;
auto value_position = value_to_id_.find(value);
if (value_position == value_to_id_.end()) {
id = id_to_value_.size();
value_to_id_[value] = id;
id_to_value_.push_back(value);
} else {
id = value_position->second;
}
return id;
}
const T& GetValue(IdType id) {
return id_to_value_[id];
}
private:
std::vector<T> id_to_value_;
std::unordered_map<T, IdType> value_to_id_;
};
} // namespace utils