Commit 6324bf44 authored by Roberto Raggi's avatar Roberto Raggi

Introduced IdExpressionAST.

parent 6226cfe1
......@@ -136,6 +136,12 @@ void ResolveExpression::addResult(const FullySpecifiedType &ty, Scope *scope)
_results.append(item);
}
bool ResolveExpression::visit(IdExpressionAST *ast)
{
accept(ast->name);
return false;
}
bool ResolveExpression::visit(BinaryExpressionAST *ast)
{
if (tokenKind(ast->binary_op_token) == T_COMMA && ast->right_expression && ast->right_expression->asQtMethod() != 0) {
......
......@@ -71,6 +71,7 @@ protected:
using ASTVisitor::visit;
virtual bool visit(IdExpressionAST *ast);
virtual bool visit(BinaryExpressionAST *ast);
virtual bool visit(CastExpressionAST *ast);
virtual bool visit(ConditionAST *ast);
......
......@@ -863,13 +863,15 @@ public:
if (path.size() > 1) {
if (CallAST *call = path.at(path.size() - 2)->asCall()) {
if (call->base_expression) {
if (SimpleNameAST *functionName = call->base_expression->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (id == "QT_TRANSLATE_NOOP" || id == "tr" || id == "trUtf8"
|| (type == TypeString && (id == "QLatin1String" || id == "QLatin1Literal"))
|| (type == TypeChar && id == "QLatin1Char"))
return noResult(); // skip it
if (IdExpressionAST *idExpr = call->base_expression->asIdExpression()) {
if (SimpleNameAST *functionName = idExpr->name->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (id == "QT_TRANSLATE_NOOP" || id == "tr" || id == "trUtf8"
|| (type == TypeString && (id == "QLatin1String" || id == "QLatin1Literal"))
|| (type == TypeChar && id == "QLatin1Char"))
return noResult(); // skip it
}
}
}
}
......@@ -958,14 +960,16 @@ public:
if (path.size() >= 2) {
if (CallAST *call = path.at(path.size() - 2)->asCall()) {
if (call->base_expression) {
if (SimpleNameAST *functionName = call->base_expression->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (id == "tr" || id == "trUtf8"
|| id == "translate"
|| id == "QT_TRANSLATE_NOOP"
|| id == "QLatin1String" || id == "QLatin1Literal")
return noResult(); // skip it
if (IdExpressionAST *idExpr = call->base_expression->asIdExpression()) {
if (SimpleNameAST *functionName = idExpr->name->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (id == "tr" || id == "trUtf8"
|| id == "translate"
|| id == "QT_TRANSLATE_NOOP"
|| id == "QLatin1String" || id == "QLatin1Literal")
return noResult(); // skip it
}
}
}
}
......@@ -1075,11 +1079,13 @@ public:
else if (path.size() > 1) {
if (CallAST *call = path.at(path.size() - 2)->asCall()) {
if (call->base_expression) {
if (SimpleNameAST *functionName = call->base_expression->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (IdExpressionAST *idExpr = call->base_expression->asIdExpression()) {
if (SimpleNameAST *functionName = idExpr->name->asSimpleName()) {
const QByteArray id(state.tokenAt(functionName->identifier_token).identifier->chars());
if (id == "QLatin1String" || id == "QLatin1Literal")
qlatin1Call = call;
if (id == "QLatin1String" || id == "QLatin1Literal")
qlatin1Call = call;
}
}
}
}
......@@ -1365,14 +1371,14 @@ protected:
bool preVisit(AST *ast) {
if (CaseStatementAST *cs = ast->asCaseStatement()) {
foundCaseStatementLevel = true;
ExpressionAST *expression = cs->expression->asSimpleName();
if (!expression)
expression = cs->expression->asQualifiedName();
if (expression) {
LookupItem item = typeOfExpression(expression,
document,
scope).first();
values << prettyPrint(LookupContext::fullyQualifiedName(item.declaration()));
if (ExpressionAST *expression = cs->expression->asIdExpression()) {
QList<LookupItem> candidates = typeOfExpression(expression,
document,
scope);
if (!candidates .isEmpty() && candidates.first().declaration()) {
Symbol *decl = candidates.first().declaration();
values << prettyPrint(LookupContext::fullyQualifiedName(decl));
}
}
return true;
} else if (foundCaseStatementLevel) {
......@@ -1555,8 +1561,9 @@ public:
for (int index = path.size() - 1; index != -1; --index) {
if (BinaryExpressionAST *binary = path.at(index)->asBinaryExpression()) {
if (binary->left_expression && binary->right_expression && state.tokenAt(binary->binary_op_token).is(T_EQUAL)) {
if (state.isCursorOn(binary->left_expression) && binary->left_expression->asSimpleName() != 0) {
SimpleNameAST *nameAST = binary->left_expression->asSimpleName();
IdExpressionAST *idExpr = binary->left_expression->asIdExpression();
if (state.isCursorOn(binary->left_expression) && idExpr && idExpr->name->asSimpleName() != 0) {
SimpleNameAST *nameAST = idExpr->name->asSimpleName();
const QList<LookupItem> results = state.context().lookup(nameAST->name, state.scopeAt(nameAST->firstToken()));
Declaration *decl = 0;
foreach (const LookupItem &r, results) {
......
......@@ -4211,3 +4211,21 @@ unsigned AttributeSpecifierAST::lastToken() const
return 0;
}
/** \generated */
unsigned IdExpressionAST::firstToken() const
{
if (name)
if (unsigned candidate = name->firstToken())
return candidate;
return 0;
}
/** \generated */
unsigned IdExpressionAST::lastToken() const
{
if (name)
if (unsigned candidate = name->lastToken())
return candidate;
return 0;
}
......@@ -201,6 +201,7 @@ public:
virtual FunctionDeclaratorAST *asFunctionDeclarator() { return 0; }
virtual FunctionDefinitionAST *asFunctionDefinition() { return 0; }
virtual GotoStatementAST *asGotoStatement() { return 0; }
virtual IdExpressionAST *asIdExpression() { return 0; }
virtual IfStatementAST *asIfStatement() { return 0; }
virtual LabeledStatementAST *asLabeledStatement() { return 0; }
virtual LambdaCaptureAST *asLambdaCapture() { return 0; }
......@@ -337,7 +338,7 @@ public:
virtual DeclarationAST *clone(MemoryPool *pool) const = 0;
};
class CPLUSPLUS_EXPORT NameAST: public ExpressionAST
class CPLUSPLUS_EXPORT NameAST: public AST
{
public: // annotations
const Name *name;
......@@ -965,6 +966,28 @@ protected:
virtual bool match0(AST *, ASTMatcher *);
};
class CPLUSPLUS_EXPORT IdExpressionAST: public ExpressionAST
{
public:
NameAST *name;
public:
IdExpressionAST()
: name(0)
{}
virtual IdExpressionAST *asIdExpression() { return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual IdExpressionAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
virtual bool match0(AST *, ASTMatcher *);
};
class CPLUSPLUS_EXPORT CompoundExpressionAST: public ExpressionAST
{
public:
......
......@@ -273,6 +273,14 @@ BaseSpecifierAST *BaseSpecifierAST::clone(MemoryPool *pool) const
return ast;
}
IdExpressionAST *IdExpressionAST::clone(MemoryPool *pool) const
{
IdExpressionAST *ast = new (pool) IdExpressionAST;
if (name)
ast->name = name->clone(pool);
return ast;
}
CompoundExpressionAST *CompoundExpressionAST::clone(MemoryPool *pool) const
{
CompoundExpressionAST *ast = new (pool) CompoundExpressionAST;
......
......@@ -201,6 +201,14 @@ bool BaseSpecifierAST::match0(AST *pattern, ASTMatcher *matcher)
return false;
}
bool IdExpressionAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (IdExpressionAST *_other = pattern->asIdExpression())
return matcher->match(this, _other);
return false;
}
bool CompoundExpressionAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (CompoundExpressionAST *_other = pattern->asCompoundExpression())
......
......@@ -427,6 +427,19 @@ bool ASTMatcher::match(BaseSpecifierAST *node, BaseSpecifierAST *pattern)
return true;
}
bool ASTMatcher::match(IdExpressionAST *node, IdExpressionAST *pattern)
{
(void) node;
(void) pattern;
if (! pattern->name)
pattern->name = node->name;
else if (! AST::match(node->name, pattern->name, this))
return false;
return true;
}
bool ASTMatcher::match(CompoundExpressionAST *node, CompoundExpressionAST *pattern)
{
(void) node;
......
......@@ -90,6 +90,7 @@ public:
virtual bool match(FunctionDeclaratorAST *node, FunctionDeclaratorAST *pattern);
virtual bool match(FunctionDefinitionAST *node, FunctionDefinitionAST *pattern);
virtual bool match(GotoStatementAST *node, GotoStatementAST *pattern);
virtual bool match(IdExpressionAST *node, IdExpressionAST *pattern);
virtual bool match(IfStatementAST *node, IfStatementAST *pattern);
virtual bool match(LabeledStatementAST *node, LabeledStatementAST *pattern);
virtual bool match(LinkageBodyAST *node, LinkageBodyAST *pattern);
......
......@@ -205,6 +205,14 @@ void BaseSpecifierAST::accept0(ASTVisitor *visitor)
visitor->endVisit(this);
}
void IdExpressionAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
accept(name, visitor);
}
visitor->endVisit(this);
}
void CompoundExpressionAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
......
......@@ -151,6 +151,7 @@ public:
virtual bool visit(FunctionDeclaratorAST *) { return true; }
virtual bool visit(FunctionDefinitionAST *) { return true; }
virtual bool visit(GotoStatementAST *) { return true; }
virtual bool visit(IdExpressionAST *) { return true; }
virtual bool visit(IfStatementAST *) { return true; }
virtual bool visit(LabeledStatementAST *) { return true; }
virtual bool visit(LinkageBodyAST *) { return true; }
......@@ -293,6 +294,7 @@ public:
virtual void endVisit(FunctionDeclaratorAST *) { }
virtual void endVisit(FunctionDefinitionAST *) { }
virtual void endVisit(GotoStatementAST *) { }
virtual void endVisit(IdExpressionAST *) { }
virtual void endVisit(IfStatementAST *) { }
virtual void endVisit(LabeledStatementAST *) { }
virtual void endVisit(LinkageBodyAST *) { }
......
......@@ -108,6 +108,7 @@ class ForeachStatementAST;
class FunctionDeclaratorAST;
class FunctionDefinitionAST;
class GotoStatementAST;
class IdExpressionAST;
class IfStatementAST;
class LabeledStatementAST;
class LambdaCaptureAST;
......
......@@ -3822,7 +3822,9 @@ bool Parser::parsePrimaryExpression(ExpressionAST *&node)
default: {
NameAST *name = 0;
if (parseNameId(name)) {
node = name;
IdExpressionAST *ast = new (_pool) IdExpressionAST;
ast->name = name;
node = ast;
return true;
}
break;
......
......@@ -116,8 +116,8 @@ void tst_AST::simple_name_1()
AST *ast = unit->ast();
QVERIFY(ast != 0);
QVERIFY(ast->asSimpleName() != 0);
QCOMPARE(ast->asSimpleName()->identifier_token, 1U);
QVERIFY(ast->asIdExpression()->name->asSimpleName() != 0);
QCOMPARE(ast->asIdExpression()->name->asSimpleName()->identifier_token, 1U);
}
void tst_AST::template_id_1()
......@@ -126,15 +126,15 @@ void tst_AST::template_id_1()
AST *ast = unit->ast();
QVERIFY(ast != 0);
QVERIFY(ast->asTemplateId() != 0);
QCOMPARE(ast->asTemplateId()->identifier_token, 1U);
QCOMPARE(ast->asTemplateId()->less_token, 2U);
QVERIFY(ast->asTemplateId()->template_argument_list != 0);
QVERIFY(ast->asTemplateId()->template_argument_list->value != 0);
QVERIFY(ast->asTemplateId()->template_argument_list->value->asNumericLiteral() != 0);
QCOMPARE(ast->asTemplateId()->template_argument_list->value->asNumericLiteral()->literal_token, 3U);
QVERIFY(ast->asTemplateId()->template_argument_list->next == 0);
QCOMPARE(ast->asTemplateId()->greater_token, 4U);
QVERIFY(ast->asIdExpression()->name->asTemplateId() != 0);
QCOMPARE(ast->asIdExpression()->name->asTemplateId()->identifier_token, 1U);
QCOMPARE(ast->asIdExpression()->name->asTemplateId()->less_token, 2U);
QVERIFY(ast->asIdExpression()->name->asTemplateId()->template_argument_list != 0);
QVERIFY(ast->asIdExpression()->name->asTemplateId()->template_argument_list->value != 0);
QVERIFY(ast->asIdExpression()->name->asTemplateId()->template_argument_list->value->asNumericLiteral() != 0);
QCOMPARE(ast->asIdExpression()->name->asTemplateId()->template_argument_list->value->asNumericLiteral()->literal_token, 3U);
QVERIFY(ast->asIdExpression()->name->asTemplateId()->template_argument_list->next == 0);
QCOMPARE(ast->asIdExpression()->name->asTemplateId()->greater_token, 4U);
}
void tst_AST::new_expression_1()
......@@ -213,7 +213,7 @@ void tst_AST::condition_1()
QVERIFY(ltExpr->left_expression);
QVERIFY(ltExpr->right_expression);
SimpleNameAST *x = ltExpr->left_expression->asSimpleName();
SimpleNameAST *x = ltExpr->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(x);
QCOMPARE(unit->spell(x->identifier_token), "x");
......@@ -227,7 +227,7 @@ void tst_AST::condition_1()
QVERIFY(gtExpr->left_expression);
QVERIFY(gtExpr->right_expression);
SimpleNameAST *y = gtExpr->left_expression->asSimpleName();
SimpleNameAST *y = gtExpr->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(y);
QCOMPARE(unit->spell(y->identifier_token), "y");
......@@ -240,7 +240,7 @@ void tst_AST::condition_1()
QVERIFY(intType);
// ### here we could check if the type is an actual int
SimpleNameAST *a = cast->expression->asSimpleName();
SimpleNameAST *a = cast->expression->asIdExpression()->name->asSimpleName();
QVERIFY(a);
QCOMPARE(unit->spell(a->identifier_token), "a");
}
......@@ -285,7 +285,7 @@ void tst_AST::conditional_1()
QVERIFY(ltExpr->left_expression);
QVERIFY(ltExpr->right_expression);
SimpleNameAST *x = ltExpr->left_expression->asSimpleName();
SimpleNameAST *x = ltExpr->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(x);
QCOMPARE(unit->spell(x->identifier_token), "x");
......@@ -299,7 +299,7 @@ void tst_AST::conditional_1()
QVERIFY(gtExpr->left_expression);
QVERIFY(gtExpr->right_expression);
SimpleNameAST *y = gtExpr->left_expression->asSimpleName();
SimpleNameAST *y = gtExpr->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(y);
QCOMPARE(unit->spell(y->identifier_token), "y");
......@@ -318,7 +318,7 @@ void tst_AST::conditional_1()
QVERIFY(intSpec);
QCOMPARE(unit->spell(intSpec->specifier_token), "int");
SimpleNameAST *a = cast->expression->asSimpleName();
SimpleNameAST *a = cast->expression->asIdExpression()->name->asSimpleName();
QVERIFY(a);
QCOMPARE(unit->spell(a->identifier_token), "a");
......@@ -326,7 +326,7 @@ void tst_AST::conditional_1()
QVERIFY(equals);
QCOMPARE(unit->tokenKind(equals->binary_op_token), (int) T_EQUAL_EQUAL);
x = equals->left_expression->asSimpleName();
x = equals->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(x);
QCOMPARE(unit->spell(x->identifier_token), "x");
......@@ -338,7 +338,7 @@ void tst_AST::conditional_1()
QVERIFY(assignment);
QCOMPARE(unit->tokenKind(assignment->binary_op_token), (int) T_EQUAL);
y = assignment->left_expression->asSimpleName();
y = assignment->left_expression->asIdExpression()->name->asSimpleName();
QVERIFY(y);
QCOMPARE(unit->spell(y->identifier_token), "y");
......@@ -434,7 +434,7 @@ void tst_AST::if_statement_1()
QVERIFY(then_stmt->expression != 0);
QCOMPARE(then_stmt->semicolon_token, 6U);
SimpleNameAST *id_expr = then_stmt->expression->asSimpleName();
SimpleNameAST *id_expr = then_stmt->expression->asIdExpression()->name->asSimpleName();
QVERIFY(id_expr != 0);
QCOMPARE(id_expr->identifier_token, 5U);
}
......@@ -490,7 +490,7 @@ void tst_AST::if_else_statement()
QVERIFY(then_stmt->expression != 0);
QCOMPARE(then_stmt->semicolon_token, 6U);
SimpleNameAST *a_id_expr = then_stmt->expression->asSimpleName();
SimpleNameAST *a_id_expr = then_stmt->expression->asIdExpression()->name->asSimpleName();
QVERIFY(a_id_expr != 0);
QCOMPARE(a_id_expr->identifier_token, 5U);
......@@ -500,7 +500,7 @@ void tst_AST::if_else_statement()
QVERIFY(else_stmt->expression != 0);
QCOMPARE(else_stmt->semicolon_token, 9U);
SimpleNameAST *b_id_expr = else_stmt->expression->asSimpleName();
SimpleNameAST *b_id_expr = else_stmt->expression->asIdExpression()->name->asSimpleName();
QVERIFY(b_id_expr != 0);
QCOMPARE(b_id_expr->identifier_token, 8U);
}
......@@ -521,8 +521,8 @@ void tst_AST::while_statement()
QVERIFY(stmt->statement != 0);
// check condition
QVERIFY(stmt->condition->asSimpleName() != 0);
QCOMPARE(stmt->condition->asSimpleName()->identifier_token, 3U);
QVERIFY(stmt->condition->asIdExpression()->name->asSimpleName() != 0);
QCOMPARE(stmt->condition->asIdExpression()->name->asSimpleName()->identifier_token, 3U);
// check the `body' statement
CompoundStatementAST *body_stmt = stmt->statement->asCompoundStatement();
......@@ -562,8 +562,8 @@ void tst_AST::while_condition_statement()
QCOMPARE(condition->declarator->core_declarator->asDeclaratorId()->name->asSimpleName()->identifier_token, 4U);
QVERIFY(condition->declarator->postfix_declarator_list == 0);
QVERIFY(condition->declarator->initializer != 0);
QVERIFY(condition->declarator->initializer->asSimpleName() != 0);
QCOMPARE(condition->declarator->initializer->asSimpleName()->identifier_token, 6U);
QVERIFY(condition->declarator->initializer->asIdExpression()->name->asSimpleName() != 0);
QCOMPARE(condition->declarator->initializer->asIdExpression()->name->asSimpleName()->identifier_token, 6U);
// check the `body' statement
CompoundStatementAST *body_stmt = stmt->statement->asCompoundStatement();
......@@ -816,14 +816,14 @@ void tst_AST::normal_array_access()
{ // check the left-hand side:
ExpressionAST *lhs = arrayExpr->base_expression;
QVERIFY(lhs);
SimpleNameAST *a = lhs->asSimpleName();
SimpleNameAST *a = lhs->asIdExpression()->name->asSimpleName();
QVERIFY(a);
QCOMPARE(QLatin1String(unit->identifier(a->identifier_token)->chars()), QLatin1String("a"));
}
{ // check the right-hand side:
QVERIFY(arrayExpr->expression);
SimpleNameAST *b = arrayExpr->expression->asSimpleName();
SimpleNameAST *b = arrayExpr->expression->asIdExpression()->name->asSimpleName();
QVERIFY(b);
QCOMPARE(QLatin1String(unit->identifier(b->identifier_token)->chars()), QLatin1String("b"));
}
......@@ -860,14 +860,14 @@ void tst_AST::array_access_with_nested_expression()
QVERIFY(lhs);
NestedExpressionAST *nested_a = lhs->asNestedExpression();
QVERIFY(nested_a && nested_a->expression);
SimpleNameAST *a = nested_a->expression->asSimpleName();
SimpleNameAST *a = nested_a->expression->asIdExpression()->name->asSimpleName();
QVERIFY(a);
QCOMPARE(QLatin1String(unit->identifier(a->identifier_token)->chars()), QLatin1String("a"));
}
{ // check the RHS:
QVERIFY(arrayExpr->expression);
SimpleNameAST *b = arrayExpr->expression->asSimpleName();
SimpleNameAST *b = arrayExpr->expression->asIdExpression()->name->asSimpleName();
QVERIFY(b);
QCOMPARE(QLatin1String(unit->identifier(b->identifier_token)->chars()), QLatin1String("b"));
}
......@@ -946,7 +946,7 @@ void tst_AST::objc_msg_send_expression()
QVERIFY(msgExpr);
QVERIFY(msgExpr->receiver_expression);
SimpleNameAST *receiver = msgExpr->receiver_expression->asSimpleName();
SimpleNameAST *receiver = msgExpr->receiver_expression->asIdExpression()->name->asSimpleName();
QVERIFY(receiver);
QCOMPARE(QLatin1String(unit->identifier(receiver->identifier_token)->chars()), QLatin1String("obj"));
......
......@@ -275,6 +275,12 @@ virtual bool visit(BaseSpecifierAST *ast)
return false;
}
virtual bool visit(IdExpressionAST *ast)
{
nonterminal(ast->name);
return false;
}
virtual bool visit(CompoundExpressionAST *ast)
{
if (ast->lparen_token)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment