121 Commits

Author SHA1 Message Date
Kuba Mracek
c3f865be4a Add #objectFormat compilation conditional (SE-0492) 2025-10-16 15:15:27 -07:00
Allan Shortlidge
e8329d292b Parse: Avoid including DiagnosticsParse.h in Parser.h.
This prevents the include of DiagnosticsParse.h from leaking into `.cpp` files
in libraries besides Parse.
2025-02-19 07:56:38 -08:00
Rintaro Ishizaki
694e815f07 [Parser] Include all AST nodes from every #if region in ParserUnit
'ParserUnit' is used for analyzing syntax structures _mainly_ in
SourceKit.

Since we removed IfConfigDecl from AST, ParserUnit didn't
inclue any AST in #if ... #endif regions even for active region because
it used to consider all inactive. Instead, consider every region
"active" and include all the AST nodes.

rdar://117387631
2025-02-03 16:22:11 -08:00
Doug Gregor
02a20ded57 [Parsers] Stop sharing CustomAttributeInitializer instances across properties
We don't need to share them, and it's far simpler if we don't.
2024-12-06 17:40:34 -08:00
Doug Gregor
ee9c066050 Introduce a new Initializer subclass for the arguments of custom attributes
Since the introduction of custom attributes (as part of property
wrappers), we've modeled the context of expressions within these
attributes as PatternBindingInitializers. These
PatternBindingInitializers would get wired in to the variable
declarations they apply to, establishing the appropriate declaration
context hierarchy. This worked because property wrappers only every
applied to---you guessed it!---properties, so the
PatternBindingInitializer would always get filled in.

