Skip to content

Add support for C unions in Swift. #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SWIFT_SOURCES := main.swift
SWIFTFLAGS_EXTRAS = -I$(SRCDIR)

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil
import os
import unittest2


class TestSwiftAnyType(lldbtest.TestBase):

mydir = lldbtest.TestBase.compute_mydir(__file__)

@swiftTest
def test_c_unions(self):
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'))
self.expect("target variable -- i", substrs=['42'])
self.expect("target variable -- d", substrs=['23'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import U

func use<T>(_ t : T) {}

let i = IntDoubleUnion(i: 42)
let d = IntDoubleUnion(d: 23)
use((i, d)) // break here
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module U { header "union.h" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
union IntDoubleUnion {
int i;
double d;
};
42 changes: 42 additions & 0 deletions lldb/source/Symbol/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6648,6 +6648,16 @@ uint32_t SwiftASTContext::GetNumFields(void *type) {
case swift::TypeKind::BoundGenericClass:
case swift::TypeKind::BoundGenericStruct: {
auto nominal = swift_can_type->getAnyNominal();
// Imported unions don't have stored properties.
if (auto *ntd =
llvm::dyn_cast_or_null<swift::NominalTypeDecl>(nominal->getDecl()))
if (auto *rd = llvm::dyn_cast_or_null<clang::RecordDecl>(ntd->getClangDecl()))
if (rd->isUnion()) {
swift::DeclRange ms = ntd->getMembers();
return std::count_if(ms.begin(), ms.end(), [](swift::Decl *D) {
return llvm::isa<swift::VarDecl>(D);
});
}
return GetStoredProperties(nominal).size();
}

Expand Down Expand Up @@ -7295,6 +7305,38 @@ CompilerType SwiftASTContext::GetChildCompilerTypeAtIndex(
case swift::TypeKind::Struct:
case swift::TypeKind::BoundGenericStruct: {
auto nominal = swift_can_type->getAnyNominal();

// Imported unions don't have stored properties, iterate over the
// VarDecls instead.
if (auto *ntd =
llvm::dyn_cast_or_null<swift::NominalTypeDecl>(nominal->getDecl()))
if (auto *rd = llvm::dyn_cast_or_null<clang::RecordDecl>(ntd->getClangDecl()))
if (rd->isUnion()) {
unsigned count = 0;
for (swift::Decl *D : ntd->getMembers()) {
auto *VD = llvm::dyn_cast_or_null<swift::VarDecl>(D);
if (!VD)
continue;
if (count++ < idx)
continue;

swift::Type child_swift_type = VD->getType();

CompilerType child_type =
ToCompilerType(VD->getType().getPointer());
child_name = VD->getNameStr();
if (!get_type_size(child_byte_size, child_type))
return {};
child_is_base_class = false;
child_is_deref_of_parent = false;
child_byte_offset = 0;
child_bitfield_bit_size = 0;
child_bitfield_bit_offset = 0;
return child_type;
}
return {};
}

auto stored_properties = GetStoredProperties(nominal);
if (idx >= stored_properties.size())
break;
Expand Down