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

Sebastian Redl sebastian.redl at getdesigned.at
Sat May 16 11:48:57 PDT 2009


moataz ragab wrote:
> 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.
Be careful. Everyone who's working on template instantiation could be
implementing these as they go along. I'm about to commit cloners for
various literals (FloatingLiteral, CharacterLiteral, StringLiteral,
CXXBooleanLiteralExpr, CXXNullPtrExpr, GNUNullExpr, PredefinedExpr).
>
> 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.
Yes, it should.
> 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.
Always return the type you're cloning. Covariance is a wonderful thing.
> 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.
>
> Stmt *ImplicitCastExpr::Clone(ASTContext &C) const {
>   SourceLocation cpLoc;
>   ImplicitCastExpr *cp= new (C) ImplicitCastExpr(getType(),
> dyn_cast<Expr>(getSubExpr()->Clone(C)), isLvalueCast());
>   return cp;
> }
I see nothing wrong with this. Perhaps if you sent a patch and an
example where we can try it out?

One comment: in the code above, you know that the result of
getSubExpr()->Clone() has to be an Expr. Use cast<> instead of
dyn_cast<> there.

Sebastian



More information about the cfe-dev mailing list