Commit Graph

2523 Commits

Author SHA1 Message Date
Joe Groff
4d4a324497 Import Protocol as a foreign class.
In modern ObjC Protocol's object-ness is vestigial, and its class object isn't even visible from newer runtimes, so we can't use it as type metadata. Import it as a foreign class so that we make our own artificial metadata. Fixes <rdar://problem/17303759>.

Swift SVN r18882
2014-06-13 23:07:10 +00:00
Jordan Rose
a4ea927426 [ClangImporter] If a protocol and a class have the same name, add "Protocol".
.../if/ the protocol and the class are from the same top-level Clang module.
If not, the protocol is /not/ renamed, and users will have to disambiguate
with module qualification.

This kills our hardcoded "RenamedProtocols" list; it turns out this pattern
is more common than we thought /and/ leads to cross-referencing issues.

<rdar://problem/16206627>

Swift SVN r18809
2014-06-11 23:00:00 +00:00
Chris Lattner
4dbff38780 Change NULL/Nil/nil and various other macros to be imported as an unavailable decl,
instead of importing them as _Nil (which will be going away when nil becomes an expr).

You now get an error saying:
error: 'NULL' is unavailable: use 'nil' instead of this imported macro

This is pretty cool all around, except for the GCD macros that need to be fixed, I'll
handle that as a follow-up.


Swift SVN r18747
2014-06-09 17:08:11 +00:00
Jordan Rose
577f16c48a [ClangImporter] Import constant globals using 'let' instead of 'var'.
Previously, we considered all imported globals to be mutable, which would
cause problems if you actually tried to change them.

<rdar://problem/17020952>

Swift SVN r18663
2014-05-29 23:04:26 +00:00
Jordan Rose
3ba8808a6f [ClangImporter] When remapping an init method's name, change the selector too.
This is what we use to drop the variadic parameter on UIActionSheet and
UIAlertView's initializers, along with secretly adding a category to each
to provide the one-fewer-parameter init method implementation. However,
we haven't been using the Swift name for the method to generate Objective-C
calls for a while now--we use the @objc attribute. And that was still using
the original selector, and so we crashed.

Fixed by passing the new selector to the @objc attribute.

<rdar://problem/17012323>

Swift SVN r18582
2014-05-23 01:56:56 +00:00
Doug Gregor
67ca1c9ea1 Implement the new casting syntaxes "as" and "as?".
There's a bit of a reshuffle of the ExplicitCastExpr subclasses:
  - The existing ConditionalCheckedCastExpr expression node now represents
"as?". 
  - A new ForcedCheckedCastExpr node represents "as" when it is a
  downcast.
  - CoerceExpr represents "as" when it is a coercion.
  - A new UnresolvedCheckedCastExpr node describes "as" before it has
  been type-checked down to ForcedCheckedCastExpr or CoerceExpr. This
  wasn't a strictly necessary change, but it helps us detangle what's
  going on.

There are a few new diagnostics to help users avoid getting bitten by
as/as? mistakes:
  - Custom errors when a forced downcast (as) is used as the operand
  of postfix '!' or '?', with Fix-Its to remove the '!' or make the
  downcast conditional (with as?), respectively.
  - A warning when a forced downcast is injected into an optional,
  with a suggestion to use a conditional downcast.
  - A new error when the postfix '!' is used for a contextual
  downcast, with a Fix-It to replace it with "as T" with the
  contextual type T.

Lots of test updates, none of which felt like regressions. The new
tests are in test/expr/cast/optionals.swift. 

Addresses <rdar://problem/17000058>


Swift SVN r18556
2014-05-22 06:15:29 +00:00
Argyrios Kyrtzidis
5d2d762cd3 [ClangImporter] A typedef to a typedef should get imported as a typealias.
Swift SVN r18328
2014-05-18 16:08:56 +00:00
Doug Gregor
f338c9f9bf Extend ObjC methods whitelist to support adding an "unavailable" attribute.
Use this for -class and +class, to make them less ad hoc. More to follow.

