1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <cmajor/symbols/SymbolReader.hpp>
  7 #include <cmajor/symbols/ArrayTypeSymbol.hpp>
  8 #include <cmajor/symbols/DerivedTypeSymbol.hpp>
  9 #include <cmajor/symbols/ClassTemplateSpecializationSymbol.hpp>
 10 #include <cmajor/symbols/SymbolTable.hpp>
 11 
 12 namespace cmajor { namespace symbols {
 13 
 14 SymbolReader::SymbolReader(const std::string& fileName_) :
 15     astReader(fileName_)symbolTable(nullptr)module(nullptr)rootModule(nullptr)conversions(nullptr)arrayTypes(nullptr)derivedTypes(nullptr)
 16     classTemplateSpecializations(nullptr)typeAndConceptRequests(nullptr)functionRequests(nullptr)setProjectBit(false)symbolsCached(false)
 17 {
 18 }
 19 
 20 Symbol* SymbolReader::ReadSymbol(Symbol* parent)
 21 {
 22     SymbolType symbolType = static_cast<SymbolType>(astReader.GetBinaryReader().ReadByte());
 23     Span span = astReader.ReadSpan();
 24     boost::uuids::uuid sourceModuleId;
 25     GetBinaryReader().ReadUuid(sourceModuleId);
 26     std::u32string name = astReader.GetBinaryReader().ReadUtf32String();
 27     Symbol* symbol = SymbolFactory::Instance().CreateSymbol(symbolTypespansourceModuleIdname);
 28     symbol->SetModule(module);
 29     symbol->SetParent(parent);
 30     symbol->Read(*this);
 31     return symbol;
 32 }
 33 
 34 ArrayTypeSymbol* SymbolReader::ReadArrayTypeSymbol(Symbol* parent)
 35 {
 36     Symbol* symbol = ReadSymbol(parent);
 37     if (symbol->GetSymbolType() == SymbolType::arrayTypeSymbol)
 38     {
 39         return static_cast<ArrayTypeSymbol*>(symbol);
 40     }
 41     else
 42     {
 43         throw std::runtime_error("internal error: array type symbol expected");
 44     }
 45 }
 46 
 47 DerivedTypeSymbol* SymbolReader::ReadDerivedTypeSymbol(Symbol* parent)
 48 {
 49     Symbol* symbol = ReadSymbol(parent);
 50     if (symbol->GetSymbolType() == SymbolType::derivedTypeSymbol)
 51     {
 52         return static_cast<DerivedTypeSymbol*>(symbol);
 53     }
 54     else
 55     {
 56         throw std::runtime_error("internal error: derived type symbol expected");
 57     }
 58 }
 59 
 60 ClassTemplateSpecializationSymbol* SymbolReader::ReadClassTemplateSpecializationSymbol(Symbol* parent)
 61 {
 62     Symbol* symbol = ReadSymbol(parent);
 63     if (symbol->GetSymbolType() == SymbolType::classTemplateSpecializationSymbol)
 64     {
 65         return static_cast<ClassTemplateSpecializationSymbol*>(symbol);
 66     }
 67     else
 68     {
 69         throw std::runtime_error("internal error: class template specialization symbol expected");
 70     }
 71 }
 72 
 73 ParameterSymbol* SymbolReader::ReadParameterSymbol(Symbol* parent)
 74 {
 75     Symbol* symbol = ReadSymbol(parent);
 76     if (symbol->GetSymbolType() == SymbolType::parameterSymbol)
 77     {
 78         return static_cast<ParameterSymbol*>(symbol);
 79     }
 80     else
 81     {
 82         throw std::runtime_error("internal error: parameter symbol expected");
 83     }
 84 }
 85 
 86 void SymbolReader::AddConversion(FunctionSymbol* conversion)
 87 {
 88     if (conversions)
 89     {
 90         conversions->push_back(conversion);
 91     }
 92     else
 93     {
 94         throw std::runtime_error("reader conversions target not set");
 95     }
 96 }
 97 
 98 void SymbolReader::AddArrayType(ArrayTypeSymbol* arrayType)
 99 {
100     if (arrayTypes)
101     {
102         arrayTypes->push_back(arrayType);
103     }
104     else
105     {
106         throw std::runtime_error("reader array types target not set");
107     }
108 }
109 
110 void SymbolReader::AddDerivedType(DerivedTypeSymbol* derivedType)
111 {
112     if (derivedTypes)
113     {
114         derivedTypes->push_back(derivedType);
115     }
116     else
117     {
118         throw std::runtime_error("reader derived types target not set");
119     }
120 }
121 
122 void SymbolReader::AddClassTemplateSpecialization(ClassTemplateSpecializationSymbol* classTemplateSpecialization)
123 {
124     if (classTemplateSpecializations)
125     {
126         classTemplateSpecializations->push_back(classTemplateSpecialization);
127     }
128     else
129     {
130         throw std::runtime_error("reader class template specialization target not set");
131     }
132 }
133 
134 void SymbolReader::AddTypeOrConceptRequest(TypeOrConceptRequest&& typeOrConceptRequest)
135 {
136     if (typeAndConceptRequests)
137     {
138         typeAndConceptRequests->push_back(std::move(typeOrConceptRequest));
139     }
140     else
141     {
142         throw std::runtime_error("reader type and concept request target not set");
143     }
144 }
145 
146 void SymbolReader::AddFunctionRequest(FunctionRequest&& functionRequest)
147 {
148     if (functionRequests)
149     {
150         functionRequests->push_back(std::move(functionRequest));
151     }
152     else
153     {
154         throw std::runtime_error("reader function request target not set");
155     }
156 }
157 
158 } } // namespace cmajor::symbols