Commit Graph

143 Commits

Author SHA1 Message Date
Ben Barham
05d62155f8 Merge remote-tracking branch 'origin/main' into manual-main-20240814
Conflicts:
  - `include/swift/Localization/LocalizationFormat.h`
  - `lib/ClangImporter/SwiftLookupTable.cpp`
  - `lib/ClangImporter/SwiftLookupTable.h`
  - `lib/Serialization/ModuleFormat.h`
  - `lib/Serialization/Serialization.cpp`

All from the hash changes being added to main. Took main except for the
lookup table minor version, which needs to be bumped still because of
other changes.
2024-08-14 19:21:52 -07:00
Ben Barham
273d7ee79d [Serialization] Do not serialize unstable hashes
https://github.com/llvm/llvm-project/pull/96282 changed
`get_execution_seed` to be non-deterministic. Use stable hashes instead.
2024-08-13 19:51:35 -07:00
Ben Barham
cfbc007535 [rebranch] Do not serialize unstable hashes
https://github.com/llvm/llvm-project/pull/96282 changed
`get_execution_seed` to be non-deterministic. Use stable hashes instead.
2024-08-01 14:33:44 -07:00
Becca Royal-Gordon
0a6e5874e2 Accommodate 64-bit clang serialization IDs
Several serialization IDs that used to be 32 bits are being widened to 64. Modify SwiftLookupTable and its supporting types to accommodate this.

The new design uses a 64-bit integer for the pointer, decl, macro, or identifier ID, plus a 32-bit integer for the submodule ID (this field is set to all ones to indicate a decl vs. a macro). An additional in-memory bool distinguishes pointer nodes from ID nodes. Advantages:

• The main ID is now 64 bits wide, accommodating recent changes in clang.
• We’re no longer stealing bits from clang (we *do* steal the max value of the submodule ID, though).
• There’s no on-disk bit that, when set, will cause an ID to be interpreted as a pointer.
• Design is robust against `clang::serialization::SubmoduleID` also becoming 64-bit (although this will waste space).

Fixes rdar://131134424.
2024-07-31 08:57:31 -07:00
Ben Barham
70c348129f [ClangImporter] Use new LocalDeclID
There were a number of upstream LLVM changes to `DeclID` in order to
separate "local" and "global" IDs:
07b1177eed
b8e3b2ad66
b467c6b536
https://github.com/llvm/llvm-project/pull/89873
d86cc73bbf
8af86025af
... and probably more.

This likely needs further cleaning up to not use `DeclID` at all.
2024-07-02 16:13:49 -07:00
Ben Barham
b7954411ec Merge remote-tracking branch 'origin/main' into manually-merge-main-to-rebranch
Conflicts:
  - `include/swift/AST/PluginRegistry.h`
2024-06-27 14:56:11 -07:00
Hamish Knight
bb4d0126f4 [ClangImporter] Switch lookupGlobalsAsMembers to take a non-optional EffectiveClangContext
The only caller is `loadNamedMembers`, and that
passes in a non-optional EffectiveClangContext,
meaning that we'd miss the case when
`getEffectiveClangContext` returns `nullptr`, crashing
in `translateContext`. No test case unfortunately
as I haven't been able to come up with a reproducer.

rdar://129619711
2024-06-25 12:27:48 +01:00
Xi Ge
736ccef626 Merge remote-tracking branch 'apple/main' into rebranch 2024-06-20 15:16:55 -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
Ben Barham
cacfd3e3ae Rename llvm::support::endianness to llvm::endianness
LLVM is gearing up to move to `std::endianness` and as part of that has
moved `llvm::support::endianness` to `llvm::endianness`
(bbdbcd83e6702f314d147a680247058a899ba261). Rename our uses.
2024-04-08 08:58:58 -07:00
Ben Barham
f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -08: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
Egor Zhdan
c8017e7d9f [cxx-interop] Add friend operators to the lookup table properly
Previously, `friend` operators declared in C++ classes were added to the lookup table when the class is being imported.

The operators were added to the wrong lookup table if the class is declared in a C++ namespace. Since a namespace can span across multiple Clang modules, its contents should be added to a translation unit level lookup table, not to a module level lookup table.

This change makes sure we add `friend` operators to the lookup table earlier, when we are actually building the lookup table. Note that this is not possible for class template instantiations, because those are instantiated later, so for templates we still handle `friend` operators when importing the instantiation.

rdar://116349899
2023-12-08 18:48:58 +00:00
Egor Zhdan
efc008a2ca [cxx-interop] Import using decls that expose methods from private base classes
If a C++ type `Derived` inherits from `Base` privately, the public methods from `Base` should not be callable on an instance of `Derived`. However, C++ supports exposing such methods via a using declaration: `using MyPrivateBase::myPublicMethod;`.

