1
2
3
4
5
6 #include <cmajor/symbols/ConversionTable.hpp>
7 #include <cmajor/symbols/FunctionSymbol.hpp>
8 #include <cmajor/symbols/ClassTemplateSpecializationSymbol.hpp>
9 #include <cmajor/symbols/Exception.hpp>
10 #include <cmajor/symbols/Module.hpp>
11 #include <cmajor/symbols/DebugFlags.hpp>
12
13 namespace cmajor { namespace symbols {
14
15 ConversionTableEntry::ConversionTableEntry(TypeSymbol* sourceType_, TypeSymbol* targetType_) : sourceType(sourceType_), targetType(targetType_)
16 {
17 #ifdef VALID_CONVERSION_TABLE_ENTRY_CHECK
18
19 #endif
20 }
21
22 ConversionTable::ConversionTable(Owner owner_, Module* module_) : owner(owner_), module(module_)
23 {
24 }
25
26 void ConversionTableEntry::CheckValid() const
27 {
28 if (!sourceType || !targetType)
29 {
30 throw std::runtime_error("invalid conversion table entry: source type is null or target type is null");
31 }
32 if (!sourceType->BaseType() || !targetType->BaseType())
33 {
34 throw std::runtime_error("invalid conversion table entry: source base type is null or target base type is null");
35 }
36 }
37
38 void ConversionTable::AddConversion(FunctionSymbol* conversion)
39 {
40 #ifdef IMMUTABLE_MODULE_CHECK
41
42
43
44
45 #endif
46 ConversionTableEntry entry(conversion->ConversionSourceType(), conversion->ConversionTargetType());
47 #ifdef VALID_CONVERSION_TABLE_ENTRY_CHECK
48
49 #endif
50 conversionMap.erase(entry);
51 conversionMap.insert(std::make_pair(entry, conversion));
52 }
53
54 void ConversionTable::Add(const ConversionTable& that)
55 {
56 #ifdef IMMUTABLE_MODULE_CHECK
57
58
59
60
61 #endif
62 for (const auto& p : that.conversionMap)
63 {
64 ConversionTableEntry entry(p.first);
65 #ifdef VALID_CONVERSION_TABLE_ENTRY_CHECK
66
67 #endif
68 conversionMap[entry] = p.second;
69 }
70 }
71
72 void ConversionTable::Check()
73 {
74 for (const auto& p : conversionMap)
75 {
76 const ConversionTableEntry& entry = p.first;
77 entry.CheckValid();
78 }
79 }
80
81 FunctionSymbol* ConversionTable::GetConversion(TypeSymbol* sourceType, TypeSymbol* targetType, const Span& span, const boost::uuids::uuid& moduleId) const
82 {
83 TypeSymbol* sourcePlainType = sourceType->PlainType(span, moduleId);
84 TypeSymbol* targetPlainType = targetType->PlainType(span, moduleId);
85 ConversionTableEntry entry(sourcePlainType, targetPlainType);
86 auto it = conversionMap.find(entry);
87 if (it != conversionMap.cend())
88 {
89 return it->second;
90 }
91 return nullptr;
92 }
93
94 void ConversionTable::AddGeneratedConversion(std::std::unique_ptr<FunctionSymbol>&&generatedConversion)
95 {
96 generatedConversions.push_back(std::move(generatedConversion));
97 }
98
99 } }