|
decompiler 1.0.0
|
Base class (and interface) for pretty printing and XML markup of tokens. More...
#include <prettyprint.hh>
Public Types | |
| enum | syntax_highlight { keyword_color = 0 , comment_color = 1 , type_color = 2 , funcname_color = 3 , var_color = 4 , const_color = 5 , param_color = 6 , global_color = 7 , no_color = 8 } |
| Possible types of syntax highlighting. More... | |
Public Member Functions | |
| EmitXml (void) | |
| Constructor. | |
| virtual | ~EmitXml (void) |
| Destructor. | |
| virtual int4 | beginDocument (void) |
| Begin a whole document of output. More... | |
| virtual void | endDocument (int4 id) |
| End a whole document of output. More... | |
| virtual int4 | beginFunction (const Funcdata *fd) |
| Begin a whole declaration and body of a function. More... | |
| virtual void | endFunction (int4 id) |
| End a whole declaration and body of a function. More... | |
| virtual int4 | beginBlock (const FlowBlock *bl) |
| Begin a control-flow element. More... | |
| virtual void | endBlock (int4 id) |
| End a control-flow element. More... | |
| virtual void | tagLine (void) |
| Force a line break. More... | |
| virtual void | tagLine (int4 indent) |
| Force a line break and indent level. More... | |
| virtual int4 | beginReturnType (const Varnode *vn) |
| Begin a return type declaration. More... | |
| virtual void | endReturnType (int4 id) |
| End a return type declaration. More... | |
| virtual int4 | beginVarDecl (const Symbol *sym) |
| Begin a variable declaration. More... | |
| virtual void | endVarDecl (int4 id) |
| End a variable declaration. More... | |
| virtual int4 | beginStatement (const PcodeOp *op) |
| Begin a source code statement. More... | |
| virtual void | endStatement (int4 id) |
| End a source code statement. More... | |
| virtual int4 | beginFuncProto (void) |
| Begin a function prototype declaration. More... | |
| virtual void | endFuncProto (int4 id) |
| End a function prototype declaration. More... | |
| virtual void | tagVariable (const char *ptr, syntax_highlight hl, const Varnode *vn, const PcodeOp *op) |
| Emit a variable token. More... | |
| virtual void | tagOp (const char *ptr, syntax_highlight hl, const PcodeOp *op) |
| Emit an operation token. More... | |
| virtual void | tagFuncName (const char *ptr, syntax_highlight hl, const Funcdata *fd, const PcodeOp *op) |
| Emit a function identifier. More... | |
| virtual void | tagType (const char *ptr, syntax_highlight hl, const Datatype *ct) |
| Emit a data-type identifier. More... | |
| virtual void | tagField (const char *ptr, syntax_highlight hl, const Datatype *ct, int4 off) |
| Emit an identifier for a field within a structured data-type. More... | |
| virtual void | tagComment (const char *ptr, syntax_highlight hl, const AddrSpace *spc, uintb off) |
| Emit a comment string as part of the generated source code. More... | |
| virtual void | tagLabel (const char *ptr, syntax_highlight hl, const AddrSpace *spc, uintb off) |
| Emit a code label identifier. More... | |
| virtual void | print (const char *str, syntax_highlight hl=no_color) |
| Emit other (more unusual) syntax as part of source code generation. More... | |
| virtual int4 | openParen (char o, int4 id=0) |
| Emit an open parenthesis. More... | |
| virtual void | closeParen (char c, int4 id) |
| Emit a close parenthesis. More... | |
| virtual int4 | openGroup (void) |
| Start a group of things that are printed together. More... | |
| virtual void | closeGroup (int4 id) |
| End a group of things that are printed together. More... | |
| virtual void | clear (void) |
| Reset the emitter to its initial state. More... | |
| virtual void | setOutputStream (ostream *t) |
| Set the output stream for the emitter. More... | |
| virtual ostream * | getOutputStream (void) const |
| Get the current output stream. More... | |
| virtual void | spaces (int4 num, int4 bump=0) |
| Emit a sequence of space characters as part of source code. More... | |
| virtual int4 | startIndent (void) |
| Start a new indent level. More... | |
| virtual void | stopIndent (int4 id) |
| End an indent level. More... | |
| virtual int4 | startComment (void) |
| Start a comment block within the emitted source code. More... | |
| virtual void | stopComment (int4 id) |
| End a comment block. More... | |
| virtual void | flush (void) |
| Flush any remaining character data. More... | |
| virtual void | setMaxLineSize (int4 mls) |
| Provide a maximum line size to the pretty printer. More... | |
| virtual int4 | getMaxLineSize (void) const |
| Get the current maximum line size. More... | |
| virtual void | setCommentFill (const string &fill) |
| Set the comment fill characters for when line breaks are forced. More... | |
| virtual bool | emitsXml (void) const |
| Determine if this is an XML markup emitter. More... | |
| virtual void | resetDefaults (void) |
| (Re)set the default emitting options More... | |
| int4 | getParenLevel (void) const |
| Get the current parentheses depth. More... | |
| int4 | getIndentIncrement (void) const |
| Get the number of characters indented per level of nesting. More... | |
| void | setIndentIncrement (int4 val) |
| Set the number of characters indented per level of nesting. More... | |
| void | setPendingPrint (PendPrint *pend) |
| Set a pending print callback. More... | |
| void | cancelPendingPrint (void) |
| Cancel any pending print callback. More... | |
| bool | hasPendingPrint (PendPrint *pend) const |
| Check if the given print callback is still pending. More... | |
Protected Member Functions | |
| void | resetDefaultsInternal (void) |
| Set options to default values for EmitXml. | |
| void | emitPending (void) |
| Emit any pending print commands. | |
Protected Attributes | |
| ostream * | s |
| Stream being emitted to. | |
| int4 | indentlevel |
| Current indent level (in fixed width characters) | |
| int4 | parenlevel |
| Current depth of parentheses. | |
| int4 | indentincrement |
| Change in indentlevel per level of nesting. | |
| PendPrint * | pendPrint |
| Pending print callback. | |
Static Private Attributes | |
| static const char * | highlight [] |
| Map from syntax_highlight enumeration to color attribute string. More... | |
Base class (and interface) for pretty printing and XML markup of tokens.
There are two basic functions being implemented through this interface:
XML markup: allows recording of the natural grouping of the high-level tokens and directly links the nodes of the abstract syntax tree to the emitted tokens.
Pretty printing: Line breaks and additional white space characters are inserted within the emitted source code to enforce a maximum number of characters per line while minimizing breaks in important groups of syntax. Where extra line breaks are necessary, additional indenting is provided to reduce the impact on readability.
All printing must be surrounded by at least one begin and end tag pair:
Additional printing groups can be specified with tag pairs:
The tag* functions, emit the actual language tokens, supplying appropriate markup.
This base class does not actually do any pretty printing it only does the XML markup. For an implementation that actually does pretty printing, see EmitPrettyPrint. This class can be used as the low-level back-end to EmitPrettyPrint to provide a solution that does both pretty printing and XML markup.
Possible types of syntax highlighting.
|
virtual |
Begin a control-flow element.
Inform the emitter that a new control-flow section is starting. This is a source code unit usually surrounded with curly braces '{' and '}'.
| bl | is the block structure object associated with the section |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockCondition(), PrintC::emitBlockDoWhile(), PrintC::emitBlockGraph(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockLs(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), and PrintC::emitForLoop().
|
virtual |
Begin a whole document of output.
Inform the emitter that generation of the source code document has begun
Reimplemented in EmitNoXml, and EmitPrettyPrint.
References highlight, no_color, and s.
Referenced by PrintC::docAllGlobals(), and PrintC::docSingleGlobal().
|
virtual |
Begin a function prototype declaration.
Inform the emitter that a function prototype is starting.
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitFunctionDeclaration().
|
virtual |
Begin a whole declaration and body of a function.
Inform the emitter that generation of a function body has begun
Reimplemented in EmitNoXml, and EmitPrettyPrint.
References highlight, no_color, and s.
Referenced by PrintC::docFunction().
|
virtual |
Begin a return type declaration.
Inform the emitter that generation of a function's return type is starting.
| vn | (if non-null) is the storage location for the return value |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitPrototypeOutput().
|
virtual |
Begin a source code statement.
Inform the emitter that a source code statement is beginning.
| op | is the root p-code operation of the statement |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockBasic(), PrintC::emitForLoop(), PrintC::emitGotoStatement(), and PrintC::emitStatement().
|
virtual |
Begin a variable declaration.
Inform the emitter that a variable declaration has started.
| sym | is the symbol being declared |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitVarDecl().
|
inline |
Cancel any pending print callback.
If there is any print callback pending, cancel it
References pendPrint.
Referenced by PrintC::emitBlockIf().
|
inlinevirtual |
Reset the emitter to its initial state.
Reimplemented in EmitPrettyPrint.
References indentlevel, parenlevel, and pendPrint.
Referenced by EmitPrettyPrint::clear(), and PrintLanguage::clear().
|
inlinevirtual |
End a group of things that are printed together.
Inform the emitter that a printing group is ending.
| id | is the id associated with the group (as returned by openGroup) |
Reimplemented in EmitPrettyPrint.
Referenced by PrintC::emitFunctionDeclaration(), PrintC::opCbranch(), and PrintLanguage::pushAtom().
|
virtual |
Emit a close parenthesis.
This method emits the parenthesis character itself and ends the printing unit that was started by the matching open parenthesis.
| c | is the close parenthesis character to emit |
| id | is the id associated with the matching open parenthesis (as returned by openParen) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockCondition(), PrintC::emitBlockInfLoop(), PrintC::emitBlockWhileDo(), PrintC::emitForLoop(), PrintC::emitFunctionDeclaration(), PrintLanguage::emitOp(), PrintC::opBranchind(), PrintC::opCbranch(), and PrintLanguage::pushAtom().
|
inlinevirtual |
Determine if this is an XML markup emitter.
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by EmitPrettyPrint::emitsXml(), and PrintLanguage::emitsXml().
|
virtual |
End a control-flow element.
Inform the emitter that a control-flow section is ending.
| id | is the id associated with the section (as returned by beginBlock) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockCondition(), PrintC::emitBlockDoWhile(), PrintC::emitBlockGraph(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockLs(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), and PrintC::emitForLoop().
|
virtual |
End a whole document of output.
Inform the emitter that generation of the source code document is finished
| id | is the id associated with the document (as returned by beginDocument) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
References s.
Referenced by PrintC::docAllGlobals(), and PrintC::docSingleGlobal().
|
virtual |
End a function prototype declaration.
Inform the emitter that a function prototype is ending.
| id | is the id associated with the prototype (as returned by beginFuncProto) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitFunctionDeclaration().
|
virtual |
End a whole declaration and body of a function.
Inform the emitter that generation of a function body has ended
| id | is the id associated with the function body (as returned by beginFunction) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::docFunction().
|
virtual |
End a return type declaration.
Inform the emitter that generation of a function's return type is ending.
| id | is the id associated with the return type (as returned by beginReturnType) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitPrototypeOutput().
|
virtual |
End a source code statement.
Inform the emitter that a source code statement is ending.
| id | is the id associated with the statement (as returned by beginStatement) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockBasic(), PrintC::emitForLoop(), PrintC::emitGotoStatement(), and PrintC::emitStatement().
|
virtual |
End a variable declaration.
Inform the emitter that a variable declaration has ended.
| id | is the id associated with the declaration (as returned by beginVarDecl) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitVarDecl().
|
inlinevirtual |
Flush any remaining character data.
Depending on the particular emitter, tokens and syntax that have been submitted to the emitter may be held internally for a time before getting output to the final stream. This routine makes sure submitted syntax is fully output.
Reimplemented in EmitPrettyPrint.
Referenced by PrintC::docAllGlobals(), PrintC::docFunction(), PrintC::docSingleGlobal(), and EmitPrettyPrint::flush().
|
inline |
Get the number of characters indented per level of nesting.
References indentincrement.
|
inlinevirtual |
Get the current maximum line size.
If the emitter respects a maximum line size, return that size.
Reimplemented in EmitPrettyPrint.
Referenced by PrintLanguage::setLineCommentIndent().
|
inlinevirtual |
Get the current output stream.
Reimplemented in EmitPrettyPrint.
References s.
Referenced by EmitPrettyPrint::getOutputStream(), PrintLanguage::getOutputStream(), and EmitPrettyPrint::setXML().
|
inline |
Get the current parentheses depth.
References parenlevel.
|
inline |
Check if the given print callback is still pending.
| pend | is the given print callback to check |
References pendPrint.
Referenced by PrintC::emitBlockIf().
|
inlinevirtual |
Start a group of things that are printed together.
Inform the emitter that a new printing group is starting.
Reimplemented in EmitPrettyPrint.
Referenced by PrintC::emitFunctionDeclaration(), PrintC::opCbranch(), and PrintLanguage::pushOp().
|
virtual |
Emit an open parenthesis.
This method emits the parenthesis character itself and also starts a printing unit of the source code being surrounded by the parentheses.
| o | is the open parenthesis character to emit |
| id | is an id to associate with the parenthesis |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitBlockCondition(), PrintC::emitBlockInfLoop(), PrintC::emitBlockWhileDo(), PrintC::emitForLoop(), PrintC::emitFunctionDeclaration(), PrintLanguage::emitOp(), PrintC::opBranchind(), PrintC::opCbranch(), and PrintLanguage::pushOp().
|
virtual |
Emit other (more unusual) syntax as part of source code generation.
This method is used to emit syntax not covered by the other methods, such as spaces, semi-colons, braces, and other punctuation.
| str | is the character data of the syntax being emitted |
| hl | indicates how the syntax should be highlighted |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PendingBrace::callback(), PrintC::docFunction(), PrintLanguage::emitAtom(), PrintC::emitBlockBasic(), PrintC::emitBlockDoWhile(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), PrintC::emitEnumDefinition(), PrintC::emitForLoop(), PrintC::emitFunctionDeclaration(), PrintC::emitGotoStatement(), PrintC::emitLabelStatement(), PrintC::emitPrototypeInputs(), PrintC::emitStatement(), PrintC::emitStructDefinition(), PrintC::emitSwitchCase(), PrintC::emitSymbolScope(), PrintC::emitVarDeclStatement(), PrintC::opCbranch(), EmitPrettyPrint::overflow(), and EmitPrettyPrint::print().
|
virtual |
(Re)set the default emitting options
Reimplemented in EmitPrettyPrint.
Referenced by EmitPrettyPrint::resetDefaults(), and PrintLanguage::resetDefaults().
|
inlinevirtual |
Set the comment fill characters for when line breaks are forced.
If the pretty printer forces a line break in the middle of a comment, this string is emitted to provide proper syntax and indenting to continue the comment.
| fill | is the set of fill characters |
Reimplemented in EmitPrettyPrint.
Referenced by PrintLanguage::setCommentDelimeter().
|
inline |
Set the number of characters indented per level of nesting.
| val | is the desired number of characters to indent |
References indentincrement.
Referenced by PrintLanguage::setIndentIncrement().
|
inlinevirtual |
Provide a maximum line size to the pretty printer.
The emitter may insert line breaks to enforce this maximum.
| mls | is the number of characters to set for the maximum line size |
Reimplemented in EmitPrettyPrint.
Referenced by PrintLanguage::setMaxLineSize().
|
inlinevirtual |
Set the output stream for the emitter.
Reimplemented in EmitPrettyPrint.
References s.
Referenced by EmitPrettyPrint::setOutputStream(), PrintLanguage::setOutputStream(), and EmitPrettyPrint::setXML().
|
inline |
Set a pending print callback.
The callback will be issued prior to the the next call to tagLine() unless a the method cancelPendingPrint() is called first.
| pend | is the callback to be issued |
References pendPrint.
Referenced by PrintC::emitBlockIf().
|
virtual |
Emit a sequence of space characters as part of source code.
| num | is the number of space characters to emit |
| bump | is the number of characters to indent if the spaces force a line break |
Reimplemented in EmitPrettyPrint.
Referenced by PrintC::emitBlockBasic(), PrintC::emitBlockDoWhile(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), PrintC::emitEnumDefinition(), PrintC::emitForLoop(), PrintC::emitFunctionDeclaration(), PrintC::emitGotoStatement(), PrintLanguage::emitLineComment(), PrintLanguage::emitOp(), PrintC::emitStructDefinition(), PrintC::emitSwitchCase(), PrintC::opBranch(), PrintC::opCbranch(), PrintC::opReturn(), and EmitPrettyPrint::print().
|
inlinevirtual |
Start a comment block within the emitted source code.
Inform the emitter that a set of comment tokens/lines is starting.
Reimplemented in EmitPrettyPrint.
Referenced by PrintLanguage::emitLineComment().
|
inlinevirtual |
Start a new indent level.
Inform the emitter that one level of nesting is being added.
Reimplemented in EmitPrettyPrint.
References indentincrement, and indentlevel.
Referenced by PendingBrace::callback(), PrintC::docFunction(), PrintC::emitBlockDoWhile(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), PrintC::emitEnumDefinition(), PrintC::emitForLoop(), and PrintC::emitStructDefinition().
|
inlinevirtual |
End a comment block.
Inform the emitter that a set of comment tokens/lines is ending.
| id | is the id associated with the block (as returned by startComment) |
Reimplemented in EmitPrettyPrint.
Referenced by PrintLanguage::emitLineComment().
|
inlinevirtual |
End an indent level.
Inform the emitter that the current nesting has ended, and we are returning to the previous level.
| id | is the id associated with the nesting (as returned by startIndent) |
Reimplemented in EmitPrettyPrint.
References indentincrement, and indentlevel.
Referenced by PrintC::docFunction(), PrintC::emitBlockDoWhile(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), PrintC::emitEnumDefinition(), PrintC::emitForLoop(), and PrintC::emitStructDefinition().
|
virtual |
Emit a comment string as part of the generated source code.
Individual comments can be broken up and emitted using multiple calls to this method, but ultimately the comment delimiters and the body of the comment are both emitted with this method, which may provide addition markup.
| ptr | is the character data for the comment |
| hl | indicates how the comment should be highlighted |
| spc | is the address space of the address where the comment is attached |
| off | is the offset of the address where the comment is attached |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitLineComment().
|
virtual |
Emit an identifier for a field within a structured data-type.
A string representing an individual component of a structured data-type is emitted, possibly with additional markup.
| ptr | is the character data for the identifier |
| hl | indicates how the identifier should be highlighted |
| ct | is the data-type associated with the field |
| o | is the (byte) offset of the field within its structured data-type |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitAtom().
|
virtual |
Emit a function identifier.
An identifier string representing the symbol name of the function is emitted, possible with additional markup.
| ptr | is the character data for the identifier |
| hl | indicates how the identifier should be highlighted |
| fd | is the function |
| op | is the CALL operation associated within the syntax tree or null for a declaration |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitAtom(), and PrintC::emitFunctionDeclaration().
|
virtual |
Emit a code label identifier.
A string describing a control-flow destination, as appropriate for the source language is output, possibly with additional markup.
| ptr | is the character data of the label |
| hl | indicates how the label should be highlighted |
| spc | is the address space of the code address being labeled |
| off | is the offset of the code address being labeled |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintC::emitLabel().
|
virtual |
Force a line break and indent level.
Tell the emitter that a new line is desired at a specific indent level. The indent level is overridden only for the line, then it returns to its previous value.
| indent | is the desired indent level for the new line |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
|
virtual |
Force a line break.
Tell the emitter that a new line is desired at the current indent level.
Reimplemented in EmitPrettyPrint.
Referenced by PrintC::docAllGlobals(), PrintC::docFunction(), PrintC::docSingleGlobal(), PrintC::emitBlockBasic(), PrintC::emitBlockDoWhile(), PrintC::emitBlockGoto(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockSwitch(), PrintC::emitBlockWhileDo(), PrintC::emitCommentFuncHeader(), PrintC::emitEnumDefinition(), PrintC::emitForLoop(), PrintC::emitLabelStatement(), PrintLanguage::emitLineComment(), PrintC::emitLocalVarDecls(), PrintC::emitStructDefinition(), PrintC::emitSwitchCase(), PrintC::emitVarDeclStatement(), EmitPrettyPrint::overflow(), and EmitPrettyPrint::print().
|
virtual |
Emit an operation token.
The string representing the operation as appropriate for the source language is emitted, possibly with additional markup.
| ptr | is the character data for the emitted representation |
| hl | indicates how the token should be highlighted |
| op | is the PcodeOp object associated with the operation with the syntax tree |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitAtom(), PrintC::emitBlockDoWhile(), PrintC::emitBlockIf(), PrintC::emitBlockInfLoop(), PrintC::emitBlockWhileDo(), PrintC::emitForLoop(), PrintLanguage::emitOp(), PrintC::opBranch(), PrintC::opBranchind(), PrintC::opCbranch(), and PrintC::opReturn().
|
virtual |
Emit a data-type identifier.
A string representing the name of a data-type, as appropriate for the source language is emitted, possibly with additional markup.
| ptr | is the character data for the identifier |
| hl | indicates how the identifier should be highlighted |
| ct | is the data-type description object |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitAtom().
|
virtual |
Emit a variable token.
An identifier string representing the variable is output, possibly with additional markup.
| ptr | is the character data for the identifier |
| hl | indicates how the identifier should be highlighted |
| vn | is the Varnode representing the variable within the syntax tree |
| op | is a p-code operation related to the use of the variable (may be null) |
Reimplemented in EmitNoXml, and EmitPrettyPrint.
Referenced by PrintLanguage::emitAtom().
|
staticprivate |
Map from syntax_highlight enumeration to color attribute string.
Referenced by beginDocument(), and beginFunction().