1
2
3
4
5
6 #include <sngcm/ast/Constant.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 ConstantNode::ConstantNode(const Span& span_) : Node(NodeType::constantNode, span_), specifiers(Specifiers::none)
15 {
16 }
17
18 ConstantNode::ConstantNode(const Span& span_, Specifiers specifiers_, Node* typeExpr_, IdentifierNode* id_, Node* value_) :
19 Node(NodeType::constantNode, span_), specifiers(specifiers_), typeExpr(typeExpr_), id(id_), value(value_)
20 {
21 typeExpr->SetParent(this);
22 id->SetParent(this);
23 if (value)
24 {
25 value->SetParent(this);
26 }
27 }
28
29 Node* ConstantNode::Clone(CloneContext& cloneContext) const
30 {
31 Node* clonedValue = nullptr;
32 if (value)
33 {
34 clonedValue = value->Clone(cloneContext);
35 }
36 return new ConstantNode(GetSpan(), specifiers, typeExpr->Clone(cloneContext), static_cast<IdentifierNode*>(id->Clone(cloneContext)), clonedValue);
37 }
38
39 void ConstantNode::Accept(Visitor& visitor)
40 {
41 visitor.Visit(*this);
42 }
43
44 void ConstantNode::Write(AstWriter& writer)
45 {
46 Node::Write(writer);
47 writer.Write(specifiers);
48 writer.Write(typeExpr.get());
49 writer.Write(id.get());
50 bool hasValue = value != nullptr;
51 writer.GetBinaryWriter().Write(hasValue);
52 if (hasValue)
53 {
54 writer.Write(value.get());
55 }
56 writer.GetBinaryWriter().Write(strValue);
57 }
58
59 void ConstantNode::Read(AstReader& reader)
60 {
61 Node::Read(reader);
62 specifiers = reader.ReadSpecifiers();
63 typeExpr.reset(reader.ReadNode());
64 typeExpr->SetParent(this);
65 id.reset(reader.ReadIdentifierNode());
66 id->SetParent(this);
67 bool hasValue = reader.GetBinaryReader().ReadBool();
68 if (hasValue)
69 {
70 value.reset(reader.ReadNode());
71 value->SetParent(this);
72 }
73 strValue = reader.GetBinaryReader().ReadUtf32String();
74 }
75
76 } }