Commit Graph

3461 Commits

Author SHA1 Message Date
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
Doug Gregor
6ad8dfd230 <rdar://problem/13698402> Use raw_ostream::has_colors rather than is_displayed
to determine whether we should display colors.


Swift SVN r4832
2013-04-20 06:55:12 +00:00
Joe Groff
88d5192a69 SILGen: Don't crash if FuncDecls lack bodies.
FuncDecls for builtins don't have bodies. Deal with this when determining function natural uncurry levels and captures for SILGen.

Swift SVN r4734
2013-04-14 19:45:02 +00:00
Joe Groff
d39ee86c71 Sema: Check func operator arity syntactically.
This gives us a couple things:

- It lets name binding match up operator funcs to operator decls reliably without depending on unary operators being properly attributed;
- It allows unary operators on tuples to be distinguished from binary operators; the former should always be declared 'func +(_:(x:A,y:B))', and the latter as 'func +(x:A,y:B)'.

Swift SVN r4636
2013-04-08 18:01:19 +00:00
Joe Groff
ac23437886 Replace infix attributes with operator decl lookup
During name binding, associate func decls with operator decls. When parsing SequenceExprs, look up operator decls to determine associativity and precedence of infix operators. Remove the infix_left and infix_left attributes, and make the infix attribute a simple declared attribute [infix] with no precedence.

Operator decls are resolved as follows:

- If an operator is declared in the same module as the use, resolve to the declaration in the current module.
- Otherwise, import operator declarations from all imported modules. If more than one declaration is imported for the operator and they conflict, raise an ambiguity error. If they are equivalent, pick one arbitrarily.

This allows operator declarations within the current module to override imported declarations if desired or to disambiguate conflicting operator declarations.

I've updated the standard library and the tests. stdlib2 and some of the examples still need to be updated.

Swift SVN r4629
2013-04-07 02:43:03 +00:00
Chris Lattner
b4fd6dd04a Change TopLevelCodeDecl to allow it to hold a sequence of different exprs and statements in one unit, wrapping them into a BraceStmt. This makes it more similar to other decls (e.g. funcdecl, ctor decls, etc) and will be useful for future sil work.
Unfortunately, this regresses the repl when expressions like (1,2) are entered. This is because the repl is violating some invariants (forming dags out of ASTs, making ASDAG's which upset the type checker).  I'm going to fix this next, but can't bring myself to do it in the same commit.



Swift SVN r4617
2013-04-05 22:33:14 +00:00
Joe Groff
aeeda4ee12 Parser: Parse operator decls.
At the top level, if 'operator' is followed by 'infix', 'prefix', or 'postfix', consider it a contextual keyword, and parse an operator decl following it that looks like:

  operator {infix|postfix|prefix} <+> {
    attributes…
  }

Prefix and postfix operator decls currently admit no attributes. Infix operators have 'associativity {left|right|none}' and 'precedence <int>' attributes.

This patch implements parsing for operator declarations but does not yet attach the declared attributes to func decls for the operators.

Swift SVN r4596
2013-04-03 23:30:50 +00:00
Joe Groff
8caf747853 Put alignas(N) on classes with strict alignment.
We use three tag bits on Expr*, Stmt*, Decl*, TypeBase* and SILTypeInfo*, and four on DeclContext*, so set the alignment of the pointed-to types formally with alignas(N) instead of relying on operator new passing down the right alignment to the allocator. Get rid of the informal T::Alignment members of these classes and pass alignof(T) to their allocators. Fix the 'operator new' of DeclContext subclasses so that we can actually use the four tag bits PointerLikeTypeTraits<DeclContext*> claims are available.

Swift SVN r4587
2013-04-03 17:27:05 +00:00
Joe Groff
4923acce2b Use C++11 alignof(T) instead of llvm::AlignOf<T>.
Swift SVN r4583
2013-04-03 02:41:29 +00:00
Joe Groff
470ad60d60 IRGen: Emit properties for ivars of ObjC classes.
If a class has ObjC interop, synthesize property accessors for its ivars so that things like IBOutlets and bindings work correctly.

Swift SVN r4240
2013-02-28 20:24:43 +00:00
Joe Groff
be3f4ae1ec IRGen: Emit ObjC methods for properties.
Emit getter and setter ObjC methods and selectors for Swift properties declared in ObjC classes or extensions. If the type of the property is an ObjC class, also emit special-cased ObjC formal property metadata for the property as if it were declared @property (nonatomic, {readonly,strong}) in ObjC.

Swift SVN r4207
2013-02-26 22:57:45 +00:00
Joe Groff
a8b5a2ec26 AST: Add UnresolvedSpecializeExpr.
This node represents a type parameter list application in an unresolved expr context. The type checker will use these to explicitly bind type variables of generic types.

Swift SVN r4046
2013-02-14 19:41:23 +00:00
Doug Gregor
5c26e6cc3d Make sure that the output stream gets flushed before forming the result of getObjCSelector().
Swift SVN r4037
2013-02-13 21:03:11 +00:00
Doug Gregor
b660580f35 Don't permit calling static methods on an instance <rdar://problem/13206762>.
Swift SVN r4033
2013-02-13 19:27:39 +00:00
Joe Groff
750374f61a Sema: Typecheck super.method expressions.
Add support to the constraint checker for typechecking UnresolvedSuperMemberRef expressions and constructing SuperMemberRef or SuperCall expressions as appropriate. We’ll also need a GenericSuperMemberRefExpr to refer to properties of generic supertypes, but in the interests of demo expedience I’m leaving that case partially-implemented for now.

Swift SVN r4020
2013-02-12 19:23:34 +00:00
Doug Gregor
a9a90fd56a Track the Clang macros that were used to generate Swift constants.
Swift SVN r3960
2013-02-06 00:57:03 +00:00
Doug Gregor
d17b175a29 When importing an Objective-C class, category, or extension that
explicitly conforms to protocols, mirror any protocol methods that
aren't also declared in the class/category/extension within the
extension. Among other things, this makes sure that "-hash" and
"-isKindOfClass:" are available on NSObject (they come from the
NSObject protocol). 

Fixes <rdar://problem/13074691>.


Swift SVN r3920
2013-02-01 00:44:53 +00:00
Doug Gregor
2b2b2cfc31 Replace the constructor 'alllocates_this' attribute with an 'allocate-this' expression.
By splitting out the expression used to allocate 'this' (which exists
in the AST but cannot be written in the Swift language proper), we
make it possible to emit non-allocating constructors for imported
Objective-C classes, which are the only classes that have an
allocate-this expression.


Swift SVN r3558
2012-12-20 15:28:37 +00:00
Doug Gregor
75c8591487 When lookup finds a generic parameter, treat it like a local declaration, not a member declaration.
Swift SVN r3534
2012-12-18 22:50:49 +00:00
Joe Groff
e43761c199 AST: add print() and dump() methods to Pattern.
Expose the PrintPattern visitor implementation in AST/Decl.cpp through methods on Pattern, as is done with the other AST node types.

Swift SVN r3448
2012-12-12 17:42:15 +00:00
Doug Gregor
f3857df469 Implement an AST pretty-printer for declarations, under -print-ast.
Swift SVN r3431
2012-12-10 23:21:49 +00:00
Joe Groff
ac40cb59ff AST: Dump getter/setter link for FuncDecls.
Swift SVN r3363
2012-12-05 17:59:01 +00:00
Doug Gregor
d04db691ec Retain the first selector piece when importing init/new methods as constructors.
This is the conservative solution that we can revisit later.


Swift SVN r3352
2012-12-04 20:00:01 +00:00
Doug Gregor
629b06623d Import Objective-C categories and extensions as Swift extensions.
Swift SVN r3262
2012-11-27 22:19:16 +00:00
Doug Gregor
311520a3ec Implement support for calling Objective-C instance methods imported from Clang.
Tweak the import of Objective-C methods to build the proper FuncExpr
and tag the FuncDecl as an Objective-C method, along with a few other
tweaks, so calls to the imported Objective-C methods go through
objc_msgSend().

At this moment, this is aborting in the Objective-C runtime due to an
unrecognized selector. The issue does not appear related to the
importer.


Swift SVN r3255
2012-11-27 07:50:52 +00:00
Doug Gregor
83f2874ba1 Implement support for calling imported C functions from Swift.
There is no protection whatsoever if the Clang-to-Swift type
conversion produces something that Swift doesn't lower in an
ABI-compatible way. That will be dealt with later.


Swift SVN r3249
2012-11-27 00:34:48 +00:00
Doug Gregor
bb26f52585 Initial support for loading Clang modules into Swift.
From a user's perspective, one imports Clang modules using the normal
Swift syntax for module imports, e.g.,

  import Cocoa

However, to enable importing Clang modules, one needs to point Swift
at a particular SDK with the -sdk= argument, e.g.,

  swift -sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9M.sdk

and, of course, that SDK needs to provide support for modules.

There are a number of moving parts here. The major pieces are:

CMake support for linking Clang into Swift: CMake users will now need
to set the SWIFT_PATH_TO_CLANG_SOURCE and SWIFT_PATH_TO_CLANG_BUILD
to the locations of the Clang source tree (which defaults to
tools/clang under your LLVM source tree) and the Clang build tree.

Makefile support for linking Clang into Swift: Makefile users will
need to have Clang located in tools/clang and Swift located in
tools/swift, and builds should just work.

Module loader abstraction: similar to Clang's module loader,
a module loader is responsible for resolving a module name to an
actual module, loading that module in the process. It will also be
responsible for performing name lookup into that module.

Clang importer: the only implementation of the module loader
abstraction, the importer creates a Clang compiler instance capable of
building and loading Clang modules. The approach we take here is to
parse a dummy .m file in Objective-C ARC mode with modules enabled,
but never tear down that compilation unit. Then, when we get a request
to import a Clang module, we turn that into a module-load request to
Clang's module loader, which will build an appropriate module
on-the-fly or used a cached module file.

Note that name lookup into Clang modules is not yet
implemented. That's the next major step.



Swift SVN r3199
2012-11-16 18:17:05 +00:00
John McCall
2568c1b043 getSelector -> getObjCSelector per Doug's review.
Swift SVN r3186
2012-11-14 19:30:22 +00:00
Doug Gregor
2183b57fa4 Introduce a special memory arena in ASTContext for the type checker.
This introduces the notion of arenas into ASTContext, with two arenas
currently defined: one for 'permanent' storage, and another for the
current constraint checker. The latter is used when allocating any
types that involve type variables, which are only used temporarily
during type checking anyway.

This gives us a 1% speedup on swift.swift (because we're hitting
smaller hash tables when doing lookups) and < 1% memory reduction
(since that's not the main source of memory usage). It's more
important architecturally, so our memory usage doesn't grow with the
number of type-checks performed.

Note also that this arena scheme could be generalized, which we may
very well want to do in the future. For example, we could easily have
an arena for temporary nodes introduced by parsing (e.g.,
UnresolvedDeclRefExpr) or by name binding (OverloadedDeclRefExpr), and
clear that arena when we successfully move onto the next phase. Or, in
a REPL/debugger context, have a 'temporary' arena for
statements/expressions that can be removed.



Swift SVN r3175
2012-11-14 06:57:16 +00:00