Skip to content

Commit a9648a6

Browse files
authored
---
yaml --- r: 326205 b: refs/heads/master-next c: 4804444 h: refs/heads/master i: 326203: 3cc993d
1 parent e82fb05 commit a9648a6

29 files changed

+1736
-101
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
refs/heads/master: e052da7d8886fa0439677852e8f7830b20c2e1da
3-
refs/heads/master-next: f919b2ddd866dbf90a38612a4ba711253e85d280
3+
refs/heads/master-next: 4804444c145de553dfd23eb5a7a1b85be7181663
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea
66
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-b: 66d897bfcf64a82cb9a87f5e663d889189d06d07

branches/master-next/docs/CToSwiftNameTranslation.md

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Name Translation from C to Swift
22

3-
This document gives a high-level description of how C and Objective-C declarations are translated to Swift, with particular focus on how names are adjusted. It is not attempting to be a *complete* description of everything the compiler does except with regards to how *names* are transformed; even there, some special cases that only apply to Apple's SDKs have been omitted.
3+
This document gives a high-level description of how C and Objective-C declarations are translated to Swift, with particular focus on how names are adjusted. It is not attempting to be a *complete* description of everything the compiler does except with regards to how *names* are transformed; even there, some special cases that only apply to Apple's SDKs have been omitted. The example code shown is for illustrative purposes and does not always include all parts of an imported API's interface.
44

55
## Word boundaries
66

@@ -239,4 +239,208 @@ _The original intent of the `swift_private` attribute was additionally to limit
239239
_For "historical reasons", the `swift_private` attribute is ignored on factory methods with no arguments imported as initializers. This is essentially matching the behavior of older Swift compilers for source compatibility in case someone has marked such a factory method as `swift_private`._
240240

241241

