Skip to content

Commit f0b44b4

Browse files
authored
Code cleanup and some optimizations (#1911)
* Code cleanup * Run symbol conversion in parallel * Fix build error in p/invoke move * Fix debug compile * Address review comments
1 parent 7fd8727 commit f0b44b4

File tree

26 files changed

+523
-555
lines changed

26 files changed

+523
-555
lines changed

src/AST/Class.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,31 @@ public enum ClassType
7474
// Represents a C++ record decl.
7575
public class Class : DeclarationContext
7676
{
77-
public List<BaseClassSpecifier> Bases;
78-
public List<Field> Fields;
79-
public List<Property> Properties;
80-
public List<Method> Methods;
81-
public List<AccessSpecifierDecl> Specifiers;
77+
public List<BaseClassSpecifier> Bases = new();
78+
public List<Field> Fields = new();
79+
public List<Property> Properties = new();
80+
public List<Method> Methods = new();
81+
public List<AccessSpecifierDecl> Specifiers = new();
8282

8383
// True if the record is a POD (Plain Old Data) type.
84-
public bool IsPOD;
84+
public bool IsPOD = false;
8585

8686
// Semantic type of the class.
87-
public ClassType Type;
87+
public ClassType Type = ClassType.RefType;
8888

8989
// ABI-specific class layout.
90-
public ClassLayout Layout;
90+
public ClassLayout Layout = new();
9191

9292
// True if class provides pure virtual methods.
93-
public bool IsAbstract;
93+
public bool IsAbstract = false;
9494

9595
// True if the type is to be treated as a union.
96-
public bool IsUnion;
96+
public bool IsUnion = false;
9797

9898
public TagKind TagKind { get; set; }
9999

100100
// True if the class is final / sealed.
101-
public bool IsFinal { get; set; }
101+
public bool IsFinal { get; set; } = false;
102102

103103
private bool? isOpaque;
104104

@@ -129,23 +129,6 @@ public bool IsOpaque
129129
// True if the class represents a static class.
130130
public bool IsStatic;
131131

132-
public Class()
133-
{
134-
Bases = new List<BaseClassSpecifier>();
135-
Fields = new List<Field>();
136-
Properties = new List<Property>();
137-
Methods = new List<Method>();
138-
Specifiers = new List<AccessSpecifierDecl>();
139-
IsAbstract = false;
140-
IsUnion = false;
141-
IsFinal = false;
142-
IsPOD = false;
143-
Type = ClassType.RefType;
144-
Layout = new ClassLayout();
145-
templateParameters = new List<Declaration>();
146-
specializations = new List<ClassTemplateSpecialization>();
147-
}
148-
149132
public bool HasBase => Bases.Count > 0;
150133

151134
public bool HasBaseClass => BaseClass != null;
@@ -296,7 +279,7 @@ public override T Visit<T>(IDeclVisitor<T> visitor)
296279
return visitor.VisitClassDecl(this);
297280
}
298281

299-
private List<Declaration> templateParameters;
300-
private List<ClassTemplateSpecialization> specializations;
282+
private readonly List<Declaration> templateParameters = new();
283+
private readonly List<ClassTemplateSpecialization> specializations = new();
301284
}
302285
}

src/AST/DeclarationsList.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ protected override void RemoveItem(int index)
5959
base.RemoveItem(index);
6060
for (var i = Kind.Namespace; i <= Kind.Event; i++)
6161
{
62-
if (offsets.ContainsKey(i) && index < offsets[i])
62+
if (!offsets.TryGetValue(i, out int start))
63+
continue;
64+
65+
if (index < start)
6366
{
6467
offsets[i]--;
6568
}
@@ -74,12 +77,11 @@ protected override void ClearItems()
7477

7578
private IEnumerable<T> OfType<T>(Kind kind) where T : Declaration
7679
{
77-
if (!offsets.ContainsKey(kind))
80+
if (!offsets.TryGetValue(kind, out var offset))
7881
{
7982
yield break;
8083
}
8184

82-
var offset = offsets[kind];
8385
for (var i = GetStart(kind); i < offset; i++)
8486
{
8587
yield return (T)this[i];
@@ -105,28 +107,30 @@ private static Kind GetKind(Declaration item)
105107

106108
private int GetOffset(Kind kind)
107109
{
108-
if (offsets.ContainsKey(kind))
109-
return offsets[kind];
110+
if (offsets.TryGetValue(kind, out int o))
111+
return o;
110112

113+
// First time we see this kind of declaration. Insert it between the previous and next kinds.
111114
for (var i = kind - 1; i >= Kind.Namespace; i--)
112115
{
113-
if (offsets.ContainsKey(i))
114-
{
115-
return offsets[kind] = offsets[i];
116-
}
116+
if (!offsets.TryGetValue(i, out var start))
117+
continue;
118+
119+
offsets[kind] = start;
120+
return start;
117121
}
118122

119123
offsets[kind] = 0;
120-
return offsets[kind];
124+
return 0;
121125
}
122126

123127
private int GetStart(Kind kind)
124128
{
125129
for (var i = kind - 1; i >= Kind.Namespace; i--)
126130
{
127-
if (offsets.ContainsKey(i))
131+
if (offsets.TryGetValue(i, out var start))
128132
{
129-
return offsets[i];
133+
return start;
130134
}
131135
}
132136
return 0;
@@ -154,7 +158,7 @@ private int BinarySearch(int start, int end, Declaration item)
154158
return middle;
155159
}
156160

157-
private Dictionary<Kind, int> offsets = new Dictionary<Kind, int>();
161+
private readonly Dictionary<Kind, int> offsets = new();
158162

159163
private enum Kind
160164
{

src/AST/Namespace.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ public abstract class DeclarationContext : Declaration
3636
// True if the context is inside an extern "C" context.
3737
public bool IsExternCContext;
3838

39-
public override string LogicalName
40-
{
41-
get { return IsAnonymous ? "<anonymous>" : base.Name; }
42-
}
39+
public override string LogicalName => IsAnonymous ? "<anonymous>" : base.Name;
4340

44-
public override string LogicalOriginalName
45-
{
46-
get { return IsAnonymous ? "<anonymous>" : base.OriginalName; }
47-
}
41+
public override string LogicalOriginalName => IsAnonymous ? "<anonymous>" : base.OriginalName;
4842

4943
protected DeclarationContext()
5044
{
@@ -80,7 +74,7 @@ public IEnumerable<DeclarationContext> GatherParentNamespaces()
8074

8175
public Declaration FindAnonymous(ulong key)
8276
{
83-
return Anonymous.ContainsKey(key) ? Anonymous[key] : null;
77+
return Anonymous.GetValueOrDefault(key);
8478
}
8579

8680
public Namespace FindNamespace(string name)
@@ -172,7 +166,7 @@ public IEnumerable<Function> FindFunction(string name, bool createDecl = false)
172166
{
173167
var functions = Functions.Where(e => e.Name.Equals(name));
174168

175-
if (!functions.Any() && createDecl)
169+
if (createDecl && !functions.Any())
176170
{
177171
var function = new Function() { Name = name, Namespace = this };
178172
Declarations.Add(function);

src/AST/SymbolContext.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ public void IndexSymbols()
8080
{
8181
foreach (var symbol in library.Symbols)
8282
{
83-
if (!Symbols.ContainsKey(symbol))
84-
Symbols[symbol] = library;
85-
if (symbol.StartsWith("__", StringComparison.Ordinal))
86-
{
87-
string stripped = symbol.Substring(1);
88-
if (!Symbols.ContainsKey(stripped))
89-
Symbols[stripped] = library;
90-
}
83+
Symbols.TryAdd(symbol, library);
84+
85+
if (!symbol.StartsWith("__", StringComparison.Ordinal))
86+
continue;
87+
88+
Symbols.TryAdd(symbol[1..], library);
9189
}
9290
}
9391
}

src/Core/Platform.cs

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,83 +17,68 @@ public enum TargetPlatform
1717

1818
public static class Platform
1919
{
20-
public static bool IsWindows
21-
{
22-
get
20+
public static bool IsWindows =>
21+
Environment.OSVersion.Platform switch
2322
{
24-
switch (Environment.OSVersion.Platform)
25-
{
26-
case PlatformID.Win32NT:
27-
case PlatformID.Win32S:
28-
case PlatformID.Win32Windows:
29-
case PlatformID.WinCE:
30-
return true;
31-
}
23+
PlatformID.Win32NT => true,
24+
PlatformID.Win32S => true,
25+
PlatformID.Win32Windows => true,
26+
PlatformID.WinCE => true,
27+
_ => false
28+
};
3229

33-
return false;
34-
}
35-
}
36-
37-
[DllImport("libc")]
38-
static extern int uname(IntPtr buf);
30+
public static bool IsMacOS { get; } = GetIsMacOS();
3931

40-
public static bool IsMacOS
32+
private static bool GetIsMacOS()
4133
{
42-
get
43-
{
44-
if (Environment.OSVersion.Platform != PlatformID.Unix)
45-
return false;
34+
if (Environment.OSVersion.Platform != PlatformID.Unix)
35+
return false;
4636

47-
IntPtr buf = Marshal.AllocHGlobal(8192);
48-
if (uname(buf) == 0)
37+
IntPtr buf = Marshal.AllocHGlobal(8192);
38+
if (uname(buf) == 0)
39+
{
40+
string os = Marshal.PtrToStringAnsi(buf);
41+
switch (os)
4942
{
50-
string os = Marshal.PtrToStringAnsi(buf);
51-
switch (os)
52-
{
53-
case "Darwin":
54-
return true;
55-
}
43+
case "Darwin":
44+
return true;
5645
}
57-
Marshal.FreeHGlobal(buf);
58-
59-
return false;
6046
}
61-
}
47+
Marshal.FreeHGlobal(buf);
6248

63-
public static bool IsLinux
64-
{
65-
get { return Environment.OSVersion.Platform == PlatformID.Unix && !IsMacOS; }
66-
}
49+
return false;
6750

68-
public static bool IsMono
69-
{
70-
get { return Type.GetType("Mono.Runtime") != null; }
51+
[DllImport("libc")]
52+
static extern int uname(IntPtr buf);
7153
}
7254

55+
public static bool IsLinux => Environment.OSVersion.Platform == PlatformID.Unix && !IsMacOS;
56+
57+
public static bool IsMono => Type.GetType("Mono.Runtime") != null;
58+
7359
public static bool IsUnixPlatform
7460
{
7561
get
7662
{
7763
var platform = Environment.OSVersion.Platform;
78-
return platform == PlatformID.Unix || platform == PlatformID.MacOSX;
64+
return platform is PlatformID.Unix or PlatformID.MacOSX;
7965
}
8066
}
8167

82-
public static TargetPlatform Host
68+
public static TargetPlatform Host { get; } = GetHostPlatform();
69+
70+
private static TargetPlatform GetHostPlatform()
8371
{
84-
get
85-
{
86-
if (IsWindows)
87-
return TargetPlatform.Windows;
72+
if (IsWindows)
73+
return TargetPlatform.Windows;
8874

89-
if (IsMacOS)
90-
return TargetPlatform.MacOS;
75+
if (IsMacOS)
76+
return TargetPlatform.MacOS;
9177

92-
if (IsLinux)
93-
return TargetPlatform.Linux;
78+
if (IsLinux)
79+
return TargetPlatform.Linux;
9480

95-
throw new NotImplementedException();
96-
}
81+
throw new NotImplementedException();
9782
}
9883
}
9984
}

src/CppParser/AST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ ASTContext::ASTContext() {}
935935

936936
ASTContext::~ASTContext() {}
937937

938-
TranslationUnit* ASTContext::FindOrCreateModule(std::string File)
938+
TranslationUnit* ASTContext::FindOrCreateModule(const std::string& File)
939939
{
940940
auto normalizedFile = normalizePath(File);
941941

src/CppParser/ASTNameMangler.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ASTNameMangler::ASTNameMangler(ASTContext& Ctx)
4040
{
4141
}
4242

43-
std::string ASTNameMangler::GetName(const Decl* D) {
43+
std::string ASTNameMangler::GetName(const Decl* D) const {
4444
std::string Name;
4545
{
4646
llvm::raw_string_ostream OS(Name);
@@ -49,8 +49,7 @@ std::string ASTNameMangler::GetName(const Decl* D) {
4949
return Name;
5050
}
5151

52-
bool ASTNameMangler::WriteName(const Decl* D, raw_ostream& OS) {
53-
// First apply frontend mangling.
52+
bool ASTNameMangler::WriteName(const Decl* D, raw_ostream& OS) const {
5453
if (auto* FD = dyn_cast<FunctionDecl>(D)) {
5554
if (FD->isDependentContext())
5655
return true;
@@ -62,8 +61,7 @@ bool ASTNameMangler::WriteName(const Decl* D, raw_ostream& OS) {
6261
return true;
6362
}
6463
else if (auto* MD = dyn_cast<ObjCMethodDecl>(D)) {
65-
MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
66-
/*includeCategoryNamespace=*/true);
64+
MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false, /*includeCategoryNamespace=*/true);
6765
return false;
6866
}
6967
else if (auto* ID = dyn_cast<ObjCInterfaceDecl>(D)) {
@@ -76,7 +74,7 @@ bool ASTNameMangler::WriteName(const Decl* D, raw_ostream& OS) {
7674
return false;
7775
}
7876

79-
std::string ASTNameMangler::GetMangledStructor(const NamedDecl* ND, unsigned StructorType) {
77+
std::string ASTNameMangler::GetMangledStructor(const NamedDecl* ND, unsigned StructorType) const {
8078
std::string FrontendBuf;
8179
llvm::raw_string_ostream FOS(FrontendBuf);
8280

@@ -90,7 +88,7 @@ std::string ASTNameMangler::GetMangledStructor(const NamedDecl* ND, unsigned Str
9088
return FrontendBuf;
9189
}
9290

93-
std::string ASTNameMangler::GetMangledThunk(const CXXMethodDecl* MD, const ThunkInfo& T, bool /*ElideOverrideInfo*/) {
91+
std::string ASTNameMangler::GetMangledThunk(const CXXMethodDecl* MD, const ThunkInfo& T, bool /*ElideOverrideInfo*/) const {
9492
std::string FrontendBuf;
9593
llvm::raw_string_ostream FOS(FrontendBuf);
9694

0 commit comments

Comments
 (0)