Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sourcegen fails to process clib signature #1450

Closed
ischoegl opened this issue Mar 4, 2023 · 8 comments · Fixed by #1773
Closed

sourcegen fails to process clib signature #1450

ischoegl opened this issue Mar 4, 2023 · 8 comments · Fixed by #1773
Labels

Comments

@ischoegl
Copy link
Member

ischoegl commented Mar 4, 2023

Problem description

The following function signature causes a syntax error in sourcgen:

int soln_newInterface(const char* infile, const char* name, int na, const int* adjacent);

Steps to reproduce

Add function with signature and run GH CI (see #1448).

Fwiw, a similar signature in ctonedim.h (which is currently not included in .NET) will presumably fail also:

int sim1D_new(size_t nd, const int* domains);

Behavior

See GH Action

Relevant details from log
2023-03-04T18:21:24.9253446Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1031: Type expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:24.9383516Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1001: Identifier expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:24.9385819Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1026: ) expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:24.9387491Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1002: ; expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:24.9389312Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,113): error CS0145: A const field requires a value to be provided [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:24.9391610Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,121): error CS1003: Syntax error, ',' expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0662852Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1031: Type expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0667480Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1001: Identifier expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0670849Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1026: ) expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0673541Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1002: ; expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0676471Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,113): error CS0145: A const field requires a value to be provided [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.0679731Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,121): error CS1003: Syntax error, ',' expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1429675Z 
2023-03-04T18:21:25.1430446Z Build FAILED.
2023-03-04T18:21:25.1432509Z 
2023-03-04T18:21:25.1433930Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1031: Type expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1436932Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1001: Identifier expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1439026Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1026: ) expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1441390Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1002: ; expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1443993Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,113): error CS0145: A const field requires a value to be provided [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1447134Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/netstandard2.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,121): error CS1003: Syntax error, ',' expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1449333Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1031: Type expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1451544Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1001: Identifier expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1453548Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1026: ) expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1455523Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,102): error CS1002: ; expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1457449Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,113): error CS0145: A const field requires a value to be provided [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1460279Z ##[error]/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/obj/Debug/net6.0/sourcegen/Interop.LibCantera.ct.h.g.cs(22,121): error CS1003: Syntax error, ',' expected [/home/runner/work/cantera/cantera/interfaces/dotnet/Cantera/Cantera.csproj]
2023-03-04T18:21:25.1461576Z     0 Warning(s)
2023-03-04T18:21:25.1461811Z     12 Error(s)

System information

  • Cantera version: current main
  • OS: GH Actions

Additional context

Bug emerged in #1448; as an aside, it would be terrific if the following could be updated:

## running from the command line

@burkenyo
Copy link
Contributor

burkenyo commented Mar 5, 2023

Hi @ischoegl, currently the C# side doesn't know what to do with the const int*. Remind me, what is the bit-size of an int?

@ischoegl
Copy link
Member Author

ischoegl commented Mar 5, 2023

@burkenyo ... thanks for your response! The answer is probably not as clear-cut as it is platform dependent, see this SO post. In any case, @speth is likely a lot more knowledgeable in this area.

PS: It's actually not super important that it would be a const int*, as it could likewise be a const size_t* or any other integer type.

@burkenyo
Copy link
Contributor

burkenyo commented Mar 5, 2023

@ischoegl Just got a bit more info, it seems this “int*” is actually a bunch of references/handles to other Cantera objects, correct? My intuition is this will require hand-coding on the C# side anyway and not be scaffolded. I put a quick note in #1448: adding the function to the ignore list in the config.yaml is perfect.

@burkenyo
Copy link
Contributor

burkenyo commented Mar 5, 2023

Aside: the reason this works in C# is because we have partial classes. A class can be split across multiple files, some perhaps auto-generated by a tool, others written by hand, and still be treated as one unit. It's similar to header files, but more opinionated.

@ischoegl
Copy link
Member Author

ischoegl commented Mar 5, 2023

@ischoegl Just got a bit more info, it seems this “int*” is actually a bunch of references/handles to other Cantera objects, correct? My intuition is this will require hand-coding on the C# side anyway and not be scaffolded. I put a quick note in #1448: adding the function to the ignore list in the config.yaml is perfect.

This is correct - it is a list of handles, where the length is passed as a separate int na parameter (which avoids the kludgy interface of kin_newFromFile). At the face of it, I was hoping that this would be similar to what is already done for const char* for passing strings. So it's not possible to handle this in a similar fashion?

PS: it just occurred to me that this is also the same as what is already done for interfacing with double arrays, see for example (never mind the missing const):

CANTERA_CAPI int thermo_chemPotentials(int n, size_t lenm, double* murt);

If there's a preference for size_t for the length to have a sentinel type, this would certainly be ok.

@burkenyo
Copy link
Contributor

burkenyo commented Mar 5, 2023

@ischoegl, the C#-side has special wrapper classes for each of the Cantera handles that represent Cantera objects. So the interop function signature looks something like:

[DllImport(LibFile)]
    public static extern nuint thermo_nSpecies(ThermoPhaseHandle n);

ThermoPhaseHandle derives from CanteraHandle which derives from a special framework class that encloses the raw handle. The marshalling on this is handled by the runtime. So what I am not clear on yet is how to marshal an array of CanteraHandles.

Talked to @speth, and it seems all the “int”s coming out of Cantera representing handles to object are 32-bits on platforms the .NET interface will support. My special xHandle classes on the .NET side all use a native-pointer-sized int (I think this is the same as size_t). Passing a single handle is OK because the system aligns arguments to native word size, but it adds complication to handling an array of these handles. And while it would be great to just use size_t in all the places where we exchange handles in CLIB, @speth further informs me that doing so would break MatLab.

I actually think the 32-bit ints are just fine, and the solution may be to change the .NET side to use these directly. The special framework class (SafeHandle) with its native-int handles provides great safety guarantees for shared OS resources like file handles and sockets, but may be overkill for Cantera handles, which are always uniquely owned by the one process.


p.s. Marshalling is what the .NET world calls handling arguments to the native C types from the .NET types. So for example, the runtime can automatically convert a .NET System.String to a const char*. The default marshalling may not be ideal for performance, e.g too many buffer allocations and copies, so there are ways to influence this process. For example, for getting strings from Cantera I use the raw pointer to avoid extra allocations.

@ischoegl
Copy link
Member Author

ischoegl commented Mar 5, 2023

@burkenyo … thanks for the explanations!

@ischoegl
Copy link
Member Author

ischoegl commented Aug 4, 2024

Here's another clib signature that cannot be processed (from #1754)

    CANTERA_CAPI int thermo_size();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants