Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: src/ast/ast.cc

Issue 2169833002: [parser] Refactor AstTraversalVisitor (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/ast.h ('k') | src/ast/ast-expression-visitor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/ast/ast.h" 5 #include "src/ast/ast.h"
6 6
7 #include <cmath> // For isfinite. 7 #include <cmath> // For isfinite.
8 8
9 #include "src/ast/prettyprinter.h" 9 #include "src/ast/prettyprinter.h"
10 #include "src/ast/scopes.h" 10 #include "src/ast/scopes.h"
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 return is_super ? NAMED_SUPER_PROPERTY_CALL : NAMED_PROPERTY_CALL; 916 return is_super ? NAMED_SUPER_PROPERTY_CALL : NAMED_PROPERTY_CALL;
917 } else { 917 } else {
918 return is_super ? KEYED_SUPER_PROPERTY_CALL : KEYED_PROPERTY_CALL; 918 return is_super ? KEYED_SUPER_PROPERTY_CALL : KEYED_PROPERTY_CALL;
919 } 919 }
920 } 920 }
921 921
922 return OTHER_CALL; 922 return OTHER_CALL;
923 } 923 }
924 924
925 925
926 // ----------------------------------------------------------------------------
927 // Implementation of AstTraversalVisitor
928
929 #define RECURSE(call) \
930 do { \
931 DCHECK(!HasStackOverflow()); \
932 call; \
933 if (HasStackOverflow()) return; \
934 } while (false)
935
936 #define RECURSE_EXPRESSION(call) \
937 do { \
938 DCHECK(!HasStackOverflow()); \
939 ++depth_; \
940 call; \
941 --depth_; \
942 if (HasStackOverflow()) return; \
943 } while (false)
944
945 AstTraversalVisitor::AstTraversalVisitor(Isolate* isolate) : depth_(0) {
946 InitializeAstVisitor(isolate);
947 }
948
949 AstTraversalVisitor::AstTraversalVisitor(uintptr_t stack_limit) : depth_(0) {
950 InitializeAstVisitor(stack_limit);
951 }
952
953 void AstTraversalVisitor::VisitDeclarations(ZoneList<Declaration*>* decls) {
954 for (int i = 0; i < decls->length(); ++i) {
955 Declaration* decl = decls->at(i);
956 RECURSE(Visit(decl));
957 }
958 }
959
960 void AstTraversalVisitor::VisitStatements(ZoneList<Statement*>* stmts) {
961 for (int i = 0; i < stmts->length(); ++i) {
962 Statement* stmt = stmts->at(i);
963 RECURSE(Visit(stmt));
964 if (stmt->IsJump()) break;
965 }
966 }
967
968 void AstTraversalVisitor::VisitVariableDeclaration(VariableDeclaration* decl) {}
969
970 void AstTraversalVisitor::VisitFunctionDeclaration(FunctionDeclaration* decl) {
971 RECURSE(Visit(decl->fun()));
972 }
973
974 void AstTraversalVisitor::VisitBlock(Block* stmt) {
975 RECURSE(VisitStatements(stmt->statements()));
976 }
977
978 void AstTraversalVisitor::VisitExpressionStatement(ExpressionStatement* stmt) {
979 RECURSE(Visit(stmt->expression()));
980 }
981
982 void AstTraversalVisitor::VisitEmptyStatement(EmptyStatement* stmt) {}
983
984 void AstTraversalVisitor::VisitSloppyBlockFunctionStatement(
985 SloppyBlockFunctionStatement* stmt) {
986 RECURSE(Visit(stmt->statement()));
987 }
988
989 void AstTraversalVisitor::VisitIfStatement(IfStatement* stmt) {
990 RECURSE(Visit(stmt->condition()));
991 RECURSE(Visit(stmt->then_statement()));
992 RECURSE(Visit(stmt->else_statement()));
993 }
994
995 void AstTraversalVisitor::VisitContinueStatement(ContinueStatement* stmt) {}
996
997 void AstTraversalVisitor::VisitBreakStatement(BreakStatement* stmt) {}
998
999 void AstTraversalVisitor::VisitReturnStatement(ReturnStatement* stmt) {
1000 RECURSE(Visit(stmt->expression()));
1001 }
1002
1003 void AstTraversalVisitor::VisitWithStatement(WithStatement* stmt) {
1004 RECURSE(stmt->expression());
1005 RECURSE(stmt->statement());
1006 }
1007
1008 void AstTraversalVisitor::VisitSwitchStatement(SwitchStatement* stmt) {
1009 RECURSE(Visit(stmt->tag()));
1010
1011 ZoneList<CaseClause*>* clauses = stmt->cases();
1012
1013 for (int i = 0; i < clauses->length(); ++i) {
1014 CaseClause* clause = clauses->at(i);
1015 if (!clause->is_default()) {
1016 Expression* label = clause->label();
1017 RECURSE(Visit(label));
1018 }
1019 ZoneList<Statement*>* stmts = clause->statements();
1020 RECURSE(VisitStatements(stmts));
1021 }
1022 }
1023
1024 void AstTraversalVisitor::VisitCaseClause(CaseClause* clause) { UNREACHABLE(); }
1025
1026 void AstTraversalVisitor::VisitDoWhileStatement(DoWhileStatement* stmt) {
1027 RECURSE(Visit(stmt->body()));
1028 RECURSE(Visit(stmt->cond()));
1029 }
1030
1031 void AstTraversalVisitor::VisitWhileStatement(WhileStatement* stmt) {
1032 RECURSE(Visit(stmt->cond()));
1033 RECURSE(Visit(stmt->body()));
1034 }
1035
1036 void AstTraversalVisitor::VisitForStatement(ForStatement* stmt) {
1037 if (stmt->init() != NULL) {
1038 RECURSE(Visit(stmt->init()));
1039 }
1040 if (stmt->cond() != NULL) {
1041 RECURSE(Visit(stmt->cond()));
1042 }
1043 if (stmt->next() != NULL) {
1044 RECURSE(Visit(stmt->next()));
1045 }
1046 RECURSE(Visit(stmt->body()));
1047 }
1048
1049 void AstTraversalVisitor::VisitForInStatement(ForInStatement* stmt) {
1050 RECURSE(Visit(stmt->enumerable()));
1051 RECURSE(Visit(stmt->body()));
1052 }
1053
1054 void AstTraversalVisitor::VisitForOfStatement(ForOfStatement* stmt) {
1055 RECURSE(Visit(stmt->assign_iterator()));
1056 RECURSE(Visit(stmt->next_result()));
1057 RECURSE(Visit(stmt->result_done()));
1058 RECURSE(Visit(stmt->assign_each()));
1059 RECURSE(Visit(stmt->body()));
1060 }
1061
1062 void AstTraversalVisitor::VisitTryCatchStatement(TryCatchStatement* stmt) {
1063 RECURSE(Visit(stmt->try_block()));
1064 RECURSE(Visit(stmt->catch_block()));
1065 }
1066
1067 void AstTraversalVisitor::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1068 RECURSE(Visit(stmt->try_block()));
1069 RECURSE(Visit(stmt->finally_block()));
1070 }
1071
1072 void AstTraversalVisitor::VisitDebuggerStatement(DebuggerStatement* stmt) {}
1073
1074 void AstTraversalVisitor::VisitFunctionLiteral(FunctionLiteral* expr) {
1075 Scope* scope = expr->scope();
1076 RECURSE_EXPRESSION(VisitDeclarations(scope->declarations()));
1077 RECURSE_EXPRESSION(VisitStatements(expr->body()));
1078 }
1079
1080 void AstTraversalVisitor::VisitNativeFunctionLiteral(
1081 NativeFunctionLiteral* expr) {}
1082
1083 void AstTraversalVisitor::VisitDoExpression(DoExpression* expr) {
1084 RECURSE(VisitBlock(expr->block()));
1085 RECURSE(VisitVariableProxy(expr->result()));
1086 }
1087
1088 void AstTraversalVisitor::VisitConditional(Conditional* expr) {
1089 RECURSE_EXPRESSION(Visit(expr->condition()));
1090 RECURSE_EXPRESSION(Visit(expr->then_expression()));
1091 RECURSE_EXPRESSION(Visit(expr->else_expression()));
1092 }
1093
1094 void AstTraversalVisitor::VisitVariableProxy(VariableProxy* expr) {}
1095
1096 void AstTraversalVisitor::VisitLiteral(Literal* expr) {}
1097
1098 void AstTraversalVisitor::VisitRegExpLiteral(RegExpLiteral* expr) {}
1099
1100 void AstTraversalVisitor::VisitObjectLiteral(ObjectLiteral* expr) {
1101 ZoneList<ObjectLiteralProperty*>* props = expr->properties();
1102 for (int i = 0; i < props->length(); ++i) {
1103 ObjectLiteralProperty* prop = props->at(i);
1104 if (!prop->key()->IsLiteral()) {
1105 RECURSE_EXPRESSION(Visit(prop->key()));
1106 }
1107 RECURSE_EXPRESSION(Visit(prop->value()));
1108 }
1109 }
1110
1111 void AstTraversalVisitor::VisitArrayLiteral(ArrayLiteral* expr) {
1112 ZoneList<Expression*>* values = expr->values();
1113 for (int i = 0; i < values->length(); ++i) {
1114 Expression* value = values->at(i);
1115 RECURSE_EXPRESSION(Visit(value));
1116 }
1117 }
1118
1119 void AstTraversalVisitor::VisitAssignment(Assignment* expr) {
1120 RECURSE_EXPRESSION(Visit(expr->target()));
1121 RECURSE_EXPRESSION(Visit(expr->value()));
1122 }
1123
1124 void AstTraversalVisitor::VisitYield(Yield* expr) {
1125 RECURSE_EXPRESSION(Visit(expr->generator_object()));
1126 RECURSE_EXPRESSION(Visit(expr->expression()));
1127 }
1128
1129 void AstTraversalVisitor::VisitThrow(Throw* expr) {
1130 RECURSE_EXPRESSION(Visit(expr->exception()));
1131 }
1132
1133 void AstTraversalVisitor::VisitProperty(Property* expr) {
1134 RECURSE_EXPRESSION(Visit(expr->obj()));
1135 RECURSE_EXPRESSION(Visit(expr->key()));
1136 }
1137
1138 void AstTraversalVisitor::VisitCall(Call* expr) {
1139 RECURSE_EXPRESSION(Visit(expr->expression()));
1140 ZoneList<Expression*>* args = expr->arguments();
1141 for (int i = 0; i < args->length(); ++i) {
1142 Expression* arg = args->at(i);
1143 RECURSE_EXPRESSION(Visit(arg));
1144 }
1145 }
1146
1147 void AstTraversalVisitor::VisitCallNew(CallNew* expr) {
1148 RECURSE_EXPRESSION(Visit(expr->expression()));
1149 ZoneList<Expression*>* args = expr->arguments();
1150 for (int i = 0; i < args->length(); ++i) {
1151 Expression* arg = args->at(i);
1152 RECURSE_EXPRESSION(Visit(arg));
1153 }
1154 }
1155
1156 void AstTraversalVisitor::VisitCallRuntime(CallRuntime* expr) {
1157 ZoneList<Expression*>* args = expr->arguments();
1158 for (int i = 0; i < args->length(); ++i) {
1159 Expression* arg = args->at(i);
1160 RECURSE_EXPRESSION(Visit(arg));
1161 }
1162 }
1163
1164 void AstTraversalVisitor::VisitUnaryOperation(UnaryOperation* expr) {
1165 RECURSE_EXPRESSION(Visit(expr->expression()));
1166 }
1167
1168 void AstTraversalVisitor::VisitCountOperation(CountOperation* expr) {
1169 RECURSE_EXPRESSION(Visit(expr->expression()));
1170 }
1171
1172 void AstTraversalVisitor::VisitBinaryOperation(BinaryOperation* expr) {
1173 RECURSE_EXPRESSION(Visit(expr->left()));
1174 RECURSE_EXPRESSION(Visit(expr->right()));
1175 }
1176
1177 void AstTraversalVisitor::VisitCompareOperation(CompareOperation* expr) {
1178 RECURSE_EXPRESSION(Visit(expr->left()));
1179 RECURSE_EXPRESSION(Visit(expr->right()));
1180 }
1181
1182 void AstTraversalVisitor::VisitThisFunction(ThisFunction* expr) {}
1183
1184 void AstTraversalVisitor::VisitClassLiteral(ClassLiteral* expr) {
1185 if (expr->extends() != nullptr) {
1186 RECURSE_EXPRESSION(Visit(expr->extends()));
1187 }
1188 RECURSE_EXPRESSION(Visit(expr->constructor()));
1189 ZoneList<ObjectLiteralProperty*>* props = expr->properties();
1190 for (int i = 0; i < props->length(); ++i) {
1191 ObjectLiteralProperty* prop = props->at(i);
1192 if (!prop->key()->IsLiteral()) {
1193 RECURSE_EXPRESSION(Visit(prop->key()));
1194 }
1195 RECURSE_EXPRESSION(Visit(prop->value()));
1196 }
1197 }
1198
1199 void AstTraversalVisitor::VisitSpread(Spread* expr) {
1200 RECURSE_EXPRESSION(Visit(expr->expression()));
1201 }
1202
1203 void AstTraversalVisitor::VisitEmptyParentheses(EmptyParentheses* expr) {}
1204
1205 void AstTraversalVisitor::VisitSuperPropertyReference(
1206 SuperPropertyReference* expr) {
1207 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_var()));
1208 RECURSE_EXPRESSION(Visit(expr->home_object()));
1209 }
1210
1211 void AstTraversalVisitor::VisitSuperCallReference(SuperCallReference* expr) {
1212 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_var()));
1213 RECURSE_EXPRESSION(VisitVariableProxy(expr->new_target_var()));
1214 RECURSE_EXPRESSION(VisitVariableProxy(expr->this_function_var()));
1215 }
1216
1217 void AstTraversalVisitor::VisitRewritableExpression(
1218 RewritableExpression* expr) {
1219 RECURSE(Visit(expr->expression()));
1220 }
1221
1222 #undef RECURSE_EXPRESSION
1223 #undef RECURSE
1224
1225 CaseClause::CaseClause(Zone* zone, Expression* label, 926 CaseClause::CaseClause(Zone* zone, Expression* label,
1226 ZoneList<Statement*>* statements, int pos) 927 ZoneList<Statement*>* statements, int pos)
1227 : Expression(zone, pos, kCaseClause), 928 : Expression(zone, pos, kCaseClause),
1228 label_(label), 929 label_(label),
1229 statements_(statements), 930 statements_(statements),
1230 compare_type_(Type::None()) {} 931 compare_type_(Type::None()) {}
1231 932
1232 uint32_t Literal::Hash() { 933 uint32_t Literal::Hash() {
1233 return raw_value()->IsString() 934 return raw_value()->IsString()
1234 ? raw_value()->AsString()->hash() 935 ? raw_value()->AsString()->hash()
1235 : ComputeLongHash(double_to_uint64(raw_value()->AsNumber())); 936 : ComputeLongHash(double_to_uint64(raw_value()->AsNumber()));
1236 } 937 }
1237 938
1238 939
1239 // static 940 // static
1240 bool Literal::Match(void* literal1, void* literal2) { 941 bool Literal::Match(void* literal1, void* literal2) {
1241 const AstValue* x = static_cast<Literal*>(literal1)->raw_value(); 942 const AstValue* x = static_cast<Literal*>(literal1)->raw_value();
1242 const AstValue* y = static_cast<Literal*>(literal2)->raw_value(); 943 const AstValue* y = static_cast<Literal*>(literal2)->raw_value();
1243 return (x->IsString() && y->IsString() && x->AsString() == y->AsString()) || 944 return (x->IsString() && y->IsString() && x->AsString() == y->AsString()) ||
1244 (x->IsNumber() && y->IsNumber() && x->AsNumber() == y->AsNumber()); 945 (x->IsNumber() && y->IsNumber() && x->AsNumber() == y->AsNumber());
1245 } 946 }
1246 947
1247 948
1248 } // namespace internal 949 } // namespace internal
1249 } // namespace v8 950 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/ast.h ('k') | src/ast/ast-expression-visitor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698