@@ -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)
@@ -1022,7 +1022,8 @@ class TypeDecoder {
1022
1022
1023
1023
case NodeKind::Tuple: {
1024
1024
llvm::SmallVector<BuiltType, 8 > elements;
1025
- std::string labels;
1025
+ llvm::SmallVector<StringRef, 8 > labels;
1026
+
1026
1027
for (auto &element : *Node) {
1027
1028
if (element->getKind () != NodeKind::TupleElement)
1028
1029
return MAKE_NODE_TYPE_ERROR0 (Node, " unexpected kind" );
@@ -1033,31 +1034,32 @@ class TypeDecoder {
1033
1034
return MAKE_NODE_TYPE_ERROR0 (element->getChild (typeChildIndex),
1034
1035
" no children" );
1035
1036
}
1036
- if (element->getChild (typeChildIndex)->getKind () == NodeKind::TupleElementName) {
1037
- // Add spaces to terminate all the previous labels if this
1038
- // is the first we've seen.
1039
- if (labels.empty ()) labels.append (elements.size (), ' ' );
1040
1037
1041
- // Add the label and its terminator.
1042
- labels += element->getChild (typeChildIndex)->getText ();
1043
- labels += ' ' ;
1038
+ StringRef label;
1039
+ if ( element->getChild (typeChildIndex)->getKind () == NodeKind::TupleElementName) {
1040
+ label = element-> getChild (typeChildIndex)-> getText () ;
1044
1041
typeChildIndex++;
1045
-
1046
- // Otherwise, add a space if a previous element had a label.
1047
- } else if (!labels.empty ()) {
1048
- labels += ' ' ;
1049
1042
}
1050
1043
1051
1044
// Decode the element type.
1052
- auto elementType =
1053
- decodeMangledType (element->getChild (typeChildIndex), depth + 1 ,
1054
- /* forRequirement=*/ false );
1055
- if (elementType.isError ())
1056
- return elementType;
1057
-
1058
- 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;
1059
1053
}
1060
- return Builder.createTupleType (elements, std::move (labels));
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
+
1062
+ return Builder.createTupleType (elements, labels);
1061
1063
}
1062
1064
case NodeKind::TupleElement:
1063
1065
if (Node->getNumChildren () < 1 )
@@ -1082,12 +1084,13 @@ class TypeDecoder {
1082
1084
1083
1085
for (auto &element : *Node) {
1084
1086
// Decode the element type.
1085
- auto elementType =
1086
- decodeMangledType (element, depth + 1 , /* forRequirement=*/ false );
1087
- if (elementType.isError ())
1088
- return elementType;
1089
-
1090
- 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;
1091
1094
}
1092
1095
1093
1096
switch (Node->getKind ()) {
@@ -1103,16 +1106,8 @@ class TypeDecoder {
1103
1106
}
1104
1107
1105
1108
case NodeKind::PackExpansion: {
1106
- if (Node->getNumChildren () < 2 )
1107
- return MAKE_NODE_TYPE_ERROR (Node,
1108
- " fewer children (%zu) than required (2)" ,
1109
- Node->getNumChildren ());
1110
-
1111
- auto patternType = decodeMangledType (Node->getChild (0 ), depth + 1 );
1112
- auto countType = decodeMangledType (Node->getChild (1 ), depth + 1 );
1113
-
1114
- return Builder.createPackExpansionType (patternType.getType (),
1115
- countType.getType ());
1109
+ return MAKE_NODE_TYPE_ERROR0 (Node,
1110
+ " pack expansion type in unsupported position" );
1116
1111
}
1117
1112
1118
1113
case NodeKind::DependentGenericType: {
@@ -1264,9 +1259,9 @@ return {}; // Not Implemented!
1264
1259
/* forRequirement=*/ false );
1265
1260
if (substTy.isError ())
1266
1261
return substTy;
1267
- substitutions. emplace_back (
1268
- Builder. createGenericTypeParameterType ( paramDepth, index),
1269
- substTy.getType ());
1262
+ auto paramTy = Builder. createGenericTypeParameterType (
1263
+ paramDepth, index);
1264
+ substitutions. emplace_back (paramTy, substTy.getType ());
1270
1265
++index;
1271
1266
}
1272
1267
}
@@ -1369,6 +1364,58 @@ return {}; // Not Implemented!
1369
1364
}
1370
1365
1371
1366
private:
1367
+ template <typename Fn>
1368
+ llvm::Optional<TypeLookupError>
1369
+ decodeTypeSequenceElement (Demangle::NodePointer node, unsigned depth,
1370
+ Fn resultCallback) {
1371
+ if (node->getKind () == NodeKind::Type)
1372
+ node = node->getChild (0 );
1373
+
1374
+ if (node->getKind () == NodeKind::PackExpansion) {
1375
+ if (node->getNumChildren () < 2 )
1376
+ return MAKE_NODE_TYPE_ERROR (node,
1377
+ " fewer children (%zu) than required (2)" ,
1378
+ node->getNumChildren ());
1379
+
1380
+ auto patternType = node->getChild (0 );
1381
+
1382
+ // Decode the shape pack first, to form a metadata pack.
1383
+ auto countType = decodeMangledType (node->getChild (1 ), depth);
1384
+ if (countType.isError ())
1385
+ return *countType.getError ();
1386
+
1387
+ // Push the pack expansion on the active expansion stack inside the
1388
+ // builder concept.
1389
+ size_t numElements = Builder.beginPackExpansion (countType.getType ());
1390
+
1391
+ for (size_t i = 0 ; i < numElements; ++i) {
1392
+ // Advance the lane index inside the builder concept.
1393
+ Builder.advancePackExpansion (i);
1394
+
1395
+ // Decode the pattern type, taking the ith element of each pack
1396
+ // referenced therein.
1397
+ auto expandedElementType = decodeMangledType (patternType, depth);
1398
+ if (expandedElementType.isError ())
1399
+ return *expandedElementType.getError ();
1400
+
1401
+ resultCallback (Builder.createExpandedPackElement (
1402
+ expandedElementType.getType ()));
1403
+ }
1404
+
1405
+ // Pop the active expansion stack inside the builder concept.
1406
+ Builder.endPackExpansion ();
1407
+ } else {
1408
+ auto elementType =
1409
+ decodeMangledType (node, depth, /* forRequirement=*/ false );
1410
+ if (elementType.isError ())
1411
+ return *elementType.getError ();
1412
+
1413
+ resultCallback (elementType.getType ());
1414
+ }
1415
+
1416
+ return llvm::None;
1417
+ }
1418
+
1372
1419
template <typename T>
1373
1420
bool decodeImplFunctionPart (Demangle::NodePointer node, unsigned depth,
1374
1421
llvm::SmallVectorImpl<T> &results) {
@@ -1532,12 +1579,12 @@ return {}; // Not Implemented!
1532
1579
return Builder.createProtocolDecl (node);
1533
1580
}
1534
1581
1535
- bool decodeMangledFunctionInputType (
1582
+ llvm::Optional<TypeLookupError> decodeMangledFunctionInputType (
1536
1583
Demangle::NodePointer node, unsigned depth,
1537
1584
llvm::SmallVectorImpl<FunctionParam<BuiltType>> ¶ms,
1538
1585
bool &hasParamFlags) {
1539
1586
if (depth > TypeDecoder::MaxDepth)
1540
- return false ;
1587
+ return llvm::None ;
1541
1588
1542
1589
// Look through a couple of sugar nodes.
1543
1590
if (node->getKind () == NodeKind::Type ||
@@ -1548,7 +1595,7 @@ return {}; // Not Implemented!
1548
1595
1549
1596
auto decodeParamTypeAndFlags =
1550
1597
[&](Demangle::NodePointer typeNode,
1551
- FunctionParam<BuiltType> ¶m) -> bool {
1598
+ FunctionParam<BuiltType> ¶m) -> llvm::Optional<TypeLookupError> {
1552
1599
Demangle::NodePointer node = typeNode;
1553
1600
1554
1601
bool recurse = true ;
@@ -1597,17 +1644,15 @@ return {}; // Not Implemented!
1597
1644
}
1598
1645
}
1599
1646
1600
- auto paramType = decodeMangledType (node, depth + 1 ,
1601
- /* forRequirement=*/ false );
1602
- if (paramType.isError ())
1603
- return false ;
1604
-
1605
- param.setType (paramType.getType ());
1606
- return true ;
1647
+ return decodeTypeSequenceElement (node, depth + 1 ,
1648
+ [&](BuiltType paramType) {
1649
+ param.setType (paramType);
1650
+ params.push_back (param);
1651
+ });
1607
1652
};
1608
1653
1609
1654
auto decodeParam =
1610
- [&](NodePointer paramNode) -> llvm::Optional<FunctionParam<BuiltType> > {
1655
+ [&](NodePointer paramNode) -> llvm::Optional<TypeLookupError > {
1611
1656
if (paramNode->getKind () != NodeKind::TupleElement)
1612
1657
return llvm::None;
1613
1658
@@ -1623,40 +1668,41 @@ return {}; // Not Implemented!
1623
1668
hasParamFlags = true ;
1624
1669
break ;
1625
1670
1626
- case NodeKind::Type:
1627
- if (!decodeParamTypeAndFlags (child->getFirstChild (), param))
1628
- return llvm::None;
1671
+ case NodeKind::Type: {
1672
+ auto optError = decodeParamTypeAndFlags (
1673
+ child->getFirstChild (), param);
1674
+ if (optError)
1675
+ return optError;
1629
1676
break ;
1677
+ }
1630
1678
1631
1679
default :
1632
- return llvm::None ;
1680
+ return TYPE_LOOKUP_ERROR_FMT ( " unknown node " ) ;
1633
1681
}
1634
1682
}
1635
1683
1636
- return param ;
1684
+ return llvm::None ;
1637
1685
};
1638
1686
1639
1687
// Expand a single level of tuple.
1640
1688
if (node->getKind () == NodeKind::Tuple) {
1641
1689
// Decode all the elements as separate arguments.
1642
1690
for (const auto &elt : *node) {
1643
- auto param = decodeParam (elt);
1644
- if (!param)
1645
- return false ;
1646
-
1647
- params.push_back (std::move (*param));
1691
+ auto optError = decodeParam (elt);
1692
+ if (optError)
1693
+ return *optError;
1648
1694
}
1649
1695
1650
- return true ;
1696
+ return llvm::None ;
1651
1697
}
1652
1698
1653
1699
// Otherwise, handle the type as a single argument.
1654
1700
FunctionParam<BuiltType> param;
1655
- if (!decodeParamTypeAndFlags (node, param))
1656
- return false ;
1701
+ auto optError = decodeParamTypeAndFlags (node, param);
1702
+ if (optError)
1703
+ return *optError;
1657
1704
1658
- params.push_back (std::move (param));
1659
- return true ;
1705
+ return llvm::None;
1660
1706
}
1661
1707
};
1662
1708
0 commit comments