fixes, build_visitor TupleType and VariantType fix

This commit is contained in:
ProgramSnail 2023-05-17 17:57:56 +03:00
parent 692f7ea3ec
commit b723fd6a65
12 changed files with 321 additions and 241 deletions

View file

@ -132,7 +132,7 @@ private:
return true;
}
if (variables_.count(name) > 0) {
if (variables_.count(name) != 0) {
return false;
}
variables_[name] = value_id;
@ -140,7 +140,7 @@ private:
}
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
if (local_types_.count(name) > 0) {
if (local_types_.count(name) != 0) {
return false;
}
local_types_[name] = type_id;

View file

@ -48,7 +48,7 @@ struct AnyType {
interpreter::tokens::AnyType* value;
utils::ClassModifier modifier;
utils::IdType parent_namespace;
utils::IdType parent_namespace = 0;
};
struct Type {
@ -57,8 +57,8 @@ struct Type {
struct Constructor {
std::string name;
size_t order;
utils::IdType type_id;
std::optional<size_t> order; // no order for tuple types
utils::IdType type_id = 0;
std::optional<interpreter::tokens::TupleType*> constructor_tuple_node;
};
@ -83,9 +83,9 @@ struct Typeclass {
std::vector<Parameter> parameters;
interpreter::tokens::TypeclassDefinitionStatement* node;
utils::IdType parent_namespace;
utils::IdType parent_namespace = 0;
utils::IdType graph_id_; // TODO: make safe??
utils::IdType graph_id_ = 0; // TODO: make safe??
};
struct Import {
@ -101,7 +101,7 @@ struct Namespace {
std::unordered_map<std::string, utils::IdType> var_namespaces;
std::unordered_map<std::string, utils::IdType> const_namespaces;
utils::IdType parent_namespace;
utils::IdType parent_namespace = 0;
utils::ClassInternalsModifier modifier = utils::ClassInternalsModifier::Static;
std::string type_name;

View file

@ -32,146 +32,32 @@ public:
interpreter::tokens::TypeclassDefinitionStatement* definition,
const std::vector<std::string>& dependencies, // TODO: parameters
const std::vector<std::pair<std::string, std::pair<utils::ClassInternalsModifier, interpreter::tokens::FunctionDeclaration*>>>& function_declarations,
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinitionStatement*>>& function_definitions) {
for (auto& method : function_declarations) {
if (method_to_typeclass_.count(method.first) != 0) {
return std::nullopt;
}
}
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinitionStatement*>>& function_definitions);
is_calculated_ = false;
typeclasses_.emplace_back();
TypeclassVertex& typeclass = typeclasses_.back();
typeclass.name = name;
typeclass.definition = definition;
for (auto& dependency : dependencies) {
typeclass.dependencies.insert(dependency);
}
for (auto& method : function_declarations) {
FunctionInfo function_info;
function_info.modifier = method.second.first;
function_info.declaration = method.second.second;
typeclass.functions[method.first] = function_info;
method_to_typeclass_[method.first] = typeclasses_.size();
}
name_to_typeclass_[name] = typeclasses_.size() - 1;
for (auto& method : function_definitions) {
typeclass.functions[method.first].definition = method.second;
}
return typeclasses_.size() - 1;
}
std::optional<utils::IdType> FindFunctionTypeclass(const std::string& name) {
auto function_iter = method_to_typeclass_.find(name);
if (function_iter == method_to_typeclass_.end()) {
return std::nullopt;
}
return function_iter->second;
}
std::optional<utils::IdType> FindFunctionTypeclass(const std::string& name);
bool IsFunctionInTypeclass(const std::string& name, utils::IdType typeclass_id) {
return typeclasses_[typeclass_id].functions.count(name) != 0;
}
std::optional<FunctionInfo*> GetFunctionInfo(const std::string& name,
std::optional<utils::IdType> typeclass_id) {
if (typeclass_id.has_value()) {
return &typeclasses_[typeclass_id.value()].functions[name];
}
auto maybe_function_typeclass_id = FindFunctionTypeclass(name);
if (!maybe_function_typeclass_id.has_value()) {
return std::nullopt;
}
return &typeclasses_[maybe_function_typeclass_id.value()].functions[name];
}
std::optional<utils::IdType> typeclass_id);
const TypeclassVertex& GetTypeclass(utils::IdType typeclass_id) { // check, if calculated ??
return typeclasses_.at(typeclass_id);
}
// cache ??
std::vector<utils::IdType> GetTypeclassDependencies(utils::IdType id) {
std::vector<utils::IdType> dependencies;
dependencies.reserve(typeclasses_.at(id).dependencies.size());
for (auto& dependency : typeclasses_[id].dependencies) {
dependencies.push_back(name_to_typeclass_[dependency]);
}
return dependencies;
}
std::vector<utils::IdType> GetTypeclassDependencies(utils::IdType id);
// cache ??
std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo*>> GetTypeclassFunctions(utils::IdType id) {
std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo*>> functions;
functions.reserve(typeclasses_.at(id).functions.size());
for (auto& function : typeclasses_[id].functions) {
functions.push_back({function.first, &function.second});
}
return functions;
}
std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo*>> GetTypeclassFunctions(utils::IdType id);
bool IsCalculated() {
return is_calculated_;
}
bool CalculateGraph() {
if (is_calculated_) {
return true;
}
std::vector<std::vector<size_t>> edges(typeclasses_.size());
for (size_t i = 0; i < typeclasses_.size(); ++i) {
edges[i].reserve(typeclasses_[i].dependencies.size());
for (auto& dependency :typeclasses_[i].dependencies) {
auto dependency_iter = name_to_typeclass_.find(dependency);
if (dependency_iter == name_to_typeclass_.end()) {
return false;
}
edges[i].push_back(dependency_iter->second);
}
}
std::vector<size_t> sorted_verticles = utils::BackTopSort(edges);
std::reverse(sorted_verticles.begin(), sorted_verticles.end());
for (auto& id : sorted_verticles) {
for (auto& dependency : typeclasses_[id].dependencies) {
for (auto& method : typeclasses_[name_to_typeclass_[dependency]].functions) {
auto function_iter = typeclasses_[id].functions.find(method.first);
if (function_iter == typeclasses_[id].functions.end()) {
typeclasses_[id].functions[method.first] = method.second;
} else {
if (!function_iter->second.definition.has_value()) {
function_iter->second.definition = method.second.definition;
}
}
}
for (auto& inherited_dependency : typeclasses_[name_to_typeclass_[dependency]].dependencies) {
typeclasses_[id].dependencies.insert(inherited_dependency);
}
}
}
is_calculated_ = true;
return true;
}
bool CalculateGraph();
private:
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;

View file

@ -8,6 +8,7 @@
#include <unordered_map>
// for clangd
#include "error_handling.hpp"
#include "utils.hpp"
namespace info::type {
@ -24,8 +25,8 @@ public:
const std::vector<utils::IdType>& requirement_graph_ids)
: modifier_(modifier),
name_(name) {
for (auto& typeclass : requirement_graph_ids) {
requirement_graph_ids_.insert(typeclass);
for (auto& requirement_graph_id : requirement_graph_ids) {
requirement_graph_ids_.insert(requirement_graph_id);
}
}

View file

@ -228,7 +228,8 @@ private:
std::vector<size_t> ranks_;
};
static void BackVisitDfs(size_t id,
// move to .cpp ??
inline void BackVisitDfs(size_t id,
std::vector<size_t>& verticles,
std::vector<size_t>& marks,
const std::vector<std::vector<size_t>>& edges,
@ -245,7 +246,8 @@ static void BackVisitDfs(size_t id,
}
}
static std::vector<size_t> BackTopSort(const std::vector<std::vector<size_t>>& edges_) {
// move to .cpp ??
inline std::vector<size_t> BackTopSort(const std::vector<std::vector<size_t>>& edges_) {
std::vector<size_t> sorted_verticles;
std::vector<size_t> marks(edges_.size(), 0);