Commit Graph

723 Commits

Author SHA1 Message Date
Jordan Rose
2fa3da4089 Drop the notion of "alias" names for CF types.
Previously we imported a Core Foundation type "CCFooRef" as "CCFoo",
but also provided a typealias "CCFooRef". In Swift 3, we decided to
mark "CCFooRef" unavailable to force developers to consistently use
"CCFoo". Now that we have infrastructure to mark /all/ renamed
declarations as unavailable, just use that to track the renaming,
i.e. pretend that "CCFooRef" was the "Swift 2" name for the type.

This doesn't change the conflict resolution behavior: if there's
another name "CCFoo" in the same module, the CF type will be
imported as just "CCFooRef".

Groundwork cleanup for rdar://problem/26347297, which notes that our
import-as-member fix-its use the "Ref" names rather than the short
names.
2016-05-19 13:21:47 -07:00
Jordan Rose
26e1f5be27 [ClangImporter] Don't include Swift members in AnyObject lookup. (#2503)
More specifically, don't include declarations of methods and properties
in the list of "all imported Objective-C members" if said method or
property is in a generated header. (We actually key off of whether the
enclosing class, protocol, or category is marked as coming from Swift,
but since users aren't supposed to modify generated headers themselves
it's much the same thing.)

This previously caused a crash because we tried to import a Clang member
onto a Swift decl in order to provide the particular member on AnyObject.

rdar://problem/25955831 and probably also rdar://problem/25828987, which
deals with the fix-it to migrate to #selector. (We do an AnyObject-like
lookup to find out which class likely implements the specified selector.)
2016-05-12 16:58:31 -07:00
Michael Ilseman
a8cebdc5d3 [swift_newtype] Special handling for Notifications
We now specially import global decls who we identify as fitting the
notification pattern: extern NSStrings whose name ends in
"Notification". When we see them, we import them as a member of
NSNotificationName and, if NSNotificationName is swift_newtype-ed, we
use that new type.

Test cases included.
2016-05-08 19:17:32 -07:00
Doug Gregor
55a3f5398c [Clang importer] Import Swift 2 "stubs" to improve errors in "Swift 2" code.
When attempting to compile Swift 2 code (or any Swift code using the
Swift 2 names) in Swift 3, the compiler diagnostics are often entirely
useless because the names have changed radically enough that one
generally gets "no member named 'foo'" errors rather than a helpful
"'foo' was renamed to 'bar'" error. This makes for a very poor user
experience when (e.g.) trying to move Swift 2 code forward to Swift 3.

To improve the experience, when the Swift 2 and Swift 3 names of an
API differ, the Clang importer will produce a "stub" declaration that
matches the Swift 2 API. That stub will be marked with a synthesized
attribute

  @available(unavailable, renamed: "the-swift-3-name")

that enables better diagnostics (e.g., "'foo' is unavailable: renamed
to 'bar') along with Fix-Its (courtesy of @jrose-apple's recent work)
that fix the Swift 2 code to compile in Swift 3.

This change addresses much of rdar://problem/25309323 (concerning QoI
of Swift 2 code compiled with a Swift 3 compiler), but some cleanup
remains.
2016-05-06 21:12:20 -07:00
Doug Gregor
9f6734312d [Clang importer] Factor the checking for -enable-swift-newtype more sensibly. NFC 2016-05-06 16:45:13 -07:00
Doug Gregor
a2e03d556b Stage implementation of SE-0033 behind a flag, -enable-swift-newtype.
We're going to quarantine this feature behind a frontend flag for a
bit.
2016-04-25 18:03:04 -07:00
Doug Gregor
138bbe2167 [Clang importer] Delay resolution of import-as-member declarations when needed.
It's possible for swift_name to make a global declaration into a
member of another entity that has not been seen yet. In such cases,
delay resolution until the end of the translation unit (module). Fixes
the rest of rdar://problem/25502497.
2016-04-05 00:03:36 -07:00
Doug Gregor
398d216eca [Clang importer] Always pass an effective context to importDeclContextOf.
Some of the callers to importDeclContextOf could and should have
passed an effective context, but didn't, so (e.g.) an enum defined in
C couldn't be mapped to a nested type in Swift. Eliminate the
single-argument importDeclContextOf honeypot so we remember to pass
down the effective Clang context. Fixes rdar://problem/25502497.
2016-04-04 15:18:37 -07:00
Arnold Schwaighofer
7a129649bc Add a hidden flag to disable the swift bridge attribute
This will be used for writing performance tests
2016-03-29 14:12:45 -07:00
Chris Willmore
3bcb169f0b Import lightweight Objective-C generics as Swift generic type
parameters.
2016-03-28 09:50:30 -07:00
Michael Ilseman
47e73ad4f1 Merge branch 'master' of github.com:apple/swift into import-as-member 2016-03-23 17:01:47 -07:00
Michael Ilseman
d7581d1b36 Merge commit 'a31edf53d0580efe47f4e9ef89dccc4429c056e8' into import-as-member 2016-03-23 13:05:57 -07:00
Doug Gregor
593932741c Remove historical flags -enable-omit-needless-words/-enable-infer-default-arguments/-enable-swift-name-lookup-tables.
NFC; all of these options are always-on now and we no longer have a
way to turn them off.
2016-03-22 17:04:19 -07:00
Doug Gregor
0e40128d1a [Clang importer] Use Clang swift_bridge attribute for bridging.
When an Objective-C class type is annotated with the swift_bridge
value type, bridge it to the named type. Use API notes on Foundation
rather than special cases.

