Commit Graph

103 Commits

Author SHA1 Message Date
Chris Lattner
fe9fe47b7e Implement support for generic typealiases. 2016-03-07 22:20:16 -08:00
Chris Lattner
868a795566 Introduce a new class between TypeDecl and NominalTypeDecl named GenericTypeDecl.
This factors the DeclContext and generic signature behavior out of NTD, allowing
it to be reused in the future.  NFC.
2016-03-04 23:09:15 -08:00
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
Slava Pestov
27da265abb Refactor some random usages of contextual types, NFC 2016-01-27 23:22:33 -08:00
Jordan Rose
de7678118f Dependencies: lookups in subscript signatures count as dependencies.
Fills in Chris's placeholder in feace85d5. I'm not quite sure why a private
subscript doesn't produce a non-cascading dependency right now, but this is
at least conservatively correct.

(For more infomation on dependencies, check out "Dependency Analysis.rst".)
2016-01-06 16:13:02 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Chris Lattner
feace85d5a Enhance SubscriptDecl to be a DeclContext, so it can hold its indices.
This is necessary for some other work I'm doing, which really wants
paramdecls to have reasonable declcontexts.  It is also a small step
towards generic subscripts.
2015-12-31 12:38:28 -08:00
Doug Gregor
7c0c092004 Eagerly update a protocol's "has missing required members" flag.
Rather than plumbing a "has missing required members" flag all the way
through the LazyResolver's loadAllMembers and its implementations,
just eagerly update the "has missing required members" flag in the
Clang importer when it happens. More NFC cleanup.
2015-12-23 13:38:35 -08:00
Slava Pestov
d6ea5d8717 Sema: Chain all generic parameter lists
Previously, methods on DeclContext for getting generic parameters
and signatures did not walk up from type contexts to function
contexts, or function contexts to function contexts.

Presumably this is because SIL doesn't completely support nested
generics yet, instead only handling these two special cases:

- non-generic local function inside generic function
- generic method inside generic type

For local functions nested inside generic functions, SIL expects
the closure to not have an interface type or generic signature,
even if the contextual type signature contains archetypes.
This should probably be revisited some day.

Recall that these cases are explicitly rejected by Sema diagnostics
because they lack SIL support:

- generic function inside generic function
- generic type inside generic function

After the previous patches in this series, it becomes possible to
construct types that are the same as before for the supported uses of
nested generics, while introducing a more self-consistent conceptual
model for the unsupported cases.

Some new tests show we generate diagnotics in various cases that
used to crash.

The conceptual model might still not be completely right, and of
course SIL, IRGen and runtime support is still missing.
2015-12-16 11:32:56 -08:00
Slava Pestov
74e575e638 Sema: Add DeclContext::getGenericTypeContextDepth()
Now that generic signatures of types include generic parameters
introduced by outer generic functions, we need to know to skip
them when forming bound generic types or substitutions.

Add a function that computes the depth of the innermost generic
context that is not a generic type context.
2015-12-15 22:59:38 -08:00
Slava Pestov
57dfb45ba6 Sema: Add DeclContext::isGenericTypeContext()
Once nested generic parameter lists are properly chained, we need a
way of checking if we're inside a generic type context that's
distinct from just checking if we have a generic type signature
available.

This distinguishes between these two cases:

class A<T> {
  // generic signature
  func method() -> T { // <T> A<T> -> () -> T
  }
}

func f<T>() {
  class A {
    // no generic signature
    func method() -> T { // A -> () -> T
    }
  }
}
2015-12-14 13:46:45 -08:00
Slava Pestov
dfbb580de3 AST: Store the GenericSignature in the AbstractFunctionDecl, NFC
For better or worse, the type of a function can end up as ErrorType,
and the generic signature was not stored anywhere else, causing
crashes from orphaned generic type parameters.

This patch is the first in a series to make this more robust by
storing the generic signature before the interface type is computed.
2015-12-14 13:29:52 -08:00
Xi Ge
71cb5e60e7 Add checks for erroneous extended type. rdar://22277406
Swift SVN r31235
2015-08-14 00:08:16 +00:00
Xi Ge
11600e1543 [CodeCompletion] Add checks for erroneous extended types to prevent crash. rdar://21436558
Swift SVN r30708
2015-07-28 00:02:41 +00:00
Xi Ge
705246bf11 Add checks for erroneous extended type to prevent crashes. rdar://21859941
Swift SVN r30326
2015-07-17 20:27:20 +00:00
Xi Ge
ae290e1b02 Add a missing null check. rdar://21790830
Swift SVN r30197
2015-07-14 21:17:34 +00:00
Doug Gregor
fab5e741bd Unrevert "Sema: Make derived conformances work from extensions"
Update IRGen test for 32/64-bit differences.

