Ignore:
Timestamp:
Jan 21, 2015, 10:39:31 PM (10 years ago)
Author:
[email protected]
Message:

Consolidate out arguments of parseFunctionInfo into a struct
https://bugs.webkit.org/show_bug.cgi?id=140754

Reviewed by Oliver Hunt.

Introduced ParserFunctionInfo for storing out arguments of parseFunctionInfo.

(JSC::ASTBuilder::createFunctionExpr):
(JSC::ASTBuilder::createGetterOrSetterProperty): This one takes a property name in addition to
ParserFunctionInfo since the property name and the function name could differ.
(JSC::ASTBuilder::createFuncDeclStatement):

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::parseFunctionInfo):
(JSC::Parser<LexerType>::parseFunctionDeclaration):
(JSC::Parser<LexerType>::parseProperty):
(JSC::Parser<LexerType>::parseMemberExpression):

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

(JSC::SyntaxChecker::createFunctionExpr):
(JSC::SyntaxChecker::createFuncDeclStatement):
(JSC::SyntaxChecker::createClassDeclStatement):
(JSC::SyntaxChecker::createGetterOrSetterProperty):

File:
1 edited

Legend:

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

    r178692 r178888  
    12741274
    12751275template <typename LexerType>
    1276 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)
     1276template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, FunctionParseMode mode, bool nameIsInContainingScope, ParserFunctionInfo<TreeBuilder>& info)
    12771277{
    12781278    AutoPopScopeRef functionScope(this, pushScope());
     
    12821282    m_lastFunctionName = nullptr;
    12831283    if (match(IDENT)) {
    1284         name = m_token.m_data.ident;
    1285         m_lastFunctionName = name;
     1284        info.name = m_token.m_data.ident;
     1285        m_lastFunctionName = info.name;
    12861286        next();
    12871287        if (!nameIsInContainingScope)
    1288             failIfFalseIfStrict(functionScope->declareVariable(name), "'", name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode");
     1288            failIfFalseIfStrict(functionScope->declareVariable(info.name), "'", info.name->impl(), "' is not a valid ", stringForFunctionMode(mode), " name in strict mode");
    12891289    } else if (requirements == FunctionNeedsName) {
    12901290        if (match(OPENPAREN) && mode == FunctionMode)
     
    12991299    }
    13001300    if (!match(CLOSEPAREN)) {
    1301         parameters = parseFormalParameters(context);
    1302         failIfFalse(parameters, "Cannot parse parameters for this ", stringForFunctionMode(mode));
     1301        info.parameters = parseFormalParameters(context);
     1302        failIfFalse(info.parameters, "Cannot parse parameters for this ", stringForFunctionMode(mode));
    13031303    }
    13041304    consumeOrFail(CLOSEPAREN, "Expected a ')' or a ',' after a parameter declaration");
    13051305    matchOrFail(OPENBRACE, "Expected an opening '{' at the start of a ", stringForFunctionMode(mode), " body");
    13061306   
    1307     openBraceOffset = m_token.m_data.offset;
    1308     bodyStartLine = tokenLine();
    1309     bodyStartColumn = m_token.m_data.offset - m_token.m_data.lineStartOffset;
     1307    info.openBraceOffset = m_token.m_data.offset;
     1308    info.bodyStartLine = tokenLine();
     1309    info.bodyStartColumn = m_token.m_data.offset - m_token.m_data.lineStartOffset;
    13101310    JSTokenLocation startLocation(tokenLocation());
    13111311   
    13121312    // If we know about this function already, we can use the cached info and skip the parser to the end of the function.
    1313     if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(openBraceOffset) : 0) {
     1313    if (const SourceProviderCacheItem* cachedInfo = TreeBuilder::CanUseFunctionCache ? findCachedFunctionInfo(info.openBraceOffset) : 0) {
    13141314        // If we're in a strict context, the cached function info must say it was strict too.
    13151315        ASSERT(!strictMode() || cachedInfo->strictMode);
     
    13201320        endLocation.lineStartOffset = cachedInfo->closeBraceLineStartOffset;
    13211321
    1322         bool endColumnIsOnStartLine = (endLocation.line == bodyStartLine);
     1322        bool endColumnIsOnStartLine = (endLocation.line == info.bodyStartLine);
    13231323        ASSERT(endLocation.startOffset >= endLocation.lineStartOffset);
    13241324        unsigned bodyEndColumn = endColumnIsOnStartLine ?
     
    13271327        unsigned currentLineStartOffset = m_token.m_location.lineStartOffset;
    13281328
    1329         body = context.createFunctionBody(startLocation, endLocation, bodyStartColumn, bodyEndColumn, cachedInfo->strictMode);
     1329        info.body = context.createFunctionBody(startLocation, endLocation, info.bodyStartColumn, bodyEndColumn, cachedInfo->strictMode);
    13301330       
    13311331        functionScope->restoreFromSourceProviderCache(cachedInfo);
    13321332        failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error");
    13331333       
    1334         closeBraceOffset = cachedInfo->closeBraceOffset;
    1335 
    1336         context.setFunctionNameStart(body, functionNameStart);
     1334        info.closeBraceOffset = cachedInfo->closeBraceOffset;
     1335
     1336        context.setFunctionNameStart(info.body, functionNameStart);
    13371337        m_token = cachedInfo->closeBraceToken();
    13381338        if (endColumnIsOnStartLine)
     
    13421342        m_lexer->setLineNumber(m_token.m_location.line);
    13431343
    1344         context.setEndOffset(body, m_lexer->currentOffset());
     1344        context.setEndOffset(info.body, m_lexer->currentOffset());
    13451345       
    13461346        next();
     1347        info.bodyEndLine = m_lastTokenEndPosition.line;
    13471348        return true;
    13481349    }
    13491350    m_lastFunctionName = lastFunctionName;
    13501351    ParserState oldState = saveState();
    1351     body = parseFunctionBody(context);
     1352    info.body = parseFunctionBody(context);
    13521353    restoreState(oldState);
    1353     failIfFalse(body, "Cannot parse the body of this ", stringForFunctionMode(mode));
    1354     context.setEndOffset(body, m_lexer->currentOffset());
    1355     if (functionScope->strictMode() && name) {
     1354    failIfFalse(info.body, "Cannot parse the body of this ", stringForFunctionMode(mode));
     1355    context.setEndOffset(info.body, m_lexer->currentOffset());
     1356    if (functionScope->strictMode() && info.name) {
    13561357        RELEASE_ASSERT(mode == FunctionMode);
    1357         semanticFailIfTrue(m_vm->propertyNames->arguments == *name, "'", name->impl(), "' is not a valid function name in strict mode");
    1358         semanticFailIfTrue(m_vm->propertyNames->eval == *name, "'", name->impl(), "' is not a valid function name in strict mode");
    1359     }
    1360     closeBraceOffset = m_token.m_data.offset;
     1358        semanticFailIfTrue(m_vm->propertyNames->arguments == *info.name, "'", info.name->impl(), "' is not a valid function name in strict mode");
     1359        semanticFailIfTrue(m_vm->propertyNames->eval == *info.name, "'", info.name->impl(), "' is not a valid function name in strict mode");
     1360    }
     1361    info.closeBraceOffset = m_token.m_data.offset;
    13611362    unsigned closeBraceLine = m_token.m_data.line;
    13621363    unsigned closeBraceLineStartOffset = m_token.m_data.lineStartOffset;
     
    13661367    static const int minimumFunctionLengthToCache = 16;
    13671368    std::unique_ptr<SourceProviderCacheItem> newInfo;
    1368     int functionLength = closeBraceOffset - openBraceOffset;
     1369    int functionLength = info.closeBraceOffset - info.openBraceOffset;
    13691370    if (TreeBuilder::CanUseFunctionCache && m_functionCache && functionLength > minimumFunctionLengthToCache) {
    13701371        SourceProviderCacheItemCreationParameters parameters;
    13711372        parameters.functionNameStart = functionNameStart;
    13721373        parameters.closeBraceLine = closeBraceLine;
    1373         parameters.closeBraceOffset = closeBraceOffset;
     1374        parameters.closeBraceOffset = info.closeBraceOffset;
    13741375        parameters.closeBraceLineStartOffset = closeBraceLineStartOffset;
    13751376        functionScope->fillParametersForSourceProviderCache(parameters);
     
    13771378
    13781379    }
    1379     context.setFunctionNameStart(body, functionNameStart);
     1380    context.setFunctionNameStart(info.body, functionNameStart);
    13801381   
    13811382    failIfFalse(popScope(functionScope, TreeBuilder::NeedsFreeVariableInfo), "Parser error");
     
    13831384   
    13841385    if (newInfo)
    1385         m_functionCache->add(openBraceOffset, WTF::move(newInfo));
     1386        m_functionCache->add(info.openBraceOffset, WTF::move(newInfo));
    13861387   
    13871388    next();
     1389    info.bodyEndLine = m_lastTokenEndPosition.line;
    13881390    return true;
    13891391}
     
    13961398    unsigned functionKeywordStart = tokenStart();
    13971399    next();
    1398     const Identifier* name = 0;
    1399     TreeFormalParameterList parameters = 0;
    1400     TreeFunctionBody body = 0;
    1401     unsigned openBraceOffset = 0;
    1402     unsigned closeBraceOffset = 0;
    1403     int bodyStartLine = 0;
    1404     unsigned bodyStartColumn = 0;
    1405     failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, true, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse this function");
    1406     failIfFalse(name, "Function statements must have a name");
    1407     failIfFalseIfStrict(declareVariable(name), "Cannot declare a function named '", name->impl(), "' in strict mode");
    1408     return context.createFuncDeclStatement(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn, functionKeywordStart);
     1400    ParserFunctionInfo<TreeBuilder> info;
     1401    failIfFalse((parseFunctionInfo(context, FunctionNeedsName, FunctionMode, true, info)), "Cannot parse this function");
     1402    failIfFalse(info.name, "Function statements must have a name");
     1403    failIfFalseIfStrict(declareVariable(info.name), "Cannot declare a function named '", info.name->impl(), "' in strict mode");
     1404    return context.createFuncDeclStatement(location, info, functionKeywordStart);
    14091405}
    14101406
     
    18001796        }
    18011797        failIfFalse(wasIdent, "Expected an identifier as property name");
    1802         const Identifier* accessorName = 0;
    1803         TreeFormalParameterList parameters = 0;
    1804         TreeFunctionBody body = 0;
    1805         unsigned openBraceOffset = 0;
    1806         unsigned closeBraceOffset = 0;
    1807         int bodyStartLine = 0;
    1808         unsigned bodyStartColumn = 0;
    18091798        PropertyNode::Type type;
    18101799        if (*ident == m_vm->propertyNames->get)
     
    18241813        JSTokenLocation location(tokenLocation());
    18251814        next();
     1815        ParserFunctionInfo<TreeBuilder> info;
    18261816        if (type == PropertyNode::Getter) {
    18271817            failIfFalse(match(OPENPAREN), "Expected a parameter list for getter definition");
    1828             failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse getter definition");
     1818            failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, GetterMode, false, info)), "Cannot parse getter definition");
    18291819        } else {
    18301820            failIfFalse(match(OPENPAREN), "Expected a parameter list for setter definition");
    1831             failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, accessorName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse setter definition");
     1821            failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, SetterMode, false, info)), "Cannot parse setter definition");
    18321822        }
    18331823        if (stringPropertyName)
    1834             return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn, getterOrSetterStartOffset);
    1835         return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, complete, numericPropertyName, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn, getterOrSetterStartOffset);
     1824            return context.createGetterOrSetterProperty(location, type, complete, stringPropertyName, info, getterOrSetterStartOffset);
     1825        return context.createGetterOrSetterProperty(const_cast<VM*>(m_vm), m_parserArena, location, type, complete, numericPropertyName, info, getterOrSetterStartOffset);
    18361826    }
    18371827    case NUMBER: {
     
    21732163        newCount++;
    21742164    }
    2175    
     2165
    21762166    if (match(FUNCTION)) {
    2177         const Identifier* name = &m_vm->propertyNames->nullIdentifier;
    2178         TreeFormalParameterList parameters = 0;
    2179         TreeFunctionBody body = 0;
    2180         unsigned openBraceOffset = 0;
    2181         unsigned closeBraceOffset = 0;
    2182         int bodyStartLine = 0;
    2183         unsigned bodyStartColumn = 0;
    21842167        unsigned functionKeywordStart = tokenStart();
    21852168        location = tokenLocation();
    21862169        next();
    2187         failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, name, parameters, body, openBraceOffset, closeBraceOffset, bodyStartLine, bodyStartColumn)), "Cannot parse function expression");
    2188         base = context.createFunctionExpr(location, name, body, parameters, openBraceOffset, closeBraceOffset, bodyStartLine, m_lastTokenEndPosition.line, bodyStartColumn, functionKeywordStart);
     2170        ParserFunctionInfo<TreeBuilder> info;
     2171        info.name = &m_vm->propertyNames->nullIdentifier;
     2172        failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, FunctionMode, false, info)), "Cannot parse function expression");
     2173        base = context.createFunctionExpr(location, info, functionKeywordStart);
    21892174    } else
    21902175        base = parsePrimaryExpression(context);
Note: See TracChangeset for help on using the changeset viewer.