Commit Graph

1426 Commits

Author SHA1 Message Date
Jordan Rose
5e9b5472c9 Add Parse + Sema support for '@_frozen'
(currently spelled with an underscore to indicate its WIP state)

Later commits will handle imported enums correctly and implement the
checks for switch cases.
2018-03-20 10:39:01 -07:00
Jordan Rose
8a66d998fa Decide if a class inherits convenience inits alongside implicit inits
We have a predicate in ClassDecl, 'inheritsSuperclassInitializers',
that is used in a few places to decide if we need to do lookups into a
superclass to find all relevant initializers. That's useful, but the
actual work being computed in that function is almost identical to the
work done in figuring out whether the class has provided all its
superclass's /required/ initializers, which is part of the type
checker operation 'resolveImplicitConstructors'. Furthermore,
'inheritsSuperclassInitializers' is /already/ calling
'resolveImplicitConstructors' because those implicit constructors
might affect the result.

Simplify this whole mess and prevent further inconsistencies like the
previous commit by just making 'resolveImplicitConstructors' decide
whether superclass convenience initializers are inherited. It does
make that function more complicated, but with the benefit of not
having duplication anymore.

No intended user-visible change, except that this bit is now
serialized instead of being recomputed, which means the module format
changed.
2018-03-19 18:28:41 -07:00
Mark Lacey
85f25003ce Replace uses of getOptionalObjectType(bool &).
Instead, use the one that doesn't have a reference parameter.
2018-03-16 21:19:49 -07:00
Slava Pestov
34fd4ae512 AST: Use DeclBaseName::Kind::Constructor
Fixes <rdar://problem/35852727>, <https://bugs.swift.org/browse/SR-1660>,
<https://bugs.swift.org/browse/SR-6557>.
2018-03-16 00:25:56 -07:00
Jordan Rose
520d6b9b91 Make NS_TYPED_ENUMS ObjectiveCBridgeable when they wrap an object (#15270)
This allows them to be used in generic arguments for NSArray et al.
We already do this for the ones that wrap bridged values (like
NSString/String), but failed to do it for objects that /weren't/
bridged to Swift values (class instances and protocol compositions),
or for Error-which-is-special.

In addition to this being a sensible thing to do, /not/ doing this led
to IRGen getting very confused (i.e. crashing) when we imported a
Objective-C protocol that actually used an NS_TYPED_ENUM in this way.

(We actually shouldn't be using Swift's IRGen logic to emit protocol
descriptors for imported protocols at all, because it's possible we
weren't able to import all the requirements. But that's a separate
issue.)

https://bugs.swift.org/browse/SR-6844
2018-03-15 16:17:38 -07:00
Slava Pestov
93d271900c ClangImporter: Don't import initializers as functions named 'init' 2018-03-14 20:18:52 -07:00
Jordan Rose
342743a0a5 [ClangImporter] Don't import C structs with non-trivial fields (#15209)
Swift currently assumes that copies and deinitializations of C structs
are always "trivial", i.e. a copy can be done by just copying the raw
bits of the value, and a deinit requires no work at all. Until we do
anything to change that, just don't import any of the new C structs
that have non-trivial copy or deinitialization operations (those with
ARC references in them).

rdar://problem/35698831
2018-03-14 17:56:44 -07:00
Huon Wilson
b330c6078f Merge pull request #14948 from huonw/at-owned
Make __owned functional, maybe
2018-03-08 13:18:19 +11:00
Huon Wilson
b1115af1eb Switch a pile of VarDecl::Specifier::Owneds to ::Default.
A lot of these probably should be Owned, but deciding that is work for
another time.
2018-03-08 12:36:37 +11:00
Jordan Rose
e202e90294 Clean up _BridgedNSError and _BridgedStoredNSError (#14682)
Despite their similar names and uses, these protocols no longer share
much functionality - the former is used to take @objc enums defined in
Swift that conform to Error and expose them as NSErrors, and the
latter handles NS_ERROR_ENUM C enums, which get imported into Swift as
a wrapper around NSError. We can actually simplify them quite a bit.

- Eliminate base protocol __BridgedNSError, which no longer provides
  any implementation for _BridgedStoredNSError.

- Eliminate default implementations that match what the compiler would
  synthesize.

- Adopt recursive constraints and where-clauses on associated types
  (and update the Clang importer to handle this).

- Collapse signed and unsigned default implementations when reasonable.

- Fold _BridgedStoredNSError's _nsErrorDomain into the existing public
  requirement CustomNSError.errorDomain.

rdar://problem/35230080
2018-03-07 13:14:36 -08:00
Huon Wilson
b94c5364f5 [NFC] Rename 'Ownership' to 'ReferenceOwnership'.
There's really two forms of ownership: references and values. Renaming
to make way for better distinguishing of the two.
2018-03-02 11:38:28 -08:00
Graydon Hoare
819275945d [Stats] Replace RecursiveSharedTimer with FrontendStatsTracer. 2018-02-21 14:49:24 -08:00
Graydon Hoare
76b82accbc [Stats] Simplify FrontendStatsTracer uses and formatter-definitions. 2018-02-21 14:49:24 -08:00
Graydon Hoare
6f46362f38 [Stats] Improve stats tracing readability a little. 2018-02-21 14:49:24 -08:00
Jordan Rose
fbe65e2bc5 [ClangImporter] Import objc_subclassing_restricted classes as 'public' (#14766)
(rather than 'open')

This attribute was originally introduced for Swift classes exposed to
Objective-C, but if it /is/ used on a pure Objective-C class Swift
should respect it. There might be an actual reason, such as a hot code
path in ContrivedExampleKit checking against the class object's
address instead of using -isKindOfClass:.

For source compatibility, only enforce this in Swift 5 mode.

rdar://problem/33971529
2018-02-21 10:14:42 -08:00
Mark Lacey
d63bb3fc53 Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with
either nullability or failability.
2018-02-10 16:24:09 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Jordan Rose
c06cc016f9 [ClangImporter] Fix handling of bitfields in unions (#14412)
Previously this caused an assertion failure in +Asserts builds
and incorrect behavior in -Asserts builds.

rdar://problem/37242238
2018-02-05 18:12:58 -08:00
Mark Lacey
f08823757a IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T>.
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.

Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.

Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works

Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
  rather than Decl.

Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.

There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.

There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
  rare circumstances be inferred differently. This shows up in
  test/ClangImporter/objc_parse.swift, where we have
    var optStr = obj.nsstringProperty
  Rather than inferring optStr to be 'String!?', we now infer this to
  be 'String??', which is in line with the expectations of SE-0054.
  The fact that we were only inferring the outermost IUO to be an
  Optional in Swift 4 was a result of the incomplete implementation of
  SE-0054 as opposed to a particular design. This should rarely cause
  problems since in the common-case of actually using the property rather
  than just assigning it to a value with inferred type, we will behave
  the same way.
- Overloading functions with inout parameters strictly by a difference
  in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
  will result in an error rather than the diagnostic that was added
  in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
  allowed by SE-0054 will now treat the '!' as if it were '?'.
  Swift 4.1 generates warnings for these saying that putting '!'
  in that location is deprecated. These locations include for example
  typealiases or any place where '!' is nested in another type like
  `Int!?` or `[Int!]`.

This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.

ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!

Resolves rdar://problem/33272674.
2018-01-31 12:15:58 -08:00
Jordan Rose
75dcf27896 [ClangImporter] swift_wrapper: transfer inherited synthesized protos (#14254)
That is, if the wrapped type conforms to special known protocol X via
synthesized conformance to protocol Y which inherits from X, the
wrapper should conform to X as well.

https://bugs.swift.org/browse/SR-6842
2018-01-30 08:48:17 -08:00
Graydon Hoare
9334779f33 [Stats] Reduce layering violations in FrontendStatsTracer. 2018-01-30 01:54:07 -08:00
Graydon Hoare
ff104e4f11 Merge pull request #14059 from graydon/count-stats-stacks
[Stats] Expand FrontendStatsTracer to trace multiple entity-types.
2018-01-24 17:30:26 -08:00
Jordan Rose
3f4cba25d4 Use a semantic ClangImporterSynthesizedTypeAttr for error structs
...rather than the ad hoc CustomTypeNameManglingAttr I was using
before. As John pointed out, the AST should be semantic wherever
possible.

We may someday want to get out of this being an attribute altogether,
or duplicating information that's available in the original Clang
node, by actually storing a reference to that node somewhere. This is
tricky and mixed up with deciding what hasClangNode() or
getClangDecl() would mean, though, so for now the attribute just
carries the information we need.
2018-01-24 10:57:52 -08:00
Jordan Rose
e63879dc48 [Mangling] Define "related entity" operators 'LA'...'LJ'
(and 'La'...'Lj')

Use this for the synthesized structs for error enums, as described in
the previous commit, instead of reusing the "private discriminator"
feature. I left some space in the APIs for "related entity kinds" that
are longer than a single character, but I don't actually expect to use
it any time soon. It's mostly just easier to deal with StringRef than
with a bare char.

Note that this doesn't perfectly round-trip to the old mangling; I had
it treat these nodes as private discriminators with a prefixed "$"
instead. We don't depend on that for anything, though.
2018-01-24 10:52:46 -08:00
Jordan Rose
9c1a22ad0f Mangle synthesized error structs as a variant of the imported enum
When importing a C enum with the ns_error_domain attribute, we
synthesize a struct containing an NSError object to represent errors
in that domain. That synthesized struct should have a mangled name
that ties it to the original C enum, if we want it to be stable, and
now it does.

Before: $SSC7MyErrorV (a normal struct, which is a lie)
After: $SSC11MyErrorCode13ns_error_enumLLV
  kind=Global
    kind=Structure
      kind=Module, text="__C_Synthesized"
      kind=PrivateDeclName
        kind=Identifier, text="ns_error_enum"
        kind=Identifier, text="MyErrorCode"

Using the "private discriminator" feature allows us to pack in extra
information about the declaration without changing the mangling
grammar, and without stepping on anything the importer is using.

More rdar://problem/24688918
2018-01-23 17:05:43 -08:00
Graydon Hoare
5f130a8d35 [Stats] Expand FrontendStatsTracer to trace multiple entity-types. 2018-01-21 23:37:54 -08:00
Slava Pestov
240409761b ClangImporter: Give error wrappers an explicit '@_fixed_layout' attribute
This ensures they answer false to isFormallyResilient(), without
complicating the implementation of this method with a module
context check.
2018-01-12 19:03:48 -08:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
Jordan Rose
ff41cf3c7c [ClangImporter] Don't drop CF retain/release functions with swift_name (#13834)
Swift uses ARC for CF types, so CFRetain and its variants aren't
necessary. But sometimes functions /look/ like retain/release variants
(to the compiler, anyway) but actually do useful things, like
CMBufferQueueDequeueAndRetain. Recognize this with the presence of a
swift_name attribute (either from CF_SWIFT_NAME or from API notes).

rdar://problem/23480844
2018-01-09 17:55:58 -08:00
Mark Lacey
167e484187 IUO: Use new ImportedType class rather than std::pair<Type, bool>. 2018-01-06 00:30:14 -08:00
Mark Lacey
dea38a7710 IUO: Rename some importer functions.
Minor update to improve naming.
2018-01-05 16:52:50 -08:00
Mark Lacey
0544e2a26e IUO: Fix missed update to clang importer for constructor parameters. 2018-01-05 14:40:15 -08:00
Mark Lacey
4ca3e20651 IUO: Update the ClangImporter to surface whether decls are implicitly unwrapped.
Update several functions to return a Type/bool pair with the bool
representing whether the type is one that should be implicitly
unwrapped. This bool is surfaced up to the decl importing functions,
allowing them to set the appropriate decl attribute so that the
expression type checker will consider selecting the underlying type of
the optional involved during solving if the expression doesn't type
check with the optional type.

When importing Decls, use the IUO information to set the
ImplicitlyUnwrappedOptionalAttr attribute when appropriate.

We're still generating IUO types at the moment, so this change doesn't
really have much of an affect other than the creation of those
attributes.
2018-01-04 18:23:42 -08:00
Ben Langmuir
fc556317b6 Reapply "Normalize version tuples in availability attributes coming from clang to use ".""
This reapplies f1c48daf70 with the test split up so that the
availability bits don't affect other platforms.

Seen as @available attributes being printed with "_" in interface
generation, but fixing it in the importer means they can't leak into
anywhere else.

rdar://problem/30451293
2018-01-03 13:09:21 -08:00
Jordan Rose
e1f46b5a26 [ClangImporter] Handle subscript getters redeclared in subclasses (#13470)
Objective-C subscripts don't have special declarations like
properties; they're just specially-named methods (or method pairs),
and we have to make an independent SubscriptDecl in Swift. This means
that when a subclass wants to make a subscript settable, they just add
the appropriately-named setter method.

Swift handled this by detecting when the getter and setter weren't
declared in the same type, and assuming this meant it was a subclass
adding a setter. Unfortunately, the same condition /also/ picked up
the case where the getter (and only the getter) is /redeclared/ in a
subclass (perhaps to add an attribute), and the new subscript was
getting added to the base class instead of the subclass.

The fix relies on the fact that the original decl we provide is what
we use to look up the other accessor. If the getter and setter are in
different types, whichever one we started with must be the
more-derived one. So the final change is just "did we start with the
setter?" rather than "is there a setter at all?".

I'm not sure why this is only just now causing problems, given that we
seem to have been getting this wrong for years, but it definitely
/was/ wrong and now it's not.

rdar://problem/36033356
2017-12-18 11:01:34 -08:00
Slava Pestov
1f1fa3b91d AST: Remove NominalTypeDecl::hasDelayedMembers() 2017-12-12 14:01:55 -08:00
Slava Pestov
db7aa52c5c ClangImporter: Simplify finishPendingActions() 2017-12-12 14:01:54 -08:00
Graydon Hoare
eba924b1c2 [ClangImporter] call addImplicitDestructor() on imported CF types. 2017-12-01 16:52:25 -08:00
Arnold Schwaighofer
eba12a7c3e Revert "Finish and default-enable named lazy member loading" 2017-12-01 07:25:54 -08:00
swift-ci
7e6f7e1c04 Merge pull request #12843 from graydon/force-on-named-lazy-member-loading 2017-12-01 02:32:10 -08:00
Graydon Hoare
0e3bde6744 [ClangImporter] call addImplicitDestructor() on imported CF types. 2017-11-30 22:01:06 -08:00
Jordan Rose
f5989d2f6f [ClangImporter] Don't use instance methods to suppress class methods (#13100)
(and vice versa)

Inadvertant fallout from 7725e38d96, which concerned accidental ambiguity
when mirroring protocol methods onto an implementing class.

https://bugs.swift.org/browse/SR-5959
2017-11-28 08:53:08 -08:00
Doug Gregor
7ec8512b11 Merge pull request #12970 from DougGregor/conformance-interface-type
[AST] Store only interface types in NormalProtocolConformances.
2017-11-17 07:52:59 -08:00
Slava Pestov
ca1586d667 ClangImporter: Don't synthesize zero initializer body if we finished type checking 2017-11-16 21:11:41 -08:00
Huon Wilson
5f70f68c0d [AST] Store only interface types in NormalProtocolConformances.
Rather than storing contextual types in the type witnesses and associated
conformances of NormalProtocolConformance, store only interface types.

@huonw did most of the work here, and @DougGregor patched things up to
complete the change.
2017-11-16 11:45:18 -08:00
Slava Pestov
29279afd9b Merge pull request #12940 from slavapestov/clang-importer-type-checked-ast
ClangImporter: Synthesize fully-type checked AST for common functions
2017-11-15 16:23:20 -08:00
Slava Pestov
415fce274b ClangImporter: Synthesize fully-type checked AST for common functions
These kinds of synthesized declarations no longer need to go
through the constraint solver, since they have simple enough
bodies that we can construct type checked AST directly:

- rawValue getter and init(rawValue:)
- memberwise struct initializer
- zero struct initializer
2017-11-14 21:52:29 -08:00
Graydon Hoare
c179431724 [ClangImporter] Use ClassDecl::addImplicitDestructor more thoroughly. 2017-11-06 14:59:51 -08:00
Graydon Hoare
7e363ff97a [AST] Add implicit dtors outside typechecking, rdar://35198678 2017-11-04 02:59:52 -07:00
Jordan Rose
85ccbb4990 [ClangImporter] Bridging can happen even without full bridgeability
Partially reverts f4f8349 (from July!) which caused us to start
importing global blocks with unbridged parameters, breaking source
compatibility. I'm still investigating whether there's an actual hole
in the logic; see next few commits.

rdar://problem/34913634
2017-11-02 14:40:39 -07:00