Skip to content

Commit

Permalink
Parser support for PHP 7 Scalar Types
Browse files Browse the repository at this point in the history
Summary: Adds support for declare(strict_types=...), and scalar type hints gated by a
runtime option. A stacked diff will use the flag that is set in Unit.

Reviewed By: jwatzman

Differential Revision: D2674350

fb-gh-sync-id: 1af7a338eb5419c59a86da450826b047ddfe56d3
  • Loading branch information
paulbiss authored and hhvm-bot committed Dec 2, 2015
1 parent 9409346 commit ae189f3
Show file tree
Hide file tree
Showing 26 changed files with 2,328 additions and 1,966 deletions.
35 changes: 35 additions & 0 deletions hphp/compiler/analysis/emitter.cpp
Expand Up @@ -98,6 +98,7 @@
#include "hphp/compiler/statement/trait_prec_statement.h"
#include "hphp/compiler/statement/trait_alias_statement.h"
#include "hphp/compiler/statement/typedef_statement.h"
#include "hphp/compiler/statement/declare_statement.h"
#include "hphp/compiler/parser/parser.h"
#include "hphp/hhbbc/hhbbc.h"

Expand Down Expand Up @@ -2558,6 +2559,7 @@ void EmitterVisitor::visit(FileScopePtr file) {
const std::string& filename = file->getName();
m_ue.m_filepath = makeStaticString(filename);
m_ue.m_isHHFile = file->isHHFile();
m_ue.m_useStrictTypes = file->useStrictTypes();

FunctionScopePtr func(file->getPseudoMain());
if (!func) return;
Expand Down Expand Up @@ -2630,6 +2632,26 @@ void EmitterVisitor::visit(FileScopePtr file) {
}
break;
}
case Construct::KindOfDeclareStatement: {
auto ds = static_pointer_cast<DeclareStatement>(s);
for (auto& decl : ds->getDeclareMap()) {
if (decl.first == "strict_types") {
if (ds->getBlock()->getStmts()->getCount()) {
emitMakeUnitFatal(e, "strict_types declaration must not use "
"block mode");
break;
}
if (!RuntimeOption::PHP7_ScalarTypes) {
emitMakeUnitFatal(e, "strict_types can only be used when"
"hhvm.php7.scalar_types = true");
break;
}
}
}

visit(ds->getBlock());
break;
}
case Statement::KindOfTypedefStatement: {
auto const id =
emitTypedef(e, static_pointer_cast<TypedefStatement>(s));
Expand Down Expand Up @@ -2955,6 +2977,19 @@ bool EmitterVisitor::visit(ConstructPtr node) {
return false;
}

case Construct::KindOfDeclareStatement: {
auto ds = static_pointer_cast<DeclareStatement>(node);
for (auto& decl : ds->getDeclareMap()) {
if (decl.first == "strict_types") {
emitMakeUnitFatal(e, "strict_types declaration must not use "
"block mode");
}
}

visit(ds->getBlock());
return false;
}

case Construct::KindOfContinueStatement:
case Construct::KindOfBreakStatement: {
auto s = static_pointer_cast<Statement>(node);
Expand Down
6 changes: 5 additions & 1 deletion hphp/compiler/analysis/file_scope.cpp
Expand Up @@ -50,7 +50,7 @@ namespace HPHP {
FileScope::FileScope(const std::string &fileName, int fileSize, const MD5 &md5)
: BlockScope("", "", StatementPtr(), BlockScope::FileScope),
m_size(fileSize), m_md5(md5), m_system(false),
m_isHHFile(false), m_preloadPriority(0),
m_isHHFile(false), m_useStrictTypes(false), m_preloadPriority(0),
m_fileName(fileName), m_redeclaredFunctions(0) {
pushAttribute(); // for global scope
}
Expand Down Expand Up @@ -82,6 +82,10 @@ void FileScope::setHHFile() {
m_isHHFile = true;
}

void FileScope::setUseStrictTypes() {
m_useStrictTypes = true;
}

FunctionScopePtr FileScope::setTree(AnalysisResultConstPtr ar,
StatementListPtr tree) {
m_tree = tree;
Expand Down
4 changes: 4 additions & 0 deletions hphp/compiler/analysis/file_scope.h
Expand Up @@ -165,6 +165,9 @@ class FileScope : public BlockScope,
void setHHFile();
bool isHHFile() const { return m_isHHFile; }

void setUseStrictTypes();
bool useStrictTypes() const { return m_useStrictTypes; }

void setPreloadPriority(int p) { m_preloadPriority = p; }
int preloadPriority() const { return m_preloadPriority; }

Expand All @@ -190,6 +193,7 @@ class FileScope : public BlockScope,
MD5 m_md5;
unsigned m_system : 1;
unsigned m_isHHFile : 1;
unsigned m_useStrictTypes : 1;
int m_preloadPriority;

std::vector<int> m_attributes;
Expand Down
3 changes: 2 additions & 1 deletion hphp/compiler/construct.h
Expand Up @@ -104,7 +104,8 @@ class IParseHandler {
x(TraitPrecStatement) \
x(TraitAliasStatement) \
x(TypedefStatement) \
x(UseDeclarationStatementFragment)
x(UseDeclarationStatementFragment) \
x(DeclareStatement)

#define DECLARE_EXPRESSION_TYPES(x) \
x(Expression, None) \
Expand Down

0 comments on commit ae189f3

Please sign in to comment.