MSVC started using this feature for `std::optional` which means Swift doesn't correctly import `var pointee: Pointee` for instantiations of `std::optional` on Windows. This prevents the automatic conformance to `CxxOptional` from being synthesized.

 rdar://114282353 / resolves https://github.com/apple/swift/issues/68068
2023-11-14 00:30:54 +00:00
Egor Zhdan
64401aeeea [ClangImporter] NFC: Remove unused definitions 2023-07-20 19:37:58 +01: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
zoecarver
4c73369366 Revert "[cxx-interop] Add debug dump when we hit The Flakey Issue™."
This reverts commit 75dc69a81d.
2023-03-21 13:27:17 -07:00
zoecarver
00dc5406fb Revert "[nfc] Fix a few warnings I introduced."
This reverts commit 077d3cb8b3.
2023-03-21 13:27:11 -07:00
zoecarver
077d3cb8b3 [nfc] Fix a few warnings I introduced. 2023-03-17 13:49:47 -07:00
zoecarver
75dc69a81d [cxx-interop] Add debug dump when we hit The Flakey Issue™.
This commit should be reverted in a week or two. Sorry.
2023-03-15 12:46:53 -07:00
Egor Zhdan
aeebef9396 [cxx-interop][rebranch] Handle clang elaborated types, part 2
This applies the same trick as https://github.com/apple/swift/pull/62190 for newly written code.

Fixes two C++ interop tests in rebranch:
* `Interop/Cxx/enum/c-enums-NS_OPTIONS-api-notes-renamed-options.swift`
* `Interop/Cxx/enum/c-enums-NS_OPTIONS-swift-named-options.swift`

rdar://102858524
2022-12-01 15:46:33 +00:00
Nuri Amari
5f5bebfe3e Correct effective context translation for NS_OPTIONS anon C++ enums
Prior to this patch, SwiftLookupTable::translateDeclToContext relied
on the `TypedefNameDeclOrQualifier` field of an anonymous tag decl to
create a name for entry representing an anonymous tag in the lookup
table. This field is not always populated by Clang, it is often
populated only for the purposes of generating a linkage name when the
type is introduced via typedef as follows:

```
typedef enum { option1, option2} MyAnonEnum;
```

The field is not populated for anonymous enums introduced by NS_OPTIONS
with cxx interop enabled. This patch adds a fallback check in
`translateDeclToContext` that if the field is empty, check if the enum
is backed by a typedef that is unavailable in Swift. If that is the
case, use that name for the lookup table entry.
2022-11-18 11:12:05 -08:00
Egor Zhdan
ddf2ea28c0 [cxx-interop] Improve performance of importing C++ namespaces, part 2
Typedef-ed template types are now added to the lookup table in `addEntryToLookupTable`, there is no need to duplicate this logic in `populateTableWithDecl`.
2022-07-29 14:31:19 +01:00
Egor Zhdan
160b332403 Merge pull request #60262 from apple/egorzhdan/cxx-namespace-perf
[cxx-interop] Improve performance of importing C++ namespaces
2022-07-27 23:08:46 +01:00
Egor Zhdan
fbb3762c4f [cxx-interop] Improve performance of importing C++ namespaces
When ClangImporter calls Clang to import a module, Clang calls `SwiftLookupTableWriter::writeExtensionContents` for each namespace redecl, which called `addEntryToLookupTable` (a few frames omitted) which iterated over all of the namespace redecls, doing duplicate work. This caused the complexity to degrade from linear to quadratic.

`SwiftDeclConverter::VisitNamespaceDecl` is still called once per namespace, not per redecl, so we need to iterate over all the redecls there.

This dramatically improves the build time for SwiftCompilerSources when importing Clang headers in addition to LLVM headers.
2022-07-27 20:24:52 +01:00
Egor Zhdan
900ca68560 [cxx-interop] Make sure to print template instantiations in the module interface
#60246
2022-07-26 20:30:15 +01:00
zoecarver
a353d6f272 [cxx-interop] Fix lazy member lookup for forward-declared structs inside of a namespace. 2022-07-05 11:54:02 -07:00
Egor Zhdan
c952fc175b [cxx-interop] Fix lookup of class template instantiations
To fix a crash during deserialization, we need to store the mangled names of C++ class template instantiations in the lookup table.

