Advertisement
Guest User

Get the ObjCIvarDecl

a guest
Mar 16th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. clang::ObjCIvarDecl *getIvarDeclOrNull(clang::ObjCPropertyRefExpr *prop) {
  2.   // the simple case, it is an explicitproperty and that points us to the ivardecl
  3.   if(prop->getExplicitProperty() && prop->getExplicitProperty()->getPropertyIvarDecl())
  4.     return prop->getExplicitProperty()->getPropertyIvarDecl();
  5.  
  6.   // the more complicated case...
  7.   // find the corresponding ValueDecl to the Base of the ObjCPropertyRefExpr
  8.   // when the ValueDecls type is a ObjCInterfaceDecl and clang knows the interface-implementation
  9.   // we iterate over all property-implementations and compare their names with the name of the
  10.   // getter-selector
  11.   if(clang::ImplicitCastExpr *base= dyn_cast_or_null<clang::ImplicitCastExpr>(prop->getBase())) {
  12.     if(clang::DeclRefExpr *d= dyn_cast_or_null<clang::DeclRefExpr>(base->getSubExpr())) {
  13.       clang::ValueDecl *val= d->getDecl();
  14.       if(val->getType()->isObjCObjectPointerType()) {
  15.         clang::ObjCInterfaceDecl *iface= val->getType()->getAsObjCInterfacePointerType()->getInterfaceDecl();
  16.         if(iface && iface->getImplementation()) {
  17.           clang::ObjCImplementationDecl *impl= iface->getImplementation();
  18.           for(clang::ObjCImplementationDecl::propimpl_iterator i= impl->propimpl_begin(); i!= impl->propimpl_end(); ++i) {
  19.             // does the property know which ivar it belongs to and does the ivar has the same name as the requested property?
  20.             if((*i)->getPropertyIvarDecl() && (*i)->getPropertyIVarDecl()->getNameAsString()== prop->getGetterSelector().getAsString())
  21.               return (*i)->getPropertyIvarDecl(); // match of the ivar to the corresponding property-getter
  22.           }
  23.         }
  24.       }
  25.     }
  26.   }
  27.   return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement