forked from xoofx/CppAst.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCppClass.cs
179 lines (151 loc) · 5.61 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
// 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);
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 CppContainerList<CppAttribute>(this);
}
/// <summary>
/// Kind of the instance (`class` `struct` or `union`)
/// </summary>
public CppClassKind ClassKind { get; set; }
/// <inheritdoc />
public string Name { get; set; }
/// <inheritdoc />
public CppVisibility Visibility { get; set; }
/// <inheritdoc />
public CppContainerList<CppAttribute> Attributes { get; }
/// <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; }
/// <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; }
/// <summary>
/// Gets the specialized class template of this instance.
/// </summary>
public CppClass SpecializedTemplate { get; set; }
private bool Equals(CppClass other)
{
return base.Equals(other) && Equals(Parent, other.Parent) && Name.Equals(other.Name);
}
/// <inheritdoc />
public override int SizeOf { get; set; }
/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppClass other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
int hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Name.GetHashCode();
foreach (var templateParameter in TemplateParameters)
{
hashCode = (hashCode * 397) ^ templateParameter.GetHashCode();
}
return hashCode;
}
}
/// <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);
}
}
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;
}
}
}
}