1
2
3
4
5
6 #include <sngcm/ast/GlobalVariable.hpp>
7 #include <sngcm/ast/Identifier.hpp>
8 #include <sngcm/ast/Visitor.hpp>
9 #include <sngcm/ast/AstWriter.hpp>
10 #include <sngcm/ast/AstReader.hpp>
11
12 namespace sngcm { namespace ast {
13
14 GlobalVariableNode::GlobalVariableNode(const Span& span_) : Node(NodeType::globalVariableNode, span_), specifiers(Specifiers::none), cu(nullptr)
15 {
16 }
17
18 GlobalVariableNode::GlobalVariableNode(const Span& span_, Specifiers specifiers_, Node* typeExpr_, IdentifierNode* id_, CompileUnitNode* cu_) :
19 Node(NodeType::globalVariableNode, span_), specifiers(specifiers_), typeExpr(typeExpr_), id(id_), cu(cu_)
20 {
21 typeExpr->SetParent(this);
22 id->SetParent(this);
23 }
24
25 Node* GlobalVariableNode::Clone(CloneContext& cloneContext) const
26 {
27 GlobalVariableNode* clone = new GlobalVariableNode(GetSpan(), specifiers, typeExpr->Clone(cloneContext), static_cast<IdentifierNode*>(id->Clone(cloneContext)), cu);
28 if (initializer)
29 {
30 clone->SetInitializer(initializer->Clone(cloneContext));
31 }
32 return clone;
33 }
34
35 void GlobalVariableNode::Accept(Visitor& visitor)
36 {
37 visitor.Visit(*this);
38 }
39
40 void GlobalVariableNode::Write(AstWriter& writer)
41 {
42 Node::Write(writer);
43 writer.Write(specifiers);
44 writer.Write(typeExpr.get());
45 writer.Write(id.get());
46 bool hasInitializer = initializer != nullptr;
47 writer.GetBinaryWriter().Write(hasInitializer);
48 if (hasInitializer)
49 {
50 writer.Write(initializer.get());
51 }
52
53 }
54 void GlobalVariableNode::Read(AstReader& reader)
55 {
56 Node::Read(reader);
57 specifiers = reader.ReadSpecifiers();
58 typeExpr.reset(reader.ReadNode());
59 typeExpr->SetParent(this);
60 id.reset(reader.ReadIdentifierNode());
61 id->SetParent(this);
62 bool hasInitializer = reader.GetBinaryReader().ReadBool();
63 if (hasInitializer)
64 {
65 initializer.reset(reader.ReadNode());
66 initializer->SetParent(this);
67 }
68 }
69
70 void GlobalVariableNode::SetInitializer(Node* initializer_)
71 {
72 initializer.reset(initializer_);
73 if (initializer)
74 {
75 initializer->SetParent(this);
76 }
77 }
78
79 } }