Commit Graph

937 Commits

Author SHA1 Message Date
Michael Gottesman
373a007efa [send-not-sendable] Eliminate relative includes and add a file header.
Specifically, the two routines we were importing relatively were:

1. TypeChecker::conformsToProtocol. I moved this onto a helper routine on
SILType.

2. swift::findOriginalValueType(Expr *). This routine just looks through various
implicit conversions to find the original underlying type. I moved it to a
helper method on Expr.
2023-08-23 14:38:30 -07:00
Hamish Knight
1e3dd5c50c [AST] Improve source range info for TapExpr
Previously we would only base the start loc on the
`SubExpr`, but that isn't set until CSApply. Change
it to take both `SubExpr` and `Body`'s source range
into account.

Also tighten up the invariant that a TapExpr must
be created with a non-null BraceStmt.
2023-08-08 18:08:51 +01: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
Evan Wilde
83b044f5fb Migrating LLVM API usage on main
This patch migrates the compiler off of the deprecated LLVM APIs where I
can.

 - APInt::getAllOnesValue -> APInt::getAllOnes
 - APInt::getNullValue -> APInt::getZero
 - APInt::isNullValue -> APInt::isZero
 - APInt::getMinSignedBits -> APInt::getSignificantBits
 - clang::Module::submodule_{begin,end} -> clang::Module::submodules
2023-07-13 11:22:35 -07:00
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
Rintaro Ishizaki
86d405bcd0 [Macros] Unify MacroExpansionDecl/MacroExpansionExpr expansion logic
'MacroExpansionDecl' and 'MacroExpansionExpr' have many common methods.
Introduce a common base class 'FreestandingMacroExpansion' that holds
'MacroExpansionInfo'.

Factor out common expansion logic to 'evaluateFreestandingMacro'
function that resembles 'evaluateAttachedMacro'.
2023-06-02 11:54:05 -07:00
Doug Gregor
7386a7e7bd [Macros] Rework the way we assign closure and local discriminators
Setting closure and local discriminators depends on an in-order walk
of the AST. For macros, it was walking into both macro expansions and
arguments. However, this doesn't work well with lazy macro expansions,
and could result in some closures/local variables not getting
discriminators set at all.

Make the assignment of discriminators only walk macro arguments, and
then lazily assign discriminators for anything within a macro
expansion or in ill-formed code. This replaces the single global "next
autoclosure discriminator" scheme with a per-DeclContext scheme, that
is more reliable/robust, although it does mean that discriminators
of closures and locals within macro expansions are dependent on
ordering. That shouldn't matter, because these are local values.

Fixes rdar://108682196.
2023-05-28 16:49:20 -07:00
Michael Gottesman
fd25cf379a Rename MoveExpr -> ConsumeExpr to reflect the final name.
NFC.
2023-05-17 22:42:42 -07:00
Michael Gottesman
e6f1691122 [copy-operator] Add support for the copy operator in preparation for making consuming and borrowing no implicit copy.
Some notes:

1. I implemented this as a contextual keyword that can only apply directly to
lvalues. This ensures that we can still call functions called copy, define
variables named copy, etc. I added tests for both the c++ and swift-syntax based
parsers to validate this. So there shouldn't be any source breaks.

2. I did a little bit of type checker work to ensure that we do not treat
copy_expr's result as an lvalue. Otherwise, one could call mutating functions on
it or assign to it, which we do not want since the result of copy_value is

3. As expected, by creating a specific expr, I was able to have much greater
control of the SILGen codegen and thus eliminate extraneous copies and other
weirdness than if we used a function and had to go through SILGenApply.

rdar://101862423
2023-05-17 22:42:42 -07:00
Pavel Yaskevich
583649088a [AST/CSGen] Move expanded pack collection into constraint generator
This is going to help us identify and fix positions were 'each'
keyword is mising.
2023-05-08 13:30:51 -07:00
Hamish Knight
3f7c3a2faf Handle #if for if/switch expressions
- Allow an if/switch expression to become an
implicit return of a function that has a `#if`
body with a single active element that is an `if`
or `switch`.
- Allow `#if` branches of an if/switch expression,
as long as there is a single active expression
element.

