Commit Graph

1960 Commits

Author SHA1 Message Date
Doug Gregor
5f56d2faf9 [Runtime] Properly turn a const char* into a StringRef for a mangled name.
Use makeSymbolicMangledNameStringRef() to skip over null characters within
symbolic references.
2018-09-26 23:19:33 -07:00
Doug Gregor
c3d0ba8df4 [IRGen/Runtime] Witness tables with dependent associated types need instantiation.
Associated type witnesses in a witness table are cache entries, which are
updated by the runtime when the associated types are first accessed. The
presence of an associated type witness that involves type parameters requires
the runtime to instantiate the witness table; account for that in the runtime.
The presence of any associated type witness makes the witness table
non-constant.
2018-09-26 23:19:33 -07:00
Doug Gregor
b531b3923f [ABI] Use mangled names for associated type witnesses.
Rather than storing associated type metadata access functions in
witness tables, initially store a pointer to a mangled type name.
On first access, demangle that type name and replace the witness
table entry with the resulting type metadata.

This reduces the code size of protocol conformances, because we no
longer need to create associated type metadata access functions for
every associated type, and the mangled names are much smaller (and
sharable). The same code size improvements apply to defaulted
associated types for resilient protocols, although those are more
rare. Witness tables themselves are slightly smaller, because we
don’t need separate private entries in them to act as caches.

On the caller side, associated type metadata is always produced via
a call to swift_getAssociatedTypeWitness(), which handles the demangling
and caching behavior.

In all, this reduces the size of the standard library by ~70k. There
are additional code-size wins that are possible with follow-on work:

* We can stop emitting type metadata access functions for non-resilient
types that have constant metadata (like `Int`), because they’re only
currently used as associated type metadata access functions.
* We can stop emitting separate associated type reflection metadata,
because the reflection infrastructure can use these mangled names
directly.
2018-09-26 23:19:33 -07:00
Doug Gregor
c45c84d913 [Runtime] Handle all generic parameters at once demangling to metadata.
When demangling a symbolic reference to a nested generic type, the
demangle-to-metadata path will be given all levels of generic arguments at
once. Cope with this in the demangling-to-metadata path.

Eventually, we would like to switch all clients over to take all
levels of generic arguments at once.
2018-09-26 15:33:15 -07:00
Doug Gregor
cf2a47e14c [Runtime] Handle non-key generic parameters when demangling to metadata.
When forming metadata for a nested generic type, gather all of the
generic arguments from the parent type “as written”, so that we can directly
map generic parameters to those generic arguments when they occur within
requirements. This allows us to demangle nested types within extensions
that have same-type constraints on generic parameters into type metadata.

Fixes rdar://problem/37170296.
2018-09-26 14:41:04 -07:00
Doug Gregor
546e7e264b [Runtime] Factor out a function object to look into “written” generic args.
NFC
2018-09-26 14:01:03 -07:00
Doug Gregor
9436793852 [Runtime] Split out the code to extract all “written” generic args from metadata
NFC. We’ll need this code elsewhere.
2018-09-26 13:47:01 -07:00
Maxim Moiseev
5f17e64be0 try! error message should reprt the right location
At the moment the location being reported is inside the standard
library, which is not very helpful. Instead, the location should point
at the `try!` expression in the application code.

Fixes: rdar://problem/21407683
2018-09-26 11:22:41 -07:00
Doug Gregor
d73a2e0d39 [Runtime] Properly form generic arguments for metadata-to-demangle-tree.
When mapping from type metadata to a demangle tree, fill in the complete
set of generic arguments. Most of the effort here is in dealing with
extensions that involve same-type constraints on a generic parameter, e.g.,

  extension Array where String == Element { }
  extension Dictionary where Key == Value { }

In such cases, the metadata won’t contain generic arguments for every
generic parameter. Rather, the generic arguments for non-key generic
parameters will need to be computed based on the same-type requirements
of the context. Do so, and eliminate the old hacks that put the generic
arguments on the innermost type. We don’t need them any more.

