@@ -113,15 +113,24 @@ internal TupleValue CreateTuple(ImmutableArray<ResolvedType> elementTypes, bool
113113 /// <param name="tupleElements">The tuple elements</param>
114114 internal TupleValue CreateTuple ( ImmutableArray < TypedExpression > tupleElements , bool registerWithScopeManager = true )
115115 {
116- var elements = tupleElements . Select ( this . sharedState . EvaluateSubexpression ) . ToArray ( ) ;
117- return this . CreateTuple ( null , registerWithScopeManager , elements ) ;
116+ var elementTypes = tupleElements . Select ( v => v . ResolvedType ) . ToImmutableArray ( ) ;
117+ TupleValue tuple = new TupleValue ( elementTypes , this . sharedState , registerWithScopeManager ) ;
118+ PointerValue [ ] itemPointers = tuple . GetTupleElementPointers ( ) ;
119+
120+ var elements = tupleElements . Select ( this . sharedState . BuildSubitem ) . ToArray ( ) ;
121+ for ( var i = 0 ; i < itemPointers . Length ; ++ i )
122+ {
123+ itemPointers [ i ] . StoreValue ( elements [ i ] ) ;
124+ }
125+
126+ return tuple ;
118127 }
119128
120129 /// <summary>
121130 /// Builds a tuple with the items set to the given tuple elements.
122131 /// The tuple represents a value of user defined type if a name is specified.
123132 /// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
124- /// Does *not* increase the reference count for the tuple elements.
133+ /// Increases the reference count for the tuple elements.
125134 /// </summary>
126135 /// <param name="typeName">The name of the user defined typed that the tuple represents</param>
127136 /// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -135,6 +144,7 @@ private TupleValue CreateTuple(UserDefinedType? typeName, bool registerWithScope
135144 for ( var i = 0 ; i < itemPointers . Length ; ++ i )
136145 {
137146 itemPointers [ i ] . StoreValue ( tupleElements [ i ] ) ;
147+ this . sharedState . ScopeMgr . IncreaseReferenceCount ( tupleElements [ i ] ) ;
138148 }
139149
140150 return tuple ;
@@ -143,7 +153,7 @@ private TupleValue CreateTuple(UserDefinedType? typeName, bool registerWithScope
143153 /// <summary>
144154 /// Builds a tuple with the items set to the given tuple elements.
145155 /// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
146- /// Does *not* increase the reference count for the tuple elements.
156+ /// Increases the reference count for the tuple elements.
147157 /// </summary>
148158 /// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
149159 /// <param name="tupleElements">The tuple elements</param>
@@ -153,16 +163,16 @@ internal TupleValue CreateTuple(bool registerWithScopeManager, params IValue[] t
153163 /// <summary>
154164 /// Builds a tuple with the items set to the given tuple elements.
155165 /// Registers the value with the scope manager.
156- /// Does *not* increase the reference count for the tuple elements.
166+ /// Increases the reference count for the tuple elements.
157167 /// </summary>
158168 /// <param name="tupleElements">The tuple elements</param>
159169 internal TupleValue CreateTuple ( params IValue [ ] tupleElements ) =>
160- this . CreateTuple ( null , true , tupleElements ) ;
170+ this . CreateTuple ( true , tupleElements ) ;
161171
162172 /// <summary>
163173 /// Builds a tuple representing a Q# value of user defined type with the items set to the given elements.
164174 /// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
165- /// Does *not* increase the reference count for the tuple elements.
175+ /// Increases the reference count for the tuple elements.
166176 /// </summary>
167177 /// <param name="typeName">The name of the user defined type</param>
168178 /// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -173,7 +183,7 @@ internal TupleValue CreateCustomType(UserDefinedType typeName, bool registerWith
173183 /// <summary>
174184 /// Builds a tuple representing a Q# value of user defined type with the items set to the given elements.
175185 /// Registers the value with the scope manager.
176- /// Does *not* increase the reference count for the tuple elements.
186+ /// Increases the reference count for the tuple elements.
177187 /// </summary>
178188 /// <param name="typeName">The name of the user defined type</param>
179189 /// <param name="tupleElements">The tuple elements</param>
@@ -196,14 +206,22 @@ internal ArrayValue CreateArray(Value length, ResolvedType elementType, bool reg
196206 /// <param name="arrayElements">The elements in the array</param>
197207 internal ArrayValue CreateArray ( ResolvedType elementType , ImmutableArray < TypedExpression > arrayElements , bool registerWithScopeManager = true )
198208 {
199- var elements = arrayElements . Select ( this . sharedState . EvaluateSubexpression ) . ToArray ( ) ;
200- return this . CreateArray ( elementType , registerWithScopeManager , elements ) ;
209+ var array = new ArrayValue ( ( uint ) arrayElements . Length , elementType , this . sharedState , registerWithScopeManager ) ;
210+ var itemPointers = array . GetArrayElementPointers ( ) ;
211+
212+ var elements = arrayElements . Select ( this . sharedState . BuildSubitem ) . ToArray ( ) ;
213+ for ( var i = 0 ; i < itemPointers . Length ; ++ i )
214+ {
215+ itemPointers [ i ] . StoreValue ( elements [ i ] ) ;
216+ }
217+
218+ return array ;
201219 }
202220
203221 /// <summary>
204222 /// Builds an array that containsthe given array elements.
205223 /// Registers the value with the scope manager, unless registerWithScopeManager is set to false.
206- /// Does *not* increase the reference count for the array elements.
224+ /// Increases the reference count for the array elements.
207225 /// </summary>
208226 /// <param name="elementType">The Q# type of the array elements</param>
209227 /// <param name="registerWithScopeManager">Whether or not to register the built tuple with the scope manager</param>
@@ -216,6 +234,7 @@ internal ArrayValue CreateArray(ResolvedType elementType, bool registerWithScope
216234 for ( var i = 0 ; i < itemPointers . Length ; ++ i )
217235 {
218236 itemPointers [ i ] . StoreValue ( arrayElements [ i ] ) ;
237+ this . sharedState . ScopeMgr . IncreaseReferenceCount ( arrayElements [ i ] ) ;
219238 }
220239
221240 return array ;
@@ -224,21 +243,10 @@ internal ArrayValue CreateArray(ResolvedType elementType, bool registerWithScope
224243 /// <summary>
225244 /// Builds an array that containsthe given array elements.
226245 /// Registers the value with the scope manager.
227- /// Does *not* increase the reference count for the array elements.
246+ /// Increases the reference count for the array elements.
228247 /// </summary>
229248 /// <param name="arrayElements">The elements in the array</param>
230249 internal ArrayValue CreateArray ( ResolvedType elementType , params IValue [ ] arrayElements ) =>
231250 this . CreateArray ( elementType , true , arrayElements ) ;
232-
233- /// <summary>
234- /// Creates a callable value of the given type and registers it with the scope manager.
235- /// The necessary functions to invoke the callable are defined by the callable table;
236- /// i.e. the globally defined array of function pointers accessible via the given global variable.
237- /// </summary>
238- /// <param name="callableType">The Q# type of the callable value.</param>
239- /// <param name="table">The global variable that contains the array of function pointers defining the callable.</param>
240- /// <param name="captured">All captured values.</param>
241- internal CallableValue CreateCallable ( ResolvedType callableType , GlobalVariable table , ImmutableArray < TypedExpression > ? captured = null ) =>
242- new CallableValue ( callableType , table , this . sharedState , captured ) ;
243251 }
244252}
0 commit comments