[PATCH 1/5] changes to: the parser, the ast, and the default visitor to enable pointer to member typs
Floris Ruijter
flo.ruijt at hotmail.com
Sat Feb 5 04:14:00 UTC 2011
includes a test too
---
languages/cpp/parser/ast.h | 2 +
languages/cpp/parser/default_visitor.cpp | 4 +-
languages/cpp/parser/parser.cpp | 50 ++++++++++------------------
languages/cpp/parser/tests/test_parser.cpp | 25 ++++++++++++++
4 files changed, 47 insertions(+), 34 deletions(-)
diff --git a/languages/cpp/parser/ast.h b/languages/cpp/parser/ast.h
index cd896f7..9369ff7 100644
--- a/languages/cpp/parser/ast.h
+++ b/languages/cpp/parser/ast.h
@@ -833,6 +833,8 @@ class PtrToMemberAST : public AST
public:
DECLARE_AST_NODE(PtrToMember)
+
+ TypeSpecifierAST *class_type;
};
class JumpStatementAST : public StatementAST
diff --git a/languages/cpp/parser/default_visitor.cpp b/languages/cpp/parser/default_visitor.cpp
index 3d061ff..73c66ce 100644
--- a/languages/cpp/parser/default_visitor.cpp
+++ b/languages/cpp/parser/default_visitor.cpp
@@ -320,9 +320,9 @@ void DefaultVisitor::visitPtrOperator(PtrOperatorAST *node)
visit(node->mem_ptr);
}
-void DefaultVisitor::visitPtrToMember(PtrToMemberAST *)
+void DefaultVisitor::visitPtrToMember(PtrToMemberAST *node)
{
- // nothing to do
+ visit(node->class_type);
}
void DefaultVisitor::visitReturnStatement(ReturnStatementAST *node)
diff --git a/languages/cpp/parser/parser.cpp b/languages/cpp/parser/parser.cpp
index b07910a..c38527f 100644
--- a/languages/cpp/parser/parser.cpp
+++ b/languages/cpp/parser/parser.cpp
@@ -561,8 +561,11 @@ bool Parser::parseName(NameAST*& node, ParseNameAcceptTemplate acceptTemplateId)
if (!ast)
ast = CreateNode<NameAST>(session->mempool);
- if (session->token_stream->lookAhead() == Token_scope)
+ if (session->token_stream->lookAhead() == Token_scope &&
+ //ptr-to-member
+ session->token_stream->lookAhead(1) != '*')
{
+
advance();
ast->qualified_names
@@ -2637,42 +2640,25 @@ bool Parser::parseInitializerClause(InitializerClauseAST *&node)
bool Parser::parsePtrToMember(PtrToMemberAST *&node)
{
-#if defined(__GNUC__)
-#warning "implemente me (AST)"
-#endif
-
uint start = session->token_stream->cursor();
- uint global_scope = 0;
- if (session->token_stream->lookAhead() == Token_scope)
+ TypeSpecifierAST* type_ast = 0;
+
+ if(parseTypeSpecifier(type_ast)){
+ if (session->token_stream->lookAhead() == Token_scope
+ && session->token_stream->lookAhead(1) == '*')
{
- global_scope = session->token_stream->cursor();
advance();
+ advance();
+
+ PtrToMemberAST *ast = CreateNode<PtrToMemberAST>(session->mempool);
+ ast->class_type=type_ast;
+ UPDATE_POS(ast, start, _M_last_valid_token+1);
+ node = ast;
+ return true;
}
-
- UnqualifiedNameAST *name = 0;
- while (session->token_stream->lookAhead() == Token_identifier)
- {
- if (!parseUnqualifiedName(name))
- break;
-
- if (session->token_stream->lookAhead() == Token_scope
- && session->token_stream->lookAhead(1) == '*')
- {
- advance();
- advance();
-
- PtrToMemberAST *ast = CreateNode<PtrToMemberAST>(session->mempool);
- UPDATE_POS(ast, start, _M_last_valid_token+1);
- node = ast;
-
- return true;
- }
-
- if (session->token_stream->lookAhead() == Token_scope)
- advance();
- }
-
+ }
+
rewind(start);
return false;
}
diff --git a/languages/cpp/parser/tests/test_parser.cpp b/languages/cpp/parser/tests/test_parser.cpp
index 2e683e4..06c95fd 100644
--- a/languages/cpp/parser/tests/test_parser.cpp
+++ b/languages/cpp/parser/tests/test_parser.cpp
@@ -684,6 +684,31 @@ private slots:
QVERIFY(hasKind(funcAst, AST::Kind_FunctionCall));
QVERIFY(hasKind(funcAst, AST::Kind_InitDeclarator));
}
+
+ void testPtrToMemberAst(){
+ pool memPool;
+ TranslationUnitAST* ast = parse("\nstruct AA {"
+ "\n int j;"
+ "\n};"
+ "\nstruct BB{"
+ "\n int AA::* pj;"
+ "\n};"
+ "\nvoid f(){"
+ "\n int AA::* BB::* ppj=&BB::pj;"
+ "\n}"
+ , &memPool);
+ QVERIFY(ast!=0);
+ QCOMPARE(ast->declarations->count(), 3);
+ QVERIFY(hasKind(ast,AST::Kind_PtrToMember));
+ FunctionDefinitionAST* f_ast=static_cast<FunctionDefinitionAST*>(getAST(ast,AST::Kind_FunctionDefinition));
+ QVERIFY(hasKind(f_ast,AST::Kind_PtrToMember));
+ DeclaratorAST* d_ast=static_cast<DeclaratorAST*>(getAST(f_ast->function_body,AST::Kind_Declarator));
+ QCOMPARE(d_ast-> ptr_ops->count(),2);
+
+ DumpTree dumper;
+ dumper.dump(ast, lastSession->token_stream);
+
+ }
private:
ParseSession* lastSession;
--
1.7.2.3
--=-jnTU9w3TPwGq4NfAnuZF
Content-Disposition: attachment; filename*0=0002-make-the-typebuilder-aware-of-pointer-to-membertypes.pat; filename*1=ch
Content-Type: text/x-patch; name="0002-make-the-typebuilder-aware-of-pointer-to-membertypes.patch"; charset="UTF-8"
Content-Transfer-Encoding: 7bit
More information about the KDevelop-devel
mailing list