コード例 #1
0
ファイル: file_scope.cpp プロジェクト: enov/hhvm
FunctionScopePtr FileScope::createPseudoMain(AnalysisResultConstPtr ar) {
  StatementListPtr st = m_tree;
  auto labelScope = std::make_shared<LabelScope>();
  auto f =
    std::make_shared<FunctionStatement>(
      BlockScopePtr(),
      labelScope,
      Location::Range(),
      ModifierExpressionPtr(),
      false, pseudoMainName(),
      ExpressionListPtr(), TypeAnnotationPtr(),
      st, 0, "", ExpressionListPtr());
  f->setFileLevel();
  auto pseudoMain =
    std::make_shared<HPHP::FunctionScope>(
      ar, true,
      pseudoMainName().c_str(),
      f, false, 0, 0,
      ModifierExpressionPtr(),
      m_attributes[0], "",
      shared_from_this(),
      vector<UserAttributePtr>(),
      true);
  f->setBlockScope(pseudoMain);
  auto& fs = m_functions[pseudoMainName()];
  always_assert(!fs);
  fs = pseudoMain;
  m_pseudoMain = pseudoMain;
  return pseudoMain;
}
コード例 #2
0
ファイル: closure_expression.cpp プロジェクト: c9s/hhvm
void ClosureExpression::initializeFromUseList(ExpressionListPtr vars) {
  m_vars = ExpressionListPtr(
    new ExpressionList(vars->getScope(), vars->getRange()));

  // Because PHP is insane you can have a use variable with the same
  // name as a param name.
  // In that case, params win (which is different than zend but much easier)
  auto seenBefore = collectParamNames();

  for (int i = vars->getCount() - 1; i >= 0; i--) {
    auto param = dynamic_pointer_cast<ParameterExpression>((*vars)[i]);
    assert(param);
    if (param->getName() == "this") {
      // "this" is automatically included.
      // Once we get rid of all the callsites, make this an error
      continue;
    }
    if (seenBefore.find(param->getName().c_str()) == seenBefore.end()) {
      seenBefore.insert(param->getName().c_str());
      m_vars->insertElement(param);
    }
  }

  initializeValuesFromVars();
}
コード例 #3
0
void ClosureExpression::setCaptureList(
    AnalysisResultConstRawPtr ar,
    const std::set<std::string>& captureNames) {
  assert(m_captureState == CaptureState::Unknown);
  m_captureState = CaptureState::Known;

  bool usedThis = false;
  SCOPE_EXIT {
    /*
     * TODO: closures in a non-class scope should be neither static
     * nor non-static, but right now we don't really have this idea.
     *
     * This would allow not having to check for a $this or late bound
     * class in the closure object or on the ActRec when returning
     * from those closures.
     *
     * (We could also mark closures that don't use late static binding
     * with this flag to avoid checks on closures in member functions
     * when they use neither $this nor static::)
     */
    if (!usedThis && !m_func->getModifiers()->isStatic()) {
      m_func->getModifiers()->add(T_STATIC);
    }
  };

  if (captureNames.empty()) return;

  m_vars = ExpressionListPtr(
    new ExpressionList(getScope(), getRange()));

  for (auto const& name : captureNames) {
    if (name == "this") {
      usedThis = true;
      continue;
    }

    auto expr = std::make_shared<ParameterExpression>(
      BlockScopePtr(getScope()),
      getRange(),
      TypeAnnotationPtr(),
      true /* hhType */,
      name,
      ParamMode::In,
      0 /* token modifier thing */,
      ExpressionPtr(),
      ExpressionPtr()
    );
    m_vars->insertElement(expr);
  }

  initializeValuesFromVars();
  analyzeVarsForClosure(ar);
  analyzeVarsForClosureExpression(ar);
}
コード例 #4
0
ファイル: closure_expression.cpp プロジェクト: c9s/hhvm
void ClosureExpression::initializeValuesFromVars() {
  if (!m_vars) return;

  m_values = ExpressionListPtr
    (new ExpressionList(m_vars->getScope(), m_vars->getRange()));
  for (int i = 0; i < m_vars->getCount(); i++) {
    auto param = dynamic_pointer_cast<ParameterExpression>((*m_vars)[i]);
    auto const& name = param->getName();

    SimpleVariablePtr var(new SimpleVariable(param->getScope(),
                                             param->getRange(),
                                             name));
    if (param->isRef()) {
      var->setContext(RefValue);
    }
    m_values->addElement(var);
  }
  assert(m_vars->getCount() == m_values->getCount());
}