Ignore:
Timestamp:
Dec 10, 2013, 1:19:15 PM (12 years ago)
Author:
[email protected]
Message:

Reduce the mass templatizing of the JS parser
https://bugs.webkit.org/show_bug.cgi?id=125535

Reviewed by Michael Saboff.

The various caches we have now have removed the need for many of
the template vs. regular parameters. This patch converts those
template parameters to regular parameters and updates the call
sites. This reduces the code size of the parser by around 15%.

  • parser/ASTBuilder.h:

(JSC::ASTBuilder::createGetterOrSetterProperty):
(JSC::ASTBuilder::createProperty):

  • parser/Parser.cpp:

(JSC::::parseInner):
(JSC::::parseSourceElements):
(JSC::::parseVarDeclarationList):
(JSC::::createBindingPattern):
(JSC::::tryParseDeconstructionPatternExpression):
(JSC::::parseDeconstructionPattern):
(JSC::::parseSwitchClauses):
(JSC::::parseSwitchDefaultClause):
(JSC::::parseBlockStatement):
(JSC::::parseFormalParameters):
(JSC::::parseFunctionInfo):
(JSC::::parseFunctionDeclaration):
(JSC::::parseProperty):
(JSC::::parseObjectLiteral):
(JSC::::parseStrictObjectLiteral):
(JSC::::parseMemberExpression):

  • parser/Parser.h:
  • parser/SyntaxChecker.h:

