mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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.
88 lines
3.1 KiB
C++
88 lines
3.1 KiB
C++
//===--- ClangImporterRequests.cpp - Clang Importer Requests --------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2021 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/AST/NameLookupRequests.h"
|
|
#include "swift/AST/ASTContext.h"
|
|
#include "swift/AST/Decl.h"
|
|
#include "swift/AST/Evaluator.h"
|
|
#include "swift/AST/Module.h"
|
|
#include "swift/AST/SourceFile.h"
|
|
#include "swift/ClangImporter/ClangImporterRequests.h"
|
|
#include "swift/Subsystems.h"
|
|
|
|
using namespace swift;
|
|
|
|
llvm::Optional<ObjCInterfaceAndImplementation>
|
|
ObjCInterfaceAndImplementationRequest::getCachedResult() const {
|
|
auto passedDecl = std::get<0>(getStorage());
|
|
if (!passedDecl)
|
|
return {};
|
|
|
|
auto cachedDecl = passedDecl->getCachedObjCImplementationDecl();
|
|
|
|
// !cachedDecl means that no decl has been cached and we need to evaluate the
|
|
// request.
|
|
if (!cachedDecl)
|
|
return llvm::None;
|
|
|
|
// nullptr cachedDecl means that the lack of a decl was cached.
|
|
else if (!*cachedDecl)
|
|
return {};
|
|
|
|
// A decl was cached! Arbitrarily guess that we looked up the implementation
|
|
// from the interface.
|
|
ObjCInterfaceAndImplementation result{passedDecl, *cachedDecl};
|
|
|
|
// An imported decl can only be an interface; a native decl can only be an
|
|
// implementation. If `implementationDecl` has a Clang node, we must have
|
|
// looked up the interface from the implementation.
|
|
if (result.implementationDecl->hasClangNode())
|
|
std::swap(result.interfaceDecl, result.implementationDecl);
|
|
|
|
return result;
|
|
}
|
|
|
|
void ObjCInterfaceAndImplementationRequest::
|
|
cacheResult(ObjCInterfaceAndImplementation value) const {
|
|
Decl *decl = std::get<0>(getStorage());
|
|
|
|
if (!value) {
|
|
// `decl` is neither an interface nor an implementation.
|
|
decl->setCachedObjCImplementationDecl(nullptr);
|
|
return;
|
|
}
|
|
|
|
// Cache computed pointers from implementations to interfaces.
|
|
value.interfaceDecl->
|
|
setCachedObjCImplementationDecl(value.implementationDecl);
|
|
value.implementationDecl->
|
|
setCachedObjCImplementationDecl(value.interfaceDecl);
|
|
|
|
// If this was a duplicate implementation, cache a null so we don't recompute.
|
|
if (decl != value.interfaceDecl && decl != value.implementationDecl)
|
|
decl->setCachedObjCImplementationDecl(nullptr);
|
|
}
|
|
|
|
// Define request evaluation functions for each of the name lookup requests.
|
|
static AbstractRequestFunction *clangImporterRequestFunctions[] = {
|
|
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
|
|
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
|
|
#include "swift/ClangImporter/ClangImporterTypeIDZone.def"
|
|
#undef SWIFT_REQUEST
|
|
};
|
|
|
|
void swift::registerClangImporterRequestFunctions(Evaluator &evaluator) {
|
|
evaluator.registerRequestFunctions(Zone::ClangImporter,
|
|
clangImporterRequestFunctions);
|
|
}
|
|
|