20 #ifndef CIRCT_SUPPORT_PRETTYPRINTER_H
21 #define CIRCT_SUPPORT_PRETTYPRINTER_H
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
92 template <Kind k,
typename T>
95 return t.data.stringInfo;
97 return t.data.breakInfo;
99 return t.data.beginInfo;
101 return t.data.endInfo;
103 return t.data.callbackInfo;
104 llvm_unreachable(
"unhandled token kind");
114 template <
class DerivedT, Token::Kind DerivedKind>
121 using InfoType = std::remove_reference_t<std::invoke_result_t<
122 decltype(Token::getInfoImpl<DerivedKind, Token &>),
Token &>>;
127 return Token::getInfoImpl<DerivedKind>(*
this);
130 template <
typename... Args>
192 uint32_t currentColumn = 0,
195 :
space(
margin - std::max(currentColumn, baseIndent)),
204 baseIndent > currentColumn ? baseIndent - currentColumn : 0;
212 template <
typename R>
216 llvm::SaveAndRestore<bool> save(
donotClear,
true);
313 llvm::raw_ostream &
os;
323 1UL << (std::numeric_limits<decltype(
leftTotal)>::digits - 3);
assert(baseType &&"element must be base type")
SmallVector< PrintEntry > printStack
Stack of printing contexts (indentation + breaking behavior).
void rebaseIfNeeded()
Reset leftTotal and tokenOffset, rebase size data and scanStack indices.
void add(Token t)
Add token for printing. In Oppen, this is "scan".
bool donotClear
Flag to identify a state when the clear cannot be called.
void addTokens(R &&tokens)
Add a range of tokens.
std::deque< FormattedToken > tokens
Unprinted tokens, combination of 'token' and 'size' in Oppen.
Listener * listener
Hook for Token storage events.
PrintBreaks
Breaking style for a printStack entry.
void setListener(Listener *newListener)
auto * getListener() const
uint32_t indent
Current indentation level.
uint32_t tokenOffset
index of first token, for resolving scanStack entries.
const uint32_t maxStartingIndent
Maximum starting indentation level (default=kInfinity/4).
PrettyPrinter(llvm::raw_ostream &os, uint32_t margin, uint32_t baseIndent=0, uint32_t currentColumn=0, uint32_t maxStartingIndent=kInfinity/4, Listener *listener=nullptr)
PrettyPrinter for specified stream.
int32_t space
Characters left on this line.
static constexpr uint32_t kInfinity
llvm::raw_ostream & os
Output stream.
static constexpr decltype(leftTotal) rebaseThreshold
Threshold for walking scan state and "rebasing" totals/offsets.
void checkStack()
Break encountered, set sizes of begin/breaks in scanStack we now know.
auto & getPrintFrame()
Get current printing frame.
void print(const FormattedToken &f)
Print a token, maintaining printStack for context.
const uint32_t margin
Target line width.
uint32_t pendingIndentation
Whitespace to print before next, tracked to avoid trailing whitespace.
int32_t leftTotal
Sizes: printed, enqueued.
std::deque< uint32_t > scanStack
Stack of begin/break tokens, adjust by tokenOffset to index into tokens.
void clear()
Clear token buffer, scanStack must be empty.
void advanceLeft()
Print out tokens we know sizes for, and drop from token buffer.
uint32_t alwaysFits
Number of "AlwaysFits" on print stack.
void checkStream()
Check if there's enough tokens to hit width, if so print.
const PrintEntry defaultFrame
Printing context when stack is empty.
union circt::pretty::Token::@5 data
CallbackInfo callbackInfo
static auto & getInfoImpl(T &t)
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.
IndentStyle style() const
BeginToken(int32_t offset=2, Breaks breaks=Breaks::Inconsistent, IndentStyle style=IndentStyle::Visual)
BreakToken(uint32_t spaces=1, int32_t offset=0, bool neverbreak=false)
Listener to Token storage events.
virtual void clear()
No tokens referencing external memory are present.
virtual ~Listener()
Destructor, anchor.
virtual void print()
Listener for print event.
Printing information for active scope, stored in printStack.
StringToken(llvm::StringRef text)
Helper class to CRTP-derive common functions.
const InfoType & getInfo() const
std::remove_reference_t< std::invoke_result_t< decltype(Token::getInfoImpl< DerivedKind, Token & >), Token & > > InfoType
void initialize(Args &&...args)
static bool classof(const Token *t)