Added ObjC method prototypes to the AST.

This commit is contained in:
Erik Verbruggen
2009-07-16 17:38:24 +02:00
parent 01ee7a95c9
commit a3d129527f
8 changed files with 233 additions and 28 deletions

View File

@@ -2258,4 +2258,62 @@ unsigned ObjCPropertyDeclarationAST::lastToken() const
return property_token + 1;
}
unsigned ObjCMessageArgumentDeclarationAST::firstToken() const
{
return param_selector_token;
}
unsigned ObjCMessageArgumentDeclarationAST::lastToken() const
{
if (param_name_token)
return param_name_token + 1;
else if (colon_token)
return colon_token + 1;
else if (type_name)
return type_name->lastToken();
else if (attributes)
return attributes->lastToken();
else
return param_name_token + 1;
}
unsigned ObjCMessageArgumentDeclarationListAST::firstToken() const
{
if (argument_declaration)
return argument_declaration->firstToken();
else if (next)
return next->firstToken();
else
// ### Assert?
return 0;
}
unsigned ObjCMessageArgumentDeclarationListAST::lastToken() const
{
for (const ObjCMessageArgumentDeclarationListAST *it = this; it; it = it->next) {
if (! it->next && it->argument_declaration) {
return it->argument_declaration->lastToken();
}
}
// ### assert?
return 0;
}
unsigned ObjCMethodPrototypeAST::firstToken() const
{
return method_type_token;
}
unsigned ObjCMethodPrototypeAST::lastToken() const
{
if (attributes)
return attributes->lastToken();
else if (arguments)
return arguments->lastToken();
else if (type_name)
return type_name->lastToken();
else
return method_type_token + 1;
}
CPLUSPLUS_END_NAMESPACE

View File

@@ -2820,6 +2820,68 @@ protected:
virtual void accept0(ASTVisitor *visitor);
};
class CPLUSPLUS_EXPORT ObjCMessageArgumentDeclarationAST: public AST
{
public:
unsigned param_selector_token;
unsigned colon_token;
ObjCTypeNameAST* type_name;
SpecifierAST *attributes;
unsigned param_name_token;
public:
virtual ObjCMessageArgumentDeclarationAST *asObjCMessageArgumentDeclaration()
{ return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual ObjCMessageArgumentDeclarationAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
};
class CPLUSPLUS_EXPORT ObjCMessageArgumentDeclarationListAST: public AST
{
public:
ObjCMessageArgumentDeclarationAST *argument_declaration;
ObjCMessageArgumentDeclarationListAST *next;
public:
virtual ObjCMessageArgumentDeclarationListAST *asObjCMessageArgumentDeclarationList()
{ return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual ObjCMessageArgumentDeclarationListAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
};
class CPLUSPLUS_EXPORT ObjCMethodPrototypeAST: public DeclarationAST
{
public:
unsigned method_type_token;
ObjCTypeNameAST *type_name;
ObjCMessageArgumentDeclarationListAST *arguments;
SpecifierAST *attributes;
public:
virtual ObjCMethodPrototypeAST *asObjCMethodPrototype()
{ return this; }
virtual unsigned firstToken() const;
virtual unsigned lastToken() const;
virtual ObjCMethodPrototypeAST *clone(MemoryPool *pool) const;
protected:
virtual void accept0(ASTVisitor *visitor);
};
CPLUSPLUS_END_NAMESPACE
CPLUSPLUS_END_HEADER

View File

@@ -1390,4 +1390,33 @@ ObjCPropertyDeclarationAST *ObjCPropertyDeclarationAST::clone(MemoryPool *pool)
return ast;
}
ObjCMessageArgumentDeclarationAST *ObjCMessageArgumentDeclarationAST::clone(MemoryPool *pool) const
{
ObjCMessageArgumentDeclarationAST *ast = new (pool) ObjCMessageArgumentDeclarationAST;
ast->param_selector_token = param_selector_token;
ast->colon_token = colon_token;
if (type_name) ast->type_name = type_name->clone(pool);
if (attributes) ast->attributes = attributes->clone(pool);
ast->param_name_token = param_name_token;
return ast;
}
ObjCMessageArgumentDeclarationListAST *ObjCMessageArgumentDeclarationListAST::clone(MemoryPool *pool) const
{
ObjCMessageArgumentDeclarationListAST *ast = new (pool) ObjCMessageArgumentDeclarationListAST;
if (argument_declaration) ast->argument_declaration = argument_declaration->clone(pool);
if (next) ast->next = next->clone(pool);
return ast;
}
ObjCMethodPrototypeAST *ObjCMethodPrototypeAST::clone(MemoryPool *pool) const
{
ObjCMethodPrototypeAST *ast = new (pool) ObjCMethodPrototypeAST;
ast->method_type_token = method_type_token;
if (type_name) ast->type_name = type_name->clone(pool);
if (arguments) ast->arguments = arguments->clone(pool);
if (attributes) ast->attributes = attributes->clone(pool);
return ast;
}
CPLUSPLUS_END_NAMESPACE

View File

@@ -1309,8 +1309,6 @@ void ObjcPropertyAttributeListAST::accept0(ASTVisitor *visitor)
// visit ObjcPropertyAttributeListAST
if (attr)
accept(attr, visitor);
if (next)
accept(next, visitor);
// visit AST
}
visitor->endVisit(this);
@@ -1320,8 +1318,8 @@ void ObjCPropertyDeclarationAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
// visit ObjCPropertyDeclarationAST:
if (property_attributes)
accept(property_attributes, visitor);
for (ObjcPropertyAttributeListAST *it = property_attributes; it; it = it->next)
accept(it, visitor);
if (simple_declaration)
accept(simple_declaration, visitor);
// visit DeclarationAST:
@@ -1329,4 +1327,43 @@ void ObjCPropertyDeclarationAST::accept0(ASTVisitor *visitor)
visitor->endVisit(this);
}
void ObjCMessageArgumentDeclarationAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
// visit ObjCMessageArgumentDeclarationAST
if (type_name)
accept(type_name, visitor);
if (attributes)
accept(attributes, visitor);
// visit AST
}
visitor->endVisit(this);
}
void ObjCMessageArgumentDeclarationListAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
// visit ObjCMessageArgumentDeclarationListAST
if (argument_declaration)
accept(argument_declaration, visitor);
// visit AST
}
visitor->endVisit(this);
}
void ObjCMethodPrototypeAST::accept0(ASTVisitor *visitor)
{
if (visitor->visit(this)) {
// visit ObjCMethodPrototypeAST
if (type_name)
accept(type_name, visitor);
for (ObjCMessageArgumentDeclarationListAST *it = arguments; it; it = it->next)
accept(it, visitor);
if (attributes)
accept(attributes, visitor);
// visit AST
}
visitor->endVisit(this);
}
CPLUSPLUS_END_NAMESPACE

