/
Declaration.cs
618 lines (553 loc) · 22.1 KB
/
Declaration.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
using Antlr4.Runtime;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.ComReflection;
using Rubberduck.Parsing.Grammar;
using Rubberduck.VBEditor;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
namespace Rubberduck.Parsing.Symbols
{
/// <summary>
/// Defines a declared identifier.
/// </summary>
[DebuggerDisplay("({DeclarationType}) {Accessibility} {IdentifierName} As {AsTypeName} | {Selection}")]
public class Declaration : IEquatable<Declaration>
{
public const int MaxModuleNameLength = 31;
public const int MaxMemberNameLength = 255;
public Declaration(
QualifiedMemberName qualifiedName,
Declaration parentDeclaration,
Declaration parentScope,
string asTypeName,
string typeHint,
bool isSelfAssigned,
bool isWithEvents,
Accessibility accessibility,
DeclarationType declarationType,
ParserRuleContext context,
ParserRuleContext attributesPassContext,
Selection selection,
bool isArray,
VBAParser.AsTypeClauseContext asTypeContext,
bool isUserDefined = true,
IEnumerable<IAnnotation> annotations = null,
Attributes attributes = null,
bool undeclared = false)
: this(
qualifiedName,
parentDeclaration,
parentScope?.Scope,
asTypeName,
typeHint,
isSelfAssigned,
isWithEvents,
accessibility,
declarationType,
context,
attributesPassContext,
selection,
isArray,
asTypeContext,
isUserDefined,
annotations,
attributes)
{
ParentScopeDeclaration = parentScope;
IsUndeclared = undeclared;
}
public Declaration(
QualifiedMemberName qualifiedName,
Declaration parentDeclaration,
string parentScope,
string asTypeName,
string typeHint,
bool isSelfAssigned,
bool isWithEvents,
Accessibility accessibility,
DeclarationType declarationType,
bool isArray,
VBAParser.AsTypeClauseContext asTypeContext,
bool isUserDefined = true,
IEnumerable<IAnnotation> annotations = null,
Attributes attributes = null)
: this(
qualifiedName,
parentDeclaration,
parentScope,
asTypeName,
typeHint,
isSelfAssigned,
isWithEvents,
accessibility,
declarationType,
null,
null,
Selection.Home,
isArray,
asTypeContext,
isUserDefined,
annotations,
attributes)
{ }
public Declaration(
QualifiedMemberName qualifiedName,
Declaration parentDeclaration,
string parentScope,
string asTypeName,
string typeHint,
bool isSelfAssigned,
bool isWithEvents,
Accessibility accessibility,
DeclarationType declarationType,
ParserRuleContext context,
ParserRuleContext attributesPassContext,
Selection selection,
bool isArray,
VBAParser.AsTypeClauseContext asTypeContext,
bool isUserDefined = true,
IEnumerable<IAnnotation> annotations = null,
Attributes attributes = null)
{
QualifiedName = qualifiedName;
ParentDeclaration = parentDeclaration;
ParentScopeDeclaration = ParentDeclaration;
ParentScope = parentScope ?? string.Empty;
IdentifierName = qualifiedName.MemberName;
AsTypeName = asTypeName;
IsSelfAssigned = isSelfAssigned;
IsWithEvents = isWithEvents;
Accessibility = accessibility;
DeclarationType = declarationType;
Selection = selection;
Context = context;
AttributesPassContext = attributesPassContext;
IsUserDefined = isUserDefined;
_annotations = annotations;
_attributes = attributes ?? new Attributes();
ProjectId = QualifiedName.QualifiedModuleName.ProjectId;
var projectDeclaration = GetProjectParent(parentDeclaration);
if (projectDeclaration != null)
{
ProjectName = projectDeclaration.IdentifierName;
}
else if (DeclarationType == DeclarationType.Project)
{
ProjectName = IdentifierName;
}
IsArray = isArray;
AsTypeContext = asTypeContext;
TypeHint = typeHint;
}
public Declaration(ComEnumeration enumeration, Declaration parent, QualifiedModuleName module) : this(
module.QualifyMemberName(enumeration.Name),
parent,
parent,
"Long",
//Match the VBA default type declaration. Technically these *can* be a LongLong on 64 bit systems, but would likely crash the VBE...
null,
false,
false,
Accessibility.Global,
DeclarationType.Enumeration,
null,
null,
Selection.Home,
false,
null,
false,
null,
new Attributes()) { }
public Declaration(ComStruct structure, Declaration parent, QualifiedModuleName module)
: this(
module.QualifyMemberName(structure.Name),
parent,
parent,
structure.Name,
null,
false,
false,
Accessibility.Global,
DeclarationType.UserDefinedType,
null,
null,
Selection.Home,
false,
null,
false,
null,
new Attributes()) { }
public Declaration(ComEnumerationMember member, Declaration parent, QualifiedModuleName module) : this(
module.QualifyMemberName(member.Name),
parent,
parent,
parent.IdentifierName,
null,
false,
false,
Accessibility.Global,
DeclarationType.EnumerationMember,
null,
null,
Selection.Home,
false,
null,
false,
null,
new Attributes()) { }
public Declaration(ComField field, Declaration parent, QualifiedModuleName module)
: this(
module.QualifyMemberName(field.Name),
parent,
parent,
field.ValueType,
null,
false,
false,
Accessibility.Global,
field.Type,
null,
null,
Selection.Home,
false,
null,
false,
null,
new Attributes()) { }
public static Declaration GetModuleParent(Declaration declaration)
{
if (declaration == null)
{
return null;
}
if (declaration.DeclarationType.HasFlag(DeclarationType.ClassModule) || declaration.DeclarationType == DeclarationType.ProceduralModule)
{
return declaration;
}
return GetModuleParent(declaration.ParentDeclaration);
}
public static Declaration GetProjectParent(Declaration declaration)
{
if (declaration == null)
{
return null;
}
if (declaration.DeclarationType == DeclarationType.Project)
{
return declaration;
}
return GetProjectParent(declaration.ParentDeclaration);
}
public bool IsArray { get; }
public VBAParser.AsTypeClauseContext AsTypeContext { get; }
public string TypeHint { get; }
public bool HasTypeHint => !string.IsNullOrWhiteSpace(TypeHint);
public bool IsTypeSpecified => HasTypeHint || AsTypeContext != null;
public bool IsUserDefined { get; }
public Declaration ParentDeclaration { get; }
public QualifiedMemberName QualifiedName { get; }
public QualifiedModuleName QualifiedModuleName => QualifiedName.QualifiedModuleName;
public ParserRuleContext Context { get; }
public ParserRuleContext AttributesPassContext { get; }
private ConcurrentDictionary<IdentifierReference, int> _references = new ConcurrentDictionary<IdentifierReference, int>();
public IEnumerable<IdentifierReference> References => _references.Keys;
protected IEnumerable<IAnnotation> _annotations;
public IEnumerable<IAnnotation> Annotations => _annotations ?? new List<IAnnotation>();
private readonly Attributes _attributes;
public Attributes Attributes => _attributes;
/// <summary>
/// Gets an attribute value that contains the docstring for a member.
/// </summary>
public string DescriptionString
{
get
{
string literalDescription;
var memberAttribute = _attributes.SingleOrDefault(a => a.Name == $"{IdentifierName}.VB_Description");
if (memberAttribute != null)
{
literalDescription = memberAttribute.Values.SingleOrDefault() ?? string.Empty;
return CorrectlyFormatedDescription(literalDescription);
}
var moduleAttribute = _attributes.SingleOrDefault(a => a.Name == "VB_Description");
if (moduleAttribute != null)
{
literalDescription = moduleAttribute.Values.SingleOrDefault() ?? string.Empty;
return CorrectlyFormatedDescription(literalDescription);
}
return string.Empty;
}
}
private static string CorrectlyFormatedDescription(string literalDescription)
{
if (string.IsNullOrEmpty(literalDescription)
|| literalDescription.Length < 2
|| literalDescription[0] != '"'
|| literalDescription[literalDescription.Length -1] != '"')
{
return literalDescription;
}
var text = literalDescription.Substring(1, literalDescription.Length - 2);
return text.Replace("\"\"", "\"");
}
/// <summary>
/// Gets an attribute value indicating whether a member is an enumerator provider.
/// Types with such a member support For Each iteration.
/// </summary>
public bool IsEnumeratorMember => _attributes.Any(a => a.Name.EndsWith("VB_UserMemId") && a.Values.Contains("-4"));
public virtual bool IsObject
{
get
{
if (AsTypeName == Tokens.Object ||
(AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false))
{
return true;
}
var isIntrinsic = AsTypeIsBaseType
|| IsArray
|| (AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.UserDefinedType) ?? false)
|| (AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.Enumeration) ?? false);
return !isIntrinsic;
}
}
public void AddReference(
QualifiedModuleName module,
Declaration scope,
Declaration parent,
ParserRuleContext callSiteContext,
string identifier,
Declaration callee,
Selection selection,
IEnumerable<IAnnotation> annotations,
bool isAssignmentTarget = false,
bool hasExplicitLetStatement = false,
bool isSetAssigned = false
)
{
var oldReference = _references.FirstOrDefault(r =>
r.Key.QualifiedModuleName == module &&
// ReSharper disable once PossibleUnintendedReferenceComparison
r.Key.ParentScoping == scope &&
// ReSharper disable once PossibleUnintendedReferenceComparison
r.Key.ParentNonScoping == parent &&
r.Key.IdentifierName == identifier &&
r.Key.Selection == selection);
if (oldReference.Key != null)
{
_references.TryRemove(oldReference.Key, out _);
}
var newReference = new IdentifierReference(
module,
scope,
parent,
identifier,
selection,
callSiteContext,
callee,
isAssignmentTarget,
hasExplicitLetStatement,
annotations,
isSetAssigned);
_references.AddOrUpdate(newReference, 1, (key, value) => 1);
}
/// <summary>
/// Gets a <c>Selection</c> representing the position of the declaration in the code module.
/// </summary>
/// <remarks>
/// Returns <c>default(Selection)</c> for module identifiers.
/// </remarks>
public Selection Selection { get; }
public QualifiedSelection QualifiedSelection => new QualifiedSelection(QualifiedName.QualifiedModuleName, Selection);
/// <summary>
/// Gets a reference to the VBProject the declaration is made in.
/// </summary>
/// <remarks>
/// This property is intended to differenciate identically-named VBProjects.
/// </remarks>
public virtual IVBProject Project => ParentDeclaration.Project;
/// <summary>
/// Gets a unique identifier for the VBProject the declaration is made in.
/// </summary>
public string ProjectId { get; }
public string ProjectName { get; }
/// <summary>
/// WARNING: This property has side effects. It changes the ActiveVBProject, which causes a flicker in the VBE.
/// This should only be called if it is *absolutely* necessary.
/// </summary>
public virtual string ProjectDisplayName => ParentDeclaration.ProjectDisplayName;
public object[] ToArray()
{
return new object[] { ProjectName, CustomFolder, ComponentName, DeclarationType.ToString(), Scope, IdentifierName, AsTypeName };
}
/// <summary>
/// Gets the name of the VBComponent the declaration is made in.
/// </summary>
public string ComponentName => QualifiedName.QualifiedModuleName.ComponentName;
/// <summary>
/// Gets the parent scope of the declaration.
/// </summary>
public string ParentScope { get; }
/// <summary>
/// Gets the <see cref="Declaration"/> object representing the parent scope of this declaration.
/// </summary>
public Declaration ParentScopeDeclaration { get; }
/// <summary>
/// Gets the declared name of the identifier.
/// </summary>
public string IdentifierName { get; }
/// <summary>
/// Gets the name of the declared type.
/// </summary>
/// <remarks>
/// This value is <c>null</c> if not applicable,
/// and <c>Variant</c> if applicable but unspecified.
/// </remarks>
public string AsTypeName { get; }
public string AsTypeNameWithoutArrayDesignator
{
get
{
if (string.IsNullOrWhiteSpace(AsTypeName))
{
return AsTypeName;
}
return AsTypeName.Replace("(", "").Replace(")", "").Trim();
}
}
public bool AsTypeIsBaseType => string.IsNullOrWhiteSpace(AsTypeName) || SymbolList.BaseTypes.Contains(AsTypeName.ToUpperInvariant());
private Declaration _asTypeDeclaration;
public Declaration AsTypeDeclaration
{
get { return _asTypeDeclaration; }
internal set
{
_asTypeDeclaration = value;
IsSelfAssigned = IsSelfAssigned || (DeclarationType == DeclarationType.Variable &&
AsTypeDeclaration.DeclarationType == DeclarationType.UserDefinedType);
}
}
private readonly IReadOnlyList<DeclarationType> _neverArray = new[]
{
DeclarationType.ClassModule,
DeclarationType.Control,
DeclarationType.Document,
DeclarationType.Enumeration,
DeclarationType.EnumerationMember,
DeclarationType.Event,
DeclarationType.Function,
DeclarationType.LibraryFunction,
DeclarationType.LibraryProcedure,
DeclarationType.LineLabel,
DeclarationType.ProceduralModule,
DeclarationType.Project,
DeclarationType.Procedure,
DeclarationType.PropertyGet,
DeclarationType.PropertyLet,
DeclarationType.PropertyLet,
DeclarationType.UserDefinedType,
DeclarationType.Constant
};
public bool IsSelected(QualifiedSelection selection)
{
return QualifiedName.QualifiedModuleName == selection.QualifiedName &&
Selection.ContainsFirstCharacter(selection.Selection);
}
/// <summary>
/// Gets a value indicating whether the declaration is a joined assignment (e.g. "As New xxxxx")
/// </summary>
public bool IsSelfAssigned { get; private set; }
/// <summary>
/// Gets a value specifying the declaration's visibility.
/// This value is used in determining the declaration's scope.
/// </summary>
public Accessibility Accessibility { get; }
/// <summary>
/// Gets a value specifying the type of declaration.
/// </summary>
public DeclarationType DeclarationType { get; }
/// <summary>
/// Gets a value specifying whether the declared type is an event provider.
/// </summary>
/// <remarks>
/// WithEvents declarations are used to identify event handler procedures in a module.
/// </remarks>
public bool IsWithEvents { get; }
/// <summary>
/// Returns a string representing the scope of an identifier.
/// </summary>
public string Scope
{
get
{
switch (DeclarationType)
{
case DeclarationType.Project:
return "VBE";
case DeclarationType.ClassModule:
case DeclarationType.Document:
case DeclarationType.ProceduralModule:
return QualifiedModuleName.ToString();
case DeclarationType.Procedure:
case DeclarationType.Function:
return $"{QualifiedModuleName}.{IdentifierName}";
case DeclarationType.PropertyGet:
return $"{QualifiedModuleName}.{IdentifierName}.Get";
case DeclarationType.PropertyLet:
return $"{QualifiedModuleName}.{IdentifierName}.Let";
case DeclarationType.PropertySet:
return $"{QualifiedModuleName}.{IdentifierName}.Set";
case DeclarationType.Event:
return $"{ParentScope}.{IdentifierName}";
default:
return ParentScope;
}
}
}
/// <summary>
/// Indicates whether the declaration is an ad-hoc declaration created by the resolver.
/// </summary>
public bool IsUndeclared { get; }
public virtual string CustomFolder => ParentDeclaration?.CustomFolder ?? ProjectName;
public bool Equals(Declaration other)
{
return other != null
&& other.ProjectId == ProjectId
&& other.IdentifierName == IdentifierName
&& other.DeclarationType == DeclarationType
&& other.Scope == Scope
&& other.ParentScope == ParentScope
&& other.Selection.Equals(Selection);
}
public override bool Equals(object obj)
{
return Equals(obj as Declaration);
}
public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = hash * 23 + QualifiedName.QualifiedModuleName.GetHashCode();
hash = hash * 23 + IdentifierName.GetHashCode();
hash = hash * 23 + DeclarationType.GetHashCode();
hash = hash * 23 + Scope.GetHashCode();
hash = hash * 23 + ParentScope.GetHashCode();
hash = hash * 23 + Selection.GetHashCode();
return hash;
}
}
public void ClearReferences()
{
_references = new ConcurrentDictionary<IdentifierReference, int>();
}
public void RemoveReferencesFrom(IReadOnlyCollection<QualifiedModuleName> modulesByWhichToRemoveReferences)
{
_references = new ConcurrentDictionary<IdentifierReference, int>(_references.Where(reference => !modulesByWhichToRemoveReferences.Contains(reference.Key.QualifiedModuleName)));
}
}
}