1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <sngcpp/ast/Declarator.hpp>
  7 #include <sngcpp/ast/Expression.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 IdDeclaratorNode::IdDeclaratorNode() : Node(NodeType::idDeclaratorNode)
 15 {
 16 }
 17 
 18 IdDeclaratorNode::IdDeclaratorNode(const Span& span_Node* idNode_) : Node(NodeType::idDeclaratorNodespan_)idNode(idNode_)
 19 {
 20 }
 21 
 22 void IdDeclaratorNode::Accept(Visitor& visitor)
 23 {
 24     visitor.Visit(*this);
 25 }
 26 
 27 void IdDeclaratorNode::Write(Writer& writer)
 28 {
 29     Node::Write(writer);
 30     idNode->Write(writer);
 31 }
 32 
 33 void IdDeclaratorNode::Read(Reader& reader)
 34 {
 35     Node::Read(reader);
 36     idNode.reset(reader.ReadNode());
 37 }
 38 
 39 ArrayDeclaratorNode::ArrayDeclaratorNode() : Node(NodeType::arrayDeclaratorNode)
 40 {
 41 }
 42 
 43 ArrayDeclaratorNode::ArrayDeclaratorNode(const Span& span_Node* declarator_Node* size_) : Node(NodeType::arrayDeclaratorNodespan_)declarator(declarator_)size(size_)
 44 {
 45 }
 46 
 47 void ArrayDeclaratorNode::Accept(Visitor& visitor)
 48 {
 49     visitor.Visit(*this);
 50 }
 51 
 52 void ArrayDeclaratorNode::Write(Writer& writer)
 53 {
 54     Node::Write(writer);
 55     declarator->Write(writer);
 56     writer.GetBinaryWriter().Write(size != nullptr);
 57     if (size)
 58     {
 59         size->Write(writer);
 60     }
 61 }
 62 
 63 void ArrayDeclaratorNode::Read(Reader& reader)
 64 {
 65     Node::Read(reader);
 66     declarator.reset(reader.ReadNode());
 67     bool hasSize = reader.GetBinaryReader().ReadBool();
 68     if (hasSize)
 69     {
 70         size.reset(reader.ReadNode());
 71     }
 72 }
 73 
 74 FunctionDeclaratorNode::FunctionDeclaratorNode() : Node(NodeType::functionDeclaratorNode)
 75 {
 76 }
 77 
 78 FunctionDeclaratorNode::FunctionDeclaratorNode(const Span& span_Node* declarator_Node* parameters_Specifier cvSpecifiers_) :
 79     Node(NodeType::functionDeclaratorNodespan_)declarator(declarator_)parameters(parameters_)cvSpecifiers(cvSpecifiers_)
 80 {
 81 }
 82 
 83 void FunctionDeclaratorNode::Accept(Visitor& visitor)
 84 {
 85     visitor.Visit(*this);
 86 }
 87 
 88 void FunctionDeclaratorNode::Write(Writer& writer)
 89 {
 90     Node::Write(writer);
 91     declarator->Write(writer);
 92     writer.GetBinaryWriter().Write(parameters != nullptr);
 93     if (parameters)
 94     {
 95         parameters->Write(writer);
 96     }
 97     writer.Write(cvSpecifiers);
 98 }
 99 
