Commit Graph

22474 Commits

Author SHA1 Message Date
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
Joe Groff
41a96b71ca Non-settable lvalue checks for TypeCheckConstraints
rdar://11259972
Add a bit to LValueType representing NonSettable, and set it in the constraint-
based checker where a property or subscript expression resolves to a decl
without a setter or to a settable member of an unsettable value type. Tweak
constraint rules for subscript, address-of, assignment, and implicit byref
arguments to rule out operands that are not settable in a context that
requires settability, such as assignment or pass-by-reference.


Swift SVN r3136
2012-11-07 01:47:10 +00:00
John McCall
3989da0acf Add a method for getting the selector string of a method.
Swift SVN r3103
2012-11-03 00:26:55 +00:00
Joe Groff
267e78db4c Add "invalid" bit to Decl type.
rdar://10157547
This just adds the bit to Decl. Actually making use of it looks like it'll require some type checker work, which should probably wait till the new checker settles down. Also add some xfail tests of the sorts of cascading diagnostic errors we should be able to prune using invalid bits on decls.

Swift SVN r3099
2012-11-02 22:50:17 +00:00
Joe Groff
6449655e21 Implement selector-style function definition syntax.
rdar://12315571
Allow a function to be defined with this syntax:

  func doThing(a:Thing) withItem(b:Item) -> Result { ... }

This allows the keyword names in the function type (in this case
`(_:Thing, withItem:Item) -> Result`) to differ from the names bound in the
function body (in this case `(a:Thing, b:Item) -> Result`, which allows
for Cocoa-style `verbingNoun` keyword idioms to be used without requiring
those keywords to also be used as awkward variable names. In addition
to modifying the parser, this patch extends the FuncExpr type by replacing
the former `getParamPatterns` accessor with separate `getArgParamPatterns`
and `getBodyParamPatterns`, which retrieve the argument name patterns and
body parameter binding patterns respectively.



Swift SVN r3098
2012-11-01 21:53:15 +00:00
Eli Friedman
b62d47a2e5 Rewrite the way we build capture list so we don't miss capturing "this". <rdar://problem/12583581>.
Swift SVN r3093
2012-10-30 22:33:18 +00:00
Doug Gregor
981e622355 Optimize our handling of referenced type variables slightly.
Swift SVN r3091
2012-10-30 00:22:58 +00:00
John McCall
bdf2730f78 Add an 'objc' method, which is currently only allowed on
classes and methods.

Swift SVN r3087
2012-10-30 00:17:50 +00:00
John McCall
24d819e133 Split BoundGenericType into BoundGeneric{Class,OneOf,Struct}Type.
We'll want a superclass pointer on ClassType and BoundGenericClassType,
and this also makes it easier to detect both kinds of class type.

Swift SVN r3061
2012-10-25 21:44:44 +00:00
John McCall
c8f53e3535 Add a MetatypeConversionExpr for doing derived-to-base
conversions on metatypes;  at runtime it has no effect,
since those conversions are always trivial.  Fix a number
of bugs involving the conversion of metatypes, in both
typecheckers.

Swift SVN r3055
2012-10-25 10:21:44 +00:00
John McCall
2f8f05615e Rename TypeOfExpr / TypeOfInst to MetatypeExpr / MetatypeInst.
Introduce a '.metatype' form in the syntax and do some basic
type-checking that I probably haven't done right.  Change
IR-generation for that and GetMetatypeExpr to use code that
actually honors the dynamic type of an expression.

Swift SVN r3053
2012-10-24 07:54:23 +00:00
Jordan Rose
cbf72166ba Swallow semicolons after decls when in a container.
Outside of a container, semicolons after decls are just parsed as SemiStmts.
Inside a container, though, we only allow decls...but we should still allow
trailing (delimiting?) semicolons.

If you have multiple semicolons in a row, though, that's probably a typo,
so parseDecl will now also complain (error) if it sees a semicolon where
a decl is expected.

<rdar://problem/12540877>

Swift SVN r3051
2012-10-24 00:54:51 +00:00
Chris Lattner
3e4562cbdd refactor the body of visitTupleShuffleExpr out to a helper function that
can be reused.  No functionality change.


Swift SVN r3026
2012-10-19 20:41:48 +00:00
Chris Lattner
bafdd11e90 comment typo
Swift SVN r3015
2012-10-16 21:50:45 +00:00
Doug Gregor
ce33027f0a Add a variant of TypeBase::hasTypeVariable() that gathers all of the
type variables mentioned in the type. This will be used by the
constraint-based type checker in the near future.


Swift SVN r3004
2012-10-15 15:19:08 +00:00
Chris Lattner
a052faf351 remove some implementations of classof that are now unneeded with mainline LLVM changes.
Swift SVN r2989
2012-10-12 18:22:16 +00:00
Chris Lattner
6a6dfed3ec CFGGen non-varargs TupleShuffleExprs as an extract+insert sequence.
Swift SVN r2984
2012-10-11 21:10:29 +00:00
John McCall
ae978371ac Make calls to non-static methods on classes use virtual
dispatch.  Currently there is no possibility of override.

This was really not as difficult as I managed to make it
the first time through.

Swift SVN r2960
2012-10-10 01:31:47 +00:00
Doug Gregor
408ff40ec0 Add a verifier for NewArrayExprs, which were the last holdout to get
the constraint-based type checker building the Swift standard library.


Swift SVN r2951
2012-10-08 20:38:28 +00:00
Doug Gregor
13d8eed15c Add a verifier for ReturnStmt.
Swift SVN r2947
2012-10-08 19:03:51 +00:00
John McCall
00126779d6 The world demands a common generics header, and who am I
to disagree with billions of other people?

Swift SVN r2926
2012-10-03 08:57:36 +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
John McCall
72c5c2867c Add the ability to propagate arbitrary other information
around TypeVisitors.

Swift SVN r2899
2012-09-27 06:17:40 +00:00
Eli Friedman
77c0114186 Make sure TupleType::get never returns an invalid TupleType.
Finishes off <rdar://problem/12337042>.

Also, fix constraint application so that test/Constraints/closures.swift
doesn't explode with the above fix.



Swift SVN r2888
2012-09-21 01:54:26 +00:00
Eli Friedman
d8b84d6cd0 Add ScalarToTupleExpr to represent an implicit conversion from a scalar to a tuple. Part of <rdar://problem/12337042>.
Swift SVN r2887
2012-09-21 00:45:33 +00:00
Doug Gregor
578ff6631a ColonLoc -> DotLoc, to reflect the current syntax for unresolved member expressions
Swift SVN r2867
2012-09-18 21:59:21 +00:00
Doug Gregor
42b1ab6fbd Introduce a LangOptions class to capture various type-checker-tweaking flags. For now, introduce bits to enable the constraint solver and to enable debugging of the constraint solver, and use those to eliminate the "useConstraintSolver" bit that was threaded through too much of the type checker.
Swift SVN r2836
2012-09-12 20:19:33 +00:00
Doug Gregor
693318642d Remove the "type checker cannot handle well-typed expressions" error,
since we're now applying solved constraints to an expression to
produce a well-typed expression. The resulting expressions are now
returned and run through the verifier, so deal with some of the
byref(implicit) fallout.


Swift SVN r2835
2012-09-12 02:14:13 +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
029f7329c5 Re-introduce the notion of implicit lvalues, to be used within the
constraint-based type checker. We now model the address-of operator
with the constraint

  [byref] T1 < T2

where T1 is a fresh variable and T2 is the type of the subexpression
of &. 

Note that there's a little hack in the constraint generator that
strips off the 'heap' qualifier when performing constraint-based type
checking, because we're not actually using it for overloading (merely
as an aid for IRgen) and it causes some trouble in the modeling of the
address-of operator.

