Commit Graph

283 Commits

Author SHA1 Message Date
Doug Gregor
b9561e00ab Introduce an empty, implicit default constructor into structs.
When the struct definition has no user-defined constructors, and there
is at least one instance variable, add an implicit default
constructor. This default constructor is currently empty; that's to be
fixed next.


Swift SVN r4868
2013-04-24 05:50:06 +00:00
Joe Groff
262baea632 AST: Use lambdas instead of ^blocks in Verifier.
Swift SVN r4856
2013-04-22 01:10:00 +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
10438902dd Remove GetMetatypeExpr.
Now that we don't allow static methods to be invoked from instances we no longer need an AST node to represent an implicit instance-to-metatype conversion. MetatypeExpr encodes the explicit '.metatype' operation.

Swift SVN r4472
2013-03-22 00:28:21 +00:00
Joe Groff
be09b16f93 Sema: Allow returns from constructors/destructors.
In the StmtChecker, consider a ConstructorDecl or DestructorDecl context to be a function context that returns (). In IRGen for constructors and destructors, set up proper return blocks so that 'return' does the right thing, and in constructors, pretend that the body has no return value even though the underlying constructor mechanism totally returns a value. Fixes <rdar://problem/12950817>.

Swift SVN r3915
2013-01-31 22:30:12 +00:00
John McCall
310268c4fd Fix several bugs in the emission of metatype references.
In particular, fix a bug where DREs that refer to types were
always given trivial reps because we were trying to emit the
metatype's metatype.  This in turn exposes a number of bugs,
including a typechecker bug (where GetMetatypeExpr bases
weren't converted to r-values) and a bug where MREs that
refer to types were always assumed to produce trivial reps.

Swift SVN r3095
2012-10-31 08:09:28 +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
Doug Gregor
ff6b7e52b4 Kill an unnecessary semicolons
Swift SVN r2952
2012-10-08 21:14:58 +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
Doug Gregor
95830d4db0 Clean up some type computations in the application of a constraint
system to an expression, and fix a bogus check in the verifier.


Swift SVN r2881
2012-09-20 20:46:37 +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
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
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
Doug Gregor
8d583f412b Add a verifier to check that bound generic types always have
substitution information, but keep it turned off because it fails
everywhere :(


Swift SVN r2673
2012-08-17 18:12:15 +00:00
Doug Gregor
011e9da1ef When we're specializing, we need to know how many levels of generic
parameters we're substituting. This is either "all levels" (when we're
substituting a specific base type in a member access) or "the
innermost level" (when we're substituting after deducing from a use or
application of a polymorphic function).


Swift SVN r2576
2012-08-07 22:04:31 +00:00
Eli Friedman
84e858da4e Fix static member functions so a member of type T has type "metatype<T> -> () -> ()" instead of "() -> ()".
This is much more convenient for IRGen, and gives us a reasonable representation for a static
polymorphic function on a polymorphic type.

I had to hack up irgen::emitArrayInjectionCall a bit to make the rest of this patch work; John, please
revert those bits once emitCallee is fixed.



Swift SVN r2488
2012-07-28 06:47:25 +00:00
Doug Gregor
9e8633ac41 Encode and pass all of the archetypes, including derived archetypes,
in SpecializeExpr, so that we have complete substitution and
protocol-conformance information. On the IR generation side, pass
witness tables for all of the archetypes (again, including derived
archetypes) into generic functions, so that we have witness tables for
all of the associated types.

There are at least two major issues:

  (1) This is a terribly inefficient way to pass witness tables for
  associated types. The witness tables for associated types should be
  accessible via the witness tables of their parent. However, we need
  more information in the ASTs here, because there may be additional
  witness tables that will need to be passed for requirements that are
  placed on the associated type by the generic function itself.

  (2) Something about my test triggers a void/non-void verification failure
  in the witness build for an instance function whose abstracted form
  returns an associated type archetype and whose concrete form returns
  an empty struct. See the FIXME in the test.



Swift SVN r2464
2012-07-26 17:52:04 +00:00
Doug Gregor
67c2354d28 When building archetypes, gather all of the archetypes (both primary
and derived) so they can be stored within the generic parameter list
for use 'later'.

More immediately, when we deduce arguments for a polymorphic function
type, check that all of the derived archetypes conform to all of the
protocol requirements, stashing that protocol-conformance information
in the coercion context (also for use 'later'). From a type-checking
perspective, we now actually verify requirements on associated types
such as the requirement on R.Element in, e.g.,

  func minElement<R : Range requires R.Element : Ordered>(range : R)
         -> R.Element