As part of this, actually mark imported unavailable declarations in a
protocol as "optional", because nobody should have to implement an
unavailable declaration.

Swift SVN r18262
2014-05-17 19:18:02 +00:00
Jordan Rose
3a8208ec17 [ClangImporter] Drop support for @partial_interface.
We're not going to ship this feature.

Swift SVN r18168
2014-05-16 03:49:02 +00:00
Doug Gregor
d640eb4083 Introduce support for blacklisting factory-methods-as-inits.
Some fcactory methods shouldn't come in as initializers, per
<rdar://problem/16908950>.


Swift SVN r18101
2014-05-15 07:04:46 +00:00
John McCall
a3fb501114 Recognize the objc_bridge and objc_bridge_mutable
attributes and create implicit conversions for them.

Also, when generating Clang modules for import, set the
appropriate macro to make CoreFoundation actually apply
those attributes to its typedefs.

Also, give shared linkage to imported class method
definitions.

The net effect is that CFString now automatically
converts to NSString and vice-versa.

Depends on Clang r208756 in order to preserve attributes
applied to tag types in multiple typedef declarations.

Swift SVN r18069
2014-05-14 08:19:39 +00:00
Doug Gregor
7bedd5ca22 Recognize colliding imported constructors properly.
I had gotten the type computation incorrect, causing the
deserialization crashes seen on the buildbot.

Swift SVN r18032
2014-05-13 22:30:19 +00:00
Doug Gregor
d5a9c2ab94 Only supersede initializers with other imported initializers when the types match.
This fixes a case where the Swift-variadic and C-varargs versions of
various initializers were superseding each other
<rdar://problem/16801456>.

It also uncovered some more cases where we weren't getting quite the
right semantics for factory-methods-as-initializers, which are also
fixed here.

Swift SVN r18010
2014-05-13 16:49:39 +00:00
Doug Gregor
9ecb07409b Make the "superseded" diagnostic better.
Users shouldn't actually see this diagnostic, but if it does show up,
it should at least be useful:

error: 'init' is unavailable: superseded by import of
    -[NSArray(NSArrayCreation) initWithObjects:]

Swift SVN r18009
2014-05-13 16:49:36 +00:00
Jordan Rose
5d2ad6c4d9 [ClangImporter] Import known property accessors with consistent types.
Previously, the getter and setter for a property could disagree on what the
"type of the property" was: Unmanaged<CFType> vs. CFType, or COpaquePointer
vs. CMutableVoidPointer. Now, we treat property accessors as distinct from
normal methods when importing their parameter and result types, and have
those types follow the same rules as they would for the property itself.

This will need a bit of cleanup work once we're importing implicit properties
everywhere, but this handles the crashes and unfortunate limitations we were
seeing for WWDC.

<rdar://problem/16544938>

Swift SVN r17987
2014-05-13 01:32:12 +00:00
Doug Gregor
73528dc0e0 Enable importing factory methods as initializers by default.
Finishes <rdar://problem/16509024>.

Swift SVN r17972
2014-05-12 23:01:20 +00:00
Doug Gregor
2702c943aa When determining whether an imported initializer is a designated initializer, use the context it's being imported into.
Previously, we were using the context where the initializer was declared in Objective-C, rather than where it was being imported. This meant that we wouldn't treat an inherited designated initializer as a designated initializer if (for example) the designated-initializer marking came from our internal table of DIs. Fixes <rdar://problem/16838515>.

Swift SVN r17968
2014-05-12 22:47:38 +00:00
Ted Kremenek
6e83fde849 Suggest ".self", not ".Type" (the old name) for +class remapping.
Swift SVN r17884
2014-05-11 20:28:48 +00:00
Ted Kremenek
4479eb85c0 Import +class and -class as unavailable, suggesting ".type" and ".dynamicType" instead.
Implements <rdar://problem/16871007>.

