Commit Graph

6196 Commits

Author SHA1 Message Date
Doug Gregor
f8f14b4ab6 [Clang importer] Use ParseSourceFileRequest for parsing swift_attr attributes
The Clang importer was directly calling into the parser to parse the
attribute (or modifier) within swift_attr. Aside from being gross, this
isn't possible with ASTGen.

Instead, teach ParseSourceFileRequest to deal with modifiers in the
same way that the Clang importer was hardcoding, and have the Clang
importer pull the attributes/modifiers off of the "missing"
declaration introduced by the request.

One benefit of this approach is that we're only parsing each
swift_attr source buffer once, then cloning the attributes each time
it's used, so we should be doing less work overall.

Fixes rdar://139119159.
2024-11-15 09:02:47 -08:00
Egor Zhdan
aa584bf876 [cxx-interop] Check the presence of copy constructor correctly
This reverts commit 3066bd6919.

This re-lands a change after it got reverted because of a regression in the build of SwiftCompilerSources.

rdar://136838485
2024-11-15 15:21:55 +00:00
Gabor Horvath
558380f6f8 [cxx-interop] Support conditional escapability
This PR adds a variadic macro that builds a SwiftAttr string containing
the names of the template type parameters that need to be escapable for
the type to be considered escapable. It also adds logic to interpret
this annotation.

rdar://139065437
2024-11-15 14:27:54 +00:00
Egor Zhdan
99320c7a8b Merge pull request #77616 from swiftlang/egorzhdan/revert-copy-ctor
Revert "[cxx-interop] Check the presence of copy constructor correctly"
2024-11-14 20:28:33 +00:00
Egor Zhdan
3066bd6919 Revert "[cxx-interop] Check the presence of copy constructor correctly"
This reverts commit fbbec48c

The change was causing regressions in certain build configs that need to be investigated.

rdar://139723218
2024-11-14 16:31:27 +00:00
Egor Zhdan
f88b29bfdc Merge pull request #77445 from CrazyFanFan/feature_cxxset_remove
[cxx-interop] Allow removing elements from `std::set`.
2024-11-14 13:09:21 +00:00
Doug Gregor
f802b67fc0 Merge pull request #77580 from DougGregor/swift-attr-parsing-fixes
Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
2024-11-14 03:19:16 -08:00
Egor Zhdan
6aa9d439d1 Merge pull request #77591 from swiftlang/egorzhdan/class-template-nullptr
[cxx-interop] Disambiguate template instantiations with nullptr parameters
2024-11-14 10:45:11 +00:00
Doug Gregor
11ed132614 [Clang importer + macros] Handle name lookup and type checking for expanded macros
Introduce a number of fixes to allow us to fully use declarations that
are produced by applying a peer macro to an imported declarations.
These changes include:
* Ensuring that we have the right set of imports in the source file
containing the macro expansion, because it depends only on the module
it comes from
* Ensuring that name lookup looks in that file even when the
DeclContext hierarchy doesn't contain the source file (because it's
based on the Clang module structure)

Expand testing to be sure that we're getting the right calls,
diagnostics, and generated IR symbols.
2024-11-13 21:21:56 -08:00
Doug Gregor
aa4c548c0f Clang importer: switch swift_attr attribute text cache over to be module-sensitive
We need different buffers for each imported module that has swift_attr attributes,
so cache them appropriately.
2024-11-13 21:19:39 -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
Artem Chikin
d1ef379ea2 Merge pull request #77585 from artemcm/RemoveExtraneousErrorFromHeaderScan
[Dependency Scan] Remove newly-introduced error failure on missing header dependency of a binary module dependency
2024-11-13 12:13:57 -08:00
Egor Zhdan
3d80ab37b2 [cxx-interop] Disambiguate template instantiations with nullptr parameters
This makes sure that different class template instantiations get distinct generated Swift type names.

Similar to aa6804a3.
2024-11-13 19:18:08 +00:00
Artem Chikin
d68b5b2b6e [Dependency Scan] Remove newly-introduced error failure on missing header dependency of a binary module dependency
Previous behavior had the scanner simply proceed if the header input of a binary Swift module dependency could not be resolved on the filesystem

Resolves rdar://139736789
2024-11-13 08:24:27 -08:00
Crazy凡
aeaa8ecadd [cxx-interop] Allow removing elements from std::set. 2024-11-13 23:16:45 +08:00
Meghana Gupta
184058e4ac Merge pull request #76588 from meg-gupta/lifetimeremovefeatureguardincompiler
Remove feature guard on lifetime dependence inference
2024-11-12 18:38:41 -08:00
Jan Svoboda
ec086e1a3c [ClangImporter] Fix build after LLVM #9573 (#77557) 2024-11-12 14:50:08 -08:00
Meghana Gupta
6061d465a6 C++Interop: Don't create default argument expressions that are templated
SwiftDeclSynthesizer::makeDefaultArgument does not create substituion map while
creating the implicit FuncDecl which leads to TypeChecker complaining about
the absence when there are type parameters. Avoid importing such C++ functions for now.
2024-11-12 12:05:23 -08:00
Egor Zhdan
655336cceb Merge pull request #77555 from swiftlang/egorzhdan/builtin-names
[cxx-interop] Simplify name generation for template arguments of builtin types
2024-11-12 18:43:52 +00:00
Egor Zhdan
cbca55af64 [cxx-interop] Simplify name generation for template arguments of builtin types
We were searching for types in the Swift stdlib by name, just to get the names back from the types. Let's just return the type name without performing the search.

