Commit Graph

287 Commits

Author SHA1 Message Date
Slava Pestov
c5edc4574d ASTScope: PatternEntryDeclScope changes the insertion point for local bindings
This gives us the desired behavior, where local bindings are in
scope after their definition.

Note that BraceStmt still introduces all bindings at the beginning,
but now we change BraceStmt to only introduce local functions and
types, allowing us to disable parse-time lookup.
2020-09-23 22:37:00 -04:00
Slava Pestov
7c43e1010f ASTScope: Remove getEnclosingAbstractStorageDecl() 2020-09-23 13:09:01 -04:00
Slava Pestov
32957259e8 ASTScope: Remove VarDeclScope 2020-09-23 13:09:01 -04:00
Slava Pestov
de08e25ddf ASTScope: Remove AbstractPatternEntryScope::isLastEntry() 2020-09-23 13:09:00 -04:00
Slava Pestov
9119f736ed ASTScope: PatternEntryInitializerScope does not change the insertion point 2020-09-23 13:09:00 -04:00
Slava Pestov
f1d2db5eb4 ASTScope: ParameterListScope does not change the insertion point 2020-09-23 13:09:00 -04:00
Slava Pestov
fd53040932 ASTScope: Refactor DeclAttribute scopes
DifferentiableAttr, SpecializeAttr and CustomAttr create scopes
because lookups can be performed from inside them. Previously
we would sort DifferentiableAttr and SpecializeAttr by source
order, but we consider that a declaration may also have more
than one _kind_ of attribute requiring special handling.

The case where this comes up is properties that have both a
DifferentiableAttr and CustomAttr. This fixes test failures in
AutoDiff tests with subsequent commits in this PR.
2020-09-23 13:09:00 -04:00
Slava Pestov
87593fc0be ASTScope: Fold AbstractFunctionBodyScope into FunctionBodyScope 2020-09-23 13:09:00 -04:00
Slava Pestov
62764bb9ef ASTScope: Assert that we don't re-expand 2020-09-23 01:26:06 -04:00
Slava Pestov
eedcc6da0e ASTScope: Remove re-expansion mechanism 2020-09-23 01:26:06 -04:00
Slava Pestov
2e5e94ff51 ASTScope: SourceFile scopes are always current 2020-09-23 01:26:06 -04:00
Slava Pestov
b670453bd8 ASTScope: PatternBindingEntry scopes are always current 2020-09-23 01:26:06 -04:00
Slava Pestov
d3d1ceec58 ASTScope: AbstractFunctionBodyScope scopes are always current 2020-09-23 01:26:06 -04:00
Slava Pestov
8f586d011c ASTScope: TopLevelCodeDecl scopes are always current 2020-09-23 01:26:06 -04:00
Slava Pestov
d664f5d905 ASTScope: IterableDeclContext bodies are always current 2020-09-23 01:26:06 -04:00
Slava Pestov
c9f2060e54 ASTScope: Remove shouldThisNodeBeScopedWhenFoundInSourceFileBraceStmtOrType() 2020-09-22 13:42:48 -04:00
Slava Pestov
753d303c73 ASTScope: Remove expandIfConfigClauses()
We're always guaranteed to visit the elements of the active clause
as members of the parent AST node.
2020-09-22 13:42:47 -04:00
Slava Pestov
76802dd635 ASTScope: FunctionBodyScope should not be nested inside ParameterListScope 2020-09-22 13:42:47 -04:00
Slava Pestov
c662c10d1e ASTScope: Use visitParsedAccessors() instead of getAllAccessors() 2020-09-22 02:06:32 -04:00
Slava Pestov
85ae227465 ASTScope: Remove hacks for PatternBindingDecls with invalid source ranges 2020-09-22 02:06:32 -04:00
Slava Pestov
9b851bf8cb ASTScope: Collapse PureFunctionBodyScope and MethodBodyScope
This centralizes some invariants around the 'self' parameter.
While all ConstructorDecls and DestructorDecls have a 'self',
even if they're invalid because they're not nested inside a type,
we don't want to consider this as the base 'self' for lookups.

