Skip to content

Commit 9e0dee8

Browse files
Merge pull request #1355 from adrian-prantl/aggregate
Implement TypeSystemSwiftTypeRef::IsAggregateType() (NFC)
2 parents e90b7b1 + 7a67401 commit 9e0dee8

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

lldb/source/Symbol/TypeSystemSwiftTypeRef.cpp

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -801,35 +801,19 @@ template <> bool Equivalent<ConstString>(ConstString l, ConstString r) {
801801
return l == r;
802802
}
803803

804-
/// A list of testcases for which the validation is disabled.
805-
bool Whitelisted(SwiftASTContext &swift_ast_context) {
806-
auto *sym_file = swift_ast_context.GetSymbolFile();
807-
if (!sym_file)
808-
return false;
809-
auto *obj_file = sym_file->GetObjectFile();
810-
if (!obj_file)
811-
return false;
812-
auto &file_spec = obj_file->GetFileSpec();
813-
std::string path = file_spec.GetPath(false);
814-
llvm::StringRef p(path);
815-
return p.contains("lang/swift/clangimporter/include_conflict/"
816-
"TestSwiftIncludeConflict") ||
817-
p.contains("lang/swift/clangimporter/headermap_conflict/"
818-
"TestSwiftHeadermapConflict") ||
819-
p.contains("lang/swift/clangimporter/extra_clang_flags/"
820-
"TestSwiftExtraClangFlags");
821-
}
822804
}
823805
#endif
824806