Part of rdar://problem/37170296.
2018-09-25 23:10:08 -07:00
Doug Gregor
3dda40b17e [Runtime] Match up extensions of Objective-C classes by mangled nodes.
When comparing an extension context for Objective-C classes, there are no
context descriptors to use. Instead, perform a string match.
2018-09-25 10:53:54 -07:00
Slava Pestov
c67d4ab3b0 Merge pull request #19474 from slavapestov/class-backward-deployment
More progress on backward deployment support for classes with resiliently-sized fields
2018-09-24 15:34:42 -07:00
Saleem Abdulrasool
e0e75ead37 Merge pull request #19434 from compnerd/a-stable-future
runtime: remove unnecessary preprocessor condition, flip cases (NFC)
2018-09-24 14:33:41 -07:00
Mike Ash
1fb165a0ea Merge branch 'master' into rename-conflicting-classes-and-methods 2018-09-24 10:17:22 -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
0088055f77 Runtime: Set up the class's runtime name in initGenericObjCClass() 2018-09-23 21:26:46 -07:00
Slava Pestov
0e4248375d Runtime: Introduce a new code path for initializing non-generic class metadata
It's a bit simpler because we don't have to change the ivar descriptors
back and forth.
2018-09-23 21:26:46 -07:00
Slava Pestov
62aecd31b7 Runtime: Split up swift_initClassMetadata()
- Rename _swift_initializeSuperclass() to copySuperclassMetadataToSubclass(),
- Factor out initClassFieldOffsetVector()
- Factor out initClassVTable()
- Factor out initGenericObjCClass()
2018-09-23 21:26:46 -07:00
Slava Pestov
6084dc285e Runtime: Only initialize metaclass superclass if superclass is generic
Otherwise, we emitted a static reference to its metaclass, so there's
no need to overwrite it here. This doesn't really improve anything,
it was just something I noticed while auditing this code in
preparation for a refactoring, so may as well fix it.
2018-09-23 21:26:46 -07:00
Slava Pestov
724830acd5 Runtime: Use the resilient metadata allocator instead of malloc() for resilient classes
Also move getResilientMetadataAllocator() and swift_relocateClassMetadata() to the top
of the "classes" section, instead of putting it in the middle.
2018-09-23 21:26:46 -07:00
Doug Gregor
18e6ea76dd Merge pull request #19486 from DougGregor/runtime-metadata-subst-same-type-extensions
[Runtime] Handle non-key arguments when substituting from metadata.
2018-09-23 20:26:28 -07:00
Doug Gregor
3ea48770fe [Runtime] Handle non-key arguments when substituting from metadata.
Reimplement SubstGenericParametersFromMetadata to cope with non-key
generic parameters, which are counted when referring to generic parameters
from metadata but do not have corresponding generic arguments.
2018-09-22 23:32:49 -07:00
Doug Gregor
5a87435b8e [Runtime] Simplify ProtocolConformanceDescriptor::getWitnessTable().
Use SubstGenericParametersFromMetadata to handle substitutions when
checking generic requirements, extending SubstGenericParametersFromMetadata
for this purpose.
2018-09-22 20:51:03 -07:00
Doug Gregor
0a775dab54 [Runtime] Extract a function object for demangling using base metadata.
The field metadata translation has a great little lambda for extracting
generic arguments from metadata when demangling. Extract it into a
reusable function object.
2018-09-22 20:38:47 -07:00
Doug Gregor
15214b3f5b Merge pull request #19483 from DougGregor/runtime-demangle-fixes
[Runtime] Fixes for demangling to metadata
2018-09-22 07:26:27 -07:00
Doug Gregor
298458a926 [Runtime demangler] Mangled extension context doesn’t always have Type.
The demangle tree for an extension type context doesn’t always have a
Type node wrapping it, and that’s okay.
2018-09-22 00:08:48 -07:00
Doug Gregor
3f79cb9976 [Runtime] Replace outdated signature for associated type access functions.
The resolution of generic parameter references, which is used for
checking generic requirements at runtime, was written in terms of an
outdated signature for associated type access functions that did not
account for the MetadataRequest parameter or MetadataResponse result.
Use the existing AssociatedTypeAccessFunction typedef instead.

