Commit Graph

425 Commits

Author SHA1 Message Date
Jordan Rose
9e61c4bb62 [ClangImporter] Import NULL, nil, Nil, and ((void*)0) as Swift.nil.
Also, look through one layer of parentheses in general for macros, rather
than special-casing it for each token count.

<rdar://problem/16198517>

Swift SVN r17460
2014-05-05 17:37:00 +00:00
Jordan Rose
2b1a092c27 [ClangImporter] Prefer typedef sugar for blocks even in bridge-able contexts.
We'd rather show dispatch_async as

  // new
  func dispatch_async(queue: dispatch_queue_t!, block: dispatch_block_t!)

than

  // old
  func dispatch_async(queue: dispatch_queue_t!, block: (() -> Void)!)

which was only happening because we were trying to hide the @objc_block
annotation when possible. That's not necessary when we have a typealias.

<rdar://problem/16679280>

Swift SVN r17273
2014-05-02 21:36:59 +00:00
Jordan Rose
16f96f1139 [PrintAsObjC] Guard @import lines with __has_feature(modules).
Also, use "#include <objc/NSObject.h>" instead of "@import ObjectiveC;"
to get access to BOOL, SEL, NSObject, and NSString.

This allows generated headers to be used in Objective-C++ contexts, where
modules don't yet work. The dependencies will unfortunately need to be
imported separately (because there's not a direct mapping from module name
back to header file), but that's still better than just being incompatible.

<rdar://problem/16796627>

Swift SVN r17272
2014-05-02 21:36:55 +00:00
Jordan Rose
3b43187703 [ClangImporter] Strip 'k' prefix even when there is nothing else in common.
<rdar://problem/16768954>

Swift SVN r17126
2014-05-01 00:06:23 +00:00
Jordan Rose
8e44c2ee79 Break "NSUTF16" as "NSUTF, 16", not "NSUT, F16".
Previously, this declaration:

  typedef NS_OPTIONS(NSUInteger, NSABitmapFormat5) {
    NSAA16d,
    NSAB32d,
  };

...would import with members .A16d and .B32d, which is not necessarily
correct. (Is it "NS_AA_16d", or "NSA_A_16d"?) Be more conservative here.

Swift SVN r17125
2014-05-01 00:06:22 +00:00
Doug Gregor
00e4edb620 Test that @optional initializers are imported as required.
... because Swift doesn't have optional initializer requirements yet.

Swift SVN r16851
2014-04-25 23:39:05 +00:00
Jordan Rose
76c89fe1e2 [ClangImporter] Avoid Clang's isModuleVisible.
Swift sometimes has a different notion of visibility, even for Clang modules.
In particular, this was causing the C version of NSUTF8StringEncoding
(from Foundation) to show up in AppKit, bypassing the Swift version in the
overlay.

We still want to search for redeclarations of functions, typedefs, and globals,
since these are often repeated in multiple modules without harm. The decl will
still have a "home" module, but this should allow them to be found by lookup.

<rdar://problem/16396994> and possibly <rdar://problem/14665250>

Swift SVN r16762
2014-04-24 18:50:01 +00:00
Jordan Rose
7542b996d0 [ClangImporter] Fix NSDictionary NSCopying hack for non-nullable object refs.
<rdar://problem/16533816>

Swift SVN r16720
2014-04-23 21:37:34 +00:00
Joe Groff
4dc7628cfd ClangImporter: Drop the variadic parameter to UIKit variadic DIs.
Hack the clang importer to drop the 'otherButtonTitles:' argument when we see the UIActionSheet or UIAlertView init methods, so we'll end up importing them as the non-variadic initializers added by the overlay.

Swift SVN r16717
2014-04-23 21:28:08 +00:00
Jordan Rose
98cc7524bd [ClangImporter] Rewrite enum prefix stripping to use the camel_case helpers.
Previously, we didn't correctly handle acronyms as single words, leading to
importing the NS16BitLittleEndianBitmapFormat enumerator of NSBitmapFormat as
"S16BitLittleEndianBitmapFormat". Now we get "NS16BitLittleEndianBitmapFormat",
which is a little better. See the examples for a few more ways this shakes out.

<rdar://problem/16683848>