View File

@@ -214,6 +214,9 @@ public:
virtual bool visit(ObjcPropertyAttributeAST *) { return true; }
virtual bool visit(ObjcPropertyAttributeListAST *) { return true; }
virtual bool visit(ObjCPropertyDeclarationAST *) { return true; }
virtual bool visit(ObjCMethodPrototypeAST *) { return true; }
virtual bool visit(ObjCMessageArgumentDeclarationListAST *) { return true; }
virtual bool visit(ObjCMessageArgumentDeclarationAST *) { return true; }
virtual bool visit(DeclarationListAST *) { return true; }
virtual void endVisit(DeclarationListAST *) { }
@@ -339,6 +342,9 @@ public:
virtual void endVisit(ObjcPropertyAttributeAST *) { }
virtual void endVisit(ObjcPropertyAttributeListAST *) { }
virtual void endVisit(ObjCPropertyDeclarationAST *) { }
virtual void endVisit(ObjCMethodPrototypeAST *) { }
virtual void endVisit(ObjCMessageArgumentDeclarationListAST *) { }
virtual void endVisit(ObjCMessageArgumentDeclarationAST *) { }
private:
Control *_control;

View File

@@ -187,6 +187,9 @@ class ObjCVisibilityDeclarationAST;
class ObjCPropertyDeclarationAST;
class ObjcPropertyAttributeListAST;
class ObjcPropertyAttributeAST;
class ObjCMethodPrototypeAST;
class ObjCMessageArgumentDeclarationListAST;
class ObjCMessageArgumentDeclarationAST;
CPLUSPLUS_END_NAMESPACE
CPLUSPLUS_END_HEADER

View File

