-
Notifications
You must be signed in to change notification settings - Fork 294
/
DeclarationFinder.cs
733 lines (642 loc) · 38.5 KB
/
DeclarationFinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
using NLog;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Binding;
using Rubberduck.VBEditor;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.Parsing.Grammar;
using Rubberduck.VBEditor.Application;
namespace Rubberduck.Parsing.Symbols
{
internal static class DictionaryExtensions
{
public static IEnumerable<TValue> AllValues<TKey, TValue>(
this ConcurrentDictionary<TKey, ConcurrentBag<TValue>> source)
{
return source.SelectMany(item => item.Value).ToList();
}
public static IEnumerable<TValue> AllValues<TKey, TValue>(
this IDictionary<TKey, IList<TValue>> source)
{
return source.SelectMany(item => item.Value).ToList();
}
public static ConcurrentDictionary<TKey, ConcurrentBag<TValue>> ToConcurrentDictionary<TKey, TValue>(this IEnumerable<IGrouping<TKey, TValue>> source)
{
return new ConcurrentDictionary<TKey, ConcurrentBag<TValue>>(source.Select(x => new KeyValuePair<TKey, ConcurrentBag<TValue>>(x.Key, new ConcurrentBag<TValue>(x))));
}
}
public class DeclarationFinder
{
private static readonly SquareBracketedNameComparer NameComparer = new SquareBracketedNameComparer();
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IHostApplication _hostApp;
private readonly AnnotationService _annotationService;
private readonly ConcurrentDictionary<string, ConcurrentBag<Declaration>> _declarationsByName;
private readonly ConcurrentDictionary<QualifiedModuleName, ConcurrentBag<Declaration>> _declarations;
private readonly ConcurrentDictionary<QualifiedMemberName, ConcurrentBag<Declaration>> _newUndeclared;
private readonly ConcurrentBag<UnboundMemberDeclaration> _newUnresolved;
private readonly List<UnboundMemberDeclaration> _unresolved;
private readonly ConcurrentDictionary<QualifiedModuleName, ConcurrentBag<IAnnotation>> _annotations;
private readonly ConcurrentDictionary<Declaration, ConcurrentBag<Declaration>> _parametersByParent;
private readonly ConcurrentDictionary<DeclarationType, ConcurrentBag<Declaration>> _userDeclarationsByType;
private readonly Lazy<ConcurrentDictionary<Declaration, Declaration[]>> _handlersByWithEventsField;
private readonly Lazy<ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>> _membersByImplementsContext;
private readonly Lazy<ConcurrentDictionary<Declaration, Declaration[]>> _interfaceMembers;
private static readonly object ThreadLock = new object();
public DeclarationFinder(IReadOnlyList<Declaration> declarations, IEnumerable<IAnnotation> annotations, IReadOnlyList<UnboundMemberDeclaration> unresolvedMemberDeclarations, IHostApplication hostApp = null)
{
_hostApp = hostApp;
_annotations = annotations.GroupBy(node => node.QualifiedSelection.QualifiedName).ToConcurrentDictionary();
_declarations = declarations.GroupBy(item => item.QualifiedName.QualifiedModuleName).ToConcurrentDictionary();
_declarationsByName = declarations.GroupBy(declaration => declaration.IdentifierName.ToLowerInvariant()).ToConcurrentDictionary();
_parametersByParent = declarations.Where(declaration => declaration.DeclarationType == DeclarationType.Parameter)
.GroupBy(declaration => declaration.ParentDeclaration).ToConcurrentDictionary();
_userDeclarationsByType = declarations.Where(declaration => !declaration.IsBuiltIn).GroupBy(declaration => declaration.DeclarationType).ToConcurrentDictionary();
_builtinEvents = new Lazy<ConcurrentBag<Declaration>>(() => FindBuiltInEventHandlers(declarations), true);
_projects = _projects = new Lazy<ConcurrentBag<Declaration>>(() => new ConcurrentBag<Declaration>(declarations.Where(d => d.DeclarationType == DeclarationType.Project)), true);
_classes = new Lazy<ConcurrentBag<Declaration>>(() => new ConcurrentBag<Declaration>(declarations.Where(d => d.DeclarationType == DeclarationType.ClassModule)), true);
var withEventsFields = UserDeclarations(DeclarationType.Variable).Where(item => item.IsWithEvents).ToArray();
var events = withEventsFields.Select(field =>
new
{
WithEventsField = field,
AvailableEvents = FindEvents(field.AsTypeDeclaration).ToArray()
});
_handlersByWithEventsField = new Lazy<ConcurrentDictionary<Declaration, Declaration[]>>(() =>
new ConcurrentDictionary<Declaration, Declaration[]>(
events.Select(item =>
new
{
item.WithEventsField,
Handlers = item.AvailableEvents.SelectMany(evnt =>
_declarations[item.WithEventsField.ParentDeclaration.QualifiedName.QualifiedModuleName]
.Where(member => member.DeclarationType == DeclarationType.Procedure
&& member.IdentifierName == item.WithEventsField.IdentifierName + "_" + evnt.IdentifierName))
})
.ToDictionary(item => item.WithEventsField, item => item.Handlers.ToArray())
), true);
_newUndeclared = new ConcurrentDictionary<QualifiedMemberName, ConcurrentBag<Declaration>>(new Dictionary<QualifiedMemberName, ConcurrentBag<Declaration>>());
_newUnresolved = new ConcurrentBag<UnboundMemberDeclaration>(new List<UnboundMemberDeclaration>());
_unresolved = unresolvedMemberDeclarations.ToList();
_annotationService = new AnnotationService(this);
var implementsInstructions = UserDeclarations(DeclarationType.ClassModule).SelectMany(cls =>
cls.References.Where(reference => ParserRuleContextHelper.HasParent<VBAParser.ImplementsStmtContext>(reference.Context))
.Select(reference => new { IdentifierReference = reference, Context = ParserRuleContextHelper.GetParent<VBAParser.ImplementsStmtContext>(reference.Context)}));
var interfaceModules = implementsInstructions.Select(item => item.IdentifierReference.Declaration).Distinct();
var interfaceMembers = interfaceModules.Select(item => new
{
InterfaceModule = item,
InterfaceMembers = _declarations[item.QualifiedName.QualifiedModuleName]
.Where(member => member.DeclarationType.HasFlag(DeclarationType.Member))
});
_interfaceMembers = new Lazy<ConcurrentDictionary<Declaration, Declaration[]>>(() =>
new ConcurrentDictionary<Declaration, Declaration[]>(
interfaceMembers.ToDictionary(
item => item.InterfaceModule,
item => item.InterfaceMembers.ToArray()
)
)
, true);
var implementingNames = new Lazy<IEnumerable<string>>(() => implementsInstructions.SelectMany(item =>
_declarations[item.IdentifierReference.Declaration.QualifiedName.QualifiedModuleName]
.Where(member => member.DeclarationType.HasFlag(DeclarationType.Member))
.Select(member => item.IdentifierReference.Declaration.IdentifierName + "_" + member.IdentifierName)), true);
var implementableMembers = implementsInstructions.Select(item =>
new
{
item.Context,
Members = _declarations[item.IdentifierReference.QualifiedModuleName].Where(implementingTypeMember =>
implementingNames.Value.Contains(implementingTypeMember.IdentifierName)).ToArray()
});
_membersByImplementsContext = new Lazy<ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>>(() =>
new ConcurrentDictionary<VBAParser.ImplementsStmtContext, Declaration[]>(
implementableMembers.ToDictionary(item => item.Context, item => item.Members)), true);
}
public IEnumerable<Declaration> FreshUndeclared
{
get { return _newUndeclared.AllValues(); }
}
public IEnumerable<Declaration> Members(Declaration module)
{
return Members(module.QualifiedName.QualifiedModuleName);
}
public IEnumerable<Declaration> Members(QualifiedModuleName module)
{
return _declarations[module];
}
private IEnumerable<Declaration> _nonBaseAsType;
public IEnumerable<Declaration> FindDeclarationsWithNonBaseAsType()
{
lock (ThreadLock)
{
return _nonBaseAsType ?? (_nonBaseAsType = _declarations.AllValues().Where(d =>
!string.IsNullOrWhiteSpace(d.AsTypeName)
&& !d.AsTypeIsBaseType
&& d.DeclarationType != DeclarationType.Project
&& d.DeclarationType != DeclarationType.ProceduralModule).ToList());
}
}
private readonly Lazy<ConcurrentBag<Declaration>> _builtinEvents;
public IEnumerable<Declaration> FindBuiltinEventHandlers()
{
lock (ThreadLock)
{
return _builtinEvents.Value;
}
}
private readonly Lazy<ConcurrentBag<Declaration>> _classes;
public IEnumerable<Declaration> Classes
{
get
{
lock (ThreadLock)
{
return _classes.Value;
}
}
}
private readonly Lazy<ConcurrentBag<Declaration>> _projects;
public IEnumerable<Declaration> Projects
{
get
{
lock (ThreadLock)
{
return _projects.Value;
}
}
}
public IEnumerable<Declaration> UserDeclarations(DeclarationType type)
{
ConcurrentBag<Declaration> result;
if (!_userDeclarationsByType.TryGetValue(type, out result))
{
result = new ConcurrentBag<Declaration>(_userDeclarationsByType
.Where(item => item.Key.HasFlag(type))
.SelectMany(item => item.Value));
}
return result;
}
public IEnumerable<UnboundMemberDeclaration> FreshUnresolvedMemberDeclarations()
{
lock (ThreadLock)
{
return _newUnresolved.ToArray();
}
}
public IEnumerable<UnboundMemberDeclaration> UnresolvedMemberDeclarations()
{
return _unresolved.ToList();
}
public IEnumerable<Declaration> FindHandlersForWithEventsField(Declaration field)
{
Declaration[] result;
return _handlersByWithEventsField.Value.TryGetValue(field, out result)
? result
: Enumerable.Empty<Declaration>();
}
public IEnumerable<Declaration> FindInterfaceMembersForImplementsContext(VBAParser.ImplementsStmtContext context)
{
Declaration[] result;
return _membersByImplementsContext.Value.TryGetValue(context, out result)
? result
: Enumerable.Empty<Declaration>();
}
public IEnumerable<Declaration> FindAllInterfaceMembers()
{
return _interfaceMembers.Value.SelectMany(item => item.Value);
}
public IEnumerable<Declaration> FindAllInterfaceImplementingMembers()
{
return _membersByImplementsContext.Value.SelectMany(item => item.Value);
}
public Declaration FindParameter(Declaration procedure, string parameterName)
{
return _parametersByParent[procedure].SingleOrDefault(parameter => parameter.IdentifierName == parameterName);
}
public IEnumerable<Declaration> FindMemberMatches(Declaration parent, string memberName)
{
ConcurrentBag<Declaration> children;
if (_declarations.TryGetValue(parent.QualifiedName.QualifiedModuleName, out children))
{
return children.Where(item => item.DeclarationType.HasFlag(DeclarationType.Member)
&& item.IdentifierName == memberName).ToList();
}
return Enumerable.Empty<Declaration>();
}
public IEnumerable<IAnnotation> FindAnnotations(QualifiedModuleName module)
{
ConcurrentBag<IAnnotation> result;
return _annotations.TryGetValue(module, out result) ? result : Enumerable.Empty<IAnnotation>();
}
public bool IsMatch(string declarationName, string potentialMatchName)
{
return string.Equals(declarationName, potentialMatchName, StringComparison.OrdinalIgnoreCase);
}
private IEnumerable<Declaration> FindEvents(Declaration module)
{
Debug.Assert(module != null);
var members = _declarations[module.QualifiedName.QualifiedModuleName];
return members == null
? Enumerable.Empty<Declaration>()
: members.Where(declaration => declaration.DeclarationType == DeclarationType.Event).ToList();
}
public Declaration FindEvent(Declaration module, string eventName)
{
var matches = MatchName(eventName);
return matches.FirstOrDefault(m => module.Equals(Declaration.GetModuleParent(m)) && m.DeclarationType == DeclarationType.Event);
}
public Declaration FindLabel(Declaration procedure, string label)
{
var matches = MatchName(label);
return matches.FirstOrDefault(m => procedure.Equals(m.ParentDeclaration) && m.DeclarationType == DeclarationType.LineLabel);
}
public IEnumerable<Declaration> MatchName(string name)
{
var normalizedName = ToNormalizedName(name);
ConcurrentBag<Declaration> result;
return _declarationsByName.TryGetValue(normalizedName, out result)
? result
: Enumerable.Empty<Declaration>();
}
private string ToNormalizedName(string name)
{
var lower = name.ToLowerInvariant();
if (lower.Length > 1 && lower[0] == '[' && lower[lower.Length - 1] == ']')
{
var result = lower.Substring(1, lower.Length - 2);
return result;
}
return lower;
}
public Declaration FindProject(string name, Declaration currentScope = null)
{
Declaration result = null;
try
{
result = MatchName(name).SingleOrDefault(project => project.DeclarationType.HasFlag(DeclarationType.Project)
&& (currentScope == null || project.ProjectId == currentScope.ProjectId));
}
catch (InvalidOperationException exception)
{
Logger.Error(exception, "Multiple matches found for project '{0}'.", name);
}
return result;
}
public Declaration FindStdModule(string name, Declaration parent = null, bool includeBuiltIn = false)
{
Debug.Assert(parent != null);
Declaration result = null;
try
{
var matches = MatchName(name);
result = matches.SingleOrDefault(declaration => declaration.DeclarationType.HasFlag(DeclarationType.ProceduralModule)
&& (parent.Equals(declaration.ParentDeclaration))
&& (includeBuiltIn || !declaration.IsBuiltIn));
}
catch (InvalidOperationException exception)
{
Logger.Error(exception, "Multiple matches found for std.module '{0}'.", name);
}
return result;
}
public Declaration FindClassModule(string name, Declaration parent = null, bool includeBuiltIn = false)
{
Debug.Assert(parent != null);
Declaration result = null;
try
{
var matches = MatchName(name);
result = matches.SingleOrDefault(declaration => declaration.DeclarationType.HasFlag(DeclarationType.ClassModule)
&& (parent.Equals(declaration.ParentDeclaration))
&& (includeBuiltIn || !declaration.IsBuiltIn));
}
catch (InvalidOperationException exception)
{
Logger.Error(exception, "Multiple matches found for class module '{0}'.", name);
}
return result;
}
public Declaration FindReferencedProject(Declaration callingProject, string referencedProjectName)
{
return FindInReferencedProjectByPriority(callingProject, referencedProjectName, p => p.DeclarationType.HasFlag(DeclarationType.Project));
}
public Declaration FindModuleEnclosingProjectWithoutEnclosingModule(Declaration callingProject, Declaration callingModule, string calleeModuleName, DeclarationType moduleType)
{
var nameMatches = MatchName(calleeModuleName);
var moduleMatches = nameMatches.Where(m =>
m.DeclarationType.HasFlag(moduleType)
&& Declaration.GetProjectParent(m).Equals(callingProject)
&& !m.Equals(callingModule));
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindDefaultInstanceVariableClassEnclosingProject(Declaration callingProject, Declaration callingModule, string defaultInstanceVariableClassName)
{
var nameMatches = MatchName(defaultInstanceVariableClassName);
var moduleMatches = nameMatches.Where(m =>
m.DeclarationType == DeclarationType.ClassModule && ((ClassModuleDeclaration)m).HasDefaultInstanceVariable
&& Declaration.GetProjectParent(m).Equals(callingProject)).ToList();
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindModuleReferencedProject(Declaration callingProject, Declaration callingModule, string calleeModuleName, DeclarationType moduleType)
{
var moduleMatches = FindAllInReferencedProjectByPriority(callingProject, calleeModuleName, p => p.DeclarationType.HasFlag(moduleType));
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindModuleReferencedProject(Declaration callingProject, Declaration callingModule, Declaration referencedProject, string calleeModuleName, DeclarationType moduleType)
{
var moduleMatches = FindAllInReferencedProjectByPriority(callingProject, calleeModuleName, p => referencedProject.Equals(Declaration.GetProjectParent(p)) && p.DeclarationType.HasFlag(moduleType));
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindDefaultInstanceVariableClassReferencedProject(Declaration callingProject, Declaration callingModule, string calleeModuleName)
{
var moduleMatches = FindAllInReferencedProjectByPriority(callingProject, calleeModuleName, p => p.DeclarationType == DeclarationType.ClassModule && ((ClassModuleDeclaration)p).HasDefaultInstanceVariable);
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindDefaultInstanceVariableClassReferencedProject(Declaration callingProject, Declaration callingModule, Declaration referencedProject, string calleeModuleName)
{
var moduleMatches = FindAllInReferencedProjectByPriority(callingProject, calleeModuleName,
p => referencedProject.Equals(Declaration.GetProjectParent(p))
&& p.DeclarationType == DeclarationType.ClassModule
&& ((ClassModuleDeclaration)p).HasDefaultInstanceVariable);
var accessibleModules = moduleMatches.Where(calledModule => AccessibilityCheck.IsModuleAccessible(callingProject, callingModule, calledModule));
var match = accessibleModules.FirstOrDefault();
return match;
}
public Declaration FindMemberWithParent(Declaration callingProject, Declaration callingModule, Declaration callingParent, Declaration parent, string memberName, DeclarationType memberType)
{
var allMatches = MatchName(memberName);
var memberMatches = allMatches
.Where(m => m.DeclarationType.HasFlag(memberType)
&& parent.Equals(m.ParentDeclaration))
.ToList();
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
if (match != null)
{
return match;
}
return ClassModuleDeclaration
.GetSupertypes(parent)
.Select(supertype =>
FindMemberWithParent(callingProject, callingModule, callingParent, supertype, memberName, memberType))
.FirstOrDefault(supertypeMember => supertypeMember != null);
}
public Declaration FindMemberEnclosingModule(Declaration callingModule, Declaration callingParent, string memberName, DeclarationType memberType)
{
// We do not explicitly pass the callingProject here because we have to walk up the type hierarchy
// and thus the project differs depending on the callingModule.
var callingProject = Declaration.GetProjectParent(callingModule);
var allMatches = MatchName(memberName);
var memberMatches = allMatches
.Where(m => m.DeclarationType.HasFlag(memberType)
&& Declaration.GetProjectParent(m).Equals(callingProject)
&& callingModule.Equals(Declaration.GetModuleParent(m))
).ToList();
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
if (match != null)
{
return match;
}
// Classes such as Worksheet have properties such as Range that can be access in a user defined class such as Sheet1,
// that's why we have to walk the type hierarchy and find these implementations.
foreach (var supertype in ClassModuleDeclaration.GetSupertypes(callingModule))
{
// Only built-in classes such as Worksheet can be considered "real base classes".
// User created interfaces work differently and don't allow accessing accessing implementations.
if (!supertype.IsBuiltIn)
{
continue;
}
var supertypeMatch = FindMemberEnclosingModule(supertype, callingParent, memberName, memberType);
if (supertypeMatch != null)
{
return supertypeMatch;
}
}
return null;
}
public Declaration FindMemberEnclosingProcedure(Declaration enclosingProcedure, string memberName, DeclarationType memberType, ParserRuleContext onSiteContext = null)
{
if (memberType == DeclarationType.Variable && NameComparer.Equals(enclosingProcedure.IdentifierName, memberName))
{
return enclosingProcedure;
}
var allMatches = MatchName(memberName);
var memberMatches = allMatches.Where(m =>
m.DeclarationType.HasFlag(memberType)
&& enclosingProcedure.Equals(m.ParentDeclaration));
return memberMatches.FirstOrDefault();
}
public Declaration OnUndeclaredVariable(Declaration enclosingProcedure, string identifierName, ParserRuleContext context)
{
var annotations = _annotationService.FindAnnotations(enclosingProcedure.QualifiedName.QualifiedModuleName, context.Start.Line);
var undeclaredLocal =
new Declaration(
new QualifiedMemberName(enclosingProcedure.QualifiedName.QualifiedModuleName, identifierName),
enclosingProcedure, enclosingProcedure, "Variant", string.Empty, false, false,
Accessibility.Implicit, DeclarationType.Variable, context, context.GetSelection(), false, null,
false, annotations, null, true);
var hasUndeclared = _newUndeclared.ContainsKey(enclosingProcedure.QualifiedName);
if (hasUndeclared)
{
ConcurrentBag<Declaration> undeclared;
while (!_newUndeclared.TryGetValue(enclosingProcedure.QualifiedName, out undeclared))
{
_newUndeclared.TryGetValue(enclosingProcedure.QualifiedName, out undeclared);
}
var inScopeUndeclared = undeclared.FirstOrDefault(d => d.IdentifierName == identifierName);
if (inScopeUndeclared != null)
{
return inScopeUndeclared;
}
undeclared.Add(undeclaredLocal);
}
else
{
_newUndeclared.TryAdd(enclosingProcedure.QualifiedName, new ConcurrentBag<Declaration> { undeclaredLocal });
}
return undeclaredLocal;
}
public void AddUnboundContext(Declaration parentDeclaration, VBAParser.LExprContext context, IBoundExpression withExpression)
{
//The only forms we care about right now are MemberAccessExprContext or WithMemberAccessExprContext.
var access = (ParserRuleContext)context.GetChild<VBAParser.MemberAccessExprContext>(0)
?? context.GetChild<VBAParser.WithMemberAccessExprContext>(0);
if (access == null)
{
return;
}
var identifier = access.GetChild<VBAParser.UnrestrictedIdentifierContext>(0);
var annotations = _annotationService.FindAnnotations(parentDeclaration.QualifiedName.QualifiedModuleName, context.Start.Line);
var declaration = new UnboundMemberDeclaration(parentDeclaration, identifier,
(access is VBAParser.MemberAccessExprContext) ? (ParserRuleContext)access.children[0] : withExpression.Context,
annotations);
_newUnresolved.Add(declaration);
}
public Declaration OnBracketedExpression(string expression, ParserRuleContext context)
{
var hostApp = FindProject(_hostApp == null ? "VBA" : _hostApp.ApplicationName);
Debug.Assert(hostApp != null, "Host application project can't be null. Make sure VBA standard library is included if host is unknown.");
var qualifiedName = hostApp.QualifiedName.QualifiedModuleName.QualifyMemberName(expression);
ConcurrentBag<Declaration> undeclared;
if (_newUndeclared.TryGetValue(qualifiedName, out undeclared))
{
return undeclared.SingleOrDefault();
}
var item = new Declaration(qualifiedName, hostApp, hostApp, Tokens.Variant, string.Empty, false, false, Accessibility.Global, DeclarationType.BracketedExpression, context, context.GetSelection(), false, null);
_newUndeclared.TryAdd(qualifiedName, new ConcurrentBag<Declaration> { item });
return item;
}
public Declaration FindMemberEnclosedProjectWithoutEnclosingModule(Declaration callingProject, Declaration callingModule, Declaration callingParent, string memberName, DeclarationType memberType)
{
var allMatches = MatchName(memberName);
var memberMatches = allMatches.Where(m => m.DeclarationType.HasFlag(memberType)
&& (Declaration.GetModuleParent(m).DeclarationType == DeclarationType.ProceduralModule
|| memberType == DeclarationType.Enumeration
|| memberType == DeclarationType.EnumerationMember)
&& Declaration.GetProjectParent(m).Equals(callingProject)
&& !callingModule.Equals(Declaration.GetModuleParent(m)))
.ToList();
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
return match;
}
private static bool IsInstanceSensitive(DeclarationType memberType)
{
return memberType.HasFlag(DeclarationType.Procedure)
|| memberType.HasFlag(DeclarationType.Function)
|| memberType.HasFlag(DeclarationType.Variable)
|| memberType.HasFlag(DeclarationType.Constant);
}
public Declaration FindMemberEnclosedProjectInModule(Declaration callingProject, Declaration callingModule, Declaration callingParent, Declaration memberModule, string memberName, DeclarationType memberType)
{
var allMatches = MatchName(memberName);
var memberMatches = allMatches
.Where(m => m.DeclarationType.HasFlag(memberType)
&& Declaration.GetProjectParent(m).Equals(callingProject)
&& memberModule.Equals(Declaration.GetModuleParent(m)))
.ToList();
var match = memberMatches.FirstOrDefault(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
if (match != null)
{
return match;
}
return ClassModuleDeclaration
.GetSupertypes(memberModule)
.Select(supertype =>
FindMemberEnclosedProjectInModule(callingProject, callingModule, callingParent, supertype, memberName, memberType))
.FirstOrDefault(supertypeMember => supertypeMember != null);
}
public Declaration FindMemberReferencedProject(Declaration callingProject, Declaration callingModule, Declaration callingParent, string memberName, DeclarationType memberType)
{
var isInstanceSensitive = IsInstanceSensitive(memberType);
var memberMatches = FindAllInReferencedProjectByPriority(callingProject, memberName, p => (!isInstanceSensitive || Declaration.GetModuleParent(p) == null || Declaration.GetModuleParent(p).DeclarationType != DeclarationType.ClassModule) && p.DeclarationType.HasFlag(memberType));
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
return match;
}
public Declaration FindMemberReferencedProjectInModule(Declaration callingProject, Declaration callingModule, Declaration callingParent, DeclarationType moduleType, string memberName, DeclarationType memberType)
{
var memberMatches = FindAllInReferencedProjectByPriority(callingProject, memberName, p => p.DeclarationType.HasFlag(memberType) && (Declaration.GetModuleParent(p) == null || Declaration.GetModuleParent(p).DeclarationType == moduleType));
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
return match;
}
public Declaration FindMemberReferencedProjectInGlobalClassModule(Declaration callingProject, Declaration callingModule, Declaration callingParent, string memberName, DeclarationType memberType)
{
var memberMatches = FindAllInReferencedProjectByPriority(callingProject, memberName, p => p.DeclarationType.HasFlag(memberType) && (Declaration.GetModuleParent(p) == null || Declaration.GetModuleParent(p).DeclarationType == DeclarationType.ClassModule) && ((ClassModuleDeclaration)Declaration.GetModuleParent(p)).IsGlobalClassModule);
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
return match;
}
public Declaration FindMemberReferencedProjectInModule(Declaration callingProject, Declaration callingModule, Declaration callingParent, Declaration memberModule, string memberName, DeclarationType memberType)
{
var memberMatches = FindAllInReferencedProjectByPriority(callingProject, memberName, p => p.DeclarationType.HasFlag(memberType) && memberModule.Equals(Declaration.GetModuleParent(p)));
var accessibleMembers = memberMatches.Where(m => AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
var match = accessibleMembers.FirstOrDefault();
if (match != null)
{
return match;
}
return ClassModuleDeclaration
.GetSupertypes(memberModule)
.Select(supertype =>
FindMemberReferencedProjectInModule(callingProject, callingModule, callingParent, supertype, memberName, memberType))
.FirstOrDefault(supertypeMember => supertypeMember != null);
}
public Declaration FindMemberReferencedProject(Declaration callingProject, Declaration callingModule, Declaration callingParent, Declaration referencedProject, string memberName, DeclarationType memberType)
{
var memberMatches = FindAllInReferencedProjectByPriority(callingProject, memberName, p => p.DeclarationType.HasFlag(memberType) && referencedProject.Equals(Declaration.GetProjectParent(p)));
return memberMatches.FirstOrDefault(m =>
AccessibilityCheck.IsMemberAccessible(callingProject, callingModule, callingParent, m));
}
private Declaration FindInReferencedProjectByPriority(Declaration enclosingProject, string name, Func<Declaration, bool> predicate)
{
return FindAllInReferencedProjectByPriority(enclosingProject, name, predicate).FirstOrDefault();
}
private IEnumerable<Declaration> FindAllInReferencedProjectByPriority(Declaration enclosingProject, string name, Func<Declaration, bool> predicate)
{
var interprojectMatches = MatchName(name).Where(predicate).ToList();
var projectReferences = ((ProjectDeclaration)enclosingProject).ProjectReferences.ToList();
if (interprojectMatches.Count == 0)
{
yield break;
}
foreach (var projectReference in projectReferences)
{
var match = interprojectMatches.FirstOrDefault(interprojectMatch => interprojectMatch.ProjectId == projectReference.ReferencedProjectId);
if (match != null)
{
yield return match;
}
}
}
public ConcurrentBag<Declaration> FindBuiltInEventHandlers(IEnumerable<Declaration> declarations)
{
var declarationList = declarations.ToList();
var handlerNames = declarationList.Where(declaration => declaration.IsBuiltIn && declaration.DeclarationType == DeclarationType.Event)
.SelectMany(e =>
{
var parentModuleSubtypes = ((ClassModuleDeclaration)e.ParentDeclaration).Subtypes;
return parentModuleSubtypes.Any()
? parentModuleSubtypes.Select(v => v.IdentifierName + "_" + e.IdentifierName)
: new[] { e.ParentDeclaration.IdentifierName + "_" + e.IdentifierName };
});
var handlers = declarationList.Where(item =>
// class module built-in events
(item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule && (
item.IdentifierName.Equals("Class_Initialize", StringComparison.InvariantCultureIgnoreCase) ||
item.IdentifierName.Equals("Class_Terminate", StringComparison.InvariantCultureIgnoreCase))) ||
// standard module built-in handlers (Excel specific):
(_hostApp != null &&
_hostApp.ApplicationName.Equals("Excel", StringComparison.InvariantCultureIgnoreCase) &&
item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ProceduralModule && (
item.IdentifierName.Equals("auto_open", StringComparison.InvariantCultureIgnoreCase) ||
item.IdentifierName.Equals("auto_close", StringComparison.InvariantCultureIgnoreCase))) ||
// user handlers:
(!item.IsBuiltIn &&
item.DeclarationType == DeclarationType.Procedure &&
handlerNames.Contains(item.IdentifierName))
);
return new ConcurrentBag<Declaration>(handlers);
}
}
}