1
2
3
4
5
6 #ifndef CMAJOR_SYMBOLS_CONVERSION_TABLE_INCLUDED
7 #define CMAJOR_SYMBOLS_CONVERSION_TABLE_INCLUDED
8 #include <cmajor/symbols/TypeSymbol.hpp>
9 #include <cmajor/symbols/FunctionSymbol.hpp>
10 #include <boost/functional/hash.hpp>
11
12 namespace cmajor { namespace symbols {
13
14 class Module;
15
16 struct ConversionTableEntry
17 {
18 ConversionTableEntry(TypeSymbol* sourceType_, TypeSymbol* targetType_);
19 void CheckValid() const;
20 TypeSymbol* sourceType;
21 TypeSymbol* targetType;
22 };
23
24 inline bool operator==(const ConversionTableEntry& left, const ConversionTableEntry& right)
25 {
26 return TypesEqual(left.sourceType, right.sourceType) && TypesEqual(left.targetType, right.targetType);
27 }
28
29 struct ConversionTableEntryHash
30 {
31 size_t operator()(const ConversionTableEntry& entry) const
32 {
33 return boost::hash<boost::uuids::uuid>()(entry.sourceType->TypeId()) ^ boost::hash<boost::uuids::uuid>()(entry.targetType->TypeId());
34 }
35 };
36
37 class ConversionTable
38 {
39 public:
40 enum class Owner
41 {
42 compileUnit, symbolTable
43 };
44 ConversionTable(Owner owner_, Module* module_);
45 ConversionTable(const ConversionTable&) = delete;
46 ConversionTable& operator=(const ConversionTable&) = delete;
47 void AddConversion(FunctionSymbol* conversion);
48 FunctionSymbol* GetConversion(TypeSymbol* sourceType, TypeSymbol* targetType, const Span& span, const boost::uuids::uuid& moduleId) const;
49 void AddGeneratedConversion(std::std::unique_ptr<FunctionSymbol>&&generatedConversion);
50 void Add(const ConversionTable& that);
51 void Check();
52 private:
53 Owner owner;
54 Module* module;
55 std::unordered_map<ConversionTableEntry, FunctionSymbol*, ConversionTableEntryHash> conversionMap;
56 std::vector<std::std::unique_ptr<FunctionSymbol>>generatedConversions;
57 };
58
59 } }
60
61 #endif // CMAJOR_SYMBOLS_CONVERSION_TABLE_INCLUDED