static void parse_string_arg(ExpressionPtr exp, string &var, string &lit) {
  if (exp->is(Expression::KindOfUnaryOpExpression)) {
    UnaryOpExpressionPtr u(static_pointer_cast<UnaryOpExpression>(exp));
    if (u->getOp() == '(') {
      parse_string_arg(u->getExpression(), var, lit);
      return;
    }
  } else if (exp->is(Expression::KindOfBinaryOpExpression)) {
    BinaryOpExpressionPtr b(static_pointer_cast<BinaryOpExpression>(exp));
    if (b->getOp() == '.') {
      string v, l;
      parse_string_arg(b->getExp2(), v, l);
      if (v.empty()) {
        parse_string_arg(b->getExp1(), var, lit);
        lit += l;
        return;
      }
    }
  }
  if (exp->isLiteralString()) {
    var = "";
    lit = exp->getLiteralString();
    return;
  }
  var = exp->getText();
  lit = "";
  return;
}
bool
ExpressionList::flattenLiteralStrings(vector<ExpressionPtr> &literals) const {
  for (unsigned i = 0; i < m_exps.size(); i++) {
    ExpressionPtr e = m_exps[i];
    if (e->is(Expression::KindOfArrayPairExpression)) {
      ArrayPairExpressionPtr ap = dynamic_pointer_cast<ArrayPairExpression>(e);
      if (ap->getName()) return false;
      e = ap->getValue();
    }
    if (e->is(Expression::KindOfUnaryOpExpression)) {
      UnaryOpExpressionPtr unary = dynamic_pointer_cast<UnaryOpExpression>(e);
      if (unary->getOp() == T_ARRAY) {
        ExpressionListPtr el =
          dynamic_pointer_cast<ExpressionList>(unary->getExpression());
        if (!el->flattenLiteralStrings(literals)) {
          return false;
        }
      }
    }
    else if (e->isLiteralString()) {
      literals.push_back(e);
    } else {
      return false;
    }
  }
  return true;
}
示例#3
0
void QOpExpression::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) {
  if (!m_cppValue.empty()) {
    cg_printf("%s", m_cppValue.c_str());
  } else {
    ExpressionPtr expYes = m_expYes ? m_expYes : m_condition;
    bool wrapped = !isUnused();
    if (wrapped) {
      cg_printf("(");
    }
    wrapBoolean(cg, ar, m_condition);
    if (isUnused()) {
      cg_printf(" ? ");
      outputUnneededExpr(cg, ar, expYes);
      cg_printf(" : ");
      outputUnneededExpr(cg, ar, m_expNo);
    } else {
      TypePtr typeYes = expYes->getActualType();
      TypePtr typeNo = m_expNo->getActualType();
      const char *castType =
        typeYes && typeNo && Type::SameType(typeYes, typeNo) &&
        !typeYes->is(Type::KindOfVariant) &&
        expYes->isLiteralString() == m_expNo->isLiteralString()
        ? "" : "(Variant)";

      cg_printf(" ? (%s(", castType);
      expYes->outputCPP(cg, ar);
      cg_printf(")) : (%s(", castType);
      m_expNo->outputCPP(cg, ar);
      cg_printf("))");
    }
    if (wrapped) {
      cg_printf(")");
    }
  }
}
int BinaryOpExpression::getConcatList(ExpressionPtrVec &ev, ExpressionPtr exp,
                                      bool &hasVoid, bool &hasLitStr) {
  if (!exp->hasCPPTemp()) {
    if (exp->is(Expression::KindOfUnaryOpExpression)) {
      UnaryOpExpressionPtr u = static_pointer_cast<UnaryOpExpression>(exp);
      if (u->getOp() == '(') {
        return getConcatList(ev, u->getExpression(), hasVoid, hasLitStr);
      }
    } else if (exp->is(Expression::KindOfBinaryOpExpression)) {
      BinaryOpExpressionPtr b = static_pointer_cast<BinaryOpExpression>(exp);
      if (b->getOp() == '.') {
        return getConcatList(ev, b->getExp1(), hasVoid, hasLitStr) +
          getConcatList(ev, b->getExp2(), hasVoid, hasLitStr);
      }
    } else if (exp->is(Expression::KindOfEncapsListExpression)) {
      EncapsListExpressionPtr e =
        static_pointer_cast<EncapsListExpression>(exp);
      if (e->getType() != '`') {
        ExpressionListPtr el = e->getExpressions();
        int num = 0;
        for (int i = 0, s = el->getCount(); i < s; i++) {
          ExpressionPtr exp = (*el)[i];
          num += getConcatList(ev, exp, hasVoid, hasLitStr);
        }
        return num;
      }
    }
  }

  ev.push_back(exp);
  bool isVoid = !exp->getActualType();
  hasVoid |= isVoid;
  hasLitStr |= exp->isLiteralString();
  return isVoid ? 0 : 1;
}
static void outputStringExpr(CodeGenerator &cg, AnalysisResultPtr ar,
                             ExpressionPtr exp, bool asLitStr) {
  if (asLitStr && exp->isLiteralString()) {
    const std::string &s = exp->getLiteralString();
    std::string enc = string_cplus_escape(s.c_str(), s.size());
    cg_printf("\"%s\", %d", enc.c_str(), (int)s.size());
    return;
  }

  TypePtr et(exp->getExpectedType());
  exp->setExpectedType(Type::String);
  exp->outputCPP(cg, ar);
  exp->setExpectedType(et);
}
static void outputStringExpr(CodeGenerator &cg, AnalysisResultPtr ar,
                             ExpressionPtr exp, bool asLitStr) {
  if (asLitStr && exp->isLiteralString()) {
    const std::string &s = exp->getLiteralString();
    char *enc = string_cplus_escape(s.c_str(), s.size());
    cg_printf("\"%s\", %d", enc, s.size());
    free(enc);
    return;
  }

  bool close = false;
  if ((exp->hasContext(Expression::LValue) &&
       (!exp->getActualType()->is(Type::KindOfString) ||
        (exp->getImplementedType() &&
         !exp->getImplementedType()->is(Type::KindOfString))))
      ||
      !exp->getType()->is(Type::KindOfString)) {
    cg_printf("toString(");
    close = true;
  }
  exp->outputCPP(cg, ar);
  if (close) cg_printf(")");
}