Eg, consider this invalid code:

class C {
  func f() {
    init() {
      x
    }
  }
}

The base for the lookup should be f.self, not f.init.self.
2020-09-18 15:05:48 -04:00
Slava Pestov
38883ce100 ASTScope: Remove unused shouldCreateAccessorScope() method 2020-09-18 03:00:09 -04:00
Slava Pestov
3801d16d6f ASTScope: Simplify representation of closures
Let's use a ClosureParametersScope for all closures, even those
without an 'in' keyword. This eliminates the need for the
ClosureBodyScope and WholeClosureScope.

Also, let's move the lookup of capture list bindings from
CaptureParametersScope to CaptureListScope. This eliminates the
need for CaptureParametersScope to store a reference to the
capture list, which allows us to remove the AbstractClosureScope
base class entirely.
2020-09-17 14:46:47 -04:00
Slava Pestov
d260fbb9b1 ASTScope: More accurate modeling of 'case' statements
In a code snippet like the following,

  static func ==(a: Foo, b: Foo) -> Bool {
    switch (a, b) {
    case (.x(let aa), .x(let bb)) where condition(aa, bb),
         (.y(let aa), .y(let bb)) where condition(aa, bb):
      return aa == bb
    default:
      return false
    }
  }

The CaseStmt defines two patterns, both of which bind
'aa' and 'bb'. The first 'aa'/'bb' are in scope inside the
first 'where' clause, and the second 'aa'/'bb' are in scope
inside the second 'where' clause.

Furthermore, the parser creates a "fake" VarDecl for
'aa' and 'bb' to represent the phi node merging the two
values along the two control flow paths; these are in scope
inside the body.

Model this situation by introducing a new CaseLabelItemScope
for the 'where' clauses, and a CaseStmtBodyScope for the
body.
2020-09-16 00:21:13 -04:00
Slava Pestov
713b57becb Frontend: Remove -lazy-astscopes flag 2020-09-05 01:16:21 -04:00
Slava Pestov
0310a701d9 AST: Remove EnableASTScope flag and force it to always be on 2020-09-04 16:15:36 -04:00
Rintaro Ishizaki
66531729eb [ASTScope] Remove includeInactiveIfConfigClauses flag
This flag has never been enabled.
Now that, Parser treats IfConfig block containing CC token as "active",
so code completion doesn't lookup from inactive blocks.
2020-08-14 10:33:53 -07:00
Rintaro Ishizaki
513fed7d8c [CodeCompletion] Enable ASTScope in code completion
* Re-create `ASTScope` for each completion
* Add generic params and where clause scope even without missing body
* Use `getOriginalBodySourceRange()` for `AbstractFunctionBodyScope`
* Source range translations for replaced ranges when finding scopes
* Bypass source range checks when the completion happens in the replaced
  range
