Commit Graph

430 Commits

Author SHA1 Message Date
Chris Lattner
bc005219d1 Implement most of support for properties in function scope, part of rdar://15922884.
Most of the complexity here is teaching SILGen how to handle closed-over direct 
accesses to observing properties, since all of the getter/setter/willSet/didSet
members of the property are actually full closures when in a function body.

We generate correct but really terrible code here, since the setter captures the
willset/didset members themselves.  I'm not worrying about the performance of 
this construct though, functionality is what matters.


Swift SVN r13778
2014-02-11 06:23:14 +00:00
Joe Groff
05c1594441 SILGen: Derive protocol witness context generic params independent of type.
We still glue the generic params of the requirement to the generic params of the conformance, but now we do it without relying on TypeLowering.

Swift SVN r13771
2014-02-11 02:05:00 +00:00
Joe Groff
30a3a9a0a0 SILGen: Hand down context generic params for reabstraction thunks independent from their type.
Pass the context generic params for a reabstraction thunk down to getOrCreateReabstractionThunk from the enclosing function, where it can either use them to define a new thunk or ignore them if it has an equivalent thunk already. Tweak the mangling of reabstraction thunks to use the generic signature with decontextualized "from" and "to" types instead of the generic param list.

Swift SVN r13763
2014-02-10 23:37:11 +00:00
Chris Lattner
827acad533 Various inout improvements:
- purge @inout from comments in the compiler except for places talking about
   the SIL argument convention.
 - change diagnostics to not refer to @inout
 - Change the astprinter to print InoutType without the @, so it doesn't show
   up in diagnostics or in closure argument types in code completion.
 - Implement type parsing support for the new inout syntax (before we just 
   handled patterns).
 - Switch the last couple of uses in the stdlib (in types) to inout.
 - Various testcase updates (more to come).



Swift SVN r13564
2014-02-06 06:22:27 +00:00
Joe Groff
0c0d30d5eb SIL: Make ContextGenericParams a constructor parameter of SILFunction.
Edge SILFunction one step closer to independence from SILFunctionType context by taking the generic param list as a separate constructor parameter, and serializing those params alongside the function record. For now we still pass in the context params from the SILFunctionType in most cases, because the logic for finding the generic params tends to be entangled in type lowering, but this pushes the problem up a step.

Thanks Jordan for helping work out the serialization changes needed.

Compared to r13036, this version of the patch includes the decls_block RecordKind enumerators for the GENERIC_PARAM_LIST layouts in the sil_block RecordKind enumerator, as Jordan had suggested before. r13036 caused buildbot failures when building for iOS, but I am unable to reproduce those failures locally now.

Swift SVN r13485
2014-02-05 16:52:02 +00:00
Joe Groff
1655298ae0 SILGen: Emit protocol witnesses as public.
Specialization and devirtualization can produce legitimate direct references to them from outside, and at least in the short term all conformances will be public.

Swift SVN r13287
2014-02-01 04:17:32 +00:00
Joe Groff
71379f5bad SILGen: Explicitly mark uninitialized locals with mark_uninitialized, and have DI only consider mark_uninitialized storage.
Have SILGen mark all variables bound from pattern bindings without initializers (and *only* ones without initializers) with mark_uninitialized [var] pseudo instructions. On the DI end, *only* consider mark_uninitialized instructions for DI analysis. This has many benefits:

- DI doesn't waste time analyzing locals that are trivially initialized in the original source code.
- DI doesn't try to mangle canonical SIL that has been inlined from transparent functions, which may have been optimized into a form DI isn't written to understand.

While we're here, fix an issue with DCE where it would try to kill unused MarkUninitialized instructions. Although MarkUninitialized has no side effects, it still is semantically important to raw SIL, and can't be killed.

Chris did most of the work here; I just finished updating tests and fixing bugs.

Swift SVN r13247
2014-01-31 22:50:21 +00:00
Joe Groff
e7a870ff77 SILGen: Local functions in a generic context always need to capture their enclosing generic context.
Fixes a bug encountered trying to repro <rdar://problem/15921334>.

Swift SVN r13081
2014-01-29 00:44:29 +00:00
Michael Gottesman
800ea37c34 Revert r13036 as per Joe's request.
Swift SVN r13063
2014-01-28 21:36:06 +00:00
Joe Groff
7f5091ad86 SILGen: Build forwarding substitutions from context generic params.
Severs another easy dependency on SILFunctionType context.

Swift SVN r13052
2014-01-28 05:25:56 +00:00
Chris Lattner
64f45b40d8 abstractstoragedecls with objc accessor thunks always have associated accessor
functions now, so simplify code.


