lang_2023/src/types.cpp

470 lines
13 KiB
C++
Raw Normal View History

2023-05-02 15:18:08 +03:00
// for clangd
#include "../include/types.hpp"
namespace info::type {
2023-05-03 15:03:57 +03:00
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;
}
2023-05-02 15:18:08 +03:00
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;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
std::optional<utils::IdType> DefinedType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
2023-05-09 15:51:13 +03:00
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_type_replacement.has_value()) {
type_ = maybe_type_replacement.value();
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
bool DefinedType::Same(const DefinedType& type) const {
return type_id_ == type.type_id_
2023-05-09 15:51:13 +03:00
&& type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
2023-05-02 15:18:08 +03:00
}
bool DefinedType::operator<(const DefinedType& type) const {
return type_id_ == type.type_id_
2023-05-09 15:51:13 +03:00
&& *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
2023-05-02 15:18:08 +03:00
}
bool DefinedType::operator>(const DefinedType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name) const {
2023-05-09 15:51:13 +03:00
return type_manager_->GetAnyValue(type_)->GetFieldType(name);
2023-05-06 19:26:14 +03:00
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
std::optional<utils::IdType> TupleType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
for (size_t i = 0; i < fields_.size(); ++i) {
2023-05-09 15:51:13 +03:00
std::optional<utils::IdType> maybe_field_replacement = type_manager_->GetAnyValue(fields_[i].second)->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_field_replacement.has_value()) {
fields_[i].second = maybe_field_replacement.value();
}
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
bool TupleType::Same(const TupleType& type) const {
if (fields_.size() != type.fields_.size()) {
return false;
}
for (size_t i = 0; i < fields_.size(); ++i) {
2023-05-09 15:51:13 +03:00
if (!type_manager_->GetAnyValue(fields_[i].second)->Same(*type_manager_->GetAnyValue(type.fields_[i].second))) {
2023-05-02 15:18:08 +03:00
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) {
2023-05-09 15:51:13 +03:00
if (!(*type_manager_->GetAnyValue(fields_[i].second) < *type_manager_->GetAnyValue(type.fields_[i].second))) {
2023-05-02 15:18:08 +03:00
return false;
}
}
return true;
}
bool TupleType::operator>(const TupleType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
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;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
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;
}
2023-05-02 15:18:08 +03:00
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;
}
2023-05-06 19:26:14 +03:00
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;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
std::optional<utils::IdType> OptionalType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
2023-05-09 15:51:13 +03:00
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_type_replacement.has_value()) {
type_ = maybe_type_replacement.value();
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
bool OptionalType::Same(const OptionalType& type) const {
2023-05-09 15:51:13 +03:00
return type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
2023-05-02 15:18:08 +03:00
}
bool OptionalType::operator<(const OptionalType& type) const {
2023-05-09 15:51:13 +03:00
return *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
2023-05-02 15:18:08 +03:00
}
bool OptionalType::operator>(const OptionalType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
std::optional<utils::IdType> ReferenceToType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
2023-05-09 15:51:13 +03:00
std::optional<utils::IdType> maybe_type_replacement = type_manager_->GetAnyValue(type_)->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_type_replacement.has_value()) {
type_ = maybe_type_replacement.value();
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
bool ReferenceToType::Same(const ReferenceToType& type) const {
2023-05-09 15:51:13 +03:00
return references_ == type.references_ && type_manager_->GetAnyValue(type_)->Same(*type_manager_->GetAnyValue(type.type_));
2023-05-02 15:18:08 +03:00
}
bool ReferenceToType::operator<(const ReferenceToType& type) const {
2023-05-09 15:51:13 +03:00
return references_ == type.references_ && *type_manager_->GetAnyValue(type_) < *type_manager_->GetAnyValue(type.type_);
2023-05-02 15:18:08 +03:00
}
bool ReferenceToType::operator>(const ReferenceToType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name) const {
2023-05-09 15:51:13 +03:00
return type_manager_->GetAnyValue(type_)->GetFieldType(name);
2023-05-06 19:26:14 +03:00
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
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 =
2023-05-09 15:51:13 +03:00
type_manager_->GetAnyValue(argument_types_[i])->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_argument_type_replacement.has_value()) {
argument_types_[i] = maybe_argument_type_replacement.value();
}
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
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) {
2023-05-09 15:51:13 +03:00
if (!type_manager_->GetAnyValue(argument_types_[i])->Same(*type_manager_->GetAnyValue(type.argument_types_[i]))) {
2023-05-02 15:18:08 +03:00
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) {
2023-05-09 15:51:13 +03:00
if (!(*type_manager_->GetAnyValue(argument_types_[i]) < *type_manager_->GetAnyValue(type.argument_types_[i]))) {
2023-05-02 15:18:08 +03:00
return false;
}
}
return true;
}
bool FunctionType::operator>(const FunctionType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
std::optional<utils::IdType> ArrayType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
std::optional<utils::IdType> maybe_elements_type_replacement =
2023-05-09 15:51:13 +03:00
type_manager_->GetAnyValue(elements_type_)->InContext(context);
2023-05-03 15:03:57 +03:00
if (maybe_elements_type_replacement.has_value()) {
elements_type_ = maybe_elements_type_replacement.value();
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
bool ArrayType::Same(const ArrayType& type) const {
2023-05-09 15:51:13 +03:00
return size_ == type.size_ && type_manager_->GetAnyValue(elements_type_)->Same(*type_manager_->GetAnyValue(type.elements_type_));
2023-05-02 15:18:08 +03:00
}
bool ArrayType::operator<(const ArrayType& type) const {
2023-05-09 15:51:13 +03:00
return size_ == type.size_ && *type_manager_->GetAnyValue(elements_type_) < *type_manager_->GetAnyValue(type.elements_type_);
2023-05-02 15:18:08 +03:00
}
bool ArrayType::operator>(const ArrayType& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name) const {
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
//
2023-05-03 15:03:57 +03:00
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);
2023-05-06 19:26:14 +03:00
case 8:
return std::get<OptionalType>(type_).InContext(context);
2023-05-03 15:03:57 +03:00
default:
// error
break;
}
return std::nullopt;
}
2023-05-02 15:18:08 +03:00
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_));
2023-05-06 19:26:14 +03:00
case 8:
return std::get<OptionalType>(type_).Same(std::get<OptionalType>(type.type_));
2023-05-02 15:18:08 +03:00
default:
// error
break;
}
}
return false;
2023-05-03 15:03:57 +03:00
}
2023-05-02 15:18:08 +03:00
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_);
2023-05-06 19:26:14 +03:00
case 8:
return std::get<OptionalType>(type_) < std::get<OptionalType>(type.type_);
2023-05-02 15:18:08 +03:00
default:
// error
break;
}
}
return false;
2023-05-03 15:03:57 +03:00
}
2023-05-02 15:18:08 +03:00
bool Type::operator>(const Type& type) const {
return type < *this;
}
2023-05-06 19:26:14 +03:00
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;
}
2023-05-07 15:17:37 +03:00
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 ""; // ??
}
2023-05-02 15:18:08 +03:00
} // namespace info::type