-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move non-scoped enums nested in classes to their parents
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
- Loading branch information
Showing
7 changed files
with
100 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Generator/Passes/MoveNonScopedNestedEnumsToParentPass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using CppSharp.AST; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CppSharp.Passes | ||
{ | ||
/// <summary> | ||
/// This pass moves old-style (non-scoped) enumerations nested in classes | ||
/// to the parents of those classes. | ||
/// </summary> | ||
/// <remarks> | ||
/// Such enums are presumably written this way because C++ before 11 | ||
/// could not scope enums and nesting was the only way to do so | ||
/// in order to prevent conflicts. But target languages don't have | ||
/// this limitation so we can generate a more sensible API. | ||
/// </remarks> | ||
public class MoveNonScopedNestedEnumsToParentPass : TranslationUnitPass | ||
{ | ||
public override bool VisitASTContext(ASTContext context) | ||
{ | ||
bool result = base.VisitASTContext(context); | ||
|
||
foreach (var movableEnum in movableEnums) | ||
{ | ||
DeclarationContext declarationContext = movableEnum.Namespace; | ||
declarationContext.Declarations.Remove(movableEnum); | ||
declarationContext.Namespace.Declarations.Add(movableEnum); | ||
movableEnum.Namespace = declarationContext.Namespace; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public override bool VisitEnumDecl(Enumeration @enum) | ||
{ | ||
if (!base.VisitEnumDecl(@enum)) | ||
return false; | ||
|
||
if (string.IsNullOrEmpty(@enum.Name) || | ||
!(@enum.Namespace is Class) || | ||
@enum.Access != AccessSpecifier.Public || | ||
@enum.IsScoped) | ||
return false; | ||
|
||
if (@enum.Namespace.Namespace.Declarations.Union(movableEnums).Any( | ||
e => e.Name == @enum.Name)) | ||
return false; | ||
|
||
movableEnums.Add(@enum); | ||
|
||
return true; | ||
} | ||
|
||
private readonly List<Enumeration> movableEnums = new List<Enumeration>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters