Commit Graph

184 Commits

Author SHA1 Message Date
Jordan Rose
3087e8d5ea Add the notion of "re-exported" modules, and use that where it makes sense.
Rather than automatically re-exporting or not re-exporting every import in
a TranslationUnit, we'll eventually want to control which imports are local
(most of them) and which imports are shared with eventual module loaders.
It's probably not worth implementing this for TranslationUnit, but
LoadedModule can certainly do something here.

Currently, a LoadedModule is even more permissive than a TranslationUnit:
all imports are re-exported. We can lock down on this once we have a
re-export syntax.

Swift SVN r6523
2013-07-23 23:10:17 +00:00
Dmitri Hrybenko
03e6c36676 Code completion: correctly do name lookup into extensions
This change also exposes and fixes a bug where we would improperly filter
shadowed declarations.  removeShadowedDecls() assumed that all decls had the
same name, but when it is used from lookupVisibleDecls(), this is not true.
Thus, we group decls not only by signature, but also by name.


Swift SVN r6460
2013-07-22 18:05:43 +00:00
Dmitri Hrybenko
295a263004 Remove a stale comment
Swift SVN r6397
2013-07-19 23:44:10 +00:00
Dmitri Hrybenko
bc67a82be8 lookupVisibleDecls(): use correct abstractions of lookup kinds
Removes the boolean IsTypeLookup and OnlyInstanceMembers, which were totally
misleading.

This fixes some bugs in lookupVisibleDecls, which allows us to remove
workarounds in code completion, that were not correct in some corner cases.

Now we also code complete references to instance functions in static contexts,
but code completion results in this case state the type of the resulting object
incorrectly.


Swift SVN r6396
2013-07-19 23:42:54 +00:00
Dmitri Hrybenko
44b4b38ac3 Use nullptr instead of '0'
Swift SVN r6381
2013-07-19 18:06:59 +00:00
Dmitri Hrybenko
16229f80a6 Eliminate IsTypeLookup parameter in the implementation of lookupVisibleDecls()
It is not used at all.


Swift SVN r6380
2013-07-19 17:22:34 +00:00
John McCall
d710c0f5f2 Use the type-in-context in the ExtensionDecl for a generic type.
This is a temporary hack until we properly make these carry
their own generic parameters.

This removes the last valid way to get UnboundGenericTypes in
a checked program.

Swift SVN r6373
2013-07-19 03:10:10 +00:00
Dmitri Hrybenko
1ae5699215 Code completion: don't add 'This' and typealiases to code completion results
for protocols when code completing outside the protocol's DeclContext on a
protocol's instance

