Skip to content

Commit 32b83a3

Browse files
IIFEBuild Agent
andauthored
Generate forward ref header for enum defined inside a class in C++/CLI (#1322)
Co-authored-by: Build Agent <[email protected]>
1 parent 344656c commit 32b83a3

File tree

9 files changed

+59
-2
lines changed

9 files changed

+59
-2
lines changed

src/Generator/AST/ASTRecord.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ public static bool IsFieldValueType(this ASTRecord record)
150150
return field.Type.Desugar().TryGetClass(out decl) && decl.IsValueType;
151151
}
152152

153+
public static bool IsEnumNestedInClass(this ASTRecord<Declaration> record)
154+
{
155+
Enumeration @enum = record.Value as Enumeration;
156+
var typedDecl = record.Value as ITypedDecl;
157+
if (@enum != null
158+
|| (typedDecl?.Type?.TryGetEnum(out @enum)).GetValueOrDefault())
159+
{
160+
return @enum.Namespace is Class;
161+
}
162+
163+
return false;
164+
}
165+
153166
public static bool IsDelegate(this ASTRecord record)
154167
{
155168
var typedef = record.Object as TypedefDecl;

src/Generator/Generators/CLI/CLITypeReferences.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public bool IsIncludeInHeader(ASTRecord<Declaration> record)
176176
if (TranslationUnit == record.Value.Namespace.TranslationUnit)
177177
return false;
178178

179-
return record.IsBaseClass() || record.IsFieldValueType() || record.IsDelegate();
179+
return record.IsBaseClass() || record.IsFieldValueType() || record.IsDelegate()
180+
|| record.IsEnumNestedInClass();
180181
}
181182

182183
public override bool VisitDeclaration(Declaration decl)

tests/CLI/CLI.Tests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ public void GetEmployeeNameFromOrgTest()
3535
Assert.AreEqual("Employee", org.Employee.Name);
3636
}
3737
}
38+
39+
[Test]
40+
public void TestConsumerOfEnumNestedInClass()
41+
{
42+
using (NestedEnumConsumer consumer = new NestedEnumConsumer())
43+
{
44+
Assert.AreEqual(ClassWithNestedEnum.NestedEnum.E1, consumer.GetPassedEnum(ClassWithNestedEnum.NestedEnum.E1));
45+
}
46+
}
3847
}

tests/CLI/CLI.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "UseTemplateTypeFromIgnoredClassTemplate/Employee.h"
44
#include "UseTemplateTypeFromIgnoredClassTemplate/EmployeeOrg.h"
55

6+
#include "NestedEnumInClassTest/ClassWithNestedEnum.h"
7+
#include "NestedEnumInClassTest/NestedEnumConsumer.h"
8+
69
#include <ostream>
710

811
// Tests for C++ types
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "ClassWithNestedEnum.h"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "../../Tests.h"
4+
5+
class DLL_API ClassWithNestedEnum
6+
{
7+
public:
8+
enum NestedEnum
9+
{
10+
E1,
11+
E2
12+
};
13+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "NestedEnumConsumer.h"
2+
3+
ClassWithNestedEnum::NestedEnum NestedEnumConsumer::GetPassedEnum(ClassWithNestedEnum::NestedEnum e)
4+
{
5+
return e;
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "../../Tests.h"
4+
5+
#include "ClassWithNestedEnum.h"
6+
7+
class DLL_API NestedEnumConsumer
8+
{
9+
public:
10+
ClassWithNestedEnum::NestedEnum GetPassedEnum(ClassWithNestedEnum::NestedEnum e);
11+
};

tests/CLI/premake4.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group "Tests/CLI"
2-
SetupTestCLI("CLI", { "Employee", "EmployeeOrg" })
2+
SetupTestCLI("CLI", { "Employee", "EmployeeOrg", "ClassWithNestedEnum", "NestedEnumConsumer" })

0 commit comments

Comments
 (0)