1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
//===--- RefactoringContinuations.h - Defines refactoring continuations ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LIB_TOOLING_REFACTOR_REFACTORING_CONTINUATIONS_H
#define LLVM_CLANG_LIB_TOOLING_REFACTOR_REFACTORING_CONTINUATIONS_H
#include "clang/AST/Decl.h"
#include "clang/Tooling/Refactor/IndexerQuery.h"
#include "clang/Tooling/Refactor/RefactoringOperation.h"
#include "llvm/ADT/StringMap.h"
#include <tuple>
namespace clang {
namespace tooling {
namespace detail {
struct ValidBase {};
/// The ContinuationPassType determine which type is passed into the refactoring
/// continuation.
template <typename T> struct ContinuationPassType { using Type = T; };
template <typename T> struct ContinuationPassType<std::vector<T>> {
using Type = ArrayRef<T>;
};
/// Refactoring operations can pass state to the continuations. Valid state
/// values should have a corresponding \c StateTraits specialization.
template <typename T> struct StateTraits {
/// Specializations should define the following types:
///
/// StoredResultType: The TU-specific type which is then passed into the
/// continuation function. The continuation receives the result whose type is
/// \c ContinuationPassType<StoredResultType>::Type.
///
/// PersistentType: The TU-independent type that's persisted even after the
/// TU in which the continuation was created is disposed.
};
template <typename T>
struct StateTraits<const T *>
: std::enable_if<std::is_base_of<Decl, T>::value, ValidBase>::type {
using StoredResultType = const T *;
using PersistentType = PersistentDeclRef<T>;
};
template <typename T>
struct StateTraits<ArrayRef<const T *>>
: std::enable_if<std::is_base_of<Decl, T>::value, ValidBase>::type {
using StoredResultType = std::vector<const T *>;
using PersistentType = std::vector<PersistentDeclRef<T>>;
};
template <typename T>
struct StateTraits<std::unique_ptr<indexer::ManyToManyDeclarationsQuery<T>>>
: std::enable_if<std::is_base_of<Decl, T>::value, ValidBase>::type {
using StoredResultType = std::vector<indexer::Indexed<const T *>>;
using PersistentType = std::vector<indexer::Indexed<PersistentDeclRef<T>>>;
};
template <> struct StateTraits<std::vector<std::string>> {
using StoredResultType = std::vector<std::string>;
using PersistentType = std::vector<std::string>;
};
/// Conversion functions convert the TU-specific state to a TU independent
/// state and vice-versa.
template <typename T>
PersistentDeclRef<T> convertToPersistentRepresentation(
const T *Declaration,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
return PersistentDeclRef<T>::create(Declaration);
}
template <typename T>
std::vector<PersistentDeclRef<T>> convertToPersistentRepresentation(
ArrayRef<const T *> Declarations,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
std::vector<PersistentDeclRef<T>> Result;
Result.reserve(Declarations.size());
for (const T *D : Declarations)
Result.push_back(PersistentDeclRef<T>::create(D));
return Result;
}
template <typename T>
std::vector<indexer::Indexed<PersistentDeclRef<T>>>
convertToPersistentRepresentation(
std::unique_ptr<indexer::ManyToManyDeclarationsQuery<T>> &Query,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
Query->invalidateTUSpecificState();
return Query->getOutput();
}
inline std::vector<std::string>
convertToPersistentRepresentation(const std::vector<std::string> &Values) {
return Values;
}
/// Converts the TU-independent state to the TU-specific state.
class PersistentToASTSpecificStateConverter {
ASTContext &Context;
llvm::StringMap<const Decl *> ConvertedDeclRefs;
const Decl *lookupDecl(StringRef USR);
public:
// FIXME: We can hide the addConvertible/convert interface so that
// the continuation will just invoke one conversion function for the entire
// tuple.
PersistentToASTSpecificStateConverter(ASTContext &Context)
: Context(Context) {}
template <typename T>
bool addConvertible(
const PersistentDeclRef<T> &Ref,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
if (!Ref.USR.empty())
ConvertedDeclRefs[Ref.USR] = nullptr;
return true;
}
template <typename T>
const T *
convert(const PersistentDeclRef<T> &Ref,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
return dyn_cast_or_null<T>(lookupDecl(Ref.USR));
}
template <typename T>
bool addConvertible(
const std::vector<PersistentDeclRef<T>> &Refs,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
for (const auto &Ref : Refs) {
if (!Ref.USR.empty())
ConvertedDeclRefs[Ref.USR] = nullptr;
}
return true;
}
template <typename T>
std::vector<const T *>
convert(const std::vector<PersistentDeclRef<T>> &Refs,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
std::vector<const T *> Results;
Results.reserve(Refs.size());
// Allow nulls in the produced array, the continuation will have to deal
// with them by itself.
for (const auto &Ref : Refs)
Results.push_back(dyn_cast_or_null<T>(lookupDecl(Ref.USR)));
return Results;
}
template <typename T>
bool addConvertible(
const std::vector<indexer::Indexed<PersistentDeclRef<T>>> &Refs,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
for (const auto &Ref : Refs) {
if (!Ref.Decl.USR.empty())
ConvertedDeclRefs[Ref.Decl.USR] = nullptr;
}
return true;
}
template <typename T>
std::vector<indexer::Indexed<const T *>>
convert(const std::vector<indexer::Indexed<PersistentDeclRef<T>>> &Refs,
typename std::enable_if<std::is_base_of<Decl, T>::value>::type * =
nullptr) {
std::vector<indexer::Indexed<const T *>> Results;
Results.reserve(Refs.size());
// Allow nulls in the produced array, the continuation will have to deal
// with them by itself.
for (const auto &Ref : Refs)
Results.push_back(indexer::Indexed<const T *>(
dyn_cast_or_null<T>(lookupDecl(Ref.Decl.USR)), Ref.IsNotDefined));
return Results;
}
bool addConvertible(const PersistentFileID &) {
// Do nothing since FileIDs are converted one-by-one.
return true;
}
FileID convert(const PersistentFileID &Ref);
bool addConvertible(const std::vector<std::string> &) { return true; }
std::vector<std::string> convert(const std::vector<std::string> &Values) {
return Values;
}
/// Converts the added persistent state into TU-specific state using one
/// efficient operation.
void runCoalescedConversions();
};
template <typename T, typename ASTQueryType, typename... QueryOrState>
struct ContinuationFunction {
using Type = llvm::Expected<RefactoringResult> (*)(
ASTContext &, const T &,
typename ContinuationPassType<
typename StateTraits<QueryOrState>::StoredResultType>::Type...);
template <size_t... Is>
static llvm::Expected<RefactoringResult> dispatch(
Type Fn, detail::PersistentToASTSpecificStateConverter &Converter,
ASTContext &Context, const ASTQueryType &Query,
const std::tuple<typename StateTraits<QueryOrState>::StoredResultType...>
&Arguments,
std::index_sequence<Is...>) {
auto ASTQueryResult = Converter.convert(Query.getResult());
return Fn(Context, ASTQueryResult, std::get<Is>(Arguments)...);
}
};
template <typename ASTQueryType, typename... QueryOrState>
struct ContinuationFunction<void, ASTQueryType, QueryOrState...> {
using Type = llvm::Expected<RefactoringResult> (*)(
ASTContext &,
typename ContinuationPassType<
typename StateTraits<QueryOrState>::StoredResultType>::Type...);
template <size_t... Is>
static llvm::Expected<RefactoringResult> dispatch(
Type Fn, detail::PersistentToASTSpecificStateConverter &,
ASTContext &Context, const ASTQueryType &,
const std::tuple<typename StateTraits<QueryOrState>::StoredResultType...>
&Arguments,
std::index_sequence<Is...>) {
return Fn(Context, std::get<Is>(Arguments)...);
}
};
/// The refactoring contination contains a set of structures that implement
/// the refactoring operation continuation mechanism.
template <typename ASTQueryType, typename... QueryOrState>
class SpecificRefactoringContinuation final : public RefactoringContinuation {
public:
static_assert(std::is_base_of<indexer::ASTProducerQuery, ASTQueryType>::value,
"Invalid AST Query");
// TODO: Validate the QueryOrState types.
/// The consumer function is the actual continuation. It receives the state
/// that was passed-in in the request or the results of the indexing queries
/// that were passed-in in the request.
using ConsumerFn =
typename ContinuationFunction<typename ASTQueryType::ResultTy,
ASTQueryType, QueryOrState...>::Type;
private:
ConsumerFn Consumer;
std::unique_ptr<ASTQueryType> ASTQuery;
/// Inputs store state that's dependent on the original TU.
std::optional<std::tuple<QueryOrState...>> Inputs;
/// State contains TU-independent values.
std::optional<
std::tuple<typename StateTraits<QueryOrState>::PersistentType...>>
State;
/// Converts a tuple that contains the TU dependent state to a tuple with
/// TU independent state.
template <size_t... Is>
std::tuple<typename StateTraits<QueryOrState>::PersistentType...>
convertToPersistentImpl(std::index_sequence<Is...>) {
assert(Inputs && "TU-dependent state is already converted");
return std::make_tuple(
detail::convertToPersistentRepresentation(std::get<Is>(*Inputs))...);
}
template <typename T>
bool gatherQueries(
std::vector<indexer::IndexerQuery *> &Queries,
const std::unique_ptr<T> &Query,
typename std::enable_if<
std::is_base_of<indexer::IndexerQuery, T>::value>::type * = nullptr) {
Queries.push_back(Query.get());
return true;
}
template <typename T>
bool gatherQueries(std::vector<indexer::IndexerQuery *> &, const T &) {
// This input element is not a query.
return true;
}
template <size_t... Is>
std::vector<indexer::IndexerQuery *>
gatherQueries(std::index_sequence<Is...>) {
assert(Inputs && "TU-dependent state is already converted");
std::vector<indexer::IndexerQuery *> Queries;
std::make_tuple(gatherQueries(Queries, std::get<Is>(*Inputs))...);
return Queries;
}
/// Calls the consumer function with the given \p Context and the state
/// whose values are converted from the TU-independent to TU-specific values.
template <size_t... Is>
llvm::Expected<RefactoringResult> dispatch(ASTContext &Context,
std::index_sequence<Is...> Seq) {
assert(State && "TU-independent state is not yet produced");
detail::PersistentToASTSpecificStateConverter Converter(Context);
(void)std::make_tuple(Converter.addConvertible(std::get<Is>(*State))...);
Converter.runCoalescedConversions();
auto Results = std::make_tuple(Converter.convert(std::get<Is>(*State))...);
// TODO: Check for errors?
return detail::ContinuationFunction<
typename ASTQueryType::ResultTy, ASTQueryType,
QueryOrState...>::dispatch(Consumer, Converter, Context, *ASTQuery,
Results, Seq);
}
public:
SpecificRefactoringContinuation(ConsumerFn Consumer,
std::unique_ptr<ASTQueryType> ASTQuery,
QueryOrState... Inputs)
: Consumer(Consumer), ASTQuery(std::move(ASTQuery)),
Inputs(std::make_tuple(std::move(Inputs)...)) {}
SpecificRefactoringContinuation(SpecificRefactoringContinuation &&) = default;
SpecificRefactoringContinuation &
operator=(SpecificRefactoringContinuation &&) = default;
indexer::ASTProducerQuery *getASTUnitIndexerQuery() override {
return ASTQuery.get();
}
std::vector<indexer::IndexerQuery *> getAdditionalIndexerQueries() override {
return gatherQueries(std::index_sequence_for<QueryOrState...>());
}
/// Query results are fetched. State is converted to a persistent
/// representation.
void persistTUSpecificState() override {
ASTQuery->invalidateTUSpecificState();
State =
convertToPersistentImpl(std::index_sequence_for<QueryOrState...>());
Inputs = std::nullopt;
}
/// The state is converted to the AST representation in the given ASTContext
/// and the continuation is dispatched.
llvm::Expected<RefactoringResult>
runInExternalASTUnit(ASTContext &Context) override {
return dispatch(Context, std::index_sequence_for<QueryOrState...>());
}
};
} // end namespace detail
/// Returns a refactoring continuation that will run within the context of a
/// single external AST unit.
///
/// The indexer determines which AST unit should receive the continuation by
/// evaluation the AST query operation \p ASTQuery.
///
/// \param ASTQuery The query that will determine which AST unit should the
/// continuation run in.
///
/// \param Consumer The continuation function that will be called once the
/// external AST unit is loaded.
///
/// \param Inputs Each individiual input element can contain either some
/// state value that will be passed into the \p Consumer function or an
/// indexer query whose results will be passed into the \p Consumer function.
template <typename ASTQueryType, typename... QueryOrState>
typename std::enable_if<
std::is_base_of<indexer::ASTProducerQuery, ASTQueryType>::value,
std::unique_ptr<RefactoringContinuation>>::type
continueInExternalASTUnit(
std::unique_ptr<ASTQueryType> ASTQuery,
typename detail::SpecificRefactoringContinuation<
ASTQueryType, QueryOrState...>::ConsumerFn Consumer,
QueryOrState... Inputs) {
return std::make_unique<
detail::SpecificRefactoringContinuation<ASTQueryType, QueryOrState...>>(
Consumer, std::move(ASTQuery), std::move(Inputs)...);
}
} // end namespace tooling
} // end namespace clang
#endif // LLVM_CLANG_LIB_TOOLING_REFACTOR_REFACTORING_CONTINUATIONS_H
|