ImportC now supports function-like macros

Carl Sturtivant sturtivant at gmail.com
Wed Feb 21 15:40:29 UTC 2024


On Tuesday, 20 February 2024 at 02:07:45 UTC, Walter Bright wrote:
> #importc now successfully converts function-like C macros to 
> #dlang function templates:
>
> ```C
> #define ADD(a, b) a + b
> ```
>
> is converted to a D function template:
>
> ```D
> auto ADD(T1, T2)(T1 a, T2 b) { return a + b; }
> ```
ImportC is now fantastically useful! However, does Exception 
connected behavior limit this?

The ImportC documentation says for [Exception 
Handling](https://dlang.org/spec/importc.html#limitations) that 
"ImportC is assumed to never throw exceptions. setjmp and longjmp 
are not supported".

I am unclear under what circumstances D throwing exceptions in 
the presence of ImportC works.

Reading that sentence carefully, it does not seem to imply that 
exceptions cannot occur in D code linked to C code compiled with 
ImportC in all cases. Of course if executing D code hasn't got 
ImportC code in its call chain then exception handling will work. 
But there are two other cases.

1. I imagine that if C code calls D code which throws and the 
exception is caught in D code without unwinding the C part of the 
call stack this is OK. In some sense ImportC did not throw an 
exception in this case, even though it executed a call chain to 
do so, which could be interpreted as ImportC throwing an 
exception.

2. What if D throws an exception that would supposedly unwind the 
C part of the call stack and be caught from D further up? I might 
guess this counts as ImportC throwing an exception  and so is not 
OK, but I hope I am wrong.

Could you please explain the actual situation of ImportC in these 
cases?




More information about the Digitalmars-d mailing list