ValueDecl *valueDeclFromIncExpr(Expr *incExpr)
 {
     UnaryOperator *unaryOperator = dyn_cast_or_null<UnaryOperator>(incExpr);
     if (unaryOperator)
     {
         Expr *unaryOpSubExpr = unaryOperator->getSubExpr();
         if (unaryOpSubExpr && isa<DeclRefExpr>(unaryOpSubExpr))
         {
             DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(unaryOpSubExpr);
             return declRefExpr->getDecl();
         }
     }
     return nullptr;
 }
  string NetworkDriverRewriteVisitor::GetSharedStructStr(CallExpr *callExpr)
  {
    string shared_struct_str = "";

    Expr *callee = callExpr->getCallee();
    if (!isa<ImplicitCastExpr>(callee))
      return shared_struct_str;

    ImplicitCastExpr *calleeImplExpr = cast<ImplicitCastExpr>(callee);
    if (!isa<DeclRefExpr>(calleeImplExpr->getSubExpr()))
      return shared_struct_str;

    DeclRefExpr *calleeDeclExpr = cast<DeclRefExpr>(calleeImplExpr->getSubExpr());
    Stmt *body = callExpr->getCalleeDecl()->getBody();

    if (calleeDeclExpr->getNameInfo().getAsString() != "alloc_etherdev")
      shared_struct_str = GetSharedStructStrInFunctionBody(body, false);
    if (calleeDeclExpr->getNameInfo().getAsString() != "alloc_etherdev")
      return shared_struct_str;

    for (auto i = callExpr->arg_begin(), e = callExpr->arg_end(); i != e; ++i)
    {
      if (!isa<ImplicitCastExpr>(*i))
        continue;

      ImplicitCastExpr *argImplExpr = cast<ImplicitCastExpr>(*i);
      if (!isa<UnaryExprOrTypeTraitExpr>(argImplExpr->getSubExpr()))
        continue;

      UnaryExprOrTypeTraitExpr *argExpr = cast<UnaryExprOrTypeTraitExpr>(argImplExpr->getSubExpr());
      ParenExpr *parenExpr = cast<ParenExpr>(argExpr->getArgumentExpr());
      UnaryOperator *uop = cast<UnaryOperator>(parenExpr->getSubExpr());
      ImplicitCastExpr *implExpr = cast<ImplicitCastExpr>(uop->getSubExpr());
      DeclRefExpr *declExpr = cast<DeclRefExpr>(implExpr->getSubExpr());
      ValueDecl *valueDecl = cast<ValueDecl>(declExpr->getDecl());

      shared_struct_str = valueDecl->getType().getAsString(Context->getPrintingPolicy());
      break;
    }

    return shared_struct_str;
  }
Beispiel #3
0
//--------------------------------------------------------- 
VarDecl* isFortranLoop(ForStmt* Node, const char*& errMsg)
{
  // cond must be of form "x >/</!=/ expr":
  BinaryOperator* condCmp = dyn_cast_or_null<BinaryOperator>(Node->getCond());
  if (condCmp == 0 || 
      ((!condCmp->isRelationalOp()) && condCmp->getOpcode() != BO_NE))
  {
    errMsg = "for-cond not fortran-like (must be x rel expr)";  
    return 0;
  }
  DeclRefExpr* condVar = dyn_cast<DeclRefExpr>(stripParenCasts(condCmp->getLHS()));
  if (condVar == 0 || !condVar->getType()->isIntegerType())
  {
    errMsg = "no integer for-init variable";  
    return 0;
  }
  VarDecl* VD = dyn_cast<VarDecl>(condVar->getDecl());
  if (VD == 0)
  {
    errMsg = "strange unrecognized lhs in for-condition";  
    return 0;
  }

  // inc must be of form "++x/x++":
  UnaryOperator* incStmt = dyn_cast_or_null<UnaryOperator>(Node->getInc());
  if (incStmt == 0 || (!incStmt->isIncrementOp()))
  {
    errMsg = "for-inc not fortran-like (must be ++x/x++)";  
    return 0;
  }
  DeclRefExpr* incVar = dyn_cast<DeclRefExpr>(incStmt->getSubExpr());
  if (incVar == 0 || incVar->getDecl() != VD)
  {
    errMsg = "for-inc doesn't refer to for-cond variable";
    return 0;
  }
  return VD;
}