Jump to page: 1 2
Thread overview
need help to translate C into D
Sep 13, 2022
test123
Sep 13, 2022
Marvin
Sep 13, 2022
test123
Sep 13, 2022
Dennis
Sep 13, 2022
test123
Sep 13, 2022
test123
Sep 13, 2022
Dennis
Sep 13, 2022
test123
Sep 13, 2022
Dennis
Sep 13, 2022
test123
Sep 13, 2022
test123
Sep 13, 2022
test123
Sep 13, 2022
Ali Çehreli
Sep 14, 2022
test123
September 13, 2022

I can not use importC, I need it to be work in D code.

typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be tested with mask.
  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t data[];       // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;

I am not sure how to define the __gshared const upb_MiniTable_Enum object for this struct.

September 13, 2022

On Tuesday, 13 September 2022 at 06:04:49 UTC, test123 wrote:

>

I can not use importC, I need it to be work in D code.

typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be tested with mask.
  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t data[];       // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;

I am not sure how to define the __gshared const upb_MiniTable_Enum object for this struct.

struct mystruct {
  uint32_t mask_limit;   // Limit enum value that can be tested with mask.
  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t[] data;       // Bitmask + enumerated values follow.
} __gshared mystruct upb_MiniTable_Enum;
September 13, 2022

On Tuesday, 13 September 2022 at 07:31:50 UTC, Marvin wrote:

>

On Tuesday, 13 September 2022 at 06:04:49 UTC, test123 wrote:

>

I can not use importC, I need it to be work in D code.

typedef struct {
  uint32_t mask_limit;   // Limit enum value that can be tested with mask.
  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t data[];       // Bitmask + enumerated values follow.
} upb_MiniTable_Enum;

I am not sure how to define the __gshared const upb_MiniTable_Enum object for this struct.

struct mystruct {
  uint32_t mask_limit;   // Limit enum value that can be tested with mask.
  uint32_t value_count;  // Number of values after the bitfield.
  uint32_t[] data;       // Bitmask + enumerated values follow.
} __gshared mystruct upb_MiniTable_Enum;

This will not work since the C have no array like D.

September 13, 2022

On Tuesday, 13 September 2022 at 09:43:46 UTC, test123 wrote:

>

This will not work since the C have no array like D.

You can use a 0-size static array:

struct mystruct {
	uint32_t mask_limit;   // Limit enum value that can be tested with mask.
	uint32_t value_count;  // Number of values after the bitfield.
	uint32_t[0] data;      // Bitmask + enumerated values follow.
}

Then you have to index with mystructVar.data.ptr[i] to avoid bounds checking.

September 13, 2022

On Tuesday, 13 September 2022 at 09:57:38 UTC, Dennis wrote:

>

On Tuesday, 13 September 2022 at 09:43:46 UTC, test123 wrote:

>

This will not work since the C have no array like D.

You can use a 0-size static array:

struct mystruct {
	uint32_t mask_limit;   // Limit enum value that can be tested with mask.
	uint32_t value_count;  // Number of values after the bitfield.
	uint32_t[0] data;      // Bitmask + enumerated values follow.
}

Then you have to index with mystructVar.data.ptr[i] to avoid bounds checking.

Thanks, this look nice.

What I need is init the const object at CTFE.

I try this way:

    struct validate_KnownRegex_enum_init_type { const upb_MiniTable_Enum header; uint[2] data; }
    __gshared const validate_KnownRegex_enum_init_type validate_KnownRegex_enum_init = { {64, 2}, [7, 0] };

The problem is I can not assign it into one array (give error):

    __gshared const test = cast(upb_MiniTable_Enum*) &validate_KnownRegex_enum_init;
    __gshared const __enums_layout = [
        test,
    ];

This will also not work:

    __gshared const upb_MiniTable_Enum*[1] __enums_layout = [
        &validate_KnownRegex_enum_init.header,
    ];

Is there a way to init the __gshared fixed length upb_MiniTable_Enum array ?

September 13, 2022

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

>

upb_MiniTable_Enum array ?

2 type error I think it cloud be compiler bugs.

1): expression &validate_KnownRegex_enum_init_type(64u, 2u, [7u, 0u], ).header is not a constant

    union validate_KnownRegex_enum_init_type { struct {uint a, b; uint[2] data;} upb_MiniTable_Enum header; }
    __gshared const validate_KnownRegex_enum_init_type validate_KnownRegex_enum_init = { 64, 2, [7, 0] };
    __gshared const upb_MiniTable_Enum*[1] __enums_layout = [
        &validate_KnownRegex_enum_init.header,
    ];

2:) Error: reinterpreting cast from const(validate_KnownRegex_enum_init_type)toconst(upb_MiniTable_Enum) is not supported in CTFE

    struct validate_KnownRegex_enum_init_type { uint a, b; uint[2] data;}
    __gshared const validate_KnownRegex_enum_init_type validate_KnownRegex_enum_init = { 64, 2, [7, 0] };
    __gshared const upb_MiniTable_Enum*[1] __enums_layout = [
        cast(const upb_MiniTable_Enum*) &validate_KnownRegex_enum_init,
    ];
September 13, 2022

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

>

Is there a way to init the __gshared fixed length upb_MiniTable_Enum array ?

I don't think so. You could leave your array typed as validate_KnownRegex_enum_init_type and access it through a function that casts it to upb_MiniTable_Enum.

Side node, you can use immutable instead of __gshared const, it amounts to the same for global variables.

September 13, 2022

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

>

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

>

Is there a way to init the __gshared fixed length upb_MiniTable_Enum array ?

I don't think so. You could leave your array typed as validate_KnownRegex_enum_init_type and access it through a function that casts it to upb_MiniTable_Enum.

Side node, you can use immutable instead of __gshared const, it amounts to the same for global variables.

I can not do this. because fixed sized array upb_MiniTable_Enum[] is passed into other const object. and upb_MiniTable_Enum can include a lot diff types. (for example mixed diff size upb_MiniTable_Enum)

September 13, 2022

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

>

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

>

Is there a way to init the __gshared fixed length upb_MiniTable_Enum array ?

I don't think so. You could leave your array typed as validate_KnownRegex_enum_init_type and access it through a function that casts it to upb_MiniTable_Enum.

Side node, you can use immutable instead of __gshared const, it amounts to the same for global variables.

replace with immutable get this error:

static variable `__enums_layout` cannot be read at compile time
September 13, 2022

On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote:

>

On Tuesday, 13 September 2022 at 10:45:03 UTC, test123 wrote:

>

Is there a way to init the __gshared fixed length upb_MiniTable_Enum array ?

I don't think so. You could leave your array typed as validate_KnownRegex_enum_init_type and access it through a function that casts it to upb_MiniTable_Enum.

Side node, you can use immutable instead of __gshared const, it amounts to the same for global variables.

because __enums_layout.ptr need to be part of other object, and this const ptr cloud be included in multi objects.

« First   ‹ Prev
1 2