Commit Graph

381 Commits

Author SHA1 Message Date
Slava Pestov
2c015164cf Stop passing NodePointer by reference 2019-02-13 21:51:28 -05:00
Slava Pestov
d0c7b1547e ASTMangler: Mangle sugared types for the debugger
Fixes <rdar://problem/48004306>.
2019-02-13 19:17:25 -05:00
John McCall
d07efd37e0 Requestify the mangling-to-metadata APIs.
Note that I've called out a couple of suspicious places where we
are requesting abstract metadata for superclasses but probably
need to be requesting something more complete.
2019-02-05 16:20:48 -05:00
Slava Pestov
36d3f47388 ASTDemangler: Preserve generic arguments for bound generic Objective-C classes
MetadataLookup gives special treatment to imported Objective-C classes,
since there's no nominal type descriptor and metadata is obtained
directly by calling into the Objective-C runtime.

Remote reflection also gives special treatment to imported Objective-C
classes; they don't have field descriptors.

However, the ASTDemangler needs to treat them like ordinary classes,
in particular it wants to preserve the generic arguments here so that
we can round-trip debug info.
2019-01-30 01:28:48 -05:00
Slava Pestov
4a74bf4ba1 ASTDemangler: Add support for lowered function types 2019-01-29 21:55:57 -05:00
Slava Pestov
13a50c2d2d ASTDemangler: Add support for lowered metatypes 2019-01-29 19:15:17 -05:00
Slava Pestov
fce933910d ASTDemangler: Implement types in generic local context
If we nest a type inside a local context inside a generic type,
we have to look through the local context(s) to find the outer
generic type when stripping off generic arguments.

We don't support nominal types inside generic local context
right now, but this can happen with type aliases.
2019-01-29 19:15:17 -05:00
Arnold Schwaighofer
bcc7588db9 Merge pull request #22125 from aschwaighofer/workaround_dynamic_replacement_failure
DynamicReplacement: Don't fail when a library is closed and reloaded
2019-01-26 07:57:06 -08:00
Slava Pestov
b6951932b4 ASTDemangler: Implement type alias types
Debug info uses a special mangling where type aliases can be
represented without being desugared; attempt to reconstruct
the TypeAliasType in this case.
2019-01-25 21:59:48 -05:00
Slava Pestov
55605ceab9 ASTDemangler: Implement DynamicSelfType 2019-01-25 21:44:02 -05:00
Slava Pestov
bbe6a56e22 ASTDemangler: Implement builtin types 2019-01-25 21:44:02 -05:00
Slava Pestov
2d21233378 ASTDemangler: Implement unresolved DependentMemberType
The debug mangling emits these when the type parameter only
conforms to one protocol having an associated type with this
name.
2019-01-25 21:44:02 -05:00
Arnold Schwaighofer
8394fc525e DynamicReplacement: Don't fail when a library is closed and reloaded
rdar://47560273
2019-01-25 13:50:27 -08:00
Doug Gregor
f223d0b886 [Runtime] Eliminate a use-after-free when comparing @objc type names.
We were creating a local Demangler instance, demangling a type name
using it, and then returning one of the resulting nodes to the caller.

Fixes rdar://problem/46817009.
2018-12-20 20:28:23 -08:00
Joe Groff
85eb634191 stdlib: Make _typeByName Foundation SPI reject symbolic references.
It's used for recovering type metadata from deserialized mangled names, which should never have symbolic
references in them.
2018-12-10 20:22:03 -08:00
Joe Groff
021053d499 Runtime: Make getTypeByMangledNameIn(Context|Environment) a public entry point.
This can be used by compiler-generated code as a size optimization for metadata access, using a
mangled name instead of possibly many open-coded metadata calls. It can also allow reflection
libraries outside of the standard library to turn type reference strings into in-process metadata
pointers in a robust way. rdar://problem/46451849
2018-12-10 14:22:41 -08:00
Greg Parker
70bbeaab78 [runtime] Register a hook for class name lookup from libobjc (#20650)
libobjc needs to look up classes by name. Some Swift classes, such as
instantiated generics and their subclasses, are created only on demand.
Now a by-name lookup from libobjc counts as a demand for those classes.

rdar://problem/27808571
2018-11-29 17:06:28 -08:00
Doug Gregor
d4fe6e9ff7 [Runtime] Fix a bug in handling multi-level generic environment substitutions. 2018-11-16 10:39:19 -08:00
Doug Gregor
cc2ee165a1 [ABI] Use generic environment to handle mangled generic keypath types.
Always use mangled type names to represent type metadata in keypath patterns.
For generic types, use the generic environment to pull substituted types
from the instantiation arguments.

Finishes the type metadata part of rdar://problem/38038799.
2018-11-16 10:13:07 -08:00
Doug Gregor
cdd2928fec [Runtime] Generalize SubstGenericParametersFromMetadata slightly.
Only use the metadata and context information during initial setup, not
later on. NFC just yet.
2018-11-16 10:13:06 -08:00
John McCall
ff6d031482 Allow TypeReference to refer to protocol descriptors.
We should also allow references via manglings just to cover the
general case if we need it, but this is useful on its own so that
we can emit a reference to any natively-declared Swift type.
2018-11-16 00:39:10 -05:00
Doug Gregor
986b8f3f9f [Runtime] Make swift_getTypeByMangled(Name|Node) overridable.
Rename the funnel points for demangling strings/nodes to metadata to
swift_getTypeByMangled(Name|Node) and make them overridable. This will let
us back-deploy mangling improvements and bug fixes.
2018-11-15 16:43:38 -08:00
Doug Gregor
723b56e64f [Standard library] Simplify _getTypeByMangledName.
Simplify the signature of the internal _getTypeByMangledName() used by the
standard library to what we actually (currently) use. Drop it as a
compatibility override, because it’s not a useful place to introduce
customization.
2018-11-15 16:10:11 -08:00
Doug Gregor
cd6d6be64d [Runtime] Consolidate runtime functions used for demangle-to-metadata.
Clean up the interfaces used to go from a mangled name or demangle tree to
metadata. Parameterize these interfaces for generic parameter substitutions
(already in use) and dependent conformance substitutions (speculative).
2018-11-15 15:57:44 -08:00
Doug Gregor
9191ffe124 [Runtime] Eliminate more dead code for flat generic argument indices. 2018-11-15 11:25:02 -08:00
Doug Gregor
201c13ab01 [Runtime] Remove code for handling “flat” generic argument indices.
We no longer encode “flat” indices in the metadata, and have no need for
this now-dead decoding code.
2018-11-15 11:12:41 -08:00
Arnold Schwaighofer
fb7b223ba2 Add support to modify chaining behavior of dynamic replacements
Default to not chain dynamic replacements: Only one replacement and the
original implementation are active.
2018-11-09 13:17:09 -08:00
Arnold Schwaighofer
4cb1c339b3 The runtime entry points for enabling/disabling a dynamic replacement scope also must hold the lock 2018-11-09 09:01:07 -08:00
Doug Gregor
5758cdcfcc [ABI] Eliminate the special structure for generic parameter references.
TargetGenericParamRef is a specialized structure used to describe the
subject of a generic requirement, e.g., the “T.Assoc” in “T.Assoc: P”.
Replace it with a mangled name, for several reasons:

1) Mangled type names are also fairly concise, can often be shared, and
are a well-tested path
2) Mangled type names can express any type, which might be useful in the
future
3) This structure doesn’t accommodate specifically stating where the
conformances come from (to extract associated type witnesses). Neither
can mangled names, but we’d like to do that work in only one place.

