Commit Graph

217 Commits

Author SHA1 Message Date
Hamish Knight
01a082a058 [AST] Adopt ArgumentList
Switch out the representation of argument lists
in various AST nodes with ArgumentList.
2021-09-01 18:40:23 +01:00
Becca Royal-Gordon
59bb325e4b [NFC] Factor out ASTContext operator news
Many, many, many types in the Swift compiler are intended to only be allocated in the ASTContext. We have previously implemented this by writing several `operator new` and `operator delete` implementations into these types. Factor those out into a new base class instead.
2021-08-19 11:19:52 -07:00
Robert Widmann
d52b33d409 Special-case Pattern Binding Decls Created by LLDB
When LLDB wraps a user-defined expression in the REPL, it takes something like this
```
<expr>
```

and turns it into (very very abstractly)

```
var result
do {
  result = <expr>
}
print(result)
```

In the process, it creates an implicit pattern binding and an implicit do block. Of these, only the implicit do is considered by ASTScope lookup to be relevant. This presents a problem when <expr> is or contains a closure, as the parameters of that closure are defined within a scope that will never be expanded. Thus,

```
> [42].map { x in x } // <- cannot find 'x' in scope
```

This patch provides the Swift half of the fix wherein we privilege pattern bindings created by the debugger and look through them to the underlying user expression when performing ASTScope expansion.

rdar://78256873
2021-06-01 22:25:17 -07:00
Holly Borla
97ecb9435f [ASTScope] Always skip implicit attributes.
ASTScope only cares about attributes when lookup can be done inside of the
attribute, which isn't the case for implicit attributes because they're
typically built fully type-checked. This also avoids a crash when an
implicit attribute does not have a source range.
2021-04-06 16:38:46 -07:00
zoecarver
a5c7ab14c4 [nfc] Add comment describing when there may not be a parent source file.
Add a comment explaining that C++ function wont have an attached parent
source file. Refs #35311.
2021-01-11 20:54:34 -08:00
zoecarver
6bc2696e24 [cxx-interop] Inline constexpr vars.
If a static variable can be evaluated at compile time, create an
accessor using that value. This means static variables will always be
inlined and removed.

Note: currently this only works with numeric types.
2021-01-08 14:22:25 -08:00
Slava Pestov
b1f9c10d07 ASTScope: Small optimization to ASTScopeImpl memory layout
Combine the wasExpanded flag with the parent pointer, and remove
the haveAddedCleanup flag since it's no longer necessary now that
"re-expansion" is gone.
2020-12-18 15:43:33 -05:00
Slava Pestov
0fa84c14a0 ASTScope: Remove the ExpandASTScopeRequest
This doesn't really fit the request evaluator model since the
result of evaluating the request is the new insertion point,
but we don't have a way to get the insertion point of an
already-expanded scope.

Instead, let's change the callers of the now-removed
expandAndBeCurrentDetectingRecursion() to instead call
expandAndBeCurrent(), while checking first if the scope was
already expanded.

Also, set the expanded flag before calling expandSpecifically()
from inside expandAndBeCurrent(), to ensure that re-entrant
calls to expandAndBeCurrent() are flagged by the existing
assertion there.

Finally, replace a couple of existing counters, and the
now-gone request counter with a single ASTScopeExpansions
counter to track expansions.
2020-12-18 15:43:33 -05:00
Slava Pestov
6599bef24a ASTScope: Fix assertion with pattern bindings that don't introduce any variables
We would get confused if we saw a PatternBindingDecl where
one entry introduced a binding, and the other did not.

Thanks to @DougGregor for the test case!
2020-11-04 03:12:38 -05:00
Slava Pestov
b13ce50448 Merge pull request #34232 from slavapestov/misc-astscope-cleanups
One last round of ASTScope cleanups
2020-10-10 00:06:11 -04:00
Slava Pestov
ef26ecfb13 ASTScope: Allocate list of local bindings for BraceStmt in the ASTContext
Scopes need to register cleanups for non-trivial ivars in the ASTContext.
Instead of doing that here, let's just change the SmallVectors into
ArrayRefs.

