Commit Graph

794 Commits

Author SHA1 Message Date
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Holly Borla
da8e84caac Merge pull request #66914 from hborla/conformance-macro-in-extension
[Macros] Don't visit macro-generated extensions in `visitAuxiliaryDecls`.
2023-06-26 15:45:03 -07:00
Holly Borla
83030a9c63 [Macros] Don't visit macro-generated extensions in 'visitAuxiliaryDecls'.
Macro-generated extensions are hoisted to file scope, because extensions are
not valid in nested scopes. Callers of 'visitAuxiliaryDecls' assume that the
auxiliary decls are in the same decl context as the original, which is clearly
not the case for extensions, and it leads to issues like visiting extension at
the wrong time during SILGen. The extensions are already added to the top-level
decls, so we don't need to visit them as auxiliary decls, and we can type-check
macro-expanded decls at the end of visitation in TypeCheckDeclPrimary.
2023-06-23 23:25:32 -07:00
Doug Gregor
e306c11a22 [Macros] Ensure that we visit *all* members when emitting memberwise initializer
Fixes rdar://111122261.
2023-06-21 16:12:11 -07:00
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Pavel Yaskevich
6c0e3914b8 [SILGen] InitAccessor: Emit init accessor function
Emit all initializes/accesses properties as arguments
in custom prolog and map them back to the originating
property declarations.
2023-06-06 18:59:13 -07:00
Michael Gottesman
8579c1924d [move-only] Make sure that we serialize deinits and if we are not able to see the value witness function, call it via the value witness function.
Some notes:

1. I put in both a swiftpm like test case and a library evolution test case. I
also updated the moveonly_deinit serialization swift test to show that we
actually serialize the deinit.

2. I changed when we emit the deinit table to only be when we have a type with
an actual value type destructor. Notably this doesn't include classes today so
as a side-effect, we no longer attempt to devirtualize moveonly class deinits.
This doesn't affect anything we are trying to actually do since we do not
support noncopyable classes today. With that in mind, I changed one test that
was showing that deinit devirtualization worked to use a struct with deinit
instead of a class.

rdar://109679168
2023-05-27 22:07:04 -07:00
Holly Borla
ae32673553 [Macros] Fix visiting nested conformance macro declarations in SIL and IRGen.
When expanding a conformance macro, the generated extension decls are added to
the TopLevelDecls vector of the synthesized file unit to expose them to
sf->getSynthesizedFile()->getTopLevelDecls(). There are two problems with this:

  1. These decls are also visited via visitAuxiliaryDecls() for the purpose of
     type checking. This causes problems in code generation because the extensions
     are visited separately, and because SIL and IRGen assume nested auxiliary decls
     are members.
  2. SILGen only emits top-level decls directly from the source file rather than its
     synthesized file. Auxiliary decls are visited here, but this doesn't work for
     nested conformance macros because the attached-to decl is not at the top-level,
     so macro-generated conformances for nested types never emit their descriptor.

To fix this in the short term, visit top-level decls in the synthesized file that are
generated by conformance macros, and skip auxiliary extension decls when emitting type
members. This fix is narrowly scoped to only impact macros, but in the future this
warrants a more structural fix to better handle top-level decls in the synthesized file.
2023-04-17 10:33:25 -07:00
Allan Shortlidge
ada0f09615 SILGen: Implement 'complete' unavailable decl optimization.
Avoid SIL lowering for declarations marked unavailable when
`-unavailable-decl-optimiation=complete` is specified.

Part of rdar://106674022
2023-03-27 11:40:54 -07:00
nate-chandler
af5d611962 Merge pull request #64583 from nate-chandler/rdar79508092
[SILGen] Use decl location for main's functions.
2023-03-23 18:47:24 -07:00
Nate Chandler
8c4a0aed53 [SILGen] Use decl location for main's functions.
Previously, the location used for functions corresponding to @main was
just RegularLocation::getModuleLocation().  Make that more specific by
using the annotated type's location instead.

