Skip to content

Commit f58ca3b

Browse files
committed
Minor code cleanups in CLITypeReference.cs.
1 parent f79e0ef commit f58ca3b

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

src/Generator/AST/ASTRecord.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public bool GetParent<T>(out T @out)
1717
{
1818
@out = default(T);
1919

20-
if (Parent == null)
21-
return false;
22-
23-
var v = Parent.Object;
20+
var v = Parent?.Object;
2421
if (!(v is T))
2522
return false;
2623

@@ -45,8 +42,7 @@ public bool GetAncestors<T>(ref Stack<object> ancestors)
4542
{
4643
ancestors.Push(this);
4744

48-
T value;
49-
if (GetParent(out value))
45+
if (GetParent(out T value))
5046
{
5147
ancestors.Push(value);
5248
return true;
@@ -59,10 +55,7 @@ public bool GetAncestors<T>(ref Stack<object> ancestors)
5955

6056
public class ASTRecord<T> : ASTRecord
6157
{
62-
public T Value
63-
{
64-
get { return (T) Object; }
65-
}
58+
public T Value => (T) Object;
6659

6760
public override string ToString()
6861
{
@@ -130,12 +123,10 @@ static class ASTRecordExtensions
130123
{
131124
public static bool IsBaseClass(this ASTRecord record)
132125
{
133-
Class decl;
134-
if (!record.GetParent(out decl))
126+
if (!record.GetParent(out Class decl))
135127
return false;
136128

137-
var recordDecl = record.Object as Class;
138-
return recordDecl != null && recordDecl == decl.BaseClass;
129+
return record.Object is Class recordDecl && recordDecl == decl.BaseClass;
139130
}
140131

141132
public static bool IsFieldValueType(this ASTRecord record)
@@ -145,25 +136,22 @@ public static bool IsFieldValueType(this ASTRecord record)
145136
return false;
146137

147138
var field = (Field)ancestors.Pop();
148-
149-
Class decl;
150-
return field.Type.Desugar().TryGetClass(out decl) && decl.IsValueType;
139+
return field.Type.Desugar().TryGetClass(out var decl) && decl.IsValueType;
151140
}
152141

153142
public static bool IsEnumNestedInClass(this ASTRecord<Declaration> record)
154143
{
155-
Enumeration @enum = record.Value as Enumeration;
144+
var @enum = record.Value as Enumeration;
156145
var typedDecl = record.Value as ITypedDecl;
157-
if (@enum != null
158-
|| (typedDecl?.Type?.TryGetEnum(out @enum)).GetValueOrDefault())
146+
if (@enum != null || (typedDecl?.Type?.TryGetEnum(out @enum)).GetValueOrDefault())
159147
{
160-
return @enum.Namespace is Class;
148+
return @enum?.Namespace is Class;
161149
}
162150

163151
return false;
164152
}
165153

166-
public static bool IsClassReturn(this ASTRecord record)
154+
public static bool FunctionReturnsClassByValue(this ASTRecord record)
167155
{
168156
var ancestors = new Stack<object>();
169157
if(!record.GetAncestors<Function>(ref ancestors))
@@ -177,8 +165,7 @@ public static bool IsClassReturn(this ASTRecord record)
177165

178166
public static bool IsDelegate(this ASTRecord record)
179167
{
180-
var typedef = record.Object as TypedefDecl;
181-
return typedef != null && typedef.Type.GetPointee() is FunctionType;
168+
return record.Object is TypedefDecl typedef && typedef.Type.GetPointee() is FunctionType;
182169
}
183170
}
184171

@@ -223,7 +210,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
223210
return false;
224211
}
225212

226-
public bool ShouldVisitChilds(Declaration decl)
213+
private bool ShouldVisitChilds(Declaration decl)
227214
{
228215
if (decl == translationUnit)
229216
return true;

src/Generator/Generators/CLI/CLITypeReferences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public bool IsIncludeInHeader(ASTRecord<Declaration> record)
164164
return false;
165165

166166
return record.IsBaseClass() || record.IsFieldValueType() || record.IsDelegate()
167-
|| record.IsEnumNestedInClass() || record.IsClassReturn();
167+
|| record.IsEnumNestedInClass() || record.FunctionReturnsClassByValue();
168168
}
169169

170170
public override bool VisitDeclaration(Declaration decl)

0 commit comments

Comments
 (0)