Commit Graph

794 Commits

Author SHA1 Message Date
John McCall
1b3771c10b Fix a pair of bugs with rvalue addressed subscripting.
Swift SVN r22258
2014-09-24 09:07:30 +00:00
John McCall
b0a843630e Add some SILGen tests for addressors and fix some
bugs involving non-member variables with addressors.

Swift SVN r22256
2014-09-24 07:18:20 +00:00
Devin Coughlin
b727b6d932 Treat potentially unavailable global variable references as optional.
This patch adds the ability (-enable-experimental-unavailable-as-optional) to
treat potentially unavailable declarations as if they had optional types. For
the moment, this is only implemented for global variables.

The high-level approach is to (1) record the potential unavailability of a
declaration reference in the overload choice during constraint generation; (2)
treat the declaration as if it had an optional type during overload resolution
(this is similar to how optional protocol members are treated); and (3) add an
implicit conversion (UnavailableToOptionalExpr) during constraint application
to represent the run-time availability check and optional injection.

This patch does not implement SILGen for UnavailableToOptionalExpr.


Swift SVN r22245
2014-09-24 00:07:46 +00:00
John McCall
defaf07046 SILGen support for addressors.
Addressors now appear to pass a simple smoke-test; in
order to allow some parallel developement, I'll be
committing actual SILGen tests for this later and
separately.

Swift SVN r22243
2014-09-23 23:46:52 +00:00
Erik Eckstein
c16c510167 Set SILLinkage according to visibility.
Now the SILLinkage for functions and global variables is according to the swift visibility (private, internal or public).

In addition, the fact whether a function or global variable is considered as fragile, is kept in a separate flag at SIL level.
Previously the linkage was used for this (e.g. no inlining of less visible functions to more visible functions). But it had no effect,
because everything was public anyway.

For now this isFragile-flag is set for public transparent functions and for everything if a module is compiled with -sil-serialize-all,
i.e. for the stdlib.

For details see <rdar://problem/18201785> Set SILLinkage correctly and better handling of fragile functions.

The benefits of this change are:
*) Enable to eliminate unused private and internal functions
*) It should be possible now to use private in the stdlib
*) The symbol linkage is as one would expect (previously almost all symbols were public).

More details:

Specializations from fragile functions (e.g. from the stdlib) now get linkonce_odr,default
linkage instead of linkonce_odr,hidden, i.e. they have public visibility.
The reason is: if such a function is called from another fragile function (in the same module),
then it has to be visible from a third module, in case the fragile caller is inlined but not
the specialized function.

I had to update lots of test files, because many CHECK-LABEL lines include the linkage, which has changed.

The -sil-serialize-all option is now handled at SILGen and not at the Serializer.
This means that test files in sil format which are compiled with -sil-serialize-all
must have the [fragile] attribute set for all functions and globals.

The -disable-access-control option doesn't help anymore if the accessed module is not compiled
with -sil-serialize-all, because the linker will complain about unresolved symbols.

A final note: I tried to consider all the implications of this change, but it's not a low-risk change.
If you have any comments, please let me know.



Swift SVN r22215
2014-09-23 12:33:18 +00:00
Joe Groff
6cd5123712 SILGen: Use the formal type when taking dynamicTypes of values.
The metatype is associated with the formal AST type of the value, not whatever lowered SIL type we happen to have lying around. Adjust the SIL verifier to check that value_metatype instructions produce a metatype for which the instance is a potential lowering rather than by exact type match. This lets us take the metatype of metatypes (and incidentally, of functions, and of tuples thereof), fixing rdar://problem/17242770.

Swift SVN r22202
2014-09-23 02:56:48 +00:00
Anna Zaks
5fca94b1b2 [SIL Unreachability] Remove spurious warnings in the deinit method.
Mark the deinitializer auto-generated code as cleanup.

radar://18225133

Swift SVN r22111
2014-09-19 01:40:08 +00:00
John McCall
8cae5ba1d0 Generalize 'isDirectPropertyAccess' to allow for
direct (i.e. non-polymorphic) access to accessor
functions, and use this in materializeForSet for
computed properties.

