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

Side by Side Diff: src/parser.cc

Issue 1060913005: [strong] Stricter check for referring to other classes inside methods. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: . Created 5 years, 8 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
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/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after
1004 1004
1005 // Enters 'scope'. 1005 // Enters 'scope'.
1006 AstNodeFactory function_factory(ast_value_factory()); 1006 AstNodeFactory function_factory(ast_value_factory());
1007 FunctionState function_state(&function_state_, &scope_, scope, 1007 FunctionState function_state(&function_state_, &scope_, scope,
1008 kNormalFunction, &function_factory); 1008 kNormalFunction, &function_factory);
1009 1009
1010 scope_->SetLanguageMode(info->language_mode()); 1010 scope_->SetLanguageMode(info->language_mode());
1011 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); 1011 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
1012 bool ok = true; 1012 bool ok = true;
1013 int beg_pos = scanner()->location().beg_pos; 1013 int beg_pos = scanner()->location().beg_pos;
1014 function_state.set_consecutive_class_declaration_batch_start(beg_pos);
1014 if (info->is_module()) { 1015 if (info->is_module()) {
1015 DCHECK(allow_harmony_modules()); 1016 DCHECK(allow_harmony_modules());
1016 ParseModuleItemList(body, &ok); 1017 ParseModuleItemList(body, &ok);
1017 } else { 1018 } else {
1018 Scope* eval_scope = nullptr; 1019 Scope* eval_scope = nullptr;
1019 ParseStatementList(body, Token::EOS, info->is_eval(), &eval_scope, &ok); 1020 ParseStatementList(body, Token::EOS, info->is_eval(), &eval_scope, &ok);
1020 if (eval_scope != nullptr) 1021 if (eval_scope != nullptr)
1021 eval_scope->set_end_position(scanner()->peek_location().beg_pos); 1022 eval_scope->set_end_position(scanner()->peek_location().beg_pos);
1022 } 1023 }
1023 1024
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 // Ok to use Isolate here, since lazy function parsing is only done in the 1124 // Ok to use Isolate here, since lazy function parsing is only done in the
1124 // main thread. 1125 // main thread.
1125 DCHECK(parsing_on_main_thread_); 1126 DCHECK(parsing_on_main_thread_);
1126 scope = Scope::DeserializeScopeChain(isolate, zone(), 1127 scope = Scope::DeserializeScopeChain(isolate, zone(),
1127 info->closure()->context(), scope); 1128 info->closure()->context(), scope);
1128 } 1129 }
1129 original_scope_ = scope; 1130 original_scope_ = scope;
1130 AstNodeFactory function_factory(ast_value_factory()); 1131 AstNodeFactory function_factory(ast_value_factory());
1131 FunctionState function_state(&function_state_, &scope_, scope, 1132 FunctionState function_state(&function_state_, &scope_, scope,
1132 shared_info->kind(), &function_factory); 1133 shared_info->kind(), &function_factory);
1134 function_state.set_consecutive_class_declaration_batch_start(
1135 scanner()->location().beg_pos);
1133 DCHECK(is_sloppy(scope->language_mode()) || 1136 DCHECK(is_sloppy(scope->language_mode()) ||
1134 is_strict(info->language_mode())); 1137 is_strict(info->language_mode()));
1135 DCHECK(info->language_mode() == shared_info->language_mode()); 1138 DCHECK(info->language_mode() == shared_info->language_mode());
1136 scope->SetLanguageMode(shared_info->language_mode()); 1139 scope->SetLanguageMode(shared_info->language_mode());
1137 FunctionLiteral::FunctionType function_type = shared_info->is_expression() 1140 FunctionLiteral::FunctionType function_type = shared_info->is_expression()
1138 ? (shared_info->is_anonymous() 1141 ? (shared_info->is_anonymous()
1139 ? FunctionLiteral::ANONYMOUS_EXPRESSION 1142 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1140 : FunctionLiteral::NAMED_EXPRESSION) 1143 : FunctionLiteral::NAMED_EXPRESSION)
1141 : FunctionLiteral::DECLARATION; 1144 : FunctionLiteral::DECLARATION;
1142 bool ok = true; 1145 bool ok = true;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 return 0; 1310 return 0;
1308 } 1311 }
1309 1312
1310 1313
1311 Statement* Parser::ParseStatementListItem(bool* ok) { 1314 Statement* Parser::ParseStatementListItem(bool* ok) {
1312 // (Ecma 262 6th Edition, 13.1): 1315 // (Ecma 262 6th Edition, 13.1):
1313 // StatementListItem: 1316 // StatementListItem:
1314 // Statement 1317 // Statement
1315 // Declaration 1318 // Declaration
1316 1319
1320 if (peek() != Token::CLASS) {
rossberg 2015/04/20 11:15:25 Hm, I'm not sure I fully understand the logic. You
marja 2015/04/20 15:58:22 Done + added a test for the counterexample (which
1321 // No more classes follow; reset the start position for the consecutive
1322 // class declaration batch.
1323 function_state_->set_consecutive_class_declaration_batch_start(
1324 scanner()->peek_location().beg_pos);
1325 }
1326
1317 switch (peek()) { 1327 switch (peek()) {
1318 case Token::FUNCTION: 1328 case Token::FUNCTION:
1319 return ParseFunctionDeclaration(NULL, ok); 1329 return ParseFunctionDeclaration(NULL, ok);
1320 case Token::CLASS: 1330 case Token::CLASS:
1321 return ParseClassDeclaration(NULL, ok); 1331 return ParseClassDeclaration(NULL, ok);
1322 case Token::CONST: 1332 case Token::CONST:
1323 case Token::VAR: 1333 case Token::VAR:
1324 return ParseVariableStatement(kStatementListItem, NULL, ok); 1334 return ParseVariableStatement(kStatementListItem, NULL, ok);
1325 case Token::LET: 1335 case Token::LET:
1326 if (is_strict(language_mode())) { 1336 if (is_strict(language_mode())) {
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 if (declaration_scope->is_function_scope() || 1949 if (declaration_scope->is_function_scope() ||
1940 declaration_scope->is_strict_eval_scope() || 1950 declaration_scope->is_strict_eval_scope() ||
1941 declaration_scope->is_block_scope() || 1951 declaration_scope->is_block_scope() ||
1942 declaration_scope->is_module_scope() || 1952 declaration_scope->is_module_scope() ||
1943 declaration_scope->is_script_scope()) { 1953 declaration_scope->is_script_scope()) {
1944 // Declare the variable in the declaration scope. 1954 // Declare the variable in the declaration scope.
1945 var = declaration_scope->LookupLocal(name); 1955 var = declaration_scope->LookupLocal(name);
1946 if (var == NULL) { 1956 if (var == NULL) {
1947 // Declare the name. 1957 // Declare the name.
1948 Variable::Kind kind = Variable::NORMAL; 1958 Variable::Kind kind = Variable::NORMAL;
1959 int consecutive_declaration_batch_start = -1;
1949 if (declaration->IsFunctionDeclaration()) { 1960 if (declaration->IsFunctionDeclaration()) {
1950 kind = Variable::FUNCTION; 1961 kind = Variable::FUNCTION;
1951 } else if (declaration->IsVariableDeclaration() && 1962 } else if (declaration->IsVariableDeclaration() &&
1952 declaration->AsVariableDeclaration()->is_class_declaration()) { 1963 declaration->AsVariableDeclaration()->is_class_declaration()) {
1953 kind = Variable::CLASS; 1964 kind = Variable::CLASS;
1965 consecutive_declaration_batch_start =
1966 declaration->AsVariableDeclaration()
1967 ->consecutive_declaration_batch_start();
1954 } 1968 }
1955 var = declaration_scope->DeclareLocal( 1969 var = declaration_scope->DeclareLocal(
1956 name, mode, declaration->initialization(), kind, kNotAssigned); 1970 name, mode, declaration->initialization(), kind, kNotAssigned,
1971 consecutive_declaration_batch_start);
1957 } else if (IsLexicalVariableMode(mode) || 1972 } else if (IsLexicalVariableMode(mode) ||
1958 IsLexicalVariableMode(var->mode()) || 1973 IsLexicalVariableMode(var->mode()) ||
1959 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && 1974 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) &&
1960 !declaration_scope->is_script_scope())) { 1975 !declaration_scope->is_script_scope())) {
1961 // The name was declared in this scope before; check for conflicting 1976 // The name was declared in this scope before; check for conflicting
1962 // re-declarations. We have a conflict if either of the declarations is 1977 // re-declarations. We have a conflict if either of the declarations is
1963 // not a var (in script scope, we also have to ignore legacy const for 1978 // not a var (in script scope, we also have to ignore legacy const for
1964 // compatibility). There is similar code in runtime.cc in the Declare 1979 // compatibility). There is similar code in runtime.cc in the Declare
1965 // functions. The function CheckConflictingVarDeclarations checks for 1980 // functions. The function CheckConflictingVarDeclarations checks for
1966 // var and let bindings from different scopes whereas this is a check for 1981 // var and let bindings from different scopes whereas this is a check for
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
2166 bool is_strict_reserved = false; 2181 bool is_strict_reserved = false;
2167 const AstRawString* name = 2182 const AstRawString* name =
2168 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); 2183 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
2169 ClassLiteral* value = ParseClassLiteral(name, scanner()->location(), 2184 ClassLiteral* value = ParseClassLiteral(name, scanner()->location(),
2170 is_strict_reserved, pos, CHECK_OK); 2185 is_strict_reserved, pos, CHECK_OK);
2171 2186
2172 VariableMode mode = is_strong(language_mode()) ? CONST : LET; 2187 VariableMode mode = is_strong(language_mode()) ? CONST : LET;
2173 VariableProxy* proxy = NewUnresolved(name, mode); 2188 VariableProxy* proxy = NewUnresolved(name, mode);
2174 const bool is_class_declaration = true; 2189 const bool is_class_declaration = true;
2175 Declaration* declaration = factory()->NewVariableDeclaration( 2190 Declaration* declaration = factory()->NewVariableDeclaration(
2176 proxy, mode, scope_, pos, is_class_declaration); 2191 proxy, mode, scope_, pos, is_class_declaration,
2192 function_state_->consecutive_class_declaration_batch_start());
2177 Declare(declaration, true, CHECK_OK); 2193 Declare(declaration, true, CHECK_OK);
2178 proxy->var()->set_initializer_position(position()); 2194 proxy->var()->set_initializer_position(position());
2179 2195
2180 Token::Value init_op = 2196 Token::Value init_op =
2181 is_strong(language_mode()) ? Token::INIT_CONST : Token::INIT_LET; 2197 is_strong(language_mode()) ? Token::INIT_CONST : Token::INIT_LET;
2182 Assignment* assignment = factory()->NewAssignment(init_op, proxy, value, pos); 2198 Assignment* assignment = factory()->NewAssignment(init_op, proxy, value, pos);
2183 Statement* assignment_statement = 2199 Statement* assignment_statement =
2184 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition); 2200 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
2185 if (names) names->Add(name, zone()); 2201 if (names) names->Add(name, zone());
2186 return assignment_statement; 2202 return assignment_statement;
(...skipping 1713 matching lines...) Expand 10 before | Expand all | Expand 10 after
3900 // expressions. This also marks the FunctionState as a generator. 3916 // expressions. This also marks the FunctionState as a generator.
3901 Variable* temp = scope_->DeclarationScope()->NewTemporary( 3917 Variable* temp = scope_->DeclarationScope()->NewTemporary(
3902 ast_value_factory()->dot_generator_object_string()); 3918 ast_value_factory()->dot_generator_object_string());
3903 function_state.set_generator_object_variable(temp); 3919 function_state.set_generator_object_variable(temp);
3904 } 3920 }
3905 3921
3906 // FormalParameterList :: 3922 // FormalParameterList ::
3907 // '(' (Identifier)*[','] ')' 3923 // '(' (Identifier)*[','] ')'
3908 Expect(Token::LPAREN, CHECK_OK); 3924 Expect(Token::LPAREN, CHECK_OK);
3909 scope->set_start_position(scanner()->location().beg_pos); 3925 scope->set_start_position(scanner()->location().beg_pos);
3926 function_state.set_consecutive_class_declaration_batch_start(
3927 scanner()->location().beg_pos);
3910 3928
3911 // We don't yet know if the function will be strict, so we cannot yet 3929 // We don't yet know if the function will be strict, so we cannot yet
3912 // produce errors for parameter names or duplicates. However, we remember 3930 // produce errors for parameter names or duplicates. However, we remember
3913 // the locations of these errors if they occur and produce the errors later. 3931 // the locations of these errors if they occur and produce the errors later.
3914 Scanner::Location eval_args_loc = Scanner::Location::invalid(); 3932 Scanner::Location eval_args_loc = Scanner::Location::invalid();
3915 Scanner::Location dupe_loc = Scanner::Location::invalid(); 3933 Scanner::Location dupe_loc = Scanner::Location::invalid();
3916 Scanner::Location reserved_loc = Scanner::Location::invalid(); 3934 Scanner::Location reserved_loc = Scanner::Location::invalid();
3917 3935
3918 // Similarly for strong mode. 3936 // Similarly for strong mode.
3919 Scanner::Location undefined_loc = Scanner::Location::invalid(); 3937 Scanner::Location undefined_loc = Scanner::Location::invalid();
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
4324 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE); 4342 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
4325 block_scope->tag_as_class_scope(); 4343 block_scope->tag_as_class_scope();
4326 BlockState block_state(&scope_, block_scope); 4344 BlockState block_state(&scope_, block_scope);
4327 scope_->SetLanguageMode( 4345 scope_->SetLanguageMode(
4328 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT)); 4346 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
4329 scope_->SetScopeName(name); 4347 scope_->SetScopeName(name);
4330 4348
4331 VariableProxy* proxy = NULL; 4349 VariableProxy* proxy = NULL;
4332 if (name != NULL) { 4350 if (name != NULL) {
4333 proxy = NewUnresolved(name, CONST); 4351 proxy = NewUnresolved(name, CONST);
4334 Declaration* declaration = 4352 const bool is_class_declaration = true;
4335 factory()->NewVariableDeclaration(proxy, CONST, block_scope, pos); 4353 Declaration* declaration = factory()->NewVariableDeclaration(
4354 proxy, CONST, block_scope, pos, is_class_declaration,
4355 function_state_->consecutive_class_declaration_batch_start());
4336 Declare(declaration, true, CHECK_OK); 4356 Declare(declaration, true, CHECK_OK);
4337 } 4357 }
4338 4358
4339 Expression* extends = NULL; 4359 Expression* extends = NULL;
4340 if (Check(Token::EXTENDS)) { 4360 if (Check(Token::EXTENDS)) {
4341 block_scope->set_start_position(scanner()->location().end_pos); 4361 block_scope->set_start_position(scanner()->location().end_pos);
4342 extends = ParseLeftHandSideExpression(CHECK_OK); 4362 extends = ParseLeftHandSideExpression(CHECK_OK);
4343 } else { 4363 } else {
4344 block_scope->set_start_position(scanner()->location().end_pos); 4364 block_scope->set_start_position(scanner()->location().end_pos);
4345 } 4365 }
(...skipping 1435 matching lines...) Expand 10 before | Expand all | Expand 10 after
5781 5801
5782 Expression* Parser::SpreadCallNew(Expression* function, 5802 Expression* Parser::SpreadCallNew(Expression* function,
5783 ZoneList<v8::internal::Expression*>* args, 5803 ZoneList<v8::internal::Expression*>* args,
5784 int pos) { 5804 int pos) {
5785 args->InsertAt(0, function, zone()); 5805 args->InsertAt(0, function, zone());
5786 5806
5787 return factory()->NewCallRuntime( 5807 return factory()->NewCallRuntime(
5788 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5808 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5789 } 5809 }
5790 } } // namespace v8::internal 5810 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698