@@ -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)
@@ -1019,7 +1019,8 @@ class TypeDecoder {
1019
1019
1020
1020
case NodeKind::Tuple: {
1021
1021
llvm::SmallVector<BuiltType, 8 > elements;
1022
- std::string labels;
1022
+ llvm::SmallVector<StringRef, 8 > labels;
1023
+
1023
1024
for (auto &element : *Node) {
1024
1025
if (element->getKind () != NodeKind::TupleElement)
1025
1026
return MAKE_NODE_TYPE_ERROR0 (Node, " unexpected kind" );
@@ -1030,31 +1031,32 @@ class TypeDecoder {
1030
1031
return MAKE_NODE_TYPE_ERROR0 (element->getChild (typeChildIndex),
1031
1032
" no children" );
1032
1033
}
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 (), ' ' );
1037
1034
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 () ;
1041
1038
typeChildIndex++;
1042
-
1043
- // Otherwise, add a space if a previous element had a label.
1044
- } else if (!labels.empty ()) {
1045
- labels += ' ' ;
1046
1039
}
1047
1040
1048
1041
// 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;
1056
1050
}
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);
1058
1060
}
1059
1061
case NodeKind::TupleElement:
1060
1062
if (Node->getNumChildren () < 1 )
@@ -1079,12 +1081,13 @@ class TypeDecoder {
1079
1081
1080
1082
for (auto &element : *Node) {
1081
1083
// 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;
1088
1091
}
1089
1092
1090
1093
switch (Node->getKind ()) {
@@ -1100,16 +1103,8 @@ class TypeDecoder {
1100
1103
}
1101
1104
1102
1105
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" );
1113
1108
}
1114
1109
1115
1110
case NodeKind::DependentGenericType: {
@@ -1261,9 +1256,9 @@ return {}; // Not Implemented!
1261
1256
/* forRequirement=*/ false );
1262
1257
if (substTy.isError ())
1263
1258
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 ());
1267
1262
++index;
1268
1263
}
1269
1264
}
@@ -1366,6 +1361,58 @@ return {}; // Not Implemented!
1366
1361
}
1367
1362
1368
1363
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
+
1369
1416
template <typename T>
1370
1417
bool decodeImplFunctionPart (Demangle::NodePointer node, unsigned depth,
1371
1418
llvm::SmallVectorImpl<T> &results) {
@@ -1529,12 +1576,12 @@ return {}; // Not Implemented!
1529
1576
return Builder.createProtocolDecl (node);
1530
1577
}
1531
1578
1532
- bool decodeMangledFunctionInputType (
1579
+ llvm::Optional<TypeLookupError> decodeMangledFunctionInputType (
1533
1580
Demangle::NodePointer node, unsigned depth,
1534
1581
llvm::SmallVectorImpl<FunctionParam<BuiltType>> ¶ms,
1535
1582
bool &hasParamFlags) {
1536
1583
if (depth > TypeDecoder::MaxDepth)
1537
- return false ;
1584
+ return llvm::None ;
1538
1585
1539
1586
// Look through a couple of sugar nodes.
1540
1587
if (node->getKind () == NodeKind::Type ||
@@ -1545,7 +1592,7 @@ return {}; // Not Implemented!
1545
1592
1546
1593
auto decodeParamTypeAndFlags =
1547
1594
[&](Demangle::NodePointer typeNode,
1548
- FunctionParam<BuiltType> ¶m) -> bool {
1595
+ FunctionParam<BuiltType> ¶m) -> llvm::Optional<TypeLookupError> {
1549
1596
Demangle::NodePointer node = typeNode;
1550
1597
1551
1598
bool recurse = true ;
@@ -1594,17 +1641,15 @@ return {}; // Not Implemented!
1594
1641
}
1595
1642
}
1596
1643
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
+ });
1604
1649
};
1605
1650
1606
1651
auto decodeParam =
1607
- [&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType> > {
1652
+ [&](NodePointer paramNode) -> llvm::Optional<TypeLookupError > {
1608
1653
if (paramNode->getKind () != NodeKind::TupleElement)
1609
1654
return None;
1610
1655
@@ -1620,40 +1665,41 @@ return {}; // Not Implemented!
1620
1665
hasParamFlags = true ;
1621
1666
break ;
1622
1667
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;
1626
1673
break ;
1674
+ }
1627
1675
1628
1676
default :
1629
- return None ;
1677
+ return TYPE_LOOKUP_ERROR_FMT ( " unknown node " ) ;
1630
1678
}
1631
1679
}
1632
1680
1633
- return param ;
1681
+ return None ;
1634
1682
};
1635
1683
1636
1684
// Expand a single level of tuple.
1637
1685
if (node->getKind () == NodeKind::Tuple) {
1638
1686
// Decode all the elements as separate arguments.
1639
1687
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;
1645
1691
}
1646
1692
1647
- return true ;
1693
+ return None ;
1648
1694
}
1649
1695
1650
1696
// Otherwise, handle the type as a single argument.
1651
1697
FunctionParam<BuiltType> param;
1652
- if (!decodeParamTypeAndFlags (node, param))
1653
- return false ;
1698
+ auto optError = decodeParamTypeAndFlags (node, param);
1699
+ if (optError)
1700
+ return *optError;
1654
1701
1655
- params.push_back (std::move (param));
1656
- return true ;
1702
+ return None;
1657
1703
}
1658
1704
};
1659
1705
0 commit comments