contexts merged with type_info_contexts

This commit is contained in:
ProgramSnail 2023-05-09 15:37:30 +03:00
parent e1b9d42da1
commit 6850863f58
7 changed files with 143 additions and 315 deletions

View file

@ -3,14 +3,15 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <optional>
#include <unordered_map> #include <unordered_map>
// for clangd // for clangd
#include "values.hpp"
#include "utils.hpp" #include "utils.hpp"
namespace info { namespace info {
template<typename Value, typename ValueManager>
class ContextManager { class ContextManager {
public: public:
ContextManager() { ContextManager() {
@ -22,29 +23,37 @@ public:
return value_manager_.AddValue(value, value_type); return value_manager_.AddValue(value, value_type);
} }
utils::IdType AddAnyValue(value::Value&& value, utils::ValueType value_type) { utils::IdType AddAnyValue(Value&& value, utils::ValueType value_type) {
return value_manager_.AddAnyValue(std::move(value), value_type); return value_manager_.AddAnyValue(std::move(value), value_type);
} }
template<typename T> template<typename T>
std::optional<T*> GetValue(utils::IdType value_id) { std::optional<T*> GetValue(utils::IdType value_id) {
return value_manager_.GetValue<T>(value_id); return value_manager_.template GetValue<T>(value_id);
} }
value::Value* GetAnyValue(utils::IdType value_id) { Value* GetAnyValue(utils::IdType value_id) {
return value_manager_.GetAnyValue(value_id); return value_manager_.GetAnyValue(value_id);
} }
bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
return value_manager_.AddTypeRequirement(type, requrement);
}
bool EqualValues(utils::IdType first_type, utils::IdType second_type) {
return value_manager_.EqualTypes(first_type, second_type);
}
utils::ValueType GetValueType(utils::IdType value_id) { utils::ValueType GetValueType(utils::IdType value_id) {
return value_manager_.GetValueType(value_id); return value_manager_.GetValueType(value_id);
} }
utils::IdType ToModifiedValue(utils::IdType value_id, utils::ValueType new_value_type) { utils::IdType ToModifiedValue(utils::IdType value_id, utils::ValueType new_value_type) {
value::Value value = *GetAnyValue(value_id); Value value = *GetAnyValue(value_id);
return AddAnyValue(std::move(value), new_value_type); return AddAnyValue(std::move(value), new_value_type);
} }
value::ValueManager* GetValueManager() { ValueManager* GetValueManager() {
return &value_manager_; return &value_manager_;
} }
@ -168,7 +177,7 @@ private:
}; };
std::vector<Context> contexts_; std::vector<Context> contexts_;
value::ValueManager value_manager_; ValueManager value_manager_;
}; };
} // namespace info } // namespace info

View file