242+
## Custom names
243+
244+
The `swift_name` Clang attribute can be used to control how a declaration imports into Swift. If it's valid, the value of the `swift_name` attribute always overrides any other name transformation rules (prefix-stripping, underscore-prepending, etc.)
245+
246+
### Types and globals
247+
248+
The `swift_name` attribute can be used to give a type or a global a custom name. In the simplest form, the value of the attribute must be a valid Swift identifier.
249+
250+
```objc
251+
__attribute__((swift_name("SpacecraftCoordinates")))
252+
struct SPKSpacecraftCoordinates {
253+
double x, y, z, t; // space and time, of course
254+
};
255+
```
256+
257+
```swift
258+
struct SpacecraftCoordinates {
259+
var x, y, z, t: Double
260+
}
261+
```
262+
263+
### Import-as-member
264+
265+
A custom name that starts with an identifier followed by a period is taken to be a member name. The identifier should be the imported Swift name of a C/Objective-C type in the same module. In this case, the type or global will be imported as a static member of the named type.
266+
267+
```objc
268+
__attribute__((swift_name("SpacecraftCoordinates.earth")))
269+
extern const struct SPKSpacecraftCoordinates SPKSpacecraftCoordinatesEarth;
270+
```
271+
272+
```swift
273+
extension SpacecraftCoordinates {
274+
static var earth: SpacecraftCoordinates { get }
275+
}
276+
```
277+
278+
Note that types cannot be imported as members of protocols.
279+
280+
_The "in the same module" restriction is considered a technical limitation; a forward declaration of the base type will work around it._
281+
282+
283+
### C functions with custom names
284+
285+
The `swift_name` attribute can be used to give a C function a custom name. The value of the attribute must be a full Swift function name, including parameter labels.
286+
287+
```objc
288+
__attribute__((swift_name("doSomething(to:bar:)")))
289+
void doSomethingToFoo(Foo *foo, int bar);
290+
291+
// Usually seen as NS_SWIFT_NAME.
292+
```
293+
294+
```swift
295+
func doSomething(foo: UnsafeMutablePointer<Foo>, bar: Int32)
296+
```
297+
298+
An underscore can be used in place of an empty parameter label, as in Swift.
299+
300+
A C function with zero arguments and a non-`void` return type can also be imported as a computed variable by using the `getter:` prefix on the name. A function with one argument and a `void` return type can optionally serve as the setter for that variable using `setter:`.
301+
302+
```objc
303+
__attribute__((swift_name("getter:globalCounter()")))
304+
int getGlobalCounter(void);
305+
__attribute__((swift_name("setter:globalCounter(_:)")))
306+
void setGlobalCounter(int newValue);
307+
```
308+
309+
```swift
310+
var globalCounter: Int32 { get set }
311+
```
312+
313+
Note that the argument lists must still be present even though the name used is the name of the variable. (Also note the `void` parameter list to specify a C function with zero arguments. This is required!)
314+
315+
Variables with setters and no getters are not supported.
316+
317+
318+
#### Import-as-member
319+
320+
Like types and globals, functions can be imported as static members of types.
321+
322+
```objc
323+
__attribute__((swift_name("NSSound.beep()")))
324+
void NSBeep(void);
325+
```
326+
327+
```swift
328+
extension NSSound {
329+
static func beep()
330+
}
331+
```
332+
333+
However, by giving a parameter the label `self`, a function can also be imported as an instance member of a type __T__. In this case, the parameter labeled `self` must either have the type __T__ itself, or be a pointer to __T__. The latter is only valid if __T__ is imported as a value type; if the pointer is non-`const`, the resulting method will be `mutating`. If __T__ is a class, the function will be `final`.
334+
335+
```objc
336+
typedef struct {
337+
int value;
338+
} Counter;
339+
340+
__attribute__((swift_name("Counter.printValue(self:)")))
341+
void CounterPrintValue(Counter c);
342+
__attribute__((swift_name("Counter.printValue2(self:)")))
343+
void CounterPrintValue2(const Counter *c);
344+
__attribute__((swift_name("Counter.resetValue(self:)")))
345+
void CounterResetValue(Counter *c);
346+
```
347+
348+
```swift
349+
struct Counter {
350+
var value: Int32 { get set }
351+
}
352+
353+
extension Counter {
354+
func printValue()
355+
func printValue2()
356+
mutating func resetValue()
357+
}
358+
```
359+
360+
This also applies to getters and setters, to be imported as instance properties.
361+
362+
```objc
363+
__attribute__((swift_name("getter:Counter.absoluteValue(self:)")))
364+
int CounterGetAbsoluteValue(Counter c);
365+
```
366+
367+
```swift
368+
extension Counter {
369+
var absoluteValue: Int32 { get }
370+
}
371+
```
372+
373+
The getter/setter syntax also allows for subscripts by using the base name `subscript`.
374+
375+
```objc
376+
__attribute__((swift_name("getter:LinkedListOfInts.subscript(self:_:)")))
377+
int LinkedListGetAtIndex(const LinkedListOfInts *head, int index);
378+
```
379+
380+
```swift
381+
extension LinkedListOfInts {
382+
subscript(_ index: Int32) -> Int32 { get }
383+
}
384+
```
385+
386+
Finally, functions can be imported as initializers as well by using the base name `init`. These are considered "factory" initializers and are never inherited or overridable. They must not have a `self` parameter.
387+
388+
```objc
389+
__attribute__((swift_name("Counter.init(initialValue:)")))
390+
Counter CounterCreateWithInitialValue(int value);
391+
```
392+
393+
```swift
394+
extension Counter {
395+
/* non-inherited */ init(initialValue value: Int32)
396+
}
397+
```
398+
399+
400+
### Enumerators (enum cases)
401+
402+
The `swift_name` attribute can be used to rename enumerators. As mentioned above, not only does no further prefix-stripping occur on the resulting name, but the presence of a custom name removes the enum case from the computation of a prefix for the other cases.
403+
404+
```
405+
// Actual example from Apple's SDKs; in fact, the first shipping example of
406+
// swift_name on an enumerator at all!
407+
typedef NS_ENUM(NSUInteger, NSXMLNodeKind) {
408+
NSXMLInvalidKind = 0,
409+
NSXMLDocumentKind,
410+
NSXMLElementKind,
411+
NSXMLAttributeKind,
412+
NSXMLNamespaceKind,
413+
NSXMLProcessingInstructionKind,
414+
NSXMLCommentKind,
415+
NSXMLTextKind,
416+
NSXMLDTDKind NS_SWIFT_NAME(DTDKind),
417+
NSXMLEntityDeclarationKind,
418+
NSXMLAttributeDeclarationKind,
419+
NSXMLElementDeclarationKind,
420+
NSXMLNotationDeclarationKind
421+
};
422+
```
423+
424+
```
425+
public enum Kind : UInt {
426+
case invalid
427+
case document
428+
case element
429+
case attribute
430+
case namespace
431+
case processingInstruction
432+
case comment
433+
case text
434+
case DTDKind
435+
case entityDeclaration
436+
case attributeDeclaration
437+
case elementDeclaration
438+
case notationDeclaration
439+
}
440+
```
441+
442+
Although enumerators always have global scope in C, they are often imported as members in Swift, and thus the `swift_name` attribute cannot be used to import them as members of another type unless the enum type is anonymous.
443+
444+
_Currently, `swift_name` does not even allow importing an enum case as a member of the enum type itself, even if the enum is not recognized as an `@objc` enum, error code enum, or option set (i.e. the situation where a case is imported as a global constant)._
445+
242446
## More to come...

