Commit Graph

779 Commits

Author SHA1 Message Date
Joe Groff
ee08197010 Factor appendAnyProtocolConformance out of the conformance mangling.
We want to be able to use mangled names to refer to protocol conformances in addition to type
metadata. Provide an ASTMangler method that can render an arbitrary abstract or concrete
`ProtocolConformanceRef`, factoring it out of the code used to emit conditional conformance arguments
in `appendProtocolConformance`.
2020-03-20 15:06:42 -07:00
Slava Pestov
9ec80df97e SIL: Remove curried SILDeclRefs 2020-03-19 02:20:21 -04:00
John McCall
81ba9fd223 Merge pull request #30309 from rjmccall/accessor-subst-function-types
Use pattern substitutions to consistently abstract yields
2020-03-10 13:05:28 -04:00
John McCall
07f03bd287 Use pattern substitutions to consistently abstract yields.
The design implemented in this patch is that we lower the types of accessors with pattern substitutions when lowering them against a different accessor, which happens with class overrides and protocol witnesses, and that we introduce pattern substitutions when substituting into a non-patterned coroutine type.  This seems to achieve consistent abstraction without introduce a ton of new complexity.

An earlier version of this patch tried to define witness thunks (conservatively, just for accessors) by simply applying the requirement substitutions directly to the requirement.  Conceptually that should work, but I ran into a lot of trouble with things that assumed that pattern substitutions didn't conceal significant substitution work.  for example, resolving a dependent member in a component type could find a new use of an opaque archetype when the code assumed that such types had already been substituted away.  So while I think that is definiteely a promising direction, I had to back that out in order to make the number of changes manageable for a single PR.

As part of this, I had to fix a number of little bugs here and there, some of which I just introduced.  One of these bugfixes is a place where the substitution code was trying to improperly abstract function types when substituting them in for a type parameter, and it's been in the code for a really long time, and I'm really not sure how it's never blown up before.

I'm increasingly of the opinion that invocation substitutions are not actually necessary, but that --- after we've solved the substitution issues above --- we may want the ability to build multiple levels of pattern substitution so that we can guarantee that e.g. witness thunks always have the exact component structure of the requirement before a certain level of substitution, thus allowing the witness substitutions to be easily extracted.
2020-03-10 01:26:31 -04:00
Argyrios Kyrtzidis
21e1fc4039 [AST/ASTMangler] Fix linker error for SWIFT_BUILD_ONLY_SYNTAXPARSERLIB build
With such a build we avoid linking the `clangAST` library.
2020-03-09 19:43:37 -07:00
John McCall
ceff414820 Distinguish invocation and pattern substitutions on SILFunctionType.
In order to allow this, I've had to rework the syntax of substituted function types; what was previously spelled `<T> in () -> T for <X>` is now spelled `@substituted <T> () -> T for <X>`.  I think this is a nice improvement for readability, but it did require me to churn a lot of test cases.

Distinguishing the substitutions has two chief advantages over the existing representation.  First, the semantics seem quite a bit clearer at use points; the `implicit` bit was very subtle and not always obvious how to use.  More importantly, it allows the expression of generic function types that must satisfy a particular generic abstraction pattern, which was otherwise impossible to express.

As an example of the latter, consider the following protocol conformance:

```
protocol P { func foo() }
struct A<T> : P { func foo() {} }
```

The lowered signature of `P.foo` is `<Self: P> (@in_guaranteed Self) -> ()`.  Without this change, the lowered signature of `A.foo`'s witness would be `<T> (@in_guaranteed A<T>) -> ()`, which does not preserve information about the conformance substitution in any useful way.  With this change, the lowered signature of this witness could be `<T> @substituted <Self: P> (@in_guaranteed Self) -> () for <A<T>>`, which nicely preserves the exact substitutions which relate the witness to the requirement.

When we adopt this, it will both obviate the need for the special witness-table conformance field in SILFunctionType and make it far simpler for the SILOptimizer to devirtualize witness methods.  This patch does not actually take that step, however; it merely makes it possible to do so.

As another piece of unfinished business, while `SILFunctionType::substGenericArgs()` conceptually ought to simply set the given substitutions as the invocation substitutions, that would disturb a number of places that expect that method to produce an unsubstituted type.  This patch only set invocation arguments when the generic type is a substituted type, which we currently never produce in type-lowering.

