Commit Graph

1086 Commits

Author SHA1 Message Date
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
John McCall
afdda3d107 Implement SE-0117.
One minor revision: this lifts the proposed restriction against
overriding a non-open method with an open one.  On reflection,
that was inconsistent with the existing rule permitting non-public
methods to be overridden with public ones.  The restriction on
subclassing a non-open class with an open class remains, and is
in fact consistent with the existing access rule.
2016-08-02 07:46:38 -07:00
Joe Groff
10a0eb1c13 Merge pull request #3710 from jckarter/turn-on-id-as-any
Turn on id as any
2016-07-25 18:58:27 -07:00
Doug Gregor
8c105862ce Merge pull request #3746 from DougGregor/arg-labels-from-exprs
Refactoring toward SE-0111
2016-07-25 15:25:30 -07:00
Doug Gregor
fdcf45b497 [AST] Introduce factory methods to create CallExprs.
Introduce several new factory methods to create CallExprs, and hide
the constructor. The primary reason for this refactor is to start
moving clients over to the factory method that takes the call
arguments separately from the argument labels. Internally, it
repackages those arguments into a TupleExpr or ParenExpr (as
appropriate) so the result ASTs are the same. However, this will make
it easier for us to tease out the arguments themselves in the
implementation of SE-0111.
2016-07-25 13:27:35 -07:00
Ayaka Nonaka
a8f3eb8f66 Import @compatibility_alias as typealias
Objective-C’s @compatibility_alias compiler directive is imported as
a Swift typealias.
2016-07-25 12:28:44 -07:00
Joe Groff
93d2d299fd ClangImporter: Fix handling of 'id' typedefs to drop the typedef.
The code added here to introduce an unavailable typealias accidentally returned the typealias, causing us to import all 'id's as Any even in unbridgeable positions, using the 'id' alias as their sugar name instead of 'Any'.
2016-07-25 06:01:21 -07:00
Slava Pestov
ddc51c5917 AST: Implement SE-0102, introducing new semantics for Never alongside @noreturn
No migrator support yet, and the code for @noreturn is still in
place.
2016-07-22 14:56:39 -07:00
Joe
67dccb283e [SE-0095] Code feedback changes; Any is parsed as a keyword
- Any is made into a keyword which is always resolved into a TypeExpr,
allowing the removal of the type system code to find TheAnyType before
an unconstrained lookup.
- Types called `Any` can be declared, they are looked up as any other
identifier is
- Renaming/redefining behaviour of source loc methods on
ProtocolCompositionTypeRepr. Added a createEmptyComposition static
method too.
- Code highlighting treats Any as a type
- simplifyTypeExpr also does not rely on source to get operator name.
- Any is now handled properly in canParseType() which was causing
generic param lists containing ‘Any’ to fail
- The import objc id as Any work has been relying on getting a decl for
the Any type. I fix up the clang importer to use Context.TheAnyType
(instead of getAnyDecl()->getDeclaredType()). When importing the id
typedef, we create a typealias to Any and declare it unavaliable.
2016-07-19 12:01:37 -07:00
Michael Ilseman
d1e0f517f3 Merge pull request #3453 from milseman/id_as_any
[id as Any] Import with the proper Any type
2016-07-12 13:15:24 -07:00
Devin Coughlin
4b566df9f3 [ClangImporter] Update AvailAttr import for "macosx" to "macos" rename.
This resolves a number of failing tests caused by availability attributes
not getting imported for macOS.
2016-07-12 12:41:42 -07:00
Doug Gregor
b86b8126a7 [SE-0112] Import an Objective-C error enum as a struct wrapping NSError.
A given Objective-C error enum, which is effectively an NS_ENUM that
specifies its corresponding error domain, will now be mapped to an
ErrorProtocol-conforming struct that wraps an NSError, much like
NSCocoaError does. The actual enum is mapped to a nested "Code"
enum. For example, CoreLocation's CLError becomes:

  struct CLError : ErrorProtocol {
    let _nsError: NSError
    // ...
   @objc enum Code : Int {
     case ...
   }
  }

