type_check_visitor function_call_expression structure change

This commit is contained in:
ProgramSnail 2023-05-17 10:44:20 +03:00
parent 7fc56df2b7
commit a90bcb5d92
2 changed files with 91 additions and 114 deletions

View file

@ -141,12 +141,15 @@ private:
void CheckPattern(Pattern& node, const BaseNode& base_node);
std::optional<utils::IdType>
FindSubExpressionMethod(utils::IdType expression_type,
std::optional<FunctionDeclaration*>
FindSubExpressionMethodAndUpdate(FunctionCallExpression* node,
SubExpressionToken* expression_node,
const std::string& name,
const BaseNode& base_node);
std::optional<utils::IdType> FindTypeFunction(TypeExpression* node,
std::optional<FunctionDeclaration*>
FindTypeFunctionAndUpdate(FunctionCallExpression* node,
TypeExpression* type_node,
const std::string& name,
std::unordered_map<std::string, utils::IdType>& context);

View file

@ -795,10 +795,9 @@ void TypeCheckVisitor::Visit(AccessExpression* node) {
// TODO: builtin functions/methods
// TODO: abstract types (check typeclass requirements)
void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
std::optional<utils::IdType> maybe_function_id;
std::unordered_map<std::string, utils::IdType> context;
std::optional<FunctionDeclaration*> maybe_function_declaration;
std::optional<utils::IdType> expression_type;
std::unordered_map<std::string, utils::IdType> context;
// guaranteed, that name.size() > 0
if (node->name[0] == '_') {
@ -806,17 +805,17 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
"TypeCheckVisitor.FunctionCallExpresssion");
}
// try to find function id
// try to find function declaration
if (node->prefix.has_value()) {
if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) {
Visitor::Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
expression_type = current_type_;
maybe_function_id = FindSubExpressionMethod(
expression_type.value(),
maybe_function_declaration = FindSubExpressionMethodAndUpdate(
node,
std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()).get(),
node->name,
node->base);
} else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix.value())) {
maybe_function_id = FindTypeFunction(
maybe_function_declaration = FindTypeFunctionAndUpdate(
node,
std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get(),
node->name,
context);
@ -825,6 +824,8 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
"TypeCheckVisitor.FunctionCallExpression");
}
} else {
std::optional<utils::IdType> maybe_function_id;
if (namespace_visitor_.GetCurrentNamespace()->modifier != utils::ClassInternalsModifier::Static) {
// call functions from static namespace of current type
std::vector<std::string> current_type_path { namespace_visitor_.GetCurrentNamespace()->type_name };
@ -832,27 +833,23 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
} else {
maybe_function_id = namespace_visitor_.FindFunctionId(std::nullopt, node->name);
}
if (maybe_function_id.has_value() && global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.has_value()) {
node->function_id_ = maybe_function_id.value();
maybe_function_declaration =
global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.value().node;
} else {
maybe_function_declaration = std::nullopt;
}
}
if (maybe_function_id.has_value()) {
// found function id => function defined or declared in type // ??
// find function declaration
std::optional<info::definition::FunctionDeclaration>& maybe_function_declaration =
global_info_.GetFunctionInfo(maybe_function_id.value()).declaration;
// function declaration check
FunctionDeclaration* function_declaration = nullptr;
if (maybe_function_declaration.has_value()) {
function_declaration = maybe_function_declaration.value().node;
} else {
auto maybe_function_info =
typeclass_graph_.GetFunctionInfo(node->name, std::nullopt);
if (!maybe_function_info.has_value()) {
if (!maybe_function_declaration.has_value()) {
error_handling::HandleTypecheckError("No function declaration found for function in function call expression", node->base);
}
function_declaration = maybe_function_info.value()->declaration;
}
function_declaration = maybe_function_declaration.value();
// check & collect parmeters
if (function_declaration->parameters.size() != node->parameters.size()) {
@ -882,50 +879,10 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
context_manager_.AddValueRequirement(current_type_, argument_type);
}
node->function_id_ = maybe_function_id.value(); // IMPORTANT
// node->function_id_ = maybe_function_id.value(); // IMPORTANT // TODO: do in functions
Visitor::Visit(function_declaration->type->types.back());
current_type_ = TypeInContext(current_type_, context);
} else {
// TODO: handle typeclass functions
auto maybe_function_info = typeclass_graph_.GetFunctionInfo(node->name, std::nullopt);
if (!maybe_function_info.has_value()) {
error_handling::HandleTypecheckError("Can't find function", node->base);
}
if (node->prefix.has_value()) {
if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) {
// std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()).get()
// TODO
} else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix.value())) {
// std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get()
// context_manager_.GetLocalType()
// TODO
} else {
error_handling::HandleInternalError("Unexpected prefix type (when checking typeclass function)",
"TypeCheckVisitor.FunctionCallExpression");
}
} else {
// TODO
}
}
/*
if (node->path.empty() && maybe_local_abstract_type.has_value()) {
// TODO: find function in one of typeclasses
}
if (!maybe_function_id.has_value()) {
auto maybe_any_type_info = global_info_.GetTypeInfo<info::definition::AnyType>(node->type_id_.value());
if (maybe_function_info.has_value() && node->type_id_.has_value() && global_info_.GetAnnotatedTypeFunctionsMap(.value()->type.node).count(name) != 0) {
}
// TODO: try to find in one of typeclasses
}
*/
}
void TypeCheckVisitor::Visit(TupleExpression* node) {
@ -1507,11 +1464,16 @@ void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
// TODO: internal types ??
// TODO: abstract types, typeclasses
std::optional<utils::IdType>
TypeCheckVisitor::FindSubExpressionMethod(utils::IdType expression_type,
std::optional<FunctionDeclaration*>
TypeCheckVisitor::FindSubExpressionMethodAndUpdate(FunctionCallExpression* node,
SubExpressionToken* expression_node,
const std::string& name,
const BaseNode& base_node) {
std::optional<utils::IdType> maybe_function_id;
std::optional<FunctionDeclaration*> maybe_function_declaration;
Visitor::Visit(*expression_node);
utils::IdType expression_type = current_type_;
std::optional<info::type::DefinedType*> maybe_expression_type_info =
context_manager_.GetValue<info::type::DefinedType>(expression_type);
@ -1550,37 +1512,49 @@ std::optional<utils::IdType>
maybe_function_id = maybe_const_function_id;
}
return maybe_function_id;
if (maybe_function_id.has_value()) {
node->function_id_ = maybe_function_id.value();
maybe_function_declaration =
global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.value().node;
}
return maybe_function_declaration;
}
// TODO: internal types ??
// TODO: abstract types, typeclasses
std::optional<utils::IdType>
TypeCheckVisitor::FindTypeFunction(TypeExpression* node,
std::optional<FunctionDeclaration*>
TypeCheckVisitor::FindTypeFunctionAndUpdate(FunctionCallExpression* node,
TypeExpression* type_node,
const std::string& name,
std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_function_id;
std::optional<FunctionDeclaration*> maybe_function_declaration;
if (node->array_size.has_value()) {
if (type_node->array_size.has_value()) {
error_handling::HandleTypecheckError("Can't call function from array type namespace", node->base);
}
std::optional<utils::IdType> maybe_local_abstract_type =
context_manager_.GetLocalType(node->type.type);
context_manager_.GetLocalType(type_node->type.type);
std::vector<std::string> path;
path.reserve(node->path.size() + 1);
path.reserve(type_node->path.size() + 1);
for (auto& path_type : node->path) {
for (auto& path_type : type_node->path) {
path.push_back(path_type.type);
}
path.push_back(node->type.type);
path.push_back(type_node->type.type);
maybe_function_id = namespace_visitor_.FindFunctionId(path, name);
CollectTypeExpressionContext(*node, context);
CollectTypeExpressionContext(*type_node, context);
return maybe_function_id;
if (maybe_function_id.has_value()) {
node->function_id_ = maybe_function_id.value();
maybe_function_declaration =
global_info_.GetFunctionInfo(maybe_function_id.value()).declaration.value().node;
}
return maybe_function_declaration;
}
} // namespace interpreter