68 #include "llvm/ADT/TypeSwitch.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/raw_ostream.h"
81 auto addScanToken = [&](
auto offset) {
84 tokens.push_back({t, offset});
86 llvm::TypeSwitch<Token *>(&t)
91 assert(!s->
text().empty() &&
"empty string token");
115 return print({t, 0});
123 return print({t, 0});
143 if (isa<BreakToken, BeginToken>(&t.token)) {
145 assert(t.size + adjust < 0);
169 assert(
scanStack.empty() &&
"clearing tokens while still on scan stack");
185 if (llvm::isa<BeginToken>(&t.token)) {
191 }
else if (llvm::isa<EndToken>(&t.token)) {
228 const auto &f =
tokens.front();
231 llvm::TypeSwitch<const Token *, int32_t>(&f.token)
234 .Default([](
const auto *) {
return 0; });
244 uint32_t maxStartingIndent) {
245 return std::clamp<ssize_t>(newIndent + offset, 0, maxStartingIndent);
250 llvm::TypeSwitch<const Token *>(&f.
token)
260 "newline inside never group");
283 ssize_t newIndent =
indent;
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.
std::deque< FormattedToken > tokens
Unprinted tokens, combination of 'token' and 'size' in Oppen.
Listener * listener
Hook for Token storage events.
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.
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.
static uint32_t computeNewIndent(ssize_t newIndent, int32_t offset, uint32_t maxStartingIndent)
Compute indentation w/o overflow, clamp to [0,maxStartingIndent].
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
virtual void clear()
No tokens referencing external memory are present.
virtual ~Listener()
Destructor, anchor.
virtual void print()
Listener for print event.