For example, `std::string` will be serialized as `__CxxTemplateInstNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE`, and when another instance of the Swift compiler will later attempt to deserialize a decl referencing this instantiation, it will query the `SwiftLookupTable` for a decl with this name. If this instantiation is not referenced from the module that is currently being compiled, Swift will not instantiate `std::string` during typechecking, and the mangled name will be missing from `SwiftLookupTable`, leading to a deserialization failure. This happens when compiling with cross-module optimization enabled, and the current module does not reference `std::string`, but one of its dependencies does.
2022-04-29 21:55:59 +01:00
Egor Zhdan
2285c518f7 Merge pull request #58428 from apple/egorzhdan/cxx-sil-deserialization
[cxx-interop] Fix SIL deserialization error
2022-04-27 20:31:20 +02:00
Egor Zhdan
2472dfc32e [cxx-interop] Fix SIL deserialization error
Make sure nested namespaces are added to the SwiftLookupTable when they are declared in a non-canonical redecl of the parent namespace.

This was blocking C++ interop adoption in SwiftCompilerSources.
2022-04-27 01:10:46 +01:00
Robert Widmann
3394c6bc29 Remove Bogus Macro Assert in LUT Building
This assertion assumes that only visibility directives for builtin-in
macros find their way here. However, the presence of textual headers
in modules means any macro directive can wind up here since clang will
report that it is currently not building a module.

It's been half a decade since this code did anything useful with
macro directives in textual headers, so just leave things be.

rdar://91768988
2022-04-18 17:22:34 -07:00
swift-ci
ee96d2798c Merge remote-tracking branch 'origin/main' into rebranch 2021-11-30 08:33:02 -08:00
Saleem Abdulrasool
910fbee14e gardening: make c++98-compat-extra-semi an error
This cleans up 90 instances of this warning and reduces the build spew
when building on Linux.  This helps identify actual issues when
building which can get lost in the stream of warning messages.  It also
helps restore the ability to build the compiler with gcc.
2021-11-27 11:40:17 -08:00
Ben Barham
a6f99a08c4 [rebranch][ClangImporter] Update hashExtension to use HashBuilder
llvm-project updated `hashExtension` in
655bea4226b401a11164f99c6344e38d8742b8e4 to use a `HashBuilder` rather
than `hash_code`. Update use in ClangImporter.
2021-11-13 15:33:09 +10:00
Becca Royal-Gordon
689c2a510e [ClangImporter] Fix (lack of) name importing for ObjC ivars
When apple/swift#39664 moved the logic for generating anonymous fields' names from ImportDecl to ImportName, it inadvertently replaced a check that the decl was *precisely* `clang::FieldDecl` with a check that it was `FieldDecl` *or a subclass*. This could cause a crash when it tried to call `FieldDecl::getFieldIndex()`, which doesn't work properly on instance variables even though `ObjCIvarDecl` is a subclass of `FieldDecl`. The easiest way to reproduce this is to use a bit field in a class's instance variables, since clang inserts an anonymous instance variable after it for padding.

This commit adds a test of a bit field instance variable and fixes the bug. It also adds a PrettyStackTrace frame in the Swift lookup table preparation code, which should make other bugs like this easier to diagnose.

Fixes rdar://85173321.
2021-11-09 17:54:53 -08:00
zoecarver
f8abf50855 [cxx-interop] Bump data length to use uint32_t.
We're seeing some C++ projects that cause an integer overflow when this is only 16 bits.
2021-10-22 14:40:21 -07:00
Doug Gregor
b86e39d614 Remove "spawn" modifier 2021-07-26 10:22:02 -07:00
Saleem Abdulrasool
714eaefc78 Importer: remove ImportAsMember support
This functionality is not actively in use and the last usage of this has
been removed.  Remove the infrastructure that is no longer in need.
2021-03-22 08:53:56 -07:00
Becca (née Brent) Royal-Gordon
0f6925020b Merge pull request #36198 from beccadax/a-swift-by-any-other-name-would-warn-as-bleat
Fix rebranch SwiftNameAttr warning regression
2021-03-01 22:06:38 -08:00
Becca Royal-Gordon
4e0905ae3f Fix rebranch SwiftNameAttr warning regression
A change in the new clang branch seems to have caused it to start applying SwiftNameAttrs to forward declarations. We have apparently always tried to add these forward declarations to the lookup tables in PCH files, but never diagnosed the resulting failures because they did not have SwiftNameAttrs. Now they do, so we started emitting incorrect warnings.

We *probably* don’t need to process these at all, but there’s a risk of unintended behavior changes from that; instead, this commit takes a conservative approach and simply suppresses the warnings like we always have.

