char32_t value from APSInt


Is there a way to get a char32_t value from a variable of type llvm::APSInt?

I tried something like -

const char32_t Ch = Val.getZExtValue();

(where Val is of type llvm::APSInt) but this implicit casting doesn’t seem to work as it stores the same value as of type char.

I tried playing around with but couldn’t find a way to make this work.


Well, an llvm::APSInt represents an arbitrary-width signed or unsigned integer, and a char32_t (generally) represents a UCS-4 code unit / Unicode code point. How do you want to interpret the APSInt, such that it can be converted to a Unicode code point? If the APSInt represents the Unicode code point value, then Val.getLimitedValue() is probably reasonable, but you’ll also need to check for Val.isNegative() to avoid the possibility of a narrow negative APSInt getting confused for a small positive integer. (Generally you should also beware of getZExtValue() because it has a precondition that the value fits in 64 bits; it’s also questionable to call getZExtValue on an APSInt because it assumes the APSInt represents an unsigned integer.)