How is LLVM isa<> implemented?

后端 未结 3 535
野趣味
野趣味 2020-12-04 14:38

From http://llvm.org/docs/CodingStandards.html#ci_rtti_exceptions

LLVM does make extensive use of a hand-rolled form of RTTI that use templates li

3条回答
  •  悲哀的现实
    2020-12-04 15:34

    I should mention that http://llvm.org/docs/ProgrammersManual.html#isa - this document have some additional description.

    The source code of isa, cast and dyn_cast is located in single file, and commented a lot.

    http://llvm.org/doxygen/Casting_8h_source.html

    00047 // isa - Return true if the parameter to the template is an instance of the
    00048 // template type argument.  Used like this:
    00049 //
    00050 //  if (isa(myVal)) { ... }
    00051 //
    00052 template 
    00053 struct isa_impl {
    00054   static inline bool doit(const From &Val) {
    00055     return To::classof(&Val);
    00056   }
    00057 };
    
    00193 // cast - Return the argument parameter cast to the specified type.  This
    00194 // casting operator asserts that the type is correct, so it does not return null
    00195 // on failure.  It does not allow a null argument (use cast_or_null for that).
    00196 // It is typically used like this:
    00197 //
    00198 //  cast(myVal)->getParent()
    00199 //
    00200 template 
    00201 inline typename cast_retty::ret_type cast(const Y &Val) {
    00202   assert(isa(Val) && "cast() argument of incompatible type!");
    00203   return cast_convert_val::SimpleType>::doit(Val);
    00205 }
    
    00218 // dyn_cast - Return the argument parameter cast to the specified type.  This
    00219 // casting operator returns null if the argument is of the wrong type, so it can
    00220 // be used to test for a type as well as cast if successful.  This should be
    00221 // used in the context of an if statement like this:
    00222 //
    00223 //  if (const Instruction *I = dyn_cast(myVal)) { ... }
    00224 //
    00225 
    00226 template 
    00227 inline typename cast_retty::ret_type dyn_cast(const Y &Val) {
    00228   return isa(Val) ? cast(Val) : 0;
    00229 }
    

提交回复
热议问题