0

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.

How can I do this?

Thanks for any help.

4

1 回答 1

0

将任何子类型Value转换为另一个的正确方法是 via cast<>(),例如:

Value* v = ...
ConstantDataArray* result = cast<ConstantDataArray>(v);

但是,请记住,在您的示例中,您没有返回 type 的对象,但ConstantDataArray您返回的返回类型为可能是 a ,而不是 a )。ConstantDataArray::getString()ConstantDataArrayConstantConstantAggregateZeroConstantDataArray

在任何情况下,如果您不确定它v确实属于特定类型,请先使用isa<>()(或使用idiom dyn_cast<>()检查它,然后再执行cast<>().

于 2013-04-21T09:58:54.797 回答