[cfe-commits] r62605 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp lib/Sema/SemaExpr.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Tue Jan 20 13:33:08 PST 2009


Steve Naroff wrote:
> Author: snaroff
> Date: Tue Jan 20 13:53:53 2009
> New Revision: 62605
>
> URL: http://llvm.org/viewvc/llvm-project?rev=62605&view=rev
> Log:
> Allocate expresssions through ASTContext (still more work to do).
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jan 20 13:53:53 2009
> @@ -373,11 +373,14 @@
>  DeclRefExpr *Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc,
>                                      bool TypeDependent, bool ValueDependent,
>                                      const CXXScopeSpec *SS) {
> -  if (SS && !SS->isEmpty())
> -    return new QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent,
> -                                    SS->getRange().getBegin());
> -  else
> -    return new DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent);
> +  if (SS && !SS->isEmpty()) {
> +    void *Mem = Context.getAllocator().Allocate<QualifiedDeclRefExpr>();
> +    return new (Mem) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, 
> +                       ValueDependent, SS->getRange().getBegin());
> +  } else {
> +    void *Mem = Context.getAllocator().Allocate<DeclRefExpr>();
> +    return new (Mem) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent);
> +  }
>  }
>  
>   

Oh, dear. Shouldn't we write Create() functions like decls use? Or are
the few places where Exprs and Stmts are allocated not worth it?

How about a placement new?

void *operator new(size_t bytes, ASTContext& C) {
  return C.getAllocator().Allocate(bytes, some_default_alignment);
}
// This one is not really necessary, as it cannot be called directly. It
is called implicitly
// by the compiler if the constructor in a placement new expression throws.
void operator delete(void *ptr, ASTContext& C) {
  C.getAllocator().Deallocate(ptr);
}

This allows this nice syntax for creating the nodes:

return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, ...);

Sebastian



More information about the cfe-commits mailing list