This implements bullet (2) in the proposed solution of SE-0112, so
that Cocoa error types are mapped into structures that maintain the
underlying NSError to allow more information to be extracted from it.
2016-07-12 10:53:52 -07:00
Michael Ilseman
aa7b206fab [id as Any] Import with the proper Any type
Changes how we import from just protocol<> to the proper Any
type. This means that uses of AnyObject will either implicitly cast up
to Any, or must be explicitly cast through a temporary if used as an
lvalue. id as Any is still predicated on the id-as-any flags.
2016-07-08 16:17:34 -07:00
Mishal Shah
23b646eed2 Update master to build with Xcode 8 beta 2, OS X 10.12, iOS 10, tvOS 10, and watchOS 3 SDKs 2016-07-06 10:48:45 -07:00
Doug Gregor
71882c6516 [API Notes] Use API notes rather than overlays to make error enums bridge.
The Clang attribute allows one to state that a particular enumeration
type describes an error, and associates it with a particular domain
constant. However, due to lack of API notes support, this attribute
wasn't actually getting used. Instead, we had a number of explicit
extensions to enum types to make them conform to the _BridgedNSError
protocol explicitly.

Now that we have API notes, use them to make these enums into error
enums with the appropriate domain, so that the Clang importer will
synthesize the _BridgedNSError conformances. Then, remove all of the
explicit conformances---and with them, the overlays for 12 frameworks.

There is a small fix to more eagerly consider these conformances as
"used" if an expression is formed with the error enum as a value
type. This better ensures that the conformances will be available at
runtime when needed.

This cleanup is needed to implement SE-0112 (NSError bridging),
although it is useful by itself.
2016-07-06 09:54:59 -07:00
Joe Groff
8c2631a37f Merge pull request #3294 from jckarter/id-as-any-import
Initial 'id as Any' Clang importer implementation
2016-07-05 16:03:35 -07:00
Joe Groff
231db9d108 Clang importer: Import ObjC 'id' as Swift 'Any' (behind a flag).
For experimentation purposes, add a flag to cause the Clang importer to map ObjC APIs using `id` to `Any` instead of `AnyObject`.
2016-07-05 12:29:45 -07:00
Slava Pestov
5a902935e8 Sema: Explicitly set interface type on all AbstractFunctionDecls
Previously getInterfaceType() would punt to getType() if no
interface type was set. This patch changes getInterfaceType()
to assert if no interface type is set, and updates various
places to set the interface type explicitly.

This brings us a step closer to removing PolymorphicFunctionType.
2016-07-05 00:24:28 -07:00
Slava Pestov
584eda38ed ClangImporter: More principled use of mapType{Into,OutOf}Context()
Before we would construct types containing a mix of interface and
contextual types, and then map them in and out. Straighten this out.

Note that I've also had to start untangling the issue where
synthesized ParamDecls do not have an interface type.
2016-07-02 05:36:35 -07:00
Slava Pestov
680688cbf5 AST: Cleanups for TypeBase::getSuperclass()
First, enforce that the superclass of a class is an interface type.
Previously, Swift classes used interface types but imported
Objective-C generics used archetypes.

When the superclass type is always an interface type, we
can use the recently-added gatherAllSubstitutions() instead of
rolling our own parent type walk.

Also, this exposed an issue in name lookup where we would call
getSuperclass() on a type whose parent was an unbound generic.
This doesn't make sense, so generalize the existing check there.
2016-07-02 05:35:16 -07:00
Slava Pestov
409af27eb3 Sema: Use DeclContext::getSelfInterfaceType() and DeclContext::getSelfTypeInContext() more, NFC
There are many places where we do the 'if inside a protocol, get the
Self type parameter, otherwise, use the declared type' dance.
We actually have really handy utility methods that encapsulate this,
so let's use them more.
2016-07-02 05:35:15 -07:00
Jordan Rose
3b6e40c030 Use ClassDecl::ForeignKind to model Clang's objc_runtime_visible.
We're now correctly checking for inheritance, adding @objc methods,
and adding @objc protocols for both CF types and objc_runtime_visible
classes (those without visible symbols). The latter is used for some
of the types in Dispatch, which has exposed some of the classes that
were considered implementation details on past OSs.

We still don't properly implement using 'as?' to check conformance to
a Swift protocol for a CF or objc_runtime_visible type, but we can do
that later.

