CIRCT  20.0.0git
Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
circt::pretty::PrettyPrinter Class Reference

#include <PrettyPrinter.h>

Collaboration diagram for circt::pretty::PrettyPrinter:
Collaboration graph
[legend]

Classes

struct  FormattedToken
 Format token with tracked size. More...
 
struct  Listener
 Listener to Token storage events. More...
 
struct  PrintEntry
 Printing information for active scope, stored in printStack. More...
 

Public Member Functions

 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. More...
 
 ~PrettyPrinter ()
 
void add (Token t)
 Add token for printing. In Oppen, this is "scan". More...
 
template<typename R >
void addTokens (R &&tokens)
 Add a range of tokens. More...
 
void eof ()
 
void setListener (Listener *newListener)
 
auto * getListener () const
 

Static Public Attributes

static constexpr uint32_t kInfinity = (1U << 15) - 1
 

Private Types

enum class  PrintBreaks { Consistent , Inconsistent , AlwaysFits , Fits }
 Breaking style for a printStack entry. More...
 

Private Member Functions

void advanceLeft ()
 Print out tokens we know sizes for, and drop from token buffer. More...
 
void checkStack ()
 Break encountered, set sizes of begin/breaks in scanStack we now know. More...
 
void checkStream ()
 Check if there's enough tokens to hit width, if so print. More...
 
void print (const FormattedToken &f)
 Print a token, maintaining printStack for context. More...
 
void clear ()
 Clear token buffer, scanStack must be empty. More...
 
void rebaseIfNeeded ()
 Reset leftTotal and tokenOffset, rebase size data and scanStack indices. More...
 
auto & getPrintFrame ()
 Get current printing frame. More...
 

Private Attributes

int32_t space
 Characters left on this line. More...
 
int32_t leftTotal
 Sizes: printed, enqueued. More...
 
int32_t rightTotal
 
std::deque< FormattedTokentokens
 Unprinted tokens, combination of 'token' and 'size' in Oppen. More...
 
uint32_t tokenOffset = 0
 index of first token, for resolving scanStack entries. More...
 
std::deque< uint32_t > scanStack
 Stack of begin/break tokens, adjust by tokenOffset to index into tokens. More...
 
SmallVector< PrintEntryprintStack
 Stack of printing contexts (indentation + breaking behavior). More...
 
const PrintEntry defaultFrame
 Printing context when stack is empty. More...
 
uint32_t alwaysFits = 0
 Number of "AlwaysFits" on print stack. More...
 
uint32_t indent
 Current indentation level. More...
 
uint32_t pendingIndentation
 Whitespace to print before next, tracked to avoid trailing whitespace. More...
 
const uint32_t margin
 Target line width. More...
 
const uint32_t maxStartingIndent
 Maximum starting indentation level (default=kInfinity/4). More...
 
llvm::raw_ostream & os
 Output stream. More...
 
Listenerlistener = nullptr
 Hook for Token storage events. More...
 
bool donotClear = false
 Flag to identify a state when the clear cannot be called. More...
 

Static Private Attributes

