Commit Graph

62 Commits

Author SHA1 Message Date
Chris Miles
f2846ce2cb Fixes crash while instrumenting generic func body embedded in another func.
The crash was caused by attempting to add logging expressions that capture generic variables while using the outer func decl context that was not the generic decl context of the inner (generic) func.

The fix "pushes" the current func decl context while instrumenting the body. Rather than always using the top-level func decl context for all nested func bodies.
2025-01-14 11:19:27 -08:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Hamish Knight
16cfca4186 [ASTWalker] NFC: Rename SkipChildren -> SkipNode
This better describes what the action currently
does, and allows us to re-introduce `SkipChildren`
with the correct behavior.
2024-02-05 15:27:25 +00:00
Hamish Knight
c97d80b1c3 [AST] NFC: Add convenience constructors for ReturnStmt
Add `ReturnStmt::createParsed` and `createImplict`.
2024-01-23 19:30:18 +00:00
Hamish Knight
f4e09c5531 [AST] Tighten up invariants around IfStmt
The 'then' statement must be a BraceStmt, and
the 'else' must either be a BraceStmt or an IfStmt.
2023-12-04 11:09:01 +00:00
Doug Gregor
236418dbf8 Make the "typechecked function body" request more central and resilient
The "typechecked function body" request was defined to type-check a
function body that is known to be present, and not skipped, and would
assert these conditions, requiring its users to check whether a body
was expected. Often, this means that callers would use `getBody()`
instead, which retrieves the underlying value in whatever form it
happens to be, and assume it has been mutated appropriately.

Make the "typechecked function body" request, triggered by
`getTypecheckedBody()`, more resilient and central. A `NULL` result is
now acceptable, signifying that there is no body. Clients will need to
tolerate NULL results.

* When there is no body but should be one, produce an appropriate
error.
* When there shouldn't be a body but is, produce an appropriate error
* Handle skipping of function bodies here, rather than elsewhere.

Over time, we should move clients off of `getBody` and `hasBody`
entirely, and toward `getTypecheckedBody` or some yet-to-be-introduced
forms like `getBodyAsWritten` for the pre-typechecked body.
2023-11-26 09:09:29 -08:00
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Doug Gregor
593c2364e8 [Macros] "Subsume" the initializer when an accessor macros adds non-observers
When an accessor macro adds a non-observing accessor to a property, it
subsumes the initializer. We had previously modeled this as removing
the initializer, but doing so means that the initializer could not be
used for type inference and was lost in the AST.

Explicitly mark the initializer as "subsumed" here, and be more
careful when querying the initializer to distinguish between "the
initializer that was written" and "the initializer that will execute"
in more places. This distinction already existed at the
pattern-binding level, but not at the variable-declaration level.

This is the proper fix for the circular reference issue described in
rdar://108565923 (test case in the prior commit).
2023-04-28 09:50:00 -07:00
Hamish Knight
d56f4633c3 [Sema] Ensure synthesized NamedPatterns have types 2023-04-06 16:11:11 +01:00
Anders Bertelrud
8eb931d617 [Sema] Playground transform should also log function and closure parameter values (#63929)
Add logging of function and closure parameter values when the playground transform is enabled and its "high-performance" mode is off.

MOTIVATION

The goal of the optional "playground transform" step in Sema is to instrument the code by inserting calls to `__builtin_log()` and similar log functions, in order to record the execution of the compiled code. Some IDEs (such as Xcode) enable this transform by passing -playground and provide implementations of the logger functions that record information that can then be shown in the IDE.

The playground transform currently logs variable assignments and return statements, but it doesn't log the input parameters received by functions and closures. Knowing these values can be very useful in order to understand the behaviour of functions and closures.

CHANGES

- add a `ParameterList` parameter to `InstrumenterSupport::transformBraceStmt()`
  - this is an optional parameter list that, if given, specifies the parameters that should be logged at the start of the brace statement
  - this has to be passed into the function because it comes from the owner of the BraceStmt
- adjust `PlaygroundTransform.cpp` to make use of this list
  - the transform will insert calls to `__builtin_log()` for each of the parameters, in order
- adjust `PCMacro.cpp` to accept the parameter, though this instrumenter doesn't currently make use of the new information
- add two new unit tests (one for functions and one for closures)
- adjust four existing unit tests to account for the new log calls

REMARKS

- this is currently guarded by the existing "high performance" option (parameter logging is omitted in that case)

rdar://104974072
2023-03-03 12:37:31 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Erik Eckstein
8dbbfabea5 AST: add debug locations for generated IntegerLiteralExprs 2023-01-02 13:52:21 +01:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Pavel Yaskevich
b7860ea055 [TypeChecker] Split for-in sequence into parsed and type-checked versions 2022-05-30 23:17:41 -07:00
Hamish Knight
47754822c7 [CodeSynthesis] Adopt ArgumentList
Most of this should be fairly mechanical, the
changes in PlaygroundTransform are a little more
involved though.
2021-09-01 18:40:26 +01:00
Slava Pestov
0e276456bd AST: The body of a GuardStmt is always a BraceStmt 2020-10-06 15:39:09 -04:00
Slava Pestov
d7f4b1a1bd AST: Capture list bindings now point back to their parent CaptureListExpr
We'll need this to get the right 'selfDC' when name lookup
finds a 'self' declaration in a capture list, eg

class C {
  func bar() {}
  func foo() {
    _ = { [self] in bar() }
  }
}
2020-09-18 02:59:15 -04:00
Hamish Knight
a7a8d9fd26 [AST] Remove NewBodyKind default
A bunch of callers were accidentally misusing it.
2020-09-04 13:24:34 -07:00
Anthony Latsis
21faa48298 Remove FuncDecl::getBodyResultTypeLoc 2020-08-19 00:09:10 +03:00
Doug Gregor
52591ef982 [Sema] Rename error-handling code to effects-handling code.
The error-handling code is getting generalized for 'async' handling as
well, so rename the various entry points to talk about "effects"
instead.
2020-08-10 13:41:24 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Owen Voorhees
45bc578ae5 [SourceManager] Rename line and column APIs for clarity 2020-05-21 12:54:07 -05:00
Robert Widmann
0342855e50 [NFC] Debride Pattern.h of Implicit Tri-State
Remove all of this in favor of explicit constructors to preserve the one-liners, or distribute the setImplicit() calls to the callsites if necessary.
2020-04-30 22:03:55 -07:00
Varun Gandhi
a1716fe2a6 [Diagnostics] Update compiler diagnostics to use less jargon. (#31315)
Fixes rdar://problem/62375243.
2020-04-28 14:11:39 -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
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
Brent Royal-Gordon
db15d82ffa [NFC] Miscellaneous improvements to Playground and PCMacro transforms 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
36b949c555 [NFC] Improve identifier usage in AST transforms
Change the various AST transforms to use prebuilt DeclName constants more heavily rather than an ad-hoc mix of Identifiers, string literals, std::strings, and StringRefs with questionable memory management.
2019-12-11 00:45:08 -08:00
Robert Widmann
fcf4703c18 Strip PCMacro of its TopLevelContext 2019-11-13 15:28:23 -08:00
Robert Widmann
88ee618a33 Move Vending Top-Level Autoclosure discriminators into ASTContext
This bit has historically survived typechecking and parsing across source files.  Stick it where we stick the other global state.

This also means we don't have to thread TopLevelContext around anymore when invoking high-level typechecking entrypoints.
2019-11-13 15:28:23 -08:00
Hamish Knight
8b9cced36b CheckErrorCoverage doesn't need a TypeChecker 2019-10-22 09:23:33 -07:00
Robert Widmann
742f6b2102 Drastically Simplify VarDecl Validation
This is an amalgam of simplifications to the way VarDecls are checked
and assigned interface types.

First, remove TypeCheckPattern's ability to assign the interface and
contextual types for a given var decl.  Instead, replace it with the
notion of a "naming pattern".  This is the pattern that semantically
binds a given VarDecl into scope, and whose type will be used to compute
the interface type. Note that not all VarDecls have a naming pattern
because they may not be canonical.

Second, remove VarDecl's separate contextual type member, and force the
contextual type to be computed the way it always was: by mapping the
interface type into the parent decl context.

Third, introduce a catch-all diagnostic to properly handle the change in
the way that circularity checking occurs.  This is also motivated by
TypeCheckPattern not being principled about which parts of the AST it
chooses to invalidate, especially the parent pattern and naming patterns
for a given VarDecl.  Once VarDecls are invalidated along with their
parent patterns, a large amount of this diagnostic churn can disappear.
Unfortunately, if this isn't here, we will fail to catch a number of
obviously circular cases and fail to emit a diagnostic.
2019-10-14 12:06:50 -07:00
Jordan Rose
8d7f1b7c5d [AST] Separate SourceFile from FileUnit.h
Like the last commit, SourceFile is used a lot by Parse and Sema, but
less so by the ClangImporter and (de)Serialization. Split it out to
cut down on recompilation times when something changes.

This commit does /not/ split the implementation of SourceFile out of
Module.cpp, which is where most of it lives. That might also be a
reasonable change, but the reason I was reluctant to is because a
number of SourceFile members correspond to the entry points in
ModuleDecl. Someone else can pick this up later if they decide it's a
good idea.

No functionality change.
2019-09-17 17:54:41 -07:00
Jordan Rose
853caa66d4 [AST] Split FileUnit and its subclasses out of Module.h
Most of AST, Parse, and Sema deal with FileUnits regularly, but SIL
and IRGen certainly don't. Split FileUnit out into its own header to
cut down on recompilation times when something changes.

No functionality change.
2019-09-17 17:54:41 -07:00
David Ungar
b38076490b Address review comments re clarity
setOrigInit -> setOriginalInit
2019-08-05 13:42:35 -07:00
Slava Pestov
a532a325e1 AST: Move a few methods from VarDecl down to ParamDecl 2019-07-22 20:19:09 -04:00
Parker Schuh
687ff25157 Convert ForEachStmt to not use tc.callWitness().
For reference, everything else except string interpolation has been converted
not to call this method.
2019-06-28 14:20:42 -07:00
Chris Brough
87e8627f2d [5.1] Upgrade PCMacro/PlaygroundTransform to support module/file IDs
This change PCMacro and PlaygroundTransform to return an a moduleID and
fileID in addition to the source location information. The Frontend has
been changed to run PCMacro and PlaygroundTransform on all input files
instead of the main file only.

The tests have been updated to conform to these changes with an addition
of module and file ID specific tests. The Playgrounds related tests were
adjusted to make a module out of the stub interface files since those
files should not have PCMacro and PlaygroundTransform applied to them.

rdar://problem/50821146
2019-05-28 14:20:59 -07:00
Slava Pestov
c81e525c54 Sema: Clean up top level code handling a little 2019-04-29 10:37:59 -04:00
Doug Gregor
599e07e5d9 [Type checker] Keep the type checker alive as long as the ASTContext is.
It is possible for the SIL optimizers, IRGen, etc. to request information
from the AST that only the type checker can provide, but the type checker
is typically torn down after the “type checking” phase. This can lead to
various crashes late in the compilation cycle.

Keep the type checker instance around as long as the ASTContext is alive
or until someone asks for it to be destroyed.

Fixes SR-285 / rdar://problem/23677338.
2018-10-10 16:44:42 -07:00
Slava Pestov
dffcafbdb7 IRGen: VarDecls always have an interface type and a contextual type 2018-08-14 02:19:31 -07:00
Slava Pestov
4b258e86e6 AST: Stop setting contextual types on ParamDecls
VarDecl::getType() lazily maps the interface type into context if needed.
2018-08-10 13:33:12 -07:00
Slava Pestov
e1da265873 Sema: Remove uses of AbstractFunctionDecl::getParameterLists() 2018-07-19 21:21:17 -07:00
Hamish Knight
893d33128c Add a convenience PatternBindingDecl::createImplicit member and mark some PBDs as implicit (#17441)
Previously, some PBDs weren't being marked implicit even though the associated vars were implicit. PatternBindingDecl::createImplicit will be even nicer when we start parsing the location of the equals token.
2018-07-02 10:42:03 -07:00
Brent Royal-Gordon
6323a5131c Extract helper for making integer literals
Several different places in the codebase synthesize IntegerLiteralExprs from computed unsigned variables; each one requires several lines of code and does things slightly differently. Write one central helper method to handle this.
2018-06-16 12:31:46 -07:00
Connor Wakamo
1d602f2062 Added comments based on review feedback. 2018-01-09 16:11:10 -08:00
Connor Wakamo
2c04058710 [PCMacro] Implemented support for defer statements in the PC macro.
As with the playground transform, defer statements were not supported in the PC macro.
This commit addresses that oversight.

This partially addresses <rdar://problem/29007242>.
2018-01-09 11:21:52 -08:00
Jordan Rose
99cfcf13f3 Transforms: re-check error handling for nested functions as well (#12950)
Since both in-tree AST transforms (playground and program counter) add
new ApplyExprs (and literals that turn into ApplyExprs), we need to
recheck nested function bodies as well as top-level ones.

rdar://problem/28784059
2017-11-16 13:12:34 -08:00