Fixes SR-7553 / rdar://problem/39769906
2018-09-21 23:47:02 -07:00
Doug Gregor
6982284baf [Runtime] Only request Abstract metadata when demangling to metadata.
When SWIFT_ENABLE_MANGLED_NAME_VERIFICATION is set, we would end up
deadlocking when we encounter a metadata cycle. The demangling code only
requires abstract metadata, because at most it needs type identity and
filling in the type arguments of generics. Update clients of
_getTypeByMangledName to assert the kind of metadata they require.
2018-09-21 22:40:44 -07:00
Saleem Abdulrasool
f9af5cb685 Merge pull request #19462 from compnerd/what-is-in-a-name
stdlib: check for ARM/ARM64 more thoroughly (NFC)
2018-09-21 14:19:47 -07:00
Mike Ash
49c3547449 Merge remote-tracking branch 'origin/master' into rename-conflicting-classes-and-methods 2018-09-21 15:52:38 -04:00
Saleem Abdulrasool
2d04b8491b stdlib: check for ARM/ARM64 more thoroughly (NFC)
Update the instances of checks for architectures to be more broad for different
spellings of the architecture macro.  Certain targets use `_M_ARM` and others
use `__arm__`.  Similarly, arm64/aarch64 has `_M_ARM64`, `__arm64__` and
`__aarch64__` as spellings.  This just mechanically goes through and encodes the
various spellings.