* Be lenient with ASTScope / DeclContext mismatch in code completion
2020-08-12 16:36:09 -07:00
Anthony Latsis
78f0259bfb Merge pull request #33283 from AnthonyLatsis/type-locura-2
ASTWalker: Offload TypeLoc
2020-08-05 00:17:33 +03:00
Anthony Latsis
b26310ee97 TypeLoc: Offload TypeLoc off the ASTWalker 2020-08-04 18:13:28 +03:00
Doug Gregor
6c959af579 [ASTScope] Model "do" statements in the scope map 2020-07-30 22:18:27 -07:00
Slava Pestov
14dd4eef2d AST: Add a way to distinguish a parsed generic parameter list from a synthesized one
Add a new GenericContext::getParsedGenericParams(). This produces
the same value as GenericContext::getGenericParams() if the generic
parameter list was written in source. For extensions and protocols,
this returns nullptr without synthesizing anything.
2020-07-28 02:07:16 -04:00
Owen Voorhees
45bc578ae5 [SourceManager] Rename line and column APIs for clarity 2020-05-21 12:54:07 -05:00
Slava Pestov
3ee2409890 SIL: Use ASTScope for SIL parsing 2020-05-08 00:31:23 -04:00
Varun Gandhi
65577940d0 [NFC] Get rid of -Wrange-loop-analysis warnings. (#31324) 2020-04-27 09:47:52 -07:00
David Ungar
5d5776b64d Tolerate non-empty braces in braces 2020-04-10 18:15:12 -07:00
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00
Robert Widmann
987cd55f50 [NFC] Drop llvm::Expected from Evaluation Points
A request is intended to be a pure function of its inputs. That function could, in theory, fail. In practice, there were basically no requests taking advantage of this ability - the few that were using it to explicitly detect cycles can just return reasonable defaults instead of forwarding the error on up the stack.

This is because cycles are checked by *the Evaluator*, and are unwound by the Evaluator.

Therefore, restore the idea that the evaluate functions are themselves pure, but keep the idea that *evaluation* of those requests may fail. This model enables the best of both worlds: we not only keep the evaluator flexible enough to handle future use cases like cancellation and diagnostic invalidation, but also request-based dependencies using the values computed at the evaluation points. These aforementioned use cases would use the llvm::Expected interface and the regular evaluation-point interface respectively.
2020-03-26 23:08:02 -07:00
Robert Widmann
d2360d2e8c [Gardening] dyn_cast -> isa 2020-02-07 16:09:31 -08:00
Dan Zheng
6c29939f59 [AutoDiff upstream] Add @differentiable ASTScope support. (#29171)
`@differentiable` attributes may contain `where` clauses referencing generic
parameters from some generic context, just like `@_specialize` attributes.

Without special ASTScope support for `@differentiable` attributes,
ASTScopeLookup.cpp logic tries to resolve the generic parameter `DeclName`s in
`where` clauses based on source location alone
(`ASTScopeImpl::findChildContaining`) and fails.

The fix is to add a special `DifferentiableAttributeScope`, mimicking
`SpecializeAttributeScope`. Every `@differentiable` attribute has its own scope,
derived from the declaration on which it is declared. Unlike `@_specialize`,
`@differentiable` may also be declared on `AbstractStorageDecl` declarations
(subscripts and variables).

Upstreams https://github.com/apple/swift/pull/27451.

Progress towards TF-828: upstream `@differentiable` attribute type-checking.
2020-01-13 17:48:44 -08:00
Robert Widmann
96b3b9f0f4 [NFC] Hide SourceFile::Decls
In preparation for installing some stable paths infrastructure here,
hide access to the array of top-level decls.
2020-01-03 14:14:00 -08:00
Hamish Knight
01d5c00f9b [Sema] Requestify default arg type checking
This commit introduces a request to type-check a
default argument expression and splits
`getDefaultValue` into 2 accessors:

- `getStructuralDefaultExpr` which retrieves the
potentially un-type-checked default argument
expression.

- `getTypeCheckedDefaultExpr` which retrieves a
fully type-checked default argument expression.

In addition, this commit adds `hasDefaultExpr`,
which allows checking for a default expr without
kicking off a request.
2019-11-11 13:49:06 -08:00
David Ungar
3e4a7f99c8 Add assertion to ensure ASTScope enabled when expanding. 2019-11-06 10:37:06 -08:00
David Ungar
15846cf71a Only do expansion if source file is suitable. 2019-11-06 10:37:06 -08:00
David Ungar
87799339c7 Eagerly expand function bodies before type-checking them, which scrambles them. 2019-11-06 10:36:14 -08:00
David Ungar
0838a6646d Provide hook to see if ASTScopes support lookups into inactive clauses 2019-11-05 12:10:43 -08:00
David Ungar
f162a84e01 Factoring 2019-11-02 20:44:03 -07:00
David Ungar
b263e45441 Putative ordering fix. 2019-11-02 13:53:05 -07:00
Brent Royal-Gordon
17169fc1fe Merge pull request #27950 from brentdax/dumpster-fire
[NFC] Standardize dump() methods in frontend
2019-10-31 20:36:26 -07:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00