@ -7,6 +7,7 @@
#include "global_info.hpp" #include "global_info.hpp"
#include "interpreter_tree.hpp" #include "interpreter_tree.hpp"
#include "type_info_contexts.hpp" #include "type_info_contexts.hpp"
#include "types.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "values.hpp" #include "values.hpp"
#include "visitor.hpp" #include "visitor.hpp"
@ -17,11 +18,11 @@ namespace interpreter {
class ExecuteVisitor : public Visitor { class ExecuteVisitor : public Visitor {
public: public:
explicit ExecuteVisitor(info::GlobalInfo& global_info, explicit ExecuteVisitor(info::GlobalInfo& global_info,
info::TypeInfoContextManager& type_info_context_manager, info::ContextManager<info::type::Type, info::type::TypeManager>& type_context_manager,
info::ContextManager& context_manager, info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager,
interpreter::tokens::PartitionStatement* execution_root) interpreter::tokens::PartitionStatement* execution_root)
: namespace_visitor_(global_info.CreateVisitor()), : namespace_visitor_(global_info.CreateVisitor()),
type_info_context_manager_(type_info_context_manager), type_context_manager_(type_context_manager),
context_manager_(context_manager) {} context_manager_(context_manager) {}
void VisitSourceFile(SourceFile* source_file) override { void VisitSourceFile(SourceFile* source_file) override {
@ -161,8 +162,8 @@ private:
} }
private: private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_; info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& type_info_context_manager_; info::ContextManager<info::type::Type, info::type::TypeManager>& type_context_manager_;
info::ContextManager& context_manager_; info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager_;
utils::IdType current_value_; utils::IdType current_value_;
std::optional<LoopControlExpression> active_loop_control_expression_; std::optional<LoopControlExpression> active_loop_control_expression_;

View file

@ -2,7 +2,8 @@
// for clangd // for clangd
#include "interpreter_tree.hpp" #include "interpreter_tree.hpp"
#include "type_info_contexts.hpp" #include "types.hpp"
#include "contexts.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "visitor.hpp" #include "visitor.hpp"
#include "global_info.hpp" #include "global_info.hpp"
@ -14,7 +15,7 @@ namespace interpreter {
class TypeCheckVisitor : public Visitor { class TypeCheckVisitor : public Visitor {
public: public:
explicit TypeCheckVisitor(info::GlobalInfo& global_info, explicit TypeCheckVisitor(info::GlobalInfo& global_info,
info::TypeInfoContextManager& context_manager) info::ContextManager<info::type::Type, info::type::TypeManager>& context_manager)
: namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {} : namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {}
private: private:
@ -139,7 +140,7 @@ private:
private: private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_; info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& context_manager_; info::ContextManager<info::type::Type, info::type::TypeManager>& context_manager_;
utils::IdType current_type_; utils::IdType current_type_;
std::optional<utils::IdType> returned_type_; std::optional<utils::IdType> returned_type_;

View file

@ -1,183 +0,0 @@
#pragma once
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
// for clangd
#include "types.hpp"
#include "utils.hpp"
namespace info {
// TODO: remember about type pointers
class TypeInfoContextManager {
public:
TypeInfoContextManager() {
contexts_.emplace_back();
}
template<typename T>
utils::IdType AddType(const T& type, utils::ValueType value_type) {
return type_manager_.AddType(type, value_type);
}
utils::IdType AddAnyType(type::Type&& type, utils::ValueType value_type) {
return type_manager_.AddAnyType(std::move(type), value_type);
}
template<typename T>
std::optional<T*> GetType(utils::IdType type_id) {
return type_manager_.GetType<T>(type_id);
}
type::Type* GetAnyType(utils::IdType type_id) {
return type_manager_.GetAnyType(type_id);
}
utils::ValueType GetValueType(utils::IdType type_id) {
return type_manager_.GetValueType(type_id);
}
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return type_manager_.AddTypeRequirement(type, requrement);
}
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) {
return type_manager_.EqualTypes(first_type, second_type);
}
utils::IdType ToModifiedType(utils::IdType type_id, utils::ValueType new_value_type) {
type::Type type = *GetAnyType(type_id);
return AddAnyType(std::move(type), new_value_type);
}
type::TypeManager* GetTypeManager() {
return &type_manager_;
}
void EnterContext() {
contexts_.emplace_back();
}
void ExitContext() {
if (contexts_.empty()) {
// error
}
contexts_.pop_back();
}
void ExitFromAllContexts() {
contexts_.clear();
contexts_.emplace_back();
}
bool DefineVariable(const std::string& name, utils::IdType type_id) {
// check in previous contexts ??
return contexts_.back().DefineVariable(name, type_id);
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (GetLocalAbstractType(name).has_value()) {
return false;
}
return contexts_.back().DefineLocalAbstractType(name, type_id);
}
bool RemoveVariable(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
if (contexts_[i].RemoveVariable(name)) {
return true;
}
}
return false;
}
void EnterVariableContext(const std::string& name, utils::IdType type_id) {
// for variable namespaces, for loops
contexts_.emplace_back();
DefineVariable(name, type_id);
}
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetVariableInfo(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetLocalAbstractType(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
private:
class Context {
public:
Context() = default;
bool DefineVariable(const std::string& name, utils::IdType type_id) {
if (name == "_") { // placeholder // TODO: ??
return true;
}
if (variables_.count(name) > 0) {
return false;
}
variables_[name] = type_id;
return true;
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (local_abstract_types_.count(name) > 0) {
return false;
}
local_abstract_types_[name] = type_id;
return true;
}
bool RemoveVariable(const std::string& name) {
return variables_.erase(name);
}
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
auto variable_iter = variables_.find(name);
if (variable_iter == variables_.end()) {
return std::nullopt;
}
return variable_iter->second;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
auto local_abstract_type_iter = local_abstract_types_.find(name);
if (local_abstract_type_iter == local_abstract_types_.end()) {
return std::nullopt;
}
return local_abstract_type_iter->second;
}
private:
std::unordered_map<std::string, utils::IdType> variables_;
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
};
std::vector<Context> contexts_;
type::TypeManager type_manager_;
};
} // namespace info

View file

@ -293,25 +293,25 @@ private:
class TypeManager { class TypeManager {
public: public:
template<typename T> template<typename T>
utils::IdType AddType(const T& type, utils::ValueType value_type) { utils::IdType AddValue(const T& type, utils::ValueType value_type) {
types_.push_back(std::pair<Type, utils::ValueType> {type, value_type}); types_.push_back(std::pair<Type, utils::ValueType> {type, value_type});
return types_.size() - 1; return types_.size() - 1;
} }
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type) { utils::IdType AddAnyValue(Type&& type, utils::ValueType value_type) {
types_.push_back(std::pair<Type, utils::ValueType> {std::move(type), value_type}); types_.push_back(std::pair<Type, utils::ValueType> {std::move(type), value_type});
return types_.size() - 1; return types_.size() - 1;
} }
template<typename T> template<typename T>
std::optional<T*> GetType(utils::IdType type_id) { std::optional<T*> GetValue(utils::IdType type_id) {
if (!std::holds_alternative<T>(types_.at(type_id).first.GetType())) { if (!std::holds_alternative<T>(types_.at(type_id).first.GetType())) {
return std::nullopt; return std::nullopt;
} }
return &std::get<T>(types_.at(type_id).first.GetType()); return &std::get<T>(types_.at(type_id).first.GetType());
} }
Type* GetAnyType(utils::IdType type_id) { Type* GetAnyValue(utils::IdType type_id) {
return &types_.at(type_id).first; return &types_.at(type_id).first;
} }
@ -320,11 +320,11 @@ public:
} }
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) { bool EqualTypes(utils::IdType first_type, utils::IdType second_type) {
return GetAnyType(first_type)->Same(*GetAnyType(second_type)); return GetAnyValue(first_type)->Same(*GetAnyValue(second_type));
} }
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) { bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return *GetAnyType(requrement) < *GetAnyType(type); return *GetAnyValue(requrement) < *GetAnyValue(type);
} }
private: private:

View file

