12
12
13
13
#include " DeclAndTypePrinter.h"
14
14
#include " CxxSynthesis.h"
15
- #include " PrimitiveTypeMapping.h"
16
15
17
16
#include " swift/AST/ASTContext.h"
18
17
#include " swift/AST/ASTMangler.h"
@@ -1443,7 +1442,7 @@ class DeclAndTypePrinter::Implementation
1443
1442
// / Otherwise returns null.
1444
1443
const ClassDecl *getObjCBridgedClass (const NominalTypeDecl *nominal) {
1445
1444
// Print known types as their unbridged type.
1446
- if (owningPrinter. typeMapping . getKnownObjCTypeInfo (nominal))
1445
+ if (getKnownTypeInfo (nominal))
1447
1446
return nullptr ;
1448
1447
1449
1448
// Print imported bridgeable decls as their unbridged type.
@@ -1547,6 +1546,102 @@ class DeclAndTypePrinter::Implementation
1547
1546
return false ;
1548
1547
}
1549
1548
1549
+ // / If \p typeDecl is one of the standard library types used to map in Clang
1550
+ // / primitives and basic types, return the info in \c specialNames containing
1551
+ // / the Clang name and whether it can be nullable in C.
1552
+ Optional<CTypeInfo> getKnownTypeInfo (const TypeDecl *typeDecl) {
1553
+ auto &specialNames = owningPrinter.specialNames ;
1554
+ if (specialNames.empty ()) {
1555
+ ASTContext &ctx = getASTContext ();
1556
+ #define MAP (SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY ) \
1557
+ specialNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = \
1558
+ { CLANG_REPR, NEEDS_NULLABILITY }
1559
+
1560
+ MAP (CBool, " bool" , false );
1561
+
1562
+ MAP (CChar, " char" , false );
1563
+ MAP (CWideChar, " wchar_t" , false );
1564
+ MAP (CChar16, " char16_t" , false );
1565
+ MAP (CChar32, " char32_t" , false );
1566
+
1567
+ MAP (CSignedChar, " signed char" , false );
1568
+ MAP (CShort, " short" , false );
1569
+ MAP (CInt, " int" , false );
1570
+ MAP (CLong, " long" , false );
1571
+ MAP (CLongLong, " long long" , false );
1572
+
1573
+ MAP (CUnsignedChar, " unsigned char" , false );
1574
+ MAP (CUnsignedShort, " unsigned short" , false );
1575
+ MAP (CUnsignedInt, " unsigned int" , false );
1576
+ MAP (CUnsignedLong, " unsigned long" , false );
1577
+ MAP (CUnsignedLongLong, " unsigned long long" , false );
1578
+
1579
+ MAP (CFloat, " float" , false );
1580
+ MAP (CDouble, " double" , false );
1581
+
1582
+ MAP (Int8, " int8_t" , false );
1583
+ MAP (Int16, " int16_t" , false );
1584
+ MAP (Int32, " int32_t" , false );
1585
+ MAP (Int64, " int64_t" , false );
1586
+ MAP (UInt8, " uint8_t" , false );
1587
+ MAP (UInt16, " uint16_t" , false );
1588
+ MAP (UInt32, " uint32_t" , false );
1589
+ MAP (UInt64, " uint64_t" , false );
1590
+
1591
+ MAP (Float, " float" , false );
1592
+ MAP (Double, " double" , false );
1593
+ MAP (Float32, " float" , false );
1594
+ MAP (Float64, " double" , false );
1595
+
1596
+ MAP (Int, " NSInteger" , false );
1597
+ MAP (UInt, " NSUInteger" , false );
1598
+ MAP (Bool, " BOOL" , false );
1599
+
1600
+ MAP (OpaquePointer, " void *" , true );
1601
+ MAP (UnsafeRawPointer, " void const *" , true );
1602
+ MAP (UnsafeMutableRawPointer, " void *" , true );
1603
+
1604
+ Identifier ID_ObjectiveC = ctx.Id_ObjectiveC ;
1605
+ specialNames[{ID_ObjectiveC, ctx.getIdentifier (" ObjCBool" )}]
1606
+ = { " BOOL" , false };
1607
+ specialNames[{ID_ObjectiveC, ctx.getIdentifier (" Selector" )}]
1608
+ = { " SEL" , true };
1609
+ specialNames[{ID_ObjectiveC,
1610
+ ctx.getIdentifier (
1611
+ ctx.getSwiftName (KnownFoundationEntity::NSZone))}]
1612
+ = { " struct _NSZone *" , true };
1613
+
1614
+ specialNames[{ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" )}]
1615
+ = { " Boolean" , false };
1616
+
1617
+ specialNames[{ctx.Id_CoreGraphics , ctx.Id_CGFloat }]
1618
+ = { " CGFloat" , false };
1619
+
1620
+ specialNames[{ctx.Id_CoreFoundation , ctx.Id_CGFloat }]
1621
+ = { " CGFloat" , false };
1622
+
1623
+ // Use typedefs we set up for SIMD vector types.
1624
+ #define MAP_SIMD_TYPE (BASENAME, _, __ ) \
1625
+ specialNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" )}] \
1626
+ = { " swift_" #BASENAME " 2" , false }; \
1627
+ specialNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" )}] \
1628
+ = { " swift_" #BASENAME " 3" , false }; \
1629
+ specialNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" )}] \
1630
+ = { " swift_" #BASENAME " 4" , false };
1631
+ #include " swift/ClangImporter/SIMDMappedTypes.def"
1632
+ static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
1633
+ " must add or remove special name mappings if max number of "
1634
+ " SIMD elements is changed" );
1635
+ }
1636
+
1637
+ Identifier moduleName = typeDecl->getModuleContext ()->getName ();
1638
+ Identifier name = typeDecl->getName ();
1639
+ auto iter = specialNames.find ({moduleName, name});
1640
+ if (iter == specialNames.end ())
1641
+ return None;
1642
+ return iter->second ;
1643
+ }
1644
+
1550
1645
// / If \p typeDecl is one of the standard library types used to map in Clang
1551
1646
// / primitives and basic types, print out the appropriate spelling and
1552
1647
// / return true.
@@ -1555,8 +1650,7 @@ class DeclAndTypePrinter::Implementation
1555
1650
// / for interfacing with C and Objective-C.
1556
1651
bool printIfKnownSimpleType (const TypeDecl *typeDecl,
1557
1652
Optional<OptionalTypeKind> optionalKind) {
1558
- auto knownTypeInfo =
1559
- owningPrinter.typeMapping .getKnownObjCTypeInfo (typeDecl);
1653
+ Optional<CTypeInfo> knownTypeInfo = getKnownTypeInfo (typeDecl);
1560
1654
if (!knownTypeInfo)
1561
1655
return false ;
1562
1656
os << knownTypeInfo->name ;
0 commit comments