@@ -4195,7 +4195,8 @@ bool Parser::parseObjCMethodDefinitionList()
bool Parser::parseObjCMethodDefinition()
{
if (! parseObjCMethodPrototype())
DeclarationAST *ast = 0;
if (! parseObjCMethodPrototype(ast))
return false;
if (LA() == T_SEMICOLON)
@@ -4298,7 +4299,7 @@ bool Parser::parseObjCInterfaceMemberDeclaration(DeclarationAST *&node)
case T_PLUS:
case T_MINUS:
return parseObjCMethodPrototype();
return parseObjCMethodPrototype(node);
case T_ENUM:
case T_CLASS:
@@ -4377,22 +4378,31 @@ bool Parser::parseObjCPropertyDeclaration(DeclarationAST *&node, SpecifierAST *)
// objc-method-decl ::= objc-type-name? objc-selector
// objc-method-decl ::= objc-type-name? objc-keyword-decl-list objc-parmlist-opt
//
bool Parser::parseObjCMethodPrototype()
bool Parser::parseObjCMethodPrototype(DeclarationAST *&node)
{
if (LA() != T_PLUS && LA() != T_MINUS)
return false;
/*unsigned method_type_token = */ consumeToken();
ObjCMethodPrototypeAST *ast = new (_pool) ObjCMethodPrototypeAST;
ast->method_type_token = consumeToken();
ObjCTypeNameAST *type_name = 0;
parseObjCTypeName(type_name);
unsigned selector_token = 0;
parseObjCTypeName(ast->type_name);
if ((lookAtObjCSelector() && LA(2) == T_COLON) || LA() == T_COLON) {
while (parseObjCKeywordDeclaration()) {
ObjCMessageArgumentDeclarationAST *declaration = 0;
parseObjCKeywordDeclaration(declaration);
ast->arguments = new (_pool) ObjCMessageArgumentDeclarationListAST;
ast->arguments->argument_declaration = declaration;
ObjCMessageArgumentDeclarationListAST **last = &(ast->arguments);
while (parseObjCKeywordDeclaration(declaration)) {
(*last)->next = new (_pool) ObjCMessageArgumentDeclarationListAST;
last = &((*last)->next);
(*last)->argument_declaration = declaration;
}
// TODO: err, what does this parse?
while (LA() == T_COMMA) {
consumeToken();
@@ -4405,15 +4415,18 @@ bool Parser::parseObjCMethodPrototype()
parseParameterDeclaration(parameter_declaration);
}
} else if (lookAtObjCSelector()) {
parseObjCSelector(selector_token);
ast->arguments = new (_pool) ObjCMessageArgumentDeclarationListAST;
ast->arguments->argument_declaration = new (_pool) ObjCMessageArgumentDeclarationAST;
parseObjCSelector(ast->arguments->argument_declaration->param_selector_token);
} else {
_translationUnit->error(cursor(), "expected a selector");
}
SpecifierAST *attributes = 0, **attr = &attributes;
SpecifierAST **attr = &(ast->attributes);
while (parseAttributeSpecifier(*attr))
attr = &(*attr)->next;
node = ast;
return true;
}
@@ -4471,27 +4484,24 @@ bool Parser::parseObjCSelector(unsigned &selector_token)
// objc-keyword-decl ::= objc-selector? T_COLON objc-type-name? objc-keyword-attributes-opt T_IDENTIFIER
//
bool Parser::parseObjCKeywordDeclaration()
bool Parser::parseObjCKeywordDeclaration(ObjCMessageArgumentDeclarationAST *&node)
{
if (! (LA() == T_COLON || (lookAtObjCSelector() && LA(2) == T_COLON)))
return false;
unsigned selector_token = 0;
parseObjCSelector(selector_token);
ObjCMessageArgumentDeclarationAST *ast = new (_pool) ObjCMessageArgumentDeclarationAST;
unsigned colon_token = 0;
match(T_COLON, &colon_token);
parseObjCSelector(ast->param_selector_token);
match(T_COLON, &(ast->colon_token));
parseObjCTypeName(ast->type_name);
ObjCTypeNameAST *type_name = 0;
parseObjCTypeName(type_name);
SpecifierAST *attributes = 0, **attr = &attributes;
SpecifierAST **attr = &(ast->attributes);
while (parseAttributeSpecifier(*attr))
attr = &(*attr)->next;
unsigned identifier_token = 0;
match(T_IDENTIFIER, &identifier_token);
match(T_IDENTIFIER, &(ast->param_name_token));
node = ast;
return true;
}

View File

@@ -238,11 +238,11 @@ public:
bool parseObjCPropertyDeclaration(DeclarationAST *&node,
SpecifierAST *attributes = 0);
bool parseObjCImplementation(DeclarationAST *&node);
bool parseObjCMethodPrototype();
bool parseObjCMethodPrototype(DeclarationAST *&node);
bool parseObjCPropertyAttribute(ObjcPropertyAttributeAST *&node);
bool parseObjCTypeName(ObjCTypeNameAST *&node);
bool parseObjCSelector(unsigned &selector_token);
bool parseObjCKeywordDeclaration();
bool parseObjCKeywordDeclaration(ObjCMessageArgumentDeclarationAST *&node);
bool parseObjCTypeQualifiers(unsigned &type_qualifier);
bool parseObjCEnd(DeclarationAST *&node);