We can now properly reject code such as

  swap(a, b)

which should be

  swap(&a, &b)



Swift SVN r2797
2012-08-28 18:21:27 +00:00
Doug Gregor
81d8f27bde When dumping constraints via TypeChecker::typeCheckExpression(), don't
rely at all on the existing type checker. Instead, just fold sequence
expressions (which are effectively a delayed parsing phase) and then
hand off the expression to the constraint solver.

Allows us to type-check expressions with operators, e.g., f + 1.


Swift SVN r2793
2012-08-27 18:33:20 +00:00
Ted Kremenek
a5ec0af7e8 Rename 'BraceStmt::elements()' to 'getElements()' to match Swift
naming style for accessors.

Swift SVN r2742
2012-08-24 14:17:28 +00:00
Eli Friedman
50e21a838c More semantic analysis for classes inheriting from generic classes.
Swift SVN r2736
2012-08-24 00:01:24 +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
Eli Friedman
da954c0cbd Minor cleanup for type validation: a type only needs to be validated once,
and type validation should never fail in a call to validateTypeSimple.

The one thing I really don't like about this whole approach is that we end
up with validateTypeSimple calls scattered all over the place...
the ultimate solution here is probably something along the lines of
introducing getUnvalidated() getters for types for use from the parser,
and make the standard getters for types assert that the type is valid
and perform any necessary computations themselves.



Swift SVN r2728
2012-08-23 22:42:36 +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
John McCall
e7b9ae47fa Defer to the value witness when moving an archetype. This
is really a deficiency in TypeInfo::initializeWithTake, which
is now virtual and not implemented in TypeInfo anymore.  This
fixes rdar://problem/12153619.