Swift SVN r22059
2014-09-18 05:51:32 +00:00
Joe Groff
0279b570ac SILGen: Class-constrained generic bases are +1 for setters too.
Factor out the common logic here for future consistency. Fixes rdar://problem/18275858.

Swift SVN r22057
2014-09-18 02:31:11 +00:00
Joe Groff
2d792ba60b SILGen: Class-constrained generic property getters have a +1 base.
Fixes rdar://problem/18294424 and its many dupes.

Swift SVN r22048
2014-09-17 22:53:33 +00:00
John McCall
75050f8166 Generate an implicit 'materializeForSet' accessor
along with getters and setters.

Just generate it for now.

Swift SVN r22011
2014-09-17 08:08:03 +00:00
Joe Groff
1b3790034b SILGen: Don't try to emit closures in stored property initializers multiple times.
We emit initializer expressions once for every init definition, which caused us to try to emit the same symbol for closure bodies in initializers multiple times. Fixes rdar://problem/17971234.

Swift SVN r21997
2014-09-16 23:34:56 +00:00
Joe Groff
1c4e08a9c8 Fix up a tangle of issues related to reabstraction.
- A spot fix in SILGen for reabstracting the result of a downcast, which fixes checked casts to function types.
- Associate the layout information in type metadata records with the most abstract representation of the type. This is the correct thing to do in cases where we need the metadata as a tag for an opaque value--if we store a value in an Any, or pass it as an unconstrained generic parameter, we must maximally reabstract it. This fixes the value semantics of existentials containing trivial metatypes.
- To ensure that we get runtime layout of structs and enums correct when they contain reabstractable types, introduce a "metadata for layout" concept, which doesn't need to describe the canonical metadata for the type, but only needs to describe a type with equivalent layout and value semantics. This is a correctness fix that allows us to correctly lay out generic types containing dependent tuples and functions, and although we don't really take advantage of it here, it's also a potential runtime performance win down the road, because we could potentially produce direct metadata for a primitive type that's layout-equivalent with a runtime-instantiated type. To aid in type safety here, push SILType deeper into IRGen in places where we potentially care about specific representations of types.
- Finally, fix an inconsistency between the runtime and IRGen's concept of what spare bits unmanaged references and thick metatypes have.

Together, these fixes address rdar://problem/16406907, rdar://problem/17822208, rdar://problem/18189508, and likely many other related issues, and also fixes crash suite cases 012 and 024.

Swift SVN r21963
2014-09-16 01:44:34 +00:00
Manman Ren
a952c556c9 [Global Opt] replace GlobalAddrInst with SILGlobalAddrInst.
Update SILGen to create SILGlobalVariable and SILGlobalAddrInst instead of
GlobalAddrInst. When we see a definition for a global variable, we create
the corrsponding SILGlobalVariable definition.

When creating SILGlobalVariable from a global VarDecl, we mangle the global
VarDecl in the same way as we mangle it at IRGen. The SILLinkage is also
set in the same way as we set it at IRGen.

At IRGen, we use the associated VarDecl for SILGlobalVariable if it exists,
to have better debugging information.

We set the initializer for SILGlobalVariable definition only.

We also handle SILGlobalAddrInst in various SILPasses, in the similar way
as we handle GlobalAddrInst.

rdar://15493694


