Commit Graph

627 Commits

Author SHA1 Message Date
swift-ci
36d8c65f53 Merge remote-tracking branch 'origin/master' into master-next 2019-03-27 05:29:27 -07:00
Slava Pestov
68c07620cd IRGen: Emit Objective-C resilient class stubs if experimental flag is on
Non-generic classes with resilient ancestry do not have statically-emitted
metadata, so we can now emit an Objective-C resilient class stub instead.

Also, when emitting an Objective-C category, reference the class stub if
the class has resilient ancestry; previously this case would hit an assert.

Note that class stubs always start with a zero word, with the address point
pointing immediately after. This works around a linker issue, where the
linker tries to coalesce categories and gets confused upon encountering a
class stub.
2019-03-26 18:53:09 -04:00
Slava Pestov
4207738e17 Add mangling and IRGen LinkEntity for Objective-C resilient class stubs 2019-03-26 18:44:44 -04:00
Saleem Abdulrasool
b97857e99f IRGen: adjust for SVN r355989
SVN r355989 adds support for the XCOFF file format.  For now, treat it as a COFF
target, though XCOFF and COFF are different.
2019-03-13 13:31:24 -07:00
John McCall
7f55a4a4f0 Always give known-empty class properties a zero offset in the static layout.
Field offset vectors are always filled out with either zero or the static layout's offset, depending on the metadata initialization strategy.  This change means that the static layout's offset will only be non-zero for properties with a statically-known layout.  Existing runtimes doing dynamic class layout assign class properties a zero offset if the field offset vector entry is zero and the property is zero-sized.  So this effectively brings the compiler into accord with the runtime (for all newly-compiled Swift code, which will eventually be all Swift code because the current public releases of Swift 5 are not yet considered ABI-stable) and guarantees a zero value for the offset everywhere.

Since the runtime will agree with the compiler about the zero value of the offset, the compiler can continue to emit such offset variables as constant.  The exception to this rule is if the class has non-fragile ObjC ancestry, in which case the ObjC runtime (which is not aware of this special rule for empty fields) will attempt to slide it along with everything else.

Fixes rdar://48031465, in which the `FixedClassMetadataBuilder` for a class with a legacy-fixed layout was writing a non-zero offset for an empty field into the field offset vector, causing the runtime to not apply the special case and thus to compute a non-zero offset, which it then attempted to copy into the global field offset variable, which the compiler had emitted as a true-constant zero.
2019-02-20 00:53:11 -05:00
Slava Pestov
eec0fcdf49 IRGen: When available, use ClassMetadataStrategy::Update even if we don't have legacy type info
If we know our deployment target has a new Objective-C runtime, we can
emit fixed metadata for classes with resilient types even if those
types do not appear in the YAML legacy type info.

Fixes <rdar://problem/47649465>.
2019-02-18 22:39:13 -05:00
Slava Pestov
9a6126b148 IRGen: Move fine-grained flags from ClassLayoutBuilder to ClassLayout
Another refactoring in preparation for adding the new type of class
metadata emitted when deploying to a target with a new Objective-C
runtime.

Progress on <rdar://problem/47649465>.
2019-02-18 22:39:13 -05:00
Slava Pestov
040323e967 IRGen: Introduce ClassMetadataStrategy to clean up some checks
This consolidates the various doesClassMetadataRequire*() checks, making
them more managable.

This also adds a forth state, ClassMetadataStrategy::Update. This will be used
when deploying to the new Objective-C runtime. For now it's not plumbed through.