100 void FunctionDeclaratorNode::Read(Reader& reader)
101 {
102     Node::Read(reader);
103     declarator.reset(reader.ReadNode());
104     bool hasParameters = reader.GetBinaryReader().ReadBool();
105     if (hasParameters)
106     {
107         parameters.reset(reader.ReadNode());
108     }
109     cvSpecifiers = reader.ReadSpecifiers();
110 }
111 
112 FunctionPtrIdNode::FunctionPtrIdNode() : Node(NodeType::functionPtrIdNode)
113 {
114 }
115 
116 FunctionPtrIdNode::FunctionPtrIdNode(const Span& span_const std::u32string& id_) : Node(NodeType::functionPtrIdNodespan_)id(id_)
117 {
118 }
119 
120 void FunctionPtrIdNode::Accept(Visitor& visitor)
121 {
122     visitor.Visit(*this);
123 }
124 
125 void FunctionPtrIdNode::Write(Writer& writer)
126 {
127     Node::Write(writer);
128     writer.GetBinaryWriter().Write(id);
129 }
130 
131 void FunctionPtrIdNode::Read(Reader& reader)
132 {
133     Node::Read(reader);
134     id = reader.GetBinaryReader().ReadUtf32String();
135 }
136 
137 MemberFunctionPtrIdNode::MemberFunctionPtrIdNode() : Node(NodeType::memberFunctionPtrIdNode)
138 {
139 }
140 
141 MemberFunctionPtrIdNode::MemberFunctionPtrIdNode(const Span& span_Node* classNameNode_const std::u32string& id_) :
142     Node(NodeType::memberFunctionPtrIdNodespan_)classNameNode(classNameNode_)id(id_)
143 {
144 }
145 
146 void MemberFunctionPtrIdNode::Accept(Visitor& visitor)
147 {
148     visitor.Visit(*this);
149 }
150 
151 void MemberFunctionPtrIdNode::Write(Writer& writer)
152 {
153     Node::Write(writer);
154     classNameNode->Write(writer);
155     writer.GetBinaryWriter().Write(id);
156 }
157 
158 void MemberFunctionPtrIdNode::Read(Reader& reader)
159 {
160     Node::Read(reader);
161     classNameNode.reset(reader.ReadNode());
162     id = reader.GetBinaryReader().ReadUtf32String();
163 }
164 
165 InitDeclaratorNode::InitDeclaratorNode() : Node(NodeType::initDeclaratorNode)
166 {
167 }
168 
169 InitDeclaratorNode::InitDeclaratorNode(const Span& span_Node* declarator_Node* initializer_) : Node(NodeType::initDeclaratorNodespan_)declarator(declarator_)initializer(initializer_)
170 {
171 }
172 
173 void InitDeclaratorNode::Accept(Visitor& visitor)
174 {
175     visitor.Visit(*this);
176 }
177 
178 void InitDeclaratorNode::Write(Writer& writer)
179 {
180     Node::Write(writer);
181     declarator->Write(writer);
182     writer.GetBinaryWriter().Write(initializer != nullptr);
183     if (initializer)
184     {
185         initializer->Write(writer);
186     }
187 }
188 
189 void InitDeclaratorNode::Read(Reader& reader)
190 {
191     Node::Read(reader);
192     declarator.reset(reader.ReadNode());
193     bool hasInitializer = reader.GetBinaryReader().ReadBool();
194     if (hasInitializer)
195     {
196         initializer.reset(reader.ReadNode());
197     }
198 }
199 
200 AssignmentInitializerNode::AssignmentInitializerNode() : UnaryNode(NodeType::assignmentInitializerNode)
201 {
202 }
203 
204 AssignmentInitializerNode::AssignmentInitializerNode(const Span& span_Node* initializer_) : UnaryNode(NodeType::assignmentInitializerNodespan_initializer_)
205 {
206 }
207 
208 void AssignmentInitializerNode::Accept(Visitor& visitor)
209 {
210     visitor.Visit(*this);
211 }
212 
213 ExpressionListInitializerNode::ExpressionListInitializerNode() : UnaryNode(NodeType::expressionListInitializerNode)
214 {
215 }
216 
217 ExpressionListInitializerNode::ExpressionListInitializerNode(const Span& span_Node* initializer_) : UnaryNode(NodeType::expressionListInitializerNodespan_initializer_)
218 {
219 }
220 
221 void ExpressionListInitializerNode::Accept(Visitor& visitor)
222 {
223     visitor.Visit(*this);
224 }
225 
226 ExpressionInitializerNode::ExpressionInitializerNode() : UnaryNode(NodeType::expressionInitializerNode)
227 {
228 }
229 
230 ExpressionInitializerNode::ExpressionInitializerNode(const Span& span_Node* expr_) : UnaryNode(NodeType::expressionInitializerNodespan_expr_)
231 {
232 }
233 
234 void ExpressionInitializerNode::Accept(Visitor& visitor)
235 {
236     visitor.Visit(*this);
237 }
238 
239 BracedInitializerListNode::BracedInitializerListNode() : UnaryNode(NodeType::bracedInitializerListNode)
240 {
241 }
242 
243 BracedInitializerListNode::BracedInitializerListNode(const Span& span_) : UnaryNode(NodeType::bracedInitializerListNodespan_)
244 {
245 }
246 
247 void BracedInitializerListNode::Accept(Visitor& visitor)
248 {
249     visitor.Visit(*this);
250 }
251 
252 void BracedInitializerListNode::AddInitializer(const Span& spanNode* initializer)
253 {
254     if (Child())
255     {
256         SetChild(new ExpressionSequenceNode(spanReleaseChild()initializer));
257     }
258     else
259     {
260         SetChild(initializer);
261     }
262 }
263 
264 void BracedInitializerListNode::Write(Writer& writer)
265 {
266     UnaryNode::Write(writer);
267     writer.Write(endBraceSpan);
268 }
269 
270 void BracedInitializerListNode::Read(Reader& reader)
271 {
272     UnaryNode::Read(reader);
273     reader.Read(endBraceSpan);
274 }
275 
276 } } // namespace sngcpp::ast