Commit Graph

252 Commits

Author SHA1 Message Date
Doug Gregor
e045429471 [Type Checker] Improve interface to TypeChecker::conformsToProtocol().
This function had a weird, pre-ProtocolConformanceRef interface that
returned true when the type conformed to the protocol, then had a
separate indirect return value for the concrete conformance (if there
is one). Refactor this API, and the similar
TypeChecker::containsProtocol(), to produce an optional
ProtocolConformanceRef, which is far more idiomatic and easier to
use. Push ProtocolConformanceRef into a few more places. Should be NFC
2016-11-14 16:00:58 -08:00
Jordan Rose
7add05fa38 [PrintAsObjC] Use of imported generics require the full definition. (#5518)
I'm not sure why this didn't occur to me in 8282160d: of course if you
see a generic type with arguments, you need to see the @interface for
that type in order to supply the arguments. Maybe I was thinking the
generated interface would automatically import anything the module
itself imports, but that hasn't ever been true.

rdar://problem/28738008
2016-10-31 10:34:25 -07:00
Graydon Hoare
8970d44675 Add "-swift-version <n>" that sets LangOpts.EffectiveLanguageVersion.
This flag switches the "effective language version" of the compiler,
at least to any version supported (as of this change: "3" or "3.0").

At the moment nothing uses it except the language version build
configuration statements (#if swift(...)) and various other places
that report, encode, or otherwise check version numbers.

In the future, it's intended as scaffolding for backwards compatibility.

Fixes SR-2582
2016-09-20 15:11:37 -07:00
Jacob Bandes-Storch
89d0d62bd6 [PrintAsObjC] print __attribute__((noescape)) in Obj-C headers (#4438)
https://bugs.swift.org/browse/SR-2406
2016-08-31 09:27:59 -07:00
Doug Gregor
51529ae888 Eliminate the -enable-id-as-any flag; it's always on now anyway.
Simplify e.g., ASTContext::getBridgedToObjC(), which no longer needs
the optional return.

Eliminate the now-unused constraint kind for checking bridging to
Objective-C.
2016-08-19 21:17:09 -07:00
Slava Pestov
677e72a9c4 AST: Remove GenericParamList::getPrimaryArchetypes(), NFC 2016-08-18 18:43:41 -07:00
Jordan Rose
ea4d110146 [PrintAsObjC] Handle typealiases in ObjC generics.
More specifically, don't try to emit a definition for them. Just fall
through to what we do for forward-declarations...which also needed some
fixing, to make sure we don't use a Swift typealias as its underlying
type but never import the underlying type.

https://bugs.swift.org/browse/SR-2352
2016-08-16 11:09:49 -07:00
Jordan Rose
5e06eb28df [PrintAsObjC] Assert on types we don't expect in @objc methods.
No intended functionality change, because we shouldn't see any of
these types. Should help prevent errors like the previous commit.
2016-08-10 19:45:51 -07:00
Jordan Rose
ca009b7604 [PrintAsObjC] Handle forward-declarations for 'Class <SomeProto>'.
rdar://problem/27746149
2016-08-10 19:45:51 -07:00
Jordan Rose
8282160de8 [PrintAsObjC] Handle circularities introduced by ObjC generics.
Like Swift generics, Objective-C generics may have constraints; unlike
Swift generics, Objective-C doesn't do separate parsing and
type-checking passes. This means that any generic arguments for
constrained generic parameters must be fully-defined, in order to
check that they satisfy the constraints.

This commit addresses this problem with three different mechanisms,
one for each kind of declaration that might run into this issue:

- For classes, if a member references a type with constrained generic
  parameter, and the corresponding argument type hasn't been printed
  yet, that member is "delayed", which means it is put into a category
  at the end of the file.

- Protocols cannot have categories, so for protocols we instead see if
  we can print the definition of the other type first. To break
  circular dependencies, the printer will not attempt this if both the
  type and the protocol are already being depended on. This isn't
  perfect (see below).

- Rather than delaying members of extensions, we just delay them
  wholesale. This keeps related members together, but also has
  problems (see below).

These approaches solve the most common cases while still not crashing
in the uncommon ones. However, there are still a number of problems:

- The protocol heuristic is overly negative, which means we may generate
  an invalid header even when there's a reasonable ordering. For example,
  a single class might inherit from a class A and conform to protocol P,
  and protocol P depends on class A as a generic argument. In this case,
  defining class A first is the right thing to do, but it's possible for
  the printer to decide that there's circularity here and just forward-
  declare A instead.

- Protocols really can be circular. This can be fixed by printing a
  forward-declared protocol alongside the generic constraints, i.e.
  'id <MoreThanNSCopying, NSCopying>' instead of just
  'id <MoreThanNSCopying>'.

- Extensions can introduce protocols as well. This is not modeled at
  all; if a member depends on a protocol conformance, it's assumed
  that simply printing the class would be sufficient. This could be
  fixed by checking how a generic argument satisfies its constraints,
  possibly delaying individual members from extensions in order to
  print them sooner.

- More cases I haven't thought about.

Test cases for some of these problems are in the new
circularity-errors.swift file, mostly to make sure the ObjC printer
doesn't crash when it encounters them.

rdar://problem/27109377
2016-08-09 10:41:09 -07:00
Jordan Rose
02d2517866 [PrintAsObjC] Hack: Assume all option sets have typedefs. (#3851)
...because otherwise option sets that get imported as members using
NS_SWIFT_NAME are printed with an 'enum' tag, and the definition of
NS_OPTIONS only declares the typedef under C++.

We should come back and figure out something more principled for this
later, but for now this solves an issue with generated headers
imported into C++ translation units.

rdar://problem/27130343
2016-08-03 13:03:50 -07:00
Mishal Shah
8d854d24b5 Merge pull request #3918 from jckarter/printasobjc-crash
PrintAsObjC: Handle new upper bounds of Array/Dictionary/Set correctly.
2016-08-02 15:14:38 -07:00
Doug Gregor
6575877124 [@objc checking] Only 'Error' is representable in Objective-C (as 'NSError').
Don't allow types conforming to 'Error' or protocol compositions
involving 'Error' to be reflected in Objective-C. We still allow
bridging conversions, but they are not statically bridged. Fixes
SR-2249/rdar://problem/27658940.
2016-08-02 10:13:54 -07:00
Joe Groff
cfcb6ea50e PrintAsObjC: Handle new upper bounds of Array/Dictionary/Set correctly.
We would crash because 'Any' doesn't have a corresponding bridged type through the normal bridging mechanism. Handle this correctly, and correctly recognize 'AnyHashable' and 'Any' as the upper bounds of Dictionary, Set, and Array so we present the unqualified NS types in the generated header.
2016-08-02 09:54:22 -07:00
Kevin Ballard
0473e99988 [PrintAsObjC] Add unavailable attribute to unavailable obj-c initializers (#3852)
* [PrintAsObjC] Add unavailable attribute to non-inherited initializers

Initializers that aren't inherited by subclasses cannot be called, so we
should make this visible to Obj-C.

Due to SR-2211, non-inherited convenience initializers do not get this
same treatment.

* [PrintAsObjC] Add unavailable initializers for private overrides

When a public initializer is overridden with a private one, we need to
mark these as unavailable to Obj-C as they're not supposed to be
callable even though they do exist.
2016-08-01 10:06:51 -07:00
Andrew Trick
a18d490d6a Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3773)
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`.

As proposed in SE-0107: UnsafeRawPointer.

`void*` imports as `UnsafeMutableRawPointer`.
`const void*` imports as `UnsafeRawPointer`.

Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer.

* Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer.

This requires explicit memory binding in several places,
particularly in NSData and CoreAudio.

* Fix a bunch of test cases for Void->Raw migration.

* qsort takes IUO values

* Bridge `Unsafe[Mutable]RawPointer as `void [const] *`.

* Parse #dsohandle as UnsafeMutableRawPointer

* Update a bunch of test cases for Void->Raw migration.

* Trivial fix for the SceneKit test case.

* Add an UnsafeRawPointer self initializer.

This is unfortunately necessary for assignment between types imported from C.

* Tiny simplification of the initializer.
2016-07-26 14:21:15 -07:00
Alsey Coleman Miller
a00fa74b8d [PrintAsObjC] Fix printing of 'Error' values as 'NSError *'.
Fixes SE-2159 / rdar://problem/27439384.
2016-07-26 11:50:40 -07:00
Andrew Trick
0ed9ee8dee Revert "Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3724)"
This reverts commit ece0951924.

This results in lldb failues on linux that I can't readily debug.
Backing out until they can be resolved.
2016-07-26 02:50:57 -07:00
Andrew Trick
ece0951924 Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3724)
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`.

As proposed in SE-0107: UnsafeRawPointer.

`void*` imports as `UnsafeMutableRawPointer`.
`const void*` imports as `UnsafeRawPointer`.

Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer.

* Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer.

This requires explicit memory binding in several places,
particularly in NSData and CoreAudio.

* Fix a bunch of test cases for Void->Raw migration.

* qsort takes IUO values

* Bridge `Unsafe[Mutable]RawPointer as `void [const] *`.

* Parse #dsohandle as UnsafeMutableRawPointer

* Update a bunch of test cases for Void->Raw migration.

* Trivial fix for the SceneKit test case.

* Add an UnsafeRawPointer self initializer.

This is unfortunately necessary for assignment between types imported from C.

* Tiny simplification of the initializer.
2016-07-26 02:18:21 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Jordan Rose
e837d88472 Revert "[ObjC Interop] Map Swift @objc properties named isFoo to ObjC Cocoa conventions" (#3254)
It sounds good on paper, but in practice we ended up breaking Core Data
projects (because people name their boolean properties 'isFoo' rather
than the Objective-C 'foo'), forcing an Objective-C-side change when
a mixed-source project upgrades to Swift 3, and causing collisions when
there are properties named both 'foo' and 'isFoo'. If people care about
their Swift boolean properties strictly following the Objective-C Cocoa
naming conventions, they'll have to specify them manually.

(We do have a bug to make it easier to rename the getter of a stored
property exposed to Objective-C: rdar://problem/21261564.)

This reverts commit 6fe6266c99.

rdar://problem/26847223
2016-07-01 10:22:46 -07:00
Jordan Rose
53118e9a5f Split the "Foreign" flag into a ForeignKind enum.
This flag tracks whether we have a special kind of imported class
that has limitations in what you can do with it. Currently it's
used for two things: CF classes, and the magic "Protocol" class used
to represent Objective-C protocol metadata. I'm planning to add a
third to handle classes with the recently-added objc_runtime_visible
attribute, which describes an Objective-C class whose runtime symbols
are hidden (forcibly preventing categories and subclassing). This is
used for some of the types in Dispatch, which has exposed some of the
classes that were considered implementation details on past OSs.

I'm splitting the flag into an enum rather than just marking the
Dispatch classes with the existing flag because we still need to
be able to /cast/ to the Dispatch types (which you can't do with CF
types today) and because they deserve better than to be lumped in
with CF for diagnostic purposes.

Groundwork for rdar://problem/26850367, which is that Swift will
happily let you extend the new Dispatch classes but then fails to find
the symbols at link-time.
2016-06-29 14:20:21 -07:00
David Farler
c9f5504797 Cascading Doc Comments: Look up class hierarchy when doc comments are missing
If a class member doesn't have a doc comment but a base class does, show
the base class's comment and add a note about where it came from.

rdar://problem/16512247
2016-06-27 11:55:31 -07:00
Jordan Rose
d83db6123a [PrintAsObjC] Put "copy" in the property decls for value types. (#3141)
Bridged value types are implicitly copied as part of bridging.

This isn't 100% correct because it doesn't handle bridged types
that /don't/ conform to NSCopying, but there aren't any of those
today and there probably shouldn't be.

rdar://problem/26917017
2016-06-22 13:28:26 -07:00
Jordan Rose
a679aca7c5 [PrintAsObjC] Fix class properties with generic parameters.
public static var myDictionary: [String: AnyObject]

becomes

    SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, copy)
      NSDictionary<NSString *, id> * _Nonnull myDictionary;)

and the commas in the ObjC generic break the macro, just like they do with
C++ templates. Fix this by making the macro variadic.

rdar://problem/26879147
2016-06-20 15:12:45 -07:00
Jordan Rose
e7fe0abeed Don't treat Swift methods named "init" as ObjC ARC init methods. (#2989)
Under ARC, methods in the "init" family are considered to have
NS_REPLACES_RECEIVER semantics ("consumes" self and returning a
value at +1). This is correct for Objective-C "init methods",
which are equivalent for Swift's initializers, but almost never
correct for any other methods that happen to start with the word
"init".

Note that Swift still follows all the other ARC conventions, so
if you name a method or property, say, "newItemController", the
value will be returned at +1. For methods this is probably
desirable, but for properties maybe not. We could do something
similar for property accessors to make sure they always have
the default "no method family" semantics in Objective-C.

rdar://problem/25759260
2016-06-20 14:09:53 -07:00
Jordan Rose
8643de9842 [PrintAsObjC] Respect private(set) on class properties.
rdar://problem/24564858
2016-06-09 18:25:15 -07:00
Jordan Rose
eeac42c13f Cache the Identifier for "_ObjectiveCType". NFC. 2016-05-26 17:55:18 -07:00
Jordan Rose
8547b545c9 [PrintAsObjC] Handle imported swift_newtype typedefs.
A typedef with the swift_newtype attribute is imported as a struct
wrapping the underlying type instead of just a typealias. If the
underlying type is a bridged type (like String), the newtype struct
is bridged as well. However, we don't want to use that type when
bridging back to Objective-C, because

(1) Objective-C header generation is done too late to fill out the
    _ObjectiveCBridgeable conformance for the type, so if it wasn't
    type-checked then the conformance won't have the original type
    in it.
(2) There's a perfectly good typedef we should be using anyway.

Just use the type as written in Objective-C (instead of crashing).

Finishes rdar://problem/26372925.
2016-05-25 12:03:28 -07:00
Jordan Rose
1bdd092cd9 [PrintAsObjC] Use C names when printing imported structs.
(and enums)

Previously this part of the compiler assumed that any imported
struct or enum would have the same name as it does in C, which is
no longer true.

Part of rdar://problem/26372925
2016-05-25 11:34:02 -07:00
Jordan Rose
135e9b99f1 [PrintAsObjC] Emit Xcode-7-compatible class properties.
There's not yet a released version of Apple Clang that supports
Objective-C class properties, so make sure the generated header
guards any uses of them with a __has_feature check, and provides
declarations of the accessor methods as well.

https://bugs.swift.org/browse/SR-1442
2016-05-09 14:13:57 -07:00
Jordan Rose
bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00
David Farler
a9297eed9f Rename llvm::markup namespace to swift::markup
This was naming was cargoed from long ago and this functionality isn't
directly related to LLVM, it's specific to Swift.
2016-04-10 13:46:25 -07:00
Jordan Rose
50e3b33739 [ClangImporter] Implement importing of ObjC class properties.
For the most part this was just "check isInstanceProperty"; the one feature not yet implemented
is the emission of ObjC metadata for class properties.

rdar://problem/16830785
2016-03-31 14:27:56 -07:00
Joe Groff
874dbd8de4 PrintAsObjC: Handle imported generic classes. 2016-03-28 09:50:30 -07:00
Stephen Canon
8187dfb140 Added uint[2,3,4] to simd, bridged to vector_uintN. 2016-03-24 07:56:59 -04:00
Doug Gregor
1322de08bc [PrintAsObjC] Use _ObjectiveCBridgeable conformances to print bridged types.
Rather than hardcoding String/Array/Dictionary/Set to print as
NSString/NSArray/NSDictionary/NSSet, use _ObjectiveCBridgeable
conformances to determine how to print them. Another step toward
generalized _ObjectiveCBridgeable, although the general form is not
yet useful.
2016-03-11 10:43:50 -08:00
Joe Groff
013aad13d4 Initial implementation of a @_cdecl attribute to export top-level functions to C.
There's an immediate need for this in the core libs, and we have most of the necessary pieces on hand to make it easy to implement. This is an unpolished initial implementation, with the following limitations, among others:

- It doesn't support bridging error conventions,
- It relies on ObjC interop,
- It doesn't check for symbol name collisions,
- It has an underscored name with required symbol name `@cdecl("symbol_name")`, awaiting official bikeshed painting.
2016-03-10 13:27:39 -08:00
Max Moiseev
7fe6916bf6 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-07 12:10:47 -08:00
Doug Gregor
e16d6898ef [PrintAsObjC] Sentence-case the enum case name when appending it to the enum name.
Otherwise, we cram a conventionally UpperCamelCase thing with a
newly-conventinally-lowerCamelCased thing together and destroy the
word boundaries. Fixes rdar://problem/24947695.
2016-03-04 15:42:22 -08:00
Max Moiseev
cf4bafe9e3 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-03 13:22:03 -08:00
Kevin Ballard
00ccc9ddc3 [PrintAsObjC] Print argument names for function/block types
Including the argument names helps code completion in Xcode.

Fixes SR-365.

Also fixes an issue where a property of a block/function type whose name
is a clang keyword would produce an invalid declaration, e.g.

    var `struct`: (Int -> Int)?

was printing as

    @property (nonatomic, copy, getter=struct, setter=setStruct:) NSInteger (^ _Nullable struct)(NSInteger)_;
2016-03-02 21:02:32 -08:00
Max Moiseev
bb3eaaf308 Merging in latest master 2016-02-24 15:10:25 -08:00
Doug Gregor
6fe6266c99 [ObjC Interop] Map Swift @objc properties named isFoo to ObjC Cocoa conventions
The Objective-C Cocoa convention eschew "is" on property names, but
use it on the getter, while the Swift API guidelines state that
Boolean properties should read as assertions (e.g., "isEmpty" rather
than "empty"). Map Swift properties named "isFoo" to Objective-C by
removing the "is" from the resulting Objective-C property name (so it
will be named "foo") and from the setter (which will have the
Objective-C selector "setFoo:") while retaining the "is" for the
getter selector ("isFoo").

Fixes rdar://problem/17090661.
2016-02-23 20:49:20 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
Kevin Ballard
4da629f1be [PrintAsObjC] Use @objc name for enums in enum references
Also make sure the `FooDomain` constant for `ErrorType` enums uses the
correct name.

Fixes SR-693: Custom named @objc enum still exposes original Swift name
in Objective-C
2016-02-09 13:10:47 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
practicalswift
6d0eee9b8c Remove unused variables. 2016-01-21 10:33:17 +01:00
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Doug Gregor
c4a6902589 Abstract the set of known Foundation entities into a .def-driven enum. NFC
Specifically, we don't want to hard-code the Swift names of these
Objective-C entities, because the importer renaming will affect them.
2016-01-17 23:40:14 -08:00