Swift SVN r16682
2014-04-23 00:32:39 +00:00
Joe Pamer
7245e40825 The type checker collects all overloads for a given function application during the pre-type check phase, but equality conformances (and their associated '==' overloads) may be added to a type lazily during type resolution. This can lead to inconsistent behavior during type checking equality comparisons on imported enumeration types. (rdar://problem/16526249)
Basically, if an imported enumeration case is referenced without qualification as an argument to a '==' application (E.g., "foo.bar == .Baz"), and the enumeration type had not previously been resolved, overloads to '==' will be added to the global scope while performing overload resolution. This means the overloads will be ignored while solving for that application, but will be available for subsequent applications. (So you'll get an "expression does not type check" error the first time around, but not for subsequent applications of '==' to that enumeration type.)

The Equatable protocol is rather lightweight, however, and adding it to imported types directly results in no meaningful overhead to type check performance; we should just add it outright. As things evolve, though, it'll be worth considering how to make the type checker more amenable to lazy declarations.

Swift SVN r16557
2014-04-19 00:12:08 +00:00
Doug Gregor
c56d50a12c Introduce a named argument for imported nullary factory methods with long names.
We started tracking statistics for these in r16529, but because we've
"always" done this for initializers, it's trivial to just implement
the splitting. As with initializers, we synthesize a named argument of
type '()' to capture the part of the first selector piece that follows
the class name.


Swift SVN r16530
2014-04-18 16:41:18 +00:00
Doug Gregor
812dc091eb Introduce the notion of factory initializers.
Factory initializers express an initializer that produces an object of
the given type, but is not inherited and not designated. Although they
have a syntactic form for presentation purposes (-> ClassName), there
is no way to specify or implement them within Swift. Rather, factory
initializers are created when importing an Objective-C factory method
that returns the class type rather than instancetype.

Swift SVN r16528
2014-04-18 16:04:48 +00:00
Doug Gregor
f700a114f4 Handle inheritance of factory methods imported as initializers.
Swift SVN r16500
2014-04-18 06:14:27 +00:00
Doug Gregor
f56c68386e Start importing factory methods as initializers.
When an Objective-C class method follows the naming convention of a
factory method, i.e., its starting words match the ending words of the
class name, import it as a convenience initializer when it also:
  - Returns instancetype (i.e., dynamic Self in Swift parlance)
  - Has no NSError** parameters, which indicate the potential for failures

This is under a new flag (-enable-objc-factory-method-constructors)
because it is not generally functional. However, this is a step toward
<rdar://problem/16509024>.

Swift SVN r16479
2014-04-17 23:34:00 +00:00
Ted Kremenek
6caa1d4ae8 Make 'NSInvocation' unavailable.
Implements <rdar://problem/16638093>.

Swift SVN r16441
2014-04-17 04:49:28 +00:00
Ted Kremenek
fd864384df Imported '_attribute__((deprecated)' as 'unavailable'.
Swift SVN r16402
2014-04-16 06:52:10 +00:00
Doug Gregor
54e12fb13b Clang importer: use the selector-based lookup table to avoid importing
methods with conflicting selectors.

We were doing this in a very ad hoc manner before; centralizing the
lookup table should make this significantly more robust. There's also
some scaffolding here to handle initializers better.

Fixes <rdar://problem/16516638>.

Swift SVN r16349
2014-04-15 00:35:41 +00:00
Doug Gregor
6e5ca8a91f Clean up a number of Cocoa selector -> method name mappings.
Swift SVN r16302
2014-04-14 05:15:10 +00:00
Doug Gregor
9d7f0b6211 Rework the selector-splitting heuristics.
This makes a number of changes to the selector-splitting
heuristics. Specifically:

  - Eliminate last-word splitting, and with it the notion of
    multi-words. We only split at prepositions now.
  - Introduce the notion of "linking verbs" such as "will" or
    "should"; when these show up, we refuse to split a selector, which
    helps with delegates.
  - Eliminate the special case for "get" and "set". It wasn't
    helping.
  




Swift SVN r16265
2014-04-12 20:32:16 +00:00
Ted Kremenek
656fd1a2ee Add blacklist for imported macros, which can be extended over time.
Implements:

<rdar://problem/16454306> NS_BLOCKS_AVAILABLE probably should not be imported since by default it's true

Swift SVN r16067
2014-04-08 19:26:17 +00:00
Ted Kremenek
79bddbc66f Import -animator (from NSAnimatablePropertyContainer) as returning 'id' instead of 'instancetype'.
Fixes <rdar://problem/16020273>.

Swift SVN r16050
2014-04-08 08:27:51 +00:00
Jordan Rose
684e4d7b80 [ClangImporter] Move implicit properties test helpers to their own header file.
No functionality change.

Swift SVN r16034
2014-04-08 01:23:21 +00:00
Jordan Rose
835bfb15a9 [ClangImporter] Add an option (off by default) to infer "implicit properties".
In Objective-C, any method with no arguments can be used with dot syntax, as
can any method that takes one argument whose name starts with "set". This
commit adds a frontend-only flag -enable-objc-implicit-properties to look for
"setter-like" methods that match up with "getter-like" methods to import them
as Swift properties. By default, such methods are just considered unrelated
methods.

Part of <rdar://problem/16215476>

Swift SVN r16025
2014-04-07 21:49:37 +00:00
Jordan Rose
7810edea97 [ClangImporter] "NSPost" is not a prefix of "NSPostingStyle"
A small error in last week's enum changes.

<rdar://problem/16540910>

Swift SVN r16019
2014-04-07 20:49:54 +00:00
Doug Gregor
49387beb25 Selector splitting: don't split selectors in the middle of whitelisted "multi-words".
Swift SVN r16016
2014-04-07 19:17:23 +00:00
Doug Gregor
4130bb15ff Add a whitelist of designated initializers and use it in the Clang importer.
Seed the whitelist with the designated initializers for NSObject,
NSDocument, and UIDocument. We'll grow this list over time.
Fixes <rdar://problem/16521299>.


Swift SVN r16013
2014-04-07 15:06:59 +00:00
Doug Gregor
a87d65d617 Bridge NSArray <-> AnyObject[].
Allows AnyObject[] to occur in @objc methods/properties/etc., then
bridges between the two in SILGen based on the new array
implementation. <rdar://problem/16535097>.

Note that this commit does not change the Clang module importer to
import NSArray* as AnyObject[] (yet).


Swift SVN r16004
2014-04-07 05:49:48 +00:00
Doug Gregor
57d54cacee Clang importer: add a hardwired table mapping selectors to method names.
Regardless of the heuristic we choose when importing Objective-C
selectors into Swift method names, there will be poor cases. Allow us
to bake specific mappings into the compiler so we can address those
cases without having to modify the Objective-C headers.

Part of <rdar://problem/16341485>.


Swift SVN r15984
2014-04-05 06:52:38 +00:00
Jordan Rose
3b3d10707e Prefer class methods to curried instance methods.
That is, NSObject.isEqual(someObj) should call +isEqual:, not be equivalent
to someObj.isEqual, unless there's a type context that says otherwise.

<rdar://problem/16527717>

Swift SVN r15955
2014-04-04 20:57:31 +00:00
Joe Groff
bef2cdfac4 ClangImporter: Enable importing NSFoo** as ObjCMutablePointer<NSFoo?>.
And blast away a bunch of withUnsafePointer* noise this obviates in the stdlib.

Swift SVN r15896
2014-04-03 22:33:33 +00:00
Joe Groff
527b97895a ClangImporter: Import void* parameters as C*VoidPointer.
Swift SVN r15878
2014-04-03 17:04:37 +00:00
Doug Gregor
1dd795998f Imported C functions never have API names.
The parameter names in C functions are not API in C and can easily be
inconsistent from one redeclaration to another. Therefore, those
parameter names are not considered API.


Swift SVN r15853
2014-04-03 04:13:29 +00:00
Jordan Rose
97627b1812 [ClangImporter] When matching enum prefixes, handle plural enum names.
...so that the enumerators of this declaration:

typedef NS_OPTIONS(NSUInteger, NSKeyValueObservingOptions) {
    NSKeyValueObservingOptionNew = 0x01,
    NSKeyValueObservingOptionOld = 0x02,
    NSKeyValueObservingOptionInitial NS_ENUM_AVAILABLE(10_5, 2_0) = 0x04,
    NSKeyValueObservingOptionPrior NS_ENUM_AVAILABLE(10_5, 2_0) = 0x08
};

...come in as .New, .Old, .Initial, and .Prior. The code checks for plurals
of the form -s, -es, and -ies, which covers all of the NS_OPTIONS in our SDK.

<rdar://problem/16448966>

Swift SVN r15712
2014-04-01 00:13:27 +00:00
Jordan Rose
e9499430de [ClangImporter] Drop the 'k' prefix of an enumerator named 'kConstant'.
This is more common for static constants, but still occurs sometimes in Cocoa.

<rdar://problem/16451607>

Swift SVN r15711
2014-04-01 00:13:26 +00:00
Jordan Rose
fdbfd2439a [ClangImporter] Revise enum splitting to not stop right before a number.
Otherwise we'd import NSNumberFormatterBehavior10_0 of
NSNumberFormatterBehavior as '10_0'. (Yes, you could escape it, but...)

<rdar://problem/16452174>

Swift SVN r15710
2014-04-01 00:13:25 +00:00
Joe Groff
b9299ed04d ClangImporter: Import pointer parameters as CMutablePointer/CConstPointer.
When we see pointer types in function or method parameters, import them as the bridged CMutablePointer/CConstPointer types instead of UnsafePointer, enabling the array and inout conversions with imported APIs.

Swift SVN r15705
2014-03-31 23:06:50 +00:00
Dmitri Hrybenko
2c96263f02 Clang importer: when importing a macro A that resolves to another macro B, put
the imported macro into the module that owns A, not B

rdar://16449405


Swift SVN r15675
2014-03-31 12:11:19 +00:00
Ted Kremenek
16ffbc6f3a Extend @availability(*,unavailable) checking to protocol methods.
Protocols can declare methods as being unavailable, as they do
in NSObjectProtocol (e.g., 'retain').  We both need to flag these
uses, but understand this for protocol conformance.  For protocol
conformance, treat unavailable methods as if they were marked
optional.  The compiler will not allow you to use these methods
anyway.

This finishes up support for:

    <rdar://problem/16331335> Ban ObjC ARC entry points

Swift SVN r15644
2014-03-30 07:11:39 +00:00
Ted Kremenek
80df698ccc Teach ClangImporter to import __attribute__((unavailable)) as @availability(*,unavailable).
This is a direct translation which happens when a Clang declaration
gets translated to a Swift declaration.  This changed, coupled
with the current @availability checking (which is still limited)
now prohibits cases such as using 'NSDeallocateObject()' or
'- (BOOL) allowsWeakReference' from Swift.

Interestingly, it doesn't catch uses of -retain/-release yet, because
those methods are marked unavailable in the NSObject *protocol*.
While the attributes are being mapped over, the @availability
checking needs to be enhanced to replicate more of what Clang does
for this case.

Swift SVN r15643
2014-03-30 05:25:52 +00:00
Dmitri Hrybenko
1d6c76c352 Module interface printing: pass through documentation comments for imported
declarations

rdar://16408910


Swift SVN r15576
2014-03-28 10:54:06 +00:00
Dmitri Hrybenko
a422886e6f Clang importer: add a workaround so that we don't import literals with ud-suffix
Workaround for rdar://16445608


Swift SVN r15559
2014-03-27 17:59:19 +00:00
Dmitri Hrybenko
92b29ef045 Revert "Module interface printing: pass through documentation comments for
imported declarations"

rdar://16408910


Swift SVN r15527
2014-03-26 21:28:42 +00:00
Dmitri Hrybenko
30c6eeff16 Module interface printing: pass through documentation comments for imported
declarations

rdar://16408910


Swift SVN r15511
2014-03-26 16:16:02 +00:00
Jordan Rose
d98030b9d9 [ClangImporter] Include the enum type name in enum prefix stripping.
This keeps us from accidentally stripping off something semantically
meaningful, like in Foundation's NSDirectoryEnumerationOptions:

  NSDirectoryEnumerationSkipsSubdirectoryDescendants
  NSDirectoryEnumerationSkipsPackageDescendants
  NSDirectoryEnumerationSkipsHiddenFiles

<rdar://problem/15496513>

Swift SVN r15436
2014-03-25 01:43:20 +00:00
Doug Gregor
1a25251ea6 Selector splitting: "scaleXBy" is three words, not two.
Swift SVN r15299
2014-03-20 21:38:04 +00:00
Doug Gregor
5d0abd9849 Another preposition splitting option: directional prepositions.
Swift SVN r15297
2014-03-20 21:24:57 +00:00
Doug Gregor
e03cb7c0a0 Don't split when the preposition is the last word in the first selector piece.
Swift SVN r15244
2014-03-19 20:15:50 +00:00
Doug Gregor
e5fff12bf0 Add option to split Objective-C selectors based on the last preposition.
The frontend option -split-objc-selectors splits the first part of an
Objective-C selector into both a function name and the first parameter
name at the last preposition. For example, this Objective-C method:

  - (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex

is imported as

  func stringByPadding toLength(newLength: Int) withString(padString: String) startingAtIndex(padIndex: Int) -> String




Swift SVN r15156
2014-03-17 20:34:48 +00:00
Doug Gregor
163d2fa24d [Clang importer] Go back to mirroring superclass initializers in subclasses.
When importing an Objective-C class, import all of the initializers in
all of its superclasses. When we have no information about designated
initializers for a class, the initializers come in as subobject
initializers. When we do have information about designated
initializers, we (1) use it to sort out subobject from complete object
initializers in that class, and (2) assume that all initializers from
superclasses are complete object initializers. Overall, this better
matches Objective-C's semantics.



Swift SVN r14841
2014-03-09 08:01:37 +00:00