@@ -1047,3 +1047,63 @@ Some kinds need arguments, which precede ``Tf``.
1047
1047
If the first character of the string literal is a digit ``[0-9] `` or an
1048
1048
underscore ``_ ``, the identifier for the string literal is prefixed with an
1049
1049
additional underscore ``_ ``.
1050
+
1051
+ Conventions for foreign symbols
1052
+ -------------------------------
1053
+
1054
+ Swift interoperates with multiple other languages - C, C++, Objective-C, and
1055
+ Objective-C++. Each of these languages defines their own mangling conventions,
1056
+ so Swift must take care to follow them. However, these conventions do not cover
1057
+ Swift-specific symbols like Swift type metadata for foreign types, so Swift uses
1058
+ its own mangling scheme for those symbols.
1059
+
1060
+ Importing C and C++ structs
1061
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1062
+
1063
+ Types imported from C and C++ are imported as if they are located in the ``__C ``
1064
+ module, regardless of the actual Clang module that they are coming from. This
1065
+ can be observed when mangling a Swift function that accepts a C/C++ struct as a
1066
+ parameter:
1067
+
1068
+ C++ module ``CxxStructModule ``:
1069
+
1070
+ .. code-block :: c++
1071
+
1072
+ struct CxxStruct {};
1073
+
1074
+ inline void cxxFunction(CxxStruct s) {}
1075
+
1076
+ Swift module ``main `` that imports ``CxxStructModule ``:
1077
+
1078
+ .. code-block :: swift
1079
+
1080
+ import CxxStructModule
1081
+
1082
+ public func swiftFunction (_ s : CxxStruct) {}
1083
+
1084
+ Resulting symbols (showing only Itanium-mangled C++ symbols for brevity):
1085
+
1086
+ .. code ::
1087
+
1088
+ _Z11cxxFunction9CxxStruct // -> cxxFunction(CxxStruct)
1089
+ s4main13swiftFunctionyySo9CxxStructVF // -> main.swiftFunction(__C.CxxStruct) -> ()
1090
+
1091
+ The reason for ignoring the Clang module and always putting C and C++ types into
1092
+ ``__C `` at the Swift ABI level is that the Clang module is not a part of the C
1093
+ or C++ ABI. When owners of C and C++ Clang modules decide what changes are
1094
+ ABI-compatible or not, they will likely take into account C and C++ ABI, but not
1095
+ the Swift ABI. Therefore, Swift ABI can only encode information about a C or C++
1096
+ type that the C and C++ ABI already encodes in order to remain compatible with
1097
+ future versions of libraries that evolve according to C and C++ ABI
1098
+ compatibility principles.
1099
+
1100
+ The C/C++ compiler does not generate Swift metadata symbols and value witness
1101
+ tables for C and C++ types. To make a foreign type usable in Swift in the same
1102
+ way as a native type, the Swift compiler must generate these symbols.
1103
+ Specifically, each Swift module that uses a given C or C++ type generates the
1104
+ necessary Swift symbols. For the example above the Swift compiler will generate following
1105
+ nominal type descriptor symbol for ``CxxStruct `` while compiling the ``main `` module:
1106
+
1107
+ .. code ::
1108
+
1109
+ sSo9CxxStructVMn // -> nominal type descriptor for __C.CxxStruct
0 commit comments