Permalink
Browse files

Remove whole program constant analysis

Summary:
This was pretty much dead code already. I noticed it while removing
the class analysis.

Reviewed By: ricklavoie

Differential Revision: D6770658

fbshipit-source-id: 6c35faf43935b0f7500a25d509f3980f3ca805dd
  • Loading branch information...
markw65 authored and hhvm-bot committed Jan 23, 2018
1 parent 0a072cf commit 38fa75e0ad42e4dc74349c2889c54a14f2f324f6
@@ -187,14 +187,6 @@ FileScopePtr AnalysisResult::findFileScope(const std::string &name) const {
return FileScopePtr();
}
BlockScopePtr AnalysisResult::findConstantDeclarer(
const std::string &name) {
if (getConstants()->isPresent(name)) return shared_from_this();
auto iter = m_constDecs.find(name);
if (iter != m_constDecs.end()) return iter->second;
return BlockScopePtr();
}
int AnalysisResult::getFunctionCount() const {
int total = 0;
for (auto& pair : m_files) {
@@ -214,17 +206,6 @@ int AnalysisResult::getClassCount() const {
///////////////////////////////////////////////////////////////////////////////
// static analysis functions
bool AnalysisResult::declareConst(FileScopePtr fs, const std::string &name) {
if (getConstants()->isPresent(name) ||
m_constDecs.find(name) != m_constDecs.end()) {
m_constRedeclared.insert(name);
return false;
} else {
m_constDecs[name] = fs;
return true;
}
}
static bool by_source(const BlockScopePtr &b1, const BlockScopePtr &b2) {
if (auto d = b1->getStmt()->getRange().compare(b2->getStmt()->getRange())) {
return d < 0;
@@ -238,28 +219,6 @@ void AnalysisResult::canonicalizeSymbolOrder() {
getVariables()->canonicalizeSymbolOrder();
}
///////////////////////////////////////////////////////////////////////////////
// Dependencies
bool AnalysisResult::isConstantDeclared(const std::string &constName) const {
if (m_constants->isPresent(constName)) return true;
auto const iter = m_constDecs.find(constName);
if (iter == m_constDecs.end()) return false;
FileScopePtr fileScope = iter->second;
ConstantTablePtr constants = fileScope->getConstants();
ConstructPtr decl = constants->getValue(constName);
if (decl) return true;
return false;
}
bool AnalysisResult::isConstantRedeclared(const std::string &constName) const {
return m_constRedeclared.find(constName) != m_constRedeclared.end();
}
bool AnalysisResult::isSystemConstant(const std::string &constName) const {
return m_constants->isSystem(constName);
}
///////////////////////////////////////////////////////////////////////////////
// Program
@@ -221,20 +221,6 @@ struct AnalysisResult : BlockScope, FunctionContainer {
void addFileScope(FileScopePtr fileScope);
/**
* Declarations
*/
bool declareConst(FileScopePtr fs, const std::string &name);
BlockScopeConstPtr findConstantDeclarer(const std::string &constName) const {
return const_cast<AnalysisResult*>(this)->findConstantDeclarer(constName);
}
BlockScopePtr findConstantDeclarer(const std::string &constName);
bool isConstantDeclared(const std::string &constName) const;
bool isConstantRedeclared(const std::string &constName) const;
bool isSystemConstant(const std::string &constName) const;
/**
* For function declaration parsing.
*/
@@ -265,8 +251,6 @@ struct AnalysisResult : BlockScope, FunctionContainer {
std::map<std::string, std::string> m_extraCodes;
StringToClassScopePtrMap m_systemClasses;
StringToFileScopePtrMap m_constDecs;
std::set<std::string> m_constRedeclared;
std::vector<StatementPtr> m_stmts;
StatementPtr m_stmt;
@@ -4327,13 +4327,7 @@ void EmitterVisitor::visit(FileScopePtr file) {
StringData *name;
TypedValue tv;
if (func->isSimpleDefine(&name, &tv)) {
auto k = func->isDefineWithoutImpl(ar)
? Unit::MergeKind::PersistentDefine
: Unit::MergeKind::Define;
if (tv.m_type == KindOfUninit) {
tv.m_type = KindOfNull;
}
m_ue.pushMergeableDef(k, name, tv);
m_ue.pushMergeableDef(Unit::MergeKind::Define, name, tv);
visit(s);
continue;
}
@@ -221,10 +221,6 @@ int FileScope::popAttribute() {
///////////////////////////////////////////////////////////////////////////////
void FileScope::declareConstant(AnalysisResultPtr ar, const std::string &name) {
ar->declareConst(shared_from_this(), name);
}
void FileScope::analyzeProgram(AnalysisResultConstRawPtr ar) {
if (!m_pseudoMain) return;
s_current = this;
@@ -111,14 +111,6 @@ struct FileScope : BlockScope, FunctionContainer,
void addAnonClass(ClassStatementPtr stmt);
const std::vector<ClassStatementPtr>& getAnonClasses() const;
/**
* For separate compilation
* These add edges between filescopes in the other dep graph and
* save the symbols for our iface.
* This stuff only happens in the filechanged state.
*/
void declareConstant(AnalysisResultPtr ar, const std::string &name);
void setSystem();
bool isSystem() const { return m_system; }
@@ -100,40 +100,6 @@ bool ConstantExpression::getScalarValue(Variant &value) {
return true;
}
///////////////////////////////////////////////////////////////////////////////
// parser functions
///////////////////////////////////////////////////////////////////////////////
// static analysis functions
Symbol *ConstantExpression::resolveNS(AnalysisResultConstRawPtr ar) {
BlockScopeConstPtr block = ar->findConstantDeclarer(m_name);
if (!block) {
if (!hadBackslash() && Option::WholeProgram) {
int pos = m_name.rfind('\\');
m_name = m_name.substr(pos + 1);
block = ar->findConstantDeclarer(m_name);
}
if (!block) return 0;
}
Symbol *sym = const_cast<Symbol*>(block->getConstants()->getSymbol(m_name));
always_assert(sym);
return sym;
}
void ConstantExpression::analyzeProgram(AnalysisResultConstRawPtr ar) {
if (ar->getPhase() == AnalysisResult::AnalyzeAll) {
Symbol *sym = resolveNS(ar);
if (!(m_context & LValue) && !m_dynamic) {
if (sym && !sym->isSystem()) {
if (sym->isDynamic()) {
m_dynamic = true;
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// code generation functions
@@ -33,7 +33,6 @@ struct ConstantExpression : Expression, private IParseHandler {
const std::string &docComment = "");
DECLARE_BASE_EXPRESSION_VIRTUAL_FUNCTIONS;
void analyzeProgram(AnalysisResultConstRawPtr ar) override;
void onParse(AnalysisResultConstRawPtr ar, FileScopePtr scope) override;
bool isScalar() const override;
bool isLiteralNull() const override;
@@ -65,7 +64,6 @@ struct ConstantExpression : Expression, private IParseHandler {
bool hadBackslash() const { return m_hadBackslash; }
private:
Symbol *resolveNS(AnalysisResultConstRawPtr ar);
std::string m_name;
std::string m_origName;
bool m_hadBackslash;
@@ -175,20 +175,6 @@ void SimpleFunctionCall::mungeIfSpecialFunction(AnalysisResultConstRawPtr ar,
m_params->removeElement(0);
m_params->insertElement(ename);
}
auto name = dynamic_pointer_cast<ScalarExpression>(ename);
if (name) {
auto const varName = name->getIdentifier();
if (varName.empty()) break;
AnalysisResult::Locker lock(ar);
fs->declareConstant(lock.get(), varName);
// handling define("CONSTANT", ...);
ExpressionPtr value = (*m_params)[1];
BlockScopePtr block = lock->findConstantDeclarer(varName);
ConstantTablePtr constants = block->getConstants();
if (constants != ar->getConstants()) {
constants->add(varName, value, ar, self);
}
}
}
break;
@@ -273,70 +259,6 @@ void SimpleFunctionCall::analyzeProgram(AnalysisResultConstRawPtr ar) {
// Look up the corresponding FunctionScope and ClassScope
// for this function call
setupScopes(ar);
// check for dynamic constant and volatile function/class
if (!m_class && !hasStaticClass() &&
(m_type == FunType::Define ||
m_type == FunType::Defined ||
m_type == FunType::FunctionExists ||
m_type == FunType::ClassExists ||
m_type == FunType::InterfaceExists) &&
m_params && m_params->getCount() >= 1) {
ExpressionPtr value = (*m_params)[0];
if (value->isScalar()) {
auto name = dynamic_pointer_cast<ScalarExpression>(value);
if (name && name->isLiteralString()) {
auto const symbol = name->getLiteralString();
switch (m_type) {
case FunType::Define: {
// system constant
if (ar->getConstants()->isPresent(symbol)) {
break;
}
// user constant
auto const arv = ar->lock();
auto const block = ar->findConstantDeclarer(symbol);
// not found (i.e., undefined)
if (!block) break;
auto const constants = block->getConstants();
const Symbol *sym = constants->getSymbol(symbol);
always_assert(sym);
if (!sym->isDynamic()) {
if (FunctionScopeRawPtr fsc = getFunctionScope()) {
if (!fsc->inPseudoMain()) {
const_cast<Symbol*>(sym)->setDynamic();
}
}
}
break;
}
case FunType::Defined: {
if (!ar->getConstants()->isPresent(symbol)) {
auto const arv = ar->lock();
// user constant
auto const block = arv->findConstantDeclarer(symbol);
if (block) { // found the constant
auto const constants = block->getConstants();
// set to be dynamic
if (m_type == FunType::Defined) {
constants->setDynamic(ar, symbol);
}
}
}
break;
}
case FunType::FunctionExists:
break;
case FunType::InterfaceExists:
case FunType::ClassExists:
break;
default:
assert(false);
}
}
}
}
if (m_params) m_params->markParams();
}
}
@@ -404,31 +326,6 @@ bool SimpleFunctionCall::isSimpleDefine(StringData **outName,
return true;
}
bool SimpleFunctionCall::isDefineWithoutImpl(AnalysisResultConstRawPtr ar) {
if (m_class || hasStaticClass()) return false;
if (m_type == FunType::Define && m_params &&
unsigned(m_params->getCount() - 2) <= 1u) {
if (m_dynamicConstant) return false;
auto name = dynamic_pointer_cast<ScalarExpression>((*m_params)[0]);
if (!name) return false;
auto const varName = name->getIdentifier();
if (varName.empty()) return false;
if (!SystemLib::s_inited || ar->isSystemConstant(varName)) {
return true;
}
ExpressionPtr value = (*m_params)[1];
if (ar->isConstantRedeclared(varName)) {
return false;
}
Variant scalarValue;
return (value->isScalar() &&
value->getScalarValue(scalarValue) &&
scalarValue.isAllowedAsConstantValue());
} else {
return false;
}
}
const StaticString
s_GLOBALS("GLOBALS"),
s_this("this");
@@ -37,7 +37,6 @@ struct SimpleFunctionCall final : FunctionCall {
void analyzeProgram(AnalysisResultConstRawPtr ar) override;
void deepCopy(SimpleFunctionCallPtr exp);
bool isDefineWithoutImpl(AnalysisResultConstRawPtr ar);
void setValid() { m_valid = true; }
void setThrowFatal() { m_type = FunType::ThrowFatal; }
void setThrowParseFatal() { m_type = FunType::ThrowParseFatal; }

0 comments on commit 38fa75e

Please sign in to comment.