summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthijs Tijink <matthijstijink@gmail.com>2017-07-17 07:47:58 (GMT)
committerAlexander Zhigalin <alexander@zhigalin.tk>2017-07-17 07:56:30 (GMT)
commit3a8ad5cc279b6996ca29f622c195c7893566d805 (patch)
treeb92cd4d45803404922991f6663a0cde339c79d84
parent8da5db996bf9bd8432f73335b30fe85db05decfa (diff)
Allow non-scalar constants in PHP language support
Summary: Newer PHP versions allow almost any expression in the definition of a constant, as long as it is actually constant. This is hard to test in the parser (and impossibly in the grammar definition) though, so this diff just allows any expression. Test Plan: I edited some tests to make sure the new behaviour is allowed. Reviewers: #kdevelop Subscribers: kdevelop-devel, #kdevelop Tags: #kdevelop Differential Revision: https://phabricator.kde.org/D6670
-rw-r--r--duchain/builders/declarationbuilder.cpp21
-rw-r--r--duchain/tests/duchain.cpp8
-rw-r--r--parser/php.g4
3 files changed, 6 insertions, 27 deletions
diff --git a/duchain/builders/declarationbuilder.cpp b/duchain/builders/declarationbuilder.cpp
index 214f311..1a90595 100644
--- a/duchain/builders/declarationbuilder.cpp
+++ b/duchain/builders/declarationbuilder.cpp
@@ -649,27 +649,6 @@ void DeclarationBuilder::visitConstantDeclaration(ConstantDeclarationAst *node)
}
DeclarationBuilderBase::visitConstantDeclaration(node);
closeDeclaration();
- if ( m_reportErrors ) {
- // const class members may only be ints, floats, bools or strings
- bool badType = true;
- if ( IntegralType* type = fastCast<IntegralType*>(lastType().data()) ) {
- switch( type->dataType() ) {
- case IntegralType::TypeBoolean:
- case IntegralType::TypeFloat:
- case IntegralType::TypeInt:
- case IntegralType::TypeString:
- case IntegralType::TypeNull:
- badType = false;
- break;
- default:
- // every other type is a badType (see above)
- break;
- }
- }
- if ( badType ) {
- reportError(i18n("Only booleans, ints, floats and strings are allowed for class constants."), node->scalar);
- }
- }
}
void DeclarationBuilder::visitTraitAliasStatement(TraitAliasStatementAst *node)
diff --git a/duchain/tests/duchain.cpp b/duchain/tests/duchain.cpp
index 1b37505..b00c709 100644
--- a/duchain/tests/duchain.cpp
+++ b/duchain/tests/duchain.cpp
@@ -1223,8 +1223,8 @@ void TestDUChain::classConst_data()
QTest::newRow("selfConst") << "const C2 = 1; const C = self::C2;" << 0;
QTest::newRow("parentConst") << "const C = parent::P;" << 0;
QTest::newRow("null") << "const C = null;" << 0;
-
- QTest::newRow("array") << "const C = array();" << 1;
+ QTest::newRow("array") << "const C = array();" << 0;
+ QTest::newRow("expression") << "const C = 'foo' . 'foo';" << 0;
}
void TestDUChain::classConst()
@@ -1255,8 +1255,8 @@ void TestDUChain::fileConst_data()
QTest::newRow("string") << "const C = 'asdf';" << 0 << (uint) IntegralType::TypeString;
QTest::newRow("float") << "const C = 0.5;" << 0 << (uint) IntegralType::TypeFloat;
QTest::newRow("bool") << "const C = true;" << 0 << (uint) IntegralType::TypeBoolean;
-
- QTest::newRow("array") << "const C = array();" << 1 << (uint) IntegralType::TypeArray;
+ QTest::newRow("array") << "const C = array();" << 0 << (uint) IntegralType::TypeArray;
+ QTest::newRow("expression") << "const C = 'foo' . 'foo';" << 0 << (uint) IntegralType::TypeString;
}
void TestDUChain::fileConst()
diff --git a/parser/php.g b/parser/php.g
index 78af85d..cecd25f 100644
--- a/parser/php.g
+++ b/parser/php.g
@@ -725,7 +725,7 @@ expression=nullCoalesceExpression
identifier=namespacedIdentifier (AS aliasIdentifier=identifier | 0)
-> useNamespace ;;
- identifier=identifier ASSIGN scalar=staticScalar
+ identifier=identifier ASSIGN scalar=expr
-> constantDeclaration ;;
SEMICOLON | CLOSE_TAG
@@ -889,7 +889,7 @@ arrayIndex=arrayIndexSpecifier | LBRACE expr=expr RBRACE
-> parameterList ;;
(parameterType=parameterType | 0) (isRef=BIT_AND | 0)
- (isVariadic=ELLIPSIS | 0) variable=variableIdentifier (ASSIGN defaultValue=staticScalar | 0)
+ (isVariadic=ELLIPSIS | 0) variable=variableIdentifier (ASSIGN defaultValue=expr | 0)
-> parameter ;;
(isNullable=QUESTION | 0) (