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.
8 #include "src/allocation.h"
10 #include "src/compiler.h" // For CachedDataMode
11 #include "src/preparse-data.h"
12 #include "src/preparse-data-format.h"
13 #include "src/preparser.h"
14 #include "src/scopes.h"
21 class CompilationInfo;
27 class FunctionEntry BASE_EMBEDDED {
35 kUsesSuperPropertyIndex,
39 explicit FunctionEntry(Vector<unsigned> backing)
40 : backing_(backing) { }
42 FunctionEntry() : backing_() { }
44 int start_pos() { return backing_[kStartPositionIndex]; }
45 int end_pos() { return backing_[kEndPositionIndex]; }
46 int literal_count() { return backing_[kLiteralCountIndex]; }
47 int property_count() { return backing_[kPropertyCountIndex]; }
48 LanguageMode language_mode() {
49 DCHECK(is_valid_language_mode(backing_[kLanguageModeIndex]));
50 return static_cast<LanguageMode>(backing_[kLanguageModeIndex]);
52 bool uses_super_property() { return backing_[kUsesSuperPropertyIndex]; }
54 bool is_valid() { return !backing_.is_empty(); }
57 Vector<unsigned> backing_;
61 // Wrapper around ScriptData to provide parser-specific functionality.
64 static ParseData* FromCachedData(ScriptData* cached_data) {
65 ParseData* pd = new ParseData(cached_data);
66 if (pd->IsSane()) return pd;
67 cached_data->Reject();
73 FunctionEntry GetFunctionEntry(int start);
78 unsigned* Data() { // Writable data as unsigned int array.
79 return reinterpret_cast<unsigned*>(const_cast<byte*>(script_data_->data()));
82 void Reject() { script_data_->Reject(); }
84 bool rejected() const { return script_data_->rejected(); }
87 explicit ParseData(ScriptData* script_data) : script_data_(script_data) {}
94 // Script data length is already checked to be a multiple of unsigned size.
95 return script_data_->length() / sizeof(unsigned);
98 ScriptData* script_data_;
101 DISALLOW_COPY_AND_ASSIGN(ParseData);
104 // ----------------------------------------------------------------------------
107 // A BufferedZoneList is an automatically growing list, just like (and backed
108 // by) a ZoneList, that is optimized for the case of adding and removing
109 // a single element. The last element added is stored outside the backing list,
110 // and if no more than one element is ever added, the ZoneList isn't even
112 // Elements must not be NULL pointers.
113 template <typename T, int initial_size>
114 class BufferedZoneList {
116 BufferedZoneList() : list_(NULL), last_(NULL) {}
118 // Adds element at end of list. This element is buffered and can
119 // be read using last() or removed using RemoveLast until a new Add or until
120 // RemoveLast or GetList has been called.
121 void Add(T* value, Zone* zone) {
124 list_ = new(zone) ZoneList<T*>(initial_size, zone);
126 list_->Add(last_, zone);
132 DCHECK(last_ != NULL);
137 DCHECK(last_ != NULL);
139 if ((list_ != NULL) && (list_->length() > 0))
140 last_ = list_->RemoveLast();
147 DCHECK((0 <= i) && (i < length()));
152 if (i == list_->length()) {
153 DCHECK(last_ != NULL);
167 int length = (list_ == NULL) ? 0 : list_->length();
168 return length + ((last_ == NULL) ? 0 : 1);
171 ZoneList<T*>* GetList(Zone* zone) {
173 list_ = new(zone) ZoneList<T*>(initial_size, zone);
176 list_->Add(last_, zone);
188 // Accumulates RegExp atoms and assertions into lists of terms and alternatives.
189 class RegExpBuilder: public ZoneObject {
191 explicit RegExpBuilder(Zone* zone);
192 void AddCharacter(uc16 character);
193 // "Adds" an empty expression. Does nothing except consume a
194 // following quantifier
196 void AddAtom(RegExpTree* tree);
197 void AddAssertion(RegExpTree* tree);
198 void NewAlternative(); // '|'
199 void AddQuantifierToAtom(
200 int min, int max, RegExpQuantifier::QuantifierType type);
201 RegExpTree* ToRegExp();
204 void FlushCharacters();
207 Zone* zone() const { return zone_; }
211 ZoneList<uc16>* characters_;
212 BufferedZoneList<RegExpTree, 2> terms_;
213 BufferedZoneList<RegExpTree, 2> text_;
214 BufferedZoneList<RegExpTree, 2> alternatives_;
216 enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
217 #define LAST(x) last_added_ = x;
224 class RegExpParser BASE_EMBEDDED {
226 RegExpParser(FlatStringReader* in, Handle<String>* error, bool multiline_mode,
227 bool unicode, Isolate* isolate, Zone* zone);
229 static bool ParseRegExp(Isolate* isolate, Zone* zone, FlatStringReader* input,
230 bool multiline, bool unicode,
231 RegExpCompileData* result);
233 RegExpTree* ParsePattern();
234 RegExpTree* ParseDisjunction();
235 RegExpTree* ParseGroup();
236 RegExpTree* ParseCharacterClass();
238 // Parses a {...,...} quantifier and stores the range in the given
240 bool ParseIntervalQuantifier(int* min_out, int* max_out);
242 // Parses and returns a single escaped character. The character
243 // must not be 'b' or 'B' since they are usually handle specially.
244 uc32 ParseClassCharacterEscape();
246 // Checks whether the following is a length-digit hexadecimal number,
247 // and sets the value if it is.
248 bool ParseHexEscape(int length, uc32* value);
249 bool ParseUnicodeEscape(uc32* value);
250 bool ParseUnlimitedLengthHexNumber(int max_value, uc32* value);
252 uc32 ParseOctalLiteral();
254 // Tries to parse the input as a back reference. If successful it
255 // stores the result in the output parameter and returns true. If
256 // it fails it will push back the characters read so the same characters
258 bool ParseBackReferenceIndex(int* index_out);
260 CharacterRange ParseClassAtom(uc16* char_class);
261 RegExpTree* ReportError(Vector<const char> message);
263 void Advance(int dist);
266 // Reports whether the pattern might be used as a literal search string.
267 // Only use if the result of the parse is a single atom node.
269 bool contains_anchor() { return contains_anchor_; }
270 void set_contains_anchor() { contains_anchor_ = true; }
271 int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
272 int position() { return next_pos_ - 1; }
273 bool failed() { return failed_; }
275 static bool IsSyntaxCharacter(uc32 c);
277 static const int kMaxCaptures = 1 << 16;
278 static const uc32 kEndMarker = (1 << 21);
281 enum SubexpressionType {
283 CAPTURE, // All positive values represent captures.
289 class RegExpParserState : public ZoneObject {
291 RegExpParserState(RegExpParserState* previous_state,
292 SubexpressionType group_type,
293 int disjunction_capture_index,
295 : previous_state_(previous_state),
296 builder_(new(zone) RegExpBuilder(zone)),
297 group_type_(group_type),
298 disjunction_capture_index_(disjunction_capture_index) {}
299 // Parser state of containing expression, if any.
300 RegExpParserState* previous_state() { return previous_state_; }
301 bool IsSubexpression() { return previous_state_ != NULL; }
302 // RegExpBuilder building this regexp's AST.
303 RegExpBuilder* builder() { return builder_; }
304 // Type of regexp being parsed (parenthesized group or entire regexp).
305 SubexpressionType group_type() { return group_type_; }
306 // Index in captures array of first capture in this sub-expression, if any.
307 // Also the capture index of this sub-expression itself, if group_type
309 int capture_index() { return disjunction_capture_index_; }
312 // Linked list implementation of stack of states.
313 RegExpParserState* previous_state_;
314 // Builder for the stored disjunction.
315 RegExpBuilder* builder_;
316 // Stored disjunction type (capture, look-ahead or grouping), if any.
317 SubexpressionType group_type_;
318 // Stored disjunction's capture index (if any).
319 int disjunction_capture_index_;
322 Isolate* isolate() { return isolate_; }
323 Zone* zone() const { return zone_; }
325 uc32 current() { return current_; }
326 bool has_more() { return has_more_; }
327 bool has_next() { return next_pos_ < in()->length(); }
329 FlatStringReader* in() { return in_; }
330 void ScanForCaptures();
334 Handle<String>* error_;
335 ZoneList<RegExpCapture*>* captures_;
336 FlatStringReader* in_;
339 // The capture count is only valid after we have scanned for captures.
345 bool contains_anchor_;
346 bool is_scanned_for_captures_;
350 // ----------------------------------------------------------------------------
351 // JAVASCRIPT PARSING
354 class SingletonLogger;
359 // TODO(marja): To be removed. The Traits object should contain all the data
361 typedef v8::internal::Parser* Parser;
363 typedef Variable GeneratorVariable;
365 typedef v8::internal::AstProperties AstProperties;
367 // Return types for traversing functions.
368 typedef const AstRawString* Identifier;
369 typedef v8::internal::Expression* Expression;
370 typedef Yield* YieldExpression;
371 typedef v8::internal::FunctionLiteral* FunctionLiteral;
372 typedef v8::internal::ClassLiteral* ClassLiteral;
373 typedef v8::internal::Literal* Literal;
374 typedef ObjectLiteral::Property* ObjectLiteralProperty;
375 typedef ZoneList<v8::internal::Expression*>* ExpressionList;
376 typedef ZoneList<ObjectLiteral::Property*>* PropertyList;
377 typedef ZoneList<v8::internal::Statement*>* StatementList;
379 // For constructing objects returned by the traversing functions.
380 typedef AstNodeFactory Factory;
383 explicit ParserTraits(Parser* parser) : parser_(parser) {}
385 // Helper functions for recursive descent.
386 bool IsEval(const AstRawString* identifier) const;
387 bool IsArguments(const AstRawString* identifier) const;
388 bool IsEvalOrArguments(const AstRawString* identifier) const;
389 V8_INLINE bool IsFutureStrictReserved(const AstRawString* identifier) const;
391 // Returns true if the expression is of type "this.foo".
392 static bool IsThisProperty(Expression* expression);
394 static bool IsIdentifier(Expression* expression);
396 bool IsPrototype(const AstRawString* identifier) const;
398 bool IsConstructor(const AstRawString* identifier) const;
400 static const AstRawString* AsIdentifier(Expression* expression) {
401 DCHECK(IsIdentifier(expression));
402 return expression->AsVariableProxy()->raw_name();
405 static bool IsBoilerplateProperty(ObjectLiteral::Property* property) {
406 return ObjectLiteral::IsBoilerplateProperty(property);
409 static bool IsArrayIndex(const AstRawString* string, uint32_t* index) {
410 return string->AsArrayIndex(index);
413 static Expression* GetPropertyValue(ObjectLiteral::Property* property) {
414 return property->value();
417 // Functions for encapsulating the differences between parsing and preparsing;
418 // operations interleaved with the recursive descent.
419 static void PushLiteralName(FuncNameInferrer* fni, const AstRawString* id) {
420 fni->PushLiteralName(id);
423 void PushPropertyName(FuncNameInferrer* fni, Expression* expression);
425 static void InferFunctionName(FuncNameInferrer* fni,
426 FunctionLiteral* func_to_infer) {
427 fni->AddFunction(func_to_infer);
430 static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
431 Scope* scope, ObjectLiteralProperty* property, bool* has_function) {
432 Expression* value = property->value();
433 if (scope->DeclarationScope()->is_script_scope() &&
434 value->AsFunctionLiteral() != NULL) {
435 *has_function = true;
436 value->AsFunctionLiteral()->set_pretenure();
440 // If we assign a function literal to a property we pretenure the
441 // literal so it can be added as a constant function property.
442 static void CheckAssigningFunctionLiteralToProperty(Expression* left,
445 // Keep track of eval() calls since they disable all local variable
446 // optimizations. This checks if expression is an eval call, and if yes,
447 // forwards the information to scope.
448 void CheckPossibleEvalCall(Expression* expression, Scope* scope);
450 // Determine if the expression is a variable proxy and mark it as being used
451 // in an assignment or with a increment/decrement operator.
452 static Expression* MarkExpressionAsAssigned(Expression* expression);
454 // Returns true if we have a binary expression between two numeric
455 // literals. In that case, *x will be changed to an expression which is the
457 bool ShortcutNumericLiteralBinaryExpression(Expression** x, Expression* y,
458 Token::Value op, int pos,
459 AstNodeFactory* factory);
461 // Rewrites the following types of unary expressions:
462 // not <literal> -> true / false
463 // + <numeric literal> -> <numeric literal>
464 // - <numeric literal> -> <numeric literal with value negated>
465 // ! <literal> -> true / false
466 // The following rewriting rules enable the collection of type feedback
467 // without any special stub and the multiplication is removed later in
468 // Crankshaft's canonicalization pass.
470 // - foo -> foo * (-1)
471 // ~ foo -> foo ^(~0)
472 Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
473 int pos, AstNodeFactory* factory);
475 // Generate AST node that throws a ReferenceError with the given type.
476 Expression* NewThrowReferenceError(const char* type, int pos);
478 // Generate AST node that throws a SyntaxError with the given
479 // type. The first argument may be null (in the handle sense) in
480 // which case no arguments are passed to the constructor.
481 Expression* NewThrowSyntaxError(
482 const char* type, const AstRawString* arg, int pos);
484 // Generate AST node that throws a TypeError with the given
485 // type. Both arguments must be non-null (in the handle sense).
486 Expression* NewThrowTypeError(const char* type, const AstRawString* arg,
489 // Generic AST generator for throwing errors from compiled code.
490 Expression* NewThrowError(
491 const AstRawString* constructor, const char* type,
492 const AstRawString* arg, int pos);
495 void ReportMessageAt(Scanner::Location source_location,
497 const char* arg = NULL,
498 bool is_reference_error = false);
499 void ReportMessage(const char* message,
500 const char* arg = NULL,
501 bool is_reference_error = false);
502 void ReportMessage(const char* message,
503 const AstRawString* arg,
504 bool is_reference_error = false);
505 void ReportMessageAt(Scanner::Location source_location,
507 const AstRawString* arg,
508 bool is_reference_error = false);
510 // "null" return type creators.
511 static const AstRawString* EmptyIdentifier() {
514 static Expression* EmptyExpression() {
517 static Expression* EmptyArrowParamList() { return NULL; }
518 static Literal* EmptyLiteral() {
521 static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return NULL; }
522 static FunctionLiteral* EmptyFunctionLiteral() { return NULL; }
524 // Used in error return values.
525 static ZoneList<Expression*>* NullExpressionList() {
529 // Non-NULL empty string.
530 V8_INLINE const AstRawString* EmptyIdentifierString();
532 // Odd-ball literal creators.
533 Literal* GetLiteralTheHole(int position, AstNodeFactory* factory);
535 // Producing data during the recursive descent.
536 const AstRawString* GetSymbol(Scanner* scanner);
537 const AstRawString* GetNextSymbol(Scanner* scanner);
538 const AstRawString* GetNumberAsSymbol(Scanner* scanner);
540 Expression* ThisExpression(Scope* scope, AstNodeFactory* factory,
541 int pos = RelocInfo::kNoPosition);
542 Expression* SuperReference(Scope* scope, AstNodeFactory* factory,
543 int pos = RelocInfo::kNoPosition);
544 Expression* DefaultConstructor(bool call_super, Scope* scope, int pos,
546 Literal* ExpressionFromLiteral(Token::Value token, int pos, Scanner* scanner,
547 AstNodeFactory* factory);
548 Expression* ExpressionFromIdentifier(const AstRawString* name, int pos,
549 Scope* scope, AstNodeFactory* factory);
550 Expression* ExpressionFromString(int pos, Scanner* scanner,
551 AstNodeFactory* factory);
552 Expression* GetIterator(Expression* iterable, AstNodeFactory* factory);
553 ZoneList<v8::internal::Expression*>* NewExpressionList(int size, Zone* zone) {
554 return new(zone) ZoneList<v8::internal::Expression*>(size, zone);
556 ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) {
557 return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone);
559 ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) {
560 return new(zone) ZoneList<v8::internal::Statement*>(size, zone);
562 V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type,
563 FunctionKind kind = kNormalFunction);
566 int DeclareArrowParametersFromExpression(Expression* expression, Scope* scope,
567 Scanner::Location* dupe_loc,
570 // Temporary glue; these functions will move to ParserBase.
571 Expression* ParseV8Intrinsic(bool* ok);
572 FunctionLiteral* ParseFunctionLiteral(
573 const AstRawString* name, Scanner::Location function_name_location,
574 bool name_is_strict_reserved, FunctionKind kind,
575 int function_token_position, FunctionLiteral::FunctionType type,
576 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
577 V8_INLINE void SkipLazyFunctionBody(const AstRawString* name,
578 int* materialized_literal_count,
579 int* expected_property_count, bool* ok);
580 V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody(
581 const AstRawString* name, int pos, Variable* fvar,
582 Token::Value fvar_init_op, FunctionKind kind, bool* ok);
584 ClassLiteral* ParseClassLiteral(const AstRawString* name,
585 Scanner::Location class_name_location,
586 bool name_is_strict_reserved, int pos,
589 V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope,
592 class TemplateLiteral : public ZoneObject {
594 TemplateLiteral(Zone* zone, int pos)
595 : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {}
597 const ZoneList<Expression*>* cooked() const { return &cooked_; }
598 const ZoneList<Expression*>* raw() const { return &raw_; }
599 const ZoneList<Expression*>* expressions() const { return &expressions_; }
600 int position() const { return pos_; }
602 void AddTemplateSpan(Literal* cooked, Literal* raw, int end, Zone* zone) {
603 DCHECK_NOT_NULL(cooked);
604 DCHECK_NOT_NULL(raw);
605 cooked_.Add(cooked, zone);
609 void AddExpression(Expression* expression, Zone* zone) {
610 DCHECK_NOT_NULL(expression);
611 expressions_.Add(expression, zone);
615 ZoneList<Expression*> cooked_;
616 ZoneList<Expression*> raw_;
617 ZoneList<Expression*> expressions_;
621 typedef TemplateLiteral* TemplateLiteralState;
623 V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos);
624 V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool tail);
625 V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
626 Expression* expression);
627 V8_INLINE Expression* CloseTemplateLiteral(TemplateLiteralState* state,
628 int start, Expression* tag);
629 V8_INLINE Expression* NoTemplateTag() { return NULL; }
630 V8_INLINE static bool IsTaggedTemplate(const Expression* tag) {
639 class Parser : public ParserBase<ParserTraits> {
641 Parser(CompilationInfo* info, uintptr_t stack_limit, uint32_t hash_seed,
642 UnicodeCache* unicode_cache);
644 delete reusable_preparser_;
645 reusable_preparser_ = NULL;
646 delete cached_parse_data_;
647 cached_parse_data_ = NULL;
650 // Parses the source code represented by the compilation info and sets its
651 // function literal. Returns false (and deallocates any allocated AST
652 // nodes) if parsing failed.
653 static bool ParseStatic(CompilationInfo* info, bool allow_lazy = false);
654 bool Parse(CompilationInfo* info);
655 void ParseOnBackground(CompilationInfo* info);
657 // Handle errors detected during parsing, move statistics to Isolate,
658 // internalize strings (move them to the heap).
659 void Internalize(CompilationInfo* info);
660 void HandleSourceURLComments(CompilationInfo* info);
663 friend class ParserTraits;
665 // Limit the allowed number of local variables in a function. The hard limit
666 // is that offsets computed by FullCodeGenerator::StackOperand and similar
667 // functions are ints, and they should not overflow. In addition, accessing
668 // local variables creates user-controlled constants in the generated code,
669 // and we don't want too much user-controlled memory inside the code (this was
670 // the reason why this limit was introduced in the first place; see
671 // https://codereview.chromium.org/7003030/ ).
672 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1
674 // Returns NULL if parsing failed.
675 FunctionLiteral* ParseProgram(CompilationInfo* info);
677 FunctionLiteral* ParseLazy(CompilationInfo* info);
678 FunctionLiteral* ParseLazy(CompilationInfo* info,
679 Utf16CharacterStream* source);
681 // Called by ParseProgram after setting up the scanner.
682 FunctionLiteral* DoParseProgram(CompilationInfo* info, Scope** scope,
683 Scope** ad_hoc_eval_scope);
685 void SetCachedData(CompilationInfo* info);
687 bool inside_with() const { return scope_->inside_with(); }
688 ScriptCompiler::CompileOptions compile_options() const {
689 return compile_options_;
691 bool consume_cached_parse_data() const {
692 return compile_options_ == ScriptCompiler::kConsumeParserCache &&
693 cached_parse_data_ != NULL;
695 bool produce_cached_parse_data() const {
696 return compile_options_ == ScriptCompiler::kProduceParserCache;
698 Scope* DeclarationScope(VariableMode mode) {
699 return IsLexicalVariableMode(mode)
700 ? scope_ : scope_->DeclarationScope();
703 // All ParseXXX functions take as the last argument an *ok parameter
704 // which is set to false if parsing failed; it is unchanged otherwise.
705 // By making the 'exception handling' explicit, we are forced to check
706 // for failure at the call sites.
707 void* ParseStatementList(ZoneList<Statement*>* body, int end_token,
708 bool is_eval, Scope** ad_hoc_eval_scope, bool* ok);
709 Statement* ParseStatementListItem(bool* ok);
710 Statement* ParseModule(bool* ok);
711 Statement* ParseModuleItem(bool* ok);
712 Literal* ParseModuleSpecifier(bool* ok);
713 Statement* ParseImportDeclaration(bool* ok);
714 Statement* ParseExportDeclaration(bool* ok);
715 Statement* ParseExportDefault(bool* ok);
716 void* ParseExportClause(ZoneList<const AstRawString*>* names,
717 Scanner::Location* reserved_loc, bool* ok);
718 void* ParseNamedImports(ZoneList<const AstRawString*>* names, bool* ok);
719 Statement* ParseStatement(ZoneList<const AstRawString*>* labels, bool* ok);
720 Statement* ParseSubStatement(ZoneList<const AstRawString*>* labels, bool* ok);
721 Statement* ParseFunctionDeclaration(ZoneList<const AstRawString*>* names,
723 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names,
725 Statement* ParseNativeDeclaration(bool* ok);
726 Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok);
727 Block* ParseVariableStatement(VariableDeclarationContext var_context,
728 ZoneList<const AstRawString*>* names,
730 Block* ParseVariableDeclarations(VariableDeclarationContext var_context,
731 VariableDeclarationProperties* decl_props,
732 ZoneList<const AstRawString*>* names,
733 const AstRawString** out,
735 Statement* ParseExpressionOrLabelledStatement(
736 ZoneList<const AstRawString*>* labels, bool* ok);
737 IfStatement* ParseIfStatement(ZoneList<const AstRawString*>* labels,
739 Statement* ParseContinueStatement(bool* ok);
740 Statement* ParseBreakStatement(ZoneList<const AstRawString*>* labels,
742 Statement* ParseReturnStatement(bool* ok);
743 Statement* ParseWithStatement(ZoneList<const AstRawString*>* labels,
745 CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
746 SwitchStatement* ParseSwitchStatement(ZoneList<const AstRawString*>* labels,
748 DoWhileStatement* ParseDoWhileStatement(ZoneList<const AstRawString*>* labels,
750 WhileStatement* ParseWhileStatement(ZoneList<const AstRawString*>* labels,
752 Statement* ParseForStatement(ZoneList<const AstRawString*>* labels, bool* ok);
753 Statement* ParseThrowStatement(bool* ok);
754 Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
755 TryStatement* ParseTryStatement(bool* ok);
756 DebuggerStatement* ParseDebuggerStatement(bool* ok);
758 // Support for hamony block scoped bindings.
759 Block* ParseScopedBlock(ZoneList<const AstRawString*>* labels, bool* ok);
761 // Initialize the components of a for-in / for-of statement.
762 void InitializeForEachStatement(ForEachStatement* stmt,
766 Statement* DesugarLetBindingsInForStatement(
767 Scope* inner_scope, ZoneList<const AstRawString*>* names,
768 ForStatement* loop, Statement* init, Expression* cond, Statement* next,
769 Statement* body, bool* ok);
771 FunctionLiteral* ParseFunctionLiteral(
772 const AstRawString* name, Scanner::Location function_name_location,
773 bool name_is_strict_reserved, FunctionKind kind,
774 int function_token_position, FunctionLiteral::FunctionType type,
775 FunctionLiteral::ArityRestriction arity_restriction, bool* ok);
778 ClassLiteral* ParseClassLiteral(const AstRawString* name,
779 Scanner::Location class_name_location,
780 bool name_is_strict_reserved, int pos,
783 // Magical syntax support.
784 Expression* ParseV8Intrinsic(bool* ok);
786 // Get odd-ball literals.
787 Literal* GetLiteralUndefined(int position);
789 // For harmony block scoping mode: Check if the scope has conflicting var/let
790 // declarations from different scopes. It covers for example
792 // function f() { { { var x; } let x; } }
793 // function g() { { var x; let x; } }
795 // The var declarations are hoisted to the function scope, but originate from
796 // a scope where the name has also been let bound or the var declaration is
797 // hoisted over such a scope.
798 void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
801 VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode);
802 void Declare(Declaration* declaration, bool resolve, bool* ok);
804 bool TargetStackContainsLabel(const AstRawString* label);
805 BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok);
806 IterationStatement* LookupContinueTarget(const AstRawString* label, bool* ok);
808 void AddAssertIsConstruct(ZoneList<Statement*>* body, int pos);
811 FunctionLiteral* DefaultConstructor(bool call_super, Scope* scope, int pos,
814 // Skip over a lazy function, either using cached data if we have it, or
815 // by parsing the function with PreParser. Consumes the ending }.
816 void SkipLazyFunctionBody(const AstRawString* function_name,
817 int* materialized_literal_count,
818 int* expected_property_count,
821 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser(
822 SingletonLogger* logger);
824 // Consumes the ending }.
825 ZoneList<Statement*>* ParseEagerFunctionBody(
826 const AstRawString* function_name, int pos, Variable* fvar,
827 Token::Value fvar_init_op, FunctionKind kind, bool* ok);
829 void ThrowPendingError(Isolate* isolate, Handle<Script> script);
831 TemplateLiteralState OpenTemplateLiteral(int pos);
832 void AddTemplateSpan(TemplateLiteralState* state, bool tail);
833 void AddTemplateExpression(TemplateLiteralState* state,
834 Expression* expression);
835 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start,
837 uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit);
840 PreParser* reusable_preparser_;
841 Scope* original_scope_; // for ES5 function declarations in sloppy eval
842 Target* target_stack_; // for break, continue statements
843 ScriptCompiler::CompileOptions compile_options_;
844 ParseData* cached_parse_data_;
846 bool parsing_lazy_arrow_parameters_; // for lazily parsed arrow functions.
849 bool has_pending_error_;
850 Scanner::Location pending_error_location_;
851 const char* pending_error_message_;
852 const AstRawString* pending_error_arg_;
853 const char* pending_error_char_arg_;
854 bool pending_error_is_reference_error_;
856 // Other information which will be stored in Parser and moved to Isolate after
858 int use_counts_[v8::Isolate::kUseCounterFeatureCount];
859 int total_preparse_skipped_;
860 HistogramTimer* pre_parse_timer_;
862 bool parsing_on_main_thread_;
866 bool ParserTraits::IsFutureStrictReserved(
867 const AstRawString* identifier) const {
868 return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier);
872 Scope* ParserTraits::NewScope(Scope* parent_scope, ScopeType scope_type,
874 return parser_->NewScope(parent_scope, scope_type, kind);
878 const AstRawString* ParserTraits::EmptyIdentifierString() {
879 return parser_->ast_value_factory()->empty_string();
883 void ParserTraits::SkipLazyFunctionBody(const AstRawString* function_name,
884 int* materialized_literal_count,
885 int* expected_property_count,
887 return parser_->SkipLazyFunctionBody(
888 function_name, materialized_literal_count, expected_property_count, ok);
892 ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody(
893 const AstRawString* name, int pos, Variable* fvar,
894 Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
895 return parser_->ParseEagerFunctionBody(name, pos, fvar, fvar_init_op, kind,
899 void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope,
901 parser_->CheckConflictingVarDeclarations(scope, ok);
905 // Support for handling complex values (array and object literals) that
906 // can be fully handled at compile time.
907 class CompileTimeValue: public AllStatic {
910 OBJECT_LITERAL_FAST_ELEMENTS,
911 OBJECT_LITERAL_SLOW_ELEMENTS,
915 static bool IsCompileTimeValue(Expression* expression);
917 // Get the value as a compile time value.
918 static Handle<FixedArray> GetValue(Isolate* isolate, Expression* expression);
920 // Get the type of a compile time value returned by GetValue().
921 static LiteralType GetLiteralType(Handle<FixedArray> value);
923 // Get the elements array of a compile time value returned by GetValue().
924 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
927 static const int kLiteralTypeSlot = 0;
928 static const int kElementsSlot = 1;
930 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
934 ParserTraits::TemplateLiteralState ParserTraits::OpenTemplateLiteral(int pos) {
935 return parser_->OpenTemplateLiteral(pos);
939 void ParserTraits::AddTemplateSpan(TemplateLiteralState* state, bool tail) {
940 parser_->AddTemplateSpan(state, tail);
944 void ParserTraits::AddTemplateExpression(TemplateLiteralState* state,
945 Expression* expression) {
946 parser_->AddTemplateExpression(state, expression);
950 Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state,
951 int start, Expression* tag) {
952 return parser_->CloseTemplateLiteral(state, start, tag);
954 } } // namespace v8::internal
956 #endif // V8_PARSER_H_