Commit Graph

49 Commits

Author SHA1 Message Date
Anthony Latsis
ae70d384e0 [NFC] Basic: Restrict adoption mode to upcoming and experimental features 2025-03-11 01:15:33 +00:00
Anthony Latsis
6a3903bb53 Revert "Revert "Introduce adoption mode for Swift features""
This reverts commit 393c59c078.
2025-03-05 15:21:01 +00:00
Rintaro Ishizaki
393c59c078 Revert "Introduce adoption mode for Swift features" 2025-03-04 19:13:05 -08:00
Anthony Latsis
8dab67bcee Basic: Adjust feature macros for adoption mode 2025-03-04 13:43:28 +00:00
Rintaro Ishizaki
aae50a88c5 [ASTGen] Fix expanded macro buffer parsing and AST generation
Parse expanded buffer into dedicated syntax.

Also rename `BridgedGeneratedSourceFileKindAttribute` to
`BridgedGeneratedSourceFileKindAttributeFromClang` because C++ decl
(i.e. `GeneratedSourceInfo::Kind::AttributeFromClang`) was renamed a
while ago.
2025-02-21 09:56:36 -08:00
Rintaro Ishizaki
6ea6a31ec7 [ASTGen] Generate TopLevelCodeDecl for PatternBindingDecl
* Instead of hoisting VarDecl in the bridging functions, do it in
  ASTGen.
* Introduce `Decl::forEachDeclToHoist` to handle VarDecls in
  PatternBindingDecl, and EnumElementDecl in EnumCaseDecl.
* Intorduce `withBridgedSwiftClosure(closure:call:)` as a callback
  mechanism between Swift and C++
* In `generate(sourceFile:)`, instead of using `generate(codeBlockItem:)`
  handle `CodeBlockItemSyntax.Item` manually to handle `TLCD` wrapping
  and `VarDecl` hoisting.
* Make `generate(variableDecl:)` handle TLCD correctly.
2025-02-13 00:09:22 -08:00
Rintaro Ishizaki
ac2603cad6 Merge pull request #79168 from rintaro/astgen-tweaks
[ASTGen] Small followup tweaks after AvailableAttr
2025-02-05 16:48:35 -08:00
Rintaro Ishizaki
ebf7a421fd [Bridging] Add 'const' variations to BRIDGING_WRAPPER
Only for `AvailabilityMacroMap` for now.
2025-02-05 11:44:54 -08:00
Rintaro Ishizaki
1559b37c03 [Basic] Remove unnecessary 'const' from several briding functions 2025-02-05 10:37:56 -08:00
Rintaro Ishizaki
df2ada37df [ASTGen] Generate AvailableAttr
* Move `AvailabilitySpec` handling logic to AST, so they can be shared
  between libParse and ASTGen
* Requestify '-define-availability' arguments parsing and parse them
  with 'SwiftParser' according to the 'ParserASTGen' feature flag
* Implement 'AvailableAttr' generation in ASTGen
2025-02-04 23:40:01 -08:00
Rintaro Ishizaki
e153164677 [ASTGen] Handle '#sourceLocation' directives
Use `ExportedSourceFile.sourceLocationConverter.lineTable.virtualFiles`
to populate the information in `swift::SourceManger` and
`swift::SourceFile` when "parsing" with ASTGen
2024-11-21 08:23:08 -08:00
Doug Gregor
989c73d014 Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
2024-11-13 21:19:37 -08:00
Rintaro Ishizaki
e566a746c8 [ASTGen] Support macro expanded buffer
* Make ExportedSourceFile hold any Syntax as the root node
* Move `ExportedSourceFileRequest::evaluate()` to `ParseRequests.cpp`
* Pass  the decl context and `GeneatedSourceFileInfo::Kind` to
  `swift_ASTGen_parseSourceFile()` to customize the parsing
* Make `ExportedSourceFile` to hold an arbitrary Syntax node
* Move round-trip checking into `ExportedSourceFileRequest::evaluate()`
* Split `parseSourceFileViaASTGen` completely from C++ parsing logic
  (in `ParseSourceFileRequest::evaluate()`)
* Remove 'ParserDiagnostics' experimental feature: Now that we have
  ParserASTGen mode which includes the swift-syntax parser diagnostics.
2024-11-05 11:00:33 -08:00
Hamish Knight
9d4a78678a [Sema] Add logic to diagnose regex feature availability
Add the necessary compiler-side logic to allow
the regex parsing library to hand back a set of
features for a regex literal, which can then be
diagnosed by ExprAvailabilityWalker if the
availability context isn't sufficient. No tests
as this only adds the necessary infrastructure,
we don't yet hand back the features from the regex
parsing library.
2024-10-28 17:09:47 +00:00
Erik Eckstein
ed67e36ce5 bridging: reduce #ifdef USED_IN_CPP_SOURCE in bridging headers
Especially avoid any constructors in `#ifdef USED_IN_CPP_SOURCE` blocks, because this breaks Windows ARM64.
2024-10-25 09:47:56 +02:00
Hiroshi Yamauchi
dedfb9e0ab Ensure that bridged types are indirectly returned on Windows ARM64
On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Following https://github.com/swiftlang/swift/pull/76433 add
constructors to several bridged C++ struct/class types so that the
calling convention matches.

This is a partial fix for https://github.com/swiftlang/swift/issues/74866
2024-09-19 16:00:15 -07:00
Egor Zhdan
586ac0953c Merge pull request #76408 from swiftlang/egorzhdan/scs-owned-string
[cxx-interop][SwiftCompilerSources] Do not use `SWIFT_IMPORT_UNSAFE` with `BridgedOwnedString`
2024-09-16 20:00:20 +01:00
Rintaro Ishizaki
d7303d141c [Basic] Remove llvm::JSON C bridging
This was used for plugin message serialization, but it's migrated to
SwiftCompilerPluginMessageHandling JSON.
2024-09-12 12:04:28 -07:00
Egor Zhdan
35263ef9d6 [cxx-interop][SwiftCompilerSources] Do not use SWIFT_IMPORT_UNSAFE with BridgedOwnedString
`SWIFT_IMPORT_UNSAFE` is an escape hatch that can be used to make the Swift compiler ignore its usual safety heuristics for C++ types.

`BridgedOwnedString` fits into the definition of a self-contained C++ type in Swift: it manages the lifetimes of its own fields.

This removes the usages of `SWIFT_IMPORT_UNSAFE` for C++ functions that return `BridgedOwnedString`, and annotates `BridgedOwnedString` as a self-contained type.
2024-09-11 19:58:14 +01:00
Andrew Trick
22f01a956a [SwiftCompilerSources] add BridgedOStream and debugLog().
So we can trace optimizer passes using the same output stream as the
C++ passes and don't get garbled output.
2024-07-29 21:25:43 -07:00
Erik Eckstein
81005c18a2 SwiftCompilerSources: forward assertion and precondition failures to the assertion-handling in the C++ code base. 2024-06-25 14:14:43 +02:00
Anthony Latsis
de499d1060 [NFC] ASTGen: Refactor for recursive MemberTypeRepr representation 2024-02-09 17:23:40 +03: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
Alex Hoppen
cc858ab253 Merge pull request #70008 from ahoppen/ahoppen/name-matcher-in-swift
Share implementation of local rename and related identifiers + implement `NameMatcher` in Swift
2023-11-30 09:37:53 -08:00
Alex Hoppen
14bc8148fe Don’t include vector in `BasicBridging.h 2023-11-28 14:03:31 -08:00
Erik Eckstein
8681131c58 add some comments in the bridging headers 2023-11-28 09:02:10 +01:00
Alex Hoppen
b31398215d Don’t include SourceLoc.h when USED_IN_CPP_SOURCE is not set 2023-11-27 19:34:27 -08:00
Alex Hoppen
8fd025625b [SourceKit] Use NameMatcher that is rewritten in Swift for syntactic rename 2023-11-17 09:30:04 -08:00
Hamish Knight
9216672db7 Improve some bridging interfaces
Migrate a bunch of structs to classes, and
uppercase field names.
2023-10-31 11:06:41 +00:00
Hamish Knight
e2b8d98edc [Basic] Abstract BridgedOStream
Define using the wrapper macro.
2023-10-31 11:06:41 +00:00
Hamish Knight
5d99fe63e9 Rename get() -> unbridged() on bridging wrappers 2023-10-31 11:06:39 +00:00
Hamish Knight
3cb74e911b Address review feedback 2023-10-30 23:50:01 +00:00
Hamish Knight
2a6d93f505 [Basic] Improve API of BridgedArrayRef + BridgedData
Remove the default constructor footgun present with
the struct implementations, and sprinkle some
`SWIFT_NAME` and bridging utilities to make them
nicer to work with.
2023-10-30 23:49:59 +00:00
Hamish Knight
e0f57f84f0 [AST] Cleanup ASTBridging + BasicBridging
Sort AST node bridging functions into Expr, Stmt,
Decl, and TypeRepr, and tweak the headers to use
`MARK`.
2023-10-30 23:49:59 +00:00
Hamish Knight
25546d2178 [AST] Remove redundant typedefs
These aren't needed in C++
2023-10-30 23:49:58 +00:00
Hamish Knight
6afde8a6d2 Remove CBasicBridging and CASTBridging
Merge with BasicBridging and ASTBridging
respectively. The changes here should be pretty
uncontroversial, I tried to keep it to just moving
code about.
2023-10-30 23:49:56 +00:00
Hamish Knight
fce1cb54d5 [AST] Merge BridgedDiagnosticEngine + BridgedDiagEngine
Introduce a macro that can stamp out wrapper
classes for underlying C++ pointers, and use
it to define BridgedDiagnosticEngine in
ASTBridging. Then, migrate users of
BridgedDiagEngine onto it.
2023-10-30 23:49:55 +00:00
Hamish Knight
ce23252a3f [Basic] Improve some bridging APIs
Improve APIs for BridgedStringRef,
BridgedOwnedString, and BridgedSourceLoc.
2023-10-30 23:49:53 +00:00
Erik Eckstein
22979b9a7a fix an uninitialized pointer in BridgedSourceLoc
It's so easy to run into undefined behavior in C++
2023-10-09 17:32:51 +02:00
Erik Eckstein
2dbd6cc56b SwiftCompilerSources: rework bridging
Introduce two modes of bridging:
* inline mode: this is basically how it worked so far. Using full C++ interop which allows bridging functions to be inlined.
* pure mode: bridging functions are not inlined but compiled in a cpp file. This allows to reduce the C++ interop requirements to a minimum. No std/llvm/swift headers are imported.

This change requires a major refactoring of bridging sources. The implementation of bridging functions go to two separate files: SILBridgingImpl.h and OptimizerBridgingImpl.h.
Depending on the mode, those files are either included in the corresponding header files (inline mode), or included in the c++ file (pure mode).

The mode can be selected with the BRIDGING_MODE cmake variable. By default it is set to the inline mode (= existing behavior). The pure mode is only selected in certain configurations to work around C++ interop issues:
* In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
* On windows to workaround a build problem.
2023-10-09 09:52:52 +02:00
Allan Shortlidge
f9a93a55d7 NFC: Fix duplicate definition of macro warnings.
Centralize the `SWIFT_{BEGIN/END}_NULLABILITY_ANNOTATIONS` definitions in a new
Nullability.h header to share.
2023-07-27 13:02:22 -07:00
Matt Jacobson
2e629e9fb9 basic: fix FreeBSD build failure around usage of assert()
LLVM's `CASReference.h` uses `assert()` without first including `<cassert>`,
which poses a problem here when `CASReference.h` is included first thing in a
submodule.

In FreeBSD's <assert.h>, `assert()` expands to a call to a function that is
declared within header guards.  That is, only the first include of `<assert.h>`
gets the function declaration.

This is module-unfriendly, since it means that if multiple submodules of a
module both include `<assert.h>`, only one of them ends up with a usable
`assert()` macro.  If you haven't (transitively) included the right submodule,
and you haven't included `<assert.h>` yourself, then you get a compile error
that looks like the following:

```
llvm-project/llvm/include/llvm/CAS/CASReference.h:75:5: error: declaration of '__assert' must be imported from module 'LLVM_Utils.Support.MathExtras' before it is required
    assert(InternalRef != getDenseMapEmptyRef() && "Reserved for DenseMapInfo");

/usr/include/assert.h:77:6: note: declaration here is not visible
void __assert(const char *, const char *, int, const char *) __dead2;

swift/include/swift/AST/DeclContext.h:31:10: error: could not build module 'BasicBridging'
#include "swift/Basic/SourceLoc.h"

swift/SwiftCompilerSources/Sources/Basic/SourceLoc.swift:13:8: error: could not build C module 'ASTBridging'
import ASTBridging
```

Ideally `CASReference.h` would include `<cassert>` itself.  But use of
`assert()` without a prior include of `<cassert>` is common in LLVM.  It's even
encouraged by the LLVM "coding standards", which say:

> Use the “assert” macro to its fullest. [...] The “<cassert>” header file is
> probably already included by the header files you are using, so it doesn’t
> cost anything to use it.

Since the include of `CASReference.h` appears to be a temporary workaround, add
to it by including `<cassert>`, too.
2023-03-15 06:49:29 -04:00
Erik Eckstein
87bb0cfd3c Workaround to avoid a compiler error because cas::ObjectRef is not defined when including VirtualFileSystem.h 2022-11-04 20:44:19 +01:00
Egor Zhdan
0e2d438c5b [cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
2022-07-21 16:32:16 +01:00
Egor Zhdan
220d95e3d9 [cxx-interop][SwiftCompilerSources] Use swift::CharSourceRange instead of BridgedCharSourceRange
This removes some of the bridging code and replaces it with C++ calls.

rdar://83361087
2022-07-07 12:00:12 +01:00
Egor Zhdan
038a4f0b80 [cxx-interop][SwiftCompilerSources] Use swift::SourceLoc instead of BridgedSourceLoc
C++ interop is now enabled in SwiftCompilerSources, so we can remove some of the C bridging layer and use C++ classes directly from Swift.

rdar://83361087
2022-06-11 00:13:39 +01:00
Rintaro Ishizaki
4d9b65d961 [SwiftCompiler] Add DiagnosticEngine bridging
* 'SourceLoc' and 'CharSourceRange' bridging in Basic
* New 'AST' bridging. 'DiagID', 'DiagnosticArgument', 'DiagnosticFixIt',
  and 'DiagnosticEngine'
2022-02-23 15:57:33 -08:00
Rintaro Ishizaki
da6814d510 [SwiftCompiler] Make BridgedArrayRef.data nullable
ArrayRef.data() can be nullptr when the array is empty.
2022-02-20 22:30:18 -08:00
Rintaro Ishizaki
7486cd1c21 [SwiftCompiler] Move common bridging facilities to 'Basic'
A preparation for AST/DiagnosticEngine bridging
2022-02-20 22:06:39 -08:00