static constexpr decltype(leftTotalrebaseThreshold
 Threshold for walking scan state and "rebasing" totals/offsets. More...
 

Detailed Description

Definition at line 175 of file PrettyPrinter.h.

Member Enumeration Documentation

◆ PrintBreaks

Breaking style for a printStack entry.

This is "Breaks" values with extra for "Fits". Breaks::Never is "AlwaysFits" here.

Enumerator
Consistent 
Inconsistent 
AlwaysFits 
Fits 

Definition at line 242 of file PrettyPrinter.h.

Constructor & Destructor Documentation

◆ PrettyPrinter()

circt::pretty::PrettyPrinter::PrettyPrinter ( llvm::raw_ostream &  os,
uint32_t  margin,
uint32_t  baseIndent = 0,
uint32_t  currentColumn = 0,
uint32_t  maxStartingIndent = kInfinity / 4,
Listener listener = nullptr 
)
inline

PrettyPrinter for specified stream.

  • margin: line width.
  • baseIndent: always indent at least this much (starting 'indent' value).
  • currentColumn: current column, used to calculate space remaining.
  • maxStartingIndent: max column indentation starts at, must be >= margin.

Definition at line 191 of file PrettyPrinter.h.

References assert(), circt::pretty::Inconsistent, kInfinity, margin, maxStartingIndent, and pendingIndentation.

◆ ~PrettyPrinter()

circt::pretty::PrettyPrinter::~PrettyPrinter ( )
inline

Definition at line 206 of file PrettyPrinter.h.

References eof().

Member Function Documentation

◆ add()

void circt::pretty::PrettyPrinter::add ( Token  t)

◆ addTokens()

template<typename R >
void circt::pretty::PrettyPrinter::addTokens ( R &&  tokens)
inline

Add a range of tokens.

Definition at line 213 of file PrettyPrinter.h.

References add(), clear(), donotClear, scanStack, and tokens.

Referenced by circt::pretty::BufferingPP::flush().

◆ advanceLeft()

void circt::pretty::PrettyPrinter::advanceLeft ( )
private

Print out tokens we know sizes for, and drop from token buffer.

Definition at line 224 of file PrettyPrinter.cpp.

References assert(), leftTotal, print(), circt::pretty::BreakToken::spaces(), circt::pretty::StringToken::text(), tokenOffset, and tokens.

Referenced by checkStream(), and eof().

◆ checkStack()

void circt::pretty::PrettyPrinter::checkStack ( )
private

Break encountered, set sizes of begin/breaks in scanStack we now know.

Break encountered, set sizes of begin/breaks in scanStack that we now know.

Definition at line 179 of file PrettyPrinter.cpp.

References assert(), rightTotal, scanStack, tokenOffset, and tokens.

Referenced by add(), and eof().

◆ checkStream()

void circt::pretty::PrettyPrinter::checkStream ( )
private

Check if there's enough tokens to hit width, if so print.

Check if there are enough tokens to hit width, if so print.

If scan size is wider than line, it's infinity.

Definition at line 206 of file PrettyPrinter.cpp.

References advanceLeft(), assert(), kInfinity, leftTotal, rightTotal, scanStack, space, tokenOffset, and tokens.

Referenced by add().

◆ clear()

void circt::pretty::PrettyPrinter::clear ( )
private

Clear token buffer, scanStack must be empty.

Definition at line 168 of file PrettyPrinter.cpp.

References assert(), circt::pretty::PrettyPrinter::Listener::clear(), donotClear, leftTotal, listener, rightTotal, scanStack, tokenOffset, and tokens.

Referenced by add(), addTokens(), and eof().

◆ eof()

void circt::pretty::PrettyPrinter::eof ( )

◆ getListener()

auto* circt::pretty::PrettyPrinter::getListener ( ) const
inline

Definition at line 228 of file PrettyPrinter.h.

References listener.

◆ getPrintFrame()

auto& circt::pretty::PrettyPrinter::getPrintFrame ( )
inlineprivate

Get current printing frame.

Definition at line 270 of file PrettyPrinter.h.

References defaultFrame, and printStack.

Referenced by print().

◆ print()

void circt::pretty::PrettyPrinter::print ( const FormattedToken f)
private

◆ rebaseIfNeeded()

void circt::pretty::PrettyPrinter::rebaseIfNeeded ( )
private

Reset leftTotal and tokenOffset, rebase size data and scanStack indices.

Definition at line 129 of file PrettyPrinter.cpp.

References assert(), leftTotal, rebaseThreshold, rightTotal, scanStack, tokenOffset, and tokens.

Referenced by add().

◆ setListener()

void circt::pretty::PrettyPrinter::setListener ( Listener newListener)
inline

Definition at line 227 of file PrettyPrinter.h.

References listener.

Member Data Documentation

◆ alwaysFits

uint32_t circt::pretty::PrettyPrinter::alwaysFits = 0
private

Number of "AlwaysFits" on print stack.

Definition at line 296 of file PrettyPrinter.h.

Referenced by print().

◆ defaultFrame

const PrintEntry circt::pretty::PrettyPrinter::defaultFrame
private

Printing context when stack is empty.

Definition at line 293 of file PrettyPrinter.h.

Referenced by getPrintFrame().

◆ donotClear

bool circt::pretty::PrettyPrinter::donotClear = false
private

Flag to identify a state when the clear cannot be called.

Definition at line 319 of file PrettyPrinter.h.

Referenced by addTokens(), and clear().

◆ indent

uint32_t circt::pretty::PrettyPrinter::indent
private

Current indentation level.

Definition at line 299 of file PrettyPrinter.h.

Referenced by print().

◆ kInfinity

constexpr uint32_t circt::pretty::PrettyPrinter::kInfinity = (1U << 15) - 1
staticconstexpr

◆ leftTotal

int32_t circt::pretty::PrettyPrinter::leftTotal
private

Sizes: printed, enqueued.

Definition at line 278 of file PrettyPrinter.h.

Referenced by advanceLeft(), checkStream(), clear(), and rebaseIfNeeded().

◆ listener

Listener* circt::pretty::PrettyPrinter::listener = nullptr
private

Hook for Token storage events.

Definition at line 316 of file PrettyPrinter.h.

Referenced by add(), clear(), getListener(), print(), and setListener().

◆ margin

const uint32_t circt::pretty::PrettyPrinter::margin
private

Target line width.

Definition at line 305 of file PrettyPrinter.h.

Referenced by PrettyPrinter(), and print().

◆ maxStartingIndent

const uint32_t circt::pretty::PrettyPrinter::maxStartingIndent
private

Maximum starting indentation level (default=kInfinity/4).

Useful to continue indentation past margin while still providing a limit to avoid pathological output and for consumption by tools with limits.

Definition at line 310 of file PrettyPrinter.h.

Referenced by PrettyPrinter(), and print().

◆ os

llvm::raw_ostream& circt::pretty::PrettyPrinter::os
private

Output stream.

Definition at line 313 of file PrettyPrinter.h.

Referenced by print().

◆ pendingIndentation

uint32_t circt::pretty::PrettyPrinter::pendingIndentation
private

Whitespace to print before next, tracked to avoid trailing whitespace.

Definition at line 302 of file PrettyPrinter.h.

Referenced by PrettyPrinter(), and print().

◆ printStack

SmallVector<PrintEntry> circt::pretty::PrettyPrinter::printStack
private

Stack of printing contexts (indentation + breaking behavior).

Definition at line 290 of file PrettyPrinter.h.

Referenced by getPrintFrame(), and print().

◆ rebaseThreshold

constexpr decltype(leftTotal) circt::pretty::PrettyPrinter::rebaseThreshold
staticconstexprprivate
Initial value:
=
1UL << (std::numeric_limits<decltype(leftTotal)>::digits - 3)
int32_t leftTotal
Sizes: printed, enqueued.

Threshold for walking scan state and "rebasing" totals/offsets.

Definition at line 322 of file PrettyPrinter.h.

Referenced by rebaseIfNeeded().

◆ rightTotal

int32_t circt::pretty::PrettyPrinter::rightTotal
private

Definition at line 279 of file PrettyPrinter.h.

Referenced by add(), checkStack(), checkStream(), clear(), and rebaseIfNeeded().

◆ scanStack

std::deque<uint32_t> circt::pretty::PrettyPrinter::scanStack
private

Stack of begin/break tokens, adjust by tokenOffset to index into tokens.

Definition at line 287 of file PrettyPrinter.h.

Referenced by add(), addTokens(), checkStack(), checkStream(), clear(), eof(), and rebaseIfNeeded().

◆ space

int32_t circt::pretty::PrettyPrinter::space
private

Characters left on this line.

Definition at line 275 of file PrettyPrinter.h.

Referenced by checkStream(), and print().

◆ tokenOffset

uint32_t circt::pretty::PrettyPrinter::tokenOffset = 0
private

index of first token, for resolving scanStack entries.

Definition at line 284 of file PrettyPrinter.h.

Referenced by add(), advanceLeft(), checkStack(), checkStream(), clear(), and rebaseIfNeeded().

◆ tokens

std::deque<FormattedToken> circt::pretty::PrettyPrinter::tokens
private

Unprinted tokens, combination of 'token' and 'size' in Oppen.

Definition at line 282 of file PrettyPrinter.h.

Referenced by add(), addTokens(), advanceLeft(), checkStack(), checkStream(), clear(), and rebaseIfNeeded().


The documentation for this class was generated from the following files: