RNA: Fix compile error when assigning to a typed enum in C++ #109136

Merged
Julian Eisel merged 2 commits from JulianEisel/blender:temp-makesrna-setter-cast into main 2023-06-20 11:30:32 +02:00
Member

When a RNA file was compiled in C++, building would fail when defining a
RNA enum property that assigns to a enum value with a defined (i.e.
non-integer) type.


For example, #104831 contains the following in a non-DNA header:

typedef struct AssetShelfType {
  /*...*/
    AssetShelfTypeFlag flag;
  /*...*/
} AssetShelfType;

And a RNA definition like this:

prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "type->flag");
/*...*/

There's no custom setter callback defined so it would be generated by
makesrna. It would be generated as data->type->flag = value; whereby
value is of type int, which doesn't compile in C++ without cast to the
enum type.

When a RNA file was compiled in C++, building would fail when defining a RNA enum property that assigns to a enum value with a defined (i.e. non-integer) type. ---- For example, #104831 contains the following in a non-DNA header: ```C typedef struct AssetShelfType { /*...*/ AssetShelfTypeFlag flag; /*...*/ } AssetShelfType; ``` And a RNA definition like this: ```C prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE); RNA_def_property_enum_sdna(prop, NULL, "type->flag"); /*...*/ ``` There's no custom setter callback defined so it would be generated by `makesrna`. It would be generated as `data->type->flag = value;` whereby `value` is of type `int`, which doesn't compile in C++ without cast to the enum type.
Julian Eisel added the
Module
Core
label 2023-06-19 19:51:06 +02:00
Julian Eisel added 1 commit 2023-06-19 19:51:20 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
14599f0258
RNA: Fix compile error when assigning to a typed enum in C++
When a RNA file was compiled in C++, building would fail when defining a
RNA enum property that assigns to a enum value with a defined (i.e.
non-integer) type.

----

For example, #104831 contains the following in a non-DNA header:
```
typedef struct AssetShelfType {
  /*...*/
    AssetShelfTypeFlag flag;
  /*...*/
} AssetShelfType;
```

And a RNA definition like this:
```
prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "type->flag");
/*...*/
```

There's no custom setter callback defined so it would be generated by
`makesrna`. It would be generated as `data->type->flag = value;` whereby
`value` is of type `int`, which doesn't work in C++. The integer value
needs to be cast to the enum type.
Author
Member

@blender-bot build

@blender-bot build
Julian Eisel requested review from Jacques Lucke 2023-06-19 19:55:18 +02:00
Member

Afaik we don't allow using enums directly in DNA structs, because their size is not consistent between compilers.

[EDIT] Ah wait, misread, one moment.

Afaik we don't allow using enums directly in DNA structs, because their size is not consistent between compilers. [EDIT] Ah wait, misread, one moment.
Member

Assuming typeof compiles with all compilers we support, this is fine. Currently, I'd be surprised if that this extension is supported by every compiler, but we'll see.

If this issue is in C++ only, it maybe better to have a separate code path for C++ that uses decltype.

Assuming `typeof` compiles with all compilers we support, this is fine. Currently, I'd be surprised if that this extension is supported by every compiler, but we'll see. If this issue is in C++ only, it maybe better to have a separate code path for C++ that uses `decltype`.
Jacques Lucke approved these changes 2023-06-19 20:08:37 +02:00
Author
Member

Meeh, MSVC fails of course. Always thought typeof() is C standard like sizeof(). Back to ifdef'ed decltype() then.

Meeh, MSVC fails of course. Always thought `typeof()` is C standard like `sizeof()`. Back to `ifdef`'ed `decltype()` then.
Julian Eisel changed title from RNA: Fix compile error when assigning to a typed enum in C++ to WIP: RNA: Fix compile error when assigning to a typed enum in C++ 2023-06-19 20:16:08 +02:00
Julian Eisel added 1 commit 2023-06-19 21:01:21 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
5be805f9f0
Use `decltype()` instead of `typeof()`
Julian Eisel changed title from WIP: RNA: Fix compile error when assigning to a typed enum in C++ to RNA: Fix compile error when assigning to a typed enum in C++ 2023-06-19 21:01:54 +02:00
Author
Member

Using decltype convolutes code a bit, which is annoying. Generated output looks like this for example:

RNA_EXTERN_C void Panel_bl_space_type_set(PointerRNA *ptr, int value)
{
    Panel *data = (Panel *)(ptr->data);
#ifdef __cplusplus
    data->type->space_type = (std::remove_reference_t<decltype(data->type->space_type)>)value;
#else
    data->type->space_type = value;
#endif
}

I guess using -fpermissive is not an option, and I don't see another flag addressing this build error.

Using decltype convolutes code a bit, which is annoying. Generated output looks like this for example: ```Cpp RNA_EXTERN_C void Panel_bl_space_type_set(PointerRNA *ptr, int value) { Panel *data = (Panel *)(ptr->data); #ifdef __cplusplus data->type->space_type = (std::remove_reference_t<decltype(data->type->space_type)>)value; #else data->type->space_type = value; #endif } ``` I guess using `-fpermissive` is not an option, and I don't see another flag addressing this build error.
Julian Eisel requested review from Jacques Lucke 2023-06-19 21:05:09 +02:00
Jacques Lucke approved these changes 2023-06-19 21:06:43 +02:00
Jacques Lucke left a comment
Member

Could also use std::decay_t but this is fine as well.

Could also use `std::decay_t` but this is fine as well.
Author
Member

Could also use std::decay_t but this is fine as well.

std::remove_reference_t makes the intent explicit, so I prefer that.

Thanks!

> Could also use `std::decay_t` but this is fine as well. `std::remove_reference_t` makes the intent explicit, so I prefer that. Thanks!
Author
Member

@blender-bot build

@blender-bot build
Julian Eisel merged commit fa59084025 into main 2023-06-20 11:30:32 +02:00
Julian Eisel deleted branch temp-makesrna-setter-cast 2023-06-20 11:30:33 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109136
No description provided.