Line data Source code
1 : #ifndef TYPE_TABLE_H
2 : #define TYPE_TABLE_H
3 :
4 : #include "../../errors.h"
5 : #include "../../lexicalAnalysis/type.h"
6 : #include "types/basicTypes/basicTypes.h"
7 : #include "types/specialTypes/breakType.h"
8 : #include "types/specialTypes/constType.h"
9 : #include "types/specialTypes/noPropagateType.h"
10 : #include "types/specialTypes/nullType.h"
11 : #include "types/specialTypes/ptrType.h"
12 : #include "types/specialTypes/vectorType.h"
13 : #include "types/specialTypes/voidType.h"
14 : #include "types/type.h"
15 : #include "types/userTypes/enumType.h"
16 : #include "types/userTypes/genericInstanceType.h"
17 : #include "types/userTypes/genericParameter.h"
18 : #include "types/userTypes/placeHolder.h"
19 : #include "types/userTypes/userType.h"
20 : #include <memory>
21 : #include <string>
22 : #include <unordered_map>
23 :
24 : namespace nicole {
25 :
26 : class TypeTable {
27 : private:
28 : std::unordered_map<std::string, std::shared_ptr<Type>> table_{
29 : {"bool", std::make_shared<BasicType>(BasicKind::Bool)},
30 : {"int", std::make_shared<BasicType>(BasicKind::Int)},
31 : {"float", std::make_shared<BasicType>(BasicKind::Float)},
32 : {"double", std::make_shared<BasicType>(BasicKind::Double)},
33 : {"char", std::make_shared<BasicType>(BasicKind::Char)},
34 : {"str", std::make_shared<BasicType>(BasicKind::Str)},
35 : {"void", std::make_shared<VoidType>()},
36 : };
37 :
38 : std::shared_ptr<NullType> null_{std::make_shared<NullType>()};
39 :
40 : std::shared_ptr<NoPropagateType> noPropagate_{
41 : std::make_shared<NoPropagateType>()};
42 :
43 : std::shared_ptr<BreakType> breakType_{std::make_shared<BreakType>()};
44 :
45 : bool canAssignImpl(const std::shared_ptr<Type> &target,
46 : const std::shared_ptr<Type> &source,
47 : bool pointerContext) const noexcept;
48 :
49 : public:
50 0 : [[nodiscard]] const std::shared_ptr<NullType> &null() const noexcept {
51 0 : return null_;
52 0 : }
53 :
54 0 : [[nodiscard]] const std::shared_ptr<Type> &boolType() const noexcept {
55 0 : return *getType("bool");
56 0 : }
57 :
58 0 : [[nodiscard]] const std::shared_ptr<Type> &intType() const noexcept {
59 0 : return *getType("int");
60 0 : }
61 :
62 0 : [[nodiscard]] const std::shared_ptr<Type> &floatType() const noexcept {
63 0 : return *getType("float");
64 0 : }
65 :
66 0 : [[nodiscard]] const std::shared_ptr<Type> &doubleType() const noexcept {
67 0 : return *getType("double");
68 0 : }
69 :
70 0 : [[nodiscard]] const std::shared_ptr<Type> &charType() const noexcept {
71 0 : return *getType("char");
72 0 : }
73 :
74 0 : [[nodiscard]] const std::shared_ptr<Type> &strType() const noexcept {
75 0 : return *getType("str");
76 0 : }
77 :
78 0 : [[nodiscard]] const std::shared_ptr<Type> &voidType() const noexcept {
79 0 : return *getType("void");
80 0 : }
81 :
82 : [[nodiscard]] const std::shared_ptr<NoPropagateType> &
83 0 : noPropagateType() const noexcept {
84 0 : return noPropagate_;
85 0 : }
86 :
87 0 : [[nodiscard]] const std::shared_ptr<BreakType> &breakType() const noexcept {
88 0 : return breakType_;
89 0 : }
90 :
91 : [[nodiscard]] bool has(const std::string &id) const noexcept;
92 :
93 : [[nodiscard]] const std::expected<std::shared_ptr<Type>, Error>
94 : getType(const std::string &id) const noexcept;
95 :
96 : [[nodiscard]] std::expected<std::monostate, Error>
97 : insert(const std::shared_ptr<Type> &type) noexcept;
98 :
99 : [[nodiscard]] bool
100 : isPossibleType(const std::shared_ptr<Type> &type) const noexcept;
101 :
102 : [[nodiscard]] bool
103 : isGenericType(const std::shared_ptr<Type> &type,
104 : const std::vector<GenericParameter> &generics) const noexcept;
105 :
106 : [[nodiscard]] std::expected<std::shared_ptr<Type>, Error>
107 : isCompundEnumType(const std::shared_ptr<Type> &type) const noexcept;
108 :
109 : [[nodiscard]] std::expected<std::shared_ptr<Type>, Error>
110 : isCompundGenericType(
111 : const std::shared_ptr<Type> &type,
112 : const std::vector<GenericParameter> &list) const noexcept;
113 :
114 : [[nodiscard]] bool
115 : areSameType(const std::shared_ptr<Type> &type1,
116 : const std::shared_ptr<Type> &type2) const noexcept;
117 :
118 : [[nodiscard]] bool
119 : canAssign(const std::shared_ptr<Type> &target,
120 : const std::shared_ptr<Type> &source) const noexcept;
121 :
122 : [[nodiscard]] bool
123 : haveCommonAncestor(const std::shared_ptr<Type> &type1,
124 : const std::shared_ptr<Type> &type2) const noexcept;
125 :
126 : [[nodiscard]] std::expected<std::shared_ptr<Type>, Error>
127 : applyUnaryOperator(const std::shared_ptr<Type> &operand,
128 : const TokenType op) const noexcept;
129 :
130 : [[nodiscard]] std::expected<std::shared_ptr<Type>, Error>
131 : applyBinaryOperator(const std::shared_ptr<Type> &left,
132 : const std::shared_ptr<Type> &right,
133 : TokenType op) const noexcept;
134 :
135 : [[nodiscard]] std::expected<std::string, Error>
136 : nameMangling(const std::shared_ptr<Type> &type) const noexcept;
137 :
138 : [[nodiscard]] std::expected<std::string, Error>
139 : nameManglingImpl(const std::shared_ptr<Type> &type,
140 : std::string &result) const noexcept;
141 : };
142 :
143 : } // namespace nicole
144 :
145 : #endif
|