Progress on <rdar://problem/47649465>.
2019-02-18 22:39:13 -05:00
Doug Gregor
d31ef61a28 Eliminate "sorting" of DeclContext-local protocols / conformances.
Sorting of DeclContext-local protocols and conformances shouldn't ever
be necessary, because the underlying data structures that produce
these lists should be deterministic. Sorting can hide any
non-determinism, so stop doing it and we can address the underlying
nondeterminism.
2019-02-15 14:16:48 -08:00
Slava Pestov
b64db715a8 IRGen: Remove -enable-class-resilience staging flag 2019-02-08 14:22:00 -05:00
Slava Pestov
c28848ac70 IRGen: Stop using 'completely fragile' mode when emitting class metadata
This was a stop-gap measure until the YAML legacy type layout infrastructure
was in place.
2019-02-08 14:22:00 -05:00
Slava Pestov
5680bfde55 IRGen: Always use YAML files for completely fragile class layout
The layouts of resilient value types shipped in the Swift 5 standard library
x and overlays will forever be frozen in time for backward deployment to old
Objective-C runtimes. This PR ensures that even if the layouts of these types
evolve in the future, binaries built to run on the old runtime will continue
to lay out class instances in a manner compatible with Swift 5.

Fixes <rdar://problem/45646886>.
2019-02-08 14:22:00 -05:00
Slava Pestov
bb4cae06d0 IRGen: Rename -read-type-info-path flag to -read-legacy-type-info-path 2019-02-08 14:22:00 -05:00
Ding Ye
56888e2395 [IRGen] Do not emit ObjC property ivar field for non-trivially-representable types. (#21976)
This patch prevents writing ivar field for ObjC property whose type
is not trivially representable in ObjC within its DeclContext.
For example, in the case of the property `@objc var foo: String`,
it is accessed as an NSString on the ObjC side. Without this patch,
this property is backed with an ivar, to which direct accesses would
fail.

Resolves: SR-9557.
2019-01-18 17:58:53 -08:00
Daniel Dunbar
8980213f7c Merge pull request #20687 from ddunbar/swasm-allow-wasm-object-format
[Swift+WASM] Allow Wasm object format.
2019-01-04 08:34:43 -08:00
Félix Cloutier
c43ee6b355 Do not write out ObjC property ivar field for static properties 2018-12-19 10:12:19 -08:00
Jordan Rose
4abb570025 [IRGen] Stop using "extended" type encodings where not supported
These are only used in two places in the Apple Objective-C runtime:

- A protocol's "extended method types" list
- Block type descriptors

We were using them when dynamically registering a protocol with the
Objective-C runtime, but that's just expecting "normal" types; the
"extended method types" list is never present in such a protocol.
2018-12-08 14:20:49 -08:00
Slava Pestov
ff09603f30 IRGen: @_fixed_layout classes still have resilient metadata 2018-11-29 23:20:02 -05:00
Slava Pestov
16a459d0f1 IRGen: Refactor ClassLayoutBuilder a bit to help distinguish resilient-metadata from resilient-storage
We want @_fixed_layout classes to have non-resilient storage, but still have
resilient metadata.
2018-11-29 23:20:02 -05:00
Daniel Dunbar
1efee0c27a [Swift+WASM] Allow Wasm object format.
- This currently does nothing more than adopt the ELF conventions, but for the
   Wasm object file format.
2018-11-20 15:22:26 -07:00
Saleem Abdulrasool
13d56a4f56 IRGen: force lazy metadata initialization for PE/COFF
The metadata reference to the pre-exsting VWT cannot be supported on PE/COFF
due to the direct reference to a value in an external module that is in
static data (the model requires indirecting through memory).  Always
force the lazy initialization for the metadata on such platforms.

This requires a secondary change - to initialize the VWT as well.  This
is ideally moved into the runtime where we can do this uniformly.
2018-11-15 11:05:47 -08:00
Slava Pestov
dbd116119e IRGen: Only mark field offset globals as constant if they won't be updated at runtime
Even if we have a constant value, we might be emitting a legacy layout
that can be updated in place by newer runtimes. In this case, clients
cannot assume the field offsets are constant, and the globals cannot
be constant either.

Part of <rdar://problem/17528739>.
2018-10-31 20:45:18 -04:00
Slava Pestov
4cecc268dc IRGen: Emit Objective-C metadata update callbacks 2018-10-26 16:54:23 -04:00
Slava Pestov
87ec607233 IRGen: Add a new 'Legacy' lowering mode that loads type info from a YAML file
The YAML format is the same one produced by the -dump-type-info
frontend mode.

For now this is only enabled if the -read-type-info-path frontend
flag is specified.

Progress on <rdar://problem/17528739>.
2018-10-04 23:33:17 -04:00
Slava Pestov
d9d64db53f IRGen: We no longer deallocate partially-initialized foreign classes
This was an old workaround for a bug that should no longer
happen now that convenience initializers only have allocating
entry points.
2018-10-04 20:01:23 -04:00
Slava Pestov
4da47823a5 Runtime/IRGen: Add new initialization pattern for classes with backward deployment layout
If a class has a backward deployment layout:

- We still want to emit it using the FixedClassMetadataBuilder.

- We still want it to appear in the objc_classes section, and get an
  OBJC_CLASS_$_ symbol if its @objc.

- However, we want to use the singleton metadata initialization pattern
  in the metadata accessor.

- We want to emit metadata for all field types, and call the
  swift_updateClassMetadata() function to initialize the class
  metadata.

For now, this function just performs the idempotent initialization of
invoking a static method on the class, causing it to be realized with
the Objective-C runtime.
2018-09-23 21:26:46 -07:00
Slava Pestov
a7f668c89c IRGen: Clean up class metadata emission 2018-09-23 21:26:46 -07:00
Slava Pestov
5e56e486b5 IRGen: Clean up ivar descriptor emission 2018-09-23 21:26:46 -07:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Slava Pestov
719ba2fb27 IRGen: Fix case where we would go through the vtable entry and not the dispatch thunk
Make sure we check resilience of the class defining the method,
and not the class with the override we're calling.
2018-09-07 21:57:16 -07:00
Slava Pestov
3808ae0d58 IRGen: Use method lookup function for resilient super method calls
Fixes <https://bugs.swift.org/browse/SR-3928>, <rdar://problem/31411193>.
2018-09-07 21:57:16 -07:00
Slava Pestov
b3060d8836 IRGen: Emit dispatch thunks while emitting nominal type descriptor 2018-08-31 00:20:38 -06:00
Slava Pestov
98b5eafcca IRGen: Split up doesClassMetadataRequireDynamicInitialization() into two predicates
- doesClassMetadataRequireRelocation() -- returns true if we must
  allocate new metadata at runtime and fill it in, because the class
  has multiple instantiations (generic case) or because the total size
  of the metadata is not known at compile time (resilient ancestry).

- doesClassMetadataRequireInitialization() -- weaker condition than
  the above. It's true if the metadata must be relocated, but it is
  also true if the metadata has otherwise fixed size but must be
  filled in dynamically. This occurs if the class has generic
  ancestry but is itself not generic, or if the class has
  resiliently-sized fields, or missing members.

For now, we don't actually care about the distinciton anywhere,
because we cannot do in-place initialization of class metadata yet.
2018-08-20 14:24:19 -07:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Jordan Rose
f505c8b8a0 [IRGen] Add some PrettyStackTraces for metadata emission (#18727) 2018-08-15 12:51:20 -07:00
Doug Gregor
b08a6f56b4 [AST] Drop resilience expansion from TypeBase::getReferenceCounting().
We’re not using this parameter, and don’t expect to do so in the future,
so remove it. Also fold away TypeBase::usesNativeReferenceCounting()
and irgen::getReferenceCountingForType(), both of which are trivial.
2018-08-14 09:10:49 -07:00
Slava Pestov
e44721dd19 IRGen: Remove the 'access type'/'layout type' distinction from StructLayout
This was part of the old resilience workaround for classes and
is no longer used.
2018-08-10 00:42:34 -07:00
Slava Pestov
b26d8ca6ed IRGen: For classes with resiliently-sized fields, calculate both a fragile and resilient layout
Use the fragile layout to emit static metadata, and use the
resilient metadata for almost everything else.

Add some tests to ensure that we use runtime metadata, even
for classes with fully fragile layout.
2018-08-10 00:42:34 -07:00
Slava Pestov
9f5505d95c IRGen: Don't make values constant in getAddrOfSimpleVariable()
We also use this for field offset globals, which are not always
constant. I think in practice everything was getting set
correctly, but it was hard to follow the logic.
2018-08-10 00:42:34 -07:00
Slava Pestov
90f01deba2 IRGen: Remove tryEmitClassConstantFragileInstance{Size,AlignMask}() 2018-08-10 00:42:34 -07:00
Slava Pestov
b26f3efced IRGen: Remove ClassTypeInfo::getHeapAlignment() 2018-08-10 00:42:34 -07:00
Slava Pestov
69be7d93b2 IRGen: Split up ClassLayoutBuilder::ClassMetadataRequiresDynamicInitialization into fine-grained conditions 2018-08-10 00:42:34 -07:00
Slava Pestov
f594d5bfbf IRGen: Split off ClassLayout implementation into its own file 2018-08-07 05:34:29 -07:00
Slava Pestov
fc91a58d82 IRGen: [ClassLayout] Better fix for SR-4687
We cannot use field offset globals if *any* field of a generic class
with Objective-C ancestry is dependent.

This is because the Swift runtime first performs layout starting
from a static instance start offset, and then asks the Objective-C
runtime to slide the offsets based on the dynamic superclass size.

So if the class has a field of generic type, the alignment of that
type can change the offsets of fields *before* it as well as after.

So we cannot assuem that any fields in such a class have the same
offset across instantiations at all.

The previous fix captured the intent of the above, but it only
kicked in if the immediate superclass of the class was imported
from Objective-C. But really we need to do this for any class with
Objective-C ancestry.

While fixing this, re-organize the code in ClassLayoutBuilder a
little bit to untangle the stored property iteration from the
interesting FieldAccess adjustments that take place after.
2018-08-07 04:26:43 -07:00
Slava Pestov
d460d5def6 IRGen: Remove ASTTy field from StructLayout 2018-08-07 04:26:43 -07:00
Slava Pestov
92f1ddc981 IRGen: [ClassLayout] Don't compute FieldAccess for superclass fields
Previously we would recursively get the abstract layout for the fully
generic class type for every superclass. But really we only need to do
that for fields of the class itself, not any superclass fields, since
we throw out the superclass field information anyway.
2018-08-07 04:26:43 -07:00
Slava Pestov
bdb5eb3289 IRGen: [ClassLayout] Don't build a separate StructLayout for classes
The type info for a class described its layout using a combination of a
StructLayout and ClassLayout, with different information stored in both.

Since we never use a class as a struct, it's simpler to add the relevant
bits to ClassLayout, and not build a StructLayout at all.

Also, drop inherited properties from the ClassLayout -- they're no
longer needed.
2018-08-07 00:37:03 -07:00
Slava Pestov
7315893d3d IRGen: [ClassLayout] Only look for fields of the immediate class
When emitting fixed class metadata, we emit field offsets for all fields,
including those from superclasses, if any.

Get the ClassLayout for the correct class before looking up a field that
might potentially belong to a superclass. Soon, I'm going to slim down
ClassLayout instances to only store the fields belonging to the class
itself, removing the InheritedStoredProperites array altogether.
2018-08-07 00:34:55 -07:00
Slava Pestov
a4f06c40fc IRGen: [ClassLayout] Don't recursively compute layout of the same type
Even if two types are different, they might still have the same
type info, so don't call getLayout() without checking the type info
for identity first. This allows simplifying an early exit into an
assertion elsewhere.
2018-08-06 23:22:16 -07:00
Doug Gregor
4d39506c4a [IRGen] Use a more precise computation for the kind of reference count.
TypeBase::usesNativeReferenceCounting() was doing a lot of work to
find the class that a type refers to, then determine whether it
would use the native reference-counting scheme. Its primary caller
in IRGen would use an overly-conservative approximation to decide
between the “Objective-C” and “unknown” cases, which resulted in
uses of “unknown” reference counting for some obviously-ObjC cases
(e.g., values of “NSObject”).

Moreover, the approximation would try to call into the type checker
(because it relied unnecessarily on the superclass *type* of a class
declaration), causing an assertion.

Fixes rdar://problem/42828798.
2018-08-06 17:04:47 -07:00