destroy() information?

Chris,

Chris Lattner wrote:

Eventually we want to use Decl->destroy(ASTContext&) instead of
calling delete directly. This was the second half of the
"Creatification" patch that I haven't gotten to yet.

I'd like to implement this, if wouldn't mind giving me some pointers on
how you'd like it done.

Thanks,
Sam Bishop

Cool! I was thinking of something sleazy like a non-virtual, const Destroy method in Decl. Something like this:

void Decl::Destroy(ASTContext &C) const {
   switch (getKind()) {
   case FieldDecl: cast<FieldDecl>(this)->~FieldDecl(); break;
   ... one case for each concrete Decl class ...
   }

   C.getAllocator().Delete(this);
}

and then make the Decl destructors private or protected, so they can't be directly called by clients.

What do you think? Incidentally, things like this are very mechanical: maybe we should have a DeclNodes.def file like StmtNodes.def to automate it?

-Chris

Chris,

Chris Lattner wrote:

Cool! I was thinking of something sleazy like a non-virtual, const
Destroy method in Decl. Something like this:

void Decl::Destroy(ASTContext &C) const {
   switch (getKind()) {
   case FieldDecl: cast<FieldDecl>(this)->~FieldDecl(); break;
   ... one case for each concrete Decl class ...
   }

   C.getAllocator().Delete(this);
}

I've got one, silly question... Where should this code go? It needs to
have the declarations for all concrete classes in scope.

Incidentally, things like this are very
mechanical: maybe we should have a DeclNodes.def file like
StmtNodes.def to automate it?

I'm leaning toward just a simple macro. I looked for other uses for a
.def file, and all I could come up with was the Decl::Kind enum. But
after trying to create the *First/*Last members using the C
preprocessor, I decided it wasn't worth it.

Thanks,
Sam

Chris,

Chris Lattner wrote:

Cool! I was thinking of something sleazy like a non-virtual, const
Destroy method in Decl. Something like this:

void Decl::Destroy(ASTContext &C) const {
  switch (getKind()) {
  case FieldDecl: cast<FieldDecl>(this)->~FieldDecl(); break;
  ... one case for each concrete Decl class ...
  }

  C.getAllocator().Delete(this);
}

I've got one, silly question... Where should this code go? It needs to
have the declarations for all concrete classes in scope.

Probably just Decl.cpp, you can #include all the language-specific decl.h files there.

Incidentally, things like this are very
mechanical: maybe we should have a DeclNodes.def file like
StmtNodes.def to automate it?

I'm leaning toward just a simple macro.

Ok!

-Chris