Commit ea4b4bff authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Nikolai Kosjar

C++: Split designator AST

Change-Id: I9bfed2023624c818c0f35f24476693cffeaf2bbc
Reviewed-by: default avatarWang Hoi <wanghoi@126.com>
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent cb079c53
......@@ -4478,41 +4478,49 @@ unsigned DesignatedInitializerAST::lastToken() const
return 1;
}
unsigned DesignatorAST::firstToken() const
{
if (type == DesignatorAST::Dot) {
if (u.dot.dot_token)
return u.dot.dot_token;
if (u.dot.identifier_token)
return u.dot.identifier_token;
} else if (type == DesignatorAST::Bracket) {
if (u.bracket.lbracket_token)
return u.bracket.lbracket_token;
if (u.bracket.expression)
if (unsigned candidate = u.bracket.expression->firstToken())
return candidate;
if (u.bracket.rbracket_token)
return u.bracket.rbracket_token;
}
return 0;
}
unsigned DesignatorAST::lastToken() const
{
if (type == DesignatorAST::Dot) {
if (u.dot.identifier_token)
return u.dot.identifier_token + 1;
if (u.dot.dot_token)
return u.dot.dot_token + 1;
} else if (type == DesignatorAST::Bracket) {
if (u.bracket.rbracket_token)
return u.bracket.rbracket_token + 1;
if (u.bracket.expression)
if (unsigned candidate = u.bracket.expression->lastToken())
return candidate;
if (u.bracket.lbracket_token)
return u.bracket.lbracket_token + 1;
}
/** \generated */
unsigned BracketDesignatorAST::firstToken() const
{
if (lbracket_token)
return lbracket_token;
if (expression)
if (unsigned candidate = expression->firstToken())
return candidate;
if (rbracket_token)
return rbracket_token;
return 0;
}
/** \generated */
unsigned BracketDesignatorAST::lastToken() const
{
if (rbracket_token)
return rbracket_token + 1;
if (expression)
if (unsigned candidate = expression->lastToken())
return candidate;
if (lbracket_token)
return lbracket_token + 1;
return 1;
}
/** \generated */
unsigned DotDesignatorAST::firstToken() const
{
if (dot_token)
return dot_token;
if (identifier_token)
return identifier_token;
return 0;
}
/** \generated */
unsigned DotDesignatorAST::lastToken() const
{
if (identifier_token)
return identifier_token + 1;
if (dot_token)
return dot_token + 1;
return 1;
}
......@@ -138,6 +138,7 @@ public:
virtual BinaryExpressionAST *asBinaryExpression() { return 0; }
virtual BoolLiteralAST *asBoolLiteral() { return 0; }
virtual BracedInitializerAST *asBracedInitializer() { return 0; }
virtual BracketDesignatorAST *asBracketDesignator() { return 0; }
virtual BreakStatementAST *asBreakStatement() { return 0; }
virtual CallAST *asCall() { return 0; }
virtual CaptureAST *asCapture() { return 0; }
......@@ -165,6 +166,7 @@ public:
virtual DesignatorAST *asDesignator() { return 0; }
virtual DestructorNameAST *asDestructorName() { return 0; }
virtual DoStatementAST *asDoStatement() { return 0; }
virtual DotDesignatorAST *asDotDesignator() { return 0; }
virtual DynamicExceptionSpecificationAST *asDynamicExceptionSpecification() { return 0; }
virtual ElaboratedTypeSpecifierAST *asElaboratedTypeSpecifier() { return 0; }
virtual EmptyDeclarationAST *asEmptyDeclaration() { return 0; }
......@@ -4531,38 +4533,54 @@ protected:
class DesignatorAST: public AST
{
public:
enum Type
{
Invalid,
Dot,
Bracket
};
Type type;
union Designator
{
struct DotDesignator
{
unsigned dot_token;
unsigned identifier_token;
} dot;
struct BracketDesignator
{
unsigned lbracket_token;
ExpressionAST *expression;
unsigned rbracket_token;
} bracket;
} u;
public:
DesignatorAST()
{}
virtual DesignatorAST *asDesignator() { return this; }
virtual DesignatorAST *clone(MemoryPool *pool) const = 0;
};
class DotDesignatorAST: public DesignatorAST
{
public:
unsigned dot_token;
unsigned identifier_token;
public:
DotDesignatorAST()
: dot_token(0)
, identifier_token(0)
{}
virtual DotDesignatorAST *asDotDesignator() { return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual DotDesignatorAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
virtual bool match0(AST *, ASTMatcher *);
};
class BracketDesignatorAST: public DesignatorAST
{
public:
unsigned lbracket_token;
ExpressionAST *expression;
unsigned rbracket_token;
public:
BracketDesignatorAST()
: lbracket_token(0)
, expression(0)
, rbracket_token(0)
{}
virtual BracketDesignatorAST *asBracketDesignator() { return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual DesignatorAST *clone(MemoryPool *pool) const;
virtual BracketDesignatorAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
......
......@@ -1762,9 +1762,21 @@ BracedInitializerAST *BracedInitializerAST::clone(MemoryPool *pool) const
return ast;
}
DesignatorAST *DesignatorAST::clone(MemoryPool *pool) const
DotDesignatorAST *DotDesignatorAST::clone(MemoryPool *pool) const
{
DesignatorAST *ast = new (pool) DesignatorAST;
DotDesignatorAST *ast = new (pool) DotDesignatorAST;
ast->dot_token = dot_token;
ast->identifier_token = identifier_token;
return ast;
}
BracketDesignatorAST *BracketDesignatorAST::clone(MemoryPool *pool) const
{
BracketDesignatorAST *ast = new (pool) BracketDesignatorAST;
ast->lbracket_token = lbracket_token;
if (expression)
ast->expression = expression->clone(pool);
ast->rbracket_token = rbracket_token;
return ast;
}
......
......@@ -1192,9 +1192,17 @@ bool BracedInitializerAST::match0(AST *pattern, ASTMatcher *matcher)
return false;
}
bool DesignatorAST::match0(AST *pattern, ASTMatcher *matcher)
bool DotDesignatorAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (DesignatorAST *_other = pattern->asDesignator())
if (DotDesignatorAST *_other = pattern->asDotDesignator())
return matcher->match(this, _other);
return false;
}
bool BracketDesignatorAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (BracketDesignatorAST *_other = pattern->asBracketDesignator())
return matcher->match(this, _other);
return false;
......
......@@ -2999,11 +2999,32 @@ bool ASTMatcher::match(BracedInitializerAST *node, BracedInitializerAST *pattern
return true;
}
bool ASTMatcher::match(DesignatorAST *node, DesignatorAST *pattern)
bool ASTMatcher::match(DotDesignatorAST *node, DotDesignatorAST *pattern)
{
(void) node;
(void) pattern;
pattern->dot_token = node->dot_token;
pattern->identifier_token = node->identifier_token;
return true;
}
bool ASTMatcher::match(BracketDesignatorAST *node, BracketDesignatorAST *pattern)
{
(void) node;
(void) pattern;
pattern->lbracket_token = node->lbracket_token;
if (! pattern->expression)
pattern->expression = node->expression;
else if (! AST::match(node->expression, pattern->expression, this))
return false;
pattern->rbracket_token = node->rbracket_token;
return true;
}
......
......@@ -45,6 +45,7 @@ public:
virtual bool match(BinaryExpressionAST *node, BinaryExpressionAST *pattern);
virtual bool match(BoolLiteralAST *node, BoolLiteralAST *pattern);
virtual bool match(BracedInitializerAST *node, BracedInitializerAST *pattern);
virtual bool match(BracketDesignatorAST *node, BracketDesignatorAST *pattern);
virtual bool match(BreakStatementAST *node, BreakStatementAST *pattern);
virtual bool match(CallAST *node, CallAST *pattern);
virtual bool match(CaptureAST *node, CaptureAST *pattern);
......@@ -67,9 +68,9 @@ public:
virtual bool match(DecltypeSpecifierAST *node, DecltypeSpecifierAST *pattern);
virtual bool match(DeleteExpressionAST *node, DeleteExpressionAST *pattern);
virtual bool match(DesignatedInitializerAST *node, DesignatedInitializerAST *pattern);
virtual bool match(DesignatorAST *node, DesignatorAST *pattern);
virtual bool match(DestructorNameAST *node, DestructorNameAST *pattern);
virtual bool match(DoStatementAST *node, DoStatementAST *pattern);
virtual bool match(DotDesignatorAST *node, DotDesignatorAST *pattern);
virtual bool match(DynamicExceptionSpecificationAST *node, DynamicExceptionSpecificationAST *pattern);
virtual bool match(ElaboratedTypeSpecifierAST *node, ElaboratedTypeSpecifierAST *pattern);
virtual bool match(EmptyDeclarationAST *node, EmptyDeclarationAST *pattern);
......
......@@ -1147,9 +1147,16 @@ public:
return __ast;
}
DesignatorAST *Designator()
DotDesignatorAST *DotDesignator()
{
DesignatorAST *__ast = new (&pool) DesignatorAST;
DotDesignatorAST *__ast = new (&pool) DotDesignatorAST;
return __ast;
}
BracketDesignatorAST *BracketDesignator(ExpressionAST *expression = 0)
{
BracketDesignatorAST *__ast = new (&pool) BracketDesignatorAST;
__ast->expression = expression;
return __ast;
}
......
......@@ -1279,13 +1279,21 @@ void BracedInitializerAST::accept0(ASTVisitor *visitor)
visitor->endVisit(this);
}
void DesignatorAST::accept0(ASTVisitor *visitor)
void DotDesignatorAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
}
visitor->endVisit(this);
}
void BracketDesignatorAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
accept(expression, visitor);
}
visitor->endVisit(this);
}
void DesignatedInitializerAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
......
......@@ -87,6 +87,7 @@ public:
virtual bool visit(BinaryExpressionAST *) { return true; }
virtual bool visit(BoolLiteralAST *) { return true; }
virtual bool visit(BracedInitializerAST *) { return true; }
virtual bool visit(BracketDesignatorAST *) { return true; }
virtual bool visit(BreakStatementAST *) { return true; }
virtual bool visit(CallAST *) { return true; }
virtual bool visit(CaptureAST *) { return true; }
......@@ -109,9 +110,9 @@ public:
virtual bool visit(DecltypeSpecifierAST *) { return true; }
virtual bool visit(DeleteExpressionAST *) { return true; }
virtual bool visit(DesignatedInitializerAST *) { return true; }
virtual bool visit(DesignatorAST *) { return true; }
virtual bool visit(DestructorNameAST *) { return true; }
virtual bool visit(DoStatementAST *) { return true; }
virtual bool visit(DotDesignatorAST *) { return true; }
virtual bool visit(DynamicExceptionSpecificationAST *) { return true; }
virtual bool visit(ElaboratedTypeSpecifierAST *) { return true; }
virtual bool visit(EmptyDeclarationAST *) { return true; }
......@@ -235,6 +236,7 @@ public:
virtual void endVisit(BinaryExpressionAST *) {}
virtual void endVisit(BoolLiteralAST *) {}
virtual void endVisit(BracedInitializerAST *) {}
virtual void endVisit(BracketDesignatorAST *) {}
virtual void endVisit(BreakStatementAST *) {}
virtual void endVisit(CallAST *) {}
virtual void endVisit(CaptureAST *) {}
......@@ -257,9 +259,9 @@ public:
virtual void endVisit(DecltypeSpecifierAST *) {}
virtual void endVisit(DeleteExpressionAST *) {}
virtual void endVisit(DesignatedInitializerAST *) {}
virtual void endVisit(DesignatorAST *) {}
virtual void endVisit(DestructorNameAST *) {}
virtual void endVisit(DoStatementAST *) {}
virtual void endVisit(DotDesignatorAST *) {}
virtual void endVisit(DynamicExceptionSpecificationAST *) {}
virtual void endVisit(ElaboratedTypeSpecifierAST *) {}
virtual void endVisit(EmptyDeclarationAST *) {}
......
......@@ -45,6 +45,7 @@ class BaseSpecifierAST;
class BinaryExpressionAST;
class BoolLiteralAST;
class BracedInitializerAST;
class BracketDesignatorAST;
class BreakStatementAST;
class CallAST;
class CaptureAST;
......@@ -72,6 +73,7 @@ class DesignatedInitializerAST;
class DesignatorAST;
class DestructorNameAST;
class DoStatementAST;
class DotDesignatorAST;
class DynamicExceptionSpecificationAST;
class ElaboratedTypeSpecifierAST;
class EmptyDeclarationAST;
......
......@@ -5528,18 +5528,16 @@ bool Parser::parseDesignator(DesignatorAST *&node)
DEBUG_THIS_RULE();
const unsigned start = cursor();
if (LA() == T_DOT) {
DesignatorAST *ast = new (_pool) DesignatorAST;
ast->type = DesignatorAST::Dot;
ast->u.dot.dot_token = consumeToken();
match(T_IDENTIFIER, &ast->u.dot.identifier_token);
DotDesignatorAST *ast = new (_pool) DotDesignatorAST;
ast->dot_token = consumeToken();
match(T_IDENTIFIER, &ast->identifier_token);
node = ast;
return true;
} else if (LA() == T_LBRACKET) {
DesignatorAST *ast = new (_pool) DesignatorAST;
ast->type = DesignatorAST::Bracket;
ast->u.bracket.lbracket_token = consumeToken();
if (parseConditionalExpression(ast->u.bracket.expression)) {
match(T_RBRACKET, &ast->u.bracket.rbracket_token);
BracketDesignatorAST *ast = new (_pool) BracketDesignatorAST;
ast->lbracket_token = consumeToken();
if (parseConditionalExpression(ast->expression)) {
match(T_RBRACKET, &ast->rbracket_token);
node = ast;
return true;
}
......
......@@ -496,10 +496,9 @@ bool CheckSymbols::visit(EnumeratorAST *ast)
return true;
}
bool CheckSymbols::visit(DesignatorAST *ast)
bool CheckSymbols::visit(DotDesignatorAST *ast)
{
if (ast->type == DesignatorAST::Dot)
addUse(ast->u.dot.identifier_token, CppHighlightingSupport::FieldUse);
addUse(ast->identifier_token, CppHighlightingSupport::FieldUse);
return true;
}
......
......@@ -160,7 +160,7 @@ protected:
virtual bool visit(CPlusPlus::MemInitializerAST *ast);
virtual bool visit(CPlusPlus::EnumeratorAST *ast);
virtual bool visit(CPlusPlus::DesignatorAST *ast);
virtual bool visit(CPlusPlus::DotDesignatorAST *ast);
CPlusPlus::NameAST *declaratorId(CPlusPlus::DeclaratorAST *ast) const;
......
......@@ -1698,8 +1698,22 @@ virtual bool visit(BracedInitializerAST *ast)
return false;
}
virtual bool visit(DesignatorAST *ast)
virtual bool visit(DotDesignatorAST *ast)
{
if (ast->dot_token)
terminal(ast->dot_token, ast);
if (ast->identifier_token)
terminal(ast->identifier_token, ast);
return false;
}
virtual bool visit(BracketDesignatorAST *ast)
{
if (ast->lbracket_token)
terminal(ast->lbracket_token, ast);
nonterminal(ast->expression);
if (ast->rbracket_token)
terminal(ast->rbracket_token, ast);
return false;
}
......
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