[PATCH] D48807: Add llvm::Any

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 3 10:43:21 PDT 2018


zturner added a comment.

In https://reviews.llvm.org/D48807#1151012, @zturner wrote:

> In https://reviews.llvm.org/D48807#1151011, @timshen wrote:
>
> > Aren't virtual functions exactly for homogeneous operations over sub-classes?
> >
> >   unique_ptr<EventBase> Event = waitForEvent();
> >   const EXCEPTION_RECORD *E;
> >   switch (Event->Type) {
> >   case EventBase::Breakpoint:
> >   case EventBase::ArithmeticError:
> >   case EventBase::IllegalInstruction:
> >   case EventBase::InvalidMemoryReference:
> >     E = Event->getPlatformPiece();  // getPlatformPiece() is a virtual function in EventBase.
> >     break;
> >   }
> >
>
>
> And what type does it return?


To elaborate, `EXCEPTION_RECORD` here is a platform specific type.  It will fail to compile on any platform except Windows.  So we can't expose that through a platform-agnostic interface.  We could wrap it in a base class whose sole purpose is to hide this, such as:

  struct PlatformPieceBase {
    virtual ~PlatformPieceBase() {}
  };
  
  struct Win32ExceptionRecordPlatformPiece : public PlatformPiece {
    EXCEPTION_RECORD E;
  };

and then re-write the line in the switch as:

  E = llvm::cast<Win32ExceptionRecordPlatformPiece>(Event->getPlatformPiece())->E;

But now you need to build an entire inheritance hierarchy just to wrap one structure which you already have an instance of from the OS.  It seems like a lot of trouble to avoid using `Any`, and the "building an inheritance hierarchy to wrap..." is ultimately exactly what the implementation of `Any` does anyway.


https://reviews.llvm.org/D48807





More information about the llvm-commits mailing list