type_check_visitor FindSubExpressionMethodAndUpdate fixes, fixes

This commit is contained in:
ProgramSnail 2023-05-17 12:23:01 +03:00
parent f177961fbf
commit d2a01abc9c
3 changed files with 56 additions and 30 deletions

Binary file not shown.

View file

@ -11,6 +11,13 @@ namespace interpreter {
// Namespaces, partitions ----------------- // Namespaces, partitions -----------------
void FindSymbolsVisitor::Visit(Namespace* node) { void FindSymbolsVisitor::Visit(Namespace* node) {
if (namespace_visitor_.GetCurrentNamespace()->modifier != utils::ClassInternalsModifier::Static) {
// other type of error??
error_handling::HandleParsingError("Can't use const /var namespace inside const / var namespace",
node->base.start_position,
node->base.end_position);
}
namespace_visitor_.AddEnterNamespace(node->type, node->modifier, node, node->base); namespace_visitor_.AddEnterNamespace(node->type, node->modifier, node, node->base);
Visitor::Visit(&node->scope); Visitor::Visit(&node->scope);

View file

@ -138,8 +138,7 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
context_manager_.EnterContext(); context_manager_.EnterContext();
for (auto& parameter : node->parameters) { // declaration correctness check // TODO: needed?? for (auto& parameter : node->parameters) { // declaration correctness check // TODO: needed??
std::vector<utils::IdType> requirements = auto requirements = global_info_.GetAnnotatedTypeTypeclassesSet(parameter.get());
global_info_.GetAnnotatedTypeTypeclassesVector(parameter.get());
current_type_ = context_manager_.AddValue( current_type_ = context_manager_.AddValue(
info::type::AbstractType(utils::AbstractTypeModifier::Abstract, info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
parameter->type, parameter->type,
@ -218,8 +217,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
} }
for (auto& parameter : declaration->parameters) { for (auto& parameter : declaration->parameters) {
std::vector<utils::IdType> requirements = auto requirements = global_info_.GetAnnotatedTypeTypeclassesSet(parameter.get());
global_info_.GetAnnotatedTypeTypeclassesVector(parameter.get());
current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract, current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
parameter->type, parameter->type,
requirements), requirements),
@ -283,8 +281,7 @@ void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) { void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
is_in_statement_ = true; is_in_statement_ = true;
std::vector<utils::IdType> requirements = auto requirements = global_info_.GetAnnotatedTypeTypeclassesSet(node->type.get());
global_info_.GetAnnotatedTypeTypeclassesVector(node->type.get());
current_type_ = context_manager_.AddValue(info::type::AbstractType(node->modifier, node->type->type, requirements), current_type_ = context_manager_.AddValue(info::type::AbstractType(node->modifier, node->type->type, requirements),
utils::ValueType::Tmp); utils::ValueType::Tmp);
if (!context_manager_.DefineLocalType(node->type->type, current_type_)) { if (!context_manager_.DefineLocalType(node->type->type, current_type_)) {
@ -792,7 +789,7 @@ void TypeCheckVisitor::Visit(AccessExpression* node) {
// Other Expressions // Other Expressions
// TODO: builtin functions/methods // TODO: builtin functions / methods
// TODO: abstract types (check typeclass requirements) // TODO: abstract types (check typeclass requirements)
void TypeCheckVisitor::Visit(FunctionCallExpression* node) { void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
std::optional<FunctionDeclaration*> maybe_function_declaration; std::optional<FunctionDeclaration*> maybe_function_declaration;
@ -827,9 +824,20 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
std::optional<utils::IdType> maybe_function_id; std::optional<utils::IdType> maybe_function_id;
if (namespace_visitor_.GetCurrentNamespace()->modifier != utils::ClassInternalsModifier::Static) { if (namespace_visitor_.GetCurrentNamespace()->modifier != utils::ClassInternalsModifier::Static) {
// call functions from static namespace of current type std::string namespace_name =
std::vector<std::string> current_type_path { namespace_visitor_.GetCurrentNamespace()->type_name }; namespace_visitor_.GetCurrentNamespace()->type_name;
maybe_function_id = namespace_visitor_.FindFunctionId(current_type_path, node->name); // TODO: check !!! utils::ClassInternalsModifier namespace_modifier =
namespace_visitor_.GetCurrentNamespace()->modifier;
namespace_visitor_.ExitNamespace();
maybe_function_id = namespace_visitor_.FindFunctionId(std::nullopt, node->name);
if (!maybe_function_id.has_value()) {
// call functions from static namespace of current type
std::vector<std::string> current_type_path { namespace_visitor_.GetCurrentNamespace()->type_name };
maybe_function_id = namespace_visitor_.FindFunctionId(current_type_path, node->name); // TODO: check !!!
}
namespace_visitor_.EnterNamespace(namespace_name, namespace_modifier);
} else { } else {
maybe_function_id = namespace_visitor_.FindFunctionId(std::nullopt, node->name); maybe_function_id = namespace_visitor_.FindFunctionId(std::nullopt, node->name);
} }
@ -845,11 +853,10 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
} }
// function declaration check // function declaration check
FunctionDeclaration* function_declaration = nullptr;
if (!maybe_function_declaration.has_value()) { if (!maybe_function_declaration.has_value()) {
error_handling::HandleTypecheckError("No function declaration found for function in function call expression", node->base); error_handling::HandleTypecheckError("No function declaration found for function in function call expression", node->base);
} }
function_declaration = maybe_function_declaration.value(); FunctionDeclaration* function_declaration = maybe_function_declaration.value();
// check & collect parmeters // check & collect parmeters
if (function_declaration->parameters.size() != node->parameters.size()) { if (function_declaration->parameters.size() != node->parameters.size()) {
@ -1474,29 +1481,27 @@ std::optional<FunctionDeclaration*>
Visitor::Visit(*expression_node); Visitor::Visit(*expression_node);
utils::IdType expression_type = current_type_; utils::IdType expression_type = current_type_;
auto maybe_typeclass_function_info = auto maybe_typeclass_graph_id = typeclass_graph_.FindFunctionTypeclass(node->name);
typeclass_graph_.GetFunctionInfo(node->name, std::nullopt); auto maybe_abstract_type_info = context_manager_.GetValue<info::type::AbstractType>(expression_type);
auto maybe_abstract_type_info =
context_manager_.GetValue<info::type::AbstractType>(expression_type);
if (maybe_abstract_type_info.has_value()) { if (maybe_abstract_type_info.has_value()) {
if (!maybe_typeclass_function_info.has_value()) { if (!maybe_typeclass_graph_id.has_value()) {
error_handling::HandleTypecheckError("Typeclass function not found", base_node); error_handling::HandleTypecheckError("Typeclass function not found", base_node);
} }
info::type::AbstractType* abstract_type_info = maybe_abstract_type_info.value(); info::type::AbstractType* abstract_type_info = maybe_abstract_type_info.value();
if (!typeclass_graph_.FindFunctionTypeclass(node->name).value()) { auto maybe_typeclass_function_info = typeclass_graph_.GetFunctionInfo(node->name,
error_handling::HandleInternalError("Typeclass function info found, but typeclas id not found", maybe_typeclass_graph_id.value());
if (!maybe_typeclass_function_info.has_value()) {
error_handling::HandleInternalError("Typeclass function info found, but typeclass id not found (abstract type)",
"TypeCheckVisitor.FindSubExpressionMethodAndUpdate"); "TypeCheckVisitor.FindSubExpressionMethodAndUpdate");
} }
if (abstract_type_info->HasTypeclass(maybe_typeclass_graph_id.value())) {
if (abstract_type_info->HasTypeclass(typeclass_graph_.FindFunctionTypeclass(node->name).value())) { node->typeclass_graph_id_ = maybe_typeclass_graph_id;
maybe_function_declaration = maybe_typeclass_function_info.value()->declaration; maybe_function_declaration = maybe_typeclass_function_info.value()->declaration;
} }
} else { } else {
// TODO: check typeclass methods for defined type
auto maybe_defined_type_info = auto maybe_defined_type_info =
context_manager_.GetValue<info::type::DefinedType>(expression_type); context_manager_.GetValue<info::type::DefinedType>(expression_type);
if (!maybe_defined_type_info.has_value()) { if (!maybe_defined_type_info.has_value()) {
@ -1504,21 +1509,20 @@ std::optional<FunctionDeclaration*>
} }
utils::IdType type_id = maybe_defined_type_info.value()->GetTypeId(); utils::IdType type_id = maybe_defined_type_info.value()->GetTypeId();
std::optional<info::definition::AnyType*> maybe_type_info = auto maybe_type_info = global_info_.GetTypeInfo<info::definition::AnyType>(type_id);
global_info_.GetTypeInfo<info::definition::AnyType>(type_id);
if (!maybe_type_info.has_value()) { if (!maybe_type_info.has_value()) {
error_handling::HandleInternalError("Functions/Methods implemented only for AnyType", error_handling::HandleInternalError("Functions/Methods implemented only for AnyType",
"TypeCheckVisitor.FunctionCallExpresssion"); "TypeCheckVisitor.FunctionCallExpresssion");
} }
info::definition::AnyType* type_info = maybe_type_info.value();
// TODO: better decision ?? std::optional<utils::IdType> maybe_const_function_id = global_info_.GetNamespaceInfo(global_info_.GetNamespaceInfo(type_info->parent_namespace).const_namespaces.at(type_info->type.type)).functions[node->name];
std::optional<utils::IdType> maybe_const_function_id = global_info_.GetNamespaceInfo(global_info_.GetNamespaceInfo(maybe_type_info.value()->parent_namespace).const_namespaces.at(maybe_type_info.value()->type.type)).functions[node->name];
utils::ValueType expression_value_type = context_manager_.GetValueType(expression_type); utils::ValueType expression_value_type = context_manager_.GetValueType(expression_type);
if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) { if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) {
// TODO: choose expression value types // TODO: choose expression value types
maybe_function_id = global_info_.GetNamespaceInfo(global_info_.GetNamespaceInfo(maybe_type_info.value()->parent_namespace).var_namespaces.at(maybe_type_info.value()->type.type)).functions[node->name]; maybe_function_id = global_info_.GetNamespaceInfo(global_info_.GetNamespaceInfo(type_info->parent_namespace).var_namespaces.at(type_info->type.type)).functions[node->name];
} }
if (maybe_const_function_id.has_value() && maybe_function_id.has_value()) { // TODO: handle on link_types stage if (maybe_const_function_id.has_value() && maybe_function_id.has_value()) { // TODO: handle on link_types stage
@ -1534,6 +1538,21 @@ std::optional<FunctionDeclaration*>
maybe_function_declaration = maybe_function_declaration =
global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.value().node; global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.value().node;
} }
if (!maybe_function_declaration.has_value() && maybe_typeclass_graph_id.has_value()) {
auto typeclasses = global_info_.GetAnnotatedTypeTypeclassesSet(type_info->type.node);
auto function_typeclass_iter = typeclasses.find(maybe_typeclass_graph_id.value());
if (function_typeclass_iter != typeclasses.end()) {
auto maybe_typeclass_function_info = typeclass_graph_.GetFunctionInfo(node->name,
maybe_typeclass_graph_id.value());
if (!maybe_typeclass_function_info.has_value()) {
error_handling::HandleInternalError("Typeclass function info found, but typeclass id not found (definied type)",
"TypeCheckVisitor.FindSubExpressionMethodAndUpdate");
}
maybe_function_declaration = maybe_typeclass_function_info.value()->declaration;
}
}
} }
return maybe_function_declaration; return maybe_function_declaration;
} }
@ -1551,8 +1570,8 @@ std::optional<FunctionDeclaration*>
error_handling::HandleTypecheckError("Can't call function from array type namespace", node->base); error_handling::HandleTypecheckError("Can't call function from array type namespace", node->base);
} }
std::optional<utils::IdType> maybe_local_abstract_type = // std::optional<utils::IdType> maybe_local_abstract_type =
context_manager_.GetLocalType(type_node->type.type); // context_manager_.GetLocalType(type_node->type.type);
std::vector<std::string> path; std::vector<std::string> path;
path.reserve(type_node->path.size() + 1); path.reserve(type_node->path.size() + 1);