我从这样的整数常量创建一个 llvm::Value* :
llvm::Value* constValue = llvm::ConstantInt::get( llvmContext , llvm::APInt( node->someInt() ));
现在我想找回编译时常量值;
int constIntValue = constValue->???
LLVM 程序员手册 中显示的示例似乎暗示 cast<> 将在使用类型(而不是类型加指针)模板参数时接受指针,但是我很确定从 2.8 开始就失败了:
llvm::Value* foo = 0;
llvm::ConstantInt* intValue = & llvm::cast< llvm::ConstantInt , llvm::Value >(foo );
//build error:
//error: no matching function for call to ‘cast(llvm::Value*&)’
这里的正确方法是什么?
原文由 lurscher 发布,翻译遵循 CC BY-SA 4.0 许可协议
Given
llvm::Value* foo
and you know thatfoo
is actually aConstantInt
, I believe that the idiomatic LLVM code approach is to usedyn_cast
as follows :如果您完全 确定
foo
是ConstantInt
并且准备好被断言失败(如果不是),您可以使用cast
dyn_cast
。PS 请注意
cast
和dyn_cast
是 LLVM 自己的 RTTI 实现的一部分。dyn_cast
行为有点类似于标准 C++dynamic_cast
,尽管在实现和性能上存在差异(可以在 这里阅读)。