Commit Graph

113 Commits

Author SHA1 Message Date
Slava Pestov
85a75524d5 Reflection: Clearer metadata offset calculations, NFC 2016-05-11 22:51:32 -07:00
David Farler
9dddc6492b SwiftRemoteMirror: Project error existentials
Error existentials have a kind of special heap layout and can also
be compatible as NSError instances, too.
2016-05-10 12:50:31 -07:00
Slava Pestov
c90ee3c322 Reflection: Fix for last-minute fix for 32-bit platforms that broke 64-bit platforms 2016-05-09 14:18:04 -07:00
Slava Pestov
75bd780aca Reflection: Add support for closure contexts to readMetadataFromInstance()
Also add end-to-end tests for this finally, and fix a bug in
the SwiftReflectionTest library where we would give up on an
module completely if it did not have a field metadata section.
This is of course wrong if the module defines closures but
not nominal types.
2016-05-09 13:41:56 -07:00
Slava Pestov
4fccd2f6fc Reflection: Closure context layout
This adds various MetadataReader methods to support closure layout:
- Reading generic arguments from metadata
- Reading parent metadata
- Reading capture descriptor from heap metadata

To a large extent, this is not currently taken advantage of, because
SILGen always wraps address-only captures in SIL box types.

Tests are in the next patch.
2016-05-09 13:40:58 -07:00
David Farler
7e06ee45e8 SwiftReflectionContext: Remove dependency on SwiftRemoteMirror header
Although mechanically it's not a circular dependency yet, conceptually
it is. Use RemoteAddress instead of the raw addr_t typedef.
2016-05-04 13:33:08 -07:00
David Farler
656c3936d2 projectExistential: Use the metadata field offset as size comparison
It's more accurate to use the offset of the "metadata field" of the
existential's RecordTypeInfo when projecting an existential
during remote reflection.
2016-05-04 00:23:29 -07:00
David Farler
7827b75493 projectExistential: Fix doc comment re: class existential layout 2016-05-04 00:02:50 -07:00
David Farler
5f5ce39a1d SwiftRemoteMirror: Wire up existential projection API
Implement the ReflectionContext's implementation of:
swift_reflection_projectExistential.

First, we get the type info of the existential typeref - it should be a
record type info. If it's a class existential, it has trivial layout:
the first word is a pointer to the class instance. Otherwise, if the
value fits in the 3-word buffer of the existential container, it
trivially is also at the start of the container. Otherwise, the value is
off in a heap box somewhere, but the first word of the container is a
pointer to that box.
2016-05-03 21:04:31 -07:00
Slava Pestov
1d5b9b09ac Reflection: Add instance-specific layout entry point, and do some refactoring
Closure context layout will depend on the instance itself as well
as the isa pointer, because instead of instantiating metadata for
closures that capture generic parameters, we store the substitutions
inside the context itself.

For classes, this entry point just reads the isa pointer, applies
the isa mask and proceeds down the metadata path.

For now, the only the latter is hooked up.
2016-04-29 15:39:49 -07:00
Slava Pestov
1bd536e577 Reflection: Fix compile error, oops 2016-04-29 15:39:49 -07:00
Slava Pestov
8f06358b5b Reflection: Fix typos 2016-04-29 15:13:29 -07:00
Slava Pestov
e81fca926a Reflection: Use correct starting offset and alignment in class instance layout
Also, add caching for class instance layout.
2016-04-28 22:56:15 -07:00
Slava Pestov
ce1c30b1bc Reflection: Support reading the remote process's isaMask
Also, use the instance layout entry point in swift-reflection-test,
so that we can dump the layout of a class instance and not the
lowering of the reference value.
2016-04-28 22:56:15 -07:00
Slava Pestov
7a9a4dca83 Reflection: Preliminary C API entry points for class instance layout
Tested by manually running swift-reflection-test, no automated
tests yet, but coming soon.
2016-04-27 23:15:08 -07:00
Slava Pestov
95d648779b Reflection: Fix some bugs in swift-reflection-test
- Improper handling of read() returning an incomplete read
- Update SwiftReflectionTest library for new builtin types section

Only tested manually so far; automated tests coming soon.
2016-04-27 18:17:25 -07:00
Slava Pestov
15d2149d78 Reflection: Should not depend on header file from SwiftRemoteMirror, NFC 2016-04-22 19:03:14 -07:00
Slava Pestov
d91d865667 Reflection: Split up TypeRefBuilder::getFieldTypeRefs()
We want to look at the nominal type kind before lowering any field
types, since the lowering of a class type does not depend on any
of its field types at all.

