CTFE and BetterC compatibility

Claude claudemr at live.fr
Wed Apr 27 14:21:15 UTC 2022


Hello,

I want to make a SAX XML parser in D that I could both use at 
run-time or compile-time.

Also when I use it at compile-time, I would like to use BetterC 
so I don't have to link D-runtime.

But I have some compilation problems. I use GDC (GCC 9.4.0).

Here's a reduced sample code:

```
struct Data
{
     int[] digits;
}

int parseDigit(char c) pure
{
     return c - '0';
}

Data parse(string str) pure
{
     Data data;

     while (str.length != 0)
     {
         // Skip spaces
         while (str[0] == ' ')
             str = str[1 .. $];

         // Parse single digit integer
         data.digits ~= parseDigit(str[0]);

         // Consume digit
         str = str[1 .. $];
     }

     return data;
}

enum Data parsedData = parse("5 4 2 6 9");

extern(C) int main()
{
     pragma(msg, "First digit=", parsedData.digits[0]);
     return 0;
}
```

If I compile and link against D-runtime, it works:
```
$ gcc test.d -lgdruntime -o test
First digit=5
```

If I compile with BetterC (no D-runtime for GDC), I get a 
compilation error about RTTI:
```
$ gcc test.d -fno-druntime -o test
test.d: In function ‘parse’:
test.d:25:21: error: ‘object.TypeInfo’ cannot be used with 
-fno-rtti
    25 |         data.digits ~= parseDigit(str[0]);
       |                     ^
```

If I compile without the BetterC switch, compilation actually 
works but I'll have some linker issues:
```
$ gcc test.d -o test
First digit=5
/tmp/ccuPwjdv.o : In function 
« _D5test5parseFNaAyaZS5test4Data » :
test.d:(.text+0x137) : undefined reference to « _d_arraybounds »
test.d:(.text+0x183) : undefined reference to « _d_arraybounds »

etc...
```

The operation requiring the D-runtime is appending the array, but 
it should **only** be done at compile-time.

I don't understand why it requires to link against the D-runtime 
whereas it only needs it at compilation-time (and the compilation 
and CTFE interpretation works, as we can see in the last example).

Is there a way to force the compiler to not emit any object ode 
for those functions?

Or am I missing something?

Regards,

Claude


More information about the Digitalmars-d-learn mailing list