Commit Graph

3502 Commits

Author SHA1 Message Date
Doug Gregor
40a813bc8d [GenericSigBuilder] Sink RequirementSource into GenericSignatureBuilder. 2017-02-28 09:41:23 -08:00
Doug Gregor
5c2fe3496f Merge pull request #7740 from huonw/parse-assoc-type-where
(Mostly) Type-check where clauses on associated types
2017-02-27 22:06:51 -08:00
Doug Gregor
fafb244ee4 [AST] Add ProtocolDecl::walkInheritedProtocols.
Use this in ProtocolDecl::requiresClassSlow(), and hope its presence
discourages more potentially-infinitely-recursive walks of the
inherited-protocols lists.
2017-02-27 15:57:18 -08:00
Doug Gregor
09a5807c30 [AST] Teach ProtocolDecl::requiresClassSlow() to tolerate recursive protocol inheritance.
Recursive protocol inheritance is ill-formed, but we should
nonetheless tolerate it within the AST rather than infinitely recursing.
2017-02-27 15:24:01 -08:00
Huon Wilson
644f6df424 [AST] getInterfaceType never returns null, so guard it.
There's no point testing for null on the next line after just calling
getInterfaceType(), because that function asserts that the interface
type exists.
2017-02-24 22:00:59 -08:00
Huon Wilson
5e1bb40298 [AST] Compute the (trivial) generic signature of a protocol on demand. 2017-02-24 19:24:13 -08:00
Huon Wilson
84e0a109a2 [AST] Explicitly track the validation state of Decls.
Previously some decls (TypeAliasDecl and ExtensionDecl) had bits
explicitly marking whether they've been validated, while other decls
just deduced this from hasInterfaceType. The doing the latter doesn't
work when the interface type can be computed before doing full
validation (such as protocols and associatedtypes, which have trivial
interface types), and so an explicit bit is adopted for all decls.
2017-02-24 19:21:33 -08:00
Huon Wilson
8423018057 [Parse] Parse trailing where clauses on associated types. 2017-02-24 19:21:32 -08:00
Slava Pestov
fdb0a18647 AST: Add a GenericParamList::clone() method
This will be immediately needed for generic subscripts, and also
for fixing the remaining cases where initializer inheritance
doesn't work.
2017-02-23 20:57:15 -08:00
Slava Pestov
ecda075792 AST: Fix signature of GenericParamList::create() 2017-02-23 20:57:14 -08:00
Xi Ge
2e447dc812 NameTranslation: ensure that preferred names are preferred to implicit names in @objc(). rdar://30658589 (#7692)
Implicit names in @objc() are essentially cached auto-translated Swift names. This patch ensures that we don't mistake them for explicitly specified objc selectors.
2017-02-22 14:48:19 -08:00
Nathan Hawes
444775f58a Merge pull request #7670 from nathawes/rdar16271632
Use clang-style USRs for swift decls that are exposed to Objective C
2017-02-21 17:00:28 -08:00
Nathan Hawes
0dc0985f45 Move SwiftNameTranslation implementation out of Decl.cpp and into its own file.
Also fix code formatting issues and simplify the code in USRGeneration.cpp based on review comments in PR #7670.
2017-02-21 14:54:31 -08:00
Nathan Hawes
c9758c2c0f Use clang-style USRs for swift decls that are exposed to Objective C 2017-02-21 09:42:33 -08:00
Slava Pestov
a79fd9eff1 Merge pull request #7639 from slavapestov/sourcekit-indexing-protocol-typealias
SourceKit: Fix indexing crash with protocol typealiases
2017-02-20 12:07:37 -08:00
Doug Gregor
042e6510c3 [AST] Drop ProtocolDecl's "inherited protocols" list.
The list of directly inherited protocols of a ProtocolDecl is already
encoded in the requirement signature, as conformance constraints where
the subject is Self. Gather the list from there rather than separately
computing/storing the list of "inherited protocols".
2017-02-20 09:41:00 -08:00
Slava Pestov
0bc802e7ad SourceKit: Fix indexing crash with protocol typealiases
The root cause is that NormalProtocolConformance::forEachValueWitness()
needs to skip protocol members that are not requirements.