No user-facing change intended.
2024-11-12 14:57:18 +00:00
Egor Zhdan
6943986c71 [cxx-interop] Import private fields of C++ structs
While private and protected fields coming from C++ cannot be accessed from Swift, they can affect Swift typechecking.

For instance, the Swift typechecker mechanism that adds implicit `Sendable` conformances works by iterating over all of the struct's fields and checking whether all of them are `Sendable`. This logic was broken for C++ types with private fields, since they were never accounted for. This resulted in erroneous implicit `Sendable` confromances being added.

Same applies for `BitwiseCopyable`.

In addition to this, ClangImporter used to mistakenly mark all C++ structs that have private fields as types with unreferenceable storage, which hampered optimizations.

As a side effect of this change, we now also provide a better diagnostic when someone tries to access a private C++ field from Swift.

rdar://134430857
2024-11-12 12:47:26 +00:00
Egor Zhdan
e345ebd1a1 [cxx-interop] Disambiguate template instantiations with enum parameters
This makes sure that different class template instantiations with enum arguments get distinct generated Swift type names.

Similar to aa6804a3.

rdar://139437761 / resolves https://github.com/swiftlang/swift/issues/77358
2024-11-11 18:03:09 +00:00
Egor Zhdan
fa8852bf47 Merge pull request #77450 from swiftlang/egorzhdan/class-template-packs
[cxx-interop] Disambiguate template instantiations with parameter packs
2024-11-11 17:48:02 +00:00
Steven Wu
828fff5eb6 Merge pull request #77485 from cachemeifyoucan/eng/PR-138664252-swift
[ClangImporter] Allow direct explicit load when implicit module is enabled
2024-11-09 09:25:06 -08:00
Steven Wu
ed8032cbc3 [ClangImporter] Allow direct explicit load when implicit module is enabled
Improve clang importor so it can directly load explicit module even
implicit module is enabled. This is a special configuration used by lldb
because lldb sometimes need to load additional modules when binding
external types. This provide the path in clang importer that can load
explicit module without locating the clang module map, while fallback to
module map lookup when implicit module is needed.
2024-11-08 15:34:55 -08:00
Egor Zhdan
dcdcd13036 [cxx-interop] Workaround build error in SwiftCompilerSources
Certain build configurations of SwiftCompilerSources now incorrectly treat `BridgedSwiftObject` as a non-copyable type, causing compiler errors. This is a short-term workaround for these errors.

rdar://138924133
2024-11-08 21:27:37 +00:00
Egor Zhdan
d66c8edda2 [cxx-interop] Disambiguate template instantiations with parameter packs
This makes sure that different template instantiations of `std::tuple` get distinct Swift type names.

Similar to aa6804a3.

This also refactors `swift::importer::printClassTemplateSpecializationName` to follow a proper visitor pattern for the C++ template arguments.