Tested by upcoming type lowering patch, for now NFC.
2016-04-18 21:18:01 -07:00
Slava Pestov
d184122e83 Reflection: Move field and associated type metadata parsing from ReflectionContext to TypeRefBuilder, NFC
ReflectionContext is now solely concerned with converting runtime
metadata in a remote address space into a TypeRef.

TypeRefBuilder now knows how to parse reflection metadata, and
in particular look up associated type witnesses. This decouples
the TypeRef substitution code from the ReflectionContext. Now
substitution only needs a TypeRefBuilder, which means the code
is no longer templated, and can be moved from TypeRef.h to
TypeRef.cpp.

This also allows the upcoming TypeRef lowering code to live in
a source file instead of headers.
2016-04-16 02:19:04 -07:00
Slava Pestov
29aa01fcf4 Reflection: Don't accidentally copy or assign TypeRefBuilders and ReflectionContexts, NFC 2016-04-16 02:18:31 -07:00
Slava Pestov
a3e0b63423 Reflection: Split off TypeRefBuilder.h from ReflectionContext.h, NFC
ReflectionContext is getting too large, and having to thread a
<Remote> template parameter through all code that wants to construct
typerefs is getting tricky. This is the first patch in a refactoring
to move some stuff out of ReflectionContext.
2016-04-16 02:18:26 -07:00
David Farler
f5583c8295 Restore const-ness of TypeRefs
We got rid of the last mutating methods on TypeRefs, so let's
make these all const again.
2016-04-15 13:13:34 -07:00
Slava Pestov
7b0e8d2b2f Reflection: Clean up duplication in typeref_decoding*.result.txt
Previously we would pre-process the same input files in the ObjC
and non-ObjC tests. This made the tests difficult to update because
the output of one was a subset of the other, and only one of the
two tests would run on any given platform.

Instead, let's just put the Objective-C tests in their own test
and input files.
2016-04-15 00:12:29 -07:00
Slava Pestov
824bd7544d IRGen: Emit typerefs for all builtin types referenced from reflection metadata sections
In order to perform layout, the remote mirrors library needs to know
about the size, alignment and extra inhabitants of builtin types.

Ideally we would emit a reflection info section in libswiftRuntime.o,
but in the meantime just duplicate builtin type metadata for all
builtin types referenced from the current module instead.

In practice only the stdlib and a handful of overlays like the SIMD
overlay use builtin types, and only a few at a time.

Tested manually by running swift-reflection-tool on the standard
library -- I'll add automated tests by using -parse-stdlib to
reference Builtin types in a subsequent patch that adds more layout
logic.

NFC if -enable-reflection-metadata is off.
2016-04-15 00:12:11 -07:00
Slava Pestov
f236383865 Reflection: Fix metadata for empty cases
Also change std::endl to '\n' in the dumper, since std::endl flushes
the output stream.
2016-04-14 23:44:45 -07:00
John McCall
5eef441240 Extract the core implementation of ReflectionContext and
template it over the builder type.

Also preserves more information about function types until
the builder, at least.
2016-04-14 21:21:44 -07:00
Slava Pestov
d39c7f29c4 Reflection: Eliminate NominalTypeTrait::setParent()
When creating a TypeRef from metadata, we have a parent pointer
handy, and construct the TypeRef directly, so there's no need
to mutate the TypeRef after the fact.

When demangling a TypeRef from a string, the mangling encodes
the parent module or type context, so we can set it when
constructing the TypeRef there too.
2016-04-14 15:31:15 -07:00
Slava Pestov
d998fda52e Reflection: Nitpicks, NFC 2016-04-14 15:31:14 -07:00
John McCall
7ff15dd075 Progress towards marking ReflectionContext templated over the builder. 2016-04-14 13:39:35 -07:00
David Farler
cf97a12109 Tie lifetime of typerefs to the ReflectionContext
We will be handing pointers to typerefs over the SwiftRemoteMirrors C
API boundary, at which point it is unclear who will hold onto a shared
pointer. The useful lifetime of a typeref is tied to the
ReflectionContext for which they were created anyway so, when it goes
away, all of those typerefs can go away anyway.

