blob: 266f77d578f45902c68a011283258a9ccfe275c8 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "ast.h"
32#include "bootstrapper.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "compiler.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010035#include "func-name-inferrer.h"
Steve Block6ded16b2010-05-10 14:33:55 +010036#include "messages.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010037#include "parser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038#include "platform.h"
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080039#include "preparser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040#include "runtime.h"
Ben Murdoch3bec4d22010-07-22 14:51:16 +010041#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042#include "string-stream.h"
43
Kristian Monsen25f61362010-05-21 11:50:48 +010044#include "ast-inl.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010045
Steve Blocka7e24c12009-10-30 11:49:00 +000046namespace v8 {
47namespace internal {
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049// PositionStack is used for on-stack allocation of token positions for
50// new expressions. Please look at ParseNewExpression.
51
52class PositionStack {
53 public:
54 explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {}
55 ~PositionStack() { ASSERT(!*ok_ || is_empty()); }
56
57 class Element {
58 public:
59 Element(PositionStack* stack, int value) {
60 previous_ = stack->top();
61 value_ = value;
62 stack->set_top(this);
63 }
64
65 private:
66 Element* previous() { return previous_; }
67 int value() { return value_; }
68 friend class PositionStack;
69 Element* previous_;
70 int value_;
71 };
72
73 bool is_empty() { return top_ == NULL; }
74 int pop() {
75 ASSERT(!is_empty());
76 int result = top_->value();
77 top_ = top_->previous();
78 return result;
79 }
80
81 private:
82 Element* top() { return top_; }
83 void set_top(Element* value) { top_ = value; }
84 Element* top_;
85 bool* ok_;
86};
87
88
Steve Blocka7e24c12009-10-30 11:49:00 +000089RegExpBuilder::RegExpBuilder()
Ben Murdoch8b112d22011-06-08 16:22:53 +010090 : zone_(Isolate::Current()->zone()),
91 pending_empty_(false),
92 characters_(NULL),
93 terms_(),
94 alternatives_()
Steve Blocka7e24c12009-10-30 11:49:00 +000095#ifdef DEBUG
Ben Murdoch8b112d22011-06-08 16:22:53 +010096 , last_added_(ADD_NONE)
Steve Blocka7e24c12009-10-30 11:49:00 +000097#endif
98 {}
99
100
101void RegExpBuilder::FlushCharacters() {
102 pending_empty_ = false;
103 if (characters_ != NULL) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100104 RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector());
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 characters_ = NULL;
106 text_.Add(atom);
107 LAST(ADD_ATOM);
108 }
109}
110
111
112void RegExpBuilder::FlushText() {
113 FlushCharacters();
114 int num_text = text_.length();
115 if (num_text == 0) {
116 return;
117 } else if (num_text == 1) {
118 terms_.Add(text_.last());
119 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100120 RegExpText* text = new(zone()) RegExpText();
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 for (int i = 0; i < num_text; i++)
122 text_.Get(i)->AppendToText(text);
123 terms_.Add(text);
124 }
125 text_.Clear();
126}
127
128
129void RegExpBuilder::AddCharacter(uc16 c) {
130 pending_empty_ = false;
131 if (characters_ == NULL) {
132 characters_ = new ZoneList<uc16>(4);
133 }
134 characters_->Add(c);
135 LAST(ADD_CHAR);
136}
137
138
139void RegExpBuilder::AddEmpty() {
140 pending_empty_ = true;
141}
142
143
144void RegExpBuilder::AddAtom(RegExpTree* term) {
145 if (term->IsEmpty()) {
146 AddEmpty();
147 return;
148 }
149 if (term->IsTextElement()) {
150 FlushCharacters();
151 text_.Add(term);
152 } else {
153 FlushText();
154 terms_.Add(term);
155 }
156 LAST(ADD_ATOM);
157}
158
159
160void RegExpBuilder::AddAssertion(RegExpTree* assert) {
161 FlushText();
162 terms_.Add(assert);
163 LAST(ADD_ASSERT);
164}
165
166
167void RegExpBuilder::NewAlternative() {
168 FlushTerms();
169}
170
171
172void RegExpBuilder::FlushTerms() {
173 FlushText();
174 int num_terms = terms_.length();
175 RegExpTree* alternative;
176 if (num_terms == 0) {
177 alternative = RegExpEmpty::GetInstance();
178 } else if (num_terms == 1) {
179 alternative = terms_.last();
180 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100181 alternative = new(zone()) RegExpAlternative(terms_.GetList());
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 }
183 alternatives_.Add(alternative);
184 terms_.Clear();
185 LAST(ADD_NONE);
186}
187
188
189RegExpTree* RegExpBuilder::ToRegExp() {
190 FlushTerms();
191 int num_alternatives = alternatives_.length();
192 if (num_alternatives == 0) {
193 return RegExpEmpty::GetInstance();
194 }
195 if (num_alternatives == 1) {
196 return alternatives_.last();
197 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100198 return new(zone()) RegExpDisjunction(alternatives_.GetList());
Steve Blocka7e24c12009-10-30 11:49:00 +0000199}
200
201
Leon Clarkee46be812010-01-19 14:06:41 +0000202void RegExpBuilder::AddQuantifierToAtom(int min,
203 int max,
204 RegExpQuantifier::Type type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 if (pending_empty_) {
206 pending_empty_ = false;
207 return;
208 }
209 RegExpTree* atom;
210 if (characters_ != NULL) {
211 ASSERT(last_added_ == ADD_CHAR);
212 // Last atom was character.
213 Vector<const uc16> char_vector = characters_->ToConstVector();
214 int num_chars = char_vector.length();
215 if (num_chars > 1) {
216 Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100217 text_.Add(new(zone()) RegExpAtom(prefix));
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 char_vector = char_vector.SubVector(num_chars - 1, num_chars);
219 }
220 characters_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100221 atom = new(zone()) RegExpAtom(char_vector);
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 FlushText();
223 } else if (text_.length() > 0) {
224 ASSERT(last_added_ == ADD_ATOM);
225 atom = text_.RemoveLast();
226 FlushText();
227 } else if (terms_.length() > 0) {
228 ASSERT(last_added_ == ADD_ATOM);
229 atom = terms_.RemoveLast();
230 if (atom->max_match() == 0) {
231 // Guaranteed to only match an empty string.
232 LAST(ADD_TERM);
233 if (min == 0) {
234 return;
235 }
236 terms_.Add(atom);
237 return;
238 }
239 } else {
240 // Only call immediately after adding an atom or character!
241 UNREACHABLE();
242 return;
243 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100244 terms_.Add(new(zone()) RegExpQuantifier(min, max, type, atom));
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 LAST(ADD_TERM);
246}
247
248
Steve Block9fac8402011-05-12 15:51:54 +0100249Handle<String> Parser::LookupSymbol(int symbol_id) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800250 // Length of symbol cache is the number of identified symbols.
251 // If we are larger than that, or negative, it's not a cached symbol.
252 // This might also happen if there is no preparser symbol data, even
253 // if there is some preparser data.
254 if (static_cast<unsigned>(symbol_id)
255 >= static_cast<unsigned>(symbol_cache_.length())) {
Steve Block9fac8402011-05-12 15:51:54 +0100256 if (scanner().is_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 return isolate()->factory()->LookupAsciiSymbol(
258 scanner().literal_ascii_string());
Steve Block9fac8402011-05-12 15:51:54 +0100259 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100260 return isolate()->factory()->LookupTwoByteSymbol(
261 scanner().literal_uc16_string());
Steve Block9fac8402011-05-12 15:51:54 +0100262 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800263 }
Steve Block9fac8402011-05-12 15:51:54 +0100264 return LookupCachedSymbol(symbol_id);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800265}
266
267
Steve Block9fac8402011-05-12 15:51:54 +0100268Handle<String> Parser::LookupCachedSymbol(int symbol_id) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800269 // Make sure the cache is large enough to hold the symbol identifier.
270 if (symbol_cache_.length() <= symbol_id) {
271 // Increase length to index + 1.
272 symbol_cache_.AddBlock(Handle<String>::null(),
273 symbol_id + 1 - symbol_cache_.length());
274 }
275 Handle<String> result = symbol_cache_.at(symbol_id);
276 if (result.is_null()) {
Steve Block9fac8402011-05-12 15:51:54 +0100277 if (scanner().is_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100278 result = isolate()->factory()->LookupAsciiSymbol(
279 scanner().literal_ascii_string());
Steve Block9fac8402011-05-12 15:51:54 +0100280 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100281 result = isolate()->factory()->LookupTwoByteSymbol(
282 scanner().literal_uc16_string());
Steve Block9fac8402011-05-12 15:51:54 +0100283 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800284 symbol_cache_.at(symbol_id) = result;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100285 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 }
Steve Block44f0eee2011-05-26 01:26:41 +0100287 isolate()->counters()->total_preparse_symbols_skipped()->Increment();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800288 return result;
289}
Steve Blocka7e24c12009-10-30 11:49:00 +0000290
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800291
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100292FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) {
293 // The current pre-data entry must be a FunctionEntry with the given
294 // start position.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100295 if ((function_index_ + FunctionEntry::kSize <= store_.length())
296 && (static_cast<int>(store_[function_index_]) == start)) {
297 int index = function_index_;
298 function_index_ += FunctionEntry::kSize;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100299 return FunctionEntry(store_.SubVector(index,
300 index + FunctionEntry::kSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 }
302 return FunctionEntry();
303}
304
305
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100306int ScriptDataImpl::GetSymbolIdentifier() {
307 return ReadNumber(&symbol_data_);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100308}
309
310
Iain Merrick9ac36c92010-09-13 15:29:50 +0100311bool ScriptDataImpl::SanityCheck() {
312 // Check that the header data is valid and doesn't specify
313 // point to positions outside the store.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800314 if (store_.length() < PreparseDataConstants::kHeaderSize) return false;
315 if (magic() != PreparseDataConstants::kMagicNumber) return false;
316 if (version() != PreparseDataConstants::kCurrentVersion) return false;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100317 if (has_error()) {
318 // Extra sane sanity check for error message encoding.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800319 if (store_.length() <= PreparseDataConstants::kHeaderSize
320 + PreparseDataConstants::kMessageTextPos) {
321 return false;
322 }
323 if (Read(PreparseDataConstants::kMessageStartPos) >
324 Read(PreparseDataConstants::kMessageEndPos)) {
325 return false;
326 }
327 unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
328 int pos = PreparseDataConstants::kMessageTextPos;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100329 for (unsigned int i = 0; i <= arg_count; i++) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800330 if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) {
331 return false;
332 }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100333 int length = static_cast<int>(Read(pos));
334 if (length < 0) return false;
335 pos += 1 + length;
336 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800337 if (store_.length() < PreparseDataConstants::kHeaderSize + pos) {
338 return false;
339 }
Iain Merrick9ac36c92010-09-13 15:29:50 +0100340 return true;
341 }
342 // Check that the space allocated for function entries is sane.
343 int functions_size =
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800344 static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100345 if (functions_size < 0) return false;
346 if (functions_size % FunctionEntry::kSize != 0) return false;
347 // Check that the count of symbols is non-negative.
348 int symbol_count =
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800349 static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100350 if (symbol_count < 0) return false;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100351 // Check that the total size has room for header and function entries.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100352 int minimum_size =
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800353 PreparseDataConstants::kHeaderSize + functions_size;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100354 if (store_.length() < minimum_size) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 return true;
356}
357
358
Steve Block59151502010-09-22 15:07:15 +0100359
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100360const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 int length = start[0];
362 char* result = NewArray<char>(length + 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100363 for (int i = 0; i < length; i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 result[i] = start[i + 1];
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100365 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 result[length] = '\0';
367 if (chars != NULL) *chars = length;
368 return result;
369}
370
Steve Blocka7e24c12009-10-30 11:49:00 +0000371Scanner::Location ScriptDataImpl::MessageLocation() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800372 int beg_pos = Read(PreparseDataConstants::kMessageStartPos);
373 int end_pos = Read(PreparseDataConstants::kMessageEndPos);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 return Scanner::Location(beg_pos, end_pos);
375}
376
377
378const char* ScriptDataImpl::BuildMessage() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800379 unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100380 return ReadString(start, NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000381}
382
383
384Vector<const char*> ScriptDataImpl::BuildArgs() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800385 int arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 const char** array = NewArray<const char*>(arg_count);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100387 // Position after text found by skipping past length field and
388 // length field content words.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800389 int pos = PreparseDataConstants::kMessageTextPos + 1
390 + Read(PreparseDataConstants::kMessageTextPos);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 for (int i = 0; i < arg_count; i++) {
392 int count = 0;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100393 array[i] = ReadString(ReadAddress(pos), &count);
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 pos += count + 1;
395 }
396 return Vector<const char*>(array, arg_count);
397}
398
399
400unsigned ScriptDataImpl::Read(int position) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800401 return store_[PreparseDataConstants::kHeaderSize + position];
Steve Blocka7e24c12009-10-30 11:49:00 +0000402}
403
404
405unsigned* ScriptDataImpl::ReadAddress(int position) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800406 return &store_[PreparseDataConstants::kHeaderSize + position];
Steve Blocka7e24c12009-10-30 11:49:00 +0000407}
408
409
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800410Scope* Parser::NewScope(Scope* parent, Scope::Type type, bool inside_with) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100411 Scope* result = new(zone()) Scope(parent, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 result->Initialize(inside_with);
413 return result;
414}
415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416// ----------------------------------------------------------------------------
417// Target is a support class to facilitate manipulation of the
418// Parser's target_stack_ (the stack of potential 'break' and
419// 'continue' statement targets). Upon construction, a new target is
420// added; it is removed upon destruction.
421
422class Target BASE_EMBEDDED {
423 public:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800424 Target(Target** variable, AstNode* node)
425 : variable_(variable), node_(node), previous_(*variable) {
426 *variable = this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 }
428
429 ~Target() {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800430 *variable_ = previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 }
432
433 Target* previous() { return previous_; }
434 AstNode* node() { return node_; }
435
436 private:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800437 Target** variable_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 AstNode* node_;
439 Target* previous_;
440};
441
442
443class TargetScope BASE_EMBEDDED {
444 public:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800445 explicit TargetScope(Target** variable)
446 : variable_(variable), previous_(*variable) {
447 *variable = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 }
449
450 ~TargetScope() {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800451 *variable_ = previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 }
453
454 private:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800455 Target** variable_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 Target* previous_;
457};
458
459
460// ----------------------------------------------------------------------------
461// LexicalScope is a support class to facilitate manipulation of the
462// Parser's scope stack. The constructor sets the parser's top scope
463// to the incoming scope, and the destructor resets it.
Steve Block44f0eee2011-05-26 01:26:41 +0100464//
Ben Murdoch8b112d22011-06-08 16:22:53 +0100465// Additionally, it stores transient information used during parsing.
Steve Block44f0eee2011-05-26 01:26:41 +0100466// These scopes are not kept around after parsing or referenced by syntax
467// trees so they can be stack-allocated and hence used by the pre-parser.
Steve Blocka7e24c12009-10-30 11:49:00 +0000468
469class LexicalScope BASE_EMBEDDED {
470 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100471 LexicalScope(Parser* parser, Scope* scope, Isolate* isolate);
472 ~LexicalScope();
473
474 int NextMaterializedLiteralIndex() {
475 int next_index =
476 materialized_literal_count_ + JSFunction::kLiteralsPrefixSize;
477 materialized_literal_count_++;
478 return next_index;
479 }
480 int materialized_literal_count() { return materialized_literal_count_; }
481
482 void SetThisPropertyAssignmentInfo(
483 bool only_simple_this_property_assignments,
484 Handle<FixedArray> this_property_assignments) {
485 only_simple_this_property_assignments_ =
486 only_simple_this_property_assignments;
487 this_property_assignments_ = this_property_assignments;
488 }
489 bool only_simple_this_property_assignments() {
490 return only_simple_this_property_assignments_;
491 }
492 Handle<FixedArray> this_property_assignments() {
493 return this_property_assignments_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000494 }
495
Steve Block44f0eee2011-05-26 01:26:41 +0100496 void AddProperty() { expected_property_count_++; }
497 int expected_property_count() { return expected_property_count_; }
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100500 // Captures the number of literals that need materialization in the
501 // function. Includes regexp literals, and boilerplate for object
502 // and array literals.
503 int materialized_literal_count_;
504
505 // Properties count estimation.
506 int expected_property_count_;
507
508 // Keeps track of assignments to properties of this. Used for
509 // optimizing constructors.
510 bool only_simple_this_property_assignments_;
511 Handle<FixedArray> this_property_assignments_;
512
Steve Block44f0eee2011-05-26 01:26:41 +0100513 // Bookkeeping
514 Parser* parser_;
515 // Previous values
516 LexicalScope* lexical_scope_parent_;
517 Scope* previous_scope_;
518 int previous_with_nesting_level_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100519 unsigned previous_ast_node_id_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000520};
521
Steve Block44f0eee2011-05-26 01:26:41 +0100522
523LexicalScope::LexicalScope(Parser* parser, Scope* scope, Isolate* isolate)
524 : materialized_literal_count_(0),
525 expected_property_count_(0),
526 only_simple_this_property_assignments_(false),
527 this_property_assignments_(isolate->factory()->empty_fixed_array()),
Steve Block44f0eee2011-05-26 01:26:41 +0100528 parser_(parser),
529 lexical_scope_parent_(parser->lexical_scope_),
530 previous_scope_(parser->top_scope_),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100531 previous_with_nesting_level_(parser->with_nesting_level_),
532 previous_ast_node_id_(isolate->ast_node_id()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100533 parser->top_scope_ = scope;
534 parser->lexical_scope_ = this;
535 parser->with_nesting_level_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100536 isolate->set_ast_node_id(AstNode::kFunctionEntryId + 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100537}
538
539
540LexicalScope::~LexicalScope() {
541 parser_->top_scope_->Leave();
542 parser_->top_scope_ = previous_scope_;
543 parser_->lexical_scope_ = lexical_scope_parent_;
544 parser_->with_nesting_level_ = previous_with_nesting_level_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100545 parser_->isolate()->set_ast_node_id(previous_ast_node_id_);
Steve Block44f0eee2011-05-26 01:26:41 +0100546}
547
548
Steve Blocka7e24c12009-10-30 11:49:00 +0000549// ----------------------------------------------------------------------------
550// The CHECK_OK macro is a convenient macro to enforce error
551// handling for functions that may fail (by returning !*ok).
552//
553// CAUTION: This macro appends extra statements after a call,
554// thus it must never be used where only a single statement
555// is correct (e.g. an if statement branch w/o braces)!
556
557#define CHECK_OK ok); \
558 if (!*ok) return NULL; \
559 ((void)0
560#define DUMMY ) // to make indentation work
561#undef DUMMY
562
563#define CHECK_FAILED /**/); \
564 if (failed_) return NULL; \
565 ((void)0
566#define DUMMY ) // to make indentation work
567#undef DUMMY
568
569// ----------------------------------------------------------------------------
570// Implementation of Parser
571
572Parser::Parser(Handle<Script> script,
573 bool allow_natives_syntax,
574 v8::Extension* extension,
Steve Blocka7e24c12009-10-30 11:49:00 +0000575 ScriptDataImpl* pre_data)
Steve Block44f0eee2011-05-26 01:26:41 +0100576 : isolate_(script->GetIsolate()),
577 symbol_cache_(pre_data ? pre_data->symbol_count() : 0),
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800578 script_(script),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100579 scanner_(isolate_->unicode_cache()),
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 top_scope_(NULL),
581 with_nesting_level_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100582 lexical_scope_(NULL),
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 target_stack_(NULL),
584 allow_natives_syntax_(allow_natives_syntax),
585 extension_(extension),
Steve Block6ded16b2010-05-10 14:33:55 +0100586 pre_data_(pre_data),
Ben Murdochb0fe1622011-05-05 13:52:32 +0100587 fni_(NULL),
Ben Murdochb8e0da22011-05-16 14:20:40 +0100588 stack_overflow_(false),
589 parenthesized_function_(false) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100590 AstNode::ResetIds();
Steve Blocka7e24c12009-10-30 11:49:00 +0000591}
592
593
Steve Blocka7e24c12009-10-30 11:49:00 +0000594FunctionLiteral* Parser::ParseProgram(Handle<String> source,
Steve Block1e0659c2011-05-24 12:43:12 +0100595 bool in_global_context,
596 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000597 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
598
Steve Block44f0eee2011-05-26 01:26:41 +0100599 HistogramTimerScope timer(isolate()->counters()->parse());
600 isolate()->counters()->total_parse_size()->Increment(source->length());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100601 fni_ = new(zone()) FuncNameInferrer();
Steve Blocka7e24c12009-10-30 11:49:00 +0000602
603 // Initialize parser state.
Steve Block6ded16b2010-05-10 14:33:55 +0100604 source->TryFlatten();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100605 if (source->IsExternalTwoByteString()) {
606 // Notice that the stream is destroyed at the end of the branch block.
607 // The last line of the blocks can't be moved outside, even though they're
608 // identical calls.
609 ExternalTwoByteStringUC16CharacterStream stream(
610 Handle<ExternalTwoByteString>::cast(source), 0, source->length());
Steve Block9fac8402011-05-12 15:51:54 +0100611 scanner_.Initialize(&stream);
Steve Block1e0659c2011-05-24 12:43:12 +0100612 return DoParseProgram(source, in_global_context, strict_mode, &zone_scope);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100613 } else {
614 GenericStringUC16CharacterStream stream(source, 0, source->length());
Steve Block9fac8402011-05-12 15:51:54 +0100615 scanner_.Initialize(&stream);
Steve Block1e0659c2011-05-24 12:43:12 +0100616 return DoParseProgram(source, in_global_context, strict_mode, &zone_scope);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100617 }
618}
619
620
621FunctionLiteral* Parser::DoParseProgram(Handle<String> source,
622 bool in_global_context,
Steve Block1e0659c2011-05-24 12:43:12 +0100623 StrictModeFlag strict_mode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 ZoneScope* zone_scope) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 ASSERT(target_stack_ == NULL);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100626 if (pre_data_ != NULL) pre_data_->Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000627
628 // Compute the parsing mode.
629 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY;
630 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY;
631
632 Scope::Type type =
633 in_global_context
634 ? Scope::GLOBAL_SCOPE
635 : Scope::EVAL_SCOPE;
Steve Block44f0eee2011-05-26 01:26:41 +0100636 Handle<String> no_name = isolate()->factory()->empty_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000637
638 FunctionLiteral* result = NULL;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800639 { Scope* scope = NewScope(top_scope_, type, inside_with());
Steve Block44f0eee2011-05-26 01:26:41 +0100640 LexicalScope lexical_scope(this, scope, isolate());
Steve Block1e0659c2011-05-24 12:43:12 +0100641 if (strict_mode == kStrictMode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100642 top_scope_->EnableStrictMode();
Steve Block1e0659c2011-05-24 12:43:12 +0100643 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800644 ZoneList<Statement*>* body = new ZoneList<Statement*>(16);
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 bool ok = true;
Steve Block1e0659c2011-05-24 12:43:12 +0100646 int beg_loc = scanner().location().beg_pos;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800647 ParseSourceElements(body, Token::EOS, &ok);
Steve Block44f0eee2011-05-26 01:26:41 +0100648 if (ok && top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100649 CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok);
650 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 if (ok) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100652 result = new(zone()) FunctionLiteral(
Steve Blocka7e24c12009-10-30 11:49:00 +0000653 no_name,
654 top_scope_,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800655 body,
Steve Block44f0eee2011-05-26 01:26:41 +0100656 lexical_scope.materialized_literal_count(),
657 lexical_scope.expected_property_count(),
658 lexical_scope.only_simple_this_property_assignments(),
659 lexical_scope.this_property_assignments(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 0,
661 0,
662 source->length(),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100663 false);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 } else if (stack_overflow_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100665 isolate()->StackOverflow();
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 }
667 }
668
669 // Make sure the target stack is empty.
670 ASSERT(target_stack_ == NULL);
671
672 // If there was a syntax error we have to get rid of the AST
673 // and it is not safe to do so before the scope has been deleted.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100674 if (result == NULL) zone_scope->DeleteOnExit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000675 return result;
676}
677
Steve Block44f0eee2011-05-26 01:26:41 +0100678FunctionLiteral* Parser::ParseLazy(CompilationInfo* info) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
Steve Block44f0eee2011-05-26 01:26:41 +0100680 HistogramTimerScope timer(isolate()->counters()->parse_lazy());
Ben Murdochf87a2032010-10-22 12:50:53 +0100681 Handle<String> source(String::cast(script_->source()));
Steve Block44f0eee2011-05-26 01:26:41 +0100682 isolate()->counters()->total_parse_size()->Increment(source->length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000683
Steve Block44f0eee2011-05-26 01:26:41 +0100684 Handle<SharedFunctionInfo> shared_info = info->shared_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100685 // Initialize parser state.
686 source->TryFlatten();
687 if (source->IsExternalTwoByteString()) {
688 ExternalTwoByteStringUC16CharacterStream stream(
689 Handle<ExternalTwoByteString>::cast(source),
Steve Block44f0eee2011-05-26 01:26:41 +0100690 shared_info->start_position(),
691 shared_info->end_position());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100692 FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope);
693 return result;
694 } else {
695 GenericStringUC16CharacterStream stream(source,
Steve Block44f0eee2011-05-26 01:26:41 +0100696 shared_info->start_position(),
697 shared_info->end_position());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100698 FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope);
699 return result;
700 }
701}
702
703
Steve Block44f0eee2011-05-26 01:26:41 +0100704FunctionLiteral* Parser::ParseLazy(CompilationInfo* info,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100705 UC16CharacterStream* source,
706 ZoneScope* zone_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +0100707 Handle<SharedFunctionInfo> shared_info = info->shared_info();
Steve Block9fac8402011-05-12 15:51:54 +0100708 scanner_.Initialize(source);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100709 ASSERT(target_stack_ == NULL);
710
Steve Block44f0eee2011-05-26 01:26:41 +0100711 Handle<String> name(String::cast(shared_info->name()));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100712 fni_ = new(zone()) FuncNameInferrer();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100713 fni_->PushEnclosingName(name);
714
Steve Blocka7e24c12009-10-30 11:49:00 +0000715 mode_ = PARSE_EAGERLY;
716
717 // Place holder for the result.
718 FunctionLiteral* result = NULL;
719
720 {
721 // Parse the function literal.
Steve Block44f0eee2011-05-26 01:26:41 +0100722 Handle<String> no_name = isolate()->factory()->empty_symbol();
723 Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with());
724 if (!info->closure().is_null()) {
725 scope = Scope::DeserializeScopeChain(info, scope);
726 }
727 LexicalScope lexical_scope(this, scope, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000728
Steve Block44f0eee2011-05-26 01:26:41 +0100729 if (shared_info->strict_mode()) {
730 top_scope_->EnableStrictMode();
Steve Block1e0659c2011-05-24 12:43:12 +0100731 }
732
Ben Murdochf87a2032010-10-22 12:50:53 +0100733 FunctionLiteralType type =
Steve Block44f0eee2011-05-26 01:26:41 +0100734 shared_info->is_expression() ? EXPRESSION : DECLARATION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 bool ok = true;
Steve Block1e0659c2011-05-24 12:43:12 +0100736 result = ParseFunctionLiteral(name,
737 false, // Strict mode name already checked.
738 RelocInfo::kNoPosition, type, &ok);
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 // Make sure the results agree.
740 ASSERT(ok == (result != NULL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 }
742
743 // Make sure the target stack is empty.
744 ASSERT(target_stack_ == NULL);
745
746 // If there was a stack overflow we have to get rid of AST and it is
747 // not safe to do before scope has been deleted.
748 if (result == NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100749 zone_scope->DeleteOnExit();
Steve Block44f0eee2011-05-26 01:26:41 +0100750 if (stack_overflow_) isolate()->StackOverflow();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100751 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100752 Handle<String> inferred_name(shared_info->inferred_name());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100753 result->set_inferred_name(inferred_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000754 }
755 return result;
756}
757
Ben Murdochf87a2032010-10-22 12:50:53 +0100758
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800759Handle<String> Parser::GetSymbol(bool* ok) {
760 int symbol_id = -1;
761 if (pre_data() != NULL) {
762 symbol_id = pre_data()->GetSymbolIdentifier();
Leon Clarke4515c472010-02-03 11:58:03 +0000763 }
Steve Block9fac8402011-05-12 15:51:54 +0100764 return LookupSymbol(symbol_id);
Leon Clarke4515c472010-02-03 11:58:03 +0000765}
Steve Blocka7e24c12009-10-30 11:49:00 +0000766
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800767
Steve Blocka7e24c12009-10-30 11:49:00 +0000768void Parser::ReportMessage(const char* type, Vector<const char*> args) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100769 Scanner::Location source_location = scanner().location();
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 ReportMessageAt(source_location, type, args);
771}
772
773
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800774void Parser::ReportMessageAt(Scanner::Location source_location,
775 const char* type,
776 Vector<const char*> args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000777 MessageLocation location(script_,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100778 source_location.beg_pos,
779 source_location.end_pos);
Steve Block44f0eee2011-05-26 01:26:41 +0100780 Factory* factory = isolate()->factory();
781 Handle<FixedArray> elements = factory->NewFixedArray(args.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 for (int i = 0; i < args.length(); i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100783 Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i]));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100784 elements->set(i, *arg_string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 }
Steve Block44f0eee2011-05-26 01:26:41 +0100786 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
787 Handle<Object> result = factory->NewSyntaxError(type, array);
788 isolate()->Throw(*result, &location);
Steve Blocka7e24c12009-10-30 11:49:00 +0000789}
790
791
Ben Murdochb8e0da22011-05-16 14:20:40 +0100792void Parser::ReportMessageAt(Scanner::Location source_location,
793 const char* type,
794 Vector<Handle<String> > args) {
795 MessageLocation location(script_,
796 source_location.beg_pos,
797 source_location.end_pos);
Steve Block44f0eee2011-05-26 01:26:41 +0100798 Factory* factory = isolate()->factory();
799 Handle<FixedArray> elements = factory->NewFixedArray(args.length());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100800 for (int i = 0; i < args.length(); i++) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100801 elements->set(i, *args[i]);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100802 }
Steve Block44f0eee2011-05-26 01:26:41 +0100803 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
804 Handle<Object> result = factory->NewSyntaxError(type, array);
805 isolate()->Throw(*result, &location);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100806}
807
808
Steve Blocka7e24c12009-10-30 11:49:00 +0000809// Base class containing common code for the different finder classes used by
810// the parser.
811class ParserFinder {
812 protected:
813 ParserFinder() {}
814 static Assignment* AsAssignment(Statement* stat) {
815 if (stat == NULL) return NULL;
816 ExpressionStatement* exp_stat = stat->AsExpressionStatement();
817 if (exp_stat == NULL) return NULL;
818 return exp_stat->expression()->AsAssignment();
819 }
820};
821
822
823// An InitializationBlockFinder finds and marks sequences of statements of the
Steve Blockd0582a62009-12-15 09:54:21 +0000824// form expr.a = ...; expr.b = ...; etc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000825class InitializationBlockFinder : public ParserFinder {
826 public:
827 InitializationBlockFinder()
828 : first_in_block_(NULL), last_in_block_(NULL), block_size_(0) {}
829
830 ~InitializationBlockFinder() {
831 if (InBlock()) EndBlock();
832 }
833
834 void Update(Statement* stat) {
835 Assignment* assignment = AsAssignment(stat);
836 if (InBlock()) {
837 if (BlockContinues(assignment)) {
838 UpdateBlock(assignment);
839 } else {
840 EndBlock();
841 }
842 }
843 if (!InBlock() && (assignment != NULL) &&
844 (assignment->op() == Token::ASSIGN)) {
845 StartBlock(assignment);
846 }
847 }
848
849 private:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800850 // The minimum number of contiguous assignment that will
851 // be treated as an initialization block. Benchmarks show that
852 // the overhead exceeds the savings below this limit.
853 static const int kMinInitializationBlock = 3;
854
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 // Returns true if the expressions appear to denote the same object.
856 // In the context of initialization blocks, we only consider expressions
Steve Blockd0582a62009-12-15 09:54:21 +0000857 // of the form 'expr.x' or expr["x"].
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 static bool SameObject(Expression* e1, Expression* e2) {
859 VariableProxy* v1 = e1->AsVariableProxy();
860 VariableProxy* v2 = e2->AsVariableProxy();
861 if (v1 != NULL && v2 != NULL) {
862 return v1->name()->Equals(*v2->name());
863 }
864 Property* p1 = e1->AsProperty();
865 Property* p2 = e2->AsProperty();
866 if ((p1 == NULL) || (p2 == NULL)) return false;
867 Literal* key1 = p1->key()->AsLiteral();
868 Literal* key2 = p2->key()->AsLiteral();
869 if ((key1 == NULL) || (key2 == NULL)) return false;
870 if (!key1->handle()->IsString() || !key2->handle()->IsString()) {
871 return false;
872 }
873 String* name1 = String::cast(*key1->handle());
874 String* name2 = String::cast(*key2->handle());
875 if (!name1->Equals(name2)) return false;
876 return SameObject(p1->obj(), p2->obj());
877 }
878
879 // Returns true if the expressions appear to denote different properties
880 // of the same object.
881 static bool PropertyOfSameObject(Expression* e1, Expression* e2) {
882 Property* p1 = e1->AsProperty();
883 Property* p2 = e2->AsProperty();
884 if ((p1 == NULL) || (p2 == NULL)) return false;
885 return SameObject(p1->obj(), p2->obj());
886 }
887
888 bool BlockContinues(Assignment* assignment) {
889 if ((assignment == NULL) || (first_in_block_ == NULL)) return false;
890 if (assignment->op() != Token::ASSIGN) return false;
891 return PropertyOfSameObject(first_in_block_->target(),
892 assignment->target());
893 }
894
895 void StartBlock(Assignment* assignment) {
896 first_in_block_ = assignment;
897 last_in_block_ = assignment;
898 block_size_ = 1;
899 }
900
901 void UpdateBlock(Assignment* assignment) {
902 last_in_block_ = assignment;
903 ++block_size_;
904 }
905
906 void EndBlock() {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800907 if (block_size_ >= kMinInitializationBlock) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 first_in_block_->mark_block_start();
909 last_in_block_->mark_block_end();
910 }
911 last_in_block_ = first_in_block_ = NULL;
912 block_size_ = 0;
913 }
914
915 bool InBlock() { return first_in_block_ != NULL; }
916
917 Assignment* first_in_block_;
918 Assignment* last_in_block_;
919 int block_size_;
920
921 DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder);
922};
923
924
925// A ThisNamedPropertyAssigmentFinder finds and marks statements of the form
926// this.x = ...;, where x is a named property. It also determines whether a
927// function contains only assignments of this type.
928class ThisNamedPropertyAssigmentFinder : public ParserFinder {
929 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100930 explicit ThisNamedPropertyAssigmentFinder(Isolate* isolate)
931 : isolate_(isolate),
932 only_simple_this_property_assignments_(true),
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 names_(NULL),
934 assigned_arguments_(NULL),
935 assigned_constants_(NULL) {}
936
937 void Update(Scope* scope, Statement* stat) {
Steve Blockd0582a62009-12-15 09:54:21 +0000938 // Bail out if function already has property assignment that are
939 // not simple this property assignments.
940 if (!only_simple_this_property_assignments_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 return;
942 }
943
944 // Check whether this statement is of the form this.x = ...;
945 Assignment* assignment = AsAssignment(stat);
946 if (IsThisPropertyAssignment(assignment)) {
947 HandleThisPropertyAssignment(scope, assignment);
948 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 only_simple_this_property_assignments_ = false;
950 }
951 }
952
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 // Returns whether only statements of the form this.x = y; where y is either a
954 // constant or a function argument was encountered.
955 bool only_simple_this_property_assignments() {
956 return only_simple_this_property_assignments_;
957 }
958
959 // Returns a fixed array containing three elements for each assignment of the
960 // form this.x = y;
961 Handle<FixedArray> GetThisPropertyAssignments() {
962 if (names_ == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100963 return isolate_->factory()->empty_fixed_array();
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 }
965 ASSERT(names_ != NULL);
966 ASSERT(assigned_arguments_ != NULL);
967 ASSERT_EQ(names_->length(), assigned_arguments_->length());
968 ASSERT_EQ(names_->length(), assigned_constants_->length());
969 Handle<FixedArray> assignments =
Steve Block44f0eee2011-05-26 01:26:41 +0100970 isolate_->factory()->NewFixedArray(names_->length() * 3);
Steve Blocka7e24c12009-10-30 11:49:00 +0000971 for (int i = 0; i < names_->length(); i++) {
972 assignments->set(i * 3, *names_->at(i));
973 assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_->at(i)));
974 assignments->set(i * 3 + 2, *assigned_constants_->at(i));
975 }
976 return assignments;
977 }
978
979 private:
980 bool IsThisPropertyAssignment(Assignment* assignment) {
981 if (assignment != NULL) {
982 Property* property = assignment->target()->AsProperty();
983 return assignment->op() == Token::ASSIGN
984 && property != NULL
985 && property->obj()->AsVariableProxy() != NULL
986 && property->obj()->AsVariableProxy()->is_this();
987 }
988 return false;
989 }
990
991 void HandleThisPropertyAssignment(Scope* scope, Assignment* assignment) {
Steve Block6ded16b2010-05-10 14:33:55 +0100992 // Check that the property assigned to is a named property, which is not
993 // __proto__.
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 Property* property = assignment->target()->AsProperty();
995 ASSERT(property != NULL);
996 Literal* literal = property->key()->AsLiteral();
997 uint32_t dummy;
998 if (literal != NULL &&
999 literal->handle()->IsString() &&
Steve Block44f0eee2011-05-26 01:26:41 +01001000 !String::cast(*(literal->handle()))->Equals(
1001 isolate_->heap()->Proto_symbol()) &&
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
1003 Handle<String> key = Handle<String>::cast(literal->handle());
1004
1005 // Check whether the value assigned is either a constant or matches the
1006 // name of one of the arguments to the function.
1007 if (assignment->value()->AsLiteral() != NULL) {
1008 // Constant assigned.
1009 Literal* literal = assignment->value()->AsLiteral();
1010 AssignmentFromConstant(key, literal->handle());
Steve Blockd0582a62009-12-15 09:54:21 +00001011 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001012 } else if (assignment->value()->AsVariableProxy() != NULL) {
1013 // Variable assigned.
1014 Handle<String> name =
1015 assignment->value()->AsVariableProxy()->name();
1016 // Check whether the variable assigned matches an argument name.
Steve Blocka7e24c12009-10-30 11:49:00 +00001017 for (int i = 0; i < scope->num_parameters(); i++) {
1018 if (*scope->parameter(i)->name() == *name) {
1019 // Assigned from function argument.
Steve Blockd0582a62009-12-15 09:54:21 +00001020 AssignmentFromParameter(key, i);
1021 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001022 }
1023 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001024 }
1025 }
Steve Blockd0582a62009-12-15 09:54:21 +00001026 // It is not a simple "this.x = value;" assignment with a constant
1027 // or parameter value.
1028 AssignmentFromSomethingElse();
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 }
1030
1031 void AssignmentFromParameter(Handle<String> name, int index) {
1032 EnsureAllocation();
1033 names_->Add(name);
1034 assigned_arguments_->Add(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001035 assigned_constants_->Add(isolate_->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001036 }
1037
1038 void AssignmentFromConstant(Handle<String> name, Handle<Object> value) {
1039 EnsureAllocation();
1040 names_->Add(name);
1041 assigned_arguments_->Add(-1);
1042 assigned_constants_->Add(value);
1043 }
1044
Steve Blockd0582a62009-12-15 09:54:21 +00001045 void AssignmentFromSomethingElse() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001046 // The this assignment is not a simple one.
1047 only_simple_this_property_assignments_ = false;
1048 }
1049
1050 void EnsureAllocation() {
1051 if (names_ == NULL) {
1052 ASSERT(assigned_arguments_ == NULL);
1053 ASSERT(assigned_constants_ == NULL);
1054 names_ = new ZoneStringList(4);
1055 assigned_arguments_ = new ZoneList<int>(4);
1056 assigned_constants_ = new ZoneObjectList(4);
1057 }
1058 }
1059
Steve Block44f0eee2011-05-26 01:26:41 +01001060 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 bool only_simple_this_property_assignments_;
1062 ZoneStringList* names_;
1063 ZoneList<int>* assigned_arguments_;
1064 ZoneObjectList* assigned_constants_;
1065};
1066
1067
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001068void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 int end_token,
1070 bool* ok) {
1071 // SourceElements ::
1072 // (Statement)* <end_token>
1073
1074 // Allocate a target stack to use for this set of source
1075 // elements. This way, all scripts and functions get their own
1076 // target stack thus avoiding illegal breaks and continues across
1077 // functions.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001078 TargetScope scope(&this->target_stack_);
Steve Blocka7e24c12009-10-30 11:49:00 +00001079
1080 ASSERT(processor != NULL);
1081 InitializationBlockFinder block_finder;
Steve Block44f0eee2011-05-26 01:26:41 +01001082 ThisNamedPropertyAssigmentFinder this_property_assignment_finder(isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01001083 bool directive_prologue = true; // Parsing directive prologue.
1084
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 while (peek() != end_token) {
Steve Block1e0659c2011-05-24 12:43:12 +01001086 if (directive_prologue && peek() != Token::STRING) {
1087 directive_prologue = false;
1088 }
1089
1090 Scanner::Location token_loc = scanner().peek_location();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001091
1092 Statement* stat;
1093 if (peek() == Token::FUNCTION) {
1094 // FunctionDeclaration is only allowed in the context of SourceElements
1095 // (Ecma 262 5th Edition, clause 14):
1096 // SourceElement:
1097 // Statement
1098 // FunctionDeclaration
1099 // Common language extension is to allow function declaration in place
1100 // of any statement. This language extension is disabled in strict mode.
1101 stat = ParseFunctionDeclaration(CHECK_OK);
1102 } else {
1103 stat = ParseStatement(NULL, CHECK_OK);
1104 }
Steve Block1e0659c2011-05-24 12:43:12 +01001105
1106 if (stat == NULL || stat->IsEmpty()) {
1107 directive_prologue = false; // End of directive prologue.
1108 continue;
1109 }
1110
1111 if (directive_prologue) {
1112 // A shot at a directive.
1113 ExpressionStatement *e_stat;
1114 Literal *literal;
1115 // Still processing directive prologue?
1116 if ((e_stat = stat->AsExpressionStatement()) != NULL &&
1117 (literal = e_stat->expression()->AsLiteral()) != NULL &&
1118 literal->handle()->IsString()) {
1119 Handle<String> directive = Handle<String>::cast(literal->handle());
1120
1121 // Check "use strict" directive (ES5 14.1).
Steve Block44f0eee2011-05-26 01:26:41 +01001122 if (!top_scope_->is_strict_mode() &&
1123 directive->Equals(isolate()->heap()->use_strict()) &&
Steve Block1e0659c2011-05-24 12:43:12 +01001124 token_loc.end_pos - token_loc.beg_pos ==
Steve Block44f0eee2011-05-26 01:26:41 +01001125 isolate()->heap()->use_strict()->length() + 2) {
1126 top_scope_->EnableStrictMode();
Steve Block1e0659c2011-05-24 12:43:12 +01001127 // "use strict" is the only directive for now.
1128 directive_prologue = false;
1129 }
1130 } else {
1131 // End of the directive prologue.
1132 directive_prologue = false;
1133 }
1134 }
1135
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 // We find and mark the initialization blocks on top level code only.
1137 // This is because the optimization prevents reuse of the map transitions,
1138 // so it should be used only for code that will only be run once.
1139 if (top_scope_->is_global_scope()) {
1140 block_finder.Update(stat);
1141 }
1142 // Find and mark all assignments to named properties in this (this.x =)
1143 if (top_scope_->is_function_scope()) {
1144 this_property_assignment_finder.Update(top_scope_, stat);
1145 }
1146 processor->Add(stat);
1147 }
1148
1149 // Propagate the collected information on this property assignments.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001150 if (top_scope_->is_function_scope()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001151 bool only_simple_this_property_assignments =
Andrei Popescu402d9372010-02-26 13:31:12 +00001152 this_property_assignment_finder.only_simple_this_property_assignments()
1153 && top_scope_->declarations()->length() == 0;
Steve Blockd0582a62009-12-15 09:54:21 +00001154 if (only_simple_this_property_assignments) {
Steve Block44f0eee2011-05-26 01:26:41 +01001155 lexical_scope_->SetThisPropertyAssignmentInfo(
Steve Blockd0582a62009-12-15 09:54:21 +00001156 only_simple_this_property_assignments,
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 this_property_assignment_finder.GetThisPropertyAssignments());
1158 }
1159 }
1160 return 0;
1161}
1162
1163
1164Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
1165 // Statement ::
1166 // Block
1167 // VariableStatement
1168 // EmptyStatement
1169 // ExpressionStatement
1170 // IfStatement
1171 // IterationStatement
1172 // ContinueStatement
1173 // BreakStatement
1174 // ReturnStatement
1175 // WithStatement
1176 // LabelledStatement
1177 // SwitchStatement
1178 // ThrowStatement
1179 // TryStatement
1180 // DebuggerStatement
1181
1182 // Note: Since labels can only be used by 'break' and 'continue'
1183 // statements, which themselves are only valid within blocks,
1184 // iterations or 'switch' statements (i.e., BreakableStatements),
1185 // labels can be simply ignored in all other cases; except for
1186 // trivial labeled break statements 'label: break label' which is
1187 // parsed into an empty statement.
1188
1189 // Keep the source position of the statement
1190 int statement_pos = scanner().peek_location().beg_pos;
1191 Statement* stmt = NULL;
1192 switch (peek()) {
1193 case Token::LBRACE:
1194 return ParseBlock(labels, ok);
1195
1196 case Token::CONST: // fall through
1197 case Token::VAR:
1198 stmt = ParseVariableStatement(ok);
1199 break;
1200
1201 case Token::SEMICOLON:
1202 Next();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001203 return EmptyStatement();
Steve Blocka7e24c12009-10-30 11:49:00 +00001204
1205 case Token::IF:
1206 stmt = ParseIfStatement(labels, ok);
1207 break;
1208
1209 case Token::DO:
Steve Block3ce2e202009-11-05 08:53:23 +00001210 stmt = ParseDoWhileStatement(labels, ok);
Steve Blocka7e24c12009-10-30 11:49:00 +00001211 break;
1212
1213 case Token::WHILE:
1214 stmt = ParseWhileStatement(labels, ok);
1215 break;
1216
1217 case Token::FOR:
1218 stmt = ParseForStatement(labels, ok);
1219 break;
1220
1221 case Token::CONTINUE:
1222 stmt = ParseContinueStatement(ok);
1223 break;
1224
1225 case Token::BREAK:
1226 stmt = ParseBreakStatement(labels, ok);
1227 break;
1228
1229 case Token::RETURN:
1230 stmt = ParseReturnStatement(ok);
1231 break;
1232
1233 case Token::WITH:
1234 stmt = ParseWithStatement(labels, ok);
1235 break;
1236
1237 case Token::SWITCH:
1238 stmt = ParseSwitchStatement(labels, ok);
1239 break;
1240
1241 case Token::THROW:
1242 stmt = ParseThrowStatement(ok);
1243 break;
1244
1245 case Token::TRY: {
1246 // NOTE: It is somewhat complicated to have labels on
1247 // try-statements. When breaking out of a try-finally statement,
1248 // one must take great care not to treat it as a
1249 // fall-through. It is much easier just to wrap the entire
1250 // try-statement in a statement block and put the labels there
Ben Murdoch8b112d22011-06-08 16:22:53 +01001251 Block* result = new(zone()) Block(labels, 1, false);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001252 Target target(&this->target_stack_, result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001253 TryStatement* statement = ParseTryStatement(CHECK_OK);
1254 if (statement) {
1255 statement->set_statement_pos(statement_pos);
1256 }
1257 if (result) result->AddStatement(statement);
1258 return result;
1259 }
1260
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001261 case Token::FUNCTION: {
1262 // In strict mode, FunctionDeclaration is only allowed in the context
1263 // of SourceElements.
Steve Block44f0eee2011-05-26 01:26:41 +01001264 if (top_scope_->is_strict_mode()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001265 ReportMessageAt(scanner().peek_location(), "strict_function",
1266 Vector<const char*>::empty());
1267 *ok = false;
1268 return NULL;
1269 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001270 return ParseFunctionDeclaration(ok);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001271 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001272
1273 case Token::NATIVE:
1274 return ParseNativeDeclaration(ok);
1275
1276 case Token::DEBUGGER:
1277 stmt = ParseDebuggerStatement(ok);
1278 break;
1279
1280 default:
1281 stmt = ParseExpressionOrLabelledStatement(labels, ok);
1282 }
1283
1284 // Store the source position of the statement
1285 if (stmt != NULL) stmt->set_statement_pos(statement_pos);
1286 return stmt;
1287}
1288
1289
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001290VariableProxy* Parser::Declare(Handle<String> name,
1291 Variable::Mode mode,
1292 FunctionLiteral* fun,
1293 bool resolve,
1294 bool* ok) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 Variable* var = NULL;
1296 // If we are inside a function, a declaration of a variable
1297 // is a truly local variable, and the scope of the variable
1298 // is always the function scope.
1299
1300 // If a function scope exists, then we can statically declare this
1301 // variable and also set its mode. In any case, a Declaration node
1302 // will be added to the scope so that the declaration can be added
1303 // to the corresponding activation frame at runtime if necessary.
1304 // For instance declarations inside an eval scope need to be added
1305 // to the calling function context.
1306 if (top_scope_->is_function_scope()) {
1307 // Declare the variable in the function scope.
1308 var = top_scope_->LocalLookup(name);
1309 if (var == NULL) {
1310 // Declare the name.
Steve Block053d10c2011-06-13 19:13:29 +01001311 var = top_scope_->DeclareLocal(name, mode, Scope::VAR_OR_CONST);
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 } else {
1313 // The name was declared before; check for conflicting
1314 // re-declarations. If the previous declaration was a const or the
1315 // current declaration is a const then we have a conflict. There is
1316 // similar code in runtime.cc in the Declare functions.
1317 if ((mode == Variable::CONST) || (var->mode() == Variable::CONST)) {
1318 // We only have vars and consts in declarations.
1319 ASSERT(var->mode() == Variable::VAR ||
1320 var->mode() == Variable::CONST);
1321 const char* type = (var->mode() == Variable::VAR) ? "var" : "const";
1322 Handle<String> type_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001323 isolate()->factory()->NewStringFromUtf8(CStrVector(type), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001324 Expression* expression =
Steve Block44f0eee2011-05-26 01:26:41 +01001325 NewThrowTypeError(isolate()->factory()->redeclaration_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001326 type_string, name);
1327 top_scope_->SetIllegalRedeclaration(expression);
1328 }
1329 }
1330 }
1331
1332 // We add a declaration node for every declaration. The compiler
1333 // will only generate code if necessary. In particular, declarations
1334 // for inner local variables that do not represent functions won't
1335 // result in any generated code.
1336 //
1337 // Note that we always add an unresolved proxy even if it's not
1338 // used, simply because we don't know in this method (w/o extra
1339 // parameters) if the proxy is needed or not. The proxy will be
1340 // bound during variable resolution time unless it was pre-bound
1341 // below.
1342 //
1343 // WARNING: This will lead to multiple declaration nodes for the
1344 // same variable if it is declared several times. This is not a
1345 // semantic issue as long as we keep the source order, but it may be
1346 // a performance issue since it may lead to repeated
1347 // Runtime::DeclareContextSlot() calls.
1348 VariableProxy* proxy = top_scope_->NewUnresolved(name, inside_with());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001349 top_scope_->AddDeclaration(new(zone()) Declaration(proxy, mode, fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001350
1351 // For global const variables we bind the proxy to a variable.
1352 if (mode == Variable::CONST && top_scope_->is_global_scope()) {
1353 ASSERT(resolve); // should be set by all callers
1354 Variable::Kind kind = Variable::NORMAL;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001355 var = new(zone()) Variable(top_scope_, name, Variable::CONST, true, kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 }
1357
1358 // If requested and we have a local variable, bind the proxy to the variable
1359 // at parse-time. This is used for functions (and consts) declared inside
1360 // statements: the corresponding function (or const) variable must be in the
1361 // function scope and not a statement-local scope, e.g. as provided with a
1362 // 'with' statement:
1363 //
1364 // with (obj) {
1365 // function f() {}
1366 // }
1367 //
1368 // which is translated into:
1369 //
1370 // with (obj) {
1371 // // in this case this is not: 'var f; f = function () {};'
1372 // var f = function () {};
1373 // }
1374 //
1375 // Note that if 'f' is accessed from inside the 'with' statement, it
1376 // will be allocated in the context (because we must be able to look
1377 // it up dynamically) but it will also be accessed statically, i.e.,
1378 // with a context slot index and a context chain length for this
1379 // initialization code. Thus, inside the 'with' statement, we need
1380 // both access to the static and the dynamic context chain; the
1381 // runtime needs to provide both.
1382 if (resolve && var != NULL) proxy->BindTo(var);
1383
1384 return proxy;
1385}
1386
1387
1388// Language extension which is only enabled for source files loaded
1389// through the API's extension mechanism. A native function
1390// declaration is resolved by looking up the function through a
1391// callback provided by the extension.
1392Statement* Parser::ParseNativeDeclaration(bool* ok) {
1393 if (extension_ == NULL) {
1394 ReportUnexpectedToken(Token::NATIVE);
1395 *ok = false;
1396 return NULL;
1397 }
1398
1399 Expect(Token::NATIVE, CHECK_OK);
1400 Expect(Token::FUNCTION, CHECK_OK);
1401 Handle<String> name = ParseIdentifier(CHECK_OK);
1402 Expect(Token::LPAREN, CHECK_OK);
1403 bool done = (peek() == Token::RPAREN);
1404 while (!done) {
1405 ParseIdentifier(CHECK_OK);
1406 done = (peek() == Token::RPAREN);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001407 if (!done) {
1408 Expect(Token::COMMA, CHECK_OK);
1409 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 }
1411 Expect(Token::RPAREN, CHECK_OK);
1412 Expect(Token::SEMICOLON, CHECK_OK);
1413
Steve Blocka7e24c12009-10-30 11:49:00 +00001414 // Make sure that the function containing the native declaration
1415 // isn't lazily compiled. The extension structures are only
1416 // accessible while parsing the first time not when reparsing
1417 // because of lazy compilation.
1418 top_scope_->ForceEagerCompilation();
1419
1420 // Compute the function template for the native function.
1421 v8::Handle<v8::FunctionTemplate> fun_template =
1422 extension_->GetNativeFunction(v8::Utils::ToLocal(name));
1423 ASSERT(!fun_template.IsEmpty());
1424
Steve Block6ded16b2010-05-10 14:33:55 +01001425 // Instantiate the function and create a shared function info from it.
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
1427 const int literals = fun->NumberOfLiterals();
1428 Handle<Code> code = Handle<Code>(fun->shared()->code());
Leon Clarkee46be812010-01-19 14:06:41 +00001429 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
Steve Block6ded16b2010-05-10 14:33:55 +01001430 Handle<SharedFunctionInfo> shared =
Steve Block44f0eee2011-05-26 01:26:41 +01001431 isolate()->factory()->NewSharedFunctionInfo(name, literals, code,
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001432 Handle<SerializedScopeInfo>(fun->shared()->scope_info()));
Steve Block6ded16b2010-05-10 14:33:55 +01001433 shared->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001434
Steve Block6ded16b2010-05-10 14:33:55 +01001435 // Copy the function data to the shared function info.
1436 shared->set_function_data(fun->shared()->function_data());
Steve Blocka7e24c12009-10-30 11:49:00 +00001437 int parameters = fun->shared()->formal_parameter_count();
Steve Block6ded16b2010-05-10 14:33:55 +01001438 shared->set_formal_parameter_count(parameters);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439
1440 // TODO(1240846): It's weird that native function declarations are
1441 // introduced dynamically when we meet their declarations, whereas
1442 // other functions are setup when entering the surrounding scope.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001443 SharedFunctionInfoLiteral* lit =
1444 new(zone()) SharedFunctionInfoLiteral(shared);
Steve Blocka7e24c12009-10-30 11:49:00 +00001445 VariableProxy* var = Declare(name, Variable::VAR, NULL, true, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001446 return new(zone()) ExpressionStatement(new(zone()) Assignment(
1447 Token::INIT_VAR, var, lit, RelocInfo::kNoPosition));
Steve Blocka7e24c12009-10-30 11:49:00 +00001448}
1449
1450
1451Statement* Parser::ParseFunctionDeclaration(bool* ok) {
1452 // FunctionDeclaration ::
1453 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1454 Expect(Token::FUNCTION, CHECK_OK);
1455 int function_token_position = scanner().location().beg_pos;
Steve Block1e0659c2011-05-24 12:43:12 +01001456 bool is_reserved = false;
1457 Handle<String> name = ParseIdentifierOrReservedWord(&is_reserved, CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00001458 FunctionLiteral* fun = ParseFunctionLiteral(name,
Steve Block1e0659c2011-05-24 12:43:12 +01001459 is_reserved,
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 function_token_position,
1461 DECLARATION,
1462 CHECK_OK);
1463 // Even if we're not at the top-level of the global or a function
1464 // scope, we treat is as such and introduce the function with it's
1465 // initial value upon entering the corresponding scope.
1466 Declare(name, Variable::VAR, fun, true, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001467 return EmptyStatement();
Steve Blocka7e24c12009-10-30 11:49:00 +00001468}
1469
1470
1471Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) {
1472 // Block ::
1473 // '{' Statement* '}'
1474
1475 // Note that a Block does not introduce a new execution scope!
1476 // (ECMA-262, 3rd, 12.2)
1477 //
1478 // Construct block expecting 16 statements.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001479 Block* result = new(zone()) Block(labels, 16, false);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001480 Target target(&this->target_stack_, result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 Expect(Token::LBRACE, CHECK_OK);
1482 while (peek() != Token::RBRACE) {
1483 Statement* stat = ParseStatement(NULL, CHECK_OK);
1484 if (stat && !stat->IsEmpty()) result->AddStatement(stat);
1485 }
1486 Expect(Token::RBRACE, CHECK_OK);
1487 return result;
1488}
1489
1490
1491Block* Parser::ParseVariableStatement(bool* ok) {
1492 // VariableStatement ::
1493 // VariableDeclarations ';'
1494
1495 Expression* dummy; // to satisfy the ParseVariableDeclarations() signature
1496 Block* result = ParseVariableDeclarations(true, &dummy, CHECK_OK);
1497 ExpectSemicolon(CHECK_OK);
1498 return result;
1499}
1500
Steve Block44f0eee2011-05-26 01:26:41 +01001501
1502bool Parser::IsEvalOrArguments(Handle<String> string) {
1503 return string.is_identical_to(isolate()->factory()->eval_symbol()) ||
1504 string.is_identical_to(isolate()->factory()->arguments_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +01001505}
Steve Blocka7e24c12009-10-30 11:49:00 +00001506
Steve Block44f0eee2011-05-26 01:26:41 +01001507
Steve Blocka7e24c12009-10-30 11:49:00 +00001508// If the variable declaration declares exactly one non-const
1509// variable, then *var is set to that variable. In all other cases,
1510// *var is untouched; in particular, it is the caller's responsibility
1511// to initialize it properly. This mechanism is used for the parsing
1512// of 'for-in' loops.
1513Block* Parser::ParseVariableDeclarations(bool accept_IN,
1514 Expression** var,
1515 bool* ok) {
1516 // VariableDeclarations ::
1517 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
1518
1519 Variable::Mode mode = Variable::VAR;
1520 bool is_const = false;
1521 if (peek() == Token::VAR) {
1522 Consume(Token::VAR);
1523 } else if (peek() == Token::CONST) {
1524 Consume(Token::CONST);
Steve Block44f0eee2011-05-26 01:26:41 +01001525 if (top_scope_->is_strict_mode()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001526 ReportMessage("strict_const", Vector<const char*>::empty());
1527 *ok = false;
1528 return NULL;
1529 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 mode = Variable::CONST;
1531 is_const = true;
1532 } else {
1533 UNREACHABLE(); // by current callers
1534 }
1535
1536 // The scope of a variable/const declared anywhere inside a function
1537 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
1538 // transform a source-level variable/const declaration into a (Function)
1539 // Scope declaration, and rewrite the source-level initialization into an
1540 // assignment statement. We use a block to collect multiple assignments.
1541 //
1542 // We mark the block as initializer block because we don't want the
1543 // rewriter to add a '.result' assignment to such a block (to get compliant
1544 // behavior for code such as print(eval('var x = 7')), and for cosmetic
1545 // reasons when pretty-printing. Also, unless an assignment (initialization)
1546 // is inside an initializer block, it is ignored.
1547 //
1548 // Create new block with one expected declaration.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001549 Block* block = new(zone()) Block(NULL, 1, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001550 VariableProxy* last_var = NULL; // the last variable declared
1551 int nvars = 0; // the number of variables declared
1552 do {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001553 if (fni_ != NULL) fni_->Enter();
1554
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 // Parse variable name.
1556 if (nvars > 0) Consume(Token::COMMA);
1557 Handle<String> name = ParseIdentifier(CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001558 if (fni_ != NULL) fni_->PushVariableName(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001559
Steve Block1e0659c2011-05-24 12:43:12 +01001560 // Strict mode variables may not be named eval or arguments
Steve Block44f0eee2011-05-26 01:26:41 +01001561 if (top_scope_->is_strict_mode() && IsEvalOrArguments(name)) {
Steve Block1e0659c2011-05-24 12:43:12 +01001562 ReportMessage("strict_var_name", Vector<const char*>::empty());
1563 *ok = false;
1564 return NULL;
1565 }
1566
Steve Blocka7e24c12009-10-30 11:49:00 +00001567 // Declare variable.
1568 // Note that we *always* must treat the initial value via a separate init
1569 // assignment for variables and constants because the value must be assigned
1570 // when the variable is encountered in the source. But the variable/constant
1571 // is declared (and set to 'undefined') upon entering the function within
1572 // which the variable or constant is declared. Only function variables have
1573 // an initial value in the declaration (because they are initialized upon
1574 // entering the function).
1575 //
1576 // If we have a const declaration, in an inner scope, the proxy is always
1577 // bound to the declared variable (independent of possibly surrounding with
1578 // statements).
1579 last_var = Declare(name, mode, NULL,
1580 is_const /* always bound for CONST! */,
1581 CHECK_OK);
1582 nvars++;
Steve Block053d10c2011-06-13 19:13:29 +01001583 if (top_scope_->num_var_or_const() > kMaxNumFunctionLocals) {
1584 ReportMessageAt(scanner().location(), "too_many_variables",
1585 Vector<const char*>::empty());
1586 *ok = false;
1587 return NULL;
1588 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001589
1590 // Parse initialization expression if present and/or needed. A
1591 // declaration of the form:
1592 //
1593 // var v = x;
1594 //
1595 // is syntactic sugar for:
1596 //
1597 // var v; v = x;
1598 //
1599 // In particular, we need to re-lookup 'v' as it may be a
1600 // different 'v' than the 'v' in the declaration (if we are inside
1601 // a 'with' statement that makes a object property with name 'v'
1602 // visible).
1603 //
1604 // However, note that const declarations are different! A const
1605 // declaration of the form:
1606 //
1607 // const c = x;
1608 //
1609 // is *not* syntactic sugar for:
1610 //
1611 // const c; c = x;
1612 //
1613 // The "variable" c initialized to x is the same as the declared
1614 // one - there is no re-lookup (see the last parameter of the
1615 // Declare() call above).
1616
1617 Expression* value = NULL;
1618 int position = -1;
1619 if (peek() == Token::ASSIGN) {
1620 Expect(Token::ASSIGN, CHECK_OK);
1621 position = scanner().location().beg_pos;
1622 value = ParseAssignmentExpression(accept_IN, CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001623 // Don't infer if it is "a = function(){...}();"-like expression.
1624 if (fni_ != NULL && value->AsCall() == NULL) fni_->Infer();
Steve Blocka7e24c12009-10-30 11:49:00 +00001625 }
1626
1627 // Make sure that 'const c' actually initializes 'c' to undefined
1628 // even though it seems like a stupid thing to do.
1629 if (value == NULL && is_const) {
1630 value = GetLiteralUndefined();
1631 }
1632
1633 // Global variable declarations must be compiled in a specific
1634 // way. When the script containing the global variable declaration
1635 // is entered, the global variable must be declared, so that if it
1636 // doesn't exist (not even in a prototype of the global object) it
1637 // gets created with an initial undefined value. This is handled
1638 // by the declarations part of the function representing the
1639 // top-level global code; see Runtime::DeclareGlobalVariable. If
1640 // it already exists (in the object or in a prototype), it is
1641 // *not* touched until the variable declaration statement is
1642 // executed.
1643 //
1644 // Executing the variable declaration statement will always
1645 // guarantee to give the global object a "local" variable; a
1646 // variable defined in the global object and not in any
1647 // prototype. This way, global variable declarations can shadow
1648 // properties in the prototype chain, but only after the variable
1649 // declaration statement has been executed. This is important in
1650 // browsers where the global object (window) has lots of
1651 // properties defined in prototype objects.
1652
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001653 if (top_scope_->is_global_scope()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001654 // Compute the arguments for the runtime call.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001655 ZoneList<Expression*>* arguments = new ZoneList<Expression*>(3);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001656 // We have at least 1 parameter.
1657 arguments->Add(new(zone()) Literal(name));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001658 CallRuntime* initialize;
1659
1660 if (is_const) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001661 arguments->Add(value);
1662 value = NULL; // zap the value to avoid the unnecessary assignment
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001663
1664 // Construct the call to Runtime_InitializeConstGlobal
1665 // and add it to the initialization statement block.
1666 // Note that the function does different things depending on
1667 // the number of arguments (1 or 2).
Steve Blocka7e24c12009-10-30 11:49:00 +00001668 initialize =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001669 new(zone()) CallRuntime(
Steve Block44f0eee2011-05-26 01:26:41 +01001670 isolate()->factory()->InitializeConstGlobal_symbol(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001671 Runtime::FunctionForId(Runtime::kInitializeConstGlobal),
1672 arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001673 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001674 // Add strict mode.
1675 // We may want to pass singleton to avoid Literal allocations.
1676 arguments->Add(NewNumberLiteral(
Steve Block44f0eee2011-05-26 01:26:41 +01001677 top_scope_->is_strict_mode() ? kStrictMode : kNonStrictMode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001678
1679 // Be careful not to assign a value to the global variable if
1680 // we're in a with. The initialization value should not
1681 // necessarily be stored in the global object in that case,
1682 // which is why we need to generate a separate assignment node.
1683 if (value != NULL && !inside_with()) {
1684 arguments->Add(value);
1685 value = NULL; // zap the value to avoid the unnecessary assignment
1686 }
1687
1688 // Construct the call to Runtime_InitializeVarGlobal
1689 // and add it to the initialization statement block.
1690 // Note that the function does different things depending on
1691 // the number of arguments (2 or 3).
Steve Blocka7e24c12009-10-30 11:49:00 +00001692 initialize =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001693 new(zone()) CallRuntime(
Steve Block44f0eee2011-05-26 01:26:41 +01001694 isolate()->factory()->InitializeVarGlobal_symbol(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001695 Runtime::FunctionForId(Runtime::kInitializeVarGlobal),
1696 arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001697 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001698
Ben Murdoch8b112d22011-06-08 16:22:53 +01001699 block->AddStatement(new(zone()) ExpressionStatement(initialize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001700 }
1701
1702 // Add an assignment node to the initialization statement block if
1703 // we still have a pending initialization value. We must distinguish
1704 // between variables and constants: Variable initializations are simply
1705 // assignments (with all the consequences if they are inside a 'with'
1706 // statement - they may change a 'with' object property). Constant
1707 // initializations always assign to the declared constant which is
1708 // always at the function scope level. This is only relevant for
1709 // dynamically looked-up variables and constants (the start context
1710 // for constant lookups is always the function context, while it is
1711 // the top context for variables). Sigh...
1712 if (value != NULL) {
1713 Token::Value op = (is_const ? Token::INIT_CONST : Token::INIT_VAR);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001714 Assignment* assignment =
1715 new(zone()) Assignment(op, last_var, value, position);
1716 if (block) {
1717 block->AddStatement(new(zone()) ExpressionStatement(assignment));
1718 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001719 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001720
1721 if (fni_ != NULL) fni_->Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +00001722 } while (peek() == Token::COMMA);
1723
1724 if (!is_const && nvars == 1) {
1725 // We have a single, non-const variable.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001726 ASSERT(last_var != NULL);
1727 *var = last_var;
Steve Blocka7e24c12009-10-30 11:49:00 +00001728 }
1729
1730 return block;
1731}
1732
1733
1734static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) {
1735 ASSERT(!label.is_null());
1736 if (labels != NULL)
1737 for (int i = labels->length(); i-- > 0; )
1738 if (labels->at(i).is_identical_to(label))
1739 return true;
1740
1741 return false;
1742}
1743
1744
1745Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
1746 bool* ok) {
1747 // ExpressionStatement | LabelledStatement ::
1748 // Expression ';'
1749 // Identifier ':' Statement
Steve Block1e0659c2011-05-24 12:43:12 +01001750 bool starts_with_idenfifier = peek_any_identifier();
Steve Blocka7e24c12009-10-30 11:49:00 +00001751 Expression* expr = ParseExpression(true, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001752 if (peek() == Token::COLON && starts_with_idenfifier && expr &&
Steve Blocka7e24c12009-10-30 11:49:00 +00001753 expr->AsVariableProxy() != NULL &&
1754 !expr->AsVariableProxy()->is_this()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001755 // Expression is a single identifier, and not, e.g., a parenthesized
1756 // identifier.
Steve Blocka7e24c12009-10-30 11:49:00 +00001757 VariableProxy* var = expr->AsVariableProxy();
1758 Handle<String> label = var->name();
1759 // TODO(1240780): We don't check for redeclaration of labels
1760 // during preparsing since keeping track of the set of active
1761 // labels requires nontrivial changes to the way scopes are
1762 // structured. However, these are probably changes we want to
1763 // make later anyway so we should go back and fix this then.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001764 if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
1765 SmartPointer<char> c_string = label->ToCString(DISALLOW_NULLS);
1766 const char* elms[2] = { "Label", *c_string };
1767 Vector<const char*> args(elms, 2);
1768 ReportMessage("redeclaration", args);
1769 *ok = false;
1770 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001771 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001772 if (labels == NULL) labels = new ZoneStringList(4);
1773 labels->Add(label);
1774 // Remove the "ghost" variable that turned out to be a label
1775 // from the top scope. This way, we don't try to resolve it
1776 // during the scope processing.
1777 top_scope_->RemoveUnresolved(var);
Steve Blocka7e24c12009-10-30 11:49:00 +00001778 Expect(Token::COLON, CHECK_OK);
1779 return ParseStatement(labels, ok);
1780 }
1781
1782 // Parsed expression statement.
1783 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001784 return new(zone()) ExpressionStatement(expr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001785}
1786
1787
1788IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) {
1789 // IfStatement ::
1790 // 'if' '(' Expression ')' Statement ('else' Statement)?
1791
1792 Expect(Token::IF, CHECK_OK);
1793 Expect(Token::LPAREN, CHECK_OK);
1794 Expression* condition = ParseExpression(true, CHECK_OK);
1795 Expect(Token::RPAREN, CHECK_OK);
1796 Statement* then_statement = ParseStatement(labels, CHECK_OK);
1797 Statement* else_statement = NULL;
1798 if (peek() == Token::ELSE) {
1799 Next();
1800 else_statement = ParseStatement(labels, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001801 } else {
1802 else_statement = EmptyStatement();
Steve Blocka7e24c12009-10-30 11:49:00 +00001803 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001804 return new(zone()) IfStatement(condition, then_statement, else_statement);
Steve Blocka7e24c12009-10-30 11:49:00 +00001805}
1806
1807
1808Statement* Parser::ParseContinueStatement(bool* ok) {
1809 // ContinueStatement ::
1810 // 'continue' Identifier? ';'
1811
1812 Expect(Token::CONTINUE, CHECK_OK);
1813 Handle<String> label = Handle<String>::null();
1814 Token::Value tok = peek();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001815 if (!scanner().has_line_terminator_before_next() &&
Steve Blocka7e24c12009-10-30 11:49:00 +00001816 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
1817 label = ParseIdentifier(CHECK_OK);
1818 }
1819 IterationStatement* target = NULL;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001820 target = LookupContinueTarget(label, CHECK_OK);
1821 if (target == NULL) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001822 // Illegal continue statement.
1823 const char* message = "illegal_continue";
1824 Vector<Handle<String> > args;
1825 if (!label.is_null()) {
1826 message = "unknown_label";
1827 args = Vector<Handle<String> >(&label, 1);
1828 }
1829 ReportMessageAt(scanner().location(), message, args);
1830 *ok = false;
1831 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001832 }
1833 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001834 return new(zone()) ContinueStatement(target);
Steve Blocka7e24c12009-10-30 11:49:00 +00001835}
1836
1837
1838Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) {
1839 // BreakStatement ::
1840 // 'break' Identifier? ';'
1841
1842 Expect(Token::BREAK, CHECK_OK);
1843 Handle<String> label;
1844 Token::Value tok = peek();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001845 if (!scanner().has_line_terminator_before_next() &&
Steve Blocka7e24c12009-10-30 11:49:00 +00001846 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
1847 label = ParseIdentifier(CHECK_OK);
1848 }
1849 // Parse labeled break statements that target themselves into
1850 // empty statements, e.g. 'l1: l2: l3: break l2;'
1851 if (!label.is_null() && ContainsLabel(labels, label)) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001852 return EmptyStatement();
Steve Blocka7e24c12009-10-30 11:49:00 +00001853 }
1854 BreakableStatement* target = NULL;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001855 target = LookupBreakTarget(label, CHECK_OK);
1856 if (target == NULL) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01001857 // Illegal break statement.
1858 const char* message = "illegal_break";
1859 Vector<Handle<String> > args;
1860 if (!label.is_null()) {
1861 message = "unknown_label";
1862 args = Vector<Handle<String> >(&label, 1);
1863 }
1864 ReportMessageAt(scanner().location(), message, args);
1865 *ok = false;
1866 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001867 }
1868 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001869 return new(zone()) BreakStatement(target);
Steve Blocka7e24c12009-10-30 11:49:00 +00001870}
1871
1872
1873Statement* Parser::ParseReturnStatement(bool* ok) {
1874 // ReturnStatement ::
1875 // 'return' Expression? ';'
1876
1877 // Consume the return token. It is necessary to do the before
1878 // reporting any errors on it, because of the way errors are
1879 // reported (underlining).
1880 Expect(Token::RETURN, CHECK_OK);
1881
1882 // An ECMAScript program is considered syntactically incorrect if it
1883 // contains a return statement that is not within the body of a
1884 // function. See ECMA-262, section 12.9, page 67.
1885 //
1886 // To be consistent with KJS we report the syntax error at runtime.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001887 if (!top_scope_->is_function_scope()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001888 Handle<String> type = isolate()->factory()->illegal_return_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001889 Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001890 return new(zone()) ExpressionStatement(throw_error);
Steve Blocka7e24c12009-10-30 11:49:00 +00001891 }
1892
1893 Token::Value tok = peek();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001894 if (scanner().has_line_terminator_before_next() ||
Steve Blocka7e24c12009-10-30 11:49:00 +00001895 tok == Token::SEMICOLON ||
1896 tok == Token::RBRACE ||
1897 tok == Token::EOS) {
1898 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001899 return new(zone()) ReturnStatement(GetLiteralUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +00001900 }
1901
1902 Expression* expr = ParseExpression(true, CHECK_OK);
1903 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001904 return new(zone()) ReturnStatement(expr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001905}
1906
1907
1908Block* Parser::WithHelper(Expression* obj,
1909 ZoneStringList* labels,
1910 bool is_catch_block,
1911 bool* ok) {
1912 // Parse the statement and collect escaping labels.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001913 ZoneList<Label*>* target_list = new ZoneList<Label*>(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001914 TargetCollector collector(target_list);
1915 Statement* stat;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001916 { Target target(&this->target_stack_, &collector);
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 with_nesting_level_++;
1918 top_scope_->RecordWithStatement();
1919 stat = ParseStatement(labels, CHECK_OK);
1920 with_nesting_level_--;
1921 }
1922 // Create resulting block with two statements.
1923 // 1: Evaluate the with expression.
1924 // 2: The try-finally block evaluating the body.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001925 Block* result = new(zone()) Block(NULL, 2, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001926
1927 if (result != NULL) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001928 result->AddStatement(new(zone()) WithEnterStatement(obj, is_catch_block));
Steve Blocka7e24c12009-10-30 11:49:00 +00001929
1930 // Create body block.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001931 Block* body = new(zone()) Block(NULL, 1, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001932 body->AddStatement(stat);
1933
1934 // Create exit block.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001935 Block* exit = new(zone()) Block(NULL, 1, false);
1936 exit->AddStatement(new(zone()) WithExitStatement());
Steve Blocka7e24c12009-10-30 11:49:00 +00001937
1938 // Return a try-finally statement.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001939 TryFinallyStatement* wrapper = new(zone()) TryFinallyStatement(body, exit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001940 wrapper->set_escaping_targets(collector.targets());
1941 result->AddStatement(wrapper);
1942 }
1943 return result;
1944}
1945
1946
1947Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
1948 // WithStatement ::
1949 // 'with' '(' Expression ')' Statement
1950
1951 Expect(Token::WITH, CHECK_OK);
Steve Block1e0659c2011-05-24 12:43:12 +01001952
Steve Block44f0eee2011-05-26 01:26:41 +01001953 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01001954 ReportMessage("strict_mode_with", Vector<const char*>::empty());
1955 *ok = false;
1956 return NULL;
1957 }
1958
Steve Blocka7e24c12009-10-30 11:49:00 +00001959 Expect(Token::LPAREN, CHECK_OK);
1960 Expression* expr = ParseExpression(true, CHECK_OK);
1961 Expect(Token::RPAREN, CHECK_OK);
1962
1963 return WithHelper(expr, labels, false, CHECK_OK);
1964}
1965
1966
1967CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
1968 // CaseClause ::
1969 // 'case' Expression ':' Statement*
1970 // 'default' ':' Statement*
1971
1972 Expression* label = NULL; // NULL expression indicates default case
1973 if (peek() == Token::CASE) {
1974 Expect(Token::CASE, CHECK_OK);
1975 label = ParseExpression(true, CHECK_OK);
1976 } else {
1977 Expect(Token::DEFAULT, CHECK_OK);
1978 if (*default_seen_ptr) {
1979 ReportMessage("multiple_defaults_in_switch",
1980 Vector<const char*>::empty());
1981 *ok = false;
1982 return NULL;
1983 }
1984 *default_seen_ptr = true;
1985 }
1986 Expect(Token::COLON, CHECK_OK);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001987 int pos = scanner().location().beg_pos;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001988 ZoneList<Statement*>* statements = new ZoneList<Statement*>(5);
Steve Blocka7e24c12009-10-30 11:49:00 +00001989 while (peek() != Token::CASE &&
1990 peek() != Token::DEFAULT &&
1991 peek() != Token::RBRACE) {
1992 Statement* stat = ParseStatement(NULL, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001993 statements->Add(stat);
Steve Blocka7e24c12009-10-30 11:49:00 +00001994 }
1995
Ben Murdoch8b112d22011-06-08 16:22:53 +01001996 return new(zone()) CaseClause(label, statements, pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00001997}
1998
1999
2000SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels,
2001 bool* ok) {
2002 // SwitchStatement ::
2003 // 'switch' '(' Expression ')' '{' CaseClause* '}'
2004
Ben Murdoch8b112d22011-06-08 16:22:53 +01002005 SwitchStatement* statement = new(zone()) SwitchStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002006 Target target(&this->target_stack_, statement);
Steve Blocka7e24c12009-10-30 11:49:00 +00002007
2008 Expect(Token::SWITCH, CHECK_OK);
2009 Expect(Token::LPAREN, CHECK_OK);
2010 Expression* tag = ParseExpression(true, CHECK_OK);
2011 Expect(Token::RPAREN, CHECK_OK);
2012
2013 bool default_seen = false;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002014 ZoneList<CaseClause*>* cases = new ZoneList<CaseClause*>(4);
Steve Blocka7e24c12009-10-30 11:49:00 +00002015 Expect(Token::LBRACE, CHECK_OK);
2016 while (peek() != Token::RBRACE) {
2017 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002018 cases->Add(clause);
Steve Blocka7e24c12009-10-30 11:49:00 +00002019 }
2020 Expect(Token::RBRACE, CHECK_OK);
2021
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002022 if (statement) statement->Initialize(tag, cases);
Steve Blocka7e24c12009-10-30 11:49:00 +00002023 return statement;
2024}
2025
2026
2027Statement* Parser::ParseThrowStatement(bool* ok) {
2028 // ThrowStatement ::
2029 // 'throw' Expression ';'
2030
2031 Expect(Token::THROW, CHECK_OK);
2032 int pos = scanner().location().beg_pos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002033 if (scanner().has_line_terminator_before_next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 ReportMessage("newline_after_throw", Vector<const char*>::empty());
2035 *ok = false;
2036 return NULL;
2037 }
2038 Expression* exception = ParseExpression(true, CHECK_OK);
2039 ExpectSemicolon(CHECK_OK);
2040
Ben Murdoch8b112d22011-06-08 16:22:53 +01002041 return new(zone()) ExpressionStatement(new(zone()) Throw(exception, pos));
Steve Blocka7e24c12009-10-30 11:49:00 +00002042}
2043
2044
2045TryStatement* Parser::ParseTryStatement(bool* ok) {
2046 // TryStatement ::
2047 // 'try' Block Catch
2048 // 'try' Block Finally
2049 // 'try' Block Catch Finally
2050 //
2051 // Catch ::
2052 // 'catch' '(' Identifier ')' Block
2053 //
2054 // Finally ::
2055 // 'finally' Block
2056
2057 Expect(Token::TRY, CHECK_OK);
2058
Ben Murdoch8b112d22011-06-08 16:22:53 +01002059 ZoneList<Label*>* target_list = new ZoneList<Label*>(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002060 TargetCollector collector(target_list);
2061 Block* try_block;
2062
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002063 { Target target(&this->target_stack_, &collector);
Steve Blocka7e24c12009-10-30 11:49:00 +00002064 try_block = ParseBlock(NULL, CHECK_OK);
2065 }
2066
2067 Block* catch_block = NULL;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002068 Variable* catch_var = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00002069 Block* finally_block = NULL;
2070
2071 Token::Value tok = peek();
2072 if (tok != Token::CATCH && tok != Token::FINALLY) {
2073 ReportMessage("no_catch_or_finally", Vector<const char*>::empty());
2074 *ok = false;
2075 return NULL;
2076 }
2077
2078 // If we can break out from the catch block and there is a finally block,
2079 // then we will need to collect jump targets from the catch block. Since
2080 // we don't know yet if there will be a finally block, we always collect
2081 // the jump targets.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002082 ZoneList<Label*>* catch_target_list = new ZoneList<Label*>(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002083 TargetCollector catch_collector(catch_target_list);
2084 bool has_catch = false;
2085 if (tok == Token::CATCH) {
2086 has_catch = true;
2087 Consume(Token::CATCH);
2088
2089 Expect(Token::LPAREN, CHECK_OK);
2090 Handle<String> name = ParseIdentifier(CHECK_OK);
Steve Block1e0659c2011-05-24 12:43:12 +01002091
Steve Block44f0eee2011-05-26 01:26:41 +01002092 if (top_scope_->is_strict_mode() && IsEvalOrArguments(name)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002093 ReportMessage("strict_catch_variable", Vector<const char*>::empty());
2094 *ok = false;
2095 return NULL;
2096 }
2097
Steve Blocka7e24c12009-10-30 11:49:00 +00002098 Expect(Token::RPAREN, CHECK_OK);
2099
2100 if (peek() == Token::LBRACE) {
2101 // Allocate a temporary for holding the finally state while
2102 // executing the finally block.
Steve Block44f0eee2011-05-26 01:26:41 +01002103 catch_var =
2104 top_scope_->NewTemporary(isolate()->factory()->catch_var_symbol());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002105 Literal* name_literal = new(zone()) Literal(name);
2106 VariableProxy* catch_var_use = new(zone()) VariableProxy(catch_var);
2107 Expression* obj =
2108 new(zone()) CatchExtensionObject(name_literal, catch_var_use);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002109 { Target target(&this->target_stack_, &catch_collector);
Steve Blocka7e24c12009-10-30 11:49:00 +00002110 catch_block = WithHelper(obj, NULL, true, CHECK_OK);
2111 }
2112 } else {
2113 Expect(Token::LBRACE, CHECK_OK);
2114 }
2115
2116 tok = peek();
2117 }
2118
2119 if (tok == Token::FINALLY || !has_catch) {
2120 Consume(Token::FINALLY);
2121 // Declare a variable for holding the finally state while
2122 // executing the finally block.
2123 finally_block = ParseBlock(NULL, CHECK_OK);
2124 }
2125
2126 // Simplify the AST nodes by converting:
2127 // 'try { } catch { } finally { }'
2128 // to:
2129 // 'try { try { } catch { } } finally { }'
2130
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002131 if (catch_block != NULL && finally_block != NULL) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002132 VariableProxy* catch_var_defn = new(zone()) VariableProxy(catch_var);
Steve Block3ce2e202009-11-05 08:53:23 +00002133 TryCatchStatement* statement =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002134 new(zone()) TryCatchStatement(try_block, catch_var_defn, catch_block);
Steve Blocka7e24c12009-10-30 11:49:00 +00002135 statement->set_escaping_targets(collector.targets());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002136 try_block = new(zone()) Block(NULL, 1, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 try_block->AddStatement(statement);
2138 catch_block = NULL;
2139 }
2140
2141 TryStatement* result = NULL;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002142 if (catch_block != NULL) {
2143 ASSERT(finally_block == NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002144 VariableProxy* catch_var_defn = new(zone()) VariableProxy(catch_var);
2145 result =
2146 new(zone()) TryCatchStatement(try_block, catch_var_defn, catch_block);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002147 result->set_escaping_targets(collector.targets());
2148 } else {
2149 ASSERT(finally_block != NULL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002150 result = new(zone()) TryFinallyStatement(try_block, finally_block);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002151 // Add the jump targets of the try block and the catch block.
2152 for (int i = 0; i < collector.targets()->length(); i++) {
2153 catch_collector.AddTarget(collector.targets()->at(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00002154 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002155 result->set_escaping_targets(catch_collector.targets());
Steve Blocka7e24c12009-10-30 11:49:00 +00002156 }
2157
2158 return result;
2159}
2160
2161
Steve Block3ce2e202009-11-05 08:53:23 +00002162DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels,
2163 bool* ok) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002164 // DoStatement ::
2165 // 'do' Statement 'while' '(' Expression ')' ';'
2166
Ben Murdoch8b112d22011-06-08 16:22:53 +01002167 DoWhileStatement* loop = new(zone()) DoWhileStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002168 Target target(&this->target_stack_, loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00002169
2170 Expect(Token::DO, CHECK_OK);
2171 Statement* body = ParseStatement(NULL, CHECK_OK);
2172 Expect(Token::WHILE, CHECK_OK);
2173 Expect(Token::LPAREN, CHECK_OK);
Steve Blockd0582a62009-12-15 09:54:21 +00002174
2175 if (loop != NULL) {
2176 int position = scanner().location().beg_pos;
2177 loop->set_condition_position(position);
2178 }
2179
Steve Blocka7e24c12009-10-30 11:49:00 +00002180 Expression* cond = ParseExpression(true, CHECK_OK);
2181 Expect(Token::RPAREN, CHECK_OK);
2182
2183 // Allow do-statements to be terminated with and without
2184 // semi-colons. This allows code such as 'do;while(0)return' to
2185 // parse, which would not be the case if we had used the
2186 // ExpectSemicolon() functionality here.
2187 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
2188
Steve Block3ce2e202009-11-05 08:53:23 +00002189 if (loop != NULL) loop->Initialize(cond, body);
Steve Blocka7e24c12009-10-30 11:49:00 +00002190 return loop;
2191}
2192
2193
Steve Block3ce2e202009-11-05 08:53:23 +00002194WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002195 // WhileStatement ::
2196 // 'while' '(' Expression ')' Statement
2197
Ben Murdoch8b112d22011-06-08 16:22:53 +01002198 WhileStatement* loop = new(zone()) WhileStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002199 Target target(&this->target_stack_, loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00002200
2201 Expect(Token::WHILE, CHECK_OK);
2202 Expect(Token::LPAREN, CHECK_OK);
2203 Expression* cond = ParseExpression(true, CHECK_OK);
2204 Expect(Token::RPAREN, CHECK_OK);
2205 Statement* body = ParseStatement(NULL, CHECK_OK);
2206
Steve Block3ce2e202009-11-05 08:53:23 +00002207 if (loop != NULL) loop->Initialize(cond, body);
Steve Blocka7e24c12009-10-30 11:49:00 +00002208 return loop;
2209}
2210
2211
2212Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2213 // ForStatement ::
2214 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2215
2216 Statement* init = NULL;
2217
2218 Expect(Token::FOR, CHECK_OK);
2219 Expect(Token::LPAREN, CHECK_OK);
2220 if (peek() != Token::SEMICOLON) {
2221 if (peek() == Token::VAR || peek() == Token::CONST) {
2222 Expression* each = NULL;
2223 Block* variable_statement =
2224 ParseVariableDeclarations(false, &each, CHECK_OK);
2225 if (peek() == Token::IN && each != NULL) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002226 ForInStatement* loop = new(zone()) ForInStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002227 Target target(&this->target_stack_, loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00002228
2229 Expect(Token::IN, CHECK_OK);
2230 Expression* enumerable = ParseExpression(true, CHECK_OK);
2231 Expect(Token::RPAREN, CHECK_OK);
2232
2233 Statement* body = ParseStatement(NULL, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002234 loop->Initialize(each, enumerable, body);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002235 Block* result = new(zone()) Block(NULL, 2, false);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002236 result->AddStatement(variable_statement);
2237 result->AddStatement(loop);
2238 // Parsed for-in loop w/ variable/const declaration.
2239 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002240 } else {
2241 init = variable_statement;
2242 }
2243
2244 } else {
2245 Expression* expression = ParseExpression(false, CHECK_OK);
2246 if (peek() == Token::IN) {
2247 // Signal a reference error if the expression is an invalid
2248 // left-hand side expression. We could report this as a syntax
2249 // error here but for compatibility with JSC we choose to report
2250 // the error at runtime.
2251 if (expression == NULL || !expression->IsValidLeftHandSide()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002252 Handle<String> type =
2253 isolate()->factory()->invalid_lhs_in_for_in_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00002254 expression = NewThrowReferenceError(type);
2255 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01002256 ForInStatement* loop = new(zone()) ForInStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002257 Target target(&this->target_stack_, loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00002258
2259 Expect(Token::IN, CHECK_OK);
2260 Expression* enumerable = ParseExpression(true, CHECK_OK);
2261 Expect(Token::RPAREN, CHECK_OK);
2262
2263 Statement* body = ParseStatement(NULL, CHECK_OK);
2264 if (loop) loop->Initialize(expression, enumerable, body);
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 // Parsed for-in loop.
2266 return loop;
2267
2268 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002269 init = new(zone()) ExpressionStatement(expression);
Steve Blocka7e24c12009-10-30 11:49:00 +00002270 }
2271 }
2272 }
2273
2274 // Standard 'for' loop
Ben Murdoch8b112d22011-06-08 16:22:53 +01002275 ForStatement* loop = new(zone()) ForStatement(labels);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002276 Target target(&this->target_stack_, loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00002277
2278 // Parsed initializer at this point.
2279 Expect(Token::SEMICOLON, CHECK_OK);
2280
2281 Expression* cond = NULL;
2282 if (peek() != Token::SEMICOLON) {
2283 cond = ParseExpression(true, CHECK_OK);
2284 }
2285 Expect(Token::SEMICOLON, CHECK_OK);
2286
2287 Statement* next = NULL;
2288 if (peek() != Token::RPAREN) {
2289 Expression* exp = ParseExpression(true, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002290 next = new(zone()) ExpressionStatement(exp);
Steve Blocka7e24c12009-10-30 11:49:00 +00002291 }
2292 Expect(Token::RPAREN, CHECK_OK);
2293
2294 Statement* body = ParseStatement(NULL, CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00002295 if (loop) loop->Initialize(init, cond, next, body);
2296 return loop;
2297}
2298
2299
2300// Precedence = 1
2301Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
2302 // Expression ::
2303 // AssignmentExpression
2304 // Expression ',' AssignmentExpression
2305
2306 Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK);
2307 while (peek() == Token::COMMA) {
2308 Expect(Token::COMMA, CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002309 int position = scanner().location().beg_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002311 result = new(zone()) BinaryOperation(Token::COMMA, result, right, position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002312 }
2313 return result;
2314}
2315
2316
2317// Precedence = 2
2318Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
2319 // AssignmentExpression ::
2320 // ConditionalExpression
2321 // LeftHandSideExpression AssignmentOperator AssignmentExpression
2322
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002323 if (fni_ != NULL) fni_->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00002324 Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK);
2325
2326 if (!Token::IsAssignmentOp(peek())) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002327 if (fni_ != NULL) fni_->Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +00002328 // Parsed conditional expression only (no assignment).
2329 return expression;
2330 }
2331
2332 // Signal a reference error if the expression is an invalid left-hand
2333 // side expression. We could report this as a syntax error here but
2334 // for compatibility with JSC we choose to report the error at
2335 // runtime.
2336 if (expression == NULL || !expression->IsValidLeftHandSide()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002337 Handle<String> type =
2338 isolate()->factory()->invalid_lhs_in_assignment_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00002339 expression = NewThrowReferenceError(type);
2340 }
2341
Steve Block44f0eee2011-05-26 01:26:41 +01002342 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002343 // Assignment to eval or arguments is disallowed in strict mode.
2344 CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK);
2345 }
2346
Steve Blocka7e24c12009-10-30 11:49:00 +00002347 Token::Value op = Next(); // Get assignment operator.
2348 int pos = scanner().location().beg_pos;
2349 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
2350
2351 // TODO(1231235): We try to estimate the set of properties set by
2352 // constructors. We define a new property whenever there is an
2353 // assignment to a property of 'this'. We should probably only add
2354 // properties if we haven't seen them before. Otherwise we'll
2355 // probably overestimate the number of properties.
2356 Property* property = expression ? expression->AsProperty() : NULL;
2357 if (op == Token::ASSIGN &&
2358 property != NULL &&
2359 property->obj()->AsVariableProxy() != NULL &&
2360 property->obj()->AsVariableProxy()->is_this()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002361 lexical_scope_->AddProperty();
Steve Blocka7e24c12009-10-30 11:49:00 +00002362 }
2363
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002364 // If we assign a function literal to a property we pretenure the
2365 // literal so it can be added as a constant function property.
2366 if (property != NULL && right->AsFunctionLiteral() != NULL) {
2367 right->AsFunctionLiteral()->set_pretenure(true);
2368 }
2369
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002370 if (fni_ != NULL) {
2371 // Check if the right hand side is a call to avoid inferring a
2372 // name if we're dealing with "a = function(){...}();"-like
2373 // expression.
2374 if ((op == Token::INIT_VAR
2375 || op == Token::INIT_CONST
2376 || op == Token::ASSIGN)
2377 && (right->AsCall() == NULL)) {
2378 fni_->Infer();
2379 }
2380 fni_->Leave();
2381 }
2382
Ben Murdoch8b112d22011-06-08 16:22:53 +01002383 return new(zone()) Assignment(op, expression, right, pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00002384}
2385
2386
2387// Precedence = 3
2388Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) {
2389 // ConditionalExpression ::
2390 // LogicalOrExpression
2391 // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
2392
2393 // We start using the binary expression parser for prec >= 4 only!
2394 Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK);
2395 if (peek() != Token::CONDITIONAL) return expression;
2396 Consume(Token::CONDITIONAL);
2397 // In parsing the first assignment expression in conditional
2398 // expressions we always accept the 'in' keyword; see ECMA-262,
2399 // section 11.12, page 58.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002400 int left_position = scanner().peek_location().beg_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00002401 Expression* left = ParseAssignmentExpression(true, CHECK_OK);
2402 Expect(Token::COLON, CHECK_OK);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002403 int right_position = scanner().peek_location().beg_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00002404 Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002405 return new(zone()) Conditional(expression, left, right,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002406 left_position, right_position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002407}
2408
2409
2410static int Precedence(Token::Value tok, bool accept_IN) {
2411 if (tok == Token::IN && !accept_IN)
2412 return 0; // 0 precedence will terminate binary expression parsing
2413
2414 return Token::Precedence(tok);
2415}
2416
2417
2418// Precedence >= 4
2419Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) {
2420 ASSERT(prec >= 4);
2421 Expression* x = ParseUnaryExpression(CHECK_OK);
2422 for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
2423 // prec1 >= 4
2424 while (Precedence(peek(), accept_IN) == prec1) {
2425 Token::Value op = Next();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002426 int position = scanner().location().beg_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00002427 Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK);
2428
2429 // Compute some expressions involving only number literals.
2430 if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber() &&
2431 y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) {
2432 double x_val = x->AsLiteral()->handle()->Number();
2433 double y_val = y->AsLiteral()->handle()->Number();
2434
2435 switch (op) {
2436 case Token::ADD:
2437 x = NewNumberLiteral(x_val + y_val);
2438 continue;
2439 case Token::SUB:
2440 x = NewNumberLiteral(x_val - y_val);
2441 continue;
2442 case Token::MUL:
2443 x = NewNumberLiteral(x_val * y_val);
2444 continue;
2445 case Token::DIV:
2446 x = NewNumberLiteral(x_val / y_val);
2447 continue;
2448 case Token::BIT_OR:
2449 x = NewNumberLiteral(DoubleToInt32(x_val) | DoubleToInt32(y_val));
2450 continue;
2451 case Token::BIT_AND:
2452 x = NewNumberLiteral(DoubleToInt32(x_val) & DoubleToInt32(y_val));
2453 continue;
2454 case Token::BIT_XOR:
2455 x = NewNumberLiteral(DoubleToInt32(x_val) ^ DoubleToInt32(y_val));
2456 continue;
2457 case Token::SHL: {
2458 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
2459 x = NewNumberLiteral(value);
2460 continue;
2461 }
2462 case Token::SHR: {
2463 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
2464 uint32_t value = DoubleToUint32(x_val) >> shift;
2465 x = NewNumberLiteral(value);
2466 continue;
2467 }
2468 case Token::SAR: {
2469 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
2470 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
2471 x = NewNumberLiteral(value);
2472 continue;
2473 }
2474 default:
2475 break;
2476 }
2477 }
2478
Steve Blocka7e24c12009-10-30 11:49:00 +00002479 // For now we distinguish between comparisons and other binary
2480 // operations. (We could combine the two and get rid of this
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002481 // code and AST node eventually.)
Steve Blocka7e24c12009-10-30 11:49:00 +00002482 if (Token::IsCompareOp(op)) {
2483 // We have a comparison.
2484 Token::Value cmp = op;
2485 switch (op) {
2486 case Token::NE: cmp = Token::EQ; break;
2487 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
2488 default: break;
2489 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002490 x = NewCompareNode(cmp, x, y, position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002491 if (cmp != op) {
2492 // The comparison was negated - add a NOT.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002493 x = new(zone()) UnaryOperation(Token::NOT, x);
Steve Blocka7e24c12009-10-30 11:49:00 +00002494 }
2495
2496 } else {
2497 // We have a "normal" binary operation.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002498 x = new(zone()) BinaryOperation(op, x, y, position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002499 }
2500 }
2501 }
2502 return x;
2503}
2504
2505
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002506Expression* Parser::NewCompareNode(Token::Value op,
2507 Expression* x,
2508 Expression* y,
2509 int position) {
2510 ASSERT(op != Token::NE && op != Token::NE_STRICT);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002511 if (op == Token::EQ || op == Token::EQ_STRICT) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002512 bool is_strict = (op == Token::EQ_STRICT);
2513 Literal* x_literal = x->AsLiteral();
2514 if (x_literal != NULL && x_literal->IsNull()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002515 return new(zone()) CompareToNull(is_strict, y);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002516 }
2517
2518 Literal* y_literal = y->AsLiteral();
2519 if (y_literal != NULL && y_literal->IsNull()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002520 return new(zone()) CompareToNull(is_strict, x);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002521 }
2522 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01002523 return new(zone()) CompareOperation(op, x, y, position);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002524}
2525
2526
Steve Blocka7e24c12009-10-30 11:49:00 +00002527Expression* Parser::ParseUnaryExpression(bool* ok) {
2528 // UnaryExpression ::
2529 // PostfixExpression
2530 // 'delete' UnaryExpression
2531 // 'void' UnaryExpression
2532 // 'typeof' UnaryExpression
2533 // '++' UnaryExpression
2534 // '--' UnaryExpression
2535 // '+' UnaryExpression
2536 // '-' UnaryExpression
2537 // '~' UnaryExpression
2538 // '!' UnaryExpression
2539
2540 Token::Value op = peek();
2541 if (Token::IsUnaryOp(op)) {
2542 op = Next();
2543 Expression* expression = ParseUnaryExpression(CHECK_OK);
2544
2545 // Compute some expressions involving only number literals.
2546 if (expression != NULL && expression->AsLiteral() &&
2547 expression->AsLiteral()->handle()->IsNumber()) {
2548 double value = expression->AsLiteral()->handle()->Number();
2549 switch (op) {
2550 case Token::ADD:
2551 return expression;
2552 case Token::SUB:
2553 return NewNumberLiteral(-value);
2554 case Token::BIT_NOT:
2555 return NewNumberLiteral(~DoubleToInt32(value));
2556 default: break;
2557 }
2558 }
2559
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002560 // "delete identifier" is a syntax error in strict mode.
Steve Block44f0eee2011-05-26 01:26:41 +01002561 if (op == Token::DELETE && top_scope_->is_strict_mode()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002562 VariableProxy* operand = expression->AsVariableProxy();
2563 if (operand != NULL && !operand->is_this()) {
2564 ReportMessage("strict_delete", Vector<const char*>::empty());
2565 *ok = false;
2566 return NULL;
2567 }
2568 }
2569
Ben Murdoch8b112d22011-06-08 16:22:53 +01002570 return new(zone()) UnaryOperation(op, expression);
Steve Blocka7e24c12009-10-30 11:49:00 +00002571
2572 } else if (Token::IsCountOp(op)) {
2573 op = Next();
2574 Expression* expression = ParseUnaryExpression(CHECK_OK);
2575 // Signal a reference error if the expression is an invalid
2576 // left-hand side expression. We could report this as a syntax
2577 // error here but for compatibility with JSC we choose to report the
2578 // error at runtime.
2579 if (expression == NULL || !expression->IsValidLeftHandSide()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002580 Handle<String> type =
2581 isolate()->factory()->invalid_lhs_in_prefix_op_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00002582 expression = NewThrowReferenceError(type);
2583 }
Steve Block1e0659c2011-05-24 12:43:12 +01002584
Steve Block44f0eee2011-05-26 01:26:41 +01002585 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002586 // Prefix expression operand in strict mode may not be eval or arguments.
2587 CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
2588 }
2589
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002590 int position = scanner().location().beg_pos;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002591 return new(zone()) CountOperation(op,
2592 true /* prefix */,
2593 expression,
2594 position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002595
2596 } else {
2597 return ParsePostfixExpression(ok);
2598 }
2599}
2600
2601
2602Expression* Parser::ParsePostfixExpression(bool* ok) {
2603 // PostfixExpression ::
2604 // LeftHandSideExpression ('++' | '--')?
2605
2606 Expression* expression = ParseLeftHandSideExpression(CHECK_OK);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002607 if (!scanner().has_line_terminator_before_next() &&
2608 Token::IsCountOp(peek())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002609 // Signal a reference error if the expression is an invalid
2610 // left-hand side expression. We could report this as a syntax
2611 // error here but for compatibility with JSC we choose to report the
2612 // error at runtime.
2613 if (expression == NULL || !expression->IsValidLeftHandSide()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002614 Handle<String> type =
2615 isolate()->factory()->invalid_lhs_in_postfix_op_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00002616 expression = NewThrowReferenceError(type);
2617 }
Steve Block1e0659c2011-05-24 12:43:12 +01002618
Steve Block44f0eee2011-05-26 01:26:41 +01002619 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01002620 // Postfix expression operand in strict mode may not be eval or arguments.
2621 CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
2622 }
2623
Steve Blocka7e24c12009-10-30 11:49:00 +00002624 Token::Value next = Next();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002625 int position = scanner().location().beg_pos;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002626 expression =
2627 new(zone()) CountOperation(next,
2628 false /* postfix */,
2629 expression,
2630 position);
Steve Blocka7e24c12009-10-30 11:49:00 +00002631 }
2632 return expression;
2633}
2634
2635
2636Expression* Parser::ParseLeftHandSideExpression(bool* ok) {
2637 // LeftHandSideExpression ::
2638 // (NewExpression | MemberExpression) ...
2639
2640 Expression* result;
2641 if (peek() == Token::NEW) {
2642 result = ParseNewExpression(CHECK_OK);
2643 } else {
2644 result = ParseMemberExpression(CHECK_OK);
2645 }
2646
2647 while (true) {
2648 switch (peek()) {
2649 case Token::LBRACK: {
2650 Consume(Token::LBRACK);
2651 int pos = scanner().location().beg_pos;
2652 Expression* index = ParseExpression(true, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002653 result = new(zone()) Property(result, index, pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00002654 Expect(Token::RBRACK, CHECK_OK);
2655 break;
2656 }
2657
2658 case Token::LPAREN: {
2659 int pos = scanner().location().beg_pos;
2660 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
2661
2662 // Keep track of eval() calls since they disable all local variable
2663 // optimizations.
2664 // The calls that need special treatment are the
2665 // direct (i.e. not aliased) eval calls. These calls are all of the
2666 // form eval(...) with no explicit receiver object where eval is not
Ben Murdochb8e0da22011-05-16 14:20:40 +01002667 // declared in the current scope chain.
2668 // These calls are marked as potentially direct eval calls. Whether
2669 // they are actually direct calls to eval is determined at run time.
2670 // TODO(994): In ES5, it doesn't matter if the "eval" var is declared
2671 // in the local scope chain. It only matters that it's called "eval",
2672 // is called without a receiver and it refers to the original eval
2673 // function.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002674 VariableProxy* callee = result->AsVariableProxy();
Steve Block44f0eee2011-05-26 01:26:41 +01002675 if (callee != NULL &&
2676 callee->IsVariable(isolate()->factory()->eval_symbol())) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002677 Handle<String> name = callee->name();
2678 Variable* var = top_scope_->Lookup(name);
2679 if (var == NULL) {
2680 top_scope_->RecordEvalCall();
Steve Blocka7e24c12009-10-30 11:49:00 +00002681 }
2682 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002683 result = NewCall(result, args, pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00002684 break;
2685 }
2686
2687 case Token::PERIOD: {
2688 Consume(Token::PERIOD);
2689 int pos = scanner().location().beg_pos;
Ben Murdochbb769b22010-08-11 14:56:33 +01002690 Handle<String> name = ParseIdentifierName(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002691 result = new(zone()) Property(result, new(zone()) Literal(name), pos);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002692 if (fni_ != NULL) fni_->PushLiteralName(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002693 break;
2694 }
2695
2696 default:
2697 return result;
2698 }
2699 }
2700}
2701
2702
Steve Blocka7e24c12009-10-30 11:49:00 +00002703Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) {
2704 // NewExpression ::
2705 // ('new')+ MemberExpression
2706
2707 // The grammar for new expressions is pretty warped. The keyword
2708 // 'new' can either be a part of the new expression (where it isn't
2709 // followed by an argument list) or a part of the member expression,
2710 // where it must be followed by an argument list. To accommodate
2711 // this, we parse the 'new' keywords greedily and keep track of how
2712 // many we have parsed. This information is then passed on to the
2713 // member expression parser, which is only allowed to match argument
2714 // lists as long as it has 'new' prefixes left
2715 Expect(Token::NEW, CHECK_OK);
2716 PositionStack::Element pos(stack, scanner().location().beg_pos);
2717
2718 Expression* result;
2719 if (peek() == Token::NEW) {
2720 result = ParseNewPrefix(stack, CHECK_OK);
2721 } else {
2722 result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK);
2723 }
2724
2725 if (!stack->is_empty()) {
2726 int last = stack->pop();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002727 result = new(zone()) CallNew(result, new ZoneList<Expression*>(0), last);
Steve Blocka7e24c12009-10-30 11:49:00 +00002728 }
2729 return result;
2730}
2731
2732
2733Expression* Parser::ParseNewExpression(bool* ok) {
2734 PositionStack stack(ok);
2735 return ParseNewPrefix(&stack, ok);
2736}
2737
2738
2739Expression* Parser::ParseMemberExpression(bool* ok) {
2740 return ParseMemberWithNewPrefixesExpression(NULL, ok);
2741}
2742
2743
2744Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack,
2745 bool* ok) {
2746 // MemberExpression ::
2747 // (PrimaryExpression | FunctionLiteral)
2748 // ('[' Expression ']' | '.' Identifier | Arguments)*
2749
2750 // Parse the initial primary or function expression.
2751 Expression* result = NULL;
2752 if (peek() == Token::FUNCTION) {
2753 Expect(Token::FUNCTION, CHECK_OK);
2754 int function_token_position = scanner().location().beg_pos;
2755 Handle<String> name;
Steve Block1e0659c2011-05-24 12:43:12 +01002756 bool is_reserved_name = false;
2757 if (peek_any_identifier()) {
2758 name = ParseIdentifierOrReservedWord(&is_reserved_name, CHECK_OK);
2759 }
2760 result = ParseFunctionLiteral(name, is_reserved_name,
2761 function_token_position, NESTED, CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00002762 } else {
2763 result = ParsePrimaryExpression(CHECK_OK);
2764 }
2765
2766 while (true) {
2767 switch (peek()) {
2768 case Token::LBRACK: {
2769 Consume(Token::LBRACK);
2770 int pos = scanner().location().beg_pos;
2771 Expression* index = ParseExpression(true, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002772 result = new(zone()) Property(result, index, pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00002773 Expect(Token::RBRACK, CHECK_OK);
2774 break;
2775 }
2776 case Token::PERIOD: {
2777 Consume(Token::PERIOD);
2778 int pos = scanner().location().beg_pos;
Ben Murdochbb769b22010-08-11 14:56:33 +01002779 Handle<String> name = ParseIdentifierName(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002780 result = new(zone()) Property(result, new(zone()) Literal(name), pos);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002781 if (fni_ != NULL) fni_->PushLiteralName(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002782 break;
2783 }
2784 case Token::LPAREN: {
2785 if ((stack == NULL) || stack->is_empty()) return result;
2786 // Consume one of the new prefixes (already parsed).
2787 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
2788 int last = stack->pop();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002789 result = new CallNew(result, args, last);
Steve Blocka7e24c12009-10-30 11:49:00 +00002790 break;
2791 }
2792 default:
2793 return result;
2794 }
2795 }
2796}
2797
2798
2799DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
2800 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
2801 // contexts this is used as a statement which invokes the debugger as i a
2802 // break point is present.
2803 // DebuggerStatement ::
2804 // 'debugger' ';'
2805
2806 Expect(Token::DEBUGGER, CHECK_OK);
2807 ExpectSemicolon(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002808 return new(zone()) DebuggerStatement();
Steve Blocka7e24c12009-10-30 11:49:00 +00002809}
2810
2811
2812void Parser::ReportUnexpectedToken(Token::Value token) {
2813 // We don't report stack overflows here, to avoid increasing the
2814 // stack depth even further. Instead we report it after parsing is
Leon Clarke4515c472010-02-03 11:58:03 +00002815 // over, in ParseProgram/ParseJson.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002816 if (token == Token::ILLEGAL && stack_overflow_) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002817 // Four of the tokens are treated specially
2818 switch (token) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002819 case Token::EOS:
2820 return ReportMessage("unexpected_eos", Vector<const char*>::empty());
2821 case Token::NUMBER:
2822 return ReportMessage("unexpected_token_number",
2823 Vector<const char*>::empty());
2824 case Token::STRING:
2825 return ReportMessage("unexpected_token_string",
2826 Vector<const char*>::empty());
2827 case Token::IDENTIFIER:
2828 return ReportMessage("unexpected_token_identifier",
2829 Vector<const char*>::empty());
Steve Block1e0659c2011-05-24 12:43:12 +01002830 case Token::FUTURE_RESERVED_WORD:
Steve Block44f0eee2011-05-26 01:26:41 +01002831 return ReportMessage(top_scope_->is_strict_mode() ?
Steve Block1e0659c2011-05-24 12:43:12 +01002832 "unexpected_strict_reserved" :
2833 "unexpected_token_identifier",
2834 Vector<const char*>::empty());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002835 default:
2836 const char* name = Token::String(token);
2837 ASSERT(name != NULL);
2838 ReportMessage("unexpected_token", Vector<const char*>(&name, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00002839 }
2840}
2841
2842
Leon Clarkeac952652010-07-15 11:15:24 +01002843void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) {
2844 SmartPointer<char> name_string = name->ToCString(DISALLOW_NULLS);
2845 const char* element[1] = { *name_string };
2846 ReportMessage("invalid_preparser_data",
2847 Vector<const char*>(element, 1));
2848 *ok = false;
2849}
2850
2851
Steve Blocka7e24c12009-10-30 11:49:00 +00002852Expression* Parser::ParsePrimaryExpression(bool* ok) {
2853 // PrimaryExpression ::
2854 // 'this'
2855 // 'null'
2856 // 'true'
2857 // 'false'
2858 // Identifier
2859 // Number
2860 // String
2861 // ArrayLiteral
2862 // ObjectLiteral
2863 // RegExpLiteral
2864 // '(' Expression ')'
2865
2866 Expression* result = NULL;
2867 switch (peek()) {
2868 case Token::THIS: {
2869 Consume(Token::THIS);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002870 VariableProxy* recv = top_scope_->receiver();
2871 result = recv;
Steve Blocka7e24c12009-10-30 11:49:00 +00002872 break;
2873 }
2874
2875 case Token::NULL_LITERAL:
2876 Consume(Token::NULL_LITERAL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002877 result = new(zone()) Literal(isolate()->factory()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002878 break;
2879
2880 case Token::TRUE_LITERAL:
2881 Consume(Token::TRUE_LITERAL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002882 result = new(zone()) Literal(isolate()->factory()->true_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002883 break;
2884
2885 case Token::FALSE_LITERAL:
2886 Consume(Token::FALSE_LITERAL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002887 result = new(zone()) Literal(isolate()->factory()->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002888 break;
2889
Steve Block1e0659c2011-05-24 12:43:12 +01002890 case Token::IDENTIFIER:
2891 case Token::FUTURE_RESERVED_WORD: {
Steve Blocka7e24c12009-10-30 11:49:00 +00002892 Handle<String> name = ParseIdentifier(CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002893 if (fni_ != NULL) fni_->PushVariableName(name);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002894 result = top_scope_->NewUnresolved(name,
2895 inside_with(),
2896 scanner().location().beg_pos);
Steve Blocka7e24c12009-10-30 11:49:00 +00002897 break;
2898 }
2899
2900 case Token::NUMBER: {
2901 Consume(Token::NUMBER);
Steve Block9fac8402011-05-12 15:51:54 +01002902 ASSERT(scanner().is_literal_ascii());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002903 double value = StringToDouble(isolate()->unicode_cache(),
2904 scanner().literal_ascii_string(),
Steve Block9fac8402011-05-12 15:51:54 +01002905 ALLOW_HEX | ALLOW_OCTALS);
Steve Blocka7e24c12009-10-30 11:49:00 +00002906 result = NewNumberLiteral(value);
2907 break;
2908 }
2909
2910 case Token::STRING: {
2911 Consume(Token::STRING);
Iain Merrick9ac36c92010-09-13 15:29:50 +01002912 Handle<String> symbol = GetSymbol(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002913 result = new(zone()) Literal(symbol);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002914 if (fni_ != NULL) fni_->PushLiteralName(symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002915 break;
2916 }
2917
2918 case Token::ASSIGN_DIV:
2919 result = ParseRegExpLiteral(true, CHECK_OK);
2920 break;
2921
2922 case Token::DIV:
2923 result = ParseRegExpLiteral(false, CHECK_OK);
2924 break;
2925
2926 case Token::LBRACK:
2927 result = ParseArrayLiteral(CHECK_OK);
2928 break;
2929
2930 case Token::LBRACE:
2931 result = ParseObjectLiteral(CHECK_OK);
2932 break;
2933
2934 case Token::LPAREN:
2935 Consume(Token::LPAREN);
Ben Murdochb8e0da22011-05-16 14:20:40 +01002936 // Heuristically try to detect immediately called functions before
2937 // seeing the call parentheses.
2938 parenthesized_function_ = (peek() == Token::FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +00002939 result = ParseExpression(true, CHECK_OK);
2940 Expect(Token::RPAREN, CHECK_OK);
2941 break;
2942
2943 case Token::MOD:
2944 if (allow_natives_syntax_ || extension_ != NULL) {
2945 result = ParseV8Intrinsic(CHECK_OK);
2946 break;
2947 }
2948 // If we're not allowing special syntax we fall-through to the
2949 // default case.
2950
2951 default: {
Ben Murdochbb769b22010-08-11 14:56:33 +01002952 Token::Value tok = Next();
Steve Blocka7e24c12009-10-30 11:49:00 +00002953 ReportUnexpectedToken(tok);
2954 *ok = false;
2955 return NULL;
2956 }
2957 }
2958
2959 return result;
2960}
2961
2962
Leon Clarke4515c472010-02-03 11:58:03 +00002963void Parser::BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* values,
2964 Handle<FixedArray> literals,
2965 bool* is_simple,
2966 int* depth) {
2967 // Fill in the literals.
2968 // Accumulate output values in local variables.
2969 bool is_simple_acc = true;
2970 int depth_acc = 1;
2971 for (int i = 0; i < values->length(); i++) {
2972 MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral();
2973 if (m_literal != NULL && m_literal->depth() >= depth_acc) {
2974 depth_acc = m_literal->depth() + 1;
2975 }
2976 Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i));
2977 if (boilerplate_value->IsUndefined()) {
2978 literals->set_the_hole(i);
2979 is_simple_acc = false;
2980 } else {
2981 literals->set(i, *boilerplate_value);
2982 }
2983 }
2984
2985 *is_simple = is_simple_acc;
2986 *depth = depth_acc;
2987}
2988
2989
Steve Blocka7e24c12009-10-30 11:49:00 +00002990Expression* Parser::ParseArrayLiteral(bool* ok) {
2991 // ArrayLiteral ::
2992 // '[' Expression? (',' Expression?)* ']'
2993
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08002994 ZoneList<Expression*>* values = new ZoneList<Expression*>(4);
Steve Blocka7e24c12009-10-30 11:49:00 +00002995 Expect(Token::LBRACK, CHECK_OK);
2996 while (peek() != Token::RBRACK) {
2997 Expression* elem;
2998 if (peek() == Token::COMMA) {
2999 elem = GetLiteralTheHole();
3000 } else {
3001 elem = ParseAssignmentExpression(true, CHECK_OK);
3002 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003003 values->Add(elem);
Steve Blocka7e24c12009-10-30 11:49:00 +00003004 if (peek() != Token::RBRACK) {
3005 Expect(Token::COMMA, CHECK_OK);
3006 }
3007 }
3008 Expect(Token::RBRACK, CHECK_OK);
3009
3010 // Update the scope information before the pre-parsing bailout.
Steve Block44f0eee2011-05-26 01:26:41 +01003011 int literal_index = lexical_scope_->NextMaterializedLiteralIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00003012
Steve Blocka7e24c12009-10-30 11:49:00 +00003013 // Allocate a fixed array with all the literals.
3014 Handle<FixedArray> literals =
Steve Block44f0eee2011-05-26 01:26:41 +01003015 isolate()->factory()->NewFixedArray(values->length(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003016
3017 // Fill in the literals.
3018 bool is_simple = true;
3019 int depth = 1;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003020 for (int i = 0, n = values->length(); i < n; i++) {
3021 MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral();
Steve Blocka7e24c12009-10-30 11:49:00 +00003022 if (m_literal != NULL && m_literal->depth() + 1 > depth) {
3023 depth = m_literal->depth() + 1;
3024 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003025 Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00003026 if (boilerplate_value->IsUndefined()) {
3027 literals->set_the_hole(i);
3028 is_simple = false;
3029 } else {
3030 literals->set(i, *boilerplate_value);
3031 }
3032 }
3033
Iain Merrick75681382010-08-19 15:07:18 +01003034 // Simple and shallow arrays can be lazily copied, we transform the
3035 // elements array to a copy-on-write array.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003036 if (is_simple && depth == 1 && values->length() > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01003037 literals->set_map(isolate()->heap()->fixed_cow_array_map());
Iain Merrick75681382010-08-19 15:07:18 +01003038 }
3039
Ben Murdoch8b112d22011-06-08 16:22:53 +01003040 return new(zone()) ArrayLiteral(literals, values,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003041 literal_index, is_simple, depth);
Steve Blocka7e24c12009-10-30 11:49:00 +00003042}
3043
3044
3045bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) {
3046 return property != NULL &&
3047 property->kind() != ObjectLiteral::Property::PROTOTYPE;
3048}
3049
3050
3051bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01003052 if (expression->AsLiteral() != NULL) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00003053 MaterializedLiteral* lit = expression->AsMaterializedLiteral();
3054 return lit != NULL && lit->is_simple();
3055}
3056
Iain Merrick75681382010-08-19 15:07:18 +01003057
3058bool CompileTimeValue::ArrayLiteralElementNeedsInitialization(
3059 Expression* value) {
3060 // If value is a literal the property value is already set in the
3061 // boilerplate object.
3062 if (value->AsLiteral() != NULL) return false;
3063 // If value is a materialized literal the property value is already set
3064 // in the boilerplate object if it is simple.
3065 if (CompileTimeValue::IsCompileTimeValue(value)) return false;
3066 return true;
3067}
3068
3069
Steve Blocka7e24c12009-10-30 11:49:00 +00003070Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) {
3071 ASSERT(IsCompileTimeValue(expression));
Steve Block44f0eee2011-05-26 01:26:41 +01003072 Handle<FixedArray> result = FACTORY->NewFixedArray(2, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003073 ObjectLiteral* object_literal = expression->AsObjectLiteral();
3074 if (object_literal != NULL) {
3075 ASSERT(object_literal->is_simple());
Steve Block6ded16b2010-05-10 14:33:55 +01003076 if (object_literal->fast_elements()) {
3077 result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
3078 } else {
3079 result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
3080 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003081 result->set(kElementsSlot, *object_literal->constant_properties());
3082 } else {
3083 ArrayLiteral* array_literal = expression->AsArrayLiteral();
3084 ASSERT(array_literal != NULL && array_literal->is_simple());
3085 result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL));
Leon Clarkee46be812010-01-19 14:06:41 +00003086 result->set(kElementsSlot, *array_literal->constant_elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00003087 }
3088 return result;
3089}
3090
3091
3092CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) {
3093 Smi* type_value = Smi::cast(value->get(kTypeSlot));
3094 return static_cast<Type>(type_value->value());
3095}
3096
3097
3098Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3099 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3100}
3101
3102
3103Handle<Object> Parser::GetBoilerplateValue(Expression* expression) {
3104 if (expression->AsLiteral() != NULL) {
3105 return expression->AsLiteral()->handle();
3106 }
3107 if (CompileTimeValue::IsCompileTimeValue(expression)) {
3108 return CompileTimeValue::GetValue(expression);
3109 }
Steve Block44f0eee2011-05-26 01:26:41 +01003110 return isolate()->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003111}
3112
Steve Block1e0659c2011-05-24 12:43:12 +01003113// Defined in ast.cc
3114bool IsEqualString(void* first, void* second);
3115bool IsEqualNumber(void* first, void* second);
3116
3117
3118// Validation per 11.1.5 Object Initialiser
3119class ObjectLiteralPropertyChecker {
3120 public:
3121 ObjectLiteralPropertyChecker(Parser* parser, bool strict) :
3122 props(&IsEqualString),
3123 elems(&IsEqualNumber),
3124 parser_(parser),
3125 strict_(strict) {
3126 }
3127
3128 void CheckProperty(
3129 ObjectLiteral::Property* property,
3130 Scanner::Location loc,
3131 bool* ok);
3132
3133 private:
3134 enum PropertyKind {
3135 kGetAccessor = 0x01,
3136 kSetAccessor = 0x02,
3137 kAccessor = kGetAccessor | kSetAccessor,
3138 kData = 0x04
3139 };
3140
3141 static intptr_t GetPropertyKind(ObjectLiteral::Property* property) {
3142 switch (property->kind()) {
3143 case ObjectLiteral::Property::GETTER:
3144 return kGetAccessor;
3145 case ObjectLiteral::Property::SETTER:
3146 return kSetAccessor;
3147 default:
3148 return kData;
3149 }
3150 }
3151
3152 HashMap props;
3153 HashMap elems;
3154 Parser* parser_;
3155 bool strict_;
3156};
3157
3158
3159void ObjectLiteralPropertyChecker::CheckProperty(
3160 ObjectLiteral::Property* property,
3161 Scanner::Location loc,
3162 bool* ok) {
3163
3164 ASSERT(property != NULL);
3165
3166 Literal *lit = property->key();
3167 Handle<Object> handle = lit->handle();
3168
3169 uint32_t hash;
3170 HashMap* map;
3171 void* key;
3172
3173 if (handle->IsSymbol()) {
3174 Handle<String> name(String::cast(*handle));
3175 if (name->AsArrayIndex(&hash)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003176 Handle<Object> key_handle = FACTORY->NewNumberFromUint(hash);
Steve Block1e0659c2011-05-24 12:43:12 +01003177 key = key_handle.location();
3178 map = &elems;
3179 } else {
3180 key = handle.location();
3181 hash = name->Hash();
3182 map = &props;
3183 }
3184 } else if (handle->ToArrayIndex(&hash)) {
3185 key = handle.location();
3186 map = &elems;
3187 } else {
3188 ASSERT(handle->IsNumber());
3189 double num = handle->Number();
3190 char arr[100];
3191 Vector<char> buffer(arr, ARRAY_SIZE(arr));
3192 const char* str = DoubleToCString(num, buffer);
Steve Block44f0eee2011-05-26 01:26:41 +01003193 Handle<String> name = FACTORY->NewStringFromAscii(CStrVector(str));
Steve Block1e0659c2011-05-24 12:43:12 +01003194 key = name.location();
3195 hash = name->Hash();
3196 map = &props;
3197 }
3198
3199 // Lookup property previously defined, if any.
3200 HashMap::Entry* entry = map->Lookup(key, hash, true);
3201 intptr_t prev = reinterpret_cast<intptr_t> (entry->value);
3202 intptr_t curr = GetPropertyKind(property);
3203
3204 // Duplicate data properties are illegal in strict mode.
3205 if (strict_ && (curr & prev & kData) != 0) {
3206 parser_->ReportMessageAt(loc, "strict_duplicate_property",
3207 Vector<const char*>::empty());
3208 *ok = false;
3209 return;
3210 }
3211 // Data property conflicting with an accessor.
3212 if (((curr & kData) && (prev & kAccessor)) ||
3213 ((prev & kData) && (curr & kAccessor))) {
3214 parser_->ReportMessageAt(loc, "accessor_data_property",
3215 Vector<const char*>::empty());
3216 *ok = false;
3217 return;
3218 }
3219 // Two accessors of the same type conflicting
3220 if ((curr & prev & kAccessor) != 0) {
3221 parser_->ReportMessageAt(loc, "accessor_get_set",
3222 Vector<const char*>::empty());
3223 *ok = false;
3224 return;
3225 }
3226
3227 // Update map
3228 entry->value = reinterpret_cast<void*> (prev | curr);
3229 *ok = true;
3230}
3231
Steve Blocka7e24c12009-10-30 11:49:00 +00003232
Leon Clarke4515c472010-02-03 11:58:03 +00003233void Parser::BuildObjectLiteralConstantProperties(
3234 ZoneList<ObjectLiteral::Property*>* properties,
3235 Handle<FixedArray> constant_properties,
3236 bool* is_simple,
Steve Block6ded16b2010-05-10 14:33:55 +01003237 bool* fast_elements,
Leon Clarke4515c472010-02-03 11:58:03 +00003238 int* depth) {
3239 int position = 0;
3240 // Accumulate the value in local variables and store it at the end.
3241 bool is_simple_acc = true;
3242 int depth_acc = 1;
Steve Block6ded16b2010-05-10 14:33:55 +01003243 uint32_t max_element_index = 0;
3244 uint32_t elements = 0;
Leon Clarke4515c472010-02-03 11:58:03 +00003245 for (int i = 0; i < properties->length(); i++) {
3246 ObjectLiteral::Property* property = properties->at(i);
3247 if (!IsBoilerplateProperty(property)) {
3248 is_simple_acc = false;
3249 continue;
3250 }
3251 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
3252 if (m_literal != NULL && m_literal->depth() >= depth_acc) {
3253 depth_acc = m_literal->depth() + 1;
3254 }
3255
3256 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined
3257 // value for COMPUTED properties, the real value is filled in at
3258 // runtime. The enumeration order is maintained.
3259 Handle<Object> key = property->key()->handle();
3260 Handle<Object> value = GetBoilerplateValue(property->value());
3261 is_simple_acc = is_simple_acc && !value->IsUndefined();
3262
Steve Block6ded16b2010-05-10 14:33:55 +01003263 // Keep track of the number of elements in the object literal and
3264 // the largest element index. If the largest element index is
3265 // much larger than the number of elements, creating an object
3266 // literal with fast elements will be a waste of space.
3267 uint32_t element_index = 0;
3268 if (key->IsString()
3269 && Handle<String>::cast(key)->AsArrayIndex(&element_index)
3270 && element_index > max_element_index) {
3271 max_element_index = element_index;
3272 elements++;
3273 } else if (key->IsSmi()) {
3274 int key_value = Smi::cast(*key)->value();
3275 if (key_value > 0
3276 && static_cast<uint32_t>(key_value) > max_element_index) {
3277 max_element_index = key_value;
3278 }
3279 elements++;
3280 }
3281
Leon Clarke4515c472010-02-03 11:58:03 +00003282 // Add name, value pair to the fixed array.
3283 constant_properties->set(position++, *key);
3284 constant_properties->set(position++, *value);
3285 }
Steve Block6ded16b2010-05-10 14:33:55 +01003286 *fast_elements =
3287 (max_element_index <= 32) || ((2 * elements) >= max_element_index);
Leon Clarke4515c472010-02-03 11:58:03 +00003288 *is_simple = is_simple_acc;
3289 *depth = depth_acc;
3290}
3291
3292
Ben Murdochbb769b22010-08-11 14:56:33 +01003293ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter,
3294 bool* ok) {
3295 // Special handling of getter and setter syntax:
3296 // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
3297 // We have already read the "get" or "set" keyword.
3298 Token::Value next = Next();
Steve Block9fac8402011-05-12 15:51:54 +01003299 bool is_keyword = Token::IsKeyword(next);
3300 if (next == Token::IDENTIFIER || next == Token::NUMBER ||
Steve Block1e0659c2011-05-24 12:43:12 +01003301 next == Token::FUTURE_RESERVED_WORD ||
Steve Block9fac8402011-05-12 15:51:54 +01003302 next == Token::STRING || is_keyword) {
3303 Handle<String> name;
3304 if (is_keyword) {
Steve Block44f0eee2011-05-26 01:26:41 +01003305 name = isolate_->factory()->LookupAsciiSymbol(Token::String(next));
Steve Block9fac8402011-05-12 15:51:54 +01003306 } else {
3307 name = GetSymbol(CHECK_OK);
3308 }
Ben Murdochbb769b22010-08-11 14:56:33 +01003309 FunctionLiteral* value =
3310 ParseFunctionLiteral(name,
Steve Block1e0659c2011-05-24 12:43:12 +01003311 false, // reserved words are allowed here
Ben Murdochbb769b22010-08-11 14:56:33 +01003312 RelocInfo::kNoPosition,
3313 DECLARATION,
3314 CHECK_OK);
Steve Block9fac8402011-05-12 15:51:54 +01003315 // Allow any number of parameters for compatiabilty with JSC.
3316 // Specification only allows zero parameters for get and one for set.
Ben Murdochbb769b22010-08-11 14:56:33 +01003317 ObjectLiteral::Property* property =
Ben Murdoch8b112d22011-06-08 16:22:53 +01003318 new(zone()) ObjectLiteral::Property(is_getter, value);
Ben Murdochbb769b22010-08-11 14:56:33 +01003319 return property;
3320 } else {
3321 ReportUnexpectedToken(next);
3322 *ok = false;
3323 return NULL;
3324 }
3325}
3326
3327
Steve Blocka7e24c12009-10-30 11:49:00 +00003328Expression* Parser::ParseObjectLiteral(bool* ok) {
3329 // ObjectLiteral ::
3330 // '{' (
Ben Murdochbb769b22010-08-11 14:56:33 +01003331 // ((IdentifierName | String | Number) ':' AssignmentExpression)
3332 // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral)
Steve Blocka7e24c12009-10-30 11:49:00 +00003333 // )*[','] '}'
3334
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003335 ZoneList<ObjectLiteral::Property*>* properties =
3336 new ZoneList<ObjectLiteral::Property*>(4);
Steve Blocka7e24c12009-10-30 11:49:00 +00003337 int number_of_boilerplate_properties = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01003338 bool has_function = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003339
Steve Block44f0eee2011-05-26 01:26:41 +01003340 ObjectLiteralPropertyChecker checker(this, top_scope_->is_strict_mode());
Steve Block1e0659c2011-05-24 12:43:12 +01003341
Steve Blocka7e24c12009-10-30 11:49:00 +00003342 Expect(Token::LBRACE, CHECK_OK);
Steve Block1e0659c2011-05-24 12:43:12 +01003343 Scanner::Location loc = scanner().location();
3344
Steve Blocka7e24c12009-10-30 11:49:00 +00003345 while (peek() != Token::RBRACE) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003346 if (fni_ != NULL) fni_->Enter();
3347
Steve Blocka7e24c12009-10-30 11:49:00 +00003348 Literal* key = NULL;
Ben Murdochbb769b22010-08-11 14:56:33 +01003349 Token::Value next = peek();
Steve Block1e0659c2011-05-24 12:43:12 +01003350
3351 // Location of the property name token
3352 Scanner::Location loc = scanner().peek_location();
3353
Ben Murdochbb769b22010-08-11 14:56:33 +01003354 switch (next) {
Steve Block1e0659c2011-05-24 12:43:12 +01003355 case Token::FUTURE_RESERVED_WORD:
Steve Blocka7e24c12009-10-30 11:49:00 +00003356 case Token::IDENTIFIER: {
Steve Blocka7e24c12009-10-30 11:49:00 +00003357 bool is_getter = false;
3358 bool is_setter = false;
3359 Handle<String> id =
3360 ParseIdentifierOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003361 if (fni_ != NULL) fni_->PushLiteralName(id);
3362
Ben Murdochbb769b22010-08-11 14:56:33 +01003363 if ((is_getter || is_setter) && peek() != Token::COLON) {
Steve Block1e0659c2011-05-24 12:43:12 +01003364 // Update loc to point to the identifier
3365 loc = scanner().peek_location();
Steve Blocka7e24c12009-10-30 11:49:00 +00003366 ObjectLiteral::Property* property =
Ben Murdochbb769b22010-08-11 14:56:33 +01003367 ParseObjectLiteralGetSet(is_getter, CHECK_OK);
3368 if (IsBoilerplateProperty(property)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003369 number_of_boilerplate_properties++;
Ben Murdochbb769b22010-08-11 14:56:33 +01003370 }
Steve Block1e0659c2011-05-24 12:43:12 +01003371 // Validate the property.
3372 checker.CheckProperty(property, loc, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003373 properties->Add(property);
Steve Blocka7e24c12009-10-30 11:49:00 +00003374 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003375
3376 if (fni_ != NULL) {
3377 fni_->Infer();
3378 fni_->Leave();
3379 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003380 continue; // restart the while
Steve Blocka7e24c12009-10-30 11:49:00 +00003381 }
Ben Murdochbb769b22010-08-11 14:56:33 +01003382 // Failed to parse as get/set property, so it's just a property
3383 // called "get" or "set".
Ben Murdoch8b112d22011-06-08 16:22:53 +01003384 key = new(zone()) Literal(id);
Steve Blocka7e24c12009-10-30 11:49:00 +00003385 break;
3386 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003387 case Token::STRING: {
3388 Consume(Token::STRING);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003389 Handle<String> string = GetSymbol(CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003390 if (fni_ != NULL) fni_->PushLiteralName(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00003391 uint32_t index;
Iain Merrick75681382010-08-19 15:07:18 +01003392 if (!string.is_null() && string->AsArrayIndex(&index)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003393 key = NewNumberLiteral(index);
Ben Murdochbb769b22010-08-11 14:56:33 +01003394 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00003395 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003396 key = new(zone()) Literal(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00003397 break;
3398 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003399 case Token::NUMBER: {
3400 Consume(Token::NUMBER);
Steve Block9fac8402011-05-12 15:51:54 +01003401 ASSERT(scanner().is_literal_ascii());
Ben Murdoch8b112d22011-06-08 16:22:53 +01003402 double value = StringToDouble(isolate()->unicode_cache(),
3403 scanner().literal_ascii_string(),
Steve Block9fac8402011-05-12 15:51:54 +01003404 ALLOW_HEX | ALLOW_OCTALS);
Steve Blocka7e24c12009-10-30 11:49:00 +00003405 key = NewNumberLiteral(value);
3406 break;
3407 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003408 default:
Ben Murdochbb769b22010-08-11 14:56:33 +01003409 if (Token::IsKeyword(next)) {
3410 Consume(next);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003411 Handle<String> string = GetSymbol(CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003412 key = new(zone()) Literal(string);
Ben Murdochbb769b22010-08-11 14:56:33 +01003413 } else {
3414 // Unexpected token.
3415 Token::Value next = Next();
3416 ReportUnexpectedToken(next);
3417 *ok = false;
3418 return NULL;
3419 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003420 }
3421
3422 Expect(Token::COLON, CHECK_OK);
3423 Expression* value = ParseAssignmentExpression(true, CHECK_OK);
3424
3425 ObjectLiteral::Property* property =
Ben Murdoch8b112d22011-06-08 16:22:53 +01003426 new(zone()) ObjectLiteral::Property(key, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003427
Steve Block44f0eee2011-05-26 01:26:41 +01003428 // Mark object literals that contain function literals and pretenure the
3429 // literal so it can be added as a constant function property.
3430 if (value->AsFunctionLiteral() != NULL) {
3431 has_function = true;
3432 value->AsFunctionLiteral()->set_pretenure(true);
3433 }
3434
Steve Blocka7e24c12009-10-30 11:49:00 +00003435 // Count CONSTANT or COMPUTED properties to maintain the enumeration order.
3436 if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++;
Steve Block1e0659c2011-05-24 12:43:12 +01003437 // Validate the property
3438 checker.CheckProperty(property, loc, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003439 properties->Add(property);
Steve Blocka7e24c12009-10-30 11:49:00 +00003440
3441 // TODO(1240767): Consider allowing trailing comma.
3442 if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003443
3444 if (fni_ != NULL) {
3445 fni_->Infer();
3446 fni_->Leave();
3447 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003448 }
3449 Expect(Token::RBRACE, CHECK_OK);
Steve Block1e0659c2011-05-24 12:43:12 +01003450
Steve Blocka7e24c12009-10-30 11:49:00 +00003451 // Computation of literal_index must happen before pre parse bailout.
Steve Block44f0eee2011-05-26 01:26:41 +01003452 int literal_index = lexical_scope_->NextMaterializedLiteralIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00003453
Steve Block44f0eee2011-05-26 01:26:41 +01003454 Handle<FixedArray> constant_properties = isolate()->factory()->NewFixedArray(
3455 number_of_boilerplate_properties * 2, TENURED);
Leon Clarke4515c472010-02-03 11:58:03 +00003456
Steve Blocka7e24c12009-10-30 11:49:00 +00003457 bool is_simple = true;
Steve Block6ded16b2010-05-10 14:33:55 +01003458 bool fast_elements = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00003459 int depth = 1;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003460 BuildObjectLiteralConstantProperties(properties,
Leon Clarke4515c472010-02-03 11:58:03 +00003461 constant_properties,
3462 &is_simple,
Steve Block6ded16b2010-05-10 14:33:55 +01003463 &fast_elements,
Leon Clarke4515c472010-02-03 11:58:03 +00003464 &depth);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003465 return new(zone()) ObjectLiteral(constant_properties,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003466 properties,
Steve Blocka7e24c12009-10-30 11:49:00 +00003467 literal_index,
3468 is_simple,
Steve Block6ded16b2010-05-10 14:33:55 +01003469 fast_elements,
Steve Block44f0eee2011-05-26 01:26:41 +01003470 depth,
3471 has_function);
Steve Blocka7e24c12009-10-30 11:49:00 +00003472}
3473
3474
3475Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01003476 if (!scanner().ScanRegExpPattern(seen_equal)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003477 Next();
3478 ReportMessage("unterminated_regexp", Vector<const char*>::empty());
3479 *ok = false;
3480 return NULL;
3481 }
3482
Steve Block44f0eee2011-05-26 01:26:41 +01003483 int literal_index = lexical_scope_->NextMaterializedLiteralIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00003484
Steve Block9fac8402011-05-12 15:51:54 +01003485 Handle<String> js_pattern = NextLiteralString(TENURED);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003486 scanner().ScanRegExpFlags();
Steve Block9fac8402011-05-12 15:51:54 +01003487 Handle<String> js_flags = NextLiteralString(TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003488 Next();
3489
Ben Murdoch8b112d22011-06-08 16:22:53 +01003490 return new(zone()) RegExpLiteral(js_pattern, js_flags, literal_index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003491}
3492
3493
3494ZoneList<Expression*>* Parser::ParseArguments(bool* ok) {
3495 // Arguments ::
3496 // '(' (AssignmentExpression)*[','] ')'
3497
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003498 ZoneList<Expression*>* result = new ZoneList<Expression*>(4);
Steve Blocka7e24c12009-10-30 11:49:00 +00003499 Expect(Token::LPAREN, CHECK_OK);
3500 bool done = (peek() == Token::RPAREN);
3501 while (!done) {
3502 Expression* argument = ParseAssignmentExpression(true, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003503 result->Add(argument);
Steve Block053d10c2011-06-13 19:13:29 +01003504 if (result->length() > kMaxNumFunctionParameters) {
3505 ReportMessageAt(scanner().location(), "too_many_arguments",
3506 Vector<const char*>::empty());
3507 *ok = false;
3508 return NULL;
3509 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003510 done = (peek() == Token::RPAREN);
3511 if (!done) Expect(Token::COMMA, CHECK_OK);
3512 }
3513 Expect(Token::RPAREN, CHECK_OK);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003514 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003515}
3516
3517
3518FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
Steve Block1e0659c2011-05-24 12:43:12 +01003519 bool name_is_reserved,
Steve Blocka7e24c12009-10-30 11:49:00 +00003520 int function_token_position,
3521 FunctionLiteralType type,
3522 bool* ok) {
3523 // Function ::
3524 // '(' FormalParameterList? ')' '{' FunctionBody '}'
Steve Blocka7e24c12009-10-30 11:49:00 +00003525 bool is_named = !var_name.is_null();
3526
3527 // The name associated with this function. If it's a function expression,
3528 // this is the actual function name, otherwise this is the name of the
3529 // variable declared and initialized with the function (expression). In
3530 // that case, we don't have a function name (it's empty).
Steve Block44f0eee2011-05-26 01:26:41 +01003531 Handle<String> name =
3532 is_named ? var_name : isolate()->factory()->empty_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00003533 // The function name, if any.
Steve Block44f0eee2011-05-26 01:26:41 +01003534 Handle<String> function_name = isolate()->factory()->empty_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00003535 if (is_named && (type == EXPRESSION || type == NESTED)) {
3536 function_name = name;
3537 }
3538
3539 int num_parameters = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +01003540 Scope* scope = NewScope(top_scope_, Scope::FUNCTION_SCOPE, inside_with());
3541 ZoneList<Statement*>* body = new ZoneList<Statement*>(8);
3542 int materialized_literal_count;
3543 int expected_property_count;
3544 int start_pos;
3545 int end_pos;
3546 bool only_simple_this_property_assignments;
3547 Handle<FixedArray> this_property_assignments;
Steve Blocka7e24c12009-10-30 11:49:00 +00003548 // Parse function body.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003549 { LexicalScope lexical_scope(this, scope, isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003550 top_scope_->SetScopeName(name);
3551
3552 // FormalParameterList ::
3553 // '(' (Identifier)*[','] ')'
3554 Expect(Token::LPAREN, CHECK_OK);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003555 start_pos = scanner().location().beg_pos;
Steve Block1e0659c2011-05-24 12:43:12 +01003556 Scanner::Location name_loc = Scanner::NoLocation();
3557 Scanner::Location dupe_loc = Scanner::NoLocation();
3558 Scanner::Location reserved_loc = Scanner::NoLocation();
3559
Steve Blocka7e24c12009-10-30 11:49:00 +00003560 bool done = (peek() == Token::RPAREN);
3561 while (!done) {
Steve Block1e0659c2011-05-24 12:43:12 +01003562 bool is_reserved = false;
3563 Handle<String> param_name =
3564 ParseIdentifierOrReservedWord(&is_reserved, CHECK_OK);
3565
3566 // Store locations for possible future error reports.
3567 if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) {
3568 name_loc = scanner().location();
3569 }
3570 if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) {
3571 dupe_loc = scanner().location();
3572 }
3573 if (!reserved_loc.IsValid() && is_reserved) {
3574 reserved_loc = scanner().location();
3575 }
3576
Steve Block053d10c2011-06-13 19:13:29 +01003577 Variable* parameter = top_scope_->DeclareLocal(param_name,
3578 Variable::VAR,
3579 Scope::PARAMETER);
Steve Block1e0659c2011-05-24 12:43:12 +01003580 top_scope_->AddParameter(parameter);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003581 num_parameters++;
Steve Block1e0659c2011-05-24 12:43:12 +01003582 if (num_parameters > kMaxNumFunctionParameters) {
3583 ReportMessageAt(scanner().location(), "too_many_parameters",
3584 Vector<const char*>::empty());
3585 *ok = false;
3586 return NULL;
3587 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003588 done = (peek() == Token::RPAREN);
3589 if (!done) Expect(Token::COMMA, CHECK_OK);
3590 }
3591 Expect(Token::RPAREN, CHECK_OK);
3592
3593 Expect(Token::LBRACE, CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00003594
3595 // If we have a named function expression, we add a local variable
3596 // declaration to the body of the function with the name of the
3597 // function and let it refer to the function itself (closure).
3598 // NOTE: We create a proxy and resolve it here so that in the
3599 // future we can change the AST to only refer to VariableProxies
3600 // instead of Variables and Proxis as is the case now.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003601 if (!function_name.is_null() && function_name->length() > 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003602 Variable* fvar = top_scope_->DeclareFunctionVar(function_name);
3603 VariableProxy* fproxy =
3604 top_scope_->NewUnresolved(function_name, inside_with());
3605 fproxy->BindTo(fvar);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003606 body->Add(new(zone()) ExpressionStatement(
3607 new(zone()) Assignment(Token::INIT_CONST, fproxy,
3608 new(zone()) ThisFunction(),
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003609 RelocInfo::kNoPosition)));
Steve Blocka7e24c12009-10-30 11:49:00 +00003610 }
3611
3612 // Determine if the function will be lazily compiled. The mode can
3613 // only be PARSE_LAZILY if the --lazy flag is true.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003614 bool is_lazily_compiled = (mode() == PARSE_LAZILY &&
3615 top_scope_->outer_scope()->is_global_scope() &&
3616 top_scope_->HasTrivialOuterContext() &&
3617 !parenthesized_function_);
3618 parenthesized_function_ = false; // The bit was set for this function only.
Steve Blocka7e24c12009-10-30 11:49:00 +00003619
Ben Murdochb0fe1622011-05-05 13:52:32 +01003620 int function_block_pos = scanner().location().beg_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00003621 if (is_lazily_compiled && pre_data() != NULL) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003622 FunctionEntry entry = pre_data()->GetFunctionEntry(function_block_pos);
Leon Clarkeac952652010-07-15 11:15:24 +01003623 if (!entry.is_valid()) {
3624 ReportInvalidPreparseData(name, CHECK_OK);
3625 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003626 end_pos = entry.end_pos();
3627 if (end_pos <= function_block_pos) {
Leon Clarkeac952652010-07-15 11:15:24 +01003628 // End position greater than end of stream is safe, and hard to check.
3629 ReportInvalidPreparseData(name, CHECK_OK);
3630 }
Steve Block44f0eee2011-05-26 01:26:41 +01003631 isolate()->counters()->total_preparse_skipped()->Increment(
3632 end_pos - function_block_pos);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003633 // Seek to position just before terminal '}'.
3634 scanner().SeekForward(end_pos - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00003635 materialized_literal_count = entry.literal_count();
3636 expected_property_count = entry.property_count();
Steve Blocka7e24c12009-10-30 11:49:00 +00003637 only_simple_this_property_assignments = false;
Steve Block44f0eee2011-05-26 01:26:41 +01003638 this_property_assignments = isolate()->factory()->empty_fixed_array();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003639 Expect(Token::RBRACE, CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00003640 } else {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003641 ParseSourceElements(body, Token::RBRACE, CHECK_OK);
3642
Steve Block44f0eee2011-05-26 01:26:41 +01003643 materialized_literal_count = lexical_scope.materialized_literal_count();
3644 expected_property_count = lexical_scope.expected_property_count();
Steve Blocka7e24c12009-10-30 11:49:00 +00003645 only_simple_this_property_assignments =
Steve Block44f0eee2011-05-26 01:26:41 +01003646 lexical_scope.only_simple_this_property_assignments();
3647 this_property_assignments = lexical_scope.this_property_assignments();
Steve Blocka7e24c12009-10-30 11:49:00 +00003648
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003649 Expect(Token::RBRACE, CHECK_OK);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003650 end_pos = scanner().location().end_pos;
Steve Blocka7e24c12009-10-30 11:49:00 +00003651 }
3652
Steve Block1e0659c2011-05-24 12:43:12 +01003653 // Validate strict mode.
Steve Block44f0eee2011-05-26 01:26:41 +01003654 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003655 if (IsEvalOrArguments(name)) {
3656 int position = function_token_position != RelocInfo::kNoPosition
3657 ? function_token_position
3658 : (start_pos > 0 ? start_pos - 1 : start_pos);
3659 Scanner::Location location = Scanner::Location(position, start_pos);
3660 ReportMessageAt(location,
3661 "strict_function_name", Vector<const char*>::empty());
3662 *ok = false;
3663 return NULL;
3664 }
3665 if (name_loc.IsValid()) {
3666 ReportMessageAt(name_loc, "strict_param_name",
3667 Vector<const char*>::empty());
3668 *ok = false;
3669 return NULL;
3670 }
3671 if (dupe_loc.IsValid()) {
3672 ReportMessageAt(dupe_loc, "strict_param_dupe",
3673 Vector<const char*>::empty());
3674 *ok = false;
3675 return NULL;
3676 }
3677 if (name_is_reserved) {
3678 int position = function_token_position != RelocInfo::kNoPosition
3679 ? function_token_position
3680 : (start_pos > 0 ? start_pos - 1 : start_pos);
3681 Scanner::Location location = Scanner::Location(position, start_pos);
3682 ReportMessageAt(location, "strict_reserved_word",
3683 Vector<const char*>::empty());
3684 *ok = false;
3685 return NULL;
3686 }
3687 if (reserved_loc.IsValid()) {
3688 ReportMessageAt(reserved_loc, "strict_reserved_word",
3689 Vector<const char*>::empty());
3690 *ok = false;
3691 return NULL;
3692 }
3693 CheckOctalLiteral(start_pos, end_pos, CHECK_OK);
3694 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003695 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003696
3697 FunctionLiteral* function_literal =
3698 new(zone()) FunctionLiteral(name,
3699 scope,
3700 body,
3701 materialized_literal_count,
3702 expected_property_count,
3703 only_simple_this_property_assignments,
3704 this_property_assignments,
3705 num_parameters,
3706 start_pos,
3707 end_pos,
3708 (function_name->length() > 0));
3709 function_literal->set_function_token_position(function_token_position);
3710
3711 if (fni_ != NULL && !is_named) fni_->AddFunction(function_literal);
3712 return function_literal;
Steve Blocka7e24c12009-10-30 11:49:00 +00003713}
3714
3715
3716Expression* Parser::ParseV8Intrinsic(bool* ok) {
3717 // CallRuntime ::
3718 // '%' Identifier Arguments
3719
3720 Expect(Token::MOD, CHECK_OK);
3721 Handle<String> name = ParseIdentifier(CHECK_OK);
Steve Blocka7e24c12009-10-30 11:49:00 +00003722 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003723
3724 if (extension_ != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003725 // The extension structures are only accessible while parsing the
3726 // very first time not when reparsing because of lazy compilation.
3727 top_scope_->ForceEagerCompilation();
3728 }
3729
Steve Block44f0eee2011-05-26 01:26:41 +01003730 const Runtime::Function* function = Runtime::FunctionForSymbol(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00003731
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003732 // Check for built-in IS_VAR macro.
3733 if (function != NULL &&
3734 function->intrinsic_type == Runtime::RUNTIME &&
3735 function->function_id == Runtime::kIS_VAR) {
3736 // %IS_VAR(x) evaluates to x if x is a variable,
3737 // leads to a parse error otherwise. Could be implemented as an
3738 // inline function %_IS_VAR(x) to eliminate this special case.
3739 if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
3740 return args->at(0);
3741 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00003742 ReportMessage("unable_to_parse", Vector<const char*>::empty());
Steve Block6ded16b2010-05-10 14:33:55 +01003743 *ok = false;
3744 return NULL;
Steve Block6ded16b2010-05-10 14:33:55 +01003745 }
3746 }
3747
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003748 // Check that the expected number of arguments are being passed.
3749 if (function != NULL &&
3750 function->nargs != -1 &&
3751 function->nargs != args->length()) {
3752 ReportMessage("illegal_access", Vector<const char*>::empty());
3753 *ok = false;
3754 return NULL;
3755 }
3756
3757 // We have a valid intrinsics call or a call to a builtin.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003758 return new(zone()) CallRuntime(name, function, args);
Steve Blocka7e24c12009-10-30 11:49:00 +00003759}
3760
3761
Steve Block1e0659c2011-05-24 12:43:12 +01003762bool Parser::peek_any_identifier() {
3763 Token::Value next = peek();
3764 return next == Token::IDENTIFIER ||
3765 next == Token::FUTURE_RESERVED_WORD;
3766}
3767
3768
Steve Blocka7e24c12009-10-30 11:49:00 +00003769void Parser::Consume(Token::Value token) {
3770 Token::Value next = Next();
3771 USE(next);
3772 USE(token);
3773 ASSERT(next == token);
3774}
3775
3776
3777void Parser::Expect(Token::Value token, bool* ok) {
3778 Token::Value next = Next();
3779 if (next == token) return;
3780 ReportUnexpectedToken(next);
3781 *ok = false;
3782}
3783
3784
Leon Clarke4515c472010-02-03 11:58:03 +00003785bool Parser::Check(Token::Value token) {
3786 Token::Value next = peek();
3787 if (next == token) {
3788 Consume(next);
3789 return true;
3790 }
3791 return false;
3792}
3793
3794
Steve Blocka7e24c12009-10-30 11:49:00 +00003795void Parser::ExpectSemicolon(bool* ok) {
3796 // Check for automatic semicolon insertion according to
3797 // the rules given in ECMA-262, section 7.9, page 21.
3798 Token::Value tok = peek();
3799 if (tok == Token::SEMICOLON) {
3800 Next();
3801 return;
3802 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003803 if (scanner().has_line_terminator_before_next() ||
Steve Blocka7e24c12009-10-30 11:49:00 +00003804 tok == Token::RBRACE ||
3805 tok == Token::EOS) {
3806 return;
3807 }
3808 Expect(Token::SEMICOLON, ok);
3809}
3810
3811
3812Literal* Parser::GetLiteralUndefined() {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003813 return new(zone()) Literal(isolate()->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00003814}
3815
3816
3817Literal* Parser::GetLiteralTheHole() {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003818 return new(zone()) Literal(isolate()->factory()->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00003819}
3820
3821
3822Literal* Parser::GetLiteralNumber(double value) {
3823 return NewNumberLiteral(value);
3824}
3825
3826
3827Handle<String> Parser::ParseIdentifier(bool* ok) {
Steve Block1e0659c2011-05-24 12:43:12 +01003828 bool is_reserved;
3829 return ParseIdentifierOrReservedWord(&is_reserved, ok);
3830}
3831
3832
3833Handle<String> Parser::ParseIdentifierOrReservedWord(bool* is_reserved,
3834 bool* ok) {
3835 *is_reserved = false;
Steve Block44f0eee2011-05-26 01:26:41 +01003836 if (top_scope_->is_strict_mode()) {
Steve Block1e0659c2011-05-24 12:43:12 +01003837 Expect(Token::IDENTIFIER, ok);
3838 } else {
3839 if (!Check(Token::IDENTIFIER)) {
3840 Expect(Token::FUTURE_RESERVED_WORD, ok);
3841 *is_reserved = true;
3842 }
3843 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003844 if (!*ok) return Handle<String>();
Iain Merrick9ac36c92010-09-13 15:29:50 +01003845 return GetSymbol(ok);
Steve Blocka7e24c12009-10-30 11:49:00 +00003846}
3847
Ben Murdochbb769b22010-08-11 14:56:33 +01003848
3849Handle<String> Parser::ParseIdentifierName(bool* ok) {
3850 Token::Value next = Next();
Steve Block1e0659c2011-05-24 12:43:12 +01003851 if (next != Token::IDENTIFIER &&
3852 next != Token::FUTURE_RESERVED_WORD &&
3853 !Token::IsKeyword(next)) {
Ben Murdochbb769b22010-08-11 14:56:33 +01003854 ReportUnexpectedToken(next);
3855 *ok = false;
3856 return Handle<String>();
3857 }
Iain Merrick9ac36c92010-09-13 15:29:50 +01003858 return GetSymbol(ok);
Ben Murdochbb769b22010-08-11 14:56:33 +01003859}
3860
3861
Steve Block1e0659c2011-05-24 12:43:12 +01003862// Checks LHS expression for assignment and prefix/postfix increment/decrement
3863// in strict mode.
3864void Parser::CheckStrictModeLValue(Expression* expression,
3865 const char* error,
3866 bool* ok) {
Steve Block44f0eee2011-05-26 01:26:41 +01003867 ASSERT(top_scope_->is_strict_mode());
Steve Block1e0659c2011-05-24 12:43:12 +01003868 VariableProxy* lhs = expression != NULL
3869 ? expression->AsVariableProxy()
3870 : NULL;
3871
3872 if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) {
3873 ReportMessage(error, Vector<const char*>::empty());
3874 *ok = false;
3875 }
3876}
3877
3878
3879// Checks whether octal literal last seen is between beg_pos and end_pos.
3880// If so, reports an error.
3881void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
3882 int octal = scanner().octal_position();
3883 if (beg_pos <= octal && octal <= end_pos) {
3884 ReportMessageAt(Scanner::Location(octal, octal + 1), "strict_octal_literal",
3885 Vector<const char*>::empty());
3886 scanner().clear_octal_position();
3887 *ok = false;
3888 }
3889}
3890
3891
Steve Blocka7e24c12009-10-30 11:49:00 +00003892// This function reads an identifier and determines whether or not it
Steve Block1e0659c2011-05-24 12:43:12 +01003893// is 'get' or 'set'.
Steve Blocka7e24c12009-10-30 11:49:00 +00003894Handle<String> Parser::ParseIdentifierOrGetOrSet(bool* is_get,
3895 bool* is_set,
3896 bool* ok) {
Steve Block1e0659c2011-05-24 12:43:12 +01003897 Handle<String> result = ParseIdentifier(ok);
Steve Blocka7e24c12009-10-30 11:49:00 +00003898 if (!*ok) return Handle<String>();
Steve Block9fac8402011-05-12 15:51:54 +01003899 if (scanner().is_literal_ascii() && scanner().literal_length() == 3) {
3900 const char* token = scanner().literal_ascii_string().start();
3901 *is_get = strncmp(token, "get", 3) == 0;
3902 *is_set = !*is_get && strncmp(token, "set", 3) == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003903 }
Steve Block1e0659c2011-05-24 12:43:12 +01003904 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003905}
3906
3907
3908// ----------------------------------------------------------------------------
3909// Parser support
3910
3911
3912bool Parser::TargetStackContainsLabel(Handle<String> label) {
3913 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3914 BreakableStatement* stat = t->node()->AsBreakableStatement();
3915 if (stat != NULL && ContainsLabel(stat->labels(), label))
3916 return true;
3917 }
3918 return false;
3919}
3920
3921
3922BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) {
3923 bool anonymous = label.is_null();
3924 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3925 BreakableStatement* stat = t->node()->AsBreakableStatement();
3926 if (stat == NULL) continue;
3927 if ((anonymous && stat->is_target_for_anonymous()) ||
3928 (!anonymous && ContainsLabel(stat->labels(), label))) {
3929 RegisterTargetUse(stat->break_target(), t->previous());
3930 return stat;
3931 }
3932 }
3933 return NULL;
3934}
3935
3936
3937IterationStatement* Parser::LookupContinueTarget(Handle<String> label,
3938 bool* ok) {
3939 bool anonymous = label.is_null();
3940 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3941 IterationStatement* stat = t->node()->AsIterationStatement();
3942 if (stat == NULL) continue;
3943
3944 ASSERT(stat->is_target_for_anonymous());
3945 if (anonymous || ContainsLabel(stat->labels(), label)) {
3946 RegisterTargetUse(stat->continue_target(), t->previous());
3947 return stat;
3948 }
3949 }
3950 return NULL;
3951}
3952
3953
Ben Murdoch8b112d22011-06-08 16:22:53 +01003954void Parser::RegisterTargetUse(Label* target, Target* stop) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003955 // Register that a break target found at the given stop in the
3956 // target stack has been used from the top of the target stack. Add
3957 // the break target to any TargetCollectors passed on the stack.
3958 for (Target* t = target_stack_; t != stop; t = t->previous()) {
3959 TargetCollector* collector = t->node()->AsTargetCollector();
3960 if (collector != NULL) collector->AddTarget(target);
3961 }
3962}
3963
3964
3965Literal* Parser::NewNumberLiteral(double number) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003966 return new(zone()) Literal(isolate()->factory()->NewNumber(number, TENURED));
Steve Blocka7e24c12009-10-30 11:49:00 +00003967}
3968
3969
3970Expression* Parser::NewThrowReferenceError(Handle<String> type) {
Steve Block44f0eee2011-05-26 01:26:41 +01003971 return NewThrowError(isolate()->factory()->MakeReferenceError_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00003972 type, HandleVector<Object>(NULL, 0));
3973}
3974
3975
3976Expression* Parser::NewThrowSyntaxError(Handle<String> type,
3977 Handle<Object> first) {
3978 int argc = first.is_null() ? 0 : 1;
3979 Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc);
Steve Block44f0eee2011-05-26 01:26:41 +01003980 return NewThrowError(
3981 isolate()->factory()->MakeSyntaxError_symbol(), type, arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00003982}
3983
3984
3985Expression* Parser::NewThrowTypeError(Handle<String> type,
3986 Handle<Object> first,
3987 Handle<Object> second) {
3988 ASSERT(!first.is_null() && !second.is_null());
3989 Handle<Object> elements[] = { first, second };
3990 Vector< Handle<Object> > arguments =
3991 HandleVector<Object>(elements, ARRAY_SIZE(elements));
Steve Block44f0eee2011-05-26 01:26:41 +01003992 return NewThrowError(
3993 isolate()->factory()->MakeTypeError_symbol(), type, arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00003994}
3995
3996
3997Expression* Parser::NewThrowError(Handle<String> constructor,
3998 Handle<String> type,
3999 Vector< Handle<Object> > arguments) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004000 int argc = arguments.length();
Steve Block44f0eee2011-05-26 01:26:41 +01004001 Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc,
4002 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004003 for (int i = 0; i < argc; i++) {
4004 Handle<Object> element = arguments[i];
4005 if (!element.is_null()) {
Steve Block1e0659c2011-05-24 12:43:12 +01004006 elements->set(i, *element);
Steve Blocka7e24c12009-10-30 11:49:00 +00004007 }
4008 }
Steve Block44f0eee2011-05-26 01:26:41 +01004009 Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements(elements,
4010 TENURED);
Steve Block1e0659c2011-05-24 12:43:12 +01004011
Steve Blocka7e24c12009-10-30 11:49:00 +00004012 ZoneList<Expression*>* args = new ZoneList<Expression*>(2);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004013 args->Add(new(zone()) Literal(type));
4014 args->Add(new(zone()) Literal(array));
4015 return new(zone()) Throw(new(zone()) CallRuntime(constructor, NULL, args),
Steve Blocka7e24c12009-10-30 11:49:00 +00004016 scanner().location().beg_pos);
4017}
4018
Leon Clarke4515c472010-02-03 11:58:03 +00004019// ----------------------------------------------------------------------------
4020// JSON
4021
Ben Murdochb0fe1622011-05-05 13:52:32 +01004022Handle<Object> JsonParser::ParseJson(Handle<String> script,
4023 UC16CharacterStream* source) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004024 scanner_.Initialize(source);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004025 stack_overflow_ = false;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004026 Handle<Object> result = ParseJsonValue();
4027 if (result.is_null() || scanner_.Next() != Token::EOS) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004028 if (stack_overflow_) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004029 // Scanner failed.
Steve Block44f0eee2011-05-26 01:26:41 +01004030 isolate()->StackOverflow();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004031 } else {
4032 // Parse failed. Scanner's current token is the unexpected token.
4033 Token::Value token = scanner_.current_token();
4034
4035 const char* message;
4036 const char* name_opt = NULL;
4037
4038 switch (token) {
4039 case Token::EOS:
4040 message = "unexpected_eos";
4041 break;
4042 case Token::NUMBER:
4043 message = "unexpected_token_number";
4044 break;
4045 case Token::STRING:
4046 message = "unexpected_token_string";
4047 break;
4048 case Token::IDENTIFIER:
Steve Block1e0659c2011-05-24 12:43:12 +01004049 case Token::FUTURE_RESERVED_WORD:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004050 message = "unexpected_token_identifier";
4051 break;
4052 default:
4053 message = "unexpected_token";
4054 name_opt = Token::String(token);
4055 ASSERT(name_opt != NULL);
4056 break;
4057 }
4058
4059 Scanner::Location source_location = scanner_.location();
Steve Block44f0eee2011-05-26 01:26:41 +01004060 Factory* factory = isolate()->factory();
4061 MessageLocation location(factory->NewScript(script),
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004062 source_location.beg_pos,
4063 source_location.end_pos);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004064 Handle<JSArray> array;
4065 if (name_opt == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +01004066 array = factory->NewJSArray(0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004067 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004068 Handle<String> name = factory->NewStringFromUtf8(CStrVector(name_opt));
4069 Handle<FixedArray> element = factory->NewFixedArray(1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004070 element->set(0, *name);
Steve Block44f0eee2011-05-26 01:26:41 +01004071 array = factory->NewJSArrayWithElements(element);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004072 }
Steve Block44f0eee2011-05-26 01:26:41 +01004073 Handle<Object> result = factory->NewSyntaxError(message, array);
4074 isolate()->Throw(*result, &location);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004075 return Handle<Object>::null();
4076 }
4077 }
Leon Clarke4515c472010-02-03 11:58:03 +00004078 return result;
4079}
4080
4081
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004082Handle<String> JsonParser::GetString() {
4083 int literal_length = scanner_.literal_length();
4084 if (literal_length == 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01004085 return isolate()->factory()->empty_string();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004086 }
Steve Block9fac8402011-05-12 15:51:54 +01004087 if (scanner_.is_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004088 return isolate()->factory()->NewStringFromAscii(
4089 scanner_.literal_ascii_string());
Steve Block9fac8402011-05-12 15:51:54 +01004090 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004091 return isolate()->factory()->NewStringFromTwoByte(
4092 scanner_.literal_uc16_string());
Steve Block9fac8402011-05-12 15:51:54 +01004093 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004094}
4095
4096
Leon Clarke4515c472010-02-03 11:58:03 +00004097// Parse any JSON value.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004098Handle<Object> JsonParser::ParseJsonValue() {
4099 Token::Value token = scanner_.Next();
Leon Clarke4515c472010-02-03 11:58:03 +00004100 switch (token) {
Steve Block1e0659c2011-05-24 12:43:12 +01004101 case Token::STRING:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004102 return GetString();
Steve Block1e0659c2011-05-24 12:43:12 +01004103 case Token::NUMBER:
Steve Block44f0eee2011-05-26 01:26:41 +01004104 return isolate()->factory()->NewNumber(scanner_.number());
Leon Clarke4515c472010-02-03 11:58:03 +00004105 case Token::FALSE_LITERAL:
Steve Block44f0eee2011-05-26 01:26:41 +01004106 return isolate()->factory()->false_value();
Leon Clarke4515c472010-02-03 11:58:03 +00004107 case Token::TRUE_LITERAL:
Steve Block44f0eee2011-05-26 01:26:41 +01004108 return isolate()->factory()->true_value();
Leon Clarke4515c472010-02-03 11:58:03 +00004109 case Token::NULL_LITERAL:
Steve Block44f0eee2011-05-26 01:26:41 +01004110 return isolate()->factory()->null_value();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004111 case Token::LBRACE:
4112 return ParseJsonObject();
4113 case Token::LBRACK:
4114 return ParseJsonArray();
Leon Clarke4515c472010-02-03 11:58:03 +00004115 default:
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004116 return ReportUnexpectedToken();
Leon Clarke4515c472010-02-03 11:58:03 +00004117 }
4118}
4119
4120
4121// Parse a JSON object. Scanner must be right after '{' token.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004122Handle<Object> JsonParser::ParseJsonObject() {
4123 Handle<JSFunction> object_constructor(
Steve Block44f0eee2011-05-26 01:26:41 +01004124 isolate()->global_context()->object_function());
4125 Handle<JSObject> json_object =
4126 isolate()->factory()->NewJSObject(object_constructor);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004127 if (scanner_.peek() == Token::RBRACE) {
4128 scanner_.Next();
4129 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004130 if (StackLimitCheck(isolate()).HasOverflowed()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004131 stack_overflow_ = true;
4132 return Handle<Object>::null();
4133 }
Leon Clarke4515c472010-02-03 11:58:03 +00004134 do {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004135 if (scanner_.Next() != Token::STRING) {
4136 return ReportUnexpectedToken();
4137 }
4138 Handle<String> key = GetString();
4139 if (scanner_.Next() != Token::COLON) {
4140 return ReportUnexpectedToken();
4141 }
4142 Handle<Object> value = ParseJsonValue();
4143 if (value.is_null()) return Handle<Object>::null();
Leon Clarke4515c472010-02-03 11:58:03 +00004144 uint32_t index;
4145 if (key->AsArrayIndex(&index)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004146 SetOwnElement(json_object, index, value, kNonStrictMode);
Steve Block44f0eee2011-05-26 01:26:41 +01004147 } else if (key->Equals(isolate()->heap()->Proto_symbol())) {
Steve Block1e0659c2011-05-24 12:43:12 +01004148 // We can't remove the __proto__ accessor since it's hardcoded
4149 // in several places. Instead go along and add the value as
4150 // the prototype of the created object if possible.
4151 SetPrototype(json_object, value);
Leon Clarke4515c472010-02-03 11:58:03 +00004152 } else {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004153 SetLocalPropertyIgnoreAttributes(json_object, key, value, NONE);
Leon Clarke4515c472010-02-03 11:58:03 +00004154 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004155 } while (scanner_.Next() == Token::COMMA);
4156 if (scanner_.current_token() != Token::RBRACE) {
4157 return ReportUnexpectedToken();
4158 }
Leon Clarke4515c472010-02-03 11:58:03 +00004159 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004160 return json_object;
Leon Clarke4515c472010-02-03 11:58:03 +00004161}
4162
4163
4164// Parse a JSON array. Scanner must be right after '[' token.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004165Handle<Object> JsonParser::ParseJsonArray() {
4166 ZoneScope zone_scope(DELETE_ON_EXIT);
4167 ZoneList<Handle<Object> > elements(4);
Leon Clarke4515c472010-02-03 11:58:03 +00004168
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004169 Token::Value token = scanner_.peek();
4170 if (token == Token::RBRACK) {
4171 scanner_.Next();
4172 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004173 if (StackLimitCheck(isolate()).HasOverflowed()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004174 stack_overflow_ = true;
4175 return Handle<Object>::null();
4176 }
Leon Clarke4515c472010-02-03 11:58:03 +00004177 do {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004178 Handle<Object> element = ParseJsonValue();
4179 if (element.is_null()) return Handle<Object>::null();
4180 elements.Add(element);
4181 token = scanner_.Next();
4182 } while (token == Token::COMMA);
4183 if (token != Token::RBRACK) {
4184 return ReportUnexpectedToken();
4185 }
Leon Clarke4515c472010-02-03 11:58:03 +00004186 }
Leon Clarke4515c472010-02-03 11:58:03 +00004187
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004188 // Allocate a fixed array with all the elements.
4189 Handle<FixedArray> fast_elements =
Steve Block44f0eee2011-05-26 01:26:41 +01004190 isolate()->factory()->NewFixedArray(elements.length());
Leon Clarke4515c472010-02-03 11:58:03 +00004191
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004192 for (int i = 0, n = elements.length(); i < n; i++) {
4193 fast_elements->set(i, *elements[i]);
4194 }
Leon Clarke4515c472010-02-03 11:58:03 +00004195
Steve Block44f0eee2011-05-26 01:26:41 +01004196 return isolate()->factory()->NewJSArrayWithElements(fast_elements);
Leon Clarke4515c472010-02-03 11:58:03 +00004197}
4198
Steve Blocka7e24c12009-10-30 11:49:00 +00004199// ----------------------------------------------------------------------------
4200// Regular expressions
4201
4202
4203RegExpParser::RegExpParser(FlatStringReader* in,
4204 Handle<String>* error,
4205 bool multiline)
Steve Block44f0eee2011-05-26 01:26:41 +01004206 : isolate_(Isolate::Current()),
4207 error_(error),
4208 captures_(NULL),
4209 in_(in),
4210 current_(kEndMarker),
4211 next_pos_(0),
4212 capture_count_(0),
4213 has_more_(true),
4214 multiline_(multiline),
4215 simple_(false),
4216 contains_anchor_(false),
4217 is_scanned_for_captures_(false),
4218 failed_(false) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004219 Advance();
Steve Blocka7e24c12009-10-30 11:49:00 +00004220}
4221
4222
4223uc32 RegExpParser::Next() {
4224 if (has_next()) {
4225 return in()->Get(next_pos_);
4226 } else {
4227 return kEndMarker;
4228 }
4229}
4230
4231
4232void RegExpParser::Advance() {
4233 if (next_pos_ < in()->length()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004234 StackLimitCheck check(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00004235 if (check.HasOverflowed()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004236 ReportError(CStrVector(Isolate::kStackOverflowMessage));
4237 } else if (isolate()->zone()->excess_allocation()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004238 ReportError(CStrVector("Regular expression too large"));
4239 } else {
4240 current_ = in()->Get(next_pos_);
4241 next_pos_++;
4242 }
4243 } else {
4244 current_ = kEndMarker;
4245 has_more_ = false;
4246 }
4247}
4248
4249
4250void RegExpParser::Reset(int pos) {
4251 next_pos_ = pos;
4252 Advance();
4253}
4254
4255
4256void RegExpParser::Advance(int dist) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004257 next_pos_ += dist - 1;
4258 Advance();
Steve Blocka7e24c12009-10-30 11:49:00 +00004259}
4260
4261
4262bool RegExpParser::simple() {
4263 return simple_;
4264}
4265
4266RegExpTree* RegExpParser::ReportError(Vector<const char> message) {
4267 failed_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +01004268 *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004269 // Zip to the end to make sure the no more input is read.
4270 current_ = kEndMarker;
4271 next_pos_ = in()->length();
4272 return NULL;
4273}
4274
4275
4276// Pattern ::
4277// Disjunction
4278RegExpTree* RegExpParser::ParsePattern() {
4279 RegExpTree* result = ParseDisjunction(CHECK_FAILED);
4280 ASSERT(!has_more());
4281 // If the result of parsing is a literal string atom, and it has the
4282 // same length as the input, then the atom is identical to the input.
4283 if (result->IsAtom() && result->AsAtom()->length() == in()->length()) {
4284 simple_ = true;
4285 }
4286 return result;
4287}
4288
4289
4290// Disjunction ::
4291// Alternative
4292// Alternative | Disjunction
4293// Alternative ::
4294// [empty]
4295// Term Alternative
4296// Term ::
4297// Assertion
4298// Atom
4299// Atom Quantifier
4300RegExpTree* RegExpParser::ParseDisjunction() {
4301 // Used to store current state while parsing subexpressions.
4302 RegExpParserState initial_state(NULL, INITIAL, 0);
4303 RegExpParserState* stored_state = &initial_state;
4304 // Cache the builder in a local variable for quick access.
4305 RegExpBuilder* builder = initial_state.builder();
4306 while (true) {
4307 switch (current()) {
4308 case kEndMarker:
4309 if (stored_state->IsSubexpression()) {
4310 // Inside a parenthesized group when hitting end of input.
4311 ReportError(CStrVector("Unterminated group") CHECK_FAILED);
4312 }
4313 ASSERT_EQ(INITIAL, stored_state->group_type());
4314 // Parsing completed successfully.
4315 return builder->ToRegExp();
4316 case ')': {
4317 if (!stored_state->IsSubexpression()) {
4318 ReportError(CStrVector("Unmatched ')'") CHECK_FAILED);
4319 }
4320 ASSERT_NE(INITIAL, stored_state->group_type());
4321
4322 Advance();
4323 // End disjunction parsing and convert builder content to new single
4324 // regexp atom.
4325 RegExpTree* body = builder->ToRegExp();
4326
4327 int end_capture_index = captures_started();
4328
4329 int capture_index = stored_state->capture_index();
4330 SubexpressionType type = stored_state->group_type();
4331
4332 // Restore previous state.
4333 stored_state = stored_state->previous_state();
4334 builder = stored_state->builder();
4335
4336 // Build result of subexpression.
4337 if (type == CAPTURE) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01004338 RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index);
Steve Blocka7e24c12009-10-30 11:49:00 +00004339 captures_->at(capture_index - 1) = capture;
4340 body = capture;
4341 } else if (type != GROUPING) {
4342 ASSERT(type == POSITIVE_LOOKAHEAD || type == NEGATIVE_LOOKAHEAD);
4343 bool is_positive = (type == POSITIVE_LOOKAHEAD);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004344 body = new(zone()) RegExpLookahead(body,
Steve Blocka7e24c12009-10-30 11:49:00 +00004345 is_positive,
4346 end_capture_index - capture_index,
4347 capture_index);
4348 }
4349 builder->AddAtom(body);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004350 // For compatability with JSC and ES3, we allow quantifiers after
4351 // lookaheads, and break in all cases.
Steve Blocka7e24c12009-10-30 11:49:00 +00004352 break;
4353 }
4354 case '|': {
4355 Advance();
4356 builder->NewAlternative();
4357 continue;
4358 }
4359 case '*':
4360 case '+':
4361 case '?':
4362 return ReportError(CStrVector("Nothing to repeat"));
4363 case '^': {
4364 Advance();
4365 if (multiline_) {
4366 builder->AddAssertion(
Ben Murdoch8b112d22011-06-08 16:22:53 +01004367 new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE));
Steve Blocka7e24c12009-10-30 11:49:00 +00004368 } else {
4369 builder->AddAssertion(
Ben Murdoch8b112d22011-06-08 16:22:53 +01004370 new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT));
Steve Blocka7e24c12009-10-30 11:49:00 +00004371 set_contains_anchor();
4372 }
4373 continue;
4374 }
4375 case '$': {
4376 Advance();
4377 RegExpAssertion::Type type =
4378 multiline_ ? RegExpAssertion::END_OF_LINE :
4379 RegExpAssertion::END_OF_INPUT;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004380 builder->AddAssertion(new(zone()) RegExpAssertion(type));
Steve Blocka7e24c12009-10-30 11:49:00 +00004381 continue;
4382 }
4383 case '.': {
4384 Advance();
4385 // everything except \x0a, \x0d, \u2028 and \u2029
4386 ZoneList<CharacterRange>* ranges = new ZoneList<CharacterRange>(2);
4387 CharacterRange::AddClassEscape('.', ranges);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004388 RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004389 builder->AddAtom(atom);
4390 break;
4391 }
4392 case '(': {
4393 SubexpressionType type = CAPTURE;
4394 Advance();
4395 if (current() == '?') {
4396 switch (Next()) {
4397 case ':':
4398 type = GROUPING;
4399 break;
4400 case '=':
4401 type = POSITIVE_LOOKAHEAD;
4402 break;
4403 case '!':
4404 type = NEGATIVE_LOOKAHEAD;
4405 break;
4406 default:
4407 ReportError(CStrVector("Invalid group") CHECK_FAILED);
4408 break;
4409 }
4410 Advance(2);
4411 } else {
4412 if (captures_ == NULL) {
4413 captures_ = new ZoneList<RegExpCapture*>(2);
4414 }
4415 if (captures_started() >= kMaxCaptures) {
4416 ReportError(CStrVector("Too many captures") CHECK_FAILED);
4417 }
4418 captures_->Add(NULL);
4419 }
4420 // Store current state and begin new disjunction parsing.
Ben Murdoch8b112d22011-06-08 16:22:53 +01004421 stored_state = new(zone()) RegExpParserState(stored_state,
Steve Blocka7e24c12009-10-30 11:49:00 +00004422 type,
4423 captures_started());
4424 builder = stored_state->builder();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004425 continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00004426 }
4427 case '[': {
4428 RegExpTree* atom = ParseCharacterClass(CHECK_FAILED);
4429 builder->AddAtom(atom);
4430 break;
4431 }
4432 // Atom ::
4433 // \ AtomEscape
4434 case '\\':
4435 switch (Next()) {
4436 case kEndMarker:
4437 return ReportError(CStrVector("\\ at end of pattern"));
4438 case 'b':
4439 Advance(2);
4440 builder->AddAssertion(
Ben Murdoch8b112d22011-06-08 16:22:53 +01004441 new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY));
Steve Blocka7e24c12009-10-30 11:49:00 +00004442 continue;
4443 case 'B':
4444 Advance(2);
4445 builder->AddAssertion(
Ben Murdoch8b112d22011-06-08 16:22:53 +01004446 new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY));
Steve Blocka7e24c12009-10-30 11:49:00 +00004447 continue;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004448 // AtomEscape ::
4449 // CharacterClassEscape
4450 //
4451 // CharacterClassEscape :: one of
4452 // d D s S w W
Steve Blocka7e24c12009-10-30 11:49:00 +00004453 case 'd': case 'D': case 's': case 'S': case 'w': case 'W': {
4454 uc32 c = Next();
4455 Advance(2);
4456 ZoneList<CharacterRange>* ranges = new ZoneList<CharacterRange>(2);
4457 CharacterRange::AddClassEscape(c, ranges);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004458 RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004459 builder->AddAtom(atom);
4460 break;
4461 }
4462 case '1': case '2': case '3': case '4': case '5': case '6':
4463 case '7': case '8': case '9': {
4464 int index = 0;
4465 if (ParseBackReferenceIndex(&index)) {
4466 RegExpCapture* capture = NULL;
4467 if (captures_ != NULL && index <= captures_->length()) {
4468 capture = captures_->at(index - 1);
4469 }
4470 if (capture == NULL) {
4471 builder->AddEmpty();
4472 break;
4473 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004474 RegExpTree* atom = new(zone()) RegExpBackReference(capture);
Steve Blocka7e24c12009-10-30 11:49:00 +00004475 builder->AddAtom(atom);
4476 break;
4477 }
4478 uc32 first_digit = Next();
4479 if (first_digit == '8' || first_digit == '9') {
4480 // Treat as identity escape
4481 builder->AddCharacter(first_digit);
4482 Advance(2);
4483 break;
4484 }
4485 }
4486 // FALLTHROUGH
4487 case '0': {
4488 Advance();
4489 uc32 octal = ParseOctalLiteral();
4490 builder->AddCharacter(octal);
4491 break;
4492 }
4493 // ControlEscape :: one of
4494 // f n r t v
4495 case 'f':
4496 Advance(2);
4497 builder->AddCharacter('\f');
4498 break;
4499 case 'n':
4500 Advance(2);
4501 builder->AddCharacter('\n');
4502 break;
4503 case 'r':
4504 Advance(2);
4505 builder->AddCharacter('\r');
4506 break;
4507 case 't':
4508 Advance(2);
4509 builder->AddCharacter('\t');
4510 break;
4511 case 'v':
4512 Advance(2);
4513 builder->AddCharacter('\v');
4514 break;
4515 case 'c': {
Ben Murdoch086aeea2011-05-13 15:57:08 +01004516 Advance();
4517 uc32 controlLetter = Next();
4518 // Special case if it is an ASCII letter.
4519 // Convert lower case letters to uppercase.
4520 uc32 letter = controlLetter & ~('a' ^ 'A');
4521 if (letter < 'A' || 'Z' < letter) {
4522 // controlLetter is not in range 'A'-'Z' or 'a'-'z'.
4523 // This is outside the specification. We match JSC in
4524 // reading the backslash as a literal character instead
4525 // of as starting an escape.
4526 builder->AddCharacter('\\');
4527 } else {
4528 Advance(2);
4529 builder->AddCharacter(controlLetter & 0x1f);
4530 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004531 break;
4532 }
4533 case 'x': {
4534 Advance(2);
4535 uc32 value;
4536 if (ParseHexEscape(2, &value)) {
4537 builder->AddCharacter(value);
4538 } else {
4539 builder->AddCharacter('x');
4540 }
4541 break;
4542 }
4543 case 'u': {
4544 Advance(2);
4545 uc32 value;
4546 if (ParseHexEscape(4, &value)) {
4547 builder->AddCharacter(value);
4548 } else {
4549 builder->AddCharacter('u');
4550 }
4551 break;
4552 }
4553 default:
4554 // Identity escape.
4555 builder->AddCharacter(Next());
4556 Advance(2);
4557 break;
4558 }
4559 break;
4560 case '{': {
4561 int dummy;
4562 if (ParseIntervalQuantifier(&dummy, &dummy)) {
4563 ReportError(CStrVector("Nothing to repeat") CHECK_FAILED);
4564 }
4565 // fallthrough
4566 }
4567 default:
4568 builder->AddCharacter(current());
4569 Advance();
4570 break;
4571 } // end switch(current())
4572
4573 int min;
4574 int max;
4575 switch (current()) {
4576 // QuantifierPrefix ::
4577 // *
4578 // +
4579 // ?
4580 // {
4581 case '*':
4582 min = 0;
4583 max = RegExpTree::kInfinity;
4584 Advance();
4585 break;
4586 case '+':
4587 min = 1;
4588 max = RegExpTree::kInfinity;
4589 Advance();
4590 break;
4591 case '?':
4592 min = 0;
4593 max = 1;
4594 Advance();
4595 break;
4596 case '{':
4597 if (ParseIntervalQuantifier(&min, &max)) {
4598 if (max < min) {
4599 ReportError(CStrVector("numbers out of order in {} quantifier.")
4600 CHECK_FAILED);
4601 }
4602 break;
4603 } else {
4604 continue;
4605 }
4606 default:
4607 continue;
4608 }
Leon Clarkee46be812010-01-19 14:06:41 +00004609 RegExpQuantifier::Type type = RegExpQuantifier::GREEDY;
Steve Blocka7e24c12009-10-30 11:49:00 +00004610 if (current() == '?') {
Leon Clarkee46be812010-01-19 14:06:41 +00004611 type = RegExpQuantifier::NON_GREEDY;
4612 Advance();
4613 } else if (FLAG_regexp_possessive_quantifier && current() == '+') {
4614 // FLAG_regexp_possessive_quantifier is a debug-only flag.
4615 type = RegExpQuantifier::POSSESSIVE;
Steve Blocka7e24c12009-10-30 11:49:00 +00004616 Advance();
4617 }
Leon Clarkee46be812010-01-19 14:06:41 +00004618 builder->AddQuantifierToAtom(min, max, type);
Steve Blocka7e24c12009-10-30 11:49:00 +00004619 }
4620}
4621
Steve Blocka7e24c12009-10-30 11:49:00 +00004622
4623#ifdef DEBUG
4624// Currently only used in an ASSERT.
4625static bool IsSpecialClassEscape(uc32 c) {
4626 switch (c) {
4627 case 'd': case 'D':
4628 case 's': case 'S':
4629 case 'w': case 'W':
4630 return true;
4631 default:
4632 return false;
4633 }
4634}
4635#endif
4636
4637
4638// In order to know whether an escape is a backreference or not we have to scan
4639// the entire regexp and find the number of capturing parentheses. However we
4640// don't want to scan the regexp twice unless it is necessary. This mini-parser
4641// is called when needed. It can see the difference between capturing and
4642// noncapturing parentheses and can skip character classes and backslash-escaped
4643// characters.
4644void RegExpParser::ScanForCaptures() {
4645 // Start with captures started previous to current position
4646 int capture_count = captures_started();
4647 // Add count of captures after this position.
4648 int n;
4649 while ((n = current()) != kEndMarker) {
4650 Advance();
4651 switch (n) {
4652 case '\\':
4653 Advance();
4654 break;
4655 case '[': {
4656 int c;
4657 while ((c = current()) != kEndMarker) {
4658 Advance();
4659 if (c == '\\') {
4660 Advance();
4661 } else {
4662 if (c == ']') break;
4663 }
4664 }
4665 break;
4666 }
4667 case '(':
4668 if (current() != '?') capture_count++;
4669 break;
4670 }
4671 }
4672 capture_count_ = capture_count;
4673 is_scanned_for_captures_ = true;
4674}
4675
4676
4677bool RegExpParser::ParseBackReferenceIndex(int* index_out) {
4678 ASSERT_EQ('\\', current());
4679 ASSERT('1' <= Next() && Next() <= '9');
4680 // Try to parse a decimal literal that is no greater than the total number
4681 // of left capturing parentheses in the input.
4682 int start = position();
4683 int value = Next() - '0';
4684 Advance(2);
4685 while (true) {
4686 uc32 c = current();
4687 if (IsDecimalDigit(c)) {
4688 value = 10 * value + (c - '0');
4689 if (value > kMaxCaptures) {
4690 Reset(start);
4691 return false;
4692 }
4693 Advance();
4694 } else {
4695 break;
4696 }
4697 }
4698 if (value > captures_started()) {
4699 if (!is_scanned_for_captures_) {
4700 int saved_position = position();
4701 ScanForCaptures();
4702 Reset(saved_position);
4703 }
4704 if (value > capture_count_) {
4705 Reset(start);
4706 return false;
4707 }
4708 }
4709 *index_out = value;
4710 return true;
4711}
4712
4713
4714// QuantifierPrefix ::
4715// { DecimalDigits }
4716// { DecimalDigits , }
4717// { DecimalDigits , DecimalDigits }
4718//
4719// Returns true if parsing succeeds, and set the min_out and max_out
4720// values. Values are truncated to RegExpTree::kInfinity if they overflow.
4721bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) {
4722 ASSERT_EQ(current(), '{');
4723 int start = position();
4724 Advance();
4725 int min = 0;
4726 if (!IsDecimalDigit(current())) {
4727 Reset(start);
4728 return false;
4729 }
4730 while (IsDecimalDigit(current())) {
4731 int next = current() - '0';
4732 if (min > (RegExpTree::kInfinity - next) / 10) {
4733 // Overflow. Skip past remaining decimal digits and return -1.
4734 do {
4735 Advance();
4736 } while (IsDecimalDigit(current()));
4737 min = RegExpTree::kInfinity;
4738 break;
4739 }
4740 min = 10 * min + next;
4741 Advance();
4742 }
4743 int max = 0;
4744 if (current() == '}') {
4745 max = min;
4746 Advance();
4747 } else if (current() == ',') {
4748 Advance();
4749 if (current() == '}') {
4750 max = RegExpTree::kInfinity;
4751 Advance();
4752 } else {
4753 while (IsDecimalDigit(current())) {
4754 int next = current() - '0';
4755 if (max > (RegExpTree::kInfinity - next) / 10) {
4756 do {
4757 Advance();
4758 } while (IsDecimalDigit(current()));
4759 max = RegExpTree::kInfinity;
4760 break;
4761 }
4762 max = 10 * max + next;
4763 Advance();
4764 }
4765 if (current() != '}') {
4766 Reset(start);
4767 return false;
4768 }
4769 Advance();
4770 }
4771 } else {
4772 Reset(start);
4773 return false;
4774 }
4775 *min_out = min;
4776 *max_out = max;
4777 return true;
4778}
4779
4780
Steve Blocka7e24c12009-10-30 11:49:00 +00004781uc32 RegExpParser::ParseOctalLiteral() {
4782 ASSERT('0' <= current() && current() <= '7');
4783 // For compatibility with some other browsers (not all), we parse
4784 // up to three octal digits with a value below 256.
4785 uc32 value = current() - '0';
4786 Advance();
4787 if ('0' <= current() && current() <= '7') {
4788 value = value * 8 + current() - '0';
4789 Advance();
4790 if (value < 32 && '0' <= current() && current() <= '7') {
4791 value = value * 8 + current() - '0';
4792 Advance();
4793 }
4794 }
4795 return value;
4796}
4797
4798
4799bool RegExpParser::ParseHexEscape(int length, uc32 *value) {
4800 int start = position();
4801 uc32 val = 0;
4802 bool done = false;
4803 for (int i = 0; !done; i++) {
4804 uc32 c = current();
4805 int d = HexValue(c);
4806 if (d < 0) {
4807 Reset(start);
4808 return false;
4809 }
4810 val = val * 16 + d;
4811 Advance();
4812 if (i == length - 1) {
4813 done = true;
4814 }
4815 }
4816 *value = val;
4817 return true;
4818}
4819
4820
4821uc32 RegExpParser::ParseClassCharacterEscape() {
4822 ASSERT(current() == '\\');
4823 ASSERT(has_next() && !IsSpecialClassEscape(Next()));
4824 Advance();
4825 switch (current()) {
4826 case 'b':
4827 Advance();
4828 return '\b';
4829 // ControlEscape :: one of
4830 // f n r t v
4831 case 'f':
4832 Advance();
4833 return '\f';
4834 case 'n':
4835 Advance();
4836 return '\n';
4837 case 'r':
4838 Advance();
4839 return '\r';
4840 case 't':
4841 Advance();
4842 return '\t';
4843 case 'v':
4844 Advance();
4845 return '\v';
Ben Murdoch086aeea2011-05-13 15:57:08 +01004846 case 'c': {
4847 uc32 controlLetter = Next();
4848 uc32 letter = controlLetter & ~('A' ^ 'a');
4849 // For compatibility with JSC, inside a character class
4850 // we also accept digits and underscore as control characters.
4851 if ((controlLetter >= '0' && controlLetter <= '9') ||
4852 controlLetter == '_' ||
4853 (letter >= 'A' && letter <= 'Z')) {
4854 Advance(2);
4855 // Control letters mapped to ASCII control characters in the range
4856 // 0x00-0x1f.
4857 return controlLetter & 0x1f;
4858 }
4859 // We match JSC in reading the backslash as a literal
4860 // character instead of as starting an escape.
4861 return '\\';
4862 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004863 case '0': case '1': case '2': case '3': case '4': case '5':
4864 case '6': case '7':
4865 // For compatibility, we interpret a decimal escape that isn't
4866 // a back reference (and therefore either \0 or not valid according
4867 // to the specification) as a 1..3 digit octal character code.
4868 return ParseOctalLiteral();
4869 case 'x': {
4870 Advance();
4871 uc32 value;
4872 if (ParseHexEscape(2, &value)) {
4873 return value;
4874 }
4875 // If \x is not followed by a two-digit hexadecimal, treat it
4876 // as an identity escape.
4877 return 'x';
4878 }
4879 case 'u': {
4880 Advance();
4881 uc32 value;
4882 if (ParseHexEscape(4, &value)) {
4883 return value;
4884 }
4885 // If \u is not followed by a four-digit hexadecimal, treat it
4886 // as an identity escape.
4887 return 'u';
4888 }
4889 default: {
4890 // Extended identity escape. We accept any character that hasn't
4891 // been matched by a more specific case, not just the subset required
4892 // by the ECMAScript specification.
4893 uc32 result = current();
4894 Advance();
4895 return result;
4896 }
4897 }
4898 return 0;
4899}
4900
4901
4902CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) {
4903 ASSERT_EQ(0, *char_class);
4904 uc32 first = current();
4905 if (first == '\\') {
4906 switch (Next()) {
4907 case 'w': case 'W': case 'd': case 'D': case 's': case 'S': {
4908 *char_class = Next();
4909 Advance(2);
4910 return CharacterRange::Singleton(0); // Return dummy value.
4911 }
4912 case kEndMarker:
4913 return ReportError(CStrVector("\\ at end of pattern"));
4914 default:
4915 uc32 c = ParseClassCharacterEscape(CHECK_FAILED);
4916 return CharacterRange::Singleton(c);
4917 }
4918 } else {
4919 Advance();
4920 return CharacterRange::Singleton(first);
4921 }
4922}
4923
4924
Ben Murdochb0fe1622011-05-05 13:52:32 +01004925static const uc16 kNoCharClass = 0;
4926
4927// Adds range or pre-defined character class to character ranges.
4928// If char_class is not kInvalidClass, it's interpreted as a class
4929// escape (i.e., 's' means whitespace, from '\s').
4930static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges,
4931 uc16 char_class,
4932 CharacterRange range) {
4933 if (char_class != kNoCharClass) {
4934 CharacterRange::AddClassEscape(char_class, ranges);
4935 } else {
4936 ranges->Add(range);
4937 }
4938}
4939
4940
Steve Blocka7e24c12009-10-30 11:49:00 +00004941RegExpTree* RegExpParser::ParseCharacterClass() {
4942 static const char* kUnterminated = "Unterminated character class";
4943 static const char* kRangeOutOfOrder = "Range out of order in character class";
4944
4945 ASSERT_EQ(current(), '[');
4946 Advance();
4947 bool is_negated = false;
4948 if (current() == '^') {
4949 is_negated = true;
4950 Advance();
4951 }
4952 ZoneList<CharacterRange>* ranges = new ZoneList<CharacterRange>(2);
4953 while (has_more() && current() != ']') {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004954 uc16 char_class = kNoCharClass;
Steve Blocka7e24c12009-10-30 11:49:00 +00004955 CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004956 if (current() == '-') {
4957 Advance();
4958 if (current() == kEndMarker) {
4959 // If we reach the end we break out of the loop and let the
4960 // following code report an error.
4961 break;
4962 } else if (current() == ']') {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004963 AddRangeOrEscape(ranges, char_class, first);
Steve Blocka7e24c12009-10-30 11:49:00 +00004964 ranges->Add(CharacterRange::Singleton('-'));
4965 break;
4966 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004967 uc16 char_class_2 = kNoCharClass;
4968 CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED);
4969 if (char_class != kNoCharClass || char_class_2 != kNoCharClass) {
4970 // Either end is an escaped character class. Treat the '-' verbatim.
4971 AddRangeOrEscape(ranges, char_class, first);
Steve Blocka7e24c12009-10-30 11:49:00 +00004972 ranges->Add(CharacterRange::Singleton('-'));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004973 AddRangeOrEscape(ranges, char_class_2, next);
Steve Blocka7e24c12009-10-30 11:49:00 +00004974 continue;
4975 }
4976 if (first.from() > next.to()) {
4977 return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED);
4978 }
4979 ranges->Add(CharacterRange::Range(first.from(), next.to()));
4980 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01004981 AddRangeOrEscape(ranges, char_class, first);
Steve Blocka7e24c12009-10-30 11:49:00 +00004982 }
4983 }
4984 if (!has_more()) {
4985 return ReportError(CStrVector(kUnterminated) CHECK_FAILED);
4986 }
4987 Advance();
4988 if (ranges->length() == 0) {
4989 ranges->Add(CharacterRange::Everything());
4990 is_negated = !is_negated;
4991 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004992 return new(zone()) RegExpCharacterClass(ranges, is_negated);
Steve Blocka7e24c12009-10-30 11:49:00 +00004993}
4994
4995
4996// ----------------------------------------------------------------------------
4997// The Parser interface.
4998
Steve Blocka7e24c12009-10-30 11:49:00 +00004999ParserMessage::~ParserMessage() {
5000 for (int i = 0; i < args().length(); i++)
5001 DeleteArray(args()[i]);
5002 DeleteArray(args().start());
5003}
5004
5005
5006ScriptDataImpl::~ScriptDataImpl() {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005007 if (owns_store_) store_.Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +00005008}
5009
5010
5011int ScriptDataImpl::Length() {
Leon Clarkef7060e22010-06-03 12:02:55 +01005012 return store_.length() * sizeof(unsigned);
Steve Blocka7e24c12009-10-30 11:49:00 +00005013}
5014
5015
Leon Clarkef7060e22010-06-03 12:02:55 +01005016const char* ScriptDataImpl::Data() {
5017 return reinterpret_cast<const char*>(store_.start());
Steve Blocka7e24c12009-10-30 11:49:00 +00005018}
5019
5020
Leon Clarkee46be812010-01-19 14:06:41 +00005021bool ScriptDataImpl::HasError() {
5022 return has_error();
5023}
5024
5025
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005026void ScriptDataImpl::Initialize() {
5027 // Prepares state for use.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005028 if (store_.length() >= PreparseDataConstants::kHeaderSize) {
5029 function_index_ = PreparseDataConstants::kHeaderSize;
5030 int symbol_data_offset = PreparseDataConstants::kHeaderSize
5031 + store_[PreparseDataConstants::kFunctionsSizeOffset];
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005032 if (store_.length() > symbol_data_offset) {
5033 symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]);
5034 } else {
5035 // Partial preparse causes no symbol information.
5036 symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5037 }
5038 symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5039 }
5040}
5041
5042
5043int ScriptDataImpl::ReadNumber(byte** source) {
5044 // Reads a number from symbol_data_ in base 128. The most significant
5045 // bit marks that there are more digits.
5046 // If the first byte is 0x80 (kNumberTerminator), it would normally
5047 // represent a leading zero. Since that is useless, and therefore won't
5048 // appear as the first digit of any actual value, it is used to
5049 // mark the end of the input stream.
5050 byte* data = *source;
5051 if (data >= symbol_data_end_) return -1;
5052 byte input = *data;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005053 if (input == PreparseDataConstants::kNumberTerminator) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005054 // End of stream marker.
5055 return -1;
5056 }
5057 int result = input & 0x7f;
5058 data++;
5059 while ((input & 0x80u) != 0) {
5060 if (data >= symbol_data_end_) return -1;
5061 input = *data;
5062 result = (result << 7) | (input & 0x7f);
5063 data++;
5064 }
5065 *source = data;
5066 return result;
5067}
5068
5069
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005070// Create a Scanner for the preparser to use as input, and preparse the source.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005071static ScriptDataImpl* DoPreParse(UC16CharacterStream* source,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005072 bool allow_lazy,
Steve Block9fac8402011-05-12 15:51:54 +01005073 ParserRecorder* recorder) {
Steve Block44f0eee2011-05-26 01:26:41 +01005074 Isolate* isolate = Isolate::Current();
Ben Murdoch8b112d22011-06-08 16:22:53 +01005075 V8JavaScriptScanner scanner(isolate->unicode_cache());
Steve Block9fac8402011-05-12 15:51:54 +01005076 scanner.Initialize(source);
Steve Block44f0eee2011-05-26 01:26:41 +01005077 intptr_t stack_limit = isolate->stack_guard()->real_climit();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005078 if (!preparser::PreParser::PreParseProgram(&scanner,
5079 recorder,
5080 allow_lazy,
5081 stack_limit)) {
Steve Block44f0eee2011-05-26 01:26:41 +01005082 isolate->StackOverflow();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005083 return NULL;
5084 }
5085
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005086 // Extract the accumulated data from the recorder as a single
5087 // contiguous vector that we are responsible for disposing.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005088 Vector<unsigned> store = recorder->ExtractData();
Steve Blocka7e24c12009-10-30 11:49:00 +00005089 return new ScriptDataImpl(store);
5090}
5091
5092
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005093// Preparse, but only collect data that is immediately useful,
5094// even if the preparser data is only used once.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005095ScriptDataImpl* ParserApi::PartialPreParse(UC16CharacterStream* source,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005096 v8::Extension* extension) {
5097 bool allow_lazy = FLAG_lazy && (extension == NULL);
5098 if (!allow_lazy) {
5099 // Partial preparsing is only about lazily compiled functions.
5100 // If we don't allow lazy compilation, the log data will be empty.
5101 return NULL;
5102 }
5103 PartialParserRecorder recorder;
Steve Block9fac8402011-05-12 15:51:54 +01005104 return DoPreParse(source, allow_lazy, &recorder);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005105}
5106
5107
Ben Murdochb0fe1622011-05-05 13:52:32 +01005108ScriptDataImpl* ParserApi::PreParse(UC16CharacterStream* source,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005109 v8::Extension* extension) {
5110 Handle<Script> no_script;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005111 bool allow_lazy = FLAG_lazy && (extension == NULL);
5112 CompleteParserRecorder recorder;
Steve Block9fac8402011-05-12 15:51:54 +01005113 return DoPreParse(source, allow_lazy, &recorder);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005114}
5115
5116
5117bool RegExpParser::ParseRegExp(FlatStringReader* input,
5118 bool multiline,
5119 RegExpCompileData* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005120 ASSERT(result != NULL);
5121 RegExpParser parser(input, &result->error, multiline);
5122 RegExpTree* tree = parser.ParsePattern();
5123 if (parser.failed()) {
5124 ASSERT(tree == NULL);
5125 ASSERT(!result->error.is_null());
5126 } else {
5127 ASSERT(tree != NULL);
5128 ASSERT(result->error.is_null());
5129 result->tree = tree;
5130 int capture_count = parser.captures_started();
5131 result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
5132 result->contains_anchor = parser.contains_anchor();
5133 result->capture_count = capture_count;
5134 }
5135 return !parser.failed();
5136}
5137
5138
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005139bool ParserApi::Parse(CompilationInfo* info) {
Ben Murdochf87a2032010-10-22 12:50:53 +01005140 ASSERT(info->function() == NULL);
5141 FunctionLiteral* result = NULL;
5142 Handle<Script> script = info->script();
5143 if (info->is_lazy()) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005144 Parser parser(script, true, NULL, NULL);
Steve Block44f0eee2011-05-26 01:26:41 +01005145 result = parser.ParseLazy(info);
Leon Clarke4515c472010-02-03 11:58:03 +00005146 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +01005147 bool allow_natives_syntax =
Steve Block44f0eee2011-05-26 01:26:41 +01005148 info->allows_natives_syntax() || FLAG_allow_natives_syntax;
Ben Murdochf87a2032010-10-22 12:50:53 +01005149 ScriptDataImpl* pre_data = info->pre_parse_data();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08005150 Parser parser(script, allow_natives_syntax, info->extension(), pre_data);
Ben Murdochf87a2032010-10-22 12:50:53 +01005151 if (pre_data != NULL && pre_data->has_error()) {
5152 Scanner::Location loc = pre_data->MessageLocation();
5153 const char* message = pre_data->BuildMessage();
5154 Vector<const char*> args = pre_data->BuildArgs();
5155 parser.ReportMessageAt(loc, message, args);
5156 DeleteArray(message);
5157 for (int i = 0; i < args.length(); i++) {
5158 DeleteArray(args[i]);
5159 }
5160 DeleteArray(args.start());
Steve Block44f0eee2011-05-26 01:26:41 +01005161 ASSERT(info->isolate()->has_pending_exception());
Ben Murdochf87a2032010-10-22 12:50:53 +01005162 } else {
5163 Handle<String> source = Handle<String>(String::cast(script->source()));
Steve Block1e0659c2011-05-24 12:43:12 +01005164 result = parser.ParseProgram(source,
5165 info->is_global(),
5166 info->StrictMode());
Ben Murdochf87a2032010-10-22 12:50:53 +01005167 }
Leon Clarke4515c472010-02-03 11:58:03 +00005168 }
Ben Murdochf87a2032010-10-22 12:50:53 +01005169
5170 info->SetFunction(result);
5171 return (result != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00005172}
5173
Steve Blocka7e24c12009-10-30 11:49:00 +00005174} } // namespace v8::internal