For Objective-C classes where bridging was baked into the Clang
importer (NSString, NSArray, NSDictionary, NSSet), add API notes to
put the appropriate swift_bridge attribute on these Objective-C
classes.

Note: requires Clang update.
2016-03-10 23:10:28 -08:00
Michael Ilseman
1b83dee698 [Clang importer] Inferr import as member flags
Plumbing for -enable-infer-import-as-member options and flags.Tests
coming soon.
2016-03-06 23:54:56 -08:00
Doug Gregor
47b2be3138 [Clang importer] Import a getter or getter/setter pair as a property.
When a global function is determined to be a lone getter or a
getter/setter pair, import it as a property. Handle global, instance,
and static properties.

As part of this, generalize importAsMethod to also handle static
methods and map pointer-to-non-const arguments to 'mutating' methods.
2016-03-06 21:25:43 -08:00
Doug Gregor
af3769773e [Clang importer] Fix the use-after-free with enum info harder.
This is more of rdar://problem/24869070; StringRefs are leaking out of
importFullName still, in a DenseMap that can get deallocated. When we
cache this data, put them in "permanent" (Swift ASTContext) storage.
2016-03-04 14:37:03 -08:00
Doug Gregor
b2eafa4c85 [Clang importer] Fix a use-after-free in enum info caching.
Also, make the cache kick in between when a module is built and when
it is used. This isn't actually an important case, but it makes it a
whole lot more common to actually see the use-after-free break things.

Fixes rdar://problem/24869070.
2016-03-03 22:08:50 -08:00
Michael Ilseman
e452b38119 [Clang Importer] Import as methods
Print module functionality hooked up to import global functions a
methods, if dictated by the swift_name attribute. Test case included.

No SILGen support yet.
2016-03-03 21:58:57 -08:00
Michael Ilseman
98e1d2d1ae [Clang Importer] cleanup import-as-init code
NFC
2016-03-03 18:13:53 -08:00
Doug Gregor
08c8155d6c [Clang importer] Eliminate importGlobalsAsMembers. NFC 2016-03-03 17:25:58 -08:00
Doug Gregor
42f0356aec [Clang importer] Wire up extensions for globals-as-members.
Wire up the extensions created when importing globals-as-members via
the same mechanisms we use for Objective-C categories, e.g.,
implementing loadAllExtensions() to find the extensions and lazily
loading their members via the member loader. Addresses most of my
comments on the way extensions were wired in.

Extend our testing for importing globals as members to two different
submodules, so we can see the separation of extensions.

