Browse Source

Generate valid C# for internal fields of type external specialization

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1641/head
Dimitar Dobrev 4 years ago
parent
commit
09190268bc
  1. 6
      src/Generator/Extensions/TypeExtensions.cs
  2. 3
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 5
      src/Generator/Passes/TrimSpecializationsPass.cs
  4. 10
      tests/NamespacesDerived/NamespacesDerived.Gen.cs
  5. 9
      tests/NamespacesDerived/NamespacesDerived.h

6
src/Generator/Extensions/TypeExtensions.cs

@ -8,6 +8,9 @@ namespace CppSharp.Extensions
{ {
public static int GetWidth(this Type type, ParserTargetInfo targetInfo) public static int GetWidth(this Type type, ParserTargetInfo targetInfo)
{ {
if (type is TemplateSpecializationType specializationType)
type = specializationType.Desugared.Type;
if (type.IsPrimitiveType(out var primitiveType)) if (type.IsPrimitiveType(out var primitiveType))
return (int)primitiveType.GetInfo(targetInfo, out _).Width; return (int)primitiveType.GetInfo(targetInfo, out _).Width;
@ -26,6 +29,9 @@ namespace CppSharp.Extensions
public static int GetAlignment(this Type type, ParserTargetInfo targetInfo) public static int GetAlignment(this Type type, ParserTargetInfo targetInfo)
{ {
if (type is TemplateSpecializationType specializationType)
type = specializationType.Desugared.Type;
if (type.IsPrimitiveType(out var primitiveType)) if (type.IsPrimitiveType(out var primitiveType))
return (int)primitiveType.GetInfo(targetInfo, out _).Alignment; return (int)primitiveType.GetInfo(targetInfo, out _).Alignment;

3
src/Generator/Generators/CSharp/CSharpSources.cs

@ -128,7 +128,8 @@ namespace CppSharp.Generators.CSharp
var declarationContexts = new Stack<DeclarationContext>(); var declarationContexts = new Stack<DeclarationContext>();
while (!(declContext is TranslationUnit)) while (!(declContext is TranslationUnit))
{ {
declarationContexts.Push(declContext); if (!(declContext is Namespace @namespace) || !@namespace.IsInline)
declarationContexts.Push(declContext);
declContext = declContext.Namespace; declContext = declContext.Namespace;
} }

5
src/Generator/Passes/TrimSpecializationsPass.cs

@ -138,8 +138,7 @@ namespace CppSharp.Passes
template.Specializations.All(s => s.Ignore)) template.Specializations.All(s => s.Ignore))
template.ExplicitlyIgnore(); template.ExplicitlyIgnore();
if (template.Fields.Any(f => f.Type.Desugar() is TemplateParameterType)) TryMoveExternalSpecializations(template);
MoveExternalSpecializations(template);
} }
/// <summary> /// <summary>
@ -147,7 +146,7 @@ namespace CppSharp.Passes
/// the library their template is located in, to the module of said external types. /// the library their template is located in, to the module of said external types.
/// </summary> /// </summary>
/// <param name="template">The template to check for external specializations.</param> /// <param name="template">The template to check for external specializations.</param>
private static void MoveExternalSpecializations(Class template) private static void TryMoveExternalSpecializations(Class template)
{ {
for (int i = template.Specializations.Count - 1; i >= 0; i--) for (int i = template.Specializations.Count - 1; i >= 0; i--)
{ {

10
tests/NamespacesDerived/NamespacesDerived.Gen.cs

@ -1,4 +1,6 @@
using System.IO; using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators; using CppSharp.Generators;
using CppSharp.Utils; using CppSharp.Utils;
@ -28,9 +30,15 @@ namespace CppSharp.Tests
driver.Options.Modules[1].Dependencies.Add(module); driver.Options.Modules[1].Dependencies.Add(module);
} }
public override void Preprocess(Driver driver, AST.ASTContext ctx) public override void Preprocess(Driver driver, ASTContext ctx)
{ {
ctx.IgnoreClassWithName("Ignored"); ctx.IgnoreClassWithName("Ignored");
// operator= for this type isn't necessary for testing
// while also requiring a large amount of C++ to get valid symbols; better ignore
foreach (Method @operator in ctx.FindCompleteClass("StdFields").Operators)
{
@operator.ExplicitlyIgnore();
}
} }
} }

9
tests/NamespacesDerived/NamespacesDerived.h

@ -2,6 +2,7 @@
#include "../NamespacesBase/NamespacesBase.h" #include "../NamespacesBase/NamespacesBase.h"
#include "Independent.h" #include "Independent.h"
#include <string> #include <string>
#include <vector>
// Namespace clashes with NamespacesBase.OverlappingNamespace // Namespace clashes with NamespacesBase.OverlappingNamespace
// Test whether qualified names turn out right. // Test whether qualified names turn out right.
@ -105,7 +106,13 @@ public:
class DLL_API Ignored class DLL_API Ignored
{ {
private: private:
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> f; std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> customAllocatedString;
};
class DLL_API StdFields
{
private:
std::vector<unsigned int, CustomAllocator<unsigned int>> customAllocatedVector;
}; };
DLL_API bool operator<<(const Base& b, const char* str); DLL_API bool operator<<(const Base& b, const char* str);

Loading…
Cancel
Save