1
2
3
4
5
6 #include <sngcm/ast/AstReader.hpp>
7 #include <sngcm/ast/Identifier.hpp>
8 #include <sngcm/ast/Statement.hpp>
9 #include <sngcm/ast/Concept.hpp>
10 #include <sngcm/ast/Statement.hpp>
11
12 namespace sngcm { namespace ast {
13
14 AstReader::AstReader(const std::string& fileName_) : binaryReader(fileName_), moduleId(-1)
15 {
16 }
17
18 Node* AstReader::ReadNode()
19 {
20 NodeType nodeType = static_cast<NodeType>(binaryReader.ReadByte());
21 Span span = ReadSpan();
22 Node* node = NodeFactory::Instance().CreateNode(nodeType, span);
23 node->Read(*this);
24 return node;
25 }
26
27 IdentifierNode* AstReader::ReadIdentifierNode()
28 {
29 Node* node = ReadNode();
30 if (node->GetNodeType() == NodeType::identifierNode)
31 {
32 return static_cast<IdentifierNode*>(node);
33 }
34 else
35 {
36 throw std::runtime_error("identifier node expected");
37 }
38 }
39
40 LabelNode* AstReader::ReadLabelNode()
41 {
42 Node* node = ReadNode();
43 if (node->GetNodeType() == NodeType::labelNode)
44 {
45 return static_cast<LabelNode*>(node);
46 }
47 else
48 {
49 throw std::runtime_error("label node expected");
50 }
51 }
52
53 StatementNode* AstReader::ReadStatementNode()
54 {
55 Node* node = ReadNode();
56 if (node->IsStatementNode())
57 {
58 return static_cast<StatementNode*>(node);
59 }
60 else
61 {
62 throw std::runtime_error("statement node expected");
63 }
64 }
65
66 DefaultStatementNode* AstReader::ReadDefaultStatementNode()
67 {
68 Node* node = ReadNode();
69 if (node->GetNodeType() == NodeType::defaultStatementNode)
70 {
71 return static_cast<DefaultStatementNode*>(node);
72 }
73 else
74 {
75 throw std::runtime_error("default statement node expected");
76 }
77 }
78
79 CompoundStatementNode* AstReader::ReadCompoundStatementNode()
80 {
81 Node* node = ReadNode();
82 if (node->GetNodeType() == NodeType::compoundStatementNode)
83 {
84 return static_cast<CompoundStatementNode*>(node);
85 }
86 else
87 {
88 throw std::runtime_error("compound statement node expected");
89 }
90 }
91
92 ConstraintNode* AstReader::ReadConstraintNode()
93 {
94 Node* node = ReadNode();
95 if (node->NodeIsConstraintNode())
96 {
97 return static_cast<ConstraintNode*>(node);
98 }
99 else
100 {
101 throw std::runtime_error("constraint node expected");
102 }
103 }
104
105 WhereConstraintNode* AstReader::ReadWhereConstraintNode()
106 {
107 Node* node = ReadNode();
108 if (node->GetNodeType() == NodeType::whereConstraintNode)
109 {
110 return static_cast<WhereConstraintNode*>(node);
111 }
112 else
113 {
114 throw std::runtime_error("where constraint node expected");
115 }
116 }
117
118 ConceptIdNode* AstReader::ReadConceptIdNode()
119 {
120 Node* node = ReadNode();
121 if (node->GetNodeType() == NodeType::conceptIdNode)
122 {
123 return static_cast<ConceptIdNode*>(node);
124 }
125 else
126 {
127 throw std::runtime_error("concept id node expected");
128 }
129 }
130
131 ConceptNode* AstReader::ReadConceptNode()
132 {
133 Node* node = ReadNode();
134 if (node->IsConceptNode())
135 {
136 return static_cast<ConceptNode*>(node);
137 }
138 else
139 {
140 throw std::runtime_error("concept node expected");
141 }
142 }
143
144 ConditionalCompilationExpressionNode* AstReader::ReadConditionalCompilationExpressionNode()
145 {
146 Node* node = ReadNode();
147 if (node->IsConditionalCompilationExpressionNode())
148 {
149 return static_cast<ConditionalCompilationExpressionNode*>(node);
150 }
151 else
152 {
153 throw std::runtime_error("conditional compilation expression node expected");
154 }
155 }
156
157 ConditionalCompilationPartNode* AstReader::ReadConditionalCompilationPartNode()
158 {
159 Node* node = ReadNode();
160 if (node->GetNodeType() == NodeType::conditionalCompilationPartNode)
161 {
162 return static_cast<ConditionalCompilationPartNode*>(node);
163 }
164 else
165 {
166 throw std::runtime_error("conditional compilation part node expected");
167 }
168 }
169
170 Specifiers AstReader::ReadSpecifiers()
171 {
172 return static_cast<Specifiers>(binaryReader.ReadUInt());
173 }
174
175 Span AstReader::ReadSpan()
176 {
177 bool valid = binaryReader.ReadBool();
178 if (!valid)
179 {
180 return Span();
181 }
182 else
183 {
184 uint32_t fileIndex = binaryReader.ReadULEB128UInt();
185 if (moduleId != -1)
186 {
187 int16_t fileId = GetFileId(static_cast<int32_t>(fileIndex));
188 fileIndex = static_cast<uint32_t>(MakeFileIndex(moduleId, fileId));
189 }
190 uint32_t line = binaryReader.ReadULEB128UInt();
191 uint32_t start = binaryReader.ReadULEB128UInt();
192 uint32_t end = binaryReader.ReadULEB128UInt();
193 return Span(static_cast<int32_t>(fileIndex), static_cast<int32_t>(line), static_cast<int32_t>(start), static_cast<int32_t>(end));
194 }
195 }
196
197 } }