summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2016-09-25 11:06:42 -0600
committerTuowen Zhao <ztuowen@gmail.com>2016-09-25 11:06:42 -0600
commit2a5618583e1fdc8cde0308d3e5b1873bc94c5fb1 (patch)
tree51e83cb14c9ba846ff5e2b8dd6c017e9b57dea75
parent51c4aaaa21a124abafe3f950e12c3c25ddbe0812 (diff)
downloadchill-2a5618583e1fdc8cde0308d3e5b1873bc94c5fb1.tar.gz
chill-2a5618583e1fdc8cde0308d3e5b1873bc94c5fb1.tar.bz2
chill-2a5618583e1fdc8cde0308d3e5b1873bc94c5fb1.zip
staging
-rw-r--r--include/chillAST/chillAST_node.hh11
-rw-r--r--include/chillAST/chillASTs.hh40
-rw-r--r--include/printer/cfamily.h71
-rw-r--r--include/printer/dump.h10
-rw-r--r--include/printer/generic.h39
-rw-r--r--src/chillASTs.cc490
-rw-r--r--src/printer/cfamily.cpp279
7 files changed, 367 insertions, 573 deletions
diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh
index 80f1be4..bed2f22 100644
--- a/include/chillAST/chillAST_node.hh
+++ b/include/chillAST/chillAST_node.hh
@@ -25,7 +25,6 @@ public:
char *filename;
//! for compiler internals, formerly a comment
char *metacomment;
- std::vector<chillAST_Preprocessing *> preprocessinginfo;
//! for manufactured scalars
static int chill_scalar_counter;
@@ -418,7 +417,7 @@ public:
fprintf(fp, "(%s) forgot to implement printName()\n", getTypeString());
};// print CODE
- //! The AST's print version
+ //! The AST's print version, for reason unknown and incorrect
virtual char *stringRep(int indent = 0) {
fflush(stdout);
// TODO chillindent(indent, fp);
@@ -426,9 +425,6 @@ public:
exit(-1);
}
-
- virtual void printonly(int indent = 0, FILE *fp = stderr) { print(indent, fp); };
-
virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) {
int n = children.size();
for (int i = 0; i < n; i++) {
@@ -528,14 +524,11 @@ public:
fprintf(stderr, "\n\n");
}
- void printPreprocBEFORE(int indent, FILE *fp);
-
- void printPreprocAFTER(int indent, FILE *fp);
-
virtual chillAST_SymbolTable* getParameters() {return parameters;}
virtual chillAST_VarDecl* getParameter(const char * name);
virtual void addParameter(chillAST_VarDecl* name);
+ //! Emulation of the old dump function but using printer instead of hardcoded heuritics
void dump(int indent=0,FILE *fp = stderr);
};
diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh
index a84b820..36442ee 100644
--- a/include/chillAST/chillASTs.hh
+++ b/include/chillAST/chillASTs.hh
@@ -11,13 +11,6 @@ public:
virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_NULL;}
chillAST_NULL() {
};
-
- void print(int indent = 0, FILE *fp = stderr) {
- chillindent(indent, fp);
- fprintf(fp, "/* (NULL statement); */ ");
- fflush(fp);
- }
-
};
class chillAST_Preprocessing : public chillAST_Node {
@@ -90,9 +83,6 @@ public:
fprintf(stderr, "TypedefDecl getUnderLyingType()\n");
return underlyingtype;
};
-
- void print(int indent = 0, FILE *fp = stderr);
-
};
class chillAST_VarDecl : public chillAST_Node {
@@ -166,8 +156,6 @@ public:
chillAST_VarDecl(chillAST_RecordDecl *astruct, const char *n, const char *arraypart);
- void print(int indent = 0, FILE *fp = stderr);
-
void printName(int indent = 0, FILE *fp = stderr);
bool isParmVarDecl() { return (isAParameter == 1); };
@@ -446,13 +434,13 @@ public:
}; // end FunctionDecl
class chillAST_SourceFile : public chillAST_Node {
+ // TODO included source file
public:
virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_SOURCEFILE;}
// constructors
chillAST_SourceFile(const char *filename); // defined in chill_ast.cc
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
void printToFile(char *filename = NULL);
char *SourceFileName; // where this originated
@@ -522,8 +510,6 @@ public:
chillAST_Node *getBody() { return (body); }
- void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
-
chillAST_Node *clone();
// none of these make sense for macros
@@ -592,7 +578,6 @@ public:
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
void printControl(int indent = 0, FILE *fp = stderr); // print just for ( ... ) but not body
chillAST_Node *constantFold();
@@ -711,7 +696,6 @@ public:
// required methods that I can't seem to get to inherit
void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void printonly(int indent = 0, FILE *fp = stderr);
chillAST_Node *constantFold();
@@ -820,7 +804,6 @@ public:
// required methods that I can't seem to get to inherit
void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void printonly(int indent = 0, FILE *fp = stderr);
char *stringRep(int indent = 0);
@@ -892,9 +875,6 @@ public:
void replaceChild(chillAST_Node *old, chillAST_Node *newchild); // will examine index
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void printonly(int indent = 0, FILE *fp = stderr);
-
void print(int indent = 0, FILE *fp = stderr) const; // print CODE in chill_ast.cc
char *stringRep(int indent = 0);
@@ -957,10 +937,6 @@ public:
bool operator==(const chillAST_MemberExpr &);
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void printonly(int indent = 0, FILE *fp = stderr);
-
- void print(int indent = 0, FILE *fp = stderr) const; // print CODE in chill_ast.cc
char *stringRep(int indent = 0);
chillAST_Node *constantFold();
@@ -1013,7 +989,6 @@ public:
int evalAsInt() { return value; }
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1151,8 +1126,6 @@ public:
// required methods that I can't seem to get to inherit
void replaceChild(chillAST_Node *old, chillAST_Node *newchild);
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void printonly(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1352,19 +1325,15 @@ class chillAST_Malloc : public chillAST_Node { // malloc( sizeof(int) * 2048 )
public:
virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MALLOC;}
// variables that are special for this type of node
- //! to void if this is null , sizeof(thing) if it is not
- char *thing;
//! The subexpression calculating bytes
chillAST_Node *sizeexpr; // bytes
// constructors
- chillAST_Malloc(char *thething, chillAST_Node *numthings); // malloc (sizeof(int) *1024)
+ chillAST_Malloc(chillAST_Node *numthings); // malloc (sizeof(int) *1024)
// other methods particular to this type of node
-
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1412,7 +1381,6 @@ public:
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1607,7 +1575,6 @@ public:
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1641,7 +1608,6 @@ public:
chillAST_NoOp(); // { parent = p; };
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr) {}; // print CODE in chill_ast.cc
chillAST_Node *constantFold() {};
chillAST_Node *clone() { chillAST_Node* n = new chillAST_NoOp(); n->setParent(parent); return n; }; // ??
@@ -1746,8 +1712,6 @@ public:
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- //void dump( int indent=0, FILE *fp = stderr ); // print ast in chill_ast.cc
};
diff --git a/include/printer/cfamily.h b/include/printer/cfamily.h
index b36a873..f6e2284 100644
--- a/include/printer/cfamily.h
+++ b/include/printer/cfamily.h
@@ -9,46 +9,53 @@
/*!
* \file
- * \brief Print the AST for C like syntax, This replace the old print function
*/
namespace chill {
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
+ */
class CFamily : public GenericPrinter {
public:
CFamily() {}
virtual int getPrec(chillAST_Node *n);
- virtual void print(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_BinaryOperator *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CallExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CompoundStmt *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CStyleAddressOf *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CStyleCastExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CudaFree *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CudaKernelCall *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CudaMalloc *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CudaMemcpy *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_CudaSyncthreads *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_DeclRefExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_FloatingLiteral *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_ForStmt *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_Free *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_FunctionDecl *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_ImplicitCastExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_MacroDefinition *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_Malloc *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_MemberExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_ParenExpr *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_Preprocessing *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_RecordDecl *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_ReturnStmt *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_Sizeof *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_SourceFile *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_TypedefDecl *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_TernaryOperator *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_UnaryOperator *n, std::ostringstream &o);
- virtual void print(std::string ident, chillAST_VarDecl *n, std::ostringstream &o);
- virtual void print(string ident, chillAST_Node *n, ostringStream &o);
+ 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);
};
}
}
diff --git a/include/printer/dump.h b/include/printer/dump.h
index bf7168b..dd8a0e8 100644
--- a/include/printer/dump.h
+++ b/include/printer/dump.h
@@ -7,13 +7,13 @@
#include "printer/generic.h"
-/*!
- * \file
- * \brief this replace the old dump function in the chillAST
- */
-
namespace chill {
namespace printer {
+ /*!
+ * \brief this replace the old dump function in the chillAST
+ *
+ * Everthing is written in a Tree-like structure: (<NodeName> <Params>). No precedence calculation is needed.
+ */
class Dump : public GenericPrinter {
public:
Dump() {}
diff --git a/include/printer/generic.h b/include/printer/generic.h
index f3fd8f2..70ebb41 100644
--- a/include/printer/generic.h
+++ b/include/printer/generic.h
@@ -11,20 +11,25 @@
/*!
* \file
- * \brief this is a generic AST printSer that printSs the code out to a C-family like syntax
+ * \brief this is a generic AST printSer that prints the code out to a C-family like syntax
*/
namespace chill {
namespace printer {
class GenericPrinter {
private:
- std::string indentSpace;
+ std::string identSpace;
public:
- GenericPrinter() { indentSpace = " "; }
-
+ GenericPrinter() { identSpace = " "; }
+ //! Set the indentation for print
+ /*!
+ * Some subclass has indentation unused, like Dump. Also, only spaces is supported,
+ * so it is a number of the spaces in the indentaion.
+ * @param numspaces number of spaces for the indentation
+ */
void setIndentSpace(int numspaces) {
- indentSpace = "";
+ identSpace = "";
for (int i = 0; i < numspaces; ++i)
- indentSpace += " ";
+ identSpace += " ";
}
virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return INT8_MAX; }
virtual int getPrecS(chillAST_BinaryOperator *n) { return INT8_MAX; }
@@ -118,6 +123,28 @@ namespace chill {
print(ident, n, os);
return os.str();
}
+ //! Print the AST to stdout
+ /*!
+ * @param ident indentation of the node, the one inherited from the parent
+ * @param n the chillAST_Node
+ */
+ virtual void printOut(std::string ident, chillAST_Node *n) {
+ print(ident,n,std::cout);
+ }
+ //! Print the AST to stdErr
+ /*!
+ * @param ident indentation of the node
+ * @param n the chillAST_Node
+ */
+ virtual void printErr(std::string ident, chillAST_Node *n) {
+ 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<<")";
+ }
};
}
}
diff --git a/src/chillASTs.cc b/src/chillASTs.cc
index e5e9f74..69fe3ac 100644
--- a/src/chillASTs.cc
+++ b/src/chillASTs.cc
@@ -79,30 +79,6 @@ char *parseUnderlyingType(const char *sometype) {
return underlying;
}
-void printSymbolTable(chillAST_SymbolTable *st) {
- CHILL_DEBUG_PRINT("%d entries\n", st->size());
- if (!st) return;
- for (int i = 0; i < st->size(); i++) {
- printf("%d ", i);
- (*st)[i]->printName();
- printf("\n");
- }
- if (st->size())printf("\n");
- fflush(stdout);
-}
-
-void printSymbolTableMoreInfo(chillAST_SymbolTable *st) {
- CHILL_DEBUG_PRINT("%d entries\n", st->size());
- if (!st) return;
- for (int i = 0; i < st->size(); i++) {
- printf("%d ", i);
- (*st)[i]->print();
- printf("\n");
- }
- if (st->size())printf("\n");
- fflush(stdout);
-}
-
char *splitVariableName(char *name) {
char *cdot = strstr(name, ".");
char *carrow = strstr(name, "->"); // initial 'c' for const - can't change those
@@ -311,33 +287,6 @@ chillAST_RecordDecl *chillAST_Node::findRecordDeclNamed(const char *name) { // r
return parent->findRecordDeclNamed(name);
}
-
-void chillAST_Node::printPreprocBEFORE(int indent, FILE *fp) {
- int numstmts = preprocessinginfo.size();
- //if (0 != numstmts) {
- // fprintf(fp, "chillAST_Node::printPreprocBEFORE() %d statements\n", numstmts);
- //}
-
-
- for (int i = 0; i < numstmts; i++) {
- //fprintf(fp, "stmt %d %d\n", i, preprocessinginfo[i]->position);
- if (preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_LINEBEFORE ||
- preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_IMMEDIATELYBEFORE) {
- //fprintf(stderr, "before %d\n", preprocessinginfo[i]->position);
- preprocessinginfo[i]->print(indent, fp);
- }
- }
-}
-
-void chillAST_Node::printPreprocAFTER(int indent, FILE *fp) {
- for (int i = 0; i < preprocessinginfo.size(); i++) {
- if (preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_LINEAFTER ||
- preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_TOTHERIGHT) {
- preprocessinginfo[i]->print(indent, fp);
- }
- }
-}
-
chillAST_SourceFile::chillAST_SourceFile(const char *filename) {
if(filename) SourceFileName = strdup(filename);
else SourceFileName = strdup("No Source File");
@@ -347,32 +296,6 @@ chillAST_SourceFile::chillAST_SourceFile(const char *filename) {
frontend = strdup("unknown");
};
-void chillAST_SourceFile::print(int indent, FILE *fp) {
- fflush(fp);
- CHILL_DEBUG_PRINT("\n// this source derived from CHILL AST originally from file '%s' as parsed by frontend compiler %s\n\n",
- SourceFileName, frontend);
- std::vector<char *> includedfiles;
- int sofar = 0;
-
- //fprintf(fp, "#define __rose_lt(x,y) ((x)<(y)?(x):(y))\n#define __rose_gt(x,y) ((x)>(y)?(x):(y))\n"); // help diff figure out what's going on
-
- int numchildren = children.size();
-
- for (int i = 0; i < numchildren; i++) {
- if (children[i]->isFromSourceFile) {
- if (children[i]->isFunctionDecl()) {
- fprintf(stderr, "\nchild %d function %s\n", i, ((chillAST_FunctionDecl *) children[i])->functionName);
- }
- children[i]->print(indent, fp);
- if (children[i]->isVarDecl()) fprintf(fp, ";\n");
- fflush(fp); // top level vardecl\n");
- }
- }
-
- fflush(fp);
-};
-
-
void chillAST_SourceFile::printToFile(char *filename) {
char fn[1024];
@@ -461,49 +384,6 @@ chillAST_TypedefDecl::chillAST_TypedefDecl(char *t, const char *a, char *p) {
filename = NULL;
};
-
-void chillAST_TypedefDecl::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
-
- if (isStruct) {
- fprintf(fp, "\n/* A typedef STRUCT */\n");
- chillindent(indent, fp);
- }
-
- chillindent(indent, fp);
- fprintf(fp, "typedef ");
- fflush(fp);
-
- if (rd) {
- rd->print(indent, fp); // needs to not print the ending semicolon ??
- } else if (isStruct) {
- fprintf(stderr, "/* no rd */\n");
-
- //fprintf(fp, "struct %s\n", structname);
- chillindent(indent, fp);
- fprintf(fp, "{\n");
- for (int i = 0; i < subparts.size(); i++) {
- //fprintf(fp, "a %s\n", subparts[i]->getTypeString());
- subparts[i]->print(indent + 1, fp);
- fprintf(fp, ";\n");
- }
- fprintf(fp, "};\n");
- } else {
- fprintf(fp, "/* NOT A STRUCT */ typedef %s %s%s;\n", underlyingtype, newtype, arraypart);
- dump();
- printf("\n\n");
- fflush(stdout);
- }
-
- // then the newname
- fprintf(fp, "%s;\n", newtype);
- fflush(fp);
- printPreprocAFTER(indent, fp);
-
- return;
-}
-
-
chillAST_VarDecl *chillAST_TypedefDecl::findSubpart(const char *name) {
//fprintf(stderr, "chillAST_TypedefDecl::findSubpart( %s )\n", name);
//fprintf(stderr, "typedef %s %s\n", structname, newtype);
@@ -580,8 +460,6 @@ void chillAST_RecordDecl::print(int indent, FILE *fp) {
CHILL_DEBUG_PRINT("chillAST_RecordDecl::print()\n");
if (isUnnamed) return;
- printPreprocBEFORE(indent, fp);
-
chillindent(indent, fp);
if (isStruct) {
fprintf(fp, "struct ");
@@ -600,7 +478,6 @@ void chillAST_RecordDecl::print(int indent, FILE *fp) {
CHILL_ERROR("/* UNKNOWN RECORDDECL print() */ ");
exit(-1);
}
- printPreprocAFTER(indent, fp);
fflush(fp);
}
@@ -677,12 +554,10 @@ void chillAST_FunctionDecl::insertChild(int i, chillAST_Node *node) {
chillAST_VarDecl *vd = ((chillAST_VarDecl *) node);
fprintf(stderr, "functiondecl %s inserting a VarDecl named %s\n", functionName, vd->varname);
chillAST_SymbolTable *st = getSymbolTable();
- if (!st) {
+ if (!st)
fprintf(stderr, "symbol table is NULL!\n");
- } else {
+ else
fprintf(stderr, "%d entries in the symbol table\n", st->size());
- printSymbolTable(getSymbolTable());
- }
fprintf(stderr, "\n\n");
}
}
@@ -706,8 +581,6 @@ void chillAST_FunctionDecl::print(int indent, FILE *fp) {
//chillindent(indent, fp);
//fprintf(fp, "//(functiondecl) %d parameters\n", numparameters);
- printPreprocBEFORE(indent, fp);
-
fprintf(fp, "\n");
chillindent(indent, fp);
@@ -746,8 +619,6 @@ void chillAST_FunctionDecl::print(int indent, FILE *fp) {
fprintf(fp, "; // fwd decl\n");
}
- printPreprocAFTER(indent, fp);
-
fflush(fp);
}
@@ -940,26 +811,6 @@ void chillAST_MacroDefinition::addChild(chillAST_Node *node) {
node->setParent(this); // this, or body??
}
-void chillAST_MacroDefinition::print(int indent, FILE *fp) { // UHOH TODO
- CHILL_DEBUG_PRINT("macro has %d parameters\n", parameters->size());
-
- printPreprocBEFORE(indent, fp);
-
- fprintf(fp, "#define %s", macroName);
- if (0 != parameters->size()) {
- fprintf(fp, "(");
- for (int i = 0; i < parameters->size(); i++) {
- if (i) fprintf(fp, ",");
- fprintf(fp, "%s", (*parameters)[i]->varname);
- }
- fprintf(fp, ") ");
- }
-
- if (body) body->print(0, fp); // TODO should force newlines out of multiline macros
- fprintf(fp, "\n");
-}
-
-
chillAST_ForStmt::chillAST_ForStmt() {
init = cond = incr = NULL;
body = new chillAST_CompoundStmt();
@@ -1093,64 +944,6 @@ void chillAST_ForStmt::printControl(int in, FILE *fp) {
fflush(fp);
}
-
-void chillAST_ForStmt::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
- //fprintf(fp, "chillAST_ForStmt::print()\n");
- if (metacomment) {
- chillindent(indent, fp);
- //for(int i=0; i<indent; i++) fprintf(fp, "..");
- fprintf(fp, "// %s\n", metacomment);
- }
-
- printControl(indent, fp); // does not do a newline or bracket
- fprintf(fp, " {\n");
-
-
- // I have no idea what made me do this next bit.
- // A forstmt with compounds inside compounds ???
- // this should probably all go away
-
- chillAST_Node *b = body;
- //fprintf(fp, "b children %d\n", b->getNumChildren());
- //fprintf(fp, "body child 0 of type %s\n", b->children[0]->getTypeString());
- //fprintf(stderr, "forstmt body type %s\n", Chill_AST_Node_Names[b->getType()] );
- // deal with a tree of compound statements, in an ugly way. leave the ugliness
- while (1 == b->getNumChildren() && b->children[0]->isCompoundStmt()) {
- b = b->children[0];
- }
-
-
- // this was to sometimes not enclose in a bracket. stupid. always enclose in a bracket.
- //if (1 == b->getNumChildren() && b->children[0]->isForStmt()) fprintf(fp, ") {\n" );
- //else if (1 == b->getNumChildren() ) fprintf(fp, ") { ?? \n" ); // to allow for() for( ) to not have open bracket?
- //else {
- //fprintf(fp, ")\n");
- //chillindent(in, fp);
- //fprintf(fp, "{\n" );
-
- //fprintf(fp, ")");
- //}
-
- b->print(indent + 1, fp);
-
- // I think this can't happen any more. body is always a compound statement
- if (b->getType() == CHILLAST_NODE_BINARYOPERATOR) { // a single assignment statement
- fprintf(fp, ";\n");
- }
-
- // always print brackets
-
- //if ((1 == b->getNumChildren() && b->children[0]->isForStmt()) ||
- // (1 != b->getNumChildren() )) {
- chillindent(indent, fp);
- fprintf(fp, "}\n");
- //}
-
- printPreprocAFTER(indent, fp);
- fflush(fp); //
-}
-
chillAST_Node *chillAST_ForStmt::constantFold() {
init = init->constantFold();
cond = cond->constantFold();
@@ -1159,7 +952,6 @@ chillAST_Node *chillAST_ForStmt::constantFold() {
return this;
}
-
chillAST_Node *chillAST_ForStmt::clone() {
chillAST_ForStmt *fs = new chillAST_ForStmt(init->clone(), cond->clone(), incr->clone(), body->clone());
fs->isFromSourceFile = isFromSourceFile;
@@ -1677,8 +1469,6 @@ chillAST_IntegerLiteral *chillAST_BinaryOperator::evalAsIntegerLiteral() {
}
void chillAST_BinaryOperator::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
-
chillindent(indent, fp);
bool needparens = getPrec()<lhs->getPrec();
@@ -1696,8 +1486,6 @@ void chillAST_BinaryOperator::print(int indent, FILE *fp) {
else fprintf(fp, "(NULL)");
if (needparens) fprintf(fp, ")");
fflush(fp);
- printPreprocAFTER(indent, fp);
-
}
@@ -1706,18 +1494,6 @@ char *chillAST_BinaryOperator::stringRep(int indent) {
return strdup(s.c_str());
}
-
-void chillAST_BinaryOperator::printonly(int indent, FILE *fp) {
-
- lhs->printonly(indent, fp);
- fprintf(fp, " %s ", op);
- rhs->printonly(0, fp);
- fflush(fp);
-
-
-}
-
-
class chillAST_Node *chillAST_BinaryOperator::constantFold() {
//fprintf(stderr, "\nchillAST_BinaryOperator::constantFold() ");
//print(0,stderr); fprintf(stderr, "\n");
@@ -1931,7 +1707,6 @@ chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_No
}
void chillAST_TernaryOperator::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
chillindent(indent, fp);
fprintf(fp, "(");
condition->print(0, fp);
@@ -2000,18 +1775,6 @@ void chillAST_TernaryOperator::replaceVarDecls(chillAST_VarDecl *olddecl, chillA
rhs->replaceVarDecls(olddecl, newdecl);
}
-void chillAST_TernaryOperator::printonly(int indent, FILE *fp) {
- fprintf(fp, "(");
- condition->printonly(0, fp);
- fprintf(fp, "%s", op);
- lhs->printonly(0, fp);
- fprintf(fp, ":");
- rhs->printonly(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-}
-
-
class chillAST_Node *chillAST_TernaryOperator::constantFold() {
condition = condition->constantFold();
lhs = lhs->constantFold();
@@ -2164,14 +1927,6 @@ void chillAST_ArraySubscriptExpr::gatherIndeces(std::vector<chillAST_Node *> &in
ind.push_back(index);
}
-void chillAST_ArraySubscriptExpr::print(int indent, FILE *fp) {
- base->print(indent, fp);
- fprintf(fp, "[");
- index->print(0, fp);
- fprintf(fp, "]");
- fflush(fp);
-}
-
char *chillAST_ArraySubscriptExpr::stringRep(int indent) {
fprintf(stderr, "chillAST_ArraySubscriptExpr::stringRep\n");
@@ -2183,25 +1938,6 @@ char *chillAST_ArraySubscriptExpr::stringRep(int indent) {
return strdup(s.c_str());
}
-
-void chillAST_ArraySubscriptExpr::printonly(int indent, FILE *fp) {
- base->printonly(indent, fp);
- fprintf(fp, "[");
- index->printonly(0, fp);
- fprintf(fp, "]");
- fflush(fp);
-}
-
-
-void chillAST_ArraySubscriptExpr::print(int indent, FILE *fp) const {
- base->print(indent, fp);
- fprintf(fp, "[");
- index->print(0, fp);
- fprintf(fp, "]");
- fflush(fp);
-};
-
-
chillAST_VarDecl *chillAST_ArraySubscriptExpr::multibase() {
//this should probably be a chillAST_Node function instead of having all these ifs
return base->multibase();
@@ -2395,27 +2131,7 @@ chillAST_MemberExpr::chillAST_MemberExpr(chillAST_Node *bas, const char *mem, vo
}
}
-void chillAST_MemberExpr::print(int indent, FILE *fp) {
- if (base) base->print(indent, fp);
- else {
- chillindent(indent, fp);
- fprintf(fp, "(NULL)");
- }
- if (exptype == CHILLAST_MEMBER_EXP_ARROW) fprintf(fp, "->");
- else fprintf(fp, ".");
- if (member) fprintf(fp, "%s", member);
- else fprintf(fp, "(NULL)");
- fflush(fp);
-}
-
-
-void chillAST_MemberExpr::printonly(int indent, FILE *fp) {
- base->print(indent, fp);
- if (exptype == CHILLAST_MEMBER_EXP_ARROW) fprintf(fp, "->");
- else fprintf(fp, ".");
- fprintf(fp, "%s", member);
- fflush(fp);
-}
+// TODO member can be another member expression, Right?
char *chillAST_MemberExpr::stringRep(int indent) { // char pointer to what we'd print
fprintf(stderr, "*chillAST_MemberExpr::stringRep()\n");
@@ -2877,12 +2593,6 @@ chillAST_IntegerLiteral::chillAST_IntegerLiteral(int val) {
filename = NULL;
}
-void chillAST_IntegerLiteral::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "%d", value);
- fflush(fp);
-}
-
class chillAST_Node *chillAST_IntegerLiteral::constantFold() { return this; } // can never do anything
@@ -3090,18 +2800,6 @@ chillAST_ImplicitCastExpr::chillAST_ImplicitCastExpr(chillAST_Node *sub) {
//fprintf(stderr, " of type %s\n", subexpr->getTypeString());
}
-void chillAST_ImplicitCastExpr::print(int indent, FILE *fp) {
- // No need to print anything, simply forward to the sub expression.
- subexpr->print(indent, fp);
- fflush(fp);
-};
-
-void chillAST_ImplicitCastExpr::printonly(int indent, FILE *fp) {
- // No need to print anything, simply forward to the sub expression.
- subexpr->printonly(indent, fp);
- fflush(fp);
-};
-
void chillAST_ImplicitCastExpr::replaceChild(chillAST_Node *old, chillAST_Node *newchild) {
if (subexpr == old) { // should be the case for this to get called
subexpr = newchild;
@@ -3320,10 +3018,7 @@ void chillAST_CStyleAddressOf::gatherVarUsage(vector<chillAST_VarDecl *> &decls)
}
-chillAST_Malloc::chillAST_Malloc(char *thething, chillAST_Node *numthings) {
- thing = NULL;
- if (thething)
- thing = strdup(thething); // "int" or "float" or "struct widget"
+chillAST_Malloc::chillAST_Malloc(chillAST_Node *numthings) {
sizeexpr = numthings;
isFromSourceFile = true; // default
filename = NULL;
@@ -3334,7 +3029,7 @@ chillAST_Node *chillAST_Malloc::constantFold() {
}
chillAST_Node *chillAST_Malloc::clone() {
- chillAST_Malloc *M = new chillAST_Malloc(thing, sizeexpr); // the general version
+ chillAST_Malloc *M = new chillAST_Malloc(sizeexpr); // the general version
M->setParent(getParent());
M->isFromSourceFile = isFromSourceFile;
if (filename) M->filename = strdup(filename);
@@ -3366,19 +3061,6 @@ void chillAST_Malloc::gatherVarUsage(vector<chillAST_VarDecl *> &decls) {
sizeexpr->gatherVarUsage(decls);
};
-
-void chillAST_Malloc::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "malloc(");
-
- if (thing) {
- fprintf(fp, " sizeof(%s) * ", thing);
- }
- sizeexpr->print(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-};
-
chillAST_CudaMalloc::chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node *size) {
devPtr = devmemptr;
sizeinbytes = size; // probably a multiply like sizeof(int) * 1024
@@ -3495,20 +3177,6 @@ chillAST_CudaMemcpy::chillAST_CudaMemcpy(chillAST_VarDecl *d, chillAST_VarDecl *
cudaMemcpyKind = kind;
};
-void chillAST_CudaMemcpy::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "cudaMemcpy(%s,%s,", dest->varname, src->varname);
- //dest->print( 0, fp );
- //fprintf(fp, ",");
- // src->print( 0, fp ); just want the src NAME, not name and array info
- //fprintf(fp, ",");
- size->print(0, fp);
- fprintf(fp, ",%s)", cudaMemcpyKind);
- fflush(fp);
-};
-
-
-
class chillAST_Node *chillAST_CudaMemcpy::constantFold() {
dest = (chillAST_VarDecl *) dest->constantFold();
src = (chillAST_VarDecl *) src->constantFold();
@@ -3582,7 +3250,6 @@ chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval) {
void chillAST_ReturnStmt::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
chillindent(indent, fp);
if (returnvalue != NULL) {
fprintf(fp, "return(");
@@ -3654,7 +3321,6 @@ void chillAST_CallExpr::addArg(chillAST_Node *a) {
void chillAST_CallExpr::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
chillindent(indent, fp);
chillAST_FunctionDecl *FD = NULL;
chillAST_MacroDefinition *MD = NULL;
@@ -4066,137 +3732,6 @@ chillAST_VarDecl::chillAST_VarDecl(const char *t, const char *n, const char *a,
};
-
-void chillAST_VarDecl::print(int indent, FILE *fp) {
- //fprintf(fp, "chillAST_VarDecl::print()\n");
-
- printPreprocBEFORE(indent, fp);
-
- //fprintf(fp, "VarDecl vartype '%s' varname %s ", vartype, varname);
- //if (isAStruct()) fprintf(fp, "isAStruct()\n");
- //else fprintf(fp, "NOT A Struct\n");
-
- // fprintf(fp, "\n"); fflush(fp); dump(0,fp); fflush(fp); // debug
-
- chillindent(indent, fp);
- //fprintf(fp, "vardecl->print vartype '%s'\n", vartype);
- if (isDevice) fprintf(fp, "__device__ ");
- if (isShared) fprintf(fp, "__shared__ ");
-
- //if (isAStruct()) fprintf(fp, "/* isAStruct() */ ");
- //else fprintf(fp, "/* NOT A Struct() */ ");
- //if (vardef) fprintf(fp, "/* vardef */ ");
- //else fprintf(fp, "/* NOT vardef */ ");
-
-
- //fprintf(stderr, "chillAST_VarDecl::print() %s\n", varname );
- //if (isParmVarDecl()) fprintf(stderr, "%s is a parameter\n", varname);
- //if (isAStruct()) fprintf(stderr, "%s is a struct\n", varname);
- //else fprintf(stderr, "%s is NOT a struct\n", varname);
- //if (!parent) fprintf(stderr, "VARDECL HAS NO PARENT\n");
- //else fprintf(stderr, "parent of %s is type %s\n", varname, parent->getTypeString());
-
- // this logic is probably wrong (what about pointer to struct? )
-
- //fprintf(stderr, "checking for unnamed only used here\n");
-
- if ((!isAParameter) && isAStruct() && vardef) { // an unnamed struct used only here ??
-
- //fprintf(fp, "i%s sAStruct() && vardef ?? vardecl of type UNNAMED ONLY USED HERE \n", varname );
- // print the internals of the struct and then the name
- vardef->printStructure(0, fp);
- fprintf(fp, "%s", varname);
-
- return;
- }
-
- //fprintf(fp, "ugly logic\n");
- // ugly logic TODO
-
- if (typedefinition && typedefinition->isAStruct()) fprintf(fp, "struct ");
-
- if (isAParameter) {
- //fprintf(fp, "%s isaparameter\n", varname);
- //if (isAStruct()) fprintf(fp, "struct ");
- //fprintf(fp, "(param) nd %d", numdimensions );
- //dump();
- if (numdimensions > 0) {
- if (knownArraySizes) { // just [12][34][56]
- fprintf(fp, "%s ", vartype);
- if (byreference) fprintf(fp, "&");
- fprintf(fp, "%s", varname);
- for (int n = 0; n < (numdimensions); n++) fprintf(fp, "[%d]", arraysizes[n]);
- } else { // some unknown array part float *a; or float **a; or float (*)a[1234]
-
- //fprintf(fp, "\nsome unknown\n");
- //fprintf(fp, "arraypointerpart '%s'\n", arraypointerpart);
- //fprintf(fp, "arraysetpart '%s'\n", arraysetpart);
-
- if (numdimensions == 1) {
- //fprintf(fp, "\nnd1, vartype %s\n", vartype);
-
- // TODO this if means I have probably made a mistake somewhere
- if (!index(vartype, '*')) fprintf(fp, "%s *%s", vartype, varname); // float *x
- else fprintf(fp, "%s%s", vartype, varname); // float *a;
-
-
- } else { // more than one dimension
-
- if (!strcmp("", arraysetpart)) { // no known dimensions float ***a;
- fprintf(fp, "%s %s%s", vartype, arraypointerpart, varname);
- } else if (!strcmp("", arraypointerpart)) { // ALL known float a[2][7];
- fprintf(fp, "%s %s", vartype, varname);
- for (int n = 0; n < numdimensions; n++) fprintf(fp, "[%d]", arraysizes[n]);
- } else { // float (*)a[1234]
- // this seems really wrong
- // float (*)a[1234]
- fprintf(fp, "%s (", vartype);
- for (int n = 0; n < (numdimensions - 1); n++) fprintf(fp, "*");
- fprintf(fp, "%s)", varname);
- fprintf(fp, "[%d]", arraysizes[numdimensions - 1]);
- }
-
- }
- }
- } // if numdimensions > 0
- else { // parameter float x
- fprintf(fp, "%s ", vartype);
- if (byreference) fprintf(fp, "&");
- fprintf(fp, "%s", varname);
- }
- } // end parameter
-
- else { // NOT A PARAMETER
- //fprintf(fp, "NOT A PARAM ... vartype '%s'\n", vartype);
- //if (isArray()) fprintf(stderr, "an array, numdimensions %d\n", numdimensions);
- //fprintf(stderr, "arraysizes %p\n", arraysizes);
-
-
-
- //if (isArray() && arraysizes == NULL) {
- // // we just know the number of dimensions but no sizes
- // // int ***something
- // fprintf(fp, "%s ", vartype); // "int "
- // for (int i=0; i<numdimensions; i++) fprintf(fp, "*"); // ***
- // fprintf(fp, "%s", varname); // variable name
- // }
- // else
-
- fprintf(fp, "%s %s", vartype, arraypointerpart);
- if (isRestrict) fprintf(fp, " __restrict__ "); // wrong place
- fprintf(fp, "%s%s", varname, arraysetpart);
- if (init) {
- fprintf(fp, " = ");
- fflush(fp);
- init->print(0, fp);
- }
- }
- fflush(fp);
- //fprintf(stderr, "numdimensions %d arraysizes address 0x%x\n", numdimensions, arraysizes);
- //if (!isAParameter) fprintf(fp, ";\n", vartype, varname, arraypart );
-};
-
-
void chillAST_VarDecl::printName(int in, FILE *fp) {
chillindent(in, fp);
fprintf(fp, "%s", varname);
@@ -4218,9 +3753,7 @@ chillAST_CompoundStmt::chillAST_CompoundStmt() {
void chillAST_CompoundStmt::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
int numchildren = children.size();
- //fprintf(stderr, "NUMCHILDREN %d\n", numchildren); sleep(1);
for (int i = 0; i < numchildren; i++) {
children[i]->print(indent, fp);
if (children[i]->getType() != CHILLAST_NODE_FORSTMT
@@ -4510,14 +4043,6 @@ chillAST_Sizeof::chillAST_Sizeof(char *athing) {
thing = strdup(athing); // memory leak
}
-void chillAST_Sizeof::print(int indent, FILE *fp) {
- //fprintf(stderr, "chillAST_Sizeof::print()\n");
- chillindent(indent, fp); // hard to believe this will ever do anything
- fprintf(fp, "sizeof(");
- fprintf(fp, "%s)", thing);
- fflush(fp);
-}
-
void chillAST_Sizeof::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {}
void chillAST_Sizeof::gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento) {}
@@ -4741,7 +4266,6 @@ chillAST_Node *chillAST_IfStmt::clone() {
}
void chillAST_IfStmt::print(int indent, FILE *fp) {
- printPreprocBEFORE(indent, fp);
chillindent(indent, fp);
fprintf(fp, "if (");
if (cond) cond->print(0, fp);
@@ -5084,9 +4608,9 @@ chillAST_VarDecl* chillAST_Node::getParameter(const char *t) {
void chillAST_Node::dump(int indent, FILE *fp) {
if (fp == stderr) {
chill::printer::Dump d;
- d.print("",this,std::cerr);
+ d.printErr("",this);
} else {
chill::printer::Dump d;
- d.print("",this,std::cout);
+ d.printOut("",this);
}
} \ No newline at end of file
diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp
index 8bfeb82..a15c106 100644
--- a/src/printer/cfamily.cpp
+++ b/src/printer/cfamily.cpp
@@ -6,3 +6,282 @@
using namespace std;
using namespace chill::printer;
+
+bool ifSemicolonFree(CHILLAST_NODE_TYPE t) {
+ return t == CHILLAST_NODE_FUNCTIONDECL || t == CHILLAST_NODE_IFSTMT ||
+ t == CHILLAST_NODE_FORSTMT || t == CHILLAST_NODE_MACRODEFINITION;
+}
+
+void dumpVector(GenericPrinter *p, string ident, chillAST_NodeList *n, ostream &o) {
+ for (int i = 0; i < n->size(); ++i)
+ p->print("", (*n)[i], o);
+}
+
+void dumpVector(GenericPrinter *p, string ident, chillAST_SymbolTable *n, ostream &o) {
+ for (int i = 0; i < n->size(); ++i)
+ p->print("", (*n)[i], o);
+}
+
+void dumpVector(GenericPrinter *p, string ident, chillAST_TypedefTable *n, ostream &o) {
+ for (int i = 0; i < n->size(); ++i)
+ p->print("", (*n)[i], o);
+}
+
+void Dump::print(string ident, chillAST_Node *n, ostream &o) {
+ o << "(" << n->getTypeString() << " ";
+ if (n->getParameters()) {
+ o << "(Params: ";
+ dumpVector(this, ident, n->getParameters(), o);
+ o << ") ";
+ }
+ if (n->getSymbolTable()) {
+ o << "(VarScope: ";
+ dumpVector(this, ident, n->getSymbolTable(), o);
+ o << ") ";
+ }
+ if (n->getTypedefTable()) {
+ o << "(TypeDef: ";
+ dumpVector(this, ident, n->getTypedefTable(), o);
+ o << ") ";
+ }
+ o << ": ";
+ // Recurse
+ GenericPrinter::print(ident, n, o);
+ o << ") ";
+}
+
+void Dump::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) {
+ print(ident,n->base,o);
+ o<<"[";
+ print(ident,n->index,o);
+ o<<"]";
+}
+
+void Dump::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) {
+ o << n->op << " ";
+ if (n->lhs) print(ident, n->lhs, o);
+ else o << "(NULL) ";
+ if (n->rhs) print(ident, n->rhs, o);
+ else o << "(NULL) ";
+}
+
+void Dump::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) {
+ if (n->callee)
+ print(ident, n->callee, o);
+}
+
+void Dump::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) {
+ dumpVector(this, ident, n->getChildren(), o);
+}
+
+void Dump::printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) {
+ print(ident, n->subexpr, o);
+}
+
+void Dump::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) {
+ o << n->towhat << " ";
+ print(ident, n->subexpr, o);
+}
+
+void Dump::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) {
+ o << n->variable->varname << " ";
+}
+
+void Dump::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) {
+ CHILL_ERROR("Not implemented");
+}
+
+void Dump::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) {
+ print(ident, n->devPtr, o);
+ print(ident, n->sizeinbytes, o);
+}
+
+void Dump::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) {
+ o << "cudaMemcpy(" << n->dest->varname << ", " << n->src->varname << ", ";
+ print(ident, n->size, o);
+ o << ", " << n->cudaMemcpyKind << ")";
+}
+
+void Dump::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {}
+
+void Dump::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) {
+ chillAST_VarDecl *vd = n->getVarDecl();
+ if (vd)
+ if (vd->isAParameter) o << "ParmVar "; else o << "Var ";
+ o << n->declarationName << " ";
+ chillAST_FunctionDecl *fd = n->getFunctionDecl();
+ if (fd) dumpVector(this, ident, fd->getParameters(), o);
+}
+
+void Dump::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) {
+ if (n->precision == 1) o << "float ";
+ else o << "double ";
+ o << n->value;
+}
+
+void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
+ if (n->metacomment)
+ o << "// " << n->metacomment << "\n";
+ o << "for (";
+ print(ident, n->getInit(), o);
+ o << ";";
+ print(ident, n->getCond(), o);
+ o << ";";
+ print(ident, n->getInc(), o);
+ o << ")";
+ if (n->getBody()->getType() == CHILLAST_NODE_COMPOUNDSTMT) {
+ if (n->getBody()->getChildren()->size() < 2) o << "\n" << ident << identSpace;
+ else o << " ";
+ print(ident, n->getBody(), o);
+ } else {
+ CHILL_ERROR("Body of for loop not COMPOUNDSTMT\n");
+ print(ident, n->getBody(), o);
+ }
+}
+
+void Dump::printS(std::string ident, chillAST_Free *n, std::ostream &o) {}
+
+void Dump::printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) {
+ if (n->filename) o << n->filename << " ";
+ if (n->isFromSourceFile) o << "FromSourceFile" << " ";
+ o << n->returnType << " " << n->functionName << " ";
+ if (n->getBody()) print(ident, n->getBody(), o);
+}
+
+void Dump::printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) {
+ print(ident, n->cond, o);
+ print(ident, n->thenpart, o);
+ if (n->elsepart)
+ print(ident, n->elsepart, o);
+}
+
+void Dump::printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) {
+ o << n->value;
+}
+
+void Dump::printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o) {
+ print(ident, n->subexpr, o);
+}
+
+void Dump::printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) {
+ o << "#define" << n->macroName << " ";
+ int np = n->getParameters()->size();
+ if (np) {
+ o << "(" << n->getParameters()->at(0)->varname;
+ for (int i = 1; i < np; ++i)
+ o << ", " << n->getParameters()->at(i)->varname;
+ o<<")";
+ }
+ // TODO newline for multiline macro
+ print(ident, n->getBody(), o);
+}
+
+void Dump::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) {
+ o << "malloc(";
+ print(ident, n->sizeexpr, o);
+ o << ")";
+}
+
+void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) {
+ int prec = getPrec(n);
+ if (n->base) printPrec(ident, n->base, o, prec);
+ else o << "(NULL)";
+ if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "->";
+ else o << ".";
+ if (n->member) o << n->member;
+ else o << "(NULL)";
+}
+
+void Dump::printS(std::string ident, chillAST_NULL *n, std::ostream &o) {
+ o << "/* (NULL statement) */";
+}
+
+void Dump::printS(std::string ident, chillAST_NoOp *n, std::ostream &o) {}
+
+void Dump::printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) {
+ print(ident, n->subexpr, o);
+}
+
+void Dump::printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) {}
+
+void Dump::printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) {
+ // TODO access control
+ o << n->getName() << " ";
+ o << n->isAStruct() << " ";
+ o << n->isAUnion() << " ";
+}
+
+void Dump::printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) {
+ if (n->returnvalue) print(ident, n->returnvalue, o);
+}
+
+void Dump::printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) {
+ o << "sizeof(" << n->thing << ")";
+}
+
+void Dump::printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) {
+ o << "// this source is derived from CHILL AST originally from file '"
+ << n->filename << "' as parsed by frontend compiler " << n->frontend << "\n\n";
+ int nchild = n->getChildren()->size();
+ for (int i = 0; i < nchild; ++i) {
+ if (n->getChild(i)->isFromSourceFile) {
+ o << ident;
+ print(indent, n->getChild(i), o);
+ if (!isSemiColonFree(n->getChild(i)->getType())) o << ";\n";
+ else o<<"\n";
+ }
+ }
+}
+
+void Dump::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) {
+ if (n->isAStruct())
+ o << "/* A typedef STRUCT */\n";
+ o << ident << "typedef ";
+ if (n->rd) print(ident, n->rd, o);
+ else if (n->isAStruct()) {
+ o << "/* no rd */ ";
+ // the struct subparts
+ } else
+ o << "/* Not A STRUCT */ " << n->getUnderlyingType() << " " << n->newtype << " " << n->arraypart;
+ o << n->newtype;
+}
+
+void Dump::printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) {
+ o << n->op << " ";
+ print(ident, n->condition, o);
+ print(ident, n->lhs, o);
+ print(ident, n->rhs, o);
+}
+
+void Dump::printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) {
+ if (n->prefix) o << "prefix ";
+ else o << "postfix ";
+ print(ident, n->subexpr, o);
+}
+
+void Dump::printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) {
+ if (n->isDevice) o << "__device__ ";
+ if (n->isShared) o << "__shared__ ";
+ if (n->isRestrict) o << "__restrict__ ";
+
+ if ((!n->isAParameter) && n->isAStruct() && n->vardef) {
+ print(ident, n->vardef, o);
+ o << " " << n->varname;
+ }
+
+ if (n->typedefinition && n->typedefinition->isAStruct())
+ o << "struct ";
+ o << n->vartype << " ";
+ if (n->arraypointerpart)
+ o << n->arraypointerpart;
+ if (n->byreference)
+ o << "&";
+ o << n->varname;
+ if (n->knownArraySizes)
+ for (int i = 0; i < (n->numdimensions); ++i) o << "[" << n->arraysizes[i] << "]";
+
+ if (n->init) {
+ o << "= ";
+ print(ident, n->init, o);
+ }
+}