rdar://107487977
2023-04-12 14:54:22 +01:00
Doug Gregor
583ca475bf [Macros] Share storage between linked MacroExpansion(Decl|Expr).
Sometimes we build a `MacroExpansionDecl` from a `MacroExpansionExpr`.
Sometimes we do it the other way. In both cases, we risk the two
copies of must-by-shared data (macro arguments, resolved macro
reference, etc.) getting out-of-sync.

Instead, share the storage between the two representations when we
create one from the other, so that they cannot get out-of-sync. This
allows us to eliminate the extremely-dodgy `cacheOutput` call earlier.
2023-03-30 00:10:21 -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
Holly Borla
b78b6b9a77 Merge pull request #63991 from hborla/materialize-pack-from-tuple
[ConstraintSystem] Implement type checking for converting a tuple to a pack using the `.element` syntax.
2023-03-01 16:19:33 -08:00
Holly Borla
00aad22f04 [AST] Store a source location for '.element' in MaterializePackExpr. 2023-03-01 11:07:39 -08:00
Holly Borla
509188630b [ConstraintSystem] Implement type checking for converting a tuple to a
pack using the `.element` syntax.
2023-02-28 22:56:59 -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
Anthony Latsis
107ae6d589 Merge pull request #63784 from AnthonyLatsis/sugar-type-members-4
PreCheckExpr: Recognize non-identifier type qualifiers in type expressions
2023-02-23 21:19:43 +03:00
Anthony Latsis
00d09b118b AST: Allow passing arbitrary parent TypeRepr to TypeExpr::createForMemberDecl 2023-02-22 21:32:11 +03:00
Slava Pestov
e0c341d0ac AST: Rename PackReferenceTypeRepr => PackElementTypeRepr 2023-02-15 22:37:07 -05:00
Hamish Knight
c87b1b8bef Merge pull request #63022 from hamishknight/express-yourself 2023-02-03 16:40:09 +00:00
Hamish Knight
0d55f45d95 [Sema] Error on redundant try/awaits for if/switch expressions
Look through `try`/`await` markers when looking for
out of place if/switch expressions, and customize
the effect checking diagnostic such that we error
that `try`/`await` are redundant on `if`/`switch`
expressions.
2023-02-01 15:30:19 +00: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
Pavel Yaskevich
2d1588a9d2 [CSClosure] Allocate temporary type-join and opaque value exprs in solver arena
(cherry picked from commit f3df7ab31b)
2023-02-01 15:13:58 +00:00
Pavel Yaskevich
51b2481091 [AST] Expand TypeJoin expression to support joining over a type
(cherry picked from commit 4efc35a76c)
2023-02-01 15:13:58 +00:00
Holly Borla
7c50f95e1b [Macros] Use MacroExpansionExpr when resolving attached and freestanding macros. 2023-01-31 09:27:06 -08:00
Doug Gregor
4e50e29f3d Merge pull request #63233 from DougGregor/macros-mangle-buffer-names
[Macros] Mangle the names of macro expansion buffers
2023-01-26 06:57:15 -08:00
Pavel Yaskevich
7e0f02ac40 Merge pull request #62724 from valeriyvan/Formatting
Add check against NULL before dereferencing pointer
2023-01-26 00:24:47 -08:00
Doug Gregor
4ae434eaa1 [Macros] Add a mangling for macro expansions
These aren't ABI, but are useful to provide consistent names to refer
to macro expansions, e.g., in buffer names.
2023-01-25 22:45:14 -08:00
Doug Gregor
72ddbebc08 [Macros] Assign macro expansion discriminators more lazily.
This is a punt. They'll be unique, and they'll be per-context, but
they'll be lazy and therefore not stable.
2023-01-25 22:03:38 -08:00
Doug Gregor
6e3289d4d5 [Macros] Compute discriminators for freestanding macro expansions.
Introduce discriminators into freestanding macro expansion expressions
and declarations. Compute these discriminators alongside closure and
local-declaration discriminators, checking them in the AST verifier.
2023-01-25 22:03:38 -08:00
Doug Gregor
08c5553ac3 Merge pull request #62911 from DougGregor/parent-source-file-in-generated-buffers
Teach DeclContext::getParentSourceFile() to return the innermost source file
2023-01-08 21:56:51 -08:00
Doug Gregor
dfa3ed910d Teach DeclContext::getParentSourceFile() to return the innermost source file
With macro expansions, and really any code generation that produces
proper source locations, the parent source file of a declaration
context will be a generated source file rather than the source file
near the top of the declaration-context stack. Adjust the
implementation of `getParentSourceFile()` to return that innermost
source file by doing location-based lookup.
2023-01-08 11:40:02 -08:00
Holly Borla
89fa17d74c [Parser] Replace postfix ellipsis with a prefix 'repeat' keyword for pack
expansion expressions, and create them in the parser instead of during
pre-checking in the constraint system.
2023-01-07 09:49:14 -08:00
Holly Borla
ab4ec2b6cb [AST] Remove trailing pack elements from PackExpansionExpr.
This wasn't used, and callers can instead use getExpandedPacks, which
also includes TypeReprs.
2023-01-07 09:49:14 -08:00
Anthony Latsis
1bb407ac8c [NFC] AST: Rename ComponentIdentTypeReprIdentTypeRepr 2023-01-07 07:15:36 +03:00
Anthony Latsis
ad5d55c36e [NFC] AST: Rename IdentTypeReprDeclRefTypeRepr 2023-01-07 07:14:44 +03:00
Anthony Latsis
746c7aff7d [NFC] AST: Rename CompoundIdentTypeReprMemberTypeRepr 2023-01-07 07:14:41 +03:00
Anthony Latsis
0c3a72d4d7 [NFC] CompoundIdentTypeRepr: Supplant getComponents with getBaseComponent & getMemberComponents
Also store the base component separately as an arbitrary `TypeRepr`.
2023-01-07 07:11:30 +03:00
Anthony Latsis
2113e175f5 [NFC] AST: Remove IdentTypeRepr::ComponentRange 2023-01-07 07:11:30 +03:00
Anthony Latsis
8935acdd5b [NFC] AST: Remove IdentTypeRepr::create 2023-01-07 07:11:29 +03:00
Valeriy Van
694c99883f Remove AbstractClosureExpr::hasBody() not needed anymore as suggested in review 2023-01-04 12:38:10 +02:00
eeckstein
bc2bb5bf7a Merge pull request #62771 from eeckstein/debug-info
Some small debug-info fixes
2023-01-04 11:24:39 +01: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
Erik Eckstein
8dbbfabea5 AST: add debug locations for generated IntegerLiteralExprs 2023-01-02 13:52:21 +01:00
Michael Gottesman
947919228b [borrow-expr] Add simple support for borrow-expr but don't wire it up to anything. 2022-12-21 14:36:43 -08:00
Holly Borla
42ede18fb0 Merge pull request #62713 from hborla/open-pack-element-type-repr
[ConstraintSystem] Enable pack element type reprs in expressions.
2022-12-21 10:48:42 -05:00
Holly Borla
b977adfbdb [AST] Add a helper method to PackExpansionExpr that gathers all packs
that are expanded within the pattern.
2022-12-20 22:32:29 -05:00
Doug Gregor
c951497f9c Fall-back assignment of closure discriminators in ill-formed code
There are some cases of severely malformed code where we cannot compute
closure discriminators appropriate. Introduce a fallback to use the
unstable global numbering of discriminators in this case.
2022-12-19 15:52:42 -08:00
Doug Gregor
402ba1492f Set closure descriminators via a request.
Rather than set closure discriminators in both the parser (for explicit
closures) and then later as part of contextualizing closures (for
autoclosures), do so via a request that sets all of the discriminators
for a given context.
2022-12-19 15:23:45 -08:00