diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index 104ada7f..c5e0a678 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -346,6 +346,36 @@ return left.Equals(right); } + public static bool IsConst(this QualifiedType type) + { + return type.Type != null && (type.Qualifiers.IsConst || + type.Type.GetQualifiedPointee().IsConst()); + } + + public static bool IsConstCharString(this Type type) + { + var desugared = type.Desugar(); + + if (!(desugared is PointerType)) + return false; + + var pointer = desugared as PointerType; + return IsConstCharString(pointer); + } + + public static bool IsConstCharString(this PointerType pointer) + { + if (pointer.IsReference) + return false; + + var pointee = pointer.Pointee.Desugar(); + + return (pointee.IsPrimitiveType(PrimitiveType.Char) || + pointee.IsPrimitiveType(PrimitiveType.Char16) || + pointee.IsPrimitiveType(PrimitiveType.WideChar)) && + pointer.QualifiedPointee.Qualifiers.IsConst; + } + public static bool IsDependentPointer(this Type type) { var desugared = type.Desugar(); diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 93de9915..9f14f7ad 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -102,7 +102,7 @@ namespace CppSharp.Generators.CLI return true; } - if (CSharpTypePrinter.IsConstCharString(pointer)) + if (pointer.IsConstCharString()) { Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName, pointer.Pointee.Desugar() as BuiltinType)); diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index d834ff59..46b9c1fa 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -116,7 +116,7 @@ namespace CppSharp.Generators.CLI return string.Format("{0}^", function.Visit(this, quals)); } - if (CSharpTypePrinter.IsConstCharString(pointer)) + if (pointer.IsConstCharString()) return "System::String^"; // From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index bcfa0abe..ac837807 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -157,9 +157,9 @@ namespace CppSharp.Generators.CSharp var isRefParam = param != null && (param.IsInOut || param.IsOut); var pointee = pointer.Pointee.Desugar(); - bool marshalPointeeAsString = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam; + bool marshalPointeeAsString = pointee.IsConstCharString() && isRefParam; - if ((CSharpTypePrinter.IsConstCharString(pointer) && !MarshalsParameter) || + if ((pointer.IsConstCharString() && !MarshalsParameter) || marshalPointeeAsString) { Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName, @@ -604,7 +604,7 @@ namespace CppSharp.Generators.CSharp var param = Context.Parameter; var isRefParam = param != null && (param.IsInOut || param.IsOut); - if (CSharpTypePrinter.IsConstCharString(pointee) && isRefParam) + if (pointee.IsConstCharString() && isRefParam) { if (param.IsOut) { @@ -650,7 +650,7 @@ namespace CppSharp.Generators.CSharp return true; } - var marshalAsString = CSharpTypePrinter.IsConstCharString(pointer); + var marshalAsString = pointer.IsConstCharString(); var finalPointee = pointer.GetFinalPointee(); PrimitiveType primitive; if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType() || diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 0ca6d554..739ed0a7 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -926,8 +926,7 @@ namespace CppSharp.Generators.CSharp if (type.IsPointer()) { Type pointee = type.GetFinalPointee(); - if (pointee.IsPrimitiveType() && - !CSharpTypePrinter.IsConstCharString(type)) + if (pointee.IsPrimitiveType() && !type.IsConstCharString()) { Write($"({CSharpTypePrinter.IntPtrType}) "); var templateSubstitution = pointee.Desugar(false) as TemplateParameterSubstitutionType; diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index bc121405..490a155a 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -177,35 +177,6 @@ namespace CppSharp.Generators.CSharp return string.Format("Func<{0}{1}>", returnTypePrinterResult, args); } - public static bool IsConstCharString(PointerType pointer) - { - if (pointer.IsReference) - return false; - - var pointee = pointer.Pointee.Desugar(); - - return (pointee.IsPrimitiveType(PrimitiveType.Char) || - pointee.IsPrimitiveType(PrimitiveType.Char16) || - pointee.IsPrimitiveType(PrimitiveType.WideChar)) && - pointer.QualifiedPointee.Qualifiers.IsConst; - } - - public static bool IsConstCharString(Type type) - { - var desugared = type.Desugar(); - - if (!(desugared is PointerType)) - return false; - - var pointer = desugared as PointerType; - return IsConstCharString(pointer); - } - - public static bool IsConstCharString(QualifiedType qualType) - { - return IsConstCharString(qualType.Type); - } - private bool allowStrings = true; public override TypePrinterResult VisitPointerType(PointerType pointer, @@ -224,7 +195,7 @@ namespace CppSharp.Generators.CSharp var isManagedContext = ContextKind == TypePrinterContextKind.Managed; - if (allowStrings && IsConstCharString(pointer)) + if (allowStrings && pointer.IsConstCharString()) { if (isManagedContext) return "string"; @@ -259,7 +230,7 @@ namespace CppSharp.Generators.CSharp if (pointee.IsPrimitiveType(PrimitiveType.Void)) return IntPtrType; - if (IsConstCharString(pointee) && isRefParam) + if (pointee.IsConstCharString() && isRefParam) return IntPtrType + "*"; // Do not allow strings inside primitive arrays case, else we'll get invalid types diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 47c87047..62d1a654 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -93,7 +93,7 @@ namespace CppSharp.Types.Std public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { - var type = ctx.ReturnType.Type.Desugar(); + var type = Type.Desugar(resolveTemplateSubstitution: false); ClassTemplateSpecialization basicString = GetBasicString(type); var c_str = basicString.Methods.First(m => m.OriginalName == "c_str"); var typePrinter = new CSharpTypePrinter(ctx.Context);