Commit Graph

2523 Commits

Author SHA1 Message Date
Doug Gregor
104cfe8919 Clang importer: add some statistics related to selector splitting.
Swift SVN r15873
2014-04-03 16:10:13 +00:00
Doug Gregor
708bb64c6f Migrate initializer importing over to importMethodType().
We now consistently use the method name to form the types of
Objective-C methods.


Swift SVN r15851
2014-04-03 03:32:09 +00:00
Doug Gregor
e418e93511 Start cleaning up method name importing.
Import a selector into a Swift method name, performing splitting at
that point. Use the resulting method name to determine the argument
names of the parameters, rather than trying to chop up the selector
again. There's more refactoring to do here.

This fixes a longstanding bug where the first argument of an
Objective-C method got the internal parameter name when it should
have gotten no name at all.

Swift SVN r15850
2014-04-03 00:34:40 +00:00
Doug Gregor
527b147ba7 Revert "Allow the first name of a selector-style function declaration to be separated."
This reverts r15140.

Conflicts:
	include/swift/Serialization/ModuleFormat.h
	lib/Parse/ParsePattern.cpp

Swift SVN r15846
2014-04-02 23:27:52 +00:00
John McCall
c163b30f1e Enable importing ObjC pointers as UncheckedOptional.
There's no point in maintaining a driver option for this
because of the extent of the library changes required to
make it work.

Swift SVN r15784
2014-04-02 09:37:16 +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
Ted Kremenek
39dc97738b For "unknown declared type" errors involving remapped types, suggest the remapped type as a fixit.
For example:

  func foo(a: id)

now gets a fixit to turn 'id' into 'AnyObject'.

This relies on the ClangImporter recording the translation as it
processes declarations whose types are remapped.

Swift SVN r15668
2014-03-31 07:10:32 +00:00
Doug Gregor
4f9f945d62 Teach the Clang importer to add @objc(selector) attributes for imported methods and initializers.
With this in place, remove the hacks that peeked at the imported Clang
node to determine the selector for a method. We're all
attribute-driven now. Part of <rdar://problem/16019773>.


Swift SVN r15663
2014-03-31 04:26:32 +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
Doug Gregor
d51744e396 Clang importer: add @objc(name) attributes to imported classes and protocols.
Now that we represent the Objective-C runtime names of classes and
protocols in a uniform way, stop peeking into the Clang AST nodes to
get this information for imported classes and protocols. This is
better layering and helps test that new code path.


Swift SVN r15632
2014-03-29 21:15:29 +00:00
Joe Groff
9f7dab725c Make the ASTContext parameter to MetatypeType::get optional for type-checked types.
We can just get it from the instance type, if the instance type has been fully initialized, which is the case except during parsing of type decls when the decls' own types are being formed.

Swift SVN r15598
2014-03-29 02:50:30 +00:00
John McCall
c3958ea8d7 Import DynamicSelf types as unchecked optionals
when that import mode is enabled.

Patch by Jordan, thanks.

