Commit Graph

177 Commits

Author SHA1 Message Date
Doug Gregor
192234415d [AST] Store SubstitutionMaps in ConcreteDeclRef and Witness data structures.
Replace two prominent uses of SubstitutionList, in ConcreteDeclRef and
Witness, with SubstitutionMap. Deal with the myriad places where we
now have substitution maps and need substitution lists (or vice versa)
caused by this change.

Overall, removes ~50 explicit uses of SubstitutionList (of ~400).
2018-05-02 13:38:14 -07:00
Joe Groff
c6eab5cb2b SILGen: Clean up conformance handling in getOrCreateKeyPathEqualsAndHash.
Replace manual substitution mapping by using the existing ProtocolConformanceRef::getAssociatedConformance helper to look up the Equatable conformance from Hashable. Replace use of manual Substitution construction with SubstitutionMaps.
2018-03-23 12:29:13 -07:00
Joe Groff
4c2dde56a0 IRGen: Lower external key path components.
The key path pattern needs to include a reference to the external descriptor, along with hooks for lowering its type arguments and indices, if any. The runtime will need to instantiate and interpolate the external component when the key path object is instantiated.

While we're here, let's also reserve some more component header bytes for future expansion, since this is an ABI we're going to be living with for a while.
2018-02-23 19:03:15 -08:00
Joe Groff
d4e03f2ba3 SIL: Add an 'external' KeyPathPatternComponent kind.
This will allow key paths to resiliently reference public properties from other binaries by referencing a descriptor vended by the originating binary. NFC yet, this just provides printing/parsing/verification of the new component.
2018-02-14 10:48:24 -08:00
David Zarzycki
3da6fe9c0d [AST] NFC: Do not reinterpret_cast pointers into CanTypeWrappers
This also introduces 'TypeArrayView' for when a 'Type' is statically
known to be a given TypeBase subclass.
2018-01-31 11:20:05 -05:00
Slava Pestov
700aa71509 AST: Remove GenericEnvironment::getSubstitutionMap() 2017-11-20 12:59:03 -05:00
Slava Pestov
f07cde75e7 AST: Move mapTypeOutOfContext() from GenericEnvironment to TypeBase
It doesn't actually *use* the generic environment.
2017-11-15 22:52:13 -08:00
Doug Gregor
cd3c63cbfd [AST] Stop including GenericSignature.h in other headers.
Except GenericEnvironment.h, because you can't meaningfully use a
GenericEnvironment without its signature. Lots less depends on
GenericSignature.h now. NFC
2017-10-12 14:23:46 -07:00
Huon Wilson
8e50a1666f [AST&Sema] Add assertions for conditional conformances.
These are just places that may need updating for conditional
conformances, and so having them flag eagerly will help in future.
2017-10-10 20:17:41 -07:00
Slava Pestov
9c0de31c22 AST: Simpler implementation of mapTypeOutOfContext()
Now that archetypes store an interface type, GenericEnvironments
no longer need to store a reverse mapping.
2017-10-07 05:44:57 -07:00
Slava Pestov
6def545696 AST: Remove GenericEnvironment::containsPrimaryArchetype()
This is no longer necessary now that archetypes refer back to a
GenericEnvironment.
2017-10-07 04:46:37 -07:00
Doug Gregor
9f580fe7ad [GSB] Move getTypeInContext() over to EquivalenceClass.
We shouldn’t need a potential archetype to map an interface type to a
contextual type. Port `getTypeInContext()` over to `EquivalenceClass`,
largely unchanged, so we don’t directly form archetypes.

Kills a few more uses of `GenericSignatureBuilder::resolveArchetype()`.
2017-09-28 15:47:57 -07:00
Doug Gregor
ea20a1d137 [AST] Add DeclContext::get(Syntactic|Semantic)Depth.
Utilities functions used when comparing declaration contexts. Only the syntactic form is currently used (for the owning DC of a generic environment).
2017-06-01 08:25:42 -07:00
Slava Pestov
3130c3cbd7 AST: Remove an overload of GenericSignature::getSubstitutions() 2017-04-28 13:26:02 -07:00
Slava Pestov
0290c2d5d8 AST: Make GenericSignature and GenericEnvironment SubstitutionMaps interchangable
SubstitutionMap::lookupConformance() would map archetypes out
of context to compute a conformance path. Do the same thing
in SubstitutionMap::lookupSubstitution().