As part of this, fold getOrCreateExtension into importDeclContextOf.
2016-03-03 17:22:47 -08:00
Michael Ilseman
678c42490c [Clang Importer] import as member: Refactor out shared functionality
NFC
2016-03-03 16:23:42 -08:00
Michael Ilseman
e32353ec03 [Clang Importer] getOrCreateExtensionPoint for import as member
When importing as a member, we want a single unique extension
declaration per type per Clang submodule. This adds the mapping,
switches import-as-member VarDecl importing to use it, and forces the
created extensions to show up.
2016-03-02 23:30:17 -08:00
Michael Ilseman
c247bbf5b6 [Clang Importer] Import as member: bare bones variables.
Adds tentative and very basic functionality for importing global
variables as members of structs and classes. Test case added
2016-03-02 22:19:37 -08:00
Doug Gregor
3ffbe020d7 [Clang importer] Handle name mapping for "getter:" and "setter:" in swift_name.
The swift_name string format now supports "getter:" and "setter:"
prefixes to indicate that a function is the getter or setter of a
Swift-synthesized property. Start parsing these DeclNames and make
sure they're reflected in the Swift name lookup tables.

[Clang update required]
2016-03-01 15:33:21 -08:00
practicalswift
b4be924c64 [gardening] Fix recently introduced typo: "contet" → "context" 2016-03-01 11:28:42 +01:00
Doug Gregor
940a65a994 [Clang importer] Make the "effective context" a core part of ImportedName.
Previously, the "effective context" parameter to importFullName was
used only during the construction of Swift name lookup tables, so we
can associate each declaration with a context. Expand the role of
"effective context" so it is always a part of ImportedName and is also
used by importDecl when actually importing the enum declaration.

This is partially a cleanup, and partially staging for SE-0033, which
will require this functionality more broadly.
2016-02-29 15:32:34 -08:00
Andrew Trick
ff02652108 Move enums into AttrKind.h.
This reorganization allows adding attributes that refer to types.
I need this for a @_specialize attribute with a type list.

PrintOptions.h and other headers depend on these enums. But Attr.h
defines a lot of classes that almost never need to be included.
2016-02-26 21:10:22 -08:00
Doug Gregor
7d2273fe57 [Omit needless words] Don't infer empty option sets for setters.
Fixes rdar://problem/24618895.
2016-02-22 14:11:09 -08:00
Doug Gregor
c1cc39accc [Clang importer] Rely on Clang's mapping of API notes to attributes directly.
Rather than managing API notes directly in the Clang importer, tell
Clang where to find the API notes files and let it map them directly
to attributes. Then, use the existing attribute-mapping
mechanisms---with minor extensions for cases where Clang previously
didn't map some Swift-specific API notes---to get at that information.

