Commit Graph

284 Commits

Author SHA1 Message Date
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
Dmitri Hrybenko
35c587aadf Code completion: don't complete associated types on non-metatypes
Swift SVN r16817
2014-04-25 12:46:32 +00:00
Doug Gregor
292dc76420 Remove debugging dumps.
Swift SVN r16574
2014-04-20 01:06:15 +00:00
Doug Gregor
6b3ef547ec Replace "Members" arrays with an intrusive linked list.
The use of ASTContext-allocated arrays to store the members of nominal
type declarations and the extensions thereof is an
abomination. Instead, introduce the notion of an "iterable"
declaration context, which keeps track of the declarations within that
context (stored as a singly-linked list) and allows iteration over
them. When a member is added, it will also make sure that the member
goes into the lookup table for its context immediately.

This eliminates a ton of wasted memory when we have to reallocate the
members arrays for types and extensions, and moves us toward a much
more sane model. The only functionality change here is that the Clang
importer no longer puts subscript declarations into the wrong class,
nor does it nested a C struct within another C struct.



Swift SVN r16572
2014-04-19 23:37:06 +00:00
Chris Lattner
33eb263a07 use isa instead of dyncast in a boolean context, NFC.
Swift SVN r15950
2014-04-04 18:57:57 +00:00
Jordan Rose
aa6aa72ba1 Add lazy conformances to nominals and extensions.
...and deserialize conformances lazily.

No intended user-visible change.

Swift SVN r15911
2014-04-04 01:17:54 +00:00
Joe Pamer
31bab38eb4 When attempting to obtain the context type of a type extension, we were never taking into account the fact that the extended type could be potentially be unbound.
(This could occur, for instance, via a forward reference to a member defined in an extension to a generic type.)  This problem has been popping up a lot lately, and
was making some of the recent runtime work difficult.  (rdar://problem/16481483)

Swift SVN r15805
2014-04-02 16:48:14 +00:00
Dmitri Hrybenko
0168a70d09 dumpContext(): correctly dump AbstractFunctionDecl context if the type is not
set yet


Swift SVN r15452
2014-03-25 14:41:40 +00:00
Joe Groff
8e6b353542 Derive conformances of Equatable and Hashable for simple enums.
If an enum has no cases with payloads, make it implicitly Equatable and Hashable, and derive default implementations of '==' and 'hashValue'. Insert the derived '==' into module context wrapped in a new DerivedFileUnit kind, and arrange for it to be codegenned with the deriving EnumDecl by adding a 'DerivedOperatorDecls' array to NominalTypeDecls that gets visited at SILGen time.

Swift SVN r14471
2014-02-27 20:28:38 +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
Chris Lattner
0a88809fb6 now that there is only one caller of getSelfTypeInContext and
getInterfaceSelfType, inline them into their caller.  This
has the nice effect of moving getSelfTypeForContainer into
Decl.cpp instead of being in DeclContext.cpp (which never
made sense to me).


Swift SVN r12984
2014-01-27 06:32:21 +00:00
John McCall
82e192df6e Optimize some generics-related queries on DeclContexts.
Swift SVN r12941
2014-01-24 22:46:21 +00:00
Doug Gregor
88355dd322 Base the implicit 'self' parameter type on DynamicSelf when appropriate.
To get here, make the implicit 'self' parameter a bit more like a
parsed parameter, by giving it a trivial TypeRepr so that pattern
validation will validate the type. This latter piece is important for
DynamicSelf, because we need the interface type of the function to
refer to the implicit generic parameter while the actual type refers
to the archetype.



Swift SVN r12757
2014-01-22 18:36:38 +00:00
Doug Gregor
56b60d9726 Factor "self" type computation into one place within libAST.
Oh, and start creating the implicit generic parameter for
DynamicResult functions.




Swift SVN r12744
2014-01-22 16:03:26 +00:00
Joe Groff
0776c4b6b8 SIL: Reorient function type lowering toward interface types.
Lower types for SILDeclRefs from the interface types of their referents, dragging the old type along for the ride so we can still offer the context to clients that haven't been weaned off of it. Make SILFunctionType's interface types and generic signature independent arguments of its  Derive the context types of SILFunctionType from the interface types, instead of the other way around. Do a bunch of annoying inseparable work in the AST and IRGen to accommodate the switchover.

Swift SVN r12536
2014-01-18 19:42:02 +00:00
Joe Groff
70de0802e9 AST: Make the @inout-ness of interface types for protocol members consistent with their old types.
There are two places that need to be fixed:

- getSelfTypeForContainer needs to check for a ProtocolDecl context instead of assuming ArchetypeType self == protocol method. Archetypes don't appear in interface types.
- semaFuncDecl's computeSelfType helper needs to have the same special case for protocol contexts as getSelfTypeForContainer.

This will hopefully be tested soon when I can land my next round of SIL interface type changes.

This exposes a problem with Chris's approach to handling the Self of @mutating protocol methods--the fact that they're still @inout manifests to the user if they try to use an uncurried instance method of a generic or existential type. This isn't on the critical path so leave it as <rdar://problem/15821762> for now.

Swift SVN r12312
2014-01-15 04:17:42 +00:00
Chris Lattner
132aca7a46 finish a thought.
Swift SVN r12118
2014-01-10 05:54:53 +00:00
Chris Lattner
96856f2f8e For a complicated set of reasons, protocol methods will be modeled
as taking the "self" receiver as @inout in the requirement funcdecls,
whether they are @mutating or not.  This will allow us to generate better
code, eliminate MaterializeExpr, and have immutable arguments someday.

As such, I think I'm done hacking on this function.  Clean it up and document
its behavior, NFC.


Swift SVN r12117
2014-01-10 05:43:50 +00:00
Doug Gregor
fc00bf6076 Parsing and semantic analysis for delegating initializers.
Swift SVN r11921
2014-01-06 16:33:08 +00:00
John McCall
4355a21a62 Mangle closures using their context and discriminator.
Swift SVN r11885
2014-01-04 04:37:57 +00:00