From a679e8028eba8a6d9b3ed3d45ba0f397b1083f0c Mon Sep 17 00:00:00 2001
From: Tuowen Zhao <ztuowen@gmail.com>
Date: Thu, 29 Sep 2016 22:59:50 -0600
Subject: fixed funcdecl

---
 include/chillAST/chillASTs.hh | 36 ++++++++++----------------
 src/chillASTs.cc              | 60 +++++++------------------------------------
 src/printer/dump.cpp          |  8 +++---
 3 files changed, 27 insertions(+), 77 deletions(-)

diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh
index b2cea3a..a30a420 100644
--- a/include/chillAST/chillASTs.hh
+++ b/include/chillAST/chillASTs.hh
@@ -364,7 +364,6 @@ public:
 
 class chillAST_FunctionDecl : public chillAST_Node {
 private:
-  chillAST_CompoundStmt *body; // always a compound statement?
   CHILLAST_FUNCTION_TYPE function_type;  // CHILLAST_FUNCTION_CPU or  CHILLAST_FUNCTION_GPU
   bool externfunc;   // function is external 
   bool builtin;      // function is a builtin
@@ -405,12 +404,9 @@ public:
 
   void addDecl(chillAST_VarDecl *vd);  // just adds to symbol table?? TODO
 
-  void addChild(chillAST_Node *node); // special because inserts into BODY
-  void insertChild(int i, chillAST_Node *node); // special because inserts into BODY
-
-  void setBody(chillAST_Node *bod);
+  void setBody(chillAST_Node *bod) { if (bod->isCompoundStmt()) { children[0] = bod; bod->setParent(this); }}
 
-  chillAST_CompoundStmt *getBody() { return (body); }
+  chillAST_CompoundStmt *getBody() { return (chillAST_CompoundStmt*)children[0]; }
 
   void gatherVarDecls(std::vector<chillAST_VarDecl *> &decls);
 
@@ -435,10 +431,7 @@ public:
 
   chillAST_Node *constantFold();
 
-  void replaceChild(chillAST_Node *old, chillAST_Node *newchild) {
-    body->replaceChild(old, newchild);
-  }
-};  // end FunctionDecl 
+};  // end FunctionDecl
 
 class chillAST_SourceFile : public chillAST_Node {
   // TODO included source file
@@ -493,6 +486,7 @@ public:
 
 };
 
+//! A Macro definition
 class chillAST_MacroDefinition : public chillAST_Node {
 private:
   chillAST_Node *body; // rhs      always a compound statement?
@@ -538,14 +532,15 @@ public:
 };
 
 class chillAST_ForStmt : public chillAST_Node {
-public:
-  virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FORSTMT; }
-
-  // variables that are special for this type of node
+private:
   chillAST_Node *init;
   chillAST_Node *cond;
   chillAST_Node *incr;
   chillAST_Node *body; // always a compoundstmt?
+public:
+  virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FORSTMT; }
+
+  // variables that are special for this type of node
   IR_CONDITION_TYPE conditionoperator;  // from ir_code.hh
 
   bool hasSymbolTable() { return true; };
@@ -561,20 +556,17 @@ public:
   void removeSyncComment();
 
   chillAST_Node *getInit() { return init; };
+  void setInit(chillAST_Node *i) { init = i; i->parent = this; }
 
   chillAST_Node *getCond() { return cond; };
+  void setCond(chillAST_Node *c) { cond = c; c->parent = this; }
 
   chillAST_Node *getInc() { return incr; };
+  void setInc(chillAST_Node *i) { incr = i; i->parent = this; }
 
-  chillAST_Node *
-  getBody() { //fprintf(stderr, "chillAST_ForStmt::getBody(), returning a chillAST_Node of type %s\n", body->getTypeString());
-    return body;
-  };
+  chillAST_Node *getBody() { return body; };
+  void setBody(chillAST_Node *b) { body = b; b->parent = this; };
 
-  void setBody(chillAST_Node *b) {
-    body = b;
-    b->parent = this;
-  };
 
   bool isNotLeaf() { return true; };
 
diff --git a/src/chillASTs.cc b/src/chillASTs.cc
index 31c6758..e026e45 100644
--- a/src/chillASTs.cc
+++ b/src/chillASTs.cc
@@ -441,54 +441,12 @@ chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname,
   forwarddecl = externfunc = builtin = false;
   parameters = new chillAST_SymbolTable();
   this->setFunctionCPU();
-  body = new chillAST_CompoundStmt();
   returnType = strdup(rt);
   this->setFunctionCPU();
+  children.push_back(new chillAST_CompoundStmt());
   uniquePtr = unique; // a quick way to check equivalence. DO NOT ACCESS THROUGH THIS
 };
 