Take the opportunity to replace some raw checks with `defined` checks which
avoids a pedantic warning due to the undefined macro when performing the check
as the preprocessor may warn about an undefined condition evaluating to `0`.
2018-09-21 11:24:03 -07:00
Joe Groff
c87d2a8be2 Merge pull request #19340 from jckarter/generalize-tuple-extra-inhabitants
Generalize extra inhabitants of tuples.
2018-09-20 18:55:31 -07:00
Jordan Rose
6930a0f25a [runtime] Directly reference objc_setHook_getImageName (#19414)
(instead of using dlsym)

The original use went in before <objc/runtime.h> was updated, but now
it should be present in all Xcode 10 SDKs.

rdar://problem/42137765
2018-09-20 18:13:14 -07:00
Joe Groff
7f22a266b3 Merge pull request #19437 from jckarter/extra-inhabitant-warnings
Quell some warnings about casting in the runtime.
2018-09-20 18:03:45 -07:00
Joe Groff
06a1c388b3 Quell some warnings about casting. 2018-09-20 16:24:28 -07:00
Joe Groff
bbbf02e5f8 Merge pull request #19415 from jckarter/better-missing-metadata-warnings
Make runtime warnings about missing metadata more descriptive.
2018-09-20 15:40:17 -07:00
Joe Groff
93d85997e8 Generalize extra inhabitants of tuples.
Like we did for structs, make it so that tuple types can also get extra inhabitants from whichever element with the most, not only the first. This lets us move all of the extra inhabitant handling functionality between structs and tuples in IRGen up to the common RecordTypeInfo CRTP base.
2018-09-20 15:39:44 -07:00
Saleem Abdulrasool
687aeaa37d runtime: remove unnecessary preprocessor condition, flip cases (NFC)
The use of `__APPLE__` is unnecessary as the case is guarded by the
`SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT` preprocessor guard which is implicitly
specific to that environment.  Additionally, flip the condition around so that
the positive (which is the future) appears ahead of the negative case.
2018-09-20 15:27:48 -07:00
Joe Groff
e9b45178a0 Make runtime warnings about missing metadata more descriptive.
Describe the consequences of missing metadata instead of just posting a scary
message about a bug.  Furthermore, since these warnings tend to show up in
playgrounds, and probably aren't relevant to the user of a playground, suppress
them when running in a playground. rdar://problem/44642942
2018-09-20 13:03:54 -07:00
Mike Ash
317451c8ee [Runtime][Stdlib][Overlays] Add comments to all the renamed classes explaining the rename and noting that the old name cannot be used due to conflicts.
rdar://problem/35768222
2018-09-20 12:07:30 -04:00
Mike Ash
9210830351 Merge pull request #19389 from mikeash/patch-bundleforclass
[Runtime][Foundation] Supplement the class_getImageName patch with a patch to +[NSBundle bundleForClass:] on older "embedded" targets
2018-09-20 09:08:24 -04:00
Doug Gregor
72ff34e937 Merge pull request #19410 from DougGregor/runtime-remove-duplicate-witnes-check
[Runtime] Remove duplicate witness checking for now.
2018-09-19 22:26:15 -07:00
Doug Gregor
cc32f50691 [Runtime] Remove duplicate witness checking for now.
It's breaking on SwiftPM, so remove it for now and follow-up to
investigate with rdar://problem/44627274.
2018-09-19 20:16:52 -07:00
swift-ci
922c4f0808 Merge pull request #19403 from DougGregor/runtime-remove-warning 2018-09-19 19:12:24 -07:00
Doug Gregor
93f9dc8e76 [Runtime] Remove warning about "missing" witness table entries.
It introduces spurious results.
2018-09-19 15:41:33 -07:00
swift-ci
e5c9d6417e Merge pull request #19398 from DougGregor/resilient-table-init-empty-proto 2018-09-19 15:18:04 -07:00
Doug Gregor
3ed69d4ee7 [Runtime] Instantiate witness tables even with no resilient witnesses.
The witness table for an empty, resilient protocol might need to be
instantiated, if a newer version of the protocol contains defaulted
associated type requirements. In such cases, we would either fail to
instantiate or assert in the runtime. Replace the assertion with a
proper check (to require instantiation in such cases) and cope with
filling in defaults even when the provided generic witness table has
no resilient witnesses.
2018-09-19 14:00:28 -07:00
Doug Gregor
32fd274f5e Merge pull request #19391 from DougGregor/assoc-conformance-default-witnesses
[ABI] Associated conformance defaults
2018-09-19 13:15:46 -07:00
Doug Gregor
ef0aeed788 [Runtime] Reimplement initialization of resilient witness tables.
Replace the quadratic algorithm (currently O(m*n) where m is the number of requirements and n is the number of witnesses) with an O(m+n) algorithm.
Harden the algorithm a against bad and unexpected inputs:
* Fail if the requirement descriptor is out-of-bounds for the protocol
* Skip the witness if the requirement descriptor is null; this can happen
when the witness table was compiled against a newer version of the
protocol, but is deployed to a library containing an older version of the
protocol. It is expected.
* In debug builds of the runtime, complain if an entry is initialized twice
* In debug builds of the runtime, complain if an entry is NULL

Fixes rdar://problem/44434793, which covers the second bullet (backward 
deployment).
2018-09-19 11:46:29 -07:00
Mike Ash
e704dd0353 [Runtime][Foundation] Supplement the class_getImageName patch with a patch to +[NSBundle bundleForClass:] on older "embedded" targets. The patch technique of editing the symbol table doesn't work for +bundleForClass:'s call to class_getImageName when +bundleForClass: is in an embedded shared cache.
rdar://problem/44489216
2018-09-19 13:29:10 -04:00
Doug Gregor
d076e41f32 [IRGen] Put associated conformance accessors in resilient witness table
For a resilient conformance, emit the associated conformance accessor
functions into the resilient witness table (keyed on the associated
conformance descriptor) rather than in the fixed part of the witness
table. This is another part of resilience for associated conformances,
and a step toward defaults for associated conformances.
2018-09-17 21:58:56 -07:00