Commit Graph

937 Commits

Author SHA1 Message Date
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
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
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
Joe Groff
f2500d79b7 Sema: Allow dynamic casts from generics to concrete types.
Open us 'a as! T' to allow dynamic casts from archetypes to archetypes, archetypes to concrete types, existentials to archetypes, and existentials to concrete types. When the type-checker finds these cases, generate new Unchecked*To*Expr node types for each case.

We don't yet check whether the target type actually makes sense with the constraints of the archetype or existential, nor do we implement the SILGen/IRGen backends for these operations. We also don't extend 'x is T' to query the new operation kinds. There's a better factoring that would allow 'as!' and 'is' to share more code. For now, I want to make sure 'x as! T' continues to work for ObjC APIs when we flip the switch to import protocol types.

Swift SVN r5611
2013-06-16 21:54:13 +00:00
Joe Groff
3e44152023 Replace Unsequenced* placeholders with partial AssignExpr/IfExpr nodes.
We can save some source code noise and ASTContext allocation traffic by representing unsequenced assignments and ternaries using AssignExpr/IfExpr with the left and right subnodes nulled out, filling them in during sequence folding.

Swift SVN r5509
2013-06-07 16:49:57 +00:00
Joe Groff
6dcf8ae206 Parse assignments as part of expr-sequence.
Parse '=' as a binary operator with fixed precedence, parsing it into a temporary UnsequencedAssignExpr that gets matched to operands and turned into an AssignExpr during sequence expr folding. This makes '=' behave like library-defined assignment-like binary operators.

This temporarily puts '=' at the wrong precedence relative to 'as' and 'is', until 'as' and 'is' can be integrated into sequence parsing as well.

Swift SVN r5508
2013-06-07 16:15:40 +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
Joe Groff
2606b7ca57 Simplify handling of ternaries in SequenceExprs.
Instead of trying to parse '?' and ':' as separate placeholder exprs and matching them up during binary expr resolution, it's a bit cleaner to parse the entire '? ... :' middle expr of the ternary into a single placeholder node at parse time. Then binary expr resolution only ever has to consider a single sequence element.

Swift SVN r5499
2013-06-06 22:18:48 +00:00
Doug Gregor
dea2ad1932 Zap OverloadedSubscriptExpr.
Swift SVN r5414
2013-05-30 20:50:22 +00:00
Doug Gregor
e06f54273a Zap ExplicitClosureExpr; it was only used in the old type checker.
Swift SVN r5388
2013-05-29 22:25:14 +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
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
59ef7ca5ee Don't use source location information to distinguish single-expression closures.
Because we synthesize AST nodes fairly often, and those synthesized
AST nodes rarely have useful source-location information, we shouldn't
be using the validity of source locations to describe the AST. In the
case of closures, use a bit instead. No functionality change.


Swift SVN r5205
2013-05-17 17:27:34 +00:00
Doug Gregor
ce3fe3ae92 Implement Ruby-inspired closure syntax.
This commit implements closure syntax that places the (optional)
parameter list in pipes within the curly braces of a closure. This
syntax "slides" well from very simple closures with anonymous
arguments, e.g.,

  sort(array, {$1 > $0})

to naming the arguments

  sort(array, {|x, y| x > y})

to adding a return type and/or parameter types

  sort(array, {|x : String, y : String| -> Bool x > y})

and with multiple statements in the body:

  sort(array, {|x, y|
    print("Comparing \(x) and \(y)\n")
    return x > y
  })

When the body contains only a single expression, that expression
participates in type inference with its enclosing expression, which
allows one to type-check, e.g.,

  map(strings, {|x| x.toUpper()})

without context. If one has multiple statements, however, one will
need to provide additional type information either with context

  strings = map(strings, {
    return $0.toUpper()
  })

