1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <sngcpp/ast/Class.hpp>
  7 #include <sngcpp/ast/Declaration.hpp>
  8 #include <sngcpp/ast/Visitor.hpp>
  9 #include <sngcpp/ast/Writer.hpp>
 10 #include <sngcpp/ast/Reader.hpp>
 11 
 12 namespace sngcpp { namespace ast {
 13 
 14 std::u32string ToString(ClassKey classKey)
 15 {
 16     switch (classKey)
 17     {
 18         case ClassKey::class_: return U"class";
 19         case ClassKey::struct_: return U"struct";
 20         case ClassKey::union_: return U"union";
 21         case ClassKey::typename_: return U"typename";
 22     }
 23     return std::u32string();
 24 }
 25 
 26 BaseClassSpecifierNode::BaseClassSpecifierNode() : Node(NodeType::baseClassSpecifierNode)
 27 {
 28 }
 29 
 30 BaseClassSpecifierNode::BaseClassSpecifierNode(const Span& span_Specifier baseSpecifiers_Node* className_) :
 31     Node(NodeType::baseClassSpecifierNodespan_)baseSpecifiers(baseSpecifiers_)className(className_)
 32 {
 33 }
 34 
 35 void BaseClassSpecifierNode::Accept(Visitor& visitor)
 36 {
 37     visitor.Visit(*this);
 38 }
 39 
 40 void BaseClassSpecifierNode::Write(Writer& writer)
 41 {
 42     Node::Write(writer);
 43     writer.Write(baseSpecifiers);
 44     className->Write(writer);
 45 }
 46 
 47 void BaseClassSpecifierNode::Read(Reader& reader)
 48 {
 49     Node::Read(reader);
 50     baseSpecifiers = reader.ReadSpecifiers();
 51     className.reset(reader.ReadNode());
 52 }
 53 
 54 BaseClassSpecifierSequenceNode::BaseClassSpecifierSequenceNode() : BinaryNode(NodeType::baseClassSpecifierSequenceNode)
 55 {
 56 }
 57 
 58 BaseClassSpecifierSequenceNode::BaseClassSpecifierSequenceNode(const Span& span_Node* left_Node* right_) : BinaryNode(NodeType::baseClassSpecifierSequenceNodespan_left_right_)
 59 {
 60 }
 61 
 62 void BaseClassSpecifierSequenceNode::Accept(Visitor& visitor)
 63 {
 64     visitor.Visit(*this);
 65 }
 66 
 67 ForwardClassDeclarationNode::ForwardClassDeclarationNode() : Node(NodeType::forwardClassDeclarationNode)
 68 {
 69 }
 70 
 71 ForwardClassDeclarationNode::ForwardClassDeclarationNode(const Span& span_ClassKey classKey_Node* className_) : Node(NodeType::forwardClassDeclarationNodespan_)classKey(classKey_)className(className_)
 72 {
 73 }
 74 
 75 void ForwardClassDeclarationNode::Accept(Visitor& visitor)
 76 {
 77     visitor.Visit(*this);
 78 }
 79 
 80 void ForwardClassDeclarationNode::Write(Writer& writer)
 81 {
 82     Node::Write(writer);
 83     writer.Write(classKey);
 84     className->Write(writer);
 85 }
 86 
 87 void ForwardClassDeclarationNode::Read(Reader& reader)
 88 {
 89     Node::Read(reader);
 90     classKey = reader.ReadClassKey();
 91     className.reset(reader.ReadNode());
 92 }
 93 
 94 ElaborateClassNameNode::ElaborateClassNameNode() : Node(NodeType::elaborateClassNameNode)
 95 {
 96 }
 97 
 98 ElaborateClassNameNode::ElaborateClassNameNode(const Span& span_ClassKey classKey_Node* className_) : Node(NodeType::elaborateClassNameNodespan_)classKey(classKey_)className(className_)
 99 {
100 }
101 
102 void ElaborateClassNameNode::Accept(Visitor& visitor)
103 {
104     visitor.Visit(*this);
105 }
106 
107 void ElaborateClassNameNode::Write(Writer& writer)
108 {
109     Node::Write(writer);
110     writer.Write(classKey);
111     className->Write(writer);
112 }
113 
114 void ElaborateClassNameNode::Read(Reader& reader)
115 {
116     Node::Read(reader);
117     classKey = reader.ReadClassKey();
118     className.reset(reader.ReadNode());
119 }
120 
121 ClassNode::ClassNode() : Node(NodeType::classNode)classKey()classVirtSpecifiers()
122 {
123 }
124 
125 ClassNode::ClassNode(const Span& span_ClassKey classKey_Node* className_Specifier classVirtSpecifiers_Node* baseClasses_) :
126     Node(NodeType::classNodespan_)classKey(classKey_)className(className_)classVirtSpecifiers(classVirtSpecifiers_)baseClasses(baseClasses_)
127 {
128 }
129 
130 void ClassNode::Accept(Visitor& visitor)
131 {
132     visitor.Visit(*this);
133 }
134 
135 void ClassNode::Write(Writer& writer)
136 {
137     Node::Write(writer);
138     writer.Write(classKey);
139     className->Write(writer);
140     writer.Write(classVirtSpecifiers);
141     writer.GetBinaryWriter().Write(baseClasses != nullptr);
142     if (baseClasses)
143     {
144         baseClasses->Write(writer);
145     }
146     writer.GetBinaryWriter().Write(declarations != nullptr);
147     if (declarations)
148     {
149         declarations->Write(writer);
150     }
151     writer.Write(openBraceSpan);
152     writer.Write(closeBraceSpan);
153 }
154 
155 void ClassNode::Read(Reader& reader)
156 {
157     Node::Read(reader);
158     classKey = reader.ReadClassKey();
159     className.reset(reader.ReadNode());
160     classVirtSpecifiers = reader.ReadSpecifiers();
161     bool hasBaseClasses = reader.GetBinaryReader().ReadBool();
162     if (hasBaseClasses)
163     {
164         baseClasses.reset(reader.ReadNode());
165     }
166     bool hasDeclarations = reader.GetBinaryReader().ReadBool();
167     if (hasDeclarations)
168     {
169         declarations.reset(reader.ReadNode());
170     }
171     reader.Read(openBraceSpan);
172     reader.Read(closeBraceSpan);
173 }
174 
175 Specifier ClassNode::DefaultAccess() const
176 {
177     if (classKey == ClassKey::class_)
178     {
179         return Specifier::private_;
180     }
181     else
182     {
183         return Specifier::public_;
184     }
185 }
186 
187 void ClassNode::AddDeclaration(const Span& spanNode* declaration)
188 {
189     if (declarations)
190     {
191         declarations.reset(new DeclarationSequenceNode(spandeclarations.release()declaration));
192     }
193     else
194     {
195         declarations.reset(declaration);
196     }
197 }
198 
199 MemberAccessDeclarationNode::MemberAccessDeclarationNode() : Node(NodeType::memberAccessDeclarationNode)accessSpecifier()explicitAccess()
200 {
201 }
202 
203 MemberAccessDeclarationNode::MemberAccessDeclarationNode(const Span& span_Specifier accessSpecifier_Node* declaration_bool explicitAccess_) :
204     Node(NodeType::memberAccessDeclarationNodespan_)accessSpecifier(accessSpecifier_)declaration(declaration_)explicitAccess(explicitAccess_)
205 {
206 }
207 
208 void MemberAccessDeclarationNode::Accept(Visitor& visitor)
209 {
210     visitor.Visit(*this);
211 }
212 
213 void MemberAccessDeclarationNode::Write(Writer& writer)
214 {
215     Node::Write(writer);
216     writer.Write(accessSpecifier);
217     declaration->Write(writer);
218     writer.GetBinaryWriter().Write(explicitAccess);
219 }
220 
221 void MemberAccessDeclarationNode::Read(Reader& reader)
222 {
223     Node::Read(reader);
224     accessSpecifier = reader.ReadSpecifiers();
225     declaration.reset(reader.ReadNode());
226     explicitAccess = reader.GetBinaryReader().ReadBool();
227 }
228 
229 MemberDeclarationNode::MemberDeclarationNode() : Node(NodeType::memberDeclarationNode)specifiers()
230 {
231 }
232 
233 MemberDeclarationNode::MemberDeclarationNode(const Span& span_Specifier specifiers_Node* typeExpr_Node* declarator_Node* initializer_) :
234     Node(NodeType::memberDeclarationNodespan_)specifiers(specifiers_)typeExpr(typeExpr_)declarator(declarator_)initializer(initializer_)
235 {
236 }
237 
238 void MemberDeclarationNode::Accept(Visitor& visitor)
239 {
240     visitor.Visit(*this);
241 }
242 
243 void MemberDeclarationNode::Write(Writer& writer)
244 {
245     Node::Write(writer);
246     writer.Write(specifiers);
247     writer.GetBinaryWriter().Write(typeExpr != nullptr);
248     if (typeExpr)
249     {
250         typeExpr->Write(writer);
251     }
252     declarator->Write(writer);
253     writer.GetBinaryWriter().Write(initializer != nullptr);
254     if (initializer)
255     {
256         initializer->Write(writer);
257     }
258 }
259 
260 void MemberDeclarationNode::Read(Reader& reader)
261 {
262     Node::Read(reader);
263     specifiers = reader.ReadSpecifiers();
264     bool hasTypeExpr = reader.GetBinaryReader().ReadBool();
265     if (hasTypeExpr)
266     {
267         typeExpr.reset(reader.ReadNode());
268     }
269     declarator.reset(reader.ReadNode());
270     bool hasInitializer = reader.GetBinaryReader().ReadBool();
271     if (hasInitializer)
272     {
273         initializer.reset(reader.ReadNode());
274     }
275 }
276 
277 SpecialMemberFunctionNode::SpecialMemberFunctionNode() : Node(NodeType::specialMemberFunctionNode)specifiers()
278 {
279 }
280 
281 SpecialMemberFunctionNode::SpecialMemberFunctionNode(const Span& span_Specifier specifiers_Node* declarator_Node* ctorInitializer_CompoundStatementNode* functionBody_) :
282     Node(NodeType::specialMemberFunctionNodespan_)specifiers(specifiers_)declarator(declarator_)ctorInitializer(ctorInitializer_)functionBody(functionBody_)
283 {
284 }
285 
286 void SpecialMemberFunctionNode::Accept(Visitor& visitor)
287 {
288     visitor.Visit(*this);
289 }
290 
291 void SpecialMemberFunctionNode::Write(Writer& writer)
292 {
293     Node::Write(writer);
294     writer.Write(specifiers);
295     declarator->Write(writer);
296     writer.GetBinaryWriter().Write(ctorInitializer != nullptr);
297     if (ctorInitializer)
298     {
299         ctorInitializer->Write(writer);
300     }
301     writer.GetBinaryWriter().Write(functionBody != nullptr);
302     if (functionBody)
303     {
304         functionBody->Write(writer);
305     }
306 }
307 
308 void SpecialMemberFunctionNode::Read(Reader& reader)
309 {
310     Node::Read(reader);
311     specifiers = reader.ReadSpecifiers();
312     declarator.reset(reader.ReadNode());
313     bool hasCtorInitializer = reader.GetBinaryReader().ReadBool();
314     if (hasCtorInitializer)
315     {
316         ctorInitializer.reset(reader.ReadNode());
317     }
318     bool hasFunctionBody = reader.GetBinaryReader().ReadBool();
319     if (hasFunctionBody)
320     {
321         functionBody.reset(reader.ReadCompoundStatementNode());
322     }
323 }
324 
325 CtorInitializerNode::CtorInitializerNode() : UnaryNode(NodeType::ctorInitializerNode)
326 {
327 }
328 
329 CtorInitializerNode::CtorInitializerNode(const Span& span_Node* memberInitializers_) : UnaryNode(NodeType::ctorInitializerNodespan_memberInitializers_)
330 {
331 }
332 
333 void CtorInitializerNode::Accept(Visitor& visitor)
334 {
335     visitor.Visit(*this);
336 }
337 
338 MemberInitializerNode::MemberInitializerNode() : Node(NodeType::memberInitializerNode)
339 {
340 }
341 
342 MemberInitializerNode::MemberInitializerNode(const Span& span_Node* id_Node* initializer_) : Node(NodeType::memberInitializerNodespan_)id(id_)initializer(initializer_)
343 {
344 }
345 
346 void MemberInitializerNode::Accept(Visitor& visitor)
347 {
348     visitor.Visit(*this);
349 }
350 
351 void MemberInitializerNode::Write(Writer& writer)
352 {
353     Node::Write(writer);
354     id->Write(writer);
355     writer.GetBinaryWriter().Write(initializer != nullptr);
356     if (initializer)
357     {
358         initializer->Write(writer);
359     }
360 }
361 
362 void MemberInitializerNode::Read(Reader& reader)
363 {
364     Node::Read(reader);
365     id.reset(reader.ReadNode());
366     bool hasInitializer = reader.GetBinaryReader().ReadBool();
367     if (hasInitializer)
368     {
369         initializer.reset(reader.ReadNode());
370     }
371 }
372 
373 MemberInitializerSequenceNode::MemberInitializerSequenceNode() : BinaryNode(NodeType::memberInitializerSequenceNode)
374 {
375 }
376 
377 MemberInitializerSequenceNode::MemberInitializerSequenceNode(const Span& span_Node* left_Node* right_) : BinaryNode(NodeType::memberInitializerSequenceNodespan_left_right_)
378 {
379 }
380 
381 void MemberInitializerSequenceNode::Accept(Visitor& visitor)
382 {
383     visitor.Visit(*this);
384 }
385 
386 } } // namespace sngcpp::ast