Commit Graph

6754 Commits

Author SHA1 Message Date
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
8d2142dcd0 Tweak the "uses Objective-C allocation" predicate.
We use Objective-C allocation for classes whose root was defined in
Objective-C. Any Swift class whose root is defined in Swift will use
Swift's allocation routines instead.

Plus some minor cleanup in advance of implementing -dealloc.


Swift SVN r12104
2014-01-09 23:09:59 +00:00
Chris Lattner
b87dd57079 implement SILGen and parser support for 'type' let's, implementing rdar://15767586.
General compiler support is missing for 'type' properties on classes, and lets don't
work either, but at least we have a nice diagnostic now.
also, rename static -> type in a few internal diagnostic names.



Swift SVN r12102
2014-01-09 22:42:24 +00:00
Doug Gregor
3f09a9891d Add an [objc] bit to the alloc_ref instruction to indicate when to use Objective-C's +allocWithZone:.
Swift SVN r12099
2014-01-09 21:35:55 +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
Doug Gregor
622c2f6ce8 Destroy instance variables after chaining to the superclass destructor.
This is the dual of our initialization story: delay the destruction of
instance variables until after the execution of the superclass
destructor, so that memory safety is preserved even if the superclass
destructor calls a method on teh subclass.

Swift SVN r12089
2014-01-09 05:59:33 +00:00
Doug Gregor
68bcb0d2af Move emission of instance variable initial values from Sema to SILGen.
Hacking up ASTs to perform this emission was always gross; move it
over to SILGen. No functionality change.

Swift SVN r12067
2014-01-08 21:38:23 +00:00
Sean Callanan
f3e81ec9d2 Values produced by the debugger client are
lvalues.  (They're all addresses.)  We need to
mark the resulting ManagedValues as lvalues as
well, so that SILGen emits loads for them
correctly.


Swift SVN r12066
2014-01-08 21:34:23 +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
8f6a58b998 SIL: Split address-only enum dispatch from destructive projection.
Split 'destructive_switch_enum_addr' into separate 'switch_enum_addr' and 'take_enum_data_addr' instructions. This should unblock some optimization work we would like to do with enums.

Swift SVN r12015
2014-01-07 22:58:21 +00:00
Doug Gregor
23fff50b69 Use UTF-16 string literals when the (builtin) string literal type supports it.
Make the compiler fully support both UTF-8 and UTF-16 string
literals. A (standard-library-defined) string type (such as String)
that wants UTF-16 string literals should conform to the
BuiltinUTF16StringLiteralConvertible protocol; one that wants UTF-8
string literals should conform to the BuiltinStringLiteralConvertible
protocol.

Note that BuiltinUTF16StringLiteralConvertible inherits from
BuiltinStringLiteralConvertible, so a string type that wants UTF-16
string literals also has to implement support for UTF-8. The UTF-16
entry point is preferred when the compiler knows that UTF-16 is
supported. This only tends to happen when we have a generic parameter
that is required to conform to StringLiteralConvertible, e.g.,

  func f<T: StringLiteralConvertible>() {
    var t: T = "Hello, World!" // uses UTF-8 entry point
  }

because the UTF-8 entry point is the only one guaranteed to be available.



Swift SVN r12014
2014-01-07 22:55:07 +00:00
Joe Groff
530e948cb2 SIL: Rename 'enum_data_addr' to 'init_enum_data_addr'.
Emphasize the fact that this address is only intended for initialization. When we split destructive_switch_enum_addr, there will be another similar instruction for destructively taking the payload out of an already-initialized enum.

Swift SVN r12000
2014-01-07 18:40:00 +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
Doug Gregor
1cf3bdbe8c Use objc_msgSendSuper[Stret] for foreign initializer delegation.
Introduce the SIL instruction peer_method, which references a method
in the given class or one of its superclasses (but not a subclass). It
IRGen's to objc_msgSendSuper[Stret] (vs. super_method IRGen'ing to
objc_msgSendSuper[Stret]2 for superclass lookup).

Use peer_method for initializer delegation to a foreign initializer
(i.e., an init-family method written in Objective-C) to close the
safety loophole introduced by initializer delegation in r11965. The
loophole still exists, but can only be triggered from Objective-C.

Teach definite initialization that peer_method really isn't a use of
self.



