-
Notifications
You must be signed in to change notification settings - Fork 19
/
type_node.h
126 lines (105 loc) · 2.68 KB
/
type_node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <vector>
#include <memory>
struct TypeNode
{
enum class Type
{
kPointer,
kReference,
kLReference,
kLiteral,
kTemplate,
kFunction
};
TypeNode(Type t) :
type(t) {}
bool isConst = false;
bool isVolatile = false;
bool isMutable = false;
Type type;
};
struct PointerNode : public TypeNode
{
PointerNode(std::unique_ptr<TypeNode> && b) :
TypeNode(TypeNode::Type::kPointer),
base(std::forward<std::unique_ptr<TypeNode>>(b)){}
std::unique_ptr<TypeNode> base;
};
struct ReferenceNode : public TypeNode
{
ReferenceNode(std::unique_ptr<TypeNode> && b) :
TypeNode(TypeNode::Type::kReference),
base(std::forward<std::unique_ptr<TypeNode>>(b)){}
std::unique_ptr<TypeNode> base;
};
struct LReferenceNode : public TypeNode
{
LReferenceNode(std::unique_ptr<TypeNode> && b) :
TypeNode(TypeNode::Type::kLReference),
base(std::forward<std::unique_ptr<TypeNode>>(b)){}
std::unique_ptr<TypeNode> base;
};
struct TemplateNode : public TypeNode
{
TemplateNode(const std::string& n) :
TypeNode(TypeNode::Type::kTemplate),
name(n) {}
std::string name;
std::vector<std::unique_ptr<TypeNode>> arguments;
};
struct LiteralNode : public TypeNode
{
LiteralNode(const std::string& ref) :
TypeNode(TypeNode::Type::kLiteral),
name(ref) {}
std::string name;
};
struct FunctionNode : public TypeNode
{
FunctionNode() : TypeNode(TypeNode::Type::kFunction) {}
struct Argument
{
std::string name;
std::unique_ptr<TypeNode> type;
};
std::unique_ptr<TypeNode> returns;
std::vector<std::unique_ptr<Argument>> arguments;
};
struct ITypeNodeVisitor
{
virtual void VisitNode(TypeNode &node) = 0;
virtual void Visit(PointerNode& node) = 0;
virtual void Visit(ReferenceNode& node) = 0;
virtual void Visit(LReferenceNode& node) = 0;
virtual void Visit(LiteralNode& node) = 0;
virtual void Visit(TemplateNode& node) = 0;
virtual void Visit(FunctionNode& node) = 0;
};
struct TypeNodeVisitor : public ITypeNodeVisitor
{
void VisitNode(TypeNode &node) override
{
switch (node.type)
{
case TypeNode::Type::kPointer:
Visit(reinterpret_cast<PointerNode&>(node));
break;
case TypeNode::Type::kReference:
Visit(reinterpret_cast<ReferenceNode&>(node));
break;
case TypeNode::Type::kLReference:
Visit(reinterpret_cast<LReferenceNode&>(node));
break;
case TypeNode::Type::kLiteral:
Visit(reinterpret_cast<LiteralNode&>(node));
break;
case TypeNode::Type::kTemplate:
Visit(reinterpret_cast<TemplateNode&>(node));
break;
case TypeNode::Type::kFunction:
Visit(reinterpret_cast<FunctionNode&>(node));
break;
}
}
};