Swift SVN r17883
2014-05-11 20:22:53 +00:00
Joe Groff
3dda4c9cf5 SILGen: Reference ObjC initializers and factories through foreign-to-native thunks.
Eliminate the duplicate half-broken bridging logic in emitClassConstructorAllocator by referencing foreign initializers through their foreign-to-native thunks, which SILGen knows how to emit already. Do the same thing for factory initializers by suppressing their normal allocating initializer codegen and just referencing the usual foreign-to-native thunk for them. This fixes <rdar://problem/16853205> because we get the ownership thunking right now.

Swift SVN r17840
2014-05-10 20:18:53 +00:00
Argyrios Kyrtzidis
19aeaf94fc Fully embrace ParamDecls instead of AnyPattern at function creation time, thus removing the need to create param decls at SILGen or Sema.
Swift SVN r17829
2014-05-10 18:23:50 +00:00
Doug Gregor
d1e8be50be Turn -implicit-objc-with on by default <rdar://problem/16795899>.
Swift SVN r17796
2014-05-09 20:17:42 +00:00
John McCall
03d3cf9c40 Import the formal type of a property as audited if the
corresponding getter method is audited.

There are a host of other potential consistency problems
here, but this should fix a particular egregious one
that was preventing use of NSColor's CGColor property.

rdar://16846555

Swift SVN r17667
2014-05-08 01:32:25 +00:00
John McCall
c03b705dd0 Recognize CF "subclasses" and require CFTypeRef to specifically
be 'const void *', not just any 'void*'.

