[cfe-dev] Adding support for Cloning in the AST

moataz ragab mtzrgb at gmail.com
Sat May 16 11:31:55 PDT 2009


Hi,

I am implementing the clone method and would like to contribute the
implementation. Since this my first contribution, I would check if anybody
have technical issues to highlight.

I also would like to check on some issues:
1.  I started out by making it virtual and provided default impl. in the
base classes that just print the object class name. This is to allow me to
test my clone methods before implementing it in all the AST classes. but
finally it should be pure virtual.
2.  Should the clone return base class pointers or derived class pointers?
For example currently, there is clone method implemented for literals in
Expr.cpp. and it returns a pointer to the specifc literal type.
3. I have implemented the clone method for different Stmts and Exprs and it
seems working fine with exception of ImplicitCastExpr. Whenever I call clone
on objects of these class I get the base class (Stmt) clone method invoked.
I would appericiate if you could advise on this.

Code:
In the class declaration I add Stmt *Clone(ASTContext &C) const;

For the implementation:

Stmt *BinaryOperator::Clone(ASTContext &C) const {
  SourceLocation cpLoc;
  Expr* lhs =dyn_cast<Expr>(SubExprs[LHS]->Clone(C));
  Expr* rhs =dyn_cast<Expr>(SubExprs[RHS]->Clone(C));
  BinaryOperator *cp= new (C) BinaryOperator(lhs, rhs , Opc, getType(),
cpLoc);
  return cp;
}

Stmt *ArraySubscriptExpr::Clone(ASTContext &C) const {
  SourceLocation cpLoc;
  Expr* lhs =dyn_cast<Expr>(SubExprs[LHS]->Clone(C));
  Expr* rhs =dyn_cast<Expr>(SubExprs[RHS]->Clone(C));
  ArraySubscriptExpr *cp= new (C) ArraySubscriptExpr(lhs,rhs, getType(),
cpLoc);
  return cp;
}

Stmt *ImplicitCastExpr::Clone(ASTContext &C) const {
  SourceLocation cpLoc;
  ImplicitCastExpr *cp= new (C) ImplicitCastExpr(getType(),
dyn_cast<Expr>(getSubExpr()->Clone(C)), isLvalueCast());
  return cp;
}


P.S: I have read the llvm developer policy.

Thanks,
Moataz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090516/2eaf3ccc/attachment.html>


More information about the cfe-dev mailing list