1
2
3
4
5
6 #ifndef SNGCPP_AST_NODE_INCLUDED
7 #define SNGCPP_AST_NODE_INCLUDED
8 #include <sngcpp/ast/AstApi.hpp>
9 #include <soulng/lexer/Span.hpp>
10 #include <memory>
11 #include <vector>
12
13 namespace sngcpp { namespace ast {
14
15 using soulng::lexer::Span;
16
17 class Visitor;
18 class Writer;
19 class Reader;
20
21 enum class NodeType
22 {
23 baseClassSpecifierNode, baseClassSpecifierSequenceNode, forwardClassDeclarationNode, elaborateClassNameNode, classNode, memberAccessDeclarationNode, memberDeclarationNode,
24 specialMemberFunctionNode, ctorInitializerNode, memberInitializerNode, memberInitializerSequenceNode, simpleDeclarationNode, aliasDeclarationNode, usingDirectiveNode,
25 usingDeclarationNode, typedefNode, declarationSequenceNode, linkageSpecificationNode, idDeclaratorNode, arrayDeclaratorNode, functionDeclaratorNode, functionPtrIdNode,
26 memberFunctionPtrIdNode, initDeclaratorNode, assignmentInitializerNode, expressionListInitializerNode, expressionInitializerNode, bracedInitializerListNode,
27 enumTypeNode, enumeratorNode, enumeratorSequenceNode, expressionSequenceNode, commaExpressionNode, assignmentExpressionNode, conditionalExpressionNode,
28 throwExpressionNode, logicalOrExpressionNode, logicalAndExpressionNode, inclusiveOrExpressionNode, exclusiveOrExpressionNode, andExpressionNode, equalityExpressionNode,
29 relationalExpressionNode, shiftExpressionNode, additiveExpressionNode, multiplicativeExpressionNode, pmExpressionNode, castExpressionNode, unaryExpressionNode,
30 newExpressionNode, deleteExpressionNode, subscriptExpressionNode, invokeExpressionNode, dotNode, arrowNode, postfixIncNode, postfixDecNode, cppCastExpressionNode,
31 typeIdExpressionNode, thisNode, identifierNode, operatorFunctionIdNode, conversionFunctionIdNode, dtorIdNode, nestedIdNode, parenthesizedExprNode, lambdaExpressionNode,
32 captureSequenceNode, assignCaptureNode, refCaptureNode, thisCaptureNode, identifierCaptureNode, parameterNode, parameterSequenceNode, functionNode, floatingLiteralNode, integerLiteralNode,
33 characterLiteralNode, stringLiteralNode, booleanLiteralNode, nullPtrLiteralNode, namespaceNode, simpleTypeNode, sourceFileNode, sourceFileSequenceNode, labeledStatementNode,
34 caseStatementNode, defaultStatementNode, expressionStatementNode, compoundStatementNode, statementSequenceNode, ifStatementNode, switchStatementNode, whileStatementNode,
35 doStatementNode, rangeForStatementNode, forStatementNode, breakStatementNode, continueStatementNode, returnStatementNode, gotoStatementNode, declarationStatementNode,
36 tryStatementNode, handlerNode, handlerSequenceNode, catchAllNode, typeParameterNode, templateParameterSequenceNode, templateDeclarationNode, templateArgumentSequenceNode,
37 templateIdNode, templateArgumentNode, explicitInstantiationNode, explicitSpecializationNode, constNode, volatileNode, pointerNode, rValueRefNode, lValueRefNode,
38 max
39 };
40
41 class Node;
42
43 class NodeCreator
44 {
45 public:
46 virtual ~NodeCreator();
47 virtual Node* Create() = 0;
48 };
49
50 class Node
51 {
52 public:
53 Node(NodeType nodeType_);
54 Node(NodeType nodeType_, const Span& span_);
55 NodeType GetNodeType() const { return nodeType; }
56 virtual ~Node();
57 virtual void Accept(Visitor& visitor) = 0;
58 virtual void Write(Writer& writer);
59 virtual void Read(Reader& reader);
60 virtual void SetFullSpan();
61 const Span& GetSpan() const { return span; }
62 void SetSpanStart(int32_t spanStart) { span.start = spanStart; }
63 void SetSpanEnd(int32_t spanEnd) { span.end = spanEnd; }
64 virtual bool IsFunctionDeclaratorNode() const { return false; }
65 virtual bool IsTemplateArgumentSequenceNode() const { return false; }
66 virtual bool IsIdentifierNode() const { return false; }
67 virtual bool IsOperatorFunctionIdNode() const { return false; }
68 virtual bool IsConversionFunctionIdNode() const { return false; }
69 virtual bool IsDtorIdNode() const { return false; }
70 virtual bool IsTemplateIdNode() const { return false; }
71 Node* Parent() { return parent; }
72 void SetParent(Node* parent_) { parent = parent_; }
73 private:
74 NodeType nodeType;
75 Span span;
76 Node* parent;
77 };
78
79 class UnaryNode : public Node
80 {
81 public:
82 UnaryNode(NodeType nodeType_);
83 UnaryNode(NodeType nodeType_, const Span& span_);
84 UnaryNode(NodeType nodeType_, const Span& span_, Node* child_);
85 void Write(Writer& writer) override;
86 void Read(Reader& reader) override;
87 void SetFullSpan() override;
88 Node* Child() { return child.get(); }
89 void SetChild(Node* child_) { child.reset(child_); }
90 Node* ReleaseChild() { return child.release(); }
91 private:
92 std::unique_ptr<Node> child;
93 };
94
95 class BinaryNode : public Node
96 {
97 public:
98 BinaryNode(NodeType nodeType_);
99 BinaryNode(NodeType nodeType_, const Span& span_, Node* left_, Node* right_);
100 Node* Left() { return left.get(); }
101 Node* Right() { return right.get(); }
102 void Write(Writer& writer) override;
103 void Read(Reader& reader) override;
104 void SetFullSpan() override;
105 private:
106 std::unique_ptr<Node> left;
107 std::unique_ptr<Node> right;
108 };
109
110 class NodeFactory
111 {
112 public:
113 NodeFactory();
114 NodeFactory(const NodeFactory&) = delete;
115 NodeFactory(NodeFactory&&) = delete;
116 NodeFactory& operator=(const NodeFactory&) = delete;
117 NodeFactory& operator=(NodeFactory&&) = delete;
118 static void Init();
119 static void Done();
120 static NodeFactory& Instance();
121 Node* CreateNode(NodeType nodeType);
122 private:
123 static std::unique_ptr<NodeFactory> instance;
124 std::vector<std::std::unique_ptr<NodeCreator>>creators;
125 };
126
127 void NodeInit();
128 void NodeDone();
129
130 } }
131
132 #endif // SNGCPP_AST_NODE_INCLUDED