The DenseMap of replacement types in a SubstitutionMap now
always has GenericTypeParamTypes as keys.

This simplifies some code and brings us one step closer to
a more efficient representation of SubstitutionMaps.
2017-04-24 14:12:36 -07:00
Doug Gregor
d21d6f1585 [Generic environment] Only substitute top-level archetypes.
The core substitution routine for the archetypes-to-interface types
substitution was attempting to provide mappings for nested archetypes,
when in fact these archetypes would (1) always be resolvable via their
parent, and (2) could in fact cause infinite recursion, as with the
new test case. Fixes SR-4617 / rdar://problem/31673819.
2017-04-18 13:19:21 -07:00
Doug Gregor
17846e2be1 [GSB] Cope with recursive requirements by delaying them.
Rather than detecting recursion and bailing early, delay requirements
that would form recursive types. Note that we aren't actually
processing them later.
2017-04-17 23:13:21 -07:00
Doug Gregor
d0499a3613 [SubstitutionMap] Eliminate the parent map and handle archetype conformances.
When looking for a conformance for an archetype, map it out of context
to compute the conformance access path, then do the actual lookups
based on mapping the starting type back into the context. Eliminate
the parent map and "walk the conformances" functionality.
2017-04-04 10:58:01 -07:00
Doug Gregor
81c21b85f2 [AST] Track the generic signature/environment for a substitution map.
Substitution maps are effectively tied to a particular generic
signature or environment; keep track of that signature/environment so
that we can (eventually) use it to find conformances.
2017-04-04 10:58:01 -07:00
Roman Levenstein
6f54798df8 Verify the construction of SubstitutionMaps
After substitution maps are constructed, check their invariants. It helps to catch very subtle bugs related to substitutions.
2017-03-20 15:30:12 -07:00
Slava Pestov
0f4a7d246f AST: Remove GenericSignature::getAllDependentTypes()
This was a remnant of the old generics implementation, where
all nested types were expanded into an AllArchetypes list.

For quite some time, this method no longer returned *all*
dependent types, only those with generic requirements on
them, and all if its remaining uses were a bit convoluted.

- In the generic specialization code, we used this to mangle
  substitutions for generic parameters that are not subject
  to a concrete same-type constraint.

  A new GenericSignature::getSubstitutableParams()
  function handles this use-case instead. It is similar
  to getGenericParams(), but only returns generic parameters
  which require substitution.

  In the future, SubstitutionLists will only store replacement
  types for these generic parameters, instead of the list of
  types that we used to produce from getAllDependentTypes().

- In specialization mangling and speculative devirtualization,
  we relied on SubstitutionLists having the same size and
  order as getAllDependentTypes(). It's better to turn the
  SubstitutionList into a SubstitutionMap instead, and do lookups
  into the map.

- In the SIL parser, we were making a pass over the generic
  requirements before looking at getAllDependentTypes();
  enumeratePairedRequirements() gives the correct information
  upfront.

- In SIL box serialization, we don't serialize the size of the
  substitution list, since it's available from the generic
  signature. Add a GenericSignature::getSubstitutionListSize()
  method, but that will go away soon once SubstitionList
  serialization only serializes replacement types for generic
  parameters.

- A few remaining uses now call enumeratePairedRequirements()
  directly.
