Skip to content

Commit 79cd915

Browse files
committed
TypeDecoder: Implement lane-wise pack expansion
1 parent d35b8aa commit 79cd915

File tree

6 files changed

+221
-58
lines changed

6 files changed

+221
-58
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 17 additions & 1 deletion
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

@@ -117,6 +125,14 @@ class ASTBuilder {
117125

118126
Type createPackExpansionType(Type patternType, Type countType);
119127

128+
size_t beginPackExpansion(Type countType);
129+
130+
void advancePackExpansion(size_t index);
131+
132+
Type createExpandedPackElement(Type patternType);
133+
134+
void endPackExpansion();
135+
120136
Type createFunctionType(
121137
ArrayRef<Demangle::FunctionParam<Type>> params,
122138
Type output, FunctionTypeFlags flags,

include/swift/Demangling/TypeDecoder.h

Lines changed: 111 additions & 52 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)
@@ -1031,24 +1031,31 @@ class TypeDecoder {
10311031
return MAKE_NODE_TYPE_ERROR0(element->getChild(typeChildIndex),
10321032
"no children");
10331033
}
1034+
1035+
StringRef label;
10341036
if (element->getChild(typeChildIndex)->getKind() == NodeKind::TupleElementName) {
1035-
labels.push_back(element->getChild(typeChildIndex)->getText());
1037+
label = element->getChild(typeChildIndex)->getText();
10361038
typeChildIndex++;
1037-
1038-
// Otherwise, add an empty label.
1039-
} else {
1040-
labels.push_back(StringRef());
10411039
}
10421040

10431041
// Decode the element type.
1044-
auto elementType =
1045-
decodeMangledType(element->getChild(typeChildIndex), depth + 1,
1046-
/*forRequirement=*/false);
1047-
if (elementType.isError())
1048-
return elementType;
1049-
1050-
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;
10511050
}
1051+
1052+
// Unwrap 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)
1057+
// return elements[0];
1058+
10521059
return Builder.createTupleType(elements, labels);
10531060
}
10541061
case NodeKind::TupleElement:
@@ -1074,12 +1081,13 @@ class TypeDecoder {
10741081

10751082
for (auto &element : *Node) {
10761083
// Decode the element type.
1077-
auto elementType =
1078-
decodeMangledType(element, depth + 1, /*forRequirement=*/false);
1079-
if (elementType.isError())
1080-
return elementType;
1081-
1082-
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;
10831091
}
10841092

10851093
switch (Node->getKind()) {
@@ -1256,9 +1264,9 @@ return {}; // Not Implemented!
12561264
/*forRequirement=*/false);
12571265
if (substTy.isError())
12581266
return substTy;
1259-
substitutions.emplace_back(
1260-
Builder.createGenericTypeParameterType(paramDepth, index),
1261-
substTy.getType());
1267+
auto paramTy = Builder.createGenericTypeParameterType(
1268+
paramDepth, index);
1269+
substitutions.emplace_back(paramTy, substTy.getType());
12621270
++index;
12631271
}
12641272
}
@@ -1361,6 +1369,58 @@ return {}; // Not Implemented!
13611369
}
13621370

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

