When importing ncurses from D, ImportC doesn't seem to be exporting as enums (or at all) ncurses' macro definitions such as A_BOLD, as I have not been able to compile it and get errors like this:
Error: undefined identifier `A_BOLD`
Steps to reproduce:
1- Import ncurses into a D source file with "import ncurses".
2- Initialize ncurses with "initscr()" (that works without any issue)
3- Use any function with a ncurses attribute, for example "addch('a' | A_BOLD)"
4- Attempt to compile it.
Comment #1 by bugzilla — 2023-01-10T06:09:46Z
What does the definition of A_BOLD look like in the .h files on your machine?
Comment #2 by quanrong — 2023-01-10T06:44:58Z
This is the definition:
#define A_BOLD NCURSES_BITS(1U,13)
And NCURSES_BITS is defined as follows:
#define NCURSES_BITS(mask,shift) (NCURSES_CAST(chtype,(mask)) << ((shift) + NCURSES_ATTR_SHIFT))
NCURSES_CAST is defined as:
#ifdef __cplusplus
extern "C" {
#define NCURSES_CAST(type,value) static_cast<type>(value)
#else
#define NCURSES_CAST(type,value) (type)(value)
#endif
And NCURSES_ATTR_SHIFT is just 8
I'm using the stable version of this implementation (I can attach the .h file if it's better) https://invisible-island.net/ncurses/ncurses.html
Note that it doesn't just happen with A_BOLD, it happens with all the ncurses "character attributes".
Comment #3 by abryancs — 2023-02-15T00:37:58Z
It seems like the root of the problem is an inability for importC to convert function like macros that are able to be evaluated at compile time into enum definitions. For example:
alex@compy programming/importc_test dmd --version 19:33:22 23-02-14
DMD64 D Compiler v2.102.0
Copyright (C) 1999-2023 by The D Language Foundation, All Rights Reserved written by Walter Bright
alex@compy programming/importc_test cat source/foo.h 19:33:28 23-02-14
#define CONSTANT 6
#define CONTSTANT_PLUS (6 + 1)
alex@compy programming/importc_test cat source/foo.c 19:34:02 23-02-14
#include "foo.h"
alex@compy programming/importc_test cat source/app.d 19:34:03 23-02-14
import std.stdio;
import foo;
void main()
{
writeln("%d", CONSTANT); //works
writeln("%d", CONSTANT_PLUS); //does not work
}
alex@compy programming/importc_test dmd source/*.d source/*.c 19:34:08 23-02-14
source/app.d(7): Error: undefined identifier `CONSTANT_PLUS`
alex@compy programming/importc_test dub build 19:36:10 23-02-14
Starting Performing "debug" build using /usr/bin/dmd for x86_64.
Building importc_test ~master: building configuration [application]
source/app.d(7,16): Error: undefined identifier `CONSTANT_PLUS`
Error /usr/bin/dmd failed with exit code 1.
Comment #4 by bugzilla — 2023-03-20T20:31:39Z
ImportC is currently only able to convert simple #defines to enums, i.e. ones that are only a single token, like:
#define A 3
More complex ones are quite a bit more difficult to programmaticaly figure out what they are.
One pragmatic approach is to prepare by hand a table of triples:
1. the file name
2. the contents of the macro
3. the hand-crafted D translation
But, of course, this has its limits.