From: Chris Lattner Date: Sat, 6 Oct 2007 22:53:46 +0000 (+0000) Subject: introduce a new NamedDecl class, switch a couple of things over to using it. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fd5de471478a507dd2495c4ea9bcab801ea5fa65;p=clang introduce a new NamedDecl class, switch a couple of things over to using it. NamedDecl is a Decl that has an IdentifierInfo (for example, ScopedDecl), but not ObjcMethodDecl. Simplify some code in ActOnAddMethodsToObjcDecl, by doing the cast from DeclTy to Decl at the start of the method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42710 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/Decl.cpp b/AST/Decl.cpp index f046e271c3..da7aab9bcc 100644 --- a/AST/Decl.cpp +++ b/AST/Decl.cpp @@ -214,13 +214,7 @@ void Decl::addDeclKind(const Kind k) { Decl::~Decl() { } -const char *FieldDecl::getName() const { - if (const IdentifierInfo *II = getIdentifier()) - return II->getName(); - return ""; -} - -const char *ScopedDecl::getName() const { +const char *NamedDecl::getName() const { if (const IdentifierInfo *II = getIdentifier()) return II->getName(); return ""; diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 71ba023dcf..41c04b9ff8 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -1061,9 +1061,9 @@ Sema::DeclTy *Sema::ActOnStartCategoryInterface(Scope* S, IdentifierInfo *ClassName, SourceLocation ClassLoc, IdentifierInfo *CategoryName, SourceLocation CategoryLoc, IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs) { - ObjcCategoryDecl *CDecl; - ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ClassName); - CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs); + ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); + ObjcCategoryDecl *CDecl = new ObjcCategoryDecl(AtInterfaceLoc, NumProtoRefs, + CategoryName); CDecl->setClassInterface(IDecl); /// Check that class of this category is already completely declared. @@ -1074,16 +1074,14 @@ Sema::DeclTy *Sema::ActOnStartCategoryInterface(Scope* S, ObjcCategoryDecl *CDeclChain; for (CDeclChain = IDecl->getListCategories(); CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) { - if (CDeclChain->getCatName() == CategoryName) { + if (CDeclChain->getIdentifier() == CategoryName) { Diag(CategoryLoc, diag::err_dup_category_def, ClassName->getName(), CategoryName->getName()); break; } } - if (!CDeclChain) { - CDecl->setCatName(CategoryName); + if (!CDeclChain) CDecl->insertNextClassCategory(); - } } /// Check then save referenced protocols @@ -1363,7 +1361,7 @@ void Sema::ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl, } if (IncompleteImpl) Diag(CatImplDecl->getLocation(), diag::warn_incomplete_impl_category, - CatClassDecl->getCatName()->getName()); + CatClassDecl->getName()); } /// ActOnForwardClassDeclaration - @@ -1723,8 +1721,10 @@ bool Sema:: MatchTwoMethodDeclarations(const ObjcMethodDecl *Method, return true; } -void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl, +void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl, DeclTy **allMethods, unsigned allNum) { + Decl *ClassDecl = static_cast(classDecl); + // FIXME: Fix this when we can handle methods declared in protocols. // See Parser::ParseObjCAtProtocolDeclaration if (!ClassDecl) @@ -1736,8 +1736,7 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl, llvm::DenseMap ClsMap; bool isClassDeclaration = - (isa(static_cast(ClassDecl)) - || isa(static_cast(ClassDecl))); + (isa(ClassDecl) || isa(ClassDecl)); for (unsigned i = 0; i < allNum; i++ ) { ObjcMethodDecl *Method = @@ -1782,54 +1781,40 @@ void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *ClassDecl, clsMethods.push_back(Method); } } - if (isa(static_cast(ClassDecl))) { - ObjcInterfaceDecl *Interface = cast( - static_cast(ClassDecl)); - Interface->ObjcAddMethods(&insMethods[0], insMethods.size(), - &clsMethods[0], clsMethods.size()); - } - else if (isa(static_cast(ClassDecl))) { - ObjcProtocolDecl *Protocol = cast( - static_cast(ClassDecl)); - Protocol->ObjcAddProtoMethods(&insMethods[0], insMethods.size(), - &clsMethods[0], clsMethods.size()); - } - else if (isa(static_cast(ClassDecl))) { - ObjcCategoryDecl *Category = cast( - static_cast(ClassDecl)); - Category->ObjcAddCatMethods(&insMethods[0], insMethods.size(), - &clsMethods[0], clsMethods.size()); - } - else if (isa(static_cast(ClassDecl))) { - ObjcImplementationDecl* ImplClass = cast( - static_cast(ClassDecl)); - ImplClass->ObjcAddImplMethods(&insMethods[0], insMethods.size(), - &clsMethods[0], clsMethods.size()); - ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(ImplClass->getIdentifier()); - if (IDecl) - ImplMethodsVsClassMethods(ImplClass, IDecl); + + if (ObjcInterfaceDecl *I = dyn_cast(ClassDecl)) { + I->ObjcAddMethods(&insMethods[0], insMethods.size(), + &clsMethods[0], clsMethods.size()); + } else if (ObjcProtocolDecl *P = dyn_cast(ClassDecl)) { + P->ObjcAddProtoMethods(&insMethods[0], insMethods.size(), + &clsMethods[0], clsMethods.size()); } - else { - ObjcCategoryImplDecl* CatImplClass = dyn_cast( - static_cast(ClassDecl)); - if (CatImplClass) { - CatImplClass->ObjcAddCatImplMethods(&insMethods[0], insMethods.size(), - &clsMethods[0], clsMethods.size()); - ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface(); - // Find category interface decl and then check that all methods declared - // in this interface is implemented in the category @implementation. - if (IDecl) { - for (ObjcCategoryDecl *Categories = IDecl->getListCategories(); - Categories; Categories = Categories->getNextClassCategory()) { - if (Categories->getCatName() == CatImplClass->getObjcCatName()) { - ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); - break; - } + else if (ObjcCategoryDecl *C = dyn_cast(ClassDecl)) { + C->ObjcAddCatMethods(&insMethods[0], insMethods.size(), + &clsMethods[0], clsMethods.size()); + } + else if (ObjcImplementationDecl *IC = + dyn_cast(ClassDecl)) { + IC->ObjcAddImplMethods(&insMethods[0], insMethods.size(), + &clsMethods[0], clsMethods.size()); + if (ObjcInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier())) + ImplMethodsVsClassMethods(IC, IDecl); + } else { + ObjcCategoryImplDecl* CatImplClass = cast(ClassDecl); + CatImplClass->ObjcAddCatImplMethods(&insMethods[0], insMethods.size(), + &clsMethods[0], clsMethods.size()); + ObjcInterfaceDecl* IDecl = CatImplClass->getClassInterface(); + // Find category interface decl and then check that all methods declared + // in this interface is implemented in the category @implementation. + if (IDecl) { + for (ObjcCategoryDecl *Categories = IDecl->getListCategories(); + Categories; Categories = Categories->getNextClassCategory()) { + if (Categories->getIdentifier() == CatImplClass->getObjcCatName()) { + ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); + break; } } } - else - assert(0 && "Sema::ActOnAddMethodsToObjcDecl(): Unknown DeclTy"); } } diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index fe2f4c2d89..5f45041a75 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -111,12 +111,25 @@ public: static bool classof(const Decl *) { return true; } }; -/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's -/// and TypeDecl's. -class ScopedDecl : public Decl { +class NamedDecl : public Decl { /// Identifier - The identifier for this declaration (e.g. the name for the /// variable, the tag for a struct). IdentifierInfo *Identifier; +public: + NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) + : Decl(DK, L), Identifier(Id) {} + + IdentifierInfo *getIdentifier() const { return Identifier; } + const char *getName() const; + + + // FIXME: classof. + static bool classof(const NamedDecl *D) { return true; } +}; + +/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's +/// and TypeDecl's. +class ScopedDecl : public NamedDecl { /// NextDeclarator - If this decl was part of a multi-declarator declaration, /// such as "int X, Y, *Z;" this indicates Decl for the next declarator. @@ -128,12 +141,9 @@ class ScopedDecl : public Decl { /// ScopedDecl *Next; protected: - ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) - : Decl(DK, L), Identifier(Id), NextDeclarator(PrevDecl), Next(0) {} + ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,ScopedDecl *PrevDecl) + : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0) {} public: - IdentifierInfo *getIdentifier() const { return Identifier; } - const char *getName() const; - ScopedDecl *getNext() const { return Next; } void setNext(ScopedDecl *N) { Next = N; } @@ -325,7 +335,7 @@ private: /// FieldDecl - An instance of this class is created by Sema::ActOnField to /// represent a member of a struct/union/class. -class FieldDecl : public Decl { +class FieldDecl : public NamedDecl { /// Identifier - The identifier for this declaration (e.g. the name for the /// variable, the tag for a struct). IdentifierInfo *Identifier; @@ -333,12 +343,9 @@ class FieldDecl : public Decl { QualType DeclType; public: FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T) - : Decl(Field, L), Identifier(Id), DeclType(T) {} + : NamedDecl(Field, L, Id), DeclType(T) {} FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T) - : Decl(DK, L), Identifier(Id), DeclType(T) {} - - IdentifierInfo *getIdentifier() const { return Identifier; } - const char *getName() const; + : NamedDecl(DK, L, Id), DeclType(T) {} QualType getType() const { return DeclType; } QualType getCanonicalType() const { return DeclType.getCanonicalType(); } diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h index 9c1222dc60..77aadcd8a2 100644 --- a/include/clang/AST/DeclObjC.h +++ b/include/clang/AST/DeclObjC.h @@ -33,7 +33,7 @@ class ObjcCategoryDecl; /// // no instance variables or methods. /// @end /// -/// // NSResponder inherits from NSObject and implements NSCoding (a protocol). +/// // NSResponder inherits from NSObject & implements NSCoding (a protocol). /// @interface NSResponder : NSObject /// { // instance variables are represented by ObjcIvarDecl. /// id nextResponder; // nextResponder instance variable. @@ -429,13 +429,10 @@ public: /// Lisp and Smalltalk. More traditional class-based languages (C++, Java) /// don't support this level of dynamism, which is both powerful and dangerous. /// -class ObjcCategoryDecl : public Decl { +class ObjcCategoryDecl : public NamedDecl { /// Interface belonging to this category ObjcInterfaceDecl *ClassInterface; - /// Category name - IdentifierInfo *ObjcCatName; - /// referenced protocols in this category ObjcProtocolDecl **ReferencedProtocols; // Null if none int NumReferencedProtocols; // -1 if none @@ -452,10 +449,9 @@ class ObjcCategoryDecl : public Decl { ObjcCategoryDecl *NextClassCategory; public: - ObjcCategoryDecl(SourceLocation L, unsigned numRefProtocol) - : Decl(ObjcCategory, L), - ClassInterface(0), ObjcCatName(0), - ReferencedProtocols(0), NumReferencedProtocols(-1), + ObjcCategoryDecl(SourceLocation L, unsigned numRefProtocol,IdentifierInfo *Id) + : NamedDecl(ObjcCategory, L, Id), + ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(-1), InstanceMethods(0), NumInstanceMethods(-1), ClassMethods(0), NumClassMethods(-1), NextClassCategory(0) { @@ -489,9 +485,6 @@ public: void ObjcAddCatMethods(ObjcMethodDecl **insMethods, unsigned numInsMembers, ObjcMethodDecl **clsMethods, unsigned numClsMembers); - IdentifierInfo *getCatName() const { return ObjcCatName; } - void setCatName(IdentifierInfo *catName) { ObjcCatName = catName; } - ObjcCategoryDecl *getNextClassCategory() const { return NextClassCategory; } void insertNextClassCategory() { NextClassCategory = ClassInterface->getListCategories();