Commit Graph

768 Commits

Author SHA1 Message Date
Slava Pestov
9ff97367df AST: Rename hasFixedLayout() to isResilient() and flip polarity
In IRGen we call this isResilient() already in IRGen, and it's
more consistent to call it the same thing everywhere.
2018-01-11 21:57:42 -08:00
Andrew Trick
fd99f4d8e2 Code review. 2018-01-08 11:26:56 -08:00
Andrew Trick
9122e82880 Rewrite LogicalPathComponent::getMaterialized.
Collapse this routine down to its fundamental logic, removing unnecessary
hacks along the way. It's now self-documenting and robust.

The basic principle is that any special case should use the same
SILGen abstractions and utilities. The implementation should be
identical except where the special case is logically
distinct. i.e. the reason for the special case should be self-evident
from the code.

This is not NFC. Unnecessary borrow scopes are no longer emitted for
getters. More generally, there may have been some incidental behavior
specific to a corner cases that becomes more uniform now.
2018-01-08 11:26:56 -08:00
Slava Pestov
2590142b7b SILGen: Remove shouldReferenceForeignAccessor()
Also, the 'isDirectAccess' parameter to various methods is now dead,
because it was never being set to true in the foreign case anyway.
2018-01-04 21:53:34 -08:00
Andrew Trick
17fa484f04 SILGen. Fix temporary LValue cleanup.
Use the LValue formal evaluation scope to destroy temporary lvalues that aren't
part of the expression's resulting value.

Disable an old hack that destroyed call site arguments after the call.

This is the first step in cleanup up LValue materialization.
2018-01-01 19:05:22 -08:00
Michael Gottesman
1cfcfcd7d7 [silgen] When assigning into an lvalue, the value that we are storing must be at plus one.
This allows us to obey Semantic SIL when attempting to assign a guaranteed value
into a storage location.

rdar://34222540
2017-11-20 15:21:14 -08:00
Slava Pestov
0b895bbf44 SILGen: Use resilience expansion when deciding if we need to use materializeForSet or not 2017-11-10 17:21:17 -08:00
Huon Wilson
0236db7be1 [SIL] Witness methods store the conformance from which they come. 2017-11-01 11:33:26 -07:00
Slava Pestov
dfdd3a795d SILGen: Fix bug with NSString import-as-member globals
This is not really the right fix if we want to have physically
addressed lvalues support bridging in general, but doing so
requires adding a new LValue component type and doing a bunch
more refactoring, so just hack in a narrow fix for now since
it only seems to occur in one case.

Fixes <rdar://problem/34913892>.
2017-10-16 19:22:30 -07:00
Slava Pestov
b5cf13d2d7 SILGen: Fix bug with bridging peephole
When we peephole away a bridge from Objective-C followed by a
bridge to Objective-C where the destination type is AnyObject,
we need to force the source value if it was an optional.