(JSC::SyntaxChecker::createProperty):
(JSC::SyntaxChecker::createGetterOrSetterProperty):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/parser/Parser.cpp

    r159790 r160383  
    246246        m_statementDepth--;
    247247    ScopeRef scope = currentScope();
    248     SourceElements* sourceElements = parseSourceElements<CheckForStrictMode>(context);
     248    SourceElements* sourceElements = parseSourceElements(context, CheckForStrictMode);
    249249    if (!sourceElements || !consume(EOFTOK)) {
    250250        if (hasError())
     
    290290
    291291template <typename LexerType>
    292 template <SourceElementsMode mode, class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceElements(TreeBuilder& context)
     292template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceElements(TreeBuilder& context, SourceElementsMode mode)
    293293{
    294294    const unsigned lengthOfUseStrictLiteral = 12; // "use strict".length
     
    447447        } else {
    448448            lastIdent = 0;
    449             auto pattern = parseDeconstructionPattern<DeconstructToVariables>(context);
     449            auto pattern = parseDeconstructionPattern(context, DeconstructToVariables);
    450450            failIfFalse(pattern, "Cannot parse this deconstruction pattern");
    451451            hasInitializer = match(EQUAL);
     
    466466    } while (match(COMMA));
    467467    if (lastIdent)
    468         lastPattern = createBindingPattern<DeconstructToVariables>(context, *lastIdent, 0);
     468        lastPattern = createBindingPattern(context, DeconstructToVariables, *lastIdent, 0);
    469469    return varDecls;
    470470}
    471471
    472472template <typename LexerType>
    473 template <DeconstructionKind kind, class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, const Identifier& name, int depth)
     473template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DeconstructionKind kind, const Identifier& name, int depth)
    474474{
    475475    ASSERT(!name.isEmpty());
     
    525525template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::tryParseDeconstructionPatternExpression(TreeBuilder& context)
    526526{
    527     return parseDeconstructionPattern<DeconstructToExpressions>(context);
    528 }
    529 
    530 template <typename LexerType>
    531 template <DeconstructionKind kind, class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::parseDeconstructionPattern(TreeBuilder& context, int depth)
     527    return parseDeconstructionPattern(context, DeconstructToExpressions);
     528}
     529
     530template <typename LexerType>
     531template <class TreeBuilder> TreeDeconstructionPattern Parser<LexerType>::parseDeconstructionPattern(TreeBuilder& context, DeconstructionKind kind, int depth)
    532532{
    533533    failIfStackOverflow();
     
    548548            propagateError();
    549549            JSTokenLocation location = m_token.m_location;
    550             auto innerPattern = parseDeconstructionPattern<kind>(context, depth + 1);
     550            auto innerPattern = parseDeconstructionPattern(context, kind, depth + 1);
    551551            if (kind == DeconstructToExpressions && !innerPattern)
    552552                return 0;
     
    579579                next();
    580580                if (consume(COLON))
    581                     innerPattern = parseDeconstructionPattern<kind>(context, depth + 1);
     581                    innerPattern = parseDeconstructionPattern(context, kind, depth + 1);
    582582                else
    583                     innerPattern = createBindingPattern<kind>(context, propertyName, depth);
     583                    innerPattern = createBindingPattern(context, kind, propertyName, depth);
    584584            } else {
    585585                JSTokenType tokenType = m_token.m_type;
     
    611611                    failWithMessage("Expected a ':' prior to named property deconstruction");
    612612                }
    613                 innerPattern = parseDeconstructionPattern<kind>(context, depth + 1);
     613                innerPattern = parseDeconstructionPattern(context, kind, depth + 1);
    614614            }
    615615            if (kind == DeconstructToExpressions && !innerPattern)
     
    632632            failWithMessage("Expected a parameter pattern or a ')' in parameter list");
    633633        }
    634         pattern = createBindingPattern<kind>(context, *m_token.m_data.ident, depth);
     634        pattern = createBindingPattern(context, kind, *m_token.m_data.ident, depth);
    635635        next();
    636636        break;
     
    962962    failIfFalse(condition, "Cannot parse switch clause");
    963963    consumeOrFail(COLON, "Expected a ':' after switch clause expression");
    964     TreeSourceElements statements = parseSourceElements<DontCheckForStrictMode>(context);
     964    TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
    965965    failIfFalse(statements, "Cannot parse the body of a switch clause");
    966966    TreeClause clause = context.createClause(condition, statements);
     
    973973        failIfFalse(condition, "Cannot parse switch case expression");
    974974        consumeOrFail(COLON, "Expected a ':' after switch clause expression");
    975         TreeSourceElements statements = parseSourceElements<DontCheckForStrictMode>(context);
     975        TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
    976976        failIfFalse(statements, "Cannot parse the body of a switch clause");
    977977        clause = context.createClause(condition, statements);
     
    988988    next();
    989989    consumeOrFail(COLON, "Expected a ':' after switch default clause");
    990     TreeSourceElements statements = parseSourceElements<DontCheckForStrictMode>(context);
     990    TreeSourceElements statements = parseSourceElements(context, DontCheckForStrictMode);
    991991    failIfFalse(statements, "Cannot parse the body of a switch default clause");
    992992    return context.createClause(0, statements);
     
    10661066        return context.createBlockStatement(location, 0, start, m_lastTokenEndPosition.line);
    10671067    }
    1068     TreeSourceElements subtree = parseSourceElements<DontCheckForStrictMode>(context);
     1068    TreeSourceElements subtree = parseSourceElements(context, DontCheckForStrictMode);
    10691069    failIfFalse(subtree, "Cannot parse the body of the block statement");
    10701070    matchOrFail(CLOSEBRACE, "Expected a closing '}' at the end of a block statement");
     
    11441144template <class TreeBuilder> TreeFormalParameterList Parser<LexerType>::parseFormalParameters(TreeBuilder& context)
    11451145{
    1146     auto parameter = parseDeconstructionPattern<DeconstructToParameters>(context);
     1146    auto parameter = parseDeconstructionPattern(context, DeconstructToParameters);
    11471147    failIfFalse(parameter, "Cannot parse parameter pattern");
    11481148    TreeFormalParameterList list = context.createFormalParameterList(parameter);
    11491149    TreeFormalParameterList tail = list;
    11501150    while (consume(COMMA)) {
    1151         parameter = parseDeconstructionPattern<DeconstructToParameters>(context);
     1151        parameter = parseDeconstructionPattern(context, DeconstructToParameters);
    11521152        failIfFalse(parameter, "Cannot parse parameter pattern");
    11531153        tail = context.createFormalParameterList(tail, parameter);
     
    11701170    m_statementDepth = 0;
    11711171    typename TreeBuilder::FunctionBodyBuilder bodyBuilder(const_cast<VM*>(m_vm), m_lexer.get());
    1172     failIfFalse(parseSourceElements<CheckForStrictMode>(bodyBuilder), "Cannot parse body of this function");
     1172    failIfFalse(parseSourceElements(bodyBuilder, CheckForStrictMode), "Cannot parse body of this function");
    11731173    unsigned endColumn = tokenColumn();
    11741174    return context.createFunctionBody(startLocation, tokenLocation(), startColumn, endColumn, strictMode());
     
    11901190
    11911191template <typename LexerType>
    1192 template <FunctionRequirements requirements, FunctionParseMode mode, bool nameIsInContainingScope, class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, const Identifier*& name, TreeFormalParameterList& parameters, TreeFunctionBody& body, unsigned& openBraceOffset, unsigned& closeBraceOffset, int& bodyStartLine, unsigned& bodyStartColumn)
     1192template <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)
    11931193{
    11941194    AutoPopScopeRef functionScope(this, pushScope());
     
    13121312    int bodyStartLine = 0;
    13131313    unsigned bodyStartColumn = 0;
    1314     failIfFalse((parseFunctionInfo<FunctionNeedsName, FunctionMode, true>(context, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse this function");
     1314    failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, true, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse this function");
    13151315    failIfFalse(name, "Function statements must have a name");
    13161316    failIfFalseIfStrict(declareVariable(name), "Cannot declare a function named '", name->impl(), "' in strict mode");
     
    16741674
    16751675template <typename LexerType>
    1676 template <bool complete, class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeBuilder& context)
     1676template <class TreeBuilder> TreeProperty Parser<LexerType>::parseProperty(TreeBuilder& context, bool complete)
    16771677{
    16781678    bool wasIdent = false;
     
    16921692            TreeExpression node = parseAssignmentExpression(context);
    16931693            failIfFalse(node, "Cannot parse expression for property declaration");
    1694             return context.template createProperty<complete>(ident, node, PropertyNode::Constant);
     1694            return context.createProperty(ident, node, PropertyNode::Constant, complete);
    16951695        }
    16961696        failIfFalse(wasIdent, "Expected an identifier as property name");
     
    17211721        if (type == PropertyNode::Getter) {
    17221722            failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition");
    1723             failIfFalse((parseFunctionInfo<FunctionNoRequirements, GetterMode, false>(context, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse getter definition");
     1723            failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse getter definition");
    17241724        } else {
    17251725            failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition");
    1726             failIfFalse((parseFunctionInfo<FunctionNoRequirements, SetterMode, false>(context, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse setter definition");
     1726            failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse setter definition");
    17271727        }
    17281728        if (stringPropertyName)
    1729             return context.template createGetterOrSetterProperty<complete>(location, type, stringPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn);
    1730         return context.template createGetterOrSetterProperty<complete>(const_cast<VM*>(m_vm), location, type, numericPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn);
     1729            return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn);
     1730        return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), location, type, complete, numericPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn);
    17311731    }
    17321732    case NUMBER: {
     
    17361736        TreeExpression node = parseAssignmentExpression(context);
    17371737        failIfFalse(node, "Cannot parse expression for property declaration");
    1738         return context.template createProperty<complete>(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant);
     1738        return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete);
    17391739    }
    17401740    case OPENBRACKET: {
     
    17471747        TreeExpression node = parseAssignmentExpression(context);
    17481748        failIfFalse(node, "Cannot parse expression for property declaration");
    1749         return context.template createProperty<complete>(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant);
     1749        return context.createProperty(const_cast<VM*>(m_vm), propertyName, node, PropertyNode::Constant, complete);
    17501750    }
    17511751    default:
     
    17691769    }
    17701770   
    1771     TreeProperty property = parseProperty<false>(context);
     1771    TreeProperty property = parseProperty(context, false);
    17721772    failIfFalse(property, "Cannot parse object literal property");
    17731773    if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) {
     
    17831783            break;
    17841784        JSTokenLocation propertyLocation(tokenLocation());
    1785         property = parseProperty<false>(context);
     1785        property = parseProperty(context, false);
    17861786        failIfFalse(property, "Cannot parse object literal property");
    17871787        if (!m_syntaxAlreadyValidated && context.getType(property) != PropertyNode::Constant) {
     
    18131813    }
    18141814   
    1815     TreeProperty property = parseProperty<true>(context);
     1815    TreeProperty property = parseProperty(context, true);
    18161816    failIfFalse(property, "Cannot parse object literal property");
    18171817   
     
    18301830            break;
    18311831        JSTokenLocation propertyLocation(tokenLocation());
    1832         property = parseProperty<true>(context);
     1832        property = parseProperty(context, true);
    18331833        failIfFalse(property, "Cannot parse object literal property");
    18341834        if (!m_syntaxAlreadyValidated && context.getName(property)) {
     
    20772077        location = tokenLocation();
    20782078        next();
    2079         failIfFalse((parseFunctionInfo<FunctionNoRequirements, FunctionMode, false>(context, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse function expression");
     2079        failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse function expression");
    20802080        base = context.createFunctionExpr(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn);
    20812081    } else
Note: See TracChangeset for help on using the changeset viewer.