[ADT] Add an abstraction for embedding an integer within a pointer-like
authorChandler Carruth <chandlerc@gmail.com>
Sun, 10 Jan 2016 09:40:13 +0000 (09:40 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sun, 10 Jan 2016 09:40:13 +0000 (09:40 +0000)
commitca4af1ae34078c2b4b734e44a7d6bb6add27efed
tree367251946bd8f205502fbcc4508939e2d3084d67
parente496b80d0320b6cae6d9591231fd5bc38a8ff733
[ADT] Add an abstraction for embedding an integer within a pointer-like
type.

This makes it easy and safe to use a set of flags as one elmenet of
a tagged union with pointers. There is quite a bit of code that has
historically done this by casting arbitrary integers to "pointers" and
assuming that this was safe and reliable. It is neither, and has started
to rear its head by triggering safety asserts in various abstractions
like PointerLikeTypeTraits when the integers chosen are invariably poor
choices for *some* platform and *some* situation. Not to mention the
(hopefully unlikely) prospect of one of these integers actually getting
allocated!

With this, it will be straightforward to build type safe abstractions
like this without being error prone. The abstraction itself is also
remarkably simple thanks to the implicit conversion.

This use case and pattern was also independently created by the folks
working on Swift, and they're going to incrementally add any missing
functionality they find.

Differential Revision: http://reviews.llvm.org/D15844

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257284 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/ADT/PointerEmbeddedInt.h [new file with mode: 0644]
unittests/ADT/CMakeLists.txt
unittests/ADT/PointerEmbeddedIntTest.cpp [new file with mode: 0644]