Commit Graph

889 Commits

Author SHA1 Message Date
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
John McCall
b9ff2e88b8 Move getTypeOfReference fully to Sema as
TypeChecker::getUnopenedTypeOfReference.

Swift SVN r7433
2013-08-21 22:14:04 +00:00
Doug Gregor
e4eaf3fb56 Record depth/index of generic parameters.
Another baby step toward a proper canonical form for polymorphic
function types: generic parameters will eventually be uniquable by
their depth and index.


Swift SVN r7380
2013-08-20 22:44:00 +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
Doug Gregor
6c80f64c6e Diagnostic circular class inheritance.
Break cycles agressively when we find circular class inheritance. The
stronger AST invariants prevent us from having to check for loops
everywhere in the front end.


Swift SVN r7325
2013-08-19 15:31:13 +00:00
Doug Gregor
64f178a016 Reimplement circularly check for protocol inheritance.
First, make it actually check for cycles properly. Second, pull it
into the checking of the protocol itself, rather than keeping it as a
separate pass that happens too late to be useful. Finally, put the
unchecked/checking/checked bits into the AST to avoid having to keep a
separate DenseMap just for this purpose. Fixes <rdar://problem/14750346>.


Swift SVN r7324
2013-08-19 14:50:29 +00:00
Jordan Rose
ae788c8638 Mark compiler-known protocols as such early on in type-checking.
This doesn't do anything yet, but will be used to record which decls
conform to these protocols when serializing a module.

This introduces a new metaprogramming file, KnownProtocols.def.

Swift SVN r7263
2013-08-15 17:32:10 +00:00
Joe Groff
18cc8d6716 Generalize John's FieldRange and use it in UnionDecl::getAllElements.
Turn NominalType::FieldRange into a more general template that can filter by any member decl type and predicate. Use an instance of it in UnionDecl::getAllElements to iterate through all its contained UnionElementDecls.

Swift SVN r7218
2013-08-14 00:45:03 +00:00
Doug Gregor
db2428116c Don't allow loads from explicit lvalues.
Fixes <rdar://problem/13989081> and gives <rdar://problem/13827562> a
sane AST.


Swift SVN r7208
2013-08-13 20:54:35 +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
Jordan Rose
093a428ca9 Add parsing support for the [exported] attribute.
As discussed, this is an interim syntax for re-exports:
  import [exported] Foundation
In the long run, we're probably going to use the same syntax as access
control for this, but that hasn't been designed yet.

Swift SVN r7050
2013-08-08 19:09:10 +00:00
Jordan Rose
1d54332bc5 Validate import kind, e.g. "import var swift.max" now errors, with fix-it.
Again, the import kind rules are:
 - 'import KIND' can import any decl whose introducer is KIND.
 - 'import typealias' can also import a struct, class, or union.
 - Conversely, 'import KIND' can import a typealias for a decl whose
   introducer is KIND.
 - Only functions can be overloaded; anything else counts as an ambiguous
   import and is an error.
 - If an import statement only imports a single decl, but the user got the
   kind wrong, we can issue a fix-it for the kind.

We don't have source locations or synthetic source for declarations yet,
so there are no notes about what's /causing/ the ambiguities. Tracked by
<rdar://problem/14650883>

Swift SVN r6917
2013-08-05 21:03:01 +00:00
Joe Groff
ea4ace470d SILGen: Implement UnionElementPattern dispatch.
Build a switch_union instruction over all the matched union elements, and check for exhaustiveness to see if we need to emit a default branch either for subsequent matches or to fall off the end and emit unreachable if we run out of cases.

Swift SVN r6870
2013-08-02 23:26:18 +00:00
Jordan Rose
c92fa28833 Have ImportDecl vend a separate "module path" and "decl path".
This makes it very clean to reason about which part should be used
to find a module to load, and which part should be used to filter
lookup within that module.

This breaks the old "import swift.print" syntax in favor of the new
"import func swift.print", but the new syntax is currently ignored.

Swift SVN r6849
2013-08-02 21:00:22 +00:00
Jordan Rose
757cf9826f Add Parse and AST support for the new import syntax.
Also, update LangRef.

Note that an explicit "import module" has been left out for now, since
it's not strictly necessary and "module" isn't a keyword yet.

Swift SVN r6786
2013-07-31 23:23:26 +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
Jordan Rose
3e7eef56e7 Kill [stdlib] attribute.
Now that we have true serialized modules, the standard library can import
the Builtin module without any special direction (beyond -parse-stdlib),
and anyone can include those modules without special direction.