Swift SVN r11992
2014-01-07 17:33:41 +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
Chris Lattner
408011ae59 Implement silgen and DI support for delegating initializers
in value types.


Swift SVN r11982
2014-01-07 06:04:59 +00:00
Chris Lattner
7e69153d14 merge the MUI flags for delegating self: there is no need to
differentiate the 'derived' and 'base' cases for delegating
initializers.  One is better than two.


Swift SVN r11981
2014-01-07 05:39:21 +00:00
John McCall
e507341c1b SILModule lookup: now with that magic O(1) flavor.
Swift SVN r11978
2014-01-07 01:21:10 +00:00
Doug Gregor
b0440645c8 Diagnose attempts to both delegate to a peer and chain to a superclass initializer.
While we're here, cache the result of looking for delegation or
chaining calls within ConstructorDecl. Only Sema and SILGen for the
body of the constructor care, so we don't serialize this
information. Rather, we have a faster-path to recompute it should
someone ask again after it was cached.


Swift SVN r11969
2014-01-07 01:00:49 +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
Doug Gregor
90f3b554ee Initializer delegation to a foreign initializer must go through class_method.
Initiializer delegation in Swift always calls a peer initializer
directly. However, there are no direct calls for methods defined in
Objective-C, so go through Objective-C's message send. This is a
potential safety hole, because we could end up in a subclass's
initializer.


Swift SVN r11965
2014-01-06 23:51:07 +00:00
Chris Lattner
6406e54138 teach silgen to produce the right mark_unintialized tags for delegating inits.
Swift SVN r11964
2014-01-06 23:33:06 +00:00
John McCall
cca5fb3bc0 Remove name mutators for SILFunction and SILGlobalVariable.
Swift SVN r11963
2014-01-06 23:32:52 +00:00
Doug Gregor
c498a9fdb3 SIL generation for initializer delegation for structs and enums.
Much of the fun here comes from the need to delegate to the allocating
constructor for structs and enums (which have no initializing
constructors). The AST doesn't (and shouldn't) know about this, so
SILGen has to turn cope with the transformation.



Swift SVN r11949
2014-01-06 20:38:20 +00:00
Chris Lattner
f4d12a2b6e teach SILGenFunction::manageBufferForExprResult about address-only let buffers, so that
we don't emit a shadow copy in some cases where we didn't realize we successfully emitted
into our buffer.


Swift SVN r11948
2014-01-06 20:37:45 +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
Doug Gregor
5f1e886907 Wrap delegating initialization ('self.init' calls) in RebindSelfInConstructorExpr.
As with chaining initialization ('super.init' calls), wrapping the
delegating initialization in RebindSelfInConstructorExpr ensures that
'self' gets overwritten by the result of delegation.

Note that I'd much prefer that RebindSelfInConstructorExpr be
introduced by the type checker (not the parser). That cleanup will
follow.



Swift SVN r11932
2014-01-06 18:25:29 +00:00
Chris Lattner
1523798f5f simplify emitDestructorProlog, NFC.
Swift SVN r11930
2014-01-06 17:21:19 +00:00
Chris Lattner
0d3e661ea4 add a new ManagedValue::copyUnmanaged. It is exactly the same
as ::copy, but doesn't have the assertion that the copied-from
value has a cleanup associated with it.  Various bits of silgen
are trafficing in SILValue's when they are known to be live, and
this allows us to use the centralized "copy" instead of rolling 
it ourselves inline.


Swift SVN r11929
2014-01-06 17:15:48 +00:00
Chris Lattner
ed7a3fd4ae tweak comment.
Swift SVN r11928
2014-01-06 17:12:52 +00:00
Dave Abrahams
ff076dc3f2 [stdlib] "Generator" => "Stream"
This is step 1 of implementing the new Container/Sequence/Stream
protocols.  See test/Prototypes/Container.swift for the complete picture
of where this is headed.

Swift SVN r11924
2014-01-06 16:40:32 +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
Chris Lattner
dcd8581f6c Fix RValueEmitter::visitDeclRefExpr to do the right thing when SGFContext
specifies a destination to plop the loaded decl into.  This isn't actually
being used, just something I noticed in flight.


Swift SVN r11910
2014-01-04 21:11:29 +00:00
John McCall
12b1c9daf7 Add support for multiple encodings to the string_literal
SIL instruction.  Implement IR-gen support for same.  Fix
the parsing of SIL string literals to properly unescape them.

