Commit Graph

163 Commits

Author SHA1 Message Date
David Farler
45d2355cd6 Merge pull request #2206 from bitjammer/metadata-source
Add MetadataSource classes
2016-04-15 15:14:14 -07:00
David Farler
ffdc91a038 Add MetadataSource classes
The compiler is generally free to not include pointers to metadata in
heap boxes, which are used for closure captures, if it knows you can get
to metadata through some other path. These MetadataSource classes will
describe a sequence of steps to get to metadata at runtime.

In the short term, this will be useful for describing the layout of
function/closure capture contexts, which can vary depending on what is
captured.
2016-04-15 13:52:07 -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
practicalswift
51281e6fd3 [gardening] Fix spacing. 2016-04-14 09:51:27 +02: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
c373b1c015 Reflection: Some improvements to substitutions
The most important change is a fix for dependent member lookup in
bound generic TypeRefs.

The TypeRef logic here is a similified version of what Sema does,
and roughly works as follows:

When looking up a member type O.M for an original type O and
member type M, we first substitute O, to yield a concrete type S.
Then, we look up M in the serialized conformance info section
to yield the unsubstituted member type T', and apply substitutions
to produce the substituted member type T.

Trouble is, we were applying the wrong substitutions the second
time around.

If S is a bound generic type, the "base type substitutions" of S
are the substitutions required to turn the fully-abstracted
bound generic type into S. This might not be the same substitution
map that took us from O to S.

Apart from that, add some assertions that the result of a
substitution is always concrete and that the substitution map
must not contain all generic parameters referenced in the
original type.
2016-04-12 19:15:38 -07:00
Slava Pestov
22bf2dcdbe Reflection: Clean up Parent type handling a bit, NFC 2016-04-12 19:15:38 -07:00
Slava Pestov
d2b16597f8 Reflection: Fixes for tuple types with labels, and varargs
Just drop labels when demangling TypeRefs. This is OK for now
since labels do not affect layout.

Vararg tuple types cannot appear directly as the type of
storage, however they can appear in function input types, and
therefore must be minimally supported. Since we don't plan on
doing function call reflection just yet, this doesn't matter
for now, but again we need to not crash.

With this patch, all TypeRefs in the Swift standard library now
successfully demangle and print.
2016-04-12 19:15:37 -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
68f6a1179d Reflection: Add TypeRef kinds corresponding to {Unowned,Weak,Unmanaged}ReferenceStorageType 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
5ea5bb06a3 Split swift-refleciton-test into host and target test targets
swift-reflection-test is now the test that forks a swift executable
and performs remote reflection, making it runnable on other targets,
such as the iOS simulator.

swift-reflection-dump is now a host-side tool that dumps the remote
reflection sections for any platform binary and will continue to
link in LLVM object file support.

This necessitates finally moving lib/Refleciton into stdlib/public,
since we're linking target-specific versions of the test tool and
we would eventually like to adopt some of this functionality in
the runtime anyway.
2016-03-28 16:34:44 -07: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
23a823a3ed Remote Mirrors: Make MemoryReader a real class
Make the reader a class interface internally, with the
bag-of-function pointers via the eventual C API being the
special case.
2016-03-24 12:05:18 -07:00
David Farler
5ca4b428eb [TypeRef] Add isStruct, isClass, isEnum accessors
These will be used during layout calculation and for nicer
printing.
2016-03-17 19:03:13 -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
d77903c110 [TypeRef] Return const string references for name members
No need to copy these - they aren't ever modified.
2016-03-17 19:02:01 -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
579ce977f0 swift-reflection-test: Replace Mach IPC with pipes
This is more amenable to cross-platform remote reflection tests.

Also add a new callback to the memory reader: getSymbolAddress,
which will be used for getting the addresses of nominal type
descriptors for concrete and fixed generic 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
practicalswift
7bc51357be [gardening] Fix recently introduced typo: "metadat" → "metadata" 2016-03-08 18:59:53 +01: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
db1a715ef1 Fix off-by-one allocation for reading remote strings 2016-03-07 17:43:12 -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
0aba14430c Move PrintTypeRef and TypeRefSubstitution into the cpp - NFC 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
David Farler
0f05b390c3 Gardening: fix some 80-column violations 2016-03-04 17:10:40 -08:00
David Farler
3b2ceb7f19 Skip name= header if ObjCClass/Foreign TypeRef name is empty
A minor cosmetic change when dumping typerefs.
2016-03-04 17:10:40 -08:00
David Farler
5f169b466c Get typerefs for function type metadata 2016-03-04 17:10:40 -08:00
David Farler
04444c44a2 Get typerefs for tuple type metadata, including their elements 2016-03-04 17:10:40 -08:00
David Farler
5196f5b89c Get typerefs for nominal type metadata
and read their generic arguments, if there are any, too.
2016-03-04 17:10:39 -08:00
practicalswift
b9dd782e4b [gardening] Fix recently introduced file header. Also: "–" → "--". 2016-03-03 23:10:43 +01:00