|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <vector> |
| 5 | +#include <lbd/type/TypeKind.hpp> |
| 6 | + |
| 7 | +namespace lbd::type |
| 8 | +{ |
| 9 | + class Type; |
| 10 | + using TypePtr = std::shared_ptr<Type>; |
| 11 | + |
| 12 | + struct TypePtrHash |
| 13 | + { |
| 14 | + size_t operator()(const TypePtr& t) const noexcept; |
| 15 | + }; |
| 16 | + |
| 17 | + struct TypePtrEq |
| 18 | + { |
| 19 | + bool operator()(const TypePtr& a, const TypePtr& b) const noexcept; |
| 20 | + }; |
| 21 | + |
| 22 | + // Usage: std::unordered_map<TypePtr, TypePtr, TypePtrHash, TypePtrEq> typeSubstitutionMap; |
| 23 | + |
| 24 | + struct Constraint |
| 25 | + { |
| 26 | + std::string className; // Eq, Ord, Show. |
| 27 | + TypePtr type; // Most likely will be a VARIABLE: a, b. |
| 28 | + |
| 29 | + [[nodiscard]] bool operator==(const Constraint& other) const noexcept; |
| 30 | + }; |
| 31 | + |
| 32 | + class Type |
| 33 | + { |
| 34 | + public: |
| 35 | + // Factory functions. |
| 36 | + [[nodiscard]] static TypePtr named(const std::string& name); |
| 37 | + [[nodiscard]] static TypePtr variable(const std::string& name); |
| 38 | + [[nodiscard]] static TypePtr applied(const TypePtr& base, std::vector<TypePtr> arguments); |
| 39 | + [[nodiscard]] static TypePtr function(const TypePtr& from, const TypePtr& to); |
| 40 | + [[nodiscard]] static TypePtr qualified(std::vector<Constraint> constraints, const TypePtr& qualifiedType); |
| 41 | + [[nodiscard]] static TypePtr any(); |
| 42 | + |
| 43 | + // Observers. |
| 44 | + [[nodiscard]] TypeKind getKind() const noexcept; |
| 45 | + [[nodiscard]] const std::string& getName() const noexcept; |
| 46 | + [[nodiscard]] const TypePtr& getBase() const noexcept; |
| 47 | + [[nodiscard]] const std::vector<TypePtr>& getArguments() const noexcept; |
| 48 | + [[nodiscard]] const TypePtr& getFrom() const noexcept; |
| 49 | + [[nodiscard]] const TypePtr& getTo() const noexcept; |
| 50 | + [[nodiscard]] const std::vector<Constraint>& getConstraints() const noexcept; |
| 51 | + [[nodiscard]] const TypePtr& getQualifiedType() const noexcept; |
| 52 | + |
| 53 | + // Comparison. |
| 54 | + [[nodiscard]] bool equals(const Type& other) const noexcept; |
| 55 | + [[nodiscard]] bool accepts(const Type& other) const noexcept; |
| 56 | + [[nodiscard]] bool operator==(const Type& other) const noexcept; |
| 57 | + [[nodiscard]] size_t hash() const noexcept; |
| 58 | + |
| 59 | + friend std::ostream& operator<<(std::ostream&, const Type&) noexcept; |
| 60 | + |
| 61 | + private: |
| 62 | + TypeKind kind; |
| 63 | + |
| 64 | + // NAMED, VARIABLE. |
| 65 | + std::string name; |
| 66 | + |
| 67 | + // APPLIED. |
| 68 | + TypePtr base; |
| 69 | + std::vector<TypePtr> arguments; |
| 70 | + |
| 71 | + // FUNCTION. |
| 72 | + TypePtr from; |
| 73 | + TypePtr to; |
| 74 | + |
| 75 | + // QUALIFIED. |
| 76 | + std::vector<Constraint> constraints; |
| 77 | + TypePtr qualifiedType; |
| 78 | + }; |
| 79 | +} |
0 commit comments