825807
// This can be removed once the transition is complete.
826-
#define VALIDATE_AND_RETURN(IMPL, EXPECTED) \
808+
#define VALIDATE_AND_RETURN(IMPL, REFERENCE, TYPE, ...) \
827809
do { \
828810
auto result = IMPL(); \
829811
if (m_swift_ast_context) \
830-
assert((Equivalent(result, (EXPECTED)) || \
831-
Whitelisted(*m_swift_ast_context)) && \
832-
"TypeSystemSwiftTypeRef diverges from SwiftASTContext"); \
812+
assert( \
813+
!ReconstructType(TYPE) /* missing .swiftmodule */ || \
814+
(Equivalent(result, m_swift_ast_context->REFERENCE( \
815+
ReconstructType(TYPE), ##__VA_ARGS__))) && \
816+
"TypeSystemSwiftTypeRef diverges from SwiftASTContext"); \
833817
return result; \
834818
} while (0)
835819

@@ -912,13 +896,40 @@ bool TypeSystemSwiftTypeRef::IsArrayType(opaque_compiler_type_t type,
912896

913897
return true;
914898
};
915-
VALIDATE_AND_RETURN(
916-
impl, m_swift_ast_context->IsArrayType(ReconstructType(type), nullptr,
917-
nullptr, nullptr));
899+
VALIDATE_AND_RETURN(impl, IsArrayType, type, nullptr, nullptr, nullptr);
918900
}
901+
919902
bool TypeSystemSwiftTypeRef::IsAggregateType(opaque_compiler_type_t type) {
920-
return m_swift_ast_context->IsAggregateType(ReconstructType(type));
903+
auto impl = [&]() -> bool {
904+
using namespace swift::Demangle;
905+
Demangler Dem;
906+
NodePointer node = DemangleCanonicalType(Dem, type);
907+
908+
if (!node)
909+
return false;
910+
switch (node->getKind()) {
911+
case Node::Kind::Structure:
912+
case Node::Kind::Class:
913+
case Node::Kind::Enum:
914+
case Node::Kind::Tuple:
915+
case Node::Kind::Protocol:
916+
case Node::Kind::ProtocolList:
917+
case Node::Kind::ProtocolListWithClass:
918+
case Node::Kind::ProtocolListWithAnyObject:
919+
case Node::Kind::BoundGenericClass:
920+
case Node::Kind::BoundGenericEnum:
921+
case Node::Kind::BoundGenericStructure:
922+
case Node::Kind::BoundGenericProtocol:
923+
case Node::Kind::BoundGenericOtherNominalType:
924+
case Node::Kind::BoundGenericTypeAlias:
925+
return true;
926+
default:
927+
return false;
928+
}
929+
};
930+
VALIDATE_AND_RETURN(impl, IsAggregateType, type);
921931
}
932+
922933
bool TypeSystemSwiftTypeRef::IsDefined(opaque_compiler_type_t type) {
923934
return m_swift_ast_context->IsDefined(ReconstructType(type));
924935
}
@@ -938,8 +949,7 @@ bool TypeSystemSwiftTypeRef::IsFunctionType(opaque_compiler_type_t type,
938949
return node && (node->getKind() == Node::Kind::FunctionType ||
939950
node->getKind() == Node::Kind::ImplFunctionType);
940951
};
941-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->IsFunctionType(
942-
ReconstructType(type), nullptr));
952+
VALIDATE_AND_RETURN(impl, IsFunctionType, type, nullptr);
943953
}
944954
size_t TypeSystemSwiftTypeRef::GetNumberOfFunctionArguments(
945955
opaque_compiler_type_t type) {
@@ -966,8 +976,7 @@ size_t TypeSystemSwiftTypeRef::GetNumberOfFunctionArguments(
966976
}
967977
return num_args;
968978
};
969-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetNumberOfFunctionArguments(
970-
ReconstructType(type)));
979+
VALIDATE_AND_RETURN(impl, GetNumberOfFunctionArguments, type);
971980
}
972981
CompilerType
973982
TypeSystemSwiftTypeRef::GetFunctionArgumentAtIndex(opaque_compiler_type_t type,
@@ -1008,14 +1017,12 @@ TypeSystemSwiftTypeRef::GetFunctionArgumentAtIndex(opaque_compiler_type_t type,
10081017
}
10091018
return {};
10101019
};
1011-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetFunctionArgumentAtIndex(
1012-
ReconstructType(type), index));
1020+
VALIDATE_AND_RETURN(impl, GetFunctionArgumentAtIndex, type, index);
10131021
}
10141022
bool TypeSystemSwiftTypeRef::IsFunctionPointerType(
10151023
opaque_compiler_type_t type) {
10161024
auto impl = [&]() -> bool { return IsFunctionType(type, nullptr); };
1017-
VALIDATE_AND_RETURN(
1018-
impl, m_swift_ast_context->IsFunctionPointerType(ReconstructType(type)));
1025+
VALIDATE_AND_RETURN(impl, IsFunctionPointerType, type);
10191026
}
10201027
bool TypeSystemSwiftTypeRef::IsIntegerType(opaque_compiler_type_t type,
10211028
bool &is_signed) {
@@ -1042,8 +1049,7 @@ bool TypeSystemSwiftTypeRef::IsPointerType(opaque_compiler_type_t type,
10421049
(node->getText() == swift::BUILTIN_TYPE_NAME_NATIVEOBJECT) ||
10431050
(node->getText() == swift::BUILTIN_TYPE_NAME_BRIDGEOBJECT));
10441051
};
1045-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->IsPointerType(
1046-
ReconstructType(type), pointee_type));
1052+
VALIDATE_AND_RETURN(impl, IsPointerType, type, pointee_type);
10471053
}
10481054
bool TypeSystemSwiftTypeRef::IsScalarType(opaque_compiler_type_t type) {
10491055
return m_swift_ast_context->IsScalarType(ReconstructType(type));
@@ -1056,8 +1062,7 @@ bool TypeSystemSwiftTypeRef::IsVoidType(opaque_compiler_type_t type) {
10561062
return node && node->getNumChildren() == 0 &&
10571063
node->getKind() == Node::Kind::Tuple;
10581064
};
1059-
VALIDATE_AND_RETURN(impl,
1060-
m_swift_ast_context->IsVoidType(ReconstructType(type)));
1065+
VALIDATE_AND_RETURN(impl, IsVoidType, type);
10611066
}
10621067
// Type Completion
10631068
bool TypeSystemSwiftTypeRef::GetCompleteType(opaque_compiler_type_t type) {
@@ -1083,8 +1088,7 @@ ConstString TypeSystemSwiftTypeRef::GetTypeName(opaque_compiler_type_t type) {
10831088
return ConstString(SwiftLanguageRuntime::DemangleSymbolAsString(
10841089
remangled, SwiftLanguageRuntime::eTypeName));
10851090
};
1086-
VALIDATE_AND_RETURN(impl,
1087-
m_swift_ast_context->GetTypeName(ReconstructType(type)));
1091+
VALIDATE_AND_RETURN(impl, GetTypeName, type);
10881092
}
10891093
ConstString
10901094
TypeSystemSwiftTypeRef::GetDisplayTypeName(opaque_compiler_type_t type,
@@ -1103,8 +1107,7 @@ TypeSystemSwiftTypeRef::GetDisplayTypeName(opaque_compiler_type_t type,
11031107
return ConstString(SwiftLanguageRuntime::DemangleSymbolAsString(
11041108
remangled, SwiftLanguageRuntime::eDisplayTypeName, sc));
11051109
};
1106-
VALIDATE_AND_RETURN(
1107-
impl, m_swift_ast_context->GetDisplayTypeName(ReconstructType(type), sc));
1110+
VALIDATE_AND_RETURN(impl, GetDisplayTypeName, type, sc);
11081111
}
11091112
uint32_t TypeSystemSwiftTypeRef::GetTypeInfo(
11101113
opaque_compiler_type_t type, CompilerType *pointee_or_element_clang_type) {
@@ -1129,8 +1132,7 @@ TypeSystemSwiftTypeRef::GetArrayElementType(opaque_compiler_type_t type,
11291132
IsArrayType(type, &element_type, nullptr, nullptr);
11301133
return element_type;
11311134
};
1132-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetArrayElementType(
1133-
ReconstructType(type), nullptr));
1135+
VALIDATE_AND_RETURN(impl, GetArrayElementType, type, nullptr);
11341136
}
11351137
CompilerType
11361138
TypeSystemSwiftTypeRef::GetCanonicalType(opaque_compiler_type_t type) {
@@ -1139,14 +1141,12 @@ TypeSystemSwiftTypeRef::GetCanonicalType(opaque_compiler_type_t type) {
11391141
int TypeSystemSwiftTypeRef::GetFunctionArgumentCount(
11401142
opaque_compiler_type_t type) {
11411143
auto impl = [&]() -> int { return GetNumberOfFunctionArguments(type); };
1142-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetFunctionArgumentCount(
1143-
ReconstructType(type)));
1144+
VALIDATE_AND_RETURN(impl, GetFunctionArgumentCount, type);
11441145
}
11451146
CompilerType TypeSystemSwiftTypeRef::GetFunctionArgumentTypeAtIndex(
11461147
opaque_compiler_type_t type, size_t idx) {
11471148
auto impl = [&] { return GetFunctionArgumentAtIndex(type, idx); };
1148-
VALIDATE_AND_RETURN(impl, m_swift_ast_context->GetFunctionArgumentTypeAtIndex(
1149-
ReconstructType(type), idx));
1149+
VALIDATE_AND_RETURN(impl, GetFunctionArgumentTypeAtIndex, type, idx);
11501150
}
11511151
CompilerType
11521152
TypeSystemSwiftTypeRef::GetFunctionReturnType(opaque_compiler_type_t type) {
@@ -1177,8 +1177,7 @@ TypeSystemSwiftTypeRef::GetFunctionReturnType(opaque_compiler_type_t type) {
11771177
type->addChild(tuple, Dem);
11781178
return RemangleAsType(Dem, type);
11791179
};
1180-
VALIDATE_AND_RETURN(
1181-
impl, m_swift_ast_context->GetFunctionReturnType(ReconstructType(type)));
1180+
VALIDATE_AND_RETURN(impl, GetFunctionReturnType, type);
11821181
}
11831182
size_t
11841183
TypeSystemSwiftTypeRef::GetNumMemberFunctions(opaque_compiler_type_t type) {
@@ -1438,9 +1437,7 @@ bool TypeSystemSwiftTypeRef::IsReferenceType(opaque_compiler_type_t type,
14381437
return true;
14391438
};
14401439

1441-
VALIDATE_AND_RETURN(
1442-
impl, m_swift_ast_context->IsReferenceType(ReconstructType(type),
1443-
pointee_type, is_rvalue));
1440+
VALIDATE_AND_RETURN(impl, IsReferenceType, type, pointee_type, is_rvalue);
14441441
}
14451442
bool TypeSystemSwiftTypeRef::ShouldTreatScalarValueAsAddress(
14461443
opaque_compiler_type_t type) {

lldb/unittests/Symbol/TestTypeSystemSwiftTypeRef.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class NodeBuilder {
4242
NodePointer Node(Kind kind, StringRef text) {
4343
return m_dem.createNode(kind, text);
4444
}
45+
NodePointer NodeWithIndex(Kind kind, swift::Demangle::Node::IndexType index) {
46+
return m_dem.createNode(kind, index);
47+
}
4548
NodePointer Node(Kind kind, NodePointer child0 = nullptr,
4649
NodePointer child1 = nullptr,
4750
NodePointer child2 = nullptr,
@@ -244,3 +247,23 @@ TEST_F(TestTypeSystemSwiftTypeRef, Reference) {
244247
ASSERT_FALSE(is_rvalue);
245248
}
246249
}
250+
251+
TEST_F(TestTypeSystemSwiftTypeRef, Aggregate) {
252+
using namespace swift::Demangle;
253+
Demangler dem;
254+
NodeBuilder b(dem);
255+
{
256+
NodePointer n = b.GlobalType(b.Node(Node::Kind::Tuple));
257+
CompilerType tuple = GetCompilerType(b.Mangle(n));
258+
ASSERT_TRUE(tuple.IsAggregateType());
259+
// Yes, Int is a struct.
260+
NodePointer int_node = b.GlobalTypeMangling(b.IntType());
261+
CompilerType int_type = GetCompilerType(b.Mangle(int_node));
262+
ASSERT_TRUE(int_type.IsAggregateType());
263+
NodePointer t = b.GlobalType(b.Node(Node::Kind::DependentGenericParamType,
264+
b.NodeWithIndex(Node::Kind::Index, 0),
265+
b.NodeWithIndex(Node::Kind::Index, 0)));
266+
CompilerType tau = GetCompilerType(b.Mangle(t));
267+
ASSERT_FALSE(tau.IsAggregateType());
268+
}
269+
}

0 commit comments

Comments
 (0)