From e7de858ce27ded585d6e2ec4a5cbc3467c6a7593 Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Sun, 25 Sep 2016 14:30:34 -0600 Subject: CFamily AST printer --- include/printer/cfamily.h | 57 ++++++++++- include/printer/dump.h | 35 +++++++ include/printer/generic.h | 238 +++++++++++++++++++++++++++++++--------------- 3 files changed, 252 insertions(+), 78 deletions(-) (limited to 'include/printer') diff --git a/include/printer/cfamily.h b/include/printer/cfamily.h index f6e2284..83e7765 100644 --- a/include/printer/cfamily.h +++ b/include/printer/cfamily.h @@ -12,49 +12,102 @@ */ namespace chill { - namespace printer{ + namespace printer { /*! * \brief Print the AST for C like syntax, This replace the old print function * Custom multiplexer should not be needed. This version should calculate the correct precedence for expressions. * Expression should be encapsulated in {} or () or ended with ; with heuristics at the parent node + * + * All precedence calculation taken from http://en.cppreference.com/w/cpp/language/operator_precedence */ class CFamily : public GenericPrinter { public: CFamily() {} - virtual int getPrec(chillAST_Node *n); + + virtual int getPrecS(chillAST_BinaryOperator *n); + + virtual int getPrecS(chillAST_CallExpr *n); + + virtual int getPrecS(chillAST_CStyleAddressOf *n); + + virtual int getPrecS(chillAST_CStyleCastExpr *n); + + virtual int getPrecS(chillAST_TernaryOperator *n); + + virtual int getPrecS(chillAST_UnaryOperator *n); + virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o); + + //! Compound statement is responsible to break a new line if necessary virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o); + /*! + * Prints the floatpoint literal, only the showpoint flag is currently set + * @param ident + * @param n + * @param o + */ virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o); }; } diff --git a/include/printer/dump.h b/include/printer/dump.h index dd8a0e8..fd382a3 100644 --- a/include/printer/dump.h +++ b/include/printer/dump.h @@ -17,40 +17,75 @@ namespace chill { class Dump : public GenericPrinter { public: Dump() {} + virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o); + /*! * Just prints everything. Indent is igored due to need to limit the number of output * @param ident diff --git a/include/printer/generic.h b/include/printer/generic.h index 70ebb41..6918136 100644 --- a/include/printer/generic.h +++ b/include/printer/generic.h @@ -16,7 +16,7 @@ namespace chill { namespace printer { class GenericPrinter { - private: + protected: std::string identSpace; public: GenericPrinter() { identSpace = " "; } @@ -31,80 +31,165 @@ namespace chill { for (int i = 0; i < numspaces; ++i) identSpace += " "; } - virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_BinaryOperator *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CallExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CompoundStmt *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CStyleAddressOf *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CStyleCastExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CudaFree *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CudaKernelCall *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CudaMalloc *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CudaMemcpy *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_CudaSyncthreads *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_DeclRefExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_FloatingLiteral *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_ForStmt *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_Free *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_FunctionDecl *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_IfStmt *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_IntegerLiteral *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_ImplicitCastExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_MacroDefinition *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_Malloc *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_MemberExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_NULL *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_NoOp *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_ParenExpr *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_Preprocessing *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_RecordDecl *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_ReturnStmt *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_Sizeof *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_SourceFile *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_TypedefDecl *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_TernaryOperator *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_UnaryOperator *n) { return INT8_MAX; } - virtual int getPrecS(chillAST_VarDecl *n) { return INT8_MAX; } + + /*! + * Default return value for get prec, can be used as a reference + * @return default precedence + */ + virtual int defGetPrecS() { return INT8_MAX; } + + virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_BinaryOperator *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CallExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CompoundStmt *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CStyleAddressOf *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CStyleCastExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CudaFree *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CudaKernelCall *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CudaMalloc *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CudaMemcpy *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_CudaSyncthreads *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_DeclRefExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_FloatingLiteral *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_ForStmt *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_Free *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_FunctionDecl *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_IfStmt *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_IntegerLiteral *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_ImplicitCastExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_MacroDefinition *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_Malloc *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_MemberExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_NULL *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_NoOp *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_ParenExpr *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_Preprocessing *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_RecordDecl *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_ReturnStmt *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_Sizeof *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_SourceFile *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_TypedefDecl *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_TernaryOperator *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_UnaryOperator *n) { return defGetPrecS(); } + + virtual int getPrecS(chillAST_VarDecl *n) { return defGetPrecS(); } //! return the Precedence of the corresponding AST node /*! * @param n the chillAST_Node * @return a int representing the subnodes's precedence, 0 being the highest, INT8_MAX being the default */ virtual int getPrec(chillAST_Node *n); - virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o)=0; - virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o)=0; + //! default error output when encountering not recognized error code + /*! + * @param ident + * @param n + * @param o + */ + virtual void errorPrintS(std::string ident, chillAST_Node *n, std::ostream &o); + + virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) { + errorPrintS(ident, n, o); + } + + virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { errorPrintS(ident, n, o); } + +// virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o) { + errorPrintS(ident, n, o); + } + + virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); } + + virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) { errorPrintS(ident, n, o); } //! Print the AST to string stream, multiplexer /*! * @param ident indentation of the node @@ -129,7 +214,7 @@ namespace chill { * @param n the chillAST_Node */ virtual void printOut(std::string ident, chillAST_Node *n) { - print(ident,n,std::cout); + print(ident, n, std::cout); } //! Print the AST to stdErr /*! @@ -137,15 +222,16 @@ namespace chill { * @param n the chillAST_Node */ virtual void printErr(std::string ident, chillAST_Node *n) { - print(ident,n,std::cerr); + print(ident, n, std::cerr); } + //! Print the subexpression with precedence - virtual void printPrec(std::string ident,chillAST_Node *n,std::ostream &o, int prec) { - if (getPrec(n) > prec) o<<"("; - print(ident,n,prec); - if (getPrec(n) > prec) o<<")"; + virtual void printPrec(std::string ident, chillAST_Node *n, std::ostream &o, int prec) { + if (getPrec(n) > prec) o << "("; + print(ident, n, o); + if (getPrec(n) > prec) o << ")"; } - }; + }; } } -- cgit v1.2.3-70-g09d2