Skip to content

Commit

Permalink
(Rosebud) Moved destructorHelper from SgAsmNode to SgNode
Browse files Browse the repository at this point in the history
* This function will be needed by more than just binary analysis once
  we start using Rosebud to generate other AST node types.

RPM-357
  • Loading branch information
matzke1 committed Apr 21, 2023
1 parent 06f504d commit e2b28fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
6 changes: 0 additions & 6 deletions src/AstNodes/BinaryAnalysis/SgAsmNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@ class SgAsmNode: public SgNode {
* instead of the more streamlined @c node->setAttribute(....). */
[[using Rosebud: large, mutators(), no_serialize]]
Sawyer::Attribute::Storage<> attributes;

protected:
// Rosebud generates call to destructorHelper in every destructor implementation. This gives the user a chance to do something
// special. Any implementations should be prepared to handle multiple invocations on the same object, and should not access any
// data members in any derived class. Standard C++ rules about calling virtual functions in destructors apply here.
virtual void destructorHelper() {}
};
32 changes: 32 additions & 0 deletions src/ROSETTA/Grammar/Node.code
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,38 @@ private:
void debugSerializationHelper(const char*, bool) {}
#endif

protected:
/** This gets called by all Rosebud-generated destructors.
*
* Rosebud generates a call to @p destructorHelper from every destructor implementation. This gives the user a chance to
* do something special. There are some things to be aware of when reimplementing this function, and you should be well
* versed in the C++ rules about calling virtual functions from destructors.
*
* @li The implementation must not attempt to access any data members of derived classes. These data members have already
* been destroyed by time this function is called.
*
* @li Any calls you make to virtual functions declared in this class will dispatch only to implementations in this class
* or its base classes, not any derived classes.
*
* @li Your implementation must expect to be called multiple times for the same object. This occurs when this class implements
* this function but its derived class does not. During destruction of a derived object, the derived class's Rosebud-generated
* destructor will call @p destructorHelper, which will dispatch to the implementation in this class. Then, when the part of
* the object corresponding to this class is being destroyed, the destructor will call @p destructorHelper again, which will
* dispatch to this implementation again. This may happen recursively mutliple times.
*
* @li Your implementation should not destroy anything that a subclass might need during its destruction. This follows from the
* rule above, but is generally not a problem in practice. The reason it's not a problem is that the derived classes are
* usually Rosebud-generated and thus their destructors only call @p destructorHelper. Therefore, in order for any derived
* class to any work, it must be done in its @p destructorHelper, which would be called before the implementation of @p
* destructorHelper in this class is called.
*
* All attempts should be made to avoid having to implement a @p destructorHelper in any class but the most base class (and
* that one does nothing). Instead, use data members whose own destructors do their cleanup. For instance, if this class needs
* a pointer to data allocated on the stack, then instead of using a raw pointer which needs to be deleted in this class's
* destructor, use a smart pointer whose own destructor deletes the data. If you follow this advice, then you don't need to
* be well-versed in the details of C++ object destruction. */
virtual void destructorHelper() {}

protected:
/** Called by generated serializers.
*
Expand Down
6 changes: 1 addition & 5 deletions src/ROSETTA/src/binaryInstruction.C
Original file line number Diff line number Diff line change
Expand Up @@ -39191,11 +39191,7 @@ public:
Sawyer::Attribute::Storage<> const& attributes() const;
Sawyer::Attribute::Storage<>& attributes();
/** @} */
protected:
// Rosebud generates call to destructorHelper in every destructor implementation. This gives the user a chance to do something
// special. Any implementations should be prepared to handle multiple invocations on the same object, and should not access any
// data members in any derived class. Standard C++ rules about calling virtual functions in destructors apply here.
virtual void destructorHelper() {}

public:
/** Destructor. */
virtual ~SgAsmNode();
Expand Down

0 comments on commit e2b28fb

Please sign in to comment.