Swift SVN r13046
2014-01-28 04:22:56 +00:00
Joe Groff
5d58f10e48 SIL: Make ContextGenericParams a constructor parameter of SILFunction.
Edge SILFunction one step closer to independence from SILFunctionType context by taking the generic param list as a separate constructor parameter, and serializing those params alongside the function record. For now we still pass in the context params from the SILFunctionType in most cases, because the logic for finding the generic params tends to be entangled in type lowering, but this pushes the problem up a step.

Thanks Jordan for helping work out the serialization changes needed.

Swift SVN r13036
2014-01-28 02:17:46 +00:00
Chris Lattner
a945271aa8 Now that properties in protocols are always computed, and that StoredObjC
properties have accessors, we have an amazing property: everything that we
want to form a getter or setter for ... really has one! I suspect many things
can be simplified now, but the first on the chopping block is 
StorageDecl::getGetterType (and its three friends) which is now always exactly
just getGetter()->getType().



Swift SVN r12983
2014-01-27 06:17:28 +00:00
Chris Lattner
bea19a3605 Push the AST representation of properties in protocols forward,
mostly to get the brokenness inherent in their current representation out
of my way.

The biggest part of this is that properties in protocols are now always
represented as Computed VarDecls.  If you write "var x : Int" in a protocol,
you get an getter FuncDecl.  If you write "var x : Int { get}" you get the same
thing.  If you write "var x : Int { get set }" then you get a getter and setter
prototype associated with the vardecl.

This then readjusts the various hacks that sort of pass through such things
in SILGen and IRGen, so that we have the same level of hacky support for properties
in protocols.

From the functionality perspective, this enables the { get set } syntax described
in rdar://15827219, and means that "var x : Int" is uniformly treated as read-only
(it was treated as mutable in some cases before).  Properties in protocols are
still quite broken though.



Swift SVN r12981
2014-01-27 05:56:57 +00:00
Chris Lattner
ddc5ca81d6 Fix <rdar://problem/15858869> *all* objc accessors disabled in init methods, not just for self
This patch: 
 - Changes sema to set the "IsDirectPropertyAccess" on MemberRefExprs
   in inits/destructors that should be done directly because they are
   on the local object.
 - Removes the "SGF.AlwaysDirectStoredPropertyAccess" bool in SILGen,
   which was the source of the problem above and was otherwise problematic.

This will get a bit simpler when -enable-new-objc-properties rolls out. 


Swift SVN r12967
2014-01-26 07:23:42 +00:00
Joe Groff
fc4ecc92c7 SILGen: Don't box 'let' bindings inside 'switch' patterns.
If all of the bindings in a pattern column are 'let' bindings, don't box the binding. If there is any 'var' in the column, conservatively fall back to binding a box. Factor out the logic for producing an initialization for a variable into an new emitInitializationForVarDecl method that SILGenPattern can use. Add a 'copyInto' method to RValue that can bind a copy of an rvalue to an Initialization.

This doesn't use Chris's new +0 ManagedValue optimization yet, so we end up with an extra copy_value when the value is bound that might still be avoidable.

Swift SVN r12903
2014-01-24 05:33:14 +00:00
Chris Lattner
fc16e213fe Remove DestroyAddr/CleanupRValue/CleanupMaterializedValue.
All of the clients can use enterDestroyCleanup.


Swift SVN r12830
2014-01-23 00:49:36 +00:00
Chris Lattner
2cf6d814c7 more tidying:
- Strength reduce the interface to LogicalPathComponent::getMaterialized
   to now just return a SILValue for the address.  The full "Materialize"
   structure hasn't been needed since MaterializeExpr got removed.
 - Move 'struct Materialize' out of SILGen.h into SILGenLValues.cpp now
   that it is only used for logical property materialization.
 - Drop the dead 'loc' argument on DeallocStackCleanup.  The location is
   already specified when the cleanup is emitted.


Swift SVN r12827
2014-01-23 00:43:20 +00:00
Chris Lattner
63784ca41c remove the ManagedValue::Unmanaged marker, lets just use ManagedValue::forUnmanaged()
instead, which is shorter.  It is better to have one way to do things than two.


Swift SVN r12710
2014-01-22 06:26:34 +00:00
Chris Lattner
f5b85341a1 Expand out the "isComputed" property in AbstractStorageDecl to be an enum
with two kinds, and some more specific predicates that clients can use.

The notion of 'computed or not' isn't specific enough for how properties
are accessed.  We already have problems with ObjC properties that are 
stored but usually accessed through getters and setters, and a bool here
isn't helping matters.

