forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESTree.cpp
121 lines (108 loc) · 3.51 KB
/
ESTree.cpp
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/AST/ESTree.h"
#include "llvh/Support/raw_ostream.h"
using llvh::dyn_cast;
using llvh::isa;
namespace hermes {
namespace ESTree {
NodeList &getParams(FunctionLikeNode *node) {
switch (node->getKind()) {
default:
assert(
node->getKind() == NodeKind::Program && "invalid FunctionLikeNode");
return cast<ProgramNode>(node)->dummyParamList;
case NodeKind::FunctionExpression:
return cast<FunctionExpressionNode>(node)->_params;
case NodeKind::ArrowFunctionExpression:
return cast<ArrowFunctionExpressionNode>(node)->_params;
case NodeKind::FunctionDeclaration:
return cast<FunctionDeclarationNode>(node)->_params;
}
}
BlockStatementNode *getBlockStatement(FunctionLikeNode *node) {
switch (node->getKind()) {
default:
assert(
node->getKind() == NodeKind::Program && "invalid FunctionLikeNode");
return nullptr;
case NodeKind::FunctionExpression:
return cast<BlockStatementNode>(
cast<FunctionExpressionNode>(node)->_body);
case NodeKind::FunctionDeclaration:
return cast<BlockStatementNode>(
cast<FunctionDeclarationNode>(node)->_body);
case NodeKind::ArrowFunctionExpression: {
return dyn_cast<BlockStatementNode>(
cast<FunctionDeclarationNode>(node)->_body);
}
}
}
Node *getObject(MemberExpressionLikeNode *node) {
switch (node->getKind()) {
case NodeKind::MemberExpression:
return cast<MemberExpressionNode>(node)->_object;
case NodeKind::OptionalMemberExpression:
return cast<OptionalMemberExpressionNode>(node)->_object;
default:
break;
}
llvm_unreachable("invalid MemberExpressionLikeNode");
}
Node *getProperty(MemberExpressionLikeNode *node) {
switch (node->getKind()) {
case NodeKind::MemberExpression:
return cast<MemberExpressionNode>(node)->_property;
case NodeKind::OptionalMemberExpression:
return cast<OptionalMemberExpressionNode>(node)->_property;
default:
break;
}
llvm_unreachable("invalid MemberExpressionLikeNode");
}
NodeBoolean getComputed(MemberExpressionLikeNode *node) {
switch (node->getKind()) {
case NodeKind::MemberExpression:
return cast<MemberExpressionNode>(node)->_computed;
case NodeKind::OptionalMemberExpression:
return cast<OptionalMemberExpressionNode>(node)->_computed;
default:
break;
}
llvm_unreachable("invalid MemberExpressionLikeNode");
}
Node *getCallee(CallExpressionLikeNode *node) {
switch (node->getKind()) {
case NodeKind::CallExpression:
return cast<CallExpressionNode>(node)->_callee;
case NodeKind::OptionalCallExpression:
return cast<OptionalCallExpressionNode>(node)->_callee;
default:
break;
}
llvm_unreachable("invalid CallExpressionLikeNode");
}
NodeList &getArguments(CallExpressionLikeNode *node) {
switch (node->getKind()) {
case NodeKind::CallExpression:
return cast<CallExpressionNode>(node)->_arguments;
case NodeKind::OptionalCallExpression:
return cast<OptionalCallExpressionNode>(node)->_arguments;
default:
break;
}
llvm_unreachable("invalid CallExpressionLikeNode");
}
bool hasSimpleParams(FunctionLikeNode *node) {
for (Node ¶m : getParams(node)) {
if (isa<PatternNode>(param))
return false;
}
return true;
}
} // namespace ESTree
} // namespace hermes