Swift SVN r28988
2015-05-24 17:55:42 +00:00
Ted Kremenek
a575727a2b Revert "Sema: Make derived conformances work from extensions"
Speculatively revert; this looks like it is breaking the iOS bots.

Swift SVN r28963
2015-05-23 15:26:55 +00:00
Slava Pestov
9388a955dc Sema: Make derived conformances work from extensions
This is more complex than it could be if ExtensionDecl and NominalTypeDecl
had a common ancestor in the Decl hierarchy, however this is not possible
right now because TypeDecl inherits from ValueDecl.

Fixes <rdar://problem/20981254>.

Swift SVN r28941
2015-05-23 01:21:10 +00:00
Slava Pestov
5899e62637 AST: Add dynamic cast from Decl to IterableDeclContext
Also allow cast<> from any Decl to DeclContext, not just ValueDecl.
An example of a Decl that is a DeclContext but not a ValueDecl is
ExtensionDecl.

The const_cast<> hack is horrid but its needed to deal with both const
and non-const operands in the patch that eventually uses this.

Note that this adds an explicit 'const auto' to the prior usage of
cast<DeclContext> in Mangling.cpp.

NFC

Progress on <rdar://problem/20981254>.

Swift SVN r28929
2015-05-22 20:28:16 +00:00
Doug Gregor
570e5acc09 Make extension-related DeclContext queries more robust.
Resolve extensions when we need them, but don't bother when we
don't. Among other things, this lets us identify a protocol extension
before it's been fully validated. No behavior change for anything we
have written now.

Swift SVN r28891
2015-05-21 22:40:21 +00:00
Joe Groff
ae1f3af9be AST: Add the ability to dyn_cast a ValueDecl to DeclContext.
Swift SVN r28341
2015-05-08 23:57:23 +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
Doug Gregor
abee3281e3 Basic partial ordering for members of protocol extensions.
Implement simplistic partial ordering rules for members of protocol
extensions. Specifically:
  - A member of a concrete type is more specialized than a member of a
  protocol extension
  - A member of a protocol extension of P1 is more specialized than a
  member of a protocol extension of P2 if P1 inherits from P2

This achieves most of what rdar://problem/20335936 covers, but does
not yet handle ordering between constrained protocol extensions.

Swift SVN r26723
2015-03-30 19:14:48 +00:00
Doug Gregor
3d77855b31 Start allowing extensions of protocol types.
Remove the semantic restrictions that prohibited extensions of
protocol types, and start making some systematic changes so that
protocol extensions start to make sense:
  - Replace a lot of occurrences of isa<ProtocolDecl> and
    dyn_cast<ProtocolDecl> on DeclContexts to use the new
    DeclContext::isProtocolOrProtocolExtensionContext(), where we want
    that behavior to apply equally to protocols and protocol extensions.
  - Eliminate ProtocolDecl::getSelf() in favor of
    DeclContext::getProtocolSelf(), which produces the appropriate
    generic type parameter for the 'Self' of a protocol or protocol
    extension. Update all of the callers of ProtocolDecl::getSelf()
    appropriately.
  - Update extension validation to appropriately form generic
    parameter lists for protocol extensions.
  - Methods in protocol extensions always use the witnesscc calling
  convention.

At this point, we can type check and SILGen very basic definitions of
protocol extensions with methods that can call protocol requirements,
generic free functions, and other methods within the same protocol
extension.

Regresses four compiler crashers but improves three compiler
crashers... we'll call that "progress"; the four regressions all hit
the same assertion in the constraint system that will likely be
addressed as protocol extensions starts working.

Swift SVN r26579
2015-03-26 04:50:51 +00:00
Jordan Rose
f74bc7122c Split getAccessibility() into getFormalAccess() and getEffectiveAccess().
Currently a no-op, but effective access for entities within the current
module will soon need to take testability into account. This declaration:

  internal func foo() {}

has a formal access of 'internal', but an effective access of 'public' if
we're in a testable mode.

Part of rdar://problem/17732115 (testability)

Swift SVN r26472
2015-03-24 02:16:58 +00:00
Doug Gregor
479660b54b When !NDEBUG, track the contexts of archetype types.
Helps with debugging when archetypes get tangled together. NFC

Swift SVN r26439
2015-03-23 19:00:51 +00:00
Doug Gregor
c76fd52d63 Don't crash when dumping a deserialized extension context.
Swift SVN r26418
2015-03-22 12:35:29 +00:00
Xi Ge
520df97787 [CodeCompletion] Adopt a more efficient algorithm
to find the nearest AST parent that meets a certain condition