My plan is to start by producing substituted function types for accessors.  Accessors are an important case because the coroutine continuation function is essentially an implicit component of the function type which the current substitution rules simply erase the intended abstraction of.  They're also used in narrower ways that should exercise less of the optimizer.
2020-03-07 16:25:59 -05:00
Joe Groff
fe1186447b IRGen: Generate runtime type manglings using ObjC runtime names.
In order for the runtime demangler to be able to find ObjC classes and protocols, it needs to
have the runtime name of the declaration be in the mangled name. Only do this for runtime manglings,
to minimize the potential ABI impact for symbol names that already have the source-level names of
ObjC entities baked in. Fixes SR-12169 | rdar://59306590.
2020-03-05 16:55:00 -08:00
John McCall
9df969a627 Mangle coroutine information when mangling SILFunctionTypes. 2020-03-01 22:40:43 -05:00
Luciano Almeida
f4b530fb95 [NFC] Abstracting isStdlibType and isStdLibDecl logic into Type and Decl 2020-02-28 07:40:08 -03:00
Dan Zheng
849bd62a26 [AutoDiff upstream] Add SIL differentiability witnesses. (#29623)
SIL differentiability witnesses are a new top-level SIL construct mapping
"original" SIL functions to derivative SIL functions.

SIL differentiability witnesses have the following components:
- "Original" `SILFunction`.
- SIL linkage.
- Differentiability parameter indices (`IndexSubset`).
- Differentiability result indices (`IndexSubset`).
- Derivative `GenericSignature` representing differentiability generic
  requirements (optional).
- JVP derivative `SILFunction` (optional).
- VJP derivative `SILFunction` (optional).
- "Is serialized?" bit.

This patch adds the `SILDifferentiabilityWitness` data structure, with
documentation, parsing, and printing.

Resolves TF-911.

Todos:
- TF-1136: upstream `SILDifferentiabilityWitness` serialization.
- TF-1137: upstream `SILDifferentiabilityWitness` verification.
- TF-1138: upstream `SILDifferentiabilityWitness` SILGen from
  `@differentiable` and `@derivative` attributes.
- TF-20: robust mangling for `SILDifferentiabilityWitness` names.
2020-02-04 12:53:27 -08:00
Dan Zheng
1486d6b346 NFC: Add GenericSignature::getCanonicalSignature. (#29105)
Motivation: `GenericSignatureImpl::getCanonicalSignature` crashes for
`GenericSignature` with underlying `nullptr`. This led to verbose workarounds
when computing `CanGenericSignature` from `GenericSignature`.

Solution: `GenericSignature::getCanonicalSignature` is a wrapper around
`GenericSignatureImpl::getCanonicalSignature` that returns the canonical
signature, or `nullptr` if the underlying pointer is `nullptr`.

Rewrite all verbose workarounds using `GenericSignature::getCanonicalSignature`.
2020-01-12 12:17:41 -08:00
Xi Ge
b5a0906ebc ASTMangler: ignore original module name when DWARFMangling is enabled
Enabling DWARFMangling indicates the mangled name will be used by either
debugger or IDE. We could and should avoid using the original module name so
demangling will keep working.
2020-01-03 13:29:25 -08:00
Joe Groff
8a45d5a518 Mangler: Support substituted SIL function types in thunk manglings 2019-12-10 12:08:42 -08:00
Xi Ge
1390576528 Merge pull request #28528 from nkcsgexi/use-alternate-module-name
ASTMangler: use specified module names from @_originalDefinedIn to mangle symbols names
2019-12-03 07:25:22 -08:00
Xi Ge
7e8ed50233 ASTMangler: use specified module names from @_originalDefinedIn to mangle symbols names
When an original module name is specified via @_originalDefinedIn attribute, we need to
use the original module name for all related runtime symbol names instead of the current
module names.

rdar://55268186
2019-12-02 16:58:31 -08:00
swift-ci
3ab63dd391 Merge pull request #28420 from nathawes/accessor-usrs-fix 2019-12-02 12:44:10 -08:00
Nathan Hawes
77f3cb013f [Index] Fix pseudo accessor USRs generation to distinguish instance vs static accessors
It was hard-coding IsStatic to false rather than passing it through from the
VarDecl, giving the same USR for the two getters in the below:

class FixtureClass95 {
    private static var someVar: String!
    private var someVar: String!
}

Resolves rdar://problem/44531531
2019-11-21 15:11:47 -08:00
Nathan Hawes
d4c0acf3e9 [ASTMangler] Don't verify the USR mangling for invalid decls
USRs generated for the index store and sourcekit requests for invalid decls are
best-effort – we don't need to guarantee they can be de- or re-mangled.
2019-11-20 16:57:51 -08:00
Robert Widmann
4996858c4d Re-implement isInvalid for ValueDecls 2019-10-30 15:09:14 -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
Joe Groff
dc0f770364 remove todo warnings, oops 2019-10-26 10:49:47 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Robert Widmann
3b829943af Uniformly iterate over the pattern binding entry indices
Clarify a bunch of C-style for loops and remove a ton of references to getPatternList().
2019-10-17 13:39:07 -07:00
Robert Widmann
56b6e53dae Remove raw references to PatternBindingEntry APIs
Switch most callers to explicit indices.  The exceptions lie in things that needs to manipulate the parsed output directly including the Parser and components of the ASTScope.  These are included as friend class exceptions.
2019-10-17 13:31:14 -07:00
Jordan Rose
171ff440fc Remove swift::reversed in favor of llvm::reverse (#27610)
The former predates the latter, but we don't need it anymore! The
latter has more features anyway.

No functionality change.
2019-10-10 17:16:09 -07:00
Suyash Srijan
b5218719ff Merge pull request #27547 from theblixguy/fix/SR-11521
[ASTMangler] Do not crash when mangling a retroactive conformance
2019-10-11 01:11:41 +01:00
Suyash Srijan
97339a6ace [ASTMangler] Use llvm::SaveAndRestore to update the CurGenericSignature 2019-10-10 19:56:42 +01:00
Xi Ge
a877fe6ee3 ASTMangler: EnumElementDecl could also be a DeclContext with a parameter list 2019-10-09 16:24:20 -07:00
Suyash Srijan
9e89d56c57 [ASTMangler] Do not crash when mangling a retroactive conformance 2019-10-09 18:14:53 +01:00
Slava Pestov
6e70b397a7 IDE: Remove workaround for rdar://problem/54310026 2019-10-08 01:37:09 -04:00
Argyrios Kyrtzidis
483d835abe [AST/ASTMangler] Fix linker error for SWIFT_BUILD_ONLY_SYNTAXPARSERLIB build
With such a build we avoid linking the `clangAST` library.
2019-10-02 07:42:19 -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
Jordan Rose
a6dd630ca3 Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject
at the SIL and IRGen layers. Some notes:

- Reflection still uses the notion of "unknown object" to mean an
  object with unknown refcounting. There's no real reason to make
  this different from AnyObject (an existential containing a
  single object with unknown refcounting), but this way nothing
  changes for clients of Reflection, and it's consistent with how
  native objects are represented.

- The value witness table and reflection descriptor for AnyObject
  use the mangling "BO" instead of "yXl".

- The demangler and remangler continue to support "BO" because it's
  still in use as a type encoding, even if it's not an AST-level
  Type anymore.

- Type-based alias analysis for Builtin.UnknownObject was incorrect,
  so it's a good thing we weren't using it.

- Same with enum layout. (This one assumed UnknownObject never
  referred to an Objective-C tagged pointer. That certainly wasn't how
  we were using it!)
2019-09-26 17:48:04 -07:00
Doug Gregor
9b20012ee1 Merge pull request #27322 from DougGregor/property-wrapper-composition-di
[DI] Support definite initialization for composed property wrappers.
2019-09-24 21:22:43 -07:00
Slava Pestov
b1ef4c8561 ASTMangler: Desugar unrepresentable type alias types 2019-09-24 17:42:11 -04:00
Doug Gregor
ab5d161c05 [SILGen] Separate the initialization of a wrapped property from a wrapped value
Teach SILGen to emit a separate SIL function to capture the
initialization of the backing storage type for a wrapped property
based on the wrapped value. This eliminates manual code expansion at
every use site.
2019-09-24 09:11:53 -07:00
Robert Widmann
ed18f47d05 Port getInterfaceType() patterns in libAST 2019-09-23 16:49:09 -07:00
Jordan Rose
853caa66d4 [AST] Split FileUnit and its subclasses out of Module.h
Most of AST, Parse, and Sema deal with FileUnits regularly, but SIL
and IRGen certainly don't. Split FileUnit out into its own header to
cut down on recompilation times when something changes.

No functionality change.
2019-09-17 17:54:41 -07:00
Nathan Hawes
21636981ff Merge pull request #27108 from nathawes/r54310026-usr-generation-crash-in-invalid-code
[ASTMangler] Fix USR generation/mangling crash in invalid code
2019-09-11 13:59:52 -07:00
Nathan Hawes
d48b6204de [ASTMangler] Fix USR generation/mangling crash in invalid code
In invalid code a decl may end up with an opened archetype type that the
mangler doesn't expect. We still want to be able to generate a USR for these
decls so that we can index and rename their occurences successfully. To allow
this, this patch calls mapTypeOutOfContext on primary or opened archetype types
prior to mangling.

Resolves rdar://problem/54310026
2019-09-11 10:59:04 -07:00
Arnold Schwaighofer
47078b4b4a Mangler: Use llvm::SaveAndRestore to save/restore generic signature
NFC
2019-09-09 12:30:41 -07:00
Joe Groff
01c4717573 Merge pull request #27014 from jckarter/opaque-type-assoc-type-mangling
Mangle associated types of opaque types like dependent member types.  …
2019-09-04 12:36:08 -07:00
Joe Groff
6817c53031 Mangle associated types of opaque types like dependent member types.
The archetype mangling does not have enough information to accurately recover the associated type
at runtime. This fixes rdar://problem/54084733.

Although this changes the mangling in both runtime and symbols, this should not affect ABI, because
there is no way for associated types of opaque types to be surfaced in the types of public
declarations today.
2019-09-04 10:20:10 -07:00
Arnold Schwaighofer
55efcb74ae IRGen: Save the current generic signature before mangling an opaque type decl
It comes with its own generic signature.

rdar://54824119
2019-09-04 07:15:43 -07:00
Slava Pestov
2e3f4acfd1 AST: Fix mangling of unvalidated declarations
This is again just a temporary workaround until getInterfaceType() can
kick off a request.
2019-08-06 17:57:41 -04:00
pschuh
74803f0ab4 Import namespaces as EnumDecl. (#26339) 2019-07-30 15:17:08 -07:00
Erik Eckstein
6c88ca79f1 ASTMangler: fix mangling of generic ObjC types
The mangled context must match the number of type argument lists. This was not the case if the bound generic nominal had a special mangling context.

rdar://problem/53418742
2019-07-25 13:31:04 +02:00
John McCall
7095953f84 Fix the demangling of dependent protocol conformance refs.
When mangling a dependent protocol conformance ref, the mangler currently uses `0_` to mean an unknown index and `N_` to mean the index `N - 1`. Unfortunately, this is somewhat confused: `0_` is actually the mangling for index 1, and index 0 is supposed to be mangled as just `_`, so true indexes are actually offset by 2. So the first thing to do here is to clarify what's going on throughout the mangler, demangler, and ABI documentation.

Also, the demangler attempts to produce a `DependentProtocolConformance*` node with the appropriate child nodes and an optional index payload. Unfortunately, demangle nodes cannot have both children and a value payload, so whenever it creates a node with an index payload, the demangler will assert.  It does this whenever the mangled index is not 0; since (per above) the mangler always produces a non-zero mangled index in this production, the demangler will always assert when processing these. So clearly this is well-tested code, since +asserts builds will always trigger the demangler when mangling a name in the first place.  To fix this, we need to make the index a child of the mangling node instead of its payload; at the same time, we can make it store the semantically correct index value and just introduce a new `UnknownIndex` node to handle the `0_` case.  This is easy because all current clients ignore this information.

Finally, due to an apparent copy-and-paste error, the demangler attempts to produce a `DependentProtocolConformanceRoot` node for associated protocol conformances; this is easily resolved.

This fixes the crash in SR-10926 (rdar://51710424).  The obscurity of this crash --- which originally made us think it might be related to Error self-conformance --- is because it is only triggered when a function signature takes advantage of a concrete-but-dependent retroactive conformance, which (to be both concrete and dependent) must furthermore be conditional.  Testing the other cases besides a root conformance requires an even more obscure testcase.
2019-07-09 17:25:44 -04:00
John McCall
5e24f8d916 Remove error-prone overload of Mangler::appendOperator.
Unfortunately we're probably stuck with the bizarre behavior usage
here for dependent protocol conformances.
2019-07-09 01:18:36 -04:00
Jordan Rose
8157ccfce5 Teach SubstitutionMap::isIdentity about non-canonical generic params (#25767)
Do a weaker check here that only looks at the canonical generic params
and guarantees that *those* substitute to themselves. There may be
replacement types for other generic params too, to canonicalize them,
but that's not a problem.

This fixes a crash trying to mangle decls with opaque result types
that have generic signatures that canonicalize away a generic
parameter.

rdar://problem/51775857
2019-06-26 13:09:36 -07:00