Fixes <rdar://problem/69908937>.
2020-10-08 19:05:21 -04:00
Slava Pestov
885f6ebba3 ASTScope: Rename addChildrenForAllLocalizableAccessorsInSourceOrder() 2020-10-08 16:08:10 -04:00
Slava Pestov
ea9f84f66e ASTScope: Remove addSiblingsToScopeTree() 2020-10-08 16:08:10 -04:00
Slava Pestov
89ea51e90c ASTScope: Remove isLocalizable() 2020-10-08 16:08:10 -04:00
Slava Pestov
c5e1388245 ASTScope: Remove cull() 2020-10-08 16:08:09 -04:00
Slava Pestov
14620a34db ASTScope: Remove source range sorting 2020-10-07 23:28:29 -04:00
Slava Pestov
0c11fad5bd ASTScope: Don't silently drop duplicate AST nodes
Instead, let's rely on the existing source range assertions.
They will fire if we add two nodes with overlapping or
equal source ranges.
2020-10-07 23:28:29 -04:00
Slava Pestov
996100cfd1 ASTScope: Remove old SourceRange machinery 2020-10-07 12:33:58 -04:00
Slava Pestov
771fd6e9d1 ASTScope: Redo assertions to look at CharSourceRanges 2020-10-07 12:33:58 -04:00
Slava Pestov
b8cccb1ef8 ASTScope: Fix SourceFileScope source range
A SourceFile might contain TopLevelCodeDecls with guard statements,
which introduce names until the end of the file, so plumb that
through.
2020-10-07 12:33:30 -04:00
Slava Pestov
dac68ca047 ASTScope: Fix TopLevelCodeScope source range
Top-level code can contain guard statements which introduce
bindings until the end of the parent scope, so plumb that
through.
2020-10-07 12:33:29 -04:00
Slava Pestov
2e67c135fd ASTScope: Rework ConditionalClauseScopes
The top-level scope for a conditional clause with a pattern is now
ConditionalClausePatternUseScope, which introduces the pattern's
bindings.

Since the patterns are not visible in their own initializer, a new
ConditionalClauseInitializerScope is used for the initializer.
While it is nested inside of the ConditionalClausePatternUseScope,
it's lookup parent skips one level, giving us the desired behavior.
2020-10-07 12:33:29 -04:00
Slava Pestov
4f1ab9d8e5 ASTScope: Directly calculate end location of GuardStmtScope and PatternEntryDeclScope
Today, the reported source range of a GuardStmtScope is just that of
the statement itself, ending after the 'else' block. Similarly, a
PatternEntryDeclScope ends immediately after the initializer
expression.

Since these scopes introduce names until the end of the parent
BraceStmt (meaning they change the insertion point), we were
relying on the addition of child scopes to extend the source
range.

While we still need the hack for extending source ranges to deal
with string interpolation, at least in the other cases we can
get rid of it.

Note that this fixes a bug where a GuardStmtScope would end
before an inactive '#if' block if the inactive '#if' block was
the last element of a BraceStmt.
2020-10-02 23:49:59 -04:00
Slava Pestov
8b2cf55b70 ASTScope: Push isLocalBinding down from AbstractPatternEntryScope to PatternEntryDeclScope 2020-10-02 22:12:39 -04:00
Slava Pestov
4648587326 ASTScope: Move sortBySourceRange() and cull() calls out of addSiblingsToScopeTree() 2020-10-02 22:12:39 -04:00
Slava Pestov
3ec4ced57d ASTScope: Don't handle top-level bindings in a SourceFile for now 2020-10-01 23:50:16 -04:00
Slava Pestov
28388384f2 AST: UnqualifiedLookup only finds forward references to outer local bindings when IncludeOuterResults is set
The old behavior was that ASTScope would introduce all VarDecls
defined in a BraceStmt at the beginning of the BraceStmt.

I recently enabled the use of PatternEntryDeclScopes, which
introduce the binding at its actual source location instead of
at the beginning of the parent statement.

This patch now makes use of the new information by having
UnqualifiedLookupFlags::IncludeOuterResults toggle between
the two behaviors. When searching for outer results, we also
consider all VarDecls in a BraceStmt, not just those in scope.

This is implemented by giving AbstractASTScopeDeclConsumer a
new entry point, consumePossiblyNotInScope(). When looking up
into a BraceStmt, all VarDecls are passed in to this entry
point.

The default implementation does nothing, which means that
ASTScope::lookupSingleLocalDecl() now respects source locations
when searching for bindings, just like parse-time lookup.

However, Sema's preCheckExpression() pass, which sets
Flags::IgnoreOuterResults, will continue to find
forward-referenced VarDecls, just as it did with the old
context-based DeclContext lookup.
2020-10-01 23:46:26 -04:00
Slava Pestov
445d747622 AST: Move GenericParamList and friends to GenericParamList.{h,cpp} 2020-09-29 19:51:03 -04:00
Slava Pestov
49e371c563 ASTScope: Remove crossCheckWithAST() 2020-09-25 02:40:13 -04:00
Slava Pestov
5461508cd7 ASTScope: Remove isATypeDeclScope() 2020-09-23 22:37:00 -04:00
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