rdar://problem/26850367
2016-06-30 11:20:58 -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
Mishal Shah
87b7bcfd3e Update master to build with Xcode 8 beta 1, OS X 10.12, iOS 10, tvOS 10, and watchOS 3 SDKs. 2016-06-14 14:53:55 -07:00
Trent Nadeau
78a420d850 Made use of @warn_unused_result a warning with a fixit (#2760)
https://bugs.swift.org/browse/SR-1052
2016-06-08 10:10:21 -07:00
Doug Gregor
ee85891d11 Revert "[Type checker] Be more rigorous about extracting argument labels from calls."
This reverts commit 3753d779bc. It's
causing some type-inference problems I need to investigate.
2016-06-03 10:21:27 -07:00
Doug Gregor
3753d779bc [Type checker] Be more rigorous about extracting argument labels from calls.
Whenever we have a call, retrieve the argument labels from the
argument structurally and associate them with the callee. We were
previously doing this as a separate AST walk (which was unnecessary),
so fold that into constraint generation for a CallExpr. We were also
allowing weird ASTs to effectively disable this information: tighten
that up and require that CallExprs always have a ParenExpr, TupleExpr,
or (as a temporary hack) a TypeExpr whose representation is a
TupleTypeRepr as their argument prior to type checking. This gives us
a more sane AST to work with, and guarantees that we aren't losing
label information.

From the user perspective, this should be NFC, because it's mostly AST
cleanup and staging.
2016-06-02 17:15:51 -07:00
Slava Pestov
0d3ef548e7 ClangImporter: Add support for __attribute__((availability(..., replacement="")))
We map clang::AvailabilityAttr::getReplacement() to
swift::AvailableAttr::Rename, transforming the replacement
name using by looking up the named Clang replacement, and
importing its name.

Fixes <rdar://problem/26301742>.
2016-06-01 23:12:48 -07:00
Slava Pestov
112511cd1c Remember to set AbstractFunctionDecl's generic signature in a few places
I originally added this so that we would keep the signature around
even if type checking failed, and the function was given an
ErrorType.

Add a formal check to the AST verifier for this, and set the signature
in a few places where it wasn't being set.

Note that since we only serialize valid declarations, we don't have
to serialize a reference to the generic signature separately, but we
do have to remember to set it when deserializing, which wasn't being
done for destructors.
2016-05-28 22:30:40 -07:00
Michael Ilseman
d79546a3d7 Merge pull request #2751 from milseman/newtype_fixit
[Import as Member] Print full context in fixit
2016-05-27 13:38:12 -07:00
Michael Ilseman
22ec60007a [Import as Member] Print full context in fixit
Due to swift_name and swift_newtype, we are frequently importing onto
different contexts. This was confusing the fixit logic for unavailable
swift2 names, as we were trying to use Clang names when the Swift name
might be totally different (and even a nested type). This change has a
two-fold effect:

1) Globals who are imported onto swift_newtype-ed typedefs should be
   considered ImportAsMember.
2) When printing out the name of an ImportAsMember Swift 3 decl, we
   need to print out a fully qualified context, which also uses the
   Swift names, not the Clang names.
2016-05-27 11:39:19 -07:00
Joe Groff
b47b16850b Merge pull request #2731 from jckarter/objc-generic-protocol-conformance
Clang importer: Fix the 'self' type of imported ObjC generic members.
2016-05-26 19:11:05 -07:00
Joe Groff
615cfd4a10 Clang importer: Fix the 'self' type of imported ObjC generic members.
A variable was called `selfInterfaceType` even though it was inconsistently set to a contextual Self archetype for protocols, and an interface type for nominal types, and the one place we used the type expected to work with contextual types. Fixes rdar://problem/26396895.
2016-05-26 16:34:56 -07:00
Jordan Rose
239b5e02a6 [ClangImporter] Import NS_OPTIONS 0 values with explicit Swift-names.
This shows that the owner of the option set has thought about the
zero case, and wants it to be available in Swift, rather than disabling
it with the default "use [] to construct an empty option set" message.

rdar://problem/26290766
2016-05-26 11:08:24 -07:00
Joe Groff
9d86ca26df Merge pull request #2686 from jckarter/generic-anyobject-chaining-properties-subscripts
SILGen: Fix crashes when conditionally looking up generic subscripts and properties via AnyObject.
2016-05-25 09:46:12 -07:00
Slava Pestov
19f72c8593 ClangImporter: Fix diagnostics for NS* prefix stripping from generic types
We were failing to create an unavailable TypeAlias for the old name
in the case the renamed type was generic, leading to poor diagnostics.

Also, Sema resolves generic TypeAliases very early, while building
a Type from a TypeRepr -- this means the unavailable/deprecated
check runs too late to catch generic TypeAlises.

Add a hack where we preserve a reference to the original TypeAliasDecl
by way of constructing a SubstitutedType which desugars to the
replacement type, rather than resolving the replacement type
directly, so that the availability check can pick it up.

