Commit Graph

199 Commits

Author SHA1 Message Date
Slava Pestov
5062a81e3d AST: Start returning SelfProtocolConformances from ModuleDecl::lookupConformance()
Fixes <rdar://problem/49241923>, <https://bugs.swift.org/browse/SR-10015>.
2019-04-16 23:02:50 -04:00
Doug Gregor
4f5d2d3426 [Substitution map] When the superclass conforms to a protocol directly, use it.
When looking up a conformance in a substitution map, check whether
there is a superclass constraint that satisfies the conformance. If
so, use it directly rather than going through a slower path to find
it.

Addresses rdar://problem/46655186.
2018-12-21 15:29:09 -08:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -08:00
Slava Pestov
73f1990bc2 AST: Use getSelfInterfaceType() instead of getProtocolSelfType() where possible
The former appears in the code base a lot more frequently than the
latter, which returns a GenericTypeParamType *. Use it only in places
where the more specific type is intended.
2018-10-15 20:34:08 -07:00
Slava Pestov
a7e3513d8b AST: Use SubstitutionMap::getReplacementTypesBuffer() instead of getReplacementTypes() in a couple of places 2018-10-05 15:35:10 -04:00
Slava Pestov
ce770cdf4e AST: Introduce GenericSignature::forEachParam()
This replaces the inefficient pattern:

  for (auto param : sig->getGenericParams()) {
    if (sig->isCanonicalTypeInContext(param)) {
      ...
    } else {
      ...
    }
  }
2018-09-27 21:28:36 -07:00
Slava Pestov
c09f0578a6 AST: Fast path for SubstitutionMap::lookupConformance() 2018-09-27 21:21:59 -07:00
Doug Gregor
0972111c60 [Type checker] Start tracking overrides of protocol requirements.
When a protocol that inherits another protocol restates a requirement
from its inherited protocol, track that as an override in the AST.
2018-09-04 16:42:06 -07:00
Doug Gregor
c05415ce34 [Override checking] Switch name lookup over to normal qualified lookup.
We don’t need the extra capabilities of the TypeChecker’s name lookup,
so use Decl-based lookup.
2018-09-04 16:42:06 -07:00
Slava Pestov
c906c18db8 AST: More direct implementation of SubstitutionMap::subst()
Instead of using the callback form of SubstitutionMap::get(), let's
use the lower-level form that takes the replacement array and
conformances array directly, allowing us to bypass several
calls of subst() on 'this'.
2018-08-25 00:31:07 -07:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Slava Pestov
fc80f75273 AST: Create new 'invalid' state for ProtocolConformanceRef 2018-07-16 16:44:27 -07:00
Slava Pestov
b288bea84b AST: Use llvm::dbgs instead of llvm::errs when printing SubstitutionMap verification errors 2018-06-15 13:15:16 -07:00
Slava Pestov
d8fc9decf9 AST: Remove GenericSignature::getSubstitutionMap() 2018-05-28 19:45:28 -07:00
Slava Pestov
d888df4e93 AST: Fix SubstitutionMap::verify()
Get the code compiling. It's not enabled yet because it causes
some problems still.
2018-05-28 19:07:56 -07:00
Doug Gregor
b0cc3ff6d3 [SubstitutionMap] Canonicalize non-substitutable replacement types.
When we compute a replacement type for a non-substitutable generic
parameter, canonicalize the type when our generic signature is
canonical. This ensures that the canonical substitution map will
have canonical types for all of the replacement types… including those
for non-substitutable generic parameters that aren’t part of the
FoldingSet profile.
2018-05-23 15:00:55 -07:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07:00
Slava Pestov
ebb1198d57 AST: There's no longer any reason to pass SubstitutionMap by const reference
SubstitutionMaps are now just a trivial pointer-sized value, so
pass them by value instead.

I did have to move a couple of functors from Type.h to SubstitutionMap.h
to resolve some issues with forward declarations.
2018-05-19 00:45:36 -07:00
Huon Wilson
51c14c8a1a Merge pull request #16622 from huonw/better-protocol-conformance-printing
Better protocol conformance and substitution map printing
2018-05-16 08:40:54 +10:00
Huon Wilson
dad4169333 [AST] Make dumping for SubstitutionMap lisp-formatted and indent-aware.
This is much easier to read when part of a larger dump, where it now occurs in
expressions and specialized conformances.

