@@ -55,8 +55,11 @@ public class DeclarationFinder
55
55
private readonly Lazy < ConcurrentDictionary < Declaration , Declaration [ ] > > _handlersByWithEventsField ;
56
56
private readonly Lazy < ConcurrentDictionary < VBAParser . ImplementsStmtContext , Declaration [ ] > > _membersByImplementsContext ;
57
57
private readonly Lazy < ConcurrentDictionary < Declaration , Declaration [ ] > > _interfaceMembers ;
58
+ private Lazy < List < Declaration > > _nonBaseAsType ;
59
+ private readonly Lazy < ConcurrentBag < Declaration > > _eventHandlers ;
60
+ private readonly Lazy < ConcurrentBag < Declaration > > _classes ;
58
61
59
- private static readonly object ThreadLock = new object ( ) ;
62
+ private readonly object threadLock = new object ( ) ;
60
63
61
64
private static QualifiedSelection GetGroupingKey ( Declaration declaration )
62
65
{
@@ -153,8 +156,16 @@ public DeclarationFinder(IReadOnlyList<Declaration> declarations, IEnumerable<IA
153
156
} ) ;
154
157
155
158
_membersByImplementsContext = new Lazy < ConcurrentDictionary < VBAParser . ImplementsStmtContext , Declaration [ ] > > ( ( ) =>
156
- new ConcurrentDictionary < VBAParser . ImplementsStmtContext , Declaration [ ] > (
157
- implementableMembers . ToDictionary ( item => item . Context , item => item . Members ) ) , true ) ;
159
+ new ConcurrentDictionary < VBAParser . ImplementsStmtContext , Declaration [ ] > (
160
+ implementableMembers . ToDictionary ( item => item . Context , item => item . Members ) ) , true ) ;
161
+
162
+ _nonBaseAsType = new Lazy < List < Declaration > > ( ( ) =>
163
+ _declarations . AllValues ( ) . Where ( d =>
164
+ ! string . IsNullOrWhiteSpace ( d . AsTypeName )
165
+ && ! d . AsTypeIsBaseType
166
+ && d . DeclarationType != DeclarationType . Project
167
+ && d . DeclarationType != DeclarationType . ProceduralModule ) . ToList ( )
168
+ , true ) ;
158
169
}
159
170
160
171
public Declaration FindSelectedDeclaration ( ICodePane activeCodePane )
@@ -228,38 +239,22 @@ public IEnumerable<Declaration> Members(QualifiedModuleName module)
228
239
return _declarations [ module ] ;
229
240
}
230
241
231
- private IEnumerable < Declaration > _nonBaseAsType ;
232
242
public IEnumerable < Declaration > FindDeclarationsWithNonBaseAsType ( )
233
243
{
234
- lock ( ThreadLock )
235
- {
236
- return _nonBaseAsType ?? ( _nonBaseAsType = _declarations . AllValues ( ) . Where ( d =>
237
- ! string . IsNullOrWhiteSpace ( d . AsTypeName )
238
- && ! d . AsTypeIsBaseType
239
- && d . DeclarationType != DeclarationType . Project
240
- && d . DeclarationType != DeclarationType . ProceduralModule ) . ToList ( ) ) ;
241
- }
242
- }
244
+ return _nonBaseAsType . Value ;
243
245
244
- private readonly Lazy < ConcurrentBag < Declaration > > _eventHandlers ;
246
+ }
247
+
245
248
public IEnumerable < Declaration > FindEventHandlers ( )
246
249
{
247
- lock ( ThreadLock )
248
- {
249
- return _eventHandlers . Value ;
250
- }
250
+ return _eventHandlers . Value ;
251
251
}
252
252
253
- private readonly Lazy < ConcurrentBag < Declaration > > _classes ;
254
-
255
253
public IEnumerable < Declaration > Classes
256
254
{
257
255
get
258
256
{
259
- lock ( ThreadLock )
260
- {
261
- return _classes . Value ;
262
- }
257
+ return _classes . Value ;
263
258
}
264
259
}
265
260
@@ -269,10 +264,7 @@ public IEnumerable<Declaration> Projects
269
264
{
270
265
get
271
266
{
272
- lock ( ThreadLock )
273
- {
274
- return _projects . Value ;
275
- }
267
+ return _projects . Value ;
276
268
}
277
269
}
278
270
@@ -290,10 +282,7 @@ public IEnumerable<Declaration> UserDeclarations(DeclarationType type)
290
282
291
283
public IEnumerable < UnboundMemberDeclaration > FreshUnresolvedMemberDeclarations ( )
292
284
{
293
- lock ( ThreadLock )
294
- {
295
- return _newUnresolved . ToArray ( ) ;
296
- }
285
+ return _newUnresolved . ToArray ( ) ; //This does not need a lock because enumerators over a ConcurrentBag uses a snapshot.
297
286
}
298
287
299
288
public IEnumerable < UnboundMemberDeclaration > UnresolvedMemberDeclarations ( )
@@ -338,19 +327,18 @@ public Declaration FindParameter(Declaration procedure, string parameterName)
338
327
public IEnumerable < Declaration > FindMemberMatches ( Declaration parent , string memberName )
339
328
{
340
329
ConcurrentBag < Declaration > children ;
341
- if ( _declarations . TryGetValue ( parent . QualifiedName . QualifiedModuleName , out children ) )
342
- {
343
- return children . Where ( item => item . DeclarationType . HasFlag ( DeclarationType . Member )
344
- && item . IdentifierName == memberName ) . ToList ( ) ;
345
- }
346
-
347
- return Enumerable . Empty < Declaration > ( ) ;
330
+ return _declarations . TryGetValue ( parent . QualifiedName . QualifiedModuleName , out children )
331
+ ? children . Where ( item => item . DeclarationType . HasFlag ( DeclarationType . Member )
332
+ && item . IdentifierName == memberName ) . ToList ( )
333
+ : Enumerable . Empty < Declaration > ( ) ;
348
334
}
349
335
350
336
public IEnumerable < IAnnotation > FindAnnotations ( QualifiedModuleName module )
351
337
{
352
338
ConcurrentBag < IAnnotation > result ;
353
- return _annotations . TryGetValue ( module , out result ) ? result : Enumerable . Empty < IAnnotation > ( ) ;
339
+ return _annotations . TryGetValue ( module , out result )
340
+ ? result
341
+ : Enumerable . Empty < IAnnotation > ( ) ;
354
342
}
355
343
356
344
public bool IsMatch ( string declarationName , string potentialMatchName )
@@ -405,7 +393,8 @@ public Declaration FindProject(string name, Declaration currentScope = null)
405
393
Declaration result = null ;
406
394
try
407
395
{
408
- result = MatchName ( name ) . SingleOrDefault ( project => project . DeclarationType . HasFlag ( DeclarationType . Project )
396
+ result = MatchName ( name ) . SingleOrDefault ( project =>
397
+ project . DeclarationType . HasFlag ( DeclarationType . Project )
409
398
&& ( currentScope == null || project . ProjectId == currentScope . ProjectId ) ) ;
410
399
}
411
400
catch ( InvalidOperationException exception )
@@ -416,7 +405,7 @@ public Declaration FindProject(string name, Declaration currentScope = null)
416
405
return result ;
417
406
}
418
407
419
- public Declaration FindStdModule ( string name , Declaration parent = null , bool includeBuiltIn = false )
408
+ public Declaration FindStdModule ( string name , Declaration parent , bool includeBuiltIn = false )
420
409
{
421
410
Debug . Assert ( parent != null ) ;
422
411
Declaration result = null ;
@@ -435,7 +424,7 @@ public Declaration FindStdModule(string name, Declaration parent = null, bool in
435
424
return result ;
436
425
}
437
426
438
- public Declaration FindClassModule ( string name , Declaration parent = null , bool includeBuiltIn = false )
427
+ public Declaration FindClassModule ( string name , Declaration parent , bool includeBuiltIn = false )
439
428
{
440
429
Debug . Assert ( parent != null ) ;
441
430
Declaration result = null ;
0 commit comments