1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "bootstrapper.h"
10 #include "char-predicates-inl.h"
16 #include "preparser.h"
18 #include "scanner-character-streams.h"
19 #include "scopeinfo.h"
20 #include "string-stream.h"
25 RegExpBuilder::RegExpBuilder(Zone* zone)
27 pending_empty_(false),
32 , last_added_(ADD_NONE)
37 void RegExpBuilder::FlushCharacters() {
38 pending_empty_ = false;
39 if (characters_ != NULL) {
40 RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector());
42 text_.Add(atom, zone());
48 void RegExpBuilder::FlushText() {
50 int num_text = text_.length();
53 } else if (num_text == 1) {
54 terms_.Add(text_.last(), zone());
56 RegExpText* text = new(zone()) RegExpText(zone());
57 for (int i = 0; i < num_text; i++)
58 text_.Get(i)->AppendToText(text, zone());
59 terms_.Add(text, zone());
65 void RegExpBuilder::AddCharacter(uc16 c) {
66 pending_empty_ = false;
67 if (characters_ == NULL) {
68 characters_ = new(zone()) ZoneList<uc16>(4, zone());
70 characters_->Add(c, zone());
75 void RegExpBuilder::AddEmpty() {
76 pending_empty_ = true;
80 void RegExpBuilder::AddAtom(RegExpTree* term) {
81 if (term->IsEmpty()) {
85 if (term->IsTextElement()) {
87 text_.Add(term, zone());
90 terms_.Add(term, zone());
96 void RegExpBuilder::AddAssertion(RegExpTree* assert) {
98 terms_.Add(assert, zone());
103 void RegExpBuilder::NewAlternative() {
108 void RegExpBuilder::FlushTerms() {
110 int num_terms = terms_.length();
111 RegExpTree* alternative;
112 if (num_terms == 0) {
113 alternative = RegExpEmpty::GetInstance();
114 } else if (num_terms == 1) {
115 alternative = terms_.last();
117 alternative = new(zone()) RegExpAlternative(terms_.GetList(zone()));
119 alternatives_.Add(alternative, zone());
125 RegExpTree* RegExpBuilder::ToRegExp() {
127 int num_alternatives = alternatives_.length();
128 if (num_alternatives == 0) {
129 return RegExpEmpty::GetInstance();
131 if (num_alternatives == 1) {
132 return alternatives_.last();
134 return new(zone()) RegExpDisjunction(alternatives_.GetList(zone()));
138 void RegExpBuilder::AddQuantifierToAtom(
139 int min, int max, RegExpQuantifier::QuantifierType quantifier_type) {
140 if (pending_empty_) {
141 pending_empty_ = false;
145 if (characters_ != NULL) {
146 ASSERT(last_added_ == ADD_CHAR);
147 // Last atom was character.
148 Vector<const uc16> char_vector = characters_->ToConstVector();
149 int num_chars = char_vector.length();
151 Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1);
152 text_.Add(new(zone()) RegExpAtom(prefix), zone());
153 char_vector = char_vector.SubVector(num_chars - 1, num_chars);
156 atom = new(zone()) RegExpAtom(char_vector);
158 } else if (text_.length() > 0) {
159 ASSERT(last_added_ == ADD_ATOM);
160 atom = text_.RemoveLast();
162 } else if (terms_.length() > 0) {
163 ASSERT(last_added_ == ADD_ATOM);
164 atom = terms_.RemoveLast();
165 if (atom->max_match() == 0) {
166 // Guaranteed to only match an empty string.
171 terms_.Add(atom, zone());
175 // Only call immediately after adding an atom or character!
180 new(zone()) RegExpQuantifier(min, max, quantifier_type, atom), zone());
185 ScriptData* ScriptData::New(const char* data, int length) {
186 // The length is obviously invalid.
187 if (length % sizeof(unsigned) != 0) {
191 int deserialized_data_length = length / sizeof(unsigned);
192 unsigned* deserialized_data;
193 bool owns_store = reinterpret_cast<intptr_t>(data) % sizeof(unsigned) != 0;
195 // Copy the data to align it.
196 deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
197 i::CopyBytes(reinterpret_cast<char*>(deserialized_data),
198 data, static_cast<size_t>(length));
200 // If aligned, don't create a copy of the data.
201 deserialized_data = reinterpret_cast<unsigned*>(const_cast<char*>(data));
203 return new ScriptData(
204 Vector<unsigned>(deserialized_data, deserialized_data_length),
209 FunctionEntry ScriptData::GetFunctionEntry(int start) {
210 // The current pre-data entry must be a FunctionEntry with the given
212 if ((function_index_ + FunctionEntry::kSize <= store_.length())
213 && (static_cast<int>(store_[function_index_]) == start)) {
214 int index = function_index_;
215 function_index_ += FunctionEntry::kSize;
216 return FunctionEntry(store_.SubVector(index,
217 index + FunctionEntry::kSize));
219 return FunctionEntry();
223 int ScriptData::GetSymbolIdentifier() {
224 return ReadNumber(&symbol_data_);
228 bool ScriptData::SanityCheck() {
229 // Check that the header data is valid and doesn't specify
230 // point to positions outside the store.
231 if (store_.length() < PreparseDataConstants::kHeaderSize) return false;
232 if (magic() != PreparseDataConstants::kMagicNumber) return false;
233 if (version() != PreparseDataConstants::kCurrentVersion) return false;
235 // Extra sane sanity check for error message encoding.
236 if (store_.length() <= PreparseDataConstants::kHeaderSize
237 + PreparseDataConstants::kMessageTextPos) {
240 if (Read(PreparseDataConstants::kMessageStartPos) >
241 Read(PreparseDataConstants::kMessageEndPos)) {
244 unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
245 int pos = PreparseDataConstants::kMessageTextPos;
246 for (unsigned int i = 0; i <= arg_count; i++) {
247 if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) {
250 int length = static_cast<int>(Read(pos));
251 if (length < 0) return false;
254 if (store_.length() < PreparseDataConstants::kHeaderSize + pos) {
259 // Check that the space allocated for function entries is sane.
261 static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]);
262 if (functions_size < 0) return false;
263 if (functions_size % FunctionEntry::kSize != 0) return false;
264 // Check that the total size has room for header and function entries.
266 PreparseDataConstants::kHeaderSize + functions_size;
267 if (store_.length() < minimum_size) return false;
273 const char* ScriptData::ReadString(unsigned* start, int* chars) {
274 int length = start[0];
275 char* result = NewArray<char>(length + 1);
276 for (int i = 0; i < length; i++) {
277 result[i] = start[i + 1];
279 result[length] = '\0';
280 if (chars != NULL) *chars = length;
285 Scanner::Location ScriptData::MessageLocation() const {
286 int beg_pos = Read(PreparseDataConstants::kMessageStartPos);
287 int end_pos = Read(PreparseDataConstants::kMessageEndPos);
288 return Scanner::Location(beg_pos, end_pos);
292 bool ScriptData::IsReferenceError() const {
293 return Read(PreparseDataConstants::kIsReferenceErrorPos);
297 const char* ScriptData::BuildMessage() const {
298 unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos);
299 return ReadString(start, NULL);
303 Vector<const char*> ScriptData::BuildArgs() const {
304 int arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
305 const char** array = NewArray<const char*>(arg_count);
306 // Position after text found by skipping past length field and
307 // length field content words.
308 int pos = PreparseDataConstants::kMessageTextPos + 1
309 + Read(PreparseDataConstants::kMessageTextPos);
310 for (int i = 0; i < arg_count; i++) {
312 array[i] = ReadString(ReadAddress(pos), &count);
315 return Vector<const char*>(array, arg_count);
319 unsigned ScriptData::Read(int position) const {
320 return store_[PreparseDataConstants::kHeaderSize + position];
324 unsigned* ScriptData::ReadAddress(int position) const {
325 return &store_[PreparseDataConstants::kHeaderSize + position];
329 Scope* Parser::NewScope(Scope* parent, ScopeType scope_type) {
330 Scope* result = new(zone()) Scope(parent, scope_type, zone());
331 result->Initialize();
336 // ----------------------------------------------------------------------------
337 // Target is a support class to facilitate manipulation of the
338 // Parser's target_stack_ (the stack of potential 'break' and
339 // 'continue' statement targets). Upon construction, a new target is
340 // added; it is removed upon destruction.
342 class Target BASE_EMBEDDED {
344 Target(Target** variable, AstNode* node)
345 : variable_(variable), node_(node), previous_(*variable) {
350 *variable_ = previous_;
353 Target* previous() { return previous_; }
354 AstNode* node() { return node_; }
363 class TargetScope BASE_EMBEDDED {
365 explicit TargetScope(Target** variable)
366 : variable_(variable), previous_(*variable) {
371 *variable_ = previous_;
380 // ----------------------------------------------------------------------------
381 // The CHECK_OK macro is a convenient macro to enforce error
382 // handling for functions that may fail (by returning !*ok).
384 // CAUTION: This macro appends extra statements after a call,
385 // thus it must never be used where only a single statement
386 // is correct (e.g. an if statement branch w/o braces)!
388 #define CHECK_OK ok); \
389 if (!*ok) return NULL; \
391 #define DUMMY ) // to make indentation work
394 #define CHECK_FAILED /**/); \
395 if (failed_) return NULL; \
397 #define DUMMY ) // to make indentation work
400 // ----------------------------------------------------------------------------
401 // Implementation of Parser
403 bool ParserTraits::IsEvalOrArguments(Handle<String> identifier) const {
404 Factory* factory = parser_->isolate()->factory();
405 return identifier.is_identical_to(factory->eval_string())
406 || identifier.is_identical_to(factory->arguments_string());
410 bool ParserTraits::IsThisProperty(Expression* expression) {
411 ASSERT(expression != NULL);
412 Property* property = expression->AsProperty();
413 return property != NULL &&
414 property->obj()->AsVariableProxy() != NULL &&
415 property->obj()->AsVariableProxy()->is_this();
419 bool ParserTraits::IsIdentifier(Expression* expression) {
420 VariableProxy* operand = expression->AsVariableProxy();
421 return operand != NULL && !operand->is_this();
425 void ParserTraits::PushPropertyName(FuncNameInferrer* fni,
426 Expression* expression) {
427 if (expression->IsPropertyName()) {
428 fni->PushLiteralName(expression->AsLiteral()->AsPropertyName());
430 fni->PushLiteralName(
431 parser_->isolate()->factory()->anonymous_function_string());
436 void ParserTraits::CheckAssigningFunctionLiteralToProperty(Expression* left,
438 ASSERT(left != NULL);
439 if (left->AsProperty() != NULL &&
440 right->AsFunctionLiteral() != NULL) {
441 right->AsFunctionLiteral()->set_pretenure();
446 void ParserTraits::CheckPossibleEvalCall(Expression* expression,
448 VariableProxy* callee = expression->AsVariableProxy();
449 if (callee != NULL &&
450 callee->IsVariable(parser_->isolate()->factory()->eval_string())) {
451 scope->DeclarationScope()->RecordEvalCall();
456 Expression* ParserTraits::MarkExpressionAsLValue(Expression* expression) {
457 VariableProxy* proxy = expression != NULL
458 ? expression->AsVariableProxy()
460 if (proxy != NULL) proxy->MarkAsLValue();
465 bool ParserTraits::ShortcutNumericLiteralBinaryExpression(
466 Expression** x, Expression* y, Token::Value op, int pos,
467 AstNodeFactory<AstConstructionVisitor>* factory) {
468 if ((*x)->AsLiteral() && (*x)->AsLiteral()->value()->IsNumber() &&
469 y->AsLiteral() && y->AsLiteral()->value()->IsNumber()) {
470 double x_val = (*x)->AsLiteral()->value()->Number();
471 double y_val = y->AsLiteral()->value()->Number();
474 *x = factory->NewNumberLiteral(x_val + y_val, pos);
477 *x = factory->NewNumberLiteral(x_val - y_val, pos);
480 *x = factory->NewNumberLiteral(x_val * y_val, pos);
483 *x = factory->NewNumberLiteral(x_val / y_val, pos);
485 case Token::BIT_OR: {
486 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
487 *x = factory->NewNumberLiteral(value, pos);
490 case Token::BIT_AND: {
491 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
492 *x = factory->NewNumberLiteral(value, pos);
495 case Token::BIT_XOR: {
496 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
497 *x = factory->NewNumberLiteral(value, pos);
501 int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
502 *x = factory->NewNumberLiteral(value, pos);
506 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
507 uint32_t value = DoubleToUint32(x_val) >> shift;
508 *x = factory->NewNumberLiteral(value, pos);
512 uint32_t shift = DoubleToInt32(y_val) & 0x1f;
513 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
514 *x = factory->NewNumberLiteral(value, pos);
525 Expression* ParserTraits::BuildUnaryExpression(
526 Expression* expression, Token::Value op, int pos,
527 AstNodeFactory<AstConstructionVisitor>* factory) {
528 ASSERT(expression != NULL);
529 if (expression->AsLiteral() != NULL) {
530 Handle<Object> literal = expression->AsLiteral()->value();
531 if (op == Token::NOT) {
532 // Convert the literal to a boolean condition and negate it.
533 bool condition = literal->BooleanValue();
534 Handle<Object> result =
535 parser_->isolate()->factory()->ToBoolean(!condition);
536 return factory->NewLiteral(result, pos);
537 } else if (literal->IsNumber()) {
538 // Compute some expressions involving only number literals.
539 double value = literal->Number();
544 return factory->NewNumberLiteral(-value, pos);
546 return factory->NewNumberLiteral(~DoubleToInt32(value), pos);
552 // Desugar '+foo' => 'foo*1'
553 if (op == Token::ADD) {
554 return factory->NewBinaryOperation(
555 Token::MUL, expression, factory->NewNumberLiteral(1, pos), pos);
557 // The same idea for '-foo' => 'foo*(-1)'.
558 if (op == Token::SUB) {
559 return factory->NewBinaryOperation(
560 Token::MUL, expression, factory->NewNumberLiteral(-1, pos), pos);
562 // ...and one more time for '~foo' => 'foo^(~0)'.
563 if (op == Token::BIT_NOT) {
564 return factory->NewBinaryOperation(
565 Token::BIT_XOR, expression, factory->NewNumberLiteral(~0, pos), pos);
567 return factory->NewUnaryOperation(op, expression, pos);
571 Expression* ParserTraits::NewThrowReferenceError(const char* message, int pos) {
572 return NewThrowError(
573 parser_->isolate()->factory()->MakeReferenceError_string(),
574 message, HandleVector<Object>(NULL, 0), pos);
578 Expression* ParserTraits::NewThrowSyntaxError(
579 const char* message, Handle<Object> arg, int pos) {
580 int argc = arg.is_null() ? 0 : 1;
581 Vector< Handle<Object> > arguments = HandleVector<Object>(&arg, argc);
582 return NewThrowError(
583 parser_->isolate()->factory()->MakeSyntaxError_string(),
584 message, arguments, pos);
588 Expression* ParserTraits::NewThrowTypeError(
589 const char* message, Handle<Object> arg, int pos) {
590 int argc = arg.is_null() ? 0 : 1;
591 Vector< Handle<Object> > arguments = HandleVector<Object>(&arg, argc);
592 return NewThrowError(
593 parser_->isolate()->factory()->MakeTypeError_string(),
594 message, arguments, pos);
598 Expression* ParserTraits::NewThrowError(
599 Handle<String> constructor, const char* message,
600 Vector<Handle<Object> > arguments, int pos) {
601 Zone* zone = parser_->zone();
602 Factory* factory = parser_->isolate()->factory();
603 int argc = arguments.length();
604 Handle<FixedArray> elements = factory->NewFixedArray(argc, TENURED);
605 for (int i = 0; i < argc; i++) {
606 Handle<Object> element = arguments[i];
607 if (!element.is_null()) {
608 elements->set(i, *element);
611 Handle<JSArray> array =
612 factory->NewJSArrayWithElements(elements, FAST_ELEMENTS, TENURED);
614 ZoneList<Expression*>* args = new(zone) ZoneList<Expression*>(2, zone);
615 Handle<String> type = factory->InternalizeUtf8String(message);
616 args->Add(parser_->factory()->NewLiteral(type, pos), zone);
617 args->Add(parser_->factory()->NewLiteral(array, pos), zone);
618 CallRuntime* call_constructor =
619 parser_->factory()->NewCallRuntime(constructor, NULL, args, pos);
620 return parser_->factory()->NewThrow(call_constructor, pos);
624 void ParserTraits::ReportMessageAt(Scanner::Location source_location,
626 Vector<const char*> args,
627 bool is_reference_error) {
628 if (parser_->stack_overflow()) {
629 // Suppress the error message (syntax error or such) in the presence of a
630 // stack overflow. The isolate allows only one pending exception at at time
631 // and we want to report the stack overflow later.
634 MessageLocation location(parser_->script_,
635 source_location.beg_pos,
636 source_location.end_pos);
637 Factory* factory = parser_->isolate()->factory();
638 Handle<FixedArray> elements = factory->NewFixedArray(args.length());
639 for (int i = 0; i < args.length(); i++) {
640 Handle<String> arg_string =
641 factory->NewStringFromUtf8(CStrVector(args[i])).ToHandleChecked();
642 elements->set(i, *arg_string);
644 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
645 Handle<Object> result = is_reference_error
646 ? factory->NewReferenceError(message, array)
647 : factory->NewSyntaxError(message, array);
648 parser_->isolate()->Throw(*result, &location);
652 void ParserTraits::ReportMessage(const char* message,
653 Vector<Handle<String> > args,
654 bool is_reference_error) {
655 Scanner::Location source_location = parser_->scanner()->location();
656 ReportMessageAt(source_location, message, args, is_reference_error);
660 void ParserTraits::ReportMessageAt(Scanner::Location source_location,
662 Vector<Handle<String> > args,
663 bool is_reference_error) {
664 if (parser_->stack_overflow()) {
665 // Suppress the error message (syntax error or such) in the presence of a
666 // stack overflow. The isolate allows only one pending exception at at time
667 // and we want to report the stack overflow later.
670 MessageLocation location(parser_->script_,
671 source_location.beg_pos,
672 source_location.end_pos);
673 Factory* factory = parser_->isolate()->factory();
674 Handle<FixedArray> elements = factory->NewFixedArray(args.length());
675 for (int i = 0; i < args.length(); i++) {
676 elements->set(i, *args[i]);
678 Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
679 Handle<Object> result = is_reference_error
680 ? factory->NewReferenceError(message, array)
681 : factory->NewSyntaxError(message, array);
682 parser_->isolate()->Throw(*result, &location);
686 Handle<String> ParserTraits::GetSymbol(Scanner* scanner) {
687 Handle<String> result =
688 parser_->scanner()->AllocateInternalizedString(parser_->isolate());
689 ASSERT(!result.is_null());
694 Handle<String> ParserTraits::NextLiteralString(Scanner* scanner,
695 PretenureFlag tenured) {
696 return scanner->AllocateNextLiteralString(parser_->isolate(), tenured);
700 Expression* ParserTraits::ThisExpression(
702 AstNodeFactory<AstConstructionVisitor>* factory) {
703 return factory->NewVariableProxy(scope->receiver());
707 Literal* ParserTraits::ExpressionFromLiteral(
708 Token::Value token, int pos,
710 AstNodeFactory<AstConstructionVisitor>* factory) {
711 Factory* isolate_factory = parser_->isolate()->factory();
713 case Token::NULL_LITERAL:
714 return factory->NewLiteral(isolate_factory->null_value(), pos);
715 case Token::TRUE_LITERAL:
716 return factory->NewLiteral(isolate_factory->true_value(), pos);
717 case Token::FALSE_LITERAL:
718 return factory->NewLiteral(isolate_factory->false_value(), pos);
719 case Token::NUMBER: {
720 double value = scanner->DoubleValue();
721 return factory->NewNumberLiteral(value, pos);
730 Expression* ParserTraits::ExpressionFromIdentifier(
731 Handle<String> name, int pos, Scope* scope,
732 AstNodeFactory<AstConstructionVisitor>* factory) {
733 if (parser_->fni_ != NULL) parser_->fni_->PushVariableName(name);
734 // The name may refer to a module instance object, so its type is unknown.
736 if (FLAG_print_interface_details)
737 PrintF("# Variable %s ", name->ToAsciiArray());
739 Interface* interface = Interface::NewUnknown(parser_->zone());
740 return scope->NewUnresolved(factory, name, interface, pos);
744 Expression* ParserTraits::ExpressionFromString(
745 int pos, Scanner* scanner,
746 AstNodeFactory<AstConstructionVisitor>* factory) {
747 Handle<String> symbol = GetSymbol(scanner);
748 if (parser_->fni_ != NULL) parser_->fni_->PushLiteralName(symbol);
749 return factory->NewLiteral(symbol, pos);
753 Literal* ParserTraits::GetLiteralTheHole(
754 int position, AstNodeFactory<AstConstructionVisitor>* factory) {
755 return factory->NewLiteral(parser_->isolate()->factory()->the_hole_value(),
756 RelocInfo::kNoPosition);
760 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) {
761 return parser_->ParseV8Intrinsic(ok);
765 FunctionLiteral* ParserTraits::ParseFunctionLiteral(
767 Scanner::Location function_name_location,
768 bool name_is_strict_reserved,
770 int function_token_position,
771 FunctionLiteral::FunctionType type,
773 return parser_->ParseFunctionLiteral(name, function_name_location,
774 name_is_strict_reserved, is_generator,
775 function_token_position, type, ok);
779 Parser::Parser(CompilationInfo* info)
780 : ParserBase<ParserTraits>(&scanner_,
781 info->isolate()->stack_guard()->real_climit(),
786 isolate_(info->isolate()),
787 script_(info->script()),
788 scanner_(isolate_->unicode_cache()),
789 reusable_preparser_(NULL),
790 original_scope_(NULL),
793 cached_data_mode_(NO_CACHED_DATA),
795 ASSERT(!script_.is_null());
796 isolate_->set_ast_node_id(0);
797 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
798 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
799 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
800 set_allow_lazy(false); // Must be explicitly enabled.
801 set_allow_generators(FLAG_harmony_generators);
802 set_allow_for_of(FLAG_harmony_iteration);
803 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
807 FunctionLiteral* Parser::ParseProgram() {
808 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
809 // see comment for HistogramTimerScope class.
810 HistogramTimerScope timer_scope(isolate()->counters()->parse(), true);
811 Handle<String> source(String::cast(script_->source()));
812 isolate()->counters()->total_parse_size()->Increment(source->length());
814 if (FLAG_trace_parse) {
817 fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
819 // Initialize parser state.
820 CompleteParserRecorder recorder;
821 if (cached_data_mode_ == PRODUCE_CACHED_DATA) {
823 } else if (cached_data_mode_ == CONSUME_CACHED_DATA) {
824 (*cached_data_)->Initialize();
827 source = String::Flatten(source);
828 FunctionLiteral* result;
829 if (source->IsExternalTwoByteString()) {
830 // Notice that the stream is destroyed at the end of the branch block.
831 // The last line of the blocks can't be moved outside, even though they're
833 ExternalTwoByteStringUtf16CharacterStream stream(
834 Handle<ExternalTwoByteString>::cast(source), 0, source->length());
835 scanner_.Initialize(&stream);
836 result = DoParseProgram(info(), source);
838 GenericStringUtf16CharacterStream stream(source, 0, source->length());
839 scanner_.Initialize(&stream);
840 result = DoParseProgram(info(), source);
843 if (FLAG_trace_parse && result != NULL) {
844 double ms = timer.Elapsed().InMillisecondsF();
845 if (info()->is_eval()) {
846 PrintF("[parsing eval");
847 } else if (info()->script()->name()->IsString()) {
848 String* name = String::cast(info()->script()->name());
849 SmartArrayPointer<char> name_chars = name->ToCString();
850 PrintF("[parsing script: %s", name_chars.get());
852 PrintF("[parsing script");
854 PrintF(" - took %0.3f ms]\n", ms);
856 if (cached_data_mode_ == PRODUCE_CACHED_DATA) {
857 Vector<unsigned> store = recorder.ExtractData();
858 *cached_data_ = new ScriptData(store);
865 FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info,
866 Handle<String> source) {
867 ASSERT(scope_ == NULL);
868 ASSERT(target_stack_ == NULL);
870 Handle<String> no_name = isolate()->factory()->empty_string();
872 FunctionLiteral* result = NULL;
873 { Scope* scope = NewScope(scope_, GLOBAL_SCOPE);
874 info->SetGlobalScope(scope);
875 if (!info->context().is_null()) {
876 scope = Scope::DeserializeScopeChain(*info->context(), scope, zone());
878 original_scope_ = scope;
879 if (info->is_eval()) {
880 if (!scope->is_global_scope() || info->strict_mode() == STRICT) {
881 scope = NewScope(scope, EVAL_SCOPE);
883 } else if (info->is_global()) {
884 scope = NewScope(scope, GLOBAL_SCOPE);
886 scope->set_start_position(0);
887 scope->set_end_position(source->length());
889 // Compute the parsing mode.
890 Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY;
891 if (allow_natives_syntax() ||
892 extension_ != NULL ||
893 scope->is_eval_scope()) {
894 mode = PARSE_EAGERLY;
896 ParsingModeScope parsing_mode(this, mode);
899 FunctionState function_state(&function_state_, &scope_, scope, zone());
901 scope_->SetStrictMode(info->strict_mode());
902 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
904 int beg_pos = scanner()->location().beg_pos;
905 ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok);
906 if (ok && strict_mode() == STRICT) {
907 CheckOctalLiteral(beg_pos, scanner()->location().end_pos, &ok);
910 if (ok && allow_harmony_scoping() && strict_mode() == STRICT) {
911 CheckConflictingVarDeclarations(scope_, &ok);
914 if (ok && info->parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
915 if (body->length() != 1 ||
916 !body->at(0)->IsExpressionStatement() ||
917 !body->at(0)->AsExpressionStatement()->
918 expression()->IsFunctionLiteral()) {
919 ReportMessage("single_function_literal", Vector<const char*>::empty());
925 result = factory()->NewFunctionLiteral(
929 function_state.materialized_literal_count(),
930 function_state.expected_property_count(),
931 function_state.handler_count(),
933 FunctionLiteral::kNoDuplicateParameters,
934 FunctionLiteral::ANONYMOUS_EXPRESSION,
935 FunctionLiteral::kGlobalOrEval,
936 FunctionLiteral::kNotParenthesized,
937 FunctionLiteral::kNotGenerator,
939 result->set_ast_properties(factory()->visitor()->ast_properties());
940 result->set_dont_optimize_reason(
941 factory()->visitor()->dont_optimize_reason());
942 } else if (stack_overflow()) {
943 isolate()->StackOverflow();
947 // Make sure the target stack is empty.
948 ASSERT(target_stack_ == NULL);
954 FunctionLiteral* Parser::ParseLazy() {
955 HistogramTimerScope timer_scope(isolate()->counters()->parse_lazy());
956 Handle<String> source(String::cast(script_->source()));
957 isolate()->counters()->total_parse_size()->Increment(source->length());
959 if (FLAG_trace_parse) {
962 Handle<SharedFunctionInfo> shared_info = info()->shared_info();
964 // Initialize parser state.
965 source = String::Flatten(source);
966 FunctionLiteral* result;
967 if (source->IsExternalTwoByteString()) {
968 ExternalTwoByteStringUtf16CharacterStream stream(
969 Handle<ExternalTwoByteString>::cast(source),
970 shared_info->start_position(),
971 shared_info->end_position());
972 result = ParseLazy(&stream);
974 GenericStringUtf16CharacterStream stream(source,
975 shared_info->start_position(),
976 shared_info->end_position());
977 result = ParseLazy(&stream);
980 if (FLAG_trace_parse && result != NULL) {
981 double ms = timer.Elapsed().InMillisecondsF();
982 SmartArrayPointer<char> name_chars = result->debug_name()->ToCString();
983 PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms);
989 FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source) {
990 Handle<SharedFunctionInfo> shared_info = info()->shared_info();
991 scanner_.Initialize(source);
992 ASSERT(scope_ == NULL);
993 ASSERT(target_stack_ == NULL);
995 Handle<String> name(String::cast(shared_info->name()));
996 fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
997 fni_->PushEnclosingName(name);
999 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
1001 // Place holder for the result.
1002 FunctionLiteral* result = NULL;
1005 // Parse the function literal.
1006 Scope* scope = NewScope(scope_, GLOBAL_SCOPE);
1007 info()->SetGlobalScope(scope);
1008 if (!info()->closure().is_null()) {
1009 scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope,
1012 original_scope_ = scope;
1013 FunctionState function_state(&function_state_, &scope_, scope, zone());
1014 ASSERT(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT);
1015 ASSERT(info()->strict_mode() == shared_info->strict_mode());
1016 scope->SetStrictMode(shared_info->strict_mode());
1017 FunctionLiteral::FunctionType function_type = shared_info->is_expression()
1018 ? (shared_info->is_anonymous()
1019 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1020 : FunctionLiteral::NAMED_EXPRESSION)
1021 : FunctionLiteral::DECLARATION;
1023 result = ParseFunctionLiteral(name,
1024 Scanner::Location::invalid(),
1025 false, // Strict mode name already checked.
1026 shared_info->is_generator(),
1027 RelocInfo::kNoPosition,
1030 // Make sure the results agree.
1031 ASSERT(ok == (result != NULL));
1034 // Make sure the target stack is empty.
1035 ASSERT(target_stack_ == NULL);
1037 if (result == NULL) {
1038 if (stack_overflow()) isolate()->StackOverflow();
1040 Handle<String> inferred_name(shared_info->inferred_name());
1041 result->set_inferred_name(inferred_name);
1047 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
1052 // SourceElements ::
1053 // (ModuleElement)* <end_token>
1055 // Allocate a target stack to use for this set of source
1056 // elements. This way, all scripts and functions get their own
1057 // target stack thus avoiding illegal breaks and continues across
1059 TargetScope scope(&this->target_stack_);
1061 ASSERT(processor != NULL);
1062 bool directive_prologue = true; // Parsing directive prologue.
1064 while (peek() != end_token) {
1065 if (directive_prologue && peek() != Token::STRING) {
1066 directive_prologue = false;
1069 Scanner::Location token_loc = scanner()->peek_location();
1071 if (is_global && !is_eval) {
1072 stat = ParseModuleElement(NULL, CHECK_OK);
1074 stat = ParseBlockElement(NULL, CHECK_OK);
1076 if (stat == NULL || stat->IsEmpty()) {
1077 directive_prologue = false; // End of directive prologue.
1081 if (directive_prologue) {
1082 // A shot at a directive.
1083 ExpressionStatement* e_stat;
1085 // Still processing directive prologue?
1086 if ((e_stat = stat->AsExpressionStatement()) != NULL &&
1087 (literal = e_stat->expression()->AsLiteral()) != NULL &&
1088 literal->value()->IsString()) {
1089 Handle<String> directive = Handle<String>::cast(literal->value());
1091 // Check "use strict" directive (ES5 14.1).
1092 if (strict_mode() == SLOPPY &&
1093 String::Equals(isolate()->factory()->use_strict_string(),
1095 token_loc.end_pos - token_loc.beg_pos ==
1096 isolate()->heap()->use_strict_string()->length() + 2) {
1097 // TODO(mstarzinger): Global strict eval calls, need their own scope
1098 // as specified in ES5 10.4.2(3). The correct fix would be to always
1099 // add this scope in DoParseProgram(), but that requires adaptations
1100 // all over the code base, so we go with a quick-fix for now.
1101 // In the same manner, we have to patch the parsing mode.
1102 if (is_eval && !scope_->is_eval_scope()) {
1103 ASSERT(scope_->is_global_scope());
1104 Scope* scope = NewScope(scope_, EVAL_SCOPE);
1105 scope->set_start_position(scope_->start_position());
1106 scope->set_end_position(scope_->end_position());
1108 mode_ = PARSE_EAGERLY;
1110 scope_->SetStrictMode(STRICT);
1111 // "use strict" is the only directive for now.
1112 directive_prologue = false;
1115 // End of the directive prologue.
1116 directive_prologue = false;
1120 processor->Add(stat, zone());
1127 Statement* Parser::ParseModuleElement(ZoneStringList* labels,
1129 // (Ecma 262 5th Edition, clause 14):
1132 // FunctionDeclaration
1134 // In harmony mode we allow additionally the following productions
1138 // ModuleDeclaration
1139 // ImportDeclaration
1140 // ExportDeclaration
1141 // GeneratorDeclaration
1144 case Token::FUNCTION:
1145 return ParseFunctionDeclaration(NULL, ok);
1148 return ParseVariableStatement(kModuleElement, NULL, ok);
1150 return ParseImportDeclaration(ok);
1152 return ParseExportDeclaration(ok);
1154 Statement* stmt = ParseStatement(labels, CHECK_OK);
1155 // Handle 'module' as a context-sensitive keyword.
1156 if (FLAG_harmony_modules &&
1157 peek() == Token::IDENTIFIER &&
1158 !scanner()->HasAnyLineTerminatorBeforeNext() &&
1160 ExpressionStatement* estmt = stmt->AsExpressionStatement();
1161 if (estmt != NULL &&
1162 estmt->expression()->AsVariableProxy() != NULL &&
1163 String::Equals(isolate()->factory()->module_string(),
1164 estmt->expression()->AsVariableProxy()->name()) &&
1165 !scanner()->literal_contains_escapes()) {
1166 return ParseModuleDeclaration(NULL, ok);
1175 Statement* Parser::ParseModuleDeclaration(ZoneStringList* names, bool* ok) {
1176 // ModuleDeclaration:
1177 // 'module' Identifier Module
1179 int pos = peek_position();
1180 Handle<String> name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1183 if (FLAG_print_interface_details)
1184 PrintF("# Module %s...\n", name->ToAsciiArray());
1187 Module* module = ParseModule(CHECK_OK);
1188 VariableProxy* proxy = NewUnresolved(name, MODULE, module->interface());
1189 Declaration* declaration =
1190 factory()->NewModuleDeclaration(proxy, module, scope_, pos);
1191 Declare(declaration, true, CHECK_OK);
1194 if (FLAG_print_interface_details)
1195 PrintF("# Module %s.\n", name->ToAsciiArray());
1197 if (FLAG_print_interfaces) {
1198 PrintF("module %s : ", name->ToAsciiArray());
1199 module->interface()->Print();
1203 if (names) names->Add(name, zone());
1204 if (module->body() == NULL)
1205 return factory()->NewEmptyStatement(pos);
1207 return factory()->NewModuleStatement(proxy, module->body(), pos);
1211 Module* Parser::ParseModule(bool* ok) {
1213 // '{' ModuleElement '}'
1214 // '=' ModulePath ';'
1219 return ParseModuleLiteral(ok);
1221 case Token::ASSIGN: {
1222 Expect(Token::ASSIGN, CHECK_OK);
1223 Module* result = ParseModulePath(CHECK_OK);
1224 ExpectSemicolon(CHECK_OK);
1229 ExpectContextualKeyword(CStrVector("at"), CHECK_OK);
1230 Module* result = ParseModuleUrl(CHECK_OK);
1231 ExpectSemicolon(CHECK_OK);
1238 Module* Parser::ParseModuleLiteral(bool* ok) {
1240 // '{' ModuleElement '}'
1242 int pos = peek_position();
1243 // Construct block expecting 16 statements.
1244 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition);
1246 if (FLAG_print_interface_details) PrintF("# Literal ");
1248 Scope* scope = NewScope(scope_, MODULE_SCOPE);
1250 Expect(Token::LBRACE, CHECK_OK);
1251 scope->set_start_position(scanner()->location().beg_pos);
1252 scope->SetStrictMode(STRICT);
1255 BlockState block_state(&scope_, scope);
1256 TargetCollector collector(zone());
1257 Target target(&this->target_stack_, &collector);
1258 Target target_body(&this->target_stack_, body);
1260 while (peek() != Token::RBRACE) {
1261 Statement* stat = ParseModuleElement(NULL, CHECK_OK);
1262 if (stat && !stat->IsEmpty()) {
1263 body->AddStatement(stat, zone());
1268 Expect(Token::RBRACE, CHECK_OK);
1269 scope->set_end_position(scanner()->location().end_pos);
1270 body->set_scope(scope);
1272 // Check that all exports are bound.
1273 Interface* interface = scope->interface();
1274 for (Interface::Iterator it = interface->iterator();
1275 !it.done(); it.Advance()) {
1276 if (scope->LocalLookup(it.name()) == NULL) {
1277 Handle<String> name(it.name());
1278 ParserTraits::ReportMessage("module_export_undefined",
1279 Vector<Handle<String> >(&name, 1));
1285 interface->MakeModule(ok);
1287 interface->Freeze(ok);
1289 return factory()->NewModuleLiteral(body, interface, pos);
1293 Module* Parser::ParseModulePath(bool* ok) {
1296 // ModulePath '.' Identifier
1298 int pos = peek_position();
1299 Module* result = ParseModuleVariable(CHECK_OK);
1300 while (Check(Token::PERIOD)) {
1301 Handle<String> name = ParseIdentifierName(CHECK_OK);
1303 if (FLAG_print_interface_details)
1304 PrintF("# Path .%s ", name->ToAsciiArray());
1306 Module* member = factory()->NewModulePath(result, name, pos);
1307 result->interface()->Add(name, member->interface(), zone(), ok);
1310 if (FLAG_print_interfaces) {
1311 PrintF("PATH TYPE ERROR at '%s'\n", name->ToAsciiArray());
1313 result->interface()->Print();
1315 member->interface()->Print();
1318 ParserTraits::ReportMessage("invalid_module_path",
1319 Vector<Handle<String> >(&name, 1));
1329 Module* Parser::ParseModuleVariable(bool* ok) {
1333 int pos = peek_position();
1334 Handle<String> name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1336 if (FLAG_print_interface_details)
1337 PrintF("# Module variable %s ", name->ToAsciiArray());
1339 VariableProxy* proxy = scope_->NewUnresolved(
1340 factory(), name, Interface::NewModule(zone()),
1341 scanner()->location().beg_pos);
1343 return factory()->NewModuleVariable(proxy, pos);
1347 Module* Parser::ParseModuleUrl(bool* ok) {
1351 int pos = peek_position();
1352 Expect(Token::STRING, CHECK_OK);
1353 Handle<String> symbol = GetSymbol();
1355 // TODO(ES6): Request JS resource from environment...
1358 if (FLAG_print_interface_details) PrintF("# Url ");
1361 // Create an empty literal as long as the feature isn't finished.
1363 Scope* scope = NewScope(scope_, MODULE_SCOPE);
1364 Block* body = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
1365 body->set_scope(scope);
1366 Interface* interface = scope->interface();
1367 Module* result = factory()->NewModuleLiteral(body, interface, pos);
1368 interface->Freeze(ok);
1370 interface->Unify(scope->interface(), zone(), ok);
1376 Module* Parser::ParseModuleSpecifier(bool* ok) {
1381 if (peek() == Token::STRING) {
1382 return ParseModuleUrl(ok);
1384 return ParseModulePath(ok);
1389 Block* Parser::ParseImportDeclaration(bool* ok) {
1390 // ImportDeclaration:
1391 // 'import' IdentifierName (',' IdentifierName)* 'from' ModuleSpecifier ';'
1393 // TODO(ES6): implement destructuring ImportSpecifiers
1395 int pos = peek_position();
1396 Expect(Token::IMPORT, CHECK_OK);
1397 ZoneStringList names(1, zone());
1399 Handle<String> name = ParseIdentifierName(CHECK_OK);
1400 names.Add(name, zone());
1401 while (peek() == Token::COMMA) {
1402 Consume(Token::COMMA);
1403 name = ParseIdentifierName(CHECK_OK);
1404 names.Add(name, zone());
1407 ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1408 Module* module = ParseModuleSpecifier(CHECK_OK);
1409 ExpectSemicolon(CHECK_OK);
1411 // Generate a separate declaration for each identifier.
1412 // TODO(ES6): once we implement destructuring, make that one declaration.
1413 Block* block = factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
1414 for (int i = 0; i < names.length(); ++i) {
1416 if (FLAG_print_interface_details)
1417 PrintF("# Import %s ", names[i]->ToAsciiArray());
1419 Interface* interface = Interface::NewUnknown(zone());
1420 module->interface()->Add(names[i], interface, zone(), ok);
1423 if (FLAG_print_interfaces) {
1424 PrintF("IMPORT TYPE ERROR at '%s'\n", names[i]->ToAsciiArray());
1426 module->interface()->Print();
1429 ParserTraits::ReportMessage("invalid_module_path",
1430 Vector<Handle<String> >(&name, 1));
1433 VariableProxy* proxy = NewUnresolved(names[i], LET, interface);
1434 Declaration* declaration =
1435 factory()->NewImportDeclaration(proxy, module, scope_, pos);
1436 Declare(declaration, true, CHECK_OK);
1443 Statement* Parser::ParseExportDeclaration(bool* ok) {
1444 // ExportDeclaration:
1445 // 'export' Identifier (',' Identifier)* ';'
1446 // 'export' VariableDeclaration
1447 // 'export' FunctionDeclaration
1448 // 'export' GeneratorDeclaration
1449 // 'export' ModuleDeclaration
1451 // TODO(ES6): implement structuring ExportSpecifiers
1453 Expect(Token::EXPORT, CHECK_OK);
1455 Statement* result = NULL;
1456 ZoneStringList names(1, zone());
1458 case Token::IDENTIFIER: {
1459 int pos = position();
1460 Handle<String> name =
1461 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1462 // Handle 'module' as a context-sensitive keyword.
1463 if (!name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("module"))) {
1464 names.Add(name, zone());
1465 while (peek() == Token::COMMA) {
1466 Consume(Token::COMMA);
1467 name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1468 names.Add(name, zone());
1470 ExpectSemicolon(CHECK_OK);
1471 result = factory()->NewEmptyStatement(pos);
1473 result = ParseModuleDeclaration(&names, CHECK_OK);
1478 case Token::FUNCTION:
1479 result = ParseFunctionDeclaration(&names, CHECK_OK);
1485 result = ParseVariableStatement(kModuleElement, &names, CHECK_OK);
1490 ReportUnexpectedToken(scanner()->current_token());
1494 // Extract declared names into export declarations and interface.
1495 Interface* interface = scope_->interface();
1496 for (int i = 0; i < names.length(); ++i) {
1498 if (FLAG_print_interface_details)
1499 PrintF("# Export %s ", names[i]->ToAsciiArray());
1501 Interface* inner = Interface::NewUnknown(zone());
1502 interface->Add(names[i], inner, zone(), CHECK_OK);
1505 VariableProxy* proxy = NewUnresolved(names[i], LET, inner);
1507 // TODO(rossberg): Rethink whether we actually need to store export
1508 // declarations (for compilation?).
1509 // ExportDeclaration* declaration =
1510 // factory()->NewExportDeclaration(proxy, scope_, position);
1511 // scope_->AddDeclaration(declaration);
1514 ASSERT(result != NULL);
1519 Statement* Parser::ParseBlockElement(ZoneStringList* labels,
1521 // (Ecma 262 5th Edition, clause 14):
1524 // FunctionDeclaration
1526 // In harmony mode we allow additionally the following productions
1527 // BlockElement (aka SourceElement):
1530 // GeneratorDeclaration
1533 case Token::FUNCTION:
1534 return ParseFunctionDeclaration(NULL, ok);
1537 return ParseVariableStatement(kModuleElement, NULL, ok);
1539 return ParseStatement(labels, ok);
1544 Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
1547 // VariableStatement
1549 // ExpressionStatement
1551 // IterationStatement
1552 // ContinueStatement
1556 // LabelledStatement
1560 // DebuggerStatement
1562 // Note: Since labels can only be used by 'break' and 'continue'
1563 // statements, which themselves are only valid within blocks,
1564 // iterations or 'switch' statements (i.e., BreakableStatements),
1565 // labels can be simply ignored in all other cases; except for
1566 // trivial labeled break statements 'label: break label' which is
1567 // parsed into an empty statement.
1570 return ParseBlock(labels, ok);
1572 case Token::CONST: // fall through
1575 return ParseVariableStatement(kStatement, NULL, ok);
1577 case Token::SEMICOLON:
1579 return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1582 return ParseIfStatement(labels, ok);
1585 return ParseDoWhileStatement(labels, ok);
1588 return ParseWhileStatement(labels, ok);
1591 return ParseForStatement(labels, ok);
1593 case Token::CONTINUE:
1594 return ParseContinueStatement(ok);
1597 return ParseBreakStatement(labels, ok);
1600 return ParseReturnStatement(ok);
1603 return ParseWithStatement(labels, ok);
1606 return ParseSwitchStatement(labels, ok);
1609 return ParseThrowStatement(ok);
1612 // NOTE: It is somewhat complicated to have labels on
1613 // try-statements. When breaking out of a try-finally statement,
1614 // one must take great care not to treat it as a
1615 // fall-through. It is much easier just to wrap the entire
1616 // try-statement in a statement block and put the labels there
1618 factory()->NewBlock(labels, 1, false, RelocInfo::kNoPosition);
1619 Target target(&this->target_stack_, result);
1620 TryStatement* statement = ParseTryStatement(CHECK_OK);
1621 if (result) result->AddStatement(statement, zone());
1625 case Token::FUNCTION: {
1626 // FunctionDeclaration is only allowed in the context of SourceElements
1627 // (Ecma 262 5th Edition, clause 14):
1630 // FunctionDeclaration
1631 // Common language extension is to allow function declaration in place
1632 // of any statement. This language extension is disabled in strict mode.
1634 // In Harmony mode, this case also handles the extension:
1636 // GeneratorDeclaration
1637 if (strict_mode() == STRICT) {
1638 ReportMessageAt(scanner()->peek_location(), "strict_function");
1642 return ParseFunctionDeclaration(NULL, ok);
1645 case Token::DEBUGGER:
1646 return ParseDebuggerStatement(ok);
1649 return ParseExpressionOrLabelledStatement(labels, ok);
1654 VariableProxy* Parser::NewUnresolved(
1655 Handle<String> name, VariableMode mode, Interface* interface) {
1656 // If we are inside a function, a declaration of a var/const variable is a
1657 // truly local variable, and the scope of the variable is always the function
1659 // Let/const variables in harmony mode are always added to the immediately
1661 return DeclarationScope(mode)->NewUnresolved(
1662 factory(), name, interface, position());
1666 void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) {
1667 VariableProxy* proxy = declaration->proxy();
1668 Handle<String> name = proxy->name();
1669 VariableMode mode = declaration->mode();
1670 Scope* declaration_scope = DeclarationScope(mode);
1671 Variable* var = NULL;
1673 // If a suitable scope exists, then we can statically declare this
1674 // variable and also set its mode. In any case, a Declaration node
1675 // will be added to the scope so that the declaration can be added
1676 // to the corresponding activation frame at runtime if necessary.
1677 // For instance declarations inside an eval scope need to be added
1678 // to the calling function context.
1679 // Similarly, strict mode eval scope does not leak variable declarations to
1680 // the caller's scope so we declare all locals, too.
1681 if (declaration_scope->is_function_scope() ||
1682 declaration_scope->is_strict_eval_scope() ||
1683 declaration_scope->is_block_scope() ||
1684 declaration_scope->is_module_scope() ||
1685 declaration_scope->is_global_scope()) {
1686 // Declare the variable in the declaration scope.
1687 // For the global scope, we have to check for collisions with earlier
1688 // (i.e., enclosing) global scopes, to maintain the illusion of a single
1690 var = declaration_scope->is_global_scope()
1691 ? declaration_scope->Lookup(name)
1692 : declaration_scope->LocalLookup(name);
1694 // Declare the name.
1695 var = declaration_scope->DeclareLocal(
1696 name, mode, declaration->initialization(), proxy->interface());
1697 } else if ((mode != VAR || var->mode() != VAR) &&
1698 (!declaration_scope->is_global_scope() ||
1699 IsLexicalVariableMode(mode) ||
1700 IsLexicalVariableMode(var->mode()))) {
1701 // The name was declared in this scope before; check for conflicting
1702 // re-declarations. We have a conflict if either of the declarations is
1703 // not a var (in the global scope, we also have to ignore legacy const for
1704 // compatibility). There is similar code in runtime.cc in the Declare
1705 // functions. The function CheckNonConflictingScope checks for conflicting
1706 // var and let bindings from different scopes whereas this is a check for
1707 // conflicting declarations within the same scope. This check also covers
1710 // function () { let x; { var x; } }
1712 // because the var declaration is hoisted to the function scope where 'x'
1713 // is already bound.
1714 ASSERT(IsDeclaredVariableMode(var->mode()));
1715 if (allow_harmony_scoping() && strict_mode() == STRICT) {
1716 // In harmony we treat re-declarations as early errors. See
1717 // ES5 16 for a definition of early errors.
1718 SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
1719 const char* elms[1] = { c_string.get() };
1720 Vector<const char*> args(elms, 1);
1721 ReportMessage("var_redeclaration", args);
1725 Expression* expression = NewThrowTypeError(
1726 "var_redeclaration", name, declaration->position());
1727 declaration_scope->SetIllegalRedeclaration(expression);
1731 // We add a declaration node for every declaration. The compiler
1732 // will only generate code if necessary. In particular, declarations
1733 // for inner local variables that do not represent functions won't
1734 // result in any generated code.
1736 // Note that we always add an unresolved proxy even if it's not
1737 // used, simply because we don't know in this method (w/o extra
1738 // parameters) if the proxy is needed or not. The proxy will be
1739 // bound during variable resolution time unless it was pre-bound
1742 // WARNING: This will lead to multiple declaration nodes for the
1743 // same variable if it is declared several times. This is not a
1744 // semantic issue as long as we keep the source order, but it may be
1745 // a performance issue since it may lead to repeated
1746 // RuntimeHidden_DeclareContextSlot calls.
1747 declaration_scope->AddDeclaration(declaration);
1749 if (mode == CONST_LEGACY && declaration_scope->is_global_scope()) {
1750 // For global const variables we bind the proxy to a variable.
1751 ASSERT(resolve); // should be set by all callers
1752 Variable::Kind kind = Variable::NORMAL;
1753 var = new(zone()) Variable(
1754 declaration_scope, name, mode, true, kind,
1755 kNeedsInitialization, proxy->interface());
1756 } else if (declaration_scope->is_eval_scope() &&
1757 declaration_scope->strict_mode() == SLOPPY) {
1758 // For variable declarations in a sloppy eval scope the proxy is bound
1759 // to a lookup variable to force a dynamic declaration using the
1760 // DeclareContextSlot runtime function.
1761 Variable::Kind kind = Variable::NORMAL;
1762 var = new(zone()) Variable(
1763 declaration_scope, name, mode, true, kind,
1764 declaration->initialization(), proxy->interface());
1765 var->AllocateTo(Variable::LOOKUP, -1);
1769 // If requested and we have a local variable, bind the proxy to the variable
1770 // at parse-time. This is used for functions (and consts) declared inside
1771 // statements: the corresponding function (or const) variable must be in the
1772 // function scope and not a statement-local scope, e.g. as provided with a
1773 // 'with' statement:
1779 // which is translated into:
1782 // // in this case this is not: 'var f; f = function () {};'
1783 // var f = function () {};
1786 // Note that if 'f' is accessed from inside the 'with' statement, it
1787 // will be allocated in the context (because we must be able to look
1788 // it up dynamically) but it will also be accessed statically, i.e.,
1789 // with a context slot index and a context chain length for this
1790 // initialization code. Thus, inside the 'with' statement, we need
1791 // both access to the static and the dynamic context chain; the
1792 // runtime needs to provide both.
1793 if (resolve && var != NULL) {
1796 if (FLAG_harmony_modules) {
1799 if (FLAG_print_interface_details)
1800 PrintF("# Declare %s\n", var->name()->ToAsciiArray());
1802 proxy->interface()->Unify(var->interface(), zone(), &ok);
1805 if (FLAG_print_interfaces) {
1806 PrintF("DECLARE TYPE ERROR\n");
1808 proxy->interface()->Print();
1810 var->interface()->Print();
1813 ParserTraits::ReportMessage("module_type_error",
1814 Vector<Handle<String> >(&name, 1));
1821 // Language extension which is only enabled for source files loaded
1822 // through the API's extension mechanism. A native function
1823 // declaration is resolved by looking up the function through a
1824 // callback provided by the extension.
1825 Statement* Parser::ParseNativeDeclaration(bool* ok) {
1826 int pos = peek_position();
1827 Expect(Token::FUNCTION, CHECK_OK);
1828 // Allow "eval" or "arguments" for backward compatibility.
1829 Handle<String> name = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
1830 Expect(Token::LPAREN, CHECK_OK);
1831 bool done = (peek() == Token::RPAREN);
1833 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
1834 done = (peek() == Token::RPAREN);
1836 Expect(Token::COMMA, CHECK_OK);
1839 Expect(Token::RPAREN, CHECK_OK);
1840 Expect(Token::SEMICOLON, CHECK_OK);
1842 // Make sure that the function containing the native declaration
1843 // isn't lazily compiled. The extension structures are only
1844 // accessible while parsing the first time not when reparsing
1845 // because of lazy compilation.
1846 DeclarationScope(VAR)->ForceEagerCompilation();
1848 // TODO(1240846): It's weird that native function declarations are
1849 // introduced dynamically when we meet their declarations, whereas
1850 // other functions are set up when entering the surrounding scope.
1851 VariableProxy* proxy = NewUnresolved(name, VAR, Interface::NewValue());
1852 Declaration* declaration =
1853 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos);
1854 Declare(declaration, true, CHECK_OK);
1855 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
1856 name, extension_, RelocInfo::kNoPosition);
1857 return factory()->NewExpressionStatement(
1858 factory()->NewAssignment(
1859 Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition),
1864 Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) {
1865 // FunctionDeclaration ::
1866 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1867 // GeneratorDeclaration ::
1868 // 'function' '*' Identifier '(' FormalParameterListopt ')'
1869 // '{' FunctionBody '}'
1870 Expect(Token::FUNCTION, CHECK_OK);
1871 int pos = position();
1872 bool is_generator = allow_generators() && Check(Token::MUL);
1873 bool is_strict_reserved = false;
1874 Handle<String> name = ParseIdentifierOrStrictReservedWord(
1875 &is_strict_reserved, CHECK_OK);
1876 FunctionLiteral* fun = ParseFunctionLiteral(name,
1877 scanner()->location(),
1881 FunctionLiteral::DECLARATION,
1883 // Even if we're not at the top-level of the global or a function
1884 // scope, we treat it as such and introduce the function with its
1885 // initial value upon entering the corresponding scope.
1886 // In extended mode, a function behaves as a lexical binding, except in the
1889 allow_harmony_scoping() &&
1890 strict_mode() == STRICT && !scope_->is_global_scope() ? LET : VAR;
1891 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue());
1892 Declaration* declaration =
1893 factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos);
1894 Declare(declaration, true, CHECK_OK);
1895 if (names) names->Add(name, zone());
1896 return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1900 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) {
1901 if (allow_harmony_scoping() && strict_mode() == STRICT) {
1902 return ParseScopedBlock(labels, ok);
1906 // '{' Statement* '}'
1908 // Note that a Block does not introduce a new execution scope!
1909 // (ECMA-262, 3rd, 12.2)
1911 // Construct block expecting 16 statements.
1913 factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1914 Target target(&this->target_stack_, result);
1915 Expect(Token::LBRACE, CHECK_OK);
1916 while (peek() != Token::RBRACE) {
1917 Statement* stat = ParseStatement(NULL, CHECK_OK);
1918 if (stat && !stat->IsEmpty()) {
1919 result->AddStatement(stat, zone());
1922 Expect(Token::RBRACE, CHECK_OK);
1927 Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
1928 // The harmony mode uses block elements instead of statements.
1931 // '{' BlockElement* '}'
1933 // Construct block expecting 16 statements.
1935 factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1936 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
1938 // Parse the statements and collect escaping labels.
1939 Expect(Token::LBRACE, CHECK_OK);
1940 block_scope->set_start_position(scanner()->location().beg_pos);
1941 { BlockState block_state(&scope_, block_scope);
1942 TargetCollector collector(zone());
1943 Target target(&this->target_stack_, &collector);
1944 Target target_body(&this->target_stack_, body);
1946 while (peek() != Token::RBRACE) {
1947 Statement* stat = ParseBlockElement(NULL, CHECK_OK);
1948 if (stat && !stat->IsEmpty()) {
1949 body->AddStatement(stat, zone());
1953 Expect(Token::RBRACE, CHECK_OK);
1954 block_scope->set_end_position(scanner()->location().end_pos);
1955 block_scope = block_scope->FinalizeBlockScope();
1956 body->set_scope(block_scope);
1961 Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context,
1962 ZoneStringList* names,
1964 // VariableStatement ::
1965 // VariableDeclarations ';'
1967 Handle<String> ignore;
1969 ParseVariableDeclarations(var_context, NULL, names, &ignore, CHECK_OK);
1970 ExpectSemicolon(CHECK_OK);
1975 // If the variable declaration declares exactly one non-const
1976 // variable, then *out is set to that variable. In all other cases,
1977 // *out is untouched; in particular, it is the caller's responsibility
1978 // to initialize it properly. This mechanism is used for the parsing
1979 // of 'for-in' loops.
1980 Block* Parser::ParseVariableDeclarations(
1981 VariableDeclarationContext var_context,
1982 VariableDeclarationProperties* decl_props,
1983 ZoneStringList* names,
1984 Handle<String>* out,
1986 // VariableDeclarations ::
1987 // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[',']
1989 // The ES6 Draft Rev3 specifies the following grammar for const declarations
1991 // ConstDeclaration ::
1992 // const ConstBinding (',' ConstBinding)* ';'
1994 // Identifier '=' AssignmentExpression
1998 // BindingPattern '=' AssignmentExpression
2000 int pos = peek_position();
2001 VariableMode mode = VAR;
2002 // True if the binding needs initialization. 'let' and 'const' declared
2003 // bindings are created uninitialized by their declaration nodes and
2004 // need initialization. 'var' declared bindings are always initialized
2005 // immediately by their declaration nodes.
2006 bool needs_init = false;
2007 bool is_const = false;
2008 Token::Value init_op = Token::INIT_VAR;
2009 if (peek() == Token::VAR) {
2010 Consume(Token::VAR);
2011 } else if (peek() == Token::CONST) {
2012 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
2014 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
2016 // * It is a Syntax Error if the code that matches this production is not
2017 // contained in extended code.
2019 // However disallowing const in sloppy mode will break compatibility with
2020 // existing pages. Therefore we keep allowing const with the old
2021 // non-harmony semantics in sloppy mode.
2022 Consume(Token::CONST);
2023 switch (strict_mode()) {
2025 mode = CONST_LEGACY;
2026 init_op = Token::INIT_CONST_LEGACY;
2029 if (allow_harmony_scoping()) {
2030 if (var_context == kStatement) {
2031 // In strict mode 'const' declarations are only allowed in source
2032 // element positions.
2033 ReportMessage("unprotected_const", Vector<const char*>::empty());
2038 init_op = Token::INIT_CONST;
2040 ReportMessage("strict_const", Vector<const char*>::empty());
2047 } else if (peek() == Token::LET) {
2048 // ES6 Draft Rev4 section 12.2.1:
2050 // LetDeclaration : let LetBindingList ;
2052 // * It is a Syntax Error if the code that matches this production is not
2053 // contained in extended code.
2055 // TODO(rossberg): make 'let' a legal identifier in sloppy mode.
2056 if (!allow_harmony_scoping() || strict_mode() == SLOPPY) {
2057 ReportMessage("illegal_let", Vector<const char*>::empty());
2061 Consume(Token::LET);
2062 if (var_context == kStatement) {
2063 // Let declarations are only allowed in source element positions.
2064 ReportMessage("unprotected_let", Vector<const char*>::empty());
2070 init_op = Token::INIT_LET;
2072 UNREACHABLE(); // by current callers
2075 Scope* declaration_scope = DeclarationScope(mode);
2077 // The scope of a var/const declared variable anywhere inside a function
2078 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
2079 // transform a source-level var/const declaration into a (Function)
2080 // Scope declaration, and rewrite the source-level initialization into an
2081 // assignment statement. We use a block to collect multiple assignments.
2083 // We mark the block as initializer block because we don't want the
2084 // rewriter to add a '.result' assignment to such a block (to get compliant
2085 // behavior for code such as print(eval('var x = 7')), and for cosmetic
2086 // reasons when pretty-printing. Also, unless an assignment (initialization)
2087 // is inside an initializer block, it is ignored.
2089 // Create new block with one expected declaration.
2090 Block* block = factory()->NewBlock(NULL, 1, true, pos);
2091 int nvars = 0; // the number of variables declared
2092 Handle<String> name;
2094 if (fni_ != NULL) fni_->Enter();
2096 // Parse variable name.
2097 if (nvars > 0) Consume(Token::COMMA);
2098 name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
2099 if (fni_ != NULL) fni_->PushVariableName(name);
2101 // Declare variable.
2102 // Note that we *always* must treat the initial value via a separate init
2103 // assignment for variables and constants because the value must be assigned
2104 // when the variable is encountered in the source. But the variable/constant
2105 // is declared (and set to 'undefined') upon entering the function within
2106 // which the variable or constant is declared. Only function variables have
2107 // an initial value in the declaration (because they are initialized upon
2108 // entering the function).
2110 // If we have a const declaration, in an inner scope, the proxy is always
2111 // bound to the declared variable (independent of possibly surrounding with
2113 // For let/const declarations in harmony mode, we can also immediately
2114 // pre-resolve the proxy because it resides in the same scope as the
2116 Interface* interface =
2117 is_const ? Interface::NewConst() : Interface::NewValue();
2118 VariableProxy* proxy = NewUnresolved(name, mode, interface);
2119 Declaration* declaration =
2120 factory()->NewVariableDeclaration(proxy, mode, scope_, pos);
2121 Declare(declaration, mode != VAR, CHECK_OK);
2123 if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) {
2124 ReportMessageAt(scanner()->location(), "too_many_variables");
2128 if (names) names->Add(name, zone());
2130 // Parse initialization expression if present and/or needed. A
2131 // declaration of the form:
2135 // is syntactic sugar for:
2139 // In particular, we need to re-lookup 'v' (in scope_, not
2140 // declaration_scope) as it may be a different 'v' than the 'v' in the
2141 // declaration (e.g., if we are inside a 'with' statement or 'catch'
2144 // However, note that const declarations are different! A const
2145 // declaration of the form:
2149 // is *not* syntactic sugar for:
2153 // The "variable" c initialized to x is the same as the declared
2154 // one - there is no re-lookup (see the last parameter of the
2155 // Declare() call above).
2157 Scope* initialization_scope = is_const ? declaration_scope : scope_;
2158 Expression* value = NULL;
2160 // Harmony consts have non-optional initializers.
2161 if (peek() == Token::ASSIGN || mode == CONST) {
2162 Expect(Token::ASSIGN, CHECK_OK);
2164 value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
2165 // Don't infer if it is "a = function(){...}();"-like expression.
2167 value->AsCall() == NULL &&
2168 value->AsCallNew() == NULL) {
2171 fni_->RemoveLastFunction();
2173 if (decl_props != NULL) *decl_props = kHasInitializers;
2176 // Record the end position of the initializer.
2177 if (proxy->var() != NULL) {
2178 proxy->var()->set_initializer_position(position());
2181 // Make sure that 'const x' and 'let x' initialize 'x' to undefined.
2182 if (value == NULL && needs_init) {
2183 value = GetLiteralUndefined(position());
2186 // Global variable declarations must be compiled in a specific
2187 // way. When the script containing the global variable declaration
2188 // is entered, the global variable must be declared, so that if it
2189 // doesn't exist (on the global object itself, see ES5 errata) it
2190 // gets created with an initial undefined value. This is handled
2191 // by the declarations part of the function representing the
2192 // top-level global code; see Runtime::DeclareGlobalVariable. If
2193 // it already exists (in the object or in a prototype), it is
2194 // *not* touched until the variable declaration statement is
2197 // Executing the variable declaration statement will always
2198 // guarantee to give the global object a "local" variable; a
2199 // variable defined in the global object and not in any
2200 // prototype. This way, global variable declarations can shadow
2201 // properties in the prototype chain, but only after the variable
2202 // declaration statement has been executed. This is important in
2203 // browsers where the global object (window) has lots of
2204 // properties defined in prototype objects.
2205 if (initialization_scope->is_global_scope() &&
2206 !IsLexicalVariableMode(mode)) {
2207 // Compute the arguments for the runtime call.
2208 ZoneList<Expression*>* arguments =
2209 new(zone()) ZoneList<Expression*>(3, zone());
2210 // We have at least 1 parameter.
2211 arguments->Add(factory()->NewLiteral(name, pos), zone());
2212 CallRuntime* initialize;
2215 arguments->Add(value, zone());
2216 value = NULL; // zap the value to avoid the unnecessary assignment
2218 // Construct the call to Runtime_InitializeConstGlobal
2219 // and add it to the initialization statement block.
2220 // Note that the function does different things depending on
2221 // the number of arguments (1 or 2).
2222 initialize = factory()->NewCallRuntime(
2223 isolate()->factory()->InitializeConstGlobal_string(),
2224 Runtime::FunctionForId(Runtime::kHiddenInitializeConstGlobal),
2228 // We may want to pass singleton to avoid Literal allocations.
2229 StrictMode strict_mode = initialization_scope->strict_mode();
2230 arguments->Add(factory()->NewNumberLiteral(strict_mode, pos), zone());
2232 // Be careful not to assign a value to the global variable if
2233 // we're in a with. The initialization value should not
2234 // necessarily be stored in the global object in that case,
2235 // which is why we need to generate a separate assignment node.
2236 if (value != NULL && !inside_with()) {
2237 arguments->Add(value, zone());
2238 value = NULL; // zap the value to avoid the unnecessary assignment
2241 // Construct the call to Runtime_InitializeVarGlobal
2242 // and add it to the initialization statement block.
2243 // Note that the function does different things depending on
2244 // the number of arguments (2 or 3).
2245 initialize = factory()->NewCallRuntime(
2246 isolate()->factory()->InitializeVarGlobal_string(),
2247 Runtime::FunctionForId(Runtime::kInitializeVarGlobal),
2251 block->AddStatement(
2252 factory()->NewExpressionStatement(initialize, RelocInfo::kNoPosition),
2254 } else if (needs_init) {
2255 // Constant initializations always assign to the declared constant which
2256 // is always at the function scope level. This is only relevant for
2257 // dynamically looked-up variables and constants (the start context for
2258 // constant lookups is always the function context, while it is the top
2259 // context for var declared variables). Sigh...
2260 // For 'let' and 'const' declared variables in harmony mode the
2261 // initialization also always assigns to the declared variable.
2262 ASSERT(proxy != NULL);
2263 ASSERT(proxy->var() != NULL);
2264 ASSERT(value != NULL);
2265 Assignment* assignment =
2266 factory()->NewAssignment(init_op, proxy, value, pos);
2267 block->AddStatement(
2268 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2273 // Add an assignment node to the initialization statement block if we still
2274 // have a pending initialization value.
2275 if (value != NULL) {
2276 ASSERT(mode == VAR);
2277 // 'var' initializations are simply assignments (with all the consequences
2278 // if they are inside a 'with' statement - they may change a 'with' object
2280 VariableProxy* proxy =
2281 initialization_scope->NewUnresolved(factory(), name, interface);
2282 Assignment* assignment =
2283 factory()->NewAssignment(init_op, proxy, value, pos);
2284 block->AddStatement(
2285 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2289 if (fni_ != NULL) fni_->Leave();
2290 } while (peek() == Token::COMMA);
2292 // If there was a single non-const declaration, return it in the output
2293 // parameter for possible use by for/in.
2294 if (nvars == 1 && !is_const) {
2302 static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) {
2303 ASSERT(!label.is_null());
2305 for (int i = labels->length(); i-- > 0; )
2306 if (labels->at(i).is_identical_to(label))
2313 Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
2315 // ExpressionStatement | LabelledStatement ::
2317 // Identifier ':' Statement
2318 int pos = peek_position();
2319 bool starts_with_idenfifier = peek_any_identifier();
2320 Expression* expr = ParseExpression(true, CHECK_OK);
2321 if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL &&
2322 expr->AsVariableProxy() != NULL &&
2323 !expr->AsVariableProxy()->is_this()) {
2324 // Expression is a single identifier, and not, e.g., a parenthesized
2326 VariableProxy* var = expr->AsVariableProxy();
2327 Handle<String> label = var->name();
2328 // TODO(1240780): We don't check for redeclaration of labels
2329 // during preparsing since keeping track of the set of active
2330 // labels requires nontrivial changes to the way scopes are
2331 // structured. However, these are probably changes we want to
2332 // make later anyway so we should go back and fix this then.
2333 if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
2334 SmartArrayPointer<char> c_string = label->ToCString(DISALLOW_NULLS);
2335 const char* elms[1] = { c_string.get() };
2336 Vector<const char*> args(elms, 1);
2337 ReportMessage("label_redeclaration", args);
2341 if (labels == NULL) {
2342 labels = new(zone()) ZoneStringList(4, zone());
2344 labels->Add(label, zone());
2345 // Remove the "ghost" variable that turned out to be a label
2346 // from the top scope. This way, we don't try to resolve it
2347 // during the scope processing.
2348 scope_->RemoveUnresolved(var);
2349 Expect(Token::COLON, CHECK_OK);
2350 return ParseStatement(labels, ok);
2353 // If we have an extension, we allow a native function declaration.
2354 // A native function declaration starts with "native function" with
2355 // no line-terminator between the two words.
2356 if (extension_ != NULL &&
2357 peek() == Token::FUNCTION &&
2358 !scanner()->HasAnyLineTerminatorBeforeNext() &&
2360 expr->AsVariableProxy() != NULL &&
2361 String::Equals(isolate()->factory()->native_string(),
2362 expr->AsVariableProxy()->name()) &&
2363 !scanner()->literal_contains_escapes()) {
2364 return ParseNativeDeclaration(ok);
2367 // Parsed expression statement, or the context-sensitive 'module' keyword.
2368 // Only expect semicolon in the former case.
2369 if (!FLAG_harmony_modules ||
2370 peek() != Token::IDENTIFIER ||
2371 scanner()->HasAnyLineTerminatorBeforeNext() ||
2372 expr->AsVariableProxy() == NULL ||
2373 !String::Equals(isolate()->factory()->module_string(),
2374 expr->AsVariableProxy()->name()) ||
2375 scanner()->literal_contains_escapes()) {
2376 ExpectSemicolon(CHECK_OK);
2378 return factory()->NewExpressionStatement(expr, pos);
2382 IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) {
2384 // 'if' '(' Expression ')' Statement ('else' Statement)?
2386 int pos = peek_position();
2387 Expect(Token::IF, CHECK_OK);
2388 Expect(Token::LPAREN, CHECK_OK);
2389 Expression* condition = ParseExpression(true, CHECK_OK);
2390 Expect(Token::RPAREN, CHECK_OK);
2391 Statement* then_statement = ParseStatement(labels, CHECK_OK);
2392 Statement* else_statement = NULL;
2393 if (peek() == Token::ELSE) {
2395 else_statement = ParseStatement(labels, CHECK_OK);
2397 else_statement = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
2399 return factory()->NewIfStatement(
2400 condition, then_statement, else_statement, pos);
2404 Statement* Parser::ParseContinueStatement(bool* ok) {
2405 // ContinueStatement ::
2406 // 'continue' Identifier? ';'
2408 int pos = peek_position();
2409 Expect(Token::CONTINUE, CHECK_OK);
2410 Handle<String> label = Handle<String>::null();
2411 Token::Value tok = peek();
2412 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
2413 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2414 // ECMA allows "eval" or "arguments" as labels even in strict mode.
2415 label = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
2417 IterationStatement* target = NULL;
2418 target = LookupContinueTarget(label, CHECK_OK);
2419 if (target == NULL) {
2420 // Illegal continue statement.
2421 const char* message = "illegal_continue";
2422 Vector<Handle<String> > args;
2423 if (!label.is_null()) {
2424 message = "unknown_label";
2425 args = Vector<Handle<String> >(&label, 1);
2427 ParserTraits::ReportMessageAt(scanner()->location(), message, args);
2431 ExpectSemicolon(CHECK_OK);
2432 return factory()->NewContinueStatement(target, pos);
2436 Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) {
2437 // BreakStatement ::
2438 // 'break' Identifier? ';'
2440 int pos = peek_position();
2441 Expect(Token::BREAK, CHECK_OK);
2442 Handle<String> label;
2443 Token::Value tok = peek();
2444 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
2445 tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2446 // ECMA allows "eval" or "arguments" as labels even in strict mode.
2447 label = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
2449 // Parse labeled break statements that target themselves into
2450 // empty statements, e.g. 'l1: l2: l3: break l2;'
2451 if (!label.is_null() && ContainsLabel(labels, label)) {
2452 ExpectSemicolon(CHECK_OK);
2453 return factory()->NewEmptyStatement(pos);
2455 BreakableStatement* target = NULL;
2456 target = LookupBreakTarget(label, CHECK_OK);
2457 if (target == NULL) {
2458 // Illegal break statement.
2459 const char* message = "illegal_break";
2460 Vector<Handle<String> > args;
2461 if (!label.is_null()) {
2462 message = "unknown_label";
2463 args = Vector<Handle<String> >(&label, 1);
2465 ParserTraits::ReportMessageAt(scanner()->location(), message, args);
2469 ExpectSemicolon(CHECK_OK);
2470 return factory()->NewBreakStatement(target, pos);
2474 Statement* Parser::ParseReturnStatement(bool* ok) {
2475 // ReturnStatement ::
2476 // 'return' Expression? ';'
2478 // Consume the return token. It is necessary to do that before
2479 // reporting any errors on it, because of the way errors are
2480 // reported (underlining).
2481 Expect(Token::RETURN, CHECK_OK);
2482 Scanner::Location loc = scanner()->location();
2484 Token::Value tok = peek();
2486 Expression* return_value;
2487 if (scanner()->HasAnyLineTerminatorBeforeNext() ||
2488 tok == Token::SEMICOLON ||
2489 tok == Token::RBRACE ||
2490 tok == Token::EOS) {
2491 return_value = GetLiteralUndefined(position());
2493 return_value = ParseExpression(true, CHECK_OK);
2495 ExpectSemicolon(CHECK_OK);
2496 if (is_generator()) {
2497 Expression* generator = factory()->NewVariableProxy(
2498 function_state_->generator_object_variable());
2499 Expression* yield = factory()->NewYield(
2500 generator, return_value, Yield::FINAL, loc.beg_pos);
2501 result = factory()->NewExpressionStatement(yield, loc.beg_pos);
2503 result = factory()->NewReturnStatement(return_value, loc.beg_pos);
2506 Scope* decl_scope = scope_->DeclarationScope();
2507 if (decl_scope->is_global_scope() || decl_scope->is_eval_scope()) {
2508 ReportMessageAt(loc, "illegal_return");
2516 Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
2518 // 'with' '(' Expression ')' Statement
2520 Expect(Token::WITH, CHECK_OK);
2521 int pos = position();
2523 if (strict_mode() == STRICT) {
2524 ReportMessage("strict_mode_with", Vector<const char*>::empty());
2529 Expect(Token::LPAREN, CHECK_OK);
2530 Expression* expr = ParseExpression(true, CHECK_OK);
2531 Expect(Token::RPAREN, CHECK_OK);
2533 scope_->DeclarationScope()->RecordWithStatement();
2534 Scope* with_scope = NewScope(scope_, WITH_SCOPE);
2536 { BlockState block_state(&scope_, with_scope);
2537 with_scope->set_start_position(scanner()->peek_location().beg_pos);
2538 stmt = ParseStatement(labels, CHECK_OK);
2539 with_scope->set_end_position(scanner()->location().end_pos);
2541 return factory()->NewWithStatement(with_scope, expr, stmt, pos);
2545 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
2547 // 'case' Expression ':' Statement*
2548 // 'default' ':' Statement*
2550 Expression* label = NULL; // NULL expression indicates default case
2551 if (peek() == Token::CASE) {
2552 Expect(Token::CASE, CHECK_OK);
2553 label = ParseExpression(true, CHECK_OK);
2555 Expect(Token::DEFAULT, CHECK_OK);
2556 if (*default_seen_ptr) {
2557 ReportMessage("multiple_defaults_in_switch",
2558 Vector<const char*>::empty());
2562 *default_seen_ptr = true;
2564 Expect(Token::COLON, CHECK_OK);
2565 int pos = position();
2566 ZoneList<Statement*>* statements =
2567 new(zone()) ZoneList<Statement*>(5, zone());
2568 while (peek() != Token::CASE &&
2569 peek() != Token::DEFAULT &&
2570 peek() != Token::RBRACE) {
2571 Statement* stat = ParseStatement(NULL, CHECK_OK);
2572 statements->Add(stat, zone());
2575 return factory()->NewCaseClause(label, statements, pos);
2579 SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels,
2581 // SwitchStatement ::
2582 // 'switch' '(' Expression ')' '{' CaseClause* '}'
2584 SwitchStatement* statement =
2585 factory()->NewSwitchStatement(labels, peek_position());
2586 Target target(&this->target_stack_, statement);
2588 Expect(Token::SWITCH, CHECK_OK);
2589 Expect(Token::LPAREN, CHECK_OK);
2590 Expression* tag = ParseExpression(true, CHECK_OK);
2591 Expect(Token::RPAREN, CHECK_OK);
2593 bool default_seen = false;
2594 ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone());
2595 Expect(Token::LBRACE, CHECK_OK);
2596 while (peek() != Token::RBRACE) {
2597 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
2598 cases->Add(clause, zone());
2600 Expect(Token::RBRACE, CHECK_OK);
2602 if (statement) statement->Initialize(tag, cases);
2607 Statement* Parser::ParseThrowStatement(bool* ok) {
2608 // ThrowStatement ::
2609 // 'throw' Expression ';'
2611 Expect(Token::THROW, CHECK_OK);
2612 int pos = position();
2613 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
2614 ReportMessage("newline_after_throw", Vector<const char*>::empty());
2618 Expression* exception = ParseExpression(true, CHECK_OK);
2619 ExpectSemicolon(CHECK_OK);
2621 return factory()->NewExpressionStatement(
2622 factory()->NewThrow(exception, pos), pos);
2626 TryStatement* Parser::ParseTryStatement(bool* ok) {
2628 // 'try' Block Catch
2629 // 'try' Block Finally
2630 // 'try' Block Catch Finally
2633 // 'catch' '(' Identifier ')' Block
2638 Expect(Token::TRY, CHECK_OK);
2639 int pos = position();
2641 TargetCollector try_collector(zone());
2644 { Target target(&this->target_stack_, &try_collector);
2645 try_block = ParseBlock(NULL, CHECK_OK);
2648 Token::Value tok = peek();
2649 if (tok != Token::CATCH && tok != Token::FINALLY) {
2650 ReportMessage("no_catch_or_finally", Vector<const char*>::empty());
2655 // If we can break out from the catch block and there is a finally block,
2656 // then we will need to collect escaping targets from the catch
2657 // block. Since we don't know yet if there will be a finally block, we
2658 // always collect the targets.
2659 TargetCollector catch_collector(zone());
2660 Scope* catch_scope = NULL;
2661 Variable* catch_variable = NULL;
2662 Block* catch_block = NULL;
2663 Handle<String> name;
2664 if (tok == Token::CATCH) {
2665 Consume(Token::CATCH);
2667 Expect(Token::LPAREN, CHECK_OK);
2668 catch_scope = NewScope(scope_, CATCH_SCOPE);
2669 catch_scope->set_start_position(scanner()->location().beg_pos);
2670 name = ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
2672 Expect(Token::RPAREN, CHECK_OK);
2674 Target target(&this->target_stack_, &catch_collector);
2676 allow_harmony_scoping() && strict_mode() == STRICT ? LET : VAR;
2678 catch_scope->DeclareLocal(name, mode, kCreatedInitialized);
2680 BlockState block_state(&scope_, catch_scope);
2681 catch_block = ParseBlock(NULL, CHECK_OK);
2683 catch_scope->set_end_position(scanner()->location().end_pos);
2687 Block* finally_block = NULL;
2688 ASSERT(tok == Token::FINALLY || catch_block != NULL);
2689 if (tok == Token::FINALLY) {
2690 Consume(Token::FINALLY);
2691 finally_block = ParseBlock(NULL, CHECK_OK);
2694 // Simplify the AST nodes by converting:
2695 // 'try B0 catch B1 finally B2'
2697 // 'try { try B0 catch B1 } finally B2'
2699 if (catch_block != NULL && finally_block != NULL) {
2700 // If we have both, create an inner try/catch.
2701 ASSERT(catch_scope != NULL && catch_variable != NULL);
2702 int index = function_state_->NextHandlerIndex();
2703 TryCatchStatement* statement = factory()->NewTryCatchStatement(
2704 index, try_block, catch_scope, catch_variable, catch_block,
2705 RelocInfo::kNoPosition);
2706 statement->set_escaping_targets(try_collector.targets());
2707 try_block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
2708 try_block->AddStatement(statement, zone());
2709 catch_block = NULL; // Clear to indicate it's been handled.
2712 TryStatement* result = NULL;
2713 if (catch_block != NULL) {
2714 ASSERT(finally_block == NULL);
2715 ASSERT(catch_scope != NULL && catch_variable != NULL);
2716 int index = function_state_->NextHandlerIndex();
2717 result = factory()->NewTryCatchStatement(
2718 index, try_block, catch_scope, catch_variable, catch_block, pos);
2720 ASSERT(finally_block != NULL);
2721 int index = function_state_->NextHandlerIndex();
2722 result = factory()->NewTryFinallyStatement(
2723 index, try_block, finally_block, pos);
2724 // Combine the jump targets of the try block and the possible catch block.
2725 try_collector.targets()->AddAll(*catch_collector.targets(), zone());
2728 result->set_escaping_targets(try_collector.targets());
2733 DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels,
2736 // 'do' Statement 'while' '(' Expression ')' ';'
2738 DoWhileStatement* loop =
2739 factory()->NewDoWhileStatement(labels, peek_position());
2740 Target target(&this->target_stack_, loop);
2742 Expect(Token::DO, CHECK_OK);
2743 Statement* body = ParseStatement(NULL, CHECK_OK);
2744 Expect(Token::WHILE, CHECK_OK);
2745 Expect(Token::LPAREN, CHECK_OK);
2747 Expression* cond = ParseExpression(true, CHECK_OK);
2748 Expect(Token::RPAREN, CHECK_OK);
2750 // Allow do-statements to be terminated with and without
2751 // semi-colons. This allows code such as 'do;while(0)return' to
2752 // parse, which would not be the case if we had used the
2753 // ExpectSemicolon() functionality here.
2754 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
2756 if (loop != NULL) loop->Initialize(cond, body);
2761 WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
2762 // WhileStatement ::
2763 // 'while' '(' Expression ')' Statement
2765 WhileStatement* loop = factory()->NewWhileStatement(labels, peek_position());
2766 Target target(&this->target_stack_, loop);
2768 Expect(Token::WHILE, CHECK_OK);
2769 Expect(Token::LPAREN, CHECK_OK);
2770 Expression* cond = ParseExpression(true, CHECK_OK);
2771 Expect(Token::RPAREN, CHECK_OK);
2772 Statement* body = ParseStatement(NULL, CHECK_OK);
2774 if (loop != NULL) loop->Initialize(cond, body);
2779 bool Parser::CheckInOrOf(bool accept_OF,
2780 ForEachStatement::VisitMode* visit_mode) {
2781 if (Check(Token::IN)) {
2782 *visit_mode = ForEachStatement::ENUMERATE;
2784 } else if (allow_for_of() && accept_OF &&
2785 CheckContextualKeyword(CStrVector("of"))) {
2786 *visit_mode = ForEachStatement::ITERATE;
2793 void Parser::InitializeForEachStatement(ForEachStatement* stmt,
2795 Expression* subject,
2797 ForOfStatement* for_of = stmt->AsForOfStatement();
2799 if (for_of != NULL) {
2800 Factory* heap_factory = isolate()->factory();
2801 Variable* iterator = scope_->DeclarationScope()->NewTemporary(
2802 heap_factory->dot_iterator_string());
2803 Variable* result = scope_->DeclarationScope()->NewTemporary(
2804 heap_factory->dot_result_string());
2806 Expression* assign_iterator;
2807 Expression* next_result;
2808 Expression* result_done;
2809 Expression* assign_each;
2811 // var iterator = iterable;
2813 Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2814 assign_iterator = factory()->NewAssignment(
2815 Token::ASSIGN, iterator_proxy, subject, RelocInfo::kNoPosition);
2818 // var result = iterator.next();
2820 Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2821 Expression* next_literal = factory()->NewLiteral(
2822 heap_factory->next_string(), RelocInfo::kNoPosition);
2823 Expression* next_property = factory()->NewProperty(
2824 iterator_proxy, next_literal, RelocInfo::kNoPosition);
2825 ZoneList<Expression*>* next_arguments =
2826 new(zone()) ZoneList<Expression*>(0, zone());
2827 Expression* next_call = factory()->NewCall(
2828 next_property, next_arguments, RelocInfo::kNoPosition);
2829 Expression* result_proxy = factory()->NewVariableProxy(result);
2830 next_result = factory()->NewAssignment(
2831 Token::ASSIGN, result_proxy, next_call, RelocInfo::kNoPosition);
2836 Expression* done_literal = factory()->NewLiteral(
2837 heap_factory->done_string(), RelocInfo::kNoPosition);
2838 Expression* result_proxy = factory()->NewVariableProxy(result);
2839 result_done = factory()->NewProperty(
2840 result_proxy, done_literal, RelocInfo::kNoPosition);
2843 // each = result.value
2845 Expression* value_literal = factory()->NewLiteral(
2846 heap_factory->value_string(), RelocInfo::kNoPosition);
2847 Expression* result_proxy = factory()->NewVariableProxy(result);
2848 Expression* result_value = factory()->NewProperty(
2849 result_proxy, value_literal, RelocInfo::kNoPosition);
2850 assign_each = factory()->NewAssignment(
2851 Token::ASSIGN, each, result_value, RelocInfo::kNoPosition);
2854 for_of->Initialize(each, subject, body,
2855 assign_iterator, next_result, result_done, assign_each);
2857 stmt->Initialize(each, subject, body);
2862 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2864 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2866 int pos = peek_position();
2867 Statement* init = NULL;
2869 // Create an in-between scope for let-bound iteration variables.
2870 Scope* saved_scope = scope_;
2871 Scope* for_scope = NewScope(scope_, BLOCK_SCOPE);
2874 Expect(Token::FOR, CHECK_OK);
2875 Expect(Token::LPAREN, CHECK_OK);
2876 for_scope->set_start_position(scanner()->location().beg_pos);
2877 if (peek() != Token::SEMICOLON) {
2878 if (peek() == Token::VAR || peek() == Token::CONST) {
2879 bool is_const = peek() == Token::CONST;
2880 Handle<String> name;
2881 VariableDeclarationProperties decl_props = kHasNoInitializers;
2882 Block* variable_statement =
2883 ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2885 bool accept_OF = decl_props == kHasNoInitializers;
2886 ForEachStatement::VisitMode mode;
2888 if (!name.is_null() && CheckInOrOf(accept_OF, &mode)) {
2889 Interface* interface =
2890 is_const ? Interface::NewConst() : Interface::NewValue();
2891 ForEachStatement* loop =
2892 factory()->NewForEachStatement(mode, labels, pos);
2893 Target target(&this->target_stack_, loop);
2895 Expression* enumerable = ParseExpression(true, CHECK_OK);
2896 Expect(Token::RPAREN, CHECK_OK);
2898 VariableProxy* each =
2899 scope_->NewUnresolved(factory(), name, interface);
2900 Statement* body = ParseStatement(NULL, CHECK_OK);
2901 InitializeForEachStatement(loop, each, enumerable, body);
2903 factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
2904 result->AddStatement(variable_statement, zone());
2905 result->AddStatement(loop, zone());
2906 scope_ = saved_scope;
2907 for_scope->set_end_position(scanner()->location().end_pos);
2908 for_scope = for_scope->FinalizeBlockScope();
2909 ASSERT(for_scope == NULL);
2910 // Parsed for-in loop w/ variable/const declaration.
2913 init = variable_statement;
2915 } else if (peek() == Token::LET) {
2916 Handle<String> name;
2917 VariableDeclarationProperties decl_props = kHasNoInitializers;
2918 Block* variable_statement =
2919 ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2921 bool accept_IN = !name.is_null() && decl_props != kHasInitializers;
2922 bool accept_OF = decl_props == kHasNoInitializers;
2923 ForEachStatement::VisitMode mode;
2925 if (accept_IN && CheckInOrOf(accept_OF, &mode)) {
2926 // Rewrite a for-in statement of the form
2928 // for (let x in e) b
2932 // <let x' be a temporary variable>
2939 // TODO(keuchel): Move the temporary variable to the block scope, after
2940 // implementing stack allocated block scoped variables.
2941 Factory* heap_factory = isolate()->factory();
2942 Handle<String> tempstr;
2943 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
2945 heap_factory->NewConsString(heap_factory->dot_for_string(), name),
2947 Handle<String> tempname = heap_factory->InternalizeString(tempstr);
2948 Variable* temp = scope_->DeclarationScope()->NewTemporary(tempname);
2949 VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2950 ForEachStatement* loop =
2951 factory()->NewForEachStatement(mode, labels, pos);
2952 Target target(&this->target_stack_, loop);
2954 // The expression does not see the loop variable.
2955 scope_ = saved_scope;
2956 Expression* enumerable = ParseExpression(true, CHECK_OK);
2958 Expect(Token::RPAREN, CHECK_OK);
2960 VariableProxy* each =
2961 scope_->NewUnresolved(factory(), name, Interface::NewValue());
2962 Statement* body = ParseStatement(NULL, CHECK_OK);
2964 factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
2965 Assignment* assignment = factory()->NewAssignment(
2966 Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition);
2967 Statement* assignment_statement = factory()->NewExpressionStatement(
2968 assignment, RelocInfo::kNoPosition);
2969 body_block->AddStatement(variable_statement, zone());
2970 body_block->AddStatement(assignment_statement, zone());
2971 body_block->AddStatement(body, zone());
2972 InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
2973 scope_ = saved_scope;
2974 for_scope->set_end_position(scanner()->location().end_pos);
2975 for_scope = for_scope->FinalizeBlockScope();
2976 body_block->set_scope(for_scope);
2977 // Parsed for-in loop w/ let declaration.
2981 init = variable_statement;
2984 Scanner::Location lhs_location = scanner()->peek_location();
2985 Expression* expression = ParseExpression(false, CHECK_OK);
2986 ForEachStatement::VisitMode mode;
2987 bool accept_OF = expression->AsVariableProxy();
2989 if (CheckInOrOf(accept_OF, &mode)) {
2990 expression = this->CheckAndRewriteReferenceExpression(
2991 expression, lhs_location, "invalid_lhs_in_for", CHECK_OK);
2993 ForEachStatement* loop =
2994 factory()->NewForEachStatement(mode, labels, pos);
2995 Target target(&this->target_stack_, loop);
2997 Expression* enumerable = ParseExpression(true, CHECK_OK);
2998 Expect(Token::RPAREN, CHECK_OK);
3000 Statement* body = ParseStatement(NULL, CHECK_OK);
3001 InitializeForEachStatement(loop, expression, enumerable, body);
3002 scope_ = saved_scope;
3003 for_scope->set_end_position(scanner()->location().end_pos);
3004 for_scope = for_scope->FinalizeBlockScope();
3005 ASSERT(for_scope == NULL);
3006 // Parsed for-in loop.
3010 init = factory()->NewExpressionStatement(
3011 expression, RelocInfo::kNoPosition);
3016 // Standard 'for' loop
3017 ForStatement* loop = factory()->NewForStatement(labels, pos);
3018 Target target(&this->target_stack_, loop);
3020 // Parsed initializer at this point.
3021 Expect(Token::SEMICOLON, CHECK_OK);
3023 Expression* cond = NULL;
3024 if (peek() != Token::SEMICOLON) {
3025 cond = ParseExpression(true, CHECK_OK);
3027 Expect(Token::SEMICOLON, CHECK_OK);
3029 Statement* next = NULL;
3030 if (peek() != Token::RPAREN) {
3031 Expression* exp = ParseExpression(true, CHECK_OK);
3032 next = factory()->NewExpressionStatement(exp, RelocInfo::kNoPosition);
3034 Expect(Token::RPAREN, CHECK_OK);
3036 Statement* body = ParseStatement(NULL, CHECK_OK);
3037 scope_ = saved_scope;
3038 for_scope->set_end_position(scanner()->location().end_pos);
3039 for_scope = for_scope->FinalizeBlockScope();
3040 if (for_scope != NULL) {
3041 // Rewrite a for statement of the form
3043 // for (let x = i; c; n) b
3051 ASSERT(init != NULL);
3052 Block* result = factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
3053 result->AddStatement(init, zone());
3054 result->AddStatement(loop, zone());
3055 result->set_scope(for_scope);
3056 loop->Initialize(NULL, cond, next, body);
3059 loop->Initialize(init, cond, next, body);
3065 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
3066 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
3067 // contexts this is used as a statement which invokes the debugger as i a
3068 // break point is present.
3069 // DebuggerStatement ::
3072 int pos = peek_position();
3073 Expect(Token::DEBUGGER, CHECK_OK);
3074 ExpectSemicolon(CHECK_OK);
3075 return factory()->NewDebuggerStatement(pos);
3079 void Parser::ReportInvalidCachedData(Handle<String> name, bool* ok) {
3080 SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS);
3081 const char* element[1] = { name_string.get() };
3082 ReportMessage("invalid_cached_data_function",
3083 Vector<const char*>(element, 1));
3088 bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
3089 if (expression->AsLiteral() != NULL) return true;
3090 MaterializedLiteral* lit = expression->AsMaterializedLiteral();
3091 return lit != NULL && lit->is_simple();
3095 Handle<FixedArray> CompileTimeValue::GetValue(Isolate* isolate,
3096 Expression* expression) {
3097 Factory* factory = isolate->factory();
3098 ASSERT(IsCompileTimeValue(expression));
3099 Handle<FixedArray> result = factory->NewFixedArray(2, TENURED);
3100 ObjectLiteral* object_literal = expression->AsObjectLiteral();
3101 if (object_literal != NULL) {
3102 ASSERT(object_literal->is_simple());
3103 if (object_literal->fast_elements()) {
3104 result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
3106 result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
3108 result->set(kElementsSlot, *object_literal->constant_properties());
3110 ArrayLiteral* array_literal = expression->AsArrayLiteral();
3111 ASSERT(array_literal != NULL && array_literal->is_simple());
3112 result->set(kLiteralTypeSlot, Smi::FromInt(ARRAY_LITERAL));
3113 result->set(kElementsSlot, *array_literal->constant_elements());
3119 CompileTimeValue::LiteralType CompileTimeValue::GetLiteralType(
3120 Handle<FixedArray> value) {
3121 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3122 return static_cast<LiteralType>(literal_type->value());
3126 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3127 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3131 FunctionLiteral* Parser::ParseFunctionLiteral(
3132 Handle<String> function_name,
3133 Scanner::Location function_name_location,
3134 bool name_is_strict_reserved,
3136 int function_token_pos,
3137 FunctionLiteral::FunctionType function_type,
3140 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3142 int pos = function_token_pos == RelocInfo::kNoPosition
3143 ? peek_position() : function_token_pos;
3145 // Anonymous functions were passed either the empty symbol or a null
3146 // handle as the function name. Remember if we were passed a non-empty
3147 // handle to decide whether to invoke function name inference.
3148 bool should_infer_name = function_name.is_null();
3150 // We want a non-null handle as the function name.
3151 if (should_infer_name) {
3152 function_name = isolate()->factory()->empty_string();
3155 int num_parameters = 0;
3156 // Function declarations are function scoped in normal mode, so they are
3157 // hoisted. In harmony block scoping mode they are block scoped, so they
3160 // One tricky case are function declarations in a local sloppy-mode eval:
3161 // their declaration is hoisted, but they still see the local scope. E.g.,
3165 // try { throw 1 } catch (x) { eval("function g() { return x }") }
3169 // needs to return 1. To distinguish such cases, we need to detect
3170 // (1) whether a function stems from a sloppy eval, and
3171 // (2) whether it actually hoists across the eval.
3172 // Unfortunately, we do not represent sloppy eval scopes, so we do not have
3173 // either information available directly, especially not when lazily compiling
3174 // a function like 'g'. We hence rely on the following invariants:
3175 // - (1) is the case iff the innermost scope of the deserialized scope chain
3176 // under which we compile is _not_ a declaration scope. This holds because
3177 // in all normal cases, function declarations are fully hoisted to a
3178 // declaration scope and compiled relative to that.
3179 // - (2) is the case iff the current declaration scope is still the original
3180 // one relative to the deserialized scope chain. Otherwise we must be
3181 // compiling a function in an inner declaration scope in the eval, e.g. a
3182 // nested function, and hoisting works normally relative to that.
3183 Scope* declaration_scope = scope_->DeclarationScope();
3184 Scope* original_declaration_scope = original_scope_->DeclarationScope();
3186 function_type == FunctionLiteral::DECLARATION &&
3187 (!allow_harmony_scoping() || strict_mode() == SLOPPY) &&
3188 (original_scope_ == original_declaration_scope ||
3189 declaration_scope != original_declaration_scope)
3190 ? NewScope(declaration_scope, FUNCTION_SCOPE)
3191 : NewScope(scope_, FUNCTION_SCOPE);
3192 ZoneList<Statement*>* body = NULL;
3193 int materialized_literal_count = -1;
3194 int expected_property_count = -1;
3195 int handler_count = 0;
3196 FunctionLiteral::ParameterFlag duplicate_parameters =
3197 FunctionLiteral::kNoDuplicateParameters;
3198 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
3199 ? FunctionLiteral::kIsParenthesized
3200 : FunctionLiteral::kNotParenthesized;
3201 AstProperties ast_properties;
3202 BailoutReason dont_optimize_reason = kNoReason;
3203 // Parse function body.
3204 { FunctionState function_state(&function_state_, &scope_, scope, zone());
3205 scope_->SetScopeName(function_name);
3208 // For generators, allocating variables in contexts is currently a win
3209 // because it minimizes the work needed to suspend and resume an
3211 scope_->ForceContextAllocation();
3213 // Calling a generator returns a generator object. That object is stored
3214 // in a temporary variable, a definition that is used by "yield"
3215 // expressions. This also marks the FunctionState as a generator.
3216 Variable* temp = scope_->DeclarationScope()->NewTemporary(
3217 isolate()->factory()->dot_generator_object_string());
3218 function_state.set_generator_object_variable(temp);
3221 // FormalParameterList ::
3222 // '(' (Identifier)*[','] ')'
3223 Expect(Token::LPAREN, CHECK_OK);
3224 scope->set_start_position(scanner()->location().beg_pos);
3226 // We don't yet know if the function will be strict, so we cannot yet
3227 // produce errors for parameter names or duplicates. However, we remember
3228 // the locations of these errors if they occur and produce the errors later.
3229 Scanner::Location eval_args_error_log = Scanner::Location::invalid();
3230 Scanner::Location dupe_error_loc = Scanner::Location::invalid();
3231 Scanner::Location reserved_loc = Scanner::Location::invalid();
3233 bool done = (peek() == Token::RPAREN);
3235 bool is_strict_reserved = false;
3236 Handle<String> param_name =
3237 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
3239 // Store locations for possible future error reports.
3240 if (!eval_args_error_log.IsValid() && IsEvalOrArguments(param_name)) {
3241 eval_args_error_log = scanner()->location();
3243 if (!reserved_loc.IsValid() && is_strict_reserved) {
3244 reserved_loc = scanner()->location();
3246 if (!dupe_error_loc.IsValid() && scope_->IsDeclared(param_name)) {
3247 duplicate_parameters = FunctionLiteral::kHasDuplicateParameters;
3248 dupe_error_loc = scanner()->location();
3251 scope_->DeclareParameter(param_name, VAR);
3253 if (num_parameters > Code::kMaxArguments) {
3254 ReportMessageAt(scanner()->location(), "too_many_parameters");
3258 done = (peek() == Token::RPAREN);
3259 if (!done) Expect(Token::COMMA, CHECK_OK);
3261 Expect(Token::RPAREN, CHECK_OK);
3263 Expect(Token::LBRACE, CHECK_OK);
3265 // If we have a named function expression, we add a local variable
3266 // declaration to the body of the function with the name of the
3267 // function and let it refer to the function itself (closure).
3268 // NOTE: We create a proxy and resolve it here so that in the
3269 // future we can change the AST to only refer to VariableProxies
3270 // instead of Variables and Proxis as is the case now.
3271 Variable* fvar = NULL;
3272 Token::Value fvar_init_op = Token::INIT_CONST_LEGACY;
3273 if (function_type == FunctionLiteral::NAMED_EXPRESSION) {
3274 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3275 fvar_init_op = Token::INIT_CONST;
3277 VariableMode fvar_mode =
3278 allow_harmony_scoping() && strict_mode() == STRICT ? CONST
3280 fvar = new(zone()) Variable(scope_,
3281 function_name, fvar_mode, true /* is valid LHS */,
3282 Variable::NORMAL, kCreatedInitialized, Interface::NewConst());
3283 VariableProxy* proxy = factory()->NewVariableProxy(fvar);
3284 VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration(
3285 proxy, fvar_mode, scope_, RelocInfo::kNoPosition);
3286 scope_->DeclareFunctionVar(fvar_declaration);
3289 // Determine if the function can be parsed lazily. Lazy parsing is different
3290 // from lazy compilation; we need to parse more eagerly than we compile.
3292 // We can only parse lazily if we also compile lazily. The heuristics for
3293 // lazy compilation are:
3294 // - It must not have been prohibited by the caller to Parse (some callers
3295 // need a full AST).
3296 // - The outer scope must allow lazy compilation of inner functions.
3297 // - The function mustn't be a function expression with an open parenthesis
3298 // before; we consider that a hint that the function will be called
3299 // immediately, and it would be a waste of time to make it lazily
3301 // These are all things we can know at this point, without looking at the
3304 // In addition, we need to distinguish between these cases:
3305 // (function foo() {
3306 // bar = function() { return 1; }
3309 // (function foo() {
3311 // bar = function() { return a; }
3314 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume
3315 // parenthesis before the function means that it will be called
3316 // immediately). The inner function *must* be parsed eagerly to resolve the
3317 // possible reference to the variable in foo's scope. However, it's possible
3318 // that it will be compiled lazily.
3320 // To make this additional case work, both Parser and PreParser implement a
3321 // logic where only top-level functions will be parsed lazily.
3322 bool is_lazily_parsed = (mode() == PARSE_LAZILY &&
3323 scope_->AllowsLazyCompilation() &&
3324 !parenthesized_function_);
3325 parenthesized_function_ = false; // The bit was set for this function only.
3327 if (is_lazily_parsed) {
3328 SkipLazyFunctionBody(function_name, &materialized_literal_count,
3329 &expected_property_count, CHECK_OK);
3331 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
3332 is_generator, CHECK_OK);
3333 materialized_literal_count = function_state.materialized_literal_count();
3334 expected_property_count = function_state.expected_property_count();
3335 handler_count = function_state.handler_count();
3338 // Validate strict mode. We can do this only after parsing the function,
3339 // since the function can declare itself strict.
3340 if (strict_mode() == STRICT) {
3341 if (IsEvalOrArguments(function_name)) {
3342 ReportMessageAt(function_name_location, "strict_eval_arguments");
3346 if (name_is_strict_reserved) {
3347 ReportMessageAt(function_name_location, "unexpected_strict_reserved");
3351 if (eval_args_error_log.IsValid()) {
3352 ReportMessageAt(eval_args_error_log, "strict_eval_arguments");
3356 if (dupe_error_loc.IsValid()) {
3357 ReportMessageAt(dupe_error_loc, "strict_param_dupe");
3361 if (reserved_loc.IsValid()) {
3362 ReportMessageAt(reserved_loc, "unexpected_strict_reserved");
3366 CheckOctalLiteral(scope->start_position(),
3367 scope->end_position(),
3370 ast_properties = *factory()->visitor()->ast_properties();
3371 dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
3374 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3375 CheckConflictingVarDeclarations(scope, CHECK_OK);
3378 FunctionLiteral::IsGeneratorFlag generator = is_generator
3379 ? FunctionLiteral::kIsGenerator
3380 : FunctionLiteral::kNotGenerator;
3381 FunctionLiteral* function_literal =
3382 factory()->NewFunctionLiteral(function_name,
3385 materialized_literal_count,
3386 expected_property_count,
3389 duplicate_parameters,
3391 FunctionLiteral::kIsFunction,
3395 function_literal->set_function_token_position(function_token_pos);
3396 function_literal->set_ast_properties(&ast_properties);
3397 function_literal->set_dont_optimize_reason(dont_optimize_reason);
3399 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
3400 return function_literal;
3404 void Parser::SkipLazyFunctionBody(Handle<String> function_name,
3405 int* materialized_literal_count,
3406 int* expected_property_count,
3408 int function_block_pos = position();
3409 if (cached_data_mode_ == CONSUME_CACHED_DATA) {
3410 // If we have cached data, we use it to skip parsing the function body. The
3411 // data contains the information we need to construct the lazy function.
3412 FunctionEntry entry =
3413 (*cached_data())->GetFunctionEntry(function_block_pos);
3414 if (entry.is_valid()) {
3415 if (entry.end_pos() <= function_block_pos) {
3416 // End position greater than end of stream is safe, and hard to check.
3417 ReportInvalidCachedData(function_name, ok);
3422 scanner()->SeekForward(entry.end_pos() - 1);
3424 scope_->set_end_position(entry.end_pos());
3425 Expect(Token::RBRACE, ok);
3429 isolate()->counters()->total_preparse_skipped()->Increment(
3430 scope_->end_position() - function_block_pos);
3431 *materialized_literal_count = entry.literal_count();
3432 *expected_property_count = entry.property_count();
3433 scope_->SetStrictMode(entry.strict_mode());
3435 // This case happens when we have preparse data but it doesn't contain an
3436 // entry for the function. Fail the compilation.
3437 ReportInvalidCachedData(function_name, ok);
3441 // With no cached data, we partially parse the function, without building an
3442 // AST. This gathers the data needed to build a lazy function.
3443 SingletonLogger logger;
3444 PreParser::PreParseResult result =
3445 ParseLazyFunctionBodyWithPreParser(&logger);
3446 if (result == PreParser::kPreParseStackOverflow) {
3447 // Propagate stack overflow.
3448 set_stack_overflow();
3452 if (logger.has_error()) {
3453 const char* arg = logger.argument_opt();
3454 Vector<const char*> args;
3456 args = Vector<const char*>(&arg, 1);
3458 ParserTraits::ReportMessageAt(
3459 Scanner::Location(logger.start(), logger.end()),
3460 logger.message(), args, logger.is_reference_error());
3464 scope_->set_end_position(logger.end());
3465 Expect(Token::RBRACE, ok);
3469 isolate()->counters()->total_preparse_skipped()->Increment(
3470 scope_->end_position() - function_block_pos);
3471 *materialized_literal_count = logger.literals();
3472 *expected_property_count = logger.properties();
3473 scope_->SetStrictMode(logger.strict_mode());
3474 if (cached_data_mode_ == PRODUCE_CACHED_DATA) {
3476 // Position right after terminal '}'.
3477 int body_end = scanner()->location().end_pos;
3478 log_->LogFunction(function_block_pos, body_end,
3479 *materialized_literal_count,
3480 *expected_property_count,
3481 scope_->strict_mode());
3487 ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
3488 Handle<String> function_name, int pos, Variable* fvar,
3489 Token::Value fvar_init_op, bool is_generator, bool* ok) {
3490 // Everything inside an eagerly parsed function will be parsed eagerly
3491 // (see comment above).
3492 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
3493 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8, zone());
3495 VariableProxy* fproxy = scope_->NewUnresolved(
3496 factory(), function_name, Interface::NewConst());
3497 fproxy->BindTo(fvar);
3498 body->Add(factory()->NewExpressionStatement(
3499 factory()->NewAssignment(fvar_init_op,
3501 factory()->NewThisFunction(pos),
3502 RelocInfo::kNoPosition),
3503 RelocInfo::kNoPosition), zone());
3506 // For generators, allocate and yield an iterator on function entry.
3508 ZoneList<Expression*>* arguments =
3509 new(zone()) ZoneList<Expression*>(0, zone());
3510 CallRuntime* allocation = factory()->NewCallRuntime(
3511 isolate()->factory()->empty_string(),
3512 Runtime::FunctionForId(Runtime::kHiddenCreateJSGeneratorObject),
3514 VariableProxy* init_proxy = factory()->NewVariableProxy(
3515 function_state_->generator_object_variable());
3516 Assignment* assignment = factory()->NewAssignment(
3517 Token::INIT_VAR, init_proxy, allocation, RelocInfo::kNoPosition);
3518 VariableProxy* get_proxy = factory()->NewVariableProxy(
3519 function_state_->generator_object_variable());
3520 Yield* yield = factory()->NewYield(
3521 get_proxy, assignment, Yield::INITIAL, RelocInfo::kNoPosition);
3522 body->Add(factory()->NewExpressionStatement(
3523 yield, RelocInfo::kNoPosition), zone());
3526 ParseSourceElements(body, Token::RBRACE, false, false, CHECK_OK);
3529 VariableProxy* get_proxy = factory()->NewVariableProxy(
3530 function_state_->generator_object_variable());
3531 Expression *undefined = factory()->NewLiteral(
3532 isolate()->factory()->undefined_value(), RelocInfo::kNoPosition);
3533 Yield* yield = factory()->NewYield(
3534 get_proxy, undefined, Yield::FINAL, RelocInfo::kNoPosition);
3535 body->Add(factory()->NewExpressionStatement(
3536 yield, RelocInfo::kNoPosition), zone());
3539 Expect(Token::RBRACE, CHECK_OK);
3540 scope_->set_end_position(scanner()->location().end_pos);
3546 PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
3547 SingletonLogger* logger) {
3548 HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse());
3549 ASSERT_EQ(Token::LBRACE, scanner()->current_token());
3551 if (reusable_preparser_ == NULL) {
3552 intptr_t stack_limit = isolate()->stack_guard()->real_climit();
3553 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
3554 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
3555 reusable_preparser_->set_allow_modules(allow_modules());
3556 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
3557 reusable_preparser_->set_allow_lazy(true);
3558 reusable_preparser_->set_allow_generators(allow_generators());
3559 reusable_preparser_->set_allow_for_of(allow_for_of());
3560 reusable_preparser_->set_allow_harmony_numeric_literals(
3561 allow_harmony_numeric_literals());
3563 PreParser::PreParseResult result =
3564 reusable_preparser_->PreParseLazyFunction(strict_mode(),
3571 Expression* Parser::ParseV8Intrinsic(bool* ok) {
3573 // '%' Identifier Arguments
3575 int pos = peek_position();
3576 Expect(Token::MOD, CHECK_OK);
3577 // Allow "eval" or "arguments" for backward compatibility.
3578 Handle<String> name = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
3579 ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3581 if (extension_ != NULL) {
3582 // The extension structures are only accessible while parsing the
3583 // very first time not when reparsing because of lazy compilation.
3584 scope_->DeclarationScope()->ForceEagerCompilation();
3587 const Runtime::Function* function = Runtime::FunctionForName(name);
3589 // Check for built-in IS_VAR macro.
3590 if (function != NULL &&
3591 function->intrinsic_type == Runtime::RUNTIME &&
3592 function->function_id == Runtime::kIS_VAR) {
3593 // %IS_VAR(x) evaluates to x if x is a variable,
3594 // leads to a parse error otherwise. Could be implemented as an
3595 // inline function %_IS_VAR(x) to eliminate this special case.
3596 if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
3599 ReportMessage("not_isvar", Vector<const char*>::empty());
3605 // Check that the expected number of arguments are being passed.
3606 if (function != NULL &&
3607 function->nargs != -1 &&
3608 function->nargs != args->length()) {
3609 ReportMessage("illegal_access", Vector<const char*>::empty());
3614 // Check that the function is defined if it's an inline runtime call.
3615 if (function == NULL && name->Get(0) == '_') {
3616 ParserTraits::ReportMessage("not_defined",
3617 Vector<Handle<String> >(&name, 1));
3622 // We have a valid intrinsics call or a call to a builtin.
3623 return factory()->NewCallRuntime(name, function, args, pos);
3627 Literal* Parser::GetLiteralUndefined(int position) {
3628 return factory()->NewLiteral(
3629 isolate()->factory()->undefined_value(), position);
3633 void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
3634 Declaration* decl = scope->CheckConflictingVarDeclarations();
3636 // In harmony mode we treat conflicting variable bindinds as early
3637 // errors. See ES5 16 for a definition of early errors.
3638 Handle<String> name = decl->proxy()->name();
3639 SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
3640 const char* elms[1] = { c_string.get() };
3641 Vector<const char*> args(elms, 1);
3642 int position = decl->proxy()->position();
3643 Scanner::Location location = position == RelocInfo::kNoPosition
3644 ? Scanner::Location::invalid()
3645 : Scanner::Location(position, position + 1);
3646 ParserTraits::ReportMessageAt(location, "var_redeclaration", args);
3652 // ----------------------------------------------------------------------------
3656 bool Parser::TargetStackContainsLabel(Handle<String> label) {
3657 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3658 BreakableStatement* stat = t->node()->AsBreakableStatement();
3659 if (stat != NULL && ContainsLabel(stat->labels(), label))
3666 BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) {
3667 bool anonymous = label.is_null();
3668 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3669 BreakableStatement* stat = t->node()->AsBreakableStatement();
3670 if (stat == NULL) continue;
3671 if ((anonymous && stat->is_target_for_anonymous()) ||
3672 (!anonymous && ContainsLabel(stat->labels(), label))) {
3673 RegisterTargetUse(stat->break_target(), t->previous());
3681 IterationStatement* Parser::LookupContinueTarget(Handle<String> label,
3683 bool anonymous = label.is_null();
3684 for (Target* t = target_stack_; t != NULL; t = t->previous()) {
3685 IterationStatement* stat = t->node()->AsIterationStatement();
3686 if (stat == NULL) continue;
3688 ASSERT(stat->is_target_for_anonymous());
3689 if (anonymous || ContainsLabel(stat->labels(), label)) {
3690 RegisterTargetUse(stat->continue_target(), t->previous());
3698 void Parser::RegisterTargetUse(Label* target, Target* stop) {
3699 // Register that a break target found at the given stop in the
3700 // target stack has been used from the top of the target stack. Add
3701 // the break target to any TargetCollectors passed on the stack.
3702 for (Target* t = target_stack_; t != stop; t = t->previous()) {
3703 TargetCollector* collector = t->node()->AsTargetCollector();
3704 if (collector != NULL) collector->AddTarget(target, zone());
3709 // ----------------------------------------------------------------------------
3710 // Regular expressions
3713 RegExpParser::RegExpParser(FlatStringReader* in,
3714 Handle<String>* error,
3717 : isolate_(zone->isolate()),
3722 current_(kEndMarker),
3726 multiline_(multiline),
3728 contains_anchor_(false),
3729 is_scanned_for_captures_(false),
3735 uc32 RegExpParser::Next() {
3737 return in()->Get(next_pos_);
3744 void RegExpParser::Advance() {
3745 if (next_pos_ < in()->length()) {
3746 StackLimitCheck check(isolate());
3747 if (check.HasOverflowed()) {
3748 ReportError(CStrVector(Isolate::kStackOverflowMessage));
3749 } else if (zone()->excess_allocation()) {
3750 ReportError(CStrVector("Regular expression too large"));
3752 current_ = in()->Get(next_pos_);
3756 current_ = kEndMarker;
3762 void RegExpParser::Reset(int pos) {
3764 has_more_ = (pos < in()->length());
3769 void RegExpParser::Advance(int dist) {
3770 next_pos_ += dist - 1;
3775 bool RegExpParser::simple() {
3780 RegExpTree* RegExpParser::ReportError(Vector<const char> message) {
3782 *error_ = isolate()->factory()->NewStringFromAscii(message).ToHandleChecked();
3783 // Zip to the end to make sure the no more input is read.
3784 current_ = kEndMarker;
3785 next_pos_ = in()->length();
3792 RegExpTree* RegExpParser::ParsePattern() {
3793 RegExpTree* result = ParseDisjunction(CHECK_FAILED);
3794 ASSERT(!has_more());
3795 // If the result of parsing is a literal string atom, and it has the
3796 // same length as the input, then the atom is identical to the input.
3797 if (result->IsAtom() && result->AsAtom()->length() == in()->length()) {
3806 // Alternative | Disjunction
3814 RegExpTree* RegExpParser::ParseDisjunction() {
3815 // Used to store current state while parsing subexpressions.
3816 RegExpParserState initial_state(NULL, INITIAL, 0, zone());
3817 RegExpParserState* stored_state = &initial_state;
3818 // Cache the builder in a local variable for quick access.
3819 RegExpBuilder* builder = initial_state.builder();
3821 switch (current()) {
3823 if (stored_state->IsSubexpression()) {
3824 // Inside a parenthesized group when hitting end of input.
3825 ReportError(CStrVector("Unterminated group") CHECK_FAILED);
3827 ASSERT_EQ(INITIAL, stored_state->group_type());
3828 // Parsing completed successfully.
3829 return builder->ToRegExp();
3831 if (!stored_state->IsSubexpression()) {
3832 ReportError(CStrVector("Unmatched ')'") CHECK_FAILED);
3834 ASSERT_NE(INITIAL, stored_state->group_type());
3837 // End disjunction parsing and convert builder content to new single
3839 RegExpTree* body = builder->ToRegExp();
3841 int end_capture_index = captures_started();
3843 int capture_index = stored_state->capture_index();
3844 SubexpressionType group_type = stored_state->group_type();
3846 // Restore previous state.
3847 stored_state = stored_state->previous_state();
3848 builder = stored_state->builder();
3850 // Build result of subexpression.
3851 if (group_type == CAPTURE) {
3852 RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index);
3853 captures_->at(capture_index - 1) = capture;
3855 } else if (group_type != GROUPING) {
3856 ASSERT(group_type == POSITIVE_LOOKAHEAD ||
3857 group_type == NEGATIVE_LOOKAHEAD);
3858 bool is_positive = (group_type == POSITIVE_LOOKAHEAD);
3859 body = new(zone()) RegExpLookahead(body,
3861 end_capture_index - capture_index,
3864 builder->AddAtom(body);
3865 // For compatability with JSC and ES3, we allow quantifiers after
3866 // lookaheads, and break in all cases.
3871 builder->NewAlternative();
3877 return ReportError(CStrVector("Nothing to repeat"));
3881 builder->AddAssertion(
3882 new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE));
3884 builder->AddAssertion(
3885 new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT));
3886 set_contains_anchor();
3892 RegExpAssertion::AssertionType assertion_type =
3893 multiline_ ? RegExpAssertion::END_OF_LINE :
3894 RegExpAssertion::END_OF_INPUT;
3895 builder->AddAssertion(new(zone()) RegExpAssertion(assertion_type));
3900 // everything except \x0a, \x0d, \u2028 and \u2029
3901 ZoneList<CharacterRange>* ranges =
3902 new(zone()) ZoneList<CharacterRange>(2, zone());
3903 CharacterRange::AddClassEscape('.', ranges, zone());
3904 RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
3905 builder->AddAtom(atom);
3909 SubexpressionType subexpr_type = CAPTURE;
3911 if (current() == '?') {
3914 subexpr_type = GROUPING;
3917 subexpr_type = POSITIVE_LOOKAHEAD;
3920 subexpr_type = NEGATIVE_LOOKAHEAD;
3923 ReportError(CStrVector("Invalid group") CHECK_FAILED);
3928 if (captures_ == NULL) {
3929 captures_ = new(zone()) ZoneList<RegExpCapture*>(2, zone());
3931 if (captures_started() >= kMaxCaptures) {
3932 ReportError(CStrVector("Too many captures") CHECK_FAILED);
3934 captures_->Add(NULL, zone());
3936 // Store current state and begin new disjunction parsing.
3937 stored_state = new(zone()) RegExpParserState(stored_state, subexpr_type,
3938 captures_started(), zone());
3939 builder = stored_state->builder();
3943 RegExpTree* atom = ParseCharacterClass(CHECK_FAILED);
3944 builder->AddAtom(atom);
3952 return ReportError(CStrVector("\\ at end of pattern"));
3955 builder->AddAssertion(
3956 new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY));
3960 builder->AddAssertion(
3961 new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY));
3964 // CharacterClassEscape
3966 // CharacterClassEscape :: one of
3968 case 'd': case 'D': case 's': case 'S': case 'w': case 'W': {
3971 ZoneList<CharacterRange>* ranges =
3972 new(zone()) ZoneList<CharacterRange>(2, zone());
3973 CharacterRange::AddClassEscape(c, ranges, zone());
3974 RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
3975 builder->AddAtom(atom);
3978 case '1': case '2': case '3': case '4': case '5': case '6':
3979 case '7': case '8': case '9': {
3981 if (ParseBackReferenceIndex(&index)) {
3982 RegExpCapture* capture = NULL;
3983 if (captures_ != NULL && index <= captures_->length()) {
3984 capture = captures_->at(index - 1);
3986 if (capture == NULL) {
3987 builder->AddEmpty();
3990 RegExpTree* atom = new(zone()) RegExpBackReference(capture);
3991 builder->AddAtom(atom);
3994 uc32 first_digit = Next();
3995 if (first_digit == '8' || first_digit == '9') {
3996 // Treat as identity escape
3997 builder->AddCharacter(first_digit);
4005 uc32 octal = ParseOctalLiteral();
4006 builder->AddCharacter(octal);
4009 // ControlEscape :: one of
4013 builder->AddCharacter('\f');
4017 builder->AddCharacter('\n');
4021 builder->AddCharacter('\r');
4025 builder->AddCharacter('\t');
4029 builder->AddCharacter('\v');
4033 uc32 controlLetter = Next();
4034 // Special case if it is an ASCII letter.
4035 // Convert lower case letters to uppercase.
4036 uc32 letter = controlLetter & ~('a' ^ 'A');
4037 if (letter < 'A' || 'Z' < letter) {
4038 // controlLetter is not in range 'A'-'Z' or 'a'-'z'.
4039 // This is outside the specification. We match JSC in
4040 // reading the backslash as a literal character instead
4041 // of as starting an escape.
4042 builder->AddCharacter('\\');
4045 builder->AddCharacter(controlLetter & 0x1f);
4052 if (ParseHexEscape(2, &value)) {
4053 builder->AddCharacter(value);
4055 builder->AddCharacter('x');
4062 if (ParseHexEscape(4, &value)) {
4063 builder->AddCharacter(value);
4065 builder->AddCharacter('u');
4071 builder->AddCharacter(Next());
4078 if (ParseIntervalQuantifier(&dummy, &dummy)) {
4079 ReportError(CStrVector("Nothing to repeat") CHECK_FAILED);
4084 builder->AddCharacter(current());
4087 } // end switch(current())
4091 switch (current()) {
4092 // QuantifierPrefix ::
4099 max = RegExpTree::kInfinity;
4104 max = RegExpTree::kInfinity;
4113 if (ParseIntervalQuantifier(&min, &max)) {
4115 ReportError(CStrVector("numbers out of order in {} quantifier.")
4125 RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY;
4126 if (current() == '?') {
4127 quantifier_type = RegExpQuantifier::NON_GREEDY;
4129 } else if (FLAG_regexp_possessive_quantifier && current() == '+') {
4130 // FLAG_regexp_possessive_quantifier is a debug-only flag.
4131 quantifier_type = RegExpQuantifier::POSSESSIVE;
4134 builder->AddQuantifierToAtom(min, max, quantifier_type);
4140 // Currently only used in an ASSERT.
4141 static bool IsSpecialClassEscape(uc32 c) {
4154 // In order to know whether an escape is a backreference or not we have to scan
4155 // the entire regexp and find the number of capturing parentheses. However we
4156 // don't want to scan the regexp twice unless it is necessary. This mini-parser
4157 // is called when needed. It can see the difference between capturing and
4158 // noncapturing parentheses and can skip character classes and backslash-escaped
4160 void RegExpParser::ScanForCaptures() {
4161 // Start with captures started previous to current position
4162 int capture_count = captures_started();
4163 // Add count of captures after this position.
4165 while ((n = current()) != kEndMarker) {
4173 while ((c = current()) != kEndMarker) {
4178 if (c == ']') break;
4184 if (current() != '?') capture_count++;
4188 capture_count_ = capture_count;
4189 is_scanned_for_captures_ = true;
4193 bool RegExpParser::ParseBackReferenceIndex(int* index_out) {
4194 ASSERT_EQ('\\', current());
4195 ASSERT('1' <= Next() && Next() <= '9');
4196 // Try to parse a decimal literal that is no greater than the total number
4197 // of left capturing parentheses in the input.
4198 int start = position();
4199 int value = Next() - '0';
4203 if (IsDecimalDigit(c)) {
4204 value = 10 * value + (c - '0');
4205 if (value > kMaxCaptures) {
4214 if (value > captures_started()) {
4215 if (!is_scanned_for_captures_) {
4216 int saved_position = position();
4218 Reset(saved_position);
4220 if (value > capture_count_) {
4230 // QuantifierPrefix ::
4231 // { DecimalDigits }
4232 // { DecimalDigits , }
4233 // { DecimalDigits , DecimalDigits }
4235 // Returns true if parsing succeeds, and set the min_out and max_out
4236 // values. Values are truncated to RegExpTree::kInfinity if they overflow.
4237 bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) {
4238 ASSERT_EQ(current(), '{');
4239 int start = position();
4242 if (!IsDecimalDigit(current())) {
4246 while (IsDecimalDigit(current())) {
4247 int next = current() - '0';
4248 if (min > (RegExpTree::kInfinity - next) / 10) {
4249 // Overflow. Skip past remaining decimal digits and return -1.
4252 } while (IsDecimalDigit(current()));
4253 min = RegExpTree::kInfinity;
4256 min = 10 * min + next;
4260 if (current() == '}') {
4263 } else if (current() == ',') {
4265 if (current() == '}') {
4266 max = RegExpTree::kInfinity;
4269 while (IsDecimalDigit(current())) {
4270 int next = current() - '0';
4271 if (max > (RegExpTree::kInfinity - next) / 10) {
4274 } while (IsDecimalDigit(current()));
4275 max = RegExpTree::kInfinity;
4278 max = 10 * max + next;
4281 if (current() != '}') {
4297 uc32 RegExpParser::ParseOctalLiteral() {
4298 ASSERT(('0' <= current() && current() <= '7') || current() == kEndMarker);
4299 // For compatibility with some other browsers (not all), we parse
4300 // up to three octal digits with a value below 256.
4301 uc32 value = current() - '0';
4303 if ('0' <= current() && current() <= '7') {
4304 value = value * 8 + current() - '0';
4306 if (value < 32 && '0' <= current() && current() <= '7') {
4307 value = value * 8 + current() - '0';
4315 bool RegExpParser::ParseHexEscape(int length, uc32 *value) {
4316 int start = position();
4319 for (int i = 0; !done; i++) {
4321 int d = HexValue(c);
4328 if (i == length - 1) {
4337 uc32 RegExpParser::ParseClassCharacterEscape() {
4338 ASSERT(current() == '\\');
4339 ASSERT(has_next() && !IsSpecialClassEscape(Next()));
4341 switch (current()) {
4345 // ControlEscape :: one of
4363 uc32 controlLetter = Next();
4364 uc32 letter = controlLetter & ~('A' ^ 'a');
4365 // For compatibility with JSC, inside a character class
4366 // we also accept digits and underscore as control characters.
4367 if ((controlLetter >= '0' && controlLetter <= '9') ||
4368 controlLetter == '_' ||
4369 (letter >= 'A' && letter <= 'Z')) {
4371 // Control letters mapped to ASCII control characters in the range
4373 return controlLetter & 0x1f;
4375 // We match JSC in reading the backslash as a literal
4376 // character instead of as starting an escape.
4379 case '0': case '1': case '2': case '3': case '4': case '5':
4381 // For compatibility, we interpret a decimal escape that isn't
4382 // a back reference (and therefore either \0 or not valid according
4383 // to the specification) as a 1..3 digit octal character code.
4384 return ParseOctalLiteral();
4388 if (ParseHexEscape(2, &value)) {
4391 // If \x is not followed by a two-digit hexadecimal, treat it
4392 // as an identity escape.
4398 if (ParseHexEscape(4, &value)) {
4401 // If \u is not followed by a four-digit hexadecimal, treat it
4402 // as an identity escape.
4406 // Extended identity escape. We accept any character that hasn't
4407 // been matched by a more specific case, not just the subset required
4408 // by the ECMAScript specification.
4409 uc32 result = current();
4418 CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) {
4419 ASSERT_EQ(0, *char_class);
4420 uc32 first = current();
4421 if (first == '\\') {
4423 case 'w': case 'W': case 'd': case 'D': case 's': case 'S': {
4424 *char_class = Next();
4426 return CharacterRange::Singleton(0); // Return dummy value.
4429 return ReportError(CStrVector("\\ at end of pattern"));
4431 uc32 c = ParseClassCharacterEscape(CHECK_FAILED);
4432 return CharacterRange::Singleton(c);
4436 return CharacterRange::Singleton(first);
4441 static const uc16 kNoCharClass = 0;
4443 // Adds range or pre-defined character class to character ranges.
4444 // If char_class is not kInvalidClass, it's interpreted as a class
4445 // escape (i.e., 's' means whitespace, from '\s').
4446 static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges,
4448 CharacterRange range,
4450 if (char_class != kNoCharClass) {
4451 CharacterRange::AddClassEscape(char_class, ranges, zone);
4453 ranges->Add(range, zone);
4458 RegExpTree* RegExpParser::ParseCharacterClass() {
4459 static const char* kUnterminated = "Unterminated character class";
4460 static const char* kRangeOutOfOrder = "Range out of order in character class";
4462 ASSERT_EQ(current(), '[');
4464 bool is_negated = false;
4465 if (current() == '^') {
4469 ZoneList<CharacterRange>* ranges =
4470 new(zone()) ZoneList<CharacterRange>(2, zone());
4471 while (has_more() && current() != ']') {
4472 uc16 char_class = kNoCharClass;
4473 CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED);
4474 if (current() == '-') {
4476 if (current() == kEndMarker) {
4477 // If we reach the end we break out of the loop and let the
4478 // following code report an error.
4480 } else if (current() == ']') {
4481 AddRangeOrEscape(ranges, char_class, first, zone());
4482 ranges->Add(CharacterRange::Singleton('-'), zone());
4485 uc16 char_class_2 = kNoCharClass;
4486 CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED);
4487 if (char_class != kNoCharClass || char_class_2 != kNoCharClass) {
4488 // Either end is an escaped character class. Treat the '-' verbatim.
4489 AddRangeOrEscape(ranges, char_class, first, zone());
4490 ranges->Add(CharacterRange::Singleton('-'), zone());
4491 AddRangeOrEscape(ranges, char_class_2, next, zone());
4494 if (first.from() > next.to()) {
4495 return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED);
4497 ranges->Add(CharacterRange::Range(first.from(), next.to()), zone());
4499 AddRangeOrEscape(ranges, char_class, first, zone());
4503 return ReportError(CStrVector(kUnterminated) CHECK_FAILED);
4506 if (ranges->length() == 0) {
4507 ranges->Add(CharacterRange::Everything(), zone());
4508 is_negated = !is_negated;
4510 return new(zone()) RegExpCharacterClass(ranges, is_negated);
4514 // ----------------------------------------------------------------------------
4515 // The Parser interface.
4517 ScriptData::~ScriptData() {
4518 if (owns_store_) store_.Dispose();
4522 int ScriptData::Length() {
4523 return store_.length() * sizeof(unsigned);
4527 const char* ScriptData::Data() {
4528 return reinterpret_cast<const char*>(store_.start());
4532 bool ScriptData::HasError() {
4537 void ScriptData::Initialize() {
4538 // Prepares state for use.
4539 if (store_.length() >= PreparseDataConstants::kHeaderSize) {
4540 function_index_ = PreparseDataConstants::kHeaderSize;
4541 int symbol_data_offset = PreparseDataConstants::kHeaderSize
4542 + store_[PreparseDataConstants::kFunctionsSizeOffset];
4543 if (store_.length() > symbol_data_offset) {
4544 symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]);
4546 // Partial preparse causes no symbol information.
4547 symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
4549 symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
4554 int ScriptData::ReadNumber(byte** source) {
4555 // Reads a number from symbol_data_ in base 128. The most significant
4556 // bit marks that there are more digits.
4557 // If the first byte is 0x80 (kNumberTerminator), it would normally
4558 // represent a leading zero. Since that is useless, and therefore won't
4559 // appear as the first digit of any actual value, it is used to
4560 // mark the end of the input stream.
4561 byte* data = *source;
4562 if (data >= symbol_data_end_) return -1;
4564 if (input == PreparseDataConstants::kNumberTerminator) {
4565 // End of stream marker.
4568 int result = input & 0x7f;
4570 while ((input & 0x80u) != 0) {
4571 if (data >= symbol_data_end_) return -1;
4573 result = (result << 7) | (input & 0x7f);
4581 bool RegExpParser::ParseRegExp(FlatStringReader* input,
4583 RegExpCompileData* result,
4585 ASSERT(result != NULL);
4586 RegExpParser parser(input, &result->error, multiline, zone);
4587 RegExpTree* tree = parser.ParsePattern();
4588 if (parser.failed()) {
4589 ASSERT(tree == NULL);
4590 ASSERT(!result->error.is_null());
4592 ASSERT(tree != NULL);
4593 ASSERT(result->error.is_null());
4594 result->tree = tree;
4595 int capture_count = parser.captures_started();
4596 result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
4597 result->contains_anchor = parser.contains_anchor();
4598 result->capture_count = capture_count;
4600 return !parser.failed();
4604 bool Parser::Parse() {
4605 ASSERT(info()->function() == NULL);
4606 FunctionLiteral* result = NULL;
4607 if (info()->is_lazy()) {
4608 ASSERT(!info()->is_eval());
4609 if (info()->shared_info()->is_function()) {
4610 result = ParseLazy();
4612 result = ParseProgram();
4615 SetCachedData(info()->cached_data(), info()->cached_data_mode());
4616 if (info()->cached_data_mode() == CONSUME_CACHED_DATA &&
4617 (*info()->cached_data())->has_error()) {
4618 ScriptData* cached_data = *(info()->cached_data());
4619 Scanner::Location loc = cached_data->MessageLocation();
4620 const char* message = cached_data->BuildMessage();
4621 Vector<const char*> args = cached_data->BuildArgs();
4622 ParserTraits::ReportMessageAt(loc, message, args,
4623 cached_data->IsReferenceError());
4624 DeleteArray(message);
4625 for (int i = 0; i < args.length(); i++) {
4626 DeleteArray(args[i]);
4628 DeleteArray(args.start());
4629 ASSERT(info()->isolate()->has_pending_exception());
4631 result = ParseProgram();
4634 info()->SetFunction(result);
4635 return (result != NULL);
4638 } } // namespace v8::internal