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_, const boost::uuids::uuid& moduleId_) : Node(NodeType::constantNode, span_, moduleId_), specifiers(Specifiers::none)
15 {
16 }
17
18 ConstantNode::ConstantNode(const Span& span_, const boost::uuids::uuid& moduleId_, Specifiers specifiers_, Node* typeExpr_, IdentifierNode* id_, Node* value_) :
19 Node(NodeType::constantNode, span_, moduleId_), 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 ConstantNode* clone = new ConstantNode(GetSpan(), ModuleId(), specifiers, typeExpr->Clone(cloneContext), static_cast<IdentifierNode*>(id->Clone(cloneContext)), clonedValue);
37 return clone;
38 }
39
40 void ConstantNode::Accept(Visitor& visitor)
41 {
42 visitor.Visit(*this);
43 }
44
45 void ConstantNode::Write(AstWriter& writer)
46 {
47 Node::Write(writer);
48 writer.Write(specifiers);
49 writer.Write(typeExpr.get());
50 writer.Write(id.get());
51 bool hasValue = value != nullptr;
52 writer.GetBinaryWriter().Write(hasValue);
53 if (hasValue)
54 {
55 writer.Write(value.get());
56 }
57 writer.GetBinaryWriter().Write(strValue);
58 }
59
60 void ConstantNode::Read(AstReader& reader)
61 {
62 Node::Read(reader);
63 specifiers = reader.ReadSpecifiers();
64 typeExpr.reset(reader.ReadNode());
65 typeExpr->SetParent(this);
66 id.reset(reader.ReadIdentifierNode());
67 id->SetParent(this);
68 bool hasValue = reader.GetBinaryReader().ReadBool();
69 if (hasValue)
70 {
71 value.reset(reader.ReadNode());
72 value->SetParent(this);
73 }
74 strValue = reader.GetBinaryReader().ReadUtf32String();
75 }
76
77 } }