Commit Graph

5638 Commits

Author SHA1 Message Date
Richard Wei
01e4c8df26 [Macros] Use name lookup for lazy declaration macro expansion (#63411)
- Use the name lookup table instead of adding members from a macro expansion to the parent decl context.
- Require declaration macros to specify introduced names and used the declared names to guide macro expansions lazily.
2023-02-07 11:24:42 +08:00
Adrian Prantl
5ff139dd6e Wire up mangled macro names in debug info.
rdar://104894694
2023-02-03 10:30:48 -08:00
Hamish Knight
c87b1b8bef Merge pull request #63022 from hamishknight/express-yourself 2023-02-03 16:40:09 +00:00
Allan Shortlidge
d2524a6de8 AST: Implement parsing support for the accepted spelling of @backDeployed for SE-0376.
For source compatibility `@_backDeploy` continues to be accepted as a spelling.

rdar://102792909
2023-02-01 22:04:33 -08:00
Michael Gottesman
d909c8978e [no-implicit-copy] When accessing a field from a no-implicit-copy struct, unwrap it. Also do not run the move only borrow to destructure transform error.
The reason to do the first is to ensure that when I enable -sil-verify-all, we
do not have errors due to a copy_value of a move only type.

The reason to do the second thing is that:

1. If we have a move only type that is no implicit copy, I am in a subsequent
commit going to emit a type checker error saying that one cannot do this. When
we implement consuming/borrowing bindings/etc, we can make this looser if it
gets into the way.

2. If we have a copyable no implicit copy type, then any structural accesses
that we may want to do that would require a destructure must be to a copyable
type which is ok to copy as long as we do the unwrap from the first thing.

rdar://104929957
2023-02-01 14:48:45 -08:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Meghana Gupta
9dceb378e0 Merge pull request #63259 from meg-gupta/ptrauthaddrdiversified1
Initial support for importing address diversified pointer auth qualified field function pointers
2023-01-27 13:58:44 -08:00
Pavel Yaskevich
f558447362 Merge pull request #63254 from xedin/runtime-attr-#function-behavior
[SILGen] RuntimeMetadata: If attr is attached to a property #function…
2023-01-27 08:01:17 -08:00
Meghana Gupta
1dac5d48d3 Support for address discriminated pointers 2023-01-27 01:56:44 -08:00
nate-chandler
2fc7659ed7 Merge pull request #60670 from nate-chandler/lexical_lifetimes/owned_arguments
[SIL] Maintain owned argument lifetimes at inlining.
2023-01-26 18:30:24 -08:00
Pavel Yaskevich
33a78ad214 [SILGen] RuntimeMetadata: If attr is attached to a property #function should point to it 2023-01-26 17:30:57 -08:00
Allan Shortlidge
8a18fc0fa9 Merge pull request #63229 from tshortli/avoid-unnecessary-back-deploy-thunk
SILGen: Avoid using back deployment thunks for high enough deployment targets
2023-01-26 15:40:43 -08:00
swift-ci
281088eb73 Merge pull request #63204 from meg-gupta/ptrauthcodegenpr
Initial support for ptrauth qualified function pointers in C
2023-01-25 22:19:34 -08:00
Allan Shortlidge
80295fdaa1 SILGen: Avoid using back deployment thunks for high enough deployment targets.
If a function body references a declaration with the `@_backDeploy(before:)` attribute and that function body will only execute on deployment targets for which the ABI version of the decl is available then it is unnecessary to thunk the reference to the decl. Function bodies that may be emitted into other modules (e.g. `@inlinable`) must always use the thunk.

Resolves rdar://90729799
2023-01-25 17:22:23 -08:00
Meghana Gupta
25d83406ad Generate begin_access [signed] when handling a field function pointer with __ptrauth qualifier 2023-01-25 14:03:19 -08:00
Nate Chandler
69d1abe0c5 [SIL] Protect owned arg lifetimes at inlining.
Previously, `begin_borrow [lexical]` were created during SILGen for
@owned arguments.  Such borrows could be deleted if trivially dead,
which was the original reason why @owned arguments were considered
lexical and could not have their destroys hoisted.

Those borrows were however important during inlining because they would
maintain the lifetime of the owned argument.  Unless of course the
borrow scope was trivially dead.  In which case the owned argument's
lifetime would not be maintained.  And if the caller's value was
non-lexical, destroys of the value could be hoisted over deinit
barriers.

Here, during inlining, `move_value [lexical]`s are introduced during
inlining whever the caller's value is non-lexical.  This maintains the
lifetime of the owned argument even after inlining.
2023-01-25 11:36:33 -08:00
Allan Shortlidge
5b942eb18b SILGen/Sema: Add @_backDeploy support for constructors.
Resolves rdar://94436652
2023-01-21 18:04:54 -08:00
Doug Gregor
d192bfc7e5 Merge pull request #63125 from DougGregor/debug-info-dump-macro-expansion-buffers 2023-01-21 07:43:45 -08:00
Adrian Prantl
6335f1dee5 Debug Info: Represent macro expansions as inlined functions.
This allows the debugger to choose whether to display the expanded macro
(inlined) or the original source code (parent frame).

rdar://102916513
2023-01-20 21:43:20 -08:00
Allan Shortlidge
9f54e9cd1f SILGen: Fix double-free of __owned parameters of functions with @_backDeploy.
The following program crashed when compiled with the Swift 5.7 and 5.8 compilers:

```
@available(macOS 12, *)
@_backDeploy(before: macOS 99)
public func foo<T>(_ t: __owned T) {
  print("foo")
}

class C {
  deinit {
    print("deinit")
  }
}

foo(C())
print("done")
```

```
> ./test
foo
deinit
[1]    49162 segmentation fault  ./test
```

The root cause is that generated SIL for the back deployment thunk for `foo(_:)` included its own `destroy_addr` instruction for the value of `t`, but didn't copy the parameter before passing it to the real function implementation which also destroys the value. The fix is to forward ownership of the parameter values to the called function, which causes cleanup generation to be skipped.

Resolves rdar://104436515
2023-01-19 13:12:02 -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
nate-chandler
902f023d95 Merge pull request #62858 from nate-chandler/opaque-values/2/20230104
[OpaqueValues] Build objc thunk args with fn conv.
2023-01-06 07:08:46 -08:00
nate-chandler
191a85d333 Merge pull request #62857 from nate-chandler/opaque-values/1/20230104
[OpaqueValues] Map-into-context back-deploy thunk results.
2023-01-06 07:08:23 -08:00
Nate Chandler
927758f123 [OpaqueValues] Build objc thunk args with fn conv.
Used the function convention to determine whether a SILParameterInfo is
indirect when emitting objc thunk arguments.
2023-01-04 18:17:54 -08:00
Nate Chandler
921c6a0e34 [OpaqueValues] Map back-deploy thunk results.
When a throwing function which returns a generic (this is the simplest
example) is back deployed, it gets a back deploy thunk.  This back
deploy thunk calls one of two variations of the function, depending on
availability, both which have the same signature and in particular both
return a generic.  The result type of that function signature is an
unbound generic.

Previously, that result type was used as is.  Consequently the success
blocks for the try_apply instructions in these thunks had arguments of
unbound generic type.

Here, that type is mapped into the context of the function being
emitted.  The thunks now have the appropriate bound generic type.
2023-01-04 16:11:49 -08:00
Michael Gottesman
ef98a3cfad [borrow-operator] Add initial support for applying _borrow to self when calling a method.
Example: (_borrow f).callFunction()

This still does not support _borrow for get/set and more general accessors. That
change will be coming in a forthcoming commit.

rdar://103888591
2023-01-04 14:32:20 -08:00
Michael Gottesman
b95fe48a90 Merge pull request #62742 from gottesmm/pr-947919228bebf49e02eae5aedb1efee8038c2b4b
[borrow-expr] Add simple support for borrow-expr and wire it up to SILGenApply
2023-01-03 08:04:33 -08:00
Michael Gottesman
94f1391fbe [borrow-expr] Wire up borrow expr to SILGenApply. 2022-12-22 13:02:04 -08:00
Pavel Yaskevich
5b2ce0d663 Merge pull request #62699 from xedin/type-wrapper-fixes
[SILGen/DI] TypeWrapper: Few improvements to synthesis in user-defined initializers
2022-12-20 17:46:20 -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
91042d77aa [SILGen] Make it possible to emit generator function given its body and result type
This would be used for runtime attribute generators that have to
emit `if #available(...)` block in the body which is much easier
to do during Sema.
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
Pavel Yaskevich
4a0f9cac72 [SILGen] TypeWrappers: force emission of wrapper initializer
Make sure that wrapper's initializer is always emitted because
it could be used by DI and IRGen later on to aid in `_storage`
initialization.
2022-12-19 14:03:48 -08:00
Pavel Yaskevich
d1bb5add17 [SILGen] TypeWrappers: Handle initialization of property wrapped fields without init exprs
Type wrapper synthesis cannot reply on `getExecutableInit` to
determine whether initialization should use backing wrapper
call or not, it has to check whether the init expression exists.
2022-12-19 13:48:40 -08:00
Doug Gregor
515715befc Merge pull request #62646 from DougGregor/global-actor-formal-metatype 2022-12-16 17:13:08 -08:00
Doug Gregor
0e8d4c470e [SILGen] Fix formal type of global actor metatype.
Fixes a crash reported in rdar://103255322 .
2022-12-16 12:33:09 -08:00
Holly Borla
d3bc4f52bc Merge pull request #62582 from hborla/pack-element-expr 2022-12-16 11:42:53 -05:00
Michael Gottesman
5fb001cb48 Merge pull request #62610 from gottesmm/pr-2fc1a68995dc3f80805e722e8419a6fcf4eb25fb
[move-only] Teach move checker that captured vars are treated like inout in the relevant closures
2022-12-15 13:46:24 -08:00
nate-chandler
5f3664ef94 Merge pull request #62563 from nate-chandler/opaque-values/1/20221212
[OpaqueValues] Initial key-path support.
2022-12-15 13:14:23 -08:00
Michael Gottesman
4901529a72 [move-only-addr] Teach move only address checker that captured vars should be treated like inout arguments.
rdar://103313357
2022-12-15 09:59:36 -08:00
nate-chandler
b8a3bbb6d4 Merge pull request #62564 from nate-chandler/opaque-values/1/20221213
[OpaqueValues] Fix @in_guaranteed loadable yields.
2022-12-15 07:09:35 -08:00
nate-chandler
1938b5c5a8 Merge pull request #62565 from nate-chandler/opaque-values/2/20221213
[OpaqueValues] Emit addr-only patterns.
2022-12-15 07:08:53 -08:00
Holly Borla
f1fd9acb23 [AST] Add 'PackElementExpr' for explicit pack elements spelled with the 'each'
keyword.
2022-12-14 20:45:51 -08:00
Michael Gottesman
9e44011e4d [sil] Add a new attribute called @closureCaptured to SILFunctionArguments that are closure capture arguments.
I am adding this to make it easy to determine if a SILFunction that is not inout
aliasable is captured. This is useful when emitting certain types of
diagnostics like I need to emit with move only.
2022-12-14 15:16:43 -08:00
Michael Gottesman
f735eda198 Merge pull request #62567 from gottesmm/pr-aff949c3a3e3aaf7839d4462bd96b8eb60c06f97
[move-only-object] Teach move only object verifier how to emit proper errors for closure/defer uses
2022-12-14 13:51:28 -08:00