Part of rdar://problem/40074968.
2018-05-15 22:17:52 +10:00
Doug Gregor
140e5bfc84 [AST] Minor SubstitutionMap cleanups suggested by Slava. 2018-05-14 07:03:53 -07:00
Doug Gregor
f078fc8d18 [AST] Don't eagerly form replacements when building a SubstitutionMap.
When forming a substitution map from a type substitution function and
conformance lookup function, we were calling the type substitution function
for generic parameters that aren't canonical (e.g., because they are
same-typed to something else). The type substitution functions wouldn't
necessarily take the new generic signature into account, so we would get
incorrect replacement types.

Detect non-canonical generic parameters when forming substitution maps,
and leave those entries in the "replacements" array blank; we'll fill them
in later, properly, when needed.
2018-05-13 23:51:53 -07:00
Doug Gregor
8010939238 [AST] Eliminate several uses of enumeratePairedRequirements(). 2018-05-11 22:33:03 -07:00
Doug Gregor
ef020c74aa Eliminate all vestiges of Substitution and SubstitutionList.
Introduced during the bring-up of the generics system in July, 2012,
Substitution (and SubstitutionList) has been completely superseded by
SubstitutionMap. R.I.P.
2018-05-11 21:43:40 -07:00
Doug Gregor
38a189f650 [SubstitutionMap] Introduce hasAnySubstitutableParams().
Introduce an operation to check that a given substitution map is non-empty
(i.e., corresponds to a generic signature) and that the generic signature
has type parameters that aren’t bound to concrete types. This is the
appropriate predicate for SIL-and-later to determine whether there
will be any substitutions.
2018-05-11 13:18:06 -07:00
Doug Gregor
dc7a251d4c [SubstitutionMap] Handle substitution of non-canonical generic parameters.
SubstitutionMap::lookupSubstitution() assumed that any type parameter it was
given would already be canonical, and fail (return a null Type) when given
a type parameter that was equivalent to another type parameter but is
not itself canonical. Canonicalize, recursive, and record the replacement
type appropriately.
2018-05-11 13:18:06 -07:00
Doug Gregor
791df5ba55 [AST] Cache the computed SubstitutionList for a SubstitutionMap.
Every SubstitutionMap::toList() invocation would ASTContext-allocate
arrays for all of the conformances, then return a SmallVector of the
underlying substitutions, which both wastes memory *and* puts the onus
on the caller to allocate a copy of that outer SmallVector into the
ASTContext if it's going to be stored anywhere.

Stop the madness by caching an ASTContext-allocated SubstitutionList
within the storage for the SubstitutionMap. This both reduces repeated
allocations and eliminates the potential for errors.
2018-05-03 10:38:01 -07:00
Doug Gregor
4136a1897a [AST] Split SubstitutionMap::Storage into its own header.
Now that SubstitutionMap is used in so many places, reduce it's header
dependencies by moving SubstitutionMap::Storage into its own separate
implementation header. Use forward declarations of other entities
(GenericSignature, Substitution) instead.

Good for build times and general sanity.
2018-05-03 09:49:09 -07:00
Pavel Yaskevich
01312d9623 Merge pull request #16276 from xedin/add-implicit-inits-with-getparams
[Sema] Refactor member type matching from `checkOverrides` to make it reusable
2018-05-03 00:16:24 -07:00
Pavel Yaskevich
394ac6f4f1 [AST] lookupSubstition should produce nothing if there are not substitutions
If `SubstitutionMap` is empty there is nothing to replace so
we should always return empty substitution type and/or conformance ref.
2018-05-02 15:11:25 -07:00
Doug Gregor
bc5bbe8b64 [AST] Use SubstitutionMap, not SubstitutionList, in SILBoxType.
Eliminate the last place in the AST proper that stores
SubstitutionLists rather than SubstitutionMaps. Randomly fixes a
crasher, too.
2018-05-02 13:39:21 -07:00
Doug Gregor
3e2bed119b [AST] Switch SpecializedProtocolConformance over to SubstitutionMap.
Eliminate another common use of SubstitutionList from the AST.
2018-05-02 13:38:15 -07:00
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
Doug Gregor
cdff7427f8 [AST] Unique and ASTContext-allocate SubstitutionMaps.
Prepare for SubstitutionMaps to be stored in other AST nodes by making
them ASTContext-allocated and uniqued (via a FoldingSet). They are now
cheap to copy and have trivial destructors.
2018-04-30 10:42:03 -07:00
Doug Gregor
03a47b87ad [AST] Minor cleanups to SubstitutionMap. 2018-04-30 09:30:44 -07:00
Doug Gregor
cebbe45c23 [AST] Flatten internal representation of SubstitutionMap.
Collapse the out-of-line array of replacement types and the DenseMap used
for conformances into a single field that references out-of-line,
tail-allocated storage for all of the contents of a SubstitutionMap.
The conformances are now represented as a flat array whose indices
line up with the indices of the conformance requirements in the
underlying generic signtature.

