part of typeclass_graph

This commit is contained in:
ProgramSnail 2023-05-14 13:05:46 +03:00
parent 4f54bb4bd7
commit 9aeba7b0de
8 changed files with 154 additions and 68 deletions

View file

@ -80,6 +80,8 @@ struct Function {
struct Typeclass {
std::vector<Parameter> parameters;
interpreter::tokens::TypeclassDefinitionStatement* node;
utils::IdType parent_namespace;
};
struct Import {
@ -98,8 +100,10 @@ struct Namespace {
utils::IdType parent_namespace;
utils::ClassInternalsModifier modifier;
utils::ClassInternalsModifier modifier = utils::ClassInternalsModifier::Static;
std::string type_name;
std::optional<interpreter::tokens::Namespace*> node;
};
} // namespace info::definition

View file

@ -4,6 +4,7 @@
#include <any>
// for clangd
#include "interpreter_tree.hpp"
#include "visitor.hpp"
#include "global_info.hpp"

View file

@ -41,6 +41,7 @@ public:
void AddEnterNamespace(const std::string& name,
utils::ClassInternalsModifier modifier,
std::optional<interpreter::tokens::Namespace*> node,
const interpreter::tokens::BaseNode& base_node);
void EnterNamespace(const std::string& name,
@ -204,9 +205,17 @@ public:
return current_path_;
}
utils::IdType GetCurrentNamespace() {
utils::IdType GetCurrentNamespaceId() {
return namespace_stack_.back();
}
definition::Namespace* GetCurrentNamespace() {
return &global_info_.namespaces_[GetCurrentNamespaceId()];
}
bool IsInGlobalNamespace() {
return namespace_stack_.size() == 1;
}
private:
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
namespace_stack_ {global_info.GlobalNamespaceId} {}
@ -291,6 +300,15 @@ public:
return &typeclass_graph_;
}
std::optional<utils::IdType> AddTypeclassToGraph(utils::IdType typeclass);
private:
void CollectFunctionInfo(
utils::IdType current_namespace,
utils::ClassInternalsModifier modifier,
std::vector<std::pair<std::string, std::pair<utils::ClassInternalsModifier, interpreter::tokens::FunctionDeclaration*>>>& function_declarations,
std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinitionStatement*>>& function_definitions);
private:
const utils::IdType GlobalNamespaceId = 0;

View file

@ -33,7 +33,7 @@ private:
// // void Visit(FunctionDefinitionStatement* node) override;
// // void Visit(TypeDefinitionStatement* node) override;
// // void Visit(AbstractTypeDefinitionStatement* node) override;
// // void Visit(TypeclassDefinitionStatement* node) override;
void Visit(TypeclassDefinitionStatement* node) override;
// // void Visit(PartitionStatement* node) override;
// Definition parts

View file

@ -4,76 +4,42 @@
#include <string>
#include <unordered_map>
// for calngd
// for clangd
#include "error_handling.hpp"
#include "global_info.hpp"
#include "utils.hpp"
#include "interpreter_tree.hpp"
#include "definitions.hpp"
namespace info {
class TypeclassGraph {
public:
struct MethodInfo {
struct FunctionInfo {
utils::ClassInternalsModifier modifier;
interpreter::tokens::FunctionDeclaration* declaration = nullptr;
std::optional<interpreter::tokens::FunctionDefinition*> definition;
std::optional<interpreter::tokens::FunctionDefinitionStatement*> definition;
};
struct ParametrizedTypeclass {
std::string typeclass;
std::vector<std::string> parameters; // only abstract types from owner
};
struct AnnotatedType {
std::string name;
std::vector<ParametrizedTypeclass> requirements;
// TODO: parameters
};
struct TypeclassVertex {
std::string name;
interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr;
std::unordered_map<std::string, MethodInfo> methods;
std::unordered_map<std::string, FunctionInfo> functions;
std::vector<ParametrizedTypeclass> dependencies;
std::vector<AnnotatedType> parameters;
// TODO: parameters
};
// // TODO: write + rewrite
// std::optional<utils::IdType> AddTypeclassByNode(interpreter::tokens::TypeclassDefinitionStatement* node) {
// std::string name = node->definition->type->type;
// std::vector<ParametrizedTypeclass> dependencies;
// // std::vector<std::pair<std::string, interpreter::tokens::FunctionDeclaration*>> function_declarations;
// // std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinition*>> function_definitions;
// // std::vector<AnnotatedType> parameters;
//
// for (auto& dependency_node : node->definition->type->typeclasses) {
// ParametrizedTypeclass dependency;
// dependency.typeclass = dependency_node->typeclass;
// for (auto& dependency_parameter_node : dependency_node->parameters) { // TODO: all types, etc.
// dependency.parameters.push_back(dependency_parameter_node->type.type);
// if (dependency_parameter_node->type_id_.has_value()) { // TODO: more checks
// error_handling::HandleInternalError("Typeclasses with non-abstract parameters unimplemented",
// "TypeclassGraph.AddTypeclassByDefinition");
// }
// }
// dependencies.push_back(dependency);
// }
//
// // TODO
//
// return AddTypeclass(name,
// node,
// dependencies,
// function_declarations,
// function_definitions,
// parameters);
// }
std::optional<utils::IdType> AddTypeclass( // move parameters ??
const std::string& name,
interpreter::tokens::TypeclassDefinitionStatement* definition,
const std::vector<ParametrizedTypeclass>& dependencies,
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDeclaration*>>& function_declarations,
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinition*>>& function_definitions,
const std::vector<AnnotatedType>& 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;
@ -87,27 +53,31 @@ public:
typeclass.name = name;
typeclass.definition = definition;
typeclass.dependencies = dependencies;
typeclass.parameters = parameters;
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();
typeclass.methods[method.first].declaration = method.second;
}
name_to_typeclass_[name] = typeclasses_.size() - 1;
for (auto& method : function_definitions) {
typeclass.methods[method.first].definition = method.second;
typeclass.functions[method.first].definition = method.second;
}
return typeclasses_.size() - 1;
}
std::optional<utils::IdType> FindMethodTypeclass(const std::string& name) {
auto method_iter = method_to_typeclass_.find(name);
if (method_iter == method_to_typeclass_.end()) {
auto function_iter = method_to_typeclass_.find(name);
if (function_iter == method_to_typeclass_.end()) {
return std::nullopt;
}
return method_iter->second;
return function_iter->second;
}
const TypeclassVertex& GetTypeclass(utils::IdType id) { // check, if calculated ??
@ -143,13 +113,13 @@ public:
for (auto& id : sorted_verticles) {
for (auto& dependency : typeclasses_[id].dependencies) {
for (auto& method : typeclasses_[name_to_typeclass_[dependency.typeclass]].methods) {
auto method_iter = typeclasses_[id].methods.find(method.first);
if (method_iter == typeclasses_[id].methods.end()) {
typeclasses_[id].methods[method.first] = method.second;
for (auto& method : typeclasses_[name_to_typeclass_[dependency.typeclass]].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 (!method_iter->second.definition.has_value()) {
method_iter->second.definition = method.second.definition;
if (!function_iter->second.definition.has_value()) {
function_iter->second.definition = method.second.definition;
}
}
}
@ -160,6 +130,7 @@ public:
return true;
}
private:
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;
std::unordered_map<std::string, utils::IdType> name_to_typeclass_;