1
2
3
4
5
6 #include <sngcpp/ast/Function.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 ParameterNode::ParameterNode() : Node(NodeType::parameterNode)
14 {
15 }
16
17 ParameterNode::ParameterNode(const Span& span_, Node* typeExpr_, Node* declarator_) : Node(NodeType::parameterNode, span_), typeExpr(typeExpr_), declarator(declarator_)
18 {
19 }
20
21 void ParameterNode::Accept(Visitor& visitor)
22 {
23 visitor.Visit(*this);
24 }
25
26 void ParameterNode::Write(Writer& writer)
27 {
28 Node::Write(writer);
29 typeExpr->Write(writer);
30 writer.GetBinaryWriter().Write(declarator != nullptr);
31 if (declarator)
32 {
33 declarator->Write(writer);
34 }
35 }
36
37 void ParameterNode::Read(Reader& reader)
38 {
39 Node::Read(reader);
40 typeExpr.reset(reader.ReadNode());
41 bool hasDeclarator = reader.GetBinaryReader().ReadBool();
42 if (hasDeclarator)
43 {
44 declarator.reset(reader.ReadNode());
45 }
46 }
47
48 ParameterSequenceNode::ParameterSequenceNode() : BinaryNode(NodeType::parameterSequenceNode)
49 {
50 }
51
52 ParameterSequenceNode::ParameterSequenceNode(const Span& span_, Node* left_, Node* right_) : BinaryNode(NodeType::parameterSequenceNode, span_, left_, right_)
53 {
54 }
55
56 void ParameterSequenceNode::Accept(Visitor& visitor)
57 {
58 visitor.Visit(*this);
59 }
60
61 FunctionNode::FunctionNode() : Node(NodeType::functionNode), specifiers()
62 {
63 }
64
65 FunctionNode::FunctionNode(const Span& span_, Specifier specifiers_, Node* typeExpr_, Node* declarator_, CompoundStatementNode* body_) :
66 Node(NodeType::functionNode, span_), specifiers(specifiers_), typeExpr(typeExpr_), declarator(declarator_), body(body_)
67 {
68 }
69
70 void FunctionNode::Accept(Visitor& visitor)
71 {
72 visitor.Visit(*this);
73 }
74
75 void FunctionNode::Write(Writer& writer)
76 {
77 Node::Write(writer);
78 writer.Write(specifiers);
79 typeExpr->Write(writer);
80 declarator->Write(writer);
81 writer.GetBinaryWriter().Write(body != nullptr);
82 if (body)
83 {
84 body->Write(writer);
85 }
86 }
87
88 void FunctionNode::Read(Reader& reader)
89 {
90 Node::Read(reader);
91 specifiers = reader.ReadSpecifiers();
92 typeExpr.reset(reader.ReadNode());
93 declarator.reset(reader.ReadNode());
94 bool hasBody = reader.GetBinaryReader().ReadBool();
95 if (hasBody)
96 {
97 body.reset(reader.ReadCompoundStatementNode());
98 }
99 }
100
101 bool IsFunctionDeclarator(Node* declaratorNode)
102 {
103 return declaratorNode->IsFunctionDeclaratorNode();
104 }
105
106 bool HasPureSpecifier(Specifier specifiers)
107 {
108 return (specifiers & Specifier::pure_) != Specifier::none;
109 }
110
111 } }