Swift SVN r26134
2015-03-14 08:30:16 +00:00
Xi Ge
8367428ed0 [CodeCompletion] Literal complete.
When code completing a literal expr, it is likely that code completion engine only collects the expr
that is not fully type checked. Therefore, no members of the literal can be suggested. To address this,
we try to climb up expr hierarchy in AST to find an expr with a nominal type, and use the nominal type
to finish code completion.
rdar://20059173

Swift SVN r26116
2015-03-14 00:34:32 +00:00
Doug Gregor
0242f5af18 Add DeclContext::isNominalTypeOrNominalTypeExtensionContext(). NFC
Swift SVN r26065
2015-03-12 21:11:09 +00:00
Doug Gregor
bce5c20c25 Teach loadAllMembers() implementations to add the members themselves.
The contract for LazyResolver::loadAllMembers() was that the caller
would handle actually adding the members, since it was an iterable
declaration context and could centralize that (simple) logic. However,
this fails in the Clang importer in rare but amusing ways when some of
the deferred actions (e.g., finishing a protocol conformance) depend
on having the members already set. The deferred action occurs after
the member list is complete in ClangImporter's loadAllMembers(), but
before its caller actual set the member list, leaving incomplete
conformances. Fixes rdar://problem/18884272.

Swift SVN r25630
2015-02-28 01:03:41 +00:00
Devin Coughlin
7ea8aa82ea [Sema] Validate declaration version range availability within lexical scope
This commit validates @availability() attribute version ranges to ensure that
a declaration is not more available than its lexically containing scope. To do so,
we find the inner-most declaration containing an @availability attribute that itself
has an @availability attribute and check that first attribute's available
version range is contained in the enclosing declaration's available range. If not,
we emit a diagnostic.

This commit removes a FIXME for checking @availability and overrides. It appears that
the FIXME is a copy/paste to/from AttributeOverrideChecker, where it still resides.

Swift SVN r25453
2015-02-21 01:34:38 +00:00
David Farler
51f8070abe Serialize local types
Local type declarations are saved in the source file during parsing,
now serialized as decls. Some of these may be defined in DeclContexts
which aren't Decls and previously weren't serialized. Create four new
record kinds:

* PatternBindingInitializer
* DefaultArgumentInitializer
* AbstractClosureExpr
* TopLevelCodeDecl

These new records are used to only preserve enough information for
remangling in the debugger, and parental context relationships.

Finally, provide a lookup API in the module to search by mangled name.
With the new remangling API, the debugging lifecycle for local types
should be complete.

The extra LOCAL_CONTEXT record will compressed back down in a
subsequent patch.

Swift SVN r24739
2015-01-27 01:49:54 +00:00
David Farler
cad9f99929 Revert "Serialize local types and provide a lookup API"
Changing the design of this to maintain more local context
information and changing the lookup API.

This reverts commit 4f2ff1819064dc61c20e31c7c308ae6b3e6615d0.

Swift SVN r24432
2015-01-15 00:33:10 +00:00
David Farler
fab3d491d9 Serialize local types and provide a lookup API
rdar://problem/18295292

Locally scoped type declarations were previously not serialized into the
module, which meant that the debugger couldn't reason about the
structure of instances of those types.

Introduce a new mangling for local types:
[file basename MD5][counter][identifier]
This allows the demangle node's data to be used directly for lookup
without having to backtrack in the debugger.

Local decls are now serialized into a LOCAL_TYPE_DECLS table in the
module, which acts as the backing hash table for looking up
[file basename MD5][counter][identifier] -> DeclID mappings.

New tests:
* swift-ide-test mode for testing the demangle/lookup/mangle lifecycle
of a module that contains local decls
* mangling
* module merging with local decls

Swift SVN r24426
2015-01-14 22:08:47 +00:00
Jordan Rose
9ddd23c0ff Invert DeclContext::is[Non]CascadingContextForLookup.
...and a few other things.

Attempting to remove a few negations to minimize confusion.
No intended functionality change.

Swift SVN r23970
2014-12-17 02:42:48 +00:00
Jordan Rose
99075516ce Use "cascading/non-" terms for dependencies instead of "private/non-".
"private" is a very overloaded term already. "Cascading" instead of
"non-private" is a bit more clear about what will happen with this sort
of lookup.

No functionality change. There are some double negatives I plan to clean
up in the next commit, but this one was supposed to be very mechanical.

