lang_2023/src/types.cpp
2023-05-07 15:17:37 +03:00

507 lines
14 KiB
C++

// 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_;
}
bool AbstractType::operator<(const AbstractType& type) const {
for (auto& typeclass : requirements_) {
if (type.requirements_.count(typeclass) == 0) {
return false;
}
}
return true;
}
bool AbstractType::operator>(const AbstractType& type) const {
return type < *this;
}
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
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_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool DefinedType::operator<(const DefinedType& type) const {
return type_id_ == type.type_id_
&& *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool DefinedType::operator>(const DefinedType& type) const {
return type < *this;
}
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name) const {
return type_manager_->GetAnyType(type_)->GetFieldType(name);
}
//
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)->Same(*type_manager_->GetAnyType(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_->GetAnyType(fields_[i].second) < *type_manager_->GetAnyType(type.fields_[i].second))) {
return false;
}
}
return true;
}
bool TupleType::operator>(const TupleType& type) const {
return type < *this;
}
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name) 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<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;
}
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<utils::IdType> VariantType::GetFieldType(const std::string& name) const {
if (current_constructor_.has_value()) {
return constructors_.at(current_constructor_.value()).GetFieldType(name);
}
return std::nullopt;
}
//
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_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool OptionalType::operator<(const OptionalType& type) const {
return *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool OptionalType::operator>(const OptionalType& type) const {
return type < *this;
}
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
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_)->Same(*type_manager_->GetAnyType(type.type_));
}
bool ReferenceToType::operator<(const ReferenceToType& type) const {
return references_ == type.references_ && *type_manager_->GetAnyType(type_) < *type_manager_->GetAnyType(type.type_);
}
bool ReferenceToType::operator>(const ReferenceToType& type) const {
return type < *this;
}
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name) const {
return type_manager_->GetAnyType(type_)->GetFieldType(name);
}
//
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])->Same(*type_manager_->GetAnyType(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_->GetAnyType(argument_types_[i]) < *type_manager_->GetAnyType(type.argument_types_[i]))) {
return false;
}
}
return true;
}
bool FunctionType::operator>(const FunctionType& type) const {
return type < *this;
}
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
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_)->Same(*type_manager_->GetAnyType(type.elements_type_));
}
bool ArrayType::operator<(const ArrayType& type) const {
return size_ == type.size_ && *type_manager_->GetAnyType(elements_type_) < *type_manager_->GetAnyType(type.elements_type_);
}
bool ArrayType::operator>(const ArrayType& type) const {
return type < *this;
}
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
//
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);
case 8:
return std::get<OptionalType>(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<AbstractType>(type_).Same(std::get<AbstractType>(type.type_));
case 1:
return std::get<DefinedType>(type_).Same(std::get<DefinedType>(type.type_));
case 2:
return std::get<InternalType>(type_) == std::get<InternalType>(type.type_);
case 3:
return std::get<TupleType>(type_).Same(std::get<TupleType>(type.type_));
case 4:
return std::get<VariantType>(type_).Same(std::get<VariantType>(type.type_));
case 5:
return std::get<ReferenceToType>(type_).Same(std::get<ReferenceToType>(type.type_));
case 6:
return std::get<FunctionType>(type_).Same(std::get<FunctionType>(type.type_));
case 7:
return std::get<ArrayType>(type_).Same(std::get<ArrayType>(type.type_));
case 8:
return std::get<OptionalType>(type_).Same(std::get<OptionalType>(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<AbstractType>(type_) < std::get<AbstractType>(type.type_);
case 1:
return std::get<DefinedType>(type_) < std::get<DefinedType>(type.type_);
case 2:
return std::get<InternalType>(type_) == std::get<InternalType>(type.type_);
case 3:
return std::get<TupleType>(type_) < std::get<TupleType>(type.type_);
case 4:
return std::get<VariantType>(type_) < std::get<VariantType>(type.type_);
case 5:
return std::get<ReferenceToType>(type_) < std::get<ReferenceToType>(type.type_);
case 6:
return std::get<FunctionType>(type_) < std::get<FunctionType>(type.type_);
case 7:
return std::get<ArrayType>(type_) < std::get<ArrayType>(type.type_);
case 8:
return std::get<OptionalType>(type_) < std::get<OptionalType>(type.type_);
default:
// error
break;
}
}
return false;
}
bool Type::operator>(const Type& type) const {
return type < *this;
}
std::optional<utils::IdType> Type::GetFieldType(const std::string& name) const {
size_t index = type_.index();
switch (index) {
case 0:
return std::get<AbstractType>(type_).GetFieldType(name);
case 1:
return std::get<DefinedType>(type_).GetFieldType(name);
case 2:
return std::nullopt;
case 3:
return std::get<TupleType>(type_).GetFieldType(name);
case 4:
return std::get<VariantType>(type_).GetFieldType(name);
case 5:
return std::get<ReferenceToType>(type_).GetFieldType(name);
case 6:
return std::get<FunctionType>(type_).GetFieldType(name);
case 7:
return std::get<ArrayType>(type_).GetFieldType(name);
case 8:
return std::get<OptionalType>(type_).GetFieldType(name);
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 ""; // ??
}
//
template<typename T>
utils::IdType TypeManager::AddType(T&& type, utils::ValueType value_type) {
types_.emplace_back({std::forward(type), value_type});
return types_.size() - 1;
}
utils::IdType TypeManager::AddAnyType(Type&& type, utils::ValueType value_type) {
types_.push_back({std::move(type), value_type});
return types_.size() - 1;
}
template<typename T>
std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
if (!std::holds_alternative<T>(types_.at(type_id).second)) {
return std::nullopt;
}
return &std::get<T>(types_.at(type_id).second);
}
Type* TypeManager::GetAnyType(utils::IdType type_id) {
return &types_.at(type_id).first;
}
utils::ValueType TypeManager::GetValueType(utils::IdType type_id) {
return types_.at(type_id).second;
}
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