branches/master-next/include/swift/AST/Builtins.def

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,98 @@ BUILTIN_CAST_OR_BITCAST_OPERATION(SExtOrBitCast, "sextOrBitCast", "n")
5151
#undef BUILTIN_CAST_OR_BITCAST_OPERATION
5252

5353
/// Binary operations have type (T,T) -> T.
54+
///
55+
/// We define two different sorts of operations varying when T is static,
56+
/// specifically:
57+
///
58+
/// 1. Overloaded statically typed operations. E.x:
59+
///
60+
/// builtin "add_Vec4xInt32"(Vec4xInt32, Vec4xInt32) : Vec4xInt32.
61+
///
62+
/// 2. Polymorphic typed operations that are valid only in raw SIL. By the time
63+
/// diagnostic constant propagation runs, these must have as its operand a
64+
/// fully specialized type. If the builtin has a type that is not one of its
65+
/// overloaded types, diagnostic constant propagation will emit a diagnostic
66+
/// saying the builtin's type has not been fully resolved. Otherwise,
67+
/// diagnostic constant propagation will transform the builtin to the
68+
/// relevant static overloaded builtin form. E.x.:
69+
///
70+
/// builtin "add"(Self, Self) : Self // *error*
71+
///
72+
/// OR
73+
///
74+
/// builtin "generic_add"(Vec4xInt32, Vec4xInt32) : Vec4xInt32
75+
/// ->
76+
/// builtin "add_Vec4xInt32"(Vec4xInt32, Vec4xInt32) : Vec4xInt32
77+
///
78+
/// NOTE: If a polymorphic typed operation is not static by the time guaranteed
79+
/// constant propagation runs, we emit a diagnostic to inform the user (who is
80+
/// assumed to be an expert user) to tell them the value was unspecialized. The
81+
/// typical way this specialization occurs today is via transparent inlining
82+
/// since the transparent inliner devirtualizes and specializes as it goes. Of
83+
/// course this means mandatory inlining must /always/ occur before diagnostic
84+
/// constant propagation.
85+
///
86+
/// NOTE: Often times the builtin infrastructure wants to treat all
87+
/// binary operation builtins generic or not the same way. To ensure
88+
/// we support all use cases in the compiler, we do not declare the
89+
/// operations as part of this builtin since often times this macro is
90+
/// used to generic code. Instead, we stamp this out using the
91+
/// overloaded_static, polymorphic, and all suffixed operations.
5492
#ifndef BUILTIN_BINARY_OPERATION
55-
#define BUILTIN_BINARY_OPERATION(Id, Name, Attrs, Overload) \
56-
BUILTIN(Id, Name, Attrs)
93+
#define BUILTIN_BINARY_OPERATION(Id, Name, Attrs) BUILTIN(Id, Name, Attrs)
5794
#endif
58-
BUILTIN_BINARY_OPERATION(Add, "add", "n", IntegerOrVector)
59-
BUILTIN_BINARY_OPERATION(FAdd, "fadd", "n", FloatOrVector)
60-
BUILTIN_BINARY_OPERATION(And, "and", "n", IntegerOrVector)
61-
BUILTIN_BINARY_OPERATION(AShr, "ashr", "n", IntegerOrVector)
62-
BUILTIN_BINARY_OPERATION(LShr, "lshr", "n", IntegerOrVector)
63-
BUILTIN_BINARY_OPERATION(Or, "or", "n", IntegerOrVector)
64-
BUILTIN_BINARY_OPERATION(FDiv, "fdiv", "n", FloatOrVector)
65-
BUILTIN_BINARY_OPERATION(Mul, "mul", "n", IntegerOrVector)
66-
BUILTIN_BINARY_OPERATION(FMul, "fmul", "n", FloatOrVector)
67-
BUILTIN_BINARY_OPERATION(SDiv, "sdiv", "n", IntegerOrVector)
68-
BUILTIN_BINARY_OPERATION(ExactSDiv, "sdiv_exact", "n", IntegerOrVector)
69-
BUILTIN_BINARY_OPERATION(Shl, "shl", "n", IntegerOrVector)
70-
BUILTIN_BINARY_OPERATION(SRem, "srem", "n", IntegerOrVector)
71-
BUILTIN_BINARY_OPERATION(Sub, "sub", "n", IntegerOrVector)
72-
BUILTIN_BINARY_OPERATION(FSub, "fsub", "n", FloatOrVector)
73-
BUILTIN_BINARY_OPERATION(UDiv, "udiv", "n", IntegerOrVector)
74-
BUILTIN_BINARY_OPERATION(ExactUDiv, "udiv_exact", "n", IntegerOrVector)
75-
BUILTIN_BINARY_OPERATION(URem, "urem", "n", Integer)
76-
BUILTIN_BINARY_OPERATION(FRem, "frem", "n", FloatOrVector)
77-
BUILTIN_BINARY_OPERATION(Xor, "xor", "n", IntegerOrVector)
78-
// This builtin is an optimizer hint and always returns the first argument.
79-
BUILTIN_BINARY_OPERATION(Expect, "int_expect", "n", Integer)
95+
96+
#ifdef BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR
97+
#error "Do not define BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR before including this .def file"
98+
#endif
99+
100+
#define BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR(NAME) #NAME
101+
102+
#ifndef BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC
103+
#define BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC(Id, Name, Attrs, Overload) \
104+
BUILTIN_BINARY_OPERATION(Id, Name, Attrs)
105+
#endif
106+
107+
#ifndef BUILTIN_BINARY_OPERATION_POLYMORPHIC
108+
#define BUILTIN_BINARY_OPERATION_POLYMORPHIC(Id, Name, Attrs) \
109+
BUILTIN_BINARY_OPERATION(Id, Name, Attrs)
110+
#endif
111+
112+
// TODO: This needs a better name. We stringify generic_ in *_{OVERLOADED_STATIC,POLYMORPHIC}
113+
#ifndef BUILTIN_BINARY_OPERATION_ALL
114+
#define BUILTIN_BINARY_OPERATION_ALL(Id, Name, Attrs, Overload) \
115+
BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC(Id, BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR(Name), Attrs, Overload) \
116+
BUILTIN_BINARY_OPERATION_POLYMORPHIC(Generic##Id, BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR(generic_##Name), Attrs)
117+
#endif
118+
119+
// NOTE: Here we need our name field to be bare. We stringify them as
120+
// appropriately in BUILTIN_BINARY_OPERATION_{OVERLOADED_STATIC,POLYMORPHIC}.
121+
BUILTIN_BINARY_OPERATION_ALL(Add, add, "n", IntegerOrVector)
122+
BUILTIN_BINARY_OPERATION_ALL(FAdd, fadd, "n", FloatOrVector)
123+
BUILTIN_BINARY_OPERATION_ALL(And, and, "n", IntegerOrVector)
124+
BUILTIN_BINARY_OPERATION_ALL(AShr, ashr, "n", IntegerOrVector)
125+
BUILTIN_BINARY_OPERATION_ALL(LShr, lshr, "n", IntegerOrVector)
126+
BUILTIN_BINARY_OPERATION_ALL(Or, or, "n", IntegerOrVector)
127+
BUILTIN_BINARY_OPERATION_ALL(FDiv, fdiv, "n", FloatOrVector)
128+
BUILTIN_BINARY_OPERATION_ALL(Mul, mul, "n", IntegerOrVector)
129+
BUILTIN_BINARY_OPERATION_ALL(FMul, fmul, "n", FloatOrVector)
130+
BUILTIN_BINARY_OPERATION_ALL(SDiv, sdiv, "n", IntegerOrVector)
131+
BUILTIN_BINARY_OPERATION_ALL(ExactSDiv, sdiv_exact, "n", IntegerOrVector)
132+
BUILTIN_BINARY_OPERATION_ALL(Shl, shl, "n", IntegerOrVector)
133+
BUILTIN_BINARY_OPERATION_ALL(SRem, srem, "n", IntegerOrVector)
134+
BUILTIN_BINARY_OPERATION_ALL(Sub, sub, "n", IntegerOrVector)
135+
BUILTIN_BINARY_OPERATION_ALL(FSub, fsub, "n", FloatOrVector)
136+
BUILTIN_BINARY_OPERATION_ALL(UDiv, udiv, "n", IntegerOrVector)
137+
BUILTIN_BINARY_OPERATION_ALL(ExactUDiv, udiv_exact, "n", IntegerOrVector)
138+
BUILTIN_BINARY_OPERATION_ALL(URem, urem, "n", Integer)
139+
BUILTIN_BINARY_OPERATION_ALL(FRem, frem, "n", FloatOrVector)
140+
BUILTIN_BINARY_OPERATION_ALL(Xor, xor, "n", IntegerOrVector)
141+
BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC(Expect, "int_expect", "n", Integer)
142+
#undef BUILTIN_BINARY_OPERATION_ALL
143+
#undef BUILTIN_BINARY_OPERATION_POLYMORPHIC
144+
#undef BUILTIN_BINARY_OPERATION_OVERLOADED_STATIC
145+
#undef BUILTIN_BINARY_OPERATION_GENERIC_HELPER_STR
80146
#undef BUILTIN_BINARY_OPERATION
81147

82148
/// These builtins are analogous the similarly named llvm intrinsics. The

branches/master-next/include/swift/AST/Builtins.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ enum class BuiltinValueKind {
8383
#include "swift/AST/Builtins.def"
8484
};
8585

86+
/// Returns true if this is a polymorphic builtin that is only valid
87+
/// in raw sil and thus must be resolved to have concrete types by the
88+
/// time we are in canonical SIL.
89+
bool isPolymorphicBuiltin(BuiltinValueKind Id);
90+
8691
/// Decode the type list of a builtin (e.g. mul_Int32) and return the base
8792
/// name (e.g. "mul").
8893
StringRef getBuiltinBaseName(ASTContext &C, StringRef Name,

branches/master-next/include/swift/SIL/InstructionUtils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,38 @@ bool onlyUsedByAssignByWrapper(PartialApplyInst *PAI);
142142
void findClosuresForFunctionValue(SILValue V,
143143
TinyPtrVector<PartialApplyInst *> &results);
144144

145+
/// Given a polymorphic builtin \p bi that may be generic and thus have in/out
146+
/// params, stash all of the information needed for either specializing while
147+
/// inlining or propagating the type in constant propagation.
148+
///
149+
/// NOTE: If we perform this transformation, our builtin will no longer have any
150+
/// substitutions since we only substitute to concrete static overloads.
151+
struct PolymorphicBuiltinSpecializedOverloadInfo {
152+
Identifier staticOverloadIdentifier;
153+
SmallVector<SILType, 8> argTypes;
154+
SILType resultType;
155+
bool hasOutParam = false;
156+
157+
#ifndef NDEBUG
158+
private:
159+
bool isInitialized = false;
160+
#endif
161+
162+
public:
163+
PolymorphicBuiltinSpecializedOverloadInfo() = default;
164+
165+
bool init(SILFunction *fn, BuiltinValueKind builtinKind,
166+
ArrayRef<SILType> oldOperandTypes, SILType oldResultType);
167+
168+
bool init(BuiltinInst *bi);
169+
};
170+
171+
/// Given a polymorphic builtin \p bi, analyze its types and create a builtin
172+
/// for the static overload that the builtin corresponds to. If \p bi is not a
173+
/// polymorphic builtin or does not have any available overload for these types,
174+
/// return SILValue().
175+
SILValue getStaticOverloadForSpecializedPolymorphicBuiltin(BuiltinInst *bi);
176+
145177
} // end namespace swift
146178

147179
#endif

0 commit comments

Comments
 (0)