1527-
bool decodeMangledFunctionInputType(
1587+
llvm::Optional<TypeLookupError> decodeMangledFunctionInputType(
15281588
Demangle::NodePointer node, unsigned depth,
15291589
llvm::SmallVectorImpl<FunctionParam<BuiltType>> &params,
15301590
bool &hasParamFlags) {
15311591
if (depth > TypeDecoder::MaxDepth)
1532-
return false;
1592+
return llvm::None;
15331593

15341594
// Look through a couple of sugar nodes.
15351595
if (node->getKind() == NodeKind::Type ||
@@ -1540,7 +1600,7 @@ return {}; // Not Implemented!
15401600

15411601
auto decodeParamTypeAndFlags =
15421602
[&](Demangle::NodePointer typeNode,
1543-
FunctionParam<BuiltType> &param) -> bool {
1603+
FunctionParam<BuiltType> &param) -> llvm::Optional<TypeLookupError> {
15441604
Demangle::NodePointer node = typeNode;
15451605

15461606
bool recurse = true;
@@ -1589,17 +1649,15 @@ return {}; // Not Implemented!
15891649
}
15901650
}
15911651

1592-
auto paramType = decodeMangledType(node, depth + 1,
1593-
/*forRequirement=*/false);
1594-
if (paramType.isError())
1595-
return false;
1596-
1597-
param.setType(paramType.getType());
1598-
return true;
1652+
return decodeTypeSequenceElement(node, depth + 1,
1653+
[&](BuiltType paramType) {
1654+
param.setType(paramType);
1655+
params.push_back(param);
1656+
});
15991657
};
16001658

16011659
auto decodeParam =
1602-
[&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType>> {
1660+
[&](NodePointer paramNode) -> llvm::Optional<TypeLookupError> {
16031661
if (paramNode->getKind() != NodeKind::TupleElement)
16041662
return None;
16051663

@@ -1615,40 +1673,41 @@ return {}; // Not Implemented!
16151673
hasParamFlags = true;
16161674
break;
16171675

1618-
case NodeKind::Type:
1619-
if (!decodeParamTypeAndFlags(child->getFirstChild(), param))
1620-
return None;
1676+
case NodeKind::Type: {
1677+
auto optError = decodeParamTypeAndFlags(
1678+
child->getFirstChild(), param);
1679+
if (optError)
1680+
return optError;
16211681
break;
1682+
}
16221683

16231684
default:
1624-
return None;
1685+
return TYPE_LOOKUP_ERROR_FMT("unknown node");
16251686
}
16261687
}
16271688

1628-
return param;
1689+
return None;
16291690
};
16301691

16311692
// Expand a single level of tuple.
16321693
if (node->getKind() == NodeKind::Tuple) {
16331694
// Decode all the elements as separate arguments.
16341695
for (const auto &elt : *node) {
1635-
auto param = decodeParam(elt);
1636-
if (!param)
1637-
return false;
1638-
1639-
params.push_back(std::move(*param));
1696+
auto optError = decodeParam(elt);
1697+
if (optError)
1698+
return *optError;
16401699
}
16411700

1642-
return true;
1701+
return None;
16431702
}
16441703

16451704
// Otherwise, handle the type as a single argument.
16461705
FunctionParam<BuiltType> param;
1647-
if (!decodeParamTypeAndFlags(node, param))
1648-
return false;
1706+
auto optError = decodeParamTypeAndFlags(node, param);
1707+
if (optError)
1708+
return *optError;
16491709

1650-
params.push_back(std::move(param));
1651-
return true;
1710+
return None;
16521711
}
16531712
};
16541713

include/swift/Remote/MetadataReader.h

Lines changed: 0 additions & 1 deletion
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;

include/swift/RemoteInspection/TypeRefBuilder.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,24 @@ class TypeRefBuilder {
764764
return nullptr;
765765
}
766766

767+
size_t beginPackExpansion(const TypeRef *countType) {
768+
// FIXME: Remote mirrors support for variadic generics.
769+
return 0;
770+
}
771+
772+
void advancePackExpansion(size_t index) {
773+
// FIXME: Remote mirrors support for variadic generics.
774+
}
775+
776+
const TypeRef *createExpandedPackElement(const TypeRef *patternType) {
777+
// FIXME: Remote mirrors support for variadic generics.
778+
return nullptr;
779+
}
780+
781+
void endPackExpansion() {
782+
// FIXME: Remote mirrors support for variadic generics.
783+
}
784+
767785
const FunctionTypeRef *createFunctionType(
768786
llvm::ArrayRef<remote::FunctionParam<const TypeRef *>> params,
769787
const TypeRef *result, FunctionTypeFlags flags,

lib/AST/ASTDemangler.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,26 @@ Type ASTBuilder::createPackExpansionType(Type patternType, Type countType) {
359359
return PackExpansionType::get(patternType, countType);
360360
}
361361

362+
size_t ASTBuilder::beginPackExpansion(Type countType) {
363+
ActivePackExpansions.push_back(countType);
364+
365+
return 1;
366+
}
367+
368+
void ASTBuilder::advancePackExpansion(size_t index) {
369+
assert(index == 0);
370+
}
371+
372+
Type ASTBuilder::createExpandedPackElement(Type patternType) {
373+
assert(!ActivePackExpansions.empty());
374+
auto countType = ActivePackExpansions.back();
375+
return PackExpansionType::get(patternType, countType);
376+
}
377+
378+
void ASTBuilder::endPackExpansion() {
379+
ActivePackExpansions.pop_back();
380+
}
381+
362382
Type ASTBuilder::createFunctionType(
363383
ArrayRef<Demangle::FunctionParam<Type>> params,
364384
Type output, FunctionTypeFlags flags,
@@ -876,7 +896,7 @@ Type ASTBuilder::createParenType(Type base) {
876896
GenericSignature
877897
ASTBuilder::createGenericSignature(ArrayRef<BuiltType> builtParams,
878898
ArrayRef<BuiltRequirement> requirements) {
879-
std::vector<BuiltGenericTypeParam> params;
899+
std::vector<GenericTypeParamType *> params;
880900
for (auto &param : builtParams) {
881901
auto paramTy = param->getAs<GenericTypeParamType>();
882902
if (!paramTy)

0 commit comments

Comments
 (0)