Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching various LINQ expressions to use OfType instead of Where(x is T).Cast #91

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ private void VisitParmVarDecl(ParmVarDecl parmVarDecl, TypedefDecl typedefDecl)
var name = GetRemappedCursorName(parmVarDecl);
_outputBuilder.Write(EscapeName(name));

var parameters = typedefDecl.CursorChildren.Where((cursor) => cursor is ParmVarDecl).Cast<ParmVarDecl>().ToList();
var parameters = typedefDecl.CursorChildren.OfType<ParmVarDecl>().ToList();
var index = parameters.IndexOf(parmVarDecl);
var lastIndex = parameters.Count - 1;

Expand Down Expand Up @@ -1680,7 +1680,7 @@ private void VisitTypedefDeclForPointeeType(TypedefDecl typedefDecl, Type pointe
_outputBuilder.Write(EscapeName(name));
_outputBuilder.Write('(');

foreach (var parmVarDecl in typedefDecl.CursorChildren.Where((cursor) => cursor is ParmVarDecl))
foreach (var parmVarDecl in typedefDecl.CursorChildren.OfType<ParmVarDecl>())
{
Visit(parmVarDecl);
}
Expand Down Expand Up @@ -1776,7 +1776,7 @@ private void VisitUnexposedDecl(Decl unexposedDecl)
{
Debug.Assert(unexposedDecl.Kind == CXCursorKind.CXCursor_UnexposedDecl);

foreach (var decl in unexposedDecl.CursorChildren.Where((cursor) => cursor is Decl))
foreach (var decl in unexposedDecl.CursorChildren.OfType<Decl>())
{
Visit(decl);
}
Expand Down
12 changes: 6 additions & 6 deletions sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class CXXRecordDecl : RecordDecl

internal CXXRecordDecl(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_bases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => CursorChildren.Where((cursor) => cursor is CXXBaseSpecifier).Cast<CXXBaseSpecifier>().ToList());
_ctors = new Lazy<IReadOnlyList<CXXConstructorDecl>>(() => Methods.Where((method) => method is CXXConstructorDecl).Cast<CXXConstructorDecl>().ToList());
_destructor = new Lazy<CXXDestructorDecl>(() => Methods.Where((method) => method is CXXDestructorDecl).Cast<CXXDestructorDecl>().Single());
_friends = new Lazy<IReadOnlyList<FriendDecl>>(() => Decls.Where((decl) => decl is FriendDecl).Cast<FriendDecl>().ToList());
_methods = new Lazy<IReadOnlyList<CXXMethodDecl>>(() => Decls.Where((decl) => decl is CXXMethodDecl).Cast<CXXMethodDecl>().ToList());
_vbases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => Bases.Where((@base) => @base.IsVirtual).Cast<CXXBaseSpecifier>().ToList());
_bases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => CursorChildren.OfType<CXXBaseSpecifier>().ToList());
_ctors = new Lazy<IReadOnlyList<CXXConstructorDecl>>(() => Methods.OfType<CXXConstructorDecl>().ToList());
_destructor = new Lazy<CXXDestructorDecl>(() => Methods.OfType<CXXDestructorDecl>().Single());
_friends = new Lazy<IReadOnlyList<FriendDecl>>(() => Decls.OfType<FriendDecl>().ToList());
_methods = new Lazy<IReadOnlyList<CXXMethodDecl>>(() => Decls.OfType<CXXMethodDecl>().ToList());
_vbases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => Bases.OfType<CXXBaseSpecifier>().ToList());
}

public bool IsAbstract => Handle.CXXRecord_IsAbstract;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/Decl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Decl : Cursor

private protected Decl(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_attrs = new Lazy<IReadOnlyList<Attr>>(() => CursorChildren.Where((cursor) => cursor is Attr).Cast<Attr>().ToList());
_attrs = new Lazy<IReadOnlyList<Attr>>(() => CursorChildren.OfType<Attr>().ToList());
_canonicalDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.CanonicalCursor));
_declContext = new Lazy<IDeclContext>(() => (IDeclContext)Create(Handle.SemanticParent));
_lexicalDeclContext = new Lazy<IDeclContext>(() => (IDeclContext)Create(Handle.LexicalParent));
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/EnumConstantDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class EnumConstantDecl : ValueDecl, IMergeable<EnumConstantDecl>

internal EnumConstantDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_EnumConstantDecl)
{
_initExpr = new Lazy<Expr>(() => CursorChildren.Where((cursor) => cursor is Expr).Cast<Expr>().SingleOrDefault());
_initExpr = new Lazy<Expr>(() => CursorChildren.OfType<Expr>().SingleOrDefault());
}

public Expr InitExpr => _initExpr.Value;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/EnumDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class EnumDecl : TagDecl

internal EnumDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_EnumDecl)
{
_enumerators = new Lazy<IReadOnlyList<EnumConstantDecl>>(() => Decls.Where((decl) => decl is EnumConstantDecl).Cast<EnumConstantDecl>().ToList());
_enumerators = new Lazy<IReadOnlyList<EnumConstantDecl>>(() => Decls.OfType<EnumConstantDecl>().ToList());
_integerType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.EnumDecl_IntegerType));
}

Expand Down
6 changes: 3 additions & 3 deletions sources/ClangSharp/Cursors/Decls/FunctionDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class FunctionDecl : DeclaratorDecl, IDeclContext, IRedeclarable<Function

