Skip to content

Commit 5efec1c

Browse files
authored
Merge pull request #67181 from slavapestov/runtime-pack-expansion-demangling-5.9
Runtime demangler support for pack expansions [5.9]
2 parents 6ee3764 + 64ce9b0 commit 5efec1c

File tree

12 files changed

+393
-175
lines changed

12 files changed

+393
-175
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,20 @@ class ASTBuilder {
6363
/// is a pack or not, so we have to find it here.
6464
GenericSignature GenericSig;
6565

66+
/// This builder doesn't perform "on the fly" substitutions, so we preserve
67+
/// all pack expansions. We still need an active expansion stack though,
68+
/// for the dummy implementation of these methods:
69+
/// - beginPackExpansion()
70+
/// - advancePackExpansion()
71+
/// - createExpandedPackElement()
72+
/// - endPackExpansion()
73+
llvm::SmallVector<Type> ActivePackExpansions;
74+
6675
public:
6776
using BuiltType = swift::Type;
6877
using BuiltTypeDecl = swift::GenericTypeDecl *; // nominal or type alias
6978
using BuiltProtocolDecl = swift::ProtocolDecl *;
7079
using BuiltGenericSignature = swift::GenericSignature;
71-
using BuiltGenericTypeParam = swift::GenericTypeParamType *;
7280
using BuiltRequirement = swift::Requirement;
7381
using BuiltSubstitutionMap = swift::SubstitutionMap;
7482

@@ -109,13 +117,19 @@ class ASTBuilder {
109117
Type createBoundGenericType(GenericTypeDecl *decl, ArrayRef<Type> args,
110118
Type parent);
111119

112-
Type createTupleType(ArrayRef<Type> eltTypes, StringRef labels);
120+
Type createTupleType(ArrayRef<Type> eltTypes, ArrayRef<StringRef> labels);
113121

114122
Type createPackType(ArrayRef<Type> eltTypes);
115123

116124
Type createSILPackType(ArrayRef<Type> eltTypes, bool isElementAddress);
117125

118-
Type createPackExpansionType(Type patternType, Type countType);
126+
size_t beginPackExpansion(Type countType);
127+
128+
void advancePackExpansion(size_t index);
129+
130+
Type createExpandedPackElement(Type patternType);
131+
132+
void endPackExpansion();
119133

120134
Type createFunctionType(
121135
ArrayRef<Demangle::FunctionParam<Type>> params,

include/swift/Demangling/TypeDecoder.h

Lines changed: 116 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ class TypeDecoder {
475475
using BuiltSubstitution = typename BuilderType::BuiltSubstitution;
476476
using BuiltRequirement = typename BuilderType::BuiltRequirement;
477477
using BuiltLayoutConstraint = typename BuilderType::BuiltLayoutConstraint;
478-
using BuiltGenericTypeParam = typename BuilderType::BuiltGenericTypeParam;
479478
using BuiltGenericSignature = typename BuilderType::BuiltGenericSignature;
480479
using BuiltSubstitutionMap = typename BuilderType::BuiltSubstitutionMap;
481480
using NodeKind = Demangle::Node::Kind;
@@ -883,10 +882,11 @@ class TypeDecoder {
883882

884883
bool hasParamFlags = false;
885884
llvm::SmallVector<FunctionParam<BuiltType>, 8> parameters;
886-
if (!decodeMangledFunctionInputType(Node->getChild(firstChildIdx),
887-
depth + 1, parameters, hasParamFlags))
888-
return MAKE_NODE_TYPE_ERROR0(Node->getChild(firstChildIdx),
889-
"failed to decode function type");
885+
auto optError = decodeMangledFunctionInputType(Node->getChild(firstChildIdx),
886+
depth + 1, parameters, hasParamFlags);
887+
if (optError)
888+
return *optError;
889+
890890
flags =
891891
flags.withNumParameters(parameters.size())
892892
.withParameterFlags(hasParamFlags)
@@ -1019,7 +1019,8 @@ class TypeDecoder {
10191019

10201020
case NodeKind::Tuple: {
10211021
llvm::SmallVector<BuiltType, 8> elements;
1022-
std::string labels;
1022+
llvm::SmallVector<StringRef, 8> labels;
1023+
10231024
for (auto &element : *Node) {
10241025
if (element->getKind() != NodeKind::TupleElement)
10251026
return MAKE_NODE_TYPE_ERROR0(Node, "unexpected kind");
@@ -1030,31 +1031,32 @@ class TypeDecoder {
10301031
return MAKE_NODE_TYPE_ERROR0(element->getChild(typeChildIndex),
10311032
"no children");
10321033
}
1033-
if (element->getChild(typeChildIndex)->getKind() == NodeKind::TupleElementName) {
1034-
// Add spaces to terminate all the previous labels if this
1035-
// is the first we've seen.
1036-
if (labels.empty()) labels.append(elements.size(), ' ');
10371034

1038-
// Add the label and its terminator.
1039-
labels += element->getChild(typeChildIndex)->getText();
1040-
labels += ' ';
1035+
StringRef label;
1036+
if (element->getChild(typeChildIndex)->getKind() == NodeKind::TupleElementName) {
1037+
label = element->getChild(typeChildIndex)->getText();
10411038
typeChildIndex++;
1042-
1043-
// Otherwise, add a space if a previous element had a label.
1044-
} else if (!labels.empty()) {
1045-
labels += ' ';
10461039
}
10471040

10481041
// Decode the element type.
1049-
auto elementType =
1050-
decodeMangledType(element->getChild(typeChildIndex), depth + 1,
1051-
/*forRequirement=*/false);
1052-
if (elementType.isError())
1053-
return elementType;
1054-
1055-
elements.push_back(elementType.getType());
1042+
auto optError = decodeTypeSequenceElement(
1043+
element->getChild(typeChildIndex), depth + 1,
1044+
[&](BuiltType type) {
1045+
elements.push_back(type);
1046+
labels.push_back(label);
1047+
});
1048+
if (optError)
1049+
return *optError;
10561050
}
1057-
return Builder.createTupleType(elements, std::move(labels));
1051+
1052+
// Unwrap unlabeled one-element tuples.
1053+
//
1054+
// FIXME: The behavior of one-element labeled tuples is inconsistent throughout
1055+
// the different re-implementations of type substitution and pack expansion.
1056+
if (elements.size() == 1 && labels[0].empty())
1057+
return elements[0];
1058+
1059+
return Builder.createTupleType(elements, labels);
10581060
}
10591061
case NodeKind::TupleElement:
10601062
if (Node->getNumChildren() < 1)
@@ -1079,12 +1081,13 @@ class TypeDecoder {
10791081

10801082
for (auto &element : *Node) {
10811083
// Decode the element type.
1082-
auto elementType =
1083-
decodeMangledType(element, depth + 1, /*forRequirement=*/false);
1084-
if (elementType.isError())
1085-
return elementType;
1086-
1087-
elements.push_back(elementType.getType());
1084+
auto optError = decodeTypeSequenceElement(
1085+
element, depth + 1,
1086+
[&](BuiltType elementType) {
1087+
elements.push_back(elementType);
1088+
});
1089+
if (optError)
1090+
return *optError;
10881091
}
10891092

10901093
switch (Node->getKind()) {
@@ -1100,16 +1103,8 @@ class TypeDecoder {
11001103
}
11011104

11021105
case NodeKind::PackExpansion: {
1103-
if (Node->getNumChildren() < 2)
1104-
return MAKE_NODE_TYPE_ERROR(Node,
1105-
"fewer children (%zu) than required (2)",
1106-
Node->getNumChildren());
1107-
1108-
auto patternType = decodeMangledType(Node->getChild(0), depth + 1);
1109-
auto countType = decodeMangledType(Node->getChild(1), depth + 1);
1110-
1111-
return Builder.createPackExpansionType(patternType.getType(),
1112-
countType.getType());
1106+
return MAKE_NODE_TYPE_ERROR0(Node,
1107+
"pack expansion type in unsupported position");
11131108
}
11141109

11151110
case NodeKind::DependentGenericType: {
@@ -1261,9 +1256,9 @@ return {}; // Not Implemented!
12611256
/*forRequirement=*/false);
12621257
if (substTy.isError())
12631258
return substTy;
1264-
substitutions.emplace_back(
1265-
Builder.createGenericTypeParameterType(paramDepth, index),
1266-
substTy.getType());
1259+
auto paramTy = Builder.createGenericTypeParameterType(
1260+
paramDepth, index);
1261+
substitutions.emplace_back(paramTy, substTy.getType());
12671262
++index;
12681263
}
12691264
}
@@ -1366,6 +1361,58 @@ return {}; // Not Implemented!
13661361
}
13671362

13681363
private:
1364+
template<typename Fn>
1365+
llvm::Optional<TypeLookupError>
1366+
decodeTypeSequenceElement(Demangle::NodePointer node, unsigned depth,
1367+
Fn resultCallback) {
1368+
if (node->getKind() == NodeKind::Type)
1369+
node = node->getChild(0);
1370+
1371+
if (node->getKind() == NodeKind::PackExpansion) {
1372+
if (node->getNumChildren() < 2)
1373+
return MAKE_NODE_TYPE_ERROR(node,
1374+
"fewer children (%zu) than required (2)",
1375+
node->getNumChildren());
1376+
1377+
auto patternType = node->getChild(0);
1378+
1379+
// Decode the shape pack first, to form a metadata pack.
1380+
auto countType = decodeMangledType(node->getChild(1), depth);
1381+
if (countType.isError())
1382+
return *countType.getError();
1383+
1384+
// Push the pack expansion on the active expansion stack inside the
1385+
// builder concept.
1386+
size_t numElements = Builder.beginPackExpansion(countType.getType());
1387+
1388+
for (size_t i = 0; i < numElements; ++i) {
1389+
// Advance the lane index inside the builder concept.
1390+
Builder.advancePackExpansion(i);
1391+
1392+
// Decode the pattern type, taking the ith element of each pack
1393+
// referenced therein.
1394+
auto expandedElementType = decodeMangledType(patternType, depth);
1395+
if (expandedElementType.isError())
1396+
return *expandedElementType.getError();
1397+
1398+
resultCallback(Builder.createExpandedPackElement(
1399+
expandedElementType.getType()));
1400+
}
1401+
1402+
// Pop the active expansion stack inside the builder concept.
1403+
Builder.endPackExpansion();
1404+
} else {
1405+
auto elementType =
1406+
decodeMangledType(node, depth, /*forRequirement=*/false);
1407+
if (elementType.isError())
1408+
return *elementType.getError();
1409+
1410+
resultCallback(elementType.getType());
1411+
}
1412+
1413+
return llvm::None;
1414+
}
1415+
13691416
template <typename T>
13701417
bool decodeImplFunctionPart(Demangle::NodePointer node, unsigned depth,
13711418
llvm::SmallVectorImpl<T> &results) {
@@ -1529,12 +1576,12 @@ return {}; // Not Implemented!
15291576
return Builder.createProtocolDecl(node);
15301577
}
15311578

1532-
bool decodeMangledFunctionInputType(
1579+
llvm::Optional<TypeLookupError> decodeMangledFunctionInputType(
15331580
Demangle::NodePointer node, unsigned depth,
15341581
llvm::SmallVectorImpl<FunctionParam<BuiltType>> &params,
15351582
bool &hasParamFlags) {
15361583
if (depth > TypeDecoder::MaxDepth)
1537-
return false;
1584+
return llvm::None;
15381585

15391586
// Look through a couple of sugar nodes.
15401587
if (node->getKind() == NodeKind::Type ||
@@ -1545,7 +1592,7 @@ return {}; // Not Implemented!
15451592

15461593
auto decodeParamTypeAndFlags =
15471594
[&](Demangle::NodePointer typeNode,
1548-
FunctionParam<BuiltType> &param) -> bool {
1595+
FunctionParam<BuiltType> &param) -> llvm::Optional<TypeLookupError> {
15491596
Demangle::NodePointer node = typeNode;
15501597

15511598
bool recurse = true;
@@ -1594,17 +1641,15 @@ return {}; // Not Implemented!
15941641
}
15951642
}
15961643

1597-
auto paramType = decodeMangledType(node, depth + 1,
1598-
/*forRequirement=*/false);
1599-
if (paramType.isError())
1600-
return false;
1601-
1602-
param.setType(paramType.getType());
1603-
return true;
1644+
return decodeTypeSequenceElement(node, depth + 1,
1645+
[&](BuiltType paramType) {
1646+
param.setType(paramType);
1647+
params.push_back(param);
1648+
});
16041649
};
16051650

16061651
auto decodeParam =
1607-
[&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType>> {
1652+
[&](NodePointer paramNode) -> llvm::Optional<TypeLookupError> {
16081653
if (paramNode->getKind() != NodeKind::TupleElement)
16091654
return None;
16101655

@@ -1620,40 +1665,41 @@ return {}; // Not Implemented!
16201665
hasParamFlags = true;
16211666
break;
16221667

1623-
case NodeKind::Type:
1624-
if (!decodeParamTypeAndFlags(child->getFirstChild(), param))
1625-
return None;
1668+
case NodeKind::Type: {
1669+
auto optError = decodeParamTypeAndFlags(
1670+
child->getFirstChild(), param);
1671+
if (optError)
1672+
return optError;
16261673
break;
1674+
}
16271675

16281676
default:
1629-
return None;
1677+
return TYPE_LOOKUP_ERROR_FMT("unknown node");
16301678
}
16311679
}
16321680

1633-
return param;
1681+
return None;
16341682
};
16351683

16361684
// Expand a single level of tuple.
16371685
if (node->getKind() == NodeKind::Tuple) {
16381686
// Decode all the elements as separate arguments.
16391687
for (const auto &elt : *node) {
1640-
auto param = decodeParam(elt);
1641-
if (!param)
1642-
return false;
1643-
1644-
params.push_back(std::move(*param));
1688+
auto optError = decodeParam(elt);
1689+
if (optError)
1690+
return *optError;
16451691
}
16461692

1647-
return true;
1693+
return None;
16481694
}
16491695

16501696
// Otherwise, handle the type as a single argument.
16511697
FunctionParam<BuiltType> param;
1652-
if (!decodeParamTypeAndFlags(node, param))
1653-
return false;
1698+
auto optError = decodeParamTypeAndFlags(node, param);
1699+
if (optError)
1700+
return *optError;
16541701

1655-
params.push_back(std::move(param));
1656-
return true;
1702+
return None;
16571703
}
16581704
};
16591705

include/swift/Remote/MetadataReader.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ class MetadataReader {
179179
using BuiltRequirement = typename BuilderType::BuiltRequirement;
180180
using BuiltSubstitution = typename BuilderType::BuiltSubstitution;
181181
using BuiltSubstitutionMap = typename BuilderType::BuiltSubstitutionMap;
182-
using BuiltGenericTypeParam = typename BuilderType::BuiltGenericTypeParam;
183182
using BuiltGenericSignature = typename BuilderType::BuiltGenericSignature;
184183
using StoredPointer = typename Runtime::StoredPointer;
185184
using StoredSignedPointer = typename Runtime::StoredSignedPointer;
@@ -870,13 +869,26 @@ class MetadataReader {
870869
}
871870

872871
// Read the labels string.
873-
std::string labels;
872+
std::string labelStr;
874873
if (tupleMeta->Labels &&
875-
!Reader->readString(RemoteAddress(tupleMeta->Labels), labels))
874+
!Reader->readString(RemoteAddress(tupleMeta->Labels), labelStr))
876875
return BuiltType();
877876

877+
std::vector<llvm::StringRef> labels;
878+
std::string::size_type end, start = 0;
879+
while (true) {
880+
end = labelStr.find(' ', start);
881+
if (end == std::string::npos)
882+
break;
883+
labels.push_back(llvm::StringRef(labelStr.data() + start, end - start));
884+
start = end + 1;
885+
}
886+
// Pad the vector with empty labels.
887+
for (unsigned i = labels.size(); i < elementTypes.size(); ++i)
888+
labels.push_back(StringRef());
889+
878890
auto BuiltTuple =
879-
Builder.createTupleType(elementTypes, std::move(labels));
891+
Builder.createTupleType(elementTypes, labels);
880892
TypeCache[MetadataAddress] = BuiltTuple;
881893
return BuiltTuple;
882894
}

0 commit comments

Comments
 (0)