[cfe-dev] Need Help to Understand LLVM Source Code

Feng Lu via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 1 08:57:02 PDT 2015


Hi, All,
 I am very interested in LLVM, and starting to learn from LLVM source code.
 I encountered one problem that I cannot understand.

 The part of the code is from  llvm/include/llvm/ADT/PointerUnion.h

 class PointerUnion {PointerUnion.h
  public:
    typedef PointerIntPair<void*, 1, bool,
                           PointerUnionUIntTraits<PT1,PT2> > ValTy;
  private:
    ValTy Val;

    struct IsPT1 {
      static const int Num = 0;
    };
    struct IsPT2 {
      static const int Num = 1;
    };
    template <typename T>
    struct UNION_DOESNT_CONTAIN_TYPE { };

  public:
    PointerUnion() {}

    PointerUnion(PT1 V) : Val(
      const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
    }
    PointerUnion(PT2 V) : Val(
      const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)),
1) {
    }

    /// isNull - Return true if the pointer held in the union is null,
    /// regardless of which type it is.
    bool isNull() const {
      // Convert from the void* to one of the pointer types, to make sure
that
      // we recursively strip off low bits if we have a nested PointerUnion.
      return
!PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
    }

What confusing me is the  isNull() function.
Would it be possible to get different result if I use
  return !PointerLikeTypeTraits<PT2>::getFromVoidPointer(Val.getPointer());
instead of
  return !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());

Thanks for your help.
Feng Lu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150901/a0ca9d71/attachment.html>


More information about the cfe-dev mailing list