mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-30 10:48:16 +00:00
function declaration & definition statements for typeclasses and inherited classes in typecheck_visitor
This commit is contained in:
parent
aab455ff1d
commit
090f638e45
3 changed files with 102 additions and 21 deletions
|
|
@ -127,11 +127,19 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
|||
bool was_in_statement = is_in_statement_;
|
||||
is_in_statement_ = true;
|
||||
|
||||
if (!was_in_statement) {
|
||||
if (namespace_visitor_.GetCurrentNamespaceTypeclassId().has_value()) {
|
||||
error_handling::HandleTypecheckError("Can't declare functions in typeclass namespace", node->base);
|
||||
}
|
||||
if (typeclass_graph_.FindFunctionTypeclass(node->name).has_value()) {
|
||||
error_handling::HandleTypecheckError("Can't redeclare functions, that are declared as typeclass functions", node->base);
|
||||
}
|
||||
}
|
||||
|
||||
context_manager_.EnterContext();
|
||||
for (auto& parameter : node->parameters) { // declaration correctness check // TODO: needed??
|
||||
std::vector<utils::IdType> requirements =
|
||||
global_info_.GetAnnotatedTypeTypeclasses(parameter.get());
|
||||
global_info_.GetAnnotatedTypeTypeclassesVector(parameter.get());
|
||||
current_type_ = context_manager_.AddValue(
|
||||
info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||
parameter->type,
|
||||
|
|
@ -152,37 +160,84 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
|||
|
||||
void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||
is_in_statement_ = true;
|
||||
context_manager_.EnterContext();
|
||||
|
||||
std::string& function_name = node->definition->name;
|
||||
|
||||
info::definition::Function& function_info = global_info_.GetFunctionInfo(node->function_id_);
|
||||
|
||||
if (!function_info.declaration.has_value()) {
|
||||
error_handling::HandleTypecheckError("Function defined, but not declared", node->base);
|
||||
interpreter::tokens::FunctionDeclaration* declaration = nullptr;
|
||||
if (function_info.declaration.has_value()) {
|
||||
declaration = function_info.declaration.value().node;
|
||||
} else {
|
||||
auto maybe_typeclass_graph_id = typeclass_graph_.FindFunctionTypeclass(function_name);
|
||||
|
||||
if (!maybe_typeclass_graph_id.has_value()) {
|
||||
error_handling::HandleTypecheckError("Function defined, but not declared", node->base);
|
||||
}
|
||||
|
||||
auto maybe_curent_type = namespace_visitor_.GetCurrentNamespaceType<info::definition::AnyType>(); // TODO: aliases
|
||||
auto maybe_current_typeclass = namespace_visitor_.GetCurrentNamespaceTypeclass();
|
||||
|
||||
if (maybe_curent_type.has_value() && maybe_current_typeclass.has_value()) {
|
||||
error_handling::HandleInternalError("Current namespace is belong to type and typeclass in same time",
|
||||
"TypeCheckVisitor.FunctionDefinitionStatement");
|
||||
}
|
||||
|
||||
if (!maybe_curent_type.has_value() && !maybe_current_typeclass.has_value()) {
|
||||
error_handling::HandleTypecheckError("Function defined, but declared in typeclass and not belong to type or typeclass namespace", node->base);
|
||||
}
|
||||
|
||||
bool method_found_in_typeclass = false;
|
||||
if (maybe_curent_type.has_value()) { // optiomize??
|
||||
auto typeclasses = global_info_.GetAnnotatedTypeTypeclassesSet(maybe_curent_type.value()->type.node);
|
||||
|
||||
method_found_in_typeclass = (typeclasses.count(maybe_typeclass_graph_id.value()) != 0);
|
||||
|
||||
} else if (maybe_current_typeclass.has_value()) {
|
||||
method_found_in_typeclass =
|
||||
typeclass_graph_.IsFunctionInTypeclass(function_name, maybe_current_typeclass.value()->graph_id_);
|
||||
}
|
||||
|
||||
if (!method_found_in_typeclass) {
|
||||
error_handling::HandleTypecheckError("Function defined, but declared in different typeclass", node->base);
|
||||
}
|
||||
|
||||
auto maybe_function_info = typeclass_graph_.GetFunctionInfo(function_name, std::nullopt);
|
||||
if (!maybe_function_info.has_value()) {
|
||||
error_handling::HandleInternalError("FunctionInfo not found",
|
||||
"TypeCheckVisitor.FunctionDefinitionStatement");
|
||||
}
|
||||
|
||||
declaration = maybe_function_info.value()->declaration; // TODO: save declaration to global info ??
|
||||
}
|
||||
|
||||
info::definition::FunctionDeclaration& declaration = function_info.declaration.value();
|
||||
context_manager_.EnterContext();
|
||||
|
||||
for (auto parameter : declaration.parameters) {
|
||||
if (declaration->type->types.size() != node->definition->arguments.size() + 1) { // + return type
|
||||
error_handling::HandleTypecheckError("Wrong argument count in function definition", node->base);
|
||||
}
|
||||
|
||||
for (auto& parameter : declaration->parameters) {
|
||||
std::vector<utils::IdType> requirements =
|
||||
global_info_.GetAnnotatedTypeTypeclasses(parameter.node);
|
||||
global_info_.GetAnnotatedTypeTypeclassesVector(parameter.get());
|
||||
current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||
parameter.type,
|
||||
parameter->type,
|
||||
requirements),
|
||||
utils::ValueType::Tmp);
|
||||
if (!context_manager_.DefineLocalType(parameter.type, current_type_)) {
|
||||
if (!context_manager_.DefineLocalType(parameter->type, current_type_)) {
|
||||
error_handling::HandleTypecheckError("Can't define function parameter type: abstract type redefinition", node->base);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < node->definition->arguments.size(); ++i) {
|
||||
Visitor::Visit(*declaration.argument_types[i]);
|
||||
Visitor::Visit(declaration->type->types[i]);
|
||||
current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Const); // TODO: var function arguments??
|
||||
if (!context_manager_.DefineVariable(node->definition->arguments[i], current_type_)) { // TODO: watch to reference
|
||||
error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base);
|
||||
}
|
||||
}
|
||||
|
||||
Visitor::Visit(*declaration.argument_types.back());
|
||||
Visitor::Visit(declaration->type->types.back());
|
||||
utils::IdType return_type = current_type_;
|
||||
|
||||
Visitor::Visit(node->value);
|
||||
|
|
@ -191,6 +246,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
|||
}
|
||||
|
||||
context_manager_.ExitContext();
|
||||
|
||||
current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||
is_in_statement_ = false;
|
||||
|
||||
|
|
@ -209,7 +265,7 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
|||
is_in_statement_ = true;
|
||||
|
||||
std::vector<utils::IdType> requirements =
|
||||
global_info_.GetAnnotatedTypeTypeclasses(node->type.get());
|
||||
global_info_.GetAnnotatedTypeTypeclassesVector(node->type.get());
|
||||
current_type_ = context_manager_.AddValue(info::type::AbstractType(node->modifier, node->type->type, requirements),
|
||||
utils::ValueType::Tmp);
|
||||
if (!context_manager_.DefineLocalType(node->type->type, current_type_)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue