Commit Graph

689 Commits

Author SHA1 Message Date
Slava Pestov
0bab515f66 Sema: Build type checked bodies for trivial accessors
This includes all synthesized accessors except for lazy getters
and observer setters.

Building checked AST has certain advantages:

- It is faster, avoiding the need to create and solve ConstraintSystems
  or performing various diagnostic and semantic walks over the AST.

- It allows us to postpone delayed body synthesis until needed in SILGen,
  instead of having to walk a list of "external declarations" in Sema.

It also unblocks lazier conformance checking. Now that SILGen can
trigger conformance checking on its own, we need to be able to
synthesize bodies of accessors that witness protocol requirements.
This will allow us to eventually remove Sema's "used conformances"
list.

Note that for now, various utility functions in CodeSynthesis.cpp are
used for both checked and unchecked function bodies, so they take a
'typeChecked' boolean parameter that complicates some logic more than
necessary. Once the remaining body synthesizers are refactored to
build type-checked AST, the 'typeChecked' flag will go away.
2019-05-13 17:25:49 -04:00
Doug Gregor
eb3f28825c [Property delegates] Fix textual interfaces for properties with delegates
Don’t synthesize a missing setter for a property that has an attached
delegate and is in a .swiftinterface file.
2019-05-05 22:12:00 -07:00
Doug Gregor
f34aa79df1 [Property delegates] Implicit initialization for properties with delegates.
When a property delegate type has a default initializer, use that to
implicitly initialize properties that use that delegate and do not have
their own initializers. For example, this would allow (e.g.), delegate
types like the DelayedImmutable and DelayedMutable examples to drop
the explicit initialization, e.g.,

```
  @DelayedMutable var foo: Int
```

would be implicitly initialized via

```
  $foo = DelayedMutable()
```

This is a simplistic implementation that does not yet propertly handle
definite initialization.

Fixes rdar://problem/50266039.
2019-05-01 23:17:16 -07:00
Doug Gregor
bf2c4d951b Prevent less-visible properties with delegates from affecting memberwise initializer
When a property with an attached delegate has less-than-internal visibility
and is initialized, don’t put it into the memberwise initializer because
doing so lowers the access level of the memberwise initializer, making it
fairly useless.

Longer term, it probably makes sense to have several memberwise initializers
at different access levels, so adding an initialized `private var` make
the memberwise initializer useless throughout the rest of the module.

Addresses rdar://problem/50266245.
2019-05-01 14:10:56 -07:00
swift-ci
ff208b677f Merge pull request #24363 from DougGregor/is-memberwise-initialized 2019-04-30 11:29:27 -07:00
Doug Gregor
96952ea9a2 [AST] Distinguish memberwise-initialized properties for storage vs. declared
The determination of whether a property is memberwise-initialized is
somewhat confused for properties that have synthesized backing properties.
Some clients (Sema/Index) want to see the declared properties, while others
(SILGen) want to see the backing stored properties. Add a flag to
`VarDecl::isMemberwiseInitialized()` to capture this variation.
2019-04-30 10:12:43 -07:00
Doug Gregor
1a169b91bd Centralize the definition of isMemberwiseInitialized()
This utility was defined in Sema, used in Sema and Index, declared in
two headers, and semi- copy-pasted into SILGen. Pull it into
VarDecl::isMemberwiseInitialized() and use it consistently.
2019-04-29 10:30:38 -07:00
Doug Gregor
e7ef638285 [Property delegates] Sure synthesized PatternBindingDecl static when needed
Fixes rdar://problem/50201019.
2019-04-27 09:38:34 -07:00
Doug Gregor
6218673ead [Property delegates] Use $$foo for the backing storage and make it private.
When the property delegate type overrides the delegate value by providing
a delegateValue property, name the backing storage $$foo and make it private.
2019-04-23 21:56:03 -07:00
Doug Gregor
fcd2fd97e8 [Property delegates] Don't create backing var for ill-formed delegate type.
Fixes the crash-on-invalid in rdar://problem/49982937.
2019-04-23 11:32:29 -07:00
Doug Gregor
7df695536e [Property delegates] Fix printing of memberwise initializer default arguments 2019-04-23 11:32:28 -07:00
Doug Gregor
261b879b54 [Property delegates] Rename storageValue to delegateValue 2019-04-23 11:32:28 -07:00
Doug Gregor
2e9f8cf981 Capture a placeholder opaque value expression when needed. 2019-04-23 11:32:28 -07:00
Doug Gregor
4f56db2653 [Property delegates] Implement support for storageValue 2019-04-23 11:32:28 -07:00
Doug Gregor
cc68b12d1a [SILGen] Initialization of instance properties with property delegates
The initialization of an instance property that has an attached
property delegate involves the initial value written on the property
declaration, the implicit memberwise initializer, and the default
arguments to the implicit memberwise initializer. Implement SILGen
support for each of these cases.

There is a small semantic change to the creation of the implicit
memberwise initializer due to SE-0242 (default arguments for the
memberwise initializer). Specifically, the memberwise initializer will
use the original property type for the parameter to memberwise
initializer when either of the following is true:

  - The corresponding property has an initial value specified with the
    `=` syntax, e.g., `@Lazy var i = 17`, or
  - The corresponding property has no initial value, but the property
    delegate type has an `init(initialValue:)`.

The specific case that changed is when a property has an initial value
specified as a direct initialization of the delegate *and* the
property delegate type has an `init(initialValue:)`, e.g.,

```swift
struct X {
  @Lazy(closure: { ... })
  var i: Int
}
```

Previously, this would have synthesized an initializer:

```swift
init(i: Int = ???) { ... }
```

However, there is no way for the initialization specified within the
declaration of i to be expressed via the default argument. Now, it
synthesizes an initializer:

```swift
init(i: Lazy<Int> = Lazy(closure: { ... }))
```
2019-04-23 11:31:59 -07:00
Doug Gregor
6526cfa8d4 Memberwise initializer synthesis for properties with attached delegates. 2019-04-23 11:31:58 -07:00
Doug Gregor
b18a2902e5 Implement access control for property delegates 2019-04-23 11:31:58 -07:00
Doug Gregor
b8061eab34 Synthesize backing storage property for properties with attached delegates.
When a property has an attached property delegate, a backing storage
property of the corresponding delegate type will be
synthesized. Perform this synthesis, and also synthesize the
getter/setter for the original property to reference the backing
storage property.
2019-04-23 11:31:58 -07:00
Alexis Laferrière
007fbb6ebd Merge pull request #23932 from xymus/IsFinalRequest
Sema: implement `isFinal` using a request evaluator
2019-04-19 13:02:07 -07:00
Alexis Laferrière
98059831c9 Sema: implement isFinal using a request evaluator
Add the request evaluator `IsFinalRequest` to lazily determine if a
`ValueDecl` is final.
2019-04-17 09:17:44 -07:00
Robert Widmann
f192b92799 Merge pull request #23785 from Azoy/context-cleanups
AST NFC: Minor cleanups around ASTContext
2019-04-16 13:50:35 -04:00
Azoy
7c75836d77 context cleanup part 1 2019-04-03 20:50:58 -05:00
Brent Royal-Gordon
dd95a63e5b Merge pull request #23724 from brentdax/static-start
[NFC] Correct assumptions about static AbstractStorageDecls
2019-04-02 17:16:27 -07:00
Slava Pestov
20dc9a4c6a Sema: Fix circular validation between memberwise initializer and stored property
Fixes <rdar://problem/42704745>.
2019-04-01 22:41:16 -04:00
Brent Royal-Gordon
5c03918c82 [NFC] Remove VarDecl guards on staticness checks
Fixes various places where we assume that only a VarDecl can be static so they operate on any AbstractStorageDecl instead. NFC until static subscripts are added.
2019-04-01 18:04:00 -07:00
Slava Pestov
b9ef5708e2 Sema: Simplify representation of vararg forwarding
VarargExpansionExpr shows up in call argument lists in synthesized
initializers and modify accessors when we need to forward arguments
to a call taking varargs.

Previously we would say that the type of VarargExpansionExpr is
$T when its subexpression type is [$T]. matchCallArguments() would
then 'collect' the single VarargExpansionExpr into a variadic
argument list with a single element, and build an ArgumentShuffleExpr
for the argument list.

In turn, SILGen would peephole vararg emission of a variadic
argument list with a single entry that happens to be a
VarargExpansionExpr, by returning the subexpression's value,
which happened to be an array of the right element type,
instead of building a new array containing the elements of the
variadic argument list.

This was all too complicated. Instead, let's say that the type of
a VarargExpansionExpr is [$T], except that when it appears in a
TupleExpr, the variadic bit of the corresponding element is set.

Then, matchCallArguments() needs to support a case where both
the parameter and argument list have a matching vararg element.
In this case, instead of collecting multiple arguments into a
single variadic argument list, we treat the variadic argument like
an ordinary parameter, bypassing construction of the
ArgumentShuffleExpr altogether.

Finally, SILGen now needs to be able to emit a VarargExpansionExpr
in ordinary rvalue position, since it now appears as a child of a
TupleExpr; it can do this by simply emitting the sub-expression
to produce an array value.
2019-03-28 23:23:58 -04:00
Slava Pestov
cece98b2ed AST: Generalize ClassDecl::checkObjCAncestry() to ClassDecl::checkAncestry()
This replaces ClassDecl::hasObjCMembers() and some hand-coded walks.
2019-03-26 18:42:59 -04:00
Azoy
dcedba73f0 fix lazy vars
clean up lazy vars

more docs
2019-03-20 23:01:38 -05:00
Azoy
2bd99eecae clean up subs
value interface type
2019-03-18 00:06:24 -05:00
Azoy
bc7cb332df Fix generic types
add tests

get parent init
2019-03-14 04:07:24 -05:00
Azoy
e8bc662b35 Cleanups from Slava's comments
update module format
2019-03-13 18:58:45 -05:00
Azoy
6d00a5758f Disallow default values for patterns binding multiple variables
lets not add !
2019-03-13 18:57:36 -05:00
Azoy
6f7d20b99e Synthesize default values for memberwise init
Introduce stored property default argument kind

Fix indent

Assign nil to optionals with no initializers

Don't emit generator for stored property default arg

Fix problem with rebase

Indentation

Serialize stored property default arg text

Fix some tests

Add missing constructor in test

Print stored property's initializer expression

cleanups

preserve switch

complete_constructor

formatting

fix conflict
2019-03-13 18:57:36 -05:00
Harlan Haskins
dd5a6772f4 [ParseableInterfaces] Handle lazy vars
For lazy vars, we need to make public the top-level entry point, and the fact
that the lazy storage contributes to the layout of a type (if it’s fixed
layout, or if the type is not resilient.) We also shouldn’t ever print `lazy`
in a parseable interface.

Since we need to parse these identifiers, give them a new name,
`$__lazy_storage_$_{propname}`, which is parseable only in parseable interfaces
so it doesn’t conflict with other properties.
2019-01-22 11:03:12 -08:00
Pavel Yaskevich
20b7c2a4ba [CodeSynthesis] Make sure that LazyInitializerExpr has a type
Currently when `LazyInitializerExpr` is added to the AST it's not
given an explicit type. Because of that constraint solver silently
fails in contraint generator without diagnostic. But since
sub-expression associated with `LazyInitializerExpr` is already
type-checked it makes sense to set its type explicitly.
2018-12-19 14:04:32 -08:00
Slava Pestov
544e0a02d5 AST: Don't link together GenericParamLists of nested generic types
GenericParamList::OuterParameters would mirror the nesting structure
of generic DeclContexts. This resulted in redundant code and caused
unnecessary complications for extensions and protocols, whose
GenericParamLists are constructed after parse time.

