-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
CppEnum.cs
118 lines (98 loc) · 3.56 KB
/
CppEnum.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
// 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++ standard or scoped enum.
/// </summary>
public sealed class CppEnum : CppTypeDeclaration, ICppMemberWithVisibility, ICppAttributeContainer
{
/// <summary>
/// Creates a new instance of this enum.
/// </summary>
/// <param name="name">Name of this enum</param>
public CppEnum(string name) : base(CppTypeKind.Enum)
{
Name = name;
Items = new CppContainerList<CppEnumItem>(this);
Attributes = new List<CppAttribute>();
TokenAttributes = new List<CppAttribute>();
}
/// <inheritdoc />
public CppVisibility Visibility { get; set; }
/// <inheritdoc />
public string Name { get; set; }
public override string FullName
{
get
{
string fullparent = FullParentName;
if (string.IsNullOrEmpty(fullparent))
{
return Name;
}
else
{
return $"{fullparent}::{Name}";
}
}
}
/// <summary>
/// Gets or sets a boolean indicating if this enum is scoped.
/// </summary>
public bool IsScoped { get; set; }
/// <summary>
/// Gets or sets the underlying integer type of this enum.
/// </summary>
public CppType IntegerType { get; set; }
/// <summary>
/// Gets the definition of the enum items.
/// </summary>
public CppContainerList<CppEnumItem> Items { get; }
public bool IsAnonymous { get; set; }
/// <summary>
/// Gets the list of 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; private set; } = new MetaAttributeMap();
/// <inheritdoc />
public override int SizeOf
{
get => IntegerType?.SizeOf ?? 0;
set => throw new InvalidOperationException("Cannot set the SizeOf an enum as it is determined only by the SizeOf of its underlying IntegerType");
}
/// <inheritdoc />
public override CppType GetCanonicalType() => IntegerType;
/// <inheritdoc />
public override IEnumerable<ICppDeclaration> Children() => Items;
/// <inheritdoc />
public override string ToString()
{
var builder = new StringBuilder();
if (Visibility != CppVisibility.Default)
{
builder.Append(Visibility.ToString().ToLowerInvariant());
builder.Append(" ");
}
builder.Append("enum ");
if (IsScoped)
{
builder.Append("class ");
}
builder.Append(Name);
if (IntegerType != null && !(IntegerType is CppPrimitiveType primitive && primitive.Kind == CppPrimitiveKind.Int))
{
builder.Append(": ");
builder.Append(IntegerType.GetDisplayName());
}
builder.Append(" {...}");
return builder.ToString();
}
}
}