2017-03-02 22:57:52 -08:00
Doug Gregor
5f4b2cfec0 Revert "[Generic environment] Weaken an assertion when forming a substitution map."
This reverts commit 485ed7b4ee. The
original assertion should now suffice.
2017-02-23 10:43:10 -08:00
Doug Gregor
485ed7b4ee [Generic environment] Weaken an assertion when forming a substitution map.
It turns out that GenericSignature::getAllDependentTypes() sometimes
includes generic type parameter types that have been made
concrete. Tolerate this for now, because fixing it causes issues
elsewhere.
2017-02-22 17:29:35 -08:00
Slava Pestov
3cd7bb2571 AST: Tighten up some assertions 2017-02-12 01:42:35 -08:00
Doug Gregor
579af863c5 Rename ArchetypeBuilder -> GenericSignatureBuilder 2017-02-10 12:46:34 -08:00
Slava Pestov
bd4f31025f AST: GenericEnvironment::mapTypeIntoContext() no longer needs to take a ModuleDecl
Now, use LookUpConformanceInSignature instead.
2017-02-07 19:25:34 -08:00
Slava Pestov
79e86b66db AST: Remove Generic{Signature,Environment}::getSubstitutionMap() that update an existing map
These are no longer necessary now that we have combineSubstitutionMaps(),
and will not make sense once we switch to a more compact representation
for SubstitutionMap.
2017-02-06 22:39:27 -08:00
Slava Pestov
f2173e9b24 AST: Remove ModuleDecl parameter from GenericEnvironment::getSubstitutionMap()
This method maps interface types to archetypes, which in general
requires a module for performing conformance lookups, if mapping
a member type of a generic parameter which has been made concrete.

However, in practice the types we are mapping here are all canonical
with respect to the generic signature, because they came from
GenericSignature::getAllDependentTypes(), so we actually don't need
to do conformance lookups.

This allows some code to be simplified.
2017-02-06 22:39:26 -08:00
Slava Pestov
3519e0cd25 AST: Introduce new SubstitutionList type to replace ArrayRef<Substitution>
SubstitutionList is going to be a more compact representation of
a SubstitutionMap, suitable for inline allocation inside another
object.

For now, it's just a typedef for ArrayRef<Substitution>.
2017-02-06 21:36:33 -08:00
Slava Pestov
3fb9ffdb0c AST: New variant of GenericEnvironment::getSubstitutionMap()
Just like the recently-added GenericSignature::getSubstitutionMap(),
this takes a type substitution function and conformance lookup
function and produces a new SubstitionMap.
2017-02-06 16:14:08 -08:00
Slava Pestov
fc3244b2f2 Sema: Remove some redundant conformance lookup callbacks
Now we have LookUpConformanceInModule and
LookUpConformanceInSubstitutionMap.
2017-02-03 19:55:40 -08:00
Doug Gregor
f7f703ad04 [Archetype builder] Canonicalize and minimize same-type constraints.
Introduce an algorithm to canonicalize and minimize same-type
constraints. The algorithm itself computes the equivalence classes
that would exist if all explicitly-provided same-type constraints are
ignored, and then forms a minimal, canonical set of explicit same-type
constraints to reform the actual equivalence class known to the type
checker. This should eliminate a number of problems we've seen with
inconsistently-chosen same-type constraints affecting
canonicalization.
2017-02-01 10:51:02 -08:00
Roman Levenstein
99698c63fa Define AST level and SIL level representations of the @_specialize attribute.
This also includes serialization/deserialization and printing of this attribute.
2017-01-18 16:42:10 -08:00
Slava Pestov
24b2531e5f AST: Have SubstitutionMaps manage the memory for the conformance array 2017-01-18 00:15:59 -08:00
Doug Gregor
ac5e74601b [Archetype builder] Use a LookupConformanceFn to resolve protocol conformances.
Instead of creating an archetype builder with a module---which was
only used for protocol conformance lookups of concrete types
anyway---create it with a LookupConformanceFn. This is NFC for now,
but moves us closer to making archetype builders more canonicalizable
and reusable.
2017-01-10 16:40:38 -08:00
Doug Gregor
ad90116da9 [Serialization] Wire up archetype generic environments eagerly.
Teach the serialized form of ArchetypeType about its owning generic
environment, so we can wire up the generic environment of (primary)
archetypes eagerly (at the point of deserialization) rather than when
we form the generic environment. This ensures that there is no point
at which we have a (non-opened-existential) archetype without a
generic environment.

... except that the type reconstruction code creates such archetypes.
2017-01-06 11:16:42 -08:00
Doug Gregor
b75ef56bdc [Serialization] Record generic environments for deserialized archetypes. 2017-01-06 11:16:42 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Doug Gregor
16585992f6 [AST] Strengthen signature of SubstututionMap::addSubstitution().
It requires a CanSubstitutableType internally, so use that in the
signature and fix up all of the callers.
2017-01-05 16:04:19 -08:00
Slava Pestov
3cbc08cc4e AST/SIL: Fix problems if protocol requirement signature makes Self : P conformance implicit
Fixes assertion failures in SILGen and the optimizer with this
exotic setup:

