@@ -7,6 +7,8 @@ import 'dart:math' show min, max;
77import 'package:kernel/ast.dart' ;
88import 'package:kernel/type_environment.dart' ;
99
10+ import 'package:vm/transformations/pragma.dart' ;
11+
1012import 'dbc.dart' ;
1113import 'options.dart' show BytecodeOptions;
1214
@@ -157,8 +159,8 @@ class LocalVariables {
157159 List <VariableDeclaration > get sortedNamedParameters =>
158160 _currentFrame.sortedNamedParameters;
159161
160- LocalVariables (Member node, this .options, this .staticTypeContext) {
161- final scopeBuilder = new _ScopeBuilder (this );
162+ LocalVariables (PragmaAnnotationParser pragmaParser, Member node, this .options, this .staticTypeContext) {
163+ final scopeBuilder = new _ScopeBuilder (pragmaParser, this );
162164 node.accept (scopeBuilder);
163165
164166 final allocator = new _Allocator (this );
@@ -175,6 +177,9 @@ class LocalVariables {
175177 _currentFrameInternal = _currentScopeInternal? .frame;
176178 }
177179
180+ bool get capturesOnlyFinalAndSharedVars =>
181+ _currentFrame.capturesOnlyFinalAndSharedVars;
182+
178183 void withTemp (TreeNode node, int temp, void action ()) {
179184 final old = _temps! [node];
180185 assert (old == null || old.length == 1 );
@@ -192,16 +197,18 @@ class VarDesc {
192197 final VariableDeclaration declaration;
193198 Scope scope;
194199 bool isCaptured = false ;
200+ bool _isShared;
195201 int ? index;
196202 int ? originalParamSlotIndex;
197203
198- VarDesc (this .declaration, this .scope) {
204+ VarDesc (this .declaration, this .scope, this ._isShared ) {
199205 scope.vars.add (this );
200206 }
201207
202208 Frame get frame => scope.frame;
203209
204210 bool get isAllocated => index != null ;
211+ bool get isFinalOrShared => declaration.isFinal || _isShared;
205212
206213 void capture () {
207214 assert (! isAllocated);
@@ -243,6 +250,7 @@ class Frame {
243250 int frameSize = 0 ;
244251 List <int > temporaries = < int > [];
245252 int ? contextLevelAtEntry;
253+ bool capturesOnlyFinalAndSharedVars = true ;
246254
247255 Frame (this .function, this .parent);
248256
@@ -302,7 +310,9 @@ class _ScopeBuilder extends RecursiveVisitor {
302310 List <TreeNode > _enclosingTryCatches = const [];
303311 int _loopDepth = 0 ;
304312
305- _ScopeBuilder (this .locals);
313+ final PragmaAnnotationParser _pragmaParser;
314+
315+ _ScopeBuilder (this ._pragmaParser, this .locals);
306316
307317 List <VariableDeclaration > _sortNamedParameters (FunctionNode function) {
308318 final params = function.namedParameters.toList ();
@@ -462,7 +472,10 @@ class _ScopeBuilder extends RecursiveVisitor {
462472 if (scope == null ) {
463473 scope = _currentScope;
464474 }
465- final VarDesc v = new VarDesc (variable, scope);
475+ final isShared = _pragmaParser
476+ .parsedPragmas <ParsedVmSharedPragma >(variable.annotations)
477+ .isNotEmpty;
478+ final VarDesc v = new VarDesc (variable, scope, isShared);
466479 assert (locals._vars[variable] == null ,
467480 'Double declaring variable ${variable }!' );
468481 locals._vars[variable] = v;
@@ -475,6 +488,9 @@ class _ScopeBuilder extends RecursiveVisitor {
475488 }
476489 if (v.frame != _currentFrame) {
477490 v.capture ();
491+ if (! v.isFinalOrShared) {
492+ _currentFrame.capturesOnlyFinalAndSharedVars = false ;
493+ }
478494 }
479495 }
480496
0 commit comments