This change exposed an existing bug where we improperly calculated the
generic parameter counts for extensions of nested generic types. Fix that
bug here (which broke an execution test).
2018-11-08 13:58:17 -08:00
Arnold Schwaighofer
44b3a47e56 Merge pull request #20333 from aschwaighofer/dynamic_function_replacement
Dynamic function replacement
2018-11-07 13:08:46 -08:00
Doug Gregor
9dd6a5f033 [Runtime] Use the old remangler to compute the @objc protocol name.
The “old” mangling that is used for runtime names of @objc protocols
uses a simpler substitution scheme, so you can’t simply take a mangled
name from the new mangling and fix up the ends.

Fixes rdar://problem/45685649.
2018-11-06 15:16:33 -08:00
Arnold Schwaighofer
152e8db8bb IRGen and runtime implementation for dynamic replacements 2018-11-06 09:58:36 -08:00
Slava Pestov
d093fcb4a4 Reflection: Decode imported Objective-C classes and protocols as their own TypeRef
Right now we expect that every class and protocol has a field
descriptor that tells us if the entity is @objc or not.

For imported types, the descriptor will not exist if we did not
directly emit a field whose concrete type contains the imported
type. For example, in lldb, we might have a generic type whose
runtime substituted type includes an imported type.

In this case, TypeLowering would fail to produce a layout because
it did not find a field descriptor for the imported type.

A better approach is to have the TypeDecoder call a different
factory method for imported types, and handle them specially in
TypeLowering, bypassing the field type metadata altogether.
2018-11-02 00:47:11 -04:00
Doug Gregor
dafaabecd6 [Mangling] Mangle protocol symbolic references in any-generic-type production.
We were strangely excluding protocols from being symbolically referenced
in the any-generic-type production, which meant that we could not resolve
(e.g.) associated type references to private protocols at runtime. Allow
protocol symbolic references in this position, and cope with it in the
demangler.

Fixes the rest of rdar://problem/44977236.
2018-10-26 18:10:07 -07:00
Doug Gregor
5b41ac16db [ABI] Introduce indirect symbolic references to context descriptors.
Extending the mangling of symbolic references to also include indirect
symbolic references. This allows mangled names to refer to context
descriptors (both type and protocol) not in the current source file.

For now, only permit indirect symbolic references within the current module,
because remote mirrors (among other things) is unable to handle relocations.

Co-authored-by: Joe Groff <jgroff@apple.com>
2018-10-23 16:06:42 -07:00
Doug Gregor
aba018c1e8 [ABI] Pass requirement base descriptor to swift_getAssociatedTypeWitness().
Have clients pass the requirement base descriptor to
swift_getAssociatedTypeWitness(), so that the witness index is just one
subtraction away, avoiding several dependent loads (witness table ->
conformance descriptor -> protocol descriptor -> requirement offset)
in the hot path.
2018-10-10 22:45:59 -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
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
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
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
Joe Groff
6ea8134eac Runtime: Simplify getFieldAt and take it private. 2018-08-15 16:44:16 -07:00
Joe Groff
2a8b7f76bf Runtime: Don't register fieldmd sections.
They're no longer necessary to track separately from types.
2018-08-15 16:20:53 -07:00
Joe Groff
6f45c2a36e IRGen/Runtime: Reference field descriptor directly from type context descriptors.
This saves us some expensive cross-referencing and caching in the runtime, and lets us reclaim the `isReflectable` bit from the context descriptor flags (since a null field descriptor is a suitable and more accurate indicator of whether a type is reflectable).
2018-08-15 14:19:53 -07:00