Changeset 192935 in webkit for trunk/Source/JavaScriptCore/parser/Parser.cpp
- Timestamp:
- Dec 1, 2015, 5:37:19 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Parser.cpp
r192919 r192935 195 195 Parser<LexerType>::Parser( 196 196 VM* vm, const SourceCode& source, JSParserBuiltinMode builtinMode, 197 JSParserStrictMode strictMode, SourceParseMode parseMode, SuperBinding superBinding,197 JSParserStrictMode strictMode, SourceParseMode parseMode, 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)214 213 , m_defaultConstructorKind(defaultConstructorKind) 215 214 , m_thisTDZMode(thisTDZMode) … … 251 250 if (m_lexer->isReparsingFunction()) { 252 251 ParserFunctionInfo<ASTBuilder> functionInfo; 253 if (parseMode == SourceParseMode::GeneratorBodyMode) 254 functionInfo.parameters = createGeneratorParameters(context); 255 else 256 parseFunctionParameters(context, parseMode, functionInfo); 252 parseFunctionParameters(context, parseMode, functionInfo); 257 253 m_parameters = functionInfo.parameters; 258 254 … … 278 274 else if (isModuleParseMode(parseMode)) 279 275 sourceElements = parseModuleSourceElements(context, parseMode); 280 else { 281 if (parseMode == SourceParseMode::GeneratorWrapperFunctionMode) 282 sourceElements = parseGeneratorFunctionSourceElements(context, CheckForStrictMode); 283 else 284 sourceElements = parseSourceElements(context, CheckForStrictMode); 285 } 276 else 277 sourceElements = parseSourceElements(context, CheckForStrictMode); 286 278 } 287 279 … … 310 302 for (auto& entry : capturedVariables) 311 303 varDeclarations.markVariableAsCaptured(entry); 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 304 320 305 CodeFeatures features = context.features(); 321 306 if (scope->strictMode()) … … 327 312 if (modifiedArguments) 328 313 features |= ModifiedArgumentsFeature; 329 330 314 Vector<RefPtr<UniquedStringImpl>> closedVariables; 331 315 if (m_parsingBuiltin) { 316 IdentifierSet usedVariables; 317 scope->getUsedVariables(usedVariables); 332 318 // FIXME: This needs to be changed if we want to allow builtins to use lexical declarations. 333 319 for (const auto& variable : usedVariables) { … … 468 454 semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable"); 469 455 } 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 mode492 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);509 456 510 457 return sourceElements; … … 1654 1601 template <class TreeBuilder> TreeFunctionBody Parser<LexerType>::parseFunctionBody( 1655 1602 TreeBuilder& context, const JSTokenLocation& startLocation, int startColumn, int functionKeywordStart, int functionNameStart, int parametersStart, 1656 ConstructorKind constructorKind, SuperBinding superBinding, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) 1657 { 1603 ConstructorKind constructorKind, FunctionBodyType bodyType, unsigned parameterCount, SourceParseMode parseMode) 1604 { 1605 bool isArrowFunction = FunctionBodyType::StandardFunctionBodyBlock != bodyType; 1658 1606 bool isArrowFunctionBodyExpression = bodyType == ArrowFunctionBodyExpression; 1659 1607 if (!isArrowFunctionBodyExpression) { … … 1661 1609 if (match(CLOSEBRACE)) { 1662 1610 unsigned endColumn = tokenColumn(); 1663 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, superBinding, parameterCount, parseMode, isArrowFunctionBodyExpression);1611 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode, isArrowFunction, isArrowFunctionBodyExpression); 1664 1612 } 1665 1613 } … … 1673 1621 failIfFalse(parseSourceElements(syntaxChecker, CheckForStrictMode), bodyType == StandardFunctionBodyBlock ? "Cannot parse body of this function" : "Cannot parse body of this arrow function"); 1674 1622 unsigned endColumn = tokenColumn(); 1675 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, superBinding, parameterCount, parseMode, isArrowFunctionBodyExpression);1623 return context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, endColumn, functionKeywordStart, functionNameStart, parametersStart, strictMode(), constructorKind, parameterCount, parseMode, isArrowFunction, isArrowFunctionBodyExpression); 1676 1624 } 1677 1625 … … 1687 1635 case SourceParseMode::MethodMode: 1688 1636 return "method"; 1689 case SourceParseMode::Generator BodyMode:1637 case SourceParseMode::GeneratorMode: 1690 1638 return "generator"; 1691 case SourceParseMode::GeneratorWrapperFunctionMode:1692 return "generator function";1693 1639 case SourceParseMode::ArrowFunctionMode: 1694 1640 return "arrow function"; … … 1769 1715 1770 1716 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 // @generator1779 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 // @generatorState1784 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 // @generatorValue1789 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 // @generatorResumeMode1794 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>1802 1717 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) 1803 1718 { … … 1910 1825 unsigned currentLineStartOffset = m_token.m_location.lineStartOffset; 1911 1826 1827 bool isArrowFunction = mode == SourceParseMode::ArrowFunctionMode; 1828 1912 1829 functionInfo.body = context.createFunctionMetadata( 1913 1830 startLocation, endLocation, functionInfo.bodyStartColumn, bodyEndColumn, 1914 1831 functionKeywordStart, functionNameStart, parametersStart, 1915 cachedInfo->strictMode, constructorKind, expectedSuperBinding, cachedInfo->parameterCount, mode,functionBodyType == ArrowFunctionBodyExpression);1832 cachedInfo->strictMode, constructorKind, cachedInfo->parameterCount, mode, isArrowFunction, functionBodyType == ArrowFunctionBodyExpression); 1916 1833 1917 1834 functionScope->restoreFromSourceProviderCache(cachedInfo); … … 1927 1844 functionInfo.endOffset = cachedInfo->endFunctionOffset; 1928 1845 1929 if ( mode == SourceParseMode::ArrowFunctionMode)1846 if (isArrowFunction) 1930 1847 functionBodyType = cachedInfo->isBodyArrowExpression ? ArrowFunctionBodyExpression : ArrowFunctionBodyBlock; 1931 1848 else … … 1949 1866 m_lastFunctionName = lastFunctionName; 1950 1867 ParserState oldState = saveState(); 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(); 1868 1869 functionInfo.body = parseFunctionBody(context, startLocation, startColumn, functionKeywordStart, functionNameStart, parametersStart, constructorKind, functionBodyType, functionInfo.parameterCount, mode); 1972 1870 1973 1871 restoreState(oldState); … … 1975 1873 context.setEndOffset(functionInfo.body, m_lexer->currentOffset()); 1976 1874 if (functionScope->strictMode() && functionInfo.name) { 1977 RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode || mode == SourceParseMode::Generator BodyMode || mode == SourceParseMode::GeneratorWrapperFunctionMode);1875 RELEASE_ASSERT(mode == SourceParseMode::NormalFunctionMode || mode == SourceParseMode::MethodMode || mode == SourceParseMode::ArrowFunctionMode || mode == SourceParseMode::GeneratorMode); 1978 1876 semanticFailIfTrue(m_vm->propertyNames->arguments == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); 1979 1877 semanticFailIfTrue(m_vm->propertyNames->eval == *functionInfo.name, "'", functionInfo.name->impl(), "' is not a valid function name in strict mode"); … … 2043 1941 #if ENABLE(ES6_GENERATORS) 2044 1942 if (consume(TIMES)) 2045 parseMode = SourceParseMode::Generator WrapperFunctionMode;1943 parseMode = SourceParseMode::GeneratorMode; 2046 1944 #endif 2047 1945 failIfFalse((parseFunctionInfo(context, FunctionNeedsName, parseMode, true, ConstructorKind::None, SuperBinding::NotNeeded, functionKeywordStart, functionInfo, FunctionDefinitionType::Declaration)), "Cannot parse this function"); … … 2194 2092 if (isGenerator) { 2195 2093 isConstructor = false; 2196 parseMode = SourceParseMode::Generator WrapperFunctionMode;2094 parseMode = SourceParseMode::GeneratorMode; 2197 2095 semanticFailIfTrue(*ident == m_vm->propertyNames->prototype, "Cannot declare a generator named 'prototype'"); 2198 2096 semanticFailIfTrue(*ident == m_vm->propertyNames->constructor, "Cannot declare a generator named 'constructor'"); … … 2860 2758 2861 2759 #if ENABLE(ES6_GENERATORS) 2862 if (match(YIELD) && !isYIELDMaskedAsIDENT(currentScope()->isGenerator()))2760 if (match(YIELD)) 2863 2761 return parseYieldExpression(context); 2864 2762 #endif … … 2965 2863 2966 2864 JSTokenLocation location(tokenLocation()); 2967 JSTextPosition divotStart = tokenStartPosition();2968 2865 ASSERT(match(YIELD)); 2969 2866 SavePoint savePoint = createSavePoint(); … … 2973 2870 2974 2871 bool delegate = consume(TIMES); 2975 JSTextPosition argumentStart = tokenStartPosition();2976 2872 TreeExpression argument = parseAssignmentExpression(context); 2977 2873 if (!argument) { … … 2980 2876 return context.createYield(location); 2981 2877 } 2982 return context.createYield(location, argument, delegate , divotStart, argumentStart, lastTokenEndPosition());2878 return context.createYield(location, argument, delegate); 2983 2879 } 2984 2880 … … 3172 3068 unsigned methodStart = tokenStart(); 3173 3069 ParserFunctionInfo<TreeBuilder> methodInfo; 3174 SourceParseMode parseMode = isGenerator ? SourceParseMode::Generator WrapperFunctionMode : SourceParseMode::MethodMode;3070 SourceParseMode parseMode = isGenerator ? SourceParseMode::GeneratorMode : SourceParseMode::MethodMode; 3175 3071 failIfFalse((parseFunctionInfo(context, FunctionNoRequirements, parseMode, false, ConstructorKind::None, SuperBinding::NotNeeded, methodStart, methodInfo, FunctionDefinitionType::Method)), "Cannot parse this method"); 3176 3072 methodInfo.name = methodName; … … 3425 3321 #if ENABLE(ES6_GENERATORS) 3426 3322 if (consume(TIMES)) 3427 parseMode = SourceParseMode::Generator WrapperFunctionMode;3323 parseMode = SourceParseMode::GeneratorMode; 3428 3324 #endif 3429 3325 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.