Commit Graph

2550 Commits

Author SHA1 Message Date
Argyrios Kyrtzidis
f616eeee8b Utilize TypeReprs for type checking.
-Refactor Parser to stop creating types
-Refactor TypeChecker to create types by resolving TypeReprs.
-Remove "validation" bit from the type system.
  We don't need to "validate" every type that gets created but there's still a validation bit in TypeLoc,
  necessary because of generic substitutions.

Swift SVN r6326
2013-07-17 14:57:35 +00:00
Doug Gregor
66d3027847 Stop building UnstructuredUnresolvedTypes.
Instead, actually fill in errors types when type checking fails.


Swift SVN r6240
2013-07-13 04:17:32 +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
Argyrios Kyrtzidis
a70eff6609 Introduce TypeRepr and related subclasses, that is a representation of a type as written in source.
This the first part for improving source location fidelity for types,
changes to follow:

-The Parser will not create any types, it will just create TypeReprs.
-The type checker will create the types by going through TypeReprs.
-IdentifierType will be removed.

Swift SVN r6112
2013-07-10 14:58:52 +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
Dmitri Hrybenko
f428683842 Remove unused 'Body' parameter in FuncExpr::create()
'nullptr' is only ever passed as the argument value.


Swift SVN r5966
2013-07-02 23:12:15 +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
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
Joe Groff
647421a013 ClangImporter: Rename 'BridgedTypes' to 'MappedTypes'.
This avoids some potential confusion with what SIL calls "bridged types", which involve more than a simple mapping to bridge.

Swift SVN r5815
2013-06-26 17:20:08 +00:00
Dmitri Hrybenko
05203f70f3 ClangImporter: Use ConstDeclVisitor and constify Clang's decls throughout the importer
Swift SVN r5788
2013-06-24 20:27:57 +00:00
Joe Groff
f072c48e45 Refactor cast representation in AST and SIL, and implement 'is'.
Improve our representations of casts in the AST and SIL so that 'as!' and 'is' (and eventually 'as?') can share almost all of the same type-checking, SILGen, and IRGen code.

In the AST, we now represent 'as!' and 'is' as UnconditionalCheckedCastExpr and IsaExpr, respectively, with the semantic variations of cast (downcast, super-to-archetype, archetype-to-concrete, etc.) discriminated by an enum field. This keeps the user-visible syntactic and type behavior differences of the two forms cleanly separated for AST consumers.

At the SIL level, we transpose the representation so that the different cast semantics get their own instructions and the conditional/unconditional cast behavior is indicated by an enum, making it easy for IRGen to discriminate the different code paths for the different semantics. We also add an 'IsNonnull' instruction to cover the conditional-cast-result-to-boolean conversion common to all the forms of 'is'.

The upshot of all this is that 'x is T' now works for all the new archetype and existential cast forms supported by 'as!'.

Swift SVN r5737
2013-06-21 05:54:03 +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
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
a491a3fc93 ClangImporter: Emit metadata for imported protocol types.
Swift SVN r5605
2013-06-16 02:55:47 +00:00
Joe Groff
e9cd5b255c ClangImporter: Conditionally reenable import of ObjC protocol types.
Import ObjC protocol decls into Swift with the [class_protocol] and [objc] attributes. Add an -import-objc-protocol-types flag to reenable import of ObjC qualified object types as Swift protocol types.

Swift SVN r5604
2013-06-16 02:08:24 +00:00
Joe Groff
cb1f81db84 Make assignment an expression.
Change AssignStmt into AssignExpr; this will make assignment behave more consistently with assignment-like operators, and is a first step toward integrating '=' parsing with SequenceExpr resolution so that '=' can obey precedence rules. This also nicely simplifies the AST representation of c-style ForStmts; the initializer and increment need only be Expr* instead of awkward Expr*/AssignStmt* unions.

This doesn't actually change any user-visible behavior yet; AssignExpr is still only parsed at statement scope, and typeCheckAssignment is still segregrated from the constraint checker at large. (In particular, a PipeClosureExpr containing a single assign expr in its body still doesn't use the assign expr to resolve its own type.) The parsing issue will be addressed by handling '=' during SequenceExpr resolution. typeCheckAssignment can hopefully be reworked to work within the constraint checker too.

Swift SVN r5500
2013-06-06 22:18:54 +00:00
Doug Gregor
78f722558c Fix weird spacing
Swift SVN r5495
2013-06-06 18:54:18 +00:00
Doug Gregor
452f9562e1 [Clang importer] Only import prototyped functions.
Unprototyped C functions can't sensibly be reflected in the Swift type
system, and are awful besides, so don't import them. Then, always go
through importFunctionType to import function types, fixing the
missing pattern in <rdar://problem/13992215>.


