OLD | NEW |
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 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 for (int i = 0; i < expressions->length(); i++) { | 806 for (int i = 0; i < expressions->length(); i++) { |
807 // The variable statement visiting code may pass NULL expressions | 807 // The variable statement visiting code may pass NULL expressions |
808 // to this code. Maybe this should be handled by introducing an | 808 // to this code. Maybe this should be handled by introducing an |
809 // undefined expression or literal? Revisit this code if this | 809 // undefined expression or literal? Revisit this code if this |
810 // changes | 810 // changes |
811 Expression* expression = expressions->at(i); | 811 Expression* expression = expressions->at(i); |
812 if (expression != NULL) Visit(expression); | 812 if (expression != NULL) Visit(expression); |
813 } | 813 } |
814 } | 814 } |
815 | 815 |
| 816 // ---------------------------------------------------------------------------- |
| 817 // Implementation of AstTraversalVisitor |
| 818 |
| 819 AstTraversalVisitor::AstTraversalVisitor(Isolate* isolate) { |
| 820 InitializeAstVisitor(isolate); |
| 821 } |
| 822 |
| 823 void AstTraversalVisitor::VisitVariableDeclaration(VariableDeclaration* decl) {} |
| 824 |
| 825 void AstTraversalVisitor::VisitFunctionDeclaration(FunctionDeclaration* decl) { |
| 826 Visit(decl->fun()); |
| 827 } |
| 828 |
| 829 void AstTraversalVisitor::VisitImportDeclaration(ImportDeclaration* decl) {} |
| 830 |
| 831 void AstTraversalVisitor::VisitExportDeclaration(ExportDeclaration* decl) {} |
| 832 |
| 833 void AstTraversalVisitor::VisitStatements(ZoneList<Statement*>* stmts) { |
| 834 for (int i = 0; i < stmts->length(); ++i) { |
| 835 Statement* stmt = stmts->at(i); |
| 836 Visit(stmt); |
| 837 if (stmt->IsJump()) break; |
| 838 } |
| 839 } |
| 840 |
| 841 void AstTraversalVisitor::VisitExpressions(ZoneList<Expression*>* expressions) { |
| 842 for (int i = 0; i < expressions->length(); i++) { |
| 843 Expression* expression = expressions->at(i); |
| 844 if (expression != NULL) Visit(expression); |
| 845 } |
| 846 } |
| 847 |
| 848 void AstTraversalVisitor::VisitBlock(Block* stmt) { |
| 849 VisitStatements(stmt->statements()); |
| 850 } |
| 851 |
| 852 void AstTraversalVisitor::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 853 Visit(stmt->expression()); |
| 854 } |
| 855 |
| 856 void AstTraversalVisitor::VisitEmptyStatement(EmptyStatement* stmt) {} |
| 857 |
| 858 void AstTraversalVisitor::VisitSloppyBlockFunctionStatement( |
| 859 SloppyBlockFunctionStatement* stmt) { |
| 860 Visit(stmt->statement()); |
| 861 } |
| 862 |
| 863 void AstTraversalVisitor::VisitIfStatement(IfStatement* stmt) { |
| 864 Visit(stmt->condition()); |
| 865 Visit(stmt->then_statement()); |
| 866 Visit(stmt->else_statement()); |
| 867 } |
| 868 |
| 869 void AstTraversalVisitor::VisitContinueStatement(ContinueStatement* stmt) {} |
| 870 |
| 871 void AstTraversalVisitor::VisitBreakStatement(BreakStatement* stmt) {} |
| 872 |
| 873 void AstTraversalVisitor::VisitReturnStatement(ReturnStatement* stmt) { |
| 874 Visit(stmt->expression()); |
| 875 } |
| 876 |
| 877 void AstTraversalVisitor::VisitWithStatement(WithStatement* stmt) { |
| 878 stmt->expression(); |
| 879 stmt->statement(); |
| 880 } |
| 881 |
| 882 void AstTraversalVisitor::VisitSwitchStatement(SwitchStatement* stmt) { |
| 883 Visit(stmt->tag()); |
| 884 |
| 885 ZoneList<CaseClause*>* clauses = stmt->cases(); |
| 886 |
| 887 for (int i = 0; i < clauses->length(); ++i) { |
| 888 CaseClause* clause = clauses->at(i); |
| 889 if (!clause->is_default()) { |
| 890 Expression* label = clause->label(); |
| 891 Visit(label); |
| 892 } |
| 893 ZoneList<Statement*>* stmts = clause->statements(); |
| 894 VisitStatements(stmts); |
| 895 } |
| 896 } |
| 897 |
| 898 void AstTraversalVisitor::VisitCaseClause(CaseClause* clause) { UNREACHABLE(); } |
| 899 |
| 900 void AstTraversalVisitor::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 901 Visit(stmt->body()); |
| 902 Visit(stmt->cond()); |
| 903 } |
| 904 |
| 905 void AstTraversalVisitor::VisitWhileStatement(WhileStatement* stmt) { |
| 906 Visit(stmt->cond()); |
| 907 Visit(stmt->body()); |
| 908 } |
| 909 |
| 910 void AstTraversalVisitor::VisitForStatement(ForStatement* stmt) { |
| 911 if (stmt->init() != NULL) { |
| 912 Visit(stmt->init()); |
| 913 } |
| 914 if (stmt->cond() != NULL) { |
| 915 Visit(stmt->cond()); |
| 916 } |
| 917 if (stmt->next() != NULL) { |
| 918 Visit(stmt->next()); |
| 919 } |
| 920 Visit(stmt->body()); |
| 921 } |
| 922 |
| 923 void AstTraversalVisitor::VisitForInStatement(ForInStatement* stmt) { |
| 924 Visit(stmt->enumerable()); |
| 925 Visit(stmt->body()); |
| 926 } |
| 927 |
| 928 void AstTraversalVisitor::VisitForOfStatement(ForOfStatement* stmt) { |
| 929 Visit(stmt->assign_iterator()); |
| 930 Visit(stmt->next_result()); |
| 931 Visit(stmt->result_done()); |
| 932 Visit(stmt->assign_each()); |
| 933 Visit(stmt->body()); |
| 934 } |
| 935 |
| 936 void AstTraversalVisitor::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 937 Visit(stmt->try_block()); |
| 938 Visit(stmt->catch_block()); |
| 939 } |
| 940 |
| 941 void AstTraversalVisitor::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 942 Visit(stmt->try_block()); |
| 943 Visit(stmt->finally_block()); |
| 944 } |
| 945 |
| 946 void AstTraversalVisitor::VisitDebuggerStatement(DebuggerStatement* stmt) {} |
| 947 |
| 948 void AstTraversalVisitor::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 949 Scope* scope = expr->scope(); |
| 950 VisitDeclarations(scope->declarations()); |
| 951 VisitStatements(expr->body()); |
| 952 } |
| 953 |
| 954 void AstTraversalVisitor::VisitNativeFunctionLiteral( |
| 955 NativeFunctionLiteral* expr) {} |
| 956 |
| 957 void AstTraversalVisitor::VisitDoExpression(DoExpression* expr) { |
| 958 VisitBlock(expr->block()); |
| 959 VisitVariableProxy(expr->result()); |
| 960 } |
| 961 |
| 962 void AstTraversalVisitor::VisitConditional(Conditional* expr) { |
| 963 Visit(expr->condition()); |
| 964 Visit(expr->then_expression()); |
| 965 Visit(expr->else_expression()); |
| 966 } |
| 967 |
| 968 void AstTraversalVisitor::VisitVariableProxy(VariableProxy* expr) {} |
| 969 |
| 970 void AstTraversalVisitor::VisitLiteral(Literal* expr) {} |
| 971 |
| 972 void AstTraversalVisitor::VisitRegExpLiteral(RegExpLiteral* expr) {} |
| 973 |
| 974 void AstTraversalVisitor::VisitObjectLiteral(ObjectLiteral* expr) { |
| 975 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); |
| 976 for (int i = 0; i < props->length(); ++i) { |
| 977 ObjectLiteralProperty* prop = props->at(i); |
| 978 if (!prop->key()->IsLiteral()) { |
| 979 Visit(prop->key()); |
| 980 } |
| 981 Visit(prop->value()); |
| 982 } |
| 983 } |
| 984 |
| 985 void AstTraversalVisitor::VisitArrayLiteral(ArrayLiteral* expr) { |
| 986 ZoneList<Expression*>* values = expr->values(); |
| 987 for (int i = 0; i < values->length(); ++i) { |
| 988 Expression* value = values->at(i); |
| 989 Visit(value); |
| 990 } |
| 991 } |
| 992 |
| 993 void AstTraversalVisitor::VisitAssignment(Assignment* expr) { |
| 994 Visit(expr->target()); |
| 995 Visit(expr->value()); |
| 996 } |
| 997 |
| 998 void AstTraversalVisitor::VisitYield(Yield* expr) { |
| 999 Visit(expr->generator_object()); |
| 1000 Visit(expr->expression()); |
| 1001 } |
| 1002 |
| 1003 void AstTraversalVisitor::VisitThrow(Throw* expr) { Visit(expr->exception()); } |
| 1004 |
| 1005 void AstTraversalVisitor::VisitProperty(Property* expr) { |
| 1006 Visit(expr->obj()); |
| 1007 Visit(expr->key()); |
| 1008 } |
| 1009 |
| 1010 void AstTraversalVisitor::VisitCall(Call* expr) { |
| 1011 Visit(expr->expression()); |
| 1012 ZoneList<Expression*>* args = expr->arguments(); |
| 1013 for (int i = 0; i < args->length(); ++i) { |
| 1014 Expression* arg = args->at(i); |
| 1015 Visit(arg); |
| 1016 } |
| 1017 } |
| 1018 |
| 1019 void AstTraversalVisitor::VisitCallNew(CallNew* expr) { |
| 1020 Visit(expr->expression()); |
| 1021 ZoneList<Expression*>* args = expr->arguments(); |
| 1022 for (int i = 0; i < args->length(); ++i) { |
| 1023 Expression* arg = args->at(i); |
| 1024 Visit(arg); |
| 1025 } |
| 1026 } |
| 1027 |
| 1028 void AstTraversalVisitor::VisitCallRuntime(CallRuntime* expr) { |
| 1029 ZoneList<Expression*>* args = expr->arguments(); |
| 1030 for (int i = 0; i < args->length(); ++i) { |
| 1031 Expression* arg = args->at(i); |
| 1032 Visit(arg); |
| 1033 } |
| 1034 } |
| 1035 |
| 1036 void AstTraversalVisitor::VisitUnaryOperation(UnaryOperation* expr) { |
| 1037 Visit(expr->expression()); |
| 1038 } |
| 1039 |
| 1040 void AstTraversalVisitor::VisitCountOperation(CountOperation* expr) { |
| 1041 Visit(expr->expression()); |
| 1042 } |
| 1043 |
| 1044 void AstTraversalVisitor::VisitBinaryOperation(BinaryOperation* expr) { |
| 1045 Visit(expr->left()); |
| 1046 Visit(expr->right()); |
| 1047 } |
| 1048 |
| 1049 void AstTraversalVisitor::VisitCompareOperation(CompareOperation* expr) { |
| 1050 Visit(expr->left()); |
| 1051 Visit(expr->right()); |
| 1052 } |
| 1053 |
| 1054 void AstTraversalVisitor::VisitThisFunction(ThisFunction* expr) {} |
| 1055 |
| 1056 void AstTraversalVisitor::VisitDeclarations(ZoneList<Declaration*>* decls) { |
| 1057 for (int i = 0; i < decls->length(); ++i) { |
| 1058 Declaration* decl = decls->at(i); |
| 1059 Visit(decl); |
| 1060 } |
| 1061 } |
| 1062 |
| 1063 void AstTraversalVisitor::VisitClassLiteral(ClassLiteral* expr) { |
| 1064 if (expr->extends() != nullptr) { |
| 1065 Visit(expr->extends()); |
| 1066 } |
| 1067 Visit(expr->constructor()); |
| 1068 ZoneList<ObjectLiteralProperty*>* props = expr->properties(); |
| 1069 for (int i = 0; i < props->length(); ++i) { |
| 1070 ObjectLiteralProperty* prop = props->at(i); |
| 1071 if (!prop->key()->IsLiteral()) { |
| 1072 Visit(prop->key()); |
| 1073 } |
| 1074 Visit(prop->value()); |
| 1075 } |
| 1076 } |
| 1077 |
| 1078 void AstTraversalVisitor::VisitSpread(Spread* expr) { |
| 1079 Visit(expr->expression()); |
| 1080 } |
| 1081 |
| 1082 void AstTraversalVisitor::VisitEmptyParentheses(EmptyParentheses* expr) {} |
| 1083 |
| 1084 void AstTraversalVisitor::VisitSuperPropertyReference( |
| 1085 SuperPropertyReference* expr) { |
| 1086 VisitVariableProxy(expr->this_var()); |
| 1087 Visit(expr->home_object()); |
| 1088 } |
| 1089 |
| 1090 void AstTraversalVisitor::VisitSuperCallReference(SuperCallReference* expr) { |
| 1091 VisitVariableProxy(expr->this_var()); |
| 1092 VisitVariableProxy(expr->new_target_var()); |
| 1093 VisitVariableProxy(expr->this_function_var()); |
| 1094 } |
| 1095 |
| 1096 void AstTraversalVisitor::VisitRewritableExpression( |
| 1097 RewritableExpression* expr) { |
| 1098 Visit(expr->expression()); |
| 1099 } |
| 1100 |
816 CaseClause::CaseClause(Zone* zone, Expression* label, | 1101 CaseClause::CaseClause(Zone* zone, Expression* label, |
817 ZoneList<Statement*>* statements, int pos) | 1102 ZoneList<Statement*>* statements, int pos) |
818 : Expression(zone, pos), | 1103 : Expression(zone, pos), |
819 label_(label), | 1104 label_(label), |
820 statements_(statements), | 1105 statements_(statements), |
821 compare_type_(Type::None()) {} | 1106 compare_type_(Type::None()) {} |
822 | 1107 |
823 uint32_t Literal::Hash() { | 1108 uint32_t Literal::Hash() { |
824 return raw_value()->IsString() | 1109 return raw_value()->IsString() |
825 ? raw_value()->AsString()->hash() | 1110 ? raw_value()->AsString()->hash() |
826 : ComputeLongHash(double_to_uint64(raw_value()->AsNumber())); | 1111 : ComputeLongHash(double_to_uint64(raw_value()->AsNumber())); |
827 } | 1112 } |
828 | 1113 |
829 | 1114 |
830 // static | 1115 // static |
831 bool Literal::Match(void* literal1, void* literal2) { | 1116 bool Literal::Match(void* literal1, void* literal2) { |
832 const AstValue* x = static_cast<Literal*>(literal1)->raw_value(); | 1117 const AstValue* x = static_cast<Literal*>(literal1)->raw_value(); |
833 const AstValue* y = static_cast<Literal*>(literal2)->raw_value(); | 1118 const AstValue* y = static_cast<Literal*>(literal2)->raw_value(); |
834 return (x->IsString() && y->IsString() && x->AsString() == y->AsString()) || | 1119 return (x->IsString() && y->IsString() && x->AsString() == y->AsString()) || |
835 (x->IsNumber() && y->IsNumber() && x->AsNumber() == y->AsNumber()); | 1120 (x->IsNumber() && y->IsNumber() && x->AsNumber() == y->AsNumber()); |
836 } | 1121 } |
837 | 1122 |
838 | 1123 |
839 } // namespace internal | 1124 } // namespace internal |
840 } // namespace v8 | 1125 } // namespace v8 |
OLD | NEW |