void ObjectMethodExpression::outputCPPImpl(CodeGenerator &cg,
                                           AnalysisResultPtr ar) {

  bool fewParams = canInvokeFewArgs();
  if (!m_name.empty() && m_valid && m_object->getType()->isSpecificObject()) {
    // Static method call
    outputCPPObjectCall(cg, ar);
    cg_printf("%s%s(", m_funcScope ?
              m_funcScope->getPrefix(m_params) : Option::MethodPrefix,
              m_name.c_str());

    FunctionScope::OutputCPPArguments(m_params, m_funcScope, cg, ar,
                                      m_extraArg, m_variableArgument,
                                      m_argArrayId, m_argArrayHash,
                                      m_argArrayIndex);
    cg_printf(")");
  } else {
    cg_printf("(mcp%d.bindClass(info)->", m_ciTemp);
    if (fewParams) {
      cg_printf("getMethFewArgs())(mcp%d, ", m_ciTemp);
      int pcount = m_params ? m_params->getCount() : 0;
      if (pcount) {
        cg_printf("%d, ", pcount);
        ar->pushCallInfo(m_ciTemp);
        FunctionScope::OutputCPPArguments(m_params, m_funcScope, cg, ar, 0,
                                          false);
        ar->popCallInfo();
      } else {
        cg_printf("0");
      }
      FunctionScope::RefParamInfoPtr info;
      if (!m_name.empty()) {
        info = FunctionScope::GetRefParamInfo(m_name);
      }
      for (int i = pcount; i < Option::InvokeFewArgsCount; ++i) {
        if (info && !info->isRefParam(i)) {
          cg_printf(", null_variant");
        } else {
          // It is not safe to use null_variant here, because
          // throw_missing_arguments() might not throw at all.
          cg_printf(", null");
        }
      }
      cg_printf(")");
    } else {
      cg_printf("getMeth())(mcp%d, ", m_ciTemp);
      if (m_params && m_params->getCount()) {
        ar->pushCallInfo(m_ciTemp);
        FunctionScope::OutputCPPArguments(m_params, m_funcScope, cg, ar, -1,
                                          false);
        ar->popCallInfo();
      } else {
        cg_printf("Array()");
      }
      cg_printf(")");
    }
  }
}
void DynamicFunctionCall::outputCPPImpl(CodeGenerator &cg,
                                        AnalysisResultPtr ar) {
    if (m_class || !m_className.empty()) {
        if (m_class || m_validClass || m_redeclared) {
            cg.printf("(cit%d->getMeth())(mcp%d, ", m_ciTemp, m_ciTemp);
            if (m_params && m_params->getCount() > 0) {
                ar->pushCallInfo(m_ciTemp);
                FunctionScope::outputCPPArguments(m_params, cg, ar, -1, false);
                ar->popCallInfo();
            } else {
                cg_printf("Array()");
            }
            cg_printf(")");
            return;
        } else {
            cg_printf("throw_fatal(\"unknown class %s\")", m_className.c_str());
            cg_printf(")");
            return;
        }
    } else {
        //cg.printf("invoke(");
        cg_printf("(cit%d->getFunc())(vt%d, ", m_ciTemp, m_ciTemp);
        if (m_params && m_params->getCount() > 0) {
            ar->pushCallInfo(m_ciTemp);
            FunctionScope::outputCPPArguments(m_params, cg, ar, -1, false);
            ar->popCallInfo();
        } else {
            cg_printf("Array()");
        }
        cg_printf(")");
        return;
    }
    if (m_nameExp->is(Expression::KindOfSimpleVariable)) {
        m_nameExp->outputCPP(cg, ar);
    } else {
        cg_printf("(");
        m_nameExp->outputCPP(cg, ar);
        cg_printf(")");
    }
    cg_printf(", ");
    if (m_params && m_params->getCount() > 0) {
        FunctionScope::outputCPPArguments(m_params, cg, ar, -1, false);
    } else {
        cg_printf("Array()");
    }
    if (m_class) {
        cg_printf(")");
    } else {
        cg_printf(", -1)");
    }
}
bool NewObjectExpression::preOutputCPP(CodeGenerator &cg, AnalysisResultPtr ar,
                                       int state) {
  string &cname = (m_origName == "self" || m_origName == "parent") ?
    m_name : m_origName;
  if (m_name.empty() || m_redeclared || !m_validClass || m_dynamic) {
    // Short circuit out if inExpression() returns false
    if (!ar->inExpression()) return true;

    if (m_nameExp) m_nameExp->preOutputCPP(cg, ar, state);
    ar->wrapExpressionBegin(cg);
    m_ciTemp = cg.createNewId(ar);
    m_objectTemp = cg.createNewId(ar);
    cg_printf("Object obj%d(", m_objectTemp);
    if (m_redeclared) {
      bool outsideClass = !ar->checkClassPresent(m_origName);
      ClassScopePtr cls = ar->resolveClass(m_name);
      ASSERT(cls);
      if (outsideClass) {
        cls->outputVolatileCheckBegin(cg, ar, cname);
      }
      cg_printf("g->%s%s->createOnly()", Option::ClassStaticsObjectPrefix,
                m_name.c_str());
      if (outsideClass) {
        cls->outputVolatileCheckEnd(cg);
      }
    } else {
      cg_printf("create_object_only(");
      if (!cname.empty()) {
        cg_printf("\"%s\"", cname.c_str());
      } else if (m_nameExp->is(Expression::KindOfSimpleVariable)) {
        m_nameExp->outputCPP(cg, ar);
      } else {
        cg_printf("(");
        m_nameExp->outputCPP(cg, ar);
        cg_printf(")");
      }
      cg_printf(")");
    }
    cg_printf(");\n");
    cg_printf("MethodCallPackage mcp%d;\n", m_ciTemp,
        m_objectTemp);
    cg_printf("mcp%d.construct(obj%d);\n", m_ciTemp, m_objectTemp);
    cg_printf("const CallInfo *cit%d = mcp%d.ci;\n", m_ciTemp, m_ciTemp);

    if (m_params && m_params->getCount() > 0) {
      ar->pushCallInfo(m_ciTemp);
      m_params->preOutputCPP(cg, ar, state);
      ar->popCallInfo();
    }
    cg_printf("(cit%d->getMeth())(mcp%d, ", m_ciTemp, m_ciTemp);
    if (m_params && m_params->getOutputCount()) {
      ar->pushCallInfo(m_ciTemp);
      FunctionScope::outputCPPArguments(m_params, cg, ar, -1, false);
      ar->popCallInfo();
    } else {
      cg_printf("Array()");
    }
    cg_printf(");\n");

    if (state & FixOrder) {
      ar->pushCallInfo(m_ciTemp);
      preOutputStash(cg, ar, state);
      ar->popCallInfo();
    }

    if (hasCPPTemp() && !(state & FixOrder)) {
      cg_printf("id(%s);\n", cppTemp().c_str());
    }
    return true;
  } else {
    bool tempRcvr = true;

    bool paramEffect = false;
    if (m_params && m_params->getCount() > 0) {
      for (int i = m_params->getCount(); i--; ) {
        if (!(*m_params)[i]->isScalar()) {
          paramEffect = true;
          break;
        }
      }
    }

    if (!paramEffect) {
      tempRcvr = false;
    }

    if (tempRcvr && ar->inExpression()) {
      bool outsideClass = !ar->checkClassPresent(m_origName);
      ClassScopePtr cls = ar->resolveClass(m_name);
      ASSERT(cls);
      ar->wrapExpressionBegin(cg);
      m_receiverTemp = genCPPTemp(cg, ar);
      cg_printf("%s%s %s = ", Option::SmartPtrPrefix, cls->getId(cg).c_str(),
          m_receiverTemp.c_str());
      if (outsideClass) {
        cls->outputVolatileCheckBegin(cg, ar, cname);
      }
      cg_printf("NEWOBJ(%s%s)()", Option::ClassPrefix, cls->getId(cg).c_str());
      if (outsideClass) {
        cls->outputVolatileCheckEnd(cg);
      }
      cg_printf(";\n");
    }

    bool tempParams = FunctionCall::preOutputCPP(cg, ar, state);
    return tempRcvr || tempParams;
  }
}
bool ObjectMethodExpression::preOutputCPP(CodeGenerator &cg,
    AnalysisResultPtr ar, int state) {
  if (!m_name.empty() && m_valid && m_object->getType()->isSpecificObject()) {
    return FunctionCall::preOutputCPP(cg, ar, state);
  }
  // Short circuit out if inExpression() returns false
  if (!ar->inExpression()) return true;
  m_ciTemp = cg.createNewId(shared_from_this());

  ar->wrapExpressionBegin(cg);
  bool isThis = m_object->isThis();
  if (!isThis) {
    m_object->preOutputCPP(cg, ar, state);
  }
  if (m_name.empty()) {
    m_nameExp->preOutputCPP(cg, ar, state);
  }
  const MethodSlot *ms = NULL;
  if (!m_name.empty()) {
    ConstructPtr self = shared_from_this();
    ms = ar->getOrAddMethodSlot(m_name, self);
  }
  cg_printf("MethodCallPackage mcp%d;\n", m_ciTemp);
  if (ms) {
    cg_printf("mcp%d.methodCallWithIndex((", m_ciTemp);
  } else {
    cg_printf("mcp%d.methodCall((", m_ciTemp);
  }
  if (isThis) {
    cg_printf("GET_THIS()");
  } else {
    outputCPPObject(cg, ar);
  }
  cg_printf("), ");
  if (!m_name.empty()) {
    uint64 hash = hash_string_i(m_name.c_str());
    cg_printString(m_origName, ar, shared_from_this());
    if (ms) {
      cg_printf(", %s", ms->runObjParam().c_str());
    }
    cg_printf(", 0x%016llXLL);\n", hash);
  } else {
    m_nameExp->outputCPP(cg, ar);
    cg_printf(", -1);\n");
  }
  cg_printf("const CallInfo *cit%d  __attribute__((__unused__)) ="
      " mcp%d.ci;\n", m_ciTemp, m_ciTemp);

  if (m_params && m_params->getCount() > 0) {
    ar->pushCallInfo(m_ciTemp);
    m_params->preOutputCPP(cg, ar, state);
    ar->popCallInfo();
  }

  if (state & FixOrder) {
    ar->pushCallInfo(m_ciTemp);
    preOutputStash(cg, ar, state);
    ar->popCallInfo();
  }
  if (hasCPPTemp() && !(state & FixOrder)) {
    cg_printf("id(%s);\n", cppTemp().c_str());
  }
  return true;
}
bool DynamicFunctionCall::preOutputCPP(CodeGenerator &cg, AnalysisResultPtr ar,
                                       int state) {
    bool nonStatic = !m_class && m_className.empty();
    if (!nonStatic && !m_class && !m_validClass && !m_redeclared)
        return FunctionCall::preOutputCPP(cg, ar, state);
    // Short circuit out if inExpression() returns false
    if (!ar->inExpression()) return true;

    if (m_class) {
        m_class->preOutputCPP(cg, ar, state);
    }

    m_nameExp->preOutputCPP(cg, ar, state);

    ar->wrapExpressionBegin(cg);
    m_ciTemp = cg.createNewId(shared_from_this());
    bool lsb = false;
    ClassScopePtr cls;
    if (m_validClass) {
        cls = ar->findClass(m_className);
    }

    if (!m_classScope && !m_className.empty() && m_cppTemp.empty() &&
            m_origClassName != "self" && m_origClassName != "parent" &&
            m_origClassName != "static") {
        // Create a temporary to hold the class name, in case it is not a
        // StaticString.
        m_clsNameTemp = cg.createNewId(shared_from_this());
        cg_printf("CStrRef clsName%d(", m_clsNameTemp);
        cg_printString(m_origClassName, ar, shared_from_this());
        cg_printf(");\n");
    }

    if (nonStatic) {
        cg_printf("const CallInfo *cit%d;\n", m_ciTemp);
        cg_printf("void *vt%d;\n", m_ciTemp);
        cg_printf("get_call_info_or_fail(cit%d, vt%d, ", m_ciTemp, m_ciTemp);
    } else {
        cg_printf("MethodCallPackage mcp%d;\n", m_ciTemp);
        if (m_class) {
            if (m_class->is(KindOfScalarExpression)) {
                ASSERT(strcasecmp(dynamic_pointer_cast<ScalarExpression>(m_class)->
                                  getString().c_str(), "static") == 0);
                cg_printf("mcp%d.staticMethodCall(", m_ciTemp);
                cg_printf("\"static\"");
                lsb = true;
            } else {
                cg_printf("mcp%d.dynamicNamedCall(", m_ciTemp);
                m_class->outputCPP(cg, ar);
            }
            cg_printf(", ");
        } else if (m_validClass) {
            cg_printf("mcp%d.staticMethodCall(\"%s\", ", m_ciTemp,
                      cls->getId(cg).c_str());
        } else {
            cg_printf("mcp%d.staticMethodCall(\"%s\", ", m_ciTemp,
                      m_className.c_str());
        }
    }

    if (m_nameExp->is(Expression::KindOfSimpleVariable)) {
        m_nameExp->outputCPP(cg, ar);
    } else {
        cg_printf("(");
        m_nameExp->outputCPP(cg, ar);
        cg_printf(")");
    }

    if (!nonStatic) {
        cg_printf(");\n");
        if (lsb) cg_printf("mcp%d.lateStaticBind(info);\n");
        cg_printf("const CallInfo *&cit%d = mcp%d.ci;\n", m_ciTemp, m_ciTemp);
        if (m_validClass) {
            cg_printf("%s%s::%sget_call_info(mcp%d",
                      Option::ClassPrefix, cls->getId(cg).c_str(),
                      Option::ObjectStaticPrefix, m_ciTemp, m_className.c_str());
        } else if (m_redeclared) {
            cg_printf("g->%s%s->%sget_call_info(mcp%d",
                      Option::ClassStaticsObjectPrefix, m_className.c_str(),
                      Option::ObjectStaticPrefix, m_ciTemp, m_className.c_str());
        }
    }
    if (nonStatic || !m_class) {
        cg_printf(");\n");
    }
    if (m_params && m_params->getCount() > 0) {
        ar->pushCallInfo(m_ciTemp);
        m_params->preOutputCPP(cg, ar, state);
        ar->popCallInfo();
    }

    ar->pushCallInfo(m_ciTemp);
    preOutputStash(cg, ar, state);
    ar->popCallInfo();
    if (!(state & FixOrder)) {
        cg_printf("id(%s);\n", cppTemp().c_str());
    }
    return true;
}