diff options
Diffstat (limited to 'Source/JavaScriptCore/parser')
31 files changed, 1785 insertions, 5666 deletions
diff --git a/Source/JavaScriptCore/parser/ASTBuilder.h b/Source/JavaScriptCore/parser/ASTBuilder.h index 7b587730a..8ec1fd32a 100644 --- a/Source/JavaScriptCore/parser/ASTBuilder.h +++ b/Source/JavaScriptCore/parser/ASTBuilder.h @@ -26,11 +26,8 @@ #ifndef ASTBuilder_h #define ASTBuilder_h -#include "BuiltinNames.h" -#include "BytecodeIntrinsicRegistry.h" #include "NodeConstructors.h" #include "SyntaxChecker.h" -#include "VariableEnvironment.h" #include <utility> namespace JSC { @@ -78,10 +75,10 @@ class ASTBuilder { Operator m_op; }; public: - ASTBuilder(VM* vm, ParserArena& parserArena, SourceCode* sourceCode) + ASTBuilder(VM* vm, SourceCode* sourceCode) : m_vm(vm) - , m_parserArena(parserArena) , m_sourceCode(sourceCode) + , m_scope(vm) , m_evalCount(0) { } @@ -93,6 +90,9 @@ public: UnaryExprContext(ASTBuilder&) {} }; + + typedef SyntaxChecker FunctionBodyBuilder; + typedef ExpressionNode* Expression; typedef JSC::SourceElements* SourceElements; typedef ArgumentsNode* Arguments; @@ -101,30 +101,17 @@ public: typedef PropertyListNode* PropertyList; typedef ElementNode* ElementList; typedef ArgumentListNode* ArgumentsList; -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - typedef TemplateExpressionListNode* TemplateExpressionList; - typedef TemplateStringNode* TemplateString; - typedef TemplateStringListNode* TemplateStringList; - typedef TemplateLiteralNode* TemplateLiteral; -#endif - typedef FunctionParameters* FormalParameterList; - typedef FunctionMetadataNode* FunctionBody; -#if ENABLE(ES6_CLASS_SYNTAX) - typedef ClassExprNode* ClassExpression; -#endif - typedef ModuleNameNode* ModuleName; - typedef ImportSpecifierNode* ImportSpecifier; - typedef ImportSpecifierListNode* ImportSpecifierList; - typedef ExportSpecifierNode* ExportSpecifier; - typedef ExportSpecifierListNode* ExportSpecifierList; + typedef ParameterNode* FormalParameterList; + typedef FunctionBodyNode* FunctionBody; typedef StatementNode* Statement; typedef ClauseListNode* ClauseList; typedef CaseClauseNode* Clause; + typedef ConstDeclNode* ConstDeclList; typedef std::pair<ExpressionNode*, BinaryOpInfo> BinaryOperand; - typedef DestructuringPatternNode* DestructuringPattern; - typedef ArrayPatternNode* ArrayPattern; - typedef ObjectPatternNode* ObjectPattern; - typedef BindingNode* BindingPattern; + typedef RefPtr<DeconstructionPatternNode> DeconstructionPattern; + typedef RefPtr<ArrayPatternNode> ArrayPattern; + typedef RefPtr<ObjectPatternNode> ObjectPattern; + typedef RefPtr<BindingNode> BindingPattern; static const bool CreatesAST = true; static const bool NeedsFreeVariableInfo = true; static const bool CanUseFunctionCache = true; @@ -134,12 +121,17 @@ public: ExpressionNode* makeBinaryNode(const JSTokenLocation&, int token, std::pair<ExpressionNode*, BinaryOpInfo>, std::pair<ExpressionNode*, BinaryOpInfo>); ExpressionNode* makeFunctionCallNode(const JSTokenLocation&, ExpressionNode* func, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd); - JSC::SourceElements* createSourceElements() { return new (m_parserArena) JSC::SourceElements(); } + JSC::SourceElements* createSourceElements() { return new (m_vm) JSC::SourceElements(); } - DeclarationStacks::FunctionStack& funcDeclarations() { return m_scope.m_funcDeclarations; } + ParserArenaData<DeclarationStacks::VarStack>* varDeclarations() { return m_scope.m_varDeclarations; } + ParserArenaData<DeclarationStacks::FunctionStack>* funcDeclarations() { return m_scope.m_funcDeclarations; } int features() const { return m_scope.m_features; } int numConstants() const { return m_scope.m_numConstants; } + void appendToComma(CommaNode* commaNode, ExpressionNode* expr) { commaNode->append(expr); } + + CommaNode* createCommaExpr(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* rhs) { return new (m_vm) CommaNode(location, lhs, rhs); } + ExpressionNode* makeAssignNode(const JSTokenLocation&, ExpressionNode* left, Operator, ExpressionNode* right, bool leftHasAssignments, bool rightHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); ExpressionNode* makePrefixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); ExpressionNode* makePostfixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end); @@ -164,149 +156,92 @@ public: if (expr->isNumber()) return createBoolean(location, isZeroOrUnordered(static_cast<NumberNode*>(expr)->value())); - return new (m_parserArena) LogicalNotNode(location, expr); + return new (m_vm) LogicalNotNode(location, expr); } - ExpressionNode* createUnaryPlus(const JSTokenLocation& location, ExpressionNode* expr) { return new (m_parserArena) UnaryPlusNode(location, expr); } + ExpressionNode* createUnaryPlus(const JSTokenLocation& location, ExpressionNode* expr) { return new (m_vm) UnaryPlusNode(location, expr); } ExpressionNode* createVoid(const JSTokenLocation& location, ExpressionNode* expr) { incConstants(); - return new (m_parserArena) VoidNode(location, expr); + return new (m_vm) VoidNode(location, expr); } - ExpressionNode* createThisExpr(const JSTokenLocation& location, ThisTDZMode thisTDZMode) + ExpressionNode* thisExpr(const JSTokenLocation& location) { usesThis(); - return new (m_parserArena) ThisNode(location, thisTDZMode); - } - ExpressionNode* createSuperExpr(const JSTokenLocation& location) - { - return new (m_parserArena) SuperNode(location); - } - ExpressionNode* createNewTargetExpr(const JSTokenLocation location) - { - return new (m_parserArena) NewTargetNode(location); + return new (m_vm) ThisNode(location); } ExpressionNode* createResolve(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start) { if (m_vm->propertyNames->arguments == *ident) usesArguments(); - return new (m_parserArena) ResolveNode(location, *ident, start); + return new (m_vm) ResolveNode(location, *ident, start); } - ExpressionNode* createObjectLiteral(const JSTokenLocation& location) { return new (m_parserArena) ObjectLiteralNode(location); } - ExpressionNode* createObjectLiteral(const JSTokenLocation& location, PropertyListNode* properties) { return new (m_parserArena) ObjectLiteralNode(location, properties); } + ExpressionNode* createObjectLiteral(const JSTokenLocation& location) { return new (m_vm) ObjectLiteralNode(location); } + ExpressionNode* createObjectLiteral(const JSTokenLocation& location, PropertyListNode* properties) { return new (m_vm) ObjectLiteralNode(location, properties); } ExpressionNode* createArray(const JSTokenLocation& location, int elisions) { if (elisions) incConstants(); - return new (m_parserArena) ArrayNode(location, elisions); + return new (m_vm) ArrayNode(location, elisions); } - ExpressionNode* createArray(const JSTokenLocation& location, ElementNode* elems) { return new (m_parserArena) ArrayNode(location, elems); } + ExpressionNode* createArray(const JSTokenLocation& location, ElementNode* elems) { return new (m_vm) ArrayNode(location, elems); } ExpressionNode* createArray(const JSTokenLocation& location, int elisions, ElementNode* elems) { if (elisions) incConstants(); - return new (m_parserArena) ArrayNode(location, elisions, elems); + return new (m_vm) ArrayNode(location, elisions, elems); } - ExpressionNode* createDoubleExpr(const JSTokenLocation& location, double d) + ExpressionNode* createNumberExpr(const JSTokenLocation& location, double d) { incConstants(); - return new (m_parserArena) DoubleNode(location, d); - } - ExpressionNode* createIntegerExpr(const JSTokenLocation& location, double d) - { - incConstants(); - return new (m_parserArena) IntegerNode(location, d); + return new (m_vm) NumberNode(location, d); } ExpressionNode* createString(const JSTokenLocation& location, const Identifier* string) { incConstants(); - return new (m_parserArena) StringNode(location, *string); + return new (m_vm) StringNode(location, *string); } ExpressionNode* createBoolean(const JSTokenLocation& location, bool b) { incConstants(); - return new (m_parserArena) BooleanNode(location, b); + return new (m_vm) BooleanNode(location, b); } ExpressionNode* createNull(const JSTokenLocation& location) { incConstants(); - return new (m_parserArena) NullNode(location); + return new (m_vm) NullNode(location); } ExpressionNode* createBracketAccess(const JSTokenLocation& location, ExpressionNode* base, ExpressionNode* property, bool propertyHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - BracketAccessorNode* node = new (m_parserArena) BracketAccessorNode(location, base, property, propertyHasAssignments); + BracketAccessorNode* node = new (m_vm) BracketAccessorNode(location, base, property, propertyHasAssignments); setExceptionLocation(node, start, divot, end); return node; } ExpressionNode* createDotAccess(const JSTokenLocation& location, ExpressionNode* base, const Identifier* property, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - DotAccessorNode* node = new (m_parserArena) DotAccessorNode(location, base, *property); + DotAccessorNode* node = new (m_vm) DotAccessorNode(location, base, *property); setExceptionLocation(node, start, divot, end); return node; } ExpressionNode* createSpreadExpression(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - auto node = new (m_parserArena) SpreadExpressionNode(location, expression); - setExceptionLocation(node, start, divot, end); - return node; - } - -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - TemplateStringNode* createTemplateString(const JSTokenLocation& location, const Identifier& cooked, const Identifier& raw) - { - return new (m_parserArena) TemplateStringNode(location, cooked, raw); - } - - TemplateStringListNode* createTemplateStringList(TemplateStringNode* templateString) - { - return new (m_parserArena) TemplateStringListNode(templateString); - } - - TemplateStringListNode* createTemplateStringList(TemplateStringListNode* templateStringList, TemplateStringNode* templateString) - { - return new (m_parserArena) TemplateStringListNode(templateStringList, templateString); - } - - TemplateExpressionListNode* createTemplateExpressionList(ExpressionNode* expression) - { - return new (m_parserArena) TemplateExpressionListNode(expression); - } - - TemplateExpressionListNode* createTemplateExpressionList(TemplateExpressionListNode* templateExpressionListNode, ExpressionNode* expression) - { - return new (m_parserArena) TemplateExpressionListNode(templateExpressionListNode, expression); - } - - TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList) - { - return new (m_parserArena) TemplateLiteralNode(location, templateStringList); - } - - TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList, TemplateExpressionListNode* templateExpressionList) - { - return new (m_parserArena) TemplateLiteralNode(location, templateStringList, templateExpressionList); - } - - ExpressionNode* createTaggedTemplate(const JSTokenLocation& location, ExpressionNode* base, TemplateLiteralNode* templateLiteral, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) - { - auto node = new (m_parserArena) TaggedTemplateNode(location, base, templateLiteral); + auto node = new (m_vm) SpreadExpressionNode(location, expression); setExceptionLocation(node, start, divot, end); return node; } -#endif ExpressionNode* createRegExp(const JSTokenLocation& location, const Identifier& pattern, const Identifier& flags, const JSTextPosition& start) { if (Yarr::checkSyntax(pattern.string())) return 0; - RegExpNode* node = new (m_parserArena) RegExpNode(location, pattern, flags); + RegExpNode* node = new (m_vm) RegExpNode(location, pattern, flags); int size = pattern.length() + 2; // + 2 for the two /'s JSTextPosition end = start + size; setExceptionLocation(node, start, end, end); @@ -315,236 +250,201 @@ public: ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, ArgumentsNode* arguments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - NewExprNode* node = new (m_parserArena) NewExprNode(location, expr, arguments); + NewExprNode* node = new (m_vm) NewExprNode(location, expr, arguments); setExceptionLocation(node, start, divot, end); return node; } ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end) { - NewExprNode* node = new (m_parserArena) NewExprNode(location, expr); + NewExprNode* node = new (m_vm) NewExprNode(location, expr); setExceptionLocation(node, start, end, end); return node; } ExpressionNode* createConditionalExpr(const JSTokenLocation& location, ExpressionNode* condition, ExpressionNode* lhs, ExpressionNode* rhs) { - return new (m_parserArena) ConditionalNode(location, condition, lhs, rhs); + return new (m_vm) ConditionalNode(location, condition, lhs, rhs); } - ExpressionNode* createAssignResolve(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* rhs, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end, AssignmentContext assignmentContext) + ExpressionNode* createAssignResolve(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* rhs, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { if (rhs->isFuncExprNode()) - static_cast<FuncExprNode*>(rhs)->metadata()->setInferredName(ident); - AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, ident, rhs, assignmentContext); + static_cast<FuncExprNode*>(rhs)->body()->setInferredName(ident); + AssignResolveNode* node = new (m_vm) AssignResolveNode(location, ident, rhs); setExceptionLocation(node, start, divot, end); return node; } -#if ENABLE(ES6_CLASS_SYNTAX) - ClassExprNode* createClassExpr(const JSTokenLocation& location, const Identifier& name, ExpressionNode* constructor, - ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods) - { - return new (m_parserArena) ClassExprNode(location, name, constructor, parentClass, instanceMethods, staticMethods); - } -#endif - - ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo) + ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const Identifier* name, FunctionBodyNode* body, ParameterNode* parameters, unsigned openBraceOffset, unsigned closeBraceOffset, int bodyStartLine, int bodyEndLine, unsigned startColumn) { - FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body, - m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn)); - functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); + FuncExprNode* result = new (m_vm) FuncExprNode(location, *name, body, m_sourceCode->subExpression(openBraceOffset, closeBraceOffset, bodyStartLine, startColumn), parameters); + body->setLoc(bodyStartLine, bodyEndLine, location.startOffset, location.lineStartOffset); return result; } - FunctionMetadataNode* createFunctionMetadata( - const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, - unsigned startColumn, unsigned endColumn, int functionKeywordStart, - int functionNameStart, int parametersStart, bool inStrictContext, - ConstructorKind constructorKind, unsigned parameterCount, SourceParseMode mode) + FunctionBodyNode* createFunctionBody(const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, bool inStrictContext) { - return new (m_parserArena) FunctionMetadataNode( - m_parserArena, startLocation, endLocation, startColumn, endColumn, - functionKeywordStart, functionNameStart, parametersStart, - inStrictContext, constructorKind, parameterCount, mode); + return FunctionBodyNode::create(m_vm, startLocation, endLocation, startColumn, endColumn, inStrictContext); } - ExpressionNode* createArrowFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo) + void setFunctionNameStart(FunctionBodyNode* body, int functionNameStart) { - SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); - - FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body, source); - functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); - return result; + body->setFunctionNameStart(functionNameStart); } - - NEVER_INLINE PropertyNode* createGetterOrSetterProperty(const JSTokenLocation& location, PropertyNode::Type type, bool, - const Identifier* name, const ParserFunctionInfo<ASTBuilder>& functionInfo, SuperBinding superBinding) + + NEVER_INLINE PropertyNode* createGetterOrSetterProperty(const JSTokenLocation& location, PropertyNode::Type type, bool, const Identifier* name, ParameterNode* params, FunctionBodyNode* body, unsigned openBraceOffset, unsigned closeBraceOffset, int bodyStartLine, int bodyEndLine, unsigned bodyStartColumn) { ASSERT(name); - functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); - functionInfo.body->setInferredName(*name); - SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); - FuncExprNode* funcExpr = new (m_parserArena) FuncExprNode(location, m_vm->propertyNames->nullIdentifier, functionInfo.body, source); - return new (m_parserArena) PropertyNode(*name, funcExpr, type, PropertyNode::Unknown, superBinding); + body->setLoc(bodyStartLine, bodyEndLine, location.startOffset, location.lineStartOffset); + body->setInferredName(*name); + return new (m_vm) PropertyNode(m_vm, *name, new (m_vm) FuncExprNode(location, m_vm->propertyNames->nullIdentifier, body, m_sourceCode->subExpression(openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn), params), type); } - NEVER_INLINE PropertyNode* createGetterOrSetterProperty(VM* vm, ParserArena& parserArena, const JSTokenLocation& location, PropertyNode::Type type, bool, - double name, const ParserFunctionInfo<ASTBuilder>& functionInfo, SuperBinding superBinding) + NEVER_INLINE PropertyNode* createGetterOrSetterProperty(VM*, const JSTokenLocation& location, PropertyNode::Type type, bool, double name, ParameterNode* params, FunctionBodyNode* body, unsigned openBraceOffset, unsigned closeBraceOffset, int bodyStartLine, int bodyEndLine, unsigned bodyStartColumn) { - functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); - const Identifier& ident = parserArena.identifierArena().makeNumericIdentifier(vm, name); - SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn); - FuncExprNode* funcExpr = new (m_parserArena) FuncExprNode(location, vm->propertyNames->nullIdentifier, functionInfo.body, source); - return new (m_parserArena) PropertyNode(ident, funcExpr, type, PropertyNode::Unknown, superBinding); + body->setLoc(bodyStartLine, bodyEndLine, location.startOffset, location.lineStartOffset); + return new (m_vm) PropertyNode(m_vm, name, new (m_vm) FuncExprNode(location, m_vm->propertyNames->nullIdentifier, body, m_sourceCode->subExpression(openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn), params), type); } - ArgumentsNode* createArguments() { return new (m_parserArena) ArgumentsNode(); } - ArgumentsNode* createArguments(ArgumentListNode* args) { return new (m_parserArena) ArgumentsNode(args); } - ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, arg); } - ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ArgumentListNode* args, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, args, arg); } + ArgumentsNode* createArguments() { return new (m_vm) ArgumentsNode(); } + ArgumentsNode* createArguments(ArgumentListNode* args) { return new (m_vm) ArgumentsNode(args); } + ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ExpressionNode* arg) { return new (m_vm) ArgumentListNode(location, arg); } + ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ArgumentListNode* args, ExpressionNode* arg) { return new (m_vm) ArgumentListNode(location, args, arg); } - PropertyNode* createProperty(const Identifier* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding = SuperBinding::NotNeeded) + PropertyNode* createProperty(const Identifier* propertyName, ExpressionNode* node, PropertyNode::Type type, bool) { if (node->isFuncExprNode()) - static_cast<FuncExprNode*>(node)->metadata()->setInferredName(*propertyName); - return new (m_parserArena) PropertyNode(*propertyName, node, type, putType, superBinding); - } - PropertyNode* createProperty(VM* vm, ParserArena& parserArena, double propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool) - { - return new (m_parserArena) PropertyNode(parserArena.identifierArena().makeNumericIdentifier(vm, propertyName), node, type, putType); + static_cast<FuncExprNode*>(node)->body()->setInferredName(*propertyName); + return new (m_vm) PropertyNode(m_vm, *propertyName, node, type); } - PropertyNode* createProperty(ExpressionNode* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool) { return new (m_parserArena) PropertyNode(propertyName, node, type, putType); } - PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property) { return new (m_parserArena) PropertyListNode(location, property); } - PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property, PropertyListNode* tail) { return new (m_parserArena) PropertyListNode(location, property, tail); } + PropertyNode* createProperty(VM*, double propertyName, ExpressionNode* node, PropertyNode::Type type, bool) { return new (m_vm) PropertyNode(m_vm, propertyName, node, type); } + PropertyNode* createProperty(VM*, ExpressionNode* propertyName, ExpressionNode* node, PropertyNode::Type type, bool) { return new (m_vm) PropertyNode(m_vm, propertyName, node, type); } + PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property) { return new (m_vm) PropertyListNode(location, property); } + PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property, PropertyListNode* tail) { return new (m_vm) PropertyListNode(location, property, tail); } - ElementNode* createElementList(int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elisions, expr); } - ElementNode* createElementList(ElementNode* elems, int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elems, elisions, expr); } + ElementNode* createElementList(int elisions, ExpressionNode* expr) { return new (m_vm) ElementNode(elisions, expr); } + ElementNode* createElementList(ElementNode* elems, int elisions, ExpressionNode* expr) { return new (m_vm) ElementNode(elems, elisions, expr); } - FormalParameterList createFormalParameterList() { return new (m_parserArena) FunctionParameters(); } - void appendParameter(FormalParameterList list, DestructuringPattern pattern, ExpressionNode* defaultValue) - { - list->append(pattern, defaultValue); - } + ParameterNode* createFormalParameterList(DeconstructionPattern pattern) { return new (m_vm) ParameterNode(pattern); } + ParameterNode* createFormalParameterList(ParameterNode* list, DeconstructionPattern pattern) { return new (m_vm) ParameterNode(list, pattern); } - CaseClauseNode* createClause(ExpressionNode* expr, JSC::SourceElements* statements) { return new (m_parserArena) CaseClauseNode(expr, statements); } - ClauseListNode* createClauseList(CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(clause); } - ClauseListNode* createClauseList(ClauseListNode* tail, CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(tail, clause); } + CaseClauseNode* createClause(ExpressionNode* expr, JSC::SourceElements* statements) { return new (m_vm) CaseClauseNode(expr, statements); } + ClauseListNode* createClauseList(CaseClauseNode* clause) { return new (m_vm) ClauseListNode(clause); } + ClauseListNode* createClauseList(ClauseListNode* tail, CaseClauseNode* clause) { return new (m_vm) ClauseListNode(tail, clause); } - StatementNode* createFuncDeclStatement(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo) - { - FuncDeclNode* decl = new (m_parserArena) FuncDeclNode(location, *functionInfo.name, functionInfo.body, - m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.bodyStartColumn)); - if (*functionInfo.name == m_vm->propertyNames->arguments) - usesArguments(); - m_scope.m_funcDeclarations.append(decl->metadata()); - functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset); - return decl; - } + void setUsesArguments(FunctionBodyNode* node) { node->setUsesArguments(); } -#if ENABLE(ES6_CLASS_SYNTAX) - StatementNode* createClassDeclStatement(const JSTokenLocation& location, ClassExprNode* classExpression, - const JSTextPosition& classStart, const JSTextPosition& classEnd, unsigned startLine, unsigned endLine) + StatementNode* createFuncDeclStatement(const JSTokenLocation& location, const Identifier* name, FunctionBodyNode* body, ParameterNode* parameters, unsigned openBraceOffset, unsigned closeBraceOffset, int bodyStartLine, int bodyEndLine, unsigned bodyStartColumn) { - ExpressionNode* assign = createAssignResolve(location, classExpression->name(), classExpression, classStart, classStart + 1, classEnd, AssignmentContext::DeclarationStatement); - ClassDeclNode* decl = new (m_parserArena) ClassDeclNode(location, assign); - decl->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + FuncDeclNode* decl = new (m_vm) FuncDeclNode(location, *name, body, m_sourceCode->subExpression(openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn), parameters); + if (*name == m_vm->propertyNames->arguments) + usesArguments(); + m_scope.m_funcDeclarations->data.append(decl->body()); + body->setLoc(bodyStartLine, bodyEndLine, location.startOffset, location.lineStartOffset); return decl; } -#endif - StatementNode* createBlockStatement(const JSTokenLocation& location, JSC::SourceElements* elements, int startLine, int endLine, VariableEnvironment& lexicalVariables) + StatementNode* createBlockStatement(const JSTokenLocation& location, JSC::SourceElements* elements, int startLine, int endLine) { - BlockNode* block = new (m_parserArena) BlockNode(location, elements, lexicalVariables); + BlockNode* block = new (m_vm) BlockNode(location, elements); block->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return block; } StatementNode* createExprStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, int end) { - ExprStatementNode* result = new (m_parserArena) ExprStatementNode(location, expr); + ExprStatementNode* result = new (m_vm) ExprStatementNode(location, expr); result->setLoc(start.line, end, start.offset, start.lineStartOffset); return result; } StatementNode* createIfStatement(const JSTokenLocation& location, ExpressionNode* condition, StatementNode* trueBlock, StatementNode* falseBlock, int start, int end) { - IfElseNode* result = new (m_parserArena) IfElseNode(location, condition, trueBlock, falseBlock); + IfElseNode* result = new (m_vm) IfElseNode(location, condition, trueBlock, falseBlock); result->setLoc(start, end, location.startOffset, location.lineStartOffset); return result; } - StatementNode* createForLoop(const JSTokenLocation& location, ExpressionNode* initializer, ExpressionNode* condition, ExpressionNode* iter, StatementNode* statements, int start, int end, VariableEnvironment& lexicalVariables) + StatementNode* createForLoop(const JSTokenLocation& location, ExpressionNode* initializer, ExpressionNode* condition, ExpressionNode* iter, StatementNode* statements, int start, int end) { - ForNode* result = new (m_parserArena) ForNode(location, initializer, condition, iter, statements, lexicalVariables); + ForNode* result = new (m_vm) ForNode(location, initializer, condition, iter, statements); result->setLoc(start, end, location.startOffset, location.lineStartOffset); return result; } - StatementNode* createForInLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + StatementNode* createForInLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end) { - ForInNode* result = new (m_parserArena) ForInNode(location, lhs, iter, statements, lexicalVariables); + ForInNode* result = new (m_vm) ForInNode(location, lhs, iter, statements); result->setLoc(start, end, location.startOffset, location.lineStartOffset); setExceptionLocation(result, eStart, eDivot, eEnd); return result; } - StatementNode* createForInLoop(const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + StatementNode* createForInLoop(const JSTokenLocation& location, PassRefPtr<DeconstructionPatternNode> pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end) { - auto lexpr = new (m_parserArena) DestructuringAssignmentNode(location, pattern, 0); - return createForInLoop(location, lexpr, iter, statements, eStart, eDivot, eEnd, start, end, lexicalVariables); + ForInNode* result = new (m_vm) ForInNode(m_vm, location, pattern.get(), iter, statements); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + setExceptionLocation(result, eStart, eDivot, eEnd); + return result; } - StatementNode* createForOfLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + StatementNode* createForOfLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end) { - ForOfNode* result = new (m_parserArena) ForOfNode(location, lhs, iter, statements, lexicalVariables); + ForOfNode* result = new (m_vm) ForOfNode(location, lhs, iter, statements); result->setLoc(start, end, location.startOffset, location.lineStartOffset); setExceptionLocation(result, eStart, eDivot, eEnd); return result; } - StatementNode* createForOfLoop(const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables) + StatementNode* createForOfLoop(const JSTokenLocation& location, PassRefPtr<DeconstructionPatternNode> pattern, ExpressionNode* iter, StatementNode* statements, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end) { - auto lexpr = new (m_parserArena) DestructuringAssignmentNode(location, pattern, 0); - return createForOfLoop(location, lexpr, iter, statements, eStart, eDivot, eEnd, start, end, lexicalVariables); - } - - bool isBindingNode(const DestructuringPattern& pattern) - { - return pattern->isBindingNode(); + ForOfNode* result = new (m_vm) ForOfNode(m_vm, location, pattern.get(), iter, statements); + result->setLoc(start, end, location.startOffset, location.lineStartOffset); + setExceptionLocation(result, eStart, eDivot, eEnd); + return result; } - StatementNode* createEmptyStatement(const JSTokenLocation& location) { return new (m_parserArena) EmptyStatementNode(location); } + StatementNode* createEmptyStatement(const JSTokenLocation& location) { return new (m_vm) EmptyStatementNode(location); } - StatementNode* createDeclarationStatement(const JSTokenLocation& location, ExpressionNode* expr, int start, int end) + StatementNode* createVarStatement(const JSTokenLocation& location, ExpressionNode* expr, int start, int end) { StatementNode* result; - result = new (m_parserArena) DeclarationStatement(location, expr); + if (!expr) + result = new (m_vm) EmptyStatementNode(location); + else + result = new (m_vm) VarStatementNode(location, expr); result->setLoc(start, end, location.startOffset, location.lineStartOffset); return result; } - ExpressionNode* createEmptyVarExpression(const JSTokenLocation& location, const Identifier& identifier) + StatementNode* createReturnStatement(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& end) { - return new (m_parserArena) EmptyVarExpression(location, identifier); + ReturnNode* result = new (m_vm) ReturnNode(location, expression); + setExceptionLocation(result, start, end, end); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + return result; } - ExpressionNode* createEmptyLetExpression(const JSTokenLocation& location, const Identifier& identifier) + StatementNode* createBreakStatement(const JSTokenLocation& location, const JSTextPosition& start, const JSTextPosition& end) { - return new (m_parserArena) EmptyLetExpression(location, identifier); + BreakNode* result = new (m_vm) BreakNode(m_vm, location); + setExceptionLocation(result, start, end, end); + result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); + return result; } - StatementNode* createReturnStatement(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& end) + StatementNode* createBreakStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) { - ReturnNode* result = new (m_parserArena) ReturnNode(location, expression); + BreakNode* result = new (m_vm) BreakNode(location, *ident); setExceptionLocation(result, start, end, end); result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); return result; } - StatementNode* createBreakStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) + StatementNode* createContinueStatement(const JSTokenLocation& location, const JSTextPosition& start, const JSTextPosition& end) { - BreakNode* result = new (m_parserArena) BreakNode(location, *ident); + ContinueNode* result = new (m_vm) ContinueNode(m_vm, location); setExceptionLocation(result, start, end, end); result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); return result; @@ -552,46 +452,46 @@ public: StatementNode* createContinueStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) { - ContinueNode* result = new (m_parserArena) ContinueNode(location, *ident); + ContinueNode* result = new (m_vm) ContinueNode(location, *ident); setExceptionLocation(result, start, end, end); result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); return result; } - StatementNode* createTryStatement(const JSTokenLocation& location, StatementNode* tryBlock, const Identifier* ident, StatementNode* catchBlock, StatementNode* finallyBlock, int startLine, int endLine, VariableEnvironment& catchEnvironment) + StatementNode* createTryStatement(const JSTokenLocation& location, StatementNode* tryBlock, const Identifier* ident, StatementNode* catchBlock, StatementNode* finallyBlock, int startLine, int endLine) { - TryNode* result = new (m_parserArena) TryNode(location, tryBlock, *ident, catchBlock, catchEnvironment, finallyBlock); + TryNode* result = new (m_vm) TryNode(location, tryBlock, *ident, catchBlock, finallyBlock); if (catchBlock) usesCatch(); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } - StatementNode* createSwitchStatement(const JSTokenLocation& location, ExpressionNode* expr, ClauseListNode* firstClauses, CaseClauseNode* defaultClause, ClauseListNode* secondClauses, int startLine, int endLine, VariableEnvironment& lexicalVariables) + StatementNode* createSwitchStatement(const JSTokenLocation& location, ExpressionNode* expr, ClauseListNode* firstClauses, CaseClauseNode* defaultClause, ClauseListNode* secondClauses, int startLine, int endLine) { - CaseBlockNode* cases = new (m_parserArena) CaseBlockNode(firstClauses, defaultClause, secondClauses); - SwitchNode* result = new (m_parserArena) SwitchNode(location, expr, cases, lexicalVariables); + CaseBlockNode* cases = new (m_vm) CaseBlockNode(firstClauses, defaultClause, secondClauses); + SwitchNode* result = new (m_vm) SwitchNode(location, expr, cases); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } StatementNode* createWhileStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, int startLine, int endLine) { - WhileNode* result = new (m_parserArena) WhileNode(location, expr, statement); + WhileNode* result = new (m_vm) WhileNode(location, expr, statement); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } StatementNode* createDoWhileStatement(const JSTokenLocation& location, StatementNode* statement, ExpressionNode* expr, int startLine, int endLine) { - DoWhileNode* result = new (m_parserArena) DoWhileNode(location, statement, expr); + DoWhileNode* result = new (m_vm) DoWhileNode(location, statement, expr); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } StatementNode* createLabelStatement(const JSTokenLocation& location, const Identifier* ident, StatementNode* statement, const JSTextPosition& start, const JSTextPosition& end) { - LabelNode* result = new (m_parserArena) LabelNode(location, *ident, statement); + LabelNode* result = new (m_vm) LabelNode(location, *ident, statement); setExceptionLocation(result, start, end, end); return result; } @@ -599,14 +499,14 @@ public: StatementNode* createWithStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, unsigned start, const JSTextPosition& end, unsigned startLine, unsigned endLine) { usesWith(); - WithNode* result = new (m_parserArena) WithNode(location, expr, statement, end, end - start); + WithNode* result = new (m_vm) WithNode(location, expr, statement, end, end - start); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } StatementNode* createThrowStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end) { - ThrowNode* result = new (m_parserArena) ThrowNode(location, expr); + ThrowNode* result = new (m_vm) ThrowNode(location, expr); result->setLoc(start.line, end.line, start.offset, start.lineStartOffset); setExceptionLocation(result, start, end, end); return result; @@ -614,69 +514,24 @@ public: StatementNode* createDebugger(const JSTokenLocation& location, int startLine, int endLine) { - DebuggerStatementNode* result = new (m_parserArena) DebuggerStatementNode(location); + DebuggerStatementNode* result = new (m_vm) DebuggerStatementNode(location); result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); return result; } - - ModuleNameNode* createModuleName(const JSTokenLocation& location, const Identifier& moduleName) - { - return new (m_parserArena) ModuleNameNode(location, moduleName); - } - - ImportSpecifierNode* createImportSpecifier(const JSTokenLocation& location, const Identifier& importedName, const Identifier& localName) - { - return new (m_parserArena) ImportSpecifierNode(location, importedName, localName); - } - - ImportSpecifierListNode* createImportSpecifierList() - { - return new (m_parserArena) ImportSpecifierListNode(); - } - - void appendImportSpecifier(ImportSpecifierListNode* specifierList, ImportSpecifierNode* specifier) - { - specifierList->append(specifier); - } - - StatementNode* createImportDeclaration(const JSTokenLocation& location, ImportSpecifierListNode* importSpecifierList, ModuleNameNode* moduleName) - { - return new (m_parserArena) ImportDeclarationNode(location, importSpecifierList, moduleName); - } - - StatementNode* createExportAllDeclaration(const JSTokenLocation& location, ModuleNameNode* moduleName) - { - return new (m_parserArena) ExportAllDeclarationNode(location, moduleName); - } - - StatementNode* createExportDefaultDeclaration(const JSTokenLocation& location, StatementNode* declaration, const Identifier& localName) - { - return new (m_parserArena) ExportDefaultDeclarationNode(location, declaration, localName); - } - - StatementNode* createExportLocalDeclaration(const JSTokenLocation& location, StatementNode* declaration) - { - return new (m_parserArena) ExportLocalDeclarationNode(location, declaration); - } - - StatementNode* createExportNamedDeclaration(const JSTokenLocation& location, ExportSpecifierListNode* exportSpecifierList, ModuleNameNode* moduleName) - { - return new (m_parserArena) ExportNamedDeclarationNode(location, exportSpecifierList, moduleName); - } - - ExportSpecifierNode* createExportSpecifier(const JSTokenLocation& location, const Identifier& localName, const Identifier& exportedName) - { - return new (m_parserArena) ExportSpecifierNode(location, localName, exportedName); - } - - ExportSpecifierListNode* createExportSpecifierList() + + StatementNode* createConstStatement(const JSTokenLocation& location, ConstDeclNode* decls, int startLine, int endLine) { - return new (m_parserArena) ExportSpecifierListNode(); + ConstStatementNode* result = new (m_vm) ConstStatementNode(location, decls); + result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset); + return result; } - void appendExportSpecifier(ExportSpecifierListNode* specifierList, ExportSpecifierNode* specifier) + ConstDeclNode* appendConstDecl(const JSTokenLocation& location, ConstDeclNode* tail, const Identifier* name, ExpressionNode* initializer) { - specifierList->append(specifier); + ConstDeclNode* result = new (m_vm) ConstDeclNode(location, *name, initializer); + if (tail) + tail->m_next = result; + return result; } void appendStatement(JSC::SourceElements* elements, JSC::StatementNode* statement) @@ -684,17 +539,23 @@ public: elements->append(statement); } - CommaNode* createCommaExpr(const JSTokenLocation& location, ExpressionNode* node) + void addVar(const Identifier* ident, int attrs) { - return new (m_parserArena) CommaNode(location, node); + if (m_vm->propertyNames->arguments == *ident) + usesArguments(); + ASSERT(ident->impl()->isIdentifier()); + m_scope.m_varDeclarations->data.append(std::make_pair(*ident, attrs)); } - CommaNode* appendToCommaExpr(const JSTokenLocation& location, ExpressionNode*, ExpressionNode* tail, ExpressionNode* next) + ExpressionNode* combineCommaNodes(const JSTokenLocation& location, ExpressionNode* list, ExpressionNode* init) { - ASSERT(tail->isCommaNode()); - CommaNode* newTail = new (m_parserArena) CommaNode(location, next); - static_cast<CommaNode*>(tail)->setNext(newTail); - return newTail; + if (!list) + return init; + if (list->isCommaNode()) { + static_cast<CommaNode*>(list)->append(init); + return list; + } + return new (m_vm) CommaNode(location, list, init); } int evalCount() const { return m_evalCount; } @@ -777,85 +638,58 @@ public: assignmentStackDepth--; return result; } - - const Identifier* getName(const Property& property) const { return property->name(); } - PropertyNode::Type getType(const Property& property) const { return property->type(); } + + const Identifier* getName(Property property) const { return property->name(); } + PropertyNode::Type getType(Property property) const { return property->type(); } bool isResolve(ExpressionNode* expr) const { return expr->isResolveNode(); } - ExpressionNode* createDestructuringAssignment(const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* initializer) + ExpressionNode* createDeconstructingAssignment(const JSTokenLocation& location, PassRefPtr<DeconstructionPatternNode> pattern, ExpressionNode* initializer) { - return new (m_parserArena) DestructuringAssignmentNode(location, pattern, initializer); + return new (m_vm) DeconstructingAssignmentNode(location, pattern.get(), initializer); } ArrayPattern createArrayPattern(const JSTokenLocation&) { - return new (m_parserArena) ArrayPatternNode(); + return ArrayPatternNode::create(m_vm); } void appendArrayPatternSkipEntry(ArrayPattern node, const JSTokenLocation& location) { - node->appendIndex(ArrayPatternNode::BindingType::Elision, location, 0, nullptr); - } - - void appendArrayPatternEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* defaultValue) - { - node->appendIndex(ArrayPatternNode::BindingType::Element, location, pattern, defaultValue); - } - - void appendArrayPatternRestEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern) - { - node->appendIndex(ArrayPatternNode::BindingType::RestElement, location, pattern, nullptr); + node->appendIndex(location, 0); } - void finishArrayPattern(ArrayPattern node, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd) + void appendArrayPatternEntry(ArrayPattern node, const JSTokenLocation& location, DeconstructionPattern pattern) { - setExceptionLocation(node, divotStart, divot, divotEnd); + node->appendIndex(location, pattern.get()); } ObjectPattern createObjectPattern(const JSTokenLocation&) { - return new (m_parserArena) ObjectPatternNode(); + return ObjectPatternNode::create(m_vm); } - void appendObjectPatternEntry(ObjectPattern node, const JSTokenLocation& location, bool wasString, const Identifier& identifier, DestructuringPattern pattern, ExpressionNode* defaultValue) + void appendObjectPatternEntry(ObjectPattern node, const JSTokenLocation& location, bool wasString, const Identifier& identifier, DeconstructionPattern pattern) { - node->appendEntry(location, identifier, wasString, pattern, defaultValue); + node->appendEntry(location, identifier, wasString, pattern.get()); } - BindingPattern createBindingLocation(const JSTokenLocation&, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext context) - { - return new (m_parserArena) BindingNode(boundProperty, start, end, context); - } - - void setEndOffset(Node* node, int offset) - { - node->setEndOffset(offset); - } - - int endOffset(Node* node) - { - return node->endOffset(); - } - - void setStartOffset(CaseClauseNode* node, int offset) - { - node->setStartOffset(offset); - } - - void setStartOffset(Node* node, int offset) + BindingPattern createBindingLocation(const JSTokenLocation&, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end) { - node->setStartOffset(offset); + return BindingNode::create(m_vm, boundProperty, start, end); } private: struct Scope { - Scope() - : m_features(0) + Scope(VM* vm) + : m_varDeclarations(new (vm) ParserArenaData<DeclarationStacks::VarStack>) + , m_funcDeclarations(new (vm) ParserArenaData<DeclarationStacks::FunctionStack>) + , m_features(0) , m_numConstants(0) { } - DeclarationStacks::FunctionStack m_funcDeclarations; + ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations; + ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations; int m_features; int m_numConstants; }; @@ -876,29 +710,12 @@ private: m_evalCount++; m_scope.m_features |= EvalFeature; } - ExpressionNode* createIntegerLikeNumber(const JSTokenLocation& location, double d) + ExpressionNode* createNumber(const JSTokenLocation& location, double d) { - return new (m_parserArena) IntegerNode(location, d); + return new (m_vm) NumberNode(location, d); } - ExpressionNode* createDoubleLikeNumber(const JSTokenLocation& location, double d) - { - return new (m_parserArena) DoubleNode(location, d); - } - ExpressionNode* createNumberFromBinaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNodeA, const NumberNode& originalNodeB) - { - if (originalNodeA.isIntegerNode() && originalNodeB.isIntegerNode()) - return createIntegerLikeNumber(location, value); - return createDoubleLikeNumber(location, value); - } - ExpressionNode* createNumberFromUnaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNode) - { - if (originalNode.isIntegerNode()) - return createIntegerLikeNumber(location, value); - return createDoubleLikeNumber(location, value); - } - + VM* m_vm; - ParserArena& m_parserArena; SourceCode* m_sourceCode; Scope m_scope; Vector<BinaryOperand, 10, UnsafeVectorOverflow> m_binaryOperandStack; @@ -912,43 +729,44 @@ ExpressionNode* ASTBuilder::makeTypeOfNode(const JSTokenLocation& location, Expr { if (expr->isResolveNode()) { ResolveNode* resolve = static_cast<ResolveNode*>(expr); - return new (m_parserArena) TypeOfResolveNode(location, resolve->identifier()); + return new (m_vm) TypeOfResolveNode(location, resolve->identifier()); } - return new (m_parserArena) TypeOfValueNode(location, expr); + return new (m_vm) TypeOfValueNode(location, expr); } ExpressionNode* ASTBuilder::makeDeleteNode(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { if (!expr->isLocation()) - return new (m_parserArena) DeleteValueNode(location, expr); + return new (m_vm) DeleteValueNode(location, expr); if (expr->isResolveNode()) { ResolveNode* resolve = static_cast<ResolveNode*>(expr); - return new (m_parserArena) DeleteResolveNode(location, resolve->identifier(), divot, start, end); + return new (m_vm) DeleteResolveNode(location, resolve->identifier(), divot, start, end); } if (expr->isBracketAccessorNode()) { BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr); - return new (m_parserArena) DeleteBracketNode(location, bracket->base(), bracket->subscript(), divot, start, end); + return new (m_vm) DeleteBracketNode(location, bracket->base(), bracket->subscript(), divot, start, end); } ASSERT(expr->isDotAccessorNode()); DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr); - return new (m_parserArena) DeleteDotNode(location, dot->base(), dot->identifier(), divot, start, end); + return new (m_vm) DeleteDotNode(location, dot->base(), dot->identifier(), divot, start, end); } ExpressionNode* ASTBuilder::makeNegateNode(const JSTokenLocation& location, ExpressionNode* n) { if (n->isNumber()) { - const NumberNode& numberNode = static_cast<const NumberNode&>(*n); - return createNumberFromUnaryOperation(location, -numberNode.value(), numberNode); + NumberNode* numberNode = static_cast<NumberNode*>(n); + numberNode->setValue(-numberNode->value()); + return numberNode; } - return new (m_parserArena) NegateNode(location, n); + return new (m_vm) NegateNode(location, n); } ExpressionNode* ASTBuilder::makeBitwiseNotNode(const JSTokenLocation& location, ExpressionNode* expr) { if (expr->isNumber()) - return createIntegerLikeNumber(location, ~toInt32(static_cast<NumberNode*>(expr)->value())); - return new (m_parserArena) BitwiseNotNode(location, expr); + return createNumber(location, ~toInt32(static_cast<NumberNode*>(expr)->value())); + return new (m_vm) BitwiseNotNode(location, expr); } ExpressionNode* ASTBuilder::makeMultNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) @@ -956,19 +774,16 @@ ExpressionNode* ASTBuilder::makeMultNode(const JSTokenLocation& location, Expres expr1 = expr1->stripUnaryPlus(); expr2 = expr2->stripUnaryPlus(); - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createNumberFromBinaryOperation(location, numberExpr1.value() * numberExpr2.value(), numberExpr1, numberExpr2); - } + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, static_cast<NumberNode*>(expr1)->value() * static_cast<NumberNode*>(expr2)->value()); if (expr1->isNumber() && static_cast<NumberNode*>(expr1)->value() == 1) - return new (m_parserArena) UnaryPlusNode(location, expr2); + return new (m_vm) UnaryPlusNode(location, expr2); if (expr2->isNumber() && static_cast<NumberNode*>(expr2)->value() == 1) - return new (m_parserArena) UnaryPlusNode(location, expr1); + return new (m_vm) UnaryPlusNode(location, expr1); - return new (m_parserArena) MultNode(location, expr1, expr2, rightHasAssignments); + return new (m_vm) MultNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeDivNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) @@ -976,39 +791,26 @@ ExpressionNode* ASTBuilder::makeDivNode(const JSTokenLocation& location, Express expr1 = expr1->stripUnaryPlus(); expr2 = expr2->stripUnaryPlus(); - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - double result = numberExpr1.value() / numberExpr2.value(); - if (static_cast<int64_t>(result) == result) - return createNumberFromBinaryOperation(location, result, numberExpr1, numberExpr2); - return createDoubleLikeNumber(location, result); - } - return new (m_parserArena) DivNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, static_cast<NumberNode*>(expr1)->value() / static_cast<NumberNode*>(expr2)->value()); + return new (m_vm) DivNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeModNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { expr1 = expr1->stripUnaryPlus(); expr2 = expr2->stripUnaryPlus(); - - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, fmod(numberExpr1.value(), numberExpr2.value())); - } - return new (m_parserArena) ModNode(location, expr1, expr2, rightHasAssignments); + + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, fmod(static_cast<NumberNode*>(expr1)->value(), static_cast<NumberNode*>(expr2)->value())); + return new (m_vm) ModNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeAddNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createNumberFromBinaryOperation(location, numberExpr1.value() + numberExpr2.value(), numberExpr1, numberExpr2); - } - return new (m_parserArena) AddNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, static_cast<NumberNode*>(expr1)->value() + static_cast<NumberNode*>(expr2)->value()); + return new (m_vm) AddNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeSubNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) @@ -1016,105 +818,82 @@ ExpressionNode* ASTBuilder::makeSubNode(const JSTokenLocation& location, Express expr1 = expr1->stripUnaryPlus(); expr2 = expr2->stripUnaryPlus(); - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createNumberFromBinaryOperation(location, numberExpr1.value() - numberExpr2.value(), numberExpr1, numberExpr2); - } - return new (m_parserArena) SubNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, static_cast<NumberNode*>(expr1)->value() - static_cast<NumberNode*>(expr2)->value()); + return new (m_vm) SubNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeLeftShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) << (toUInt32(numberExpr2.value()) & 0x1f)); - } - return new (m_parserArena) LeftShiftNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toInt32(static_cast<NumberNode*>(expr1)->value()) << (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f)); + return new (m_vm) LeftShiftNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeRightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f)); - } - return new (m_parserArena) RightShiftNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toInt32(static_cast<NumberNode*>(expr1)->value()) >> (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f)); + return new (m_vm) RightShiftNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeURightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toUInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f)); - } - return new (m_parserArena) UnsignedRightShiftNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toUInt32(static_cast<NumberNode*>(expr1)->value()) >> (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f)); + return new (m_vm) UnsignedRightShiftNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeBitOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) | toInt32(numberExpr2.value())); - } - return new (m_parserArena) BitOrNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toInt32(static_cast<NumberNode*>(expr1)->value()) | toInt32(static_cast<NumberNode*>(expr2)->value())); + return new (m_vm) BitOrNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeBitAndNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) & toInt32(numberExpr2.value())); - } - return new (m_parserArena) BitAndNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toInt32(static_cast<NumberNode*>(expr1)->value()) & toInt32(static_cast<NumberNode*>(expr2)->value())); + return new (m_vm) BitAndNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeBitXOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments) { - if (expr1->isNumber() && expr2->isNumber()) { - const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1); - const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2); - return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) ^ toInt32(numberExpr2.value())); - } - return new (m_parserArena) BitXOrNode(location, expr1, expr2, rightHasAssignments); + if (expr1->isNumber() && expr2->isNumber()) + return createNumber(location, toInt32(static_cast<NumberNode*>(expr1)->value()) ^ toInt32(static_cast<NumberNode*>(expr2)->value())); + return new (m_vm) BitXOrNode(location, expr1, expr2, rightHasAssignments); } ExpressionNode* ASTBuilder::makeFunctionCallNode(const JSTokenLocation& location, ExpressionNode* func, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd) { ASSERT(divot.offset >= divot.lineStartOffset); if (!func->isLocation()) - return new (m_parserArena) FunctionCallValueNode(location, func, args, divot, divotStart, divotEnd); + return new (m_vm) FunctionCallValueNode(location, func, args, divot, divotStart, divotEnd); if (func->isResolveNode()) { ResolveNode* resolve = static_cast<ResolveNode*>(func); const Identifier& identifier = resolve->identifier(); if (identifier == m_vm->propertyNames->eval) { usesEval(); - return new (m_parserArena) EvalFunctionCallNode(location, args, divot, divotStart, divotEnd); + return new (m_vm) EvalFunctionCallNode(location, args, divot, divotStart, divotEnd); } - if (BytecodeIntrinsicNode::EmitterType emitter = m_vm->propertyNames->bytecodeIntrinsicRegistry().lookup(identifier)) - return new (m_parserArena) BytecodeIntrinsicNode(location, emitter, identifier, args, divot, divotStart, divotEnd); - return new (m_parserArena) FunctionCallResolveNode(location, identifier, args, divot, divotStart, divotEnd); + return new (m_vm) FunctionCallResolveNode(location, identifier, args, divot, divotStart, divotEnd); } if (func->isBracketAccessorNode()) { BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(func); - FunctionCallBracketNode* node = new (m_parserArena) FunctionCallBracketNode(location, bracket->base(), bracket->subscript(), bracket->subscriptHasAssignments(), args, divot, divotStart, divotEnd); + FunctionCallBracketNode* node = new (m_vm) FunctionCallBracketNode(location, bracket->base(), bracket->subscript(), args, divot, divotStart, divotEnd); node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset); return node; } ASSERT(func->isDotAccessorNode()); DotAccessorNode* dot = static_cast<DotAccessorNode*>(func); FunctionCallDotNode* node; - if (dot->identifier() == m_vm->propertyNames->builtinNames().callPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().callPrivateName()) - node = new (m_parserArena) CallFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); - else if (dot->identifier() == m_vm->propertyNames->builtinNames().applyPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().applyPrivateName()) - node = new (m_parserArena) ApplyFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + if (dot->identifier() == m_vm->propertyNames->call) + node = new (m_vm) CallFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + else if (dot->identifier() == m_vm->propertyNames->apply) + node = new (m_vm) ApplyFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); else - node = new (m_parserArena) FunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); + node = new (m_vm) FunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd); node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset); return node; } @@ -1123,10 +902,10 @@ ExpressionNode* ASTBuilder::makeBinaryNode(const JSTokenLocation& location, int { switch (token) { case OR: - return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalOr); + return new (m_vm) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalOr); case AND: - return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalAnd); + return new (m_vm) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalAnd); case BITOR: return makeBitOrNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); @@ -1138,37 +917,37 @@ ExpressionNode* ASTBuilder::makeBinaryNode(const JSTokenLocation& location, int return makeBitAndNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case EQEQ: - return new (m_parserArena) EqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) EqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case NE: - return new (m_parserArena) NotEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) NotEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case STREQ: - return new (m_parserArena) StrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) StrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case STRNEQ: - return new (m_parserArena) NotStrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) NotStrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case LT: - return new (m_parserArena) LessNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) LessNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case GT: - return new (m_parserArena) GreaterNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) GreaterNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case LE: - return new (m_parserArena) LessEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) LessEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case GE: - return new (m_parserArena) GreaterEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + return new (m_vm) GreaterEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); case INSTANCEOF: { - InstanceOfNode* node = new (m_parserArena) InstanceOfNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + InstanceOfNode* node = new (m_vm) InstanceOfNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end); return node; } case INTOKEN: { - InNode* node = new (m_parserArena) InNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); + InNode* node = new (m_vm) InNode(location, lhs.first, rhs.first, rhs.second.hasAssignment); setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end); return node; } @@ -1204,24 +983,24 @@ ExpressionNode* ASTBuilder::makeBinaryNode(const JSTokenLocation& location, int ExpressionNode* ASTBuilder::makeAssignNode(const JSTokenLocation& location, ExpressionNode* loc, Operator op, ExpressionNode* expr, bool locHasAssignments, bool exprHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { if (!loc->isLocation()) - return new (m_parserArena) AssignErrorNode(location, divot, start, end); + return new (m_vm) AssignErrorNode(location, divot, start, end); if (loc->isResolveNode()) { ResolveNode* resolve = static_cast<ResolveNode*>(loc); if (op == OpEqual) { if (expr->isFuncExprNode()) - static_cast<FuncExprNode*>(expr)->metadata()->setInferredName(resolve->identifier()); - AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, resolve->identifier(), expr, AssignmentContext::AssignmentExpression); + static_cast<FuncExprNode*>(expr)->body()->setInferredName(resolve->identifier()); + AssignResolveNode* node = new (m_vm) AssignResolveNode(location, resolve->identifier(), expr); setExceptionLocation(node, start, divot, end); return node; } - return new (m_parserArena) ReadModifyResolveNode(location, resolve->identifier(), op, expr, exprHasAssignments, divot, start, end); + return new (m_vm) ReadModifyResolveNode(location, resolve->identifier(), op, expr, exprHasAssignments, divot, start, end); } if (loc->isBracketAccessorNode()) { BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(loc); if (op == OpEqual) - return new (m_parserArena) AssignBracketNode(location, bracket->base(), bracket->subscript(), expr, locHasAssignments, exprHasAssignments, bracket->divot(), start, end); - ReadModifyBracketNode* node = new (m_parserArena) ReadModifyBracketNode(location, bracket->base(), bracket->subscript(), op, expr, locHasAssignments, exprHasAssignments, divot, start, end); + return new (m_vm) AssignBracketNode(location, bracket->base(), bracket->subscript(), expr, locHasAssignments, exprHasAssignments, bracket->divot(), start, end); + ReadModifyBracketNode* node = new (m_vm) ReadModifyBracketNode(location, bracket->base(), bracket->subscript(), op, expr, locHasAssignments, exprHasAssignments, divot, start, end); node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset); return node; } @@ -1229,23 +1008,23 @@ ExpressionNode* ASTBuilder::makeAssignNode(const JSTokenLocation& location, Expr DotAccessorNode* dot = static_cast<DotAccessorNode*>(loc); if (op == OpEqual) { if (expr->isFuncExprNode()) - static_cast<FuncExprNode*>(expr)->metadata()->setInferredName(dot->identifier()); - return new (m_parserArena) AssignDotNode(location, dot->base(), dot->identifier(), expr, exprHasAssignments, dot->divot(), start, end); + static_cast<FuncExprNode*>(expr)->body()->setInferredName(dot->identifier()); + return new (m_vm) AssignDotNode(location, dot->base(), dot->identifier(), expr, exprHasAssignments, dot->divot(), start, end); } - ReadModifyDotNode* node = new (m_parserArena) ReadModifyDotNode(location, dot->base(), dot->identifier(), op, expr, exprHasAssignments, divot, start, end); + ReadModifyDotNode* node = new (m_vm) ReadModifyDotNode(location, dot->base(), dot->identifier(), op, expr, exprHasAssignments, divot, start, end); node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset); return node; } ExpressionNode* ASTBuilder::makePrefixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - return new (m_parserArena) PrefixNode(location, expr, op, divot, start, end); + return new (m_vm) PrefixNode(location, expr, op, divot, start, end); } ExpressionNode* ASTBuilder::makePostfixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end) { - return new (m_parserArena) PostfixNode(location, expr, op, divot, start, end); + return new (m_vm) PostfixNode(location, expr, op, divot, start, end); } } diff --git a/Source/JavaScriptCore/parser/Keywords.table b/Source/JavaScriptCore/parser/Keywords.table index e2ba528a4..527eada60 100644 --- a/Source/JavaScriptCore/parser/Keywords.table +++ b/Source/JavaScriptCore/parser/Keywords.table @@ -10,16 +10,13 @@ false FALSETOKEN break BREAK case CASE catch CATCH -class CLASSTOKEN const CONSTTOKEN default DEFAULT -extends EXTENDS finally FINALLY for FOR instanceof INSTANCEOF new NEW var VAR -let LET continue CONTINUE function FUNCTION return RETURN @@ -31,7 +28,6 @@ do DO while WHILE else ELSE in INTOKEN -super SUPER switch SWITCH throw THROW try TRY @@ -40,13 +36,17 @@ with WITH debugger DEBUGGER # Reserved for future use. +class RESERVED enum RESERVED -export EXPORT -import IMPORT +export RESERVED +extends RESERVED +import RESERVED +super RESERVED # Reserved for future use in strict code. implements RESERVED_IF_STRICT interface RESERVED_IF_STRICT +let RESERVED_IF_STRICT package RESERVED_IF_STRICT private RESERVED_IF_STRICT protected RESERVED_IF_STRICT diff --git a/Source/JavaScriptCore/parser/Lexer.cpp b/Source/JavaScriptCore/parser/Lexer.cpp index c4db1618a..ca4bb4f0a 100644 --- a/Source/JavaScriptCore/parser/Lexer.cpp +++ b/Source/JavaScriptCore/parser/Lexer.cpp @@ -27,11 +27,10 @@ #include "JSFunctionInlines.h" -#include "BuiltinNames.h" #include "JSGlobalObjectFunctions.h" #include "Identifier.h" +#include "NodeInfo.h" #include "Nodes.h" -#include "JSCInlines.h" #include <wtf/dtoa.h> #include <ctype.h> #include <limits.h> @@ -44,9 +43,10 @@ namespace JSC { -bool isLexerKeyword(const Identifier& identifier) +Keywords::Keywords(VM& vm) + : m_vm(vm) + , m_keywordTable(JSC::mainTable) { - return JSC::mainTable.entry(identifier); } enum CharacterType { @@ -70,7 +70,6 @@ enum CharacterType { CharacterQuestion, CharacterTilde, CharacterQuote, - CharacterBackQuote, CharacterDot, CharacterSlash, CharacterBackSlash, @@ -91,7 +90,6 @@ enum CharacterType { // Other types (only one so far) CharacterWhiteSpace, - CharacterPrivateIdentifierStart }; // 256 Latin-1 codes @@ -160,7 +158,7 @@ static const unsigned short typesOfLatin1Characters[256] = { /* 61 - = */ CharacterEqual, /* 62 - > */ CharacterGreater, /* 63 - ? */ CharacterQuestion, -/* 64 - @ */ CharacterPrivateIdentifierStart, +/* 64 - @ */ CharacterInvalid, /* 65 - A */ CharacterIdentifierStart, /* 66 - B */ CharacterIdentifierStart, /* 67 - C */ CharacterIdentifierStart, @@ -192,11 +190,7 @@ static const unsigned short typesOfLatin1Characters[256] = { /* 93 - ] */ CharacterCloseBracket, /* 94 - ^ */ CharacterXor, /* 95 - _ */ CharacterIdentifierStart, -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) -/* 96 - ` */ CharacterBackQuote, -#else /* 96 - ` */ CharacterInvalid, -#endif /* 97 - a */ CharacterIdentifierStart, /* 98 - b */ CharacterIdentifierStart, /* 99 - c */ CharacterIdentifierStart, @@ -492,20 +486,12 @@ static const LChar singleCharacterEscapeValuesForASCII[128] = { }; template <typename T> -Lexer<T>::Lexer(VM* vm, JSParserBuiltinMode builtinMode) - : m_isReparsingFunction(false) +Lexer<T>::Lexer(VM* vm) + : m_isReparsing(false) , m_vm(vm) - , m_parsingBuiltinFunction(builtinMode == JSParserBuiltinMode::Builtin) { } -static inline JSTokenType tokenTypeForIntegerLikeToken(double doubleValue) -{ - if ((doubleValue || !std::signbit(doubleValue)) && static_cast<int64_t>(doubleValue) == doubleValue) - return INTEGER; - return DOUBLE; -} - template <typename T> Lexer<T>::~Lexer() { @@ -516,21 +502,21 @@ String Lexer<T>::invalidCharacterMessage() const { switch (m_current) { case 0: - return ASCIILiteral("Invalid character: '\\0'"); + return "Invalid character: '\\0'"; case 10: - return ASCIILiteral("Invalid character: '\\n'"); + return "Invalid character: '\\n'"; case 11: - return ASCIILiteral("Invalid character: '\\v'"); + return "Invalid character: '\\v'"; case 13: - return ASCIILiteral("Invalid character: '\\r'"); + return "Invalid character: '\\r'"; case 35: - return ASCIILiteral("Invalid character: '#'"); + return "Invalid character: '#'"; case 64: - return ASCIILiteral("Invalid character: '@'"); + return "Invalid character: '@'"; case 96: - return ASCIILiteral("Invalid character: '`'"); + return "Invalid character: '`'"; default: - return String::format("Invalid character '\\u%04u'", static_cast<unsigned>(m_current)); + return String::format("Invalid character '\\u%04u'", static_cast<unsigned>(m_current)).impl(); } } @@ -568,7 +554,6 @@ void Lexer<T>::setCode(const SourceCode& source, ParserArena* arena) m_buffer8.reserveInitialCapacity(initialReadBufferCapacity); m_buffer16.reserveInitialCapacity((m_codeEnd - m_code) / 2); - m_bufferForRawTemplateString16.reserveInitialCapacity(initialReadBufferCapacity); if (LIKELY(m_code < m_codeEnd)) m_current = *m_code; @@ -610,60 +595,22 @@ ALWAYS_INLINE T Lexer<T>::peek(int offset) const return (code < m_codeEnd) ? *code : 0; } -struct ParsedUnicodeEscapeValue { - ParsedUnicodeEscapeValue(UChar32 value) - : m_value(value) - { - ASSERT(isValid()); - } - - enum SpecialValueType { Incomplete = -2, Invalid = -1 }; - ParsedUnicodeEscapeValue(SpecialValueType type) - : m_value(type) - { - } - - bool isValid() const { return m_value >= 0; } - bool isIncomplete() const { return m_value == Incomplete; } - - UChar32 value() const - { - ASSERT(isValid()); - return m_value; - } - -private: - UChar32 m_value; -}; - -template<typename CharacterType> ParsedUnicodeEscapeValue Lexer<CharacterType>::parseUnicodeEscape() +template <typename T> +typename Lexer<T>::UnicodeHexValue Lexer<T>::parseFourDigitUnicodeHex() { - if (m_current == '{') { - shift(); - UChar32 codePoint = 0; - do { - if (!isASCIIHexDigit(m_current)) - return m_current ? ParsedUnicodeEscapeValue::Invalid : ParsedUnicodeEscapeValue::Incomplete; - codePoint = (codePoint << 4) | toASCIIHexValue(m_current); - if (codePoint > UCHAR_MAX_VALUE) - return ParsedUnicodeEscapeValue::Invalid; - shift(); - } while (m_current != '}'); - shift(); - return codePoint; - } + T char1 = peek(1); + T char2 = peek(2); + T char3 = peek(3); + + if (UNLIKELY(!isASCIIHexDigit(m_current) || !isASCIIHexDigit(char1) || !isASCIIHexDigit(char2) || !isASCIIHexDigit(char3))) + return UnicodeHexValue((m_code + 4) >= m_codeEnd ? UnicodeHexValue::IncompleteHex : UnicodeHexValue::InvalidHex); - auto character2 = peek(1); - auto character3 = peek(2); - auto character4 = peek(3); - if (UNLIKELY(!isASCIIHexDigit(m_current) || !isASCIIHexDigit(character2) || !isASCIIHexDigit(character3) || !isASCIIHexDigit(character4))) - return (m_code + 4) >= m_codeEnd ? ParsedUnicodeEscapeValue::Incomplete : ParsedUnicodeEscapeValue::Invalid; - auto result = convertUnicode(m_current, character2, character3, character4); + int result = convertUnicode(m_current, char1, char2, char3); shift(); shift(); shift(); shift(); - return result; + return UnicodeHexValue(result); } template <typename T> @@ -703,24 +650,18 @@ static ALWAYS_INLINE bool isLatin1(UChar c) return c < 256; } -static ALWAYS_INLINE bool isLatin1(UChar32 c) -{ - return !(c & ~0xFF); -} - static inline bool isIdentStart(LChar c) { return typesOfLatin1Characters[c] == CharacterIdentifierStart; } -static inline bool isIdentStart(UChar32 c) +static inline bool isIdentStart(UChar c) { return isLatin1(c) ? isIdentStart(static_cast<LChar>(c)) : isNonLatin1IdentStart(c); } -static NEVER_INLINE bool isNonLatin1IdentPart(UChar32 c) +static NEVER_INLINE bool isNonLatin1IdentPart(int c) { - // FIXME: ES6 says this should be based on the Unicode property ID_Continue now instead. return (U_GET_GC_MASK(c) & (U_GC_L_MASK | U_GC_MN_MASK | U_GC_MC_MASK | U_GC_ND_MASK | U_GC_PC_MASK)) || c == 0x200C || c == 0x200D; } @@ -732,59 +673,9 @@ static ALWAYS_INLINE bool isIdentPart(LChar c) return typesOfLatin1Characters[c] <= CharacterNumber; } -static ALWAYS_INLINE bool isIdentPart(UChar32 c) -{ - return isLatin1(c) ? isIdentPart(static_cast<LChar>(c)) : isNonLatin1IdentPart(c); -} - static ALWAYS_INLINE bool isIdentPart(UChar c) { - return isIdentPart(static_cast<UChar32>(c)); -} - -template<typename CharacterType> ALWAYS_INLINE bool isIdentPartIncludingEscapeTemplate(const CharacterType* code, const CharacterType* codeEnd) -{ - if (isIdentPart(code[0])) - return true; - - // Shortest sequence handled below is \u{0}, which is 5 characters. - if (!(code[0] == '\\' && codeEnd - code >= 5 && code[1] == 'u')) - return false; - - if (code[2] == '{') { - UChar32 codePoint = 0; - const CharacterType* pointer; - for (pointer = &code[3]; pointer < codeEnd; ++pointer) { - auto digit = *pointer; - if (!isASCIIHexDigit(digit)) - break; - codePoint = (codePoint << 4) | toASCIIHexValue(digit); - if (codePoint > UCHAR_MAX_VALUE) - return false; - } - return isIdentPart(codePoint) && pointer < codeEnd && *pointer == '}'; - } - - // Shortest sequence handled below is \uXXXX, which is 6 characters. - if (codeEnd - code < 6) - return false; - - auto character1 = code[2]; - auto character2 = code[3]; - auto character3 = code[4]; - auto character4 = code[5]; - return isASCIIHexDigit(character1) && isASCIIHexDigit(character2) && isASCIIHexDigit(character3) && isASCIIHexDigit(character4) - && isIdentPart(Lexer<LChar>::convertUnicode(character1, character2, character3, character4)); -} - -static ALWAYS_INLINE bool isIdentPartIncludingEscape(const LChar* code, const LChar* codeEnd) -{ - return isIdentPartIncludingEscapeTemplate(code, codeEnd); -} - -static ALWAYS_INLINE bool isIdentPartIncludingEscape(const UChar* code, const UChar* codeEnd) -{ - return isIdentPartIncludingEscapeTemplate(code, codeEnd); + return isLatin1(c) ? isIdentPart(static_cast<LChar>(c)) : isNonLatin1IdentPart(c); } static inline LChar singleEscape(int c) @@ -862,40 +753,7 @@ inline void Lexer<T>::record16(int c) ASSERT(c <= static_cast<int>(USHRT_MAX)); m_buffer16.append(static_cast<UChar>(c)); } - -template<typename CharacterType> inline void Lexer<CharacterType>::recordUnicodeCodePoint(UChar32 codePoint) -{ - ASSERT(codePoint >= 0); - ASSERT(codePoint <= UCHAR_MAX_VALUE); - if (U_IS_BMP(codePoint)) - record16(codePoint); - else { - UChar codeUnits[2] = { U16_LEAD(codePoint), U16_TRAIL(codePoint) }; - append16(codeUnits, 2); - } -} -#if !ASSERT_DISABLED -bool isSafeBuiltinIdentifier(VM& vm, const Identifier* ident) -{ - if (!ident) - return true; - /* Just block any use of suspicious identifiers. This is intended to - * be used as a safety net while implementing builtins. - */ - // FIXME: How can a debug-only assertion be a safety net? - if (*ident == vm.propertyNames->builtinNames().callPublicName()) - return false; - if (*ident == vm.propertyNames->builtinNames().applyPublicName()) - return false; - if (*ident == vm.propertyNames->eval) - return false; - if (*ident == vm.propertyNames->Function) - return false; - return true; -} -#endif - template <> template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<LChar>::parseIdentifier(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) { @@ -907,11 +765,7 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<LChar>::p return keyword == RESERVED_IF_STRICT && !strictMode ? IDENT : keyword; } } - - bool isPrivateName = m_current == '@' && m_parsingBuiltinFunction; - if (isPrivateName) - shift(); - + const LChar* identifierStart = currentSourcePtr(); unsigned identifierLineStart = currentLineStartOffset(); @@ -925,29 +779,18 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<LChar>::p const Identifier* ident = 0; - if (shouldCreateIdentifier || m_parsingBuiltinFunction) { + if (shouldCreateIdentifier) { int identifierLength = currentSourcePtr() - identifierStart; ident = makeIdentifier(identifierStart, identifierLength); - if (m_parsingBuiltinFunction) { - if (!isSafeBuiltinIdentifier(*m_vm, ident) && !isPrivateName) { - m_lexErrorMessage = makeString("The use of '", ident->string(), "' is disallowed in builtin functions."); - return ERRORTOK; - } - if (isPrivateName) - ident = m_vm->propertyNames->getPrivateName(*ident); - else if (*ident == m_vm->propertyNames->undefinedKeyword) - tokenData->ident = &m_vm->propertyNames->undefinedPrivateName; - if (!ident) - return INVALID_PRIVATE_NAME_ERRORTOK; - } + tokenData->ident = ident; } else tokenData->ident = 0; - if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) && !isPrivateName) { + if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords))) { ASSERT(shouldCreateIdentifier); if (remaining < maxTokenLength) { - const HashTableValue* entry = JSC::mainTable.entry(*ident); + const HashEntry* entry = m_vm->keywords->getKeyword(*ident); ASSERT((remaining < maxTokenLength) || !entry); if (!entry) return IDENT; @@ -971,10 +814,6 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<UChar>::p return keyword == RESERVED_IF_STRICT && !strictMode ? IDENT : keyword; } } - - bool isPrivateName = m_current == '@' && m_parsingBuiltinFunction; - if (isPrivateName) - shift(); const UChar* identifierStart = currentSourcePtr(); int identifierLineStart = currentLineStartOffset(); @@ -987,7 +826,6 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<UChar>::p } if (UNLIKELY(m_current == '\\')) { - ASSERT(!isPrivateName); setOffsetFromSourcePtr(identifierStart, identifierLineStart); return parseIdentifierSlowCase<shouldCreateIdentifier>(tokenData, lexerFlags, strictMode); } @@ -999,32 +837,21 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<UChar>::p const Identifier* ident = 0; - if (shouldCreateIdentifier || m_parsingBuiltinFunction) { + if (shouldCreateIdentifier) { int identifierLength = currentSourcePtr() - identifierStart; if (isAll8Bit) ident = makeIdentifierLCharFromUChar(identifierStart, identifierLength); else ident = makeIdentifier(identifierStart, identifierLength); - if (m_parsingBuiltinFunction) { - if (!isSafeBuiltinIdentifier(*m_vm, ident) && !isPrivateName) { - m_lexErrorMessage = makeString("The use of '", ident->string(), "' is disallowed in builtin functions."); - return ERRORTOK; - } - if (isPrivateName) - ident = m_vm->propertyNames->getPrivateName(*ident); - else if (*ident == m_vm->propertyNames->undefinedKeyword) - tokenData->ident = &m_vm->propertyNames->undefinedPrivateName; - if (!ident) - return INVALID_PRIVATE_NAME_ERRORTOK; - } + tokenData->ident = ident; } else tokenData->ident = 0; - if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords)) && !isPrivateName) { + if (UNLIKELY((remaining < maxTokenLength) && !(lexerFlags & LexerFlagsIgnoreReservedWords))) { ASSERT(shouldCreateIdentifier); if (remaining < maxTokenLength) { - const HashTableValue* entry = JSC::mainTable.entry(*ident); + const HashEntry* entry = m_vm->keywords->getKeyword(*ident); ASSERT((remaining < maxTokenLength) || !entry); if (!entry) return IDENT; @@ -1037,9 +864,11 @@ template <bool shouldCreateIdentifier> ALWAYS_INLINE JSTokenType Lexer<UChar>::p return IDENT; } -template<typename CharacterType> template<bool shouldCreateIdentifier> JSTokenType Lexer<CharacterType>::parseIdentifierSlowCase(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) +template <typename T> +template <bool shouldCreateIdentifier> JSTokenType Lexer<T>::parseIdentifierSlowCase(JSTokenData* tokenData, unsigned lexerFlags, bool strictMode) { - auto identifierStart = currentSourcePtr(); + const ptrdiff_t remaining = m_codeEnd - m_code; + const T* identifierStart = currentSourcePtr(); bool bufferRequired = false; while (true) { @@ -1058,18 +887,19 @@ template<typename CharacterType> template<bool shouldCreateIdentifier> JSTokenTy if (UNLIKELY(m_current != 'u')) return atEnd() ? UNTERMINATED_IDENTIFIER_ESCAPE_ERRORTOK : INVALID_IDENTIFIER_ESCAPE_ERRORTOK; shift(); - auto character = parseUnicodeEscape(); + UnicodeHexValue character = parseFourDigitUnicodeHex(); if (UNLIKELY(!character.isValid())) - return character.isIncomplete() ? UNTERMINATED_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK : INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK; - if (UNLIKELY(m_buffer16.size() ? !isIdentPart(character.value()) : !isIdentStart(character.value()))) + return character.valueType() == UnicodeHexValue::IncompleteHex ? UNTERMINATED_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK : INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK; + UChar ucharacter = static_cast<UChar>(character.value()); + if (UNLIKELY(m_buffer16.size() ? !isIdentPart(ucharacter) : !isIdentStart(ucharacter))) return INVALID_IDENTIFIER_UNICODE_ESCAPE_ERRORTOK; if (shouldCreateIdentifier) - recordUnicodeCodePoint(character.value()); + record16(ucharacter); identifierStart = currentSourcePtr(); } int identifierLength; - const Identifier* ident = nullptr; + const Identifier* ident = 0; if (shouldCreateIdentifier) { if (!bufferRequired) { identifierLength = currentSourcePtr() - identifierStart; @@ -1082,19 +912,23 @@ template<typename CharacterType> template<bool shouldCreateIdentifier> JSTokenTy tokenData->ident = ident; } else - tokenData->ident = nullptr; - - m_buffer16.shrink(0); + tokenData->ident = 0; - if (LIKELY(!(lexerFlags & LexerFlagsIgnoreReservedWords))) { + if (LIKELY(!bufferRequired && !(lexerFlags & LexerFlagsIgnoreReservedWords))) { ASSERT(shouldCreateIdentifier); - const HashTableValue* entry = JSC::mainTable.entry(*ident); - if (!entry) - return IDENT; - JSTokenType token = static_cast<JSTokenType>(entry->lexerValue()); - return (token != RESERVED_IF_STRICT) || strictMode ? token : IDENT; + // Keywords must not be recognized if there was an \uXXXX in the identifier. + if (remaining < maxTokenLength) { + const HashEntry* entry = m_vm->keywords->getKeyword(*ident); + ASSERT((remaining < maxTokenLength) || !entry); + if (!entry) + return IDENT; + JSTokenType token = static_cast<JSTokenType>(entry->lexerValue()); + return (token != RESERVED_IF_STRICT) || strictMode ? token : IDENT; + } + return IDENT; } + m_buffer16.resize(0); return IDENT; } @@ -1127,7 +961,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR LChar escape = singleEscape(m_current); - // Most common escape sequences first. + // Most common escape sequences first if (escape) { if (shouldBuildStrings) record8(escape); @@ -1137,7 +971,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR else if (m_current == 'x') { shift(); if (!isASCIIHexDigit(m_current) || !isASCIIHexDigit(peek(1))) { - m_lexErrorMessage = ASCIILiteral("\\x can only be followed by a hex character sequence"); + m_lexErrorMessage = "\\x can only be followed by a hex character sequence"; return (atEnd() || (isASCIIHexDigit(m_current) && (m_code + 1 == m_codeEnd))) ? StringUnterminated : StringCannotBeParsed; } T prev = m_current; @@ -1148,7 +982,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR } else { setOffset(startingOffset, startingLineStartOffset); setLineNumber(startingLineNumber); - m_buffer8.shrink(0); + m_buffer8.resize(0); return parseStringSlowCase<shouldBuildStrings>(tokenData, strictMode); } stringStart = currentSourcePtr(); @@ -1158,7 +992,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR if (UNLIKELY(characterRequiresParseStringSlowCase(m_current))) { setOffset(startingOffset, startingLineStartOffset); setLineNumber(startingLineNumber); - m_buffer8.shrink(0); + m_buffer8.resize(0); return parseStringSlowCase<shouldBuildStrings>(tokenData, strictMode); } @@ -1169,7 +1003,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR append8(stringStart, currentSourcePtr() - stringStart); if (shouldBuildStrings) { tokenData->ident = makeIdentifier(m_buffer8.data(), m_buffer8.size()); - m_buffer8.shrink(0); + m_buffer8.resize(0); } else tokenData->ident = 0; @@ -1177,93 +1011,7 @@ template <bool shouldBuildStrings> ALWAYS_INLINE typename Lexer<T>::StringParseR } template <typename T> -template <bool shouldBuildStrings> ALWAYS_INLINE auto Lexer<T>::parseComplexEscape(EscapeParseMode escapeParseMode, bool strictMode, T stringQuoteCharacter) -> StringParseResult -{ - if (m_current == 'x') { - shift(); - if (!isASCIIHexDigit(m_current) || !isASCIIHexDigit(peek(1))) { - m_lexErrorMessage = ASCIILiteral("\\x can only be followed by a hex character sequence"); - return StringCannotBeParsed; - } - T prev = m_current; - shift(); - if (shouldBuildStrings) - record16(convertHex(prev, m_current)); - shift(); - return StringParsedSuccessfully; - } - - if (m_current == 'u') { - shift(); - - if (escapeParseMode == EscapeParseMode::String && m_current == stringQuoteCharacter) { - if (shouldBuildStrings) - record16('u'); - return StringParsedSuccessfully; - } - - auto character = parseUnicodeEscape(); - if (character.isValid()) { - if (shouldBuildStrings) - recordUnicodeCodePoint(character.value()); - return StringParsedSuccessfully; - } - - m_lexErrorMessage = ASCIILiteral("\\u can only be followed by a Unicode character sequence"); - return character.isIncomplete() ? StringUnterminated : StringCannotBeParsed; - } - - if (strictMode) { - if (isASCIIDigit(m_current)) { - // The only valid numeric escape in strict mode is '\0', and this must not be followed by a decimal digit. - int character1 = m_current; - shift(); - if (character1 != '0' || isASCIIDigit(m_current)) { - m_lexErrorMessage = ASCIILiteral("The only valid numeric escape in strict mode is '\\0'"); - return StringCannotBeParsed; - } - if (shouldBuildStrings) - record16(0); - return StringParsedSuccessfully; - } - } else { - if (isASCIIOctalDigit(m_current)) { - // Octal character sequences - T character1 = m_current; - shift(); - if (isASCIIOctalDigit(m_current)) { - // Two octal characters - T character2 = m_current; - shift(); - if (character1 >= '0' && character1 <= '3' && isASCIIOctalDigit(m_current)) { - if (shouldBuildStrings) - record16((character1 - '0') * 64 + (character2 - '0') * 8 + m_current - '0'); - shift(); - } else { - if (shouldBuildStrings) - record16((character1 - '0') * 8 + character2 - '0'); - } - } else { - if (shouldBuildStrings) - record16(character1 - '0'); - } - return StringParsedSuccessfully; - } - } - - if (!atEnd()) { - if (shouldBuildStrings) - record16(m_current); - shift(); - return StringParsedSuccessfully; - } - - m_lexErrorMessage = ASCIILiteral("Unterminated string constant"); - return StringUnterminated; -} - -template <typename T> -template <bool shouldBuildStrings> auto Lexer<T>::parseStringSlowCase(JSTokenData* tokenData, bool strictMode) -> StringParseResult +template <bool shouldBuildStrings> typename Lexer<T>::StringParseResult Lexer<T>::parseStringSlowCase(JSTokenData* tokenData, bool strictMode) { T stringQuoteCharacter = m_current; shift(); @@ -1285,219 +1033,96 @@ template <bool shouldBuildStrings> auto Lexer<T>::parseStringSlowCase(JSTokenDat shift(); } else if (UNLIKELY(isLineTerminator(m_current))) shiftLineTerminator(); - else { - StringParseResult result = parseComplexEscape<shouldBuildStrings>(EscapeParseMode::String, strictMode, stringQuoteCharacter); - if (result != StringParsedSuccessfully) - return result; - } - - stringStart = currentSourcePtr(); - continue; - } - // Fast check for characters that require special handling. - // Catches 0, \n, \r, 0x2028, and 0x2029 as efficiently - // as possible, and lets through all common ASCII characters. - if (UNLIKELY(((static_cast<unsigned>(m_current) - 0xE) & 0x2000))) { - // New-line or end of input is not allowed - if (atEnd() || isLineTerminator(m_current)) { - m_lexErrorMessage = ASCIILiteral("Unexpected EOF"); - return atEnd() ? StringUnterminated : StringCannotBeParsed; - } - // Anything else is just a normal character - } - shift(); - } - - if (currentSourcePtr() != stringStart && shouldBuildStrings) - append16(stringStart, currentSourcePtr() - stringStart); - if (shouldBuildStrings) - tokenData->ident = makeIdentifier(m_buffer16.data(), m_buffer16.size()); - else - tokenData->ident = 0; - - m_buffer16.shrink(0); - return StringParsedSuccessfully; -} - -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) -// While the lexer accepts <LF><CR> (not <CR><LF>) sequence -// as one line terminator and increments one line number, -// TemplateLiteral considers it as two line terminators <LF> and <CR>. -// -// TemplateLiteral normalizes line terminators as follows. -// -// <LF> => <LF> -// <CR> => <LF> -// <CR><LF> => <LF> -// <\u2028> => <\u2028> -// <\u2029> => <\u2029> -// -// So, <LF><CR> should be normalized to <LF><LF>. -// However, the lexer should increment the line number only once for <LF><CR>. -// -// To achieve this, LineNumberAdder holds the current status of line terminator sequence. -// When TemplateLiteral lexer encounters a line terminator, it notifies to LineNumberAdder. -// LineNumberAdder maintains the status and increments the line number when it's necessary. -// For example, LineNumberAdder increments the line number only once for <LF><CR> and <CR><LF>. -template<typename CharacterType> -class LineNumberAdder { -public: - LineNumberAdder(int& lineNumber) - : m_lineNumber(lineNumber) - { - } - - void clear() - { - m_previous = 0; - } - - void add(CharacterType character) - { - ASSERT(Lexer<CharacterType>::isLineTerminator(character)); - if ((character + m_previous) == ('\n' + '\r')) - m_previous = 0; - else { - ++m_lineNumber; - m_previous = character; - } - } - -private: - int& m_lineNumber; - CharacterType m_previous { 0 }; -}; - -template <typename T> -template <bool shouldBuildStrings> typename Lexer<T>::StringParseResult Lexer<T>::parseTemplateLiteral(JSTokenData* tokenData, RawStringsBuildMode rawStringsBuildMode) -{ - const T* stringStart = currentSourcePtr(); - const T* rawStringStart = currentSourcePtr(); - - LineNumberAdder<T> lineNumberAdder(m_lineNumber); - - while (m_current != '`') { - if (UNLIKELY(m_current == '\\')) { - lineNumberAdder.clear(); - if (stringStart != currentSourcePtr() && shouldBuildStrings) - append16(stringStart, currentSourcePtr() - stringStart); - shift(); - - LChar escape = singleEscape(m_current); - - // Most common escape sequences first. - if (escape) { + else if (m_current == 'x') { + shift(); + if (!isASCIIHexDigit(m_current) || !isASCIIHexDigit(peek(1))) { + m_lexErrorMessage = "\\x can only be followed by a hex character sequence"; + return StringCannotBeParsed; + } + T prev = m_current; + shift(); if (shouldBuildStrings) - record16(escape); + record16(convertHex(prev, m_current)); + shift(); + } else if (m_current == 'u') { shift(); - } else if (UNLIKELY(isLineTerminator(m_current))) { - if (m_current == '\r') { - lineNumberAdder.add(m_current); + UnicodeHexValue character = parseFourDigitUnicodeHex(); + if (character.isValid()) { + if (shouldBuildStrings) + record16(character.value()); + } else if (m_current == stringQuoteCharacter) { + if (shouldBuildStrings) + record16('u'); + } else { + m_lexErrorMessage = "\\u can only be followed by a Unicode character sequence"; + return character.valueType() == UnicodeHexValue::IncompleteHex ? StringUnterminated : StringCannotBeParsed; + } + } else if (strictMode && isASCIIDigit(m_current)) { + // The only valid numeric escape in strict mode is '\0', and this must not be followed by a decimal digit. + int character1 = m_current; + shift(); + if (character1 != '0' || isASCIIDigit(m_current)) { + m_lexErrorMessage = "The only valid numeric escape in strict mode is '\\0'"; + return StringCannotBeParsed; + } + if (shouldBuildStrings) + record16(0); + } else if (!strictMode && isASCIIOctalDigit(m_current)) { + // Octal character sequences + T character1 = m_current; + shift(); + if (isASCIIOctalDigit(m_current)) { + // Two octal characters + T character2 = m_current; shift(); - if (m_current == '\n') { - lineNumberAdder.add(m_current); + if (character1 >= '0' && character1 <= '3' && isASCIIOctalDigit(m_current)) { + if (shouldBuildStrings) + record16((character1 - '0') * 64 + (character2 - '0') * 8 + m_current - '0'); shift(); + } else { + if (shouldBuildStrings) + record16((character1 - '0') * 8 + character2 - '0'); } } else { - lineNumberAdder.add(m_current); - shift(); + if (shouldBuildStrings) + record16(character1 - '0'); } + } else if (!atEnd()) { + if (shouldBuildStrings) + record16(m_current); + shift(); } else { - bool strictMode = true; - StringParseResult result = parseComplexEscape<shouldBuildStrings>(EscapeParseMode::Template, strictMode, '`'); - if (result != StringParsedSuccessfully) - return result; + m_lexErrorMessage = "Unterminated string constant"; + return StringUnterminated; } stringStart = currentSourcePtr(); continue; } - - if (m_current == '$' && peek(1) == '{') - break; - // Fast check for characters that require special handling. // Catches 0, \n, \r, 0x2028, and 0x2029 as efficiently // as possible, and lets through all common ASCII characters. if (UNLIKELY(((static_cast<unsigned>(m_current) - 0xE) & 0x2000))) { - // End of input is not allowed. - // Unlike String, line terminator is allowed. - if (atEnd()) { - m_lexErrorMessage = ASCIILiteral("Unexpected EOF"); + // New-line or end of input is not allowed + if (atEnd() || isLineTerminator(m_current)) { + m_lexErrorMessage = "Unexpected EOF"; return atEnd() ? StringUnterminated : StringCannotBeParsed; } - - if (isLineTerminator(m_current)) { - if (m_current == '\r') { - // Normalize <CR>, <CR><LF> to <LF>. - if (shouldBuildStrings) { - if (stringStart != currentSourcePtr()) - append16(stringStart, currentSourcePtr() - stringStart); - if (rawStringStart != currentSourcePtr() && rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) - m_bufferForRawTemplateString16.append(rawStringStart, currentSourcePtr() - rawStringStart); - - record16('\n'); - if (rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) - m_bufferForRawTemplateString16.append('\n'); - } - lineNumberAdder.add(m_current); - shift(); - if (m_current == '\n') { - lineNumberAdder.add(m_current); - shift(); - } - stringStart = currentSourcePtr(); - rawStringStart = currentSourcePtr(); - } else { - lineNumberAdder.add(m_current); - shift(); - } - continue; - } // Anything else is just a normal character } - - lineNumberAdder.clear(); shift(); } - bool isTail = m_current == '`'; - - if (shouldBuildStrings) { - if (currentSourcePtr() != stringStart) - append16(stringStart, currentSourcePtr() - stringStart); - if (rawStringStart != currentSourcePtr() && rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) - m_bufferForRawTemplateString16.append(rawStringStart, currentSourcePtr() - rawStringStart); - } - - if (shouldBuildStrings) { - tokenData->cooked = makeIdentifier(m_buffer16.data(), m_buffer16.size()); - // Line terminator normalization (e.g. <CR> => <LF>) should be applied to both the raw and cooked representations. - if (rawStringsBuildMode == RawStringsBuildMode::BuildRawStrings) - tokenData->raw = makeIdentifier(m_bufferForRawTemplateString16.data(), m_bufferForRawTemplateString16.size()); - else - tokenData->raw = makeEmptyIdentifier(); - } else { - tokenData->cooked = makeEmptyIdentifier(); - tokenData->raw = makeEmptyIdentifier(); - } - tokenData->isTail = isTail; - - m_buffer16.shrink(0); - m_bufferForRawTemplateString16.shrink(0); - - if (isTail) { - // Skip ` - shift(); - } else { - // Skip $ and { - shift(); - shift(); - } + if (currentSourcePtr() != stringStart && shouldBuildStrings) + append16(stringStart, currentSourcePtr() - stringStart); + if (shouldBuildStrings) + tokenData->ident = makeIdentifier(m_buffer16.data(), m_buffer16.size()); + else + tokenData->ident = 0; + m_buffer16.resize(0); return StringParsedSuccessfully; } -#endif template <typename T> ALWAYS_INLINE void Lexer<T>::parseHex(double& returnValue) @@ -1506,6 +1131,9 @@ ALWAYS_INLINE void Lexer<T>::parseHex(double& returnValue) uint32_t hexValue = 0; int maximumDigits = 7; + // Shift out the 'x' prefix. + shift(); + do { hexValue = (hexValue << 4) + toASCIIHexValue(m_current); shift(); @@ -1537,67 +1165,28 @@ ALWAYS_INLINE void Lexer<T>::parseHex(double& returnValue) } template <typename T> -ALWAYS_INLINE bool Lexer<T>::parseBinary(double& returnValue) -{ - // Optimization: most binary values fit into 4 bytes. - uint32_t binaryValue = 0; - const unsigned maximumDigits = 32; - int digit = maximumDigits - 1; - // Temporary buffer for the digits. Makes easier - // to reconstruct the input characters when needed. - LChar digits[maximumDigits]; - - do { - binaryValue = (binaryValue << 1) + (m_current - '0'); - digits[digit] = m_current; - shift(); - --digit; - } while (isASCIIBinaryDigit(m_current) && digit >= 0); - - if (!isASCIIDigit(m_current) && digit >= 0) { - returnValue = binaryValue; - return true; - } - - for (int i = maximumDigits - 1; i > digit; --i) - record8(digits[i]); - - while (isASCIIBinaryDigit(m_current)) { - record8(m_current); - shift(); - } - - if (isASCIIDigit(m_current)) - return false; - - returnValue = parseIntOverflow(m_buffer8.data(), m_buffer8.size(), 2); - return true; -} - -template <typename T> ALWAYS_INLINE bool Lexer<T>::parseOctal(double& returnValue) { // Optimization: most octal values fit into 4 bytes. uint32_t octalValue = 0; - const unsigned maximumDigits = 10; - int digit = maximumDigits - 1; + int maximumDigits = 9; // Temporary buffer for the digits. Makes easier // to reconstruct the input characters when needed. - LChar digits[maximumDigits]; + LChar digits[10]; do { octalValue = octalValue * 8 + (m_current - '0'); - digits[digit] = m_current; + digits[maximumDigits] = m_current; shift(); - --digit; - } while (isASCIIOctalDigit(m_current) && digit >= 0); + --maximumDigits; + } while (isASCIIOctalDigit(m_current) && maximumDigits >= 0); - if (!isASCIIDigit(m_current) && digit >= 0) { + if (!isASCIIDigit(m_current) && maximumDigits >= 0) { returnValue = octalValue; return true; } - for (int i = maximumDigits - 1; i > digit; --i) + for (int i = 9; i > maximumDigits; --i) record8(digits[i]); while (isASCIIOctalDigit(m_current)) { @@ -1621,25 +1210,24 @@ ALWAYS_INLINE bool Lexer<T>::parseDecimal(double& returnValue) // Since parseOctal may be executed before parseDecimal, // the m_buffer8 may hold ascii digits. if (!m_buffer8.size()) { - const unsigned maximumDigits = 10; - int digit = maximumDigits - 1; + int maximumDigits = 9; // Temporary buffer for the digits. Makes easier // to reconstruct the input characters when needed. - LChar digits[maximumDigits]; + LChar digits[10]; do { decimalValue = decimalValue * 10 + (m_current - '0'); - digits[digit] = m_current; + digits[maximumDigits] = m_current; shift(); - --digit; - } while (isASCIIDigit(m_current) && digit >= 0); + --maximumDigits; + } while (isASCIIDigit(m_current) && maximumDigits >= 0); - if (digit >= 0 && m_current != '.' && (m_current | 0x20) != 'e') { + if (maximumDigits >= 0 && m_current != '.' && (m_current | 0x20) != 'e') { returnValue = decimalValue; return true; } - for (int i = maximumDigits - 1; i > digit; --i) + for (int i = 9; i > maximumDigits; --i) record8(digits[i]); } @@ -1715,22 +1303,10 @@ bool Lexer<T>::nextTokenIsColon() } template <typename T> -void Lexer<T>::setTokenPosition(JSToken* tokenRecord) -{ - JSTokenData* tokenData = &tokenRecord->m_data; - tokenData->line = lineNumber(); - tokenData->offset = currentOffset(); - tokenData->lineStartOffset = currentLineStartOffset(); - ASSERT(tokenData->offset >= tokenData->lineStartOffset); -} - -template <typename T> JSTokenType Lexer<T>::lex(JSToken* tokenRecord, unsigned lexerFlags, bool strictMode) { JSTokenData* tokenData = &tokenRecord->m_data; JSTokenLocation* tokenLocation = &tokenRecord->m_location; - m_lastTockenLocation = JSTokenLocation(tokenRecord->m_location); - ASSERT(!m_error); ASSERT(m_buffer8.isEmpty()); ASSERT(m_buffer16.isEmpty()); @@ -1789,19 +1365,7 @@ start: } token = GT; break; - case CharacterEqual: { -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - if (peek(1) == '>') { - token = ARROWFUNCTION; - tokenData->line = lineNumber(); - tokenData->offset = currentOffset(); - tokenData->lineStartOffset = currentLineStartOffset(); - ASSERT(tokenData->offset >= tokenData->lineStartOffset); - shift(); - shift(); - break; - } -#endif + case CharacterEqual: shift(); if (m_current == '=') { shift(); @@ -1815,7 +1379,6 @@ start: } token = EQUAL; break; - } case CharacterLess: shift(); if (m_current == '!' && peek(1) == '-' && peek(2) == '-') { @@ -1904,7 +1467,7 @@ start: shift(); if (parseMultilineComment()) goto start; - m_lexErrorMessage = ASCIILiteral("Multiline comment was not closed properly"); + m_lexErrorMessage = "Multiline comment was not closed properly"; token = UNTERMINATED_MULTILINE_COMMENT_ERRORTOK; goto returnError; } @@ -2028,150 +1591,73 @@ start: goto inNumberAfterDecimalPoint; case CharacterZero: shift(); - if ((m_current | 0x20) == 'x') { - if (!isASCIIHexDigit(peek(1))) { - m_lexErrorMessage = ASCIILiteral("No hexadecimal digits after '0x'"); - token = INVALID_HEX_NUMBER_ERRORTOK; - goto returnError; - } - - // Shift out the 'x' prefix. - shift(); - + if ((m_current | 0x20) == 'x' && isASCIIHexDigit(peek(1))) { parseHex(tokenData->doubleValue); - if (isIdentStart(m_current)) { - m_lexErrorMessage = ASCIILiteral("No space between hexadecimal literal and identifier"); - token = INVALID_HEX_NUMBER_ERRORTOK; - goto returnError; - } - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); - m_buffer8.shrink(0); - break; - } - if ((m_current | 0x20) == 'b') { - if (!isASCIIBinaryDigit(peek(1))) { - m_lexErrorMessage = ASCIILiteral("No binary digits after '0b'"); - token = INVALID_BINARY_NUMBER_ERRORTOK; - goto returnError; - } - - // Shift out the 'b' prefix. - shift(); - - parseBinary(tokenData->doubleValue); - if (isIdentStart(m_current)) { - m_lexErrorMessage = ASCIILiteral("No space between binary literal and identifier"); - token = INVALID_BINARY_NUMBER_ERRORTOK; - goto returnError; - } - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); - m_buffer8.shrink(0); - break; - } - - if ((m_current | 0x20) == 'o') { - if (!isASCIIOctalDigit(peek(1))) { - m_lexErrorMessage = ASCIILiteral("No octal digits after '0o'"); - token = INVALID_OCTAL_NUMBER_ERRORTOK; - goto returnError; - } - - // Shift out the 'o' prefix. - shift(); - - parseOctal(tokenData->doubleValue); - if (isIdentStart(m_current)) { - m_lexErrorMessage = ASCIILiteral("No space between octal literal and identifier"); - token = INVALID_OCTAL_NUMBER_ERRORTOK; - goto returnError; - } - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); - m_buffer8.shrink(0); - break; - } - - record8('0'); - if (strictMode && isASCIIDigit(m_current)) { - m_lexErrorMessage = ASCIILiteral("Decimal integer literals with a leading zero are forbidden in strict mode"); - token = INVALID_OCTAL_NUMBER_ERRORTOK; - goto returnError; - } - if (isASCIIOctalDigit(m_current)) { - if (parseOctal(tokenData->doubleValue)) { - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); + token = NUMBER; + } else { + record8('0'); + if (isASCIIOctalDigit(m_current)) { + if (parseOctal(tokenData->doubleValue)) { + if (strictMode) { + m_lexErrorMessage = "Octal escapes are forbidden in strict mode"; + token = INVALID_OCTAL_NUMBER_ERRORTOK; + goto returnError; + } + token = NUMBER; + } } } FALLTHROUGH; case CharacterNumber: - if (LIKELY(token != INTEGER && token != DOUBLE)) { + if (LIKELY(token != NUMBER)) { if (!parseDecimal(tokenData->doubleValue)) { - token = INTEGER; if (m_current == '.') { shift(); inNumberAfterDecimalPoint: parseNumberAfterDecimalPoint(); - token = DOUBLE; } if ((m_current | 0x20) == 'e') { if (!parseNumberAfterExponentIndicator()) { - m_lexErrorMessage = ASCIILiteral("Non-number found after exponent indicator"); + m_lexErrorMessage = "Non-number found after exponent indicator"; token = atEnd() ? UNTERMINATED_NUMERIC_LITERAL_ERRORTOK : INVALID_NUMERIC_LITERAL_ERRORTOK; goto returnError; } } size_t parsedLength; tokenData->doubleValue = parseDouble(m_buffer8.data(), m_buffer8.size(), parsedLength); - if (token == INTEGER) - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); - } else - token = tokenTypeForIntegerLikeToken(tokenData->doubleValue); + } + token = NUMBER; } + // No identifiers allowed directly after numeric literal, e.g. "3in" is bad. if (UNLIKELY(isIdentStart(m_current))) { - m_lexErrorMessage = ASCIILiteral("No identifiers allowed directly after numeric literal"); + m_lexErrorMessage = "At least one digit must occur after a decimal point"; token = atEnd() ? UNTERMINATED_NUMERIC_LITERAL_ERRORTOK : INVALID_NUMERIC_LITERAL_ERRORTOK; goto returnError; } - m_buffer8.shrink(0); + m_buffer8.resize(0); break; - case CharacterQuote: { - StringParseResult result = StringCannotBeParsed; - if (lexerFlags & LexerFlagsDontBuildStrings) - result = parseString<false>(tokenData, strictMode); - else - result = parseString<true>(tokenData, strictMode); - - if (UNLIKELY(result != StringParsedSuccessfully)) { - token = result == StringUnterminated ? UNTERMINATED_STRING_LITERAL_ERRORTOK : INVALID_STRING_LITERAL_ERRORTOK; - goto returnError; + case CharacterQuote: + if (lexerFlags & LexerFlagsDontBuildStrings) { + StringParseResult result = parseString<false>(tokenData, strictMode); + if (UNLIKELY(result != StringParsedSuccessfully)) { + token = result == StringUnterminated ? UNTERMINATED_STRING_LITERAL_ERRORTOK : INVALID_STRING_LITERAL_ERRORTOK; + goto returnError; + } + } else { + StringParseResult result = parseString<true>(tokenData, strictMode); + if (UNLIKELY(result != StringParsedSuccessfully)) { + token = result == StringUnterminated ? UNTERMINATED_STRING_LITERAL_ERRORTOK : INVALID_STRING_LITERAL_ERRORTOK; + goto returnError; + } } shift(); token = STRING; break; - } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - case CharacterBackQuote: { - // Skip backquote. - shift(); - StringParseResult result = StringCannotBeParsed; - if (lexerFlags & LexerFlagsDontBuildStrings) - result = parseTemplateLiteral<false>(tokenData, RawStringsBuildMode::BuildRawStrings); - else - result = parseTemplateLiteral<true>(tokenData, RawStringsBuildMode::BuildRawStrings); - - if (UNLIKELY(result != StringParsedSuccessfully)) { - token = result == StringUnterminated ? UNTERMINATED_TEMPLATE_LITERAL_ERRORTOK : INVALID_TEMPLATE_LITERAL_ERRORTOK; - goto returnError; - } - token = TEMPLATE; - break; - } -#endif case CharacterIdentifierStart: ASSERT(isIdentStart(m_current)); FALLTHROUGH; case CharacterBackSlash: - parseIdent: if (lexerFlags & LexexFlagsDontBuildKeywords) token = parseIdentifier<false>(tokenData, lexerFlags, strictMode); else @@ -2184,18 +1670,13 @@ inNumberAfterDecimalPoint: m_terminator = true; m_lineStart = m_code; goto start; - case CharacterPrivateIdentifierStart: - if (m_parsingBuiltinFunction) - goto parseIdent; - - FALLTHROUGH; case CharacterInvalid: m_lexErrorMessage = invalidCharacterMessage(); token = ERRORTOK; goto returnError; default: RELEASE_ASSERT_NOT_REACHED(); - m_lexErrorMessage = ASCIILiteral("Internal Error"); + m_lexErrorMessage = "Internal Error"; token = ERRORTOK; goto returnError; } @@ -2269,7 +1750,7 @@ bool Lexer<T>::scanRegExp(const Identifier*& pattern, const Identifier*& flags, while (true) { if (isLineTerminator(m_current) || atEnd()) { - m_buffer16.shrink(0); + m_buffer16.resize(0); return false; } @@ -2303,7 +1784,7 @@ bool Lexer<T>::scanRegExp(const Identifier*& pattern, const Identifier*& flags, pattern = makeRightSizedIdentifier(m_buffer16.data(), m_buffer16.size(), charactersOredTogether); - m_buffer16.shrink(0); + m_buffer16.resize(0); charactersOredTogether = 0; while (isIdentPart(m_current)) { @@ -2313,7 +1794,7 @@ bool Lexer<T>::scanRegExp(const Identifier*& pattern, const Identifier*& flags, } flags = makeRightSizedIdentifier(m_buffer16.data(), m_buffer16.size(), charactersOredTogether); - m_buffer16.shrink(0); + m_buffer16.resize(0); return true; } @@ -2359,40 +1840,6 @@ bool Lexer<T>::skipRegExp() return true; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) -template <typename T> -JSTokenType Lexer<T>::scanTrailingTemplateString(JSToken* tokenRecord, RawStringsBuildMode rawStringsBuildMode) -{ - JSTokenData* tokenData = &tokenRecord->m_data; - JSTokenLocation* tokenLocation = &tokenRecord->m_location; - ASSERT(!m_error); - ASSERT(m_buffer16.isEmpty()); - - // Leading closing brace } is already shifted in the previous token scan. - // So in this re-scan phase, shift() is not needed here. - StringParseResult result = parseTemplateLiteral<true>(tokenData, rawStringsBuildMode); - JSTokenType token = ERRORTOK; - if (UNLIKELY(result != StringParsedSuccessfully)) { - token = result == StringUnterminated ? UNTERMINATED_TEMPLATE_LITERAL_ERRORTOK : INVALID_TEMPLATE_LITERAL_ERRORTOK; - m_error = true; - } else { - token = TEMPLATE; - m_lastToken = token; - } - - // Since TemplateString always ends with ` or }, m_atLineStart always becomes false. - m_atLineStart = false; - - // Adjust current tokenLocation data for TemplateString. - tokenLocation->line = m_lineNumber; - tokenLocation->endOffset = currentOffset(); - tokenLocation->lineStartOffset = currentLineStartOffset(); - ASSERT(tokenLocation->endOffset >= tokenLocation->lineStartOffset); - tokenRecord->m_endPosition = currentPosition(); - return token; -} -#endif - template <typename T> void Lexer<T>::clear() { @@ -2404,10 +1851,7 @@ void Lexer<T>::clear() Vector<UChar> newBuffer16; m_buffer16.swap(newBuffer16); - Vector<UChar> newBufferForRawTemplateString16; - m_bufferForRawTemplateString16.swap(newBufferForRawTemplateString16); - - m_isReparsingFunction = false; + m_isReparsing = false; } // Instantiate the two flavors of Lexer we need instead of putting most of this file in Lexer.h diff --git a/Source/JavaScriptCore/parser/Lexer.h b/Source/JavaScriptCore/parser/Lexer.h index e5f56cca4..28c61ba19 100644 --- a/Source/JavaScriptCore/parser/Lexer.h +++ b/Source/JavaScriptCore/parser/Lexer.h @@ -30,26 +30,49 @@ #include <wtf/ASCIICType.h> #include <wtf/SegmentedVector.h> #include <wtf/Vector.h> +#include <wtf/unicode/Unicode.h> namespace JSC { +class Keywords { +public: + bool isKeyword(const Identifier& ident) const + { + return m_keywordTable.entry(m_vm, ident); + } + + const HashEntry* getKeyword(const Identifier& ident) const + { + return m_keywordTable.entry(m_vm, ident); + } + + ~Keywords() + { + m_keywordTable.deleteTable(); + } + +private: + friend class VM; + + explicit Keywords(VM&); + + VM& m_vm; + const HashTable m_keywordTable; +}; + enum LexerFlags { LexerFlagsIgnoreReservedWords = 1, LexerFlagsDontBuildStrings = 2, LexexFlagsDontBuildKeywords = 4 }; -struct ParsedUnicodeEscapeValue; - -bool isLexerKeyword(const Identifier&); - template <typename T> class Lexer { WTF_MAKE_NONCOPYABLE(Lexer); WTF_MAKE_FAST_ALLOCATED; public: - Lexer(VM*, JSParserBuiltinMode); + Lexer(VM*); ~Lexer(); // Character manipulation functions. @@ -60,10 +83,9 @@ public: // Functions to set up parsing. void setCode(const SourceCode&, ParserArena*); - void setIsReparsingFunction() { m_isReparsingFunction = true; } - bool isReparsingFunction() const { return m_isReparsingFunction; } + void setIsReparsing() { m_isReparsing = true; } + bool isReparsing() const { return m_isReparsing; } - void setTokenPosition(JSToken* tokenRecord); JSTokenType lex(JSToken*, unsigned, bool strictMode); bool nextTokenIsColon(); int lineNumber() const { return m_lineNumber; } @@ -74,15 +96,10 @@ public: return JSTextPosition(m_lineNumber, currentOffset(), currentLineStartOffset()); } JSTextPosition positionBeforeLastNewline() const { return m_positionBeforeLastNewline; } - JSTokenLocation lastTokenLocation() const { return m_lastTockenLocation; } void setLastLineNumber(int lastLineNumber) { m_lastLineNumber = lastLineNumber; } int lastLineNumber() const { return m_lastLineNumber; } bool prevTerminator() const { return m_terminator; } bool scanRegExp(const Identifier*& pattern, const Identifier*& flags, UChar patternPrefix = 0); -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - enum class RawStringsBuildMode { BuildRawStrings, DontBuildRawStrings }; - JSTokenType scanTrailingTemplateString(JSToken*, RawStringsBuildMode); -#endif bool skipRegExp(); // Functions for use after parsing. @@ -109,10 +126,6 @@ public: { m_lineNumber = line; } - void setTerminator(bool terminator) - { - m_terminator = terminator; - } SourceProvider* sourceProvider() const { return m_source->provider(); } @@ -123,15 +136,42 @@ private: void append8(const T*, size_t); void record16(int); void record16(T); - void recordUnicodeCodePoint(UChar32); void append16(const LChar*, size_t); void append16(const UChar* characters, size_t length) { m_buffer16.append(characters, length); } ALWAYS_INLINE void shift(); ALWAYS_INLINE bool atEnd() const; ALWAYS_INLINE T peek(int offset) const; - - ParsedUnicodeEscapeValue parseUnicodeEscape(); + struct UnicodeHexValue { + + enum ValueType { ValidHex, IncompleteHex, InvalidHex }; + + explicit UnicodeHexValue(int value) + : m_value(value) + { + } + explicit UnicodeHexValue(ValueType type) + : m_value(type == IncompleteHex ? -2 : -1) + { + } + + ValueType valueType() const + { + if (m_value >= 0) + return ValidHex; + return m_value == -2 ? IncompleteHex : InvalidHex; + } + bool isValid() const { return m_value >= 0; } + int value() const + { + ASSERT(m_value >= 0); + return m_value; + } + + private: + int m_value; + }; + UnicodeHexValue parseFourDigitUnicodeHex(); void shiftLineTerminator(); ALWAYS_INLINE int offsetFromSourcePtr(const T* ptr) const { return ptr - m_codeStart; } @@ -149,7 +189,6 @@ private: ALWAYS_INLINE const Identifier* makeLCharIdentifier(const UChar* characters, size_t length); ALWAYS_INLINE const Identifier* makeRightSizedIdentifier(const UChar* characters, size_t length, UChar orAllChars); ALWAYS_INLINE const Identifier* makeIdentifierLCharFromUChar(const UChar* characters, size_t length); - ALWAYS_INLINE const Identifier* makeEmptyIdentifier(); ALWAYS_INLINE bool lastTokenWasRestrKeyword() const; @@ -164,14 +203,7 @@ private: }; template <bool shouldBuildStrings> ALWAYS_INLINE StringParseResult parseString(JSTokenData*, bool strictMode); template <bool shouldBuildStrings> NEVER_INLINE StringParseResult parseStringSlowCase(JSTokenData*, bool strictMode); - - enum class EscapeParseMode { Template, String }; - template <bool shouldBuildStrings> ALWAYS_INLINE StringParseResult parseComplexEscape(EscapeParseMode, bool strictMode, T stringQuoteCharacter); -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - template <bool shouldBuildStrings> ALWAYS_INLINE StringParseResult parseTemplateLiteral(JSTokenData*, RawStringsBuildMode); -#endif ALWAYS_INLINE void parseHex(double& returnValue); - ALWAYS_INLINE bool parseBinary(double& returnValue); ALWAYS_INLINE bool parseOctal(double& returnValue); ALWAYS_INLINE bool parseDecimal(double& returnValue); ALWAYS_INLINE void parseNumberAfterDecimalPoint(); @@ -185,7 +217,6 @@ private: Vector<LChar> m_buffer8; Vector<UChar> m_buffer16; - Vector<UChar> m_bufferForRawTemplateString16; bool m_terminator; int m_lastToken; @@ -197,8 +228,7 @@ private: const T* m_codeStartPlusOffset; const T* m_lineStart; JSTextPosition m_positionBeforeLastNewline; - JSTokenLocation m_lastTockenLocation; - bool m_isReparsingFunction; + bool m_isReparsing; bool m_atLineStart; bool m_error; String m_lexErrorMessage; @@ -208,7 +238,6 @@ private: IdentifierArena* m_arena; VM* m_vm; - bool m_parsingBuiltinFunction; }; template <> @@ -220,8 +249,7 @@ ALWAYS_INLINE bool Lexer<LChar>::isWhiteSpace(LChar ch) template <> ALWAYS_INLINE bool Lexer<UChar>::isWhiteSpace(UChar ch) { - // 0x180E used to be in Zs category before Unicode 6.3, and EcmaScript says that we should keep treating it as such. - return (ch < 256) ? Lexer<LChar>::isWhiteSpace(static_cast<LChar>(ch)) : (u_charType(ch) == U_SPACE_SEPARATOR || ch == 0x180E || ch == 0xFEFF); + return (ch < 256) ? Lexer<LChar>::isWhiteSpace(static_cast<LChar>(ch)) : (u_charType(ch) == U_SPACE_SEPARATOR || ch == 0xFEFF); } template <> @@ -275,12 +303,6 @@ ALWAYS_INLINE const Identifier* Lexer<UChar>::makeRightSizedIdentifier(const UCh return &m_arena->makeIdentifier(m_vm, characters, length); } -template <typename T> -ALWAYS_INLINE const Identifier* Lexer<T>::makeEmptyIdentifier() -{ - return &m_arena->makeEmptyIdentifier(m_vm); -} - template <> ALWAYS_INLINE void Lexer<LChar>::setCodeStart(const StringImpl* sourceString) { @@ -313,12 +335,6 @@ ALWAYS_INLINE const Identifier* Lexer<T>::makeLCharIdentifier(const UChar* chara return &m_arena->makeIdentifierLCharFromUChar(m_vm, characters, length); } -#if ASSERT_DISABLED -ALWAYS_INLINE bool isSafeBuiltinIdentifier(VM&, const Identifier*) { return true; } -#else -bool isSafeBuiltinIdentifier(VM&, const Identifier*); -#endif - template <typename T> ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, unsigned lexerFlags, bool strictMode) { @@ -354,15 +370,10 @@ ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, un ASSERT(currentOffset() >= currentLineStartOffset()); // Create the identifier if needed - if (lexerFlags & LexexFlagsDontBuildKeywords -#if !ASSERT_DISABLED - && !m_parsingBuiltinFunction -#endif - ) + if (lexerFlags & LexexFlagsDontBuildKeywords) tokenData->ident = 0; else tokenData->ident = makeLCharIdentifier(start, ptr - start); - tokenLocation->line = m_lineNumber; tokenLocation->lineStartOffset = currentLineStartOffset(); tokenLocation->startOffset = offsetFromSourcePtr(start); @@ -370,13 +381,6 @@ ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, un ASSERT(tokenLocation->startOffset >= tokenLocation->lineStartOffset); tokenRecord->m_startPosition = startPosition; tokenRecord->m_endPosition = currentPosition(); -#if !ASSERT_DISABLED - if (m_parsingBuiltinFunction) { - if (!isSafeBuiltinIdentifier(*m_vm, tokenData->ident)) - return ERRORTOK; - } -#endif - m_lastToken = IDENT; return IDENT; diff --git a/Source/JavaScriptCore/parser/ModuleAnalyzer.cpp b/Source/JavaScriptCore/parser/ModuleAnalyzer.cpp deleted file mode 100644 index b2de960b3..000000000 --- a/Source/JavaScriptCore/parser/ModuleAnalyzer.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "ModuleAnalyzer.h" - -#include "IdentifierInlines.h" -#include "ModuleRecord.h" - -namespace JSC { - - -ModuleAnalyzer::ModuleAnalyzer(VM& vm, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables) - : m_vm(&vm) - , m_moduleRecord(ModuleRecord::create()) - , m_declaredVariables(declaredVariables) - , m_lexicalVariables(lexicalVariables) -{ -} - -Identifier ModuleAnalyzer::exportedBinding(const RefPtr<UniquedStringImpl>& ident) -{ - const auto iterator = m_aliasMap.find(ident); - if (iterator != m_aliasMap.end()) - return iterator->value; - return Identifier::fromUid(&vm(), ident.get()); -} - -void ModuleAnalyzer::declareExportAlias(const Identifier& localName, const Identifier& exportName) -{ - m_aliasMap.add(localName.impl(), exportName); -} - -void ModuleAnalyzer::exportVariable(const RefPtr<UniquedStringImpl>& localName, const VariableEnvironmentEntry& variable) -{ - // In the parser, we already marked the variables as Exported and Imported. - // By leveraging this information, we collect the information that is needed - // to construct the module environment. - // - // I E - // * = exported module local variable - // * = imported binding - // = non-exported module local variable - // * * = indirect exported binding - // - // One exception is namespace binding (like import * as ns from "mod"). - // This is annotated as an imported, but the actual binding is locate in the - // current module. - - if (!variable.isExported()) - return; - - const Identifier exportName = exportedBinding(localName); - - // Exported module local variable. - if (!variable.isImported()) { - moduleRecord().addExportEntry(ModuleRecord::ExportEntry::createLocal(exportName, Identifier::fromUid(m_vm, localName.get()), variable)); - return; - } - - const auto& importEntry = moduleRecord().lookUpImportEntry(localName); - if (importEntry.isNamespace(vm())) { - // Exported namespace binding. - // import * as namespace from "mod" - // export { namespace } - moduleRecord().addExportEntry(ModuleRecord::ExportEntry::createNamespace(exportName, importEntry.moduleRequest)); - return; - } - - // Indirectly exported binding. - // import a from "mod" - // export { a } - moduleRecord().addExportEntry(ModuleRecord::ExportEntry::createIndirect(exportName, importEntry.importName, importEntry.moduleRequest)); -} - - - -Ref<ModuleRecord> ModuleAnalyzer::analyze(ModuleProgramNode& moduleProgramNode) -{ - // Traverse the module AST and collect - // * Import entries - // * Export entries that have FromClause (e.g. export { a } from "mod") - // * Export entries that have star (e.g. export * from "mod") - // * Aliased export names (e.g. export { a as b }) - moduleProgramNode.analyzeModule(*this); - - // Based on the collected information, categorize export entries into 3 types. - // 1. Local export entries - // This references the local variable in the current module. - // This variable should be allocated in the current module environment as a heap variable. - // - // const variable = 20 - // export { variable } - // - // 2. Namespace export entries - // This references the namespace object imported by some import entries. - // This variable itself should be allocated in the current module environment as a heap variable. - // But when the other modules attempt to resolve this export name in this module, this module - // should tell the link to the original module. - // - // import * as namespace from "mod" - // export { namespace as mod } - // - // 3. Indirect export entries - // This references the imported binding name from the other module. - // This module environment itself should hold the pointer to (1) the original module and - // (2) the binding in the original module. The variable itself is allocated in the original - // module. This indirect binding is resolved when the CodeBlock resolves the references. - // - // import mod from "mod" - // export { mod } - // - // export { a } from "mod" - // - // And separeted from the above 3 types, we also collect the star export entries. - // - // 4. Star export entries - // This exports all the names from the specified external module as the current module's name. - // - // export * from "mod" - for (const auto& pair : m_declaredVariables) - exportVariable(pair.key, pair.value); - - for (const auto& pair : m_lexicalVariables) - exportVariable(pair.key, pair.value); - - if (Options::dumpModuleRecord()) - m_moduleRecord->dump(); - - return *m_moduleRecord; -} - -} // namespace JSC diff --git a/Source/JavaScriptCore/parser/ModuleAnalyzer.h b/Source/JavaScriptCore/parser/ModuleAnalyzer.h deleted file mode 100644 index 027648476..000000000 --- a/Source/JavaScriptCore/parser/ModuleAnalyzer.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef ModuleAnalyzer_h -#define ModuleAnalyzer_h - -#include "ModuleRecord.h" -#include "Nodes.h" - -namespace JSC { - -class ModuleAnalyzer { -public: - ModuleAnalyzer(VM&, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables); - - Ref<ModuleRecord> analyze(ModuleProgramNode&); - - VM& vm() { return *m_vm; } - - ModuleRecord& moduleRecord() { return *m_moduleRecord; } - - void declareExportAlias(const Identifier& localName, const Identifier& exportName); - -private: - typedef HashMap<RefPtr<UniquedStringImpl>, Identifier, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> IdentifierAliasMap; - - void exportVariable(const RefPtr<UniquedStringImpl>&, const VariableEnvironmentEntry&); - - Identifier exportedBinding(const RefPtr<UniquedStringImpl>& ident); - - VM* m_vm; - RefPtr<ModuleRecord> m_moduleRecord; - VariableEnvironment m_declaredVariables; - VariableEnvironment m_lexicalVariables; - IdentifierAliasMap m_aliasMap; -}; - -} // namespace JSC - -#endif // ModuleAnalyzer_h diff --git a/Source/JavaScriptCore/parser/ModuleRecord.cpp b/Source/JavaScriptCore/parser/ModuleRecord.cpp deleted file mode 100644 index ec973568b..000000000 --- a/Source/JavaScriptCore/parser/ModuleRecord.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "ModuleRecord.h" - -#include "IdentifierInlines.h" - -namespace JSC { - - -auto ModuleRecord::ExportEntry::createLocal(const Identifier& exportName, const Identifier& localName, const VariableEnvironmentEntry& variable) -> ExportEntry -{ - return ExportEntry { Type::Local, exportName, Identifier(), Identifier(), localName, variable }; -} - -auto ModuleRecord::ExportEntry::createNamespace(const Identifier& exportName, const Identifier& moduleName) -> ExportEntry -{ - return ExportEntry { Type::Namespace, exportName, moduleName, Identifier(), Identifier(), VariableEnvironmentEntry() }; -} - -auto ModuleRecord::ExportEntry::createIndirect(const Identifier& exportName, const Identifier& importName, const Identifier& moduleName) -> ExportEntry -{ - return ExportEntry { Type::Indirect, exportName, moduleName, importName, Identifier(), VariableEnvironmentEntry() }; -} - -static String printableName(const RefPtr<UniquedStringImpl>& uid) -{ - if (uid->isSymbol()) - return uid.get(); - return WTF::makeString("'", String(uid.get()), "'"); -} - -static String printableName(const Identifier& ident) -{ - return printableName(ident.impl()); -} - - -void ModuleRecord::dump() -{ - dataLog("\nAnalyzing ModuleRecord\n"); - - dataLog(" Dependencies: ", m_requestedModules.size(), " modules\n"); - for (const auto& moduleName : m_requestedModules) - dataLog(" module(", printableName(moduleName), ")\n"); - - dataLog(" Import: ", m_importEntries.size(), " entries\n"); - for (const auto& pair : m_importEntries) - dataLog(" import(", printableName(pair.value.importName), "), local(", printableName(pair.value.localName), "), module(", printableName(pair.value.moduleRequest), ")\n"); - - dataLog(" Export: ", m_exportEntries.size(), " entries\n"); - for (const auto& pair : m_exportEntries) { - const ExportEntry& entry = pair.value; - switch (entry.type) { - case ExportEntry::Type::Local: - dataLog(" [Local] ", "export(", printableName(entry.exportName), "), local(", printableName(entry.localName), ")\n"); - break; - - case ExportEntry::Type::Namespace: - dataLog(" [Namespace] ", "export(", printableName(entry.exportName), "), module(", printableName(entry.moduleName), ")\n"); - break; - - case ExportEntry::Type::Indirect: - dataLog(" [Indirect] ", "export(", printableName(entry.exportName), "), import(", printableName(entry.importName), "), module(", printableName(entry.moduleName), ")\n"); - break; - } - } - for (const auto& moduleName : m_starExportEntries) - dataLog(" [Star] module(", printableName(moduleName.get()), ")\n"); -} - -} // namespace JSC diff --git a/Source/JavaScriptCore/parser/ModuleRecord.h b/Source/JavaScriptCore/parser/ModuleRecord.h deleted file mode 100644 index ef081c03b..000000000 --- a/Source/JavaScriptCore/parser/ModuleRecord.h +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef ModuleRecord_h -#define ModuleRecord_h - -#include "Nodes.h" -#include <wtf/HashMap.h> -#include <wtf/ListHashSet.h> - -namespace JSC { - -// Based on the Source Text Module Record -// http://www.ecma-international.org/ecma-262/6.0/#sec-source-text-module-records -class ModuleRecord : public RefCounted<ModuleRecord> { -public: - struct ExportEntry { - enum class Type { - Local, - Namespace, - Indirect - }; - - static ExportEntry createLocal(const Identifier& exportName, const Identifier& localName, const VariableEnvironmentEntry&); - static ExportEntry createNamespace(const Identifier& exportName, const Identifier& moduleName); - static ExportEntry createIndirect(const Identifier& exportName, const Identifier& importName, const Identifier& moduleName); - - Type type; - Identifier exportName; - Identifier moduleName; - Identifier importName; - Identifier localName; - VariableEnvironmentEntry variable; - }; - - struct ImportEntry { - Identifier moduleRequest; - Identifier importName; - Identifier localName; - - bool isNamespace(VM& vm) const - { - return importName == vm.propertyNames->timesIdentifier; - } - }; - - typedef WTF::ListHashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> OrderedIdentifierSet; - typedef HashMap<RefPtr<UniquedStringImpl>, ImportEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> ImportMap; - typedef HashMap<RefPtr<UniquedStringImpl>, ExportEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> ExportMap; - - static Ref<ModuleRecord> create() - { - return adoptRef(*new ModuleRecord); - } - - void appendRequestedModule(const Identifier&); - void addStarExportEntry(const Identifier&); - void addImportEntry(const ImportEntry&); - void addExportEntry(const ExportEntry&); - - const ImportEntry& lookUpImportEntry(const RefPtr<UniquedStringImpl>& localName); - - void dump(); - -private: - // Map localName -> ImportEntry. - ImportMap m_importEntries; - - // Map exportName -> ExportEntry. - ExportMap m_exportEntries; - - IdentifierSet m_starExportEntries; - - // Save the occurrence order since the module loader loads and runs the modules in this order. - // http://www.ecma-international.org/ecma-262/6.0/#sec-moduleevaluation - OrderedIdentifierSet m_requestedModules; -}; - -inline void ModuleRecord::appendRequestedModule(const Identifier& moduleName) -{ - m_requestedModules.add(moduleName.impl()); -} - -inline void ModuleRecord::addImportEntry(const ImportEntry& entry) -{ - m_importEntries.add(entry.localName.impl(), entry); -} - -inline void ModuleRecord::addExportEntry(const ExportEntry& entry) -{ - m_exportEntries.add(entry.exportName.impl(), entry); -} - -inline void ModuleRecord::addStarExportEntry(const Identifier& moduleName) -{ - m_starExportEntries.add(moduleName.impl()); -} - -inline auto ModuleRecord::lookUpImportEntry(const RefPtr<UniquedStringImpl>& localName) -> const ImportEntry& -{ - return (*m_importEntries.find(localName)).value; -} - -} // namespace JSC - -#endif // ModuleRecord_h diff --git a/Source/JavaScriptCore/parser/NodeConstructors.h b/Source/JavaScriptCore/parser/NodeConstructors.h index abb72a74e..917126975 100644 --- a/Source/JavaScriptCore/parser/NodeConstructors.h +++ b/Source/JavaScriptCore/parser/NodeConstructors.h @@ -27,24 +27,23 @@ namespace JSC { - inline void* ParserArenaFreeable::operator new(size_t size, ParserArena& parserArena) + inline void* ParserArenaFreeable::operator new(size_t size, VM* vm) { - return parserArena.allocateFreeable(size); + return vm->parserArena->allocateFreeable(size); } - inline void* ParserArenaDeletable::operator new(size_t size, ParserArena& parserArena) + inline void* ParserArenaDeletable::operator new(size_t size, VM* vm) { - return parserArena.allocateDeletable(size); + return vm->parserArena->allocateDeletable(size); } - inline ParserArenaRoot::ParserArenaRoot(ParserArena& parserArena) + inline ParserArenaRefCounted::ParserArenaRefCounted(VM* vm) { - m_arena.swap(parserArena); + vm->parserArena->derefWithArena(adoptRef(this)); } inline Node::Node(const JSTokenLocation& location) : m_position(location.line, location.startOffset, location.lineStartOffset) - , m_endOffset(-1) { ASSERT(location.startOffset >= location.lineStartOffset); } @@ -57,7 +56,6 @@ namespace JSC { inline StatementNode::StatementNode(const JSTokenLocation& location) : Node(location) - , m_next(nullptr) , m_lastLine(-1) { } @@ -84,74 +82,12 @@ namespace JSC { { } - inline DoubleNode::DoubleNode(const JSTokenLocation& location, double value) - : NumberNode(location, value) - { - } - - inline IntegerNode::IntegerNode(const JSTokenLocation& location, double value) - : DoubleNode(location, value) - { - } - inline StringNode::StringNode(const JSTokenLocation& location, const Identifier& value) : ConstantNode(location, ResultType::stringType()) , m_value(value) { } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - inline TemplateExpressionListNode::TemplateExpressionListNode(ExpressionNode* node) - : m_node(node) - { - } - - inline TemplateExpressionListNode::TemplateExpressionListNode(TemplateExpressionListNode* previous, ExpressionNode* node) - : m_node(node) - { - previous->m_next = this; - } - - inline TemplateStringNode::TemplateStringNode(const JSTokenLocation& location, const Identifier& cooked, const Identifier& raw) - : ExpressionNode(location) - , m_cooked(cooked) - , m_raw(raw) - { - } - - inline TemplateStringListNode::TemplateStringListNode(TemplateStringNode* node) - : m_node(node) - { - } - - inline TemplateStringListNode::TemplateStringListNode(TemplateStringListNode* previous, TemplateStringNode* node) - : m_node(node) - { - previous->m_next = this; - } - - inline TemplateLiteralNode::TemplateLiteralNode(const JSTokenLocation& location, TemplateStringListNode* templateStrings) - : ExpressionNode(location) - , m_templateStrings(templateStrings) - , m_templateExpressions(nullptr) - { - } - - inline TemplateLiteralNode::TemplateLiteralNode(const JSTokenLocation& location, TemplateStringListNode* templateStrings, TemplateExpressionListNode* templateExpressions) - : ExpressionNode(location) - , m_templateStrings(templateStrings) - , m_templateExpressions(templateExpressions) - { - } - - inline TaggedTemplateNode::TaggedTemplateNode(const JSTokenLocation& location, ExpressionNode* tag, TemplateLiteralNode* templateLiteral) - : ExpressionNode(location) - , m_tag(tag) - , m_templateLiteral(templateLiteral) - { - } -#endif - inline RegExpNode::RegExpNode(const JSTokenLocation& location, const Identifier& pattern, const Identifier& flags) : ExpressionNode(location) , m_pattern(pattern) @@ -159,23 +95,12 @@ namespace JSC { { } - inline ThisNode::ThisNode(const JSTokenLocation& location, ThisTDZMode thisTDZMode) - : ExpressionNode(location) - , m_shouldAlwaysEmitTDZCheck(thisTDZMode == ThisTDZMode::AlwaysCheck) - { - } - - inline SuperNode::SuperNode(const JSTokenLocation& location) - : ExpressionNode(location) - { - } - - inline NewTargetNode::NewTargetNode(const JSTokenLocation& location) + inline ThisNode::ThisNode(const JSTokenLocation& location) : ExpressionNode(location) { } - inline ResolveNode::ResolveNode(const JSTokenLocation& location, const Identifier& ident, const JSTextPosition& start) +inline ResolveNode::ResolveNode(const JSTokenLocation& location, const Identifier& ident, const JSTextPosition& start) : ExpressionNode(location) , m_ident(ident) , m_start(start) @@ -222,22 +147,25 @@ namespace JSC { { } - inline PropertyNode::PropertyNode(const Identifier& name, ExpressionNode* assign, Type type, PutType putType, SuperBinding superBinding = SuperBinding::NotNeeded) + inline PropertyNode::PropertyNode(VM*, const Identifier& name, ExpressionNode* assign, Type type) : m_name(&name) , m_assign(assign) , m_type(type) - , m_needsSuperBinding(superBinding == SuperBinding::Needed) - , m_putType(putType) { } - inline PropertyNode::PropertyNode(ExpressionNode* name, ExpressionNode* assign, Type type, PutType putType) + inline PropertyNode::PropertyNode(VM* vm, double name, ExpressionNode* assign, Type type) + : m_name(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name)) + , m_assign(assign) + , m_type(type) + { + } + + inline PropertyNode::PropertyNode(VM*, ExpressionNode* name, ExpressionNode* assign, Type type) : m_name(0) , m_expression(name) , m_assign(assign) , m_type(type) - , m_needsSuperBinding(false) - , m_putType(putType) { } @@ -353,13 +281,12 @@ namespace JSC { { } - inline FunctionCallBracketNode::FunctionCallBracketNode(const JSTokenLocation& location, ExpressionNode* base, ExpressionNode* subscript, bool subscriptHasAssignments, ArgumentsNode* args, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd) + inline FunctionCallBracketNode::FunctionCallBracketNode(const JSTokenLocation& location, ExpressionNode* base, ExpressionNode* subscript, ArgumentsNode* args, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd) : ExpressionNode(location) , ThrowableSubExpressionData(divot, divotStart, divotEnd) , m_base(base) , m_subscript(subscript) , m_args(args) - , m_subscriptHasAssignments(subscriptHasAssignments) { } @@ -372,15 +299,6 @@ namespace JSC { { } - inline BytecodeIntrinsicNode::BytecodeIntrinsicNode(const JSTokenLocation& location, EmitterType emitter, const Identifier& ident, ArgumentsNode* args, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd) - : ExpressionNode(location) - , ThrowableExpressionData(divot, divotStart, divotEnd) - , m_emitter(emitter) - , m_ident(ident) - , m_args(args) - { - } - inline CallFunctionCallDotNode::CallFunctionCallDotNode(const JSTokenLocation& location, ExpressionNode* base, const Identifier& ident, ArgumentsNode* args, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd) : FunctionCallDotNode(location, base, ident, args, divot, divotStart, divotEnd) { @@ -639,11 +557,10 @@ namespace JSC { { } - inline AssignResolveNode::AssignResolveNode(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* right, AssignmentContext assignmentContext) + inline AssignResolveNode::AssignResolveNode(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* right) : ExpressionNode(location) , m_ident(ident) , m_right(right) - , m_assignmentContext(assignmentContext) { } @@ -698,16 +615,22 @@ namespace JSC { { } - inline CommaNode::CommaNode(const JSTokenLocation& location, ExpressionNode* expr) + inline CommaNode::CommaNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2) : ExpressionNode(location) - , m_expr(expr) - , m_next(nullptr) + { + ASSERT(expr1); + ASSERT(expr2); + m_expressions.append(expr1); + m_expressions.append(expr2); + } + + inline ConstStatementNode::ConstStatementNode(const JSTokenLocation& location, ConstDeclNode* next) + : StatementNode(location) + , m_next(next) { } inline SourceElements::SourceElements() - : m_head(nullptr) - , m_tail(nullptr) { } @@ -727,81 +650,11 @@ namespace JSC { { } - inline DeclarationStatement::DeclarationStatement(const JSTokenLocation& location, ExpressionNode* expr) + inline VarStatementNode::VarStatementNode(const JSTokenLocation& location, ExpressionNode* expr) : StatementNode(location) , m_expr(expr) { } - - inline ModuleDeclarationNode::ModuleDeclarationNode(const JSTokenLocation& location) - : StatementNode(location) - { - } - - inline ModuleNameNode::ModuleNameNode(const JSTokenLocation& location, const Identifier& moduleName) - : Node(location) - , m_moduleName(moduleName) - { - } - - inline ImportSpecifierNode::ImportSpecifierNode(const JSTokenLocation& location, const Identifier& importedName, const Identifier& localName) - : Node(location) - , m_importedName(importedName) - , m_localName(localName) - { - } - - inline ImportDeclarationNode::ImportDeclarationNode(const JSTokenLocation& location, ImportSpecifierListNode* importSpecifierList, ModuleNameNode* moduleName) - : ModuleDeclarationNode(location) - , m_specifierList(importSpecifierList) - , m_moduleName(moduleName) - { - } - - inline ExportAllDeclarationNode::ExportAllDeclarationNode(const JSTokenLocation& location, ModuleNameNode* moduleName) - : ModuleDeclarationNode(location) - , m_moduleName(moduleName) - { - } - - inline ExportDefaultDeclarationNode::ExportDefaultDeclarationNode(const JSTokenLocation& location, StatementNode* declaration, const Identifier& localName) - : ModuleDeclarationNode(location) - , m_declaration(declaration) - , m_localName(localName) - { - } - - inline ExportLocalDeclarationNode::ExportLocalDeclarationNode(const JSTokenLocation& location, StatementNode* declaration) - : ModuleDeclarationNode(location) - , m_declaration(declaration) - { - } - - inline ExportNamedDeclarationNode::ExportNamedDeclarationNode(const JSTokenLocation& location, ExportSpecifierListNode* exportSpecifierList, ModuleNameNode* moduleName) - : ModuleDeclarationNode(location) - , m_specifierList(exportSpecifierList) - , m_moduleName(moduleName) - { - } - - inline ExportSpecifierNode::ExportSpecifierNode(const JSTokenLocation& location, const Identifier& localName, const Identifier& exportedName) - : Node(location) - , m_localName(localName) - , m_exportedName(exportedName) - { - } - - inline EmptyVarExpression::EmptyVarExpression(const JSTokenLocation& location, const Identifier& ident) - : ExpressionNode(location) - , m_ident(ident) - { - } - - inline EmptyLetExpression::EmptyLetExpression(const JSTokenLocation& location, const Identifier& ident) - : ExpressionNode(location) - , m_ident(ident) - { - } inline IfElseNode::IfElseNode(const JSTokenLocation& location, ExpressionNode* condition, StatementNode* ifBlock, StatementNode* elseBlock) : StatementNode(location) @@ -825,9 +678,8 @@ namespace JSC { { } - inline ForNode::ForNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, ExpressionNode* expr3, StatementNode* statement, VariableEnvironment& lexicalVariables) + inline ForNode::ForNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, ExpressionNode* expr3, StatementNode* statement) : StatementNode(location) - , VariableEnvironmentNode(lexicalVariables) , m_expr1(expr1) , m_expr2(expr2) , m_expr3(expr3) @@ -836,12 +688,24 @@ namespace JSC { ASSERT(statement); } + inline ContinueNode::ContinueNode(VM* vm, const JSTokenLocation& location) + : StatementNode(location) + , m_ident(vm->propertyNames->nullIdentifier) + { + } + inline ContinueNode::ContinueNode(const JSTokenLocation& location, const Identifier& ident) : StatementNode(location) , m_ident(ident) { } + inline BreakNode::BreakNode(VM* vm, const JSTokenLocation& location) + : StatementNode(location) + , m_ident(vm->propertyNames->nullIdentifier) + { + } + inline BreakNode::BreakNode(const JSTokenLocation& location, const Identifier& ident) : StatementNode(location) , m_ident(ident) @@ -876,51 +740,44 @@ namespace JSC { { } - inline TryNode::TryNode(const JSTokenLocation& location, StatementNode* tryBlock, const Identifier& thrownValueIdent, StatementNode* catchBlock, VariableEnvironment& catchEnvironment, StatementNode* finallyBlock) + inline TryNode::TryNode(const JSTokenLocation& location, StatementNode* tryBlock, const Identifier& exceptionIdent, StatementNode* catchBlock, StatementNode* finallyBlock) : StatementNode(location) , m_tryBlock(tryBlock) - , m_thrownValueIdent(thrownValueIdent) + , m_exceptionIdent(exceptionIdent) , m_catchBlock(catchBlock) , m_finallyBlock(finallyBlock) { - m_catchEnvironment.swap(catchEnvironment); } - inline FunctionParameters::FunctionParameters() + inline ParameterNode::ParameterNode(PassRefPtr<DeconstructionPatternNode> pattern) + : m_pattern(pattern) + , m_next(0) { + ASSERT(m_pattern); } - inline FuncExprNode::FuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source) - : ExpressionNode(location) - , m_metadata(m_metadata) + inline ParameterNode::ParameterNode(ParameterNode* l, PassRefPtr<DeconstructionPatternNode> pattern) + : m_pattern(pattern) + , m_next(0) { - m_metadata->finishParsing(source, ident, FunctionExpression); + l->m_next = this; + ASSERT(m_pattern); + ASSERT(l->m_pattern); } - inline FuncDeclNode::FuncDeclNode(const JSTokenLocation& location, const Identifier& ident, FunctionMetadataNode* m_metadata, const SourceCode& source) - : StatementNode(location) - , m_metadata(m_metadata) + inline FuncExprNode::FuncExprNode(const JSTokenLocation& location, const Identifier& ident, FunctionBodyNode* body, const SourceCode& source, ParameterNode* parameter) + : ExpressionNode(location) + , m_body(body) { - m_metadata->finishParsing(source, ident, FunctionDeclaration); + m_body->finishParsing(source, parameter, ident, FunctionNameIsInScope); } -#if ENABLE(ES6_CLASS_SYNTAX) - inline ClassDeclNode::ClassDeclNode(const JSTokenLocation& location, ExpressionNode* classDeclaration) + inline FuncDeclNode::FuncDeclNode(const JSTokenLocation& location, const Identifier& ident, FunctionBodyNode* body, const SourceCode& source, ParameterNode* parameter) : StatementNode(location) - , m_classDeclaration(classDeclaration) - { - } - - inline ClassExprNode::ClassExprNode(const JSTokenLocation& location, const Identifier& name, ExpressionNode* constructorExpression, ExpressionNode* classHeritage, PropertyListNode* instanceMethods, PropertyListNode* staticMethods) - : ExpressionNode(location) - , m_name(name) - , m_constructorExpression(constructorExpression) - , m_classHeritage(classHeritage) - , m_instanceMethods(instanceMethods) - , m_staticMethods(staticMethods) + , m_body(body) { + m_body->finishParsing(source, parameter, ident, FunctionNameIsNotInScope); } -#endif inline CaseClauseNode::CaseClauseNode(ExpressionNode* expr, SourceElements* statements) : m_expr(expr) @@ -948,65 +805,103 @@ namespace JSC { { } - inline SwitchNode::SwitchNode(const JSTokenLocation& location, ExpressionNode* expr, CaseBlockNode* block, VariableEnvironment& lexicalVariables) + inline SwitchNode::SwitchNode(const JSTokenLocation& location, ExpressionNode* expr, CaseBlockNode* block) : StatementNode(location) - , VariableEnvironmentNode(lexicalVariables) , m_expr(expr) , m_block(block) { } - inline BlockNode::BlockNode(const JSTokenLocation& location, SourceElements* statements, VariableEnvironment& lexicalVariables) + inline ConstDeclNode::ConstDeclNode(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* init) + : ExpressionNode(location) + , m_ident(ident) + , m_next(0) + , m_init(init) + { + } + + inline BlockNode::BlockNode(const JSTokenLocation& location, SourceElements* statements) : StatementNode(location) - , VariableEnvironmentNode(lexicalVariables) , m_statements(statements) { } - inline EnumerationNode::EnumerationNode(const JSTokenLocation& location, ExpressionNode* lexpr, ExpressionNode* expr, StatementNode* statement, VariableEnvironment& lexicalVariables) + inline EnumerationNode::EnumerationNode(const JSTokenLocation& location, ExpressionNode* l, ExpressionNode* expr, StatementNode* statement) + : StatementNode(location) + , m_lexpr(l) + , m_expr(expr) + , m_statement(statement) + { + ASSERT(l); + } + + inline EnumerationNode::EnumerationNode(VM* vm, const JSTokenLocation& location, DeconstructionPatternNode* pattern, ExpressionNode* expr, StatementNode* statement) : StatementNode(location) - , VariableEnvironmentNode(lexicalVariables) - , m_lexpr(lexpr) + , m_lexpr(new (vm) DeconstructingAssignmentNode(location, pattern, 0)) , m_expr(expr) , m_statement(statement) { - ASSERT(lexpr); + ASSERT(pattern); + } + + inline ForInNode::ForInNode(const JSTokenLocation& location, ExpressionNode* l, ExpressionNode* expr, StatementNode* statement) + : EnumerationNode(location, l, expr, statement) + { + } + + inline ForInNode::ForInNode(VM* vm, const JSTokenLocation& location, DeconstructionPatternNode* pattern, ExpressionNode* expr, StatementNode* statement) + : EnumerationNode(vm, location, pattern, expr, statement) + { } - inline ForInNode::ForInNode(const JSTokenLocation& location, ExpressionNode* lexpr, ExpressionNode* expr, StatementNode* statement, VariableEnvironment& lexicalVariables) - : EnumerationNode(location, lexpr, expr, statement, lexicalVariables) + inline ForOfNode::ForOfNode(const JSTokenLocation& location, ExpressionNode* l, ExpressionNode* expr, StatementNode* statement) + : EnumerationNode(location, l, expr, statement) { } - inline ForOfNode::ForOfNode(const JSTokenLocation& location, ExpressionNode* lexpr, ExpressionNode* expr, StatementNode* statement, VariableEnvironment& lexicalVariables) - : EnumerationNode(location, lexpr, expr, statement, lexicalVariables) + inline ForOfNode::ForOfNode(VM* vm, const JSTokenLocation& location, DeconstructionPatternNode* pattern, ExpressionNode* expr, StatementNode* statement) + : EnumerationNode(vm, location, pattern, expr, statement) { } - inline DestructuringPatternNode::DestructuringPatternNode() + inline DeconstructionPatternNode::DeconstructionPatternNode(VM*) { } - inline ArrayPatternNode::ArrayPatternNode() - : DestructuringPatternNode() + inline ArrayPatternNode::ArrayPatternNode(VM* vm) + : DeconstructionPatternNode(vm) { } - inline ObjectPatternNode::ObjectPatternNode() - : DestructuringPatternNode() + inline PassRefPtr<ArrayPatternNode> ArrayPatternNode::create(VM* vm) + { + return adoptRef(new ArrayPatternNode(vm)); + } + + inline ObjectPatternNode::ObjectPatternNode(VM* vm) + : DeconstructionPatternNode(vm) + { + } + + inline PassRefPtr<ObjectPatternNode> ObjectPatternNode::create(VM* vm) + { + return adoptRef(new ObjectPatternNode(vm)); + } + + inline PassRefPtr<BindingNode> BindingNode::create(VM* vm, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end) { + return adoptRef(new BindingNode(vm, boundProperty, start, end)); } - inline BindingNode::BindingNode(const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext context) - : DestructuringPatternNode() + inline BindingNode::BindingNode(VM* vm, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end) + : DeconstructionPatternNode(vm) , m_divotStart(start) , m_divotEnd(end) , m_boundProperty(boundProperty) - , m_bindingContext(context) { } - inline DestructuringAssignmentNode::DestructuringAssignmentNode(const JSTokenLocation& location, DestructuringPatternNode* bindings, ExpressionNode* initializer) + inline DeconstructingAssignmentNode::DeconstructingAssignmentNode(const JSTokenLocation& location, PassRefPtr<DeconstructionPatternNode> bindings, ExpressionNode* initializer) : ExpressionNode(location) , m_bindings(bindings) , m_initializer(initializer) diff --git a/Source/JavaScriptCore/parser/NodeInfo.h b/Source/JavaScriptCore/parser/NodeInfo.h new file mode 100644 index 000000000..4853aec42 --- /dev/null +++ b/Source/JavaScriptCore/parser/NodeInfo.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef NodeInfo_h +#define NodeInfo_h + +#include "Nodes.h" + +namespace JSC { + + template <typename T> struct NodeInfo { + T m_node; + CodeFeatures m_features; + int m_numConstants; + }; + + typedef NodeInfo<FuncDeclNode*> FuncDeclNodeInfo; + typedef NodeInfo<FuncExprNode*> FuncExprNodeInfo; + typedef NodeInfo<ExpressionNode*> ExpressionNodeInfo; + typedef NodeInfo<ArgumentsNode*> ArgumentsNodeInfo; + typedef NodeInfo<ConstDeclNode*> ConstDeclNodeInfo; + typedef NodeInfo<PropertyNode*> PropertyNodeInfo; + typedef NodeInfo<PropertyList> PropertyListInfo; + typedef NodeInfo<ElementList> ElementListInfo; + typedef NodeInfo<ArgumentList> ArgumentListInfo; + + template <typename T> struct NodeDeclarationInfo { + T m_node; + ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations; + ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations; + CodeFeatures m_features; + int m_numConstants; + }; + + typedef NodeDeclarationInfo<StatementNode*> StatementNodeInfo; + typedef NodeDeclarationInfo<CaseBlockNode*> CaseBlockNodeInfo; + typedef NodeDeclarationInfo<CaseClauseNode*> CaseClauseNodeInfo; + typedef NodeDeclarationInfo<SourceElements*> SourceElementsInfo; + typedef NodeDeclarationInfo<ClauseList> ClauseListInfo; + typedef NodeDeclarationInfo<ExpressionNode*> VarDeclListInfo; + typedef NodeDeclarationInfo<ConstDeclList> ConstDeclListInfo; + typedef NodeDeclarationInfo<ParameterList> ParameterListInfo; + +} // namespace JSC + +#endif // NodeInfo_h diff --git a/Source/JavaScriptCore/parser/Nodes.cpp b/Source/JavaScriptCore/parser/Nodes.cpp index cf4fe450a..fc9ccd9f6 100644 --- a/Source/JavaScriptCore/parser/Nodes.cpp +++ b/Source/JavaScriptCore/parser/Nodes.cpp @@ -27,14 +27,16 @@ #include "Nodes.h" #include "NodeConstructors.h" +#include "BytecodeGenerator.h" #include "CallFrame.h" #include "Debugger.h" #include "JIT.h" #include "JSFunction.h" #include "JSGlobalObject.h" +#include "JSNameScope.h" #include "LabelScope.h" #include "Lexer.h" -#include "JSCInlines.h" +#include "Operations.h" #include "Parser.h" #include "PropertyNameArray.h" #include "RegExpObject.h" @@ -63,27 +65,20 @@ void SourceElements::append(StatementNode* statement) { if (statement->isEmptyStatement()) return; - - if (!m_head) { - m_head = statement; - m_tail = statement; - return; - } - - m_tail->setNext(statement); - m_tail = statement; + m_statements.append(statement); } StatementNode* SourceElements::singleStatement() const { - return m_head == m_tail ? m_head : nullptr; + size_t size = m_statements.size(); + return size == 1 ? m_statements[0] : 0; } // ------------------------------ ScopeNode ----------------------------- -ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, bool inStrictContext) +ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, bool inStrictContext) : StatementNode(endLocation) - , ParserArenaRoot(parserArena) + , ParserArenaRefCounted(vm) , m_startLineNumber(startLocation.line) , m_startStartOffset(startLocation.startOffset) , m_startLineStartOffset(startLocation.lineStartOffset) @@ -93,10 +88,9 @@ ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocat { } -ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack& funcStack, VariableEnvironment& lexicalVariables, CodeFeatures features, int numConstants) +ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, CodeFeatures features, int numConstants) : StatementNode(endLocation) - , ParserArenaRoot(parserArena) - , VariableEnvironmentNode(lexicalVariables) + , ParserArenaRefCounted(vm) , m_startLineNumber(startLocation.line) , m_startStartOffset(startLocation.startOffset) , m_startLineStartOffset(startLocation.lineStartOffset) @@ -105,8 +99,12 @@ ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocat , m_numConstants(numConstants) , m_statements(children) { - m_varDeclarations.swap(varEnvironment); - m_functionStack.swap(funcStack); + m_arena.swap(*vm->parserArena); + if (varStack) + m_varStack.swap(*varStack); + if (funcStack) + m_functionStack.swap(*funcStack); + m_capturedVariables.swap(capturedVariables); } StatementNode* ScopeNode::singleStatement() const @@ -116,90 +114,121 @@ StatementNode* ScopeNode::singleStatement() const // ------------------------------ ProgramNode ----------------------------- -ProgramNode::ProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, int numConstants) - : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, funcStack, lexicalVariables, features, numConstants) +inline ProgramNode::ProgramNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants) + : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants) , m_startColumn(startColumn) , m_endColumn(endColumn) { } -void ProgramNode::setClosedVariables(Vector<RefPtr<UniquedStringImpl>>&& closedVariables) +PassRefPtr<ProgramNode> ProgramNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants) { - m_closedVariables = WTF::move(closedVariables); -} + RefPtr<ProgramNode> node = new ProgramNode(vm, startLocation, endLocation, startColumn, endColumn, children, varStack, funcStack, capturedVariables, source, features, numConstants); -// ------------------------------ ModuleProgramNode ----------------------------- + ASSERT(node->m_arena.last() == node); + node->m_arena.removeLast(); + ASSERT(!node->m_arena.contains(node.get())); -ModuleProgramNode::ModuleProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, int numConstants) - : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, funcStack, lexicalVariables, features, numConstants) - , m_startColumn(startColumn) - , m_endColumn(endColumn) -{ + return node.release(); } // ------------------------------ EvalNode ----------------------------- -EvalNode::EvalNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, int numConstants) - : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, funcStack, lexicalVariables, features, numConstants) +inline EvalNode::EvalNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants) + : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants) , m_endColumn(endColumn) { } -// ------------------------------ FunctionMetadataNode ----------------------------- +PassRefPtr<EvalNode> EvalNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants) +{ + RefPtr<EvalNode> node = new EvalNode(vm, startLocation, endLocation, endColumn, children, varStack, funcStack, capturedVariables, source, features, numConstants); + + ASSERT(node->m_arena.last() == node); + node->m_arena.removeLast(); + ASSERT(!node->m_arena.contains(node.get())); + + return node.release(); +} + +// ------------------------------ FunctionBodyNode ----------------------------- -FunctionMetadataNode::FunctionMetadataNode( - ParserArena&, const JSTokenLocation& startLocation, - const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, - int functionKeywordStart, int functionNameStart, int parametersStart, bool isInStrictContext, - ConstructorKind constructorKind, unsigned parameterCount, SourceParseMode mode) - : Node(endLocation) - , m_startColumn(startColumn) - , m_endColumn(endColumn) - , m_functionKeywordStart(functionKeywordStart) - , m_functionNameStart(functionNameStart) - , m_parametersStart(parametersStart) - , m_startStartOffset(startLocation.startOffset) - , m_parameterCount(parameterCount) - , m_parseMode(mode) - , m_isInStrictContext(isInStrictContext) - , m_constructorKind(static_cast<unsigned>(constructorKind)) +PassRefPtr<FunctionParameters> FunctionParameters::create(ParameterNode* firstParameter) { - ASSERT(m_constructorKind == static_cast<unsigned>(constructorKind)); + unsigned parameterCount = 0; + for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam()) + ++parameterCount; + + size_t objectSize = sizeof(FunctionParameters) - sizeof(void*) + sizeof(DeconstructionPatternNode*) * parameterCount; + void* slot = fastMalloc(objectSize); + return adoptRef(new (slot) FunctionParameters(firstParameter, parameterCount)); } -void FunctionMetadataNode::finishParsing(const SourceCode& source, const Identifier& ident, enum FunctionMode functionMode) +FunctionParameters::FunctionParameters(ParameterNode* firstParameter, unsigned size) + : m_size(size) { - m_source = source; - m_ident = ident; - m_functionMode = functionMode; + unsigned i = 0; + for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam()) { + auto pattern = parameter->pattern(); + pattern->ref(); + patterns()[i++] = pattern; + } } -void FunctionMetadataNode::setEndPosition(JSTextPosition position) +FunctionParameters::~FunctionParameters() { - m_lastLine = position.line; - m_endColumn = position.offset - position.lineStartOffset; + for (unsigned i = 0; i < m_size; ++i) + patterns()[i]->deref(); } -// ------------------------------ FunctionNode ----------------------------- +inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, bool inStrictContext) + : ScopeNode(vm, startLocation, endLocation, inStrictContext) + , m_startColumn(startColumn) + , m_endColumn(endColumn) +{ +} -FunctionNode::FunctionNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters* parameters, const SourceCode& sourceCode, CodeFeatures features, int numConstants) - : ScopeNode(parserArena, startLocation, endLocation, sourceCode, children, varEnvironment, funcStack, lexicalVariables, features, numConstants) - , m_parameters(parameters) +inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants) + : ScopeNode(vm, startLocation, endLocation, sourceCode, children, varStack, funcStack, capturedVariables, features, numConstants) , m_startColumn(startColumn) , m_endColumn(endColumn) { } -void FunctionNode::finishParsing(const Identifier& ident, enum FunctionMode functionMode) +void FunctionBodyNode::finishParsing(const SourceCode& source, ParameterNode* firstParameter, const Identifier& ident, FunctionNameIsInScopeToggle functionNameIsInScopeToggle) +{ + setSource(source); + finishParsing(FunctionParameters::create(firstParameter), ident, functionNameIsInScopeToggle); +} + +void FunctionBodyNode::finishParsing(PassRefPtr<FunctionParameters> parameters, const Identifier& ident, FunctionNameIsInScopeToggle functionNameIsInScopeToggle) { ASSERT(!source().isNull()); + m_parameters = parameters; m_ident = ident; - m_functionMode = functionMode; + m_functionNameIsInScopeToggle = functionNameIsInScopeToggle; } -VariableEnvironmentNode::VariableEnvironmentNode(VariableEnvironment& lexicalVariables) +FunctionBodyNode* FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, bool inStrictContext) { - m_lexicalVariables.swap(lexicalVariables); + return new FunctionBodyNode(vm, startLocation, endLocation, startColumn, endColumn, inStrictContext); +} + +PassRefPtr<FunctionBodyNode> FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants) +{ + RefPtr<FunctionBodyNode> node = new FunctionBodyNode(vm, startLocation, endLocation, startColumn, endColumn , children, varStack, funcStack, capturedVariables, sourceCode, features, numConstants); + + ASSERT(node->m_arena.last() == node); + node->m_arena.removeLast(); + ASSERT(!node->m_arena.contains(node.get())); + + return node.release(); +} + +void FunctionBodyNode::setEndPosition(JSTextPosition position) +{ + m_lastLine = position.line; + m_endColumn = position.offset - position.lineStartOffset; } } // namespace JSC diff --git a/Source/JavaScriptCore/parser/Nodes.h b/Source/JavaScriptCore/parser/Nodes.h index 1d6b03d77..d779a178d 100644 --- a/Source/JavaScriptCore/parser/Nodes.h +++ b/Source/JavaScriptCore/parser/Nodes.h @@ -1,7 +1,7 @@ /* * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) - * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013, 2015 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All rights reserved. * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) * Copyright (C) 2007 Maks Orlovich * Copyright (C) 2007 Eric Seidel <eric@webkit.org> @@ -34,22 +34,19 @@ #include "ResultType.h" #include "SourceCode.h" #include "SymbolTable.h" -#include "VariableEnvironment.h" #include <wtf/MathExtras.h> namespace JSC { class ArgumentListNode; class BytecodeGenerator; - class FunctionMetadataNode; - class FunctionParameters; + class FunctionBodyNode; class Label; class PropertyListNode; class ReadModifyResolveNode; class RegisterID; class JSScope; class ScopeNode; - class ModuleAnalyzer; enum Operator { OpEqual, @@ -79,10 +76,12 @@ namespace JSC { }; inline FallThroughMode invert(FallThroughMode fallThroughMode) { return static_cast<FallThroughMode>(!fallThroughMode); } - typedef HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> IdentifierSet; + typedef HashSet<RefPtr<StringImpl>, IdentifierRepHash> IdentifierSet; namespace DeclarationStacks { - typedef Vector<FunctionMetadataNode*> FunctionStack; + enum VarAttrs { IsConstant = 1, HasInitializer = 2 }; + typedef Vector<std::pair<Identifier, unsigned>> VarStack; + typedef Vector<FunctionBodyNode*> FunctionStack; } struct SwitchInfo { @@ -91,17 +90,11 @@ namespace JSC { SwitchType switchType; }; - enum class AssignmentContext { - DeclarationStatement, - ConstDeclarationStatement, - AssignmentExpression - }; - class ParserArenaFreeable { public: // ParserArenaFreeable objects are are freed when the arena is deleted. // Destructors are not called. Clients must not call delete on such objects. - void* operator new(size_t, ParserArena&); + void* operator new(size_t, VM*); }; class ParserArenaDeletable { @@ -110,20 +103,24 @@ namespace JSC { // ParserArenaDeletable objects are deleted when the arena is deleted. // Clients must not call delete directly on such objects. - void* operator new(size_t, ParserArena&); + void* operator new(size_t, VM*); }; - class ParserArenaRoot { - WTF_MAKE_FAST_ALLOCATED; + template <typename T> + struct ParserArenaData : ParserArenaDeletable { + T data; + }; + + class ParserArenaRefCounted : public RefCounted<ParserArenaRefCounted> { + WTF_FASTMALLOC_OPERATORS; protected: - ParserArenaRoot(ParserArena&); + ParserArenaRefCounted(VM*); public: - ParserArena& parserArena() { return m_arena; } - virtual ~ParserArenaRoot() { } - - protected: - ParserArena m_arena; + virtual ~ParserArenaRefCounted() + { + ASSERT(deletionHasBegun()); + } }; class Node : public ParserArenaFreeable { @@ -133,17 +130,13 @@ namespace JSC { public: virtual ~Node() { } - int firstLine() const { return m_position.line; } + int lineNo() const { return m_position.line; } int startOffset() const { return m_position.offset; } - int endOffset() const { return m_endOffset; } int lineStartOffset() const { return m_position.lineStartOffset; } const JSTextPosition& position() const { return m_position; } - void setEndOffset(int offset) { m_endOffset = offset; } - void setStartOffset(int offset) { m_position.offset = offset; } protected: JSTextPosition m_position; - int m_endOffset; }; class ExpressionNode : public Node { @@ -159,11 +152,10 @@ namespace JSC { virtual bool isPure(BytecodeGenerator&) const { return false; } virtual bool isConstant() const { return false; } virtual bool isLocation() const { return false; } - virtual bool isAssignmentLocation() const { return isLocation(); } virtual bool isResolveNode() const { return false; } virtual bool isBracketAccessorNode() const { return false; } virtual bool isDotAccessorNode() const { return false; } - virtual bool isDestructuringNode() const { return false; } + virtual bool isDeconstructionNode() const { return false; } virtual bool isFuncExprNode() const { return false; } virtual bool isCommaNode() const { return false; } virtual bool isSimpleArray() const { return false; } @@ -171,7 +163,6 @@ namespace JSC { virtual bool isSubtract() const { return false; } virtual bool isBoolean() const { return false; } virtual bool isSpreadExpression() const { return false; } - virtual bool isSuperNode() const { return false; } virtual void emitBytecodeInConditionContext(BytecodeGenerator&, Label*, Label*, FallThroughMode); @@ -191,39 +182,20 @@ namespace JSC { virtual void emitBytecode(BytecodeGenerator&, RegisterID* destination = 0) = 0; void setLoc(unsigned firstLine, unsigned lastLine, int startOffset, int lineStartOffset); + unsigned firstLine() const { return lineNo(); } unsigned lastLine() const { return m_lastLine; } - StatementNode* next() { return m_next; } - void setNext(StatementNode* next) { m_next = next; } - virtual bool isEmptyStatement() const { return false; } virtual bool isReturnNode() const { return false; } virtual bool isExprStatement() const { return false; } virtual bool isBreak() const { return false; } virtual bool isContinue() const { return false; } virtual bool isBlock() const { return false; } - virtual bool isFuncDeclNode() const { return false; } - virtual bool isModuleDeclarationNode() const { return false; } protected: - StatementNode* m_next; int m_lastLine; }; - class VariableEnvironmentNode { - public: - VariableEnvironmentNode() - { - } - - VariableEnvironmentNode(VariableEnvironment& lexicalDeclaredVariables); - - VariableEnvironment& lexicalVariables() { return m_lexicalVariables; } - - protected: - VariableEnvironment m_lexicalVariables; - }; - class ConstantNode : public ExpressionNode { public: ConstantNode(const JSTokenLocation&, ResultType); @@ -259,32 +231,16 @@ namespace JSC { class NumberNode : public ConstantNode { public: NumberNode(const JSTokenLocation&, double value); - double value() const { return m_value; } - virtual bool isIntegerNode() const = 0; - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override final; + double value() { return m_value; } + void setValue(double value) { m_value = value; } private: - virtual bool isNumber() const override final { return true; } + virtual bool isNumber() const override { return true; } virtual JSValue jsValue(BytecodeGenerator&) const override { return jsNumber(m_value); } double m_value; }; - class DoubleNode : public NumberNode { - public: - DoubleNode(const JSTokenLocation&, double value); - - private: - virtual bool isIntegerNode() const override { return false; } - }; - - // An integer node represent a number represented as an integer (e.g. 42 instead of 42., 42.0, 42e0) - class IntegerNode : public DoubleNode { - public: - IntegerNode(const JSTokenLocation&, double value); - virtual bool isIntegerNode() const override final { return true; } - }; - class StringNode : public ConstantNode { public: StringNode(const JSTokenLocation&, const Identifier&); @@ -296,7 +252,7 @@ namespace JSC { const Identifier& m_value; }; - + class ThrowableExpressionData { public: ThrowableExpressionData() @@ -449,76 +405,6 @@ namespace JSC { uint16_t m_subexpressionLineStartOffset; }; -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - class TemplateExpressionListNode : public ParserArenaFreeable { - public: - TemplateExpressionListNode(ExpressionNode*); - TemplateExpressionListNode(TemplateExpressionListNode*, ExpressionNode*); - - ExpressionNode* value() { return m_node; } - TemplateExpressionListNode* next() { return m_next; } - - private: - TemplateExpressionListNode* m_next { nullptr }; - ExpressionNode* m_node { nullptr }; - }; - - class TemplateStringNode : public ExpressionNode { - public: - TemplateStringNode(const JSTokenLocation&, const Identifier& cooked, const Identifier& raw); - - const Identifier& cooked() { return m_cooked; } - const Identifier& raw() { return m_raw; } - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - const Identifier& m_cooked; - const Identifier& m_raw; - }; - - class TemplateStringListNode : public ParserArenaFreeable { - public: - TemplateStringListNode(TemplateStringNode*); - TemplateStringListNode(TemplateStringListNode*, TemplateStringNode*); - - TemplateStringNode* value() { return m_node; } - TemplateStringListNode* next() { return m_next; } - - private: - TemplateStringListNode* m_next { nullptr }; - TemplateStringNode* m_node { nullptr }; - }; - - class TemplateLiteralNode : public ExpressionNode { - public: - TemplateLiteralNode(const JSTokenLocation&, TemplateStringListNode*); - TemplateLiteralNode(const JSTokenLocation&, TemplateStringListNode*, TemplateExpressionListNode*); - - TemplateStringListNode* templateStrings() const { return m_templateStrings; } - TemplateExpressionListNode* templateExpressions() const { return m_templateExpressions; } - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - TemplateStringListNode* m_templateStrings; - TemplateExpressionListNode* m_templateExpressions; - }; - - class TaggedTemplateNode : public ExpressionNode, public ThrowableExpressionData { - public: - TaggedTemplateNode(const JSTokenLocation&, ExpressionNode*, TemplateLiteralNode*); - - TemplateLiteralNode* templateLiteral() const { return m_templateLiteral; } - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - ExpressionNode* m_tag; - TemplateLiteralNode* m_templateLiteral; - }; -#endif - class RegExpNode : public ExpressionNode, public ThrowableExpressionData { public: RegExpNode(const JSTokenLocation&, const Identifier& pattern, const Identifier& flags); @@ -532,26 +418,7 @@ namespace JSC { class ThisNode : public ExpressionNode { public: - ThisNode(const JSTokenLocation&, ThisTDZMode); - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - bool m_shouldAlwaysEmitTDZCheck; - }; - - class SuperNode final : public ExpressionNode { - public: - SuperNode(const JSTokenLocation&); - - private: - virtual bool isSuperNode() const override { return true; } - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - }; - - class NewTargetNode final : public ExpressionNode { - public: - NewTargetNode(const JSTokenLocation&); + ThisNode(const JSTokenLocation&); private: virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; @@ -595,7 +462,7 @@ namespace JSC { ArrayNode(const JSTokenLocation&, ElementNode*); ArrayNode(const JSTokenLocation&, int elision, ElementNode*); - ArgumentListNode* toArgumentList(ParserArena&, int, int) const; + ArgumentListNode* toArgumentList(VM*, int, int) const; ElementNode* elements() const { ASSERT(isSimpleArray()); return m_element; } private: @@ -610,27 +477,23 @@ namespace JSC { class PropertyNode : public ParserArenaFreeable { public: - enum Type { Constant = 1, Getter = 2, Setter = 4, Computed = 8, Shorthand = 16 }; - enum PutType { Unknown, KnownDirect }; - - PropertyNode(const Identifier&, ExpressionNode*, Type, PutType, SuperBinding); - PropertyNode(ExpressionNode* propertyName, ExpressionNode*, Type, PutType); + enum Type { Constant = 1, Getter = 2, Setter = 4 }; + PropertyNode(VM*, const Identifier&, ExpressionNode*, Type); + PropertyNode(VM*, double, ExpressionNode*, Type); + PropertyNode(VM*, ExpressionNode* propertyName, ExpressionNode*, Type); + ExpressionNode* expressionName() const { return m_expression; } const Identifier* name() const { return m_name; } - Type type() const { return static_cast<Type>(m_type); } - bool needsSuperBinding() const { return m_needsSuperBinding; } - PutType putType() const { return static_cast<PutType>(m_putType); } + Type type() const { return m_type; } private: friend class PropertyListNode; const Identifier* m_name; ExpressionNode* m_expression; ExpressionNode* m_assign; - unsigned m_type : 5; - unsigned m_needsSuperBinding : 1; - unsigned m_putType : 1; + Type m_type; }; class PropertyListNode : public ExpressionNode { @@ -638,10 +501,9 @@ namespace JSC { PropertyListNode(const JSTokenLocation&, PropertyNode*); PropertyListNode(const JSTokenLocation&, PropertyNode*, PropertyListNode*); - private: virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - void emitPutConstantProperty(BytecodeGenerator&, RegisterID*, PropertyNode&); + private: PropertyNode* m_node; PropertyListNode* m_next; }; @@ -773,7 +635,7 @@ namespace JSC { class FunctionCallBracketNode : public ExpressionNode, public ThrowableSubExpressionData { public: - FunctionCallBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, bool subscriptHasAssignments, ArgumentsNode*, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd); + FunctionCallBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, ArgumentsNode*, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd); private: virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; @@ -781,7 +643,6 @@ namespace JSC { ExpressionNode* m_base; ExpressionNode* m_subscript; ArgumentsNode* m_args; - bool m_subscriptHasAssignments; }; class FunctionCallDotNode : public ExpressionNode, public ThrowableSubExpressionData { @@ -797,26 +658,6 @@ namespace JSC { ArgumentsNode* m_args; }; - class BytecodeIntrinsicNode : public ExpressionNode, public ThrowableExpressionData { - public: - typedef RegisterID* (BytecodeIntrinsicNode::* EmitterType)(BytecodeGenerator&, RegisterID*); - - BytecodeIntrinsicNode(const JSTokenLocation&, EmitterType, const Identifier&, ArgumentsNode*, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd); - - const Identifier& identifier() const { return m_ident; } - -#define JSC_DECLARE_BYTECODE_INTRINSIC_FUNCTIONS(name) RegisterID* emit_intrinsic_##name(BytecodeGenerator&, RegisterID*); - JSC_COMMON_BYTECODE_INTRINSICS_EACH_NAME(JSC_DECLARE_BYTECODE_INTRINSIC_FUNCTIONS) -#undef JSC_DECLARE_BYTECODE_INTRINSIC_FUNCTIONS - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - EmitterType m_emitter; - const Identifier& m_ident; - ArgumentsNode* m_args; - }; - class CallFunctionCallDotNode : public FunctionCallDotNode { public: CallFunctionCallDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, ArgumentsNode*, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd); @@ -1179,14 +1020,13 @@ namespace JSC { class AssignResolveNode : public ExpressionNode, public ThrowableExpressionData { public: - AssignResolveNode(const JSTokenLocation&, const Identifier&, ExpressionNode* right, AssignmentContext); + AssignResolveNode(const JSTokenLocation&, const Identifier&, ExpressionNode* right); private: virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; const Identifier& m_ident; ExpressionNode* m_right; - AssignmentContext m_assignmentContext; }; class ReadModifyBracketNode : public ExpressionNode, public ThrowableSubExpressionData { @@ -1199,7 +1039,7 @@ namespace JSC { ExpressionNode* m_base; ExpressionNode* m_subscript; ExpressionNode* m_right; - unsigned m_operator : 30; + Operator m_operator : 30; bool m_subscriptHasAssignments : 1; bool m_rightHasAssignments : 1; }; @@ -1241,7 +1081,7 @@ namespace JSC { ExpressionNode* m_base; const Identifier& m_ident; ExpressionNode* m_right; - unsigned m_operator : 31; + Operator m_operator : 31; bool m_rightHasAssignments : 1; }; @@ -1253,22 +1093,54 @@ namespace JSC { virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; }; - class CommaNode final : public ExpressionNode { + typedef Vector<ExpressionNode*, 8> ExpressionVector; + + class CommaNode : public ExpressionNode, public ParserArenaDeletable { public: - CommaNode(const JSTokenLocation&, ExpressionNode*); + CommaNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2); + + using ParserArenaDeletable::operator new; - void setNext(CommaNode* next) { m_next = next; } - CommaNode* next() { return m_next; } + void append(ExpressionNode* expr) { ASSERT(expr); m_expressions.append(expr); } private: virtual bool isCommaNode() const override { return true; } virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - ExpressionNode* m_expr; - CommaNode* m_next; + ExpressionVector m_expressions; }; - class SourceElements final : public ParserArenaFreeable { + class ConstDeclNode : public ExpressionNode { + public: + ConstDeclNode(const JSTokenLocation&, const Identifier&, ExpressionNode*); + + bool hasInitializer() const { return m_init; } + const Identifier& ident() { return m_ident; } + + private: + virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; + virtual RegisterID* emitCodeSingle(BytecodeGenerator&); + + const Identifier& m_ident; + + public: + ConstDeclNode* m_next; + + private: + ExpressionNode* m_init; + }; + + class ConstStatementNode : public StatementNode { + public: + ConstStatementNode(const JSTokenLocation&, ConstDeclNode* next); + + private: + virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; + + ConstDeclNode* m_next; + }; + + class SourceElements : public ParserArenaDeletable { public: SourceElements(); @@ -1278,16 +1150,14 @@ namespace JSC { StatementNode* lastStatement() const; void emitBytecode(BytecodeGenerator&, RegisterID* destination); - void analyzeModule(ModuleAnalyzer&); private: - StatementNode* m_head; - StatementNode* m_tail; + Vector<StatementNode*> m_statements; }; - class BlockNode : public StatementNode, public VariableEnvironmentNode { + class BlockNode : public StatementNode { public: - BlockNode(const JSTokenLocation&, SourceElements*, VariableEnvironment&); + BlockNode(const JSTokenLocation&, SourceElements* = 0); StatementNode* singleStatement() const; StatementNode* lastStatement() const; @@ -1332,35 +1202,15 @@ namespace JSC { ExpressionNode* m_expr; }; - class DeclarationStatement : public StatementNode { + class VarStatementNode : public StatementNode { public: - DeclarationStatement(const JSTokenLocation&, ExpressionNode*); + VarStatementNode(const JSTokenLocation&, ExpressionNode*); private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; ExpressionNode* m_expr; }; - class EmptyVarExpression : public ExpressionNode { - public: - EmptyVarExpression(const JSTokenLocation&, const Identifier&); - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - const Identifier& m_ident; - }; - - class EmptyLetExpression : public ExpressionNode { - public: - EmptyLetExpression(const JSTokenLocation&, const Identifier&); - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - const Identifier& m_ident; - }; - class IfElseNode : public StatementNode { public: IfElseNode(const JSTokenLocation&, ExpressionNode* condition, StatementNode* ifBlock, StatementNode* elseBlock); @@ -1397,9 +1247,9 @@ namespace JSC { StatementNode* m_statement; }; - class ForNode : public StatementNode, public VariableEnvironmentNode { + class ForNode : public StatementNode { public: - ForNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, ExpressionNode* expr3, StatementNode*, VariableEnvironment&); + ForNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, ExpressionNode* expr3, StatementNode*); private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; @@ -1410,11 +1260,12 @@ namespace JSC { StatementNode* m_statement; }; - class DestructuringPatternNode; + class DeconstructionPatternNode; - class EnumerationNode : public StatementNode, public ThrowableExpressionData, public VariableEnvironmentNode { + class EnumerationNode : public StatementNode, public ThrowableExpressionData { public: - EnumerationNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*, VariableEnvironment&); + EnumerationNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*); + EnumerationNode(VM*, const JSTokenLocation&, DeconstructionPatternNode*, ExpressionNode*, StatementNode*); protected: ExpressionNode* m_lexpr; @@ -1424,19 +1275,17 @@ namespace JSC { class ForInNode : public EnumerationNode { public: - ForInNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*, VariableEnvironment&); + ForInNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*); + ForInNode(VM*, const JSTokenLocation&, DeconstructionPatternNode*, ExpressionNode*, StatementNode*); private: - RegisterID* tryGetBoundLocal(BytecodeGenerator&); - void emitLoopHeader(BytecodeGenerator&, RegisterID* propertyName); - void emitMultiLoopBytecode(BytecodeGenerator&, RegisterID* dst); - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; }; class ForOfNode : public EnumerationNode { public: - ForOfNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*, VariableEnvironment&); + ForOfNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*); + ForOfNode(VM*, const JSTokenLocation&, DeconstructionPatternNode*, ExpressionNode*, StatementNode*); private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; @@ -1444,6 +1293,7 @@ namespace JSC { class ContinueNode : public StatementNode, public ThrowableExpressionData { public: + ContinueNode(VM*, const JSTokenLocation&); ContinueNode(const JSTokenLocation&, const Identifier&); Label* trivialTarget(BytecodeGenerator&); @@ -1456,6 +1306,7 @@ namespace JSC { class BreakNode : public StatementNode, public ThrowableExpressionData { public: + BreakNode(VM*, const JSTokenLocation&); BreakNode(const JSTokenLocation&, const Identifier&); Label* trivialTarget(BytecodeGenerator&); @@ -1516,26 +1367,48 @@ namespace JSC { class TryNode : public StatementNode { public: - TryNode(const JSTokenLocation&, StatementNode* tryBlock, const Identifier& exceptionIdent, StatementNode* catchBlock, VariableEnvironment& catchEnvironment, StatementNode* finallyBlock); + TryNode(const JSTokenLocation&, StatementNode* tryBlock, const Identifier& exceptionIdent, StatementNode* catchBlock, StatementNode* finallyBlock); private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; StatementNode* m_tryBlock; - const Identifier& m_thrownValueIdent; + const Identifier& m_exceptionIdent; StatementNode* m_catchBlock; StatementNode* m_finallyBlock; - VariableEnvironment m_catchEnvironment; }; - class ScopeNode : public StatementNode, public ParserArenaRoot, public VariableEnvironmentNode { + class ParameterNode : public ParserArenaDeletable { + public: + ParameterNode(PassRefPtr<DeconstructionPatternNode>); + ParameterNode(ParameterNode*, PassRefPtr<DeconstructionPatternNode>); + + DeconstructionPatternNode* pattern() const { return m_pattern.get(); } + ParameterNode* nextParam() const { return m_next; } + + private: + RefPtr<DeconstructionPatternNode> m_pattern; + ParameterNode* m_next; + }; + + class ScopeNode : public StatementNode, public ParserArenaRefCounted { public: + typedef DeclarationStacks::VarStack VarStack; typedef DeclarationStacks::FunctionStack FunctionStack; - ScopeNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, bool inStrictContext); - ScopeNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, const SourceCode&, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, CodeFeatures, int numConstants); + ScopeNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, bool inStrictContext); + ScopeNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, const SourceCode&, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, CodeFeatures, int numConstants); - using ParserArenaRoot::operator new; + using ParserArenaRefCounted::operator new; + + void destroyData() + { + m_arena.reset(); + m_varStack.clear(); + m_functionStack.clear(); + m_statements = 0; + m_capturedVariables.clear(); + } const SourceCode& source() const { return m_source; } const String& sourceURL() const { return m_source.provider()->url(); } @@ -1551,17 +1424,16 @@ namespace JSC { bool usesEval() const { return m_features & EvalFeature; } bool usesArguments() const { return (m_features & ArgumentsFeature) && !(m_features & ShadowsArgumentsFeature); } bool modifiesParameter() const { return m_features & ModifiedParameterFeature; } - bool modifiesArguments() const { return m_features & (EvalFeature | ModifiedArgumentsFeature); } bool isStrictMode() const { return m_features & StrictModeFeature; } void setUsesArguments() { m_features |= ArgumentsFeature; } bool usesThis() const { return m_features & ThisFeature; } bool needsActivationForMoreThanVariables() const { return m_features & (EvalFeature | WithFeature | CatchFeature); } bool needsActivation() const { return (hasCapturedVariables()) || (m_features & (EvalFeature | WithFeature | CatchFeature)); } - bool hasCapturedVariables() const { return m_varDeclarations.hasCapturedVariables(); } - bool captures(UniquedStringImpl* uid) { return m_varDeclarations.captures(uid); } - bool captures(const Identifier& ident) { return captures(ident.impl()); } + bool hasCapturedVariables() const { return !!m_capturedVariables.size(); } + size_t capturedVariableCount() const { return m_capturedVariables.size(); } + bool captures(const Identifier& ident) { return m_capturedVariables.contains(ident.impl()); } - VariableEnvironment& varDeclarations() { return m_varDeclarations; } + VarStack& varStack() { return m_varStack; } FunctionStack& functionStack() { return m_functionStack; } int neededConstants() @@ -1574,12 +1446,11 @@ namespace JSC { StatementNode* singleStatement() const; void emitStatementsBytecode(BytecodeGenerator&, RegisterID* destination); - - void setClosedVariables(Vector<RefPtr<UniquedStringImpl>>&&) { } - - void analyzeModule(ModuleAnalyzer&); protected: + void setSource(const SourceCode& source) { m_source = source; } + ParserArena m_arena; + int m_startLineNumber; unsigned m_startStartOffset; unsigned m_startLineStartOffset; @@ -1587,34 +1458,36 @@ namespace JSC { private: CodeFeatures m_features; SourceCode m_source; - VariableEnvironment m_varDeclarations; + VarStack m_varStack; FunctionStack m_functionStack; int m_numConstants; SourceElements* m_statements; + IdentifierSet m_capturedVariables; }; class ProgramNode : public ScopeNode { public: - ProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, int numConstants); + static const bool isFunctionNode = false; + static PassRefPtr<ProgramNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); unsigned startColumn() const { return m_startColumn; } unsigned endColumn() const { return m_endColumn; } static const bool scopeIsFunction = false; - void setClosedVariables(Vector<RefPtr<UniquedStringImpl>>&&); - const Vector<RefPtr<UniquedStringImpl>>& closedVariables() const { return m_closedVariables; } - private: + ProgramNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); + virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - Vector<RefPtr<UniquedStringImpl>> m_closedVariables; + unsigned m_startColumn; unsigned m_endColumn; }; class EvalNode : public ScopeNode { public: - EvalNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, int numConstants); + static const bool isFunctionNode = false; + static PassRefPtr<EvalNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); ALWAYS_INLINE unsigned startColumn() const { return 0; } unsigned endColumn() const { return m_endColumn; } @@ -1622,309 +1495,94 @@ namespace JSC { static const bool scopeIsFunction = false; private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - unsigned m_endColumn; - }; - - class ModuleProgramNode : public ScopeNode { - public: - ModuleProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, int numConstants); - - unsigned startColumn() const { return m_startColumn; } - unsigned endColumn() const { return m_endColumn; } + EvalNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); - static const bool scopeIsFunction = false; - - private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - unsigned m_startColumn; - unsigned m_endColumn; - }; - - class ModuleNameNode : public Node { - public: - ModuleNameNode(const JSTokenLocation&, const Identifier& moduleName); - const Identifier& moduleName() { return m_moduleName; } - - private: - const Identifier& m_moduleName; - }; - - class ImportSpecifierNode : public Node { - public: - ImportSpecifierNode(const JSTokenLocation&, const Identifier& importedName, const Identifier& localName); - - const Identifier& importedName() { return m_importedName; } - const Identifier& localName() { return m_localName; } - - private: - const Identifier& m_importedName; - const Identifier& m_localName; - }; - - class ImportSpecifierListNode : public ParserArenaDeletable { - public: - typedef Vector<ImportSpecifierNode*, 3> Specifiers; - - const Specifiers& specifiers() const { return m_specifiers; } - void append(ImportSpecifierNode* specifier) - { - m_specifiers.append(specifier); - } - - private: - Specifiers m_specifiers; - }; - - class ModuleDeclarationNode : public StatementNode { - public: - virtual void analyzeModule(ModuleAnalyzer&) = 0; - virtual bool isModuleDeclarationNode() const { return true; } - - protected: - ModuleDeclarationNode(const JSTokenLocation&); - }; - - class ImportDeclarationNode : public ModuleDeclarationNode { - public: - ImportDeclarationNode(const JSTokenLocation&, ImportSpecifierListNode*, ModuleNameNode*); - - ImportSpecifierListNode* specifierList() const { return m_specifierList; } - ModuleNameNode* moduleName() const { return m_moduleName; } - - private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - virtual void analyzeModule(ModuleAnalyzer&) override; - - ImportSpecifierListNode* m_specifierList; - ModuleNameNode* m_moduleName; - }; - - class ExportAllDeclarationNode : public ModuleDeclarationNode { - public: - ExportAllDeclarationNode(const JSTokenLocation&, ModuleNameNode*); - - ModuleNameNode* moduleName() const { return m_moduleName; } - - private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - virtual void analyzeModule(ModuleAnalyzer&) override; - - ModuleNameNode* m_moduleName; - }; - - class ExportDefaultDeclarationNode : public ModuleDeclarationNode { - public: - ExportDefaultDeclarationNode(const JSTokenLocation&, StatementNode*, const Identifier& localName); - - const StatementNode& declaration() const { return *m_declaration; } - const Identifier& localName() const { return m_localName; } - - private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - virtual void analyzeModule(ModuleAnalyzer&) override; - StatementNode* m_declaration; - const Identifier& m_localName; - }; - - class ExportLocalDeclarationNode : public ModuleDeclarationNode { - public: - ExportLocalDeclarationNode(const JSTokenLocation&, StatementNode*); - - const StatementNode& declaration() const { return *m_declaration; } - - private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - virtual void analyzeModule(ModuleAnalyzer&) override; - StatementNode* m_declaration; + unsigned m_endColumn; }; - class ExportSpecifierNode : public Node { + class FunctionParameters : public RefCounted<FunctionParameters> { + WTF_MAKE_FAST_ALLOCATED; + WTF_MAKE_NONCOPYABLE(FunctionParameters); public: - ExportSpecifierNode(const JSTokenLocation&, const Identifier& localName, const Identifier& exportedName); + static PassRefPtr<FunctionParameters> create(ParameterNode*); + ~FunctionParameters(); - const Identifier& exportedName() { return m_exportedName; } - const Identifier& localName() { return m_localName; } + unsigned size() const { return m_size; } + DeconstructionPatternNode* at(unsigned index) { ASSERT(index < m_size); return patterns()[index]; } private: - const Identifier& m_localName; - const Identifier& m_exportedName; - }; + FunctionParameters(ParameterNode*, unsigned size); - class ExportSpecifierListNode : public ParserArenaDeletable { - public: - typedef Vector<ExportSpecifierNode*, 3> Specifiers; + DeconstructionPatternNode** patterns() { return &m_storage; } - const Specifiers& specifiers() const { return m_specifiers; } - void append(ExportSpecifierNode* specifier) - { - m_specifiers.append(specifier); - } - - private: - Specifiers m_specifiers; + unsigned m_size; + DeconstructionPatternNode* m_storage; }; - class ExportNamedDeclarationNode : public ModuleDeclarationNode { + class FunctionBodyNode : public ScopeNode { public: - ExportNamedDeclarationNode(const JSTokenLocation&, ExportSpecifierListNode*, ModuleNameNode*); + static const bool isFunctionNode = true; + static FunctionBodyNode* create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, bool isStrictMode); + static PassRefPtr<FunctionBodyNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); - ExportSpecifierListNode* specifierList() const { return m_specifierList; } - ModuleNameNode* moduleName() const { return m_moduleName; } + FunctionParameters* parameters() const { return m_parameters.get(); } + size_t parameterCount() const { return m_parameters->size(); } - private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - virtual void analyzeModule(ModuleAnalyzer&) override; - ExportSpecifierListNode* m_specifierList; - ModuleNameNode* m_moduleName { nullptr }; - }; - - class FunctionParameters : public ParserArenaDeletable { - public: - FunctionParameters(); - ALWAYS_INLINE unsigned size() const { return m_patterns.size(); } - ALWAYS_INLINE std::pair<DestructuringPatternNode*, ExpressionNode*> at(unsigned index) { return m_patterns[index]; } - bool hasDefaultParameterValues() const { return m_hasDefaultParameterValues; } - ALWAYS_INLINE void append(DestructuringPatternNode* pattern, ExpressionNode* defaultValue) - { - ASSERT(pattern); - m_patterns.append(std::make_pair(pattern, defaultValue)); - if (defaultValue) - m_hasDefaultParameterValues = true; - } - - private: - - Vector<std::pair<DestructuringPatternNode*, ExpressionNode*>, 3> m_patterns; - bool m_hasDefaultParameterValues { false }; - }; - - class FunctionMetadataNode final : public Node, public ParserArenaDeletable { - public: - using ParserArenaDeletable::operator new; - - FunctionMetadataNode( - ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, - unsigned startColumn, unsigned endColumn, int functionKeywordStart, - int functionNameStart, int parametersStart, bool isInStrictContext, - ConstructorKind, unsigned, SourceParseMode); - void finishParsing(const SourceCode&, const Identifier&, FunctionMode); + void finishParsing(const SourceCode&, ParameterNode*, const Identifier&, FunctionNameIsInScopeToggle); + void finishParsing(PassRefPtr<FunctionParameters>, const Identifier&, FunctionNameIsInScopeToggle); - void overrideName(const Identifier& ident) { m_ident = ident; } const Identifier& ident() { return m_ident; } void setInferredName(const Identifier& inferredName) { ASSERT(!inferredName.isNull()); m_inferredName = inferredName; } const Identifier& inferredName() { return m_inferredName.isEmpty() ? m_ident : m_inferredName; } - FunctionMode functionMode() { return m_functionMode; } + bool functionNameIsInScope() { return m_functionNameIsInScopeToggle == FunctionNameIsInScope; } + FunctionNameIsInScopeToggle functionNameIsInScopeToggle() { return m_functionNameIsInScopeToggle; } + void setFunctionNameStart(int functionNameStart) { m_functionNameStart = functionNameStart; } int functionNameStart() const { return m_functionNameStart; } - int functionKeywordStart() const { return m_functionKeywordStart; } - int parametersStart() const { return m_parametersStart; } unsigned startColumn() const { return m_startColumn; } unsigned endColumn() const { return m_endColumn; } - unsigned parameterCount() const { return m_parameterCount; } - SourceParseMode parseMode() const { return m_parseMode; } void setEndPosition(JSTextPosition); - const SourceCode& source() const { return m_source; } - - int startStartOffset() const { return m_startStartOffset; } - bool isInStrictContext() const { return m_isInStrictContext; } - ConstructorKind constructorKind() { return static_cast<ConstructorKind>(m_constructorKind); } + static const bool scopeIsFunction = true; - void setLoc(unsigned firstLine, unsigned lastLine, int startOffset, int lineStartOffset) - { - m_lastLine = lastLine; - m_position = JSTextPosition(firstLine, startOffset, lineStartOffset); - ASSERT(m_position.offset >= m_position.lineStartOffset); - } - unsigned lastLine() const { return m_lastLine; } + private: + FunctionBodyNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, bool inStrictContext); + FunctionBodyNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants); - protected: Identifier m_ident; Identifier m_inferredName; - FunctionMode m_functionMode; - unsigned m_startColumn; - unsigned m_endColumn; - int m_functionKeywordStart; + FunctionNameIsInScopeToggle m_functionNameIsInScopeToggle; + RefPtr<FunctionParameters> m_parameters; int m_functionNameStart; - int m_parametersStart; - SourceCode m_source; - int m_startStartOffset; - unsigned m_parameterCount; - int m_lastLine; - SourceParseMode m_parseMode; - unsigned m_isInStrictContext : 1; - unsigned m_constructorKind : 2; - }; - - class FunctionNode final : public ScopeNode { - public: - FunctionNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, int numConstants); - - FunctionParameters* parameters() const { return m_parameters; } - - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - void finishParsing(const Identifier&, FunctionMode); - - const Identifier& ident() { return m_ident; } - - FunctionMode functionMode() { return m_functionMode; } - - unsigned startColumn() const { return m_startColumn; } - unsigned endColumn() const { return m_endColumn; } - - static const bool scopeIsFunction = true; - - private: - Identifier m_ident; - FunctionMode m_functionMode; - FunctionParameters* m_parameters; unsigned m_startColumn; unsigned m_endColumn; }; class FuncExprNode : public ExpressionNode { public: - FuncExprNode(const JSTokenLocation&, const Identifier&, FunctionMetadataNode*, const SourceCode&); + FuncExprNode(const JSTokenLocation&, const Identifier&, FunctionBodyNode*, const SourceCode&, ParameterNode* = 0); - FunctionMetadataNode* metadata() { return m_metadata; } + FunctionBodyNode* body() { return m_body; } private: virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; virtual bool isFuncExprNode() const override { return true; } - FunctionMetadataNode* m_metadata; + FunctionBodyNode* m_body; }; -#if ENABLE(ES6_CLASS_SYNTAX) - class ClassExprNode final : public ExpressionNode { - public: - ClassExprNode(const JSTokenLocation&, const Identifier&, ExpressionNode* constructorExpresssion, - ExpressionNode* parentClass, PropertyListNode* instanceMethods, PropertyListNode* staticMethods); - - const Identifier& name() { return m_name; } - - private: - virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - const Identifier& m_name; - ExpressionNode* m_constructorExpression; - ExpressionNode* m_classHeritage; - PropertyListNode* m_instanceMethods; - PropertyListNode* m_staticMethods; - }; -#endif + class DeconstructionPatternNode : public RefCounted<DeconstructionPatternNode> { + WTF_MAKE_NONCOPYABLE(DeconstructionPatternNode); + WTF_MAKE_FAST_ALLOCATED; - class DestructuringPatternNode : public ParserArenaFreeable { public: - virtual ~DestructuringPatternNode() { } virtual void collectBoundIdentifiers(Vector<Identifier>&) const = 0; virtual void bindValue(BytecodeGenerator&, RegisterID* source) const = 0; virtual void toString(StringBuilder&) const = 0; @@ -1932,72 +1590,68 @@ namespace JSC { virtual bool isBindingNode() const { return false; } virtual RegisterID* emitDirectBinding(BytecodeGenerator&, RegisterID*, ExpressionNode*) { return 0; } + virtual ~DeconstructionPatternNode() = 0; + protected: - DestructuringPatternNode(); + DeconstructionPatternNode(VM*); }; - class ArrayPatternNode : public DestructuringPatternNode, public ThrowableExpressionData, public ParserArenaDeletable { + class ArrayPatternNode : public DeconstructionPatternNode { public: - using ParserArenaDeletable::operator new; - - ArrayPatternNode(); - enum class BindingType { - Elision, - Element, - RestElement - }; - - void appendIndex(BindingType bindingType, const JSTokenLocation&, DestructuringPatternNode* node, ExpressionNode* defaultValue) + static PassRefPtr<ArrayPatternNode> create(VM*); + void appendIndex(const JSTokenLocation&, DeconstructionPatternNode* node) { - m_targetPatterns.append({ bindingType, node, defaultValue }); + m_targetPatterns.append(node); } private: - struct Entry { - BindingType bindingType; - DestructuringPatternNode* pattern; - ExpressionNode* defaultValue; - }; + ArrayPatternNode(VM*); virtual void collectBoundIdentifiers(Vector<Identifier>&) const override; virtual void bindValue(BytecodeGenerator&, RegisterID*) const override; virtual RegisterID* emitDirectBinding(BytecodeGenerator&, RegisterID* dst, ExpressionNode*) override; virtual void toString(StringBuilder&) const override; - Vector<Entry> m_targetPatterns; + Vector<RefPtr<DeconstructionPatternNode>> m_targetPatterns; }; - class ObjectPatternNode : public DestructuringPatternNode, public ParserArenaDeletable { + class ObjectPatternNode : public DeconstructionPatternNode { public: - using ParserArenaDeletable::operator new; - - ObjectPatternNode(); - void appendEntry(const JSTokenLocation&, const Identifier& identifier, bool wasString, DestructuringPatternNode* pattern, ExpressionNode* defaultValue) + static PassRefPtr<ObjectPatternNode> create(VM*); + void appendEntry(const JSTokenLocation&, const Identifier& identifier, bool wasString, DeconstructionPatternNode* pattern) { - m_targetPatterns.append(Entry{ identifier, wasString, pattern, defaultValue }); + m_targetPatterns.append(Entry(identifier, wasString, pattern)); } - + private: + ObjectPatternNode(VM*); virtual void collectBoundIdentifiers(Vector<Identifier>&) const override; virtual void bindValue(BytecodeGenerator&, RegisterID*) const override; virtual void toString(StringBuilder&) const override; struct Entry { - const Identifier& propertyName; + Entry(const Identifier& propertyName, bool wasString, DeconstructionPatternNode* pattern) + : propertyName(propertyName) + , wasString(wasString) + , pattern(pattern) + { + } + Identifier propertyName; bool wasString; - DestructuringPatternNode* pattern; - ExpressionNode* defaultValue; + RefPtr<DeconstructionPatternNode> pattern; }; Vector<Entry> m_targetPatterns; }; - class BindingNode : public DestructuringPatternNode { + class BindingNode : public DeconstructionPatternNode { public: - BindingNode(const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext); + static PassRefPtr<BindingNode> create(VM*, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end); const Identifier& boundProperty() const { return m_boundProperty; } const JSTextPosition& divotStart() const { return m_divotStart; } const JSTextPosition& divotEnd() const { return m_divotEnd; } private: + BindingNode(VM*, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end); + virtual void collectBoundIdentifiers(Vector<Identifier>&) const override; virtual void bindValue(BytecodeGenerator&, RegisterID*) const override; virtual void toString(StringBuilder&) const override; @@ -2006,49 +1660,37 @@ namespace JSC { JSTextPosition m_divotStart; JSTextPosition m_divotEnd; - const Identifier& m_boundProperty; - AssignmentContext m_bindingContext; + Identifier m_boundProperty; }; - class DestructuringAssignmentNode : public ExpressionNode { + class DeconstructingAssignmentNode : public ExpressionNode, public ParserArenaDeletable { public: - DestructuringAssignmentNode(const JSTokenLocation&, DestructuringPatternNode*, ExpressionNode*); - DestructuringPatternNode* bindings() { return m_bindings; } + DeconstructingAssignmentNode(const JSTokenLocation&, PassRefPtr<DeconstructionPatternNode>, ExpressionNode*); + DeconstructionPatternNode* bindings() { return m_bindings.get(); } + using ParserArenaDeletable::operator new; + private: - virtual bool isAssignmentLocation() const override { return true; } - virtual bool isDestructuringNode() const override { return true; } + virtual bool isLocation() const override { return true; } + virtual bool isDeconstructionNode() const override { return true; } virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - DestructuringPatternNode* m_bindings; + RefPtr<DeconstructionPatternNode> m_bindings; ExpressionNode* m_initializer; }; class FuncDeclNode : public StatementNode { public: - FuncDeclNode(const JSTokenLocation&, const Identifier&, FunctionMetadataNode*, const SourceCode&); + FuncDeclNode(const JSTokenLocation&, const Identifier&, FunctionBodyNode*, const SourceCode&, ParameterNode* = 0); - virtual bool isFuncDeclNode() const override { return true; } - FunctionMetadataNode* metadata() { return m_metadata; } + FunctionBodyNode* body() { return m_body; } private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - FunctionMetadataNode* m_metadata; + FunctionBodyNode* m_body; }; -#if ENABLE(ES6_CLASS_SYNTAX) - class ClassDeclNode final : public StatementNode { - public: - ClassDeclNode(const JSTokenLocation&, ExpressionNode* classExpression); - - private: - virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; - - ExpressionNode* m_classDeclaration; - }; -#endif - class CaseClauseNode : public ParserArenaFreeable { public: CaseClauseNode(ExpressionNode*, SourceElements* = 0); @@ -2056,12 +1698,10 @@ namespace JSC { ExpressionNode* expr() const { return m_expr; } void emitBytecode(BytecodeGenerator&, RegisterID* destination); - void setStartOffset(int offset) { m_startOffset = offset; } private: ExpressionNode* m_expr; SourceElements* m_statements; - int m_startOffset; }; class ClauseListNode : public ParserArenaFreeable { @@ -2091,9 +1731,9 @@ namespace JSC { ClauseListNode* m_list2; }; - class SwitchNode : public StatementNode, public VariableEnvironmentNode { + class SwitchNode : public StatementNode { public: - SwitchNode(const JSTokenLocation&, ExpressionNode*, CaseBlockNode*, VariableEnvironment&); + SwitchNode(const JSTokenLocation&, ExpressionNode*, CaseBlockNode*); private: virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override; @@ -2117,6 +1757,16 @@ namespace JSC { ArgumentListNode* tail; }; + struct ConstDeclList { + ConstDeclNode* head; + ConstDeclNode* tail; + }; + + struct ParameterList { + ParameterNode* head; + ParameterNode* tail; + }; + struct ClauseList { ClauseListNode* head; ClauseListNode* tail; diff --git a/Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp b/Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp deleted file mode 100644 index 633deaece..000000000 --- a/Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "Nodes.h" -#include "NodeConstructors.h" - -#include "ModuleAnalyzer.h" -#include "ModuleRecord.h" - -namespace JSC { - -void ScopeNode::analyzeModule(ModuleAnalyzer& analyzer) -{ - m_statements->analyzeModule(analyzer); -} - -void SourceElements::analyzeModule(ModuleAnalyzer& analyzer) -{ - // In the module analyzer phase, only module declarations are included in the top-level SourceElements. - for (StatementNode* statement = m_head; statement; statement = statement->next()) { - ASSERT(statement->isModuleDeclarationNode()); - static_cast<ModuleDeclarationNode*>(statement)->analyzeModule(analyzer); - } -} - -void ImportDeclarationNode::analyzeModule(ModuleAnalyzer& analyzer) -{ - analyzer.moduleRecord().appendRequestedModule(m_moduleName->moduleName()); - for (auto* specifier : m_specifierList->specifiers()) { - analyzer.moduleRecord().addImportEntry(ModuleRecord::ImportEntry { - m_moduleName->moduleName(), - specifier->importedName(), - specifier->localName() - }); - } -} - -void ExportAllDeclarationNode::analyzeModule(ModuleAnalyzer& analyzer) -{ - analyzer.moduleRecord().appendRequestedModule(m_moduleName->moduleName()); - analyzer.moduleRecord().addStarExportEntry(m_moduleName->moduleName()); -} - -void ExportDefaultDeclarationNode::analyzeModule(ModuleAnalyzer& analyzer) -{ - analyzer.declareExportAlias(m_localName, analyzer.vm().propertyNames->defaultKeyword); -} - -void ExportLocalDeclarationNode::analyzeModule(ModuleAnalyzer&) -{ -} - -void ExportNamedDeclarationNode::analyzeModule(ModuleAnalyzer& analyzer) -{ - if (m_moduleName) - analyzer.moduleRecord().appendRequestedModule(m_moduleName->moduleName()); - - for (auto* specifier : m_specifierList->specifiers()) { - if (m_moduleName) { - // export { v } from "mod" - // - // In this case, no local variable names are imported into the current module. - // "v" indirectly points the binding in "mod". - analyzer.moduleRecord().addExportEntry(ModuleRecord::ExportEntry::createIndirect(specifier->exportedName(), specifier->localName(), m_moduleName->moduleName())); - continue; - } - - if (specifier->localName() != specifier->exportedName()) - analyzer.declareExportAlias(specifier->localName(), specifier->exportedName()); - } -} - -} // namespace JSC diff --git a/Source/JavaScriptCore/parser/Parser.cpp b/Source/JavaScriptCore/parser/Parser.cpp index aee70b6a7..297666e72 100644 --- a/Source/JavaScriptCore/parser/Parser.cpp +++ b/Source/JavaScriptCore/parser/Parser.cpp @@ -28,11 +28,12 @@ #include "Debugger.h" #include "JSCJSValueInlines.h" #include "Lexer.h" -#include "JSCInlines.h" +#include "NodeInfo.h" #include "SourceProvider.h" #include "VM.h" #include <utility> #include <wtf/HashFunctions.h> +#include <wtf/OwnPtr.h> #include <wtf/StringPrintStream.h> #include <wtf/WTFThreadData.h> @@ -48,7 +49,7 @@ #define failWithMessage(...) do { { handleErrorToken(); updateErrorMessage(true, __VA_ARGS__); } return 0; } while (0) #define failWithStackOverflow() do { updateErrorMessage(false, "Stack exhausted"); m_hasStackOverflow = true; return 0; } while (0) #define failIfFalse(cond, ...) do { if (!(cond)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) -#define failIfTrue(cond, ...) do { if (cond) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) +#define failIfTrue(cond, ...) do { if ((cond)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) #define failIfTrueIfStrict(cond, ...) do { if ((cond) && strictMode()) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define failIfFalseIfStrict(cond, ...) do { if ((!(cond)) && strictMode()) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define consumeOrFail(tokenType, ...) do { if (!consume(tokenType)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) @@ -56,7 +57,7 @@ #define matchOrFail(tokenType, ...) do { if (!match(tokenType)) { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0) #define failIfStackOverflow() do { if (!canRecurse()) failWithStackOverflow(); } while (0) #define semanticFail(...) do { internalFailWithMessage(false, __VA_ARGS__); } while (0) -#define semanticFailIfTrue(cond, ...) do { if (cond) internalFailWithMessage(false, __VA_ARGS__); } while (0) +#define semanticFailIfTrue(cond, ...) do { if ((cond)) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define semanticFailIfFalse(cond, ...) do { if (!(cond)) internalFailWithMessage(false, __VA_ARGS__); } while (0) #define regexFail(failure) do { setErrorMessage(failure); return 0; } while (0) #define failDueToUnexpectedToken() do {\ @@ -190,10 +191,7 @@ void Parser<LexerType>::logError(bool shouldPrintToken, const A& value1, const B } template <typename LexerType> -Parser<LexerType>::Parser( - VM* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, - JSParserStrictMode strictMode, SourceParseMode parseMode, - ConstructorKind defaultConstructorKind, ThisTDZMode thisTDZMode) +Parser<LexerType>::Parser(VM* vm, const SourceCode& source, FunctionParameters* parameters, const Identifier& name, JSParserStrictness strictness, JSParserMode parserMode) : m_vm(vm) , m_source(&source) , m_hasStackOverflow(false) @@ -206,25 +204,30 @@ Parser<LexerType>::Parser( , m_lastIdentifier(0) , m_lastFunctionName(nullptr) , m_sourceElements(0) - , m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin) - , m_defaultConstructorKind(defaultConstructorKind) - , m_thisTDZMode(thisTDZMode) { - m_lexer = std::make_unique<LexerType>(vm, builtinMode); - m_lexer->setCode(source, &m_parserArena); + m_lexer = adoptPtr(new LexerType(vm)); + m_arena = m_vm->parserArena.get(); + m_lexer->setCode(source, m_arena); m_token.m_location.line = source.firstLine(); m_token.m_location.startOffset = source.startOffset(); m_token.m_location.endOffset = source.startOffset(); m_token.m_location.lineStartOffset = source.startOffset(); m_functionCache = vm->addSourceProviderCache(source.provider()); ScopeRef scope = pushScope(); - if (isFunctionParseMode(parseMode)) + if (parserMode == JSParseFunctionCode) scope->setIsFunction(); - if (isModuleParseMode(parseMode)) - scope->setIsModule(); - if (strictMode == JSParserStrictMode::Strict) + if (strictness == JSParseStrict) scope->setStrictMode(); - + if (parameters) { + for (unsigned i = 0; i < parameters->size(); i++) { + auto parameter = parameters->at(i); + if (!parameter->isBindingNode()) + continue; + scope->declareParameter(&static_cast<BindingNode*>(parameter)->boundProperty()); + } + } + if (!name.isNull()) + scope->declareCallee(&name); next(); } @@ -234,60 +237,16 @@ Parser<LexerType>::~Parser() } template <typename LexerType> -String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMode parseMode) +String Parser<LexerType>::parseInner() { String parseError = String(); - - ASTBuilder context(const_cast<VM*>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source)); - ScopeRef scope = currentScope(); - scope->setIsLexicalScope(); - - bool isArrowFunctionBodyExpression = false; - if (m_lexer->isReparsingFunction()) { - ParserFunctionInfo<ASTBuilder> functionInfo; - parseFunctionParameters(context, parseMode, functionInfo); - m_parameters = functionInfo.parameters; - -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - if (parseMode == SourceParseMode::ArrowFunctionMode && !hasError()) { - // The only way we could have an error wile reparsing is if we run out of stack space. - RELEASE_ASSERT(match(ARROWFUNCTION)); - next(); - isArrowFunctionBodyExpression = !match(OPENBRACE); - } -#endif - } - - if (!calleeName.isNull()) - scope->declareCallee(&calleeName); - - if (m_lexer->isReparsingFunction()) + + ASTBuilder context(const_cast<VM*>(m_vm), const_cast<SourceCode*>(m_source)); + if (m_lexer->isReparsing()) m_statementDepth--; - - SourceElements* sourceElements = nullptr; - // The only way we can error this early is if we reparse a function and we run out of stack space. - if (!hasError()) { - if (isArrowFunctionBodyExpression) - sourceElements = parseArrowFunctionSingleExpressionBodySourceElements(context); -#if ENABLE(ES6_MODULES) - else if (isModuleParseMode(parseMode)) - sourceElements = parseModuleSourceElements(context, parseMode); -#endif - else - sourceElements = parseSourceElements(context, CheckForStrictMode); - } - - bool validEnding; - if (isArrowFunctionBodyExpression) { - ASSERT(m_lexer->isReparsingFunction()); - // When we reparse and stack overflow, we're not guaranteed a valid ending. If we don't run out of stack space, - // then of course this will always be valid because we already parsed for syntax errors. But we must - // be cautious in case we run out of stack space. - validEnding = isEndOfArrowFunction(); - } else - validEnding = consume(EOFTOK); - - if (!sourceElements || !validEnding) { + ScopeRef scope = currentScope(); + SourceElements* sourceElements = parseSourceElements(context, CheckForStrictMode); + if (!sourceElements || !consume(EOFTOK)) { if (hasError()) parseError = m_errorMessage; else @@ -296,12 +255,7 @@ String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMo IdentifierSet capturedVariables; bool modifiedParameter = false; - bool modifiedArguments = false; - scope->getCapturedVars(capturedVariables, modifiedParameter, modifiedArguments); - VariableEnvironment& varDeclarations = scope->declaredVariables(); - for (auto& entry : capturedVariables) - varDeclarations.markVariableAsCaptured(entry); - + scope->getCapturedVariables(capturedVariables, modifiedParameter); CodeFeatures features = context.features(); if (scope->strictMode()) features |= StrictModeFeature; @@ -309,52 +263,21 @@ String Parser<LexerType>::parseInner(const Identifier& calleeName, SourceParseMo features |= ShadowsArgumentsFeature; if (modifiedParameter) features |= ModifiedParameterFeature; - if (modifiedArguments) - features |= ModifiedArgumentsFeature; - Vector<RefPtr<UniquedStringImpl>> closedVariables; - if (m_parsingBuiltin) { - IdentifierSet usedVariables; - scope->getUsedVariables(usedVariables); - // FIXME: This needs to be changed if we want to allow builtins to use lexical declarations. - for (const auto& variable : usedVariables) { - Identifier identifier = Identifier::fromUid(m_vm, variable.get()); - if (scope->hasDeclaredVariable(identifier)) - continue; - - if (scope->hasDeclaredParameter(identifier)) - continue; - - if (variable == m_vm->propertyNames->arguments.impl()) - continue; - closedVariables.append(variable); - } - - if (!capturedVariables.isEmpty()) { - for (const auto& capturedVariable : capturedVariables) { - if (scope->hasDeclaredVariable(capturedVariable)) - continue; - - if (scope->hasDeclaredParameter(capturedVariable)) - continue; - - RELEASE_ASSERT_NOT_REACHED(); - } - } - } - didFinishParsing(sourceElements, context.funcDeclarations(), varDeclarations, features, context.numConstants(), WTF::move(closedVariables)); + didFinishParsing(sourceElements, context.varDeclarations(), context.funcDeclarations(), features, + context.numConstants(), capturedVariables); return parseError; } template <typename LexerType> -void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, DeclarationStacks::FunctionStack& funcStack, - VariableEnvironment& varDeclarations, CodeFeatures features, int numConstants, const Vector<RefPtr<UniquedStringImpl>>&& closedVariables) +void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, ParserArenaData<DeclarationStacks::VarStack>* varStack, + ParserArenaData<DeclarationStacks::FunctionStack>* funcStack, CodeFeatures features, int numConstants, IdentifierSet& capturedVars) { m_sourceElements = sourceElements; - m_funcDeclarations.swap(funcStack); - m_varDeclarations.swap(varDeclarations); - m_closedVariables = closedVariables; + m_varDeclarations = varStack; + m_funcDeclarations = funcStack; + m_capturedVariables.swap(capturedVars); m_features = features; m_numConstants = numConstants; } @@ -375,8 +298,7 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl unsigned directiveLiteralLength = 0; auto savePoint = createSavePoint(); bool hasSetStrict = false; - - while (TreeStatement statement = parseStatementListItem(context, directive, &directiveLiteralLength)) { + while (TreeStatement statement = parseStatement(context, directive, &directiveLiteralLength)) { if (mode == CheckForStrictMode && !seenNonDirective) { if (directive) { // "use strict" must be the exact literal without escape sequences or line continuation. @@ -411,116 +333,35 @@ template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceEl } template <typename LexerType> -template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context, SourceParseMode parseMode) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVarDeclaration(TreeBuilder& context) { - TreeSourceElements sourceElements = context.createSourceElements(); - SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get()); - - while (true) { - TreeStatement statement = 0; - if (match(IMPORT)) - statement = parseImportDeclaration(context); - else if (match(EXPORT)) - statement = parseExportDeclaration(context); - else { - const Identifier* directive = 0; - unsigned directiveLiteralLength = 0; - if (parseMode == SourceParseMode::ModuleAnalyzeMode) { - if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength)) - break; - continue; - } - statement = parseStatementListItem(context, directive, &directiveLiteralLength); - } - - if (!statement) - break; - context.appendStatement(sourceElements, statement); - } - + ASSERT(match(VAR)); + JSTokenLocation location(tokenLocation()); + int start = tokenLine(); + int end = 0; + int scratch; + TreeDeconstructionPattern scratch1 = 0; + TreeExpression scratch2 = 0; + JSTextPosition scratch3; + TreeExpression varDecls = parseVarDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3); propagateError(); - - for (const auto& uid : currentScope()->moduleScopeData().exportedBindings()) { - if (currentScope()->hasDeclaredVariable(uid)) { - currentScope()->declaredVariables().markVariableAsExported(uid); - continue; - } - - if (currentScope()->hasLexicallyDeclaredVariable(uid)) { - currentScope()->lexicalVariables().markVariableAsExported(uid); - continue; - } - - semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); - } - - return sourceElements; -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatementListItem(TreeBuilder& context, const Identifier*& directive, unsigned* directiveLiteralLength) -{ - // The grammar is documented here: - // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-statements - DepthManager statementDepth(&m_statementDepth); - m_statementDepth++; - TreeStatement result = 0; - bool shouldSetEndOffset = true; - switch (m_token.m_type) { - case CONSTTOKEN: - result = parseVariableDeclaration(context, DeclarationType::ConstDeclaration); - break; - case LET: { - bool shouldParseVariableDeclaration = true; - if (!strictMode()) { - SavePoint savePoint = createSavePoint(); - next(); - if (!match(IDENT) && !match(OPENBRACE) && !match(OPENBRACKET)) - shouldParseVariableDeclaration = false; - restoreSavePoint(savePoint); - } - if (shouldParseVariableDeclaration) - result = parseVariableDeclaration(context, DeclarationType::LetDeclaration); - else - result = parseExpressionOrLabelStatement(context); // Treat this as an IDENT. This is how ::parseStatement() handles IDENT. - - break; - } -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: - result = parseClassDeclaration(context); - break; -#endif - default: - m_statementDepth--; // parseStatement() increments the depth. - result = parseStatement(context, directive, directiveLiteralLength); - shouldSetEndOffset = false; - break; - } - - if (result && shouldSetEndOffset) - context.setEndOffset(result, m_lastTokenEndPosition.offset); - - return result; + failIfFalse(autoSemiColon(), "Expected ';' after var declaration"); + + return context.createVarStatement(location, varDecls, start, end); } template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVariableDeclaration(TreeBuilder& context, DeclarationType declarationType, ExportType exportType) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseConstDeclaration(TreeBuilder& context) { - ASSERT(match(VAR) || match(LET) || match(CONSTTOKEN)); + ASSERT(match(CONSTTOKEN)); JSTokenLocation location(tokenLocation()); int start = tokenLine(); int end = 0; - int scratch; - TreeDestructuringPattern scratch1 = 0; - TreeExpression scratch2 = 0; - JSTextPosition scratch3; - bool scratchBool; - TreeExpression variableDecls = parseVariableDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3, VarDeclarationContext, declarationType, exportType, scratchBool); + TreeConstDeclList constDecls = parseConstDeclarationList(context); propagateError(); - failIfFalse(autoSemiColon(), "Expected ';' after variable declaration"); + failIfFalse(autoSemiColon(), "Expected ';' after const declaration"); - return context.createDeclarationStatement(location, variableDecls, start, end); + return context.createConstStatement(location, constDecls, start, end); } template <typename LexerType> @@ -571,49 +412,27 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseWhileStatemen } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVariableDeclarationList(TreeBuilder& context, int& declarations, TreeDestructuringPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd, VarDeclarationListContext declarationListContext, DeclarationType declarationType, ExportType exportType, bool& forLoopConstDoesNotHaveInitializer) +template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVarDeclarationList(TreeBuilder& context, int& declarations, TreeDeconstructionPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd) { - ASSERT(declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::VarDeclaration || declarationType == DeclarationType::ConstDeclaration); - TreeExpression head = 0; - TreeExpression tail = 0; + TreeExpression varDecls = 0; const Identifier* lastIdent; - JSToken lastIdentToken; - AssignmentContext assignmentContext = assignmentContextFromDeclarationType(declarationType); do { lastIdent = 0; - lastPattern = TreeDestructuringPattern(0); + lastPattern = 0; JSTokenLocation location(tokenLocation()); next(); TreeExpression node = 0; declarations++; bool hasInitializer = false; - if (match(IDENT) || isLETMaskedAsIDENT()) { - failIfTrue(match(LET) && (declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::ConstDeclaration), - "Can't use 'let' as an identifier name for a LexicalDeclaration"); + if (match(IDENT)) { JSTextPosition varStart = tokenStartPosition(); - JSTokenLocation varStartLocation(tokenLocation()); identStart = varStart; const Identifier* name = m_token.m_data.ident; lastIdent = name; - lastIdentToken = m_token; next(); hasInitializer = match(EQUAL); - DeclarationResultMask declarationResult = declareVariable(name, declarationType); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare a variable named ", name->impl(), " in strict mode"); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { - if (declarationType == DeclarationType::LetDeclaration) - internalFailWithMessage(false, "Cannot declare a let variable twice: '", name->impl(), "'"); - if (declarationType == DeclarationType::ConstDeclaration) - internalFailWithMessage(false, "Cannot declare a const variable twice: '", name->impl(), "'"); - RELEASE_ASSERT_NOT_REACHED(); - } - } - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*name), "Cannot export a duplicate name '", name->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*name); - } - + failIfFalseIfStrict(declareVariable(name), "Cannot declare a variable named ", name->impl(), " in strict mode"); + context.addVar(name, (hasInitializer || (!m_allowsIn && (match(INTOKEN) || isofToken()))) ? DeclarationStacks::HasInitializer : 0); if (hasInitializer) { JSTextPosition varDivot = tokenStartPosition() + 1; initStart = tokenStartPosition(); @@ -623,82 +442,73 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVariableDecl lastInitializer = initializer; failIfFalse(initializer, "Expected expression as the intializer for the variable '", name->impl(), "'"); - node = context.createAssignResolve(location, *name, initializer, varStart, varDivot, lastTokenEndPosition(), assignmentContext); - } else { - if (declarationListContext == ForLoopContext && declarationType == DeclarationType::ConstDeclaration) - forLoopConstDoesNotHaveInitializer = true; - failIfTrue(declarationListContext != ForLoopContext && declarationType == DeclarationType::ConstDeclaration, "const declared variable '", name->impl(), "'", " must have an initializer"); - if (declarationType == DeclarationType::VarDeclaration) - node = context.createEmptyVarExpression(varStartLocation, *name); - else - node = context.createEmptyLetExpression(varStartLocation, *name); + node = context.createAssignResolve(location, *name, initializer, varStart, varDivot, lastTokenEndPosition()); } } else { lastIdent = 0; - auto pattern = parseDestructuringPattern(context, destructuringKindFromDeclarationType(declarationType), exportType, nullptr, nullptr, assignmentContext); - failIfFalse(pattern, "Cannot parse this destructuring pattern"); + auto pattern = parseDeconstructionPattern(context, DeconstructToVariables); + failIfFalse(pattern, "Cannot parse this deconstruction pattern"); hasInitializer = match(EQUAL); - failIfTrue(declarationListContext == VarDeclarationContext && !hasInitializer, "Expected an initializer in destructuring variable declaration"); lastPattern = pattern; if (hasInitializer) { next(TreeBuilder::DontBuildStrings); // consume '=' - TreeExpression rhs = parseAssignmentExpression(context); - node = context.createDestructuringAssignment(location, pattern, rhs); - lastInitializer = rhs; + TreeExpression rhs = parseExpression(context); + node = context.createDeconstructingAssignment(location, pattern, rhs); } } - if (!head) - head = node; - else if (!tail) { - head = context.createCommaExpr(location, head); - tail = context.appendToCommaExpr(location, head, head, node); - } else - tail = context.appendToCommaExpr(location, head, tail, node); + if (hasInitializer) { + if (!varDecls) + varDecls = node; + else + varDecls = context.combineCommaNodes(location, varDecls, node); + } } while (match(COMMA)); if (lastIdent) - lastPattern = context.createBindingLocation(lastIdentToken.m_location, *lastIdent, lastIdentToken.m_startPosition, lastIdentToken.m_endPosition, assignmentContext); - - return head; + lastPattern = createBindingPattern(context, DeconstructToVariables, *lastIdent, 0); + return varDecls; } template <typename LexerType> -template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& name, int depth, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DeconstructionKind kind, const Identifier& name, int depth) { + ASSERT(!name.isEmpty()); ASSERT(!name.isNull()); - ASSERT(name.impl()->isAtomic() || name.impl()->isSymbol()); - - if (kind == DestructureToVariables) - failIfTrueIfStrict(declareVariable(&name) & DeclarationResult::InvalidStrictMode, "Cannot declare a variable named '", name.impl(), "' in strict mode"); - else if (kind == DestructureToLet || kind == DestructureToConst) { - DeclarationResultMask declarationResult = declareVariable(&name, kind == DestructureToLet ? DeclarationType::LetDeclaration : DeclarationType::ConstDeclaration); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot destructure to a variable named '", name.impl(), "' in strict mode"); - failIfTrue(declarationResult & DeclarationResult::InvalidDuplicateDeclaration, "Cannot declare a lexical variable twice: '", name.impl(), "'"); - } - } else if (kind == DestructureToParameters) { - if (depth) { + ASSERT(name.impl()->isIdentifier()); + if (depth) { + if (kind == DeconstructToVariables) + failIfFalseIfStrict(declareVariable(&name), "Cannot deconstruct to a variable named '", name.impl(), "' in strict mode"); + if (kind == DeconstructToParameters) { auto bindingResult = declareBoundParameter(&name); if (bindingResult == Scope::StrictBindingFailed && strictMode()) { - semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); + semanticFailIfTrue(m_vm->propertyNames->arguments == name || m_vm->propertyNames->eval == name, "Cannot deconstruct to a parameter name '", name.impl(), "' in strict mode"); if (m_lastFunctionName && name == *m_lastFunctionName) - semanticFail("Cannot destructure to '", name.impl(), "' as it shadows the name of a strict mode function"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it shadows the name of a strict mode function"); semanticFailureDueToKeyword("bound parameter name"); if (hasDeclaredParameter(name)) - semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it has already been declared"); semanticFail("Cannot bind to a parameter named '", name.impl(), "' in strict mode"); } if (bindingResult == Scope::BindingFailed) { semanticFailureDueToKeyword("bound parameter name"); if (hasDeclaredParameter(name)) - semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared"); - semanticFail("Cannot destructure to a parameter named '", name.impl(), "'"); + semanticFail("Cannot deconstruct to '", name.impl(), "' as it has already been declared"); + semanticFail("Cannot deconstruct to a parameter named '", name.impl(), "'"); } - } else { - DeclarationResultMask declarationResult = declareParameter(&name); - if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) { - semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode"); + } + if (kind != DeconstructToExpressions) + context.addVar(&name, kind == DeconstructToParameters ? 0 : DeclarationStacks::HasInitializer); + } else { + if (kind == DeconstructToVariables) { + failIfFalseIfStrict(declareVariable(&name), "Cannot declare a variable named '", name.impl(), "' in strict mode"); + context.addVar(&name, DeclarationStacks::HasInitializer); + } + + if (kind == DeconstructToParameters) { + bool declarationResult = declareParameter(&name); + if (!declarationResult && strictMode()) { + semanticFailIfTrue(m_vm->propertyNames->arguments == name || m_vm->propertyNames->eval == name, "Cannot deconstruct to a parameter name '", name.impl(), "' in strict mode"); if (m_lastFunctionName && name == *m_lastFunctionName) semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function"); semanticFailureDueToKeyword("parameter name"); @@ -706,197 +516,122 @@ template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createB semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared"); semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode"); } - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) { - // It's not always an error to define a duplicate parameter. - // It's only an error when there are default parameter values or destructuring parameters. - // We note this value now so we can check it later. - if (duplicateIdentifier) - *duplicateIdentifier = &name; - } } } - - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(name), "Cannot export a duplicate name '", name.impl(), "'"); - currentScope()->moduleScopeData().exportBinding(name); - } - return context.createBindingLocation(token.m_location, name, token.m_startPosition, token.m_endPosition, bindingContext); -} - -template <typename LexerType> -template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseArrowFunctionSingleExpressionBodySourceElements(TreeBuilder& context) -{ - ASSERT(!match(OPENBRACE)); - - JSTokenLocation location(tokenLocation()); - JSTextPosition start = tokenStartPosition(); - - failIfStackOverflow(); - TreeExpression expr = parseAssignmentExpression(context); - failIfFalse(expr, "Cannot parse the arrow function expression"); - - context.setEndOffset(expr, m_lastTokenEndPosition.offset); - - failIfFalse(isEndOfArrowFunction(), "Expected a ';', ']', '}', ')', ',', line terminator or EOF following a arrow function statement"); - - JSTextPosition end = tokenEndPosition(); - - if (!m_lexer->prevTerminator()) - setEndOfStatement(); - - TreeSourceElements sourceElements = context.createSourceElements(); - TreeStatement body = context.createReturnStatement(location, expr, start, end); - context.setEndOffset(body, m_lastTokenEndPosition.offset); - context.appendStatement(sourceElements, body); - - return sourceElements; + return context.createBindingLocation(m_token.m_location, name, m_token.m_startPosition, m_token.m_endPosition); } template <typename LexerType> -template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::tryParseDestructuringPatternExpression(TreeBuilder& context, AssignmentContext bindingContext) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::tryParseDeconstructionPatternExpression(TreeBuilder& context) { - return parseDestructuringPattern(context, DestructureToExpressions, ExportType::NotExported, nullptr, nullptr, bindingContext); + return parseDeconstructionPattern(context, DeconstructToExpressions); } template <typename LexerType> -template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseDestructuringPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth) +template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::parseDeconstructionPattern(TreeBuilder& context, DeconstructionKind kind, int depth) { failIfStackOverflow(); int nonLHSCount = m_nonLHSCount; - TreeDestructuringPattern pattern; + TreeDeconstructionPattern pattern; switch (m_token.m_type) { case OPENBRACKET: { - JSTextPosition divotStart = tokenStartPosition(); auto arrayPattern = context.createArrayPattern(m_token.m_location); next(); - - if (hasDestructuringPattern) - *hasDestructuringPattern = true; - - bool restElementWasFound = false; - + if (kind == DeconstructToExpressions && match(CLOSEBRACKET)) + return 0; + failIfTrue(match(CLOSEBRACKET), "There must be at least one bound property in an array deconstruction pattern"); do { while (match(COMMA)) { context.appendArrayPatternSkipEntry(arrayPattern, m_token.m_location); next(); } propagateError(); - - if (match(CLOSEBRACKET)) - break; - - if (UNLIKELY(match(DOTDOTDOT))) { - JSTokenLocation location = m_token.m_location; - next(); - auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); - if (kind == DestructureToExpressions && !innerPattern) - return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - - failIfTrue(kind != DestructureToExpressions && !context.isBindingNode(innerPattern), "Expected identifier for a rest element destructuring pattern"); - - context.appendArrayPatternRestEntry(arrayPattern, location, innerPattern); - restElementWasFound = true; - break; - } - JSTokenLocation location = m_token.m_location; - auto innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); - if (kind == DestructureToExpressions && !innerPattern) + auto innerPattern = parseDeconstructionPattern(context, kind, depth + 1); + if (kind == DeconstructToExpressions && !innerPattern) return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context); - context.appendArrayPatternEntry(arrayPattern, location, innerPattern, defaultValue); + failIfFalse(innerPattern, "Cannot parse this deconstruction pattern"); + context.appendArrayPatternEntry(arrayPattern, location, innerPattern); } while (consume(COMMA)); - - if (kind == DestructureToExpressions && !match(CLOSEBRACKET)) + + if (kind == DeconstructToExpressions && !match(CLOSEBRACKET)) return 0; - consumeOrFail(CLOSEBRACKET, restElementWasFound ? "Expected a closing ']' following a rest element destructuring pattern" : "Expected either a closing ']' or a ',' following an element destructuring pattern"); - context.finishArrayPattern(arrayPattern, divotStart, divotStart, lastTokenEndPosition()); + + consumeOrFail(CLOSEBRACKET, "Expected either a closing ']' or a ',' following an element deconstruction pattern"); pattern = arrayPattern; break; } case OPENBRACE: { - auto objectPattern = context.createObjectPattern(m_token.m_location); next(); + + if (kind == DeconstructToExpressions && match(CLOSEBRACE)) + return 0; - if (hasDestructuringPattern) - *hasDestructuringPattern = true; - + failIfTrue(match(CLOSEBRACE), "There must be at least one bound property in an object deconstruction pattern"); + auto objectPattern = context.createObjectPattern(m_token.m_location); + bool wasString = false; do { - bool wasString = false; - - if (match(CLOSEBRACE)) - break; - - const Identifier* propertyName = nullptr; - TreeDestructuringPattern innerPattern = 0; + Identifier propertyName; + TreeDeconstructionPattern innerPattern = 0; JSTokenLocation location = m_token.m_location; - if (match(IDENT) || isLETMaskedAsIDENT()) { - failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration"); - propertyName = m_token.m_data.ident; - JSToken identifierToken = m_token; + if (match(IDENT)) { + propertyName = *m_token.m_data.ident; next(); if (consume(COLON)) - innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); + innerPattern = parseDeconstructionPattern(context, kind, depth + 1); else - innerPattern = createBindingPattern(context, kind, exportType, *propertyName, depth + 1, identifierToken, bindingContext, duplicateIdentifier); + innerPattern = createBindingPattern(context, kind, propertyName, depth); } else { JSTokenType tokenType = m_token.m_type; switch (m_token.m_type) { - case DOUBLE: - case INTEGER: - propertyName = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue); + case NUMBER: + propertyName = Identifier::from(m_vm, m_token.m_data.doubleValue); break; case STRING: - propertyName = m_token.m_data.ident; + propertyName = *m_token.m_data.ident; wasString = true; break; default: if (m_token.m_type != RESERVED && m_token.m_type != RESERVED_IF_STRICT && !(m_token.m_type & KeywordTokenFlag)) { - if (kind == DestructureToExpressions) + if (kind == DeconstructToExpressions) return 0; failWithMessage("Expected a property name"); } - propertyName = m_token.m_data.ident; + propertyName = *m_token.m_data.ident; break; } next(); if (!consume(COLON)) { - if (kind == DestructureToExpressions) + if (kind == DeconstructToExpressions) return 0; - semanticFailIfTrue(tokenType == RESERVED, "Cannot use abbreviated destructuring syntax for reserved name '", propertyName->impl(), "'"); - semanticFailIfTrue(tokenType == RESERVED_IF_STRICT, "Cannot use abbreviated destructuring syntax for reserved name '", propertyName->impl(), "' in strict mode"); - semanticFailIfTrue(tokenType & KeywordTokenFlag, "Cannot use abbreviated destructuring syntax for keyword '", propertyName->impl(), "'"); + semanticFailIfTrue(tokenType == RESERVED, "Cannot use abbreviated deconstruction syntax for reserved name '", propertyName.impl(), "'"); + semanticFailIfTrue(tokenType == RESERVED_IF_STRICT, "Cannot use abbreviated deconstruction syntax for reserved name '", propertyName.impl(), "' in strict mode"); + semanticFailIfTrue(tokenType & KeywordTokenFlag, "Cannot use abbreviated deconstruction syntax for keyword '", propertyName.impl(), "'"); - failWithMessage("Expected a ':' prior to a named destructuring property"); + failWithMessage("Expected a ':' prior to named property deconstruction"); } - innerPattern = parseDestructuringPattern(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1); + innerPattern = parseDeconstructionPattern(context, kind, depth + 1); } - if (kind == DestructureToExpressions && !innerPattern) + if (kind == DeconstructToExpressions && !innerPattern) return 0; - failIfFalse(innerPattern, "Cannot parse this destructuring pattern"); - TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context); - ASSERT(propertyName); - context.appendObjectPatternEntry(objectPattern, location, wasString, *propertyName, innerPattern, defaultValue); + failIfFalse(innerPattern, "Cannot parse this deconstruction pattern"); + context.appendObjectPatternEntry(objectPattern, location, wasString, propertyName, innerPattern); } while (consume(COMMA)); - - if (kind == DestructureToExpressions && !match(CLOSEBRACE)) + if (kind == DeconstructToExpressions && !match(CLOSEBRACE)) return 0; - consumeOrFail(CLOSEBRACE, "Expected either a closing '}' or an ',' after a property destructuring pattern"); + consumeOrFail(CLOSEBRACE, "Expected either a closing '}' or an ',' after a property deconstruction pattern"); pattern = objectPattern; break; } default: { - if (!match(IDENT) && !isLETMaskedAsIDENT()) { - if (kind == DestructureToExpressions) + if (!match(IDENT)) { + if (kind == DeconstructToExpressions) return 0; semanticFailureDueToKeyword("variable name"); failWithMessage("Expected a parameter pattern or a ')' in parameter list"); } - failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration"); - pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, depth, m_token, bindingContext, duplicateIdentifier); + pattern = createBindingPattern(context, kind, *m_token.m_data.ident, depth); next(); break; } @@ -906,13 +641,32 @@ template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::parseDe } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseDefaultValueForDestructuringPattern(TreeBuilder& context) +template <class TreeBuilder> TreeConstDeclList Parser<LexerType>::parseConstDeclarationList(TreeBuilder& context) { - if (!match(EQUAL)) - return 0; - - next(TreeBuilder::DontBuildStrings); // consume '=' - return parseAssignmentExpression(context); + failIfTrue(strictMode(), "Const declarations are not supported in strict mode"); + TreeConstDeclList constDecls = 0; + TreeConstDeclList tail = 0; + do { + JSTokenLocation location(tokenLocation()); + next(); + matchOrFail(IDENT, "Expected an identifier name in const declaration"); + const Identifier* name = m_token.m_data.ident; + next(); + bool hasInitializer = match(EQUAL); + declareVariable(name); + context.addVar(name, DeclarationStacks::IsConstant | (hasInitializer ? DeclarationStacks::HasInitializer : 0)); + + TreeExpression initializer = 0; + if (hasInitializer) { + next(TreeBuilder::DontBuildStrings); // consume '=' + initializer = parseAssignmentExpression(context); + failIfFalse(!!initializer, "Unable to parse initializer"); + } + tail = context.appendConstDecl(location, tail, name, initializer); + if (!constDecls) + constDecls = tail; + } while (match(COMMA)); + return constDecls; } template <typename LexerType> @@ -928,56 +682,18 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( JSTextPosition declsStart; JSTextPosition declsEnd; TreeExpression decls = 0; - TreeDestructuringPattern pattern = 0; - bool isVarDeclaraton = match(VAR); - bool isLetDeclaration = match(LET); - bool isConstDeclaration = match(CONSTTOKEN); - bool forLoopConstDoesNotHaveInitializer = false; - - VariableEnvironment dummySet; - VariableEnvironment* lexicalVariables = nullptr; - AutoCleanupLexicalScope lexicalScope; - - auto gatherLexicalVariablesIfNecessary = [&] { - if (isLetDeclaration || isConstDeclaration) { - ScopeRef scope = lexicalScope.scope(); - lexicalVariables = &scope->finalizeLexicalEnvironment(); - } else - lexicalVariables = &dummySet; - }; - - auto popLexicalScopeIfNecessary = [&] { - if (isLetDeclaration || isConstDeclaration) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - }; - - if (isVarDeclaraton || isLetDeclaration || isConstDeclaration) { + TreeDeconstructionPattern pattern = 0; + if (match(VAR)) { /* - for (var/let/const IDENT in/of expression) statement - for (var/let/const varDeclarationList; expressionOpt; expressionOpt) + for (var IDENT in expression) statement + for (var varDeclarationList; expressionOpt; expressionOpt) */ - if (isLetDeclaration || isConstDeclaration) { - ScopeRef newScope = pushScope(); - newScope->setIsLexicalScope(); - newScope->preventVarDeclarations(); - lexicalScope.setIsValid(newScope, this); - } - - TreeDestructuringPattern forInTarget = 0; + TreeDeconstructionPattern forInTarget = 0; TreeExpression forInInitializer = 0; m_allowsIn = false; JSTextPosition initStart; JSTextPosition initEnd; - DeclarationType declarationType; - if (isVarDeclaraton) - declarationType = DeclarationType::VarDeclaration; - else if (isLetDeclaration) - declarationType = DeclarationType::LetDeclaration; - else if (isConstDeclaration) - declarationType = DeclarationType::ConstDeclaration; - else - RELEASE_ASSERT_NOT_REACHED(); - decls = parseVariableDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd, ForLoopContext, declarationType, ExportType::NotExported, forLoopConstDoesNotHaveInitializer); + decls = parseVarDeclarationList(context, declarations, forInTarget, forInInitializer, declsStart, initStart, initEnd); m_allowsIn = true; propagateError(); @@ -985,21 +701,18 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( if (match(SEMICOLON)) goto standardForLoop; - failIfFalse(declarations == 1, "can only declare a single variable in an enumeration"); - failIfTrueIfStrict(forInInitializer, "Cannot use initialiser syntax in a strict mode enumeration"); - - if (forInInitializer) - failIfFalse(context.isBindingNode(forInTarget), "Cannot use initialiser syntax when binding to a pattern during enumeration"); - + failIfFalse(declarations == 1, "must declare variables after 'var'"); + failIfTrue(forInInitializer, "Cannot use initialiser syntax in a for-in loop"); + // Handle for-in with var declaration JSTextPosition inLocation = tokenStartPosition(); bool isOfEnumeration = false; if (!consume(INTOKEN)) { failIfFalse(match(IDENT) && *m_token.m_data.ident == m_vm->propertyNames->of, "Expected either 'in' or 'of' in enumeration syntax"); isOfEnumeration = true; - failIfTrue(forInInitializer, "Cannot use initialiser syntax in a for-of enumeration"); next(); } + TreeExpression expr = parseExpression(context); failIfFalse(expr, "Expected expression to enumerate"); JSTextPosition exprEnd = lastTokenEndPosition(); @@ -1013,25 +726,20 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected statement as body of for-", isOfEnumeration ? "of" : "in", " statement"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result; if (isOfEnumeration) - result = context.createForOfLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine); + return context.createForInLoop(location, forInTarget, expr, statement, declsStart, inLocation, exprEnd, startLine, endLine); } if (!match(SEMICOLON)) { if (match(OPENBRACE) || match(OPENBRACKET)) { SavePoint savePoint = createSavePoint(); declsStart = tokenStartPosition(); - pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::DeclarationStatement); + pattern = tryParseDeconstructionPatternExpression(context); declsEnd = lastTokenEndPosition(); if (pattern && (match(INTOKEN) || (match(IDENT) && *m_token.m_data.ident == m_vm->propertyNames->of))) goto enumerationLoop; - pattern = TreeDestructuringPattern(0); + pattern = 0; restoreSavePoint(savePoint); } m_allowsIn = false; @@ -1047,7 +755,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( // Standard for loop next(); TreeExpression condition = 0; - failIfTrue(forLoopConstDoesNotHaveInitializer && isConstDeclaration, "const variables in for loops must have initializers"); if (!match(SEMICOLON)) { condition = parseExpression(context); @@ -1067,13 +774,10 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseForStatement( TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected a statement as the body of a for loop"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result = context.createForLoop(location, decls, condition, increment, statement, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForLoop(location, decls, condition, increment, statement, startLine, endLine); } - // For-in and For-of loop + // For-in loop enumerationLoop: failIfFalse(nonLHSCount == m_nonLHSCount, "Expected a reference on the left hand side of an enumeration statement"); bool isOfEnumeration = false; @@ -1093,24 +797,15 @@ enumerationLoop: TreeStatement statement = parseStatement(context, unused); endLoop(); failIfFalse(statement, "Expected a statement as the body of a for-", isOfEnumeration ? "of" : "in", "loop"); - gatherLexicalVariablesIfNecessary(); - TreeStatement result; if (pattern) { ASSERT(!decls); if (isOfEnumeration) - result = context.createForOfLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); + return context.createForInLoop(location, pattern, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); } if (isOfEnumeration) - result = context.createForOfLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - else - result = context.createForInLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine, *lexicalVariables); - popLexicalScopeIfNecessary(); - return result; + return context.createForOfLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); + return context.createForInLoop(location, decls, expr, statement, declsStart, declsEnd, exprEnd, startLine, endLine); } template <typename LexerType> @@ -1124,9 +819,9 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBreakStatemen if (autoSemiColon()) { semanticFailIfFalse(breakIsValid(), "'break' is only valid inside a switch or loop statement"); - return context.createBreakStatement(location, &m_vm->propertyNames->nullIdentifier, start, end); + return context.createBreakStatement(location, start, end); } - failIfFalse(match(IDENT) || isLETMaskedAsIDENT(), "Expected an identifier as the target for a break statement"); + matchOrFail(IDENT, "Expected an identifier as the target for a break statement"); const Identifier* ident = m_token.m_data.ident; semanticFailIfFalse(getLabel(ident), "Cannot use the undeclared label '", ident->impl(), "'"); end = tokenEndPosition(); @@ -1146,13 +841,13 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseContinueState if (autoSemiColon()) { semanticFailIfFalse(continueIsValid(), "'continue' is only valid inside a loop statement"); - return context.createContinueStatement(location, &m_vm->propertyNames->nullIdentifier, start, end); + return context.createContinueStatement(location, start, end); } - failIfFalse(match(IDENT) || isLETMaskedAsIDENT(), "Expected an identifier as the target for a continue statement"); + matchOrFail(IDENT, "Expected an identifier as the target for a continue statement"); const Identifier* ident = m_token.m_data.ident; ScopeLabelInfo* label = getLabel(ident); semanticFailIfFalse(label, "Cannot use the undeclared label '", ident->impl(), "'"); - semanticFailIfFalse(label->isLoop, "Cannot continue to the label '", ident->impl(), "' as it is not targeting a loop"); + semanticFailIfFalse(label->m_isLoop, "Cannot continue to the label '", ident->impl(), "' as it is not targeting a loop"); end = tokenEndPosition(); next(); failIfFalse(autoSemiColon(), "Expected a ';' following a targeted continue statement"); @@ -1242,9 +937,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseSwitchStateme handleProductionOrFail(CLOSEPAREN, ")", "end", "subject of a 'switch'"); handleProductionOrFail(OPENBRACE, "{", "start", "body of a 'switch'"); - AutoPopScopeRef lexicalScope(this, pushScope()); - lexicalScope->setIsLexicalScope(); - lexicalScope->preventVarDeclarations(); startSwitch(); TreeClauseList firstClauses = parseSwitchClauses(context); propagateError(); @@ -1257,9 +949,8 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseSwitchStateme endSwitch(); handleProductionOrFail(CLOSEBRACE, "}", "end", "body of a 'switch'"); - TreeStatement result = context.createSwitchStatement(location, expr, firstClauses, defaultClause, secondClauses, startLine, endLine, lexicalScope->finalizeLexicalEnvironment()); - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - return result; + return context.createSwitchStatement(location, expr, firstClauses, defaultClause, secondClauses, startLine, endLine); + } template <typename LexerType> @@ -1267,7 +958,6 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause { if (!match(CASE)) return 0; - unsigned startOffset = tokenStart(); next(); TreeExpression condition = parseExpression(context); failIfFalse(condition, "Cannot parse switch clause"); @@ -1275,12 +965,10 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch clause"); TreeClause clause = context.createClause(condition, statements); - context.setStartOffset(clause, startOffset); TreeClauseList clauseList = context.createClauseList(clause); TreeClauseList tail = clauseList; while (match(CASE)) { - startOffset = tokenStart(); next(); TreeExpression condition = parseExpression(context); failIfFalse(condition, "Cannot parse switch case expression"); @@ -1288,7 +976,6 @@ template <class TreeBuilder> TreeClauseList Parser<LexerType>::parseSwitchClause TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch clause"); clause = context.createClause(condition, statements); - context.setStartOffset(clause, startOffset); tail = context.createClauseList(tail, clause); } return clauseList; @@ -1299,14 +986,11 @@ template <class TreeBuilder> TreeClause Parser<LexerType>::parseSwitchDefaultCla { if (!match(DEFAULT)) return 0; - unsigned startOffset = tokenStart(); next(); consumeOrFail(COLON, "Expected a ':' after switch default clause"); TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(statements, "Cannot parse the body of a switch default clause"); - TreeClause result = context.createClause(0, statements); - context.setStartOffset(result, startOffset); - return result; + return context.createClause(0, statements); } template <typename LexerType> @@ -1325,28 +1009,26 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseTryStatement( tryBlock = parseBlockStatement(context); failIfFalse(tryBlock, "Cannot parse the body of try block"); int lastLine = m_lastTokenEndPosition.line; - VariableEnvironment catchEnvironment; + if (match(CATCH)) { + currentScope()->setNeedsFullActivation(); next(); handleProductionOrFail(OPENPAREN, "(", "start", "'catch' target"); - if (!(match(IDENT) || isLETMaskedAsIDENT())) { + if (!match(IDENT)) { semanticFailureDueToKeyword("catch variable name"); failWithMessage("Expected identifier name as catch target"); } ident = m_token.m_data.ident; next(); AutoPopScopeRef catchScope(this, pushScope()); - catchScope->setIsLexicalScope(); - catchScope->preventVarDeclarations(); - failIfTrueIfStrict(catchScope->declareLexicalVariable(ident, false) & DeclarationResult::InvalidStrictMode, "Cannot declare a catch variable named '", ident->impl(), "' in strict mode"); + failIfFalseIfStrict(declareVariable(ident), "Cannot declare a catch variable named '", ident->impl(), "' in strict mode"); + catchScope->preventNewDecls(); handleProductionOrFail(CLOSEPAREN, ")", "end", "'catch' target"); matchOrFail(OPENBRACE, "Expected exception handler to be a block statement"); catchBlock = parseBlockStatement(context); failIfFalse(catchBlock, "Unable to parse 'catch' block"); - catchEnvironment = catchScope->finalizeLexicalEnvironment(); - RELEASE_ASSERT(catchEnvironment.size() == 1 && catchEnvironment.contains(ident->impl())); - popScope(catchScope, TreeBuilder::NeedsFreeVariableInfo); + failIfFalse(popScope(catchScope, TreeBuilder::NeedsFreeVariableInfo), "Parse error"); } if (match(FINALLY)) { @@ -1356,7 +1038,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseTryStatement( failIfFalse(finallyBlock, "Cannot parse finally body"); } failIfFalse(catchBlock || finallyBlock, "Try statements must have at least a catch or finally block"); - return context.createTryStatement(location, tryBlock, ident, catchBlock, finallyBlock, firstLine, lastLine, catchEnvironment); + return context.createTryStatement(location, tryBlock, ident, catchBlock, finallyBlock, firstLine, lastLine); } template <typename LexerType> @@ -1377,44 +1059,18 @@ template <typename LexerType> template <class TreeBuilder> TreeStatement Parser<LexerType>::parseBlockStatement(TreeBuilder& context) { ASSERT(match(OPENBRACE)); - - // We should treat the first block statement of the function (the body of the function) as the lexical - // scope of the function itself, and not the lexical scope of a 'block' statement within the function. - AutoCleanupLexicalScope lexicalScope; - bool shouldPushLexicalScope = m_statementDepth > 0; - if (shouldPushLexicalScope) { - ScopeRef newScope = pushScope(); - newScope->setIsLexicalScope(); - newScope->preventVarDeclarations(); - lexicalScope.setIsValid(newScope, this); - } JSTokenLocation location(tokenLocation()); - int startOffset = m_token.m_data.offset; int start = tokenLine(); - VariableEnvironment emptyEnvironment; next(); if (match(CLOSEBRACE)) { - int endOffset = m_token.m_data.offset; next(); - TreeStatement result = context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line, shouldPushLexicalScope ? currentScope()->finalizeLexicalEnvironment() : emptyEnvironment); - context.setStartOffset(result, startOffset); - context.setEndOffset(result, endOffset); - if (shouldPushLexicalScope) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - return result; + return context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line); } TreeSourceElements subtree = parseSourceElements(context, DontCheckForStrictMode); failIfFalse(subtree, "Cannot parse the body of the block statement"); matchOrFail(CLOSEBRACE, "Expected a closing '}' at the end of a block statement"); - int endOffset = m_token.m_data.offset; next(); - TreeStatement result = context.createBlockStatement(location, subtree, start, m_lastTokenEndPosition.line, shouldPushLexicalScope ? currentScope()->finalizeLexicalEnvironment() : emptyEnvironment); - context.setStartOffset(result, startOffset); - context.setEndOffset(result, endOffset); - if (shouldPushLexicalScope) - popScope(lexicalScope, TreeBuilder::NeedsFreeVariableInfo); - - return result; + return context.createBlockStatement(location, subtree, start, m_lastTokenEndPosition.line); } template <typename LexerType> @@ -1425,63 +1081,45 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre directive = 0; int nonTrivialExpressionCount = 0; failIfStackOverflow(); - TreeStatement result = 0; - bool shouldSetEndOffset = true; - switch (m_token.m_type) { case OPENBRACE: - result = parseBlockStatement(context); - shouldSetEndOffset = false; - break; + return parseBlockStatement(context); case VAR: - result = parseVariableDeclaration(context, DeclarationType::VarDeclaration); - break; + return parseVarDeclaration(context); + case CONSTTOKEN: + return parseConstDeclaration(context); case FUNCTION: - failIfFalseIfStrict(m_statementDepth == 1, "Strict mode does not allow function declarations in a lexically nested statement"); - result = parseFunctionDeclaration(context); - break; + failIfFalseIfStrict(m_statementDepth == 1, "Nested functions cannot be declared in strict mode"); + return parseFunctionDeclaration(context); case SEMICOLON: { JSTokenLocation location(tokenLocation()); next(); - result = context.createEmptyStatement(location); - break; + return context.createEmptyStatement(location); } case IF: - result = parseIfStatement(context); - break; + return parseIfStatement(context); case DO: - result = parseDoWhileStatement(context); - break; + return parseDoWhileStatement(context); case WHILE: - result = parseWhileStatement(context); - break; + return parseWhileStatement(context); case FOR: - result = parseForStatement(context); - break; + return parseForStatement(context); case CONTINUE: - result = parseContinueStatement(context); - break; + return parseContinueStatement(context); case BREAK: - result = parseBreakStatement(context); - break; + return parseBreakStatement(context); case RETURN: - result = parseReturnStatement(context); - break; + return parseReturnStatement(context); case WITH: - result = parseWithStatement(context); - break; + return parseWithStatement(context); case SWITCH: - result = parseSwitchStatement(context); - break; + return parseSwitchStatement(context); case THROW: - result = parseThrowStatement(context); - break; + return parseThrowStatement(context); case TRY: - result = parseTryStatement(context); - break; + return parseTryStatement(context); case DEBUGGER: - result = parseDebuggerStatement(context); - break; + return parseDebuggerStatement(context); case EOFTOK: case CASE: case CLOSEBRACE: @@ -1489,8 +1127,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre // These tokens imply the end of a set of source elements return 0; case IDENT: - result = parseExpressionOrLabelStatement(context); - break; + return parseExpressionOrLabelStatement(context); case STRING: directive = m_token.m_data.ident; if (directiveLiteralLength) @@ -1501,539 +1138,187 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatement(Tre TreeStatement exprStatement = parseExpressionStatement(context); if (directive && nonTrivialExpressionCount != m_nonTrivialExpressionCount) directive = 0; - result = exprStatement; - break; + return exprStatement; } - - if (result && shouldSetEndOffset) - context.setEndOffset(result, m_lastTokenEndPosition.offset); - return result; } template <typename LexerType> -template <class TreeBuilder> bool Parser<LexerType>::parseFormalParameters(TreeBuilder& context, TreeFormalParameterList list, unsigned& parameterCount) +template <class TreeBuilder> TreeFormalParameterList Parser<LexerType>::parseFormalParameters(TreeBuilder& context) { -#define failFromDuplicate() \ - if (duplicateParameter) {\ - semanticFailIfTrue(defaultValue, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with default parameter values");\ - semanticFailIfTrue(hasDestructuringPattern, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with destructuring parameters");\ - } - - const Identifier* duplicateParameter = nullptr; - bool hasDestructuringPattern = false; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern); + auto parameter = parseDeconstructionPattern(context, DeconstructToParameters); failIfFalse(parameter, "Cannot parse parameter pattern"); - auto defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - failFromDuplicate(); - context.appendParameter(list, parameter, defaultValue); - parameterCount++; + TreeFormalParameterList list = context.createFormalParameterList(parameter); + TreeFormalParameterList tail = list; while (consume(COMMA)) { - parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter, &hasDestructuringPattern); + parameter = parseDeconstructionPattern(context, DeconstructToParameters); failIfFalse(parameter, "Cannot parse parameter pattern"); - defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - failFromDuplicate(); - context.appendParameter(list, parameter, defaultValue); - parameterCount++; + tail = context.createFormalParameterList(tail, parameter); } - return true; -#undef failFromDuplicate + return list; } template <typename LexerType> -template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody( - TreeBuilder& context, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart, - ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) +template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody(TreeBuilder& context) { - if (bodyType == StandardFunctionBodyBlock || bodyType == ArrowFunctionBodyBlock) { - next(); - if (match(CLOSEBRACE)) { - unsigned endColumn = tokenColumn(); - return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode); - } - } + JSTokenLocation startLocation(tokenLocation()); + unsigned startColumn = tokenColumn(); + next(); + if (match(CLOSEBRACE)) { + unsigned endColumn = tokenColumn(); + return context.createFunctionBody(startLocation, tokenLocation(), startColumn, endColumn, strictMode()); + } DepthManager statementDepth(&m_statementDepth); m_statementDepth = 0; - SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get()); - if (bodyType == ArrowFunctionBodyExpression) - failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), "Cannot parse body of this arrow function"); - else - failIfFalse(parseSourceElements(syntaxChecker, CheckForStrictMode), bodyType == StandardFunctionBodyBlock ? "Cannot parse body of this function" : "Cannot parse body of this arrow function"); + typename TreeBuilder::FunctionBodyBuilder bodyBuilder(const_cast<VM*>(m_vm), m_lexer.get()); + failIfFalse(parseSourceElements(bodyBuilder, CheckForStrictMode), "Cannot parse body of this function"); unsigned endColumn = tokenColumn(); - return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode); + return context.createFunctionBody(startLocation, tokenLocation(), startColumn, endColumn, strictMode()); } -static const char* stringForFunctionMode(SourceParseMode mode) +static const char* stringForFunctionMode(FunctionParseMode mode) { switch (mode) { - case SourceParseMode::GetterMode: + case GetterMode: return "getter"; - case SourceParseMode::SetterMode: + case SetterMode: return "setter"; - case SourceParseMode::NormalFunctionMode: + case FunctionMode: return "function"; - case SourceParseMode::MethodMode: - return "method"; - case SourceParseMode::ArrowFunctionMode: - return "arrow function"; - case SourceParseMode::ProgramMode: - case SourceParseMode::ModuleAnalyzeMode: - case SourceParseMode::ModuleEvaluateMode: - RELEASE_ASSERT_NOT_REACHED(); - return ""; } RELEASE_ASSERT_NOT_REACHED(); return nullptr; } -template <typename LexerType> template <class TreeBuilder> int Parser<LexerType>::parseFunctionParameters(TreeBuilder& context, SourceParseMode mode, ParserFunctionInfo<TreeBuilder>& functionInfo) -{ - RELEASE_ASSERT(mode != SourceParseMode::ProgramMode && mode != SourceParseMode::ModuleAnalyzeMode && mode != SourceParseMode::ModuleEvaluateMode); - int parametersStart = m_token.m_location.startOffset; - TreeFormalParameterList parameterList = context.createFormalParameterList(); - functionInfo.parameters = parameterList; - functionInfo.startOffset = parametersStart; - - if (mode == SourceParseMode::ArrowFunctionMode) { - if (!match(IDENT) && !match(OPENPAREN)) { - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failWithMessage("Expected an arrow function input parameter"); - } else { - if (match(OPENPAREN)) { - next(); - - if (match(CLOSEPAREN)) - functionInfo.parameterCount = 0; - else - failIfFalse(parseFormalParameters(context, parameterList, functionInfo.parameterCount), "Cannot parse parameters for this ", stringForFunctionMode(mode)); - - consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); - } else { - functionInfo.parameterCount = 1; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported); - failIfFalse(parameter, "Cannot parse parameter pattern"); - context.appendParameter(parameterList, parameter, 0); - } - } - - return parametersStart; - } - - if (!consume(OPENPAREN)) { - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failWithMessage("Expected an opening '(' before a ", stringForFunctionMode(mode), "'s parameter list"); - } - - if (mode == SourceParseMode::GetterMode) { - consumeOrFail(CLOSEPAREN, "getter functions must have no parameters"); - functionInfo.parameterCount = 0; - } else if (mode == SourceParseMode::SetterMode) { - failIfTrue(match(CLOSEPAREN), "setter functions must have one parameter"); - const Identifier* duplicateParameter = nullptr; - auto parameter = parseDestructuringPattern(context, DestructureToParameters, ExportType::NotExported, &duplicateParameter); - failIfFalse(parameter, "setter functions must have one parameter"); - auto defaultValue = parseDefaultValueForDestructuringPattern(context); - propagateError(); - semanticFailIfTrue(duplicateParameter && defaultValue, "Duplicate parameter '", duplicateParameter->impl(), "' not allowed in function with default parameter values"); - context.appendParameter(parameterList, parameter, defaultValue); - functionInfo.parameterCount = 1; - failIfTrue(match(COMMA), "setter functions must have one parameter"); - consumeOrFail(CLOSEPAREN, "Expected a ')' after a parameter declaration"); - } else { - if (match(CLOSEPAREN)) - functionInfo.parameterCount = 0; - else - failIfFalse(parseFormalParameters(context, parameterList, functionInfo.parameterCount), "Cannot parse parameters for this ", stringForFunctionMode(mode)); - consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); - } - - return parametersStart; -} - template <typename LexerType> -template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, SourceParseMode mode, bool nameIsInContainingScope, ConstructorKind constructorKind, SuperBinding expectedSuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>& functionInfo, FunctionParseType parseType) +template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, FunctionParseMode mode, bool nameIsInContainingScope, const Identifier*& name, TreeFormalParameterList& parameters, TreeFunctionBody& body, unsigned& openBraceOffset, unsigned& closeBraceOffset, int& bodyStartLine, unsigned& bodyStartColumn) { - RELEASE_ASSERT(isFunctionParseMode(mode)); - AutoPopScopeRef functionScope(this, pushScope()); functionScope->setIsFunction(); int functionNameStart = m_token.m_location.startOffset; const Identifier* lastFunctionName = m_lastFunctionName; m_lastFunctionName = nullptr; - int parametersStart; - JSTokenLocation startLocation; - int startColumn; - FunctionBodyType functionBodyType; - - switch (parseType) { - case StandardFunctionParseType: { - RELEASE_ASSERT(mode != SourceParseMode::ArrowFunctionMode); - if (match(IDENT) || isLETMaskedAsIDENT()) { - functionInfo.name = m_token.m_data.ident; - m_lastFunctionName = functionInfo.name; - next(); - if (!nameIsInContainingScope) - failIfTrueIfStrict(functionScope->declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "'", functionInfo.name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode"); - } else if (requirements == FunctionNeedsName) { - if (match(OPENPAREN) && mode == SourceParseMode::NormalFunctionMode) - semanticFail("Function statements must have a name"); - semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); - failDueToUnexpectedToken(); - return false; - } - - startLocation = tokenLocation(); - functionInfo.startLine = tokenLine(); - startColumn = tokenColumn(); - - parametersStart = parseFunctionParameters(context, mode, functionInfo); - propagateError(); - - matchOrFail(OPENBRACE, "Expected an opening '{' at the start of a ", stringForFunctionMode(mode), " body"); - - // BytecodeGenerator emits code to throw TypeError when a class constructor is "call"ed. - // Set ConstructorKind to None for non-constructor methods of classes. - - if (m_defaultConstructorKind != ConstructorKind::None) { - constructorKind = m_defaultConstructorKind; - expectedSuperBinding = m_defaultConstructorKind == ConstructorKind::Derived ? SuperBinding::Needed : SuperBinding::NotNeeded; - } - - functionBodyType = StandardFunctionBodyBlock; - - break; - } -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - case ArrowFunctionParseType: { - RELEASE_ASSERT(mode == SourceParseMode::ArrowFunctionMode); - - startLocation = tokenLocation(); - functionInfo.startLine = tokenLine(); - startColumn = tokenColumn(); - - parametersStart = parseFunctionParameters(context, mode, functionInfo); - propagateError(); - - matchOrFail(ARROWFUNCTION, "Expected a '=>' after arrow function parameter declaration"); - - if (m_lexer->prevTerminator()) - failDueToUnexpectedToken(); - - ASSERT(constructorKind == ConstructorKind::None); - - // Check if arrow body start with {. If it true it mean that arrow function is Fat arrow function - // and we need use common approach to parse function body + if (match(IDENT)) { + name = m_token.m_data.ident; + m_lastFunctionName = name; next(); - functionBodyType = match(OPENBRACE) ? ArrowFunctionBodyBlock : ArrowFunctionBodyExpression; - - break; + if (!nameIsInContainingScope) + failIfFalseIfStrict(functionScope->declareVariable(name), "'", name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode"); + } else if (requirements == FunctionNeedsName) { + if (match(OPENPAREN) && mode == FunctionMode) + semanticFail("Function statements must have a name"); + semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); + failDueToUnexpectedToken(); + return false; } -#else - default: - RELEASE_ASSERT_NOT_REACHED(); -#endif + if (!consume(OPENPAREN)) { + semanticFailureDueToKeyword(stringForFunctionMode(mode), " name"); + failWithMessage("Expected an opening '(' before a ", stringForFunctionMode(mode), "'s parameter list"); + } + if (!match(CLOSEPAREN)) { + parameters = parseFormalParameters(context); + failIfFalse(parameters, "Cannot parse parameters for this ", stringForFunctionMode(mode)); } + consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration"); + matchOrFail(OPENBRACE, "Expected an opening '{' at the start of a ", stringForFunctionMode(mode), " body"); - bool isClassConstructor = constructorKind != ConstructorKind::None; - - functionInfo.bodyStartColumn = startColumn; + openBraceOffset = m_token.m_data.offset; + bodyStartLine = tokenLine(); + bodyStartColumn = m_token.m_data.offset - m_token.m_data.lineStartOffset; + JSTokenLocation startLocation(tokenLocation()); // If we know about this function already, we can use the cached info and skip the parser to the end of the function. - if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(functionInfo.startOffset) : 0) { + if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(openBraceOffset) : 0) { // If we're in a strict context, the cached function info must say it was strict too. ASSERT(!strictMode() || cachedInfo->strictMode); JSTokenLocation endLocation; - endLocation.line = cachedInfo->lastTockenLine; - endLocation.startOffset = cachedInfo->lastTockenStartOffset; - endLocation.lineStartOffset = cachedInfo->lastTockenLineStartOffset; + endLocation.line = cachedInfo->closeBraceLine; + endLocation.startOffset = cachedInfo->closeBraceOffset; + endLocation.lineStartOffset = cachedInfo->closeBraceLineStartOffset; - bool endColumnIsOnStartLine = (endLocation.line == functionInfo.startLine); + bool endColumnIsOnStartLine = (endLocation.line == bodyStartLine); ASSERT(endLocation.startOffset >= endLocation.lineStartOffset); unsigned bodyEndColumn = endColumnIsOnStartLine ? endLocation.startOffset - m_token.m_data.lineStartOffset : endLocation.startOffset - endLocation.lineStartOffset; - unsigned currentLineStartOffset = m_token.m_location.lineStartOffset; - functionInfo.body = context.createFunctionMetadata( - startLocation, endLocation, functionInfo.bodyStartColumn, bodyEndColumn, - functionKeywordStart, functionNameStart, parametersStart, - cachedInfo->strictMode, constructorKind, cachedInfo->parameterCount, mode); + body = context.createFunctionBody(startLocation, endLocation, bodyStartColumn, bodyEndColumn, cachedInfo->strictMode); functionScope->restoreFromSourceProviderCache(cachedInfo); - popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo); - - m_token = cachedInfo->endFunctionToken(); + failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error"); - if (endColumnIsOnStartLine) - m_token.m_location.lineStartOffset = currentLineStartOffset; + closeBraceOffset = cachedInfo->closeBraceOffset; + + context.setFunctionNameStart(body, functionNameStart); + m_token = cachedInfo->closeBraceToken(); m_lexer->setOffset(m_token.m_location.endOffset, m_token.m_location.lineStartOffset); m_lexer->setLineNumber(m_token.m_location.line); - functionInfo.endOffset = cachedInfo->endFunctionOffset; - - if (parseType == ArrowFunctionParseType) - functionBodyType = cachedInfo->isBodyArrowExpression ? ArrowFunctionBodyExpression : ArrowFunctionBodyBlock; - else - functionBodyType = StandardFunctionBodyBlock; - switch (functionBodyType) { - case ArrowFunctionBodyExpression: - next(); - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - break; - case ArrowFunctionBodyBlock: - case StandardFunctionBodyBlock: - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - next(); - break; - } - functionInfo.endLine = m_lastTokenEndPosition.line; + next(); return true; } - m_lastFunctionName = lastFunctionName; ParserState oldState = saveState(); - - functionInfo.body = parseFunctionBody(context, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, functionBodyType, functionInfo.parameterCount, mode); - + body = parseFunctionBody(context); restoreState(oldState); - failIfFalse(functionInfo.body, "Cannot parse the body of this ", stringForFunctionMode(mode)); - context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); - if (functionScope->strictMode() && functionInfo.name) { - RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode); - semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); - semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); - } - if (functionScope->hasDirectSuper()) { - semanticFailIfTrue(!isClassConstructor, "Cannot call super() outside of a class constructor"); - semanticFailIfTrue(constructorKind != ConstructorKind::Derived, "Cannot call super() in a base class constructor"); - } - if (functionScope->needsSuperBinding()) - semanticFailIfTrue(expectedSuperBinding == SuperBinding::NotNeeded, "super can only be used in a method of a derived class"); - - JSTokenLocation location = JSTokenLocation(m_token.m_location); - functionInfo.endOffset = m_token.m_data.offset; - - if (functionBodyType == ArrowFunctionBodyExpression) { - location = locationBeforeLastToken(); - functionInfo.endOffset = location.endOffset; + failIfFalse(body, "Cannot parse the body of this ", stringForFunctionMode(mode)); + if (functionScope->strictMode() && name) { + RELEASE_ASSERT(mode == FunctionMode); + semanticFailIfTrue(m_vm->propertyNames->arguments == *name, "'", name->impl(), "' is not a valid function name in strict mode"); + semanticFailIfTrue(m_vm->propertyNames->eval == *name, "'", name->impl(), "' is not a valid function name in strict mode"); } + closeBraceOffset = m_token.m_data.offset; + unsigned closeBraceLine = m_token.m_data.line; + unsigned closeBraceLineStartOffset = m_token.m_data.lineStartOffset; // Cache the tokenizer state and the function scope the first time the function is parsed. // Any future reparsing can then skip the function. static const int minimumFunctionLengthToCache = 16; std::unique_ptr<SourceProviderCacheItem> newInfo; - int functionLength = functionInfo.endOffset - functionInfo.startOffset; + int functionLength = closeBraceOffset - openBraceOffset; if (TreeBuilder::CanUseFunctionCache && m_functionCache && functionLength > minimumFunctionLengthToCache) { SourceProviderCacheItemCreationParameters parameters; - parameters.endFunctionOffset = functionInfo.endOffset; parameters.functionNameStart = functionNameStart; - parameters.lastTockenLine = location.line; - parameters.lastTockenStartOffset = location.startOffset; - parameters.lastTockenEndOffset = location.endOffset; - parameters.lastTockenLineStartOffset = location.lineStartOffset; - parameters.parameterCount = functionInfo.parameterCount; - if (functionBodyType == ArrowFunctionBodyExpression) { - parameters.isBodyArrowExpression = true; - parameters.tokenType = m_token.m_type; - } + parameters.closeBraceLine = closeBraceLine; + parameters.closeBraceOffset = closeBraceOffset; + parameters.closeBraceLineStartOffset = closeBraceLineStartOffset; functionScope->fillParametersForSourceProviderCache(parameters); newInfo = SourceProviderCacheItem::create(parameters); + } + context.setFunctionNameStart(body, functionNameStart); - popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo); - - if (functionBodyType == ArrowFunctionBodyExpression) - failIfFalse(isEndOfArrowFunction(), "Expected the closing ';' ',' ']' ')' '}', line terminator or EOF after arrow function"); - else { - matchOrFail(CLOSEBRACE, "Expected a closing '}' after a ", stringForFunctionMode(mode), " body"); - next(); - } + failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error"); + matchOrFail(CLOSEBRACE, "Expected a closing '}' after a ", stringForFunctionMode(mode), " body"); if (newInfo) - m_functionCache->add(functionInfo.startOffset, WTF::move(newInfo)); + m_functionCache->add(openBraceOffset, std::move(newInfo)); - functionInfo.endLine = m_lastTokenEndPosition.line; + next(); return true; } template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclaration(TreeBuilder& context, ExportType exportType) +template <class TreeBuilder> TreeStatement Parser<LexerType>::parseFunctionDeclaration(TreeBuilder& context) { ASSERT(match(FUNCTION)); JSTokenLocation location(tokenLocation()); - unsigned functionKeywordStart = tokenStart(); next(); - ParserFunctionInfo<TreeBuilder> functionInfo; - failIfFalse((parseFunctionInfo(context, FunctionNeedsName, SourceParseMode::NormalFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, - functionKeywordStart, functionInfo, StandardFunctionParseType)), "Cannot parse this function"); - failIfFalse(functionInfo.name, "Function statements must have a name"); - failIfTrueIfStrict(declareVariable(functionInfo.name) & DeclarationResult::InvalidStrictMode, "Cannot declare a function named '", functionInfo.name->impl(), "' in strict mode"); - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*functionInfo.name), "Cannot export a duplicate function name: '", functionInfo.name->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*functionInfo.name); - } - return context.createFuncDeclStatement(location, functionInfo); -} - -#if ENABLE(ES6_CLASS_SYNTAX) -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseClassDeclaration(TreeBuilder& context, ExportType exportType) -{ - ASSERT(match(CLASSTOKEN)); - JSTokenLocation location(tokenLocation()); - JSTextPosition classStart = tokenStartPosition(); - unsigned classStartLine = tokenLine(); - - ParserClassInfo<TreeBuilder> info; - TreeClassExpression classExpr = parseClass(context, FunctionNeedsName, info); - failIfFalse(classExpr, "Failed to parse class"); - - DeclarationResultMask declarationResult = declareVariable(info.className, DeclarationType::LetDeclaration); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Cannot declare a class twice: '", info.className->impl(), "'"); - if (exportType == ExportType::Exported) { - semanticFailIfFalse(exportName(*info.className), "Cannot export a duplicate class name: '", info.className->impl(), "'"); - currentScope()->moduleScopeData().exportBinding(*info.className); - } - - JSTextPosition classEnd = lastTokenEndPosition(); - unsigned classEndLine = tokenLine(); - - return context.createClassDeclStatement(location, classExpr, classStart, classEnd, classStartLine, classEndLine); + const Identifier* name = 0; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; + failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, true, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse this function"); + failIfFalse(name, "Function statements must have a name"); + failIfFalseIfStrict(declareVariable(name), "Cannot declare a function named '", name->impl(), "' in strict mode"); + return context.createFuncDeclStatement(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); } -template <typename LexerType> -template <class TreeBuilder> TreeClassExpression Parser<LexerType>::parseClass(TreeBuilder& context, FunctionRequirements requirements, ParserClassInfo<TreeBuilder>& info) -{ - ASSERT(match(CLASSTOKEN)); - JSTokenLocation location(tokenLocation()); - next(); - - AutoPopScopeRef classScope(this, pushScope()); - classScope->setStrictMode(); - - const Identifier* className = nullptr; - if (match(IDENT)) { - className = m_token.m_data.ident; - info.className = className; - next(); - failIfTrue(classScope->declareVariable(className) & DeclarationResult::InvalidStrictMode, "'", className->impl(), "' is not a valid class name"); - } else if (requirements == FunctionNeedsName) { - if (match(OPENBRACE)) - semanticFail("Class statements must have a name"); - semanticFailureDueToKeyword("class name"); - failDueToUnexpectedToken(); - } else - className = &m_vm->propertyNames->nullIdentifier; - ASSERT(className); - - TreeExpression parentClass = 0; - if (consume(EXTENDS)) { - parentClass = parseMemberExpression(context); - failIfFalse(parentClass, "Cannot parse the parent class name"); - } - const ConstructorKind constructorKind = parentClass ? ConstructorKind::Derived : ConstructorKind::Base; - - consumeOrFail(OPENBRACE, "Expected opening '{' at the start of a class body"); - - TreeExpression constructor = 0; - TreePropertyList staticMethods = 0; - TreePropertyList instanceMethods = 0; - TreePropertyList instanceMethodsTail = 0; - TreePropertyList staticMethodsTail = 0; - while (!match(CLOSEBRACE)) { - if (match(SEMICOLON)) { - next(); - continue; - } - - JSTokenLocation methodLocation(tokenLocation()); - unsigned methodStart = tokenStart(); - - // For backwards compatibility, "static" is a non-reserved keyword in non-strict mode. - bool isStaticMethod = match(RESERVED_IF_STRICT) && *m_token.m_data.ident == m_vm->propertyNames->staticKeyword; - if (isStaticMethod) - next(); - - // FIXME: Figure out a way to share more code with parseProperty. - const CommonIdentifiers& propertyNames = *m_vm->propertyNames; - const Identifier* ident = nullptr; - bool isGetter = false; - bool isSetter = false; - switch (m_token.m_type) { - case STRING: - ident = m_token.m_data.ident; - ASSERT(ident); - next(); - break; - case IDENT: - ident = m_token.m_data.ident; - ASSERT(ident); - next(); - if (match(IDENT) || match(STRING) || match(DOUBLE) || match(INTEGER)) { - isGetter = *ident == propertyNames.get; - isSetter = *ident == propertyNames.set; - } - break; - case DOUBLE: - case INTEGER: - ident = &m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), m_token.m_data.doubleValue); - ASSERT(ident); - next(); - break; - default: - failDueToUnexpectedToken(); - } - - TreeProperty property; - const bool alwaysStrictInsideClass = true; - if (isGetter || isSetter) { - property = parseGetterSetter(context, alwaysStrictInsideClass, isGetter ? PropertyNode::Getter : PropertyNode::Setter, methodStart, - ConstructorKind::None, SuperBinding::Needed); - failIfFalse(property, "Cannot parse this method"); - } else { - ParserFunctionInfo<TreeBuilder> methodInfo; - bool isConstructor = !isStaticMethod && *ident == propertyNames.constructor; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, isConstructor ? constructorKind : ConstructorKind::None, SuperBinding::Needed, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); - failIfTrue(!ident || (declareVariable(ident) & DeclarationResult::InvalidStrictMode), "Cannot declare a method named '", methodInfo.name->impl(), "'"); - methodInfo.name = isConstructor ? className : ident; - - TreeExpression method = context.createFunctionExpr(methodLocation, methodInfo); - if (isConstructor) { - semanticFailIfTrue(constructor, "Cannot declare multiple constructors in a single class"); - constructor = method; - continue; - } - - // FIXME: Syntax error when super() is called - semanticFailIfTrue(isStaticMethod && methodInfo.name && *methodInfo.name == propertyNames.prototype, - "Cannot declare a static method named 'prototype'"); - property = context.createProperty(methodInfo.name, method, PropertyNode::Constant, PropertyNode::Unknown, alwaysStrictInsideClass, SuperBinding::Needed); - } - - TreePropertyList& tail = isStaticMethod ? staticMethodsTail : instanceMethodsTail; - if (tail) - tail = context.createPropertyList(methodLocation, property, tail); - else { - tail = context.createPropertyList(methodLocation, property); - if (isStaticMethod) - staticMethods = tail; - else - instanceMethods = tail; - } - } - - popScope(classScope, TreeBuilder::NeedsFreeVariableInfo); - consumeOrFail(CLOSEBRACE, "Expected a closing '}' after a class body"); - - return context.createClassExpr(location, *className, constructor, parentClass, instanceMethods, staticMethods); -} -#endif - struct LabelInfo { LabelInfo(const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end) : m_ident(ident) @@ -2081,7 +1366,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL failIfTrue(getLabel(ident), "Cannot find scope for the label '", ident->impl(), "'"); labels.append(LabelInfo(ident, start, end)); } - } while (match(IDENT) || isLETMaskedAsIDENT()); + } while (match(IDENT)); bool isLoop = false; switch (m_token.m_type) { case FOR: @@ -2094,7 +1379,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL break; } const Identifier* unused = 0; - ScopeRef labelScope = currentScope(); if (!m_syntaxAlreadyValidated) { for (size_t i = 0; i < labels.size(); i++) pushLabel(labels[i].m_ident, isLoop); @@ -2102,7 +1386,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL TreeStatement statement = parseStatement(context, unused); if (!m_syntaxAlreadyValidated) { for (size_t i = 0; i < labels.size(); i++) - popLabel(labelScope); + popLabel(); } failIfFalse(statement, "Cannot parse statement"); for (size_t i = 0; i < labels.size(); i++) { @@ -2115,19 +1399,6 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrL template <typename LexerType> template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionStatement(TreeBuilder& context) { - switch (m_token.m_type) { - // Consult: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-expression-statement - // The ES6 spec mandates that we should fail from FUNCTION token here. We handle this case - // in parseStatement() which is the only caller of parseExpressionStatement(). - // We actually allow FUNCTION in situations where it should not be allowed unless we're in strict mode. - case CLASSTOKEN: - failWithMessage("'class' declaration is not directly within a block statement"); - break; - default: - // FIXME: when implementing 'let' we should fail when we see the token sequence "let [". - // https://bugs.webkit.org/show_bug.cgi?id=142944 - break; - } JSTextPosition start = tokenStartPosition(); JSTokenLocation location(tokenLocation()); TreeExpression expression = parseExpression(context); @@ -2200,9 +1471,7 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseIfStatement(T posStack.removeLast(); JSTokenLocation elseLocation = tokenLocationStack.last(); tokenLocationStack.removeLast(); - TreeStatement ifStatement = context.createIfStatement(elseLocation, condition, trueBlock, 0, pos.first, pos.second); - context.setEndOffset(ifStatement, context.endOffset(trueBlock)); - statementStack.append(ifStatement); + statementStack.append(context.createIfStatement(elseLocation, condition, trueBlock, 0, pos.first, pos.second)); } while (!exprStack.isEmpty()) { @@ -2216,392 +1485,19 @@ template <class TreeBuilder> TreeStatement Parser<LexerType>::parseIfStatement(T posStack.removeLast(); JSTokenLocation elseLocation = tokenLocationStack.last(); tokenLocationStack.removeLast(); - TreeStatement ifStatement = context.createIfStatement(elseLocation, condition, trueBlock, falseBlock, pos.first, pos.second); - context.setEndOffset(ifStatement, context.endOffset(falseBlock)); - statementStack.append(ifStatement); + statementStack.append(context.createIfStatement(elseLocation, condition, trueBlock, falseBlock, pos.first, pos.second)); } return context.createIfStatement(ifLocation, condition, trueBlock, statementStack.last(), start, end); } template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ModuleName Parser<LexerType>::parseModuleName(TreeBuilder& context) -{ - // ModuleName (ModuleSpecifier in the spec) represents the module name imported by the script. - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - JSTokenLocation specifierLocation(tokenLocation()); - failIfFalse(match(STRING), "Imported modules names must be string literals"); - const Identifier* moduleName = m_token.m_data.ident; - next(); - return context.createModuleName(specifierLocation, *moduleName); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ImportSpecifier Parser<LexerType>::parseImportClauseItem(TreeBuilder& context, ImportSpecifierType specifierType) -{ - // Produced node is the item of the ImportClause. - // That is the ImportSpecifier, ImportedDefaultBinding or NameSpaceImport. - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - JSTokenLocation specifierLocation(tokenLocation()); - JSToken localNameToken; - const Identifier* importedName = nullptr; - const Identifier* localName = nullptr; - - switch (specifierType) { - case ImportSpecifierType::NamespaceImport: { - // NameSpaceImport : - // * as ImportedBinding - // e.g. - // * as namespace - ASSERT(match(TIMES)); - importedName = &m_vm->propertyNames->timesIdentifier; - next(); - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->as), "Expected 'as' before imported binding name"); - next(); - - matchOrFail(IDENT, "Expected a variable name for the import declaration"); - localNameToken = m_token; - localName = m_token.m_data.ident; - next(); - break; - } - - case ImportSpecifierType::NamedImport: { - // ImportSpecifier : - // ImportedBinding - // IdentifierName as ImportedBinding - // e.g. - // A - // A as B - ASSERT(matchIdentifierOrKeyword()); - localNameToken = m_token; - localName = m_token.m_data.ident; - importedName = localName; - next(); - - if (matchContextualKeyword(m_vm->propertyNames->as)) { - next(); - matchOrFail(IDENT, "Expected a variable name for the import declaration"); - localNameToken = m_token; - localName = m_token.m_data.ident; - next(); - } - break; - } - - case ImportSpecifierType::DefaultImport: { - // ImportedDefaultBinding : - // ImportedBinding - ASSERT(match(IDENT)); - localNameToken = m_token; - localName = m_token.m_data.ident; - importedName = &m_vm->propertyNames->defaultKeyword; - next(); - break; - } - } - - semanticFailIfTrue(localNameToken.m_type & KeywordTokenFlag, "Cannot use keyword as imported binding name"); - DeclarationResultMask declarationResult = declareVariable(localName, DeclarationType::ConstDeclaration, DeclarationImportType::Imported); - if (declarationResult != DeclarationResult::Valid) { - failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare an imported binding named ", localName->impl(), " in strict mode"); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Cannot declare an imported binding name twice: '", localName->impl(), "'"); - } - - return context.createImportSpecifier(specifierLocation, *importedName, *localName); -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseImportDeclaration(TreeBuilder& context) -{ - // http://www.ecma-international.org/ecma-262/6.0/#sec-imports - ASSERT(match(IMPORT)); - JSTokenLocation importLocation(tokenLocation()); - next(); - - auto specifierList = context.createImportSpecifierList(); - - if (match(STRING)) { - // import ModuleSpecifier ; - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the module name"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); - return context.createImportDeclaration(importLocation, specifierList, moduleName); - } - - bool isFinishedParsingImport = false; - if (match(IDENT)) { - // ImportedDefaultBinding : - // ImportedBinding - auto specifier = parseImportClauseItem(context, ImportSpecifierType::DefaultImport); - failIfFalse(specifier, "Cannot parse the default import"); - context.appendImportSpecifier(specifierList, specifier); - if (match(COMMA)) - next(); - else - isFinishedParsingImport = true; - } - - if (!isFinishedParsingImport) { - if (match(TIMES)) { - // import NameSpaceImport FromClause ; - auto specifier = parseImportClauseItem(context, ImportSpecifierType::NamespaceImport); - failIfFalse(specifier, "Cannot parse the namespace import"); - context.appendImportSpecifier(specifierList, specifier); - } else if (match(OPENBRACE)) { - // NamedImports : - // { } - // { ImportsList } - // { ImportsList , } - next(); - - while (!match(CLOSEBRACE)) { - failIfFalse(matchIdentifierOrKeyword(), "Expected an imported name for the import declaration"); - auto specifier = parseImportClauseItem(context, ImportSpecifierType::NamedImport); - failIfFalse(specifier, "Cannot parse the named import"); - context.appendImportSpecifier(specifierList, specifier); - if (!consume(COMMA)) - break; - } - handleProductionOrFail(CLOSEBRACE, "}", "end", "import list"); - } else - failWithMessage("Expected namespace import or import list"); - } - - // FromClause : - // from ModuleSpecifier - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), "Expected 'from' before imported module name"); - next(); - - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the module name"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted import declaration"); - - return context.createImportDeclaration(importLocation, specifierList, moduleName); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::ExportSpecifier Parser<LexerType>::parseExportSpecifier(TreeBuilder& context, Vector<const Identifier*>& maybeLocalNames, bool& hasKeywordForLocalBindings) -{ - // ExportSpecifier : - // IdentifierName - // IdentifierName as IdentifierName - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - ASSERT(matchIdentifierOrKeyword()); - JSTokenLocation specifierLocation(tokenLocation()); - if (m_token.m_type & KeywordTokenFlag) - hasKeywordForLocalBindings = true; - const Identifier* localName = m_token.m_data.ident; - const Identifier* exportedName = localName; - next(); - - if (matchContextualKeyword(m_vm->propertyNames->as)) { - next(); - failIfFalse(matchIdentifierOrKeyword(), "Expected an exported name for the export declaration"); - exportedName = m_token.m_data.ident; - next(); - } - - semanticFailIfFalse(exportName(*exportedName), "Cannot export a duplicate name '", exportedName->impl(), "'"); - maybeLocalNames.append(localName); - return context.createExportSpecifier(specifierLocation, *localName, *exportedName); -} - -template <typename LexerType> -template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExportDeclaration(TreeBuilder& context) -{ - // http://www.ecma-international.org/ecma-262/6.0/#sec-exports - ASSERT(match(EXPORT)); - JSTokenLocation exportLocation(tokenLocation()); - next(); - - switch (m_token.m_type) { - case TIMES: { - // export * FromClause ; - next(); - - failIfFalse(matchContextualKeyword(m_vm->propertyNames->from), "Expected 'from' before exported module name"); - next(); - auto moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the 'from' clause"); - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - - return context.createExportAllDeclaration(exportLocation, moduleName); - } - - case DEFAULT: { - // export default HoistableDeclaration[Default] - // export default ClassDeclaration[Default] - // export default [lookahead not-in {function, class}] AssignmentExpression[In] ; - - next(); - - TreeStatement result = 0; - bool isFunctionOrClassDeclaration = false; - const Identifier* localName = nullptr; - SavePoint savePoint = createSavePoint(); - if (match(FUNCTION) -#if ENABLE(ES6_CLASS_SYNTAX) - || match(CLASSTOKEN) -#endif - ) { - isFunctionOrClassDeclaration = true; - next(); - // FIXME: When landing ES6 generators, we need to take care of that '*' comes. - if (match(IDENT)) - localName = m_token.m_data.ident; - restoreSavePoint(savePoint); - } - - if (localName) { - if (match(FUNCTION)) - result = parseFunctionDeclaration(context); -#if ENABLE(ES6_CLASS_SYNTAX) - else { - ASSERT(match(CLASSTOKEN)); - result = parseClassDeclaration(context); - } -#endif - } else { - // export default expr; - // - // It should be treated as the same to the following. - // - // const *default* = expr; - // export { *default* as default } - // - // In the above example, *default* is the invisible variable to the users. - // We use the private symbol to represent the name of this variable. - JSTokenLocation location(tokenLocation()); - JSTextPosition start = tokenStartPosition(); - TreeExpression expression = parseAssignmentExpression(context); - failIfFalse(expression, "Cannot parse expression"); - - DeclarationResultMask declarationResult = declareVariable(&m_vm->propertyNames->starDefaultPrivateName, DeclarationType::ConstDeclaration); - if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) - internalFailWithMessage(false, "Only one 'default' export is allowed"); - - TreeExpression assignment = context.createAssignResolve(location, m_vm->propertyNames->starDefaultPrivateName, expression, start, start, tokenEndPosition(), AssignmentContext::ConstDeclarationStatement); - result = context.createExprStatement(location, assignment, start, tokenEndPosition()); - if (!isFunctionOrClassDeclaration) - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - localName = &m_vm->propertyNames->starDefaultPrivateName; - } - failIfFalse(result, "Cannot parse the declaration"); - - semanticFailIfFalse(exportName(m_vm->propertyNames->defaultKeyword), "Only one 'default' export is allowed"); - currentScope()->moduleScopeData().exportBinding(*localName); - return context.createExportDefaultDeclaration(exportLocation, result, *localName); - } - - case OPENBRACE: { - // export ExportClause FromClause ; - // export ExportClause ; - // - // ExportClause : - // { } - // { ExportsList } - // { ExportsList , } - // - // ExportsList : - // ExportSpecifier - // ExportsList , ExportSpecifier - - next(); - - auto specifierList = context.createExportSpecifierList(); - Vector<const Identifier*> maybeLocalNames; - - bool hasKeywordForLocalBindings = false; - while (!match(CLOSEBRACE)) { - failIfFalse(matchIdentifierOrKeyword(), "Expected a variable name for the export declaration"); - auto specifier = parseExportSpecifier(context, maybeLocalNames, hasKeywordForLocalBindings); - failIfFalse(specifier, "Cannot parse the named export"); - context.appendExportSpecifier(specifierList, specifier); - if (!consume(COMMA)) - break; - } - handleProductionOrFail(CLOSEBRACE, "}", "end", "export list"); - - typename TreeBuilder::ModuleName moduleName = 0; - if (matchContextualKeyword(m_vm->propertyNames->from)) { - next(); - moduleName = parseModuleName(context); - failIfFalse(moduleName, "Cannot parse the 'from' clause"); - } - failIfFalse(autoSemiColon(), "Expected a ';' following a targeted export declaration"); - - if (!moduleName) { - semanticFailIfTrue(hasKeywordForLocalBindings, "Cannot use keyword as exported variable name"); - // Since this export declaration does not have module specifier part, it exports the local bindings. - // While the export declaration with module specifier does not have any effect on the current module's scope, - // the export named declaration without module specifier references the the local binding names. - // For example, - // export { A, B, C as D } from "mod" - // does not have effect on the current module's scope. But, - // export { A, B, C as D } - // will reference the current module's bindings. - for (const Identifier* localName : maybeLocalNames) { - currentScope()->useVariable(localName, m_vm->propertyNames->eval == *localName); - currentScope()->moduleScopeData().exportBinding(*localName); - } - } - - return context.createExportNamedDeclaration(exportLocation, specifierList, moduleName); - } - - default: { - // export VariableStatement - // export Declaration - TreeStatement result = 0; - switch (m_token.m_type) { - case VAR: - result = parseVariableDeclaration(context, DeclarationType::VarDeclaration, ExportType::Exported); - break; - - case CONSTTOKEN: - result = parseVariableDeclaration(context, DeclarationType::ConstDeclaration, ExportType::Exported); - break; - - case LET: - result = parseVariableDeclaration(context, DeclarationType::LetDeclaration, ExportType::Exported); - break; - - case FUNCTION: - result = parseFunctionDeclaration(context, ExportType::Exported); - break; - -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: - result = parseClassDeclaration(context, ExportType::Exported); - break; -#endif - - default: - failWithMessage("Expected either a declaration or a variable statement"); - break; - } - failIfFalse(result, "Cannot parse the declaration"); - return context.createExportLocalDeclaration(exportLocation, result); - } - } - - RELEASE_ASSERT_NOT_REACHED(); - return 0; -} - -template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseExpression(TreeBuilder& context) { failIfStackOverflow(); JSTokenLocation location(tokenLocation()); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression"); - context.setEndOffset(node, m_lastTokenEndPosition.offset); if (!match(COMMA)) return node; next(); @@ -2609,21 +1505,16 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseExpression(T m_nonLHSCount++; TreeExpression right = parseAssignmentExpression(context); failIfFalse(right, "Cannot parse expression in a comma expression"); - context.setEndOffset(right, m_lastTokenEndPosition.offset); - typename TreeBuilder::Comma head = context.createCommaExpr(location, node); - typename TreeBuilder::Comma tail = context.appendToCommaExpr(location, head, head, right); + typename TreeBuilder::Comma commaNode = context.createCommaExpr(location, node, right); while (match(COMMA)) { next(TreeBuilder::DontBuildStrings); right = parseAssignmentExpression(context); failIfFalse(right, "Cannot parse expression in a comma expression"); - context.setEndOffset(right, m_lastTokenEndPosition.offset); - tail = context.appendToCommaExpr(location, head, tail, right); + context.appendToComma(commaNode, right); } - context.setEndOffset(head, m_lastTokenEndPosition.offset); - return head; + return commaNode; } - template <typename LexerType> template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmentExpression(TreeBuilder& context) { @@ -2634,20 +1525,14 @@ template <typename TreeBuilder> TreeExpression Parser<LexerType>::parseAssignmen int initialNonLHSCount = m_nonLHSCount; if (match(OPENBRACE) || match(OPENBRACKET)) { SavePoint savePoint = createSavePoint(); - auto pattern = tryParseDestructuringPatternExpression(context, AssignmentContext::AssignmentExpression); + auto pattern = tryParseDeconstructionPatternExpression(context); if (pattern && consume(EQUAL)) { auto rhs = parseAssignmentExpression(context); if (rhs) - return context.createDestructuringAssignment(location, pattern, rhs); + return context.createDeconstructingAssignment(location, pattern, rhs); } restoreSavePoint(savePoint); } - -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - if (isArrowFunctionParamters()) - return parseArrowFunctionExpression(context); -#endif - TreeExpression lhs = parseConditionalExpression(context); failIfFalse(lhs, "Cannot parse expression"); if (initialNonLHSCount != m_nonLHSCount) { @@ -2723,12 +1608,10 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseConditionalE next(TreeBuilder::DontBuildStrings); TreeExpression lhs = parseAssignmentExpression(context); failIfFalse(lhs, "Cannot parse left hand side of ternary operator"); - context.setEndOffset(lhs, m_lastTokenEndPosition.offset); consumeOrFailWithFlags(COLON, TreeBuilder::DontBuildStrings, "Expected ':' in ternary operator"); TreeExpression rhs = parseAssignmentExpression(context); failIfFalse(rhs, "Cannot parse right hand side of ternary operator"); - context.setEndOffset(rhs, m_lastTokenEndPosition.offset); return context.createConditionalExpr(location, cond, lhs, rhs); } @@ -2748,6 +1631,7 @@ int Parser<LexerType>::isBinaryOperator(JSTokenType token) template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseBinaryExpression(TreeBuilder& context) { + int operandStackDepth = 0; int operatorStackDepth = 0; typename TreeBuilder::BinaryExprContext binaryExprContext(context); @@ -2801,36 +1685,25 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB FALLTHROUGH; case STRING: { const Identifier* ident = m_token.m_data.ident; - unsigned getterOrSetterStartOffset = tokenStart(); if (complete || (wasIdent && (*ident == m_vm->propertyNames->get || *ident == m_vm->propertyNames->set))) nextExpectIdentifier(LexerFlagsIgnoreReservedWords); else nextExpectIdentifier(LexerFlagsIgnoreReservedWords | TreeBuilder::DontBuildKeywords); - + if (match(COLON)) { next(); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(ident, node, PropertyNode::Constant, PropertyNode::Unknown, complete); + return context.createProperty(ident, node, PropertyNode::Constant, complete); } - - if (match(OPENPAREN)) { - auto method = parsePropertyMethod(context, ident); - propagateError(); - return context.createProperty(ident, method, PropertyNode::Constant, PropertyNode::KnownDirect, complete); - } - failIfFalse(wasIdent, "Expected an identifier as property name"); - - if (match(COMMA) || match(CLOSEBRACE)) { - JSTextPosition start = tokenStartPosition(); - JSTokenLocation location(tokenLocation()); - currentScope()->useVariable(ident, m_vm->propertyNames->eval == *ident); - TreeExpression node = context.createResolve(location, ident, start); - return context.createProperty(ident, node, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Shorthand), PropertyNode::KnownDirect, complete); - } - + const Identifier* accessorName = 0; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; PropertyNode::Type type; if (*ident == m_vm->propertyNames->get) type = PropertyNode::Getter; @@ -2838,43 +1711,45 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB type = PropertyNode::Setter; else failWithMessage("Expected a ':' following the property name '", ident->impl(), "'"); - return parseGetterSetter(context, complete, type, getterOrSetterStartOffset); + const Identifier* stringPropertyName = 0; + double numericPropertyName = 0; + if (m_token.m_type == IDENT || m_token.m_type == STRING) + stringPropertyName = m_token.m_data.ident; + else if (m_token.m_type == NUMBER) + numericPropertyName = m_token.m_data.doubleValue; + else + failDueToUnexpectedToken(); + JSTokenLocation location(tokenLocation()); + next(); + if (type == PropertyNode::Getter) { + failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition"); + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse getter definition"); + } else { + failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition"); + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse setter definition"); + } + if (stringPropertyName) + return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); + return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), location, type, complete, numericPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); } - case DOUBLE: - case INTEGER: { + case NUMBER: { double propertyName = m_token.m_data.doubleValue; next(); - - if (match(OPENPAREN)) { - const Identifier& ident = m_parserArena.identifierArena().makeNumericIdentifier(const_cast<VM*>(m_vm), propertyName); - auto method = parsePropertyMethod(context, &ident); - propagateError(); - return context.createProperty(&ident, method, PropertyNode::Constant, PropertyNode::Unknown, complete); - } - consumeOrFail(COLON, "Expected ':' after property name"); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(const_cast<VM*>(m_vm), m_parserArena, propertyName, node, PropertyNode::Constant, PropertyNode::Unknown, complete); + return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete); } case OPENBRACKET: { next(); - auto propertyName = parseAssignmentExpression(context); + auto propertyName = parseExpression(context); failIfFalse(propertyName, "Cannot parse computed property name"); + handleProductionOrFail(CLOSEBRACKET, "]", "end", "computed property name"); - - if (match(OPENPAREN)) { - auto method = parsePropertyMethod(context, &m_vm->propertyNames->nullIdentifier); - propagateError(); - return context.createProperty(propertyName, method, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Computed), PropertyNode::KnownDirect, complete); - } - consumeOrFail(COLON, "Expected ':' after property name"); TreeExpression node = parseAssignmentExpression(context); failIfFalse(node, "Cannot parse expression for property declaration"); - context.setEndOffset(node, m_lexer->currentOffset()); - return context.createProperty(propertyName, node, static_cast<PropertyNode::Type>(PropertyNode::Constant | PropertyNode::Computed), PropertyNode::Unknown, complete); + return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete); } default: failIfFalse(m_token.m_type & KeywordTokenFlag, "Expected a property name"); @@ -2883,71 +1758,14 @@ template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeB } template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePropertyMethod(TreeBuilder& context, const Identifier* methodName) -{ - JSTokenLocation methodLocation(tokenLocation()); - unsigned methodStart = tokenStart(); - ParserFunctionInfo<TreeBuilder> methodInfo; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::MethodMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, StandardFunctionParseType)), "Cannot parse this method"); - methodInfo.name = methodName; - return context.createFunctionExpr(methodLocation, methodInfo); -} - -template <typename LexerType> -template <class TreeBuilder> TreeProperty Parser<LexerType>::parseGetterSetter(TreeBuilder& context, bool strict, PropertyNode::Type type, unsigned getterOrSetterStartOffset, - ConstructorKind constructorKind, SuperBinding superBinding) -{ - const Identifier* stringPropertyName = 0; - double numericPropertyName = 0; - if (m_token.m_type == IDENT || m_token.m_type == STRING || isLETMaskedAsIDENT()) { - stringPropertyName = m_token.m_data.ident; - semanticFailIfTrue(superBinding == SuperBinding::Needed && *stringPropertyName == m_vm->propertyNames->prototype, - "Cannot declare a static method named 'prototype'"); - semanticFailIfTrue(superBinding == SuperBinding::Needed && *stringPropertyName == m_vm->propertyNames->constructor, - "Cannot declare a getter or setter named 'constructor'"); - } else if (m_token.m_type == DOUBLE || m_token.m_type == INTEGER) - numericPropertyName = m_token.m_data.doubleValue; - else - failDueToUnexpectedToken(); - JSTokenLocation location(tokenLocation()); - next(); - ParserFunctionInfo<TreeBuilder> info; - if (type & PropertyNode::Getter) { - failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition"); - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::GetterMode, false, constructorKind, superBinding, - getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse getter definition"); - } else { - failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition"); - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::SetterMode, false, constructorKind, superBinding, - getterOrSetterStartOffset, info, StandardFunctionParseType)), "Cannot parse setter definition"); - } - if (stringPropertyName) - return context.createGetterOrSetterProperty(location, type, strict, stringPropertyName, info, superBinding); - return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, strict, numericPropertyName, info, superBinding); -} - -template <typename LexerType> -template <class TreeBuilder> bool Parser<LexerType>::shouldCheckPropertyForUnderscoreProtoDuplicate(TreeBuilder& context, const TreeProperty& property) -{ - if (m_syntaxAlreadyValidated) - return false; - - if (!context.getName(property)) - return false; - - // A Constant property that is not a Computed or Shorthand Constant property. - return context.getType(property) == PropertyNode::Constant; -} - -template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLiteral(TreeBuilder& context) { auto savePoint = createSavePoint(); consumeOrFailWithFlags(OPENBRACE, TreeBuilder::DontBuildStrings, "Expected opening '{' at the start of an object literal"); + JSTokenLocation location(tokenLocation()); int oldNonLHSCount = m_nonLHSCount; - - JSTokenLocation location(tokenLocation()); + if (match(CLOSEBRACE)) { next(); return context.createObjectLiteral(location); @@ -2955,35 +1773,24 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseObjectLitera TreeProperty property = parseProperty(context, false); failIfFalse(property, "Cannot parse object literal property"); - - if (!m_syntaxAlreadyValidated && context.getType(property) & (PropertyNode::Getter | PropertyNode::Setter)) { + if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) { restoreSavePoint(savePoint); return parseStrictObjectLiteral(context); } - - bool seenUnderscoreProto = false; - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) - seenUnderscoreProto = *context.getName(property) == m_vm->propertyNames->underscoreProto; - TreePropertyList propertyList = context.createPropertyList(location, property); TreePropertyList tail = propertyList; while (match(COMMA)) { next(TreeBuilder::DontBuildStrings); + // allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 if (match(CLOSEBRACE)) break; JSTokenLocation propertyLocation(tokenLocation()); property = parseProperty(context, false); failIfFalse(property, "Cannot parse object literal property"); - if (!m_syntaxAlreadyValidated && context.getType(property) & (PropertyNode::Getter | PropertyNode::Setter)) { + if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) { restoreSavePoint(savePoint); return parseStrictObjectLiteral(context); } - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) { - if (*context.getName(property) == m_vm->propertyNames->underscoreProto) { - semanticFailIfTrue(seenUnderscoreProto, "Attempted to redefine __proto__ property"); - seenUnderscoreProto = true; - } - } tail = context.createPropertyList(propertyLocation, property, tail); } @@ -3010,24 +1817,30 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseStrictObject TreeProperty property = parseProperty(context, true); failIfFalse(property, "Cannot parse object literal property"); - - bool seenUnderscoreProto = false; - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) - seenUnderscoreProto = *context.getName(property) == m_vm->propertyNames->underscoreProto; - + + typedef HashMap<RefPtr<StringImpl>, unsigned, IdentifierRepHash> ObjectValidationMap; + ObjectValidationMap objectValidator; + // Add the first property + if (!m_syntaxAlreadyValidated && context.getName(property)) + objectValidator.add(context.getName(property)->impl(), context.getType(property)); + TreePropertyList propertyList = context.createPropertyList(location, property); TreePropertyList tail = propertyList; while (match(COMMA)) { next(); + // allow extra comma, see http://bugs.webkit.org/show_bug.cgi?id=5939 if (match(CLOSEBRACE)) break; JSTokenLocation propertyLocation(tokenLocation()); property = parseProperty(context, true); failIfFalse(property, "Cannot parse object literal property"); - if (shouldCheckPropertyForUnderscoreProtoDuplicate(context, property)) { - if (*context.getName(property) == m_vm->propertyNames->underscoreProto) { - semanticFailIfTrue(seenUnderscoreProto, "Attempted to redefine __proto__ property"); - seenUnderscoreProto = true; + if (!m_syntaxAlreadyValidated && context.getName(property)) { + ObjectValidationMap::AddResult propertyEntry = objectValidator.add(context.getName(property)->impl(), context.getType(property)); + if (!propertyEntry.isNewEntry) { + semanticFailIfTrue(propertyEntry.iterator->value == PropertyNode::Constant, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + semanticFailIfTrue(context.getType(property) == PropertyNode::Constant, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + semanticFailIfTrue(context.getType(property) & propertyEntry.iterator->value, "Attempted to redefine property '", propertyEntry.iterator->key.get(), "'"); + propertyEntry.iterator->value |= context.getType(property); } } tail = context.createPropertyList(propertyLocation, property, tail); @@ -3115,86 +1928,11 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrayLiteral return context.createArray(location, elementList); } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::TemplateString Parser<LexerType>::parseTemplateString(TreeBuilder& context, bool isTemplateHead, typename LexerType::RawStringsBuildMode rawStringsBuildMode, bool& elementIsTail) -{ - if (!isTemplateHead) { - matchOrFail(CLOSEBRACE, "Expected a closing '}' following an expression in template literal"); - // Re-scan the token to recognize it as Template Element. - m_token.m_type = m_lexer->scanTrailingTemplateString(&m_token, rawStringsBuildMode); - } - matchOrFail(TEMPLATE, "Expected an template element"); - const Identifier* cooked = m_token.m_data.cooked; - const Identifier* raw = m_token.m_data.raw; - elementIsTail = m_token.m_data.isTail; - JSTokenLocation location(tokenLocation()); - next(); - return context.createTemplateString(location, *cooked, *raw); -} - -template <typename LexerType> -template <class TreeBuilder> typename TreeBuilder::TemplateLiteral Parser<LexerType>::parseTemplateLiteral(TreeBuilder& context, typename LexerType::RawStringsBuildMode rawStringsBuildMode) -{ - JSTokenLocation location(tokenLocation()); - bool elementIsTail = false; - - auto headTemplateString = parseTemplateString(context, true, rawStringsBuildMode, elementIsTail); - failIfFalse(headTemplateString, "Cannot parse head template element"); - - typename TreeBuilder::TemplateStringList templateStringList = context.createTemplateStringList(headTemplateString); - typename TreeBuilder::TemplateStringList templateStringTail = templateStringList; - - if (elementIsTail) - return context.createTemplateLiteral(location, templateStringList); - - failIfTrue(match(CLOSEBRACE), "Template literal expression cannot be empty"); - TreeExpression expression = parseExpression(context); - failIfFalse(expression, "Cannot parse expression in template literal"); - - typename TreeBuilder::TemplateExpressionList templateExpressionList = context.createTemplateExpressionList(expression); - typename TreeBuilder::TemplateExpressionList templateExpressionTail = templateExpressionList; - - auto templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail); - failIfFalse(templateString, "Cannot parse template element"); - templateStringTail = context.createTemplateStringList(templateStringTail, templateString); - - while (!elementIsTail) { - failIfTrue(match(CLOSEBRACE), "Template literal expression cannot be empty"); - TreeExpression expression = parseExpression(context); - failIfFalse(expression, "Cannot parse expression in template literal"); - - templateExpressionTail = context.createTemplateExpressionList(templateExpressionTail, expression); - - auto templateString = parseTemplateString(context, false, rawStringsBuildMode, elementIsTail); - failIfFalse(templateString, "Cannot parse template element"); - templateStringTail = context.createTemplateStringList(templateStringTail, templateString); - } - - return context.createTemplateLiteral(location, templateStringList, templateExpressionList); -} -#endif - template <typename LexerType> template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpression(TreeBuilder& context) { failIfStackOverflow(); switch (m_token.m_type) { - case FUNCTION: { - JSTokenLocation location(tokenLocation()); - unsigned functionKeywordStart = tokenStart(); - next(); - ParserFunctionInfo<TreeBuilder> info; - info.name = &m_vm->propertyNames->nullIdentifier; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::NormalFunctionMode, false, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, StandardFunctionParseType)), "Cannot parse function expression"); - return context.createFunctionExpr(location, info); - } -#if ENABLE(ES6_CLASS_SYNTAX) - case CLASSTOKEN: { - ParserClassInfo<TreeBuilder> info; - return parseClass(context, FunctionNoRequirements, info); - } -#endif case OPENBRACE: if (strictMode()) return parseStrictObjectLiteral(context); @@ -3212,10 +1950,9 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre case THISTOKEN: { JSTokenLocation location(tokenLocation()); next(); - return context.createThisExpr(location, m_thisTDZMode); + return context.thisExpr(location); } case IDENT: { - identifierExpression: JSTextPosition start = tokenStartPosition(); const Identifier* ident = m_token.m_data.ident; JSTokenLocation location(tokenLocation()); @@ -3230,17 +1967,11 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre next(); return context.createString(location, ident); } - case DOUBLE: { - double d = m_token.m_data.doubleValue; - JSTokenLocation location(tokenLocation()); - next(); - return context.createDoubleExpr(location, d); - } - case INTEGER: { + case NUMBER: { double d = m_token.m_data.doubleValue; JSTokenLocation location(tokenLocation()); next(); - return context.createIntegerExpr(location, d); + return context.createNumberExpr(location, d); } case NULLTOKEN: { JSTokenLocation location(tokenLocation()); @@ -3277,14 +2008,6 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parsePrimaryExpre } return re; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - case TEMPLATE: - return parseTemplateLiteral(context, LexerType::RawStringsBuildMode::DontBuildRawStrings); -#endif - case LET: - if (!strictMode()) - goto identifierExpression; - FALLTHROUGH; default: failDueToUnexpectedToken(); } @@ -3340,45 +2063,27 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres TreeExpression base = 0; JSTextPosition expressionStart = tokenStartPosition(); int newCount = 0; - JSTokenLocation startLocation = tokenLocation(); JSTokenLocation location; while (match(NEW)) { next(); newCount++; } - -#if ENABLE(ES6_CLASS_SYNTAX) - bool baseIsSuper = match(SUPER); - semanticFailIfTrue(baseIsSuper && newCount, "Cannot use new with super"); -#else - bool baseIsSuper = false; -#endif - - bool baseIsNewTarget = false; - if (newCount && match(DOT)) { - next(); - if (match(IDENT)) { - const Identifier* ident = m_token.m_data.ident; - if (m_vm->propertyNames->target == *ident) { - semanticFailIfFalse(currentScope()->isFunction(), "new.target is only valid inside functions"); - baseIsNewTarget = true; - base = context.createNewTargetExpr(location); - newCount--; - next(); - } else - failWithMessage("\"new.\" can only followed with target"); - } else - failDueToUnexpectedToken(); - } - - if (baseIsSuper) { - semanticFailIfFalse(currentScope()->isFunction(), "super is only valid inside functions"); - base = context.createSuperExpr(location); + + if (match(FUNCTION)) { + const Identifier* name = &m_vm->propertyNames->nullIdentifier; + TreeFormalParameterList parameters = 0; + TreeFunctionBody body = 0; + unsigned openBraceOffset = 0; + unsigned closeBraceOffset = 0; + int bodyStartLine = 0; + unsigned bodyStartColumn = 0; + location = tokenLocation(); next(); - currentScope()->setNeedsSuperBinding(); - } else if (!baseIsNewTarget) + failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse function expression"); + base = context.createFunctionExpr(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn); + } else base = parsePrimaryExpression(context); - + failIfFalse(base, "Cannot parse base expression"); while (true) { location = tokenLocation(); @@ -3402,16 +2107,14 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres if (newCount) { newCount--; JSTextPosition expressionEnd = lastTokenEndPosition(); - TreeArguments arguments = parseArguments(context, AllowSpread); + TreeArguments arguments = parseArguments(context, DontAllowSpread); failIfFalse(arguments, "Cannot parse call arguments"); base = context.createNewExpr(location, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); } else { JSTextPosition expressionEnd = lastTokenEndPosition(); TreeArguments arguments = parseArguments(context, AllowSpread); failIfFalse(arguments, "Cannot parse call arguments"); - if (baseIsSuper) - currentScope()->setHasDirectSuper(); - base = context.makeFunctionCallNode(startLocation, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); + base = context.makeFunctionCallNode(location, base, arguments, expressionStart, expressionEnd, lastTokenEndPosition()); } m_nonLHSCount = nonLHSCount; break; @@ -3425,44 +2128,16 @@ template <class TreeBuilder> TreeExpression Parser<LexerType>::parseMemberExpres next(); break; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - case TEMPLATE: { - semanticFailIfTrue(baseIsSuper, "Cannot use super as tag for tagged templates"); - JSTextPosition expressionEnd = lastTokenEndPosition(); - int nonLHSCount = m_nonLHSCount; - typename TreeBuilder::TemplateLiteral templateLiteral = parseTemplateLiteral(context, LexerType::RawStringsBuildMode::BuildRawStrings); - failIfFalse(templateLiteral, "Cannot parse template literal"); - base = context.createTaggedTemplate(location, base, templateLiteral, expressionStart, expressionEnd, lastTokenEndPosition()); - m_nonLHSCount = nonLHSCount; - break; - } -#endif default: goto endMemberExpression; } - baseIsSuper = false; } endMemberExpression: - semanticFailIfTrue(baseIsSuper, "Cannot reference super"); while (newCount--) base = context.createNewExpr(location, base, expressionStart, lastTokenEndPosition()); return base; } -template <typename LexerType> -template <class TreeBuilder> TreeExpression Parser<LexerType>::parseArrowFunctionExpression(TreeBuilder& context) -{ - JSTokenLocation location; - - unsigned functionKeywordStart = tokenStart(); - location = tokenLocation(); - ParserFunctionInfo<TreeBuilder> info; - info.name = &m_vm->propertyNames->nullIdentifier; - failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SourceParseMode::ArrowFunctionMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, info, ArrowFunctionParseType)), "Cannot parse arrow function expression"); - - return context.createArrowFunctionExpr(location, info); -} - static const char* operatorString(bool prefix, unsigned tok) { switch (tok) { @@ -3660,8 +2335,7 @@ template <typename LexerType> void Parser<LexerType>::printUnexpectedTokenText(W case STRING: out.print("Unexpected string literal ", getToken()); return; - case INTEGER: - case DOUBLE: + case NUMBER: out.print("Unexpected number '", getToken(), "'"); return; @@ -3672,10 +2346,6 @@ template <typename LexerType> void Parser<LexerType>::printUnexpectedTokenText(W case RESERVED: out.print("Unexpected use of reserved word '", getToken(), "'"); return; - - case INVALID_PRIVATE_NAME_ERRORTOK: - out.print("Invalid private name '", getToken(), "'"); - return; case IDENT: out.print("Unexpected identifier '", getToken(), "'"); diff --git a/Source/JavaScriptCore/parser/Parser.h b/Source/JavaScriptCore/parser/Parser.h index c2e6a6504..b698ef936 100644 --- a/Source/JavaScriptCore/parser/Parser.h +++ b/Source/JavaScriptCore/parser/Parser.h @@ -31,14 +31,13 @@ #include "Nodes.h" #include "ParserArena.h" #include "ParserError.h" -#include "ParserFunctionInfo.h" #include "ParserTokens.h" #include "SourceProvider.h" #include "SourceProviderCache.h" #include "SourceProviderCacheItem.h" -#include "VariableEnvironment.h" #include <wtf/Forward.h> #include <wtf/Noncopyable.h> +#include <wtf/OwnPtr.h> #include <wtf/RefPtr.h> namespace JSC { struct Scope; @@ -53,7 +52,7 @@ template <> struct VectorTraits<JSC::Scope> : SimpleClassVectorTraits { namespace JSC { class ExecState; -class FunctionMetadataNode; +class FunctionBodyNode; class FunctionParameters; class Identifier; class VM; @@ -67,95 +66,54 @@ class SourceCode; #define TreeSourceElements typename TreeBuilder::SourceElements #define TreeClause typename TreeBuilder::Clause #define TreeClauseList typename TreeBuilder::ClauseList +#define TreeConstDeclList typename TreeBuilder::ConstDeclList #define TreeArguments typename TreeBuilder::Arguments #define TreeArgumentsList typename TreeBuilder::ArgumentsList #define TreeFunctionBody typename TreeBuilder::FunctionBody -#if ENABLE(ES6_CLASS_SYNTAX) -#define TreeClassExpression typename TreeBuilder::ClassExpression -#endif #define TreeProperty typename TreeBuilder::Property #define TreePropertyList typename TreeBuilder::PropertyList -#define TreeDestructuringPattern typename TreeBuilder::DestructuringPattern +#define TreeDeconstructionPattern typename TreeBuilder::DeconstructionPattern COMPILE_ASSERT(LastUntaggedToken < 64, LessThan64UntaggedTokens); enum SourceElementsMode { CheckForStrictMode, DontCheckForStrictMode }; -enum FunctionParseType { StandardFunctionParseType, ArrowFunctionParseType }; -enum FunctionBodyType { ArrowFunctionBodyExpression, ArrowFunctionBodyBlock, StandardFunctionBodyBlock }; enum FunctionRequirements { FunctionNoRequirements, FunctionNeedsName }; - -enum DestructuringKind { - DestructureToVariables, - DestructureToLet, - DestructureToConst, - DestructureToParameters, - DestructureToExpressions -}; - -enum class DeclarationType { - VarDeclaration, - LetDeclaration, - ConstDeclaration -}; - -enum class DeclarationImportType { - Imported, - NotImported +enum FunctionParseMode { FunctionMode, GetterMode, SetterMode }; +enum DeconstructionKind { + DeconstructToVariables, + DeconstructToParameters, + DeconstructToExpressions }; -enum DeclarationResult { - Valid = 0, - InvalidStrictMode = 1 << 0, - InvalidDuplicateDeclaration = 1 << 1 -}; - -typedef uint8_t DeclarationResultMask; - - template <typename T> inline bool isEvalNode() { return false; } template <> inline bool isEvalNode<EvalNode>() { return true; } -struct ScopeLabelInfo { - UniquedStringImpl* uid; - bool isLoop; -}; - -ALWAYS_INLINE static bool isArguments(const VM* vm, const Identifier* ident) -{ - return vm->propertyNames->arguments == *ident; -} -ALWAYS_INLINE static bool isEval(const VM* vm, const Identifier* ident) -{ - return vm->propertyNames->eval == *ident; -} -ALWAYS_INLINE static bool isEvalOrArgumentsIdentifier(const VM* vm, const Identifier* ident) -{ - return isEval(vm, ident) || isArguments(vm, ident); -} -ALWAYS_INLINE static bool isIdentifierOrKeyword(const JSToken& token) -{ - return token.m_type == IDENT || token.m_type & KeywordTokenFlag; -} - -class ModuleScopeData : public RefCounted<ModuleScopeData> { -public: - static Ref<ModuleScopeData> create() { return adoptRef(*new ModuleScopeData); } - - const IdentifierSet& exportedBindings() const { return m_exportedBindings; } - - bool exportName(const Identifier& exportedName) +struct DepthManager { + DepthManager(int* depth) + : m_originalDepth(*depth) + , m_depth(depth) { - return m_exportedNames.add(exportedName.impl()).isNewEntry; } - void exportBinding(const Identifier& localName) + ~DepthManager() { - m_exportedBindings.add(localName.impl()); + *m_depth = m_originalDepth; } private: - IdentifierSet m_exportedNames { }; - IdentifierSet m_exportedBindings { }; + int m_originalDepth; + int* m_depth; +}; + +struct ScopeLabelInfo { + ScopeLabelInfo(StringImpl* ident, bool isLoop) + : m_ident(ident) + , m_isLoop(isLoop) + { + } + + StringImpl* m_ident; + bool m_isLoop; }; struct Scope { @@ -164,13 +122,9 @@ struct Scope { , m_shadowsArguments(false) , m_usesEval(false) , m_needsFullActivation(false) - , m_hasDirectSuper(false) - , m_needsSuperBinding(false) - , m_allowsVarDeclarations(true) - , m_allowsLexicalDeclarations(true) + , m_allowsNewDecls(true) , m_strictMode(strictMode) , m_isFunction(isFunction) - , m_isLexicalScope(false) , m_isFunctionBoundary(false) , m_isValidStrictMode(true) , m_loopDepth(0) @@ -183,26 +137,21 @@ struct Scope { , m_shadowsArguments(rhs.m_shadowsArguments) , m_usesEval(rhs.m_usesEval) , m_needsFullActivation(rhs.m_needsFullActivation) - , m_hasDirectSuper(rhs.m_hasDirectSuper) - , m_needsSuperBinding(rhs.m_needsSuperBinding) - , m_allowsVarDeclarations(rhs.m_allowsVarDeclarations) - , m_allowsLexicalDeclarations(rhs.m_allowsLexicalDeclarations) + , m_allowsNewDecls(rhs.m_allowsNewDecls) , m_strictMode(rhs.m_strictMode) , m_isFunction(rhs.m_isFunction) - , m_isLexicalScope(rhs.m_isLexicalScope) , m_isFunctionBoundary(rhs.m_isFunctionBoundary) , m_isValidStrictMode(rhs.m_isValidStrictMode) , m_loopDepth(rhs.m_loopDepth) , m_switchDepth(rhs.m_switchDepth) - , m_moduleScopeData(rhs.m_moduleScopeData) { if (rhs.m_labels) { - m_labels = std::make_unique<LabelStack>(); + m_labels = adoptPtr(new LabelStack); typedef LabelStack::const_iterator iterator; iterator end = rhs.m_labels->end(); for (iterator it = rhs.m_labels->begin(); it != end; ++it) - m_labels->append(ScopeLabelInfo { it->uid, it->isLoop }); + m_labels->append(ScopeLabelInfo(it->m_ident, it->m_isLoop)); } } @@ -217,8 +166,8 @@ struct Scope { void pushLabel(const Identifier* label, bool isLoop) { if (!m_labels) - m_labels = std::make_unique<LabelStack>(); - m_labels->append(ScopeLabelInfo { label->impl(), isLoop }); + m_labels = adoptPtr(new LabelStack); + m_labels->append(ScopeLabelInfo(label->impl(), isLoop)); } void popLabel() @@ -233,7 +182,7 @@ struct Scope { if (!m_labels) return 0; for (int i = m_labels->size(); i > 0; i--) { - if (m_labels->at(i - 1).uid == label->impl()) + if (m_labels->at(i - 1).m_ident == label->impl()) return &m_labels->at(i - 1); } return 0; @@ -243,130 +192,31 @@ struct Scope { { m_isFunction = true; m_isFunctionBoundary = true; - setIsLexicalScope(); - } - - void setIsModule() - { - m_moduleScopeData = ModuleScopeData::create(); - } - - bool isFunction() const { return m_isFunction; } - bool isFunctionBoundary() const { return m_isFunctionBoundary; } - - void setIsLexicalScope() - { - m_isLexicalScope = true; - m_allowsLexicalDeclarations = true; - } - bool isLexicalScope() { return m_isLexicalScope; } - - VariableEnvironment& declaredVariables() { return m_declaredVariables; } - VariableEnvironment& lexicalVariables() { return m_lexicalVariables; } - VariableEnvironment& finalizeLexicalEnvironment() - { - if (m_usesEval || m_needsFullActivation) - m_lexicalVariables.markAllVariablesAsCaptured(); - else - computeLexicallyCapturedVariablesAndPurgeCandidates(); - - return m_lexicalVariables; - } - - ModuleScopeData& moduleScopeData() const - { - ASSERT(m_moduleScopeData); - return *m_moduleScopeData; - } - - void computeLexicallyCapturedVariablesAndPurgeCandidates() - { - // Because variables may be defined at any time in the range of a lexical scope, we must - // track lexical variables that might be captured. Then, when we're preparing to pop the top - // lexical scope off the stack, we should find which variables are truly captured, and which - // variable still may be captured in a parent scope. - if (m_lexicalVariables.size() && m_closedVariableCandidates.size()) { - auto end = m_closedVariableCandidates.end(); - for (auto iter = m_closedVariableCandidates.begin(); iter != end; ++iter) - m_lexicalVariables.markVariableAsCapturedIfDefined(iter->get()); - } - - // We can now purge values from the captured candidates because they're captured in this scope. - { - for (auto entry : m_lexicalVariables) { - if (entry.value.isCaptured()) - m_closedVariableCandidates.remove(entry.key); - } - } } + bool isFunction() { return m_isFunction; } + bool isFunctionBoundary() { return m_isFunctionBoundary; } void declareCallee(const Identifier* ident) { - auto addResult = m_declaredVariables.add(ident->impl()); - // We want to track if callee is captured, but we don't want to act like it's a 'var' - // because that would cause the BytecodeGenerator to emit bad code. - addResult.iterator->value.clearIsVar(); + m_declaredVariables.add(ident->string().impl()); } - DeclarationResultMask declareVariable(const Identifier* ident) + bool declareVariable(const Identifier* ident) { - ASSERT(m_allowsVarDeclarations); - DeclarationResultMask result = DeclarationResult::Valid; - bool isValidStrictMode = !isEvalOrArgumentsIdentifier(m_vm, ident); + bool isValidStrictMode = m_vm->propertyNames->eval != *ident && m_vm->propertyNames->arguments != *ident; m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode; - auto addResult = m_declaredVariables.add(ident->impl()); - addResult.iterator->value.setIsVar(); - if (!isValidStrictMode) - result |= DeclarationResult::InvalidStrictMode; - return result; - } - - DeclarationResultMask declareLexicalVariable(const Identifier* ident, bool isConstant, DeclarationImportType importType = DeclarationImportType::NotImported) - { - ASSERT(m_allowsLexicalDeclarations); - DeclarationResultMask result = DeclarationResult::Valid; - bool isValidStrictMode = !isEvalOrArgumentsIdentifier(m_vm, ident); - m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode; - auto addResult = m_lexicalVariables.add(ident->impl()); - if (isConstant) - addResult.iterator->value.setIsConst(); - else - addResult.iterator->value.setIsLet(); - - if (importType == DeclarationImportType::Imported) - addResult.iterator->value.setIsImported(); - - if (!addResult.isNewEntry) - result |= DeclarationResult::InvalidDuplicateDeclaration; - if (!isValidStrictMode) - result |= DeclarationResult::InvalidStrictMode; - - return result; + m_declaredVariables.add(ident->string().impl()); + return isValidStrictMode; } bool hasDeclaredVariable(const Identifier& ident) { - return hasDeclaredVariable(ident.impl()); - } - - bool hasDeclaredVariable(const RefPtr<UniquedStringImpl>& ident) - { - return m_declaredVariables.contains(ident.get()); - } - - bool hasLexicallyDeclaredVariable(const RefPtr<UniquedStringImpl>& ident) const - { - return m_lexicalVariables.contains(ident.get()); + return m_declaredVariables.contains(ident.impl()); } - ALWAYS_INLINE bool hasDeclaredParameter(const Identifier& ident) + bool hasDeclaredParameter(const Identifier& ident) { - return hasDeclaredParameter(ident.impl()); - } - - bool hasDeclaredParameter(const RefPtr<UniquedStringImpl>& ident) - { - return m_declaredParameters.contains(ident) || m_declaredVariables.contains(ident.get()); + return m_declaredParameters.contains(ident.impl()) || m_declaredVariables.contains(ident.impl()); } void declareWrite(const Identifier* ident) @@ -375,33 +225,19 @@ struct Scope { m_writtenVariables.add(ident->impl()); } - void preventAllVariableDeclarations() - { - m_allowsVarDeclarations = false; - m_allowsLexicalDeclarations = false; - } - void preventVarDeclarations() { m_allowsVarDeclarations = false; } - bool allowsVarDeclarations() const { return m_allowsVarDeclarations; } - bool allowsLexicalDeclarations() const { return m_allowsLexicalDeclarations; } + void preventNewDecls() { m_allowsNewDecls = false; } + bool allowsNewDecls() const { return m_allowsNewDecls; } - DeclarationResultMask declareParameter(const Identifier* ident) + bool declareParameter(const Identifier* ident) { - ASSERT(m_allowsVarDeclarations); - DeclarationResultMask result = DeclarationResult::Valid; - bool isArgumentsIdent = isArguments(m_vm, ident); - auto addResult = m_declaredVariables.add(ident->impl()); - addResult.iterator->value.clearIsVar(); - bool isValidStrictMode = addResult.isNewEntry && m_vm->propertyNames->eval != *ident && !isArgumentsIdent; + bool isArguments = m_vm->propertyNames->arguments == *ident; + bool isValidStrictMode = m_declaredVariables.add(ident->string().impl()).isNewEntry && m_vm->propertyNames->eval != *ident && !isArguments; m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode; - m_declaredParameters.add(ident->impl()); - if (!isValidStrictMode) - result |= DeclarationResult::InvalidStrictMode; - if (isArgumentsIdent) - m_shadowsArguments = true; - if (!addResult.isNewEntry) - result |= DeclarationResult::InvalidDuplicateDeclaration; + m_declaredParameters.add(ident->string().impl()); - return result; + if (isArguments) + m_shadowsArguments = true; + return isValidStrictMode; } enum BindingResult { @@ -411,104 +247,67 @@ struct Scope { }; BindingResult declareBoundParameter(const Identifier* ident) { - bool isArgumentsIdent = isArguments(m_vm, ident); - auto addResult = m_declaredVariables.add(ident->impl()); - addResult.iterator->value.setIsVar(); // Treat destructuring parameters as "var"s. - bool isValidStrictMode = addResult.isNewEntry && !isEval(m_vm, ident) && !isArgumentsIdent; + bool isArguments = m_vm->propertyNames->arguments == *ident; + bool newEntry = m_declaredVariables.add(ident->string().impl()).isNewEntry; + bool isValidStrictMode = newEntry && m_vm->propertyNames->eval != *ident && !isArguments; m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode; - if (isArgumentsIdent) + if (isArguments) m_shadowsArguments = true; - if (!addResult.isNewEntry) + if (!newEntry) return BindingFailed; return isValidStrictMode ? BindingSucceeded : StrictBindingFailed; } - void getUsedVariables(IdentifierSet& usedVariables) - { - usedVariables.swap(m_usedVariables); - } void useVariable(const Identifier* ident, bool isEval) { m_usesEval |= isEval; - m_usedVariables.add(ident->impl()); + m_usedVariables.add(ident->string().impl()); } void setNeedsFullActivation() { m_needsFullActivation = true; } - bool needsFullActivation() const { return m_needsFullActivation; } - -#if ENABLE(ES6_CLASS_SYNTAX) - bool hasDirectSuper() { return m_hasDirectSuper; } -#else - bool hasDirectSuper() { return false; } -#endif - void setHasDirectSuper() { m_hasDirectSuper = true; } - -#if ENABLE(ES6_CLASS_SYNTAX) - bool needsSuperBinding() { return m_needsSuperBinding; } -#else - bool needsSuperBinding() { return false; } -#endif - void setNeedsSuperBinding() { m_needsSuperBinding = true; } - void collectFreeVariables(Scope* nestedScope, bool shouldTrackClosedVariables) + bool collectFreeVariables(Scope* nestedScope, bool shouldTrackClosedVariables) { if (nestedScope->m_usesEval) m_usesEval = true; - - { - IdentifierSet::iterator end = nestedScope->m_usedVariables.end(); - for (IdentifierSet::iterator ptr = nestedScope->m_usedVariables.begin(); ptr != end; ++ptr) { - if (nestedScope->m_declaredVariables.contains(*ptr) || nestedScope->m_lexicalVariables.contains(*ptr)) - continue; - m_usedVariables.add(*ptr); - // We don't want a declared variable that is used in an inner scope to be thought of as captured if - // that inner scope is both a lexical scope and not a function. Only inner functions and "catch" - // statements can cause variables to be captured. - if (shouldTrackClosedVariables && (nestedScope->m_isFunctionBoundary || !nestedScope->m_isLexicalScope)) - m_closedVariableCandidates.add(*ptr); - } - } - // Propagate closed variable candidates downwards within the same function. - // Cross function captures will be realized via m_usedVariables propagation. - if (shouldTrackClosedVariables && !nestedScope->m_isFunctionBoundary && nestedScope->m_closedVariableCandidates.size()) { - IdentifierSet::iterator end = nestedScope->m_closedVariableCandidates.end(); - IdentifierSet::iterator begin = nestedScope->m_closedVariableCandidates.begin(); - m_closedVariableCandidates.add(begin, end); + IdentifierSet::iterator end = nestedScope->m_usedVariables.end(); + for (IdentifierSet::iterator ptr = nestedScope->m_usedVariables.begin(); ptr != end; ++ptr) { + if (nestedScope->m_declaredVariables.contains(*ptr)) + continue; + m_usedVariables.add(*ptr); + if (shouldTrackClosedVariables) + m_closedVariables.add(*ptr); } - if (nestedScope->m_writtenVariables.size()) { IdentifierSet::iterator end = nestedScope->m_writtenVariables.end(); for (IdentifierSet::iterator ptr = nestedScope->m_writtenVariables.begin(); ptr != end; ++ptr) { - if (nestedScope->m_declaredVariables.contains(*ptr) || nestedScope->m_lexicalVariables.contains(*ptr)) + if (nestedScope->m_declaredVariables.contains(*ptr)) continue; m_writtenVariables.add(*ptr); } } + + return true; } - - void getCapturedVars(IdentifierSet& capturedVariables, bool& modifiedParameter, bool& modifiedArguments) + + void getCapturedVariables(IdentifierSet& capturedVariables, bool& modifiedParameter) { if (m_needsFullActivation || m_usesEval) { modifiedParameter = true; - for (auto& entry : m_declaredVariables) - capturedVariables.add(entry.key); + capturedVariables.swap(m_declaredVariables); return; } - for (IdentifierSet::iterator ptr = m_closedVariableCandidates.begin(); ptr != m_closedVariableCandidates.end(); ++ptr) { + for (IdentifierSet::iterator ptr = m_closedVariables.begin(); ptr != m_closedVariables.end(); ++ptr) { if (!m_declaredVariables.contains(*ptr)) continue; capturedVariables.add(*ptr); } modifiedParameter = false; - if (shadowsArguments()) - modifiedArguments = true; if (m_declaredParameters.size()) { IdentifierSet::iterator end = m_writtenVariables.end(); for (IdentifierSet::iterator ptr = m_writtenVariables.begin(); ptr != end; ++ptr) { - if (*ptr == m_vm->propertyNames->arguments.impl()) - modifiedArguments = true; if (!m_declaredParameters.contains(*ptr)) continue; modifiedParameter = true; @@ -521,11 +320,11 @@ struct Scope { bool isValidStrictMode() const { return m_isValidStrictMode; } bool shadowsArguments() const { return m_shadowsArguments; } - void copyCapturedVariablesToVector(const IdentifierSet& capturedVariables, Vector<RefPtr<UniquedStringImpl>>& vector) + void copyCapturedVariablesToVector(const IdentifierSet& capturedVariables, Vector<RefPtr<StringImpl>>& vector) { IdentifierSet::iterator end = capturedVariables.end(); for (IdentifierSet::iterator it = capturedVariables.begin(); it != end; ++it) { - if (m_declaredVariables.contains(*it) || m_lexicalVariables.contains(*it)) + if (m_declaredVariables.contains(*it)) continue; vector.append(*it); } @@ -558,27 +357,21 @@ private: bool m_shadowsArguments : 1; bool m_usesEval : 1; bool m_needsFullActivation : 1; - bool m_hasDirectSuper : 1; - bool m_needsSuperBinding : 1; - bool m_allowsVarDeclarations : 1; - bool m_allowsLexicalDeclarations : 1; + bool m_allowsNewDecls : 1; bool m_strictMode : 1; bool m_isFunction : 1; - bool m_isLexicalScope : 1; bool m_isFunctionBoundary : 1; bool m_isValidStrictMode : 1; int m_loopDepth; int m_switchDepth; typedef Vector<ScopeLabelInfo, 2> LabelStack; - std::unique_ptr<LabelStack> m_labels; + OwnPtr<LabelStack> m_labels; IdentifierSet m_declaredParameters; - VariableEnvironment m_declaredVariables; - VariableEnvironment m_lexicalVariables; + IdentifierSet m_declaredVariables; IdentifierSet m_usedVariables; - IdentifierSet m_closedVariableCandidates; + IdentifierSet m_closedVariables; IdentifierSet m_writtenVariables; - RefPtr<ModuleScopeData> m_moduleScopeData { }; }; typedef Vector<Scope, 10> ScopeStack; @@ -614,17 +407,13 @@ class Parser { WTF_MAKE_FAST_ALLOCATED; public: - Parser( - VM*, const SourceCode&, JSParserBuiltinMode, JSParserStrictMode, SourceParseMode, - ConstructorKind defaultConstructorKind = ConstructorKind::None, ThisTDZMode = ThisTDZMode::CheckIfNeeded); + Parser(VM*, const SourceCode&, FunctionParameters*, const Identifier&, JSParserStrictness, JSParserMode); ~Parser(); template <class ParsedNode> - std::unique_ptr<ParsedNode> parse(ParserError&, const Identifier&, SourceParseMode); + PassRefPtr<ParsedNode> parse(ParserError&); JSTextPosition positionBeforeLastNewline() const { return m_lexer->positionBeforeLastNewline(); } - JSTokenLocation locationBeforeLastToken() const { return m_lexer->lastTokenLocation(); } - Vector<RefPtr<UniquedStringImpl>>&& closedVariables() { return WTF::move(m_closedVariables); } private: struct AllowInOverride { @@ -664,74 +453,6 @@ private: Parser* m_parser; }; - struct AutoCleanupLexicalScope { - // We can allocate this object on the stack without actually knowing beforehand if we're - // going to create a new lexical scope. If we decide to create a new lexical scope, we - // can pass the scope into this obejct and it will take care of the cleanup for us if the parse fails. - // This is helpful if we may fail from syntax errors after creating a lexical scope conditionally. - AutoCleanupLexicalScope() - : m_scope(nullptr, UINT_MAX) - , m_parser(nullptr) - { - } - - ~AutoCleanupLexicalScope() - { - // This should only ever be called if we fail from a syntax error. Otherwise - // it's the intention that a user of this class pops this scope manually on a - // successful parse. - if (isValid()) - m_parser->popScope(*this, false); - } - - void setIsValid(ScopeRef& scope, Parser* parser) - { - RELEASE_ASSERT(scope->isLexicalScope()); - m_scope = scope; - m_parser = parser; - } - - bool isValid() const { return !!m_parser; } - - void setPopped() - { - m_parser = nullptr; - } - - ScopeRef& scope() { return m_scope; } - - private: - ScopeRef m_scope; - Parser* m_parser; - }; - - ALWAYS_INLINE DestructuringKind destructuringKindFromDeclarationType(DeclarationType type) - { - switch (type) { - case DeclarationType::VarDeclaration: - return DestructureToVariables; - case DeclarationType::LetDeclaration: - return DestructureToLet; - case DeclarationType::ConstDeclaration: - return DestructureToConst; - } - - RELEASE_ASSERT_NOT_REACHED(); - return DestructureToVariables; - } - - ALWAYS_INLINE AssignmentContext assignmentContextFromDeclarationType(DeclarationType type) - { - switch (type) { - case DeclarationType::ConstDeclaration: - return AssignmentContext::ConstDeclarationStatement; - default: - return AssignmentContext::DeclarationStatement; - } - } - - ALWAYS_INLINE bool isEvalOrArguments(const Identifier* ident) { return isEvalOrArgumentsIdentifier(m_vm, ident); } - ScopeRef currentScope() { return ScopeRef(&m_scopeStack, m_scopeStack.size() - 1); @@ -749,79 +470,53 @@ private: return currentScope(); } - void popScopeInternal(ScopeRef& scope, bool shouldTrackClosedVariables) + bool popScopeInternal(ScopeRef& scope, bool shouldTrackClosedVariables) { ASSERT_UNUSED(scope, scope.index() == m_scopeStack.size() - 1); ASSERT(m_scopeStack.size() > 1); - m_scopeStack[m_scopeStack.size() - 2].collectFreeVariables(&m_scopeStack.last(), shouldTrackClosedVariables); - if (!m_scopeStack.last().isFunctionBoundary() && m_scopeStack.last().needsFullActivation()) - m_scopeStack[m_scopeStack.size() - 2].setNeedsFullActivation(); + bool result = m_scopeStack[m_scopeStack.size() - 2].collectFreeVariables(&m_scopeStack.last(), shouldTrackClosedVariables); m_scopeStack.removeLast(); + return result; } - ALWAYS_INLINE void popScope(ScopeRef& scope, bool shouldTrackClosedVariables) + bool popScope(ScopeRef& scope, bool shouldTrackClosedVariables) { - popScopeInternal(scope, shouldTrackClosedVariables); + return popScopeInternal(scope, shouldTrackClosedVariables); } - ALWAYS_INLINE void popScope(AutoPopScopeRef& scope, bool shouldTrackClosedVariables) + bool popScope(AutoPopScopeRef& scope, bool shouldTrackClosedVariables) { scope.setPopped(); - popScopeInternal(scope, shouldTrackClosedVariables); - } - - ALWAYS_INLINE void popScope(AutoCleanupLexicalScope& cleanupScope, bool shouldTrackClosedVariables) - { - RELEASE_ASSERT(cleanupScope.isValid()); - ScopeRef& scope = cleanupScope.scope(); - cleanupScope.setPopped(); - popScopeInternal(scope, shouldTrackClosedVariables); + return popScopeInternal(scope, shouldTrackClosedVariables); } - DeclarationResultMask declareVariable(const Identifier* ident, DeclarationType type = DeclarationType::VarDeclaration, DeclarationImportType importType = DeclarationImportType::NotImported) + bool declareVariable(const Identifier* ident) { unsigned i = m_scopeStack.size() - 1; ASSERT(i < m_scopeStack.size()); - - if (type == DeclarationType::VarDeclaration) { - while (!m_scopeStack[i].allowsVarDeclarations()) { - i--; - ASSERT(i < m_scopeStack.size()); - } - - return m_scopeStack[i].declareVariable(ident); - } - - ASSERT(type == DeclarationType::LetDeclaration || type == DeclarationType::ConstDeclaration); - - // Lexical variables declared at a top level scope that shadow arguments or vars are not allowed. - if (m_statementDepth == 1 && (hasDeclaredParameter(*ident) || hasDeclaredVariable(*ident))) - return DeclarationResult::InvalidDuplicateDeclaration; - - while (!m_scopeStack[i].allowsLexicalDeclarations()) { + while (!m_scopeStack[i].allowsNewDecls()) { i--; ASSERT(i < m_scopeStack.size()); } - - return m_scopeStack[i].declareLexicalVariable(ident, type == DeclarationType::ConstDeclaration, importType); + return m_scopeStack[i].declareVariable(ident); } NEVER_INLINE bool hasDeclaredVariable(const Identifier& ident) { unsigned i = m_scopeStack.size() - 1; ASSERT(i < m_scopeStack.size()); - while (!m_scopeStack[i].allowsVarDeclarations()) { + while (!m_scopeStack[i].allowsNewDecls()) { i--; ASSERT(i < m_scopeStack.size()); } return m_scopeStack[i].hasDeclaredVariable(ident); } - + NEVER_INLINE bool hasDeclaredParameter(const Identifier& ident) { unsigned i = m_scopeStack.size() - 1; ASSERT(i < m_scopeStack.size()); - while (!m_scopeStack[i].allowsVarDeclarations()) { + while (!m_scopeStack[i].allowsNewDecls()) { i--; ASSERT(i < m_scopeStack.size()); } @@ -833,13 +528,7 @@ private: if (!m_syntaxAlreadyValidated || strictMode()) m_scopeStack.last().declareWrite(ident); } - - bool exportName(const Identifier& ident) - { - ASSERT(currentScope().index() == 0); - return currentScope()->moduleScopeData().exportName(ident); - } - + ScopeStack m_scopeStack; const SourceProviderCacheItem* findCachedFunctionInfo(int openBracePos) @@ -848,13 +537,14 @@ private: } Parser(); - String parseInner(const Identifier&, SourceParseMode); + String parseInner(); - void didFinishParsing(SourceElements*, DeclarationStacks::FunctionStack&, VariableEnvironment&, CodeFeatures, int, const Vector<RefPtr<UniquedStringImpl>>&&); + void didFinishParsing(SourceElements*, ParserArenaData<DeclarationStacks::VarStack>*, + ParserArenaData<DeclarationStacks::FunctionStack>*, CodeFeatures, int, IdentifierSet&); // Used to determine type of error to report. - bool isFunctionMetadataNode(ScopeNode*) { return false; } - bool isFunctionMetadataNode(FunctionMetadataNode*) { return true; } + bool isFunctionBodyNode(ScopeNode*) { return false; } + bool isFunctionBodyNode(FunctionBodyNode*) { return true; } ALWAYS_INLINE void next(unsigned lexerFlags = 0) { @@ -900,57 +590,9 @@ private: return m_token.m_type == expected; } - ALWAYS_INLINE bool matchContextualKeyword(const Identifier& identifier) - { - return m_token.m_type == IDENT && *m_token.m_data.ident == identifier; - } - - ALWAYS_INLINE bool matchIdentifierOrKeyword() + ALWAYS_INLINE bool isofToken() { - return isIdentifierOrKeyword(m_token); - } - - ALWAYS_INLINE bool isEndOfArrowFunction() - { - return match(SEMICOLON) || match(COMMA) || match(CLOSEPAREN) || match(CLOSEBRACE) || match(CLOSEBRACKET) || match(EOFTOK) || m_lexer->prevTerminator(); - } - - ALWAYS_INLINE bool isArrowFunctionParamters() - { -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - bool isArrowFunction = false; - - if (match(EOFTOK)) - return isArrowFunction; - - SavePoint saveArrowFunctionPoint = createSavePoint(); - - if (consume(OPENPAREN)) { - bool isArrowFunctionParamters = true; - - while (consume(IDENT)) { - if (consume(COMMA)) { - if (!match(IDENT)) { - isArrowFunctionParamters = false; - break; - } - } else - break; - } - - if (isArrowFunctionParamters) { - if (consume(CLOSEPAREN) && match(ARROWFUNCTION)) - isArrowFunction = true; - } - } else if (consume(IDENT) && match(ARROWFUNCTION)) - isArrowFunction = true; - - restoreSavePoint(saveArrowFunctionPoint); - - return isArrowFunction; -#else - return false; -#endif + return m_token.m_type == IDENT && *m_token.m_data.ident == m_vm->propertyNames->of; } ALWAYS_INLINE unsigned tokenStart() @@ -988,9 +630,9 @@ private: return m_token.m_location; } - void setErrorMessage(const String& message) + void setErrorMessage(String msg) { - m_errorMessage = message; + m_errorMessage = msg; } NEVER_INLINE void logError(bool); @@ -1002,9 +644,9 @@ private: template <typename A, typename B, typename C, typename D, typename E, typename F> NEVER_INLINE void logError(bool, const A&, const B&, const C&, const D&, const E&, const F&); template <typename A, typename B, typename C, typename D, typename E, typename F, typename G> NEVER_INLINE void logError(bool, const A&, const B&, const C&, const D&, const E&, const F&, const G&); - NEVER_INLINE void updateErrorWithNameAndMessage(const char* beforeMessage, const String& name, const char* afterMessage) + NEVER_INLINE void updateErrorWithNameAndMessage(const char* beforeMsg, String name, const char* afterMsg) { - m_errorMessage = makeString(beforeMessage, " '", name, "' ", afterMessage); + m_errorMessage = makeString(beforeMsg, " '", name, "' ", afterMsg); } NEVER_INLINE void updateErrorMessage(const char* msg) @@ -1021,7 +663,7 @@ private: void setStrictMode() { currentScope()->setStrictMode(); } bool strictMode() { return currentScope()->strictMode(); } bool isValidStrictMode() { return currentScope()->isValidStrictMode(); } - DeclarationResultMask declareParameter(const Identifier* ident) { return currentScope()->declareParameter(ident); } + bool declareParameter(const Identifier* ident) { return currentScope()->declareParameter(ident); } Scope::BindingResult declareBoundParameter(const Identifier* ident) { return currentScope()->declareBoundParameter(ident); } bool breakIsValid() { @@ -1044,7 +686,7 @@ private: return true; } void pushLabel(const Identifier* label, bool isLoop) { currentScope()->pushLabel(label, isLoop); } - void popLabel(ScopeRef scope) { scope->popLabel(); } + void popLabel() { currentScope()->popLabel(); } ScopeLabelInfo* getLabel(const Identifier* label) { ScopeRef current = currentScope(); @@ -1056,21 +698,12 @@ private: } return result; } - - ALWAYS_INLINE bool isLETMaskedAsIDENT() - { - return match(LET) && !strictMode(); - } - + template <class TreeBuilder> TreeSourceElements parseSourceElements(TreeBuilder&, SourceElementsMode); - template <class TreeBuilder> TreeStatement parseStatementListItem(TreeBuilder&, const Identifier*& directive, unsigned* directiveLiteralLength); template <class TreeBuilder> TreeStatement parseStatement(TreeBuilder&, const Identifier*& directive, unsigned* directiveLiteralLength = 0); - enum class ExportType { Exported, NotExported }; -#if ENABLE(ES6_CLASS_SYNTAX) - template <class TreeBuilder> TreeStatement parseClassDeclaration(TreeBuilder&, ExportType = ExportType::NotExported); -#endif - template <class TreeBuilder> TreeStatement parseFunctionDeclaration(TreeBuilder&, ExportType = ExportType::NotExported); - template <class TreeBuilder> TreeStatement parseVariableDeclaration(TreeBuilder&, DeclarationType, ExportType = ExportType::NotExported); + template <class TreeBuilder> TreeStatement parseFunctionDeclaration(TreeBuilder&); + template <class TreeBuilder> TreeStatement parseVarDeclaration(TreeBuilder&); + template <class TreeBuilder> TreeStatement parseConstDeclaration(TreeBuilder&); template <class TreeBuilder> TreeStatement parseDoWhileStatement(TreeBuilder&); template <class TreeBuilder> TreeStatement parseWhileStatement(TreeBuilder&); template <class TreeBuilder> TreeStatement parseForStatement(TreeBuilder&); @@ -1101,41 +734,15 @@ private: enum SpreadMode { AllowSpread, DontAllowSpread }; template <class TreeBuilder> ALWAYS_INLINE TreeArguments parseArguments(TreeBuilder&, SpreadMode); template <class TreeBuilder> TreeProperty parseProperty(TreeBuilder&, bool strict); - template <class TreeBuilder> TreeExpression parsePropertyMethod(TreeBuilder& context, const Identifier* methodName); - template <class TreeBuilder> TreeProperty parseGetterSetter(TreeBuilder&, bool strict, PropertyNode::Type, unsigned getterOrSetterStartOffset, ConstructorKind = ConstructorKind::None, SuperBinding = SuperBinding::NotNeeded); - template <class TreeBuilder> ALWAYS_INLINE TreeFunctionBody parseFunctionBody(TreeBuilder&, const JSTokenLocation&, int, int functionKeywordStart, int functionNameStart, int parametersStart, ConstructorKind, FunctionBodyType, unsigned, SourceParseMode); - template <class TreeBuilder> ALWAYS_INLINE bool parseFormalParameters(TreeBuilder&, TreeFormalParameterList, unsigned&); - enum VarDeclarationListContext { ForLoopContext, VarDeclarationContext }; - template <class TreeBuilder> TreeExpression parseVariableDeclarationList(TreeBuilder&, int& declarations, TreeDestructuringPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd, VarDeclarationListContext, DeclarationType, ExportType, bool& forLoopConstDoesNotHaveInitializer); - template <class TreeBuilder> TreeSourceElements parseArrowFunctionSingleExpressionBodySourceElements(TreeBuilder&); - template <class TreeBuilder> TreeExpression parseArrowFunctionExpression(TreeBuilder&); - template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern createBindingPattern(TreeBuilder&, DestructuringKind, ExportType, const Identifier&, int depth, JSToken, AssignmentContext, const Identifier** duplicateIdentifier); - template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern parseDestructuringPattern(TreeBuilder&, DestructuringKind, ExportType, const Identifier** duplicateIdentifier = nullptr, bool* hasDestructuringPattern = nullptr, AssignmentContext = AssignmentContext::DeclarationStatement, int depth = 0); - template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern tryParseDestructuringPatternExpression(TreeBuilder&, AssignmentContext); - template <class TreeBuilder> NEVER_INLINE TreeExpression parseDefaultValueForDestructuringPattern(TreeBuilder&); - template <class TreeBuilder> TreeSourceElements parseModuleSourceElements(TreeBuilder&, SourceParseMode); - enum class ImportSpecifierType { NamespaceImport, NamedImport, DefaultImport }; - template <class TreeBuilder> typename TreeBuilder::ImportSpecifier parseImportClauseItem(TreeBuilder&, ImportSpecifierType); - template <class TreeBuilder> typename TreeBuilder::ModuleName parseModuleName(TreeBuilder&); - template <class TreeBuilder> TreeStatement parseImportDeclaration(TreeBuilder&); - template <class TreeBuilder> typename TreeBuilder::ExportSpecifier parseExportSpecifier(TreeBuilder& context, Vector<const Identifier*>& maybeLocalNames, bool& hasKeywordForLocalBindings); - template <class TreeBuilder> TreeStatement parseExportDeclaration(TreeBuilder&); - - template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, SourceParseMode, bool nameIsInContainingScope, ConstructorKind, SuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>&, FunctionParseType); - - template <class TreeBuilder> NEVER_INLINE int parseFunctionParameters(TreeBuilder&, SourceParseMode, ParserFunctionInfo<TreeBuilder>&); - -#if ENABLE(ES6_CLASS_SYNTAX) - template <class TreeBuilder> NEVER_INLINE TreeClassExpression parseClass(TreeBuilder&, FunctionRequirements, ParserClassInfo<TreeBuilder>&); -#endif - -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - template <class TreeBuilder> NEVER_INLINE typename TreeBuilder::TemplateString parseTemplateString(TreeBuilder& context, bool isTemplateHead, typename LexerType::RawStringsBuildMode, bool& elementIsTail); - template <class TreeBuilder> NEVER_INLINE typename TreeBuilder::TemplateLiteral parseTemplateLiteral(TreeBuilder&, typename LexerType::RawStringsBuildMode); -#endif - - template <class TreeBuilder> ALWAYS_INLINE bool shouldCheckPropertyForUnderscoreProtoDuplicate(TreeBuilder&, const TreeProperty&); - + template <class TreeBuilder> ALWAYS_INLINE TreeFunctionBody parseFunctionBody(TreeBuilder&); + template <class TreeBuilder> ALWAYS_INLINE TreeFormalParameterList parseFormalParameters(TreeBuilder&); + template <class TreeBuilder> TreeExpression parseVarDeclarationList(TreeBuilder&, int& declarations, TreeDeconstructionPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd); + template <class TreeBuilder> NEVER_INLINE TreeConstDeclList parseConstDeclarationList(TreeBuilder&); + + template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern createBindingPattern(TreeBuilder&, DeconstructionKind, const Identifier&, int depth); + template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern parseDeconstructionPattern(TreeBuilder&, DeconstructionKind, int depth = 0); + template <class TreeBuilder> NEVER_INLINE TreeDeconstructionPattern tryParseDeconstructionPatternExpression(TreeBuilder&); + template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, FunctionParseMode, bool nameIsInContainingScope, const Identifier*&, TreeFormalParameterList&, TreeFunctionBody&, unsigned& openBraceOffset, unsigned& closeBraceOffset, int& bodyStartLine, unsigned& bodyStartColumn); ALWAYS_INLINE int isBinaryOperator(JSTokenType); bool allowAutomaticSemicolon(); @@ -1148,11 +755,6 @@ private: return allowAutomaticSemicolon(); } - void setEndOfStatement() - { - m_lexer->setTokenPosition(&m_token); - } - bool canRecurse() { return m_vm->isSafeToRecurse(); @@ -1221,9 +823,8 @@ private: VM* m_vm; const SourceCode* m_source; - ParserArena m_parserArena; - std::unique_ptr<LexerType> m_lexer; - FunctionParameters* m_parameters { nullptr }; + ParserArena* m_arena; + OwnPtr<LexerType> m_lexer; bool m_hasStackOverflow; String m_errorMessage; @@ -1239,12 +840,9 @@ private: const Identifier* m_lastFunctionName; RefPtr<SourceProviderCache> m_functionCache; SourceElements* m_sourceElements; - bool m_parsingBuiltin; - ConstructorKind m_defaultConstructorKind; - ThisTDZMode m_thisTDZMode; - VariableEnvironment m_varDeclarations; - DeclarationStacks::FunctionStack m_funcDeclarations; - Vector<RefPtr<UniquedStringImpl>> m_closedVariables; + ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations; + ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations; + IdentifierSet m_capturedVariables; CodeFeatures m_features; int m_numConstants; @@ -1269,13 +867,13 @@ private: template <typename LexerType> template <class ParsedNode> -std::unique_ptr<ParsedNode> Parser<LexerType>::parse(ParserError& error, const Identifier& calleeName, SourceParseMode parseMode) +PassRefPtr<ParsedNode> Parser<LexerType>::parse(ParserError& error) { int errLine; String errMsg; if (ParsedNode::scopeIsFunction) - m_lexer->setIsReparsingFunction(); + m_lexer->setIsReparsing(); m_sourceElements = 0; @@ -1286,7 +884,7 @@ std::unique_ptr<ParsedNode> Parser<LexerType>::parse(ParserError& error, const I ASSERT(m_source->startColumn() > 0); unsigned startColumn = m_source->startColumn() - 1; - String parseError = parseInner(calleeName, parseMode); + String parseError = parseInner(); int lineNumber = m_lexer->lineNumber(); bool lexError = m_lexer->sawError(); @@ -1300,28 +898,26 @@ std::unique_ptr<ParsedNode> Parser<LexerType>::parse(ParserError& error, const I m_sourceElements = 0; } - std::unique_ptr<ParsedNode> result; + RefPtr<ParsedNode> result; if (m_sourceElements) { JSTokenLocation endLocation; endLocation.line = m_lexer->lineNumber(); endLocation.lineStartOffset = m_lexer->currentLineStartOffset(); endLocation.startOffset = m_lexer->currentOffset(); unsigned endColumn = endLocation.startOffset - endLocation.lineStartOffset; - result = std::make_unique<ParsedNode>(m_parserArena, + result = ParsedNode::create(m_vm, startLocation, endLocation, startColumn, endColumn, m_sourceElements, - m_varDeclarations, - m_funcDeclarations, - currentScope()->finalizeLexicalEnvironment(), - m_parameters, + m_varDeclarations ? &m_varDeclarations->data : 0, + m_funcDeclarations ? &m_funcDeclarations->data : 0, + m_capturedVariables, *m_source, m_features, m_numConstants); result->setLoc(m_source->firstLine(), m_lexer->lineNumber(), m_lexer->currentOffset(), m_lexer->currentLineStartOffset()); - result->setEndOffset(m_lexer->currentOffset()); } else { // We can never see a syntax error when reparsing a function, since we should have // reported the error when parsing the containing program or eval code. So if we're @@ -1329,7 +925,7 @@ std::unique_ptr<ParsedNode> Parser<LexerType>::parse(ParserError& error, const I // we ran out of stack while parsing. If we see an error while parsing eval or program // code we assume that it was a syntax error since running out of stack is much less // likely, and we are currently unable to distinguish between the two cases. - if (isFunctionMetadataNode(static_cast<ParsedNode*>(0)) || m_hasStackOverflow) + if (isFunctionBodyNode(static_cast<ParsedNode*>(0)) || m_hasStackOverflow) error = ParserError(ParserError::StackOverflow, ParserError::SyntaxErrorNone, m_token); else { ParserError::SyntaxErrorType errorType = ParserError::SyntaxErrorIrrecoverable; @@ -1345,40 +941,29 @@ std::unique_ptr<ParsedNode> Parser<LexerType>::parse(ParserError& error, const I } } - return result; + m_arena->reset(); + + return result.release(); } template <class ParsedNode> -std::unique_ptr<ParsedNode> parse( - VM* vm, const SourceCode& source, - const Identifier& name, JSParserBuiltinMode builtinMode, - JSParserStrictMode strictMode, SourceParseMode parseMode, - ParserError& error, JSTextPosition* positionBeforeLastNewline = nullptr, - ConstructorKind defaultConstructorKind = ConstructorKind::None, - ThisTDZMode thisTDZMode = ThisTDZMode::CheckIfNeeded) +PassRefPtr<ParsedNode> parse(VM* vm, const SourceCode& source, FunctionParameters* parameters, const Identifier& name, JSParserStrictness strictness, JSParserMode parserMode, ParserError& error, JSTextPosition* positionBeforeLastNewline = 0) { SamplingRegion samplingRegion("Parsing"); ASSERT(!source.provider()->source().isNull()); if (source.provider()->source().is8Bit()) { - Parser<Lexer<LChar>> parser(vm, source, builtinMode, strictMode, parseMode, defaultConstructorKind, thisTDZMode); - std::unique_ptr<ParsedNode> result = parser.parse<ParsedNode>(error, name, parseMode); + Parser<Lexer<LChar>> parser(vm, source, parameters, name, strictness, parserMode); + RefPtr<ParsedNode> result = parser.parse<ParsedNode>(error); if (positionBeforeLastNewline) *positionBeforeLastNewline = parser.positionBeforeLastNewline(); - if (builtinMode == JSParserBuiltinMode::Builtin) { - if (!result) - WTF::dataLog("Error compiling builtin: ", error.message(), "\n"); - RELEASE_ASSERT(result); - result->setClosedVariables(parser.closedVariables()); - } - return result; + return result.release(); } - ASSERT_WITH_MESSAGE(defaultConstructorKind == ConstructorKind::None, "BuiltinExecutables::createDefaultConstructor should always use a 8-bit string"); - Parser<Lexer<UChar>> parser(vm, source, builtinMode, strictMode, parseMode, defaultConstructorKind, thisTDZMode); - std::unique_ptr<ParsedNode> result = parser.parse<ParsedNode>(error, name, parseMode); + Parser<Lexer<UChar>> parser(vm, source, parameters, name, strictness, parserMode); + RefPtr<ParsedNode> result = parser.parse<ParsedNode>(error); if (positionBeforeLastNewline) *positionBeforeLastNewline = parser.positionBeforeLastNewline(); - return result; + return result.release(); } } // namespace diff --git a/Source/JavaScriptCore/parser/ParserArena.cpp b/Source/JavaScriptCore/parser/ParserArena.cpp index a27688770..c53f30753 100644 --- a/Source/JavaScriptCore/parser/ParserArena.cpp +++ b/Source/JavaScriptCore/parser/ParserArena.cpp @@ -27,7 +27,7 @@ #include "ParserArena.h" #include "Nodes.h" -#include "JSCInlines.h" +#include <wtf/PassOwnPtr.h> namespace JSC { @@ -62,6 +62,38 @@ ParserArena::~ParserArena() deallocateObjects(); } +bool ParserArena::contains(ParserArenaRefCounted* object) const +{ + return m_refCountedObjects.find(object) != notFound; +} + +ParserArenaRefCounted* ParserArena::last() const +{ + return m_refCountedObjects.last().get(); +} + +void ParserArena::removeLast() +{ + m_refCountedObjects.removeLast(); +} + +void ParserArena::reset() +{ + // Since this code path is used only when parsing fails, it's not bothering to reuse + // any of the memory the arena allocated. We could improve that later if we want to + // efficiently reuse the same arena. + + deallocateObjects(); + + m_freeableMemory = 0; + m_freeablePoolEnd = 0; + if (m_identifierArena) + m_identifierArena->clear(); + m_freeablePools.clear(); + m_deletableObjects.clear(); + m_refCountedObjects.clear(); +} + void ParserArena::allocateFreeablePool() { if (m_freeablePoolEnd) @@ -73,4 +105,18 @@ void ParserArena::allocateFreeablePool() ASSERT(freeablePool() == pool); } +bool ParserArena::isEmpty() const +{ + return !m_freeablePoolEnd + && (!m_identifierArena || m_identifierArena->isEmpty()) + && m_freeablePools.isEmpty() + && m_deletableObjects.isEmpty() + && m_refCountedObjects.isEmpty(); +} + +void ParserArena::derefWithArena(PassRefPtr<ParserArenaRefCounted> object) +{ + m_refCountedObjects.append(object); +} + } diff --git a/Source/JavaScriptCore/parser/ParserArena.h b/Source/JavaScriptCore/parser/ParserArena.h index 2a7d44de7..e806d45e1 100644 --- a/Source/JavaScriptCore/parser/ParserArena.h +++ b/Source/JavaScriptCore/parser/ParserArena.h @@ -26,7 +26,6 @@ #ifndef ParserArena_h #define ParserArena_h -#include "CommonIdentifiers.h" #include "Identifier.h" #include <array> #include <wtf/SegmentedVector.h> @@ -34,6 +33,7 @@ namespace JSC { class ParserArenaDeletable; + class ParserArenaRefCounted; class IdentifierArena { WTF_MAKE_FAST_ALLOCATED; @@ -45,11 +45,12 @@ namespace JSC { template <typename T> ALWAYS_INLINE const Identifier& makeIdentifier(VM*, const T* characters, size_t length); - ALWAYS_INLINE const Identifier& makeEmptyIdentifier(VM*); ALWAYS_INLINE const Identifier& makeIdentifierLCharFromUChar(VM*, const UChar* characters, size_t length); const Identifier& makeNumericIdentifier(VM*, double number); + bool isEmpty() const { return m_identifiers.isEmpty(); } + public: static const int MaximumCachableCharacter = 128; typedef SegmentedVector<Identifier, 64> IdentifierVector; @@ -71,36 +72,27 @@ namespace JSC { template <typename T> ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(VM* vm, const T* characters, size_t length) { - if (!length) - return vm->propertyNames->emptyIdentifier; if (characters[0] >= MaximumCachableCharacter) { - m_identifiers.append(Identifier::fromString(vm, characters, length)); + m_identifiers.append(Identifier(vm, characters, length)); return m_identifiers.last(); } if (length == 1) { if (Identifier* ident = m_shortIdentifiers[characters[0]]) return *ident; - m_identifiers.append(Identifier::fromString(vm, characters, length)); + m_identifiers.append(Identifier(vm, characters, length)); m_shortIdentifiers[characters[0]] = &m_identifiers.last(); return m_identifiers.last(); } Identifier* ident = m_recentIdentifiers[characters[0]]; if (ident && Identifier::equal(ident->impl(), characters, length)) return *ident; - m_identifiers.append(Identifier::fromString(vm, characters, length)); + m_identifiers.append(Identifier(vm, characters, length)); m_recentIdentifiers[characters[0]] = &m_identifiers.last(); return m_identifiers.last(); } - ALWAYS_INLINE const Identifier& IdentifierArena::makeEmptyIdentifier(VM* vm) - { - return vm->propertyNames->emptyIdentifier; - } - ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifierLCharFromUChar(VM* vm, const UChar* characters, size_t length) { - if (!length) - return vm->propertyNames->emptyIdentifier; if (characters[0] >= MaximumCachableCharacter) { m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length)); return m_identifiers.last(); @@ -108,7 +100,7 @@ namespace JSC { if (length == 1) { if (Identifier* ident = m_shortIdentifiers[characters[0]]) return *ident; - m_identifiers.append(Identifier::fromString(vm, characters, length)); + m_identifiers.append(Identifier(vm, characters, length)); m_shortIdentifiers[characters[0]] = &m_identifiers.last(); return m_identifiers.last(); } @@ -122,7 +114,7 @@ namespace JSC { inline const Identifier& IdentifierArena::makeNumericIdentifier(VM* vm, double number) { - m_identifiers.append(Identifier::fromString(vm, String::numberToStringECMAScript(number))); + m_identifiers.append(Identifier(vm, String::numberToStringECMAScript(number))); return m_identifiers.last(); } @@ -139,6 +131,7 @@ namespace JSC { m_identifierArena.swap(otherArena.m_identifierArena); m_freeablePools.swap(otherArena.m_freeablePools); m_deletableObjects.swap(otherArena.m_deletableObjects); + m_refCountedObjects.swap(otherArena.m_refCountedObjects); } void* allocateFreeable(size_t size) @@ -161,10 +154,18 @@ namespace JSC { return deletable; } + void derefWithArena(PassRefPtr<ParserArenaRefCounted>); + bool contains(ParserArenaRefCounted*) const; + ParserArenaRefCounted* last() const; + void removeLast(); + + bool isEmpty() const; + JS_EXPORT_PRIVATE void reset(); + IdentifierArena& identifierArena() { if (UNLIKELY (!m_identifierArena)) - m_identifierArena = std::make_unique<IdentifierArena>(); + m_identifierArena = adoptPtr(new IdentifierArena); return *m_identifierArena; } @@ -183,9 +184,10 @@ namespace JSC { char* m_freeableMemory; char* m_freeablePoolEnd; - std::unique_ptr<IdentifierArena> m_identifierArena; + OwnPtr<IdentifierArena> m_identifierArena; Vector<void*> m_freeablePools; Vector<ParserArenaDeletable*> m_deletableObjects; + Vector<RefPtr<ParserArenaRefCounted>> m_refCountedObjects; }; } diff --git a/Source/JavaScriptCore/parser/ParserError.h b/Source/JavaScriptCore/parser/ParserError.h index 89a05ab42..baa4465d0 100644 --- a/Source/JavaScriptCore/parser/ParserError.h +++ b/Source/JavaScriptCore/parser/ParserError.h @@ -27,15 +27,13 @@ #define ParserError_h #include "Error.h" -#include "ErrorHandlingScope.h" #include "ExceptionHelpers.h" #include "ParserTokens.h" #include <wtf/text/WTFString.h> namespace JSC { -class ParserError { -public: +struct ParserError { enum SyntaxErrorType { SyntaxErrorNone, SyntaxErrorIrrecoverable, @@ -51,72 +49,60 @@ public: SyntaxError }; + ErrorType m_type; + SyntaxErrorType m_syntaxErrorType; + JSToken m_token; + String m_message; + int m_line; ParserError() : m_type(ErrorNone) , m_syntaxErrorType(SyntaxErrorNone) + , m_line(-1) { } explicit ParserError(ErrorType type) : m_type(type) , m_syntaxErrorType(SyntaxErrorNone) + , m_line(-1) { } ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token) - : m_token(token) - , m_type(type) + : m_type(type) , m_syntaxErrorType(syntaxError) + , m_token(token) + , m_line(-1) { } - ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token, const String& msg, int line) - : m_token(token) + ParserError(ErrorType type, SyntaxErrorType syntaxError, JSToken token, String msg, int line) + : m_type(type) + , m_syntaxErrorType(syntaxError) + , m_token(token) , m_message(msg) , m_line(line) - , m_type(type) - , m_syntaxErrorType(syntaxError) { } - bool isValid() const { return m_type != ErrorNone; } - SyntaxErrorType syntaxErrorType() const { return m_syntaxErrorType; } - const JSToken& token() const { return m_token; } - const String& message() const { return m_message; } - int line() const { return m_line; } - - JSObject* toErrorObject( - JSGlobalObject* globalObject, const SourceCode& source, - int overrideLineNumber = -1) + JSObject* toErrorObject(JSGlobalObject* globalObject, const SourceCode& source) { - ExecState* exec = globalObject->globalExec(); switch (m_type) { case ErrorNone: - return nullptr; + return 0; case SyntaxError: - return addErrorInfo( - exec, - createSyntaxError(exec, m_message), - overrideLineNumber == -1 ? m_line : overrideLineNumber, source); + return addErrorInfo(globalObject->globalExec(), createSyntaxError(globalObject, m_message), m_line, source); case EvalError: - return createSyntaxError(exec, m_message); - case StackOverflow: { - ErrorHandlingScope errorScope(globalObject->vm()); - return createStackOverflowError(exec); - } + return createSyntaxError(globalObject, m_message); + case StackOverflow: + return createStackOverflowError(globalObject); case OutOfMemory: - return createOutOfMemoryError(exec); + return createOutOfMemoryError(globalObject); } CRASH(); - return nullptr; + return createOutOfMemoryError(globalObject); // Appease Qt bot } - -private: - JSToken m_token; - String m_message; - int m_line { -1 }; - ErrorType m_type; - SyntaxErrorType m_syntaxErrorType; +#undef GET_ERROR_CODE }; } // namespace JSC diff --git a/Source/JavaScriptCore/parser/ParserFunctionInfo.h b/Source/JavaScriptCore/parser/ParserFunctionInfo.h deleted file mode 100644 index 4c565aae6..000000000 --- a/Source/JavaScriptCore/parser/ParserFunctionInfo.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef ParserFunctionInfo_h -#define ParserFunctionInfo_h - -namespace JSC { - -template <class TreeBuilder> -struct ParserFunctionInfo { - const Identifier* name = 0; - typename TreeBuilder::FormalParameterList parameters = 0; - typename TreeBuilder::FunctionBody body = 0; - unsigned parameterCount = 0; - unsigned startOffset = 0; - unsigned endOffset = 0; - int startLine = 0; - int endLine = 0; - unsigned bodyStartColumn = 0; -}; - -#if ENABLE(ES6_CLASS_SYNTAX) -template <class TreeBuilder> -struct ParserClassInfo { - const Identifier* className = 0; -}; -#endif - -} - -#endif diff --git a/Source/JavaScriptCore/parser/ParserModes.h b/Source/JavaScriptCore/parser/ParserModes.h index a65603805..4e9a17cd2 100644 --- a/Source/JavaScriptCore/parser/ParserModes.h +++ b/Source/JavaScriptCore/parser/ParserModes.h @@ -27,115 +27,15 @@ #ifndef ParserModes_h #define ParserModes_h -#include "Identifier.h" - namespace JSC { -enum class JSParserStrictMode { NotStrict, Strict }; -enum class JSParserBuiltinMode { NotBuiltin, Builtin }; -enum class JSParserCodeType { Program, Function, Module }; - -enum class ConstructorKind { None, Base, Derived }; -enum class SuperBinding { Needed, NotNeeded }; -enum class ThisTDZMode { AlwaysCheck, CheckIfNeeded }; +enum JSParserStrictness { JSParseNormal, JSParseStrict }; +enum JSParserMode { JSParseProgramCode, JSParseFunctionCode }; enum ProfilerMode { ProfilerOff, ProfilerOn }; enum DebuggerMode { DebuggerOff, DebuggerOn }; -enum FunctionMode { FunctionExpression, FunctionDeclaration }; - -enum class SourceParseMode { - NormalFunctionMode, - GetterMode, - SetterMode, - MethodMode, - ArrowFunctionMode, - ProgramMode, - ModuleAnalyzeMode, - ModuleEvaluateMode -}; - -inline bool isFunctionParseMode(SourceParseMode parseMode) -{ - switch (parseMode) { - case SourceParseMode::NormalFunctionMode: - case SourceParseMode::GetterMode: - case SourceParseMode::SetterMode: - case SourceParseMode::MethodMode: - case SourceParseMode::ArrowFunctionMode: - return true; - - case SourceParseMode::ProgramMode: - case SourceParseMode::ModuleAnalyzeMode: - case SourceParseMode::ModuleEvaluateMode: - return false; - } - RELEASE_ASSERT_NOT_REACHED(); - return false; -} - -inline bool isModuleParseMode(SourceParseMode parseMode) -{ - switch (parseMode) { - case SourceParseMode::ModuleAnalyzeMode: - case SourceParseMode::ModuleEvaluateMode: - return true; - - case SourceParseMode::NormalFunctionMode: - case SourceParseMode::GetterMode: - case SourceParseMode::SetterMode: - case SourceParseMode::MethodMode: - case SourceParseMode::ArrowFunctionMode: - case SourceParseMode::ProgramMode: - return false; - } - RELEASE_ASSERT_NOT_REACHED(); - return false; -} - -inline bool isProgramParseMode(SourceParseMode parseMode) -{ - switch (parseMode) { - case SourceParseMode::ProgramMode: - return true; - - case SourceParseMode::NormalFunctionMode: - case SourceParseMode::GetterMode: - case SourceParseMode::SetterMode: - case SourceParseMode::MethodMode: - case SourceParseMode::ArrowFunctionMode: - case SourceParseMode::ModuleAnalyzeMode: - case SourceParseMode::ModuleEvaluateMode: - return false; - } - RELEASE_ASSERT_NOT_REACHED(); - return false; -} - -inline bool functionNameIsInScope(const Identifier& name, FunctionMode functionMode) -{ - if (name.isNull()) - return false; - - if (functionMode != FunctionExpression) - return false; - - return true; -} - -inline bool functionNameScopeIsDynamic(bool usesEval, bool isStrictMode) -{ - // If non-strict eval is in play, a function gets a separate object in the scope chain for its name. - // This enables eval to declare and then delete a name that shadows the function's name. - - if (!usesEval) - return false; - - if (isStrictMode) - return false; - - return true; -} +enum FunctionNameIsInScopeToggle { FunctionNameIsNotInScope, FunctionNameIsInScope }; typedef unsigned CodeFeatures; @@ -148,7 +48,6 @@ const CodeFeatures ThisFeature = 1 << 4; const CodeFeatures StrictModeFeature = 1 << 5; const CodeFeatures ShadowsArgumentsFeature = 1 << 6; const CodeFeatures ModifiedParameterFeature = 1 << 7; -const CodeFeatures ModifiedArgumentsFeature = 1 << 8; const CodeFeatures AllFeatures = EvalFeature | ArgumentsFeature | WithFeature | CatchFeature | ThisFeature | StrictModeFeature | ShadowsArgumentsFeature | ModifiedParameterFeature; diff --git a/Source/JavaScriptCore/parser/ParserTokens.h b/Source/JavaScriptCore/parser/ParserTokens.h index 3dfdc28a8..82a26639a 100644 --- a/Source/JavaScriptCore/parser/ParserTokens.h +++ b/Source/JavaScriptCore/parser/ParserTokens.h @@ -57,7 +57,6 @@ enum JSTokenType { FOR, NEW, VAR, - LET, CONSTTOKEN, CONTINUE, FUNCTION, @@ -76,17 +75,6 @@ enum JSTokenType { FINALLY, DEBUGGER, ELSE, - IMPORT, - EXPORT, -#if ENABLE(ES6_CLASS_SYNTAX) - CLASSTOKEN, - EXTENDS, - SUPER, -#else - CLASSTOKEN = RESERVED, - EXTENDS = RESERVED, - SUPER = RESERVED, -#endif OPENBRACE = 0, CLOSEBRACE, OPENPAREN, @@ -95,11 +83,9 @@ enum JSTokenType { CLOSEBRACKET, COMMA, QUESTION, - INTEGER, - DOUBLE, + NUMBER, IDENT, STRING, - TEMPLATE, SEMICOLON, COLON, DOT, @@ -117,9 +103,6 @@ enum JSTokenType { XOREQUAL, OREQUAL, DOTDOTDOT, -#if ENABLE(ES6_ARROWFUNCTION_SYNTAX) - ARROWFUNCTION, -#endif LastUntaggedToken, // Begin tagged tokens @@ -166,11 +149,6 @@ enum JSTokenType { INVALID_NUMERIC_LITERAL_ERRORTOK = 7 | ErrorTokenFlag, UNTERMINATED_STRING_LITERAL_ERRORTOK = 8 | ErrorTokenFlag | UnterminatedErrorTokenFlag, INVALID_STRING_LITERAL_ERRORTOK = 9 | ErrorTokenFlag, - INVALID_PRIVATE_NAME_ERRORTOK = 10 | ErrorTokenFlag, - INVALID_HEX_NUMBER_ERRORTOK = 11 | ErrorTokenFlag, - INVALID_BINARY_NUMBER_ERRORTOK = 12 | ErrorTokenFlag, - UNTERMINATED_TEMPLATE_LITERAL_ERRORTOK = 13 | ErrorTokenFlag | UnterminatedErrorTokenFlag, - INVALID_TEMPLATE_LITERAL_ERRORTOK = 14 | ErrorTokenFlag, }; struct JSTextPosition { @@ -198,11 +176,6 @@ union JSTokenData { }; double doubleValue; const Identifier* ident; - struct { - const Identifier* cooked; - const Identifier* raw; - bool isTail; - }; }; struct JSTokenLocation { diff --git a/Source/JavaScriptCore/parser/SourceCode.cpp b/Source/JavaScriptCore/parser/SourceCode.cpp index 8ef168dea..7c2d6adbc 100644 --- a/Source/JavaScriptCore/parser/SourceCode.cpp +++ b/Source/JavaScriptCore/parser/SourceCode.cpp @@ -26,7 +26,6 @@ #include "config.h" #include "SourceCode.h" -#include "JSCInlines.h" #include <wtf/text/CString.h> namespace JSC { diff --git a/Source/JavaScriptCore/parser/SourceCode.h b/Source/JavaScriptCore/parser/SourceCode.h index 7f37ecf86..cf1a4a3d9 100644 --- a/Source/JavaScriptCore/parser/SourceCode.h +++ b/Source/JavaScriptCore/parser/SourceCode.h @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -117,9 +117,11 @@ namespace JSC { { return SourceCode(StringSourceProvider::create(source, url, startPosition), startPosition.m_line.oneBasedInt(), startPosition.m_column.oneBasedInt()); } - + inline SourceCode SourceCode::subExpression(unsigned openBrace, unsigned closeBrace, int firstLine, int startColumn) { + ASSERT(provider()->source()[openBrace] == '{'); + ASSERT(provider()->source()[closeBrace] == '}'); startColumn += 1; // Convert to base 1. return SourceCode(provider(), openBrace, closeBrace + 1, firstLine, startColumn); } diff --git a/Source/JavaScriptCore/parser/SourceProvider.cpp b/Source/JavaScriptCore/parser/SourceProvider.cpp index 19ffb3372..d12045592 100644 --- a/Source/JavaScriptCore/parser/SourceProvider.cpp +++ b/Source/JavaScriptCore/parser/SourceProvider.cpp @@ -25,10 +25,8 @@ #include "config.h" #include "SourceProvider.h" - -#include "JSCInlines.h" -#include <wtf/Lock.h> #include <wtf/StdLibExtras.h> +#include <wtf/TCSpinLock.h> namespace JSC { @@ -44,11 +42,11 @@ SourceProvider::~SourceProvider() { } -static StaticLock providerIdLock; +static TCMalloc_SpinLock providerIdLock = SPINLOCK_INITIALIZER; void SourceProvider::getID() { - LockHolder lock(&providerIdLock); + SpinLockHolder lock(&providerIdLock); if (!m_id) { static intptr_t nextProviderID = 0; m_id = ++nextProviderID; diff --git a/Source/JavaScriptCore/parser/SourceProvider.h b/Source/JavaScriptCore/parser/SourceProvider.h index c263a1390..72c0de41e 100644 --- a/Source/JavaScriptCore/parser/SourceProvider.h +++ b/Source/JavaScriptCore/parser/SourceProvider.h @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -29,6 +29,7 @@ #ifndef SourceProvider_h #define SourceProvider_h +#include <wtf/PassOwnPtr.h> #include <wtf/RefCounted.h> #include <wtf/text/TextPosition.h> #include <wtf/text/WTFString.h> @@ -53,6 +54,9 @@ namespace JSC { TextPosition startPosition() const { return m_startPosition; } intptr_t asID() { + ASSERT(this); + if (!this) // Be defensive in release mode. + return nullID; if (!m_id) getID(); return m_id; @@ -74,9 +78,9 @@ namespace JSC { class StringSourceProvider : public SourceProvider { public: - static Ref<StringSourceProvider> create(const String& source, const String& url, const TextPosition& startPosition = TextPosition::minimumPosition()) + static PassRefPtr<StringSourceProvider> create(const String& source, const String& url, const TextPosition& startPosition = TextPosition::minimumPosition()) { - return adoptRef(*new StringSourceProvider(source, url, startPosition)); + return adoptRef(new StringSourceProvider(source, url, startPosition)); } virtual const String& source() const override @@ -94,37 +98,6 @@ namespace JSC { String m_source; }; -#if ENABLE(WEBASSEMBLY) - class WebAssemblySourceProvider : public SourceProvider { - public: - static Ref<WebAssemblySourceProvider> create(const Vector<uint8_t>& data, const String& url) - { - return adoptRef(*new WebAssemblySourceProvider(data, url)); - } - - virtual const String& source() const override - { - return m_source; - } - - const Vector<uint8_t>& data() const - { - return m_data; - } - - private: - WebAssemblySourceProvider(const Vector<uint8_t>& data, const String& url) - : SourceProvider(url, TextPosition::minimumPosition()) - , m_source("[WebAssembly source]") - , m_data(data) - { - } - - String m_source; - Vector<uint8_t> m_data; - }; -#endif - } // namespace JSC #endif // SourceProvider_h diff --git a/Source/JavaScriptCore/parser/SourceProviderCache.cpp b/Source/JavaScriptCore/parser/SourceProviderCache.cpp index d5958e8c4..bc28fb166 100644 --- a/Source/JavaScriptCore/parser/SourceProviderCache.cpp +++ b/Source/JavaScriptCore/parser/SourceProviderCache.cpp @@ -26,8 +26,6 @@ #include "config.h" #include "SourceProviderCache.h" -#include "JSCInlines.h" - namespace JSC { SourceProviderCache::~SourceProviderCache() @@ -42,7 +40,7 @@ void SourceProviderCache::clear() void SourceProviderCache::add(int sourcePosition, std::unique_ptr<SourceProviderCacheItem> item) { - m_map.add(sourcePosition, WTF::move(item)); + m_map.add(sourcePosition, std::move(item)); } } diff --git a/Source/JavaScriptCore/parser/SourceProviderCache.h b/Source/JavaScriptCore/parser/SourceProviderCache.h index 5070b2811..7558a09f4 100644 --- a/Source/JavaScriptCore/parser/SourceProviderCache.h +++ b/Source/JavaScriptCore/parser/SourceProviderCache.h @@ -28,6 +28,8 @@ #include "SourceProviderCacheItem.h" #include <wtf/HashMap.h> +#include <wtf/OwnPtr.h> +#include <wtf/PassOwnPtr.h> #include <wtf/RefCounted.h> namespace JSC { @@ -43,7 +45,7 @@ public: const SourceProviderCacheItem* get(int sourcePosition) const { return m_map.get(sourcePosition); } private: - HashMap<int, std::unique_ptr<SourceProviderCacheItem>, WTF::IntHash<int>, WTF::UnsignedWithZeroKeyHashTraits<int>> m_map; + HashMap<int, std::unique_ptr<SourceProviderCacheItem>> m_map; }; } diff --git a/Source/JavaScriptCore/parser/SourceProviderCacheItem.h b/Source/JavaScriptCore/parser/SourceProviderCacheItem.h index 81d221b39..396211861 100644 --- a/Source/JavaScriptCore/parser/SourceProviderCacheItem.h +++ b/Source/JavaScriptCore/parser/SourceProviderCacheItem.h @@ -27,27 +27,22 @@ #define SourceProviderCacheItem_h #include "ParserTokens.h" +#include <wtf/PassOwnPtr.h> #include <wtf/Vector.h> -#include <wtf/text/UniquedStringImpl.h> #include <wtf/text/WTFString.h> namespace JSC { struct SourceProviderCacheItemCreationParameters { unsigned functionNameStart; - unsigned lastTockenLine; - unsigned lastTockenStartOffset; - unsigned lastTockenEndOffset; - unsigned lastTockenLineStartOffset; - unsigned endFunctionOffset; - unsigned parameterCount; + unsigned closeBraceLine; + unsigned closeBraceOffset; + unsigned closeBraceLineStartOffset; bool needsFullActivation; bool usesEval; bool strictMode; - Vector<RefPtr<UniquedStringImpl>> usedVariables; - Vector<RefPtr<UniquedStringImpl>> writtenVariables; - bool isBodyArrowExpression { false }; - JSTokenType tokenType { CLOSEBRACE }; + Vector<RefPtr<StringImpl>> usedVariables; + Vector<RefPtr<StringImpl>> writtenVariables; }; #if COMPILER(MSVC) @@ -61,15 +56,15 @@ public: static std::unique_ptr<SourceProviderCacheItem> create(const SourceProviderCacheItemCreationParameters&); ~SourceProviderCacheItem(); - JSToken endFunctionToken() const + JSToken closeBraceToken() const { JSToken token; - token.m_type = isBodyArrowExpression ? tokenType : CLOSEBRACE; - token.m_data.offset = lastTockenStartOffset; - token.m_location.startOffset = lastTockenStartOffset; - token.m_location.endOffset = lastTockenEndOffset; - token.m_location.line = lastTockenLine; - token.m_location.lineStartOffset = lastTockenLineStartOffset; + token.m_type = CLOSEBRACE; + token.m_data.offset = closeBraceOffset; + token.m_location.startOffset = closeBraceOffset; + token.m_location.endOffset = closeBraceOffset + 1; + token.m_location.line = closeBraceLine; + token.m_location.lineStartOffset = closeBraceLineStartOffset; // token.m_location.sourceOffset is initialized once by the client. So, // we do not need to set it here. return token; @@ -77,30 +72,24 @@ public: unsigned functionNameStart : 31; bool needsFullActivation : 1; - - unsigned endFunctionOffset : 31; - unsigned lastTockenLine : 31; - unsigned lastTockenStartOffset : 31; - unsigned lastTockenEndOffset: 31; - unsigned parameterCount; + unsigned closeBraceLine : 31; bool usesEval : 1; + unsigned closeBraceOffset : 31; bool strictMode : 1; - unsigned lastTockenLineStartOffset; + unsigned closeBraceLineStartOffset; unsigned usedVariablesCount; unsigned writtenVariablesCount; - UniquedStringImpl** usedVariables() const { return const_cast<UniquedStringImpl**>(m_variables); } - UniquedStringImpl** writtenVariables() const { return const_cast<UniquedStringImpl**>(&m_variables[usedVariablesCount]); } - bool isBodyArrowExpression; - JSTokenType tokenType; + StringImpl** usedVariables() const { return const_cast<StringImpl**>(m_variables); } + StringImpl** writtenVariables() const { return const_cast<StringImpl**>(&m_variables[usedVariablesCount]); } private: SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters&); - UniquedStringImpl* m_variables[0]; + StringImpl* m_variables[0]; }; inline SourceProviderCacheItem::~SourceProviderCacheItem() @@ -112,7 +101,7 @@ inline SourceProviderCacheItem::~SourceProviderCacheItem() inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create(const SourceProviderCacheItemCreationParameters& parameters) { size_t variableCount = parameters.writtenVariables.size() + parameters.usedVariables.size(); - size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(UniquedStringImpl*) * variableCount; + size_t objectSize = sizeof(SourceProviderCacheItem) + sizeof(StringImpl*) * variableCount; void* slot = fastMalloc(objectSize); return std::unique_ptr<SourceProviderCacheItem>(new (slot) SourceProviderCacheItem(parameters)); } @@ -120,18 +109,13 @@ inline std::unique_ptr<SourceProviderCacheItem> SourceProviderCacheItem::create( inline SourceProviderCacheItem::SourceProviderCacheItem(const SourceProviderCacheItemCreationParameters& parameters) : functionNameStart(parameters.functionNameStart) , needsFullActivation(parameters.needsFullActivation) - , endFunctionOffset(parameters.endFunctionOffset) - , lastTockenLine(parameters.lastTockenLine) - , lastTockenStartOffset(parameters.lastTockenStartOffset) - , lastTockenEndOffset(parameters.lastTockenEndOffset) - , parameterCount(parameters.parameterCount) + , closeBraceLine(parameters.closeBraceLine) , usesEval(parameters.usesEval) + , closeBraceOffset(parameters.closeBraceOffset) , strictMode(parameters.strictMode) - , lastTockenLineStartOffset(parameters.lastTockenLineStartOffset) + , closeBraceLineStartOffset(parameters.closeBraceLineStartOffset) , usedVariablesCount(parameters.usedVariables.size()) , writtenVariablesCount(parameters.writtenVariables.size()) - , isBodyArrowExpression(parameters.isBodyArrowExpression) - , tokenType(parameters.tokenType) { unsigned j = 0; for (unsigned i = 0; i < usedVariablesCount; ++i, ++j) { diff --git a/Source/JavaScriptCore/parser/SyntaxChecker.h b/Source/JavaScriptCore/parser/SyntaxChecker.h index 0760c19eb..0328b1273 100644 --- a/Source/JavaScriptCore/parser/SyntaxChecker.h +++ b/Source/JavaScriptCore/parser/SyntaxChecker.h @@ -27,7 +27,6 @@ #define SyntaxChecker_h #include "Lexer.h" -#include "ParserFunctionInfo.h" #include "YarrSyntaxChecker.h" namespace JSC { @@ -69,25 +68,14 @@ public: { } + typedef SyntaxChecker FunctionBodyBuilder; enum { NoneExpr = 0, - ResolveEvalExpr, ResolveExpr, IntegerExpr, DoubleExpr, StringExpr, + ResolveEvalExpr, ResolveExpr, NumberExpr, StringExpr, ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr, - FunctionExpr, ClassExpr, SuperExpr, BracketExpr, DotExpr, CallExpr, + FunctionExpr, BracketExpr, DotExpr, CallExpr, NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr, - ConditionalExpr, AssignmentExpr, TypeofExpr, NewTargetExpr, - DeleteExpr, ArrayLiteralExpr, BindingDestructuring, - ArrayDestructuring, ObjectDestructuring, SourceElementsResult, - FunctionBodyResult, SpreadExpr, ArgumentsResult, - PropertyListResult, ArgumentsListResult, ElementsListResult, - StatementResult, FormalParameterListResult, ClauseResult, - ClauseListResult, CommaExpr, DestructuringAssignment, - TemplateStringResult, TemplateStringListResult, - TemplateExpressionListResult, TemplateExpr, - TaggedTemplateExpr, - ModuleNameResult, - ImportSpecifierResult, ImportSpecifierListResult, - ExportSpecifierResult, ExportSpecifierListResult - }; + ConditionalExpr, AssignmentExpr, TypeofExpr, + DeleteExpr, ArrayLiteralExpr }; typedef int ExpressionType; typedef ExpressionType Expression; @@ -116,29 +104,16 @@ public: typedef int PropertyList; typedef int ElementList; typedef int ArgumentsList; -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - typedef int TemplateExpressionList; - typedef int TemplateString; - typedef int TemplateStringList; - typedef int TemplateLiteral; -#endif typedef int FormalParameterList; typedef int FunctionBody; -#if ENABLE(ES6_CLASS_SYNTAX) - typedef int ClassExpression; -#endif - typedef int ModuleName; - typedef int ImportSpecifier; - typedef int ImportSpecifierList; - typedef int ExportSpecifier; - typedef int ExportSpecifierList; typedef int Statement; typedef int ClauseList; typedef int Clause; + typedef int ConstDeclList; typedef int BinaryOperand; - typedef int DestructuringPattern; - typedef DestructuringPattern ArrayPattern; - typedef DestructuringPattern ObjectPattern; + typedef int DeconstructionPattern; + typedef int ArrayPattern; + typedef int ObjectPattern; static const bool CreatesAST = false; static const bool NeedsFreeVariableInfo = false; @@ -146,10 +121,10 @@ public: static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords; static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings; - int createSourceElements() { return SourceElementsResult; } + int createSourceElements() { return 1; } ExpressionType makeFunctionCallNode(const JSTokenLocation&, int, int, int, int, int) { return CallExpr; } - ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType expr) { return expr; } - ExpressionType appendToCommaExpr(const JSTokenLocation&, ExpressionType& head, ExpressionType, ExpressionType next) { head = next; return next; } + void appendToComma(ExpressionType& base, ExpressionType right) { base = right; } + ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType, ExpressionType right) { return right; } ExpressionType makeAssignNode(const JSTokenLocation&, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int) { return AssignmentExpr; } ExpressionType makePrefixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PreExpr; } ExpressionType makePostfixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PostExpr; } @@ -160,16 +135,13 @@ public: ExpressionType createLogicalNot(const JSTokenLocation&, ExpressionType) { return UnaryExpr; } ExpressionType createUnaryPlus(const JSTokenLocation&, ExpressionType) { return UnaryExpr; } ExpressionType createVoid(const JSTokenLocation&, ExpressionType) { return UnaryExpr; } - ExpressionType createThisExpr(const JSTokenLocation&, ThisTDZMode) { return ThisExpr; } - ExpressionType createSuperExpr(const JSTokenLocation&) { return SuperExpr; } - ExpressionType createNewTargetExpr(const JSTokenLocation&) { return NewTargetExpr; } + ExpressionType thisExpr(const JSTokenLocation&) { return ThisExpr; } ExpressionType createResolve(const JSTokenLocation&, const Identifier*, int) { return ResolveExpr; } ExpressionType createObjectLiteral(const JSTokenLocation&) { return ObjectLiteralExpr; } ExpressionType createObjectLiteral(const JSTokenLocation&, int) { return ObjectLiteralExpr; } ExpressionType createArray(const JSTokenLocation&, int) { return ArrayLiteralExpr; } ExpressionType createArray(const JSTokenLocation&, int, int) { return ArrayLiteralExpr; } - ExpressionType createDoubleExpr(const JSTokenLocation&, double) { return DoubleExpr; } - ExpressionType createIntegerExpr(const JSTokenLocation&, double) { return IntegerExpr; } + ExpressionType createNumberExpr(const JSTokenLocation&, double) { return NumberExpr; } ExpressionType createString(const JSTokenLocation&, const Identifier*) { return StringExpr; } ExpressionType createBoolean(const JSTokenLocation&, bool) { return BoolExpr; } ExpressionType createNull(const JSTokenLocation&) { return NullExpr; } @@ -179,116 +151,84 @@ public: ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int) { return NewExpr; } ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int) { return NewExpr; } ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; } - ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int, AssignmentContext) { return AssignmentExpr; } - ExpressionType createEmptyVarExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; } - ExpressionType createEmptyLetExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; } -#if ENABLE(ES6_CLASS_SYNTAX) - ClassExpression createClassExpr(const JSTokenLocation&, const Identifier&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; } -#endif - ExpressionType createFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; } - int createFunctionMetadata(const JSTokenLocation&, const JSTokenLocation&, int, int, bool, int, int, int, ConstructorKind, unsigned, SourceParseMode) { return FunctionBodyResult; } - ExpressionType createArrowFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return FunctionExpr; } + ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int) { return AssignmentExpr; } + ExpressionType createFunctionExpr(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return FunctionExpr; } + int createFunctionBody(const JSTokenLocation&, const JSTokenLocation&, int, int, bool) { return 1; } void setFunctionNameStart(int, int) { } - int createArguments() { return ArgumentsResult; } - int createArguments(int) { return ArgumentsResult; } - ExpressionType createSpreadExpression(const JSTokenLocation&, ExpressionType, int, int, int) { return SpreadExpr; } -#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX) - TemplateString createTemplateString(const JSTokenLocation&, const Identifier&, const Identifier&) { return TemplateStringResult; } - TemplateStringList createTemplateStringList(TemplateString) { return TemplateStringListResult; } - TemplateStringList createTemplateStringList(TemplateStringList, TemplateString) { return TemplateStringListResult; } - TemplateExpressionList createTemplateExpressionList(Expression) { return TemplateExpressionListResult; } - TemplateExpressionList createTemplateExpressionList(TemplateExpressionList, Expression) { return TemplateExpressionListResult; } - TemplateLiteral createTemplateLiteral(const JSTokenLocation&, TemplateStringList) { return TemplateExpr; } - TemplateLiteral createTemplateLiteral(const JSTokenLocation&, TemplateStringList, TemplateExpressionList) { return TemplateExpr; } - ExpressionType createTaggedTemplate(const JSTokenLocation&, ExpressionType, TemplateLiteral, int, int, int) { return TaggedTemplateExpr; } -#endif - - int createArgumentsList(const JSTokenLocation&, int) { return ArgumentsListResult; } - int createArgumentsList(const JSTokenLocation&, int, int) { return ArgumentsListResult; } - Property createProperty(const Identifier* name, int, PropertyNode::Type type, PropertyNode::PutType, bool complete, SuperBinding = SuperBinding::NotNeeded) + int createArguments() { return 1; } + int createArguments(int) { return 1; } + ExpressionType createSpreadExpression(const JSTokenLocation&, ExpressionType, int, int, int) { return 1; } + int createArgumentsList(const JSTokenLocation&, int) { return 1; } + int createArgumentsList(const JSTokenLocation&, int, int) { return 1; } + Property createProperty(const Identifier* name, int, PropertyNode::Type type, bool complete) { if (!complete) return Property(type); ASSERT(name); return Property(name, type); } - Property createProperty(VM* vm, ParserArena& parserArena, double name, int, PropertyNode::Type type, PropertyNode::PutType, bool complete) + Property createProperty(VM* vm, double name, int, PropertyNode::Type type, bool complete) { if (!complete) return Property(type); - return Property(&parserArena.identifierArena().makeNumericIdentifier(vm, name), type); + return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type); } - Property createProperty(int, int, PropertyNode::Type type, PropertyNode::PutType, bool) + Property createProperty(VM*, ExpressionNode*, int, PropertyNode::Type type, bool) { return Property(type); } - int createPropertyList(const JSTokenLocation&, Property) { return PropertyListResult; } - int createPropertyList(const JSTokenLocation&, Property, int) { return PropertyListResult; } - int createElementList(int, int) { return ElementsListResult; } - int createElementList(int, int, int) { return ElementsListResult; } - int createFormalParameterList() { return FormalParameterListResult; } - void appendParameter(int, DestructuringPattern, int) { } - int createClause(int, int) { return ClauseResult; } - int createClauseList(int) { return ClauseListResult; } - int createClauseList(int, int) { return ClauseListResult; } - int createFuncDeclStatement(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&) { return StatementResult; } -#if ENABLE(ES6_CLASS_SYNTAX) - int createClassDeclStatement(const JSTokenLocation&, ClassExpression, - const JSTextPosition&, const JSTextPosition&, int, int) { return StatementResult; } -#endif - int createBlockStatement(const JSTokenLocation&, int, int, int, VariableEnvironment&) { return StatementResult; } - int createExprStatement(const JSTokenLocation&, int, int, int) { return StatementResult; } - int createIfStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; } - int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return StatementResult; } - int createForLoop(const JSTokenLocation&, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; } - int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; } - int createForOfLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; } - int createEmptyStatement(const JSTokenLocation&) { return StatementResult; } - int createDeclarationStatement(const JSTokenLocation&, int, int, int) { return StatementResult; } - int createReturnStatement(const JSTokenLocation&, int, int, int) { return StatementResult; } - int createBreakStatement(const JSTokenLocation&, int, int) { return StatementResult; } - int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int) { return StatementResult; } - int createContinueStatement(const JSTokenLocation&, int, int) { return StatementResult; } - int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int) { return StatementResult; } - int createTryStatement(const JSTokenLocation&, int, const Identifier*, int, int, int, int, VariableEnvironment&) { return StatementResult; } - int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int, VariableEnvironment&) { return StatementResult; } - int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; } - int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int) { return StatementResult; } - int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return StatementResult; } - int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int) { return StatementResult; } - int createThrowStatement(const JSTokenLocation&, int, int, int) { return StatementResult; } - int createDebugger(const JSTokenLocation&, int, int) { return StatementResult; } - int createConstStatement(const JSTokenLocation&, int, int, int) { return StatementResult; } - int createModuleName(const JSTokenLocation&, const Identifier&) { return ModuleNameResult; } - ImportSpecifier createImportSpecifier(const JSTokenLocation&, const Identifier&, const Identifier&) { return ImportSpecifierResult; } - ImportSpecifierList createImportSpecifierList() { return ImportSpecifierListResult; } - void appendImportSpecifier(ImportSpecifierList, ImportSpecifier) { } - int createImportDeclaration(const JSTokenLocation&, ImportSpecifierList, ModuleName) { return StatementResult; } - int createExportAllDeclaration(const JSTokenLocation&, ModuleName) { return StatementResult; } - int createExportDefaultDeclaration(const JSTokenLocation&, int, const Identifier&) { return StatementResult; } - int createExportLocalDeclaration(const JSTokenLocation&, int) { return StatementResult; } - int createExportNamedDeclaration(const JSTokenLocation&, ExportSpecifierList, ModuleName) { return StatementResult; } - ExportSpecifier createExportSpecifier(const JSTokenLocation&, const Identifier&, const Identifier&) { return ExportSpecifierResult; } - ExportSpecifierList createExportSpecifierList() { return ExportSpecifierListResult; } - void appendExportSpecifier(ExportSpecifierList, ExportSpecifier) { } - - int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return StatementResult; } - Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, bool strict, const Identifier* name, const ParserFunctionInfo<SyntaxChecker>&, SuperBinding) + int createPropertyList(const JSTokenLocation&, Property) { return 1; } + int createPropertyList(const JSTokenLocation&, Property, int) { return 1; } + int createElementList(int, int) { return 1; } + int createElementList(int, int, int) { return 1; } + int createFormalParameterList(DeconstructionPattern) { return 1; } + int createFormalParameterList(int, DeconstructionPattern) { return 1; } + int createClause(int, int) { return 1; } + int createClauseList(int) { return 1; } + int createClauseList(int, int) { return 1; } + void setUsesArguments(int) { } + int createFuncDeclStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int) { return 1; } + int createBlockStatement(const JSTokenLocation&, int, int, int) { return 1; } + int createExprStatement(const JSTokenLocation&, int, int, int) { return 1; } + int createIfStatement(const JSTokenLocation&, int, int, int, int) { return 1; } + int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; } + int createForLoop(const JSTokenLocation&, int, int, int, int, int, int) { return 1; } + int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; } + int createForOfLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; } + int createEmptyStatement(const JSTokenLocation&) { return 1; } + int createVarStatement(const JSTokenLocation&, int, int, int) { return 1; } + int createReturnStatement(const JSTokenLocation&, int, int, int) { return 1; } + int createBreakStatement(const JSTokenLocation&, int, int) { return 1; } + int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int) { return 1; } + int createContinueStatement(const JSTokenLocation&, int, int) { return 1; } + int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int) { return 1; } + int createTryStatement(const JSTokenLocation&, int, const Identifier*, int, int, int, int) { return 1; } + int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; } + int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; } + int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; } + int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; } + int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int) { return 1; } + int createThrowStatement(const JSTokenLocation&, int, int, int) { return 1; } + int createDebugger(const JSTokenLocation&, int, int) { return 1; } + int createConstStatement(const JSTokenLocation&, int, int, int) { return 1; } + int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return 1; } + Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, bool strict, const Identifier* name, int, int, int, int, int, int, int) { ASSERT(name); if (!strict) return Property(type); return Property(name, type); } - Property createGetterOrSetterProperty(VM* vm, ParserArena& parserArena, const JSTokenLocation&, PropertyNode::Type type, bool strict, double name, const ParserFunctionInfo<SyntaxChecker>&, SuperBinding) + Property createGetterOrSetterProperty(VM* vm, const JSTokenLocation&, PropertyNode::Type type, bool strict, double name, int, int, int, int, int, int, int) { if (!strict) return Property(type); - return Property(&parserArena.identifierArena().makeNumericIdentifier(vm, name), type); + return Property(&vm->parserArena->identifierArena().makeNumericIdentifier(vm, name), type); } void appendStatement(int, int) { } - int combineCommaNodes(const JSTokenLocation&, int, int) { return CommaExpr; } + void addVar(const Identifier*, bool) { } + int combineCommaNodes(const JSTokenLocation&, int, int) { return 1; } int evalCount() const { return 0; } void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, bool) { @@ -314,52 +254,36 @@ public: void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; } void assignmentStackAppend(int, int, int, int, int, Operator) { } - int createAssignment(const JSTokenLocation&, int, int, int, int, int) { RELEASE_ASSERT_NOT_REACHED(); return AssignmentExpr; } - const Identifier* getName(const Property& property) const { return property.name; } + int createAssignment(const JSTokenLocation&, int, int, int, int, int) { RELEASE_ASSERT_NOT_REACHED(); return 1; } + const Identifier* getName(const Property& property) const { ASSERT(property.name); return property.name; } PropertyNode::Type getType(const Property& property) const { return property.type; } bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; } - ExpressionType createDestructuringAssignment(const JSTokenLocation&, int, ExpressionType) + ExpressionType createDeconstructingAssignment(const JSTokenLocation&, int, ExpressionType) { - return DestructuringAssignment; + return 1; } ArrayPattern createArrayPattern(const JSTokenLocation&) { - return ArrayDestructuring; + return 1; } void appendArrayPatternSkipEntry(ArrayPattern, const JSTokenLocation&) { } - void appendArrayPatternEntry(ArrayPattern, const JSTokenLocation&, DestructuringPattern, int) - { - } - void appendArrayPatternRestEntry(ArrayPattern, const JSTokenLocation&, DestructuringPattern) - { - } - void finishArrayPattern(ArrayPattern, const JSTextPosition&, const JSTextPosition&, const JSTextPosition&) + void appendArrayPatternEntry(ArrayPattern, const JSTokenLocation&, DeconstructionPattern) { } ObjectPattern createObjectPattern(const JSTokenLocation&) { - return ObjectDestructuring; - } - void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, bool, const Identifier&, DestructuringPattern, int) - { + return 1; } - DestructuringPattern createBindingLocation(const JSTokenLocation&, const Identifier&, const JSTextPosition&, const JSTextPosition&, AssignmentContext) + void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, bool, const Identifier&, DeconstructionPattern) { - return BindingDestructuring; } - - bool isBindingNode(DestructuringPattern pattern) + DeconstructionPattern createBindingLocation(const JSTokenLocation&, const Identifier&, const JSTextPosition&, const JSTextPosition&) { - return pattern == BindingDestructuring; + return 1; } - - void setEndOffset(int, int) { } - int endOffset(int) { return 0; } - void setStartOffset(int, int) { } - private: int m_topBinaryExpr; int m_topUnaryToken; diff --git a/Source/JavaScriptCore/parser/VariableEnvironment.cpp b/Source/JavaScriptCore/parser/VariableEnvironment.cpp deleted file mode 100644 index ed055f98a..000000000 --- a/Source/JavaScriptCore/parser/VariableEnvironment.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "VariableEnvironment.h" - -namespace JSC { - -void VariableEnvironment::markVariableAsCapturedIfDefined(const RefPtr<UniquedStringImpl>& identifier) -{ - auto findResult = m_map.find(identifier); - if (findResult != m_map.end()) - findResult->value.setIsCaptured(); -} - -void VariableEnvironment::markVariableAsCaptured(const RefPtr<UniquedStringImpl>& identifier) -{ - auto findResult = m_map.find(identifier); - RELEASE_ASSERT(findResult != m_map.end()); - findResult->value.setIsCaptured(); -} - -void VariableEnvironment::markAllVariablesAsCaptured() -{ - if (m_isEverythingCaptured) - return; - - m_isEverythingCaptured = true; // For fast queries. - // We must mark every entry as captured for when we iterate through m_map and entry.isCaptured() is called. - for (auto iter = m_map.begin(), end = m_map.end(); iter != end; ++iter) - iter->value.setIsCaptured(); -} - -bool VariableEnvironment::hasCapturedVariables() const -{ - if (m_isEverythingCaptured) - return size() > 0; - for (auto entry : m_map) { - if (entry.value.isCaptured()) - return true; - } - return false; -} - -bool VariableEnvironment::captures(UniquedStringImpl* identifier) const -{ - if (m_isEverythingCaptured) - return true; - - auto findResult = m_map.find(identifier); - if (findResult == m_map.end()) - return false; - return findResult->value.isCaptured(); -} - -void VariableEnvironment::swap(VariableEnvironment& other) -{ - m_map.swap(other.m_map); - m_isEverythingCaptured = other.m_isEverythingCaptured; -} - -void VariableEnvironment::markVariableAsImported(const RefPtr<UniquedStringImpl>& identifier) -{ - auto findResult = m_map.find(identifier); - RELEASE_ASSERT(findResult != m_map.end()); - findResult->value.setIsImported(); -} - -void VariableEnvironment::markVariableAsExported(const RefPtr<UniquedStringImpl>& identifier) -{ - auto findResult = m_map.find(identifier); - RELEASE_ASSERT(findResult != m_map.end()); - findResult->value.setIsExported(); -} - -} // namespace JSC diff --git a/Source/JavaScriptCore/parser/VariableEnvironment.h b/Source/JavaScriptCore/parser/VariableEnvironment.h deleted file mode 100644 index 5ed2a6bda..000000000 --- a/Source/JavaScriptCore/parser/VariableEnvironment.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2015 Apple Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef VariableEnvironment_h -#define VariableEnvironment_h - -#include "Identifier.h" -#include <wtf/HashMap.h> -#include <wtf/text/UniquedStringImpl.h> - -namespace JSC { - -struct VariableEnvironmentEntry { -public: - ALWAYS_INLINE bool isCaptured() const { return m_bits & IsCaptured; } - ALWAYS_INLINE bool isConst() const { return m_bits & IsConst; } - ALWAYS_INLINE bool isVar() const { return m_bits & IsVar; } - ALWAYS_INLINE bool isLet() const { return m_bits & IsLet; } - ALWAYS_INLINE bool isExported() const { return m_bits & IsExported; } - ALWAYS_INLINE bool isImported() const { return m_bits & IsImported; } - - ALWAYS_INLINE void setIsCaptured() { m_bits |= IsCaptured; } - ALWAYS_INLINE void setIsConst() { m_bits |= IsConst; } - ALWAYS_INLINE void setIsVar() { m_bits |= IsVar; } - ALWAYS_INLINE void setIsLet() { m_bits |= IsLet; } - ALWAYS_INLINE void setIsExported() { m_bits |= IsExported; } - ALWAYS_INLINE void setIsImported() { m_bits |= IsImported; } - - ALWAYS_INLINE void clearIsVar() { m_bits &= ~IsVar; } - -private: - enum Traits { - IsCaptured = 1 << 0, - IsConst = 1 << 1, - IsVar = 1 << 2, - IsLet = 1 << 3, - IsExported = 1 << 4, - IsImported = 1 << 5 - }; - uint8_t m_bits { 0 }; -}; - -struct VariableEnvironmentEntryHashTraits : HashTraits<VariableEnvironmentEntry> { - static const bool needsDestruction = false; -}; - -class VariableEnvironment { -private: - typedef HashMap<RefPtr<UniquedStringImpl>, VariableEnvironmentEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, VariableEnvironmentEntryHashTraits> Map; -public: - ALWAYS_INLINE Map::iterator begin() { return m_map.begin(); } - ALWAYS_INLINE Map::iterator end() { return m_map.end(); } - ALWAYS_INLINE Map::const_iterator begin() const { return m_map.begin(); } - ALWAYS_INLINE Map::const_iterator end() const { return m_map.end(); } - ALWAYS_INLINE Map::AddResult add(const RefPtr<UniquedStringImpl>& identifier) { return m_map.add(identifier, VariableEnvironmentEntry()); } - ALWAYS_INLINE Map::AddResult add(const Identifier& identifier) { return add(identifier.impl()); } - ALWAYS_INLINE unsigned size() const { return m_map.size(); } - ALWAYS_INLINE bool contains(const RefPtr<UniquedStringImpl>& identifier) const { return m_map.contains(identifier); } - ALWAYS_INLINE bool remove(const RefPtr<UniquedStringImpl>& identifier) { return m_map.remove(identifier); } - void swap(VariableEnvironment& other); - void markVariableAsCapturedIfDefined(const RefPtr<UniquedStringImpl>& identifier); - void markVariableAsCaptured(const RefPtr<UniquedStringImpl>& identifier); - void markAllVariablesAsCaptured(); - bool hasCapturedVariables() const; - bool captures(UniquedStringImpl* identifier) const; - void markVariableAsImported(const RefPtr<UniquedStringImpl>& identifier); - void markVariableAsExported(const RefPtr<UniquedStringImpl>& identifier); - -private: - Map m_map; - bool m_isEverythingCaptured { false }; -}; - -} // namespace JSC - -#endif // VariableEnvironment_h |