Commit Graph

78 Commits

Author SHA1 Message Date
Joe Pamer
78db42ea71 When obtaining the decl context for an inherited protocol conformance,
use the inherited conformance's decl context if the conformance type
does not yet have a registered class decl.

(Addresses SR-1267 and SR-1270)
2016-04-20 22:42:29 -07:00
John McCall
b3a2762f59 Improve the dumping of ProtocolConformances, Substitutions,
and ErasureExpr.
2016-04-14 10:33:44 -07:00
David Farler
b0d6341420 InheritedProtocolConformance: Return the conforming type's DeclContext
Not the superclass's. This can cause mapping types out of context to not
resolve archetypes, because the DeclContexts won't match, e.g:

internal class _IteratorBox<Base: IteratorProtocol> :
_AnyIteratorBoxBase<Base.Element> { internal override func next() ->
Base.Element?  }

Here, _IteratorBox's associated type Element for IteratorProtocol is
Base.Element, but the conformance comes from the superclass,
_AnyIteratorBoxBase. This meant that Base.Element couldn't be mapped to
an interface type, which should be:

(dependent-member
  (generic-type-parameter depth=0 index=0) member=Element)
2016-03-18 17:50:32 -07:00
Slava Pestov
e1cea881ba AST: Rip out "defaulted definitions" from NormalConformance, NFC
It appears we were only using this to see if an associated type was
derived or defaulted. This code didn't mesh well with the other stuff
I was doing for default implementations, so I'd rather rip it out and
just rely on calling 'isImplicit' to check for derived associated
types instead.

Note that there's a small change of behavior -- if an associated type
is derived for one conformance, and then used as a witness in another,
we were previously only marking it as defaulted in the first one,
but now it is marked as defaulted in both. I do not believe this has
any meaningful consequences.
2016-03-17 03:57:23 -07:00
Doug Gregor
86b7322e87 [Sema -> AST] Refactor "is representable in Objective-C?" checking.
Migrate the check for whether a given type is representable in
Objective-C, which is currently used to verify when @objc can be
inferred or verify that an explicitly-written @objc is well-formed,
from Sema into a set of queries on the Type within the AST library, so
it can be used in other parts of the compiler.

As part of this refactoring, clean up and improve a number of aspects
of this code:

* Unify the "trivially representable" and "representable" code paths
  into a single code path that covers these cases. Clarify the
  different levels of "representable" we have in both the code and
  in comments.

* Distinguish between representation in C vs. representation in
  Objective-C. While we aren't using this now, I'm anticipating it
  being useful to allow exporting C interfaces via @_cdecl (or
  similar).

* Eliminate the special cases for bridging String/Array/Dictionary/Set
  with their Foundation counterparts; we now consult
  _ObjectiveCBridgeable conformances exclusively to get this
  information.

* Cache foreign-representation information on the ASTContext in a
  manner that will let us more easily get the right answer across
  different contexts while providing more sharing than the TypeChecker
  version.

Annoyingly, this only seemed to fix a small class of error where we
were permitting Unsafe(Mutable)Pointer<T> to be representable in
Objective-C when T was representable but not trivially representable,
e.g., T=String or T=AnyObject.Type.
2016-03-16 23:53:48 -07:00
Doug Gregor
659811e261 [AST Verifier] Don't deserialize protocol conformances just to verify them.
Fixes rdar://problem/24922889.
2016-03-02 09:49:24 -08:00
Doug Gregor
1c2ce6f22f [Omit needless words] Add a flag to let us skip overrides and witnesses.
When performing Swift API dumps, it's helpful to avoid putting
redundant APIs into the results. Therefore, filter out any APIs that
are overrides of another API or are witnesses for a protocol
requirement, since the original definition (that doesn't override any
other or is a protocol requirement) is what determines the APIs name.
2016-02-24 17:47:31 -08:00
Joe Groff
a1ef412815 Sema/SILGen: Get property behavior implementations to codegen.
Fix some interface type/context type confusion in the AST synthesis from the previous patch, add a unique private mangling for behavior protocol conformances, and set up SILGen to emit the conformances when property declarations with behaviors are visited. Disable synthesis of the struct memberwise initializer if any instance properties use behaviors; codegen will need to be redesigned here.
2016-02-20 15:01:06 -08:00
Joe Groff
26e55ce465 Sema: Initial parsing and synthesis for properties with behaviors.
Parse 'var [behavior] x: T', and when we see it, try to instantiate the property's
implementation in terms of the given behavior. To start out, behaviors are modeled
as protocols. If the protocol follows this pattern:

  ```
  protocol behavior {
    associatedtype Value
  }
  extension behavior {
    var value: Value { ... }
  }
  ```

