Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "src/ast.h" | |
| 6 #include "src/parser.h" | |
| 7 #include "src/pattern-rewriter.h" | |
| 8 | |
| 9 namespace v8 { | |
| 10 | |
| 11 namespace internal { | |
| 12 | |
| 13 | |
| 14 bool Parser::PatternRewriter::IsSingleVariableBinding() const { | |
| 15 return pattern_->IsVariableProxy(); | |
| 16 } | |
| 17 | |
| 18 | |
| 19 const AstRawString* Parser::PatternRewriter::SingleName() const { | |
| 20 DCHECK(IsSingleVariableBinding()); | |
| 21 return pattern_->AsVariableProxy()->raw_name(); | |
| 22 } | |
| 23 | |
| 24 | |
| 25 void Parser::PatternRewriter::DeclareAndInitializeVariables(Expression* value, | |
| 26 int* nvars, | |
| 27 bool* ok) { | |
| 28 ok_ = ok; | |
| 29 nvars_ = nvars; | |
| 30 RecurseIntoSubpattern(pattern_, value); | |
| 31 ok_ = nullptr; | |
| 32 nvars_ = nullptr; | |
| 33 } | |
| 34 | |
| 35 | |
| 36 void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) { | |
| 37 Expression* value = current_value_; | |
| 38 decl_->scope->RemoveUnresolved(pattern->AsVariableProxy()); | |
| 39 | |
| 40 // Declare variable. | |
| 41 // Note that we *always* must treat the initial value via a separate init | |
| 42 // assignment for variables and constants because the value must be assigned | |
| 43 // when the variable is encountered in the source. But the variable/constant | |
| 44 // is declared (and set to 'undefined') upon entering the function within | |
| 45 // which the variable or constant is declared. Only function variables have | |
| 46 // an initial value in the declaration (because they are initialized upon | |
| 47 // entering the function). | |
| 48 // | |
| 49 // If we have a legacy const declaration, in an inner scope, the proxy | |
| 50 // is always bound to the declared variable (independent of possibly | |
| 51 // surrounding 'with' statements). | |
| 52 // For let/const declarations in harmony mode, we can also immediately | |
| 53 // pre-resolve the proxy because it resides in the same scope as the | |
| 54 // declaration. | |
| 55 Parser* parser = decl_->parser; | |
| 56 const AstRawString* name = pattern->raw_name(); | |
| 57 VariableProxy* proxy = parser->NewUnresolved(name, decl_->mode); | |
| 58 Declaration* declaration = factory()->NewVariableDeclaration( | |
| 59 proxy, decl_->mode, decl_->scope, decl_->pos); | |
| 60 Variable* var = parser->Declare(declaration, decl_->mode != VAR, ok_); | |
| 61 if (!*ok_) return; | |
| 62 DCHECK_NOT_NULL(var); | |
| 63 DCHECK(!proxy->is_resolved() || proxy->var() == var); | |
| 64 var->set_initializer_position(decl_->initializer_position); | |
| 65 (*nvars_)++; | |
| 66 if (decl_->declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { | |
| 67 parser->ReportMessage("too_many_variables"); | |
| 68 *ok_ = false; | |
| 69 return; | |
| 70 } | |
| 71 if (decl_->names) { | |
| 72 decl_->names->Add(name, zone()); | |
| 73 } | |
| 74 | |
| 75 // Initialize variables if needed. A | |
| 76 // declaration of the form: | |
| 77 // | |
| 78 // var v = x; | |
| 79 // | |
| 80 // is syntactic sugar for: | |
| 81 // | |
| 82 // var v; v = x; | |
| 83 // | |
| 84 | |
|
rossberg
2015/05/11 15:36:24
Nit: remove empty line
Dmitry Lomov (no reviews)
2015/05/11 15:44:15
Done.
| |
| 85 // In particular, we need to re-lookup 'v' (in scope_, not | |
| 86 // declaration_scope) as it may be a different 'v' than the 'v' in the | |
| 87 // declaration (e.g., if we are inside a 'with' statement or 'catch' | |
| 88 // block). | |
| 89 // | |
| 90 // However, note that const declarations are different! A const | |
| 91 // declaration of the form: | |
| 92 // | |
| 93 // const c = x; | |
| 94 // | |
| 95 // is *not* syntactic sugar for: | |
| 96 // | |
| 97 // const c; c = x; | |
| 98 // | |
| 99 // The "variable" c initialized to x is the same as the declared | |
| 100 // one - there is no re-lookup (see the last parameter of the | |
| 101 // Declare() call above). | |
| 102 Scope* initialization_scope = | |
| 103 decl_->is_const ? decl_->declaration_scope : decl_->scope; | |
| 104 | |
| 105 | |
| 106 // Global variable declarations must be compiled in a specific | |
| 107 // way. When the script containing the global variable declaration | |
| 108 // is entered, the global variable must be declared, so that if it | |
| 109 // doesn't exist (on the global object itself, see ES5 errata) it | |
| 110 // gets created with an initial undefined value. This is handled | |
| 111 // by the declarations part of the function representing the | |
| 112 // top-level global code; see Runtime::DeclareGlobalVariable. If | |
| 113 // it already exists (in the object or in a prototype), it is | |
| 114 // *not* touched until the variable declaration statement is | |
| 115 // executed. | |
| 116 // | |
| 117 // Executing the variable declaration statement will always | |
| 118 // guarantee to give the global object an own property. | |
| 119 // This way, global variable declarations can shadow | |
| 120 // properties in the prototype chain, but only after the variable | |
| 121 // declaration statement has been executed. This is important in | |
| 122 // browsers where the global object (window) has lots of | |
| 123 // properties defined in prototype objects. | |
| 124 if (initialization_scope->is_script_scope() && | |
| 125 !IsLexicalVariableMode(decl_->mode)) { | |
| 126 // Compute the arguments for the runtime | |
| 127 // call.test-parsing/InitializedDeclarationsInStrictForOfError | |
| 128 ZoneList<Expression*>* arguments = | |
| 129 new (zone()) ZoneList<Expression*>(3, zone()); | |
| 130 // We have at least 1 parameter. | |
| 131 arguments->Add(factory()->NewStringLiteral(name, decl_->pos), zone()); | |
| 132 CallRuntime* initialize; | |
| 133 | |
| 134 if (decl_->is_const) { | |
| 135 arguments->Add(value, zone()); | |
| 136 value = NULL; // zap the value to avoid the unnecessary assignment | |
| 137 | |
| 138 // Construct the call to Runtime_InitializeConstGlobal | |
| 139 // and add it to the initialization statement block. | |
| 140 // Note that the function does different things depending on | |
| 141 // the number of arguments (1 or 2). | |
| 142 initialize = factory()->NewCallRuntime( | |
| 143 ast_value_factory()->initialize_const_global_string(), | |
| 144 Runtime::FunctionForId(Runtime::kInitializeConstGlobal), arguments, | |
| 145 decl_->pos); | |
| 146 } else { | |
| 147 // Add language mode. | |
| 148 // We may want to pass singleton to avoid Literal allocations. | |
| 149 LanguageMode language_mode = initialization_scope->language_mode(); | |
| 150 arguments->Add(factory()->NewNumberLiteral(language_mode, decl_->pos), | |
| 151 zone()); | |
| 152 | |
| 153 // Be careful not to assign a value to the global variable if | |
| 154 // we're in a with. The initialization value should not | |
| 155 // necessarily be stored in the global object in that case, | |
| 156 // which is why we need to generate a separate assignment node. | |
| 157 if (value != NULL && !inside_with()) { | |
| 158 arguments->Add(value, zone()); | |
| 159 value = NULL; // zap the value to avoid the unnecessary assignment | |
| 160 // Construct the call to Runtime_InitializeVarGlobal | |
| 161 // and add it to the initialization statement block. | |
| 162 initialize = factory()->NewCallRuntime( | |
| 163 ast_value_factory()->initialize_var_global_string(), | |
| 164 Runtime::FunctionForId(Runtime::kInitializeVarGlobal), arguments, | |
| 165 decl_->pos); | |
| 166 } else { | |
| 167 initialize = NULL; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 if (initialize != NULL) { | |
| 172 decl_->block->AddStatement( | |
| 173 factory()->NewExpressionStatement(initialize, RelocInfo::kNoPosition), | |
| 174 zone()); | |
| 175 } | |
| 176 } else if (decl_->needs_init) { | |
| 177 // Constant initializations always assign to the declared constant which | |
| 178 // is always at the function scope level. This is only relevant for | |
| 179 // dynamically looked-up variables and constants (the | |
| 180 // start context for constant lookups is always the function context, | |
| 181 // while it is the top context for var declared variables). Sigh... | |
| 182 // For 'let' and 'const' declared variables in harmony mode the | |
| 183 // initialization also always assigns to the declared variable. | |
| 184 DCHECK_NOT_NULL(proxy); | |
| 185 DCHECK_NOT_NULL(proxy->var()); | |
| 186 DCHECK_NOT_NULL(value); | |
| 187 Assignment* assignment = | |
| 188 factory()->NewAssignment(decl_->init_op, proxy, value, decl_->pos); | |
| 189 decl_->block->AddStatement( | |
| 190 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), | |
| 191 zone()); | |
| 192 value = NULL; | |
| 193 } | |
| 194 | |
| 195 // Add an assignment node to the initialization statement block if we still | |
| 196 // have a pending initialization value. | |
| 197 if (value != NULL) { | |
| 198 DCHECK(decl_->mode == VAR); | |
| 199 // 'var' initializations are simply assignments (with all the consequences | |
| 200 // if they are inside a 'with' statement - they may change a 'with' object | |
| 201 // property). | |
| 202 VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name); | |
| 203 Assignment* assignment = | |
| 204 factory()->NewAssignment(decl_->init_op, proxy, value, decl_->pos); | |
| 205 decl_->block->AddStatement( | |
| 206 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), | |
| 207 zone()); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 | |
| 212 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { | |
| 213 auto temp = decl_->declaration_scope->NewTemporary( | |
| 214 ast_value_factory()->empty_string()); | |
| 215 auto assignment = | |
| 216 factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp), | |
| 217 current_value_, RelocInfo::kNoPosition); | |
| 218 decl_->block->AddStatement( | |
| 219 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), | |
| 220 zone()); | |
| 221 for (ObjectLiteralProperty* property : *pattern->properties()) { | |
| 222 // TODO(dslomov): computed property names. | |
| 223 RecurseIntoSubpattern( | |
| 224 property->value(), | |
| 225 factory()->NewProperty(factory()->NewVariableProxy(temp), | |
| 226 property->key(), RelocInfo::kNoPosition)); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 | |
| 231 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { | |
| 232 // TODO(dslomov): implement. | |
| 233 } | |
| 234 | |
| 235 | |
| 236 void Parser::PatternRewriter::VisitAssignment(Assignment* node) { | |
| 237 // TODO(dslomov): implement. | |
| 238 } | |
| 239 | |
| 240 | |
| 241 void Parser::PatternRewriter::VisitSpread(Spread* node) { | |
| 242 // TODO(dslomov): implement. | |
| 243 } | |
| 244 | |
| 245 | |
| 246 // =============== UNREACHABLE ============================= | |
| 247 | |
| 248 void Parser::PatternRewriter::Visit(AstNode* node) { UNREACHABLE(); } | |
| 249 | |
| 250 #define NOT_A_PATTERN(Node) \ | |
| 251 void Parser::PatternRewriter::Visit##Node(v8::internal::Node*) { \ | |
| 252 UNREACHABLE(); \ | |
| 253 } | |
| 254 | |
| 255 NOT_A_PATTERN(BinaryOperation) | |
| 256 NOT_A_PATTERN(Block) | |
| 257 NOT_A_PATTERN(BreakStatement) | |
| 258 NOT_A_PATTERN(Call) | |
| 259 NOT_A_PATTERN(CallNew) | |
| 260 NOT_A_PATTERN(CallRuntime) | |
| 261 NOT_A_PATTERN(CaseClause) | |
| 262 NOT_A_PATTERN(ClassLiteral) | |
| 263 NOT_A_PATTERN(CompareOperation) | |
| 264 NOT_A_PATTERN(Conditional) | |
| 265 NOT_A_PATTERN(ContinueStatement) | |
| 266 NOT_A_PATTERN(CountOperation) | |
| 267 NOT_A_PATTERN(DebuggerStatement) | |
| 268 NOT_A_PATTERN(DoWhileStatement) | |
| 269 NOT_A_PATTERN(EmptyStatement) | |
| 270 NOT_A_PATTERN(ExportDeclaration) | |
| 271 NOT_A_PATTERN(ExpressionStatement) | |
| 272 NOT_A_PATTERN(ForInStatement) | |
| 273 NOT_A_PATTERN(ForOfStatement) | |
| 274 NOT_A_PATTERN(ForStatement) | |
| 275 NOT_A_PATTERN(FunctionDeclaration) | |
| 276 NOT_A_PATTERN(FunctionLiteral) | |
| 277 NOT_A_PATTERN(IfStatement) | |
| 278 NOT_A_PATTERN(ImportDeclaration) | |
| 279 NOT_A_PATTERN(Literal) | |
| 280 NOT_A_PATTERN(NativeFunctionLiteral) | |
| 281 NOT_A_PATTERN(Property) | |
| 282 NOT_A_PATTERN(RegExpLiteral) | |
| 283 NOT_A_PATTERN(ReturnStatement) | |
| 284 NOT_A_PATTERN(SuperReference) | |
| 285 NOT_A_PATTERN(SwitchStatement) | |
| 286 NOT_A_PATTERN(ThisFunction) | |
| 287 NOT_A_PATTERN(Throw) | |
| 288 NOT_A_PATTERN(TryCatchStatement) | |
| 289 NOT_A_PATTERN(TryFinallyStatement) | |
| 290 NOT_A_PATTERN(UnaryOperation) | |
| 291 NOT_A_PATTERN(VariableDeclaration) | |
| 292 NOT_A_PATTERN(WhileStatement) | |
| 293 NOT_A_PATTERN(WithStatement) | |
| 294 NOT_A_PATTERN(Yield) | |
| 295 | |
| 296 #undef NOT_A_PATTERN | |
| 297 } | |
| 298 } // namespace v8::internal | |
| OLD | NEW |