-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
CppClass.cs
262 lines (221 loc) · 8.38 KB
/
CppClass.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
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Text;
namespace CppAst
{
/// <summary>
/// A C++ class, struct or union.
/// </summary>
public sealed class CppClass : CppTypeDeclaration, ICppMemberWithVisibility, ICppDeclarationContainer, ICppTemplateOwner
{
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="name">Name of this type.</param>
public CppClass(string name) : base(CppTypeKind.StructOrClass)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
BaseTypes = new List<CppBaseType>();
Fields = new CppContainerList<CppField>(this);
Constructors = new CppContainerList<CppFunction>(this);
Destructors = new CppContainerList<CppFunction>(this);
Functions = new CppContainerList<CppFunction>(this);
Enums = new CppContainerList<CppEnum>(this);
Classes = new CppContainerList<CppClass>(this);
Typedefs = new CppContainerList<CppTypedef>(this);
TemplateParameters = new List<CppType>();
Attributes = new List<CppAttribute>();
TokenAttributes = new List<CppAttribute>();
}
/// <summary>
/// Kind of the instance (`class` `struct` or `union`)
/// </summary>
public CppClassKind ClassKind { get; set; }
public CppTemplateKind TemplateKind { get; set; }
/// <inheritdoc />
public string Name { get; set; }
public override string FullName
{
get
{
StringBuilder sb = new StringBuilder();
string fullparent = FullParentName;
if (string.IsNullOrEmpty(fullparent))
{
sb.Append(Name);
}
else
{
sb.Append($"{fullparent}::{Name}");
}
if (TemplateKind == CppTemplateKind.TemplateClass
|| TemplateKind == CppTemplateKind.PartialTemplateClass)
{
sb.Append('<');
for (int i = 0; i < TemplateParameters.Count; i++)
{
var tp = TemplateParameters[i];
if (i != 0)
{
sb.Append(", ");
}
sb.Append(tp.ToString());
}
sb.Append('>');
}
else if (TemplateKind == CppTemplateKind.TemplateSpecializedClass)
{
sb.Append('<');
for (int i = 0; i < TemplateSpecializedArguments.Count; i++)
{
var ta = TemplateSpecializedArguments[i];
if (i != 0)
{
sb.Append(", ");
}
sb.Append(ta.ArgString);
}
sb.Append('>');
}
//else if(TemplateKind == CppTemplateKind.PartialTemplateClass)
//{
// sb.Append('<');
// sb.Append('>');
//}
return sb.ToString();
}
}
/// <inheritdoc />
public CppVisibility Visibility { get; set; }
/// <inheritdoc />
public List<CppAttribute> Attributes { get; }
[Obsolete("TokenAttributes is deprecated. please use system attributes and annotate attributes")]
public List<CppAttribute> TokenAttributes { get; }
public MetaAttributeMap MetaAttributes { get; private set; } = new MetaAttributeMap();
/// <summary>
/// Gets or sets a boolean indicating if this type is a definition. If <c>false</c> the type was only declared but is not defined.
/// </summary>
public bool IsDefinition { get; set; }
/// <summary>
/// Gets or sets a boolean indicating if this declaration is anonymous.
/// </summary>
public bool IsAnonymous { get; set; }
/// <summary>
/// Get the base types of this type.
/// </summary>
public List<CppBaseType> BaseTypes { get; }
/// <inheritdoc />
public CppContainerList<CppField> Fields { get; }
/// <summary>
/// Gets the constructors of this instance.
/// </summary>
public CppContainerList<CppFunction> Constructors { get; set; }
/// <summary>
/// Gets the destructors of this instance.
/// </summary>
public CppContainerList<CppFunction> Destructors { get; set; }
/// <inheritdoc />
public CppContainerList<CppFunction> Functions { get; }
/// <inheritdoc />
public CppContainerList<CppEnum> Enums { get; }
/// <inheritdoc />
public CppContainerList<CppClass> Classes { get; }
/// <inheritdoc />
public CppContainerList<CppTypedef> Typedefs { get; }
/// <inheritdoc />
public List<CppType> TemplateParameters { get; }
public List<CppTemplateArgument> TemplateSpecializedArguments { get; } = new List<CppTemplateArgument>();
/// <summary>
/// Gets the specialized class template of this instance.
/// </summary>
public CppClass SpecializedTemplate { get; set; }
public bool IsEmbeded => Parent is CppClass;
public bool IsAbstract { get; set; }
/// <inheritdoc />
public override int SizeOf { get; set; }
/// <summary>
/// Gets the alignment of this instance.
/// </summary>
public int AlignOf { get; set; }
/// <inheritdoc />
public override CppType GetCanonicalType()
{
return this;
}
/// <inheritdoc />
public override string ToString()
{
var builder = new StringBuilder();
switch (ClassKind)
{
case CppClassKind.Class:
builder.Append("class ");
break;
case CppClassKind.Struct:
builder.Append("struct ");
break;
case CppClassKind.Union:
builder.Append("union ");
break;
default:
throw new ArgumentOutOfRangeException();
}
if (!string.IsNullOrEmpty(Name))
{
builder.Append(Name);
}
if (BaseTypes.Count > 0)
{
builder.Append(" : ");
for (var i = 0; i < BaseTypes.Count; i++)
{
var baseType = BaseTypes[i];
if (i > 0) builder.Append(", ");
builder.Append(baseType);
}
}
//Add template arguments here
if(TemplateKind != CppTemplateKind.NormalClass)
{
builder.Append("<");
if(TemplateKind == CppTemplateKind.TemplateSpecializedClass)
{
for(var i = 0; i < TemplateSpecializedArguments.Count; i++)
{
if(i > 0) builder.Append(", ");
builder.Append(TemplateSpecializedArguments[i].ToString());
}
}
else if(TemplateKind == CppTemplateKind.TemplateClass)
{
for (var i = 0; i < TemplateParameters.Count; i++)
{
if (i > 0) builder.Append(", ");
builder.Append(TemplateParameters[i].ToString());
}
}
builder.Append(">");
}
builder.Append(" { ... }");
return builder.ToString();
}
public override IEnumerable<ICppDeclaration> Children()
{
foreach (var item in CppContainerHelper.Children(this))
{
yield return item;
}
foreach (var item in Constructors)
{
yield return item;
}
foreach (var item in Destructors)
{
yield return item;
}
}
}
}