From f9b8512dc298e7e2bcd955ee89cda1e659f77a5c Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 30 Jan 2018 21:35:42 +0000 Subject: [PATCH] Wrap tag into anonymous struct in C++ Fixes #122 --- src/bindgen/ir/enumeration.rs | 14 ++++++++++++++ tests/expectations/display_list.cpp | 4 +++- tests/expectations/enum.cpp | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index a21f1e4..cf34fd6 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -357,7 +357,21 @@ impl Source for Enum { out.open_brace(); } + // C++ allows accessing only common initial sequence of union + // branches so we need to wrap tag into an anonymous struct + let wrap_tag = config.language == Language::Cxx && !separate_tag; + + if wrap_tag { + out.write("struct"); + out.open_brace(); + } + write!(out, "{} tag;", enum_name); + + if wrap_tag { + out.close_brace(true); + } + out.new_line(); if separate_tag { diff --git a/tests/expectations/display_list.cpp b/tests/expectations/display_list.cpp index 82f73cf..4833de0 100644 --- a/tests/expectations/display_list.cpp +++ b/tests/expectations/display_list.cpp @@ -34,7 +34,9 @@ union DisplayItem { Rect bounds; }; - Tag tag; + struct { + Tag tag; + }; Fill_Body fill; Image_Body image; }; diff --git a/tests/expectations/enum.cpp b/tests/expectations/enum.cpp index 8744afd..c90c2d4 100644 --- a/tests/expectations/enum.cpp +++ b/tests/expectations/enum.cpp @@ -56,7 +56,9 @@ union F { int16_t y; }; - Tag tag; + struct { + Tag tag; + }; Foo_Body foo; Bar_Body bar; };