@@ -1852,7 +1852,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
18521852 }
18531853 }
18541854
1855- int _genClosureBytecode (TreeNode node, String name, FunctionNode function) {
1855+ int _genClosureBytecode (
1856+ LocalFunction node, String name, FunctionNode function) {
18561857 _pushAssemblerState ();
18571858
18581859 locals.enterScope (node);
@@ -1865,15 +1866,6 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
18651866 final savedLoopDepth = currentLoopDepth;
18661867 currentLoopDepth = 0 ;
18671868
1868- int position = TreeNode .noOffset;
1869- int endPosition = TreeNode .noOffset;
1870- if (emitSourcePositions) {
1871- position = (node is ast.FunctionDeclaration )
1872- ? node.fileOffset
1873- : function.fileOffset;
1874- endPosition = function.fileEndOffset;
1875- }
1876-
18771869 if (function.typeParameters.isNotEmpty) {
18781870 functionTypeParameters ?? = new List <TypeParameter >();
18791871 functionTypeParameters.addAll (function.typeParameters);
@@ -1890,27 +1882,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
18901882 locals.sortedNamedParameters.forEach (_evaluateDefaultParameterValue);
18911883
18921884 final int closureIndex = closures.length;
1893- objectTable.declareClosure (function, enclosingMember, closureIndex);
1894- final List <NameAndType > parameters = function.positionalParameters
1895- .followedBy (function.namedParameters)
1896- .map ((v) => new NameAndType (objectTable.getNameHandle (null , v.name),
1897- objectTable.getHandle (v.type)))
1898- .toList ();
1899- final ClosureDeclaration closure = new ClosureDeclaration (
1900- objectTable
1901- .getHandle (savedIsClosure ? parentFunction : enclosingMember),
1902- objectTable.getNameHandle (null , name),
1903- position,
1904- endPosition,
1905- function.typeParameters
1906- .map ((tp) => new NameAndType (
1907- objectTable.getNameHandle (null , tp.name),
1908- objectTable.getHandle (tp.bound)))
1909- .toList (),
1910- function.requiredParameterCount,
1911- function.namedParameters.length,
1912- parameters,
1913- objectTable.getHandle (function.returnType));
1885+ final closure = getClosureDeclaration (node, function, name, closureIndex,
1886+ savedIsClosure ? parentFunction : enclosingMember);
19141887 closures.add (closure);
19151888
19161889 final int closureFunctionIndex = cp.addClosureFunction (closureIndex);
@@ -1972,6 +1945,71 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
19721945 return closureFunctionIndex;
19731946 }
19741947
1948+ ClosureDeclaration getClosureDeclaration (LocalFunction node,
1949+ FunctionNode function, String name, int closureIndex, TreeNode parent) {
1950+ objectTable.declareClosure (function, enclosingMember, closureIndex);
1951+
1952+ int flags = 0 ;
1953+ int position = TreeNode .noOffset;
1954+ int endPosition = TreeNode .noOffset;
1955+ if (emitSourcePositions) {
1956+ position = (node is ast.FunctionDeclaration )
1957+ ? node.fileOffset
1958+ : function.fileOffset;
1959+ endPosition = function.fileEndOffset;
1960+ if (position != TreeNode .noOffset) {
1961+ flags | = ClosureDeclaration .hasSourcePositionsFlag;
1962+ }
1963+ }
1964+
1965+ switch (function.dartAsyncMarker) {
1966+ case AsyncMarker .Async :
1967+ flags | = ClosureDeclaration .isAsyncFlag;
1968+ break ;
1969+ case AsyncMarker .AsyncStar :
1970+ flags | = ClosureDeclaration .isAsyncStarFlag;
1971+ break ;
1972+ case AsyncMarker .SyncStar :
1973+ flags | = ClosureDeclaration .isSyncStarFlag;
1974+ break ;
1975+ default :
1976+ break ;
1977+ }
1978+
1979+ final List <NameAndType > parameters = function.positionalParameters
1980+ .followedBy (function.namedParameters)
1981+ .map ((v) => new NameAndType (objectTable.getNameHandle (null , v.name),
1982+ objectTable.getHandle (v.type)))
1983+ .toList ();
1984+ if (function.requiredParameterCount != parameters.length) {
1985+ if (function.namedParameters.isNotEmpty) {
1986+ flags | = ClosureDeclaration .hasOptionalNamedParamsFlag;
1987+ } else {
1988+ flags | = ClosureDeclaration .hasOptionalPositionalParamsFlag;
1989+ }
1990+ }
1991+
1992+ final typeParams = function.typeParameters
1993+ .map ((tp) => new NameAndType (objectTable.getNameHandle (null , tp.name),
1994+ objectTable.getHandle (tp.bound)))
1995+ .toList ();
1996+ if (typeParams.isNotEmpty) {
1997+ flags | = ClosureDeclaration .hasTypeParamsFlag;
1998+ }
1999+
2000+ return new ClosureDeclaration (
2001+ flags,
2002+ objectTable.getHandle (parent),
2003+ objectTable.getNameHandle (null , name),
2004+ position,
2005+ endPosition,
2006+ typeParams,
2007+ function.requiredParameterCount,
2008+ function.namedParameters.length,
2009+ parameters,
2010+ objectTable.getHandle (function.returnType));
2011+ }
2012+
19752013 void _genSyncYieldingPrologue (FunctionNode function, Label continuationLabel,
19762014 int switchVarIndexInFrame) {
19772015 // switch_var = :await_jump_var
@@ -2045,7 +2083,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
20452083 asm.emitStoreFieldTOS (cp.addInstanceField (closureContext));
20462084 }
20472085
2048- void _genClosure (TreeNode node, String name, FunctionNode function) {
2086+ void _genClosure (LocalFunction node, String name, FunctionNode function) {
20492087 final int closureFunctionIndex = _genClosureBytecode (node, name, function);
20502088 _genAllocateClosureInstance (node, closureFunctionIndex, function);
20512089 }
0 commit comments