Otherwise we end up passing such a non-requirement member down to
NormalProtocolConformance::getWitness() and hit an assert when we
cannot find it.

It looks like this code path was only ever hit from SourceKit.
The fix moves TypeChecker::isRequirement() to a method on ValueDecl,
and calls it in the right places.

Fixes <https://bugs.swift.org/browse/SR-3815>.
2017-02-20 03:35:19 -08:00
Slava Pestov
b319a3aa32 AST: Clean up some more duplication using the new GenericContext 2017-02-19 21:00:00 -08:00
Slava Pestov
ad78604e32 AST: Introduce new GenericContext base class
This is in preparation for generic subscripts, which will also
expose methods like getGenericSignature(), and so on.

ExtensionDecl, GenericTypeDecl and AbstractFunctionDecl all share
code. Instead of copy and pasting it yet again into SubscriptDecl,
factor it out into a common base class.

There are more yaks to shave here, but this is a step in the right
direction.
2017-02-18 01:49:32 -08:00
Xi Ge
03a17e1f06 Cleanup: Move Swift to ObjC name translation API to libAST to allow larger audience body. NFC (#7586) 2017-02-17 22:09:25 -08:00
David Farler
3645736ac0 Merge pull request #7393 from bitjammer/syntax-tree
Start the Syntax structured editing library
2017-02-17 15:26:00 -08:00
Doug Gregor
da39d9b17b [GenericSig Builder] Rework RequirementSource to describe requirement path.
Reimplement the RequirementSource class, which captures how
a particular requirement is satisfied by a generic signature. The
primary goal of this rework is to keep the complete path one follows
in a generic signature to get from some explicit requirement in the
generic signature to some derived requirement or type, e.g.,

1) Start at an explicit requirement "C: Collection"
2) Go to the inherited protocol Sequence,
3) Get the "Iterator" associated type
4) Get its conformance to "IteratorProtocol"
5) Get the "Element" associated type

We don't currently capture all of the information we want in the path,
but the basic structure is there, and should also allow us to capture
more source-location information, find the "optimal" path, etc. There are
are a number of potential uses:

* IRGen could eventually use this to dig out the witness tables and
  type metadata it needs, instead of using its own fulfillment
  strategy
* SubstitutionMap could use this to lookup conformances, rather than
  it's egregious hacks
* The canonical generic signature builder could use this to lookup
  conformances as needed, e.g., for the recursive-conformances case.

