From be9cd842ae3287dad1dd06bfc37e6763fb6c61c1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 29 Mar 2009 07:03:30 +0000 Subject: [PATCH] add some comments, add a dyn_cast method. llvm-svn: 67992 --- include/llvm/ADT/PointerUnion.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/llvm/ADT/PointerUnion.h b/include/llvm/ADT/PointerUnion.h index 7a4de656566..01272b80e2b 100644 --- a/include/llvm/ADT/PointerUnion.h +++ b/include/llvm/ADT/PointerUnion.h @@ -78,18 +78,34 @@ namespace llvm { Val.setInt(1); } + /// isNull - Return true if the pointer help in the union is null, + /// regardless of which type it is. bool isNull() const { return Val.getPointer() == 0; } + /// is() return true if the Union currently holds the type matching T. template int is() const { return Val.getInt() == ::llvm::getPointerUnionTypeNum((T*)0); } + + /// get() - Return the value of the specified pointer type. If the + /// specified pointer type is incorrect, assert. template T get() const { assert(is() && "Invalid accessor called"); return static_cast(Val.getPointer()); } + /// dyn_cast() - If the current value is of the specified pointer type, + /// return it, otherwise return null. + template + T dyn_cast() const { + if (is()) return static_cast(Val.getPointer()); + return T(); + } + + /// Assignment operators - Allow assigning into this union from either + /// pointer type, setting the discriminator to remember what it came from. const PointerUnion &operator=(const PT1 &RHS) { Val.setPointer(RHS); Val.setInt(0);