/
ClassModuleDeclaration.cs
264 lines (227 loc) · 9.61 KB
/
ClassModuleDeclaration.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
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.ComReflection;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Rubberduck.Parsing.Symbols
{
public class ClassModuleDeclaration : ModuleDeclaration
{
private readonly List<string> _supertypeNames;
private readonly HashSet<Declaration> _supertypes;
private readonly ConcurrentDictionary<Declaration, byte> _subtypes;
private readonly Lazy<bool> _isExtensible;
private readonly Lazy<bool> _isExposed;
private readonly Lazy<bool> _hasPredeclaredId;
public ClassModuleDeclaration(
QualifiedMemberName qualifiedName,
Declaration projectDeclaration,
string name,
bool isUserDefined,
IEnumerable<IAnnotation> annotations,
Attributes attributes,
bool isWithEvents = false,
bool hasDefaultInstanceVariable = false,
bool isControl = false,
bool isDocument = false)
: base(
qualifiedName,
projectDeclaration,
name,
isDocument ? DeclarationType.Document : DeclarationType.ClassModule,
isUserDefined,
annotations,
attributes,
isWithEvents)
{
_supertypeNames = new List<string>();
_supertypes = new HashSet<Declaration>();
_subtypes = new ConcurrentDictionary<Declaration, byte>();
IsControl = isControl;
_isExtensible = new Lazy<bool>(IsExtensibleToCache);
_isExposed = new Lazy<bool>(IsExposedToCache);
_hasPredeclaredId = hasDefaultInstanceVariable
? new Lazy<bool>(() => true)
: new Lazy<bool>(HasPredeclaredIdToCache);
}
// skip IDispatch.. just about everything implements it and RD doesn't need to care about it; don't care about IUnknown either
private static readonly HashSet<string> IgnoredInterfaces = new HashSet<string>(new[] { "IDispatch", "IUnknown" });
public ClassModuleDeclaration(ComCoClass coClass, Declaration parent, QualifiedModuleName module,
Attributes attributes)
: this(
module.QualifyMemberName(coClass.Name),
parent,
coClass.Name,
false,
new List<IAnnotation>(),
attributes,
coClass.EventInterfaces.Any(),
coClass.IsAppObject,
coClass.IsControl)
{
_supertypeNames =
coClass.ImplementedInterfaces.Where(i => !i.IsRestricted && !IgnoredInterfaces.Contains(i.Name))
.Select(i => i.Name)
.ToList();
_supertypes = new HashSet<Declaration>();
_subtypes = new ConcurrentDictionary<Declaration, byte>();
_isExtensible = new Lazy<bool>(IsExtensibleToCache);
_isExposed = new Lazy<bool>(IsExposedToCache);
_hasPredeclaredId = new Lazy<bool>(HasPredeclaredIdToCache);
}
public ClassModuleDeclaration(ComInterface @interface, Declaration parent, QualifiedModuleName module,
Attributes attributes)
: this(
module.QualifyMemberName(@interface.Name),
parent,
@interface.Name,
false,
new List<IAnnotation>(),
attributes)
{ }
public static IEnumerable<Declaration> GetSupertypes(Declaration type)
{
if (type.DeclarationType != DeclarationType.ClassModule)
{
return new List<Declaration>();
}
return type is ClassModuleDeclaration classType ? classType.Supertypes : new List<Declaration>();
}
public static bool HasDefaultMember(Declaration type)
{
var classModule = type as ClassModuleDeclaration;
return classModule?.DefaultMember != null;
}
public bool IsExtensible => _isExtensible.Value;
private bool IsExtensibleToCache()
{
return HasAttribute("VB_Customizable");
}
/// <summary>
/// Gets an attribute value indicating whether a class is exposed to other projects.
/// If this value is false, any public types and members cannot be accessed from outside the project they're declared in.
/// </summary>
public bool IsExposed => _isExposed.Value;
private bool IsExposedToCache()
{
return !IsUserDefined ? IsExposedForBuiltInModules : HasAttribute("VB_Exposed");
}
// TODO: This should only be a boolean in VBA ('Private' (false) and 'PublicNotCreatable' (true)) . For VB6 it will also need to support
// 'SingleUse', 'GlobalSingleUse', 'MultiUse', and 'GlobalMultiUse'. See https://msdn.microsoft.com/en-us/library/aa234184%28v=vs.60%29.aspx
// All built-ins are public (by definition).
private static bool IsExposedForBuiltInModules { get; } = true;
public bool IsControl { get; }
private bool HasAttribute(string attributeName)
{
var hasAttribute = false;
var attribute = Attributes.FirstOrDefault(a => a.Name == attributeName);
if (attribute != null)
{
hasAttribute = attribute.Values.Single() == "True";
}
return hasAttribute;
}
private bool? _isGlobal;
private readonly object _isGlobalSyncObject = new object();
public bool IsGlobalClassModule
{
get
{
lock (_isGlobalSyncObject)
{
if (_isGlobal.HasValue)
{
return _isGlobal.Value;
}
_isGlobal = HasAttribute("VB_GlobalNamespace") || IsGlobalFromSubtypes();
return _isGlobal.Value;
}
}
}
private bool IsGlobalFromSubtypes()
{
return Subtypes.Any(subtype => subtype is ClassModuleDeclaration declaration && declaration.IsGlobalClassModule);
}
/// <summary>
/// Gets an attribute value indicating whether a class has a predeclared ID.
/// Such classes can be treated as "static classes", or as far as resolving is concerned, as standard modules.
/// </summary>
public bool HasPredeclaredId => _hasPredeclaredId.Value;
private bool HasPredeclaredIdToCache()
{
return HasAttribute("VB_PredeclaredId");
}
public bool HasDefaultInstanceVariable => HasPredeclaredId || IsGlobalClassModule;
public Declaration DefaultMember { get; internal set; }
//This is just convenience for the resolver to split gathering the names of the supertypes and resolving them.
//todo: Find a cleaner solution for this.
public IEnumerable<string> SupertypeNames => _supertypeNames;
public IEnumerable<Declaration> Supertypes => _supertypes;
public IEnumerable<Declaration> Subtypes => _subtypes.Keys;
public bool IsInterface => _subtypes.Count > 0;
public bool IsUserInterface => Subtypes.Any(s => s.IsUserDefined);
public IEnumerable<ClassModuleDeclaration> ImplementedInterfaces =>
_supertypes.Cast<ClassModuleDeclaration>().Where(type => type.Subtypes.Contains(this));
public void AddSupertypeName(string supertypeName)
{
_supertypeNames.Add(supertypeName);
}
public void AddSupertype(Declaration supertype)
{
(supertype as ClassModuleDeclaration)?.AddSubtype(this);
_supertypes.Add(supertype);
}
public void ClearSupertypes()
{
foreach (var supertype in _supertypes)
{
if (supertype is ClassModuleDeclaration classModule)
{
classModule.RemoveSubtype(this);
classModule.ClearMemberImplementationCache();
}
}
_supertypes.Clear();
ClearMemberImplementationCache();
}
private void ClearMemberImplementationCache()
{
foreach (var member in Members)
{
(member as ModuleBodyElementDeclaration)?.InvalidateInterfaceCache();
}
}
private void AddSubtype(Declaration subtype)
{
InvalidateCachedIsGlobal();
_subtypes.AddOrUpdate(subtype, 1, (key,value) => value);
}
private void RemoveSubtype(Declaration subtype)
{
InvalidateCachedIsGlobal();
byte dummy;
_subtypes.TryRemove(subtype, out dummy);
}
private void InvalidateCachedIsGlobal()
{
lock (_isGlobalSyncObject)
{
if (_isGlobal.HasValue)
{
InvalidateCachedIsGlobalForSupertypes(); //If _isGlobal is not set, it has no influence on the state of the supertypes.
_isGlobal = null;
}
}
}
private void InvalidateCachedIsGlobalForSupertypes()
{
foreach(var supertype in Supertypes)
{
(supertype as ClassModuleDeclaration)?.InvalidateCachedIsGlobal();
}
}
}
}