protocol P {
  associatedtype T : Q
}

protocol Q {
  func requirement<U : P>(u: U) where U.T == Self
}

Here, we only have a U : P conformance, and not Self : Q,
because Self : Q is available as U.T : Q.

There were three problems here:

- The SIL verifier was too strict in verifying the generic signature.
  All that matters is we can get the Self parameter conformance, not
  that it's the first requirement, etc.

- GenericSignature::getSubstitutionMap() had a TODO concerning handling
  of same-type constraints -- this is the first test-case I've found
  that triggered the problem.

- GenericEnvironment::getSubstitutionMap() incorrectly ignored
  same-type constraints where one of the two types was a generic
  parameter.

Fixes <https://bugs.swift.org/browse/SR-3321>.
2017-01-04 02:28:55 -08:00
Slava Pestov
cc2ae7b7d5 AST: Fix crash with invalid generic environment 2017-01-04 00:10:28 -08:00
Slava Pestov
caa7045ae5 AST: Remove unnecessary ModuleDecl parameter from GenericSignature::getSubstitutions() 2016-12-22 14:33:00 -05:00
Slava Pestov
bb839c5fef Sema: Remove a couple of workarounds 2016-12-19 01:38:23 -08:00
Slava Pestov
fb0f372e94 AST: Move mapType{In,OutOf}Context() out of ArchetypeBuilder and clean up headers
- The DeclContext versions of these methods have equivalents
  on the DeclContext class; use them instead.

- The GenericEnvironment versions of these methods are now
  static methods on the GenericEnvironment class. Note that
  these are not made redundant by the instance methods on
  GenericEnvironment, since the static methods can also be
  called with a null GenericEnvironment, in which case they
  just assert that the type is fully concrete.

- Remove some unnecessary #includes of ArchetypeBuilder.h
  and GenericEnvironment.h. Now changes to these files
  result in a lot less recompilation.
2016-12-18 19:55:41 -08:00
Doug Gregor
821965dee4 [Serialization] Give generic environments identitity.
Serialize generic environments via a generic environment ID with a
separte offset table, so we have identity for the generic environments
and will share generic environments on deserialization.
2016-12-16 08:41:43 -08:00
Joe Groff
57d9ad0a03 Remove Module parameter from GenericEnvironment::mapTypeOutOfContext.
The substitution only replaces archetypes with abstract generic parameters, so no conformance lookup is necessary, and we can provide a "lookup" callback now that just vends abstract conformances.

(Ideally, we'd be able to do this for mapTypeIntoContext too, but we run into problems with generic signatures with same-type constraints on associated types with protocol requirements. Mapping `t_0_0.AssocType` into such a context will require conformance lookup for the concrete type replacement, since same-type Requirements don't preserve the conformances that satisfy the protocol requirements for the same-type relationship.)
2016-12-15 10:56:19 -08:00
Joe Groff
88d064523a Change TypeSubstitutionFn-based subst to use a LookupConformanceFn instead of a ModuleDecl.
This is a more flexible interface that allows substitution operations to avoid needing an eager SubstitutionMap without relying on module-based conformance lookup. NFC yet.
2016-12-15 09:21:29 -08:00
Joe Groff
ccfabd1118 Make LookupConformanceFn callbacks return Optional<ProtocolConformanceRef>.
NFC yet, but this is a step toward centralizing error handling policy for failed conformance lookups instead of leaving it ad-hoc.
2016-12-14 18:04:35 -08:00
practicalswift
5c874877b5 [gardening] Fix recently introduced typo. 2016-12-08 18:47:11 +01:00
Doug Gregor
c250acf8d9 [AST] Record the "owning" declaration context of generic environments.
While not strictly needed for type checking, it's extremely useful for
debugging and verification to know what context a particular generic
environment is associated with. This information was in a kludgy side
table, but it's worth a pointer in GenericEnvironment to always have
it available.
2016-12-07 21:35:53 -08:00