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

Nikola Smiljanic via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 1 14:20:01 PDT 2015


You can't get a different result because it's not really important what
type you're getting. The type isn't exposed in the interface of this
method. It's just checking that pointer is null. The type doesn't matter
which is why the comment also says "one of the pointer types".

On Wed, Sep 2, 2015 at 1:57 AM, Feng Lu via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> 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
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150902/e9dcf4af/attachment.html>


More information about the cfe-dev mailing list