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");
114template <
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,
200 space = this->margin - std::max(currentColumn, baseIndent);
204 baseIndent > currentColumn ? baseIndent - currentColumn : 0;
212 template <
typename R>
216 llvm::SaveAndRestore<bool> save(
donotClear,
true);
316 llvm::raw_ostream &
os;
329 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).
auto * getListener() const
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.
static constexpr uint32_t kMaxMargin
Largest supported target line length.
PrintBreaks
Breaking style for a printStack entry.
void setListener(Listener *newListener)
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).
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.
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.
const bool noWrap
Disables line wrapping on non-newline whitespace.
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.
PrettyPrinter(llvm::raw_ostream &os, uint32_t margin, uint32_t baseIndent=0, uint32_t currentColumn=0, uint32_t maxStartingIndent=kMaxMargin/2, Listener *listener=nullptr)
PrettyPrinter for specified stream.
void checkStream()
Check if there's enough tokens to hit width, if so print.
const PrintEntry defaultFrame
Printing context when stack is empty.
auto & getPrintFrame()
Get current printing frame.
static auto & getInfoImpl(T &t)
CallbackInfo callbackInfo
union circt::pretty::Token::@7 data
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)