13 #ifndef CIRCT_SUPPORT_PRETTYPRINTERHELPERS_H
14 #define CIRCT_SUPPORT_PRETTYPRINTERHELPERS_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/StringSaver.h"
22 #include "llvm/Support/raw_ostream.h"
49 llvm::append_range(
tokens, newTokens);
75 void emitNBSP(
unsigned n, llvm::function_ref<
void(
Token)> add);
79 template <
typename PPTy = PrettyPr
inter>
89 template <
typename T,
typename... Args>
90 typename std::enable_if_t<std::is_base_of_v<Token, T>>
add(Args &&...args) {
91 pp.add(T(std::forward<Args>(args)...));
101 void literal(StringRef str) { add<StringToken>(str); }
120 void spaces(uint32_t n) { add<BreakToken>(n); }
150 void end() { add<EndToken>(); }
164 [[nodiscard]] StringRef
save(StringRef str) {
return strings.save(str); }
167 void clear()
override;
172 template <
typename CallableTy,
typename DataTy>
240 template <
typename PPTy = PrettyPr
inter>
260 Base::template add<StringToken>(
saver.
save(s));
330 template <
typename T>
338 template <
typename Callable,
unsigned BufferLen = 128>
340 SmallString<BufferLen> ss;
341 llvm::raw_svector_ostream ssos(ss);
342 auto flush = llvm::make_scope_exit([&]() {
346 return std::invoke(std::forward<Callable>(c), ssos);
354 StringRef left =
"\"",
355 StringRef right =
"\"") {
359 os.write_escaped(str, useHexEscapes);
366 template <
typename T,
typename Callable>
368 *this << std::forward<T>(t);
369 auto done = llvm::make_scope_exit([&]() { *
this << close; });
370 return std::invoke(std::forward<Callable>(c));
376 template <
typename CallableType,
typename DataType,
377 typename PPTy = PrettyPrinter>
assert(baseType &&"element must be base type")
void addTokens(R &&tokens)
Add a range of tokens.
static constexpr uint32_t kInfinity
Note: Callable class must implement a callable with signature: void (Data)
CallableTy & callable
The storage for the callback, as a function object.
PrintEventAndStorageListener(CallableTy &c)
CallbackToken getToken(DataTy obj)
Get a token with the obj data.
std::queue< DataTy > dataQ
List of all the unique data associated with each callback token.
void print() override
PrettyPrinter::Listener::print – indicates all the preceding tokens on the stream have been printed.
Add convenience methods for generating pretty-printing tokens.
void literal(StringRef str)
Add a literal (with external storage).
void newline()
Add a newline (break too wide to fit, always breaks).
void ibox(int32_t offset=0, IndentStyle style=IndentStyle::Visual)
Start an inconsistent group with specified offset.
void bbox(int32_t offset=0, Breaks breaks=Breaks::Consistent)
Start a IndentStyle::Block group with specified offset.
std::enable_if_t< std::is_base_of_v< Token, T > > add(Args &&...args)
Add new token.
void nbsp(unsigned n)
Add multiple non-breaking spaces as a single token.
void eof()
End of a stream.
void space()
Add a breakable space.
void cbox(int32_t offset=0, IndentStyle style=IndentStyle::Visual)
Start a consistent group with specified offset.
void spaces(uint32_t n)
Add breakable spaces.
void nbsp()
Add a non-breaking space.
void neverbreak()
Add a 'neverbreak' break. Always 'fits'.
void neverbox()
Start a group that cannot break, including nested groups.
void zerobreak()
Add a break that is zero-wide if not broken.
Wrap the TokenStream with a helper for CallbackTokens, to record the print events on the stream.
PrintEventAndStorageListener< CallableType, DataType > & saver
TokenStreamWithCallback(PPTy &pp, PrintEventAndStorageListener< CallableType, DataType > &saver, bool enableCallback)
void addCallback(DataType d)
Add a Callback token.
const bool enableCallback
Wrap a PrettyPrinter with TokenBuilder features as well as operator<<'s.
TokenStream & operator<<(const PPSaveString &str)
String must be saved.
auto scopedBox(T &&t, Callable &&c, Token close=EndToken())
Open a box, invoke the lambda, and close it after.
TokenStream & operator<<(const char *s)
Add a string literal (external storage).
TokenStream & operator<<(StringRef s)
Add a string token (saved to storage).
auto invokeWithStringOS(Callable &&c)
Helper to invoke code with a llvm::raw_ostream argument for compatibility.
TokenStream & writeQuotedEscaped(StringRef str, bool useHexEscapes=false, StringRef left="\"", StringRef right="\"")
TokenStream & operator<<(const PPExtString &str)
String has external storage.
TokenStream(PPTy &pp, TokenStringSaver &saver)
Create a TokenStream using the specified PrettyPrinter and StringSaver storage.
TokenStream & addAsString(T &&t)
General-purpose "format this" helper, for types not supported by operator<< yet.
TokenStream & operator<<(Token t)
Stream support for user-created Token's.
TokenStream & writeEscaped(StringRef str, bool useHexEscapes=false)
Write escaped versions of the string, saved in storage.
TokenStream & operator<<(PP s)
Convenience for inline streaming of builder methods.
PrettyPrinter::Listener that saves strings while live.
void clear() override
PrettyPrinter::Listener::clear – indicates no external refs.
llvm::StringSaver strings
StringRef save(StringRef str)
Add string, save in storage.
llvm::BumpPtrAllocator alloc
void emitNBSP(unsigned n, llvm::function_ref< void(Token)> add)
Add multiple non-breaking spaces as a single token.
PP
Send one of these to TokenStream to add the corresponding token.
IndentStyle
Style of indent when starting a group:
Breaks
Style of breaking within a group:
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Buffer tokens for clients that need to adjust things.
SmallVectorImpl< Token > BufferVec
void addTokens(R &&newTokens)
Add a range of tokens.
BufferingPP(BufferVec &tokens)
void flush(PrettyPrinter &pp)
Flush buffered tokens to the specified pretty printer.
void eof()
Buffer a final EOF, no tokens allowed after this.
String wrapper to indicate string has external storage.
PPExtString(StringRef str)
String wrapper to indicate string needs to be saved.
PPSaveString(StringRef str)
Listener to Token storage events.