When custom attributes were extended to apply to anything for the
purposes of macros, the use of PatternBindingInitializer became less
appropriate. Specifically, the binding declaration would never get
filled in (it's always NULL), so any place in the compiler that
accesses the binding might have to deal with it being NULL, which is a
new requirement. Few did, crashes ensued.

Rather than continue to play whack-a-mole with the abused
PatternBindingInitializer, introduce a new CustomAttributeInitializer
to model the context of custom attribute arguments. When the
attributes are assigned to a declaration that has a
PatternBindingInitializer, we reparent this new initializer to the
PatternBindingInitializer. This helps separate out the logic for
custom attributes vs. actual initializers.

Fixes https://github.com/swiftlang/swift/issues/76409 / rdar://136997841
2024-12-06 17:40:32 -08:00
Hamish Knight
73fb36f371 [AST] Split out "is compound" bit on FunctionRefInfo
FunctionRefKind was originally designed to represent
the handling needed for argument labels on function
references, in which the unapplied and compound cases
are effectively the same. However it has since been
adopted in a bunch of other places where the
spelling of the function reference is entirely
orthogonal to the application level.

Split out the application level from the
"is compound" bit. Should be NFC. I've left some
FIXMEs for non-NFC changes that I'll address in a
follow-up.
2024-12-02 14:11:33 +00:00
Hamish Knight
a4d51419ba [AST] NFC: Rename FunctionRefKind -> FunctionRefInfo 2024-12-02 14:11:32 +00:00
Doug Gregor
5b2520e379 Remove IfConfigDecl from the AST
The swift-syntax tree retains information about the parsed #if
regions. Drop it from the semantic AST.
2024-09-18 20:51:54 -07:00
Doug Gregor
d762dd53f8 Stop parsing into IfConfiDecl nodes in the C++ parser
When parsing #if...#endif regions, parse the active clause directly into
place in the AST without ever producing an IfConfigDecl instance.
2024-09-18 20:51:09 -07:00
Doug Gregor
225e562dda Requestify the #if computation using SwiftIfConfig
We're not caching this now, but it lets us dodge annoying layering
issues because ASTGen (where this is available) sits on top of the C++
parser, which needs to call it.
2024-08-24 21:31:41 -07:00
Doug Gregor
256f39e5ee Use the SwiftIfConfig library for evaluating #if conditions.
Replace the C++ implementation of #if condition operator folding,
validation, and evaluation with the corresponding facilities in the
SwiftIfConfig library.

Leave the C++ implementation in place for now to permit the compiler
to continue building without swift-syntax.
2024-08-24 21:31:40 -07:00
Hamish Knight
2fbc8b336f Merge pull request #74559 from hamishknight/pound-if-skip
[Parse] Handle `#if` in brace skipping logic
2024-06-21 15:04:19 +01:00
Hamish Knight
a85ca1315b [Parse] Unify recovery for invalid tokens following a #if body
Previously we would only diagnose and recover for
invalid tokens following a `#if` body for the decl
and postfix expression case. Sink this logic into
`parseIfConfigRaw`, ensuring that we do this for
all `#if` cases. This requires propagating the
context we're parsing in to customize the
diagnostic.
2024-06-19 21:39:40 +01:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Steven Wu
7d85aa423d [ScanDependencies] Make sure canImport resolution agrees with import
Fix the problem that when the only module can be found is an
invalid/out-of-date swift binary module, canImport and import statement
can have different view for if the module can be imported or not.

Now canImport will evaluate to false if the only module can be found for
name is an invalid swiftmodule, with a warning with the path to the
module so users will not be surprised by such behavior.

rdar://128876895
2024-06-17 14:14:48 -07:00
Kabir Oberai
d4ee05051e [Parse] Fix nested ifConfig compiler checks (#74415)
The parser is supposed to avoid looking inside unmatched `#if` compiler (et al) blocks.
This usually means that the following code builds fine

  #if compiler(>=100)
  foo bar
  #endif
  
however, a logical bug meant that if the check was nested inside an already-inactive
`#if` block, it would not adhere to this evaluation-skipping behavior

  #if false
  #if compiler(>=100)
  foo bar // error!
  #endif
  #endif
  
This PR fixes this specific case.
2024-06-15 00:31:24 -07: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
Rintaro Ishizaki
46e372cc5c Merge pull request #72384 from rintaro/parse-ifconfig-argdiag-rdar124160048
[Parse] Adjust diagnostic location for platform condition arguments
2024-03-19 23:10:12 +09:00
Allan Shortlidge
86200b19fe Parse: Resolve an unused variable warning.
NFC.
2024-03-18 16:32:43 -07:00
Rintaro Ishizaki
47372122a1 [Parse] Adjust diagnostic location for platform condition arguments
Previously, diagnostics for arguments of platform conditions (e.g.
'os(macOS)') used to point the condition name position instead of the
argument position.
Adjust the position to the start of the argument.

rdar://124160048
2024-03-18 23:23:27 +09:00
Rintaro Ishizaki
91dc6822fa Merge pull request #72310 from rintaro/ifconfigregisons-per-clause
[SourceKit] Use recorded #if regions in 'active-regions' request
2024-03-15 06:22:51 +09:00
Ben Barham
9779c18da3 Rename startswith to starts_with
LLVM is presumably moving towards `std::string_view` -
`StringRef::startswith` is deprecated on tip. `SmallString::startswith`
was just renamed there (maybe with some small deprecation inbetween, but
if so, we've missed it).

The `SmallString::startswith` references were moved to
`.str().starts_with()`, rather than adding the `starts_with` on
`stable/20230725` as we only had a few of them. Open to switching that
over if anyone feels strongly though.
2024-03-13 22:25:47 -07:00
Rintaro Ishizaki
2abddcb260 [SourceKit] Use recored #if regions in "active regions" request
* Record each IfConfig clause location info in SourceFile
* Update SILProfiler to handle them
* Update SwiftLangSupport::findActiveRegionsInFile() to use the recorded
  regions instead of walking into AST to find #if regions

rdar://118082146
2024-03-14 08:52:57 +09:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Ben Barham
d51c58a6f9 [Basic] hasFeature should succeed for promoted language features
Merge `$<Feature>` and `hasFeature` implementations.
  - `$<Feature>` did not support upcoming language features.
  - `hasFeature` did not support promoted language features and also
    didn't take into account `Options` in `Features.def`.

Remove `Options` entirely, it was always one of three cases:
  - `true`
  - `langOpts.hasFeature`
  - `hasSwiftSwiftParser`

Since `LangOptions::hasFeature` should always be used anyway, it's no
longer necessary. `hasSwiftSwiftParser` can be special cased when adding
the default promoted language features (by removing those features).

Resolves rdar://117917456.
2024-01-05 10:26:13 -08:00
Hamish Knight
6a58991cc4 [Parse] Store #if ranges on the SourceFile
Record any `#if`'s we encounter, and make a note
of their active region.
2023-11-14 20:41:35 +00:00
Alejandro Alonso
234410f2fe Change to _hasAtomicBitWidth 2023-10-09 14:05:45 -07:00
Alejandro Alonso
f58aa07d41 Merge pull request #68536 from Azoy/maxAtomicInlineWidth
[AST] Add #if _atomicBitWidth as a valid compilation conditional
2023-09-30 12:34:17 -07:00
Alejandro Alonso
a3f4361280 Add _atomicBitWidth compilation conditional
fix warning
2023-09-29 14:43:34 -07:00
Ben Barham
041691184c [CMake] Replace early swift-syntax with FetchContent
Use FetchContent to include swift-syntax directly in swift. This can be
thought of as an `add_subdirectory` for a directory outside the root.

The default build directory will be `_deps/swiftsyntax-subbuild/`, though
the modules and shared libraries will be built in `lib/swift/host` by
passing down `SWIFT_HOST_LIBRARIES_DEST_DIR` to avoid copying them as we
were doing previously.
2023-09-18 14:44:10 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -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
Doug Gregor
73a20417ba Only define the various Macros features when the compiler supports them
We were enabling the `$Macros` feature unconditionally, even when the
compiler itself doesn't support macros (because it's missing
swift-syntax). Change this to only enable the feature when the
compiler supports it.
2023-03-29 16:30:44 -07:00
Dario Rexin
294342a1d3 Add _pointerBitWidth platform condition (#41534)
* Add _pointerBitWidth platform condition

* Fixes after rebasing

---------

Co-authored-by: Xiaodi Wu <xiaodi.wu@gmail.com>
2023-03-21 20:59:32 -07:00
Alex Hoppen
fe2ae72ad2 [IDE] Rename CodeCompletion to IDEInspection in cases where the code path no longer exclusively applies to code completion
The code completio infrastructure is also being used for cursor info now, so it should no longer be called code completion.

rdar://103251187
2022-12-13 11:41:05 +01:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Rintaro Ishizaki
103b4a89c2 [Parse] Restore a couple of StructureMarkerRAII (#62193)
These are accidentally removed in 530d937879
2022-11-18 17:14:03 -08:00
Robert Widmann
530d937879 Remove SyntaxContext and Parser Affordances 2022-11-16 13:24:21 -08:00
Robert Widmann
18b79ffcfd Resolve a Layering Violation in libBasic
Basic should not be allowed to link Parse, yet it was doing so
to allow Version to provide a constructor that would conveniently
parse a StringRef. This entrypoint also emitted diagnostics, so it
pulled in libAST.

Sink the version parser entrypoint down into Parse where it belongs
and point all the clients to the right place.
2022-09-09 00:21:30 -07:00
Doug Gregor
bde58e7d58 Merge pull request #60208 from DougGregor/has-attribute 2022-08-18 17:55:16 -07:00
Rintaro Ishizaki
a673043737 Terminology change: 'garbage' -> 'unexpected'
There are no "garbage" characters in Swift code. They are just
"unexpected."
2022-08-15 14:32:28 -07:00
Alex Hoppen
c2695f0ffc [libSyntax] Allow adding garbage nodes in between any two children of a syntax node
When the source code is invalid, this allows us to represent tokens that could not be used to form a valid syntax tree with more fidelity.

This commit does not start using GarbageNodes yet, it just sets everything up for them.
2022-08-04 09:20:31 +02:00
Allan Shortlidge
3a5fe80b48 Parse: Fix parsing of canImport() version arguments with more than two components.
Resolves: rdar://97990159
2022-08-02 15:54:40 -07:00
Doug Gregor
3f8d80aaf6 Fix infinite recursion at the end of a file 2022-07-26 10:13:59 -04:00
Doug Gregor
b3c5fde118 Parse declaration attributes within #if...#endif blocks.
Introduce support for parsing declaration attributes that occur within
example:

    #if hasAttribute(frozen)
    @frozen
    #endif
    public struct X { ... }

will apply to "frozen" attribute to the struct `X`, but only when the
compiler supports the "frozen" attribute.

Correctly determining whether a particular `#if` block contains
attributes to be associated with the following declaration vs.
starting a new declaration requires arbitrary lookahead. The parser
will ensure that at least one of the branches of the `#if` contains an
attribute, and that none of the branches contains something that does
not fit the attribute grammar, before committing to parsing the `#if`
clause as part of the declaration attributes. This lookahead does
occur at the top level (e.g., in the parsing of top-level declarations
and code), but should only need to scan past the first `#if` line to
the following token in the common case.

Unlike other `#if` when used to wrap statements or declarations, we
make no attempt to record the `#if` not taken anywhere in the AST.
This reflects a change in attitude in the design of the AST, because
we have found that trying to represent this information there (e.g.,
via `IfConfigDecl`) complicates clients while providing little value.
This information is best kept in the syntax tree, only.
2022-07-25 21:31:17 -04:00
Doug Gregor
7a75ddafe1 Add "#if hasAttribute(X)" support query attributes. 2022-07-25 21:31:16 -04:00
Doug Gregor
2da25612ba Short-circuit validation of #if conditions after a versioned check.
When we encounter a check like `#if compiler(>=6.0) && something` or
`#if swift(<6.0) || something`, and the left-hand term is a versioning
check that determines the result of the whole condition, then we will
not attempt to validate the right-hand term. This allows us to use
versioned checks along with new discovery features (say, if we add an
`#if attribute(x)`) without having to next conditions.
2022-07-19 21:27:54 -07:00
Doug Gregor
569859ab60 Add hasFeature(X) to check for future features 2022-07-19 21:27:39 -07:00
Doug Gregor
a317fad88d Add -enable-upcoming-feature X command-line argument.
Introduce the `-enable-upcoming-feature X` command-line argument to
allow one to opt into features that will be enabled in an upcoming language
mode. Stage in several features this way (`ConciseMagicFile`,
`ForwardTrailingClosures`, `BareSlashRegexLiterals`).
2022-07-19 21:20:12 -07:00
swift-ci
645d73f27e Merge pull request #58480 from beccadax/so-i-put-versions-in-your-version-numbers
Update SWIFT_COMPILER_VERSION language features
2022-05-24 17:47:43 -07:00