Swift SVN r21887
2014-09-11 20:00:39 +00:00
Devin Coughlin
0d7996f4ee Add parsing of availability query expressions (#os(...))
This patch adds a new 'pound_os' token, a new case for it in parseExprPostfix, and parsing of platform version constraints, e.g., OSX >= 10.10. 

It also adds enough type checking and SILGen to get the parsing tests to run without triggering "Unimplemented" assertions.


Swift SVN r21865
2014-09-11 02:59:05 +00:00
Joe Groff
e004ad7df4 SIL: Carry AST types through init_existential instructions.
This is necessary to be able to properly stash values with nontrivial lowerings, such as metatypes and functions, inside existential containers. Modify SILGen to lower values to the proper abstraction level before storing them in an existential container. Part of the fix for rdar://problem/18189508, though runtime problems still remain when trying to actually dynamicCast out a metatype from an Any container.

Swift SVN r21830
2014-09-10 05:56:36 +00:00
Devin Coughlin
ab563d6ef1 Add availability query expression (#os(...))to the AST
This commit adds a new expression (AvailabilityQueryExpr) and a single kind of
specification for when a block of code or function is available
(VersionConstraintAvailabilitySpec). We may add more kinds of specifications
in the future. At the moment, the AvailabilityQueryExpr allows only a
single platform to be queried; I will add support for multiple platforms
in a later commit.

This commit contains just the added AST nodes; no parsing, type checking, etc.
I’ve added assert(false && “Unimplemented”) for places where support for
AvailabilityQueryExpr will need to be added later.



Swift SVN r21760
2014-09-06 02:11:02 +00:00
Erik Eckstein
f6c68fb1d5 SILGen: fix crash in case a function is specialized which contains an Array/Dictionary up- or downcast.
The apply insts for the call to the collection-casts were generated with substitutions w/o an ArcheType.



Swift SVN r21740
2014-09-05 14:07:25 +00:00
Joe Groff
53bbefbe42 SILGen: Thread direct access through all uses of emitRValueForDecl.
Swift SVN r21715
2014-09-04 18:11:04 +00:00
Doug Gregor
3a7b2c3622 Revert r21706 "Stop checking for 'nil' returns from non-failable Objective-C initializers."
Swift SVN r21712
2014-09-04 17:27:22 +00:00
Doug Gregor
a775b4004a Stop checking for 'nil' returns from non-failable Objective-C initializers.
This eliminates the workaround for propagating 'nil' from Objective-C
initializers up through Swift initializers, now that failable
initializers handle this propagation.


Swift SVN r21706
2014-09-04 15:55:56 +00:00
Joe Groff
4be861da99 SILGen: Don't crash when a 'static let' models a 'class var { get }' protocol requirement.
A couple of issues here:

- We didn't apply the base metatype when emitting calls to the getter for static properties, causing a crash when references to the accessor are emitted (such as when modeling a protocol requirement)
- We didn't propagate isDirectPropertyAccess through emitRValueDecl, causing the getter to recursively call itself when it was emitted successfully.

Together these fix rdar://problem/17986478.

Swift SVN r21695
2014-09-04 01:38:25 +00:00
Joe Groff
8f52302f8e SILGen: Implement artificial main for NSApplicationMain.
We're not sure how to trigger this from source code yet, so trigger it with a hidden -emit-NSApplicationMain frontend flag for now.

Swift SVN r21563
2014-08-29 04:45:47 +00:00
Joe Groff
fc5e29284f SILGen: Implement failable class initializers.
Set up the same structure of a failure block and common exit point for class initializers that fail, and fix the codegen for the allocating entry point to use the correct return type when calling a failable initializer.

Swift SVN r21536
2014-08-28 17:41:40 +00:00
Roman Levenstein
97014172b7 [sil-combine] String literal concatenation optimization. Constant-fold concatenation of string literals known at compile-time.
Addresses rdar://17033696.

Swift SVN r21526
2014-08-28 11:33:21 +00:00
Joe Groff
88da4626b8 SILGen: Clean up 'self' before entering the failure block of a failing initializer.
A couple reasons for this:

- How 'self' gets cleaned up is dependent on where the failure occurs. If we propagate failure from a class initializer, the failed 'super.init' or 'self.init' has already cleaned up the object, so we only need to deallocate the box. In cases where we fail explicit, we release 'self', which works out because we're only supporting failure with a fully-initialized object for now.
- This lets us set the location info for the cleanup to the AST node that instigated failure, giving better QoI for DI failures (such as failing with a partially-initialized object).

Swift SVN r21505
2014-08-28 01:13:49 +00:00
Joe Groff
c1456ff671 SILGen: Handle initializers that unconditionally fail.
A failable initializer that ended in an unconditional 'return nil' was leaving the exit basic block unemitted, since we exited early before writing out the unreachable epilog. Thanks Chris for catching this.

Swift SVN r21491
2014-08-27 20:19:09 +00:00
Joe Groff
e9c7cefc94 SILGen: Return nil on the failure path from failable value initializers.
Since we preserve a single exit point in SIL, provide a join point with a BB argument to receive the success or failure result to return.

Swift SVN r21450
2014-08-26 00:49:35 +00:00
Doug Gregor
51c1433ddd Add magic "literal" __DSO_HANDLE__ to refer to the DSO handle.
__DSO_HANDLE__ can be used as a callee-side default
argument. Addresses rdar://problem/17878114.


Swift SVN r21440
2014-08-25 16:33:54 +00:00
Joe Groff
d63be086e0 SILGen: Forward failure from a delegated value constructor.
Swift SVN r21427
2014-08-23 00:14:56 +00:00
Doug Gregor
17716524c5 Handle CF <-> Objective-C toll-free-bridged conversions in the type checker.
Introduce an attribute that describes when a given CF type is
toll-free-bridged to an Objective-C class, and which class that
is. Use that information in the type checker to provide the CF <->
Objective-C toll-free-bridged conversions directly, rather than using
the user-defined conversion machinery.

Swift SVN r21376
2014-08-21 21:36:05 +00:00
Joe Groff
c23434a569 SILGen: Wrap the result of a failable value constructor in an optional.
Swift SVN r21370
2014-08-21 18:35:33 +00:00
Doug Gregor
7cae0cfc8e Remove -enable-dynamic and its language option; it's always on anyway.
Swift SVN r21354
2014-08-21 15:15:12 +00:00
Joe Groff
fa75ee97c3 SILGen: Remove bogus "cannot handle specialized type references" assertion.
We can handle them just fine. Fixes <rdar://problem/17727332>.

Swift SVN r21322
2014-08-20 21:22:55 +00:00
Chris Lattner
07d8963ba9 fix <rdar://problem/16554056> __FUNCTION__ in deinit for NSObject subclasses crashes the compiler
Swift SVN r21300
2014-08-20 00:28:01 +00:00
Dmitri Hrybenko
938e7c2676 stdlib: introduce UnicodeScalarLiteralConvertible protocol
This allows UnicodeScalars to be constructed from an integer, rather
then from a string.  Not only this avoids an unnecessary memory
allocation (!) when creating a UnicodeScalar, this also allows the
compiler to statically check that the string contains a single scalar
value (in the same way the compiler checks that Character contains only
a single extended grapheme cluster).

rdar://17966622

Swift SVN r21198
2014-08-14 16:04:39 +00:00
Doug Gregor
f05cd00036 Handle reference storage types in the optional-injection workaround from r21176.
Swift SVN r21179
2014-08-13 18:57:30 +00:00
Doug Gregor
21dcd07846 Extend the optional-injection-of-erroneous-nil hack for more entities.
r21096 introduces an optimization barrier for failing initializers
that allows the pattern

  let x: NSFoo? = NSFoo()

be used to detect whether NSFoo() returned nil, even though the type
system says it cannot. Extend this check to Objective-C methods and
properties, where the non-optional annotation may be incorrect
<rdar://problem/17984530>.




Swift SVN r21176
2014-08-13 16:44:53 +00:00
Joe Groff
bde47cfe44 SILGen: Keep the 'let x: NSFoo? = NSFoo()' failing initializer hack from being optimized.
If we see that we're injecting the result of an @objc initializer into an optional, perform the T -> T? conversion using unchecked_ref_bit_cast, which is opaque to the optimizer, instead of doing a proper optional injection. This prevents the optimizer from defeating our workaround for failing initializers. <rdar://problem/17941601>

Swift SVN r21096
2014-08-07 17:40:38 +00:00
Chris Lattner
dd803a39f8 change LValue from holding its segments as a DiverseList to holding them
as a std::vector<std::unique_ptr<PathComponent>>.   DiverseList memcpy's
around its buffer on copy and move operations.  This seems safe with all
of the current implementations of Cleanup, but is absolutely not with the
LValue PathComponents.  These things hold std::vectors, RValue (which has
an std::vector in it), and a bunch of other probably non-trivial things.

While we're in here, disable the copy constructor, since it isn't safe.
This doesn't harm mainline, but was burning me on a patch I'm working on.

When/if someone cares about performance optimizing this code, a better
approach would be to use an ilist + bump pointer allocator.



Swift SVN r21057
2014-08-06 00:50:09 +00:00
Doug Gregor
79d5c69d5a Remove CheckedCastKind::ArrayDowncastBridged. Nobody F'ing Cares about it now.
Swift SVN r20730
2014-07-30 04:26:16 +00:00
Dave Abrahams
6e7f83fe98 Internalize old array casting entry-points
The core compiler now uses just two entry points for array casting

Swift SVN r20725
2014-07-29 23:56:51 +00:00
Chris Lattner
e466b4621b revert r20658, restoring us back to producing the "inout writeback to computed property"
error when detecting an inout writeback problem.


Swift SVN r20681
2014-07-29 18:12:51 +00:00
Chris Lattner
7676259ecc Fix http://swiftwtf.tumblr.com/post/91934970718/xcode-6-beta-3 to compile into the
behaviorly correct code by CSE'sing subscripts that are identical.

Also, add a dump() method to PathComponent to help visualize / debug LValue structures.



Swift SVN r20661
2014-07-29 00:27:43 +00:00
Chris Lattner
4d03ef63f7 Rip out my previous work that produced perplexing "inout writeback to
computed property" errors when SILGen could determine that there was
an inout writeback alias, and have the code instead perform CSE of the
writebacks directly.

This means that we produce more efficient code, that a lot of things
now "just work" the way users would expect, and that the still erroneous
cases now get diagnosed with the "inout arguments are not allowed to 
alias each other" error, which people have a hope of understanding.

There is still more to do here in terms of detecting identical cases,
but that was true of the previous diagnostic as well.




Swift SVN r20658
2014-07-28 23:55:14 +00:00
Chris Lattner
1c80e6454f fix <rdar://problem/17828355> curried final method on a class crashes in irgen
getNextUncurryLevelRef was using it's own local test to determine whether a
FuncDecl is dynamically dispatched (which didn't check for "final"), change it 
to use SILGenFunction::getMethodDispatch instead.


Swift SVN r20623
2014-07-28 15:42:24 +00:00
Joe Groff
4bacaffaa7 SILGen: Implement lvalue BindOptionalExprs.
Factor out the code for emitting the "bind" branching logic, and share it to implement an LValueComponent for optional binds, which makes optional assignments work.

Swift SVN r20614
2014-07-28 04:05:28 +00:00
Joe Groff
623aba1786 Encapsulate Substitution's state.
Expose Substitution's archetype, replacement, and conformances only through getters so we can actually assert invariants about them. To start, require  replacement types to be materializable in order to catch cases where the type-checker tries to bind type variables to lvalue or inout types, and require the conformance array to match the number of protocol conformances required by the archetype. This exposes some latent bugs in the test suite I've marked as failures for now:

- test/Constraints/overload.swift was quietly suffering from <rdar://problem/17507421>, but we didn't notice because we never tried to codegen it.
- test/SIL/Parser/array_roundtrip.swift doesn't correctly roundtrip substitutions, which I filed as <rdar://problem/17781140>.

Swift SVN r20418
2014-07-23 18:00:38 +00:00
Joe Groff
b5d5cf0c8d SILGen: Don't crash when peepholing a ForceValue of nested OptionalEvaluations.
The type checker produces nested optional evaluations for expressions of the form 'x.optionalProperty?.optionalProperty as NonOptionalType'. SILGen had a peephole for this case that was apparently never tested, because we tried to dereference a null failure BB. Fixes <rdar://problem/17576873> and at least half a dozen dupes. The codegen is still kind of ridiculous, but correct.

Swift SVN r20358
2014-07-23 01:34:02 +00:00
Dave Abrahams
1438d617cd [stdlib] Rename ConstUnsafePointer=>UnsafePointer
Swift SVN r20318
2014-07-22 17:10:54 +00:00