Swift SVN r17659
2014-05-08 00:51:27 +00:00
Joe Pamer
d22ffa8cb8 Re-lazify the addition of equatable conformances to imported enum types. (rdar://problem/16808612)
Rather than force conformances to Equatable to be added to all imported enumeration types outright, change them back to being lazily added. We can then handle situations where new overloads of '==' are introduced during constraint generation by re-writing the relevant overload disjunction constraint to include the newly forced declarations as bind options.

Swift SVN r17557
2014-05-06 19:56:29 +00:00
John McCall
cbd662632b Import CFTypeRef as AnyObject, but wrap it in Unmanaged<>
in all the situations that we would wrap e.g. CFStringRef.

Swift SVN r17546
2014-05-06 09:34:24 +00:00
John McCall
a83e5740de Import CF types as managed pointers when they are:
- the type of a const global variable
  - the type of a parameter, always
  - the return type of a function that has been audited
    or has an explicit retained/not-retained attribute
  - the return type of an ObjC method that has an explicit
    retain/not-retained/inner-pointer attribute

Additionally, choose the correct conventions for all
these cases during SIL type lowering.

All this importing logic is still only enabled under
-Xfrontend -import-cf-types.

Swift SVN r17543
2014-05-06 08:29:44 +00:00
Joe Groff
e51429eca5 ClangImporter: Guard loadAllMembers with ImportingEntityRAII.
We can enter here without an importing scope if we become more lazy, as can happen when <rdar://problem/16807886> is fixed. Add a regression test that fails when that patch is attached (but not yet on ToT).

Swift SVN r17513
2014-05-06 01:08:09 +00:00
Jordan Rose
0c46eb821e [ClangImporter] Handle decls declared in imported headers.
This improves the -import-objc-header option to read decls from the header
as well. Any declaration that is not from a module will be considered to be
part of the "header module". Conversely, forward-declarations appearing in
the header will be resolved by looking through all modules responsible for
importing a header.

More of <rdar://problem/16702101>

Swift SVN r17492
2014-05-05 22:52:17 +00:00
Ted Kremenek
1b2dfb79d4 Hack ClangImporter to *always* import "description", "debugDescription", and "hash" as properties.
This routes around temporary SDK discrepancies.

This change revealed some subtle issues with adding implicit property
definitions.  A big challenge here is that 'description' is defined
in NSObject's protocol, so the implicit property needed to be
generated in all classes that conformed to the protocol.  To make
things more complicated, some classes define 'description' themselves,
so we needed to take care to not generate the same implicit property
twice.

This change reveals a potential bug in the type checker.  This now
crashes:

  func bar(x: String) {
    print(x)
  }

  func foo(x : AnyObject) {
    print(x.description)
  }

but this does not:

  func bar(x: String) {
    print(x)
  }

  func foo(x : NSObject) {
    print(x.description)
  }

This is due to the fact there is a class method "description()" and
a instance property "description" in the lookup on AnyObject.

Swift SVN r17454
2014-05-05 16:37:49 +00:00
Doug Gregor
d255983103 Clang importer: synthesize fromRaw/fromMask w/o any keyword arguments.
Swift SVN r17436
2014-05-05 14:35:18 +00:00
John McCall
2b969c41a2 Track whether a class is "foreign" in the AST.
This basically just means "it's a CF class" for now,
but you could imagine applying this to all sorts of
class-like types from peer runtimes that we can't
support all possible language features for.

There are quite a few language features that require
fairly deep object-model integration to implement,
like subclassing and adding polymorphic methods.
Some of those features, like final classes, are useful
to generally support as attributes, but most of
them aren't.  At least in the short term, it makes
sense to have a big hammer we can hit things with.

Swift SVN r17428
2014-05-05 06:45:40 +00:00
Doug Gregor
1fe794b31a Don't create keyword arguments for the value constructors of imported enums.
NSFooOptions(0) is better than NSFooOptions(value: 0).


Swift SVN r17422
2014-05-05 04:18:07 +00:00
Doug Gregor
eb7a9144a8 Bring keyword arguments to subscripts.
Subscript declarations were still encoding the names of index
variables in the subscript type, which unintentionally made them
keyword arguments. Bring subscript declarations into the modern day,
using compound names to encode the subscript argument names, which
provides consistency for the keyword-argument world
<rdar://problem/14462349>. Note that arguments in subscripts default
to not being keyword arguments, which seems like the right default.

We now get keyword arguments for subscripts, so one can overload
subscripts on the names of the indices, and distinguish at the call
site. Under -strict-keyword-arguments, we require strictness here as well.

The IRGen/IDE/SILGen test updates are because the mangling of common
subscripts changed from accidentally having keyword arguments to not
having keyword arguments.

Swift SVN r17393
2014-05-04 19:31:09 +00:00
Jordan Rose
9d74d25e50 [ClangImporter] Replace one hack to deal with va_list for another.
va_list on i386 and arm64 is a pointer. On armv7, it's a struct that
/contains/ a pointer. But on x86_64, it's an array of one struct element,
which means it decays to a pointer as an argument but not as a variable or
struct field.

This patch:
- Looks for a decayed pointer argument that represents an x86_64 va_list.
- Validates that the va_list on other platforms is at least pointer-sized.
- Uses the decayed (pointer) type when getting the Clang type for
  CVaListPointer.
- Drops the implicit conversion from CVaListPointer to COpaquePointer.

All together, this provides more type-safety for functions using va_list on
x86_64.

Swift SVN r17307
2014-05-03 03:25:20 +00:00
John McCall
39453cd28e Set up protocol conformances properly for the default
protocols on imported CF types.

This restores r17244.

Swift SVN r17268
2014-05-02 20:17:15 +00:00
Joe Groff
4b327a7805 Revert "Set up protocol conformances properly for the default"
This reverts commit r17244.

Swift SVN r17250
2014-05-02 16:22:37 +00:00
John McCall
046524dd61 Set up protocol conformances properly for the default
protocols on imported CF types.

Swift SVN r17244
2014-05-02 10:50:19 +00:00
Ted Kremenek
050fd53af7 Rename UncheckedOptional to ImplicitlyUnwrappedOptional.
Swift SVN r17232
2014-05-02 06:13:57 +00:00
John McCall
207fc4db2b Instead of importing CF typedefs as typedefs for Unmanaged<T>,
import them as the underlying class type, then wrap that in
Unmanaged<T>.

Also, trust (and fix) hinting to tell us when to wrap something
in an optional type.

Swift SVN r17207
2014-05-01 23:29:28 +00:00
John McCall
e1bd429cf7 Under -import-cf-types, import
typedef struct __foo *FooRef;
as an Unmanaged<Foo>, where Foo is a class type.

Swift SVN r17206
2014-05-01 23:29:27 +00:00
Jordan Rose
92bcd97d54 [ClangImporter] Don't crash if we have a SWIFT_CLASS but the module is missing.
Also, if the generated header says that a class or protocol is available in
a module, but we can't find it, we should treat the decl as missing rather
than pretending it's a normal Objective-C decl.

Part of <rdar://problem/16776466>

Swift SVN r17175
2014-05-01 20:40:06 +00:00
Doug Gregor
9cfb1b5ca4 Keep track of the locations of the element names in a TupleExpr.
As part of this, use tail allocation to reduce the memory footprint of
TupleExprs. Use factory methods to make it easier to construct.

I'll be using this information in a follow-on patch. SourceKit
probably wants it as well.


Swift SVN r17129
2014-05-01 00:16:36 +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
John McCall
23b224a655 Change the type importer to import types abstractly first,
and only later adjust the type for the manner in which
it is actually being used.

This gives us a foundation on which to build a consistent
and defensible model for importing typedefs.

Also fix a subtle problem with typedefs imported from
multiple clang modules.

Swift SVN r17079
2014-04-30 06:46:00 +00:00
Doug Gregor
72e32c93af Make argument names default to keyword arguments in the cases where Objective-C has names.
Introduce a model where an argument name is a keyword argument if: 

  - It is an argument to an initializer, or
  - It is an argument to a method after the first argument, or
  - It is preceded by a back-tick (`), or
  - Both a keyword argument name and an internal parameter name are
    specified. 

Provide diagnostics Fix-Its to clean up cases where the user is
probably confused, i.e.,

  - "_ x: Int" -> "x: Int" where "x" would not have been a keyword
  argument anyway
  - "x x: Int" -> "`x: Int"

This covers the compiler side of <rdar://problem/16741975> and
<rdar://problem/16742001>.

Update the AST printer to print in this form, never printing just 
a type for a parameter name because we're also going to adopt
<rdar://problem/16737312> and it was easier to move the tests once
rather than twice.

Standard library and test updates coming separately.




Swift SVN r17056
2014-04-30 00:04:04 +00:00
Ted Kremenek
fa12c5ad2a Remove hack for -animator proxy introduced in <rdar://problem/16020273>.
This is no longer needed because the Swift runtime now more
generally supports proxies.

Swift SVN r16840
2014-04-25 20:27:04 +00:00
Jordan Rose
29566347dc [ClangImporter] Deal with redeclarations of properties as readwrite.
(and a similar issue for subscripts)

In Cocoa, a property can be declared readonly in the public interface of a
class and readwrite in a class extension. Similarly, a protocol can require
a readable property, but a category then declares the property as readwrite.
Swift doesn't like having two of the same declaration, and which one ended
up being used was order-dependent.

This change doesn't really fix the problem, but it does paper over it.
Now, when the importer sees a redeclaration of a readonly property as
readwrite, it forcibly updates the existing property with the new setter.
This isn't entirely correct; the redeclaration doesn't show up in its category,
and this doesn't respect visibility (i.e. the change to readwrite occurs in
a separate module that isn't visible in all source files). (But extensions
don't respect visibility in any other way, either, even in pure Swift code.)

At its heart, this is just a mismatch between Objective-C allowing
redeclarations that add capabilities and Swift not having redeclarations
at all. At some point, it may make more sense to model this as an overload,
or to mark the original declaration invalid, but for now this seems to be
the most contained change we can get that fixes the problem.

<rdar://problem/16692921>

Swift SVN r16832
2014-04-25 19:38:50 +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