'This' is obviously not allowed, but references to nested typealiases are
sometimes allowed on protocol instances, but only sometimes (filed
rdar://14489286).  Just don't display such code completion results for now.


Swift SVN r6372
2013-07-19 02:35:21 +00:00
Dmitri Hrybenko
0fc469e80a lookupVisibleDecls(): now we don't find references to generic types outside of
the containing DeclContext

Fixes a few FIXMEs in code completion where we were providing results that
refer to generic arguments outside of their scope.


Swift SVN r6370
2013-07-19 01:46:30 +00:00
Dmitri Hrybenko
e4b44a6bbb Add a 'current module' parameter to lookupVisibleDecls() so that it can do what
is documented: look up members of a type that are visible from a specific
module.  It does not matter much now, but when we have access control, it will.

Before this change, lookupVisibleDecls() tried to guess the *defining* module
of the type, and gave up if it could not.  It forced us to duplicate some
lookup logic for some types (archetypes and protocol compositions).  Also add a
test that lookup finds members of archetype's superclass, which the original
(duplicated) code did not handle.


Swift SVN r6363
2013-07-18 23:32:16 +00:00
Dmitri Hrybenko
d2b324cf63 Code completion: code complete after module names (for example, "swift.#^A^#")
Swift SVN r6354
2013-07-18 22:23:41 +00:00
Dmitri Hrybenko
722cc29cab Factor out DeclContext::getParentModule(). This cleans up a few places where
similar loops were duplicated.

No functionality changes.


Swift SVN r6353
2013-07-18 20:41:58 +00:00
Dmitri Hrybenko
c22ac1dc06 lookupVisibleDecls() should operate on a canonical type so that it does not
need to unwrap the type sugar.

This was uncovered by the removal of IdentifierType and removal of
getCanonicalType() in code completion, which was working around
lookupVisibleDecls() not working for IdentifierTypes.


Swift SVN r6332
2013-07-17 18:28:52 +00:00
Dmitri Hrybenko
fea08cb773 Code completion: add function arguments (including implicit 'this') and generic
arguments to expr-postfix code completion results


Swift SVN r6319
2013-07-17 02:33:54 +00:00
Dmitri Hrybenko
104705c187 Handle ProtocolCompositionType in lookupVisibleDecls()
This enables code completion for 'protocol<...>' types.


Swift SVN r6317
2013-07-17 01:17:35 +00:00
Dmitri Hrybenko
858ce26b2b Code complete expressions that type check to ArchetypeTypes
This required fixing a bug in lookupVisibleDecls(), which did not return any
results for ArchetypeTypes.


Swift SVN r6310
2013-07-16 23:49:25 +00:00
Dmitri Hrybenko
e650f2f8fc Implement basic code completion at the beginning of the expr-postfix
We produce only names that are found by name lookup, no keywords or types yet.


Swift SVN r6276
2013-07-16 00:49:08 +00:00
Dmitri Hrybenko
aa46064432 Pass a const ASTContext and const DeclContext whenever possible. This makes it
possible to use lookupVisibleDecls() with a const DeclContext.


Swift SVN r6274
2013-07-15 23:39:00 +00:00
Joe Groff
3b21e6a201 Add a OneOfElementPattern AST node type.
Add a node for references to oneof cases in patterns. No user-facing functionality change yet.

Swift SVN r6239
2013-07-13 01:21:31 +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
Doug Gregor
187f80cf60 [Name lookup] Introduce ASTContext::lookup() for name lookup into a type.
This lookup routine takes the place of MemberLookup for AST-level
lookups, which don't consider semantics at all and won't be able to
(for example) perform additional type checking to resolve the
lookup. No functionality change.


Swift SVN r5882
2013-06-28 22:08:42 +00:00
Joe Groff
53221db84c AST: Add 'VarPattern' node.
We decided to go with 'var' as a distributive pattern introducer which applies to bare identifiers within the subpattern. For example, 'var (a, b)' and '(var a, var b)' would be equivalent patterns. To model this, give 'var' its own AST node with a subpattern and remove the introducer loc from NamedPattern.

Swift SVN r5824
2013-06-26 23:01:47 +00:00
Joe Groff
e460a01af6 Remove the 'UnresolvedCallPattern' I stubbed out.
I talked to John about parsing patterns today, and because of the magnitude of name-lookup-dependent ambiguities between patterns and expressions, we agreed that at least for a first-pass implementation it makes sense to parse patterns as extensions of the expr grammar and charge name binding with distinguishing patterns from expressions. This gets us out of needing the concept of an "unresolved pattern", at least in the short term.

Swift SVN r5808
2013-06-26 04:23:47 +00:00
Joe Groff
7ba95cfd26 Add AST nodes for refutable patterns.
Introduce Pattern subclasses for the 'is T', 'T(<pattern>)', and '<expr>' pattern syntaxes we'll be introducing for pattern-matching "switch" statements. Also add an 'UnresolvedCalLPattern' to act as an intermediate for name lookup to resolve to a nominal type, oneof element, or function call expression pattern. Since we'll need to be able to rewrite patterns like we do expressions, add setters to AST nodes that contain references to subpatterns. Implement some basic walking logic in places we search patterns for var decls, but punt on any more complex type-checking or SILGen derived from these nodes until we actually use them.

Swift SVN r5780
2013-06-24 17:17:34 +00:00
Dmitri Hrybenko
fdb470fc54 Replace a stale function name in comment with \brief
Swift SVN r5523
2013-06-07 22:23:07 +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
e20338398e Switch visible-declaration lookup to walking extension lists.
No functionality change, but this is a win for performance and
clarity.


Swift SVN r5224
2013-05-20 18:17:25 +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
bfd2f85b5c Parse 'fallthrough' statements.
Create a new FallthroughStmt, which transfers control from a 'case' or 'default' block to the next 'case' or 'default' block within a switch. Implement parsing and sema for FallthroughStmt, which syntactically consists of a single 'fallthrough' keyword. Sema verifies that 'fallthrough' actually appears inside a switch statement and that there is a following case or default block to pass control to.

SILGen/IRGen support forthcoming.

Swift SVN r4653
2013-04-10 17:30:42 +00:00
Chris Lattner
b4fd6dd04a Change TopLevelCodeDecl to allow it to hold a sequence of different exprs and statements in one unit, wrapping them into a BraceStmt. This makes it more similar to other decls (e.g. funcdecl, ctor decls, etc) and will be useful for future sil work.
Unfortunately, this regresses the repl when expressions like (1,2) are entered. This is because the repl is violating some invariants (forming dags out of ASTs, making ASDAG's which upset the type checker).  I'm going to fix this next, but can't bring myself to do it in the same commit.



Swift SVN r4617
2013-04-05 22:33:14 +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
Joe Groff
22c3f0fcae REPL: Scan balanced brackets for context.
Walk backward from a '.' through balanced () [] <> to find context. Fix a problem with parseCompletionContextExpr where UnresolvedIdentifierTypes wasn't getting properly transferred to the TranslationUnit after parsing, causing 'Foo<T>.' completion to blow up.

Swift SVN r4075
2013-02-18 19:32:17 +00:00
Joe Groff
ddb7ead55c REPL: Contextual completions.
If the completion prefix has a '.' behind it, guesstimate a context expression by lexing backward through an identifier(.identifier)* dotted path, then attempt to parse and typecheck that expression to decide on a base type in which to find completions.

Swift SVN r4063
2013-02-16 20:07:50 +00:00
Joe Groff
7a7e3d5615 REPL: Hook up completions to name lookup.
Implement a 'lookupVisibleDecls' API similar to Clang's that replicates the UnqualifiedLookup logic for walking through a given scope looking for decls. Use it to populate the completion list in the repl.

Still to be done: Clang module lookup via Clang's lookupVisibleDecls, and context deduction from dotted path expressions.

Swift SVN r4056
2013-02-15 20:17:44 +00:00