/// printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0"
/// X = P.get<int*>(); // ok.
/// Y = P.get<float*>(); // runtime assertion failure.
- /// Z = P.get<double*>(); // does not compile.
+ /// Z = P.get<double*>(); // runtime assertion failure (regardless of tag)
/// P = (float*)0;
/// Y = P.get<float*>(); // ok.
/// X = P.get<int*>(); // runtime assertion failure.
PointerUnion() {}
PointerUnion(PT1 V) {
- Val.setPointer(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V));
+ Val.setPointer(
+ const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V)));
Val.setInt(0);
}
PointerUnion(PT2 V) {
- Val.setPointer(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V));
+ Val.setPointer(
+ const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)));
Val.setInt(1);
}
int is() const {
int TyNo = ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
assert(TyNo != -1 && "Type query could never succeed on PointerUnion!");
- return Val.getInt() == TyNo;
+ return static_cast<int>(Val.getInt()) == TyNo;
}
/// get<T>() - Return the value of the specified pointer type. If the
/// 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(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS));
+ Val.setPointer(
+ const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
Val.setInt(0);
return *this;
}
const PointerUnion &operator=(const PT2 &RHS) {
- Val.setPointer(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS));
+ Val.setPointer(
+ const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)));
Val.setInt(1);
return *this;
}
int is() const {
// Is it PT1/PT2?
if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
- return Val.get<InnerUnion>().is<T>();
- // Must be PT3 or statically invalid.
- assert(Val.is<T>());
- return true;
+ return Val.is<InnerUnion>() && Val.get<InnerUnion>().is<T>();
+ return Val.is<T>();
}
/// get<T>() - Return the value of the specified pointer type. If the
template<typename T>
T get() const {
assert(is<T>() && "Invalid accessor called");
- if (Val.is<T>())
- return Val.get<T>();
- return Val.get<InnerUnion>().get<T>();
+ // Is it PT1/PT2?
+ if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
+ return Val.get<InnerUnion>().get<T>();
+
+ return Val.get<T>();
}
/// dyn_cast<T>() - If the current value is of the specified pointer type,