Fixes rdar://74710976.
2021-03-01 15:33:05 -08:00
Doug Gregor
77105a4ee5 Enable SE-0297: Concurrency Interoperability with Objective-C 2021-02-24 14:18:16 -08:00
Arnold Schwaighofer
ca9e258dd1 Adjust to removal of RecordLayout.h header 2020-12-08 11:58:32 -08:00
Doug Gregor
1e5d30f5ca [Concurrency] Import Objective-C methods with completion handlers as async
When a given Objective-C method has a completion handler parameter
with an appropriate signature, import that Objective-C method as
async. For example, consider the following CloudKit API:

    - (void)fetchShareParticipantWithUserRecordID:(CKRecordID
*)userRecordID
            completionHandler:(void (^)(CKShareParticipant * _Nullable shareParticipant, NSError * _Nullable error))completionHandler;

With the experimental concurrency model, this would import as:

    func fetchShareParticipant(withUserRecordID userRecordID: CKRecord.ID) async throws -> CKShare.Participant?

The compiler will be responsible for turning the caller's continuation
into a block to pass along to the completion handler. When the error
parameter of the completion handler is non-null, the async call
will result in that error being thrown. Otherwise, the other arguments
passed to that completion handler will be returned as the result of
the async call.

async versions of methods are imported alongside their
completion-handler versions, to maintain source compatibility with
existing code that provides a completion handler.

Note that this only covers the Clang importer portion of this task.
2020-08-27 21:30:13 -07:00
Marcel Hlopko
cb537b41fb [cxx-interop] Import typedef-ed template instantiations #32950 (#33451)
This is a roll-forward of https://github.com/apple/swift/pull/32950, with explicit c++17 version removed from tests. This is not needed since C++17 is the default anyway.

--

In this PR we teach `ClangImporter` to import typedef statements with template instantiation as its underlying type.

```c++
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};

typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
```

will be made available in Swift as if `WrappedMagicNumber` is a regular struct. 

In C++, multiple distinct typedeffed instantiations resolve to the same canonical type. We implement this by creating a hidden intermediate struct that typedef aliasses.

The struct is named as `__CxxTemplateInst` plus Itanium mangled type of the instantiation. For the example above the name of the hidden struct is `__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Double underscore (denoting a reserved C++ identifier) is used to discourage direct usage. We chose Itanium mangling scheme because it produces valid Swift identifiers and covers all C++ edge cases.

Imported module interface of the example above:

```swift
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
  var t: MagicNumber
}
struct MagicNumber {}
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
```

We modified the `SwiftLookupTable` logic to show hidden structs in `swift_ide_test` for convenience.

Co-authored-by: Rosica Dejanovska <rosica@google.com>
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Robert Widmann <devteam.codafi@gmail.com>
2020-08-14 20:51:16 +02:00
Meghana Gupta
6b61818fff Revert "[cxx-interop] Import typedef-ed template instantiations (#32950)"
This reverts commit 643aa2d896.
2020-08-12 12:37:13 -07:00
Marcel Hlopko
643aa2d896 [cxx-interop] Import typedef-ed template instantiations (#32950)
In this PR we teach `ClangImporter` to import typedef statements with template instantiation as its underlying type.

```c++
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};

typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
```

will be made available in Swift as if `WrappedMagicNumber` is a regular struct. 

In C++, multiple distinct typedeffed instantiations resolve to the same canonical type. We implement this by creating a hidden intermediate struct that typedef aliasses.

The struct is named as `__CxxTemplateInst` plus Itanium mangled type of the instantiation. For the example above the name of the hidden struct is `__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Double underscore (denoting a reserved C++ identifier) is used to discourage direct usage. We chose Itanium mangling scheme because it produces valid Swift identifiers and covers all C++ edge cases.

Imported module interface of the example above:

```swift
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
  var t: MagicNumber
}
struct MagicNumber {}
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
```

We modified the `SwiftLookupTable` logic to show hidden structs in `swift_ide_test` for convenience.

Resolves https://bugs.swift.org/browse/SR-12591.

Co-authored-by: Rosica Dejanovska <rosica@google.com>
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Robert Widmann <devteam.codafi@gmail.com>
2020-08-12 16:54:22 +02:00
Michael Forster
26358c4588 Import member operator functions as static members (#32293)
This adds support to `ClangImporter` to import C++ member function operators as static methods into Swift, which is part of SR-12748.

The left-hand-side operand, which gets passed as the `this` pointer to the C++ function is represented as an additional first parameter in the Swift method. It gets mapped back in SILGen.

Two of the tests are disabled on Windows because we can't yet call member functions correctly on Windows (SR-13129).
2020-07-03 11:06:22 +02:00
Michael Forster
fae87c96d7 Move interleave(...) to the llvm namespace
This simplifies fixing the master-next build. Upstream LLVM already
has a copy of this function, so on master-next we only need to delete
the Swift copy, reducing the potential for merge conflicts.
2020-04-17 11:20:50 +02:00
Arnold Schwaighofer
0c99cd9734 Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-10 08:00:41 -08:00