Instead, lets only use OuterParameters to link together the multiple
parameter lists of a single extension, or parameter lists in SIL
functions.
2018-12-11 23:55:41 -05:00
Slava Pestov
392071e702 Sema: Peel off more code from the TypeChecker instance 2018-12-10 00:00:49 -05:00
Slava Pestov
06c2e980cb Merge pull request #21133 from slavapestov/lazy-implicit-inits
Lazy synthesis of implicit constructors in non-primary files
2018-12-07 22:45:40 -05:00
Slava Pestov
a55283d704 Sema: Lazy synthesis of implicit constructors in non-primary files 2018-12-07 20:39:27 -05:00
Slava Pestov
6c012b2aec AST: Remove some unnecessary LazyResolver * parameters from ASTContext methods 2018-12-07 20:39:27 -05:00
Slava Pestov
aa747dcd81 Remove property behaviors 2018-12-07 20:38:33 -05:00
Slava Pestov
a76012dc72 Sema: Tiny cleanup for createImplicitConstructor() 2018-12-07 17:11:30 -05:00
Slava Pestov
a6f6dc01bc Sema: Fix order dependency between lazy getter body synthesis and capture computation
If we computed captures before completing a lazy getter body, we would fail to
consider the 'self' capture properly. Instead make it resilient to such ordering
issues by checking in capture computation if the lazy property has a getter yet
or not.
2018-12-07 17:10:15 -05:00
Slava Pestov
7aed494bf3 Sema: Move accessor synthesis to the callback mechanism
Also change some TypeChecker usages to ASTContext.
2018-12-07 17:10:08 -05:00
Slava Pestov
77215ef5b7 Sema: Force SILGen emission of on-demand synthesized accessors
When an on-demand accessor is synthesized while checking a conformance,
make sure it ends up in the 'external declarations' list so that SILGen
can emit it.

Fixes <rdar://problem/46503121>, and part of <rdar://problem/46186045>.
2018-12-06 23:58:46 -05:00
swift-ci
39161d5b36 Merge pull request #20600 from adrian-prantl/36032653 2018-12-05 17:01:58 -08:00
Adrian Prantl
d63debeb60 Experimental: Extend ClangImporter to import clang modules from DWARF
When debugging Objective-C or C++ code on Darwin, the debug info
collected by dsymutil in the .dSYM bundle is entirely
self-contained. It is possible to debug a program, set breakpoints and
print variables even without having the complete original source code
or a matching SDK available. With Swift, this is currently not the
case. Even though .dSYM bundles contain the binary .swiftmodule for
all Swift modules, any Clang modules that the Swift modules depend on,
still need to be imported from source to even get basic LLDB
functionality to work. If ClangImporter fails to import a Clang
module, effectively the entire Swift module depending on it gets
poisoned.

This patch is addressing this issue by introducing a ModuleLoader that
can ask queries about Clang Decls to LLDB, since LLDB knows how to
reconstruct Clang decls from DWARF and clang -gmodules producxes full
debug info for Clang modules that is embedded into the .dSYM budle.

This initial version does not contain any advanced functionality at
all, it merely produces an empty ModuleDecl. Intertestingly, even this
is a considerable improvement over the status quo. LLDB can now print
Swift-only variables in modules with failing Clang depenecies, and
becuase of fallback mechanisms that were implemented earlier, it can
even display the contents of pure Objective-C objects that are
imported into Swift. C structs obviously don't work yet.

rdar://problem/36032653
2018-12-05 13:54:13 -08:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
John McCall
b9f4b17a33 Mark lazy properties as having mutating getters immediately.
We were trying to do this when synthesizing the getter prototype, but
we don't do that immediately when we're just type-checking a reference
to the storage, which could lead to the reference thinking that the
getter was non-mutating.

Fixes rdar://45712204.
2018-11-26 17:36:13 -05:00
Arnold Schwaighofer
40f0c43e31 Add isNativeDynamic() and use it 2018-11-14 12:04:41 -08:00