... and probably more simplifications, once we get this right.
2017-02-17 13:50:51 -08:00
David Farler
431b7ff2af [Syntax] Add Equal '=' token location to TypeAliasDecl
Needed for full-fidelity structured editing.
2017-02-17 12:57:04 -08:00
Erik Eckstein
8a6a9461d3 Only mangle non-private top-level classes and protocol with the old-style for the ObjC metadata.
Only those types can be de-mangled by the ObjC runtime anyway.
Also move this mangling logic into the ASTMangler class. This avoids keeping the old mangler around just for that purpose.
2017-02-16 18:20:05 -08:00
Slava Pestov
c2d07bc210 AST: Cache the result of ClassDecl::checkObjCAncestry() 2017-02-14 22:35:27 -08:00
Xi Ge
055da1fbfb [SourceKit] Teach name translation request to translate Swift names to ObjC ones (by using PrintAsObjC). (#7449) 2017-02-14 14:25:52 -08:00
practicalswift
65b0219f7b [gardening] Fix typos 2017-02-14 20:04:08 +01:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
Doug Gregor
579af863c5 Rename ArchetypeBuilder -> GenericSignatureBuilder 2017-02-10 12:46:34 -08:00
Huon Wilson
74091fbcef [AST] Store and serialize a protocol's requirement signature.
This is the "canonical" representation of the type-level requirements of
a protocol, and we intend to use it pervasively in the compiler.
2017-02-08 13:08:31 -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
swift-ci
ec274a1bbd Merge pull request #7298 from DougGregor/early-recursive-constraints 2017-02-06 23:28:51 -08:00
Doug Gregor
def14bfb70 [Archetype builder] Move checking for recursive constraints to finalize().
Rather than waiting until we try to form a contextual type to diagnose
recursion, perform the check while finalizing the archetype builder
itself.
2017-02-06 22:58:44 -08:00
Jordan Rose
898940c2dd Extensions of imported classes never provide overriding initializers. (#7284)
This addresses a crash where the compiler asks if an imported class
inherits initializers from its superclass /during the SIL passes/. In
this particular arrangement of subclasses and initializers (see test
case), this leads to us importing members of an Objective-C class for
the first time well after we've destroyed the type checker, and then
checking to see if an initializer added in a Swift extension can
prevent initializer inheritance. That initializer hasn't been
type-checked (because it's in another file and isn't supposed to
affect anything), and so the compiler chokes. A spot fix would merely
check for 'resolver' here and skip over the initializer if it doesn't
have a type, but it's not clear what the right semantics are in that
case.

The real issue here is that we don't support importing new declarations
after the type checker has been torn down, and that keeps causing us
problems, but that's a much bigger thing to fix.

https://bugs.swift.org/browse/SR-3853
2017-02-06 16:26:13 -08:00
Doug Gregor
faf659d16b [Archetype builder] Make sure the archetype builder is always "finalized".
ArchetypeBuilder::finalize() is needed to tie up any loose ends before
requesting a generic signature or generic environment. Make sure it
gets called consistently.
2017-02-05 21:23:44 -08:00
Slava Pestov
dca292c652 Serialization: Don't serialize contextual enum argument type
Storing this separately is unnecessary since we already
serialize the enum element's interface type. Also, this
eliminates one of the few remaining cases where we serialize
archetypes during AST serialization.
2017-01-30 00:08:53 -08:00
Huon Wilson
a964b6ad78 [AST] Introduce the notion of a protocol requirement signature.
This is a generic signature that stores exactly the requirements that a
protocol decl introduces, not letting them be implied by the Self :
Protocol requirement, nor storing any requirements introduced by the
protocols requirements.

Specifically, suppose we have

    protocol Foo {}
    protocol Bar {}

    protocol Baz {
        associatedtype X : Foo
    }
    protocol Qux: Baz {
        associatedtype X : Bar
    }

The normal generic signature and (canonical) protocol requirement
signature of `Baz` will be, respectively

    <Self where Self : Baz>
    <Self where Self : Baz, Self.X : Foo>

And for `Qux`, they will be:

    <Self where Self : Qux>
    <Self where Self : Qux, Self : Baz, Self.X : Bar>

Note that the `Self.X : Foo` requirement is not listed.

For the moment, this is unused except for `-debug-generic-signatures`.
2017-01-25 16:06:50 -08:00
Slava Pestov
c86b5ae427 AST: Header file gardening - include what you use 2017-01-19 20:07:06 -08:00
Slava Pestov
be0e2d77cf Merge pull request #6245 from KingOfBrian/bugfix/SR-3401
Remove the `mutating` fixit for getter subscript
2017-01-10 22:55:49 -08:00
Slava Pestov
5eb16e6b34 Include-what-you-use: Initializer.h should not be pulled in from AST.h 2017-01-09 16:46:31 -08:00
Hugh Bellamy
7a5ef4bdd1 Support building swift/AST with MSVC on Windows 2017-01-09 09:05:06 +00:00
Brian Gesiak
663b92ece9 [AST] Completely replace Module with ModuleDecl
The typedef `swift::Module` was a temporary solution that allowed
`swift::Module` to be renamed to `swift::ModuleDecl` without requiring
every single callsite to be modified.

Modify all the callsites, and get rid of the typedef.
2017-01-08 00:36:08 -05:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Robert Widmann
96f2a04f55 Merge pull request #6490 from modocache/ast
[SR-2757][Sema] Mark VarDecl in capture lists
2017-01-05 21:36:16 -07:00
Slava Pestov
2555135bbd AST: Remove DeclContext::isValidGenericContext()
This is intended to have no functional effect, but there was a
minor change to a diagnostic in invalid code in the tests for the
unfinished ASTScope code; I hope I didn't break anything more
fundamental there.
2017-01-04 01:08:28 -08:00
Slava Pestov
0a7e1d5568 Sema: Fix crash in conformance checking
Yeah, the repro is really just "class A : RangeReplaceableCollection { }".
2017-01-04 01:27:41 -07:00
Slava Pestov
e4abdfc2bd AST: Model protocols nested inside other types
We might allow protocols inside non-generic class/struct/enum
declarations eventually; there's no conceptual difficulty, just
some IRGen and Serialization work that has to happen first.

Also, this fixes a crasher :-)
2017-01-04 00:10:29 -08:00
Slava Pestov
a230bde7e7 AST: Clean up getDeclaredType() and friends 2017-01-03 23:40:47 -08:00
Slava Pestov
2932aac03a AST: More accurate AbstractFunctionDecl::getImplicitSelfDecl()
Now, the following are both true or both false:

- AFD->getDeclContext()->isTypeContext()
- AFD->getImplicitSelfDecl() != nullptr

Also, if var->isSelfParameter() returns true, then it is also
the case that var == var->getDeclContext()->getImplicitSelfDecl().
2017-01-03 23:40:45 -08:00
Slava Pestov
9db06ee36d Sema: Fixes for handling of 'Self'-returning methods
Protocol methods returning optionals, metatypes and functions
returning 'Self' are now handled correctly in Sema when
accessed with a base of existential type, eg:

protocol Clonable {
  func maybeClone() -> Self?
  func cloneMetatype() -> Self.Type
  func getClonerFunc() -> () -> Self
}

let c: Clonable = ...
let _: Clonable = c.maybeClone()
let _: Clonable.Type = c.cloneMetatype()

Previously, we were unable to model this properly, for
several reasons:

- When opening the function type, we replaced the return
  value in openedFullType _unconditionally_ with the
  existential type. This was broken if the return type
  was not the 'Self' parameter, but rather an optional,
  metatype or function type involving the 'Self' parameter.

- It was also broken because we lost information; if the
  return type originally contained an existential, we had
  no way to draw the distinction. The 'hasArchetypeSelf()'
  method was a hint that the original type contained a
  type parameter, but it was inaccurate in some subtle cases.

- Since we performed this replacement on both the
  'openedFullType' and 'openedType', CSApply had to "undo"
  it to replace the existential type with the opened
  existential archetype. This is because while the formal
  type of the access returns the existential type, in
  reality it returns the opened type, and CSApply has to
  insert the correct ErasureExpr.

  This was also done unconditionally, but even if it were
  done more carefully, it would do the wrong thing because
  of the 'loss of information' above.

- There was something fishy going on when we were dealing
  with a constructor that was declared on the Optional type
  itself, as seen in the fixed type_checker_crasher.

- TypeBase::eraseOpenedExistential() would transform a
  MetatypeType of an opened existential into a MetatypeType
  of an existential, rather than an ExistentialMetatypeType
  of the existential type.

The new logic has the following improvements:

- When opening a function type, we replace the 'Self' type
  parameter with the existential type in 'openedType', but
  *not* 'openedFullType'. This simplifies CSApply, since it
  doesn't have to "undo" this transformation when figuring
  out what coercions to perform.

- We now only use replaceCovariantResultType() when working
  with Self-returning methods on *classes*, which have more
  severe restrictions than protocols. For protocols, we walk
  the type using Type::transform() and handle all the cases
  properly.

- Not really related to the above, but there was a whole
  pile of dead code here concerning associated type references.

Note that SILGen still needs support for function conversions
involving an existential erasure; in the above Clonable example,
Sema now models this properly but SILGen still crashes:

let _: () -> Clonable = c.getClonerFunc()

Fixes <rdar://problem/28048391>, progress on <rdar://problem/21391055>.
2017-01-03 21:49:00 -08:00