I extended the LLVM Kaleidoscope example to support Strings. I added a StringExprAST, which has the virtual Codegen method impl as follows:
Value *StringExprAST::Codegen() {
StringRef r(Val);
return ConstantDataArray::getString(getGlobalContext(), r, false);
}
I am trying to concatenate Strings and have a ConcatExprAST with its Codegen method. Upon trying to access the data in the ConstantDataArray, I need to cast the Value* back to a ConstantDataArray* in order to use the getAsString() method.
I tried:
ConstantDataArray * cda = cast(v);
where v is a Value*. It does not work.
How can I do this?
Thanks for any help.
I extended the LLVM Kaleidoscope example to support Strings. I added a
StringExprAST, which has the virtual Codegen method impl as follows:
Value *StringExprAST::Codegen() {
StringRef r(Val);
return ConstantDataArray::getString(getGlobalContext(), r, false);
}
I am trying to concatenate Strings and have a ConcatExprAST with its
Codegen method. Upon trying to access the data in the ConstantDataArray, I
need to cast the Value* back to a ConstantDataArray* in order to use the
getAsString() method.
I tried:
ConstantDataArray * cda = cast<ConstantDataArray>(v);
where v is a Value*. It does not work.
*How* does it not work? You need to give us more information. Also make
sure that you have read <
http://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates
.
-- Sean Silva
ConstantDataArray * cda = cast(v);
throws this error: Assertion failed: (isa(Val) && “cast() argument of incompatible type!”), function cast, file /Users/rcatlin1/lldb/llvm/include/llvm/Support/Casting.h, line 208
Thanks for the help.
Richard Catlin
This means that V doesn’t point to a ConstantDataArray. Hence you shouldn’t be casting it to one. Use the idiom
if (ConstantDataArray *CDA = dyn_cast(V)) {
// Do something with CDA …
}
to discriminate between types when you aren’t sure about the runtime type of the argument.
– Sean Silva
cast(v) does work. The problem turned out to be my input data. Thanks for your help.