1a7dea167SDimitry Andric //===--- Stencil.h - Stencil class ------------------------------*- C++ -*-===//
2a7dea167SDimitry Andric //
3a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7dea167SDimitry Andric //
7a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8a7dea167SDimitry Andric ///
9a7dea167SDimitry Andric /// \file
10a7dea167SDimitry Andric /// This file defines the *Stencil* abstraction: a code-generating object,
11a7dea167SDimitry Andric /// parameterized by named references to (bound) AST nodes.  Given a match
12a7dea167SDimitry Andric /// result, a stencil can be evaluated to a string of source code.
13a7dea167SDimitry Andric ///
14a7dea167SDimitry Andric /// A stencil is similar in spirit to a format string: it is composed of a
15a7dea167SDimitry Andric /// series of raw text strings, references to nodes (the parameters) and helper
16a7dea167SDimitry Andric /// code-generation operations.
17a7dea167SDimitry Andric ///
18a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
19a7dea167SDimitry Andric 
20a7dea167SDimitry Andric #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_STENCIL_H_
21a7dea167SDimitry Andric #define LLVM_CLANG_TOOLING_TRANSFORMER_STENCIL_H_
22a7dea167SDimitry Andric 
23a7dea167SDimitry Andric #include "clang/AST/ASTContext.h"
24a7dea167SDimitry Andric #include "clang/AST/ASTTypeTraits.h"
25a7dea167SDimitry Andric #include "clang/ASTMatchers/ASTMatchFinder.h"
26a7dea167SDimitry Andric #include "clang/Tooling/Transformer/MatchConsumer.h"
27a7dea167SDimitry Andric #include "clang/Tooling/Transformer/RangeSelector.h"
28a7dea167SDimitry Andric #include "llvm/ADT/StringRef.h"
29a7dea167SDimitry Andric #include "llvm/Support/Error.h"
30a7dea167SDimitry Andric #include <string>
31a7dea167SDimitry Andric #include <vector>
32a7dea167SDimitry Andric 
33a7dea167SDimitry Andric namespace clang {
34a7dea167SDimitry Andric namespace transformer {
35a7dea167SDimitry Andric 
36480093f4SDimitry Andric using StencilInterface = MatchComputation<std::string>;
37a7dea167SDimitry Andric 
38a7dea167SDimitry Andric /// A sequence of code fragments, references to parameters and code-generation
39480093f4SDimitry Andric /// operations that together can be evaluated to (a fragment of) source code or
40480093f4SDimitry Andric /// a diagnostic message, given a match result.
41480093f4SDimitry Andric ///
42480093f4SDimitry Andric /// We use a `shared_ptr` to allow for easy and cheap copying of stencils.
43480093f4SDimitry Andric /// Since `StencilInterface` is an immutable interface, the sharing doesn't
44480093f4SDimitry Andric /// impose any risks. Otherwise, we would have to add a virtual `copy` method to
45480093f4SDimitry Andric /// the API and implement it for all derived classes.
46480093f4SDimitry Andric using Stencil = std::shared_ptr<StencilInterface>;
47a7dea167SDimitry Andric 
48480093f4SDimitry Andric namespace detail {
49480093f4SDimitry Andric /// Convenience function to construct a \c Stencil. Overloaded for common cases
50480093f4SDimitry Andric /// so that user doesn't need to specify which factory function to use. This
51480093f4SDimitry Andric /// pattern gives benefits similar to implicit constructors, while maintaing a
52480093f4SDimitry Andric /// higher degree of explicitness.
53480093f4SDimitry Andric Stencil makeStencil(llvm::StringRef Text);
54480093f4SDimitry Andric Stencil makeStencil(RangeSelector Selector);
makeStencil(Stencil S)55480093f4SDimitry Andric inline Stencil makeStencil(Stencil S) { return S; }
56480093f4SDimitry Andric } // namespace detail
57480093f4SDimitry Andric 
58480093f4SDimitry Andric /// Constructs the string representing the concatenation of the given \p
59480093f4SDimitry Andric /// Parts. If only one element is passed in \p Parts, returns that element.
60480093f4SDimitry Andric Stencil catVector(std::vector<Stencil> Parts);
61480093f4SDimitry Andric 
62480093f4SDimitry Andric /// Concatenates 0+ stencil pieces into a single stencil. Arguments can be raw
63480093f4SDimitry Andric /// text, ranges in the matched code (\p RangeSelector) or other `Stencil`s.
cat(Ts &&...Parts)64480093f4SDimitry Andric template <typename... Ts> Stencil cat(Ts &&... Parts) {
65480093f4SDimitry Andric   return catVector({detail::makeStencil(std::forward<Ts>(Parts))...});
66a7dea167SDimitry Andric }
67a7dea167SDimitry Andric 
68a7dea167SDimitry Andric //
69a7dea167SDimitry Andric // Functions for conveniently building stencils.
70a7dea167SDimitry Andric //
71a7dea167SDimitry Andric 
72a7dea167SDimitry Andric /// Generates the source of the expression bound to \p Id, wrapping it in
73a7dea167SDimitry Andric /// parentheses if it may parse differently depending on context. For example, a
74a7dea167SDimitry Andric /// binary operation is always wrapped, while a variable reference is never
75a7dea167SDimitry Andric /// wrapped.
76480093f4SDimitry Andric Stencil expression(llvm::StringRef Id);
77a7dea167SDimitry Andric 
78a7dea167SDimitry Andric /// Constructs an idiomatic dereferencing of the expression bound to \p ExprId.
79a7dea167SDimitry Andric /// \p ExprId is wrapped in parentheses, if needed.
80480093f4SDimitry Andric Stencil deref(llvm::StringRef ExprId);
81480093f4SDimitry Andric 
82480093f4SDimitry Andric /// If \p ExprId is of pointer type, constructs an idiomatic dereferencing of
83480093f4SDimitry Andric /// the expression bound to \p ExprId, including wrapping it in parentheses, if
84480093f4SDimitry Andric /// needed. Otherwise, generates the original expression source.
85480093f4SDimitry Andric Stencil maybeDeref(llvm::StringRef ExprId);
86a7dea167SDimitry Andric 
87a7dea167SDimitry Andric /// Constructs an expression that idiomatically takes the address of the
88a7dea167SDimitry Andric /// expression bound to \p ExprId. \p ExprId is wrapped in parentheses, if
89a7dea167SDimitry Andric /// needed.
90480093f4SDimitry Andric Stencil addressOf(llvm::StringRef ExprId);
91480093f4SDimitry Andric 
92480093f4SDimitry Andric /// If \p ExprId is not a pointer type, constructs an expression that
93480093f4SDimitry Andric /// idiomatically takes the address of the expression bound to \p ExprId,
94480093f4SDimitry Andric /// including wrapping \p ExprId in parentheses, if needed. Otherwise, generates
95480093f4SDimitry Andric /// the original expression source.
96480093f4SDimitry Andric Stencil maybeAddressOf(llvm::StringRef ExprId);
97a7dea167SDimitry Andric 
98a7dea167SDimitry Andric /// Constructs a `MemberExpr` that accesses the named member (\p Member) of the
99a7dea167SDimitry Andric /// object bound to \p BaseId. The access is constructed idiomatically: if \p
100a7dea167SDimitry Andric /// BaseId is bound to `e` and \p Member identifies member `m`, then returns
101a7dea167SDimitry Andric /// `e->m`, when e is a pointer, `e2->m` when e = `*e2` and `e.m` otherwise.
102a7dea167SDimitry Andric /// Additionally, `e` is wrapped in parentheses, if needed.
103480093f4SDimitry Andric Stencil access(llvm::StringRef BaseId, Stencil Member);
access(llvm::StringRef BaseId,llvm::StringRef Member)104480093f4SDimitry Andric inline Stencil access(llvm::StringRef BaseId, llvm::StringRef Member) {
1055ffd83dbSDimitry Andric   return access(BaseId, detail::makeStencil(Member));
106a7dea167SDimitry Andric }
107a7dea167SDimitry Andric 
108a7dea167SDimitry Andric /// Chooses between the two stencil parts, based on whether \p ID is bound in
109a7dea167SDimitry Andric /// the match.
110480093f4SDimitry Andric Stencil ifBound(llvm::StringRef Id, Stencil TrueStencil, Stencil FalseStencil);
111a7dea167SDimitry Andric 
112a7dea167SDimitry Andric /// Chooses between the two strings, based on whether \p ID is bound in the
113a7dea167SDimitry Andric /// match.
ifBound(llvm::StringRef Id,llvm::StringRef TrueText,llvm::StringRef FalseText)114480093f4SDimitry Andric inline Stencil ifBound(llvm::StringRef Id, llvm::StringRef TrueText,
115a7dea167SDimitry Andric                        llvm::StringRef FalseText) {
1165ffd83dbSDimitry Andric   return ifBound(Id, detail::makeStencil(TrueText),
1175ffd83dbSDimitry Andric                  detail::makeStencil(FalseText));
118a7dea167SDimitry Andric }
119a7dea167SDimitry Andric 
120*349cc55cSDimitry Andric /// Chooses between multiple stencils, based on the presence of bound nodes. \p
121*349cc55cSDimitry Andric /// CaseStencils takes a vector of (ID, \c Stencil) pairs and checks each ID in
122*349cc55cSDimitry Andric /// order to see if it's bound to a node.  If so, the associated \c Stencil is
123*349cc55cSDimitry Andric /// run and all other cases are ignored.  An optional \p DefaultStencil can be
124*349cc55cSDimitry Andric /// provided to be run if all cases are exhausted beacause none of the provided
125*349cc55cSDimitry Andric /// IDs are bound.  If no default case is provided and all cases are exhausted,
126*349cc55cSDimitry Andric /// the stencil will fail with error `llvm::errc::result_out_of_range`.
127*349cc55cSDimitry Andric ///
128*349cc55cSDimitry Andric /// For example, say one matches a statement's type with:
129*349cc55cSDimitry Andric ///     anyOf(
130*349cc55cSDimitry Andric ///       qualType(isInteger()).bind("int"),
131*349cc55cSDimitry Andric ///       qualType(realFloatingPointType()).bind("float"),
132*349cc55cSDimitry Andric ///       qualType(isAnyCharacter()).bind("char"),
133*349cc55cSDimitry Andric ///       booleanType().bind("bool"))
134*349cc55cSDimitry Andric ///
135*349cc55cSDimitry Andric /// Then, one can decide in a stencil how to construct a literal.
136*349cc55cSDimitry Andric ///     cat("a = ",
137*349cc55cSDimitry Andric ///         selectBound(
138*349cc55cSDimitry Andric ///             {{"int", cat("0")},
139*349cc55cSDimitry Andric ///              {"float", cat("0.0")},
140*349cc55cSDimitry Andric ///              {"char", cat("'\\0'")},
141*349cc55cSDimitry Andric ///              {"bool", cat("false")}}))
142*349cc55cSDimitry Andric ///
143*349cc55cSDimitry Andric /// In addition, one could supply a default case for all other types:
144*349cc55cSDimitry Andric ///     selectBound(
145*349cc55cSDimitry Andric ///         {{"int", cat("0")},
146*349cc55cSDimitry Andric ///          ...
147*349cc55cSDimitry Andric ///          {"bool", cat("false")}},
148*349cc55cSDimitry Andric ///         cat("{}"))
149*349cc55cSDimitry Andric Stencil selectBound(std::vector<std::pair<std::string, Stencil>> CaseStencils,
150*349cc55cSDimitry Andric                     Stencil DefaultStencil = nullptr);
151*349cc55cSDimitry Andric 
152480093f4SDimitry Andric /// Wraps a \c MatchConsumer in a \c Stencil, so that it can be used in a \c
153480093f4SDimitry Andric /// Stencil.  This supports user-defined extensions to the \c Stencil language.
154480093f4SDimitry Andric Stencil run(MatchConsumer<std::string> C);
155a7dea167SDimitry Andric 
156e8d8bef9SDimitry Andric /// Produces a human-readable rendering of the node bound to `Id`, suitable for
157e8d8bef9SDimitry Andric /// diagnostics and debugging. This operator can be applied to any node, but is
158e8d8bef9SDimitry Andric /// targeted at those whose source cannot be printed directly, including:
159e8d8bef9SDimitry Andric ///
160e8d8bef9SDimitry Andric /// * Types. represented based on their structure. Note that namespace
161e8d8bef9SDimitry Andric ///   qualifiers are always printed, with the anonymous namespace represented
162e8d8bef9SDimitry Andric ///   explicitly. No desugaring or canonicalization is applied.
163e8d8bef9SDimitry Andric Stencil describe(llvm::StringRef Id);
164e8d8bef9SDimitry Andric 
165a7dea167SDimitry Andric /// For debug use only; semantics are not guaranteed.
166a7dea167SDimitry Andric ///
167a7dea167SDimitry Andric /// \returns the string resulting from calling the node's print() method.
168480093f4SDimitry Andric Stencil dPrint(llvm::StringRef Id);
169a7dea167SDimitry Andric } // namespace transformer
170a7dea167SDimitry Andric } // namespace clang
171a7dea167SDimitry Andric #endif // LLVM_CLANG_TOOLING_TRANSFORMER_STENCIL_H_
172