Fixes <rdar://problem/33669575>.
2017-10-15 23:15:11 -07:00
Michael Gottesman
3e730d4fa6 [silgen] Rename {was,}InWritebackScope => {was,}InFormalEvaluationScope. 2017-09-06 16:31:20 -07:00
Michael Gottesman
eceb61ac9c [silgen] Try to re-abstract first before going through other code paths in visitRecNonInOutBase. NFC. (#11770)
The main thing here is that we have a large block of code that we will never hit
if we need to re-abstract. Instead, this commit reflows visitRecNonInOutBase to
first check if re-abstraction is needed before doing anything else. If
re-abstration is required, we quickly handle it and return.

This commit also fixes some variable names to match the rest of the function.

rdar://31521023
2017-09-05 14:00:58 -07:00
Michael Gottesman
92162e2174 [gardening] Rename a bunch of isGuaranteedValid parameters to => is{Base,Addr,Address}Guaranteed parameters.
These names are not perfect, but they provide more descriptive background on
what the parameter actually does. Simply, these parameters say that the
underlying base address will last longer than the usage of the underlying value,
so a begin_borrow/load_borrow could be used to access the underlying value from
the base. If the base is destroyed before the borrow finishes, we have an
ownership violation. =><=.

I also copy-edited/added doxygen comments above some of these methods as well.
2017-09-04 09:45:53 -07:00
Michael Gottesman
8dea88273b [silgen] Split the Base emission code out of visitRec into their own helper functions.
It makes the code easier to read.
2017-09-01 11:16:58 -07:00
Michael Gottesman
daeddd0075 [silgen] Enable ownership on objc factory constructor test and fix exposed problems.
The specific exposed problem had to do with my using the same emission routine
for both lvalues using delegating init self (where we want formal accesses) and
for routines that wanted normal access to self. By splitting them the issue is
resolved.

As a benefit, I added a small peephole that I needed to add for my own purposes
(i.e. to maintain invariants), but that also incidentally improve codegen in
other places!

rdar://31521023
2017-08-31 16:39:12 -07:00
David Zarzycki
ac89b14700 [SILGen] NFC: Make adding new ReferenceStorageTypes easier
Failing at compile time is better than at runtime.
2017-08-12 09:40:24 -04:00
Michael Gottesman
bdb032d941 [silgen] gen => SGF.
In a previous commit, I got rid of all SILGenFunction &gen. But looks like I
missed SILGenFunction gen(...). This fixes 3 such cases.
2017-08-11 09:50:36 -07:00
Michael Gottesman
ecf9a941d9 Merge pull request #11378 from gottesmm/check_more_rvalue_invariants
[silgen] Verify that all ManagedValues in all RValues that are loadable are actual loaded (i.e. have object type).
2017-08-08 06:28:17 -07:00
Michael Gottesman
7cf4cdf9d9 [silgen] Verify that all ManagedValues in all RValues that are loadable are actual loaded (i.e. have object type).
This is already an RValue invariant that used to be enforced upon RValue
construction. We put in a hack to work around a bug where that was not occuring
and changed RValue constructors to instead load stored objects when they needed
to. But the problem is that since then we have added more constructors that
provide other manners to create such an invalid RValue.

I added verification to many parts of RValue and exposed an additional verify
method that we can invoke at the end of emitRValue() eventually to verify our
invariants. This will give me the comfort to make that assumption in other parts
of SILGen without worry.

I also performed a small amount of cleanup of RValue construction.

rdar://33358110
2017-08-08 00:43:33 -07:00
George Karpenkov
efe143c2f4 Adding support for -sanitize=fuzzer flag (#11381)
Similarly to Clang, the flag enables coverage instrumentation, and links
`libLLVMFuzzer.a` to the produced binary.
Additionally, this change affects the driver logic, and enables the
concurrent usage of multiple sanitizers.
2017-08-07 18:16:51 -07:00
Robert Widmann
e5918f70e8 [NFC] Refactor self type computation to return a Param
Remove a user of InOutType::get and flip a few users of
FunctionType::get to pass Params instead of naked input
types.
2017-07-23 21:36:16 -07:00
John McCall
c0b3bf1711 Suppress access enforcement when an l-value is converted to a pointer
just for pointer identity.

The current technique for deciding whether that's the case is *extremely*
hacky and need to be replaced with an attribute, but I'm reluctant to
take that on so late in the schedule.  The hack is terrible but not too
hard to back out in the future.  Anyone who names a method like this just
to get the magic behavior knows well that they are not on the side of
righteousness.

rdar://33265254
2017-07-21 23:40:04 -04:00
Robert Widmann
fd7c1f8bc0 Merge pull request #10889 from CodaFi/the-bit-ter-pill
Consistently Pass Parameter Flags
2017-07-19 13:14:50 -07:00
Robert Widmann
8cdddef2f8 Refactor Params to use flags
Also, begin to pass around base types instead of raw InOutType types.  Ideally, only Sema needs to deal with them, but this means that a bunch of callers need to unwrap any inouts that might still be lying around before forming these types.

Multiple parts of the compiler were slicing, dicing, or just dropping these flags.  Because I intend to use them for the new function type representation, I need them to be preserved all across the compiler.  As a first pass, this stubs in what will eventually be structural rules as asserts and tracks down all callers of consequence to conform to the new invariants.

This is temporary.
2017-07-19 09:49:32 -07:00
Joe Groff
7705393f06 SILGen: Ease off +0 peepholes for load exprs.
Now that we more tightly close formal accesses on lvalues, having LoadExpr and friends try to return a +0 loaded value is unsafe without deeper analysis, since the access will be closed immediately after the load and potentially free temporary memory that might be the only thing keeping the borrowed object alive. Fixes rdar://problem/32730865.
2017-07-18 15:13:06 -07:00
swift-ci
210b577481 Merge pull request #11019 from gottesmm/silgen_unowned_fixes 2017-07-17 16:44:12 -07:00
Michael Gottesman
8797d88d7c [silgen] Fix a few uses of UnownedRetain, UnownedRelease, StrongRetainUnowned in favor of their ownership counterparts.
The counterparts are:

UnownedRetain -> CopyValue.
UnownedRelease -> DestroyValue.
StrongRetainUnowned -> CopyUnownedValue.

I thought I hit all of these already. When I was fixing some DI stuff I realized
that I missed a few cases in SILGenLValue.cpp. To make sure we do not regress, I
added some verifier checks to make sure these instructions can only be used in
non-ownership sil.

rdar://31880847
2017-07-17 15:59:30 -07:00
swift-ci
3b6f60e70b Merge pull request #11020 from gottesmm/silgenlvalue_gardening 2017-07-17 15:46:20 -07:00
Michael Gottesman
0ddab717de [gardening] Reduce indentation of SILGenLValue::visitRec by inverting an if condition.
The issue here is that the way the code was structured caused a large
conditional block at the beginning of the function that always exited early and
then a short unconditional block.

This commit inverts the if statement so that the short unconditional block is
now conditional and the large conditional block is unconditional, reducing the
overall indentation for the majority of the function.
2017-07-17 15:12:34 -07:00
John McCall
80b180a9a1 Implement a syntactic peephole to recognize explicit bridging
conversions that reverse an implicit conversion done to align
foreign declarations with their imported types.

For example, consider an Objective-C method that returns an NSString*:
  - (nonnull NSString*) foo;
This will be imported into Swift as a method returning a String:
  func foo() -> String
A call to this method will implicitly convert the result to String
behind the scenes.  If the user then casts the result back to NSString*,
that would normally be compiled as an additional conversion.  The
compiler cannot simply eliminate the conversion because that is not
necessarily semantically equivalent.

This peephole recognizes as-casts that immediately reverse a bridging
conversion as a special case and gives them special power to eliminate
both conversions.  For example, 'foo() as NSString' will simply return
the original return value.  In addition to call results, this also
applies to call arguments, property accesses, and subscript accesses.
2017-07-15 01:13:41 -04:00
John McCall
c005618219 Improve dumping facilities for various types in SILGen. 2017-07-15 01:12:55 -04:00
Joe Groff
a6404f0185 Merge pull request #10895 from jckarter/read-only-writable-key-path-projection
SILGen: Do a read-only projection of a writable KeyPath when the lvalue is only read.
2017-07-11 17:14:57 -07:00
Joe Groff
eb1231b722 SILGen: Do a read-only projection of a writable KeyPath when the lvalue is only read.
If we project an lvalue using a KeyPath, but the lvalue is only read from, we don't want to trigger writebacks, observers, or other side effects that a mutable projection would normally need to induce. Fixes SR-5338 | rdar://problem/33135489.
2017-07-11 15:47:59 -07:00
John McCall
7f22faf968 Substantially rework how SILGen handles bridging as part of laying the
ground work for the syntactic bridging peephole.

- Pass source and dest formal types to the bridging routines in addition
  to the dest lowered type.  The dest lowered type is still necessary
  in order to handle non-standard abstraction patterns for the dest type.

- Change bridging abstraction patterns to store bridged formal types
  instead of the formal type.

- Improve how SIL type lowering deals with import-as-member patterns.

- Fix some AST bugs where inadequate information was being stored in
  various expressions.

- Introduce the idea of a converting SGFContext and use it to regularize
  the existing id-as-Any conversion peephole.

- Improve various places in SILGen to emit directly into contexts.
2017-07-11 12:45:13 -04:00
Robert Widmann
957d633185 Rename getInOutOrLValueObjectType to getWithoutSpecifierType
Prepares the AST for a future in which more than just inout and
@lvalue need to be stripped off of ephemeral types.
2017-07-06 09:35:04 -07:00
Robert Widmann
abd5aa8e6d Rename some X-Value-related entities
* Rename coerceToMaterializableValue to coerceToRValue

* Rename isLValueType to hasLValueType to better match the
intended semantics of the member.
2017-06-14 13:18:45 -07:00
Alex Hoppen
d922944af0 Merge pull request #9976 from ahoppen/pdm-diagnostics2
Migration to DeclBaseName in diagnostic definitions
2017-06-13 12:55:16 +02:00
John McCall
f4a181bc21 Various improvements to RValue. 2017-06-11 01:39:50 -04:00
Slava Pestov
29cd885996 SILGen: Remove SILGenFunction::ArchetypeOpenings
SILBuilder now tracks data dependencies between instructions
that open existentials and uses of the opened type, so
SILGen's mechanism for this is no longer needed.

In particular, this simplifies ArchetypeCalleeBuilder.
2017-06-02 01:35:11 -07:00
Joe Groff
ad700c5d6b Merge pull request #10014 from jckarter/existential-keypath-base
SILGen: Handle existential keypath root types.
2017-05-31 16:59:09 -07:00
Joe Groff
4fc0b7df96 SILGen: Handle existential keypath root types.
SR-4917|rdar://problem/32254554
2017-05-31 16:01:02 -07:00
Alex Hoppen
faa1720c48 [Diagnostics] Adjustments for DeclBaseName
Adjust the definition of some diagnostics that are already called with
DeclBaseNames so that the implicit conversion from DeclBaseName to
Identifier is no longer needed.

Adjust the call side of diagnostics which don't have to deal with
special names to pass an Identifier to the diagnostic.
2017-05-31 15:58:46 +02:00
Alex Hoppen
8946015d5a [SIL] Preparations for removal of getName on ValueDecl
With the introduction of special decl names, `Identifier getName()` on
`ValueDecl` will be removed and pushed down to nominal declarations
whose name is guaranteed not to be special. Prepare for this by calling
to `DeclBaseName getBaseName()` instead where appropriate.
2017-05-28 19:13:24 -07:00
Jordan Rose
c0ccdb1626 Change getBaseName to return DeclBaseName instead of Identifier (#9968)
This changes `getBaseName()` on `DeclName` to return a `DeclBaseName`
instead of an `Identifier`. All places that will continue to be
expecting an `Identifier` are changed to call `getBaseIdentifier` which
will later assert that the `DeclName` is actually backed by an
identifier and not a special name.

For transitional purposes, a conversion operator from `DeclBaseName` to
`Identifier` has been added that will be removed again once migration
to DeclBaseName has been completed in other parts of the compiler.

Unify approach to printing declaration names

Printing a declaration's name using `<<` and `getBaseName()` is be
independent of the return type of `getBaseName()` which will change in
the future from `Identifier` to `DeclBaseName`
2017-05-28 17:55:03 -07:00
Alex Hoppen
f302afc97f Unify approach to printing declaration names (#9320)
Printing a declaration's name using `<<` and `getBaseName()` is be
independent of the return type of `getBaseName()` which will change in
the future from `Identifier` to `DeclBaseName`
2017-05-28 17:25:20 -07:00
Slava Pestov
1b254a9843 Sema: Kill off old modeling of generic arguments 2017-05-24 20:39:10 -07:00
John McCall
bb5fe8046f Fix the emission of open-existential-metatype l-values.
rdar://32288618
2017-05-22 20:47:25 -04:00
Slava Pestov
82ef5b3690 SILGen: Fix LogicalPathComponent::getMaterialized() for opened existentials
This is pretty awkward. If an lvalue has an open existential
as its RValue type, we would emit an alloc_stack instruction
holding the materialized temporary before we emitted the
value itself. This introduced a def-after-use violation
because the open existential type in the stack allocation
was not dominated by its definition.

To get around this, don't use an SGFContext to emit the 'get'
in-place. There's no performance degradation, because the only
time we will attempt materializing an lvalue with an open
existential type is when performing an lvalue access of a
class existential payload, and here we in-place initialization
makes no difference since the value is a single reference.
2017-05-15 18:17:26 -07:00
Slava Pestov
52b3ba1f0a SILGen: Use emitOpenExistentialLValue() when lowering OpaqueValueExprs as lvalues
This is part of allowing lvalue access on class existential
payloads.
2017-05-15 18:17:25 -07:00
Slava Pestov
f4741aa7b6 SILGen: Introduce OpenClassExistentialComponent
This is an LValue component whose value is the class
reference inside of a class existential.

Unlike OpenOpaqueExistentialComponent, this is a logical
component, with a "writeback" consisting of wrapping the
new class reference in a class existential having the
same conformances as the original.

This is slightly awkward, but adding "by-address" operations
on class existentials, and projecting the payload out is
a big change and might not make sense for other reasons.
2017-05-15 18:17:24 -07:00