1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 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& leftconst ConversionTableEntry& right)
25 {
26     return TypesEqual(left.sourceTyperight.sourceType) && TypesEqual(left.targetTyperight.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         compileUnitsymbolTable
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* sourceTypeTypeSymbol* targetTypeconst Span& spanconst 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<ConversionTableEntryFunctionSymbol*ConversionTableEntryHash> conversionMap;
56     std::vector<std::std::unique_ptr<FunctionSymbol>>generatedConversions;
57 };
58 
59 } } // namespace cmajor::symbols
60 
61 #endif // CMAJOR_SYMBOLS_CONVERSION_TABLE_INCLUDED