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

Side by Side Diff: src/parser.cc

Issue 3146037: Cleanup the AST code by removing unused parts and get rid of the... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 10 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 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
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 TemporaryScope* temp_scope_; 147 TemporaryScope* temp_scope_;
148 Mode mode_; 148 Mode mode_;
149 149
150 Target* target_stack_; // for break, continue statements 150 Target* target_stack_; // for break, continue statements
151 bool allow_natives_syntax_; 151 bool allow_natives_syntax_;
152 v8::Extension* extension_; 152 v8::Extension* extension_;
153 ParserFactory* factory_; 153 ParserFactory* factory_;
154 ParserLog* log_; 154 ParserLog* log_;
155 bool is_pre_parsing_; 155 bool is_pre_parsing_;
156 ScriptDataImpl* pre_data_; 156 ScriptDataImpl* pre_data_;
157 bool seen_loop_stmt_; // Used for inner loop detection.
158 FuncNameInferrer* fni_; 157 FuncNameInferrer* fni_;
159 158
160 bool inside_with() const { return with_nesting_level_ > 0; } 159 bool inside_with() const { return with_nesting_level_ > 0; }
161 ParserFactory* factory() const { return factory_; } 160 ParserFactory* factory() const { return factory_; }
162 ParserLog* log() const { return log_; } 161 ParserLog* log() const { return log_; }
163 Scanner& scanner() { return scanner_; } 162 Scanner& scanner() { return scanner_; }
164 Mode mode() const { return mode_; } 163 Mode mode() const { return mode_; }
165 ScriptDataImpl* pre_data() const { return pre_data_; } 164 ScriptDataImpl* pre_data() const { return pre_data_; }
166 165
167 // All ParseXXX functions take as the last argument an *ok parameter 166 // All ParseXXX functions take as the last argument an *ok parameter
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 Expression* ParseMemberExpression(bool* ok); 207 Expression* ParseMemberExpression(bool* ok);
209 Expression* ParseNewPrefix(PositionStack* stack, bool* ok); 208 Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
210 Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack, 209 Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
211 bool* ok); 210 bool* ok);
212 Expression* ParsePrimaryExpression(bool* ok); 211 Expression* ParsePrimaryExpression(bool* ok);
213 Expression* ParseArrayLiteral(bool* ok); 212 Expression* ParseArrayLiteral(bool* ok);
214 Expression* ParseObjectLiteral(bool* ok); 213 Expression* ParseObjectLiteral(bool* ok);
215 ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok); 214 ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok);
216 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok); 215 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
217 216
217 Expression* NewCompareNode(Token::Value op, Expression* x, Expression* y);
218
218 // Populate the constant properties fixed array for a materialized object 219 // Populate the constant properties fixed array for a materialized object
219 // literal. 220 // literal.
220 void BuildObjectLiteralConstantProperties( 221 void BuildObjectLiteralConstantProperties(
221 ZoneList<ObjectLiteral::Property*>* properties, 222 ZoneList<ObjectLiteral::Property*>* properties,
222 Handle<FixedArray> constants, 223 Handle<FixedArray> constants,
223 bool* is_simple, 224 bool* is_simple,
224 bool* fast_elements, 225 bool* fast_elements,
225 int* depth); 226 int* depth);
226 227
227 // Populate the literals fixed array for a materialized array literal. 228 // Populate the literals fixed array for a materialized array literal.
(...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after
1209 top_scope_(NULL), 1210 top_scope_(NULL),
1210 with_nesting_level_(0), 1211 with_nesting_level_(0),
1211 temp_scope_(NULL), 1212 temp_scope_(NULL),
1212 target_stack_(NULL), 1213 target_stack_(NULL),
1213 allow_natives_syntax_(allow_natives_syntax), 1214 allow_natives_syntax_(allow_natives_syntax),
1214 extension_(extension), 1215 extension_(extension),
1215 factory_(factory), 1216 factory_(factory),
1216 log_(log), 1217 log_(log),
1217 is_pre_parsing_(is_pre_parsing == PREPARSE), 1218 is_pre_parsing_(is_pre_parsing == PREPARSE),
1218 pre_data_(pre_data), 1219 pre_data_(pre_data),
1219 seen_loop_stmt_(false),
1220 fni_(NULL) { 1220 fni_(NULL) {
1221 } 1221 }
1222 1222
1223 1223
1224 bool Parser::PreParseProgram(Handle<String> source, 1224 bool Parser::PreParseProgram(Handle<String> source,
1225 unibrow::CharacterStream* stream) { 1225 unibrow::CharacterStream* stream) {
1226 HistogramTimerScope timer(&Counters::pre_parse); 1226 HistogramTimerScope timer(&Counters::pre_parse);
1227 AssertNoZoneAllocation assert_no_zone_allocation; 1227 AssertNoZoneAllocation assert_no_zone_allocation;
1228 AssertNoAllocation assert_no_allocation; 1228 AssertNoAllocation assert_no_allocation;
1229 NoHandleAllocation no_handle_allocation; 1229 NoHandleAllocation no_handle_allocation;
(...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after
2670 if (cond != NULL) cond->set_is_loop_condition(true); 2670 if (cond != NULL) cond->set_is_loop_condition(true);
2671 Expect(Token::RPAREN, CHECK_OK); 2671 Expect(Token::RPAREN, CHECK_OK);
2672 2672
2673 // Allow do-statements to be terminated with and without 2673 // Allow do-statements to be terminated with and without
2674 // semi-colons. This allows code such as 'do;while(0)return' to 2674 // semi-colons. This allows code such as 'do;while(0)return' to
2675 // parse, which would not be the case if we had used the 2675 // parse, which would not be the case if we had used the
2676 // ExpectSemicolon() functionality here. 2676 // ExpectSemicolon() functionality here.
2677 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON); 2677 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
2678 2678
2679 if (loop != NULL) loop->Initialize(cond, body); 2679 if (loop != NULL) loop->Initialize(cond, body);
2680
2681 seen_loop_stmt_ = true;
2682
2683 return loop; 2680 return loop;
2684 } 2681 }
2685 2682
2686 2683
2687 WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) { 2684 WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
2688 // WhileStatement :: 2685 // WhileStatement ::
2689 // 'while' '(' Expression ')' Statement 2686 // 'while' '(' Expression ')' Statement
2690 2687
2691 WhileStatement* loop = NEW(WhileStatement(labels)); 2688 WhileStatement* loop = NEW(WhileStatement(labels));
2692 Target target(this, loop); 2689 Target target(this, loop);
2693 2690
2694 Expect(Token::WHILE, CHECK_OK); 2691 Expect(Token::WHILE, CHECK_OK);
2695 Expect(Token::LPAREN, CHECK_OK); 2692 Expect(Token::LPAREN, CHECK_OK);
2696 Expression* cond = ParseExpression(true, CHECK_OK); 2693 Expression* cond = ParseExpression(true, CHECK_OK);
2697 if (cond != NULL) cond->set_is_loop_condition(true); 2694 if (cond != NULL) cond->set_is_loop_condition(true);
2698 Expect(Token::RPAREN, CHECK_OK); 2695 Expect(Token::RPAREN, CHECK_OK);
2699 Statement* body = ParseStatement(NULL, CHECK_OK); 2696 Statement* body = ParseStatement(NULL, CHECK_OK);
2700 2697
2701 if (loop != NULL) loop->Initialize(cond, body); 2698 if (loop != NULL) loop->Initialize(cond, body);
2702
2703 seen_loop_stmt_ = true;
2704
2705 return loop; 2699 return loop;
2706 } 2700 }
2707 2701
2708 2702
2709 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { 2703 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2710 // ForStatement :: 2704 // ForStatement ::
2711 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement 2705 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2712 2706
2713 Statement* init = NULL; 2707 Statement* init = NULL;
2714 2708
(...skipping 13 matching lines...) Expand all
2728 Expect(Token::RPAREN, CHECK_OK); 2722 Expect(Token::RPAREN, CHECK_OK);
2729 2723
2730 Statement* body = ParseStatement(NULL, CHECK_OK); 2724 Statement* body = ParseStatement(NULL, CHECK_OK);
2731 if (is_pre_parsing_) { 2725 if (is_pre_parsing_) {
2732 return NULL; 2726 return NULL;
2733 } else { 2727 } else {
2734 loop->Initialize(each, enumerable, body); 2728 loop->Initialize(each, enumerable, body);
2735 Block* result = NEW(Block(NULL, 2, false)); 2729 Block* result = NEW(Block(NULL, 2, false));
2736 result->AddStatement(variable_statement); 2730 result->AddStatement(variable_statement);
2737 result->AddStatement(loop); 2731 result->AddStatement(loop);
2738
2739 seen_loop_stmt_ = true;
2740
2741 // Parsed for-in loop w/ variable/const declaration. 2732 // Parsed for-in loop w/ variable/const declaration.
2742 return result; 2733 return result;
2743 } 2734 }
2744 2735
2745 } else { 2736 } else {
2746 init = variable_statement; 2737 init = variable_statement;
2747 } 2738 }
2748 2739
2749 } else { 2740 } else {
2750 Expression* expression = ParseExpression(false, CHECK_OK); 2741 Expression* expression = ParseExpression(false, CHECK_OK);
2751 if (peek() == Token::IN) { 2742 if (peek() == Token::IN) {
2752 // Signal a reference error if the expression is an invalid 2743 // Signal a reference error if the expression is an invalid
2753 // left-hand side expression. We could report this as a syntax 2744 // left-hand side expression. We could report this as a syntax
2754 // error here but for compatibility with JSC we choose to report 2745 // error here but for compatibility with JSC we choose to report
2755 // the error at runtime. 2746 // the error at runtime.
2756 if (expression == NULL || !expression->IsValidLeftHandSide()) { 2747 if (expression == NULL || !expression->IsValidLeftHandSide()) {
2757 Handle<String> type = Factory::invalid_lhs_in_for_in_symbol(); 2748 Handle<String> type = Factory::invalid_lhs_in_for_in_symbol();
2758 expression = NewThrowReferenceError(type); 2749 expression = NewThrowReferenceError(type);
2759 } 2750 }
2760 ForInStatement* loop = NEW(ForInStatement(labels)); 2751 ForInStatement* loop = NEW(ForInStatement(labels));
2761 Target target(this, loop); 2752 Target target(this, loop);
2762 2753
2763 Expect(Token::IN, CHECK_OK); 2754 Expect(Token::IN, CHECK_OK);
2764 Expression* enumerable = ParseExpression(true, CHECK_OK); 2755 Expression* enumerable = ParseExpression(true, CHECK_OK);
2765 Expect(Token::RPAREN, CHECK_OK); 2756 Expect(Token::RPAREN, CHECK_OK);
2766 2757
2767 Statement* body = ParseStatement(NULL, CHECK_OK); 2758 Statement* body = ParseStatement(NULL, CHECK_OK);
2768 if (loop) loop->Initialize(expression, enumerable, body); 2759 if (loop) loop->Initialize(expression, enumerable, body);
2769
2770 seen_loop_stmt_ = true;
2771
2772 // Parsed for-in loop. 2760 // Parsed for-in loop.
2773 return loop; 2761 return loop;
2774 2762
2775 } else { 2763 } else {
2776 init = NEW(ExpressionStatement(expression)); 2764 init = NEW(ExpressionStatement(expression));
2777 } 2765 }
2778 } 2766 }
2779 } 2767 }
2780 2768
2781 // Standard 'for' loop 2769 // Standard 'for' loop
(...skipping 10 matching lines...) Expand all
2792 } 2780 }
2793 Expect(Token::SEMICOLON, CHECK_OK); 2781 Expect(Token::SEMICOLON, CHECK_OK);
2794 2782
2795 Statement* next = NULL; 2783 Statement* next = NULL;
2796 if (peek() != Token::RPAREN) { 2784 if (peek() != Token::RPAREN) {
2797 Expression* exp = ParseExpression(true, CHECK_OK); 2785 Expression* exp = ParseExpression(true, CHECK_OK);
2798 next = NEW(ExpressionStatement(exp)); 2786 next = NEW(ExpressionStatement(exp));
2799 } 2787 }
2800 Expect(Token::RPAREN, CHECK_OK); 2788 Expect(Token::RPAREN, CHECK_OK);
2801 2789
2802 seen_loop_stmt_ = false;
2803
2804 Statement* body = ParseStatement(NULL, CHECK_OK); 2790 Statement* body = ParseStatement(NULL, CHECK_OK);
2805
2806 // Mark this loop if it is an inner loop.
2807 if (loop && !seen_loop_stmt_) loop->set_peel_this_loop(true);
2808
2809 if (loop) loop->Initialize(init, cond, next, body); 2791 if (loop) loop->Initialize(init, cond, next, body);
2810
2811 seen_loop_stmt_ = true;
2812
2813 return loop; 2792 return loop;
2814 } 2793 }
2815 2794
2816 2795
2817 // Precedence = 1 2796 // Precedence = 1
2818 Expression* Parser::ParseExpression(bool accept_IN, bool* ok) { 2797 Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
2819 // Expression :: 2798 // Expression ::
2820 // AssignmentExpression 2799 // AssignmentExpression
2821 // Expression ',' AssignmentExpression 2800 // Expression ',' AssignmentExpression
2822 2801
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
2994 if (static_cast<double>(y_int) == y_val && 2973 if (static_cast<double>(y_int) == y_val &&
2995 (IsPowerOf2(y_int) || IsPowerOf2(-y_int)) && 2974 (IsPowerOf2(y_int) || IsPowerOf2(-y_int)) &&
2996 (y_int > 2 || y_int < -2)) { 2975 (y_int > 2 || y_int < -2)) {
2997 y = NewNumberLiteral(1 / y_val); 2976 y = NewNumberLiteral(1 / y_val);
2998 op = Token::MUL; 2977 op = Token::MUL;
2999 } 2978 }
3000 } 2979 }
3001 2980
3002 // For now we distinguish between comparisons and other binary 2981 // For now we distinguish between comparisons and other binary
3003 // operations. (We could combine the two and get rid of this 2982 // operations. (We could combine the two and get rid of this
3004 // code an AST node eventually.) 2983 // code and AST node eventually.)
3005 if (Token::IsCompareOp(op)) { 2984 if (Token::IsCompareOp(op)) {
3006 // We have a comparison. 2985 // We have a comparison.
3007 Token::Value cmp = op; 2986 Token::Value cmp = op;
3008 switch (op) { 2987 switch (op) {
3009 case Token::NE: cmp = Token::EQ; break; 2988 case Token::NE: cmp = Token::EQ; break;
3010 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; 2989 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
3011 default: break; 2990 default: break;
3012 } 2991 }
3013 x = NEW(CompareOperation(cmp, x, y)); 2992 x = NewCompareNode(cmp, x, y);
3014 if (cmp != op) { 2993 if (cmp != op) {
3015 // The comparison was negated - add a NOT. 2994 // The comparison was negated - add a NOT.
3016 x = NEW(UnaryOperation(Token::NOT, x)); 2995 x = NEW(UnaryOperation(Token::NOT, x));
3017 } 2996 }
3018 2997
3019 } else { 2998 } else {
3020 // We have a "normal" binary operation. 2999 // We have a "normal" binary operation.
3021 x = NEW(BinaryOperation(op, x, y)); 3000 x = NEW(BinaryOperation(op, x, y));
3022 } 3001 }
3023 } 3002 }
3024 } 3003 }
3025 return x; 3004 return x;
3026 } 3005 }
3027 3006
3028 3007
3008 Expression* Parser::NewCompareNode(Token::Value op,
3009 Expression* x,
3010 Expression* y) {
3011 ASSERT(op != Token::NE && op != Token::NE_STRICT);
3012 if (!is_pre_parsing_ && (op == Token::EQ || op == Token::EQ_STRICT)) {
3013 bool is_strict = (op == Token::EQ_STRICT);
3014 Literal* x_literal = x->AsLiteral();
3015 if (x_literal != NULL && x_literal->IsNull()) {
3016 return NEW(CompareToNull(is_strict, y));
3017 }
3018
3019 Literal* y_literal = y->AsLiteral();
3020 if (y_literal != NULL && y_literal->IsNull()) {
3021 return NEW(CompareToNull(is_strict, x));
3022 }
3023 }
3024 return NEW(CompareOperation(op, x, y));
3025 }
3026
3027
3029 Expression* Parser::ParseUnaryExpression(bool* ok) { 3028 Expression* Parser::ParseUnaryExpression(bool* ok) {
3030 // UnaryExpression :: 3029 // UnaryExpression ::
3031 // PostfixExpression 3030 // PostfixExpression
3032 // 'delete' UnaryExpression 3031 // 'delete' UnaryExpression
3033 // 'void' UnaryExpression 3032 // 'void' UnaryExpression
3034 // 'typeof' UnaryExpression 3033 // 'typeof' UnaryExpression
3035 // '++' UnaryExpression 3034 // '++' UnaryExpression
3036 // '--' UnaryExpression 3035 // '--' UnaryExpression
3037 // '+' UnaryExpression 3036 // '+' UnaryExpression
3038 // '-' UnaryExpression 3037 // '-' UnaryExpression
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
3836 return result.elements(); 3835 return result.elements();
3837 } 3836 }
3838 3837
3839 3838
3840 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, 3839 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
3841 int function_token_position, 3840 int function_token_position,
3842 FunctionLiteralType type, 3841 FunctionLiteralType type,
3843 bool* ok) { 3842 bool* ok) {
3844 // Function :: 3843 // Function ::
3845 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3844 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3846
3847 // Reset flag used for inner loop detection.
3848 seen_loop_stmt_ = false;
3849
3850 bool is_named = !var_name.is_null(); 3845 bool is_named = !var_name.is_null();
3851 3846
3852 // The name associated with this function. If it's a function expression, 3847 // The name associated with this function. If it's a function expression,
3853 // this is the actual function name, otherwise this is the name of the 3848 // this is the actual function name, otherwise this is the name of the
3854 // variable declared and initialized with the function (expression). In 3849 // variable declared and initialized with the function (expression). In
3855 // that case, we don't have a function name (it's empty). 3850 // that case, we don't have a function name (it's empty).
3856 Handle<String> name = is_named ? var_name : factory()->EmptySymbol(); 3851 Handle<String> name = is_named ? var_name : factory()->EmptySymbol();
3857 // The function name, if any. 3852 // The function name, if any.
3858 Handle<String> function_name = factory()->EmptySymbol(); 3853 Handle<String> function_name = factory()->EmptySymbol();
3859 if (is_named && (type == EXPRESSION || type == NESTED)) { 3854 if (is_named && (type == EXPRESSION || type == NESTED)) {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
3958 only_simple_this_property_assignments, 3953 only_simple_this_property_assignments,
3959 this_property_assignments, 3954 this_property_assignments,
3960 num_parameters, 3955 num_parameters,
3961 start_pos, 3956 start_pos,
3962 end_pos, 3957 end_pos,
3963 function_name->length() > 0)); 3958 function_name->length() > 0));
3964 if (!is_pre_parsing_) { 3959 if (!is_pre_parsing_) {
3965 function_literal->set_function_token_position(function_token_position); 3960 function_literal->set_function_token_position(function_token_position);
3966 } 3961 }
3967 3962
3968 // Set flag for inner loop detection. We treat loops that contain a function
3969 // literal not as inner loops because we avoid duplicating function literals
3970 // when peeling or unrolling such a loop.
3971 seen_loop_stmt_ = true;
3972
3973 if (fni_ != NULL && !is_named) fni_->AddFunction(function_literal); 3963 if (fni_ != NULL && !is_named) fni_->AddFunction(function_literal);
3974 return function_literal; 3964 return function_literal;
3975 } 3965 }
3976 } 3966 }
3977 3967
3978 3968
3979 Expression* Parser::ParseV8Intrinsic(bool* ok) { 3969 Expression* Parser::ParseV8Intrinsic(bool* ok) {
3980 // CallRuntime :: 3970 // CallRuntime ::
3981 // '%' Identifier Arguments 3971 // '%' Identifier Arguments
3982 3972
(...skipping 1323 matching lines...) Expand 10 before | Expand all | Expand 10 after
5306 parser.ParseLazy(script_source, name, 5296 parser.ParseLazy(script_source, name,
5307 start_position, end_position, is_expression); 5297 start_position, end_position, is_expression);
5308 return result; 5298 return result;
5309 } 5299 }
5310 5300
5311 5301
5312 #undef NEW 5302 #undef NEW
5313 5303
5314 5304
5315 } } // namespace v8::internal 5305 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ia32/full-codegen-ia32.cc ('k') | src/prettyprinter.h » ('j') | src/x64/full-codegen-x64.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698