0

I'm writing a condition to see if a const llvm::Value* is a constant. The code is as following:

if(const Constant* c = dynamic_cast<Constant>(val)){
    ......
}

"val" here is a const llvm::Value*, however, the compiler says

error: 'llvm::Constant' is not a reference or pointer
if(const Constant* c = dynamic_cast<Constant>(val)){
                       ^           ~~~~~~~~~~

How can I modify it?

Crystal
  • 85
  • 9

2 Answers2

1

If you don't need to use the constant inside of the if block then you can use isa<>()

if(isa<Constant>(val)){ ...... }
nicolas-mosch
  • 469
  • 4
  • 10
0

The type used in dynamic_cast must be a pointer or reference type. I'm guessing you'd want to keep the constness as well.

Try this:

dynamic_cast<const Constant*>(val)
//           ^^^^^         ^
kmdreko
  • 42,554
  • 6
  • 57
  • 106
  • Still not work, but dyn_cast(val) can compile, I haven't check the correctness of it. – Crystal Feb 06 '18 at 05:14
  • apologies, I didn't realize you were needing the llvm `dyn_cast` function [docs](http://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates) – kmdreko Feb 06 '18 at 05:33
  • 1
    looks like directly swapping to `dyn_cast(val)` is exactly what you want – kmdreko Feb 06 '18 at 05:34