NFC.



Swift SVN r12593
2014-01-20 18:16:30 +00:00
Chris Lattner
6b93fc2034 simplify code now that getGetter/SetterType have been hoisted
to AbstractStorageDecl, NFC.


Swift SVN r12575
2014-01-20 15:37:20 +00:00
Chris Lattner
0292a5f34e merge objc thunk generation for subscripts into the thunk
generation logic for vardecls.  They are the same, subscripts
are always just computed.


Swift SVN r12542
2014-01-18 20:28:49 +00:00
Joe Groff
0776c4b6b8 SIL: Reorient function type lowering toward interface types.
Lower types for SILDeclRefs from the interface types of their referents, dragging the old type along for the ride so we can still offer the context to clients that haven't been weaned off of it. Make SILFunctionType's interface types and generic signature independent arguments of its  Derive the context types of SILFunctionType from the interface types, instead of the other way around. Do a bunch of annoying inseparable work in the AST and IRGen to accommodate the switchover.

Swift SVN r12536
2014-01-18 19:42:02 +00:00
Chris Lattner
a8d8e14254 introduce AbstractStorageDecl. Right now, just
move OverriddenDecl and usesObjCGetterAndSetter() up to it.
This allows usesObjCGetterAndSetter to subsume the logic
for subscript decls as well.



Swift SVN r12535
2014-01-18 19:08:39 +00:00
Chris Lattner
bb3c0d52dc Unfortunately, two conflated NFC patches:
1) Revert my change to give DeclContext a dump method, it confuses the debugger.
2) Refactor SILGen::requiresObjCPropertyEntryPoints out to 
   VarDecl::usesObjCGetterAndSetter.


Swift SVN r12526
2014-01-18 02:00:23 +00:00
John McCall
a1b469ed2f ExplosionKind -> ResilienceExpansion. NFC.
Swift SVN r12364
2014-01-16 00:25:29 +00:00
Chris Lattner
7db7d4a813 Three changes:
- constify the Initialization::getAddressOrNull/hasAddress functions.
 - Teach the initialization logic that 'let' initializations can be
   split into tuple subelements by address (i.e. i
   getSubInitializationsForTuple works on them) when the let decl has
   a backing memory allocation.
 - Teach LetValueInitialization to produce a backing memory object
   for address-only tuple arguments, since they are passed as multiple
   SILArguments and need some place in memory to reassemble them.

The collection fixes a bad miscompilation where the RValue logic emitted
a temporary (through getSingleValue) which we then accidentally lifetime
extended for the duration of the argument.  This led to bad things when
the temporary got destroyed too early.

Thanks to MichaelG for helping narrow down this problem, it initially looked
like a sil optimizer bug.


Swift SVN r12234
2014-01-13 17:24:40 +00:00
Chris Lattner
36b765eac9 Switch the rest of the ArgumentInitVisitor cases over to using
RValue::forwardInto.  While this isn't directly related to my
current problem, it is obvious goodness.  This avoids materializing
"as a single value" the argument values, which means that we don't
create a temporary holding the value when we have tuple elements,
allowing us to generate more efficient code (particularly for
address-only tuple).



Swift SVN r12233
2014-01-13 17:02:42 +00:00
Chris Lattner
c058f51fc9 LetValueInitialization::bindValue and bindArgument do the same thing,
so merge them.  ArgumentInitVisitor is very very similar to 
InitializeTupleValues, so start using RValue::forwardInto in
ArgumentInitVisitor for some cases instead of duplciating logic.  This
gives ArgumentInitVisitor some extra magic for lets which I'll be using
in a bit.


Swift SVN r12231
2014-01-13 16:53:01 +00:00
Chris Lattner
8f467059b0 Remove the return value of ArgumentInitVisitor, nothing consumes it.
Doing so makes it plain that the empty tuple case doesn't need to 
synthesize a tupleinst to return.  Eliminating this gets rid of the
weird dead empty tuple you may have seen get dropped in the prolog
of functions taking no arguments.

Also, push around RValue handling a bit in ArgumentInitVisitor, NFC
for this part.



Swift SVN r12230
2014-01-13 16:39:03 +00:00
Chris Lattner
76bea38d3e emit debug_value for 'self' in class destructors of all sorts, and
rename emitDestructorProlog -> emitSelfDecl, since that is what it 
does.


