forked from xoofx/CppAst.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppFunction.cs
More file actions
187 lines (152 loc) · 5.74 KB
/
Copy pathCppFunction.cs
File metadata and controls
187 lines (152 loc) · 5.74 KB
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
// 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++ function/method declaration.
/// </summary>
public sealed class CppFunction : CppDeclaration, ICppMemberWithVisibility, ICppTemplateOwner, ICppContainer, ICppAttributeContainer
{
/// <summary>
/// Creates a new instance of a function/method with the specified name.
/// </summary>
/// <param name="name">Name of this function/method.</param>
public CppFunction(string name)
{
Name = name;
Parameters = new CppContainerList<CppParameter>(this);
TemplateParameters = new CppContainerList<CppType>(this);
Attributes = new List<CppAttribute>();
TokenAttributes = new List<CppAttribute>();
}
/// <inheritdoc />
public CppVisibility Visibility { get; set; }
/// <summary>
/// Gets or sets the calling convention.
/// </summary>
public CppCallingConvention CallingConvention { get; set; }
/// <summary>
/// Gets the attached attributes.
/// </summary>
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;} = new MetaAttributeMap();
/// <summary>
/// Gets or sets the storage qualifier.
/// </summary>
public CppStorageQualifier StorageQualifier { get; set; }
/// <summary>
/// Gets or sets the linkage kind
/// </summary>
public CppLinkageKind LinkageKind { get; set; }
/// <summary>
/// Gets or sets the return type.
/// </summary>
public CppType ReturnType { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether this method is a constructor method.
/// </summary>
public bool IsConstructor { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether this method is a destructor method.
/// </summary>
public bool IsDestructor { get; set; }
/// <inheritdoc />
public string Name { get; set; }
/// <summary>
/// Gets a list of the parameters.
/// </summary>
public CppContainerList<CppParameter> Parameters { get; }
public int DefaultParamCount
{
get
{
int default_count = 0;
foreach (var param in Parameters)
{
if(param.InitExpression != null)
{
default_count++;
}
}
return default_count;
}
}
/// <summary>
/// Gets or sets the source span of the function body implementation.
/// </summary>
public CppSourceSpan? BodySpan { get; set; }
/// <summary>
/// Gets or sets the flags of this function.
/// </summary>
public CppFunctionFlags Flags { get; set; }
public bool IsCxxClassMethod => ((int)Flags & (int)CppFunctionFlags.Method) != 0;
public bool IsPureVirtual => ((int)Flags & (int)CppFunctionFlags.Pure) != 0;
public bool IsVirtual => ((int)Flags & (int)CppFunctionFlags.Virtual) != 0;
public bool IsStatic => StorageQualifier == CppStorageQualifier.Static;
public bool IsConst => ((int)Flags & (int)CppFunctionFlags.Const) != 0;
public bool IsFunctionTemplate => ((int)Flags & (int)CppFunctionFlags.FunctionTemplate) != 0;
/// <inheritdoc />
public CppContainerList<CppType> TemplateParameters { get; }
/// <inheritdoc />
public override string ToString()
{
var builder = new StringBuilder();
if (Visibility != CppVisibility.Default)
{
builder.Append(Visibility.ToString().ToLowerInvariant());
builder.Append(" ");
}
if (StorageQualifier != CppStorageQualifier.None)
{
builder.Append(StorageQualifier.ToString().ToLowerInvariant());
builder.Append(" ");
}
if ((Flags & CppFunctionFlags.Virtual) != 0)
{
builder.Append("virtual ");
}
if (!IsConstructor)
{
if (ReturnType != null)
{
builder.Append(ReturnType.GetDisplayName());
builder.Append(" ");
}
else
{
builder.Append("void ");
}
}
builder.Append(Name);
builder.Append("(");
for (var i = 0; i < Parameters.Count; i++)
{
var param = Parameters[i];
if (i > 0) builder.Append(", ");
builder.Append(param);
}
if ((Flags & CppFunctionFlags.Variadic) != 0)
{
builder.Append(", ...");
}
builder.Append(")");
if ((Flags & CppFunctionFlags.Const) != 0)
{
builder.Append(" const");
}
if ((Flags & CppFunctionFlags.Pure) != 0)
{
builder.Append(" = 0");
}
return builder.ToString();
}
/// <inheritdoc />
public IEnumerable<ICppDeclaration> Children() => Parameters;
}
}