1326941Sdim//===--- RefactoringActionRuleRequirements.h - Clang refactoring library --===// 2326941Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6326941Sdim// 7326941Sdim//===----------------------------------------------------------------------===// 8326941Sdim 9326941Sdim#ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H 10326941Sdim#define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H 11326941Sdim 12326941Sdim#include "clang/Basic/LLVM.h" 13326941Sdim#include "clang/Tooling/Refactoring/ASTSelection.h" 14326941Sdim#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h" 15326941Sdim#include "clang/Tooling/Refactoring/RefactoringOption.h" 16326941Sdim#include "clang/Tooling/Refactoring/RefactoringRuleContext.h" 17326941Sdim#include "llvm/Support/Error.h" 18326941Sdim#include <type_traits> 19326941Sdim 20326941Sdimnamespace clang { 21326941Sdimnamespace tooling { 22326941Sdim 23326941Sdim/// A refactoring action rule requirement determines when a refactoring action 24326941Sdim/// rule can be invoked. The rule can be invoked only when all of the 25326941Sdim/// requirements are satisfied. 26326941Sdim/// 27326941Sdim/// Subclasses must implement the 28326941Sdim/// 'Expected<T> evaluate(RefactoringRuleContext &) const' member function. 29326941Sdim/// \c T is used to determine the return type that is passed to the 30326941Sdim/// refactoring rule's constructor. 31326941Sdim/// For example, the \c SourceRangeSelectionRequirement subclass defines 32326941Sdim/// 'Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const' 33326941Sdim/// function. When this function returns a non-error value, the resulting 34326941Sdim/// source range is passed to the specific refactoring action rule 35326941Sdim/// constructor (provided all other requirements are satisfied). 36326941Sdimclass RefactoringActionRuleRequirement { 37326941Sdim // Expected<T> evaluate(RefactoringRuleContext &Context) const; 38326941Sdim}; 39326941Sdim 40326941Sdim/// A base class for any requirement that expects some part of the source to be 41326941Sdim/// selected in an editor (or the refactoring tool with the -selection option). 42326941Sdimclass SourceSelectionRequirement : public RefactoringActionRuleRequirement {}; 43326941Sdim 44326941Sdim/// A selection requirement that is satisfied when any portion of the source 45326941Sdim/// text is selected. 46326941Sdimclass SourceRangeSelectionRequirement : public SourceSelectionRequirement { 47326941Sdimpublic: 48326941Sdim Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const { 49326941Sdim if (Context.getSelectionRange().isValid()) 50326941Sdim return Context.getSelectionRange(); 51326941Sdim return Context.createDiagnosticError(diag::err_refactor_no_selection); 52326941Sdim } 53326941Sdim}; 54326941Sdim 55326941Sdim/// An AST selection requirement is satisfied when any portion of the AST 56326941Sdim/// overlaps with the selection range. 57326941Sdim/// 58326941Sdim/// The requirement will be evaluated only once during the initiation and 59326941Sdim/// search of matching refactoring action rules. 60326941Sdimclass ASTSelectionRequirement : public SourceRangeSelectionRequirement { 61326941Sdimpublic: 62326941Sdim Expected<SelectedASTNode> evaluate(RefactoringRuleContext &Context) const; 63326941Sdim}; 64326941Sdim 65326941Sdim/// A selection requirement that is satisfied when the selection range overlaps 66326941Sdim/// with a number of neighbouring statements in the AST. The statemenst must be 67326941Sdim/// contained in declaration like a function. The selection range must be a 68326941Sdim/// non-empty source selection (i.e. cursors won't be accepted). 69326941Sdim/// 70326941Sdim/// The requirement will be evaluated only once during the initiation and search 71326941Sdim/// of matching refactoring action rules. 72326941Sdim/// 73326941Sdim/// \see CodeRangeASTSelection 74326941Sdimclass CodeRangeASTSelectionRequirement : public ASTSelectionRequirement { 75326941Sdimpublic: 76326941Sdim Expected<CodeRangeASTSelection> 77326941Sdim evaluate(RefactoringRuleContext &Context) const; 78326941Sdim}; 79326941Sdim 80326941Sdim/// A base class for any requirement that requires some refactoring options. 81326941Sdimclass RefactoringOptionsRequirement : public RefactoringActionRuleRequirement { 82326941Sdimpublic: 83326941Sdim virtual ~RefactoringOptionsRequirement() {} 84326941Sdim 85326941Sdim /// Returns the set of refactoring options that are used when evaluating this 86326941Sdim /// requirement. 87326941Sdim virtual ArrayRef<std::shared_ptr<RefactoringOption>> 88326941Sdim getRefactoringOptions() const = 0; 89326941Sdim}; 90326941Sdim 91326941Sdim/// A requirement that evaluates to the value of the given \c OptionType when 92326941Sdim/// the \c OptionType is a required option. When the \c OptionType is an 93326941Sdim/// optional option, the requirement will evaluate to \c None if the option is 94326941Sdim/// not specified or to an appropriate value otherwise. 95326941Sdimtemplate <typename OptionType> 96326941Sdimclass OptionRequirement : public RefactoringOptionsRequirement { 97326941Sdimpublic: 98326941Sdim OptionRequirement() : Opt(createRefactoringOption<OptionType>()) {} 99326941Sdim 100326941Sdim ArrayRef<std::shared_ptr<RefactoringOption>> 101326941Sdim getRefactoringOptions() const final override { 102326941Sdim return Opt; 103326941Sdim } 104326941Sdim 105326941Sdim Expected<typename OptionType::ValueType> 106326941Sdim evaluate(RefactoringRuleContext &) const { 107326941Sdim return static_cast<OptionType *>(Opt.get())->getValue(); 108326941Sdim } 109326941Sdim 110326941Sdimprivate: 111326941Sdim /// The partially-owned option. 112326941Sdim /// 113326941Sdim /// The ownership of the option is shared among the different requirements 114326941Sdim /// because the same option can be used by multiple rules in one refactoring 115326941Sdim /// action. 116326941Sdim std::shared_ptr<RefactoringOption> Opt; 117326941Sdim}; 118326941Sdim 119326941Sdim} // end namespace tooling 120326941Sdim} // end namespace clang 121326941Sdim 122326941Sdim#endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H 123