then the property is instantiated by forming a conformance to `behavior` where
`Self` is bound to the enclosing type and `Value` is bound to the property's
declared type, and invoking the accessors of the `value` implementation:

  ```
  struct Foo {
    var [behavior] foo: Int
  }

  /* behaves like */

  extension Foo: private behavior {
    @implements(behavior.Value)
    private typealias `[behavior].Value` = Int

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

If the protocol requires a `storage` member, and provides an `initStorage` method
to provide an initial value to the storage:

  ```
  protocol storageBehavior {
    associatedtype Value

    var storage: Something<Value> { ... }
  }
  extension storageBehavior {
    var value: Value { ... }

    static func initStorage() -> Something<Value> { ... }
  }
  ```

then a stored property of the appropriate type is instantiated to witness the
requirement, using `initStorage` to initialize:

  ```
  struct Foo {
    var [storageBehavior] foo: Int
  }

  /* behaves like */

  extension Foo: private storageBehavior {
    @implements(storageBehavior.Value)
    private typealias `[storageBehavior].Value` = Int
    @implements(storageBehavior.storage)
    private var `[storageBehavior].storage`: Something<Int> = initStorage()

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

In either case, the `value` and `storage` properties should support any combination
of get-only/settable and mutating/nonmutating modifiers. The instantiated property
follows the settability and mutating-ness of the `value` implementation. The
protocol can also impose requirements on the `Self` and `Value` types.

Bells and whistles such as initializer expressions, accessors,
out-of-line initialization, etc. are not implemented. Additionally, behaviors
that instantiate storage are currently only supported on instance properties.
This also hasn't been tested past sema yet; SIL and IRGen will likely expose
additional issues.
2016-02-20 15:01:05 -08:00
Jordan Rose
91b72d3802 Fold the rest of PointerLikeTypeTraitsFwdDecl.h into TypeAlignments.h.
TypeAlignments.h predates this whole mess; it was used for types with
stronger alignment in PointerLikeTypeTraits than the old default of
"2 by fiat and assumption". All remaining forward-declared types are
AST types, so fold them into TypeAlignments.h.

(The one exception is SILTypeList.h, but that's already gone on master.)

To avoid future ODR issues, explicitly include TypeAlignments.h into
every header that defines a type it forward-declares.

I wish we could use partial specialization to provide PointerLikeTypeTraits
for all derived classes of Decl, TypeBase, etc, but that's not something
you can do in C++ if you don't control the traits class.
2016-02-06 11:22:28 -08:00
practicalswift
4e033708a3 [gardening] Fix recently introduced typo: "Specalized" → "Specialized" 2016-02-01 23:07:17 +01:00
William Dillon
ab7c87e7e8 Implemented ARMv6 and fixed up ARMv7 2016-01-29 21:41:22 +00:00
John McCall
5112864dad Remove the archetype from Substitution.
This eliminates some minor overheads, but mostly it eliminates
a lot of conceptual complexity due to the overhead basically
appearing outside of its context.
2016-01-08 15:27:13 -08:00
John McCall
2df6880617 Introduce ProtocolConformanceRef. NFC.
The main idea here is that we really, really want to be
able to recover the protocol requirement of a conformance
reference even if it's abstract due to the conforming type
being abstract (e.g. an archetype).  I've made the conversion
from ProtocolConformance* explicit to discourage casual
contamination of the Ref with a null value.

As part of this change, always make conformance arrays in
Substitutions fully parallel to the requirements, as opposed
to occasionally being empty when the conformances are abstract.

As another part of this, I've tried to proactively fix
prospective bugs with partially-concrete conformances, which I
believe can happen with concretely-bound archetypes.

In addition to just giving us stronger invariants, this is
progress towards the removal of the archetype from Substitution.
2016-01-08 00:19:59 -08:00
Jordan Rose
2c4c7bd152 Don't serialize full ConcreteDeclRefs for conformance value witnesses.
A protocol conformance needs to know what declarations satisfy requirements;
these are called "witnesses". For a value (non-type) witness, this takes the
form of a ConcreteDeclRef, i.e. a ValueDecl plus any generic specialization.
(Think Array<Int> rather than Array<T>, but for a function.)

This information is necessary to compile the conformance, but it causes
problems when the conformance is used from other modules. In particular,
the type used in a specialization might itself be a generic type in the
form of an ArchetypeType. ArchetypeTypes can't be meaningfully used
outside their original context, however, so this is a weird thing to
have to deal with. (I'm not going to go into when a generic parameter is
represented by an ArchetypeType vs. a GenericTypeParamType, partially
because I don't think I can explain it well myself.)

The above issue becomes a problem when we go to use the conformance from
another module. If module C uses a conformance from module B that has a
generic witness from module A, it'll think that the archetypes in the
specialization for the witness belong in module B. Which is just wrong.

It turns out, however, that no code is using the full specializations for
witnesses except for when the conformance is being compiled and emitted.
So this commit sidesteps the problem by just not serializing the
specializations that go with the ConcreteDeclRef for a value witness.

This doesn't fix the underlying issue, so we should probably still see
if we can either get archetypes from other contexts out of value witness
ConcreteDeclRefs, or come up with reasonable rules for when they're okay
to use.

rdar://problem/23892955
2016-01-06 14:34:48 -08:00
practicalswift
e0eba97b98 Fix typos. 2016-01-06 00:48:22 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
John McCall
76e324a950 Refactors leading towards the use of protocol witness table access functions.
This re-applies r32541 with a few changes to the demangling logic and associated test fixes.

Swift SVN r32553
2015-10-09 05:49:18 +00:00
Erik Eckstein
19bb23a63f Revert r32541 "Refactors leading towards the use of protocol witness table access functions."
It broke the build: 2 demangle tests failed.




Swift SVN r32552
2015-10-09 04:42:19 +00:00
John McCall
9af9b9914d Refactors leading towards the use of protocol witness table
access functions.  NFC for now.

Swift SVN r32541
2015-10-09 01:06:06 +00:00
Jordan Rose
babdb9e9f9 Delay loading the witnesses of a protocol conformance.
Apart from being general compile-time goodness, this helps break a
circularity issue involving serialization cross-references and the
Clang importer.

The test is being added to validation-tests because it relies on
several levels of non-laziness in the compiler, all of which we'd
like to fix. It's making sure we don't regress here, but it isn't
actually verifying this change in particular.

rdar://problem/22364953

Swift SVN r31455
2015-08-25 21:47:18 +00:00
Doug Gregor
161f309975 Improve recursion-breaking when resolving type witnesses.
Specifically track when we're resolving type witnesses for a given
conformance, and refuse to recursively attempt to resolve those type
witnesses. The standard library patch in rdar://problem/21789238
triggers this, and isolating it in a small test case has proven
illusive.

Swift SVN r30160
2015-07-13 20:06:57 +00:00
Slava Pestov
0affd67832 AST: Kill dead method, remove unnecessary mutable, NFC
Progress on <rdar://problem/20981254>.

Swift SVN r28930
2015-05-22 20:28:18 +00:00
Doug Gregor
b8995b0aa3 Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like
types (Swift.Int), which is captured by ModuleType. They can be
treated like values for disambiguation (Swift.print), which is
captured by ModuleExpr. And we jump through hoops in various places to
store "either a module or a decl".

Start cleaning this up by transforming Module into ModuleDecl, a
TypeDecl that's implicitly created to describe a module. Subsequent
changes will start folding away the special cases (ModuleExpr ->
DeclRefExpr, name lookup results stop having a separate Module case,
etc.).

Note that the Module -> ModuleDecl typedef is there to limit the
changes needed. Much of this patch is actually dealing with the fact
that Module used to have Ctx and Name public members that now need to
be accessed via getASTContext() and getName(), respectively.

Swift SVN r28284
2015-05-07 21:10:50 +00:00
Argyrios Kyrtzidis
5f6d091efc [AST] Keep track of the type decl that the type witness came from, for protocol conformances.
Swift SVN r27352
2015-04-16 06:23:54 +00:00
Doug Gregor
06a92048a1 SILGenConformance works with normal protocol conformances, only.
NFC

Swift SVN r26438
2015-03-23 17:51:40 +00:00
Doug Gregor
dc180a1e7a Introduce a "checking" protocol conformance state.
Allows us to distinguish between "we know this conformance exists" and
"we're doing a detailed check of this conformance". Use it, rather
than membership in the nebulous ASTContext-wide caching structure
"ConformsTo", to detect recursive attempts to complete a conformance.

Swift SVN r26248
2015-03-18 04:31:17 +00:00
Doug Gregor
2f4aee5ce6 Don't profile the conforming type for a normal protocol conformance.
Normal protocol conformances are uniquely determined by protocol and
DeclContext; the type is unnecessary. NFC

Swift SVN r26128
2015-03-14 06:18:12 +00:00
Doug Gregor
a09704bf0a Replace a conformance-walking loop with ProtocolConformance::getRootNormalConformance().
NFC cleanup.

Swift SVN r25994
2015-03-11 21:55:09 +00:00
Doug Gregor
bc799c3fbc Remove ProtocolConformance::getSubstitutedGenericParams().
It's unused and wrong. NFC

Swift SVN r25992
2015-03-11 20:42:47 +00:00
Chris Willmore
5b51620d1d Be more tolerant of invalid protocol conformances when applying constraint solution.
* Lift NormalProtocolConformance::hasTypeWitness() to
  ProtocolConformance. This method can be used to determine whether a
  potentially invalid conformance has a particular type witness.

* ProtocolConformance::getWitness() may return nullptr if the witness
  does not exist.

* In TypeChecker::getWitnessType(), don't complain that a builtin
  protocol is broken if it's the conformance that's broken.

* ConformanceChecker should only emit diagnostics from within calls to
  checkConformance().

* TypeChecker::conformsToProtocol() now returns true if the type
  has a declared conformance to the protocol, regardless of whether the
  conformance is valid. Clients must check the validity of the
  ProtocolConformance themselves if necessary.

<rdar://problem/19495341> Can't upcast to parent types of type constraints without forcing

Swift SVN r25326
2015-02-16 22:41:52 +00:00
Joe Groff
bf3c92c743 Sema: Move 'getBridgedToObjC' out of the type checker.
It is useful for other code to be able to query whether a type is bridged sharing the same logic as the type checker. NFC yet.

Swift SVN r24758
2015-01-27 21:20:39 +00:00
Doug Gregor
8b1d818fb5 Move some archetype-only bits from SubstitutableType to ArchetypeType. NFC
Swift SVN r23120
2014-11-05 21:19:13 +00:00
Manman Ren
c4233e98ff [Printer] use SIL print options when printing SILWitnessTable.
Add PrintOptions to Substitution::print and ProtocolConformance::printName.
rdar://18021102


Swift SVN r22366
2014-09-29 21:59:03 +00:00
John McCall
75050f8166 Generate an implicit 'materializeForSet' accessor
along with getters and setters.

Just generate it for now.

Swift SVN r22011
2014-09-17 08:08:03 +00:00
Doug Gregor
73d50e45ce Don't permit creating specialized conformances of specialized conformances...
... and stop doing it when we're separating the archetypes of
extensions from the types they extend. I wasn't able to isolate a
useful test case for this; it triggers when building the standard
library with extension archetypes separated.


Swift SVN r20814
2014-07-31 05:56:27 +00:00
Doug Gregor
ba040d9f21 Maintain the DeclContext of a NormalProtocolConformance as the type declaration or extension.
Previously, we only retained the module in which a normal protocol
conformance occurred, which meant we either had to go searching for
the appropriate extension (yuck) or do without that information. This
is part of the separating-extension-archetypes work.

Swift SVN r20793
2014-07-31 01:00:30 +00:00
Doug Gregor
bd75425b0e Remove the notion of non-inheritable conformances entirely <rdar://problem/16880016>.
Swift SVN r20627
2014-07-28 16:29:05 +00:00
Michael Gottesman
c1bd3560af [devirtualization] Make ProtocolConformance::getGenericParams() more
sane and ad ProtocolConformance::getSubstitutedGenericParams() for
returning the generic params that were substituted into by a specialized
protocol conformance.

I am going to use this to handle inherited specialized inherited
conformance devirtualization.

Swift SVN r16907
2014-04-27 02:47:44 +00:00
Joe Pamer
18f3bf67b0 More progress towards getting same-type constraints working correctly.
Swift SVN r16023
2014-04-07 21:31:52 +00:00
Michael Gottesman
2235221854 [deserialization] Unique normal protocol conformances in ASTContext like we do for specialized/inherited protocol conformances.
This fixes the following two bugs:

1. We sometimes would create new conformances when deserializing a
witness method which would not be mapped in the SILModule to the
appropriate witness table. This would cause us to be unable to perform
devirtualization of this witness method. This is tested via a new
verifier check.

2. Different conformances would be created for an instance of a base
protocol and the original protocol. This would cause IRGen to try to
emit witness table global variables with differing types, hitting an
assertion. This is tested via a traditional test.

Swift SVN r15362
2014-03-22 05:05:48 +00:00
Michael Gottesman
c45fdc91ab When performing a folding set profile of a specialied/inherited protocol conformance use the canonical version of the conforming type as the key rather than the type itself.
After chatting with Doug about this issue, the conclusion was reached that it is
more important to have the pointer identity of a protocol conformance be the
same for all types that reduce to the same canonical type.

Swift SVN r15335
2014-03-21 21:27:31 +00:00
Joe Groff
a404b0cd49 Rework Substitution::subst not to depend on the meaningless 'Archetype' field of Substitutions.
Pass in the context generic parameters that correspond to the substitution vector in Substitution::subst. When we build the type substitution map, also collect the conformances from the substitutions into a map we can use to fill in those conformances in substituted substitutions where necessary, without relying on the '.Archetype' field.

Swift SVN r14964
2014-03-12 20:29:46 +00:00
Doug Gregor
926e3711d0 Only permit inheritance of protocol conformance when it is semantically valid.
A protocol conformance of a class A to a protocol P can be inherited
by a subclass B of A unless
  - A requirement of P refers to Self (not an associated type thereof)
  in its signature, 
    + *except* when Self is the result type of the method in P and the
    corresponding witness for A's conformance to B is a DynamicSelf
    method.

Remove the uses of DynamicSelf from the literal protocols, going back
to Self. The fact that the conformances of NSDictionary, NSArray,
NSString, etc. to the corresponding literal protocols use witnesses
that return DynamicSelf makes NSMutableDictionary, NSMutableArray,
NSMutableString, and other subclasses still conform to the
protocol. We also correctly reject attempts to (for example) create an
NSDecimalNumber from a numeric literal, because NSNumber doesn't
provide a suitable factory method by which any subclass can be literal
convertible.



Swift SVN r14204
2014-02-21 07:48:28 +00:00
Joe Groff
68db63b45d AST: Have GenericFunctionType use GenericSignature.
Change GenericFunctionType to reference a GenericSignature instead of containing its generic parameters and requirements in-line, and clean up some interface type APIs that awkwardly returned ArrayRef pairs to instead return GenericSignatures instead.

Swift SVN r13807
2014-02-12 03:44:28 +00:00
Joe Groff
8cecd9fcf7 AST: ProtocolConformance::getInheritedConformance() should preserve specialization.
When applying getInheritedConformance to a specialized conformance, reapply the specialization to the found inherited conformance so we get a conformance for the same type we put in, making the specializer's job easier when finding conformances to insert into archetype_methods. To expose the problems this fixes, add a check in the SIL verifier that ArchetypeMethodInsts carry a ProtocolConformance that actually matches their lookup type.

Swift SVN r12988
2014-01-27 07:18:43 +00:00
Joe Groff
92f7b5512d AST: Add a "Substitution::subst" API.
Substitute a...um...substitution by remapping its Replacement mapping and recursively remapping any ProtocolConformances it has. Modify the Specializer to use Substitution::subst when specializing the substitutions of ArchetypeMethod and Apply instructions.

Swift SVN r12900
2014-01-24 04:35:49 +00:00
Doug Gregor
e9fdec95fe Put specialized and inherited conformances into an appropriate arena.
This is infrastructure toward allowing us to construct conformances
where there are type variables <rdar://problem/15168483>, which keeps
tripping up library work.

Swift SVN r12899
2014-01-24 04:13:17 +00:00
Nadav Rotem
270cba03fb Enable the specialization of the archetype_method instruction.
Swift SVN r12568
2014-01-20 05:37:35 +00:00
Chris Lattner
a0f0c28868 Two related but (theoretically at least) seperable changes:
1. Implement parser and sema support for our subscript syntax proposal in
   protocols.  Now you have to use subscript(..) { get } or  {get set} to 
   indicate what you want.  I suspect that the syntax will evolve, but at
   least we can express what we need now.
2. Change the representation of SubscriptDecls in protocols to make 
   (empty) funcdecls for the getter and setter.  This guarantees that 
   every subscript has at least a getter.



Swift SVN r12555
2014-01-19 01:35:13 +00:00