@@ -478,7 +478,6 @@ class TypeDecoder {
478
478
using BuiltSubstitution = typename BuilderType::BuiltSubstitution;
479
479
using BuiltRequirement = typename BuilderType::BuiltRequirement;
480
480
using BuiltLayoutConstraint = typename BuilderType::BuiltLayoutConstraint;
481
- using BuiltGenericTypeParam = typename BuilderType::BuiltGenericTypeParam;
482
481
using BuiltGenericSignature = typename BuilderType::BuiltGenericSignature;
483
482
using BuiltSubstitutionMap = typename BuilderType::BuiltSubstitutionMap;
484
483
using NodeKind = Demangle::Node::Kind;
@@ -886,10 +885,11 @@ class TypeDecoder {
886
885
887
886
bool hasParamFlags = false ;
888
887
llvm::SmallVector<FunctionParam<BuiltType>, 8 > parameters;
889
- if (!decodeMangledFunctionInputType (Node->getChild (firstChildIdx),
890
- depth + 1 , parameters, hasParamFlags))
891
- return MAKE_NODE_TYPE_ERROR0 (Node->getChild (firstChildIdx),
892
- " failed to decode function type" );
888
+ auto optError = decodeMangledFunctionInputType (Node->getChild (firstChildIdx),
889
+ depth + 1 , parameters, hasParamFlags);
890
+ if (optError)
891
+ return *optError;
892
+
893
893
flags =
894
894
flags.withNumParameters (parameters.size ())
895
895
.withParameterFlags (hasParamFlags)
@@ -1034,24 +1034,31 @@ class TypeDecoder {
1034
1034
return MAKE_NODE_TYPE_ERROR0 (element->getChild (typeChildIndex),
1035
1035
" no children" );
1036
1036
}
1037
+
1038
+ StringRef label;
1037
1039
if (element->getChild (typeChildIndex)->getKind () == NodeKind::TupleElementName) {
1038
- labels. push_back ( element->getChild (typeChildIndex)->getText () );
1040
+ label = element->getChild (typeChildIndex)->getText ();
1039
1041
typeChildIndex++;
1040
-
1041
- // Otherwise, add an empty label.
1042
- } else {
1043
- labels.push_back (StringRef ());
1044
1042
}
1045
1043
1046
1044
// Decode the element type.
1047
- auto elementType =
1048
- decodeMangledType (element->getChild (typeChildIndex), depth + 1 ,
1049
- /* forRequirement=*/ false );
1050
- if (elementType.isError ())
1051
- return elementType;
1052
-
1053
- elements.push_back (elementType.getType ());
1045
+ auto optError = decodeTypeSequenceElement (
1046
+ element->getChild (typeChildIndex), depth + 1 ,
1047
+ [&](BuiltType type) {
1048
+ elements.push_back (type);
1049
+ labels.push_back (label);
1050
+ });
1051
+ if (optError)
1052
+ return *optError;
1054
1053
}
1054
+
1055
+ // Unwrap one-element tuples.
1056
+ //
1057
+ // FIXME: The behavior of one-element labeled tuples is inconsistent throughout
1058
+ // the different re-implementations of type substitution and pack expansion.
1059
+ // if (elements.size() == 1)
1060
+ // return elements[0];
1061
+
1055
1062
return Builder.createTupleType (elements, labels);
1056
1063
}
1057
1064
case NodeKind::TupleElement:
@@ -1077,12 +1084,13 @@ class TypeDecoder {
1077
1084
1078
1085
for (auto &element : *Node) {
1079
1086
// Decode the element type.
1080
- auto elementType =
1081
- decodeMangledType (element, depth + 1 , /* forRequirement=*/ false );
1082
- if (elementType.isError ())
1083
- return elementType;
1084
-
1085
- elements.push_back (elementType.getType ());
1087
+ auto optError = decodeTypeSequenceElement (
1088
+ element, depth + 1 ,
1089
+ [&](BuiltType elementType) {
1090
+ elements.push_back (elementType);
1091
+ });
1092
+ if (optError)
1093
+ return *optError;
1086
1094
}
1087
1095
1088
1096
switch (Node->getKind ()) {
@@ -1259,9 +1267,9 @@ return {}; // Not Implemented!
1259
1267
/* forRequirement=*/ false );
1260
1268
if (substTy.isError ())
1261
1269
return substTy;
1262
- substitutions. emplace_back (
1263
- Builder. createGenericTypeParameterType ( paramDepth, index),
1264
- substTy.getType ());
1270
+ auto paramTy = Builder. createGenericTypeParameterType (
1271
+ paramDepth, index);
1272
+ substitutions. emplace_back (paramTy, substTy.getType ());
1265
1273
++index;
1266
1274
}
1267
1275
}
@@ -1364,6 +1372,58 @@ return {}; // Not Implemented!
1364
1372
}
1365
1373
1366
1374
private:
1375
+ template <typename Fn>
1376
+ llvm::Optional<TypeLookupError>
1377
+ decodeTypeSequenceElement (Demangle::NodePointer node, unsigned depth,
1378
+ Fn resultCallback) {
1379
+ if (node->getKind () == NodeKind::Type)
1380
+ node = node->getChild (0 );
1381
+
1382
+ if (node->getKind () == NodeKind::PackExpansion) {
1383
+ if (node->getNumChildren () < 2 )
1384
+ return MAKE_NODE_TYPE_ERROR (node,
1385
+ " fewer children (%zu) than required (2)" ,
1386
+ node->getNumChildren ());
1387
+
1388
+ auto patternType = node->getChild (0 );
1389
+
1390
+ // Decode the shape pack first, to form a metadata pack.
1391
+ auto countType = decodeMangledType (node->getChild (1 ), depth);
1392
+ if (countType.isError ())
1393
+ return *countType.getError ();
1394
+
1395
+ // Push the pack expansion on the active expansion stack inside the
1396
+ // builder concept.
1397
+ size_t numElements = Builder.beginPackExpansion (countType.getType ());
1398
+
1399
+ for (size_t i = 0 ; i < numElements; ++i) {
1400
+ // Advance the lane index inside the builder concept.
1401
+ Builder.advancePackExpansion (i);
1402
+
1403
+ // Decode the pattern type, taking the ith element of each pack
1404
+ // referenced therein.
1405
+ auto expandedElementType = decodeMangledType (patternType, depth);
1406
+ if (expandedElementType.isError ())
1407
+ return *expandedElementType.getError ();
1408
+
1409
+ resultCallback (Builder.createExpandedPackElement (
1410
+ expandedElementType.getType ()));
1411
+ }
1412
+
1413
+ // Pop the active expansion stack inside the builder concept.
1414
+ Builder.endPackExpansion ();
1415
+ } else {
1416
+ auto elementType =
1417
+ decodeMangledType (node, depth, /* forRequirement=*/ false );
1418
+ if (elementType.isError ())
1419
+ return *elementType.getError ();
1420
+
1421
+ resultCallback (elementType.getType ());
1422
+ }
1423
+
1424
+ return llvm::None;
1425
+ }
1426
+
1367
1427
template <typename T>
1368
1428
bool decodeImplFunctionPart (Demangle::NodePointer node, unsigned depth,
1369
1429
llvm::SmallVectorImpl<T> &results) {
@@ -1527,12 +1587,12 @@ return {}; // Not Implemented!
1527
1587
return Builder.createProtocolDecl (node);
1528
1588
}
1529
1589
1530
- bool decodeMangledFunctionInputType (
1590
+ llvm::Optional<TypeLookupError> decodeMangledFunctionInputType (
1531
1591
Demangle::NodePointer node, unsigned depth,
1532
1592
llvm::SmallVectorImpl<FunctionParam<BuiltType>> ¶ms,
1533
1593
bool &hasParamFlags) {
1534
1594
if (depth > TypeDecoder::MaxDepth)
1535
- return false ;
1595
+ return llvm::None ;
1536
1596
1537
1597
// Look through a couple of sugar nodes.
1538
1598
if (node->getKind () == NodeKind::Type ||
@@ -1543,7 +1603,7 @@ return {}; // Not Implemented!
1543
1603
1544
1604
auto decodeParamTypeAndFlags =
1545
1605
[&](Demangle::NodePointer typeNode,
1546
- FunctionParam<BuiltType> ¶m) -> bool {
1606
+ FunctionParam<BuiltType> ¶m) -> llvm::Optional<TypeLookupError> {
1547
1607
Demangle::NodePointer node = typeNode;
1548
1608
1549
1609
bool recurse = true ;
@@ -1592,17 +1652,15 @@ return {}; // Not Implemented!
1592
1652
}
1593
1653
}
1594
1654
1595
- auto paramType = decodeMangledType (node, depth + 1 ,
1596
- /* forRequirement=*/ false );
1597
- if (paramType.isError ())
1598
- return false ;
1599
-
1600
- param.setType (paramType.getType ());
1601
- return true ;
1655
+ return decodeTypeSequenceElement (node, depth + 1 ,
1656
+ [&](BuiltType paramType) {
1657
+ param.setType (paramType);
1658
+ params.push_back (param);
1659
+ });
1602
1660
};
1603
1661
1604
1662
auto decodeParam =
1605
- [&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType> > {
1663
+ [&](NodePointer paramNode) -> llvm::Optional<TypeLookupError > {
1606
1664
if (paramNode->getKind () != NodeKind::TupleElement)
1607
1665
return llvm::None;
1608
1666
@@ -1618,40 +1676,41 @@ return {}; // Not Implemented!
1618
1676
hasParamFlags = true ;
1619
1677
break ;
1620
1678
1621
- case NodeKind::Type:
1622
- if (!decodeParamTypeAndFlags (child->getFirstChild (), param))
1623
- return llvm::None;
1679
+ case NodeKind::Type: {
1680
+ auto optError = decodeParamTypeAndFlags (
1681
+ child->getFirstChild (), param);
1682
+ if (optError)
1683
+ return optError;
1624
1684
break ;
1685
+ }
1625
1686
1626
1687
default :
1627
- return llvm::None ;
1688
+ return TYPE_LOOKUP_ERROR_FMT ( " unknown node " ) ;
1628
1689
}
1629
1690
}
1630
1691
1631
- return param ;
1692
+ return llvm::None ;
1632
1693
};
1633
1694
1634
1695
// Expand a single level of tuple.
1635
1696
if (node->getKind () == NodeKind::Tuple) {
1636
1697
// Decode all the elements as separate arguments.
1637
1698
for (const auto &elt : *node) {
1638
- auto param = decodeParam (elt);
1639
- if (!param)
1640
- return false ;
1641
-
1642
- params.push_back (std::move (*param));
1699
+ auto optError = decodeParam (elt);
1700
+ if (optError)
1701
+ return *optError;
1643
1702
}
1644
1703
1645
- return true ;
1704
+ return llvm::None ;
1646
1705
}
1647
1706
1648
1707
// Otherwise, handle the type as a single argument.
1649
1708
FunctionParam<BuiltType> param;
1650
- if (!decodeParamTypeAndFlags (node, param))
1651
- return false ;
1709
+ auto optError = decodeParamTypeAndFlags (node, param);
1710
+ if (optError)
1711
+ return *optError;
1652
1712
1653
- params.push_back (std::move (param));
1654
- return true ;
1713
+ return llvm::None;
1655
1714
}
1656
1715
};
1657
1716
0 commit comments