Swift SVN r5494
2013-06-06 18:34:46 +00:00
Doug Gregor
87965ac319 When the index types of an Objective-C class's subscript getter and setter mismatch, don't build thunks for either.
Previously, we would build an invalid AST for the getter.


Swift SVN r5434
2013-06-03 18:23:48 +00:00
Doug Gregor
5e2619802d Tighten up the semantics of unchecked downcasts (x as! T).
Explicit detect (and reject) conversions that aren't class
downcasts. We'll want to lift some of these restrictions later (see
<rdar://problem/14013456>), but for now we just reject them with a
decent diagnostic (rather than crashing).

When an explicit downcast is actually just a coercion, complain,
provide a Fix-It, and update the AST appropriately.

This also splits the checking of unchecked downcasts into two different
constraint systems: one for the context of expression, and one for the
subexpression, then compares the results. This eliminates the need to
model "can be downcast to" in the constraint solver, and makes it
easier to provide good diagnostics.



Swift SVN r5377
2013-05-29 17:48:43 +00:00
Joe Groff
11c7a3a92d ClangImporter: Bridge NSString objc properties to String properties.
We had an inconsistency where the synthesized Swift getter/setters were bridged but the VarDecl wasn't, causing assertion failures in SILGen.

Swift SVN r5366
2013-05-28 22:53:14 +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
Doug Gregor
4d60bb7173 Implement trailing closure syntax.
Trailing closure syntax allows one to write a closure following any
other postfix expression, which passes the closure to that postfix
expression as an arguments. For example:

        sort(fruits) { |lhs, rhs|
          print("Comparing \(lhs) to \(rhs)\n")
          return lhs > rhs
        }

As a temporary limitation to work around the ambiguity with

  if foo { ... } { ... }

we require trailing closures to have an explicit parameter list, e.g.,

  if foo { || ... } { ... }



Swift SVN r5210
2013-05-17 19:16:18 +00:00
Doug Gregor
9a70c0ebf4 [Clang Importer] Refactor the handling of type import contexts.
The importer has always imported types differently in different
places, e.g., function parameter types have special rules for C++
references. Formalize this notion a bit, and make it work properly
when dealing with typedefs where the underlying type itself might be
imported differently in different contexts. 

As part of this, limit the import of C's 'void' type to the result of
a function type.


Swift SVN r5055
2013-05-06 18:10:46 +00:00
Doug Gregor
0f6b7a9d22 Rework our handling of "external" definitions created by the Clang importer.
Keep track of external definitions as they are created by broadcasting
them through a mutation listener interface. At name binding time, we
just cache them. When a type checker is alive, it immediately performs
any additional operations necessary on those types (e.g., declaring
implicit constructors).

This also eliminates some O(N^2) behavior in the type checker as well,
because we don't have to walk through all of the module imports to
find the external definitions. We just keep a single list in the
ASTContext along with our place in the list.

Fixes <rdar://problem/13769497>.


Swift SVN r5032
2013-05-03 00:24:34 +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
Joe Groff
2b220bcc9d ClangImporter: Work around reentrancy bug importing enum constants.
When importing an enum constant, we import its enum type, which then recursively imports the same enum constant, returns, and finishes importing the enum constant--again. Oops. Hack around this by re-checking for a cached imported definition for an enum constant after importing its type.

Swift SVN r4963
2013-04-28 03:32:35 +00:00
Joe Groff
35f81da656 ClangImporter: Suppress importing no-argument constructors besides 'init'.
NSAppleEventDescriptor has multiple "default" init methods, -init, -initRecordDescriptor, and -initListDescriptor, and they were all getting imported as default constructors in Swift. I don't know what the right thing to do long-term about init methods like these, but short-term I think we can get away with suppressing them.

Swift SVN r4962
2013-04-28 03:32:33 +00:00
Doug Gregor
c057b21193 Ban the use of construction syntax T(x) for coercions and unchecked downcasts.
Use "x as T" for coercions and "x as! T" for unchecked downcasts.


Swift SVN r4683
2013-04-11 21:17:47 +00:00
Joe Groff
fdcd04cbc1 ClangImporter: Update constructor import comments.
We don't import 'new'-family methods as Swift ctors anymore.

Swift SVN r4287
2013-03-05 21:39:40 +00:00
Joe Groff
23aaa56143 IRGen: Emit type metadata for Clang structs.
We need type metadata symbols for Clang structs so that they have witness tables and can conform to protocols (or be part of structs that conform to protocols). Consider Clang struct metadata to be 'isClangThunk' like other Clang-emitted definitions so that it all gets linkonce_odr'ed to avoid linker issues when multiple TUs import the same modules. Fixes <rdar://problem/13187143>.

Swift SVN r4170
2013-02-23 21:13:50 +00:00
Joe Groff
67748972b7 ClangImporter: Value ctors for imported structs.
Emit a value constructor for imported structs so that stuff like NSRect(NSPoint(x, y), NSSize(w, h)) works.

Swift SVN r4168
2013-02-23 20:11:05 +00:00
Doug Gregor
7cc1b6341e Map 64-bit anonymous enums and their enumerators to Int.
Following up on the NS(U)Integer -> Int mapping change, also map
64-bit anonymous enums and their enumerators to Int rather than
directly mapping the underlying integer type. This improves
interoperability with NSInteger types, allowing (e.g.) comparisons of
NSIntegers to NSNotFound.


Swift SVN r4089
2013-02-19 23:04:29 +00:00
Doug Gregor
e6880810ac Map NS(U)Integer to Int in the module importer.
This makes NS(U)Integers fit better with more Swift code, although
it's unfortunate that NSNotFound doesn't get the right type. 


Swift SVN r4088
2013-02-19 22:41:46 +00:00
Joe Groff
74589d788e Make ObjC-ness implicit to ObjC-inherited classes.
Add 'isObjC' as a property of ValueDecl, and set it during type checking if a class is either explicitly annotated with an [objc] attribute or inherits from an isObjC class, or if a func is a method of an isObjC class. Tweak the ClangImporter and other places that summon magic ValueDecl nodes to set up the decls they synthesize as isObjC. Replace logic in typechecking and IRGen that branched on the 'isObjC' attribute to now branch on the 'isObjC' property of ValueDecls.

Swift SVN r4078
2013-02-19 02:11:09 +00:00
Doug Gregor
7ff6df658c Remove unused variable
Swift SVN r4074
2013-02-18 19:19:11 +00:00
Doug Gregor
8ffd2d4df0 Always rely on Clang lookups, not Swift lookups, to establish method overrides for imported methods.
r4035 introduced overridden-method searches into the Clang importer by
looking into the Swift declarations. However, because Objective-C
allows forward declarations, there's no order in which we could
populate the Swift declarations to ensure that all of the appropriate
overrides were found. Instead, we do the right (and lazy) thing of
performing the lookup within Clang, then importing the results. This
change also introduces caching for mirrored protocol imports, so they
can be imported lazily.

Fixes <rdar://problem/13219096>.



Swift SVN r4072
2013-02-18 19:14:56 +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
Jordan Rose
67eaa1abb4 Silence -Wunused-variable warnings.
No functionality change.

Swift SVN r3988
2013-02-08 00:19:54 +00:00
Doug Gregor
32855fa161 Handle negation in imported constants, for both macros and enumerators.
Swift SVN r3963
2013-02-06 01:35:04 +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
03b1689b60 Import C enumerations into usable struct types in Swift.
Import C enumeration types as either structs wrapping the underlying
integral type (when the C enumeration type has a name) or as the
underlying integral type (when the C enumeration type has no
name). The structs have a constructor from the underlying integral
type, so one can write, e.g., NSStringCompareOptions(0) to get a
zero-valued enumeration.

Enumerators are imported as a global read-only properties.

Once oneofs start to work, we'll have a way to map some enumeration
types to oneofs, either via a Clang attribute or by sniffing out
NS_ENUM (most likely both).

Once we have static data members of structs working, we'll replace the
global constants with prefix-stripped static variables within the
struct, so we can use ".foo" notation with them.

Once we have constant declarations, we'll map to those instead of
properties.

We can add |, &, and ~ operations are part of
<rdar://problem/13028799> and have not yet been implemented.

Fixes <rdar://problem/13028891>.


Swift SVN r3945
2013-02-05 00:01:27 +00:00
Doug Gregor
0bb9f88793 Map user/library-level redefinitions of id, Class, and SEL in Objective-C to their built-in equivalents.
This makes sure that, when we load the real Foundation module, all
references to id/Class/SEL get mapped to NSObject/NSObject/ObjCSel.

Note that you'll need Clang r174234 for this to actually work
properly. Clang modules weren't coping with these redefinitions
appropriately.


Swift SVN r3934
2013-02-01 23:47:50 +00:00
Doug Gregor
299df79a43 Import Objective-C protocols properly, by introducing and using the This type
Swift SVN r3932
2013-02-01 22:28:04 +00:00
Doug Gregor
647912c3a3 "Inherit" constructors from imported Objective-C classes.
Addresses <rdar://problem/13028771>, so now we can do silly things like

  var x = new NSMutableString




Swift SVN r3931
2013-02-01 21:52:07 +00:00
Doug Gregor
272059ee55 Import init methods from protocols as constructors in classes that conform to those protocols.
Swift SVN r3927
2013-02-01 19:59:13 +00:00
Joe Groff
90afff0240 Typo in comment
Swift SVN r3924
2013-02-01 18:18:56 +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