type_check_visitor part

This commit is contained in:
ProgramSnail 2023-05-03 15:03:57 +03:00
parent d13faf104d
commit 94ef8702fb
11 changed files with 496 additions and 288 deletions

View file

@ -1,8 +1,18 @@
// for clangd
#include "../include/types.hpp"
#include <cstddef>
namespace info::type {
std::optional<utils::IdType> AbstractType::InContext(const std::unordered_map<std::string, utils::IdType>& 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_;
}
@ -22,14 +32,24 @@ bool AbstractType::operator>(const AbstractType& type) const {
//
std::optional<utils::IdType> DefinedType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyType(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_->GetAnyType(type_).value()->Same(*type_manager_->GetAnyType(type.type_).value());
&& type_manager_->GetAnyType(type_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool DefinedType::operator<(const DefinedType& type) const {
return type_id_ == type.type_id_
&& *type_manager_->GetAnyType(type_).value() < *type_manager_->GetAnyType(type.type_).value();
&& *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool DefinedType::operator>(const DefinedType& type) const {
@ -38,13 +58,25 @@ bool DefinedType::operator>(const DefinedType& type) const {
//
std::optional<utils::IdType> TupleType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
for (size_t i = 0; i < fields_.size(); ++i) {
std::optional<utils::IdType> maybe_field_replacement = type_manager_->GetAnyType(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_->GetAnyType(fields_[i].second).value()->Same(*type_manager_->GetAnyType(type.fields_[i].second).value())) {
if (!type_manager_->GetAnyType(fields_[i].second)->Same(*type_manager_->GetAnyType(type.fields_[i].second))) {
return false;
}
}
@ -58,7 +90,7 @@ bool TupleType::operator<(const TupleType& type) const {
}
for (size_t i = 0; i < fields_.size(); ++i) {
if (!(*type_manager_->GetAnyType(fields_[i].second).value() < *type_manager_->GetAnyType(type.fields_[i].second).value())) {
if (!(*type_manager_->GetAnyType(fields_[i].second) < *type_manager_->GetAnyType(type.fields_[i].second))) {
return false;
}
}
@ -72,6 +104,14 @@ bool TupleType::operator>(const TupleType& type) const {
//
std::optional<utils::IdType> VariantType::InContext(const std::unordered_map<std::string, utils::IdType>& 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;
@ -106,12 +146,22 @@ bool VariantType::operator>(const VariantType& type) const {
//
std::optional<utils::IdType> OptionalType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyType(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_->GetAnyType(type_).value()->Same(*type_manager_->GetAnyType(type.type_).value());
return type_manager_->GetAnyType(type_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool OptionalType::operator<(const OptionalType& type) const {
return *type_manager_->GetAnyType(type_).value() < *type_manager_->GetAnyType(type.type_).value();
return *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool OptionalType::operator>(const OptionalType& type) const {
@ -120,12 +170,22 @@ bool OptionalType::operator>(const OptionalType& type) const {
//
std::optional<utils::IdType> ReferenceToType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyType(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_->GetAnyType(type_).value()->Same(*type_manager_->GetAnyType(type.type_).value());
return references_ == type.references_ && type_manager_->GetAnyType(type_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool ReferenceToType::operator<(const ReferenceToType& type) const {
return references_ == type.references_ && *type_manager_->GetAnyType(type_).value() < *type_manager_->GetAnyType(type.type_).value();
return references_ == type.references_ && *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool ReferenceToType::operator>(const ReferenceToType& type) const {
@ -134,13 +194,26 @@ bool ReferenceToType::operator>(const ReferenceToType& type) const {
//
std::optional<utils::IdType> FunctionType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
for (size_t i = 0; i < argument_types_.size(); ++i) {
std::optional<utils::IdType> maybe_argument_type_replacement =
type_manager_->GetAnyType(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_->GetAnyType(argument_types_[i]).value()->Same(*type_manager_->GetAnyType(type.argument_types_[i]).value())) {
if (!type_manager_->GetAnyType(argument_types_[i])->Same(*type_manager_->GetAnyType(type.argument_types_[i]))) {
return false;
}
}
@ -154,7 +227,7 @@ bool FunctionType::operator<(const FunctionType& type) const {
}
for (size_t i = 0; i < argument_types_.size(); ++i) {
if (!(*type_manager_->GetAnyType(argument_types_[i]).value() < *type_manager_->GetAnyType(type.argument_types_[i]).value())) {
if (!(*type_manager_->GetAnyType(argument_types_[i]) < *type_manager_->GetAnyType(type.argument_types_[i]))) {
return false;
}
}
@ -168,12 +241,23 @@ bool FunctionType::operator>(const FunctionType& type) const {
//
std::optional<utils::IdType> ArrayType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_elements_type_replacement =
type_manager_->GetAnyType(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_->GetAnyType(elements_type_).value()->Same(*type_manager_->GetAnyType(type.elements_type_).value());
return size_ == type.size_ && type_manager_->GetAnyType(elements_type_)->Same(*type_manager_->GetAnyType(type.elements_type_));
}
bool ArrayType::operator<(const ArrayType& type) const {
return size_ == type.size_ && *type_manager_->GetAnyType(elements_type_).value() < *type_manager_->GetAnyType(type.elements_type_).value();
return size_ == type.size_ && *type_manager_->GetAnyType(elements_type_) < *type_manager_->GetAnyType(type.elements_type_);
}
bool ArrayType::operator>(const ArrayType& type) const {
@ -182,6 +266,34 @@ bool ArrayType::operator>(const ArrayType& type) const {
//
std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
size_t this_index = type_.index();
switch (this_index) {
case 0:
return std::get<AbstractType>(type_).InContext(context);
case 1:
return std::get<DefinedType>(type_).InContext(context);
case 2:
return std::nullopt;
case 3:
return std::get<TupleType>(type_).InContext(context);
case 4:
return std::get<VariantType>(type_).InContext(context);
case 5:
return std::get<ReferenceToType>(type_).InContext(context);
case 6:
return std::get<FunctionType>(type_).InContext(context);
case 7:
return std::get<ArrayType>(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();
@ -211,7 +323,7 @@ bool Type::Same(const Type& type) const {
}
return false;
} // some rule exceptions ??
}
bool Type::operator<(const Type& type) const {
size_t this_index = type_.index();
@ -242,7 +354,7 @@ bool Type::operator<(const Type& type) const {
}
return false;
} // TODO: some rule exceptions ??
}
bool Type::operator>(const Type& type) const {
return type < *this;
@ -264,12 +376,17 @@ std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
return &std::get<T>(types_.at(type_id));
}
std::optional<Type*> TypeManager::GetAnyType(utils::IdType type_id) {
Type* TypeManager::GetAnyType(utils::IdType type_id) {
return &types_.at(type_id);
}
void TypeManager::AddTypeRequirement(utils::IdType type, utils::IdType requrement) {} // TODO
void TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {} // TODO
bool TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {
return GetAnyType(first_type)->Same(*GetAnyType(second_type));
}
bool TypeManager::AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return *GetAnyType(requrement) < *GetAnyType(type);
}
} // namespace info::type