Swift SVN r6752
2013-07-30 21:27:42 +00:00
Doug Gregor
e444a4c7b6 Switch a bunch of ::getInherited() over to ::getProtocols().
Swift SVN r6660
2013-07-26 23:33:02 +00:00
Doug Gregor
a78aed7599 Cache the "existential conforms to self" bit in ProtocolDecl.
We might want to compute this eagerly and then serialize it.


Swift SVN r6617
2013-07-25 22:00:30 +00:00
Doug Gregor
d273682665 Sink the bits for ProtocolDecl's "requires class" cache into the Decl bits.
Micro-optimization FTW. I'll be adding more bits shortly.


Swift SVN r6616
2013-07-25 21:50:29 +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
John McCall
5db410b29f Fix latent bugs with ReferenceStorageType fields.
Swift SVN r6323
2013-07-17 09:20:24 +00:00
Doug Gregor
423abc5038 Codify the default argument hack for __FILE__/__LINE__/__COLUMN__.
Teach TuplePatternElt to keep track of the kind of the default
argument: none, normal (provided by calling into the appropriate
callee generator), __FILE__, __LINE__, or __COLUMN__. For the latter
three cases, the type checker forms the appropriate argument as part
of the call. 

The actual default argument expression will only be held in the tuple
pattern element when we've parsed it; it won't be serialized or
deserialized, because only the defining module cares. This is a step
toward eliminate the initialization expression from tuple types.