While I'm at it, fix an inefficiency in how we were handling
ignored results of generic calls, and add 4 new builtins:
  Builtin.strideof is like sizeof, but guarantees that it
  returns a multiple of the alignment (i.e., like C sizeof,
  it is the appropriate allocation size for members of an
  array).
  Builtin.destroy destroys something "in place";  previously
  this was being simulated by moving and ignoring the result.
  Builtin.allocRaw allocates raw, uninitialized memory, given
  a size and alignment.
  Builtin.deallocRaw deallocates a pointer allocated with
  Builtin.allocRaw;  it must be given the allocated size.

Swift SVN r2720
2012-08-23 05:21:31 +00:00
Doug Gregor
9d315ae72a Keep track of whether a given type involves a type variable.
Swift SVN r2705
2012-08-22 00:24:22 +00:00
Eli Friedman
eb1689710f Delete isa, cast, and dyn_cast on Type. Hopefully, this will lead to fewer stupid mistakes.
Swift SVN r2691
2012-08-20 22:15:10 +00:00
Doug Gregor
6caeebe1c8 Fix unresolved computation for array slice types.
Swift SVN r2684
2012-08-20 16:12:33 +00:00
Doug Gregor
b4c91a34a0 Whether a canonical type has been constructed for a given type is not
a reliable way to track whether a particular type has been
validated. Instead, add some bits to the type to indicate which stage
of checking it has received. I hate it, but it works and I don't know
of a better way to ensure that types get validated. This subsystem
will need to get rearchitected at some point (ugh).

Reduce the number of places where we build new BoundGenericTypes,
since they need to be validated fully to get substitutions, and then
introduces a number of validateTypeSimple() calls to validate types in
places where we know the validation will succeed, but we need that
information regardless.


Swift SVN r2681
2012-08-18 00:09:56 +00:00
Eli Friedman
bef302d2ee Initial attempt at implementing semantic analysis of overriding for classes.
Still a lot of missing pieces, but it mostly works.



Swift SVN r2633
2012-08-15 01:21:35 +00:00
Doug Gregor
1c64358126 Constraint generation: for each reference to a generic function,
replace each archetype with a fresh type variable (that conforms to
the same protocols as the archetype) and make the resulting function
type monomorphic over those type variables. The resulting constraints
provide deduction for the generic parameters, e.g.,

swift> func ident<T>(x : T) -> T { return x }
swift> var x : Int
swift> :dump_constraints(ident(x))
---Constraints for the given expression---
(paren_expr type='T2'
  (call_expr type='T2'
    (declref_expr type='(x : T0) -> T0' decl=ident)
    (paren_expr type='[byref(heap)] Int'
      (declref_expr type='[byref(heap)] Int' decl=x))))

---Type Variables---
  T0
  T1
  T2

---Constraints---
  (x : T0) -> T0 == (T1) -> T2
  [byref(heap)] Int << T1
Int = 0




Swift SVN r2632
2012-08-14 22:15:08 +00:00
Doug Gregor
0d7afbe5e0 Introduce an almost completely untested implementation of constraint
generation from an expression that has not been type-checked. One can
see the constraints introduced by an expression by using

  :dump_constraints <expression>

within the REPL. We're still missing several major pieces of
constraint generation:
  - We don't yet "open up" references to polymorphic types
  - We don't print out the child constraint systems in the dump, so
  it's not at all obvious what happens within overloading (and I'm not
  convinced I like my representation anyway)
  - There are no tests whatsoever
  - Member constraints are still very, very weird



Swift SVN r2624
2012-08-13 22:59:41 +00:00
John McCall
88e03293cc Change the formal type of subscript declarations to always
pass the index as a separate argument.  This makes it much
easier to work with these things generically.

Swift SVN r2616
2012-08-13 09:02:37 +00:00
Eli Friedman
9e06964c8a Make the TypeLoc constructor which takes just a type private, and expose a
static method to call it, to make it more explicit what is happening.  Avoid
using TypeLoc::withoutLoc for function definitions; instead, just use an empty
TypeLoc.



Swift SVN r2606
2012-08-10 02:09:00 +00:00
Eli Friedman
6781f56cfd Start of support for class inheritance.
Swift SVN r2598
2012-08-09 21:56:05 +00:00
Ted Kremenek
69b125d129 Remove dead code.
Swift SVN r2588
2012-08-09 01:08:11 +00:00
Ted Kremenek
479077e354 Remove individual element setters from BraceStmt, and just use MutableArrayRef and ArrayRef to access its elements.
Swift SVN r2586
2012-08-08 05:06:33 +00:00
Eli Friedman
95cb2d6af2 Add builtins to convert between arbitrary retainable pointers,
Builtin.ObjectPointer, and Builtin.RawPointer.  I don't really like the way
these builtins are defined (specifically, the part where messing up
gives a mysterious IRGen error), but it's workable. <rdar://problem/11976323>.



Swift SVN r2585
2012-08-08 00:40:07 +00:00