Commit ecd54059 authored by Christian Kamm's avatar Christian Kamm Committed by hjk
Browse files

C++11: Add alignof() expression.



Change-Id: Id3fb30b9a16ea724bab0d5b05e8cbddb0064e6eb
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent 1aed32d8
......@@ -4407,3 +4407,33 @@ unsigned RangeBasedForStatementAST::lastToken() const
return 1;
}
/** \generated */
unsigned AlignofExpressionAST::firstToken() const
{
if (alignof_token)
return alignof_token;
if (lparen_token)
return lparen_token;
if (typeId)
if (unsigned candidate = typeId->firstToken())
return candidate;
if (rparen_token)
return rparen_token;
return 0;
}
/** \generated */
unsigned AlignofExpressionAST::lastToken() const
{
if (rparen_token)
return rparen_token + 1;
if (typeId)
if (unsigned candidate = typeId->lastToken())
return candidate;
if (lparen_token)
return lparen_token + 1;
if (alignof_token)
return alignof_token + 1;
return 1;
}
......@@ -125,6 +125,7 @@ public:
virtual AST *clone(MemoryPool *pool) const = 0;
virtual AccessDeclarationAST *asAccessDeclaration() { return 0; }
virtual AlignofExpressionAST *asAlignofExpression() { return 0; }
virtual ArrayAccessAST *asArrayAccess() { return 0; }
virtual ArrayDeclaratorAST *asArrayDeclarator() { return 0; }
virtual ArrayInitializerAST *asArrayInitializer() { return 0; }
......@@ -3026,6 +3027,34 @@ protected:
virtual bool match0(AST *, ASTMatcher *);
};
class CPLUSPLUS_EXPORT AlignofExpressionAST: public ExpressionAST
{
public:
unsigned alignof_token;
unsigned lparen_token;
TypeIdAST *typeId;
unsigned rparen_token;
public:
AlignofExpressionAST()
: alignof_token(0)
, lparen_token(0)
, typeId(0)
, rparen_token(0)
{}
virtual AlignofExpressionAST *asAlignofExpression() { return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual AlignofExpressionAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
virtual bool match0(AST *, ASTMatcher *);
};
class CPLUSPLUS_EXPORT PointerLiteralAST: public ExpressionAST
{
public:
......
......@@ -1137,6 +1137,17 @@ SizeofExpressionAST *SizeofExpressionAST::clone(MemoryPool *pool) const
return ast;
}
AlignofExpressionAST *AlignofExpressionAST::clone(MemoryPool *pool) const
{
AlignofExpressionAST *ast = new (pool) AlignofExpressionAST;
ast->alignof_token = alignof_token;
ast->lparen_token = lparen_token;
if (typeId)
ast->typeId = typeId->clone(pool);
ast->rparen_token = rparen_token;
return ast;
}
PointerLiteralAST *PointerLiteralAST::clone(MemoryPool *pool) const
{
PointerLiteralAST *ast = new (pool) PointerLiteralAST;
......
......@@ -776,6 +776,14 @@ bool SizeofExpressionAST::match0(AST *pattern, ASTMatcher *matcher)
return false;
}
bool AlignofExpressionAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (AlignofExpressionAST *_other = pattern->asAlignofExpression())
return matcher->match(this, _other);
return false;
}
bool PointerLiteralAST::match0(AST *pattern, ASTMatcher *matcher)
{
if (PointerLiteralAST *_other = pattern->asPointerLiteral())
......
......@@ -1925,6 +1925,25 @@ bool ASTMatcher::match(SizeofExpressionAST *node, SizeofExpressionAST *pattern)
return true;
}
bool ASTMatcher::match(AlignofExpressionAST *node, AlignofExpressionAST *pattern)
{
(void) node;
(void) pattern;
pattern->alignof_token = node->alignof_token;
pattern->lparen_token = node->lparen_token;
if (! pattern->typeId)
pattern->typeId = node->typeId;
else if (! AST::match(node->typeId, pattern->typeId, this))
return false;
pattern->rparen_token = node->rparen_token;
return true;
}
bool ASTMatcher::match(PointerLiteralAST *node, PointerLiteralAST *pattern)
{
(void) node;
......
......@@ -32,6 +32,7 @@ public:
virtual ~ASTMatcher();
virtual bool match(AccessDeclarationAST *node, AccessDeclarationAST *pattern);
virtual bool match(AlignofExpressionAST *node, AlignofExpressionAST *pattern);
virtual bool match(ArrayAccessAST *node, ArrayAccessAST *pattern);
virtual bool match(ArrayDeclaratorAST *node, ArrayDeclaratorAST *pattern);
virtual bool match(ArrayInitializerAST *node, ArrayInitializerAST *pattern);
......
......@@ -747,6 +747,13 @@ public:
return __ast;
}
AlignofExpressionAST *AlignofExpression(TypeIdAST *typeId = 0)
{
AlignofExpressionAST *__ast = new (&pool) AlignofExpressionAST;
__ast->typeId = typeId;
return __ast;
}
PointerLiteralAST *PointerLiteral()
{
PointerLiteralAST *__ast = new (&pool) PointerLiteralAST;
......
......@@ -828,6 +828,14 @@ void SizeofExpressionAST::accept0(ASTVisitor *visitor)
visitor->endVisit(this);
}
void AlignofExpressionAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
accept(typeId, visitor);
}
visitor->endVisit(this);
}
void PointerLiteralAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
......
......@@ -74,6 +74,7 @@ public:
virtual void postVisit(AST *) {}
virtual bool visit(AccessDeclarationAST *) { return true; }
virtual bool visit(AlignofExpressionAST *) { return true; }
virtual bool visit(ArrayAccessAST *) { return true; }
virtual bool visit(ArrayDeclaratorAST *) { return true; }
virtual bool visit(ArrayInitializerAST *) { return true; }
......@@ -218,6 +219,7 @@ public:
virtual bool visit(WhileStatementAST *) { return true; }
virtual void endVisit(AccessDeclarationAST *) {}
virtual void endVisit(AlignofExpressionAST *) {}
virtual void endVisit(ArrayAccessAST *) {}
virtual void endVisit(ArrayDeclaratorAST *) {}
virtual void endVisit(ArrayInitializerAST *) {}
......
......@@ -32,6 +32,7 @@ class ASTVisitor;
class ASTMatcher;
class AccessDeclarationAST;
class AlignofExpressionAST;
class ArrayAccessAST;
class ArrayDeclaratorAST;
class ArrayInitializerAST;
......
......@@ -4860,6 +4860,24 @@ bool Parser::parseUnaryExpression(ExpressionAST *&node)
return true;
}
case T_ALIGNOF: {
if (!_cxx0xEnabled)
break;
AlignofExpressionAST *ast = new (_pool) AlignofExpressionAST;
ast->alignof_token = consumeToken();
match(T_LPAREN, &ast->lparen_token);
ExpressionAST *temp = 0;
parseTypeId(temp);
if (temp)
ast->typeId = temp->asTypeId();
match(T_RPAREN, &ast->rparen_token);
node = ast;
return true;
}
default:
break;
} // switch
......
int i = alignof(int);
int t = alignof(C::foo) * 7 + alignof(Foo *);
......@@ -140,6 +140,7 @@ void tst_cxx11::parse_data()
QTest::newRow("braceInitializers.2") << "braceInitializers.2.cpp" << "";
QTest::newRow("defaultdeleteInitializer.1") << "defaultdeleteInitializer.1.cpp" << "";
QTest::newRow("refQualifier.1") << "refQualifier.1.cpp" << "";
QTest::newRow("alignofAlignas.1") << "alignofAlignas.1.cpp" << "";
}
void tst_cxx11::parse()
......
......@@ -1104,6 +1104,18 @@ virtual bool visit(SizeofExpressionAST *ast)
return false;
}
virtual bool visit(AlignofExpressionAST *ast)
{
if (ast->alignof_token)
terminal(ast->alignof_token, ast);
if (ast->lparen_token)
terminal(ast->lparen_token, ast);
nonterminal(ast->typeId);
if (ast->rparen_token)
terminal(ast->rparen_token, ast);
return false;
}
virtual bool visit(PointerLiteralAST *ast)
{
if (ast->literal_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