We can't use LLVM's bump-pointer allocator here because we only build
the Support library for the host. As a compromise, stuff new typeref
pointers into a vector pool, where they will be taken down during
ReflectionContext's destructor.
2016-04-13 13:58:35 -07:00
David Farler
20f5304b90 Revised SwiftRemoteMirror C API
Some minor changes to the SwiftRemoteMirror C API, NFC yet.
2016-04-13 13:09:27 -07:00
Slava Pestov
9738d77677 Reflection: TypeRef demangling now returns nullptr if any part of the tree is invalid
Previously, we would return TypeRefs containing nullptrs.
2016-04-12 19:15:37 -07:00
Slava Pestov
39f0fff19d Reflection: Small stylistic cleanups, NFC 2016-04-12 19:15:36 -07:00
John McCall
91f02848df Organize the MemoryReader interface headers to better support
multiple clients.
2016-04-12 16:05:29 -07:00
David Farler
0b04b90895 SwiftRemoteMirrors: Stub implementations for infoFor{TypeRef,Field}
These are just some dummy implementations to temporarily appease
the linker.
2016-04-11 20:03:45 -07:00
David Farler
358a0fefe8 Add SwiftRemoteMirror library
This will wrap the ./lib/Reflection functionality in a C
interface.
2016-04-05 13:45:46 -07:00
Ge Sen
5ad36b2962 [gardening] Put white spaces in between if/while clauses and braces where it is missing.
For instance:

'if (foo){' => 'if (foo) {'
2016-04-02 14:43:45 +08:00
David Farler
76b31a9343 swift-reflection-test: Increase reading robustness
- Check return values from `read`
- Make the reflstr section optional, as it can be stripped by
  -strip-reflection-names.
2016-03-24 12:05:18 -07:00
David Farler
0dcd813c02 FieldRecord: Guard when field name is null
Field name relative offsets can be null if -strip-reflection-names
is passed to the frontend.
2016-03-24 12:05:18 -07:00
David Farler
840855253e SubstituteTypeRef: Don't rely on Metadata address for substitution
We'll need to drill into nested structs to get their field typerefs and
so on, without metadata necessarily available. Decouple the lookup from
the address.

TODO: Cache associated type descriptors based on typeref or mangled
name.
2016-03-17 19:03:03 -07:00
David Farler
be34129c43 Include protocols in DependentMemberTypeRef
This is necessary for proper uniquing when looking up associated
types.
2016-03-17 19:02:01 -07:00
David Farler
55efcb35c7 Track parent typerefs for nominal and bound generic types
- Read the Parent pointer out of Class/Value metadata and create
  typerefs for them.
- Add Parent fields to NominalTypeRef and BoundGenericTypeRef.
- Add TypeRef::getSubstMap(), which creates a new generic argument
  map after substitution has taken place on it. This is used to
  continue to burrow into nested value types, where generic type
  parameters may have a different index.
- Use a DenseMap as that generic argument map.
- Unconditionally key the generic argument map with (Depth, Index)
- Clean up ordering and presentation of Index and Depth. In the rest
  of the compiler, Depth comes before Index.
2016-03-17 19:02:01 -07:00
David Farler
80d3e7e189 [TypeRef] Look up generic substitutions with index and depth
A little bit of trivial future-proofing looking forward to nested
generics.
2016-03-07 18:53:19 -08:00
David Farler
6d32c02c74 [ReflectionContext] Remove some now dead code re: dependent members
This is done as a part of TypeRefSubstitution now.
2016-03-07 18:53:19 -08:00
David Farler
834b03b6cd [Reflection] Substitute generics and resolve dependent members
Once an unsubstituted typeref for a field is built, we substitute
`GenericTypeParameterTypeRef`s with concrete ones built from the generic
arguments of concrete bound generic metadata.

During that process, if we run into a `DependentMemberTypeRef` (e.g.
something of type T.Index), we substitute the base (T) using the current
list of substitutions, and then resolve what `Index` is for the base
using the associated type metadata in the 'assocty' data section.
2016-03-07 17:43:13 -08:00
David Farler
7fd8c79ec5 [ReflectionContext] Cache results of reading metadata and nominal type descriptors
These are the two main round-trips to the external target.
2016-03-07 17:43:13 -08:00
David Farler
96386b03be Don't use interface type as field descriptor key
Nominal type descriptors use declared types for their mangled names,
so we need to use them when scanning the fieldmd section for a
matching record. This is fine because the descriptor can tell us
about the type's generics. Individual field records continue to use
the interface type.
2016-03-07 17:43:12 -08:00
David Farler
a9404f910b Directly pass 'free' as custom deleter for SharedTargetNominalTypeDescriptorRef 2016-03-07 17:43:11 -08:00
David Farler
ea352103f8 Check result of getting function result typeref
Bail if it can't be constructed.
2016-03-07 17:43:11 -08:00
David Farler
bd6d657411 Get typerefs for existential type metadata 2016-03-04 17:10:40 -08:00