Swift SVN r2460
2012-07-26 00:24:05 +00:00
Eli Friedman
9d56f5c304 Tighten verifier restrictions on SpecializeExpr.
Swift SVN r2448
2012-07-25 21:10:47 +00:00
Eli Friedman
f6e42b36f8 Get rid of a couple abuses of SpecializeExpr by using TypeOfExpr instead.
Swift SVN r2446
2012-07-25 20:26:55 +00:00
Eli Friedman
57dcb9d810 Add a verifier for TupleShuffleExpr. Fix expression type-checking so we perform the necessary conversions in the given testcase. <rdar://problem/11935346>.
Swift SVN r2429
2012-07-24 19:09:55 +00:00
Eli Friedman
e592e07f2e Compute full SourceRanges for Decls.
Swift SVN r2376
2012-07-19 22:27:13 +00:00
Eli Friedman
308c50fdd5 Fix a verifier crash with generic types.
Swift SVN r2341
2012-07-11 02:05:17 +00:00
Eli Friedman
728f948a79 Make Builtin.load, Builtin.init, and Builtin.assign generic functions.
This is most of what is necessary to implement UnsafePointer<T>; we also
will need Builtin.sizeof eventually.



Swift SVN r2325
2012-07-09 23:55:03 +00:00
Doug Gregor
fe2059da50 Add a verifier to check that the protocol conformances provided by
SpecializeExpr match up with the protocol requirements as stated on
the archetypes.


Swift SVN r2320
2012-07-09 17:34:54 +00:00
Eli Friedman
66907893d3 Allow destructuring assignments (assignment to a tuple expression). <rdar://problem/11719247>.
Swift SVN r2285
2012-07-02 21:39:52 +00:00
Doug Gregor
cf4a05f66d Eliminate SuperConversionExpr. This expression node was only really
used in the very narrow case where we were converting from one
protocol type to another (super) protocol type. However, ErasureExpr
now handles this case via its null conformance entries (for the
"trivial" cases), and can cope with general existential types where
some conversions are trivial and others are not.

The IR generation side of this is basically just a hack to inline the
existing super-conversion code into the erasure code. This whole
routine will eventually need to be reworked anyway to deal with
destination types that are protocol-conformance types and with source
types that are archetypes (for generic/existential interactions).



Swift SVN r2213
2012-06-20 16:26:48 +00:00
Eli Friedman
338aadad57 IRGen for logical lvalues with a base of class type. <rdar://problem/11466574>
Swift SVN r2177
2012-06-08 22:38:36 +00:00
Doug Gregor
3c2fb97bdf Introduce TypeBase::isExistentialType(), to determine whether a given
type is either a protocol type or a protocol composition type. The
long form of this query returns the minimal set of protocol
declarations required by that existential type.

Use the new isExistentialType() everywhere that we previously checked
just for ProtocolType, implementing the appropriate rules. Among other
things, this includes:
  - Type coercion
  - Subtyping relationship
  - Checking of explicit protocol conformance
  - Member name lookup

Note the FIXME for IR generation; we need to decide how we want to
encode the witnesses for the different protocols.

This is most of <rdar://problem/11548207>.


Swift SVN r2086
2012-05-31 00:26:13 +00:00
Eli Friedman
0c24d526af Add rules to verifier for CoerceExpr.
Swift SVN r2072
2012-05-30 18:23:14 +00:00
Eli Friedman
155ed201c1 Name-binding doesn't touch expressions anymore, so UNBOUND_EXPR isn't necessary.
Swift SVN r1986
2012-05-25 02:28:08 +00:00
Eli Friedman
35d7084c40 Add framework for handling decls in the verifier.
Swift SVN r1926
2012-05-22 00:42:31 +00:00
Eli Friedman
3ff2ef1efb Allow the verifier to perform partial checking if there's an error in type-checking.
Swift SVN r1925
2012-05-22 00:34:12 +00:00
Doug Gregor
c68c9da1b6 Introduce ThisApplyExpr, an ApplyExpr meant for the various forms of
application expressions that provide a 'this' parameter to a
method. There will eventually be more than just DotSyntaxCallExpr, to
capture the other syntactic (and implicit) forms.


Swift SVN r1924
2012-05-22 00:25:40 +00:00
Eli Friedman
98933b8510 More work on ClassDecls; this is enough to get member access working.
Swift SVN r1863
2012-05-16 00:22:09 +00:00
Doug Gregor
bf03c9af28 Remove byref(implicit). Instead, introduce [assignment], which only
applies to operators whose first parameter is [byref]. Assignments
must return Void, and have their first arguments implicitly treated as
an lvalue.

As part of this, add the various C compound operators (+=, -=, *=, /=,
&=, |=, and ^=) to the standard library.



Swift SVN r1846
2012-05-15 00:55:32 +00:00
Doug Gregor
59db25658a Ban the use of SuperConversionExpr to produce lvalues, since that is,
in general, not sound. For the limited cases where we did use this
expression kind on lvalues (member access or instance method
invocations on a superprotocol), leave the conversion to the client of
their respective AST nodes (MemberRefExpr, DotSyntaxCallExpr). We may
decide to enrich these ASTs in the future with more information about
the conversion path, but it's not clear that it's actually useful
information for, e.g., IRgen.


Swift SVN r1830
2012-05-14 15:38:27 +00:00
Doug Gregor
ec674b3bce Make the implicit 'this' argument [byref] rather than
[byref(implicit)], and rely on type coercion for the implicit object
argument to deal with the implicitness. This is one half of
<rdar://problem/11215969>. 

The other half of <rdar://problem/11215969> requires us to invent some
syntax to allow increment/decrement/assignment to operate on lvalues
without requiring one to write '&'. There are two distinct ideas
gaining traction here:

  - Adding an [assignment] attribute to these operators, which tells
    us both that use of the operators is a statement (not an
    expression) and that the byref argument (either on the left, or
    the only argument).

  - Allowing operators to be written as member functions, so that they
    get the same treatment as "a.b".

The former seems like the better option.



Swift SVN r1819
2012-05-12 00:19:22 +00:00
Eli Friedman
01a18f6c7a Use VarDecls to represent struct members, and MemberRefExprs to represent member access in structs. Eliminate the horrible hack which was LookThroughOneOfExpr.
Swift SVN r1808
2012-05-11 03:08:15 +00:00
Doug Gregor
70bfc235b8 Implement protocol inheritance, e.g.,
protocol Document { var title : String }
  protocol Versioning { func bumpVersion() }
  protocol VersionedDocument : Document, Versioning { }

This commit covers the basic functionality of protocol inheritance, including:
  - Parsing & AST representation
  - Conforming to a protocol also requires conforming to its inherited
  protocols
  - Member lookup into a protocol also looks into its inherited
  protocols (results are aggregated; there is no name hiding)
  - Teach ErasureExpr to maintain lvalueness, so we don't end up
  performing a silly load/erase/materialize dance when accessing
  members from an inherited protocol.




Swift SVN r1804
2012-05-11 00:00:50 +00:00
Eli Friedman
77fa49ec2b Introduce StructDecl and StructType; use them for structs instead of OneOfDecl/OneOfType. To keep this patch small, I'm leaving in LookThroughOneOf etc. for the moment, but that's next on the agenda.
Swift SVN r1780
2012-05-09 00:27:44 +00:00
Eli Friedman
4ca443d0f5 Add OneOfDecl; use it as a DeclContext for OneOfElementDecls instead of OneOfType, and get rid of the implicit TypeAliasDecl. Add TypeDecl as a base class for OneOfDecl and TypeAliasDecl (and StructDecl, once we have it). Adjust a whole bunch of stuff to this new scheme.
Swift SVN r1774
2012-05-08 21:09:42 +00:00
Chris Lattner
79cf8ed4fb add a fixme explaining what is going on.
Swift SVN r1760
2012-05-05 23:41:55 +00:00
Chris Lattner
d77d0d3fe3 refactor SourceRange printing out of Verifier.cpp into methods on SourceRange and SourceLoc.
Swift SVN r1752
2012-05-05 21:08:44 +00:00
Chris Lattner
2f44c0038c Initial stab at implementing string literal interpolation for simple expressions,
e.g. "foo is \(i+j)".  This implements rdar://11223686

Doug implemented all the hard parts of this.  I ripped out support for nested string
literals (i.e. string literals within an interpolated string), which simplified the
approach and defined away some problems with his patch in progress.  I plan a few refinements
on top of this basic patch.



Swift SVN r1738
2012-05-04 05:53:50 +00:00
Doug Gregor
95cdc20902 Member expressions (which refer to properties) require their bases to
be lvalues (materializing them if necessary), as we do with subscript
expressions. Introduce a verifier for member expressions to make sure
this property holds.

Note that there is a slight regression in semantic analysis for
assignments to properties, because we no longer detect some cases of
writing into a property that should not be set. The general solution
to the problem is covered by <rdar://problem/11259972>.



Swift SVN r1530
2012-04-20 15:48:39 +00:00
Doug Gregor
59fab3f97c Teach the subscript expression verifier to check the type of the
subscript expression itself.


Swift SVN r1529
2012-04-20 15:35:30 +00:00