Commit Graph

67 Commits

Author SHA1 Message Date
Dan Zheng
c07b3b05ec NFC: add operator<< for SubstitutionMap. (#32637)
This facilitates debugging.
2020-07-05 14:29:40 -07:00
Anthony Latsis
eedb335a08 [NFC] AST: Define SubstitutionMap::getInnermostReplacementTypes 2020-05-15 14:09:47 +03:00
Slava Pestov
2d670f40a0 AST: Update documentation comment in SubstitutionMap.h 2020-01-08 22:19:15 -05:00
Arnold Schwaighofer
f5fe7c1868 Add an API to map substitution map into a type expansion context. 2019-11-11 14:21:52 -08:00
Brent Royal-Gordon
17169fc1fe Merge pull request #27950 from brentdax/dumpster-fire
[NFC] Standardize dump() methods in frontend
2019-10-31 20:36:26 -07:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Slava Pestov
2dbeeb0d3f AST: Make SubstFlags::UseErrorType the default behavior
We've fixed a number of bugs recently where callers did not expect
to get a null Type out of subst(). This occurs particularly often
in SourceKit, where the input AST is often invalid and the types
resulting from substitution are mostly used for display.

Let's fix all these potential problems in one fell swoop by changing
subst() to always return a Type, possibly one containing ErrorTypes.

Only a couple of places depended on the old behavior, and they were
easy enough to change from checking for a null Type to checking if
the result responds with true to hasError().

Also while we're at it, simplify a few call sites of subst().
2019-08-22 01:07:50 -04:00
Arnold Schwaighofer
768d1c51a1 Address review feedback 2019-05-01 09:31:07 -07:00
Arnold Schwaighofer
a793dfb451 Respect resilience when specializing opaque type archetypes 2019-05-01 09:31:07 -07:00
Joe Groff
42e1824a30 Mangle opaque result types. 2019-04-17 14:43:32 -07:00
Joe Groff
e3bbd8ce9e Remove ResilienceExpansion from substOpaqueTypes for now.
It's currently meaningless, and it'll require thought to pass the correct value when it becomes
meaningful.
2019-04-17 14:43:32 -07:00
Joe Groff
c771a7e71b SILGen: Substitute away opaque types. 2019-04-17 14:43:32 -07:00
Adrian Prantl
58475ef1ad Add debug info support for inlined and specialized generic variables.
This patch adds SIL-level debug info support for variables whose
static type is rewritten by an optimizer transformation. When a
function is (generic-)specialized or inlined, the static types of
inlined variables my change as they are remapped into the generic
environment of the inlined call site. With this patch all inlined
SILDebugScopes that point to functions with a generic signature are
recursively rewritten to point to clones of the original function with
new unique mangled names. The new mangled names consist of the old
mangled names plus the new substituions, similar (or exactly,
respectively) to how generic specialization is handled.

On libSwiftCore.dylib (x86_64), this yields a 17% increase in unique
source vars and a ~24% increase in variables with a debug location.

rdar://problem/28859432
rdar://problem/34526036
2018-07-31 16:59:56 -07:00
Adrian Prantl
fdad9076f2 Revert "Add debug info support for inlined and specialized generic variables."
There is an assertion failure building the source compatibility suite that
needs to be investigated.

This reverts commit 91f6f34119.
2018-07-07 13:01:01 -07:00
Adrian Prantl
91f6f34119 Add debug info support for inlined and specialized generic variables.
This patch adds SIL-level debug info support for variables whose
static type is rewritten by an optimizer transformation. When a
function is (generic-)specialized or inlined, the static types of
inlined variables my change as they are remapped into the generic
environment of the inlined call site. With this patch all inlined
SILDebugScopes that point to functions with a generic signature are
recursively rewritten to point to clones of the original function with
new unique mangled names. The new mangled names consist of the old
mangled names plus the new substituions, similar (or exactly,
respectively) to how generic specialization is handled.

On libSwiftCore.dylib (x86_64), this yields a 17% increase in unique
source vars and a ~24% increase in variables with a debug location.

rdar://problem/28859432
rdar://problem/34526036
2018-07-06 22:06:48 -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
835c2c9c48 [AST] Break recursion in SubstitutionMap::dump too. 2018-05-15 22:17:52 +10:00
Huon Wilson
9d13361964 [AST] Introduce a minimal SubstitutionMap dump for use inside expressions.
Within a dump of a DeclRef (or, more likely, a larger entity that includes one),
the full multiline details of a substitution map are likely to not be hugely
relevant, and just get in the way of understanding the interesting parts of the
dump. Thus, this is a variant that is a single line and just includes the
substitutions; the conformances (or, at least, the protocols involved) can be
inferred from the generic signature combined with those substitutions.

The style is controlled via a enum rather than a boolean, to avoid problems with
trying to pass an indent like `map->dump(out, indent)`, where the integer indent
coerces to a bool, rather than error.

Fixes rdar://problem/40074968.
2018-05-15 22:17:52 +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
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
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
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
ab56fa3e9c [Serialization] Support (de-)serialization of SubstitutionMaps.
Allow substitution maps to be serialized directly (via an ID), writing out
the replacement types and conformances as appropriate. This is a more
efficient form of serialization than the current SubstitutionList approach,
because it maintains uniqueness of substitution maps within a module file,
and is a step toward eliminating SubstitutionList entirely.
2018-04-30 16:21:58 -07:00
Doug Gregor
547abf1724 [AST] Fix documentation comment for SubstitutionMap. 2018-04-30 13:31:45 -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
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
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
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
6ef76a92f4 [AST] Use flat array storage for SubstitutionMap's replacement types.
The replacement types in a SubstitutionMap correspond with the generic
parameters of its generic signature, so replace the DenseMap storage
with a flat array of Types, one element for each generic parameter.
2017-04-28 16:22:07 -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
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Doug Gregor
6878538ba1 [AST] Remove GenericEnvironment from SubstitutionMap; it's unnecessary. 2017-04-05 11:21:39 -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
5381921131 AST: Generalize SubstitutionMap::combineSubstitutionMaps() 2017-03-08 13:54:28 -08:00
Slava Pestov
de2f5f78ac AST: Remove Substitution::subst()
I want to get rid of Substitution entirely, and now we have
the right abstractions to do everything with SubstitutionMap.
2017-03-07 16:04:54 -08:00
Slava Pestov
13cc1ad07c AST: Remove SubstitutionMap::getConformances() 2017-03-07 16:04:53 -08:00
Slava Pestov
b41888acdf AST: Add SubstitutionMap::subst()
This will replace Substitution::subst().
2017-03-07 16:04:46 -08:00
Slava Pestov
8fe97c46eb AST: Make SubstitutionMap::lookupSubstitution() private 2017-03-02 13:31:15 -08:00
practicalswift
65b0219f7b [gardening] Fix typos 2017-02-14 20:04:08 +01:00