diff --git a/include/.type_info_contexts.hpp.kate-swp b/include/.type_info_contexts.hpp.kate-swp deleted file mode 100644 index 126d73c..0000000 Binary files a/include/.type_info_contexts.hpp.kate-swp and /dev/null differ diff --git a/include/contexts.hpp b/include/contexts.hpp index f47f6dc..2242f73 100644 --- a/include/contexts.hpp +++ b/include/contexts.hpp @@ -3,14 +3,15 @@ #include #include #include +#include #include // for clangd -#include "values.hpp" #include "utils.hpp" namespace info { +template class ContextManager { public: ContextManager() { @@ -22,29 +23,37 @@ public: 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); } template std::optional GetValue(utils::IdType value_id) { - return value_manager_.GetValue(value_id); + return value_manager_.template GetValue(value_id); } - value::Value* GetAnyValue(utils::IdType value_id) { + Value* GetAnyValue(utils::IdType 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) { return value_manager_.GetValueType(value_id); } 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); } - value::ValueManager* GetValueManager() { + ValueManager* GetValueManager() { return &value_manager_; } @@ -168,7 +177,7 @@ private: }; std::vector contexts_; - value::ValueManager value_manager_; + ValueManager value_manager_; }; } // namespace info diff --git a/include/execute_visitor.hpp b/include/execute_visitor.hpp index d18c4e4..a3704f8 100644 --- a/include/execute_visitor.hpp +++ b/include/execute_visitor.hpp @@ -7,6 +7,7 @@ #include "global_info.hpp" #include "interpreter_tree.hpp" #include "type_info_contexts.hpp" +#include "types.hpp" #include "utils.hpp" #include "values.hpp" #include "visitor.hpp" @@ -17,11 +18,11 @@ namespace interpreter { class ExecuteVisitor : public Visitor { public: explicit ExecuteVisitor(info::GlobalInfo& global_info, - info::TypeInfoContextManager& type_info_context_manager, - info::ContextManager& context_manager, + info::ContextManager& type_context_manager, + info::ContextManager& context_manager, interpreter::tokens::PartitionStatement* execution_root) : namespace_visitor_(global_info.CreateVisitor()), - type_info_context_manager_(type_info_context_manager), + type_context_manager_(type_context_manager), context_manager_(context_manager) {} void VisitSourceFile(SourceFile* source_file) override { @@ -161,8 +162,8 @@ private: } private: info::GlobalInfo::NamespaceVisitor namespace_visitor_; - info::TypeInfoContextManager& type_info_context_manager_; - info::ContextManager& context_manager_; + info::ContextManager& type_context_manager_; + info::ContextManager& context_manager_; utils::IdType current_value_; std::optional active_loop_control_expression_; diff --git a/include/type_check_visitor.hpp b/include/type_check_visitor.hpp index 4297043..9b1c9ff 100644 --- a/include/type_check_visitor.hpp +++ b/include/type_check_visitor.hpp @@ -2,7 +2,8 @@ // for clangd #include "interpreter_tree.hpp" -#include "type_info_contexts.hpp" +#include "types.hpp" +#include "contexts.hpp" #include "utils.hpp" #include "visitor.hpp" #include "global_info.hpp" @@ -14,7 +15,7 @@ namespace interpreter { class TypeCheckVisitor : public Visitor { public: explicit TypeCheckVisitor(info::GlobalInfo& global_info, - info::TypeInfoContextManager& context_manager) + info::ContextManager& context_manager) : namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {} private: @@ -139,7 +140,7 @@ private: private: info::GlobalInfo::NamespaceVisitor namespace_visitor_; - info::TypeInfoContextManager& context_manager_; + info::ContextManager& context_manager_; utils::IdType current_type_; std::optional returned_type_; diff --git a/include/type_info_contexts.hpp b/include/type_info_contexts.hpp deleted file mode 100644 index e846db0..0000000 --- a/include/type_info_contexts.hpp +++ /dev/null @@ -1,183 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -// for clangd -#include "types.hpp" -#include "utils.hpp" - -namespace info { - -// TODO: remember about type pointers -class TypeInfoContextManager { -public: - TypeInfoContextManager() { - contexts_.emplace_back(); - } - - template - 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 - std::optional GetType(utils::IdType type_id) { - return type_manager_.GetType(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 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 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 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 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 variables_; - std::unordered_map local_abstract_types_; - }; - - std::vector contexts_; - type::TypeManager type_manager_; -}; - -} // namespace info diff --git a/include/types.hpp b/include/types.hpp index 681d8fe..92750d5 100644 --- a/include/types.hpp +++ b/include/types.hpp @@ -293,25 +293,25 @@ private: class TypeManager { public: template - 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, value_type}); 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 {std::move(type), value_type}); return types_.size() - 1; } template - std::optional GetType(utils::IdType type_id) { + std::optional GetValue(utils::IdType type_id) { if (!std::holds_alternative(types_.at(type_id).first.GetType())) { return std::nullopt; } return &std::get(types_.at(type_id).first.GetType()); } - Type* GetAnyType(utils::IdType type_id) { + Type* GetAnyValue(utils::IdType type_id) { return &types_.at(type_id).first; } @@ -320,11 +320,11 @@ public: } 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) { - return *GetAnyType(requrement) < *GetAnyType(type); + return *GetAnyValue(requrement) < *GetAnyValue(type); } private: diff --git a/src/type_check_visitor.cpp b/src/type_check_visitor.cpp index 6d1ae12..96e7de0 100644 --- a/src/type_check_visitor.cpp +++ b/src/type_check_visitor.cpp @@ -16,7 +16,7 @@ void TypeCheckVisitor::Visit(SourceFile* node) { for (auto& statement : node->statements) { 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_; } @@ -27,7 +27,7 @@ void TypeCheckVisitor::Visit(NamespaceSources* node) { for (auto& statement : node->statements) { 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_; } @@ -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 std::vector 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), IsConstModifierToValueType(node->modifier.value())); context_manager_.EnterVariableContext("self", // TODO: different name ?? abstract_type); - context_manager_.DefineLocalAbstractType(node->type, abstract_type); + context_manager_.DefineLocalType(node->type, abstract_type); } else if (node->link_type_id_.has_value()) { Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo(node->link_type_id_.value()).value()->value); // handle error? context_manager_.EnterVariableContext( "self", // TODO: different name - context_manager_.AddType( - info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetTypeManager()), + context_manager_.AddValue( + info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetValueManager()), IsConstModifierToValueType(node->modifier.value()))); } } else { @@ -65,7 +65,7 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c namespace_visitor_.ExitNamespace(); 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_; } @@ -80,18 +80,18 @@ void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) { error_handling::HandleInternalError("Unimplemented", "TypeCheckVisitor.AliasDefinitionStatement"); // 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; // } // // context_manager_.EnterContext(); // // 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, // /*TODO*/{}), // utils::ValueType::Tmp); - // context_manager_.DefineLocalAbstractType(parameter, current_type_); + // context_manager_.DefineLocalType(parameter, current_type_); // } // // Visit(node->value.get()); @@ -108,7 +108,7 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) { Visitor::Visit(node->value); // current_type from value automatically passed to name definitions 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 ?? } @@ -116,7 +116,7 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) { Visitor::Visit(node->name); 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; node->base.type_ = current_type_; @@ -130,16 +130,16 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) { for (auto& parameter : node->parameters) { // declaration correctness check // TODO: needed?? std::vector requirements; // TODO: - current_type_ = context_manager_.AddType( + current_type_ = context_manager_.AddValue( info::type::AbstractType(utils::AbstractTypeModifier::Abstract, parameter->type, requirements), // TODO: typeclasses-requirements utils::ValueType::Tmp); - context_manager_.DefineLocalAbstractType(parameter->type, current_type_); + context_manager_.DefineLocalType(parameter->type, current_type_); } Visit(node->type.get()); 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) { is_in_statement_ = false; @@ -167,18 +167,18 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) { requirements.push_back(typeclass->typeclass_id_); } // 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, requirements), 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); } } for (size_t i = 0; i < node->definition->arguments.size(); ++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 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_; 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); } 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; node->base.type_ = current_type_; @@ -201,7 +201,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) { void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) { 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; node->base.type_ = current_type_; @@ -217,13 +217,13 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) { } // 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); - 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); } - 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; node->base.type_ = current_type_; @@ -231,7 +231,7 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) { void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) { 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; node->base.type_ = current_type_; @@ -244,7 +244,7 @@ void TypeCheckVisitor::Visit(PartitionStatement* node) { Visitor::Visit(node->value); 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; node->base.type_ = current_type_; @@ -253,19 +253,19 @@ void TypeCheckVisitor::Visit(PartitionStatement* node) { // Definition parts 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_; } 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_; } 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_; } @@ -339,7 +339,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name Visitor::Visit(*type_info.value); 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_; } @@ -349,7 +349,7 @@ void TypeCheckVisitor::Visit(MatchCase* node) { is_const_definition_ = utils::IsConstModifier::Const; // TODO: or var?? 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); @@ -358,8 +358,8 @@ void TypeCheckVisitor::Visit(MatchCase* node) { if (node->condition.has_value()) { Visitor::Visit(node->condition.value()); } - if (!context_manager_.EqualTypes( - context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), + if (!context_manager_.EqualValues( + context_manager_.AddValue(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) { 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_; is_type_found = true; } 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); } } } 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; @@ -417,7 +417,7 @@ void TypeCheckVisitor::Visit(Condition* node) { for (size_t i = 0; i < node->conditions.size(); ++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); } @@ -427,7 +427,7 @@ void TypeCheckVisitor::Visit(Condition* node) { if (i == 0) { type = current_type_; } 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); } } @@ -437,13 +437,13 @@ void TypeCheckVisitor::Visit(Condition* node) { context_manager_.EnterContext(); Visitor::Visit(node->statements[node->conditions.size()]); 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); } current_type_ = type; } else { - current_type_ = context_manager_.AddType( - info::type::OptionalType(type, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::OptionalType(type, context_manager_.GetValueManager()), utils::ValueType::Tmp); // ?? } @@ -452,7 +452,7 @@ void TypeCheckVisitor::Visit(Condition* node) { void TypeCheckVisitor::Visit(DoWhileLoop* node) { 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); } @@ -460,14 +460,14 @@ void TypeCheckVisitor::Visit(DoWhileLoop* node) { Visitor::Visit(node->statement); context_manager_.ExitContext(); - current_type_ = context_manager_.AddType( - info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); } void TypeCheckVisitor::Visit(WhileLoop* node) { 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); } @@ -475,8 +475,8 @@ void TypeCheckVisitor::Visit(WhileLoop* node) { Visitor::Visit(node->statement); context_manager_.ExitContext(); - current_type_ = context_manager_.AddType( - info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -484,7 +484,7 @@ void TypeCheckVisitor::Visit(WhileLoop* node) { void TypeCheckVisitor::Visit(ForLoop* node) { Visitor::Visit(node->interval); - std::optional maybe_interval_type = context_manager_.GetType(current_type_); + std::optional maybe_interval_type = context_manager_.GetValue(current_type_); if (!maybe_interval_type.has_value()) { error_handling::HandleTypecheckError("For loop interval type mismatch", node->base); @@ -492,7 +492,7 @@ void TypeCheckVisitor::Visit(ForLoop* node) { 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)); is_const_definition_ = node->variable_modifier; @@ -503,8 +503,8 @@ void TypeCheckVisitor::Visit(ForLoop* node) { context_manager_.ExitContext(); - current_type_ = context_manager_.AddType( - info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); } @@ -513,8 +513,8 @@ void TypeCheckVisitor::Visit(LoopLoop* node) { Visitor::Visit(node->statement); context_manager_.ExitContext(); - current_type_ = context_manager_.AddType( - info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(0, current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -537,7 +537,7 @@ void TypeCheckVisitor::Visit(Block* node) { type = returned_type_.value(); is_type_found = true; } 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); } } @@ -547,7 +547,7 @@ void TypeCheckVisitor::Visit(Block* node) { context_manager_.EnterContext(); 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; @@ -563,7 +563,7 @@ void TypeCheckVisitor::Visit(ScopedStatement* node) { } 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 @@ -581,7 +581,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) { is_method = false; } else { Visitor::Visit(node->left_expression); - auto maybe_left_type = context_manager_.GetType(current_type_); + auto maybe_left_type = context_manager_.GetValue(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 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); } @@ -653,7 +653,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) { utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types 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); } @@ -695,7 +695,7 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) { utils::IdType expression_type = current_type_; // TODO: type in context of deduced types 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); } @@ -709,20 +709,20 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) { void TypeCheckVisitor::Visit(ReferenceExpression* node) { Visit(node->expression.get()); - current_type_ = context_manager_.AddType( - info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()), context_manager_.GetValueType(current_type_)); // ?? } void TypeCheckVisitor::Visit(AccessExpression* node) { 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); } Visitor::Visit(node->name.get()); - std::optional maybe_type_value = context_manager_.GetType(current_type_); + std::optional maybe_type_value = context_manager_.GetValue(current_type_); if (!maybe_type_value.has_value()) { error_handling::HandleTypecheckError("Access type is not array", node->base); @@ -755,7 +755,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) { if (std::holds_alternative>(node->prefix.value())) { Visitor::Visit(*std::get>(node->prefix.value())); - std::optional maybe_expression_type = context_manager_.GetType(current_type_); + std::optional maybe_expression_type = context_manager_.GetValue(current_type_); if (!maybe_expression_type.has_value()) { 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); 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 @@ -868,8 +868,8 @@ void TypeCheckVisitor::Visit(TupleExpression* node) { fields.push_back({std::nullopt, current_type_}); } - current_type_ = context_manager_.AddType( - info::type::TupleType(std::nullopt, fields, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::TupleType(std::nullopt, fields, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -884,14 +884,14 @@ void TypeCheckVisitor::Visit(VariantExpression* node) { // TODO: deal with expression tuple types, etc, ?? std::vector, 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); - 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); node->base.type_ = current_type_; @@ -960,7 +960,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) { 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); } } @@ -974,7 +974,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) { Visitor::Visit(*type_info.value); std::optional maybe_variant_type = - context_manager_.GetType(current_type_); + context_manager_.GetValue(current_type_); if (maybe_type_info.has_value()) { maybe_variant_type.value()->SetCurrentConstructor(constructor_info.order); @@ -1000,7 +1000,7 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) { if (i == 0) { elements_type = current_type_; } 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); } } @@ -1010,8 +1010,8 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) { error_handling::HandleInternalError("Element size is 0", "TypeCheckVisitor.ArrayExpression"); } - current_type_ = context_manager_.AddType( - info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -1042,7 +1042,7 @@ void TypeCheckVisitor::Visit(NameExpression* node) { utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_); for (size_t i = 1; i < node->names.size(); ++i) { - std::optional maybe_type_value = context_manager_.GetAnyType(current_type_)->GetFieldType(node->names[i].name); + std::optional maybe_type_value = context_manager_.GetAnyValue(current_type_)->GetFieldType(node->names[i].name); if (!maybe_type_value.has_value()) { 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_ = context_manager_.ToModifiedType(current_type_, variable_value_type); + current_type_ = context_manager_.ToModifiedValue(current_type_, variable_value_type); node->base.type_ = current_type_; } @@ -1058,12 +1058,12 @@ void TypeCheckVisitor::Visit(NameExpression* node) { void TypeCheckVisitor::Visit(TupleName* node) { utils::IdType type = current_type_; - std::optional maybe_defined_type_value = context_manager_.GetType(type); + std::optional maybe_defined_type_value = context_manager_.GetValue(type); if (maybe_defined_type_value.has_value()) { type = maybe_defined_type_value.value()->GetType(); } - std::optional maybe_type_value = context_manager_.GetType(type); + std::optional maybe_type_value = context_manager_.GetValue(type); if (maybe_type_value.has_value()) { 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; 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]); @@ -1101,12 +1101,12 @@ void TypeCheckVisitor::Visit(TupleName* node) { void TypeCheckVisitor::Visit(VariantName* node) { utils::IdType type = current_type_; - std::optional maybe_defined_type_value = context_manager_.GetType(type); + std::optional maybe_defined_type_value = context_manager_.GetValue(type); if (maybe_defined_type_value.has_value()) { type = maybe_defined_type_value.value()->GetType(); } - std::optional maybe_type_value = context_manager_.GetType(type); + std::optional maybe_type_value = context_manager_.GetValue(type); if (!maybe_type_value.has_value()) { 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) { if (maybe_type_value.value()->GetConstructors()[i].GetFields().empty()) { - current_type_ = context_manager_.AddType( - info::type::OptionalType(context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp), context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::OptionalType(context_manager_.AddValue(info::type::InternalType::Unit, utils::ValueType::Tmp), context_manager_.GetValueManager()), utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ?? } else { info::type::TupleType constructor_type_value = maybe_type_value.value()->GetConstructors()[i]; 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 ?? - current_type_ = context_manager_.AddType( - info::type::OptionalType(constructor_type, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::OptionalType(constructor_type, context_manager_.GetValueManager()), 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); } - 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)) { error_handling::HandleTypecheckError("Variable name already present in context", node->base); } if (node->type.has_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); } } @@ -1192,8 +1192,8 @@ void TypeCheckVisitor::Visit(FunctionType* node) { utils::IdType return_type = argument_types.back(); argument_types.pop_back(); - current_type_ = context_manager_.AddType( - info::type::FunctionType(argument_types, return_type, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::FunctionType(argument_types, return_type, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -1212,8 +1212,8 @@ void TypeCheckVisitor::Visit(TupleType* node) { } } - current_type_ = context_manager_.AddType( - info::type::TupleType(node->type, fields, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::TupleType(node->type, fields, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -1226,10 +1226,10 @@ void TypeCheckVisitor::Visit(VariantType* node) { for (auto& constructor : node->constructors) { if (std::holds_alternative(constructor)) { std::vector, utils::IdType>> constructor_fields; - constructors.push_back(info::type::TupleType(std::get(constructor), constructor_fields, context_manager_.GetTypeManager())); + constructors.push_back(info::type::TupleType(std::get(constructor), constructor_fields, context_manager_.GetValueManager())); } else if (std::holds_alternative>(constructor)) { Visit(std::get>(constructor).get()); - std::optional maybe_constructor = context_manager_.GetType(current_type_); + std::optional maybe_constructor = context_manager_.GetValue(current_type_); if (!maybe_constructor.has_value()) { 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); node->base.type_ = current_type_; @@ -1256,9 +1256,9 @@ void TypeCheckVisitor::Visit(TypeExpression* node) { if (maybe_internal_type.has_value()) { // checks made in link_symbols_visitor - current_type_ = context_manager_.AddType(maybe_internal_type.value(), utils::ValueType::Tmp); + current_type_ = context_manager_.AddValue(maybe_internal_type.value(), utils::ValueType::Tmp); } else { - std::optional maybe_local_abstract_type = context_manager_.GetLocalAbstractType(node->type.type); + std::optional maybe_local_abstract_type = context_manager_.GetLocalType(node->type.type); if (node->path.empty() && maybe_local_abstract_type.has_value()) { current_type_ = maybe_local_abstract_type.value(); } 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()) { - current_type_ = context_manager_.AddType( - info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); } @@ -1289,8 +1289,8 @@ void TypeCheckVisitor::Visit(TypeExpression* node) { void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) { Visitor::Visit(node->type); - current_type_ = context_manager_.AddType( - info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()), + current_type_ = context_manager_.AddValue( + info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()), utils::ValueType::Tmp); node->base.type_ = current_type_; @@ -1313,37 +1313,37 @@ void TypeCheckVisitor::Visit(ParametrizedType* node) {} // Handled in TypeExpres // Identifiers, constants, etc. ----------------- 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_; } 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_; } 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_; } 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_; } 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_; } 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_; } @@ -1390,9 +1390,9 @@ void TypeCheckVisitor::CollectTypeExpressionContext(const TypeExpression& type_e utils::IdType TypeCheckVisitor::TypeInContext(utils::IdType type, const std::unordered_map& 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); - 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 @@ -1405,7 +1405,7 @@ void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) { break; case 1: Visitor::Visit(*std::get>(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); } break;