Skip to content

Conversation

Copy link

Copilot AI commented Nov 6, 2025

C# director method dispatchers were calling delegates without required out/ref keywords, causing compilation errors.

Changes

Source/Modules/csharp.cxx (lines 4320-4332)

  • Added logic to detect out/ref modifiers in parameter typemaps
  • Modified mono_aot_dispatcher_parms construction to include modifiers
  • Checks both im_directorinattributes and imtype typemap for modifier presence
  • Handles space and tab separators

Example

Before (compilation error):

private int SwigDirectorMethodRead(IntPtr buf, int len, out int readLen) {
    return delegateSwigDelegateInStream_0(buf, len, readLen);  // Missing 'out'
}

After (correct):

private int SwigDirectorMethodRead(IntPtr buf, int len, out int readLen) {
    return delegateSwigDelegateInStream_0(buf, len, out readLen);
}

Notes

  • Affects Mono AOT compatibility mode only
  • Existing test coverage in Examples/test-suite/csharp_director_typemaps.i
Original prompt

I'd like to publish the output from the github action to an azure devops universal package, alternatively to an azure blob storage. I'll be using a managed identity. It seems like the windows zip file available on swig's website is just the entire folder

Summary

TITLE: Adding out parameter support to C# director method dispatcher calls in SWIG

USER INTENT: Fix a C# compilation error where out parameters in director methods are not being passed with the out keyword in the generated dispatcher method calls.

TASK DESCRIPTION:
The user is working on SWIG's C# code generator module (csharp.cxx) and encountering a C# compiler error. The generated code for director method dispatchers is missing the out keyword when calling delegates that have out parameters. The error shows that in the SwigDirectorRead_Dispatcher method, the parameter readLen is declared as out int readLen but when calling the delegate, it's being passed as just readLen instead of out readLen.

EXISTING:

  • SWIG's C# module generates director code for cross-language callbacks
  • The classDirectorMethod function in csharp.cxx generates dispatcher methods
  • Around line 4319, the code builds mono_aot_dispatcher_parms string with just parameter names
  • The delegate_parms string (line 4317) correctly includes modifiers from im_directorinattributes
  • The generated dispatcher signature correctly shows out modifiers

PENDING:

  • Ensure the dispatcher method calls include out or ref keywords when invoking delegates with such parameters

CODE STATE:
File: /Users/aw/code/swig/Source/Modules/csharp.cxx

Modified section (lines 4312-4327):

if (i > 0) {
  Printf(delegate_parms, ", ");
  Printf(mono_aot_dispatcher_parms, ", ");
  Printf(proxy_method_types, ", ");
  Printf(imcall_args, ", ");
}
Printf(delegate_parms, "%s%s %s", im_directorinattributes ? im_directorinattributes : empty_string, tm, ln);
// Check if the parameter has out or ref modifier and add it to the dispatcher call
if (im_directorinattributes && (Strstr(im_directorinattributes, "out ") || Strstr(im_directorinattributes, "out\t"))) {
  Printf(mono_aot_dispatcher_parms, "out %s", ln);
} else if (im_directorinattributes && (Strstr(im_directorinattributes, "ref ") || Strstr(im_directorinattributes, "ref\t"))) {
  Printf(mono_aot_dispatcher_parms, "ref %s", ln);
} else {
  Printf(mono_aot_dispatcher_parms, "%s", ln);
}

This change checks the im_directorinattributes (from tmap:imtype:directorinattributes) for out or ref modifiers and includes them when building the mono_aot_dispatcher_parms string, which is later used at line 4435 to create the delegate call.

RELEVANT CODE/DOCUMENTATION SNIPPETS:

Generated C# code flow:

  1. Line 4317: Builds delegate_parms with modifiers: "out int readLen"
  2. Lines 4319-4327: Builds mono_aot_dispatcher_parms (now with modifiers): "out readLen"
  3. Line 4435: Creates upcall_mono_aot: "delegateSwigDelegateInStream_0(buf, len, out readLen)"
  4. Line 4447: Inserts into template code that generates the actual dispatcher method

Error before fix:

return delegateSwigDelegateInStream_0(buf, len, readLen);  // Missing 'out' keyword

Expected after fix:

return delegateSwigDelegateInStream_0(buf, len, out readLen);  // Correct

OTHER NOTES:

  • The fix handles both out and ref modifiers
  • Checks for both space and tab separators after keywords
  • The im_directorinattributes comes from the typemap's directorinattributes attribute
  • The change only affects mono AOT compatibility mode dispatcher calls
  • No compilation errors introduced by the change (only pre-existing Clang-Tidy warnings)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix C# compilation error for out parameters in SWIG dispatcher Fix C# director dispatcher calls missing 'out' and 'ref' keywords Nov 6, 2025
Copilot AI requested a review from willadsen November 6, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants