Commit Graph

3579 Commits

Author SHA1 Message Date
Doug Gregor
d6ca87b614 [Constraint checker] Implement typeCheckCondition() with a constraint system.
Introduce the LogicValue protocol, which is used to establish when a
particular type can be used as a logical value in a condition. Make
Bool conform to this protocol.

Rename the Builtin.i1-returning Bool.getLogicValue() to
_getBuiltinLogicValue(), because this is meant to be a builtin hook
for the compiler. We can add more underscores here if needed to hide
this further from users. 

Move the implementation of _getBool() into C code. It was performing
an 'if' on a Builtin.i1, which does not (and should not!) conform to
the LogicValue protocol. Once more oneof IR generation comes online,
we'll be able to write both this and _getBuiltinLogicValue in Swift
directly.



Swift SVN r5140
2013-05-10 15:42:41 +00:00
Joe Groff
b56aa756b0 Sema: Only add primary archetypes as BoundGenericType parameters.
When opening generic types, only the type variables for the primary archetypes become syntactic parameters of the BoundGenericType. We still open the associated archetypes; we just don't add them as arguments.

Swift SVN r5052
2013-05-06 17:12:23 +00:00
Joe Groff
9667bda089 Implement 'as' syntax for coercions and casts.
Provide distinct syntax 'a as T' for coercions and 'a as! T' for unchecked downcasts, and add type-checker logic specialized to coercions and downcasts for these expressions. Change the AST representation of ExplicitCastExpr to keep the destination type as a TypeLoc rather than a subexpression, and change the names of the nodes to UncheckedDowncast and UncheckedSuperToArchetype to make their unchecked-ness explicit and disambiguate them from future checked casts.

In order to keep the changes staged, this doesn't yet affect the T(x) constructor syntax, which will for the time being still perform any construction, coercion, or cast.

Swift SVN r4498
2013-03-27 22:27:11 +00:00
Joe Groff
4c09ef61e3 Add conditional expressions.
Implement the syntax 'if x then y else z', which evaluates to 'y' if 'x' is true or 'z' if 'x' is false. 'x' must be a valid logic value, and 'y' and 'z' must be implicitly convertible to a common type.

Swift SVN r4407
2013-03-16 20:28:58 +00:00
Doug Gregor
b6b53b612d Basic simplification of locators, which maps diagnostics to the most specific expression.
Swift SVN r4360
2013-03-12 05:26:45 +00:00
Joe Groff
062ad267c4 Value-only switch statements.
Implement switch statements with simple value comparison to get the drudge work of parsing and generating switches in place. Cases are checked using a '=~' operator to compare the subject of the switch to the value in the case. Unlike a C switch, cases each have their own scope and don't fall through. 'break' and 'continue' apply to an outer loop rather to the switch itself. Multiple case values can be specified in a comma-separated list, as in 'case 1, 2, 3, 4:'. Currently no effort is made to check for duplicate cases or to rank cases by match strength; cases are just checked in source order, and the first one wins (aside from 'default', which is branched to if all cases fail).

Swift SVN r4359
2013-03-12 04:43:01 +00:00
Doug Gregor
315451eb45 Implement parsing, AST, semantic analysis, and IRgen for dictionary literals.
Swift SVN r4193
2013-02-25 07:27:15 +00:00
Doug Gregor
a952532467 Collapse the three array-literal test cases back into one.
Swift SVN r4137
2013-02-21 22:12:06 +00:00
Joe Groff
7ebbec8a38 Add tests for <rdar://problem/13212959>.
Swift SVN r4048
2013-02-14 19:41:28 +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
Joe Groff
b5bc022686 Incomplete support for array literal expressions.
Analyze an expression of the form [<tuple contents>] into a call to T.convertFromArrayLiteral(<tuple contents>) for some T conforming to an ArrayLiteralConvertible protocol. Because of some limitations in the constraint checker and protocol conformance checkers, it currently does an ad-hoc conformance check using member constraints. It also currently fails to typecheck for generic container types, and does not provide a default fallback to 'T[]' if unable to deduce a better type from context.

Swift SVN r3953
2013-02-05 21:23:42 +00:00
Joe Groff
a0ad52a2db Constraints: Don't bail on generic conversions.
When matchTypes is trying to match up instances of the same generic type for a Conversion or Construction constraint, it isn't an error for the generic types not to be recursively the same, because a constructor or conversion operator may be available between different instances of the type. Fixes <rdar://problem/13140447>.

Swift SVN r3938
2013-02-02 23:35:43 +00:00
Joe Groff
0a16e6e1f6 Sema: super.constructor in the constraint checker.
Set up constraints and solution application for SuperConstructorRefCallExpr.

Swift SVN r3916
2013-01-31 23:17:32 +00:00
Doug Gregor
1ac5cedebf When performing name lookup into a member of an archetype, substitute the 'This' type through. Fixes <rdar://problem/12959759>.
Swift SVN r3694
2013-01-05 18:08:39 +00:00
Dave Zarzycki
8e0a421a93 XFAIL: constraint checker vs static funcs in protocols
<rdar://problem/12959759> Constrait checker cannot handle:
	func foo<T : P>(arg : T) -> T { return T.foo(arg) }

Swift SVN r3682
2013-01-04 22:19:03 +00:00
Doug Gregor
1224ff5b8e Extend the constraint-based type checker to support assignment
statements. The approach is fairly simple: generate constraints for
both the destination and source expressions, then turn the destination
type into a type containing only rvalues and convert the source
expression to that type.


Swift SVN r2925
2012-10-02 22:40:33 +00:00
Doug Gregor
b6af1cc01d Implement support for calling operators declared in protocols when
using archetypes so that, e.g., an archetype T that conforms to
Ordered can be compared with <. Requires the introduction of
'archetype' constraints, so that we don't run amok and allow the use
of operators in protocols for *any* type that meets the requirements
of the protocol. This may change later, with default implementations,
of course.

The egregious hack introduced in Char.isSpace() works around known,
massive performance problems with the solver's exploration of the
state space. In this case, we have 6 instances of ==, each of which
has 18 overloads, for a large state space. However, we compound the
problem significantly by trying many possibilities for the ~30 type
variables in each state, rather than concluding quickly (as we should)
that most of those branches don't make sense.



Swift SVN r2922
2012-10-02 18:27:21 +00:00
Doug Gregor
91ffa957b9 Add very basic support for selecting the best solution when the
constraint solver produces multiple solutions. The criteria for "best"
are fairly simple: we compare the bindings for each type variable
bound in both solutions:

  - If the binding in the first solution is a subtype of the binding
    in the second solution, but not vice-versa, the first solution is
    at least as good as the second
  - If the type variable has a literal constraint on it, and the bound
    type in the first solution matches the default literal type for
    that literal kind, the first solution is as good as the second.

From there, we follow the obvious rules for picking a winner: if some
of the type variables in the first solution are as good as the same type
variables in the second solution, but none of the type variables are
worse, then the first solution is better.

We can now type-check "1 + 2.0". Take *that*, TypeCheckCoercion.cpp.



Swift SVN r2894
2012-09-21 18:31:25 +00:00
Doug Gregor
d09688d9e1 When we don't have any useful upper or lower bounds on any type
variables to pick as potential bindings, try the default literal
types corresponding to literal constraints on a type variable.


Swift SVN r2883
2012-09-20 22:10:22 +00:00
Doug Gregor
5dfd6e8f2d Implement support for allocating a new array within the
constraint-based type checker.


Swift SVN r2808
2012-08-29 22:32:47 +00:00
Doug Gregor
31e7be143a Introduce support for the type coercion syntax
T(x)

where T is the name of a type. This syntax either coerces 'x' to the
type T or, failing that, enumerates the constructors of T and invokes
one of them.

Note that, syntactically, this coercion is syntactically identical to
a function application

  f(x)

We distinguish these cases with a simple, syntax-based scheme: if the
'function' subexpression of the application can be trivially
determined to have metatype type, then we consider this a
construction. Otherwise, we assume that it's a function
application. This disallows some crazy coercions---e.g., those that
involve overloaded variables of metatype type---but should feel fairly
intuitive, and it keeps the constraint systems simpler.



Swift SVN r2802
2012-08-28 23:38:23 +00:00
Doug Gregor
bd571b7476 Introduce a new kind of constraint that binds a type variable directly
to a type, without removing the lvalueness of the replacement
type. Keeping the lvalueness is important when referring to a
declaration, because that reference might be an
lvalue. Properties/subscripts are working better now.


Swift SVN r2796
2012-08-28 04:35:36 +00:00
Doug Gregor
6d514c17cf When type-checking explicit closure expressions within the
constraint-based type checker, include the body of the explicit
closure within the set of constraints. This allows type information to
flow from the body expression, so that we can type-check, e.g.,

  map(new Int[N], { $0.toString() })

to a String[] without context information. Fixes <rdar://problem/11229738>.


Swift SVN r2794
2012-08-27 19:01:29 +00:00
Doug Gregor
725fecd773 Allow the scalar -> tuple conversion as a trivial subtyping, rather
than simply a subtyping. Add some basic tests for accessing members of
a class.


Swift SVN r2790
2012-08-27 15:29:14 +00:00
Doug Gregor
fa474ee750 Implement simplification of conversion constraints for user-defined
conversions. We limit the relationship between the result of the
conversion and the actual expected type to a subtyping relationship,
which effectively limits us to applying a single user-defined
conversion function (rather than a chain of such conversions).

Later, we can consider extending this to a chain of conversion
functions, but that will require us to cope with cyclic conversions
and minimizing conversion sequences (e.g., if A -> B -> C and A -> C
are both possible, we should prefer A -> C).


Swift SVN r2730
2012-08-23 23:25:02 +00:00
Doug Gregor
8015fcecf8 Implement subtyping of classes in the constraint-based type checker.
Swift SVN r2724
2012-08-23 21:55:57 +00:00
Doug Gregor
cc133b2b46 Implement subtyping and conversion for existential types. The only
difference is that a concrete type's conformance to an existential
type is considered a conversion (but not a subtyping). Relationships
between two existential types are (non-trivial) subtyping relationships.


Swift SVN r2718
2012-08-23 00:29:35 +00:00
Doug Gregor
fd37448f22 Implement tuple shuffling and variadic tuple semantics for tuple
conversion constraints.


Swift SVN r2713
2012-08-22 19:07:38 +00:00
Doug Gregor
d7e0e15090 Add a helper function to determine whether a particular constraint
system has been "solved", meaning that all type variables have been
bound to concrete types or are completely free. Add the solvedness of
a particular system to the debug output.

Start adding some simple test cases.



Swift SVN r2706
2012-08-22 00:26:01 +00:00