Swift SVN r23969
2014-12-17 02:42:45 +00:00
Jordan Rose
5033d915a3 Dependencies: Private functions do not affect downstream files.
This adds a check to isPrivateContextForLookup, and also changes Sema to
use a function itself as the lookup context for non-generic functions'
result types (like generic functions already do). It also moves
isPrivateContextForLookup onto DeclContext itself, to be used in the next
commits.

Swift SVN r23633
2014-12-03 02:56:00 +00:00
Argyrios Kyrtzidis
b11d990818 Avoid APIs that return a new ASTContext-allocated array every time you call them.
Have them fill out a vector provided by the caller instead.

It is very easy to have callers just go through the array, thus wasting memory, as
the clang importer ended up doing.
The caller should be the one deciding if the array must be copied in ASTContext or not.

Swift SVN r23472
2014-11-20 06:33:27 +00:00
Doug Gregor
caca3ea2d1 Include outer generic parameters in the generic signatures for nominal types.
Generic function signatures were including outer generic parameters,
but generic type signatures were not. This is a small part of the
problem with nested generics (in general), but also a useful cleanup
for generic signatures.

Swift SVN r23011
2014-10-29 22:27:38 +00:00
Dmitri Hrybenko
8fdd6aca87 Fix warnings about falling off the end of a function without a return
Swift SVN r22317
2014-09-27 23:34:22 +00:00
John McCall
667eae6c1f Clarify that the new declaration is placed after the
addMember hint and fix the splicing logic.

Fortunately, this wasn't extensively used.

Swift SVN r22013
2014-09-17 08:08:06 +00:00
John McCall
fab74f5dbc When dumping DeclContext chains, be more specific than
just "NominalTypeDecl".

Swift SVN r22012
2014-09-17 08:08:05 +00:00
Doug Gregor
ac332429d9 Give extensions generic parameters and signatures within the AST.
This adds generic parameters and generic signatures to extension
declarations. The actual generic parameters just mirror what is
available on the extended type; however, it is filled in via extension
validation, which is handled lazily.

This is a NFC step toward decoupling the archetypes of extensions from
the archetypes of the extended types <rdar://problem/16974298>.

Swift SVN r20675
2014-07-29 16:20:56 +00:00
Chris Lattner
f693c5c83f fix rdar://17405715 - lazy property crashes silgen of implicit memberwise initializer
The issue here is that SILGen is expecting the argument pattern for the
synthesized ConstructorDecl to line up with the stored property list for the
decl containing the constructor.  This is super fragile and probably the wrong
way to go (it would be better for sema to synthesize the entire ctor and make it
not be magical at all in silgen), but do a small fix to improve the situation
by inserting the lazy property storage next to the computed lazy property.



Swift SVN r19642
2014-07-07 22:25:52 +00:00
Jordan Rose
db3914e94b Disallow conforming to ObjC protocols with requirements that can't be imported.
For example, variadic ObjC method requirements.

<rdar://problem/17366999>

Swift SVN r19121
2014-06-24 01:23:06 +00:00
Chris Lattner
7d6740cba7 fix rdar://16805609 - <rdar://problem/16805609> Providing a 'didSet' in a generic override doesn't work
The functional difference is:

 ClassDecl *DeclContext::isClassOrClassExtensionContext() const {
-  if (auto ctx = getDeclaredTypeOfContext())
+  if (auto ctx = getDeclaredTypeInContext())

As expected, I didn't get the sense of Of/In right, manifesting in a nasty bug.



Swift SVN r18216
2014-05-16 22:05:12 +00:00
Chris Lattner
ab7725aaae introduce a new DeclContext::isClassOrClassExtensionContext helper method,
which will consolidate a bunch of places that this is used.

I'd appreciate review on this, because I can never keep getDeclaredTypeOfContext
vs getDeclaredTypeInContext straight.


Swift SVN r17891
2014-05-11 22:10:19 +00:00
Joe Groff
05c80a8fe9 AST: Avoid instantiating all members when implicit destructors are inserted into classes.
We were accidentally forcing all members of a class to be instantiated in two places:

- by trying to look up an existing destructor decl in the class, and
- by adding the implicit destructor to the class, because addMember needlessly called loadAllMembers.

Fix the former problem by adding a 'has destructor' bit to ClassDecl so we can track whether the implicit destructor needs to be added without querying its members. Fix the latter by making IterableDeclContext::addMember not call loadAllMembers, and making loadAllMembers not barf when it sees existing members in the context.

Together with Jordan and JoeP's changes, this makes many interpreter tests now compile 3-20x faster.

Swift SVN r17562
2014-05-06 20:30:08 +00:00