The extension to TupleShuffleExpr is a hack, which will also be
replicated in ScalarToTupleExpr, until we finally rework the
representation of TupleShuffleExpr (<rdar://problem/12340004>).


Swift SVN r6299
2013-07-16 22:52:38 +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
Argyrios Kyrtzidis
37dc84e13c Remove VarargBaseType from TuplePatternElt and introduce a bit in TuplePattern to indicate if there is a vararg.
The semantics of varargs (only for the last element) make it more appropriate as a property of the TuplePattern.
Also free the Parser from needing to construct synthetic types (ArraySlice for type of vararg element) to
accommodate the TypeChecker and move the logic to the TypeChecker. This will be more beneficial when the parser stops
creating types in general.

Swift SVN r6271
2013-07-15 20:21:30 +00:00
Joe Groff
d956fdbd9e Update 'oneof' syntax.
Give oneof bodies syntax consistent with other NominalTypes. Give oneof elements first-class declaration syntax using the 'case' introducer, as suggested by Jordan. Oneofs can contain 'case' decls, functions, properties, and constructors, but not physical ivars. Non-oneof scopes cannot contain 'case' decls. Add some QoI to the oneof 'case' parser to also parse and complain about things that resemble switch 'case' labels inside decl contexts.

Swift SVN r6211
2013-07-12 20:42:19 +00:00
Dmitri Hrybenko
9b3ce25c17 Add code completion for constructors
Swift SVN r6203
2013-07-12 18:34:25 +00:00
Joe Groff
070ee26c56 Replace 'requires' keyword with 'where'.
Reuse the 'where' keyword to introduce generic requirement clauses, as in 'func foo<T:Enumerator where T.Element == Int>()'.

Swift SVN r6180
2013-07-11 22:20:40 +00:00
John McCall
87ef9204d4 Rewrite the storage type of a [weak] or [unowned] variable.
getTypeOfReference drops the ReferenceStorageType from the
variable when it introduces the LValueType.

Swift SVN r6110
2013-07-10 07:56:38 +00:00
Doug Gregor
bcf41bbc3f Move the resolution of extension types, operator declarations, and superclasses into the type checker.
This eliminates the duplicate IdentifierType resolution code (fixing
<rdar://problem/13946567>), and moves us a step closure to elimining
name binding as a separate pass.


Swift SVN r5940
2013-07-01 18:38:05 +00:00
Doug Gregor
ed2823184a Have the type checker explicitly set the list of protocols
Previously, this set of protocols was computed lazily, which fails
catastrophically if getProtocols() was invoked before validation of
the types in a type/extension's "inherited" list.


Swift SVN r5924
2013-07-01 16:23:01 +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
Chris Lattner
c03d4454a0 implement support for a new [stdlib] attribute that can be slapped on an import decl.
This causes the SourceLoader to recursively parse the imported module in standard 
library mode, giving it access to the Builtin module.

This is all a terrible hack and should be ripped out with great victory someday, but 
until we have binary modules that persist the build setting used to produce the 
module, this is the best we can do.



Swift SVN r5847
2013-06-27 21:31:15 +00:00
Adrian Prantl
b9557b4d2c Add a missing case to getSourceRange() and the testcase that triggered it.
Swift SVN r5793
2013-06-25 00:19:30 +00:00
Argyrios Kyrtzidis
c7b33908ae Move the dumping visitors (PrintDecl/PrintStmt/PrintExpr) into one separate source file, so that they can easily share code, colors, and internal details.
Swift SVN r5770
2013-06-22 17:39:01 +00:00
Dmitri Hrybenko
fbc87d7b91 Add a const and non-const variants of DeclContext::getASTContext(), so that the
const one propagates constness to the returned reference.  Thanks, Argyrios!


Swift SVN r5763
2013-06-22 01:25:41 +00:00
Dmitri Hrybenko
5b86d1084f Costify DeclContext::getASTContext()
Swift SVN r5762
2013-06-22 01:09:57 +00:00
Jordan Rose
3b07d4e102 Make Pattern (more) const-correct.
Sub-patterns are now considered part of the enclosing pattern, so if the
parent pattern pointer is const, the child pointer will be too.

I changed the minimal number of files to make this work, but future code
should use "const Pattern *" when intended, and "Pattern *" only if they
intend to modify the pattern.

Swift SVN r5743
2013-06-21 17:51:39 +00:00
Joe Groff
a55f246946 Remove the controversial "bounded" from "class-bounded".
Just refer to "class archetypes" and "class protocols". Change 'isClassBounded' methods to 'requiresClass', which is a character shorter.

Swift SVN r5674
2013-06-19 03:58:11 +00:00
Joe Groff
7d322fcd35 "class-bound" -> "class-bounded"
English.

Swift SVN r5568
2013-06-11 00:19:29 +00:00
Joe Groff
27a4d0e460 Sema: Type-check class-bound protocols.
Reject attempts by non-class types to conform to class-bound protocols, whether bound directly by a [class_protocol] attribute or indirectly by refining a class-bound protocol.

Swift SVN r5552
2013-06-09 03:59:50 +00:00
Doug Gregor
8114ce16f8 Use the list of extensions of a nominal type for name lookup into that type.
This replaces the obscure, inefficient lookup into extensions with
something more straightforward: walk all of the known extensions
(available as a simple list), then eliminate any declarations that
have been shadowed by other declarations. The shadowing rules still
need to consider the module re-export DAG, but we'll leave that for
later.

As part of this, keep track of the last time we loaded extensions for
a given nominal type. If the list of extensions is out-of-date with
respect to the global generation count (which tracks resolved module
imports), ask the modules to load any additional extensions. Only the
Clang module importer can currently load extensions in this manner.


Swift SVN r5223
2013-05-20 18:06:51 +00:00
Doug Gregor
dbdc9db90a Keep track of the extensions associated with a nominal type declaration.
Switch the REPL's printing of extensions to use this mechanism, mostly
as a sanity check.



Swift SVN r5221
2013-05-20 15:23:16 +00:00
Jordan Rose
77ce3f31cb Add a DeclContextKind for Swift modules.
Swift SVN r5095
2013-05-08 18:33:34 +00:00
Doug Gregor
57002ac3dc Remove byref(heap).
Most of this is mechanical, because we weren't actually relying on
byref(heap) for anything. Simplify capture analysis, now that the only
way a variable can have non-fixed lifetime is if it is actually
captured. Fixes <rdar://problem/11247831>.


Swift SVN r5046
2013-05-06 14:07:54 +00:00
Doug Gregor
d94bb268b8 Introduce implicit constructors for imported structs in the type checker.
Previously, the Clang importer would synthesize the memberwise
constructor itself, but not a default constructor. Eliminate the
redundant code path and provide correct semantics for the second by
letting the type checker introduce the implicitly-defined constructors
itself.





Swift SVN r4973
2013-04-29 17:36:11 +00:00
Doug Gregor
c3b0320fe2 Only define the implicit default constructor if all instance variables are default-initializable.
Swift SVN r4892
2013-04-24 21:31:13 +00:00
Joe Groff
5ce1c787d2 AST: Fix SourceRange assert for implicit default ctor
Report the decl context's location as the source location of a constructor if its body BraceStmt doesn't have a real source location, like we do for the elementwise constructor.

Swift SVN r4876
2013-04-24 15:38:42 +00:00
Doug Gregor
f1e65b0583 Only introduce the implicit memberwise constructor when there are no user-declared constructors.
Rather than unconditionally introducing a memberwise constructor into
a struct, only introduce the implicit memberwise constructor if there
are not user-declared constructors within the struct declaration
itself. In other words, writing a constructor in the struct definition
takes over all of the constructors.


Swift SVN r4867
2013-04-24 05:09:18 +00:00