OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
(...skipping 999 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 IndentedScope indent(this, Token::Name(node->op()), node); | 1011 IndentedScope indent(this, Token::Name(node->op()), node); |
1012 Visit(node->left()); | 1012 Visit(node->left()); |
1013 Visit(node->right()); | 1013 Visit(node->right()); |
1014 } | 1014 } |
1015 | 1015 |
1016 | 1016 |
1017 void AstPrinter::VisitThisFunction(ThisFunction* node) { | 1017 void AstPrinter::VisitThisFunction(ThisFunction* node) { |
1018 IndentedScope indent(this, "THIS-FUNCTION"); | 1018 IndentedScope indent(this, "THIS-FUNCTION"); |
1019 } | 1019 } |
1020 | 1020 |
1021 | |
1022 TagScope::TagScope(JsonAstBuilder* builder, const char* name) | |
1023 : builder_(builder), next_(builder->tag()), has_body_(false) { | |
1024 if (next_ != NULL) { | |
1025 next_->use(); | |
1026 builder->Print(",\n"); | |
1027 } | |
1028 builder->set_tag(this); | |
1029 builder->PrintIndented("["); | |
1030 builder->Print("\"%s\"", name); | |
1031 builder->increase_indent(JsonAstBuilder::kTagIndentSize); | |
1032 } | |
1033 | |
1034 | |
1035 TagScope::~TagScope() { | |
1036 builder_->decrease_indent(JsonAstBuilder::kTagIndentSize); | |
1037 if (has_body_) { | |
1038 builder_->Print("\n"); | |
1039 builder_->PrintIndented("]"); | |
1040 } else { | |
1041 builder_->Print("]"); | |
1042 } | |
1043 builder_->set_tag(next_); | |
1044 } | |
1045 | |
1046 | |
1047 AttributesScope::AttributesScope(JsonAstBuilder* builder) | |
1048 : builder_(builder), attribute_count_(0) { | |
1049 builder->set_attributes(this); | |
1050 builder->tag()->use(); | |
1051 builder->Print(",\n"); | |
1052 builder->PrintIndented("{"); | |
1053 builder->increase_indent(JsonAstBuilder::kAttributesIndentSize); | |
1054 } | |
1055 | |
1056 | |
1057 AttributesScope::~AttributesScope() { | |
1058 builder_->decrease_indent(JsonAstBuilder::kAttributesIndentSize); | |
1059 if (attribute_count_ > 1) { | |
1060 builder_->Print("\n"); | |
1061 builder_->PrintIndented("}"); | |
1062 } else { | |
1063 builder_->Print("}"); | |
1064 } | |
1065 builder_->set_attributes(NULL); | |
1066 } | |
1067 | |
1068 | |
1069 const char* JsonAstBuilder::BuildProgram(FunctionLiteral* program) { | |
1070 Init(); | |
1071 Visit(program); | |
1072 Print("\n"); | |
1073 return Output(); | |
1074 } | |
1075 | |
1076 | |
1077 void JsonAstBuilder::AddAttributePrefix(const char* name) { | |
1078 if (attributes()->is_used()) { | |
1079 Print(",\n"); | |
1080 PrintIndented("\""); | |
1081 } else { | |
1082 Print("\""); | |
1083 } | |
1084 Print("%s\":", name); | |
1085 attributes()->use(); | |
1086 } | |
1087 | |
1088 | |
1089 void JsonAstBuilder::AddAttribute(const char* name, Handle<String> value) { | |
1090 SmartArrayPointer<char> value_string = value->ToCString(); | |
1091 AddAttributePrefix(name); | |
1092 Print("\"%s\"", *value_string); | |
1093 } | |
1094 | |
1095 | |
1096 void JsonAstBuilder::AddAttribute(const char* name, const char* value) { | |
1097 AddAttributePrefix(name); | |
1098 Print("\"%s\"", value); | |
1099 } | |
1100 | |
1101 | |
1102 void JsonAstBuilder::AddAttribute(const char* name, int value) { | |
1103 AddAttributePrefix(name); | |
1104 Print("%d", value); | |
1105 } | |
1106 | |
1107 | |
1108 void JsonAstBuilder::AddAttribute(const char* name, bool value) { | |
1109 AddAttributePrefix(name); | |
1110 Print(value ? "true" : "false"); | |
1111 } | |
1112 | |
1113 | |
1114 void JsonAstBuilder::VisitBlock(Block* stmt) { | |
1115 TagScope tag(this, "Block"); | |
1116 VisitStatements(stmt->statements()); | |
1117 } | |
1118 | |
1119 | |
1120 void JsonAstBuilder::VisitExpressionStatement(ExpressionStatement* stmt) { | |
1121 TagScope tag(this, "ExpressionStatement"); | |
1122 Visit(stmt->expression()); | |
1123 } | |
1124 | |
1125 | |
1126 void JsonAstBuilder::VisitEmptyStatement(EmptyStatement* stmt) { | |
1127 TagScope tag(this, "EmptyStatement"); | |
1128 } | |
1129 | |
1130 | |
1131 void JsonAstBuilder::VisitIfStatement(IfStatement* stmt) { | |
1132 TagScope tag(this, "IfStatement"); | |
1133 Visit(stmt->condition()); | |
1134 Visit(stmt->then_statement()); | |
1135 Visit(stmt->else_statement()); | |
1136 } | |
1137 | |
1138 | |
1139 void JsonAstBuilder::VisitContinueStatement(ContinueStatement* stmt) { | |
1140 TagScope tag(this, "ContinueStatement"); | |
1141 } | |
1142 | |
1143 | |
1144 void JsonAstBuilder::VisitBreakStatement(BreakStatement* stmt) { | |
1145 TagScope tag(this, "BreakStatement"); | |
1146 } | |
1147 | |
1148 | |
1149 void JsonAstBuilder::VisitReturnStatement(ReturnStatement* stmt) { | |
1150 TagScope tag(this, "ReturnStatement"); | |
1151 Visit(stmt->expression()); | |
1152 } | |
1153 | |
1154 | |
1155 void JsonAstBuilder::VisitWithStatement(WithStatement* stmt) { | |
1156 TagScope tag(this, "WithStatement"); | |
1157 Visit(stmt->expression()); | |
1158 Visit(stmt->statement()); | |
1159 } | |
1160 | |
1161 | |
1162 void JsonAstBuilder::VisitSwitchStatement(SwitchStatement* stmt) { | |
1163 TagScope tag(this, "SwitchStatement"); | |
1164 } | |
1165 | |
1166 | |
1167 void JsonAstBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) { | |
1168 TagScope tag(this, "DoWhileStatement"); | |
1169 Visit(stmt->body()); | |
1170 Visit(stmt->cond()); | |
1171 } | |
1172 | |
1173 | |
1174 void JsonAstBuilder::VisitWhileStatement(WhileStatement* stmt) { | |
1175 TagScope tag(this, "WhileStatement"); | |
1176 Visit(stmt->cond()); | |
1177 Visit(stmt->body()); | |
1178 } | |
1179 | |
1180 | |
1181 void JsonAstBuilder::VisitForStatement(ForStatement* stmt) { | |
1182 TagScope tag(this, "ForStatement"); | |
1183 if (stmt->init() != NULL) Visit(stmt->init()); | |
1184 if (stmt->cond() != NULL) Visit(stmt->cond()); | |
1185 Visit(stmt->body()); | |
1186 if (stmt->next() != NULL) Visit(stmt->next()); | |
1187 } | |
1188 | |
1189 | |
1190 void JsonAstBuilder::VisitForInStatement(ForInStatement* stmt) { | |
1191 TagScope tag(this, "ForInStatement"); | |
1192 Visit(stmt->each()); | |
1193 Visit(stmt->enumerable()); | |
1194 Visit(stmt->body()); | |
1195 } | |
1196 | |
1197 | |
1198 void JsonAstBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) { | |
1199 TagScope tag(this, "TryCatchStatement"); | |
1200 { AttributesScope attributes(this); | |
1201 AddAttribute("variable", stmt->variable()->name()); | |
1202 } | |
1203 Visit(stmt->try_block()); | |
1204 Visit(stmt->catch_block()); | |
1205 } | |
1206 | |
1207 | |
1208 void JsonAstBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) { | |
1209 TagScope tag(this, "TryFinallyStatement"); | |
1210 Visit(stmt->try_block()); | |
1211 Visit(stmt->finally_block()); | |
1212 } | |
1213 | |
1214 | |
1215 void JsonAstBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) { | |
1216 TagScope tag(this, "DebuggerStatement"); | |
1217 } | |
1218 | |
1219 | |
1220 void JsonAstBuilder::VisitFunctionLiteral(FunctionLiteral* expr) { | |
1221 TagScope tag(this, "FunctionLiteral"); | |
1222 { | |
1223 AttributesScope attributes(this); | |
1224 AddAttribute("name", expr->name()); | |
1225 } | |
1226 VisitDeclarations(expr->scope()->declarations()); | |
1227 VisitStatements(expr->body()); | |
1228 } | |
1229 | |
1230 | |
1231 void JsonAstBuilder::VisitSharedFunctionInfoLiteral( | |
1232 SharedFunctionInfoLiteral* expr) { | |
1233 TagScope tag(this, "SharedFunctionInfoLiteral"); | |
1234 } | |
1235 | |
1236 | |
1237 void JsonAstBuilder::VisitConditional(Conditional* expr) { | |
1238 TagScope tag(this, "Conditional"); | |
1239 } | |
1240 | |
1241 | |
1242 void JsonAstBuilder::VisitVariableProxy(VariableProxy* expr) { | |
1243 TagScope tag(this, "Variable"); | |
1244 { | |
1245 AttributesScope attributes(this); | |
1246 Variable* var = expr->var(); | |
1247 AddAttribute("name", var->name()); | |
1248 switch (var->location()) { | |
1249 case Variable::UNALLOCATED: | |
1250 AddAttribute("location", "UNALLOCATED"); | |
1251 break; | |
1252 case Variable::PARAMETER: | |
1253 AddAttribute("location", "PARAMETER"); | |
1254 AddAttribute("index", var->index()); | |
1255 break; | |
1256 case Variable::LOCAL: | |
1257 AddAttribute("location", "LOCAL"); | |
1258 AddAttribute("index", var->index()); | |
1259 break; | |
1260 case Variable::CONTEXT: | |
1261 AddAttribute("location", "CONTEXT"); | |
1262 AddAttribute("index", var->index()); | |
1263 break; | |
1264 case Variable::LOOKUP: | |
1265 AddAttribute("location", "LOOKUP"); | |
1266 break; | |
1267 } | |
1268 } | |
1269 } | |
1270 | |
1271 | |
1272 void JsonAstBuilder::VisitLiteral(Literal* expr) { | |
1273 TagScope tag(this, "Literal"); | |
1274 { | |
1275 AttributesScope attributes(this); | |
1276 Handle<Object> handle = expr->handle(); | |
1277 if (handle->IsString()) { | |
1278 AddAttribute("handle", Handle<String>(String::cast(*handle))); | |
1279 } else if (handle->IsSmi()) { | |
1280 AddAttribute("handle", Smi::cast(*handle)->value()); | |
1281 } | |
1282 } | |
1283 } | |
1284 | |
1285 | |
1286 void JsonAstBuilder::VisitRegExpLiteral(RegExpLiteral* expr) { | |
1287 TagScope tag(this, "RegExpLiteral"); | |
1288 } | |
1289 | |
1290 | |
1291 void JsonAstBuilder::VisitObjectLiteral(ObjectLiteral* expr) { | |
1292 TagScope tag(this, "ObjectLiteral"); | |
1293 } | |
1294 | |
1295 | |
1296 void JsonAstBuilder::VisitArrayLiteral(ArrayLiteral* expr) { | |
1297 TagScope tag(this, "ArrayLiteral"); | |
1298 } | |
1299 | |
1300 | |
1301 void JsonAstBuilder::VisitAssignment(Assignment* expr) { | |
1302 TagScope tag(this, "Assignment"); | |
1303 { | |
1304 AttributesScope attributes(this); | |
1305 AddAttribute("op", Token::Name(expr->op())); | |
1306 } | |
1307 Visit(expr->target()); | |
1308 Visit(expr->value()); | |
1309 } | |
1310 | |
1311 | |
1312 void JsonAstBuilder::VisitThrow(Throw* expr) { | |
1313 TagScope tag(this, "Throw"); | |
1314 Visit(expr->exception()); | |
1315 } | |
1316 | |
1317 | |
1318 void JsonAstBuilder::VisitProperty(Property* expr) { | |
1319 TagScope tag(this, "Property"); | |
1320 Visit(expr->obj()); | |
1321 Visit(expr->key()); | |
1322 } | |
1323 | |
1324 | |
1325 void JsonAstBuilder::VisitCall(Call* expr) { | |
1326 TagScope tag(this, "Call"); | |
1327 Visit(expr->expression()); | |
1328 VisitExpressions(expr->arguments()); | |
1329 } | |
1330 | |
1331 | |
1332 void JsonAstBuilder::VisitCallNew(CallNew* expr) { | |
1333 TagScope tag(this, "CallNew"); | |
1334 Visit(expr->expression()); | |
1335 VisitExpressions(expr->arguments()); | |
1336 } | |
1337 | |
1338 | |
1339 void JsonAstBuilder::VisitCallRuntime(CallRuntime* expr) { | |
1340 TagScope tag(this, "CallRuntime"); | |
1341 { | |
1342 AttributesScope attributes(this); | |
1343 AddAttribute("name", expr->name()); | |
1344 } | |
1345 VisitExpressions(expr->arguments()); | |
1346 } | |
1347 | |
1348 | |
1349 void JsonAstBuilder::VisitUnaryOperation(UnaryOperation* expr) { | |
1350 TagScope tag(this, "UnaryOperation"); | |
1351 { | |
1352 AttributesScope attributes(this); | |
1353 AddAttribute("op", Token::Name(expr->op())); | |
1354 } | |
1355 Visit(expr->expression()); | |
1356 } | |
1357 | |
1358 | |
1359 void JsonAstBuilder::VisitCountOperation(CountOperation* expr) { | |
1360 TagScope tag(this, "CountOperation"); | |
1361 { | |
1362 AttributesScope attributes(this); | |
1363 AddAttribute("is_prefix", expr->is_prefix()); | |
1364 AddAttribute("op", Token::Name(expr->op())); | |
1365 } | |
1366 Visit(expr->expression()); | |
1367 } | |
1368 | |
1369 | |
1370 void JsonAstBuilder::VisitBinaryOperation(BinaryOperation* expr) { | |
1371 TagScope tag(this, "BinaryOperation"); | |
1372 { | |
1373 AttributesScope attributes(this); | |
1374 AddAttribute("op", Token::Name(expr->op())); | |
1375 } | |
1376 Visit(expr->left()); | |
1377 Visit(expr->right()); | |
1378 } | |
1379 | |
1380 | |
1381 void JsonAstBuilder::VisitCompareOperation(CompareOperation* expr) { | |
1382 TagScope tag(this, "CompareOperation"); | |
1383 { | |
1384 AttributesScope attributes(this); | |
1385 AddAttribute("op", Token::Name(expr->op())); | |
1386 } | |
1387 Visit(expr->left()); | |
1388 Visit(expr->right()); | |
1389 } | |
1390 | |
1391 | |
1392 void JsonAstBuilder::VisitThisFunction(ThisFunction* expr) { | |
1393 TagScope tag(this, "ThisFunction"); | |
1394 } | |
1395 | |
1396 | |
1397 void JsonAstBuilder::VisitDeclaration(Declaration* decl) { | |
1398 TagScope tag(this, "Declaration"); | |
1399 { | |
1400 AttributesScope attributes(this); | |
1401 AddAttribute("mode", Variable::Mode2String(decl->mode())); | |
1402 } | |
1403 Visit(decl->proxy()); | |
1404 if (decl->fun() != NULL) Visit(decl->fun()); | |
1405 } | |
1406 | |
1407 | |
1408 #endif // DEBUG | 1021 #endif // DEBUG |
1409 | 1022 |
1410 } } // namespace v8::internal | 1023 } } // namespace v8::internal |
OLD | NEW |