Reference this storage via a single shared_ptr, so that copies of
SubstitutionMaps are cheap but we aren't (yet) uniqueing them or
storing them within the ASTContext.
2018-04-30 00:41:28 -07:00
Doug Gregor
7f2c88fad5 [AST] Internalize details of constructing a SubstitutionMap. 2018-04-29 23:03:12 -07:00
Doug Gregor
f02c1051ad [AST] Reimplement SubstitutionMap::subst() in terms of getSubstitutionMap().
The primary implementation for substituting into substitution maps was
cloning the contents of the SubstitutionMap and then performing
in-place updates to the (new) data structure, which both had too many
dependencies on the data structure and subverted some of the checking
we get through the normal path.

Reimplement this function in terms of
GenericSignature::getSubstitutionMap(), which is the primary/canonical
way to form a substitution map.
2018-04-29 22:16:43 -07:00
Doug Gregor
43931a985c [AST] Record generic signature in BoundNameAliasType.
The generic signature used to perform substitutions for a BoundNameAliasType
in ill-formed code might not line up with the generic signature in the
typealias. Separately record the signature we used to build the
BoundNameAliasType to make the AST more robust against such issues.
2018-03-25 20:46:07 -07:00
Doug Gregor
80eae200b6 [AST] Preserve type sugar for generic typealiases
Introduce a new Type node, BoundNameAliasType, which describes a
reference to a typealias that requires substitutions to produce the
underlying type. This new type node is used both for references to
generic typealiases and for references to (non-generic) typealiases
that occur within generic contexts, e.g., Array<Int>.Element.

At present, the new type node is mainly useful in preserving type
sugar for diagnostics purposes, as well as being reflected in other
tools (indexing, code completion, etc.). The intent is to completely
replace NameAliasType in the future.
2018-03-21 23:49:17 -07:00
Doug Gregor
46e623a089 [SubstitutionMap] Handle conformance lookup via superclass requirements.
Conformances that discoverably for a type parameter via a superclass
requirement occupy a bit of a gray area in the type checker and
generic signature handling right now: we have a type parameter (so the
paths that handle concrete conformances don't kick in), but the
conformance itself isn't modeled as a requirement because it is
available via lookup. Teach SubstitutionMap's conformance lookup to
detect this case and perform conformance lookup in the generic
signature (i.e., falling back to global lookup) in this case,
replicating the hack that IRGen uses to address the same issue when
accessing the conformance (see GenArchetype.cpp's TODO where we lookup
a conformance on the superclass). The removal of that hack (as well as
this one) are tracked by rdar://problem/34609744.

For now, fixes SR-7072 / rdar://problem/37904576.
2018-02-26 23:31:46 -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
Jordan Rose
10daf26bf7 [AST] Use CanType in SubstitutionMap
No functionality change---the code was already using canonical types
in practice.
2018-01-30 15:24:06 -08:00
Doug Gregor
9554e61705 [AST] Fix a crash when conformance lookup fails during substitution.
The enclosing conformance-lookup operation can fail (i.e., it returns
Optional), but the nested call was force-unwrapping the optional from
an inner call for no particular reason. Stop doing that, which fixes
SR-6466.

Note that the offending code is still something that should go away in
time, so this is merely polishing the band-aid to avoid a crash that
occurs often with conditional conformances.
2017-11-24 23:18:11 -08:00
Doug Gregor
a92d8d5760 [Cleanup] Reduce dependencies on swift/AST/LazyResolver.h. 2017-10-10 10:01:39 -07:00
Doug Gregor
1f1b75a56d [AST] Eliminate ModuleDecl parameters from GenericSignature. 2017-10-10 10:01:39 -07:00
Slava Pestov
f2582e1311 AST: Replace a few mapTypeOutOfContext() calls with ArchetypeType::getInterfaceType() 2017-10-07 05:44:57 -07:00
Doug Gregor
c0805a2bc8 [Substitution map] Don’t infinitely recurse when inferring type witnesses. 2017-09-14 09:16:14 -07:00
Slava Pestov
9f8760b942 AST: Remove unused 'resolver' parameter from ModuleDecl::lookupConformance()
... as well as a bunch of downstream plumbing that is no
longer necessary.
2017-09-07 03:36:17 -07:00
Slava Pestov
42014882ef AST: Fix potential null dereference in SubstitutionMap::lookupConformance()
If there's no lazy resolver and we're looking at an incomplete conformance,
fail with an error instead of crashing. The caller will likely crash also,
but at least this allows better recovery in this case in the future.
2017-09-07 00:24:12 -07:00