Changeset 192937 in webkit for trunk/Source/JavaScriptCore/parser/Parser.cpp
- Timestamp:
- Dec 1, 2015, 7:16:28 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Parser.cpp
r192935 r192937 195 195 Parser<LexerType>::Parser( 196 196 VM* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, 197 JSParserStrictMode strictMode, SourceParseMode parseMode, 197 JSParserStrictMode strictMode, SourceParseMode parseMode, SuperBinding superBinding, 198 198 ConstructorKind defaultConstructorKind, ThisTDZMode thisTDZMode) 199 199 : m_vm(vm) … … 211 211 , m_sourceElements(0) 212 212 , m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin) 213 , m_superBinding(superBinding) 213 214 , m_defaultConstructorKind(defaultConstructorKind) 214 215 , m_thisTDZMode(thisTDZMode) … … 250 251 if (m_lexer->isReparsingFunction()) { 251 252 ParserFunctionInfo<ASTBuilder> functionInfo; 252 parseFunctionParameters(context, parseMode, functionInfo); 253 if (parseMode == SourceParseMode::GeneratorBodyMode) 254 functionInfo.parameters = createGeneratorParameters(context); 255 else 256 parseFunctionParameters(context, parseMode, functionInfo); 253 257 m_parameters = functionInfo.parameters; 254 258 … … 274 278 else if (isModuleParseMode(parseMode)) 275 279 sourceElements = parseModuleSourceElements(context, parseMode); 276 else 277 sourceElements = parseSourceElements(context, CheckForStrictMode); 280 else { 281 if (parseMode == SourceParseMode::GeneratorWrapperFunctionMode) 282 sourceElements = parseGeneratorFunctionSourceElements(context, CheckForStrictMode); 283 else 284 sourceElements = parseSourceElements(context, CheckForStrictMode); 285 } 278 286 } 279 287 … … 302 310 for (auto& entry : capturedVariables) 303 311 varDeclarations.markVariableAsCaptured(entry); 304 312 313 IdentifierSet usedVariables; 314 scope->getUsedVariables(usedVariables); 315 if (parseMode == SourceParseMode::GeneratorWrapperFunctionMode) { 316 if (usedVariables.contains(m_vm->propertyNames->arguments.impl())) 317 context.propagateArgumentsUse(); 318 } 319 305 320 CodeFeatures features = context.features(); 306 321 if (scope->strictMode()) … … 312 327 if (modifiedArguments) 313 328 features |= ModifiedArgumentsFeature; 329 314 330 Vector<RefPtr<UniquedStringImpl>> closedVariables; 315 331 if (m_parsingBuiltin) { 316 IdentifierSet usedVariables;317 scope->getUsedVariables(usedVariables);318 332 // FIXME: This needs to be changed if we want to allow builtins to use lexical declarations. 319 333 for (const auto& variable : usedVariables) { … … 454 468 semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); 455 469 } 470 471 return sourceElements; 472 } 473 474 template <typename LexerType> 475 template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseGeneratorFunctionSourceElements(TreeBuilder& context, SourceElementsMode mode) 476 { 477 auto sourceElements = context.createSourceElements(); 478 479 unsigned functionKeywordStart = tokenStart(); 480 JSTokenLocation startLocation(tokenLocation()); 481 JSTextPosition start = tokenStartPosition(); 482 unsigned startColumn = tokenColumn(); 483 int functionNameStart = m_token.m_location.startOffset; 484 int parametersStart = m_token.m_location.startOffset; 485 486 ParserFunctionInfo<TreeBuilder> info; 487 info.name = &m_vm->propertyNames->nullIdentifier; 488 info.parameters = createGeneratorParameters(context); 489 info.startOffset = parametersStart; 490 info.startLine = tokenLine(); 491 info.parameterCount = 4; // generator, state, value, resume mode 492 493 { 494 AutoPopScopeRef generatorBodyScope(this, pushScope()); 495 generatorBodyScope->setSourceParseMode(SourceParseMode::GeneratorBodyMode); 496 SyntaxChecker generatorFunctionContext(const_cast<VM*>(m_vm), m_lexer.get()); 497 failIfFalse(parseSourceElements(generatorFunctionContext, mode), "Cannot parse the body of a generator"); 498 popScope(generatorBodyScope, TreeBuilder::NeedsFreeVariableInfo); 499 } 500 info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionKeywordStart, functionNameStart, parametersStart, strictMode(), ConstructorKind::None, m_superBinding, info.parameterCount, SourceParseMode::GeneratorBodyMode, false); 501 502 info.endLine = tokenLine(); 503 info.endOffset = m_token.m_data.offset; 504 info.bodyStartColumn = startColumn; 505 506 auto functionExpr = context.createFunctionExpr(startLocation, info); 507 auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line); 508 context.appendStatement(sourceElements, statement); 456 509 457 510 return sourceElements; … … 1601 1654 template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody( 1602 1655 TreeBuilder& context, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart, 1603 ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) 1604 { 1605 bool isArrowFunction = FunctionBodyType::StandardFunctionBodyBlock != bodyType; 1656 ConstructorKind constructorKind, SuperBinding superBinding, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) 1657 { 1606 1658 bool isArrowFunctionBodyExpression = bodyType == ArrowFunctionBodyExpression; 1607 1659 if (!isArrowFunctionBodyExpression) { … … 1609 1661 if (match(CLOSEBRACE)) { 1610 1662 unsigned endColumn = tokenColumn(); 1611 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode, isArrowFunction, isArrowFunctionBodyExpression);1663 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, superBinding, parameterCount, parseMode, isArrowFunctionBodyExpression); 1612 1664 } 1613 1665 } … … 1621 1673 failIfFalse(parseSourceElements(syntaxChecker, CheckForStrictMode), bodyType == StandardFunctionBodyBlock ? "Cannot parse body of this function" : "Cannot parse body of this arrow function"); 1622 1674 unsigned endColumn = tokenColumn(); 1623 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode, isArrowFunction, isArrowFunctionBodyExpression);1675 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, superBinding, parameterCount, parseMode, isArrowFunctionBodyExpression); 1624 1676 } 1625 1677 … … 1635 1687 case SourceParseMode::MethodMode: 1636 1688 return "method"; 1637 case SourceParseMode::Generator Mode:1689 case SourceParseMode::GeneratorBodyMode: 1638 1690 return "generator"; 1691 case SourceParseMode::GeneratorWrapperFunctionMode: 1692 return "generator function"; 1639 1693 case SourceParseMode::ArrowFunctionMode: 1640 1694 return "arrow function"; … … 1715 1769 1716 1770 template <typename LexerType> 1771 template <class TreeBuilder> typename TreeBuilder::FormalParameterList Parser<LexerType>::createGeneratorParameters(TreeBuilder& context) 1772 { 1773 auto parameters = context.createFormalParameterList(); 1774 1775 JSTokenLocation location(tokenLocation()); 1776 JSTextPosition position = tokenStartPosition(); 1777 1778 // @generator 1779 declareParameter(&m_vm->propertyNames->generatorPrivateName); 1780 auto generator = context.createBindingLocation(location, m_vm->propertyNames->generatorPrivateName, position, position, AssignmentContext::DeclarationStatement); 1781 context.appendParameter(parameters, generator, 0); 1782 1783 // @generatorState 1784 declareParameter(&m_vm->propertyNames->generatorStatePrivateName); 1785 auto generatorState = context.createBindingLocation(location, m_vm->propertyNames->generatorStatePrivateName, position, position, AssignmentContext::DeclarationStatement); 1786 context.appendParameter(parameters, generatorState, 0); 1787 1788 // @generatorValue 1789 declareParameter(&m_vm->propertyNames->generatorValuePrivateName); 1790 auto generatorValue = context.createBindingLocation(location, m_vm->propertyNames->generatorValuePrivateName, position, position, AssignmentContext::DeclarationStatement); 1791 context.appendParameter(parameters, generatorValue, 0); 1792 1793 // @generatorResumeMode 1794 declareParameter(&m_vm->propertyNames->generatorResumeModePrivateName); 1795 auto generatorResumeMode = context.createBindingLocation(location, m_vm->propertyNames->generatorResumeModePrivateName, position, position, AssignmentContext::DeclarationStatement); 1796 context.appendParameter(parameters, generatorResumeMode, 0); 1797 1798 return parameters; 1799 } 1800 1801 template <typename LexerType> 1717 1802 template <class TreeBuilder> bool Parser<LexerType>::parseFunctionInfo(TreeBuilder& context, FunctionRequirements requirements, SourceParseMode mode, bool nameIsInContainingScope, ConstructorKind constructorKind, SuperBinding expectedSuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>& functionInfo, FunctionDefinitionType functionDefinitionType) 1718 1803 { … … 1825 1910 unsigned currentLineStartOffset = m_token.m_location.lineStartOffset; 1826 1911 1827 bool isArrowFunction = mode == SourceParseMode::ArrowFunctionMode;1828 1829 1912 functionInfo.body = context.createFunctionMetadata( 1830 1913 startLocation, endLocation, functionInfo.bodyStartColumn, bodyEndColumn, 1831 1914 functionKeywordStart, functionNameStart, parametersStart, 1832 cachedInfo->strictMode, constructorKind, cachedInfo->parameterCount, mode, isArrowFunction,functionBodyType == ArrowFunctionBodyExpression);1915 cachedInfo->strictMode, constructorKind, expectedSuperBinding, cachedInfo->parameterCount, mode, functionBodyType == ArrowFunctionBodyExpression); 1833 1916 1834 1917 functionScope->restoreFromSourceProviderCache(cachedInfo); … … 1844 1927 functionInfo.endOffset = cachedInfo->endFunctionOffset; 1845 1928 1846 if ( isArrowFunction)1929 if (mode == SourceParseMode::ArrowFunctionMode) 1847 1930 functionBodyType = cachedInfo->isBodyArrowExpression ? ArrowFunctionBodyExpression : ArrowFunctionBodyBlock; 1848 1931 else … … 1866 1949 m_lastFunctionName = lastFunctionName; 1867 1950 ParserState oldState = saveState(); 1868 1869 functionInfo.body = parseFunctionBody(context, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, functionBodyType, functionInfo.parameterCount, mode); 1951 1952 auto performParsingFunctionBody = [&] { 1953 return parseFunctionBody(context, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, expectedSuperBinding, functionBodyType, functionInfo.parameterCount, mode); 1954 }; 1955 1956 if (mode == SourceParseMode::GeneratorWrapperFunctionMode) { 1957 AutoPopScopeRef generatorBodyScope(this, pushScope()); 1958 generatorBodyScope->setSourceParseMode(SourceParseMode::GeneratorBodyMode); 1959 functionInfo.body = performParsingFunctionBody(); 1960 1961 // When a generator has a "use strict" directive, a generator function wrapping it should be strict mode. 1962 if (generatorBodyScope->strictMode()) 1963 functionScope->setStrictMode(); 1964 1965 semanticFailIfTrue(generatorBodyScope->hasDirectSuper(), "Cannot call super() outside of a class constructor"); 1966 if (generatorBodyScope->needsSuperBinding()) 1967 semanticFailIfTrue(expectedSuperBinding == SuperBinding::NotNeeded, "super can only be used in a method of a derived class"); 1968 1969 popScope(generatorBodyScope, TreeBuilder::NeedsFreeVariableInfo); 1970 } else 1971 functionInfo.body = performParsingFunctionBody(); 1870 1972 1871 1973 restoreState(oldState); … … 1873 1975 context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); 1874 1976 if (functionScope->strictMode() && functionInfo.name) { 1875 RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode || mode == SourceParseMode::Generator Mode);1977 RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode || mode == SourceParseMode::GeneratorBodyMode || mode == SourceParseMode::GeneratorWrapperFunctionMode); 1876 1978 semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); 1877 1979 semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); … … 1941 2043 #if ENABLE(ES6_GENERATORS) 1942 2044 if (consume(TIMES)) 1943 parseMode = SourceParseMode::Generator Mode;2045 parseMode = SourceParseMode::GeneratorWrapperFunctionMode; 1944 2046 #endif 1945 2047 failIfFalse((parseFunctionInfo(context, FunctionNeedsName, parseMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, functionInfo, FunctionDefinitionType::Declaration)), "Cannot parse this function"); … … 2092 2194 if (isGenerator) { 2093 2195 isConstructor = false; 2094 parseMode = SourceParseMode::Generator Mode;2196 parseMode = SourceParseMode::GeneratorWrapperFunctionMode; 2095 2197 semanticFailIfTrue(*ident == m_vm->propertyNames->prototype, "Cannot declare a generator named 'prototype'"); 2096 2198 semanticFailIfTrue(*ident == m_vm->propertyNames->constructor, "Cannot declare a generator named 'constructor'"); … … 2758 2860 2759 2861 #if ENABLE(ES6_GENERATORS) 2760 if (match(YIELD) )2862 if (match(YIELD) && !isYIELDMaskedAsIDENT(currentScope()->isGenerator())) 2761 2863 return parseYieldExpression(context); 2762 2864 #endif … … 2863 2965 2864 2966 JSTokenLocation location(tokenLocation()); 2967 JSTextPosition divotStart = tokenStartPosition(); 2865 2968 ASSERT(match(YIELD)); 2866 2969 SavePoint savePoint = createSavePoint(); … … 2870 2973 2871 2974 bool delegate = consume(TIMES); 2975 JSTextPosition argumentStart = tokenStartPosition(); 2872 2976 TreeExpression argument = parseAssignmentExpression(context); 2873 2977 if (!argument) { … … 2876 2980 return context.createYield(location); 2877 2981 } 2878 return context.createYield(location, argument, delegate );2982 return context.createYield(location, argument, delegate, divotStart, argumentStart, lastTokenEndPosition()); 2879 2983 } 2880 2984 … … 3068 3172 unsigned methodStart = tokenStart(); 3069 3173 ParserFunctionInfo<TreeBuilder> methodInfo; 3070 SourceParseMode parseMode = isGenerator ? SourceParseMode::Generator Mode : SourceParseMode::MethodMode;3174 SourceParseMode parseMode = isGenerator ? SourceParseMode::GeneratorWrapperFunctionMode : SourceParseMode::MethodMode; 3071 3175 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, parseMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, FunctionDefinitionType::Method)), "Cannot parse this method"); 3072 3176 methodInfo.name = methodName; … … 3321 3425 #if ENABLE(ES6_GENERATORS) 3322 3426 if (consume(TIMES)) 3323 parseMode = SourceParseMode::Generator Mode;3427 parseMode = SourceParseMode::GeneratorWrapperFunctionMode; 3324 3428 #endif 3325 3429 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, parseMode, false, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, functionInfo, FunctionDefinitionType::Expression)), "Cannot parse function expression");
Note:
See TracChangeset
for help on using the changeset viewer.