Swift SVN r15574
2014-03-28 05:39:25 +00:00
Doug Gregor
831b444aa1 If only C++ had a definite initialization pass...
Swift SVN r15535
2014-03-26 22:54:16 +00:00
Joe Pamer
bcab632730 When requesting members or protocols from a type declaration, force any delayed members by default. This allows us to pretty-print otherwise unresolved implicit members of imported enums. (rdar://problem/16430343)
Swift SVN r15534
2014-03-26 22:31:02 +00:00
Jordan Rose
3057645677 [ClangImporter] Resolve @partial_interface to a Swift class, like @class.
@partial_interface is like @class, but it allows the user to write categories
or (eventually) subclasses in their Objective-C headers even though the
class will be implemented in Swift. The resolution rules are thus exactly the
same as for class.

I've included a template hack to allow you to keep building without switching
to the new internal Clang repo; in this case you will see one test failure.

<rdar://problem/16426884>

Swift SVN r15523
2014-03-26 21:08:50 +00:00
Doug Gregor
019bb049a1 [Clang importer] Import initializer methods in protocols as initializers.
You can't actually call these initializers with an existential yet, but nor could you safely call the init methods anyway. At least you'll be able to conform to NSCoding with an initializer rather than an "init method". Fixes <rdar://problem/15595471>.

Swift SVN r15521
2014-03-26 21:02:35 +00:00
Dmitri Hrybenko
a15acc0536 Remove duplicated type name in comment and fix some questionable formatting
Swift SVN r15503
2014-03-26 10:59:12 +00:00
Joe Pamer
eebe1ebf37 When importing external enum types, delay the creation of implicit members and their associated support functions until after the types are actually referenced.
This significantly reduces the amount of overhead incurred when naively importing large external modules without referencing many of its members, which should directly improve response times in the playground. For example, the repro code attached to rdar://problem/16387393 imports Foundation but references none of its members, and with these changes its total compilation time is almost 2.5x faster.

Swift SVN r15479
2014-03-26 01:06:27 +00:00
Jordan Rose
5779d43238 [ClangImporter] Resolve forward @protocol declarations like we do @class.
When we see an unresolved @protocol in a Clang module, we now check the name
against the protocols in the Swift module with the same name like we do for
classes.

More <rdar://problem/16295994>

Swift SVN r15472
2014-03-25 23:16:01 +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
Jordan Rose
694987defc [ClangImporter] Don't mirror root class methods returning instancetype.
In Objective-C, all instance methods on a root class are also available as
class methods (because all class objects in Objective-C are instances of the
root class). However, we were incorrectly introducing class methods that
returned 'Self' (instead of 'Self.Type') for every instance method with a
related result type. Returning 'Self.Type' exposes a new type checker bug
<rdar://problem/16414206>, but in practice we don't have much reason to do
this anyway. For now, just don't try to mirror instancetype-returning methods
as class methods.

Swift SVN r15435
2014-03-25 01:19:58 +00:00
Jordan Rose
608fee70f1 Replace ASTMutationListener with a new LazyResolver callback.
..."resolveExternalDeclImplicitMembers".

Now that the ClangImporter has direct access to the type-checker (through
a LazyResolver), there's no reason to bounce through an obtusely generic
interface on ASTContext. Just call through directly to handle the implicit
members and conformances of external decls.

There's no actual functionality change here, though we can probably do
further cleanup in this area.

Swift SVN r15356
2014-03-22 00:20:00 +00:00
Jordan Rose
8d35d75ce3 [ClangImporter] Resolve forward class declarations to definitions in Swift.
When we see an unresolved @class in a Clang module, we check the class name
against the classes in the Swift module with the same name.

This unfortunately necessitates putting a reference to the active type checker
into the ClangImporter, because the class in the current Swift module hasn't
been type-checked yet. This is now being used everywhere we do a lookup.

<rdar://problem/16295994>

Swift SVN r15355
2014-03-22 00:19:59 +00:00
Chris Lattner
1f275532a6 now that nothing uses the PatternBindingDecl::HasStorage bit, there is no
reason to compute it, store it, serialize it, etc.  Remove the associated
logic.



Swift SVN r15302
2014-03-20 22:49:43 +00:00
Jordan Rose
84d9be5610 [ClangImporter] Don't attach Clang decls to defined-in-Swift classes.
(and protocols)

I had missed this check before. It probably made very little difference, but
we use "hasClangNode" to mean "defined-in-Objective-C", so it's better to get
this correct.

The test change is fairly minimal: not giving the class a Clang node means we
have to find it by lookup, but the Objective-C part of the "Base" module
wasn't actually considered visible by Clang. In a real situation the "Base"
module would be a proper framework and both halves of the module would have
been imported, as tested by the mixed-language test.

Swift SVN r15248
2014-03-19 21:17:30 +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
8cf018a1d2 Give Pattern::clone() an OptionSet rather than a bool; it's going to get more interesting.
Swift SVN r15061
2014-03-14 18:31:21 +00:00
Jordan Rose
455e96e955 @objc should be enough to make a protocol a class protocol.
There are very few times where we'd want to use @class_protocol outside of
@objc, and it feels weird to tell someone coming from Objective-C that they
need to mark their already-@objc protocol as @class_protocol. Instead, just
change ProtocolDecl::requiresClass to check for @objc.

(The commit checks for /both/ @objc-the-attribute and the IsObjC flag.
This is to give the right answer before type-checking, or at least the
likely-intended answer.)

<rdar://problem/16302887>

Swift SVN r15060
2014-03-14 17:56:07 +00:00
Doug Gregor
ea7e70bbf7 Automatically inherit subobject initializers when none are explicitly defined.
If a subclass defines no subobject initializers and all of its stored
properties have initial values, "inherit" all of the subobject
initializers of its superclass by creating a new initializer with the
same signature that overrides (and chains to) the corresponding
subobject initializer of its parent. Do this instead of blindly
creating a default initializer.

Note that we aren't yet doing this for generic initializers. That will
be a separate step.

Swift SVN r14995
2014-03-13 18:43:30 +00:00
Jordan Rose
12964f2e27 [ClangImporter] Resolve generated header protocols to native decls as well.
This is the same as the previous commit, but for protocols. To do this I
had to modify the ObjC printer to include a SWIFT_PROTOCOL annotation like
the SWIFT_CLASS annotation already in use. This is probably a good thing
anyway.

Second half of <rdar://problem/16296027>

Swift SVN r14985
2014-03-13 00:19:12 +00:00
Jordan Rose
64debb657d [ClangImporter] Match up classes in generated headers with native classes.
This is necessary to handle Swift code using API defined in Objective-C
that itself uses classes defined in Swift. Protocols coming next.

First half of <rdar://problem/16296027>

Swift SVN r14984
2014-03-13 00:19:10 +00:00
Joe Groff
c7251df2f1 Clang importer: set the AllArchetypes of imported protocol decl generic param lists.
Swift SVN r14961
2014-03-12 18:32:58 +00:00
Dmitri Hrybenko
c330a18a61 Remove stale comment
Swift SVN r14869
2014-03-10 15:25:39 +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
Joe Groff
5bb6c6a39c ClangImporter: Import ObjCMethod selector names as Swift DeclNames.
This gets selector references working for imported ObjC methods.

Swift SVN r14797
2014-03-07 22:43:05 +00:00
Jordan Rose
205e236a7c [ClangImporter] Synthesize the Protocol class if needed.
'Protocol' is the class used by Objective-C to refer to the runtime metadata
for a protocol. It's used by APIs such as NSXPCConnection. The class is
defined as "@interface Protocol : NSObject" in the modern Objective-C runtime.

This is a workaround for a Clang bug, PR19061 / <rdar://problem/16244450>.
We ought to be able to see the definition of Protocol whenever the
ObjectiveC module is loaded, but right now we can't. Once that bug is fixed,
this change should be reverted.

<rdar://problem/16229963>

Swift SVN r14761
2014-03-06 23:20:48 +00:00
Chris Lattner
870df7f266 import noreturn objc methods as swift noreturn function type. I don't
know how to test this presently, but it should theoretically work.



Swift SVN r14727
2014-03-06 07:43:35 +00:00
Doug Gregor
7ce9ce8c57 Actually import inherited initializers for Objective-C classes.
Swift SVN r14726
2014-03-06 06:24:35 +00:00
Chris Lattner
4a827b7de4 enhance the clang importer to import noreturn functions like abort/exit as noreturn
swift functions.  Unfortunately, this can't be satisfactorily tested at the moment,
but that will be fixed soon.



Swift SVN r14709
2014-03-06 00:56:44 +00:00
Jordan Rose
a84f939545 [ClangImporter] Import string macros as CStrings.
Also, pass "-std=gnu11" so that we accept UTF-8 strings in these macros.

<rdar://problem/16198953>

Swift SVN r14664
2014-03-05 00:29:43 +00:00
Doug Gregor
9a3a9addd2 Mirror Objective-C initializers in subclasses that don't have designated inits.
This reverts part of r14563, which swapped out the mirroring of
Objective-C initializers into subclasses. We still need to do this
when a subclass does not declare it's designated initializers. At
least the mirroring code is more robust, and marks declarations as
implicit so they won't show up when printed.



Swift SVN r14633
2014-03-04 05:51:44 +00:00
Doug Gregor
6ef8e0792e When an Objective-C class that has designated initializers, mark the other initializers as complete object initializers.
This allows inheritance of complete object initializers from
Objective-C classes.

Swift SVN r14609
2014-03-03 21:38:26 +00:00
Dmitri Hrybenko
5889e17528 Rewrite the long assertion so that it does not trigger -Wlogical-op-parentheses
Swift SVN r14575
2014-03-02 16:21:29 +00:00
Dmitri Hrybenko
197a682e14 Don't compare 'const char *' with ==, use StringRef
Swift SVN r14574
2014-03-02 15:41:33 +00:00
Doug Gregor
a77d08f5a1 Use inherited initializers rather than cloning for Objective-C classes.
Make Objective-C initializers inherited according to the inheritance
rules we've specified. Prevent the Clang importer from manually
copying all of the initializers from every superclass into each
class. 

This eliminates a ton of extra allocating constructors generated when
importing Objective-C init methods. Now we only generate allocating
constructors for the init methods that are actually declared.

Note that initializer inheritance only actually works for
Objective-C-defined classes. More to come.



Swift SVN r14563
2014-03-02 01:59:54 +00:00
Joe Groff
8e6b353542 Derive conformances of Equatable and Hashable for simple enums.
If an enum has no cases with payloads, make it implicitly Equatable and Hashable, and derive default implementations of '==' and 'hashValue'. Insert the derived '==' into module context wrapped in a new DerivedFileUnit kind, and arrange for it to be codegenned with the deriving EnumDecl by adding a 'DerivedOperatorDecls' array to NominalTypeDecls that gets visited at SILGen time.

Swift SVN r14471
2014-02-27 20:28:38 +00:00
Jordan Rose
0d4b039e11 Support properties in Objective-C protocols.
There are two parts to this:
- Import protocol properties as properties, instead of as a pair of methods.
- Fix IRGen to handle property accesses in @objc protocols.

<rdar://problem/12993073>

Swift SVN r14438
2014-02-27 01:19:08 +00:00