internal FunctionDecl(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_body = new Lazy<Stmt>(() => CursorChildren.Where((cursor) => cursor is Stmt).Cast<Stmt>().SingleOrDefault());
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.Where((cursor) => cursor is Decl).Cast<Decl>().ToList());
_parameters = new Lazy<IReadOnlyList<ParmVarDecl>>(() => Decls.Where((decl) => decl is ParmVarDecl).Cast<ParmVarDecl>().ToList());
_body = new Lazy<Stmt>(() => CursorChildren.OfType<Stmt>().SingleOrDefault());
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.OfType<Decl>().ToList());
_parameters = new Lazy<IReadOnlyList<ParmVarDecl>>(() => Decls.OfType<ParmVarDecl>().ToList());
_returnType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ResultType));
}

Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/NamespaceDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class NamespaceDecl : NamedDecl, IDeclContext, IRedeclarable<Names

internal NamespaceDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_Namespace)
{
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.Where((cursor) => cursor is Decl).Cast<Decl>().ToList());
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.OfType<Decl>().ToList());
}

public bool IsAnonymousNamespace => Handle.IsAnonymous;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/RecordDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RecordDecl : TagDecl

internal RecordDecl(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_fields = new Lazy<IReadOnlyList<FieldDecl>>(() => Decls.Where((decl) => decl is FieldDecl).Cast<FieldDecl>().ToList());
_fields = new Lazy<IReadOnlyList<FieldDecl>>(() => Decls.OfType<FieldDecl>().ToList());
}

public bool IsAnonymousRecord => Handle.IsAnonymousRecordDecl;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/TagDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public unsafe class TagDecl : TypeDecl, IDeclContext, IRedeclarable<TagDecl>

private protected TagDecl(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.Where((cursor) => cursor is Decl).Cast<Decl>().ToList());
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.OfType<Decl>().ToList());
_definition = new Lazy<TagDecl>(() => TranslationUnit.GetOrCreate<TagDecl>(Handle.Definition));
}

Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Decls/TranslationUnitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed unsafe class TranslationUnitDecl : Decl, IDeclContext

internal TranslationUnitDecl(CXCursor handle) : base(handle, CXCursorKind.CXCursor_TranslationUnit)
{
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.Where((cursor) => cursor is Decl).Cast<Decl>().ToList());
_decls = new Lazy<IReadOnlyList<Decl>>(() => CursorChildren.OfType<Decl>().ToList());
}

public IReadOnlyList<Decl> Decls => _decls.Value;
Expand Down
4 changes: 2 additions & 2 deletions sources/ClangSharp/Cursors/Exprs/BinaryOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal BinaryOperator(CXCursor handle, CXCursorKind expectedKind) : base(handl
{
Debug.Assert(Children.Where((cursor) => cursor is Expr).Count() == 2);

_lhs = new Lazy<Expr>(() => Children.Where((cursor) => cursor is Expr).Cast<Expr>().First());
_lhs = new Lazy<Expr>(() => Children.OfType<Expr>().First());
_opcode = new Lazy<string>(GetOpcode);
_rhs = new Lazy<Expr>(() => Children.Where((cursor) => cursor is Expr).Cast<Expr>().Last());
_rhs = new Lazy<Expr>(() => Children.OfType<Expr>().Last());
}

public Expr LHS => _lhs.Value;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Exprs/ParenExpr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class ParenExpr : Expr

internal ParenExpr(CXCursor handle) : base(handle, CXCursorKind.CXCursor_ParenExpr)
{
_subExpr = new Lazy<Expr>(() => Children.Where((cursor) => cursor is Expr).Cast<Expr>().Single());
_subExpr = new Lazy<Expr>(() => Children.OfType<Expr>().Single());
}

public Expr SubExpr => _subExpr.Value;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Exprs/UnaryOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class UnaryOperator : Expr
internal UnaryOperator(CXCursor handle) : base(handle, CXCursorKind.CXCursor_UnaryOperator)
{
_opcode = new Lazy<(string Opcode, bool IsPrefix)>(GetOpcode);
_subExpr = new Lazy<Expr>(() => Children.Where((cursor) => cursor is Expr).Cast<Expr>().Single());
_subExpr = new Lazy<Expr>(() => Children.OfType<Expr>().Single());
}

public bool IsPrefix => _opcode.Value.IsPrefix;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ClangSharp.Interop;

namespace ClangSharp
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Stmts/ReturnStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class ReturnStmt : Stmt

internal ReturnStmt(CXCursor handle) : base(handle, CXCursorKind.CXCursor_ReturnStmt)
{
_retValue = new Lazy<Expr>(() => Children.Where((stmt) => stmt is Expr).Cast<Expr>().Single());
_retValue = new Lazy<Expr>(() => Children.OfType<Expr>().Single());
}

public Expr RetValue => _retValue.Value;
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/Cursors/Stmts/Stmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Stmt : Cursor

private protected Stmt(CXCursor handle, CXCursorKind expectedKind) : base(handle, expectedKind)
{
_children = new Lazy<IReadOnlyList<Stmt>>(() => CursorChildren.Where((cursor) => cursor is Stmt).Cast<Stmt>().ToList());
_children = new Lazy<IReadOnlyList<Stmt>>(() => CursorChildren.OfType<Stmt>().ToList());
}

public IReadOnlyList<Stmt> Children => _children.Value;
Expand Down
2 changes: 0 additions & 2 deletions sources/ClangSharp/Interop.Extensions/CXIdxLoc.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;

namespace ClangSharp.Interop
{
public unsafe partial struct CXIdxLoc
Expand Down
1 change: 0 additions & 1 deletion sources/ClangSharp/Types/Type.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using ClangSharp.Interop;

Expand Down