A better fix for this would be to introduce a BoundGenericAliasType
sugared type, but that's a bigger change that can come later.

Fixes <rdar://problem/26206263>.
2016-05-25 00:32:11 -07:00
Doug Gregor
abc42b459e [Clang importer] Allow a CF type spelled with "void *".
There wasn't any justification for the "const" restriction. Fixes
rdar://problem/24821660.
2016-05-24 22:39:20 -07:00
Joe Groff
5e2b20d05f SILGen: Fix crashes when conditionally looking up generic subscripts and properties via AnyObject.
The fix for methods to lower the dynamic method type from the substituted AST type of the expression also needed to be applied to the optional chaining, subscript, and property paths.

This also exposed a problem in the Clang importer, where imported subscript accessors would get the unbound generic context type as their Self parameter type instead of the type with the correct generic parameters. Fix this by renaming the all-too-convenient ParamDecl::createSelf factory to `createUnboundSelf`, and introduce a new `createSelf` that uses the bound generic type.

Fixes rdar://problem/26447758.
2016-05-24 19:18:37 -07:00
Michael Ilseman
bd3e590925 [Clang importer] Comments and cleanup. NFC 2016-05-24 10:12:08 -07:00
Michael Ilseman
134f41ccae [Clang importer] Use OptionSets for makeStructRawValued
NFC
2016-05-23 16:21:17 -07:00
Michael Ilseman
e0414a457f [swift_newtype] Structs have label-less init
Adds an unlabeled rawValue init for swift_newtype(struct), which
expresses the extensibility theme better.  This makes the use and
creation of new instances of that type more succinct.
swift_newtype(enum) still requires the explicit label, as it is
non-extensible.
2016-05-23 14:40:41 -07:00
Slava Pestov
170992c39f AST: Add Throws flag and ThrowsLoc to AbstractFunctionDecl
The verifier now asserts that Throws, ThrowsLoc and isBodyThrowing()
match up.

Also, add /*Label=*/ comments where necessary to make the long argument
lists easier to read, and cleaned up some inconsistent naming conventions.

I caught a case where ClangImporter where we were passing in a loc as
StaticLoc instead of FuncLoc, but probably this didn't affect anything.
2016-05-21 12:51:50 -07:00
Doug Gregor
1255a5cdb9 [Clang importer] Make sure an unavailable superclass init doesn't override an available import-as-member-init.
More generally, an unavailable initializer shouldn't stomp on an
available initializer, because it's possible that (for example) a
designated initializer will be unavailable but a factory initializer
will be available, so one still construct objects of that type.
Fixes rdar://problem/26238032.
2016-05-20 09:39:57 -07:00
David Farler
ee4d9d36aa [QoI] Don't skip over protocols when importing Swift 2 type aliases
Protocols have the implicit Self generic parameter, which was
preventing this type alias from getting created, so some renamed
imported protocols for Swift 3 weren't emitting the expected
diagnostics and fix-its.

rdar://problem/26304496
2016-05-20 09:18:09 -07:00
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
Joe Groff
7154c6167b Stray punctuation, NFC 2016-05-16 13:11:49 -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
Doug Gregor
dfc8d5a5f8 [Sema/SILGen] Track per-source-file "used" conformances to reference in SILGen
Sema was dutifully tracking conformances that were "used" as part of
type checking, so it could make sure that those conformances got
completed for SILGen to use. However, this information never actually
made it to SILGen, which included its own (more conservative, not
broad enough) heuristics for finding "used" conformances. Teach Sema
to record conformances within the appropriate source file, and have
SILGen reference the conformances when it emits SIL for the source
file.
2016-05-09 20:43:12 -07:00
Michael Ilseman
c62eb2e5aa [swift_newtype] Handle un-audited CF types
Special case logic for CF types, which may be coming in as
unmanaged. In this case, we will use audit information if present to
import with the new type, otherwise we have to fall back to
Unmanaged<CF...>.

We still import global variables that must be unmanaged onto the new
type, though they keep their unmanaged types. This helps to
consolidate the definitions, as well as make future migration easier
if they get audited.

Test cases included.
2016-05-09 14:26:26 -07:00
Michael Ilseman
b5d29b54d2 [swift_newtype] Also newtype up CF Ref types.
Code cleaup and generalization to also catch typedefs of CF reference
types. Tests will be included in a subsequent commit, as this now
exhibits a latent problem when interacting with Unmanaged.
2016-05-09 11:48:18 -07:00