SIL-gen still emits UTF8 literals unconditionally.

Swift SVN r11904
2014-01-04 11:42:57 +00:00
John McCall
17a8acfe43 Remove the vestiges of the SILGen mangler.
Swift SVN r11895
2014-01-04 09:11:15 +00:00
John McCall
8d68478c77 Move the SILDeclRef mangler to the SIL library. NFC.
Swift SVN r11894
2014-01-04 09:11:14 +00:00
John McCall
4355a21a62 Mangle closures using their context and discriminator.
Swift SVN r11885
2014-01-04 04:37:57 +00:00
Chris Lattner
b61a6fd946 Rework AST and SILGen of properties and subscripts to take advantage of the new mutability model.
- Change the AST for get/set functions to take self @inout only when they 
  are @mutating.  Setters default to @mutating, but can be explicitly marked 
  @!mutating. Getters default to not mutating, but can be marked @mutating.  
  This causes self to follow.
- Change sema to handle semantic analysis of a.y (and subscripts) based on
  whether the computed type of a allows mutation (which is when 'a' is an 
  lvalue, or both the getter and setter are non-mutating).  When both of
  these conditions fail, 'a.y' has rvalue type, and is thus non-mutable.
- Rework silgen of lvalues to handle this: now properties and subscripts 
  can have rvalues as bases, which means that all the lvalue machinery needs 
  to be able to handle the full generality of base expressions (which is 
  what my recent patches have been paving the way towards).
- Rework silgen of rvalues to similarly handle rvalue bases.
- Rework silgen of both to handle the case where the AST has found a base
  expression that is an lvalue, but where only a non-mutating getter or
  setter is needed.  Right now, we just emit a load of the lvalue, but
  it would result in better code to not require the base be an lvalue at 
  all (todo).

The upshot of all of this is that we are doing *much* less AST-level 
materialization (MaterializeExpr goes down), we generate a lot better SIL
out of SILGen in many cases, and 'self' being an rvalue in properties and
subscripts means that we correctly reject code like the examples in
test/Sema/immutability.swift.



Swift SVN r11884
2014-01-04 04:27:51 +00:00
Chris Lattner
1ca2722e83 introduce ManagedValue::forLValue(x) as a helper function for
making LValue ManagedValues, and switch SILGenLValue to use
this form of managed value consistently for lvalues, instead of
using unmanaged values in some cases.  NFC. 


Swift SVN r11878
2014-01-04 00:45:05 +00:00
Chris Lattner
1b86610e1a switch LValueWriteback to use ManagedValue now that it has access to it.
Swift SVN r11877
2014-01-04 00:29:36 +00:00
Chris Lattner
fdc3e70d6d move SILGenFunction::Writeback out of line and rename it to LValueWriteback
in preparation for more work on it.  NFC.


Swift SVN r11876
2014-01-04 00:17:09 +00:00
Dave Zarzycki
31d3c37141 Revert r11855: "simplify some code, managed value does the right thing for us here."
This was causing the tests to spin forever.

Swift SVN r11864
2014-01-03 19:37:44 +00:00
Doug Gregor
1ee513e7e8 Use Builtin.Word for array lengths, string literal lengths, etc.
This eliminates a number of 64-bit integer/64-bit pointer assumptions
in the type checker and SILGen.


Swift SVN r11863
2014-01-03 18:53:01 +00:00
Joe Groff
9631362689 SILGen: Reparent the inner context generic params of witness functions.
For a witness to a generic requirement, the generic parameters of the requirement should be parented to the generic parameters of the conformance, not to the 'Self' parameter list of the original protocol. The generic signature conversion in SILFunctionType construction revealed this improper nesting because it was introducing unused "$T_0_0" parameters into witnesses in nongeneric conformances.

Swift SVN r11861
2014-01-03 18:18:15 +00:00
Chris Lattner
5b394f67f3 sink SILDeclRef's down into emitGetAccessor/emitSetAccessor, which are
the things that apparently care about it.  The LValue/RValue machinery is 
happy to pass around the VarDecl/SubscriptDecl for the entity being
referenced, and this simplifies things.


Swift SVN r11857
2014-01-03 07:19:40 +00:00
Chris Lattner
d40295fccd simplify some code, managed value does the right thing for us here.
Swift SVN r11855
2014-01-03 06:38:27 +00:00