Skip to content

Commit 0e56bc6

Browse files
committed
Generate valid C# for returned const char*&
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 380d064 commit 0e56bc6

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
146146

147147
var pointee = pointer.Pointee.Desugar();
148148
var finalPointee = pointer.GetFinalPointee().Desugar();
149+
var type = Context.ReturnType.Type.Desugar(
150+
resolveTemplateSubstitution: false);
149151
PrimitiveType primitive;
150-
if ((pointee.IsConstCharString() && isRefParam) ||
152+
if ((pointee.IsConstCharString() && (isRefParam || type.IsReference())) ||
151153
(!finalPointee.IsPrimitiveType(out primitive) &&
152154
!finalPointee.IsEnumType()))
153155
return pointer.QualifiedPointee.Visit(this);
@@ -162,8 +164,6 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
162164
primitive == PrimitiveType.Char)
163165
Context.Return.Write($"({pointer}) ");
164166

165-
var type = Context.ReturnType.Type.Desugar(
166-
resolveTemplateSubstitution: false);
167167
if (Context.Function != null &&
168168
Context.Function.OperatorKind == CXXOperatorKind.Subscript)
169169
{

src/Generator/Types/Std/Stdlib.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,16 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
218218
if (Equals(encoding, Encoding.ASCII))
219219
encoding = Context.Options.Encoding;
220220

221-
string returnVarName = ctx.Function != null &&
222-
ctx.Function.ReturnType.Type.Desugar().IsAddress() ?
223-
$"(global::System.IntPtr) {ctx.ReturnVarName}" : ctx.ReturnVarName;
221+
string returnVarName = ctx.ReturnVarName;
222+
if (ctx.Function != null)
223+
{
224+
Type returnType = ctx.Function.ReturnType.Type.Desugar();
225+
if (returnType.IsAddress() &&
226+
returnType.GetPointee().Desugar().IsAddress())
227+
{
228+
returnVarName = $"new global::System.IntPtr(*{returnVarName})";
229+
}
230+
}
224231

225232
if (Equals(encoding, Encoding.ASCII))
226233
{

tests/CSharp/CSharp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ char* returnCharPointer()
15601560
return 0;
15611561
}
15621562

1563-
const char* takeConstCharStarRef(const char*& c)
1563+
const char*& takeConstCharStarRef(const char*& c)
15641564
{
15651565
return c;
15661566
}

tests/CSharp/CSharp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ class DLL_API HasFunctionPtrField
13051305

13061306
DLL_API void va_listFunction(va_list v);
13071307
DLL_API char* returnCharPointer();
1308-
DLL_API const char* takeConstCharStarRef(const char*& c);
1308+
DLL_API const char*& takeConstCharStarRef(const char*& c);
13091309

13101310
struct {
13111311
struct {

0 commit comments

Comments
 (0)