-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathDeclaration.cs
400 lines (335 loc) · 13.4 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace CppSharp.AST
{
public interface IRedeclarableDecl
{
Declaration PreviousDecl { get; }
}
public interface ITypedDecl
{
Type Type { get; }
QualifiedType QualifiedType { get; set; }
}
public interface INamedDecl
{
string Name { get; set; }
}
public interface IMangledDecl
{
string Mangled { get; set; }
}
/// <summary>
/// Kind of the generated declaration
/// </summary>
public enum GenerationKind
{
/// <summary>
/// Declaration is not generated.
/// </summary>
None,
/// <summary>
/// Declaration is generated.
/// </summary>
Generate,
/// <summary>
/// Declaration is generated to be used internally.
/// </summary>
Internal
}
public class DeclarationBase
{
public bool IsInvalid { get; set; }
protected GenerationKind? generationKind;
public virtual GenerationKind GenerationKind
{
get => generationKind ?? GenerationKind.Generate;
set => generationKind = value;
}
/// <summary>
/// Whether the declaration should be generated.
/// </summary>
public bool IsGenerated => GenerationKind == GenerationKind.Generate;
/// <summary>
/// Whether the declaration has an explicit set generation kind.
/// </summary>
public bool HasExplicitGenerationKind => generationKind.HasValue;
/// <summary>
/// Whether the declaration was explicitly set to be generated via
/// the GenerationKind property as opposed to the default generated state.
/// </summary>
public bool IsExplicitlyGenerated => generationKind == GenerationKind.Generate;
/// <summary>
/// Whether the declaration internal bindings should be generated.
/// </summary>
public bool IsInternal => GenerationKind == GenerationKind.Internal;
/// <summary>
/// Whether a binded version of this declaration is available.
/// </summary>
public bool IsDeclared => GenerationKind == GenerationKind.Generate
|| GenerationKind == GenerationKind.Internal;
public void ExplicitlyIgnore()
{
GenerationKind = GenerationKind.None;
}
[Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")]
public bool ExplicityIgnored
{
get => GenerationKind == GenerationKind.None;
set { if (value) ExplicitlyIgnore(); }
}
public virtual bool Ignore
{
get => GenerationKind == GenerationKind.None;
set { if (value) ExplicitlyIgnore(); }
}
}
/// <summary>
/// Represents a C++ declaration.
/// </summary>
[DebuggerDisplay("{ToString()} [{GetType().Name}]")]
public abstract class Declaration : DeclarationBase, INamedDecl
{
public SourceLocation Location;
public int LineNumberStart { get; set; }
public int LineNumberEnd { get; set; }
public bool IsImplicit { get; set; }
public int AlignAs { get; set; }
public int MaxFieldAlignment { get; set; }
private DeclarationContext @namespace;
public DeclarationContext OriginalNamespace;
// Namespace the declaration is contained in.
public DeclarationContext Namespace
{
get => @namespace;
set
{
@namespace = value;
OriginalNamespace ??= @namespace;
}
}
public TranslationUnit TranslationUnit
{
get
{
if (this is TranslationUnit)
return this as TranslationUnit;
return Namespace.TranslationUnit;
}
}
public override GenerationKind GenerationKind
{
get
{
if (generationKind.HasValue)
return generationKind.Value;
if (Namespace != null)
// fields in nested classes have to always be generated
return !Namespace.IsGenerated && this is Field ?
GenerationKind.Internal : Namespace.GenerationKind;
return GenerationKind.Generate;
}
}
public virtual string OriginalName { get; set; }
// Name of the declaration.
private string name;
public virtual string Name
{
get => name;
set
{
name = value;
OriginalName ??= name;
}
}
/// <summary>
/// The effective name of a declaration is the logical name
/// for the declaration. We need this to make the distiction between
/// the real and the "effective" name of the declaration to properly
/// support things like inline namespaces when handling type maps.
/// </summary>
public virtual string LogicalName => Name;
/// <summary>
/// The effective original name of a declaration is the logical
/// original name for the declaration.
/// </summary>
public virtual string LogicalOriginalName => OriginalName;
public static string QualifiedNameSeparator = "::";
public string GetQualifiedName(Func<Declaration, string> getName,
Func<Declaration, DeclarationContext> getNamespace, bool getOriginal)
{
DeclarationContext @namespace = getNamespace(this);
if (@namespace == null)
return getName(this);
if (@namespace.IsRoot)
return getName(this);
var namespaces = GatherNamespaces(@namespace, getOriginal);
var names = namespaces.Select(getName).ToList();
names.Add(getName(this));
names = names.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
return string.Join(QualifiedNameSeparator, names);
}
public static IEnumerable<Declaration> GatherNamespaces(DeclarationContext @namespace, bool getOriginal)
{
var namespaces = new Stack<Declaration>();
var currentNamespace = @namespace;
while (currentNamespace != null)
{
var isInlineNamespace = currentNamespace is Namespace { IsInline: true };
if (!isInlineNamespace)
namespaces.Push(currentNamespace);
currentNamespace = getOriginal ? currentNamespace.OriginalNamespace : currentNamespace.Namespace;
}
return namespaces;
}
public string QualifiedName
{
get
{
return GetQualifiedName(decl => GetDeclName(decl, decl.Name), decl => decl.Namespace, false);
}
}
public string QualifiedOriginalName
{
get
{
return GetQualifiedName(
decl => GetDeclName(decl, decl.OriginalName), decl => decl.OriginalNamespace, true);
}
}
public string QualifiedLogicalName
{
get
{
return GetQualifiedName(
decl => GetDeclName(decl, decl.LogicalName), decl => decl.Namespace, false);
}
}
public string QualifiedLogicalOriginalName
{
get
{
return GetQualifiedName(
decl => GetDeclName(decl, decl.LogicalOriginalName), decl => decl.OriginalNamespace, true);
}
}
private static string GetDeclName(Declaration decl, string name)
{
if (decl is ClassTemplateSpecialization specialization)
return string.Format("{0}<{1}>", name,
string.Join(", ", specialization.Arguments.Select(
a => a.Type.Type == null ? string.Empty : a.Type.ToString())));
return name;
}
public Declaration AssociatedDeclaration { get; set; }
// Comment associated with declaration.
public RawComment Comment;
public AccessSpecifier Access { get; set; }
// Contains debug text about the declaration.
public string DebugText;
// True if the declaration is incomplete (no definition).
public bool IsIncomplete;
// True if the declaration is dependent.
public bool IsDependent;
// True if the declaration is deprecated.
public bool IsDeprecated;
// Keeps a reference to the complete version of this declaration.
public Declaration CompleteDeclaration;
// Tracks the original declaration definition order.
public uint DefinitionOrder;
// Passes that should not be run on this declaration.
public ISet<System.Type> ExcludeFromPasses;
// List of preprocessed entities attached to this declaration.
public IList<PreprocessedEntity> PreprocessedEntities;
// Pointer to the original declaration from Clang.
public IntPtr OriginalPtr;
// The Unified Symbol Resolution (USR) for the declaration.
// A Unified Symbol Resolution (USR) is a string that identifies a
// particular entity (function, class, variable, etc.) within a program.
// USRs can be compared across translation units to determine, e.g.,
// when references in one translation refer to an entity defined in
// another translation unit.
public string USR;
public List<Attribute> Attributes { get; private set; }
public List<Declaration> Redeclarations { get; } = new List<Declaration>();
// Custom declaration map for custom code generation.
public object DeclMap { get; set; }
protected Declaration()
{
Access = AccessSpecifier.Public;
ExcludeFromPasses = new HashSet<System.Type>();
PreprocessedEntities = new List<PreprocessedEntity>();
Attributes = new List<Attribute>();
}
protected Declaration(string name)
: this()
{
this.name = name;
}
protected Declaration(Declaration declaration)
: this()
{
Namespace = declaration.Namespace;
OriginalNamespace = declaration.OriginalNamespace;
OriginalName = declaration.OriginalName;
name = declaration.Name;
Comment = declaration.Comment;
generationKind = declaration.generationKind;
Access = declaration.Access;
DebugText = declaration.DebugText;
IsIncomplete = declaration.IsIncomplete;
IsDependent = declaration.IsDependent;
CompleteDeclaration = declaration.CompleteDeclaration;
DefinitionOrder = declaration.DefinitionOrder;
ExcludeFromPasses = new HashSet<System.Type>(
declaration.ExcludeFromPasses);
PreprocessedEntities = new List<PreprocessedEntity>(
declaration.PreprocessedEntities);
OriginalPtr = declaration.OriginalPtr;
LineNumberStart = declaration.LineNumberStart;
LineNumberEnd = declaration.LineNumberEnd;
IsImplicit = declaration.IsImplicit;
AssociatedDeclaration = declaration.AssociatedDeclaration;
DeclMap = declaration.DeclMap;
}
public override string ToString()
{
return OriginalName;
}
public abstract T Visit<T>(IDeclVisitor<T> visitor);
}
public interface IDeclVisitor<out T>
{
T VisitDeclaration(Declaration decl);
T VisitTranslationUnit(TranslationUnit unit);
T VisitClassDecl(Class @class);
T VisitFieldDecl(Field field);
T VisitFunctionDecl(Function function);
T VisitMethodDecl(Method method);
T VisitParameterDecl(Parameter parameter);
T VisitTypedefNameDecl(TypedefNameDecl typedef);
T VisitTypedefDecl(TypedefDecl typedef);
T VisitTypeAliasDecl(TypeAlias typeAlias);
T VisitEnumDecl(Enumeration @enum);
T VisitEnumItemDecl(Enumeration.Item item);
T VisitVariableDecl(Variable variable);
T VisitMacroDefinition(MacroDefinition macro);
T VisitNamespace(Namespace @namespace);
T VisitEvent(Event @event);
T VisitProperty(Property @property);
T VisitFriend(Friend friend);
T VisitClassTemplateDecl(ClassTemplate template);
T VisitClassTemplateSpecializationDecl(ClassTemplateSpecialization specialization);
T VisitFunctionTemplateDecl(FunctionTemplate template);
T VisitFunctionTemplateSpecializationDecl(FunctionTemplateSpecialization specialization);
T VisitVarTemplateDecl(VarTemplate template);
T VisitVarTemplateSpecializationDecl(VarTemplateSpecialization template);
T VisitTemplateTemplateParameterDecl(TemplateTemplateParameter templateTemplateParameter);
T VisitTemplateParameterDecl(TypeTemplateParameter templateParameter);
T VisitNonTypeTemplateParameterDecl(NonTypeTemplateParameter nonTypeTemplateParameter);
T VisitTypeAliasTemplateDecl(TypeAliasTemplate typeAliasTemplate);
T VisitUnresolvedUsingDecl(UnresolvedUsingTypename unresolvedUsingTypename);
}
}