Commit Graph

1002 Commits

Author SHA1 Message Date
Doug Gregor
04d157427d Make sure that the dynamic lookup table has the right set of members.
Clean up the "can be accessed by dynamic lookup" predicate so that it
checks for a generic context. We no longer need to do the checking in
name lookup, since this means that the dynamic lookup table won't have
anything we can't use (thanks Jordan!). Drop the [objc] allowance for
members of a generic context: it still leaves us with a very weird
case of messaging something when we can't even figure out the type
that we think the object has.

Extend the walk searching for members to include inner classes within
structs, so we get all of the members globally.



Swift SVN r7799
2013-08-30 20:43:34 +00:00
Jordan Rose
a09343e519 Lock down on exports from TranslationUnit modules.
Previously, export control (via [exported]) only applied to modules that
had been serialized -- anything imported in a TranslationUnit was
automatically considered exported. This was done to make life easier when
dealing with the main source file, but it turns out we're going to want to
load other source files as imports, and export control should work there too.

Now, the module iteration methods (Module::forAllVisibleModules,
namelookup::lookupInModule, etc.) can consider the module being passed as
"top-level", meaning its private imports are visible as well. Otherwise,
proper export control is observed even for imported TranslationUnits.

This required a number of test changes involving Swift adapter modules that
forgot to re-export their Clang modules.

Swift SVN r7783
2013-08-30 17:05:32 +00:00
Doug Gregor
91010d102e Dynamic lookup can never find a member of a generic class.
If we allowed dynamic lookup to find a member of a generic class, we
would then have to figure out the generic arguments. Technically, it's
possible that we could deduce the generic arguments from the signature
of the function we found... but it feels unlikely that we'd get the
right answer often enough to make it useful. So don't do that.



Swift SVN r7758
2013-08-29 22:38:53 +00:00
Doug Gregor
9efa20e4eb Improve overload resolution for dynamic lookups.
Unlike normal overload resolution, where we always want the
most-specialized, overriding result, overload resolution for dynamic
lookups favors results in superclasses to those in subclasses, because
we want to put the least requirements on the object
type. Additionally, we don't allow overload resolution to select among
results that come from different classes or protocols.



Swift SVN r7743
2013-08-29 21:07:07 +00:00
Doug Gregor
b06e65c3b3 Add the DynamicLookup protocol for lookup across all classes and protocols.
When performing member lookup into an existential that involves the
DynamicLookup protocol, look into all classes and protocols for that
member. References to anything found via this lookup mechanism are
returned as instances of Optional.

This introduces the basic lookup mechanics into the type
checker. There are still numerous issues to work through:
  - Subscripting isn't supported yet
  - There's no SILGen or IRGen support
  - The ASTs probably aren't good enough for the above anyway
  - References to generics will be broken
  - Ambiguity resolution or non-resolution

Thanks to Jordan for the patch wiring up DynamicLookup.


Swift SVN r7689
2013-08-28 21:38:50 +00:00
Dmitri Hrybenko
69cfa73640 More 'this' -> 'self' replacements
Swift SVN r7657
2013-08-28 02:57:21 +00:00
Dmitri Hrybenko
ca1daac1d0 removeShadowedDecls(): simplify the way we get the type
Apparently, this code was written this way because getCanonicalType() on a
TypeAliasDecl which represented generic parameter used to assert().