rdar://139435937
2024-11-08 16:43:59 +00:00
fahadnayyar
72b8d22362 Merge pull request #77457 from fahadnayyar/frt-ref-semantic-check-refactor
[cxx-interop] Refactoring reference semantic check for c++ records
2024-11-08 02:14:25 -08:00
fahadnayyar
f4a0dd6867 [cxx-interop] Refactoring reference semantic check for c++ records 2024-11-07 10:54:49 -08:00
Gábor Horváth
72f49bdf1b Merge pull request #77342 from swiftlang/gaborh/import-as-unsafe-for-real
[cxx-interop] Import some C++ methods as Unsafe
2024-11-06 12:40:14 +00:00
Artem Chikin
8e3a6207b9 Merge pull request #76915 from artemcm/AsyncScanExperiment
[Dependency Scanning] Parallelize Clang module queries
2024-11-05 10:04:31 -08:00
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -07:00
Hamish Knight
df7cac3114 Merge pull request #59141 from hamishknight/wrapping-paper
[AST] Remove ParenType
2024-11-01 16:52:23 +00:00
Gabor Horvath
d885dec4e8 [cxx-interop] Import some C++ methods as Unsafe
ClangImporter already had some logic in place to rename certain unsafe
C++ methods to make sure their name indicates unsafety. With the recent
push for auditability, we have a new @unsafe attribute so we can
automate parts of the auditing process. This patch makes sure whenever
we rename a method as "Unsafe", we also add the @unsafe attribute.
2024-11-01 13:03:59 +00:00
fahadnayyar
f430088616 Merge pull request #76798 from fahadnayyar/frt-return-unannotated-warning
[cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REF…
2024-10-31 17:40:12 -07:00
Egor Zhdan
5a5b1ef7ff Merge pull request #77298 from swiftlang/egorzhdan/class-template-array-param
[cxx-interop] Disambiguate template instantiations with array type parameters
2024-10-31 17:17:58 +00:00
Gábor Horváth
217b450a39 Merge pull request #77294 from swiftlang/gaborh/import-return-independent-as-immortal
[cxx-interop] SWIFT_RETURNS_INDEPENDENT_VALUE implies immortal lifetime
2024-10-31 14:53:46 +00:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Gábor Horváth
09c89fb983 Merge pull request #77204 from swiftlang/gaborh/diagnostic-location
[cxx-interop] Import more C++ source locations into Swift
2024-10-31 11:06:11 +00:00
Tony Allevato
97186b0738 Merge pull request #69460 from dylansturg/indexstore
Enable indexing for refs to implicit declarations.
2024-10-30 16:38:37 -04:00
Artem Chikin
0f50693aa5 [Dependency Scanning] Parallelize Clang module queries
This change refactors the top-level dependency scanning flow to follow the following procedure:

Scan():
1. From the source target under scan, query all imported module identifiers for a *Swift* module. Leave unresolved identifiers unresolved. Proceed transitively to build a *Swift* module dependency graph.
2. Take every unresolved import identifier in the graph from (1) and, assuming that it must be a Clang module, dispatch all of them to be queried in-parallel by the scanner's worker pool.
3. Resolve bridging header Clang module dpendencies
4. Resolve all Swift overlay dependencies, relying on all Clang modules collected in (2) and (3)
5. For the source target under scan, use all of the above discovered module dependencies to resolve all cross-import overlay dependencies
2024-10-30 11:10:10 -07:00
Egor Zhdan
aa6804a30e [cxx-interop] Disambiguate template instantiations with array type parameters
When Swift imports C++ template class instantiations, it generates a human-readable Swift name for each instantiation.

Having name collisions causes multiple Swift type with the same name, which confuses the compiler.

`MyClass<int[]>` and `MyClass<long[]>` were both being imported as `MyClass<_>` into Swift. This patch fixes that:

* `MyClass<int[]>` is now imported as `MyClass<[CInt]>`
* `MyClass<int[123]>` is now imported as `MyClass<Vector<CInt, 123>>`

rdar://138921102
2024-10-30 17:05:14 +00:00
Gabor Horvath
17ef2f66fe [cxx-interop] Make SWIFT_RETURNS_INDEPENDENT_VALUE imply immortal lifetime
The semantics of returning independent value already matches what
immortal lifetimes are within Swift. This patch makes sure this
annotation works as expected with non-escapable types.

rdar://137671642
2024-10-30 15:35:33 +00:00
Gabor Horvath
86e708a39b [cxx-interop] Import more C++ source locations into Swift
Occasionally, when the Swift compiler emits a diagnostic for a construct
that was imported from C++ we get a diagnostic with unknown location.
This is a bad user experience. It is particularly bad with the
borrow-checker related diagnostics. This patch extends the source
location importing to declarations in ClangImporter. There are some
invariants enforced by the Swift compile, e.g., a source range is
comprised of two valid source locations or two invalid ones. As a
result, this patch adds approximate source locations to some separators
like braces or parens that are not maintained by Clang. Having slightly
incorrect ranges in this case is better than emitting unknown source
locations.
2024-10-30 10:58:26 +00:00
fahadnayyar
e640ed637d [cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types 2024-10-29 17:31:15 -07:00
Egor Zhdan
ffb22d7287 Merge pull request #73297 from swiftlang/egorzhdan/correct-check-for-copy-ctor
[cxx-interop] Check the presence of copy constructor correctly
2024-10-29 17:22:49 +00:00
Egor Zhdan
fbbec48c76 [cxx-interop] Check the presence of copy constructor correctly
This removes a longstanding workaround in the import logic for C++ structs:
Swift assumed that if a C++ struct has no copy constructor that is explicitly deleted, then the struct is copyable. This is not actually correct. This replaces the workaround with a proper check for the presence of a C++ copy constructor.

rdar://136838485
2024-10-28 20:05:57 +00:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Hiroshi Yamauchi
3c501656a1 Merge pull request #76324 from hjyamauchi/issue74866
Fix the IR gen for C++ method calls and refactor around CGFunctionInfo
2024-10-28 12:07:36 -07:00
Dylan Sturgeon
a1e888785d Enable indexing for refs to synthesized declarations.
Based on feedback in PR https://github.com/swiftlang/swift/pull/69460, enabling indexing for synthesized decls because they are usable by users and make sense to appear in the indexstore.

Sets `synthesized` on some additional decls:

  - derived `hashInto(...)`
  - Objc properties and methods derived from Objc protocols

https://github.com/apple/swift/issues/67446
2024-10-28 10:07:27 -07:00