There is a minor regression where we can no longer apply API notes to
declarations that don't have a specific Clang declaration. The
specific example (NSError's inherited init()) comes from
rdar://problem/21042412; we've regressed here, but the correct fix is
(and has always been) on the Objective-C side
(rdar://problem/19977891).

Finishes rdar://problem/24447420.
2016-02-18 16:48:02 -08:00
Doug Gregor
ca736fd27c [Omit needless words] Ensure Swift name consistency for overriding/overridden decalrations.
When we transform an Objective-C method or property name, we take into
account the parameter types, result type, and type of the enclosing
context. Doing so means that one can get different Swift names for a
particular method (or property) and one that overrides it, which leads
to dangerous inconsistencies in the Swift names. Address this
limitation by using the original declaration's name (i.e., one that
does not override any other).

It is possible that there is more than one "original declaration",
when the same method/property name comes from different
protocols. When this is the case, check for consistency and complain
if there are inconsistencies.

Amusingly, this changes exactly one method in Cocoa
(UINavigationController's showViewController:sender:), with no conflicts
detected. Fixes rdar://problem/24558337.
2016-02-17 23:35:41 -08:00
practicalswift
b1685652f3 [gardening] Fix recently introduced typo: "perfom" → "perform" 2016-02-11 19:13:10 +01:00
Doug Gregor
8aaf014de6 [Clang importer] Cache enum information based on modulename.enumname.
Caching information based on Clang declaration pointers, when that
information is used via importFullName, is unsafe because the
declaration pointers may point into Clang AST contexts built
temporarily built for modules, and therefore go stale. I believe this
is the cause of the recent ASan failures.

Again, the whole passing-through-a-different-Clang-Sema thing with
importFullName is an architectural fail that requires refactoring, so
we don't keep falling into this trap.
2016-02-10 13:32:38 -08:00
Doug Gregor
1eb1adac6b [Clang importer] Remove unused DenseMap. NFC 2016-02-10 13:32:38 -08:00
Michael Ilseman
283bfefb14 Merge pull request #1196 from milseman/error_enum
[Clang Importer] Import ns_error_domain attribute with _BridgedNSError
2016-02-04 20:45:29 -08:00
Michael Ilseman
c8be2873e8 [Clang Importer] Refactor information pertaining to enums out
Introduces new class, EnumInfo, just for the implementation of the
ClangImporter to encapsulate various computed information about the
enum we're importing.

This helps refactor some functionality, aids clarity, and also
prevents us from repeating calculations multiple times, as we were
doing with classifyEnum's macro-expansion tracking. Provides a base
where we can add more heavy lifting in classifyEnum in the future.
2016-02-04 16:57:22 -08:00
Doug Gregor
319e2a7462 [Clang importer] Strip "NS" prefix from macro names, too 2016-02-03 21:03:09 -08:00
Doug Gregor
8e000fb738 [Clang importer] Factor mapping of macro names into a single location. NFC 2016-02-03 16:11:59 -08:00
practicalswift
a3f857ca7b [gardening] Add "-*- C++ -*-" to header files currently missing it 2016-01-23 11:53:05 +01:00
Doug Gregor
312a7f0aea [Clang importer] Strip the "NS" prefix from entities in Foundation.
As part of the improved import of Objective-C APIs into Swift, strip
the "NS" prefix from entities defined in the Foundation
framework. Addresses rdar://problem/24050011, which is part of
SE-0005. Naturally, this is hidden behind -enable-omit-needless-words.
2016-01-20 15:02:54 -08:00
Doug Gregor
72aabcb6f3 Revert "[Clang importer] Strip the "NS" prefix from entities in Foundation"
This reverts commit e51e969b35. This is
a temporary revert to aid in merging to the swift-3-api-guidelines branch.
2016-01-20 13:26:05 -08:00
Doug Gregor
679ee11898 [Clang importer] Eliminate retain cycle involving the Clang importer implementation.
The Clang importer implementation held on to a Clang instance, which
held a list of module file extensions, which included the Clang
importer implementation... break the cycle by separating out the
module file extension from the Clang importer. Fixes SR-562 /
rdar://problem/24225173.
2016-01-20 11:50:55 -08:00
Doug Gregor
e51e969b35 [Clang importer] Strip the "NS" prefix from entities in Foundation
As part of the improved import of Objective-C APIs into Swift, strip
the "NS" prefix from entities defined in the Foundation
framework. Addresses rdar://problem/24050011, which is part of
SE-0005. Naturally, this is hidden behind -enable-omit-needless-words.
2016-01-17 23:40:14 -08:00
Doug Gregor
0b6cedd961 [Clang importer] Infer default argument [:] for some NSDictionary parameters.
Both option set (CF_OPTIONS/NS_OPTIONS) and NSDictionary parameters
tend to be used as option sets. The former already get a default
argument of []. This commit adds a default argument of [:] for the
latter, identified by a parameter whose argument label involves
"options", "attributes", or "userInfo".
2016-01-06 14:08:11 -08:00
Doug Gregor
5aa40dd0aa Extend DefaultArgumentKind with cases for nil, [], and [:].
Under -enable-infer-default-arguments, the Clang importer infers some
default arguments for imported declarations. Rather than jumping
through awful hoops to make sure that we create default argument
generators (which will likely imply eager type checking), simply
handle these cases as callee-side expansions.

This makes -enable-infer-default-arguments usable, fixing
rdar://problem/24049927.
2016-01-06 10:19:12 -08:00
Chris Lattner
19bb1e147b move a & from a caller to the callee to appease Jordan. 2016-01-05 21:25:40 -08:00
practicalswift
ca92efc8e6 Use consistent formatting of header comments.
Correct format:
```
//===--- Name of file - Description ----------------------------*- Lang -*-===//
```

Notes:
* Comment line should be exactly 80 chars.
* Padding: Pad with dashes after "Description" to reach 80 chars.
* "Name of file", "Description" and "Lang" are all optional.
* In case of missing "Lang": drop the "-*-" markers.
* In case of missing space: drop one, two or three dashes before "Name of file".
2016-01-04 23:00:53 +01:00
Chris Lattner
a30ae2bf55 Merge pull request #836 from zachpanz88/new-year
Update copyright date
2015-12-31 19:36:14 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -08:00