// for clangd #include "../include/types.hpp" namespace info::type { std::optional AbstractType::InContext(const std::unordered_map& context) { auto type_iter = context.find(name_); if (type_iter != context.end()) { return type_iter->second; } return std::nullopt; } bool AbstractType::Same(const AbstractType& type) const { return name_ == type.name_; } bool AbstractType::operator<(const AbstractType& type) const { for (auto& graph_id : requirement_graph_ids_) { if (type.requirement_graph_ids_.count(graph_id) == 0) { return false; } } return true; } bool AbstractType::operator>(const AbstractType& type) const { return type < *this; } std::optional AbstractType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { return std::nullopt; } // std::optional DefinedType::InContext(const std::unordered_map& context) { std::optional maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context); if (maybe_type_replacement.has_value()) { type_ = maybe_type_replacement.value(); } return std::nullopt; } bool DefinedType::Same(const DefinedType& type) const { return type_id_ == type.type_id_ && type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_)); } bool DefinedType::operator<(const DefinedType& type) const { return type_id_ == type.type_id_ && *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_); } bool DefinedType::operator>(const DefinedType& type) const { return type < *this; } std::optional DefinedType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { if (class_modifier_ == utils::ClassModifier::Struct || type_namespaces.count(type_id_) != 0) { return type_manager_->GetAnyValue(type_)->GetFieldType(name, type_namespaces); } return std::nullopt; } // std::optional TupleType::InContext(const std::unordered_map& context) { for (size_t i = 0; i < fields_.size(); ++i) { std::optional maybe_field_replacement = type_manager_->GetAnyValue(fields_[i].second)->InContext(context); if (maybe_field_replacement.has_value()) { fields_[i].second = maybe_field_replacement.value(); } } return std::nullopt; } bool TupleType::Same(const TupleType& type) const { if (fields_.size() != type.fields_.size()) { return false; } for (size_t i = 0; i < fields_.size(); ++i) { if (!type_manager_->GetAnyValue(fields_[i].second)->Same(*type_manager_->GetAnyValue(type.fields_[i].second))) { return false; } } return true; } bool TupleType::operator<(const TupleType& type) const { if (fields_.size() != type.fields_.size()) { return false; } for (size_t i = 0; i < fields_.size(); ++i) { if (!(*type_manager_->GetAnyValue(fields_[i].second) < *type_manager_->GetAnyValue(type.fields_[i].second))) { return false; } } return true; } bool TupleType::operator>(const TupleType& type) const { return type < *this; } std::optional TupleType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { for (size_t i = 0; i < fields_.size(); ++i) { // TODO: optimize?? if (fields_[i].first.has_value() && fields_[i].first.value() == name) { return fields_[i].second; } } return std::nullopt; } // std::optional VariantType::InContext(const std::unordered_map& context) { for (size_t i = 0; i < constructors_.size(); ++i) { constructors_[i].InContext(context); } return std::nullopt; } bool VariantType::Same(const VariantType& type) const { if (constructors_.size() != type.constructors_.size()) { return false; } for (size_t i = 0; i < constructors_.size(); ++i) { if (!constructors_[i].Same(constructors_[i])) { return false; } } return true; } bool VariantType::operator<(const VariantType& type) const { if (constructors_.size() != type.constructors_.size()) { return false; } for (size_t i = 0; i < constructors_.size(); ++i) { if (!(constructors_[i] < constructors_[i])) { return false; } } return true; } bool VariantType::operator>(const VariantType& type) const { return type < *this; } std::optional VariantType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { if (current_constructor_.has_value()) { return constructors_.at(current_constructor_.value()).GetFieldType(name, type_namespaces); } return std::nullopt; } // std::optional OptionalType::InContext(const std::unordered_map& context) { std::optional maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context); if (maybe_type_replacement.has_value()) { type_ = maybe_type_replacement.value(); } return std::nullopt; } bool OptionalType::Same(const OptionalType& type) const { return type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_)); } bool OptionalType::operator<(const OptionalType& type) const { return *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_); } bool OptionalType::operator>(const OptionalType& type) const { return type < *this; } std::optional OptionalType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { return std::nullopt; } // std::optional ReferenceToType::InContext(const std::unordered_map& context) { std::optional maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context); if (maybe_type_replacement.has_value()) { type_ = maybe_type_replacement.value(); } return std::nullopt; } bool ReferenceToType::Same(const ReferenceToType& type) const { return references_ == type.references_ && type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_)); } bool ReferenceToType::operator<(const ReferenceToType& type) const { return references_ == type.references_ && *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_); } bool ReferenceToType::operator>(const ReferenceToType& type) const { return type < *this; } std::optional ReferenceToType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { return type_manager_->GetAnyValue(type_)->GetFieldType(name, type_namespaces); } // std::optional FunctionType::InContext(const std::unordered_map& context) { for (size_t i = 0; i < argument_types_.size(); ++i) { std::optional maybe_argument_type_replacement = type_manager_->GetAnyValue(argument_types_[i])->InContext(context); if (maybe_argument_type_replacement.has_value()) { argument_types_[i] = maybe_argument_type_replacement.value(); } } return std::nullopt; } bool FunctionType::Same(const FunctionType& type) const { if (argument_types_.size() != type.argument_types_.size()) { return false; } for (size_t i = 0; i < argument_types_.size(); ++i) { if (!type_manager_->GetAnyValue(argument_types_[i])->Same(*type_manager_->GetAnyValue(type.argument_types_[i]))) { return false; } } return true; } bool FunctionType::operator<(const FunctionType& type) const { if (argument_types_.size() != type.argument_types_.size()) { return false; } for (size_t i = 0; i < argument_types_.size(); ++i) { if (!(*type_manager_->GetAnyValue(argument_types_[i]) < *type_manager_->GetAnyValue(type.argument_types_[i]))) { return false; } } return true; } bool FunctionType::operator>(const FunctionType& type) const { return type < *this; } std::optional FunctionType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { return std::nullopt; } // std::optional ArrayType::InContext(const std::unordered_map& context) { std::optional maybe_elements_type_replacement = type_manager_->GetAnyValue(elements_type_)->InContext(context); if (maybe_elements_type_replacement.has_value()) { elements_type_ = maybe_elements_type_replacement.value(); } return std::nullopt; } bool ArrayType::Same(const ArrayType& type) const { return size_ == type.size_ && type_manager_->GetAnyValue(elements_type_)->Same(*type_manager_->GetAnyValue(type.elements_type_)); } bool ArrayType::operator<(const ArrayType& type) const { return size_ == type.size_ && *type_manager_->GetAnyValue(elements_type_) < *type_manager_->GetAnyValue(type.elements_type_); } bool ArrayType::operator>(const ArrayType& type) const { return type < *this; } std::optional ArrayType::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { return std::nullopt; } // std::optional Type::InContext(const std::unordered_map& context) { size_t this_index = type_.index(); switch (this_index) { case 0: return std::get(type_).InContext(context); case 1: return std::get(type_).InContext(context); case 2: return std::nullopt; case 3: return std::get(type_).InContext(context); case 4: return std::get(type_).InContext(context); case 5: return std::get(type_).InContext(context); case 6: return std::get(type_).InContext(context); case 7: return std::get(type_).InContext(context); case 8: return std::get(type_).InContext(context); default: // error break; } return std::nullopt; } bool Type::Same(const Type& type) const { size_t this_index = type_.index(); size_t type_index = type.type_.index(); if (this_index == type_index) { switch (this_index) { case 0: return std::get(type_).Same(std::get(type.type_)); case 1: return std::get(type_).Same(std::get(type.type_)); case 2: return std::get(type_) == std::get(type.type_); case 3: return std::get(type_).Same(std::get(type.type_)); case 4: return std::get(type_).Same(std::get(type.type_)); case 5: return std::get(type_).Same(std::get(type.type_)); case 6: return std::get(type_).Same(std::get(type.type_)); case 7: return std::get(type_).Same(std::get(type.type_)); case 8: return std::get(type_).Same(std::get(type.type_)); default: // error break; } } return false; } bool Type::operator<(const Type& type) const { size_t this_index = type_.index(); size_t type_index = type.type_.index(); if (this_index == type_index) { switch (this_index) { case 0: return std::get(type_) < std::get(type.type_); case 1: return std::get(type_) < std::get(type.type_); case 2: return std::get(type_) == std::get(type.type_); case 3: return std::get(type_) < std::get(type.type_); case 4: return std::get(type_) < std::get(type.type_); case 5: return std::get(type_) < std::get(type.type_); case 6: return std::get(type_) < std::get(type.type_); case 7: return std::get(type_) < std::get(type.type_); case 8: return std::get(type_) < std::get(type.type_); default: // error break; } } return false; } bool Type::operator>(const Type& type) const { return type < *this; } std::optional Type::GetFieldType(const std::string& name, const std::unordered_set& type_namespaces) const { size_t index = type_.index(); switch (index) { case 0: return std::get(type_).GetFieldType(name, type_namespaces); case 1: return std::get(type_).GetFieldType(name, type_namespaces); case 2: return std::nullopt; case 3: return std::get(type_).GetFieldType(name, type_namespaces); case 4: return std::get(type_).GetFieldType(name, type_namespaces); case 5: return std::get(type_).GetFieldType(name, type_namespaces); case 6: return std::get(type_).GetFieldType(name, type_namespaces); case 7: return std::get(type_).GetFieldType(name, type_namespaces); case 8: return std::get(type_).GetFieldType(name, type_namespaces); default: // error break; } return std::nullopt; } std::string Type::GetTypeName() const { size_t index = type_.index(); switch (index) { case 0: return "AbstractType"; case 1: return "DefinedType"; case 2: return "Builtin"; case 3: return "TupleType"; case 4: return "VariantType"; case 5: return "ReferenceToType"; case 6: return "FunctionType"; case 7: return "ArrayType"; case 8: return "OptionalType"; default: // error break; } return ""; // ?? } } // namespace info::type