@ -16,7 +16,7 @@ void TypeCheckVisitor::Visit(SourceFile* node) {
for (auto& statement : node->statements) { for (auto& statement : node->statements) {
Visitor::Visit(statement); Visitor::Visit(statement);
} }
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -27,7 +27,7 @@ void TypeCheckVisitor::Visit(NamespaceSources* node) {
for (auto& statement : node->statements) { for (auto& statement : node->statements) {
Visitor::Visit(statement); Visitor::Visit(statement);
} }
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -37,20 +37,20 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
if (node->link_typeclass_id_.has_value()) { // TODO: think about typeclass if (node->link_typeclass_id_.has_value()) { // TODO: think about typeclass
std::vector<utils::IdType> requirements {node->link_typeclass_id_.value()}; std::vector<utils::IdType> requirements {node->link_typeclass_id_.value()};
utils::IdType abstract_type = context_manager_.AddType( utils::IdType abstract_type = context_manager_.AddValue(
info::type::AbstractType(utils::AbstractTypeModifier::Abstract, node->type, requirements), info::type::AbstractType(utils::AbstractTypeModifier::Abstract, node->type, requirements),
IsConstModifierToValueType(node->modifier.value())); IsConstModifierToValueType(node->modifier.value()));
context_manager_.EnterVariableContext("self", // TODO: different name ?? context_manager_.EnterVariableContext("self", // TODO: different name ??
abstract_type); abstract_type);
context_manager_.DefineLocalAbstractType(node->type, abstract_type); context_manager_.DefineLocalType(node->type, abstract_type);
} else if (node->link_type_id_.has_value()) { } else if (node->link_type_id_.has_value()) {
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value()->value); // handle error? Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value()->value); // handle error?
context_manager_.EnterVariableContext( context_manager_.EnterVariableContext(
"self", // TODO: different name "self", // TODO: different name
context_manager_.AddType( context_manager_.AddValue(
info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetTypeManager()), info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetValueManager()),
IsConstModifierToValueType(node->modifier.value()))); IsConstModifierToValueType(node->modifier.value())));
} }
} else { } else {
@ -65,7 +65,7 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
namespace_visitor_.ExitNamespace(); namespace_visitor_.ExitNamespace();
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -80,18 +80,18 @@ void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
error_handling::HandleInternalError("Unimplemented", error_handling::HandleInternalError("Unimplemented",
"TypeCheckVisitor.AliasDefinitionStatement"); "TypeCheckVisitor.AliasDefinitionStatement");
// if (!is_in_statement_) { // do nothing if alias is not applied // if (!is_in_statement_) { // do nothing if alias is not applied
// current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); // current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
// return; // return;
// } // }
// //
// context_manager_.EnterContext(); // context_manager_.EnterContext();
// //
// for (auto& parameter : node->parameters) { // for (auto& parameter : node->parameters) {
// current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract, // current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
// parameter, // parameter,
// /*TODO*/{}), // /*TODO*/{}),
// utils::ValueType::Tmp); // utils::ValueType::Tmp);
// context_manager_.DefineLocalAbstractType(parameter, current_type_); // context_manager_.DefineLocalType(parameter, current_type_);
// } // }
// //
// Visit(node->value.get()); // Visit(node->value.get());
@ -108,7 +108,7 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
Visitor::Visit(node->value); Visitor::Visit(node->value);
// current_type from value automatically passed to name definitions // current_type from value automatically passed to name definitions
if (node->assignment_modifier == utils::AssignmentModifier::Assign) { if (node->assignment_modifier == utils::AssignmentModifier::Assign) {
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp); current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
// TODO: make type tmp recursively ?? // TODO: make type tmp recursively ??
} }
@ -116,7 +116,7 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
Visitor::Visit(node->name); Visitor::Visit(node->name);
is_const_definition_ = std::nullopt; is_const_definition_ = std::nullopt;
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -130,16 +130,16 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
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; std::vector<utils::IdType> requirements;
// TODO: // TODO:
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::AbstractType(utils::AbstractTypeModifier::Abstract, info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
parameter->type, parameter->type,
requirements), // TODO: typeclasses-requirements requirements), // TODO: typeclasses-requirements
utils::ValueType::Tmp); utils::ValueType::Tmp);
context_manager_.DefineLocalAbstractType(parameter->type, current_type_); context_manager_.DefineLocalType(parameter->type, current_type_);
} }
Visit(node->type.get()); Visit(node->type.get());
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
if (!was_in_statement) { if (!was_in_statement) {
is_in_statement_ = false; is_in_statement_ = false;
@ -167,18 +167,18 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
requirements.push_back(typeclass->typeclass_id_); requirements.push_back(typeclass->typeclass_id_);
} }
// TODO: add recursive typeclasses from typeclass tree // TODO: add recursive typeclasses from typeclass tree
current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract, current_type_ = context_manager_.AddValue(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
parameter.type, parameter.type,
requirements), requirements),
utils::ValueType::Tmp); utils::ValueType::Tmp);
if (!context_manager_.DefineLocalAbstractType(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); 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) { for (size_t i = 0; i < node->definition->arguments.size(); ++i) {
Visitor::Visit(*declaration.argument_types[i]); Visitor::Visit(*declaration.argument_types[i]);
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Const); // TODO: var function arguments?? current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Const); // TODO: var function arguments??
if (!context_manager_.DefineVariable(node->definition->arguments[i].name, current_type_)) { // TODO: watch to reference if (!context_manager_.DefineVariable(node->definition->arguments[i].name, current_type_)) { // TODO: watch to reference
error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base); error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base);
} }
@ -188,12 +188,12 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
utils::IdType return_type = current_type_; utils::IdType return_type = current_type_;
Visitor::Visit(node->value); Visitor::Visit(node->value);
if (!context_manager_.EqualTypes(return_type, current_type_)) { if (!context_manager_.EqualValues(return_type, current_type_)) {
error_handling::HandleTypecheckError("Wrong function return type", node->base); error_handling::HandleTypecheckError("Wrong function return type", node->base);
} }
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -201,7 +201,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) { void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
is_in_statement_ = true; is_in_statement_ = true;
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -217,13 +217,13 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
} }
// TODO: add recursive typeclasses from typeclass tree // TODO: add recursive typeclasses from typeclass tree
current_type_ = context_manager_.AddType(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_.DefineLocalAbstractType(node->type->type, current_type_)) { if (!context_manager_.DefineLocalType(node->type->type, current_type_)) {
error_handling::HandleTypecheckError("Can't define basic/bastract type: abstract type redefinition", node->base); error_handling::HandleTypecheckError("Can't define basic/bastract type: abstract type redefinition", node->base);
} }
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -231,7 +231,7 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) { void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
is_in_statement_ = true; is_in_statement_ = true;
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -244,7 +244,7 @@ void TypeCheckVisitor::Visit(PartitionStatement* node) {
Visitor::Visit(node->value); Visitor::Visit(node->value);
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false; is_in_statement_ = false;
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -253,19 +253,19 @@ void TypeCheckVisitor::Visit(PartitionStatement* node) {
// Definition parts // Definition parts
void TypeCheckVisitor::Visit(FunctionDefinition* node) { void TypeCheckVisitor::Visit(FunctionDefinition* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(TypeDefinition* node) { void TypeCheckVisitor::Visit(TypeDefinition* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) { void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -339,7 +339,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
Visitor::Visit(*type_info.value); Visitor::Visit(*type_info.value);
current_type_ = TypeInContext(current_type_, context); current_type_ = TypeInContext(current_type_, context);
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp); current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -349,7 +349,7 @@ void TypeCheckVisitor::Visit(MatchCase* node) {
is_const_definition_ = utils::IsConstModifier::Const; // TODO: or var?? is_const_definition_ = utils::IsConstModifier::Const; // TODO: or var??
Visitor::Visit(node->value); Visitor::Visit(node->value);
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp); current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
CheckPattern(node->value, node->base); CheckPattern(node->value, node->base);
@ -358,8 +358,8 @@ void TypeCheckVisitor::Visit(MatchCase* node) {
if (node->condition.has_value()) { if (node->condition.has_value()) {
Visitor::Visit(node->condition.value()); Visitor::Visit(node->condition.value());
} }
if (!context_manager_.EqualTypes( if (!context_manager_.EqualValues(
context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp),
current_type_)) { current_type_)) {
error_handling::HandleTypecheckError("Match case condition is not bool expression", node->base); error_handling::HandleTypecheckError("Match case condition is not bool expression", node->base);
} }
@ -396,14 +396,14 @@ void TypeCheckVisitor::Visit(Match* node) { // TODO: move value to match
type = current_type_; type = current_type_;
is_type_found = true; is_type_found = true;
} else { } else {
if (!context_manager_.EqualTypes(type, current_type_)) { if (!context_manager_.EqualValues(type, current_type_)) {
error_handling::HandleTypecheckError("Match statement cases have different types", node->base); error_handling::HandleTypecheckError("Match statement cases have different types", node->base);
} }
} }
} }
if (!is_type_found) { if (!is_type_found) {
type = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); type = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
} }
current_type_ = type; current_type_ = type;
@ -417,7 +417,7 @@ void TypeCheckVisitor::Visit(Condition* node) {
for (size_t i = 0; i < node->conditions.size(); ++i) { for (size_t i = 0; i < node->conditions.size(); ++i) {
Visitor::Visit(node->conditions[i]); Visitor::Visit(node->conditions[i]);
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) { if (!context_manager_.EqualValues(context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
error_handling::HandleTypecheckError("Condition statement condition is not bool expression", node->base); error_handling::HandleTypecheckError("Condition statement condition is not bool expression", node->base);
} }
@ -427,7 +427,7 @@ void TypeCheckVisitor::Visit(Condition* node) {
if (i == 0) { if (i == 0) {
type = current_type_; type = current_type_;
} else { } else {
if (!context_manager_.EqualTypes(type, current_type_)) { if (!context_manager_.EqualValues(type, current_type_)) {
error_handling::HandleTypecheckError("Condition statement cases have different types", node->base); error_handling::HandleTypecheckError("Condition statement cases have different types", node->base);
} }
} }
@ -437,13 +437,13 @@ void TypeCheckVisitor::Visit(Condition* node) {
context_manager_.EnterContext(); context_manager_.EnterContext();
Visitor::Visit(node->statements[node->conditions.size()]); Visitor::Visit(node->statements[node->conditions.size()]);
context_manager_.ExitContext(); context_manager_.ExitContext();
if (!context_manager_.EqualTypes(type, current_type_)) { if (!context_manager_.EqualValues(type, current_type_)) {
error_handling::HandleTypecheckError("Condition statement else have different type from other cases", node->base); error_handling::HandleTypecheckError("Condition statement else have different type from other cases", node->base);
} }
current_type_ = type; current_type_ = type;
} else { } else {
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::OptionalType(type, context_manager_.GetTypeManager()), info::type::OptionalType(type, context_manager_.GetValueManager()),
utils::ValueType::Tmp); // ?? utils::ValueType::Tmp); // ??
} }
@ -452,7 +452,7 @@ void TypeCheckVisitor::Visit(Condition* node) {
void TypeCheckVisitor::Visit(DoWhileLoop* node) { void TypeCheckVisitor::Visit(DoWhileLoop* node) {
Visitor::Visit(node->condition); Visitor::Visit(node->condition);
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) { if (!context_manager_.EqualValues(context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
error_handling::HandleTypecheckError("Do while loop statement condition is not bool expression", node->base); error_handling::HandleTypecheckError("Do while loop statement condition is not bool expression", node->base);
} }
@ -460,14 +460,14 @@ void TypeCheckVisitor::Visit(DoWhileLoop* node) {
Visitor::Visit(node->statement); Visitor::Visit(node->statement);
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
void TypeCheckVisitor::Visit(WhileLoop* node) { void TypeCheckVisitor::Visit(WhileLoop* node) {
Visitor::Visit(node->condition); Visitor::Visit(node->condition);
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) { if (!context_manager_.EqualValues(context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
error_handling::HandleTypecheckError("While loop statement condition is not bool expression", node->base); error_handling::HandleTypecheckError("While loop statement condition is not bool expression", node->base);
} }
@ -475,8 +475,8 @@ void TypeCheckVisitor::Visit(WhileLoop* node) {
Visitor::Visit(node->statement); Visitor::Visit(node->statement);
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -484,7 +484,7 @@ void TypeCheckVisitor::Visit(WhileLoop* node) {
void TypeCheckVisitor::Visit(ForLoop* node) { void TypeCheckVisitor::Visit(ForLoop* node) {
Visitor::Visit(node->interval); Visitor::Visit(node->interval);
std::optional<info::type::ArrayType*> maybe_interval_type = context_manager_.GetType<info::type::ArrayType>(current_type_); std::optional<info::type::ArrayType*> maybe_interval_type = context_manager_.GetValue<info::type::ArrayType>(current_type_);
if (!maybe_interval_type.has_value()) { if (!maybe_interval_type.has_value()) {
error_handling::HandleTypecheckError("For loop interval type mismatch", node->base); error_handling::HandleTypecheckError("For loop interval type mismatch", node->base);
@ -492,7 +492,7 @@ void TypeCheckVisitor::Visit(ForLoop* node) {
context_manager_.EnterContext(); context_manager_.EnterContext();
current_type_ = context_manager_.ToModifiedType(maybe_interval_type.value()->GetElementsType(), current_type_ = context_manager_.ToModifiedValue(maybe_interval_type.value()->GetElementsType(),
utils::IsConstModifierToValueType(node->variable_modifier)); utils::IsConstModifierToValueType(node->variable_modifier));
is_const_definition_ = node->variable_modifier; is_const_definition_ = node->variable_modifier;
@ -503,8 +503,8 @@ void TypeCheckVisitor::Visit(ForLoop* node) {
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
@ -513,8 +513,8 @@ void TypeCheckVisitor::Visit(LoopLoop* node) {
Visitor::Visit(node->statement); Visitor::Visit(node->statement);
context_manager_.ExitContext(); context_manager_.ExitContext();
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -537,7 +537,7 @@ void TypeCheckVisitor::Visit(Block* node) {
type = returned_type_.value(); type = returned_type_.value();
is_type_found = true; is_type_found = true;
} else { } else {
if (!context_manager_.EqualTypes(type, returned_type_.value())) { if (!context_manager_.EqualValues(type, returned_type_.value())) {
error_handling::HandleTypecheckError("Different return types in block", node->base); error_handling::HandleTypecheckError("Different return types in block", node->base);
} }
} }
@ -547,7 +547,7 @@ void TypeCheckVisitor::Visit(Block* node) {
context_manager_.EnterContext(); context_manager_.EnterContext();
if (!is_type_found) { if (!is_type_found) {
type = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); type = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
} }
current_type_ = type; current_type_ = type;
@ -563,7 +563,7 @@ void TypeCheckVisitor::Visit(ScopedStatement* node) {
} }
void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
} }
// Operators // Operators
@ -581,7 +581,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
is_method = false; is_method = false;
} else { } else {
Visitor::Visit(node->left_expression); Visitor::Visit(node->left_expression);
auto maybe_left_type = context_manager_.GetType<info::type::DefinedType>(current_type_); auto maybe_left_type = context_manager_.GetValue<info::type::DefinedType>(current_type_);
utils::ValueType left_value_type = context_manager_.GetValueType(current_type_); utils::ValueType left_value_type = context_manager_.GetValueType(current_type_);
@ -643,7 +643,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->left_expression); Visitor::Visit(node->left_expression);
if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) { if (!context_manager_.AddValueRequirement(current_type_, left_expression_type)) {
error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base); error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base);
} }
@ -653,7 +653,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->right_expression); Visitor::Visit(node->right_expression);
if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) { if (!context_manager_.AddValueRequirement(current_type_, right_expression_type)) {
error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base); error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base);
} }
@ -695,7 +695,7 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
utils::IdType expression_type = current_type_; // TODO: type in context of deduced types utils::IdType expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->expression); Visitor::Visit(node->expression);
if (!context_manager_.AddTypeRequirement(current_type_, expression_type)) { if (!context_manager_.AddValueRequirement(current_type_, expression_type)) {
error_handling::HandleTypecheckError("Operator expression has wrong type", node->base); error_handling::HandleTypecheckError("Operator expression has wrong type", node->base);
} }
@ -709,20 +709,20 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
void TypeCheckVisitor::Visit(ReferenceExpression* node) { void TypeCheckVisitor::Visit(ReferenceExpression* node) {
Visit(node->expression.get()); Visit(node->expression.get());
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()), info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()),
context_manager_.GetValueType(current_type_)); // ?? context_manager_.GetValueType(current_type_)); // ??
} }
void TypeCheckVisitor::Visit(AccessExpression* node) { void TypeCheckVisitor::Visit(AccessExpression* node) {
Visitor::Visit(node->id); Visitor::Visit(node->id);
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Int, utils::ValueType::Tmp), current_type_)) { if (!context_manager_.EqualValues(context_manager_.AddValue(info::type::InternalType::Int, utils::ValueType::Tmp), current_type_)) {
error_handling::HandleTypecheckError("Access index has wrong type (not Int)", node->base); error_handling::HandleTypecheckError("Access index has wrong type (not Int)", node->base);
} }
Visitor::Visit(node->name.get()); Visitor::Visit(node->name.get());
std::optional<info::type::ArrayType*> maybe_type_value = context_manager_.GetType<info::type::ArrayType>(current_type_); std::optional<info::type::ArrayType*> maybe_type_value = context_manager_.GetValue<info::type::ArrayType>(current_type_);
if (!maybe_type_value.has_value()) { if (!maybe_type_value.has_value()) {
error_handling::HandleTypecheckError("Access type is not array", node->base); error_handling::HandleTypecheckError("Access type is not array", node->base);
@ -755,7 +755,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) { if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) {
Visitor::Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value())); Visitor::Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
std::optional<info::type::DefinedType*> maybe_expression_type = context_manager_.GetType<info::type::DefinedType>(current_type_); std::optional<info::type::DefinedType*> maybe_expression_type = context_manager_.GetValue<info::type::DefinedType>(current_type_);
if (!maybe_expression_type.has_value()) { if (!maybe_expression_type.has_value()) {
error_handling::HandleTypecheckError("There is no non-builtin methods for not defined type", node->base); error_handling::HandleTypecheckError("There is no non-builtin methods for not defined type", node->base);
@ -851,7 +851,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
utils::IdType argument_type = TypeInContext(current_type_, context); utils::IdType argument_type = TypeInContext(current_type_, context);
Visitor::Visit(node->arguments[i]); Visitor::Visit(node->arguments[i]);
context_manager_.AddTypeRequirement(current_type_, argument_type); context_manager_.AddValueRequirement(current_type_, argument_type);
} }
node->function_id_ = maybe_function_id.value(); // IMPORTANT node->function_id_ = maybe_function_id.value(); // IMPORTANT
@ -868,8 +868,8 @@ void TypeCheckVisitor::Visit(TupleExpression* node) {
fields.push_back({std::nullopt, current_type_}); fields.push_back({std::nullopt, current_type_});
} }
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::TupleType(std::nullopt, fields, context_manager_.GetTypeManager()), info::type::TupleType(std::nullopt, fields, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -884,14 +884,14 @@ void TypeCheckVisitor::Visit(VariantExpression* node) {
// TODO: deal with expression tuple types, etc, ?? // TODO: deal with expression tuple types, etc, ??
std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields {{std::nullopt, current_type_}}; std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields {{std::nullopt, current_type_}};
constructors.push_back(info::type::TupleType(std::nullopt, constructor_fields, context_manager_.GetTypeManager())); constructors.push_back(info::type::TupleType(std::nullopt, constructor_fields, context_manager_.GetValueManager()));
} }
current_type_ = context_manager_.AddType(info::type::VariantType(std::nullopt, constructors, -1), current_type_ = context_manager_.AddValue(info::type::VariantType(std::nullopt, constructors, -1),
utils::ValueType::Tmp); utils::ValueType::Tmp);
current_type_ = context_manager_.AddType(info::type::VariantType(std::nullopt, constructors, -1), current_type_ = context_manager_.AddValue(info::type::VariantType(std::nullopt, constructors, -1),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -960,7 +960,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
Visitor::Visit(node->parameters[i].value); Visitor::Visit(node->parameters[i].value);
if (!context_manager_.EqualTypes(parameter_type, current_type_)) { if (!context_manager_.EqualValues(parameter_type, current_type_)) {
error_handling::HandleTypecheckError("Type constructor: wrong parameter type", node->base); error_handling::HandleTypecheckError("Type constructor: wrong parameter type", node->base);
} }
} }
@ -974,7 +974,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
Visitor::Visit(*type_info.value); Visitor::Visit(*type_info.value);
std::optional<info::type::VariantType*> maybe_variant_type = std::optional<info::type::VariantType*> maybe_variant_type =
context_manager_.GetType<info::type::VariantType>(current_type_); context_manager_.GetValue<info::type::VariantType>(current_type_);
if (maybe_type_info.has_value()) { if (maybe_type_info.has_value()) {
maybe_variant_type.value()->SetCurrentConstructor(constructor_info.order); maybe_variant_type.value()->SetCurrentConstructor(constructor_info.order);
@ -1000,7 +1000,7 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) {
if (i == 0) { if (i == 0) {
elements_type = current_type_; elements_type = current_type_;
} else { } else {
if (!context_manager_.EqualTypes(elements_type, current_type_)) { if (!context_manager_.EqualValues(elements_type, current_type_)) {
error_handling::HandleTypecheckError("Different element types in array expression", node->base); error_handling::HandleTypecheckError("Different element types in array expression", node->base);
} }
} }
@ -1010,8 +1010,8 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) {
error_handling::HandleInternalError("Element size is 0", "TypeCheckVisitor.ArrayExpression"); error_handling::HandleInternalError("Element size is 0", "TypeCheckVisitor.ArrayExpression");
} }
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetTypeManager()), info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -1042,7 +1042,7 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_); utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_);
for (size_t i = 1; i < node->names.size(); ++i) { for (size_t i = 1; i < node->names.size(); ++i) {
std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyType(current_type_)->GetFieldType(node->names[i].name); std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i].name);
if (!maybe_type_value.has_value()) { if (!maybe_type_value.has_value()) {
error_handling::HandleTypecheckError("Variable field not found", node->names[i].base); error_handling::HandleTypecheckError("Variable field not found", node->names[i].base);
} }
@ -1050,7 +1050,7 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
current_type_ = maybe_type_value.value(); current_type_ = maybe_type_value.value();
} }
current_type_ = context_manager_.ToModifiedType(current_type_, variable_value_type); current_type_ = context_manager_.ToModifiedValue(current_type_, variable_value_type);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -1058,12 +1058,12 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
void TypeCheckVisitor::Visit(TupleName* node) { void TypeCheckVisitor::Visit(TupleName* node) {
utils::IdType type = current_type_; utils::IdType type = current_type_;
std::optional<info::type::DefinedType*> maybe_defined_type_value = context_manager_.GetType<info::type::DefinedType>(type); std::optional<info::type::DefinedType*> maybe_defined_type_value = context_manager_.GetValue<info::type::DefinedType>(type);
if (maybe_defined_type_value.has_value()) { if (maybe_defined_type_value.has_value()) {
type = maybe_defined_type_value.value()->GetType(); type = maybe_defined_type_value.value()->GetType();
} }
std::optional<info::type::TupleType*> maybe_type_value = context_manager_.GetType<info::type::TupleType>(type); std::optional<info::type::TupleType*> maybe_type_value = context_manager_.GetValue<info::type::TupleType>(type);
if (maybe_type_value.has_value()) { if (maybe_type_value.has_value()) {
error_handling::HandleTypecheckError("Mismatched types in tuple variable definition", node->base); error_handling::HandleTypecheckError("Mismatched types in tuple variable definition", node->base);
@ -1087,7 +1087,7 @@ void TypeCheckVisitor::Visit(TupleName* node) {
current_type_ = maybe_type_value.value()->GetFields()[i].second; current_type_ = maybe_type_value.value()->GetFields()[i].second;
if (value_type == utils::ValueType::Tmp) { // TODO: ?? if (value_type == utils::ValueType::Tmp) { // TODO: ??
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp); current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
} }
Visitor::Visit(node->names[i]); Visitor::Visit(node->names[i]);
@ -1101,12 +1101,12 @@ void TypeCheckVisitor::Visit(TupleName* node) {
void TypeCheckVisitor::Visit(VariantName* node) { void TypeCheckVisitor::Visit(VariantName* node) {
utils::IdType type = current_type_; utils::IdType type = current_type_;
std::optional<info::type::DefinedType*> maybe_defined_type_value = context_manager_.GetType<info::type::DefinedType>(type); std::optional<info::type::DefinedType*> maybe_defined_type_value = context_manager_.GetValue<info::type::DefinedType>(type);
if (maybe_defined_type_value.has_value()) { if (maybe_defined_type_value.has_value()) {
type = maybe_defined_type_value.value()->GetType(); type = maybe_defined_type_value.value()->GetType();
} }
std::optional<info::type::VariantType*> maybe_type_value = context_manager_.GetType<info::type::VariantType>(type); std::optional<info::type::VariantType*> maybe_type_value = context_manager_.GetValue<info::type::VariantType>(type);
if (!maybe_type_value.has_value()) { if (!maybe_type_value.has_value()) {
error_handling::HandleTypecheckError("Mismatched types in variant variable definition", node->base); error_handling::HandleTypecheckError("Mismatched types in variant variable definition", node->base);
@ -1128,17 +1128,17 @@ void TypeCheckVisitor::Visit(VariantName* node) {
for (size_t i = 0; i < node->names.size(); ++i) { for (size_t i = 0; i < node->names.size(); ++i) {
if (maybe_type_value.value()->GetConstructors()[i].GetFields().empty()) { if (maybe_type_value.value()->GetConstructors()[i].GetFields().empty()) {
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::OptionalType(context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp), context_manager_.GetTypeManager()), info::type::OptionalType(context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp), context_manager_.GetValueManager()),
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ?? utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
} else { } else {
info::type::TupleType constructor_type_value = maybe_type_value.value()->GetConstructors()[i]; info::type::TupleType constructor_type_value = maybe_type_value.value()->GetConstructors()[i];
utils::IdType constructor_type = utils::IdType constructor_type =
context_manager_.AddType(std::move(constructor_type_value), context_manager_.AddValue(std::move(constructor_type_value),
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ?? utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::OptionalType(constructor_type, context_manager_.GetTypeManager()), info::type::OptionalType(constructor_type, context_manager_.GetValueManager()),
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ?? utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
} }
@ -1163,13 +1163,13 @@ void TypeCheckVisitor::Visit(AnnotatedName* node) {
error_handling::HandleTypecheckError("TupleName: value type expression not match from variable definition modifier", node->base); error_handling::HandleTypecheckError("TupleName: value type expression not match from variable definition modifier", node->base);
} }
type = context_manager_.ToModifiedType(type, utils::IsConstModifierToValueType(is_const_definition_.value())); type = context_manager_.ToModifiedValue(type, utils::IsConstModifierToValueType(is_const_definition_.value()));
if (!context_manager_.DefineVariable(node->name, type)) { if (!context_manager_.DefineVariable(node->name, type)) {
error_handling::HandleTypecheckError("Variable name already present in context", node->base); error_handling::HandleTypecheckError("Variable name already present in context", node->base);
} }
if (node->type.has_value()) { if (node->type.has_value()) {
Visitor::Visit(node->type.value()); Visitor::Visit(node->type.value());
if (!context_manager_.EqualTypes(type, current_type_)) { // TODO ?? if (!context_manager_.EqualValues(type, current_type_)) { // TODO ??
error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base); error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base);
} }
} }
@ -1192,8 +1192,8 @@ void TypeCheckVisitor::Visit(FunctionType* node) {
utils::IdType return_type = argument_types.back(); utils::IdType return_type = argument_types.back();
argument_types.pop_back(); argument_types.pop_back();
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::FunctionType(argument_types, return_type, context_manager_.GetTypeManager()), info::type::FunctionType(argument_types, return_type, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -1212,8 +1212,8 @@ void TypeCheckVisitor::Visit(TupleType* node) {
} }
} }
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::TupleType(node->type, fields, context_manager_.GetTypeManager()), info::type::TupleType(node->type, fields, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -1226,10 +1226,10 @@ void TypeCheckVisitor::Visit(VariantType* node) {
for (auto& constructor : node->constructors) { for (auto& constructor : node->constructors) {
if (std::holds_alternative<Constructor>(constructor)) { if (std::holds_alternative<Constructor>(constructor)) {
std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields; std::vector<std::pair<std::optional<std::string>, utils::IdType>> constructor_fields;
constructors.push_back(info::type::TupleType(std::get<Constructor>(constructor), constructor_fields, context_manager_.GetTypeManager())); constructors.push_back(info::type::TupleType(std::get<Constructor>(constructor), constructor_fields, context_manager_.GetValueManager()));
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) { } else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get()); Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
std::optional<info::type::TupleType*> maybe_constructor = context_manager_.GetType<info::type::TupleType>(current_type_); std::optional<info::type::TupleType*> maybe_constructor = context_manager_.GetValue<info::type::TupleType>(current_type_);
if (!maybe_constructor.has_value()) { if (!maybe_constructor.has_value()) {
error_handling::HandleInternalError("Entity of VariantType is not TupleType", "TypeCheckVisitor.VariantType"); error_handling::HandleInternalError("Entity of VariantType is not TupleType", "TypeCheckVisitor.VariantType");
} }
@ -1239,7 +1239,7 @@ void TypeCheckVisitor::Visit(VariantType* node) {
} }
} }
current_type_ = context_manager_.AddType(info::type::VariantType(node->type, constructors, -1), current_type_ = context_manager_.AddValue(info::type::VariantType(node->type, constructors, -1),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -1256,9 +1256,9 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
if (maybe_internal_type.has_value()) { if (maybe_internal_type.has_value()) {
// checks made in link_symbols_visitor // checks made in link_symbols_visitor
current_type_ = context_manager_.AddType<info::type::InternalType>(maybe_internal_type.value(), utils::ValueType::Tmp); current_type_ = context_manager_.AddValue<info::type::InternalType>(maybe_internal_type.value(), utils::ValueType::Tmp);
} else { } else {
std::optional<utils::IdType> maybe_local_abstract_type = context_manager_.GetLocalAbstractType(node->type.type); std::optional<utils::IdType> maybe_local_abstract_type = context_manager_.GetLocalType(node->type.type);
if (node->path.empty() && maybe_local_abstract_type.has_value()) { if (node->path.empty() && maybe_local_abstract_type.has_value()) {
current_type_ = maybe_local_abstract_type.value(); current_type_ = maybe_local_abstract_type.value();
} else if (node->type.type_id_.has_value()) { // TODO: chack that names are different (always true ??) } else if (node->type.type_id_.has_value()) { // TODO: chack that names are different (always true ??)
@ -1278,8 +1278,8 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
} }
if (node->array_size.has_value()) { if (node->array_size.has_value()) {
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetTypeManager()), info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
} }
@ -1289,8 +1289,8 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) { void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) {
Visitor::Visit(node->type); Visitor::Visit(node->type);
current_type_ = context_manager_.AddType( current_type_ = context_manager_.AddValue(
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()), info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp); utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
@ -1313,37 +1313,37 @@ void TypeCheckVisitor::Visit(ParametrizedType* node) {} // Handled in TypeExpres
// Identifiers, constants, etc. ----------------- // Identifiers, constants, etc. -----------------
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) { void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Float, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Float, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(NumberLiteral* node) { void TypeCheckVisitor::Visit(NumberLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Int, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Int, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(StringLiteral* node) { void TypeCheckVisitor::Visit(StringLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::String, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::String, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(CharLiteral* node) { void TypeCheckVisitor::Visit(CharLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Char, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Char, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(UnitLiteral* node) { void TypeCheckVisitor::Visit(UnitLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
void TypeCheckVisitor::Visit(BoolLiteral* node) { void TypeCheckVisitor::Visit(BoolLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp); current_type_ = context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp);
node->base.type_ = current_type_; node->base.type_ = current_type_;
} }
@ -1390,9 +1390,9 @@ void TypeCheckVisitor::CollectTypeExpressionContext(const TypeExpression& type_e
utils::IdType TypeCheckVisitor::TypeInContext(utils::IdType type, utils::IdType TypeCheckVisitor::TypeInContext(utils::IdType type,
const std::unordered_map<std::string, utils::IdType>& context) { const std::unordered_map<std::string, utils::IdType>& context) {
info::type::Type type_in_context = *context_manager_.GetAnyType(type); info::type::Type type_in_context = *context_manager_.GetAnyValue(type);
type_in_context.InContext(context); type_in_context.InContext(context);
return context_manager_.AddType(std::move(type_in_context), utils::ValueType::Tmp); return context_manager_.AddValue(std::move(type_in_context), utils::ValueType::Tmp);
} }
void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) { // <-> ScopedPattern void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) { // <-> ScopedPattern
@ -1405,7 +1405,7 @@ void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) {
break; break;
case 1: case 1:
Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node)); Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node));
if (context_manager_.EqualTypes(current_type_, value_type)) { // TODO: better solution ?? if (context_manager_.EqualValues(current_type_, value_type)) { // TODO: better solution ??
error_handling::HandleTypecheckError("Literal and value have different types", base_node); error_handling::HandleTypecheckError("Literal and value have different types", base_node);
} }
break; break;