From: Steve Naroff Date: Mon, 12 Nov 2007 13:56:41 +0000 (+0000) Subject: - Minor cleanup to yesterday's changes to Sema::ObjcActOnStartOfMethodDef(); X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0330071714f1ba09e926becd666f4fc0ed62bc0b;p=clang - Minor cleanup to yesterday's changes to Sema::ObjcActOnStartOfMethodDef(); - Add Sema::CurMethodDecl, in preparation for adding ObjcIvarRefExpr. - Add ObjcInterfaceDecl::lookupInstanceVariable(), in prep for adding ivars. - A couple renames in ObjcInterfaceDecl, while I was in the vicinity:-) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44015 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/Decl.cpp b/AST/Decl.cpp index 584fbf5a63..b09960eab1 100644 --- a/AST/Decl.cpp +++ b/AST/Decl.cpp @@ -408,6 +408,23 @@ void ObjcCategoryImplDecl::addMethods(ObjcMethodDecl **insMethods, } } +ObjcIvarDecl *ObjcInterfaceDecl::lookupInstanceVariable( + IdentifierInfo *ID, ObjcInterfaceDecl *&clsDeclared) { + ObjcInterfaceDecl* ClassDecl = this; + while (ClassDecl != NULL) { + ObjcIvarDecl **ivars = ClassDecl->getInstanceVariables(); + int ivarCount = ClassDecl->getNumInstanceVariables(); + for (int i = 0; i < ivarCount; ++i) { + if (ivars[i]->getIdentifier() == ID) { + clsDeclared = ClassDecl; + return ivars[i]; + } + } + ClassDecl = ClassDecl->getSuperClass(); + } + return NULL; +} + // lookupInstanceMethod - This method returns an instance method by looking in // the class, it's categories, and it's super classes (using a linear search). ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) { diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 8b68f2489b..e47946b916 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -147,9 +147,9 @@ static void PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) { else fprintf(stderr, "\n"); - int NumIvars = OID->getIntfDeclNumIvars(); + int NumIvars = OID->getNumInstanceVariables(); if (NumIvars > 0) { - ObjcIvarDecl **Ivars = OID->getIntfDeclIvars(); + ObjcIvarDecl **Ivars = OID->getInstanceVariables(); fprintf(stderr,"{"); for (int i = 0; i < NumIvars; i++) { fprintf(stderr, "\t%s %s;\n", Ivars[i]->getType().getAsString().c_str(), diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index a0a11470dc..66f8aa8fc3 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -981,7 +981,7 @@ void RewriteTest::SynthesizeObjcInternalStruct(ObjcInterfaceDecl *CDecl, SynthesizeObjcInternalStruct(RCDecl, Result); } - int NumIvars = CDecl->getIntfDeclNumIvars(); + int NumIvars = CDecl->getNumInstanceVariables(); // If no ivars and no root or if its root, directly or indirectly, // have no ivars (thus not synthesized) then no need to synthesize this class. if (NumIvars <= 0 && (!RCDecl || !ObjcSynthesizedStructs.count(RCDecl))) @@ -1391,7 +1391,7 @@ void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl, // Build _objc_ivar_list metadata for classes ivars if needed int NumIvars = IDecl->getImplDeclNumIvars() > 0 ? IDecl->getImplDeclNumIvars() - : (CDecl ? CDecl->getIntfDeclNumIvars() : 0); + : (CDecl ? CDecl->getNumInstanceVariables() : 0); SynthesizeObjcInternalStruct(CDecl, Result); @@ -1430,7 +1430,7 @@ void RewriteTest::RewriteObjcClassMetaData(ObjcImplementationDecl *IDecl, ObjcIvarDecl **Ivars = IDecl->getImplDeclIVars() ? IDecl->getImplDeclIVars() - : CDecl->getIntfDeclIvars(); + : CDecl->getInstanceVariables(); Result += "\t,{{\""; Result += Ivars[0]->getName(); Result += "\", \""; diff --git a/Sema/Sema.h b/Sema/Sema.h index 395adbc312..4c66e10301 100644 --- a/Sema/Sema.h +++ b/Sema/Sema.h @@ -64,6 +64,10 @@ class Sema : public Action { /// CurFunctionDecl - If inside of a function body, this contains a pointer to /// the function decl for the function being parsed. FunctionDecl *CurFunctionDecl; + + /// CurMethodDecl - If inside of a method body, this contains a pointer to + /// the method decl for the method being parsed. + ObjcMethodDecl *CurMethodDecl; /// LastInGroupList - This vector is populated when there are multiple /// declarators in a single decl group (e.g. "int A, B, C"). In this case, diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 05a9f6130d..62089141cb 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -912,7 +912,9 @@ Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { assert(FD == CurFunctionDecl && "Function parsing confused"); } else if (ObjcMethodDecl *MD = dyn_cast(dcl)) { MD->setBody((Stmt*)Body); + CurMethodDecl = 0; } + // This is unconditional, since methods have a corresponding function decl. CurFunctionDecl = 0; // Verify and clean out per-function state. @@ -943,9 +945,8 @@ Sema::DeclTy *Sema::ActOnFinishFunctionBody(DeclTy *D, StmtTy *Body) { /// ObjcActOnStartOfMethodDef - This routine sets up parameters; invisible /// and user declared, in the method definition's AST. void Sema::ObjcActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { - assert(CurFunctionDecl == 0 && "Function parsing confused"); + assert(CurFunctionDecl == 0 && "Method parsing confused"); ObjcMethodDecl *MDecl = dyn_cast(static_cast(D)); - assert(MDecl != 0 && "Not a method declarator!"); Scope *GlobalScope = FnBodyScope->getParent(); @@ -957,10 +958,10 @@ void Sema::ObjcActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { Name += MDecl->getSelector().getName(); Name += "]"; IdentifierInfo *II = &Context.Idents.get(Name); - assert (II && "ObjcActOnMethodDefinition - selector name is missing"); + assert (II && "ObjcActOnStartOfMethodDef - selector name is missing"); QualType R = ObjcGetTypeForMethodDefinition(MDecl, GlobalScope); - assert(!R.isNull() && "ObjcGetTypeForMethodDefinition() returned null type"); + assert(!R.isNull() && "ObjcActOnStartOfMethodDef() returned null type"); FunctionDecl *NewFD = new FunctionDecl(MDecl->getLocation(), II, R, FunctionDecl::Static, false, 0); @@ -968,34 +969,34 @@ void Sema::ObjcActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { II->setFETokenInfo(NewFD); GlobalScope->AddDecl(NewFD); AddTopLevelDecl(NewFD, 0); + + // Allow all of Sema to see that we are entering a method definition. + CurMethodDecl = MDecl; CurFunctionDecl = NewFD; // Create Decl objects for each parameter, adding them to the FunctionDecl. llvm::SmallVector Params; struct DeclaratorChunk::ParamInfo PI; + // Insert the invisible arguments, self and _cmd! PI.Ident = &Context.Idents.get("self"); - PI.IdentLoc = SourceLocation(/*FIXME*/); - - // Insert the invisible arguments! + PI.IdentLoc = SourceLocation(); // synthesized vars have a null location. if (MDecl->isInstance()) { QualType selfTy = Context.getObjcInterfaceType(MDecl->getClassInterface()); selfTy = Context.getPointerType(selfTy); PI.TypeInfo = selfTy.getAsOpaquePtr(); } else PI.TypeInfo = Context.getObjcIdType().getAsOpaquePtr(); - Params.push_back(ParseParamDeclarator(PI, FnBodyScope)); PI.Ident = &Context.Idents.get("_cmd"); - PI.IdentLoc = SourceLocation(/*FIXME*/); PI.TypeInfo = Context.getObjcSelType().getAsOpaquePtr(); Params.push_back(ParseParamDeclarator(PI, FnBodyScope)); for (int i = 0; i < MDecl->getNumParams(); i++) { ParmVarDecl *PDecl = MDecl->getParamDecl(i); PI.Ident = PDecl->getIdentifier(); - PI.IdentLoc = PDecl->getLocation(); + PI.IdentLoc = PDecl->getLocation(); // user vars have a real location. PI.TypeInfo = PDecl->getType().getAsOpaquePtr(); Params.push_back(ParseParamDeclarator(PI, FnBodyScope)); } @@ -1406,8 +1407,8 @@ void Sema::CheckImplementationIvars(ObjcImplementationDecl *ImpDecl, // Check interface's Ivar list against those in the implementation. // names and types must match. // - ObjcIvarDecl** IntfIvars = IDecl->getIntfDeclIvars(); - int IntfNumIvars = IDecl->getIntfDeclNumIvars(); + ObjcIvarDecl** IntfIvars = IDecl->getInstanceVariables(); + int IntfNumIvars = IDecl->getNumInstanceVariables(); unsigned j = 0; bool err = false; while (numIvars > 0 && IntfNumIvars > 0) { diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h index 48cdce363e..db40ba904b 100644 --- a/include/clang/AST/DeclObjC.h +++ b/include/clang/AST/DeclObjC.h @@ -111,8 +111,8 @@ public: } int getNumIntfRefProtocols() const { return NumReferencedProtocols; } - ObjcIvarDecl **getIntfDeclIvars() const { return Ivars; } - int getIntfDeclNumIvars() const { return NumIvars; } + ObjcIvarDecl **getInstanceVariables() const { return Ivars; } + int getNumInstanceVariables() const { return NumIvars; } ObjcMethodDecl** getInstanceMethods() const { return InstanceMethods; } int getNumInstanceMethods() const { return NumInstanceMethods; } @@ -142,6 +142,8 @@ public: void setCategoryList(ObjcCategoryDecl *category) { CategoryList = category; } + ObjcIvarDecl *lookupInstanceVariable(IdentifierInfo *ivarName, + ObjcInterfaceDecl *&clsDeclared); ObjcMethodDecl *lookupInstanceMethod(Selector &Sel); ObjcMethodDecl *lookupClassMethod(Selector &Sel);