Swift SVN r7447
2013-08-22 01:20:15 +00:00
Doug Gregor
e7f5f3da01 Set the type of class/struct/union declarations during type checking.
Previously, we were creating the type corresponding to
class/struct/union declarations as part of creating the declaration
node, which happens at parse time. The main problem with this (at the
moment) occurs with nested nominal types, where we'd end up with the
wrong "parent" type when the type was nested inside an extension
(because the extension hadn't been resolved at the time we accessed
the parent's type). Amusingly, only code completion observed this,
because the canonical type system hid the problem. The churn in the
code-completion tests come from the fact that we now have the proper
declared type for class/struct/union declarations within extensions.

Take a step toward order-independent type checking by setting the type
of a class/struct/union declaration in type checking when we either
find the declaration (e.g., due to name lookup) or walk to the
declaration (in our walk of the whole translation unit to type-check
it), extending the existing TypeChecker::validateTypeDecl() entry
point and adding a few more callers.

The removeShadowedDecls() hack is awful; this needs to move out to the
callers, which should be abstracted better in the type checker anyway.

Incremental, non-obvious step toward fixing the representation of
polymorphic function types. This yak has a *lot* of hair.



Swift SVN r7444
2013-08-22 00:57:38 +00:00
Doug Gregor
1ddb34fb71 Factor generic parameters and associated types into their own decl nodes.
Previously, TypeAliasDecl was used for typealiases, generic
parameters, and assocaited types, which is hideous and the source of
much confusion. Factor the latter two out into their own decl nodes,
with a common abstract base for "type parameters", and push these
nodes throughout the frontend.

No real functionality change, but this is a step toward uniquing
polymorphic types, among other things.


Swift SVN r7345
2013-08-19 23:36:58 +00:00
Jordan Rose
5486dd4dba Module::forAllVisibleModules never needs to capture its callback.
...so, add a templated overload that ensures that lambdas aren't copied,
so that callers don't need to add an explicit makeStackLambda().

No functionality change.

Swift SVN r7303
2013-08-16 23:32:43 +00:00
Dmitri Hrybenko
67d82f6ca8 Fix 80-cols violation
Swift SVN r7212
2013-08-13 22:53:36 +00:00
Jordan Rose
c4990cfc25 Perform proper shadowing when looking for module-level visible decls.
This makes lookupVisibleDecls use the same code path as qualified and
unqualified lookup. This is more expensive, because it needs to actually
collect a list of /all/ decls in order to properly compute shadowing
information, but this list is not likely to change once the set of imports
has been computed, so we can cache it.

Swift SVN r7168
2013-08-12 18:54:53 +00:00
Dmitri Hrybenko
de59d8dcd4 Remove unneeded llvm:: qualifier for llvm::StringRef and llvm::SmallVector
Swift SVN r7089
2013-08-09 18:41:46 +00:00
Doug Gregor
0d37a6ee3d Let the type checker decide which protocol members are visible via conforming types.
Deduced associated types are implemented by allowing name lookup into
a type to find the requirements in  the protocols to which that type
conforms. If the protocol conformance produced the witness for the
requirement via deduction (or, eventually, default definitions), we
use the deduced witness; otherwise, the actual declaration within the
type itself is used.

Previously, the AST-level lookup was deciding whether to filter out
protocol requirements based on the conformance information in the
AST. However, this lead to ordering dependencies, because the type
checker doesn't record the conformances in the AST until we've visited
all of the conformances for a given declaration. This lead to an
oddity where one could have one conformance depend on another's
deduced associated type, but only if that conformance was on a later
extension. Fixes <rdar://problem/14685674>.

As part of this, teach the type checker to re-use normal conformances
that get deserialized, rather than creating new ones. Otherwise, we'll
end up with two different copies of the same conformance running
around.


Swift SVN r7075
2013-08-09 01:03:39 +00:00
Jordan Rose
841e3f4bd4 Properly ignore declarations not within the access path of a scoped import.
We now handle this correctly:
  import struct aeiou.A
  import struct aeiou.E

  var _ : aeiou.O // error

Swift SVN r7046
2013-08-08 17:38:24 +00:00
Doug Gregor
c2e5f499ae Correctly compute the set of protocol -> conformance mappings in name lookup.
Getting this wrong caused very, very weird bugs with deduced
associated types. Fixes <rdar://problem/14418181>.


Swift SVN r7018
2013-08-07 23:13:41 +00:00
Jordan Rose
49fc1ba334 Implement proper shadowing rules for unqualified lookup.
This includes proper rules for function overloads (i.e. shadowing by type)
and allowing type lookup to find a type even if it has the same name as
a non-type.

lookupVisibleDecls does not use this behavior yet.

Swift SVN r7016
2013-08-07 22:57:05 +00:00
Jordan Rose
027565a9a9 Implement proper shadowing rules for qualified lookup.
This required a general reworking of the algorithm for qualified name lookup.
Originally, qualified lookup only applied to a module -- not to its exports.
This meant that "Cocoa.NSWindow" would fail, so that was changed to grovel
through all exported modules looking for decls if the top-level module didn't
provide any.

Now, we actually do a breadth-based search, stopping at each level if decls
are provided for a given name. We also now prefer scoped imports to unscoped
imports, so "import abcde" and "import struct asdf.D" will result in a
(qualified) reference to 'D' being unambiguous.

Not working yet:
 - Shadowing for unqualified lookup.
 - Shadowing by types, so that overloads from this module can merge with
   overloads from its exports.

Swift SVN r7014
2013-08-07 22:56:49 +00:00
Dmitri Hrybenko
e93bcda055 Rename SourceManager::rangeContainsLoc() to rangeContainsTokenLoc()
to emphasize its limitations


Swift SVN r6999
2013-08-07 21:05:11 +00:00
Dmitri Hrybenko
686f9ec7fc SourceManager: add functions that compare SourceLocs and SourceRanges
Swift SVN r6994
2013-08-07 20:06:12 +00:00
Jordan Rose
2479f8087b Handle name resolution for qualified access into a module.
This is the Swift equivalent of allowing Cocoa.NSWindow to find
AppKit.NSWindow (or AppKit.NSWindow.NSWindow, really). Some of these
error messages could be improved, but the basic semantics are correct.

Swift SVN r6855
2013-08-02 21:01:03 +00:00
Jordan Rose
9d5c803aff Enable specific-decl imports.
Note that the import kind is not checked yet; this is effectively our old
behavior for "import swift.print".

Infrastructure: move Module::forAllVisibleModules out-of-line, and add
makeStackLambda to STLExtras for using a non-escaping lambda with
std::function.

Swift SVN r6852
2013-08-02 21:00:41 +00:00
Jordan Rose
674a03b085 Replace "oneof" with "union"...everywhere.
We haven't fully updated references to union cases, and enums still are not
their own thing yet, but "oneof" is gone. Long live "union"!

Swift SVN r6783
2013-07-31 21:33:33 +00:00
Doug Gregor
4aa4887343 Rework and centralize checking of inheritance clauses.
Detect duplicate and multiple inheritance, clean up diagnostics, and
unify the code that checks the types in the inheritance clause with
the code that sets the superclass and protocol lists.


Swift SVN r6706
2013-07-29 21:23:56 +00:00
Jordan Rose
acce3767db Introduce Module::forAllVisibleModules, and use it where useful.
This iterates over a module's exports, transitively, in an unspecified
but deterministic order. This is useful for any sort of lookup and for
managing transitive inclusion. It also allows us to remove the hack in
Sema for loading a Clang module's adapter module, and just rely on the
previous commit.

Swift SVN r6699
2013-07-29 18:57:04 +00:00
Jordan Rose
d9b7c8ad5a Move ClangModule into the ClangImporter library.
This makes it very clear who is depending on special behavior at the
module level. Doing isa<ClangModule> now requires a header import; anything
more requires actually linking against the ClangImporter library.

If the current source file really can't import ClangModule.h, it can
still fall back to checking against the DeclContext's getContextKind()
(and indeed AST currently does in a few places).

Swift SVN r6695
2013-07-29 18:56:35 +00:00
Doug Gregor
0842fb5cf8 Rename "base class" to "superclass" and "derived class" to "subclass".
Standardize on the more-common "superclass" and "subclass" terminology
throughout the compiler, rather than the odd mix of base/derived and
super/sub. 

Also, have ClassDecl only store the Type of the superclass. Location
information will be part of the inheritance clause for parsed classes.




Swift SVN r6687
2013-07-29 15:48:34 +00:00
Doug Gregor
d0f60ab755 Introduce some encapsulation into ProtocolConformance.
Swift SVN r6648
2013-07-26 18:12:17 +00:00
Jordan Rose
3087e8d5ea Add the notion of "re-exported" modules, and use that where it makes sense.
Rather than automatically re-exporting or not re-exporting every import in
a TranslationUnit, we'll eventually want to control which imports are local
(most of them) and which imports are shared with eventual module loaders.
It's probably not worth implementing this for TranslationUnit, but
LoadedModule can certainly do something here.

Currently, a LoadedModule is even more permissive than a TranslationUnit:
all imports are re-exported. We can lock down on this once we have a
re-export syntax.

Swift SVN r6523
2013-07-23 23:10:17 +00:00
Jordan Rose
110d644297 Provide a skeleton for re-exports from serialized modules...
...and use it for shadowed modules (e.g. the Clang module "Foundation"
referenced by the Swift module "Foundation"), so that we can actually
find "NSString" when building AppKit.

Additionally, record shadowed modules as dependencies, so that they can
be loaded when the adapter module is loaded.

Swift SVN r6522
2013-07-23 23:10:13 +00:00
Dmitri Hrybenko
03e6c36676 Code completion: correctly do name lookup into extensions
This change also exposes and fixes a bug where we would improperly filter
shadowed declarations.  removeShadowedDecls() assumed that all decls had the
same name, but when it is used from lookupVisibleDecls(), this is not true.
Thus, we group decls not only by signature, but also by name.


Swift SVN r6460
2013-07-22 18:05:43 +00:00
John McCall
d710c0f5f2 Use the type-in-context in the ExtensionDecl for a generic type.
This is a temporary hack until we properly make these carry
their own generic parameters.

This removes the last valid way to get UnboundGenericTypes in
a checked program.

Swift SVN r6373
2013-07-19 03:10:10 +00:00
Dmitri Hrybenko
722cc29cab Factor out DeclContext::getParentModule(). This cleans up a few places where
similar loops were duplicated.

No functionality changes.


Swift SVN r6353
2013-07-18 20:41:58 +00:00
Dmitri Hrybenko
be7d4ae71d Code completion: fix a few crashes while code completing expressions that have
generic parameter lists


Swift SVN r6349
2013-07-18 18:37:58 +00:00
Jordan Rose
9156e805f5 Don't crash when looking for imports in a loaded module.
At some point this will change because of re-exported modules, but for now
just don't crash when performing unqualified lookup on a loaded module.

Also, auto-load our adapter libraries for loaded modules, not just TUs.
Include "POSIX" in this list. (We really need autolinking.)

Swift SVN r6303
2013-07-16 23:10:49 +00:00
Dmitri Hrybenko
aa46064432 Pass a const ASTContext and const DeclContext whenever possible. This makes it
possible to use lookupVisibleDecls() with a const DeclContext.


Swift SVN r6274
2013-07-15 23:39:00 +00:00
Joe Groff
3b21e6a201 Add a OneOfElementPattern AST node type.
Add a node for references to oneof cases in patterns. No user-facing functionality change yet.

Swift SVN r6239
2013-07-13 01:21:31 +00:00
Doug Gregor
773153c84b Don't allow generic parameters to be found via qualified name lookup.
This was an oddity in qualified name lookup that appears to be a
workaround for missing behavior in unqualified name
lookup. Unqualified name lookup is still begging some serious
cleanup.


Swift SVN r6076
2013-07-08 23:23:55 +00:00
Doug Gregor
fc1c256ef5 Allow references to deduced associated types.
When checking a type's conformance against a protocol, we can deduce
the values of associated types. Make these associated types visible to
qualified name lookup so that (for example) VectorEnumeratorType does
not need to define the Element type. It is deduced from the signautre
of next(), and made available as, e.g.,
VectorEnumeratorType<Int>.Element through the Enumerator protocol
conformance. Fixes <rdar://problem/11510701>, but with some lingering
dependencies on lazy type resolution (<rdar://problem/12202655>).

Note that the infrastructure here is meant to be generalized to
support default implementations in protocols, but there are several
pieces still not in place.



Swift SVN r6073
2013-07-08 22:32:27 +00:00
Doug Gregor
1239757489 [Name lookup] Move ASTContext::lookup to Module::lookupQualified, where it belongs.
No functionality change.


Swift SVN r5921
2013-07-01 14:05:55 +00:00
Doug Gregor
a99df7a7b9 [Name lookup] Eliminate MemberLookup in favor of ASTContext::lookup().
Swift SVN r5888
2013-06-28 22:56:15 +00:00
Doug Gregor
187f80cf60 [Name lookup] Introduce ASTContext::lookup() for name lookup into a type.
This lookup routine takes the place of MemberLookup for AST-level
lookups, which don't consider semantics at all and won't be able to
(for example) perform additional type checking to resolve the
lookup. No functionality change.


Swift SVN r5882
2013-06-28 22:08:42 +00:00
Doug Gregor
2994801448 [Name lookup] Eliminate ConstructorLookup, inlining its behavior
This also eliminates some support routines that are no longer needed
with the new member lookup code.


Swift SVN r5878
2013-06-28 20:38:59 +00:00
Doug Gregor
4554961979 [Name lookup] Introduce a lookup table into each nominal type declaration.
The lookup table for a nominal type declaration provides efficient
(O(1)) access to all of the declarations with a given name in a
nominal type and its extensions. This is architecturally different
from Clang's handling of Objective-C classes and
categories/extensions, where each category/extension has its own
lookup table, and is meant to reduce the number of hash table lookups
required, especially once these hash tables are stored in the module.

The lookup table is built and updated lazily as extensions and members
are introduced, similarly to Clang's lookup tables. However, the
simpler name lookup rules in Swift (vs. C/C++/Objective-C) make this
approach actually semantically correct.



Swift SVN r5874
2013-06-28 18:43:41 +00:00
Doug Gregor
90d0cf0614 [Name lookup] Make MemberLookup stop filtering out static methods when looking into a type.
That one cannot call a static method is a semantic restriction, not a
behavior of name lookup, and name lookup shouldn't play Sema.


Swift SVN r5869
2013-06-28 15:28:39 +00:00
Joe Groff
53221db84c AST: Add 'VarPattern' node.
We decided to go with 'var' as a distributive pattern introducer which applies to bare identifiers within the subpattern. For example, 'var (a, b)' and '(var a, var b)' would be equivalent patterns. To model this, give 'var' its own AST node with a subpattern and remove the introducer loc from NamedPattern.

Swift SVN r5824
2013-06-26 23:01:47 +00:00
Joe Groff
e460a01af6 Remove the 'UnresolvedCallPattern' I stubbed out.
I talked to John about parsing patterns today, and because of the magnitude of name-lookup-dependent ambiguities between patterns and expressions, we agreed that at least for a first-pass implementation it makes sense to parse patterns as extensions of the expr grammar and charge name binding with distinguishing patterns from expressions. This gets us out of needing the concept of an "unresolved pattern", at least in the short term.

Swift SVN r5808
2013-06-26 04:23:47 +00:00
Joe Groff
7ba95cfd26 Add AST nodes for refutable patterns.
Introduce Pattern subclasses for the 'is T', 'T(<pattern>)', and '<expr>' pattern syntaxes we'll be introducing for pattern-matching "switch" statements. Also add an 'UnresolvedCalLPattern' to act as an intermediate for name lookup to resolve to a nominal type, oneof element, or function call expression pattern. Since we'll need to be able to rewrite patterns like we do expressions, add setters to AST nodes that contain references to subpatterns. Implement some basic walking logic in places we search patterns for var decls, but punt on any more complex type-checking or SILGen derived from these nodes until we actually use them.

Swift SVN r5780
2013-06-24 17:17:34 +00:00
Dmitri Hrybenko
8c3995996f Add infrastructure to easily map C types to swift stdlib types when they are
binary compatible.

This enables us to import MacTypes.h types as stdlib swift types to avoid name
conflicts.  We also import stdint.h types as stdlib swift types.

We can not map 'long double'-based ctypes.Float80 to swift.Float80 because size
of long double is 128 according to SysV ABI, and I compare that against what
the type name says (I did not find a way to find the size of the type).

Darwin.Float80 is a struct, so I don’t think we can import it as swift.Float80.
So with import Darwin, Float80 is still ambiguous.

I had to rename test/ClangModules/ctypes.swift ->
test/ClangModules/ctypes_test.swift because other tests do 'import ctypes',
which picks up 'ctypes.swift'.


Swift SVN r5727
2013-06-20 22:08:35 +00:00
Dmitri Hrybenko
7bf6d30b62 Disambiguate imported functions and structs that have the same name.
For example, now we disambiguate 'stat()' function and 'struct stat' from
the Darwin module.


Swift SVN r5587
2013-06-14 21:12:46 +00:00