@@ -475,7 +475,6 @@ class TypeDecoder {
475
475
using BuiltSubstitution = typename BuilderType::BuiltSubstitution;
476
476
using BuiltRequirement = typename BuilderType::BuiltRequirement;
477
477
using BuiltLayoutConstraint = typename BuilderType::BuiltLayoutConstraint;
478
- using BuiltGenericTypeParam = typename BuilderType::BuiltGenericTypeParam;
479
478
using BuiltGenericSignature = typename BuilderType::BuiltGenericSignature;
480
479
using BuiltSubstitutionMap = typename BuilderType::BuiltSubstitutionMap;
481
480
using NodeKind = Demangle::Node::Kind;
@@ -883,10 +882,11 @@ class TypeDecoder {
883
882
884
883
bool hasParamFlags = false ;
885
884
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
+
890
890
flags =
891
891
flags.withNumParameters (parameters.size ())
892
892
.withParameterFlags (hasParamFlags)
@@ -1031,24 +1031,31 @@ class TypeDecoder {
1031
1031
return MAKE_NODE_TYPE_ERROR0 (element->getChild (typeChildIndex),
1032
1032
" no children" );
1033
1033
}
1034
+
1035
+ StringRef label;
1034
1036
if (element->getChild (typeChildIndex)->getKind () == NodeKind::TupleElementName) {
1035
- labels. push_back ( element->getChild (typeChildIndex)->getText () );
1037
+ label = element->getChild (typeChildIndex)->getText ();
1036
1038
typeChildIndex++;
1037
-
1038
- // Otherwise, add an empty label.
1039
- } else {
1040
- labels.push_back (StringRef ());
1041
1039
}
1042
1040
1043
1041
// 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;
1051
1050
}
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
+
1052
1059
return Builder.createTupleType (elements, labels);
1053
1060
}
1054
1061
case NodeKind::TupleElement:
@@ -1074,12 +1081,13 @@ class TypeDecoder {
1074
1081
1075
1082
for (auto &element : *Node) {
1076
1083
// 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;
1083
1091
}
1084
1092
1085
1093
switch (Node->getKind ()) {
@@ -1256,9 +1264,9 @@ return {}; // Not Implemented!
1256
1264
/* forRequirement=*/ false );
1257
1265
if (substTy.isError ())
1258
1266
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 ());
1262
1270
++index;
1263
1271
}
1264
1272
}
@@ -1361,6 +1369,58 @@ return {}; // Not Implemented!
1361
1369
}
1362
1370
1363
1371
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
+
1364
1424
template <typename T>
1365
1425
bool decodeImplFunctionPart (Demangle::NodePointer node, unsigned depth,
1366
1426
llvm::SmallVectorImpl<T> &results) {
@@ -1524,12 +1584,12 @@ return {}; // Not Implemented!
1524
1584
return Builder.createProtocolDecl (node);
1525
1585
}
1526
1586
1527
- bool decodeMangledFunctionInputType (
1587
+ llvm::Optional<TypeLookupError> decodeMangledFunctionInputType (
1528
1588
Demangle::NodePointer node, unsigned depth,
1529
1589
llvm::SmallVectorImpl<FunctionParam<BuiltType>> ¶ms,
1530
1590
bool &hasParamFlags) {
1531
1591
if (depth > TypeDecoder::MaxDepth)
1532
- return false ;
1592
+ return llvm::None ;
1533
1593
1534
1594
// Look through a couple of sugar nodes.
1535
1595
if (node->getKind () == NodeKind::Type ||
@@ -1540,7 +1600,7 @@ return {}; // Not Implemented!
1540
1600
1541
1601
auto decodeParamTypeAndFlags =
1542
1602
[&](Demangle::NodePointer typeNode,
1543
- FunctionParam<BuiltType> ¶m) -> bool {
1603
+ FunctionParam<BuiltType> ¶m) -> llvm::Optional<TypeLookupError> {
1544
1604
Demangle::NodePointer node = typeNode;
1545
1605
1546
1606
bool recurse = true ;
@@ -1589,17 +1649,15 @@ return {}; // Not Implemented!
1589
1649
}
1590
1650
}
1591
1651
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
+ });
1599
1657
};
1600
1658
1601
1659
auto decodeParam =
1602
- [&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType> > {
1660
+ [&](NodePointer paramNode) -> llvm::Optional<TypeLookupError > {
1603
1661
if (paramNode->getKind () != NodeKind::TupleElement)
1604
1662
return None;
1605
1663
@@ -1615,40 +1673,41 @@ return {}; // Not Implemented!
1615
1673
hasParamFlags = true ;
1616
1674
break ;
1617
1675
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;
1621
1681
break ;
1682
+ }
1622
1683
1623
1684
default :
1624
- return None ;
1685
+ return TYPE_LOOKUP_ERROR_FMT ( " unknown node " ) ;
1625
1686
}
1626
1687
}
1627
1688
1628
- return param ;
1689
+ return None ;
1629
1690
};
1630
1691
1631
1692
// Expand a single level of tuple.
1632
1693
if (node->getKind () == NodeKind::Tuple) {
1633
1694
// Decode all the elements as separate arguments.
1634
1695
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;
1640
1699
}
1641
1700
1642
- return true ;
1701
+ return None ;
1643
1702
}
1644
1703
1645
1704
// Otherwise, handle the type as a single argument.
1646
1705
FunctionParam<BuiltType> param;
1647
- if (!decodeParamTypeAndFlags (node, param))
1648
- return false ;
1706
+ auto optError = decodeParamTypeAndFlags (node, param);
1707
+ if (optError)
1708
+ return *optError;
1649
1709
1650
- params.push_back (std::move (param));
1651
- return true ;
1710
+ return None;
1652
1711
}
1653
1712
};
1654
1713
0 commit comments