6 #include <common/containerutil.hpp>
7 #include <exceptions/messageexception.hpp>
8 #include <irprocessor/irprocessor.hpp>
9 #include <message/message.hpp>
17 bool IRCompiler::Compile()
21 std::size_t numberOfTrackBlocks = m_IR.TrackBlocks.size();
23 for (std::size_t i = 0; i < numberOfTrackBlocks; i++)
30 catch (const Exceptions::MessageException& e)
35 catch (const std::exception& e)
39 Message::MessageKind::FetalError,
40 Message::MessageID::UnknownInIRProcessor,
51 std::string IRCompiler::GetSourceName() const
56 const IR::Module& IRCompiler::GetIR() const
61 void IRCompiler::InitializeAttributeProcessorFactories()
66 void IRCompiler::AddAttributeProcessorFactory(std::unique_ptr<IAttributeProcessorFactory> pAttributeProcessorFactory)
68 auto name = pAttributeProcessorFactory->GetAttributeName();
69 m_AttributeProcessorFactoryMap[name] = std::move(pAttributeProcessorFactory);
72 void IRCompiler::RegisterAttributeProcessors(const std::vector<AST::Attribute>& attributes)
74 std::vector<std::unique_ptr<IAttributeProcessor>> attrProcessors;
75 attrProcessors.reserve(attributes.size());
77 for (auto&& i : attributes)
79 auto itFactory = m_AttributeProcessorFactoryMap.find(i.Name);
81 if (itFactory == m_AttributeProcessorFactoryMap.end())
83 throw Exceptions::MessageException(
85 Message::MessageKind::Error,
86 Message::MessageID::InvalidAttributeName,
95 attrProcessors.push_back(itFactory->second->CreateProcessor(i));
99 m_AttributeProcessorStack.push_back(std::move(attrProcessors));
102 IR::BlockReference IRCompiler::DuplicateBlock(IR::BlockReference blockRef)
104 IR::BlockReference newRef{m_IR.Blocks.size()};
105 m_IR.Blocks.push_back(m_IR.Blocks.at(blockRef.ID));
109 void IRCompiler::CompileTrackBlock(std::size_t index)
111 RegisterAttributeProcessors(m_IR.TrackBlocks.at(index).Attributes);
112 Common::AutoPop<decltype(m_AttributeProcessorStack)> autoPop(m_AttributeProcessorStack);
114 for (auto&& i : m_IR.TrackBlocks[index].Blocks)
116 i.apply_visitor(*this);
120 void IRCompiler::operator()(IR::TrackList& trackList)
122 RegisterAttributeProcessors(trackList.Attributes);
123 Common::AutoPop<decltype(m_AttributeProcessorStack)> autoPop(m_AttributeProcessorStack);
125 for (auto&& i : trackList.Tracks)
127 RegisterAttributeProcessors(i.Attributes);
128 Common::AutoPop<decltype(m_AttributeProcessorStack)> autoPop2(m_AttributeProcessorStack);
130 for (auto&& j : i.Items)
132 RegisterAttributeProcessors(j.Attributes);
133 Common::AutoPop<decltype(m_AttributeProcessorStack)> autoPop3(m_AttributeProcessorStack);
135 auto dupRef = DuplicateBlock(j.Block);
138 CompileBlock(dupRef);
143 void IRCompiler::operator()(AST::Command&)
148 void IRCompiler::CompileBlock(IR::BlockReference blockRef)
150 RegisterAttributeProcessors(m_IR.Blocks.at(blockRef.ID).Attributes);
151 Common::AutoPop<decltype(m_AttributeProcessorStack)> autoPop(m_AttributeProcessorStack);
153 for (auto&& i : m_IR.Blocks[blockRef.ID].Events)
155 i.apply_visitor(*this);
159 void IRCompiler::operator()(IR::Event& ev)
162 m_AttributeProcessorStack.rbegin(),
163 m_AttributeProcessorStack.rend(),
168 i->TransformEvent(ev);
174 void IRCompiler::operator()(IR::BlockReference& blockRef)
176 // 'blockRef' here should be referenced by a single block, thus no need to duplicate it.
177 // blockRef = DuplicateBlock(blockRef);
178 CompileBlock(blockRef);
181 } // namespace IRProcessor