Swift SVN r12213
2014-01-12 06:44:11 +00:00
Doug Gregor
3524a79fa0 Emit .cxx_destruct for destruction of ivars in Objective-C-derived classes.
The Objective-C runtime executes the .cxx_destruct method after the
last -dealloc has executed when destroying an object, allowing the
instance variables to remain live even after the subclass's
destructor/-dealloc has executed, which is important for memory
safety. This fixes the majority of <rdar://problem/15136592>.

Note that IRGenModule::getAddrOfIVarDestroyer() contains  an egregious
hack to find the ivar destructor SIL function via a linear
search. We need a better way to find SIL functions that we know exist,
because LinkEntity does not suffice.

Swift SVN r12206
2014-01-12 00:17:42 +00:00
Chris Lattner
f8570072cd Teach SILGen to generate debug_value and debug_value_addr for 'let' decls.
Right now I unconditionally generate these, because silgen doesn't have
easy access to whether "-g" is passed or not.  We can either make SILGen
know about this, or we can have optimizations strip these uses out.
Opinions welcome!

As an example, something like:
protocol P { func bonk() }

func foo(let a : Int, let b : P) -> Int {
  let x = a
  b.bonk()
  return x
}

compiles into:

sil @_TF1t3fooFT1aSi1bPS_1P__Si : $@thin (Int64, @in P) -> Int64 {
bb0(%0 : $Int64, %1 : $*P):
  debug_value %0 : $Int64  // let a               // id: %2
  debug_value_addr %1 : $*P  // let b             // id: %3
  debug_value %0 : $Int64  // let x               // id: %4
  %5 = project_existential %1 : $*P to $*@sil_self P // user: %7
  %6 = protocol_method %1 : $*P, #P.bonk!1 : $@cc(witness_method) @callee_owned (@inout Self) -> () // user: %7
  %7 = apply %6(%5) : $@cc(witness_method) @callee_owned (@inout Self) -> ()
  destroy_addr %1 : $*P                           // id: %8
  return %0 : $Int64                              // id: %9
}



Swift SVN r12193
2014-01-11 02:08:58 +00:00
Doug Gregor
f05263d4fe Objective-C's -dealloc is a deallocating destructor, not a destroying destructor.
Because Objective-C doesn't have the notion of a destroying
destructor, this is a matter of cleanliness rather than
correctness. Still, it's better not to lie.


Swift SVN r12160
2014-01-10 23:08:42 +00:00
Doug Gregor
2bf20ccbc3 Add the ClassDecl::getDestructor() convenience method.
Swift SVN r12140
2014-01-10 22:32:49 +00:00
Doug Gregor
5f1b8618f4 Make the SILDeclRef for a destroying destructor store a DestructorDecl.
SILDeclRef was previously storing the ClassDecl for this case, because
semantic analysis didn't guarantee that a DestructorDecl was always
present. It is now, and this representation makes more sense.

Swift SVN r12122
2014-01-10 17:33:57 +00:00
Doug Gregor
03edef185a Emit a -dealloc method for Swift classes whose root class is defined in Objective-C.
Teach SILGen to emit a -dealloc method that runs user code, destroys
instance variables, and then calls up to the superclass dealloc. Teach
IRGen to deal with Objective-C destructor methods and add -dealloc to
the instance method table.

There are a few things still wrong here: we're emitting both a Swift
destructor and -dealloc, even though only one of them should ever
actually be used. Additionally, instance variables shouldn't be
destroyed in -dealloc, but in .cxx_destruct, so they persist until the
last of the -dealloc methods is invoked.

Swift SVN r12115
2014-01-10 05:07:33 +00:00
Doug Gregor
6e6eb57857 Don't delay the emission of the destructor in SILGen.
Swift SVN r12096
2014-01-09 18:15:12 +00:00
Doug Gregor
fbb5681110 SILGen doesn't need to cope with missing DestructorDecls.
Swift SVN r12095
2014-01-09 18:15:12 +00:00
John McCall
5da6defa1f Clean up the linkage model and the computation of linkage.
In general, this forces SILGen and IRGen code that's grabbing
a declaration to state whether it's doing so to define it.

Change SIL serialization to serialize the linkage of functions
and global variables, which means also serializing declarations.

Change the deserializer to use this stored linkage, even when
only deserializing a declaration, and to call a callback to
inform the client that it has deserialized a new entity.

Take advantage of that callback in the linking pass to alter
the deserialized linkage as appropriate for the fact that we
imported the declaration.  This computation should really take
advantage of the relationship between modules, but currently
it does not.

Swift SVN r12090
2014-01-09 08:58:07 +00:00
Joe Groff
b2f0b90ba2 SIL: Switch to SILFunctionType interface types in easy-to-reach places.
In nongeneric contexts, or contexts where we only care about the indirectness of parameters or have already substituted the generic parameters for a function, the interface types are interchangeable, so just switch over.