rdar://79508092
2023-03-23 13:14:39 -07:00
Richard Wei
833338f9ce [Macros] Top-level freestanding macros (#63553)
Allow freestanding macros to be used at top-level.
- Parse top-level `#…` as `MacroExpansionDecl` when we are not in scripting mode.
- Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
- Experimental support for script mode and brace-level declaration macro expansions: When type-checking a `MacroExpansionExpr`, assign it a substitute `MacroExpansionDecl` if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.
2023-03-06 07:15:20 -08:00
Richard Wei
98c2a837d2 [Macros] Always visit macro-produced decls as auxiliary decls
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove `MacroExpansionDecl::getRewritten()`.

Also make `ExpandMacroExpansionDeclRequest` cache the buffer ID (similar to other macros) instead of an array of decls.
2023-03-04 23:48:21 -08:00
Andrew Trick
7f916bafd9 SILGen - verify incomplete OSSA lifetimes
Use DeadEndBlocks to ignore missing end-borrows and destroys.

Allow SILGen to be as sloppy as it wants with OSSA cleanups.
2023-03-01 21:41:46 -08:00
Allan Shortlidge
f1a8740ba5 AST: Only treat @backDeployed functions as fragile on platforms with an active attribute.
Previously, typechecking and SILGen would treat a function body as fragile as long as the declaration had a `@backDeployed` attribute, regardless of the platform specified by the attribute. This was overly conservative since back deployed functions are only emitted into the client on specific platforms. Now a `@backDeployed` function can reference non-`public` declarations on the platforms it is resilient on:

```
@backDeployed(before: iOS 15)
public func foo() {
  #if os(iOS)
  // Fragile; this code may be emitted into the client.
  #else
  // Resilient; this code won't ever be exposed to clients.
  #endif
}
```

Resolves rdar://105298520
2023-02-23 10:39:42 -08:00
Holly Borla
f4b2b60446 [Macros] Enable global peer macros.
Global peer macro expansions are not injected into the AST. Instead, they
are visited as "auxiliary declarations" when needed, such as in the decl
checker and during SILGen. This is the same mechanism used for local property
wrappers and local lazy variables.
2023-02-14 16:24:25 -08:00
Pavel Yaskevich
e0bf2ff854 [SIL/DI] NFC: Remove TypeWrappers feature functionality 2023-02-08 10:14:29 -08:00
Allan Shortlidge
5b942eb18b SILGen/Sema: Add @_backDeploy support for constructors.
Resolves rdar://94436652
2023-01-21 18:04:54 -08:00
Holly Borla
3f462f0f43 [Decl] Add MissingDecl to use for parser recovery. 2023-01-15 09:55:15 -08:00
Richard Wei
f17b7c48bf [Macros] Freestanding declaration macros
Add support for freestanding declaration macros.

- Parse `@declaration` attribute.
- Type check and expand `MacroExpansionDecl`.

Known issues:
- Generic macros are not yet handled.
- Expansion does not work when the parent decl context is `BraceStmt`. Need to parse freestanding declaration macro expansions in `BraceStmt` as `MacroExpansionDecl`, and add expanded decls to name lookup.
2023-01-10 19:09:11 -08:00
Pavel Yaskevich
74fdd58ad1 [SILGen] RuntimeMetadata: Change attribute generator to always produce result indirectly 2022-12-20 09:45:02 -08:00
Pavel Yaskevich
f7f2e3709b [Sema] RuntimeMetadata: Synthesize a body for runtime attribute generator
Single expression is not going to cut it in this case because attribute
and attached declaration could have different availability, so we need
to use `if #available` to guard attribute instantiation and return `nil`
in cases where types are not available.
2022-12-20 09:45:01 -08:00
Pavel Yaskevich
b83b0943b5 [SILDeclRef] RuntimeMetadata: Add a special kind for runtime attribute generator
A new `RuntimeAttributeGenerator` is used to reference runtime
attribute generator functions synthesized by SILGen.
`#function` magic literal points to the declaration that declaration
attribute is attached to.
2022-12-20 09:45:01 -08:00
Pavel Yaskevich
5442d30866 [IRGen] RuntimeMetadata: Emit runtime discoverable attributes section
Each entry relates an attribute declaration to a list of all
declarations it's associated with together with a generator
function to acquire instance of the attribute value.
2022-12-20 09:45:01 -08:00
Pavel Yaskevich
3fb69b3c3c [SIL] SILFunction: Add runtime accessible function attribute
This attribute indicates that the given SILFunction has to be
added to "accessible functions" section and could be looked up
at runtime using a special API.
2022-12-20 09:33:44 -08:00
Allan Shortlidge
95a9310e11 SILGen: Fix 'multiple definitions of symbol' error for functions with @_backDeploy containing defer blocks.
If the emission of a function is delayed via `emitOrDelayFunction()`, then the function is recorded on a list of delayed functions. If the delayed function is later referenced, then the function moves from the delayed list to the "forced" list which will cause it to actually be emitted later. The implementation of `emitOrDelayFunction()` had a bug when called twice for the same delayable function - it would emit that function prematurely if the function moved to the forced list between the two invocations. Later, during forced function emission a multiple definitions of symbol diagnostic would be emitted since the function was not empty.

This issue could be triggered by `@_backDeploy` functions that have auxilary delayable helper functions (e.g. defer blocks). SILGen visits `@_backDeploy` functions twice (once for the copy of the function emitted into the client and once for the resilient copy of the function) so `emitOrDelayFunction()` is called twice for each of the helper functions and the helper functions could become referenced between the two calls.

The fix is to check for an existing entry in the forced functions list before delaying or emitting a delayable function.

Resolves rdar://102909684
2022-12-07 20:51:39 -08:00
Steven Wu
a98e830b0a [Profile] Update swift after LLVM API update
Related to rdar://102581439
2022-12-02 15:12:40 -08:00
Pavel Yaskevich
77fb90ee19 [Sema/SILGen] Add asserts to getParameterAt call sites that expect non-null parameter 2022-11-17 11:52:00 -08:00
Doug Gregor
5ab6b72604 [Macros] Turn Macro into a declaration node.
Although the declaration of macros doesn't appear in Swift source code
that uses macros, they still operate as declarations within the
language. Rework `Macro` as `MacroDecl`, a generic value declaration,
which appropriate models its place in the language.

The vast majority of this change is in extending all of the various
switches on declaration kinds to account for macros.
2022-11-13 12:21:29 -08:00
Michael Gottesman
1084d892bd Merge pull request #62021 from gottesmm/pr-88e3b82d31a08823ec6704d7d190b78b233ac27e
[move-only] Two more small fixes
2022-11-10 07:07:14 -08:00
swift-ci
fd6fd76dae Merge pull request #42513 from jsoref/spelling-silgen
Spelling silgen
2022-11-09 23:28:43 -08:00
Michael Gottesman
4fcc60e794 [move-only] When emitting a module, do not emit the body of the deinit of a move only type.
rdar://102173152
2022-11-09 19:40:04 -08:00
Josh Soref
9a6bf46c0f Spelling silgen
* actually
* arbitrary
* cargo-culted
* clazz
* constrained
* continuation
* coordinator
* coroutine
* derivative
* destroyer
* given
* have
* imported
* initialization
* items
* necessarily
* occurring
* omitting
* overridden
* parameter
* possible
* predecessor
* preparation
* resilience
* should
* struct
* that
* the
* throwing
* unexpectedly
* uniqueness
* using
* value
* villain

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2022-11-09 21:44:17 -05:00
Richard Wei
56e7cce809 [Macros] Parse MacroExpansionExpr and MacroExpansionDecl
Introduce `MacroExpansionExpr` and `MacroExpansionDecl` and plumb it through. Parse them in roughly the same way we parse `ObjectLiteralExpr`.

The syntax is gated under `-enable-experimental-feature Macros`.
2022-10-21 01:50:35 -07:00
Becca Royal-Gordon
ad359fa753 Give @_objcImpl classes implicit deinits 2022-10-18 17:21:56 -07:00
Hamish Knight
350e28b4b7 [Profiler] Rework profiling of top-level code
Instead of creating and destroying a SILProfiler
per TopLevelCodeDecl, setup a single profiler
for the top-level entry point function, and visit
all the TopLevelCodeDecls when mapping regions.
2022-10-14 17:45:14 +01:00
Hamish Knight
7529346a0f [Profiler] Remove unnecessary createProfiler call
We never end up creating the profiler for a
backing initializer from projected value, as it
has no user-written code.
2022-10-14 17:45:13 +01:00
Hamish Knight
d1e2ac15d4 Factor out SILDeclRef::getInitializationExpr 2022-10-14 17:45:12 +01:00
Holly Borla
c4b946195e [AST] Replace the "type sequence" terminology with "parameter pack". 2022-10-10 16:28:13 -07:00
Hamish Knight
51946b16d5 [Profiler] Use proper SILDeclRef for top-level code
Use a main entry-point instead of a null
SILDeclRef. Eventually we'll want to unify the
emission here such that we visit all the
TopLevelDecls in one shot (and just use a single
profiler), but for now we can just hand the
SILProfiler the expected SILDeclRef.
2022-10-07 13:54:52 +01:00
Pavel Yaskevich
99cfbf7dc0 [SILGen] TypeWrappers: switch to emitFunctionDefinition for forced memberwise/default inits 2022-09-30 13:06:09 -07:00
Pavel Yaskevich
8d39e525c8 [SILGen] TypeWrappers: Make sure that default init of $Storage is always emitted
Although it might not be used from user-defined code directly
it's still required for injected `self.$_storage = ...` performed
by DI.
2022-09-29 20:50:37 -07:00
Pavel Yaskevich
862c9d4f83 [SILGen] TypeWrappers: Make sure that memberwise init of $Storage is always emitted
Although it might not be used from user-defined code directly
it's still required for injected `self.$_storage = ...` performed
by DI.
2022-09-29 20:50:37 -07:00
Hamish Knight
0eba486442 [SILGen] Remove setUpForProfiling
Instead, setup the profilers when emitting
function definitions. This is consistent with
what we do for all the other kinds of SILDeclRef.
2022-09-29 10:08:17 +01:00
Hamish Knight
5b1ca3f0e0 [SILGen] Avoid forcing function bodies for profiling
This is no longer necessary now that we ensure we
always SILGen functions that contain user code.
2022-09-29 10:08:17 +01:00
Hamish Knight
8bc80c146a [SILGen] Avoid delaying functions with user-written code
Previously we would delay the emission of
lazy variable getters and stored property
initializers for property wrapper backing storage.
This could lead to their definitions being dropped
if unused, meaning that we wouldn't run the
mandatory diagnostics passes over them.

Fix the logic such that we consider such cases as
having user-written code, and account for a couple
of cases where we can delay emission where we
didn't previously. There are more cases we can
handle here, but I'm leaving that as future work
for now, as `emitOrDelayFunction` is currently
only used for a handful of SILDeclRef kinds.

This is a source breaking change, but only for
invalid (albeit unused) code.

rdar://99962285
2022-09-28 16:11:12 +01:00
Hamish Knight
9f93bc8dcd [Profiler] Emit increment for property wrapper backing initializers
Previously we were creating a SILProfiler for
such functions, but weren't actually emitting the
increment, leading to missed coverage.

Part of the fix for rdar://99931619
2022-09-26 12:18:44 +01:00
Michael Gottesman
c54acc83e2 [move-only] Add the ability to specify a deinit at the SIL level for a move only type.
Specifically, we get an additional table like thing called sil_moveonlydeinit. It looks as follows:

sil_moveonlydeinit TYPE {
  @FUNC_NAME
}

It always has a single entry.
2022-09-20 15:19:31 -07:00
Michael Gottesman
9a8af70f1d [move-only] Teach SILGen how to emit deinits for nominal non-class move only types.
Even though with this change we emit the deinit, it isn't used yet since we
still need to implement the move only deinit table/teach the checker how to call
these/teach IRGen how to call this from the destroying value witness.
2022-09-20 15:19:31 -07:00
Hamish Knight
20830cc2fb [Profiler] Avoid profiling unavailable decls
Such decls don't provide useful coverage info.

rdar://83253091
2022-09-19 10:06:36 +01:00