or via annotations

  map(strings, {|x| -> String 
    return x.toUpper()
  }

because we don't perform inter-statement type inference.

The new closure expressions are only available with the new type
checker, where they completely displace the existing { $0 + $1 }
anonymous closures. 'func' expressions remain unchanged.

The tiny test changes (in SIL output and the constraint-checker test)
are due to the PipeClosureExpr AST storing anonymous closure arguments
($0, $1, etc.) within a pattern in the AST. It's far cleaner to
implement this way.

The testing here is still fairly light. In particular, we need better
testing of parser recovery, name lookup for closures with local types,
more deduction scenarios, and multi-statement closures (which don't
get exercised beyond the unit tests).



Swift SVN r5169
2013-05-14 05:17:10 +00:00
Joe Groff
e1c838962e Revert "Remove [objc_block] attribute from Swift type system."
Implementing SIL bridging is going to take more IRGen work than I anticipated.

Swift SVN r5113
2013-05-09 16:32:18 +00:00
Joe Groff
38f13e56f5 Remove [objc_block] attribute from Swift type system.
We will handle Swift-function-to-ObjC-block bridging in SILGen as part of general Cocoa-to-Swift type bridging. Temporarily disable building swiftAppKit and tests that exercise block bridging until the new implementation lands.

Swift SVN r5090
2013-05-08 16:52:12 +00:00
Doug Gregor
1a1d08a159 Use a comma-separated list for dumping captures.
Swift SVN r5048
2013-05-06 15:32:04 +00:00
Doug Gregor
7c2f86d948 Prohibit capture of [byref] parameters.
There is a special-case hack to allow capture of 'this', which is
implicitly [byref] for structs. At the moment, most of the cases where
this hack is necessary are [auto_closure] parameters (for assertions
and &&/||).



Swift SVN r5047
2013-05-06 14:54:22 +00:00
Joe Groff
f79e939460 SIL: Sever load-bearing links to the AST.
Make IntegerLiteral, FloatLiteral, and StringLiteral own their own copies of their values so they don't depend on the AST. Remove the now-redundant IntegerValueInst, which only existed to be a non-AST-dependent variant of IntegerLiteral.

Swift SVN r5045
2013-05-06 03:08:58 +00:00
Joe Groff
02d6a04b34 Remove 'new' syntax for class constructors.
Fixes <rdar://problem/13723781>.

T(x) still has some lingering conversion behavior, so there's a type-checking ambiguity in classes that are constructible from super- or subclasses, like stdlib's File is from VFSObject. I cheesed around this for now by using keywords in the constructor forms that have ambiguities. This issue should go away when we finish making T(x) mean only construction.

Swift SVN r5002
2013-04-30 23:10:53 +00:00
Doug Gregor
80b7001cea Implement support for member initializers.
One can now attach an initializer to a member variable. That value
will used as the default initialization for the member variable(s) it
initializes. Fixes <rdar://problem/12597404>.


Swift SVN r4989
2013-04-30 00:32:12 +00:00
Joe Groff
f026e44c18 Integrate ternary parsing with precedence parsing.
Give the ternary a fixed precedence, parse '?' and ':' into SequenceExprs, and fold them into IfExprs as part of sequence folding. This allows assignment operators like '+=' to have precedence below the ternary as in C. Fixes <rdar://problem/13756211>.

Swift SVN r4983
2013-04-29 22:15:52 +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
0f0dd62d52 SILGen: Hook up emission of ZeroValueExprs.
Turn ZeroValueExprs that were created for implicit constructors into SIL BuiltinZeroInsts. Fixes <rdar://problem/13733107>.

Swift SVN r4912
2013-04-25 21:39:12 +00:00
Doug Gregor
a91941b635 Introduce assignments into the implicitly-defined default constructor body.
Add assignment statements into the implicitly-defined default
constructor body to initialize all of the members appropriately, e.g.,
by calling the default constructor. For builtin types and class types,
introduce ZeroValueInitExpr to produce a "zero" value.

ZeroValueInitExpr still needs a representation in SIL. Until then,
actual generation of this AST is suppressed.



Swift SVN r4895
2013-04-25 00:00:28 +00:00
Joe Groff
e7f7df3027 Implement 'x is T' in SILGen.
Add an IsaInst to represent type tests, and implement SILGen for IsSubtypeExpr AST nodes. Get rid of SuperIsArchetypeExpr because it's not really necessary to have it different from IsaSubtype--the SIL and IR behavior is identical.

Swift SVN r4855
2013-04-21 20:33:54 +00:00
Dave Zarzycki
86021078de Fix a think-o with the last commit
The logic was inverted. Need more coffee...

Swift SVN r4679
2013-04-11 17:23:49 +00:00
Dave Zarzycki
698c02ef2e '0123' is decimal, not octal
This fixes:
  <rdar://problem/13571809> '0123' lexes as octal

Swift SVN r4678
2013-04-11 17:20:56 +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
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
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
Joe Groff
340cc73e1e SIL: Track uncurry levels of SILConstants.
Pack the uncurry level onto SILConstant, and modify SILConstant constructors to determine the natural uncurry level of their referenced entity by default. While we're here, improve how SILConstant represents different kinds of constants by using a real enum for the kind. Type closure entry points by giving them an extra curry level for their context. The implementation of ApplyInst and ClosureInst hasn't been updated yet, so tests that lower closures to SIL are temporarily broken.

Swift SVN r4354
2013-03-11 23:17:35 +00:00
Doug Gregor
638d04e536 Start tracking the how individual constraints map back to source expressions in the constraint solver.
We use uniqued "locators" to describe how a particular constraint was
derived, starting from the constraints generated on expressions (e.g.,
the argument to a function application must be convertible to the
input type of the function being applied) and narrowing down to
specific parts of the types (e.g., the result type of tuple element
2).

Note that locators are not present in all constraints yet, nor are
they actually used for anything other than slightly-more-interesting
debug dumps.



Swift SVN r4353
2013-03-11 21:47:43 +00:00
Joe Groff
8fb9a46f95 AST: Wrap super.ctor calls in a RebindThis node.
'super.constructor' shouldn't be referenceable without being called, and 'super.constructor(...)' shouldn't return a value. Require super.constructor expressions to be called at parse time, and wrap the call expression in a new RebindThisInConstructorExpr that represents consuming the delegated-to constructor by using it to reassign 'this'. This should theoretically allow super.constructor to an ill-behaved self-modifying ObjC class to work. It's also necessary to support delegating constructors of value types.

Swift SVN r4326
2013-03-08 00:09:39 +00:00
Joe Groff
c9c0d7e2cc Sema: Set an 'isSuper' flag on ApplyExprs.
SIL or IRGen need to consider the 'super'-ness of an apply when determining its dispatch semantics.

Swift SVN r4307
2013-03-06 19:36:30 +00:00
Joe Groff
f489f2a6fd Clean up AST representation of 'super'.
Replace the more specific Super*RefExpr nodes with a single SuperRefExpr that resolves members of 'this' relative to its superclass. Add an OtherConstructorDeclRefExpr for referring to a constructor as called from another constructor, and use it to represent resolved 'super.constructor' expressions. (It should also be able to represent delegating constructors for free, if we decide we want to add syntax for that.)

Swift SVN r4286
2013-03-05 02:13:49 +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
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
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
Dave Zarzycki
8a56780ab8 Subscription should implicitly tuple the key/index
This now works:

struct no_index {
  subscript () -> Int { return 42 }
  func test() -> Int { return this[] }
}

struct tuple_index {
  subscript (x : Int, y : Int) -> (Int, Int) { return (x, y) }
  func test()  -> (Int, Int) { return this[123, 456] }
  func test2() -> (Int, Int) { return this[y = 123, x = 456] }
}

Swift SVN r3965
2013-02-06 05:47:33 +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
53dd87dab3 Sema: Coercion from (A)->B to [objc_block] (A)->B.
Provide a BridgeToBlockExpr AST node as a temporary representation of func-to-block conversions. In Sema, when we see an [objc_block] type, insert a BridgeToBlock node and coerce the subexpression to the non-block func type.

Swift SVN r3897
2013-01-29 22:38:26 +00:00
Joe Groff
f0e2454202 Sema: Overload resolution for super.constructor.
Implement overload resolution for SuperConstructorRefExprs when a base class has multiple constructors.

Swift SVN r3869
2013-01-25 20:18:11 +00:00
Joe Groff
f40dfce918 IRGen: Implement SuperConstructorRefCallExprs.
Recognize super.constructor calls in IRGen and generate them as calls to the superclass initializing constructor. Note that super.constructor code still won't execute just yet because classic IRGen doesn't generate initializing constructors.

Swift SVN r3859
2013-01-24 21:58:36 +00:00
Joe Groff
e6d3c3e00c Parser: Parse 'super' expressions.
Set up AST nodes for 'super.<identifier>', 'super.constructor', and 'super[<expr>]' expressions, and implement parsing for them without any sema or backend support.

Swift SVN r3847
2013-01-23 21:24:28 +00:00
Doug Gregor
59ab9d1954 Split superclass-to-archetype downcasts into their own expression node.
Swift SVN r3537
2012-12-18 23:48:19 +00:00
Doug Gregor
96583a726b Introduce ArchetypeToSuperExpr for implicit archetype-to-superclass conversions.
The IR generation for this conversion is different from
derived-to-base conversions, because converting from an archetype to
its superclass type means projecting the buffer and then performing
the conversion.


Swift SVN r3462
2012-12-13 00:16:05 +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
96bba4e567 AST: Dump captures in AST dump for CapturingExpr.
Swift SVN r3356
2012-12-05 00:18:23 +00:00