-void chillAST_FunctionDecl::setBody(chillAST_Node *bod) {
-  if (bod->isCompoundStmt()) body = (chillAST_CompoundStmt *) bod;
-  else {
-    CHILL_ERROR("Should always be a compound statements");
-    body = new chillAST_CompoundStmt();
-    body->addChild(bod);
-  }
-  bod->setParent(this);  // well, ...
-}
-
-void chillAST_FunctionDecl::insertChild(int i, chillAST_Node *node) {
-  fprintf(stderr, "chillAST_FunctionDecl::insertChild()  ");
-  node->print(0, stderr);
-  fprintf(stderr, "\n\n");
-  body->insertChild(i, node);
-
-  if (node->isVarDecl()) {
-    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)
-      fprintf(stderr, "symbol table is NULL!\n");
-    else
-      fprintf(stderr, "%d entries in the symbol table\n", st->size());
-    fprintf(stderr, "\n\n");
-  }
-}
-
-void chillAST_FunctionDecl::addChild(chillAST_Node *node) {
-  CHILL_DEBUG_BEGIN
-    node->print(0, stderr);
-    fprintf(stderr, "\n\n");
-  CHILL_DEBUG_END
-  if (node->isVarDecl()) {
-    chillAST_VarDecl *vd = ((chillAST_VarDecl *) node);
-    CHILL_DEBUG_PRINT("functiondecl %s adding a VarDecl named %s\n", functionName, vd->varname);
-  }
-
-  body->addChild(node);
-  node->setParent(this); // this, or body??
-}
-
 void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) {
   //fprintf(stderr, "chillAST_FunctionDecl::gatherVarDecls()\n");
   //if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size());
@@ -497,7 +455,7 @@ void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) {
   //fprintf(stderr, "after parms, %d decls\n", decls.size());
   for (int i = 0; i < children.size(); i++) children[i]->gatherVarDecls(decls);
   //fprintf(stderr, "after children, %d decls\n", decls.size());
-  body->gatherVarDecls(decls);  // todo, figure out if functiondecl has actual children
+  getBody()->gatherVarDecls(decls);  // todo, figure out if functiondecl has actual children
   //fprintf(stderr, "after body, %d decls\n", decls.size());
   //for (int d=0; d<decls.size(); d++) {
   //  decls[d]->print(0,stderr); fprintf(stderr, "\n");
@@ -510,7 +468,7 @@ void chillAST_FunctionDecl::gatherScalarVarDecls(vector<chillAST_VarDecl *> &dec
 
   for (int i = 0; i < getParameters()->size(); i++) (*getSymbolTable())[i]->gatherScalarVarDecls(decls);
   for (int i = 0; i < children.size(); i++) children[i]->gatherScalarVarDecls(decls);
-  body->gatherScalarVarDecls(decls);  // todo, figure out if functiondecl has actual children
+  getBody()->gatherScalarVarDecls(decls);  // todo, figure out if functiondecl has actual children
 }
 
 
@@ -519,7 +477,7 @@ void chillAST_FunctionDecl::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decl
 
   for (int i = 0; i < getParameters()->size(); i++) (*getSymbolTable())[i]->gatherArrayVarDecls(decls);
   for (int i = 0; i < children.size(); i++) children[i]->gatherArrayVarDecls(decls);
-  body->gatherArrayVarDecls(decls);  // todo, figure out if functiondecl has actual children
+  getBody()->gatherArrayVarDecls(decls);  // todo, figure out if functiondecl has actual children
 }
 
 
@@ -529,7 +487,7 @@ chillAST_VarDecl *chillAST_FunctionDecl::findArrayDecl(const char *name) {
   //if (p) fprintf(stderr, "function %s has parameter named %s\n", functionName, name );
   if (p && p->isArray()) return p;
 
-  chillAST_VarDecl *v = body->getVariableDeclaration(name);
+  chillAST_VarDecl *v = getBody()->getVariableDeclaration(name);
   //if (v) fprintf(stderr, "function %s has symbol table variable named %s\n", functionName, name );
   if (v && v->isArray()) return v;
 
@@ -548,13 +506,13 @@ chillAST_VarDecl *chillAST_FunctionDecl::findArrayDecl(const char *name) {
 
 void chillAST_FunctionDecl::gatherVarUsage(vector<chillAST_VarDecl *> &decls) {
   for (int i = 0; i < children.size(); i++) children[i]->gatherVarUsage(decls);
-  body->gatherVarUsage(decls);  // todo, figure out if functiondecl has actual children
+  getBody()->gatherVarUsage(decls);  // todo, figure out if functiondecl has actual children
 }
 
 
 void chillAST_FunctionDecl::gatherDeclRefExprs(vector<chillAST_DeclRefExpr *> &refs) {
   for (int i = 0; i < children.size(); i++) children[i]->gatherDeclRefExprs(refs);
-  body->gatherDeclRefExprs(refs);  // todo, figure out if functiondecl has actual children
+  getBody()->gatherDeclRefExprs(refs);  // todo, figure out if functiondecl has actual children
 }
 
 
@@ -626,13 +584,13 @@ void chillAST_FunctionDecl::cleanUpVarDecls() {
 
 
 bool chillAST_FunctionDecl::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync) {
-  if (body) body->findLoopIndexesToReplace(symtab, false);
+  if (getBody()) getBody()->findLoopIndexesToReplace(symtab, false);
   return false;
 }
 
 
 chillAST_Node *chillAST_FunctionDecl::constantFold() {
-  if (body) body = (chillAST_CompoundStmt *) body->constantFold();
+  if (getBody()) setBody((chillAST_CompoundStmt *) getBody()->constantFold());
   return this;
 }
 
diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp
index a88f472..c7abc63 100644
--- a/src/printer/dump.cpp
+++ b/src/printer/dump.cpp
@@ -125,10 +125,10 @@ void Dump::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &
 }
 
 void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
-  print(ident, n->init, o);
-  print(ident, n->cond, o);
-  print(ident, n->incr, o);
-  print(ident, n->body, o);
+  print(ident, n->getInit(), o);
+  print(ident, n->getCond(), o);
+  print(ident, n->getInc(), o);
+  print(ident, n->getBody(), o);
 }
 
 void Dump::printS(std::string ident, chillAST_Free *n, std::ostream &o) {}
-- 
cgit v1.2.3-70-g09d2