Swift SVN r12044
2014-01-08 04:48:29 +00:00
Joe Groff
03d2dd7b10 Add an addGenericSignature method to ArchetypeBuilder.
Factors out the loops for adding all the generic parameters and requirements of a signature to an ArchetypeBuilder. NFC.

Swift SVN r11996
2014-01-07 17:56:36 +00:00
Joe Groff
c47820ff3d Add a convenience method for reparenting a GenericParamList.
NFC.

Swift SVN r11995
2014-01-07 17:56:36 +00:00
Joe Groff
44fb729830 SIL: Use only interface types in the verifier.
Treat the interface types of SILFunctionTypes as the canonical representation in the verifier. Do a bunch of supporting and annoyingly irreducible work to enable this:

- Stop trying to uncurry generic parameter lists during type lowering and preserve the structure of AST GenericParamLists. This makes mapping dependent types into contexts easier.
- Properly walk generic parameter lists at all depths when grooming substitution vectors for use with substGenericArgs interfaces.
- Reseat the generic parameter lists created for protocol_method results so that we don't expect the outer Self archetype to be unbound; it's provided by the extra data of the result.
- Hack SILFunctionType serialization never to use a decl reference when serializing its generic param list. When this happens, we get incorrect archetypes. This is a gross hack, but when we're able to jump all the way to interface types, it can go away.

Putting these ducks in a row nicely un-XFAILs TextFormatting.swift.

Swift SVN r11989
2014-01-07 06:50:20 +00:00
John McCall
bb63d2312b Hide the constructors of SILFunction/SILGlobalVariable
behind factory methods.

Swift SVN r11967
2014-01-07 00:22:15 +00:00
John McCall
cca5fb3bc0 Remove name mutators for SILFunction and SILGlobalVariable.
Swift SVN r11963
2014-01-06 23:32:52 +00:00
Chris Lattner
d196abc8a4 When binding address-only let declarations to let arguments, use the
buffer being passed in, instead of creating a temporary one and copying
into it.

For:

func generic_identity<T>(let a : T) -> T {
  return a
}

We now get:

sil @_TF2t216generic_identityU__FT1aQ__Q_ : $@thin <$T_0_0> (@out $T_0_0, @in $T_0_0) -> () {
bb0(%0 : $*T, %1 : $*T):
  copy_addr [take] %1 to [initialization] %0 : $*T // id: %2
  %3 = tuple ()                                   // user: %4
  return %3 : $()                                 // id: %4
}

We used to produce:

sil @_TF2t216generic_identityU__FT1aQ__Q_ : $@thin <$T_0_0> (@out $T_0_0, @in $T_0_0) -> () {
bb0(%0 : $*T, %1 : $*T):
  %2 = alloc_box $T  // let a                     // users: %5, %4, %3
  copy_addr [take] %1 to [initialization] %2#1 : $*T // id: %3
  copy_addr %2#1 to [initialization] %0 : $*T     // id: %4
  strong_release %2#0 : $Builtin.ObjectPointer    // id: %5
  %6 = tuple ()                                   // user: %7
  return %6 : $()                                 // id: %7
}

and then optimize it into:

sil @_TF2t216generic_identityU__FT1aQ__Q_ : $@thin <$T_0_0> (@out $T_0_0, @in $T_0_0) -> () {
bb0(%0 : $*T, %1 : $*T):
  %2 = alloc_stack $T  // let a                   // users: %5, %3, %4
  copy_addr [take] %1 to [initialization] %2#1 : $*T // id: %3
  copy_addr [take] %2#1 to [initialization] %0 : $*T // id: %4
  dealloc_stack %2#0 : $*@local_storage T         // id: %5
  %6 = tuple ()                                   // user: %7
  return %6 : $()                                 // id: %7
}



Swift SVN r11941
2014-01-06 19:54:04 +00:00
Chris Lattner
5c5c7829da Rework SILGen of address-only let declarations. Previously, we would just drop them
into heap boxes like var decls.  Now we drop them into stack temporaries like rvalues.

No semantic change goes with this.



Swift SVN r11939
2014-01-06 19:42:50 +00:00
Chris Lattner
1523798f5f simplify emitDestructorProlog, NFC.
Swift SVN r11930
2014-01-06 17:21:19 +00:00
Chris Lattner
ac8958a598 remove overly conservative code: destructors always have self decls.
Value types don't have dtors.


Swift SVN r11911
2014-01-04 22:48:44 +00:00