1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <sngcpp/ast/Declaration.hpp>
  7 #include <sngcpp/ast/Visitor.hpp>
  8 #include <sngcpp/ast/Writer.hpp>
  9 #include <sngcpp/ast/Reader.hpp>
 10 
 11 namespace sngcpp { namespace ast {
 12 
 13 SimpleDeclarationNode::SimpleDeclarationNode() : Node(NodeType::simpleDeclarationNode)specifiers()
 14 {
 15 }
 16 
 17 SimpleDeclarationNode::SimpleDeclarationNode(const Span& span_Specifier specifiers_Node* typeExpr_Node* declarator_) :
 18     Node(NodeType::simpleDeclarationNodespan_)specifiers(specifiers_)typeExpr(typeExpr_)declarator(declarator_)
 19 {
 20 }
 21 
 22 void SimpleDeclarationNode::Accept(Visitor& visitor)
 23 {
 24     visitor.Visit(*this);
 25 }
 26 
 27 void SimpleDeclarationNode::Write(Writer& writer)
 28 {
 29     Node::Write(writer);
 30     writer.Write(specifiers);
 31     typeExpr->Write(writer);
 32     bool hasDeclarator = declarator != nullptr;
 33     writer.GetBinaryWriter().Write(hasDeclarator);
 34     if (hasDeclarator)
 35     {
 36         declarator->Write(writer);
 37     }
 38 }
 39 
 40 void SimpleDeclarationNode::Read(Reader& reader)
 41 {
 42     Node::Read(reader);
 43     specifiers = reader.ReadSpecifiers();
 44     typeExpr.reset(reader.ReadNode());
 45     bool hasDeclarator = reader.GetBinaryReader().ReadBool();
 46     if (hasDeclarator)
 47     {
 48         declarator.reset(reader.ReadNode());
 49     }
 50 }
 51 
 52 AliasDeclarationNode::AliasDeclarationNode() : Node(NodeType::aliasDeclarationNode)
 53 {
 54 }
 55 
 56 AliasDeclarationNode::AliasDeclarationNode(const Span& span_const std::u32string& id_Node* typeExpr_) : Node(NodeType::aliasDeclarationNodespan_)id(id_)typeExpr(typeExpr_)
 57 {
 58 }
 59 
 60 void AliasDeclarationNode::Accept(Visitor& visitor)
 61 {
 62     visitor.Visit(*this);
 63 }
 64 
 65 void AliasDeclarationNode::Write(Writer& writer)
 66 {
 67     Node::Write(writer);
 68     writer.GetBinaryWriter().Write(id);
 69     typeExpr->Write(writer);
 70 }
 71 
 72 void AliasDeclarationNode::Read(Reader& reader)
 73 {
 74     Node::Read(reader);
 75     id = reader.GetBinaryReader().ReadUtf32String();
 76     typeExpr.reset(reader.ReadNode());
 77 }
 78 
 79 UsingDirectiveNode::UsingDirectiveNode() : Node(NodeType::usingDirectiveNode)
 80 {
 81 }
 82 
 83 UsingDirectiveNode::UsingDirectiveNode(const Span& span_Node* namespaceName_) : Node(NodeType::usingDirectiveNodespan_)namespaceName(namespaceName_)
 84 {
 85 }
 86 
 87 void UsingDirectiveNode::Accept(Visitor& visitor)
 88 {
 89     visitor.Visit(*this);
 90 }
 91 
 92 void UsingDirectiveNode::Write(Writer& writer)
 93 {
 94     Node::Write(writer);
 95     namespaceName->Write(writer);
 96 }
 97 
 98 void UsingDirectiveNode::Read(Reader& reader)
 99 {
100     Node::Read(reader);
101     namespaceName.reset(reader.ReadNode());
102 }
103 
104 UsingDeclarationNode::UsingDeclarationNode() : Node(NodeType::usingDeclarationNode)
105 {
106 }
107 
108 UsingDeclarationNode::UsingDeclarationNode(const Span& span_Node* qualifiedId_) : Node(NodeType::usingDeclarationNodespan_)qualifiedId(qualifiedId_)
109 {
110 }
111 
112 void UsingDeclarationNode::Accept(Visitor& visitor)
113 {
114     visitor.Visit(*this);
115 }
116 
117 void UsingDeclarationNode::Write(Writer& writer)
118 {
119     Node::Write(writer);
120     qualifiedId->Write(writer);
121 }
122 
123 void UsingDeclarationNode::Read(Reader& reader)
124 {
125     Node::Read(reader);
126     qualifiedId.reset(reader.ReadNode());
127 }
128 
129 TypedefNode::TypedefNode() : Node(NodeType::typedefNode)
130 {
131 }
132 
133 TypedefNode::TypedefNode(const Span& span_Node* typeExpr_Node* declarator_) : Node(NodeType::typedefNodespan_)typeExpr(typeExpr_)declarator(declarator_)
134 {
135 }
136 
137 void TypedefNode::Accept(Visitor& visitor)
138 {
139     visitor.Visit(*this);
140 }
141 
142 void TypedefNode::Write(Writer& writer)
143 {
144     Node::Write(writer);
145     typeExpr->Write(writer);
146     declarator->Write(writer);
147 }
148 
149 void TypedefNode::Read(Reader& reader)
150 {
151     Node::Read(reader);
152     typeExpr.reset(reader.ReadNode());
153     declarator.reset(reader.ReadNode());
154 }
155 
156 DeclarationSequenceNode::DeclarationSequenceNode() : BinaryNode(NodeType::declarationSequenceNode)
157 {
158 }
159 
160 DeclarationSequenceNode::DeclarationSequenceNode(const Span& span_Node* left_Node* right_) : BinaryNode(NodeType::declarationSequenceNodespan_left_right_)
161 {
162 }
163 
164 void DeclarationSequenceNode::Accept(Visitor& visitor)
165 {
166     visitor.Visit(*this);
167 }
168 
169 LinkageSpecificationNode::LinkageSpecificationNode() : Node(NodeType::linkageSpecificationNode)
170 {
171 }
172 
173 LinkageSpecificationNode::LinkageSpecificationNode(const Span& span_StringLiteralNode* language_) : Node(NodeType::linkageSpecificationNodespan_)language(language_)
174 {
175 }
176 
177 void LinkageSpecificationNode::Accept(Visitor& visitor)
178 {
179     visitor.Visit(*this);
180 }
181 
182 void LinkageSpecificationNode::Write(Writer& writer)
183 {
184     Node::Write(writer);
185     language->Write(writer);
186     writer.GetBinaryWriter().Write(declarations != nullptr);
187     if (declarations)
188     {
189         declarations->Write(writer);
190     }
191 }
192 
193 void LinkageSpecificationNode::Read(Reader& reader)
194 {
195     Node::Read(reader);
196     language.reset(reader.ReadStringLiteralNode());
197     bool hasDeclarations = reader.GetBinaryReader().ReadBool();
198     if (hasDeclarations)
199     {
200         declarations.reset(reader.ReadNode());
201     }
202 }
203 
204 void LinkageSpecificationNode::AddDeclaration(const Span& span_Node* declaration)
205 {
206     if (declarations)
207     {
208         declarations.reset(new DeclarationSequenceNode(span_declarations.release()declaration));
209     }
210     else
211     {
212         declarations.reset(declaration);
213     }
214 }
215 
216 } } // namespace sngcpp::ast