Make it an error if an Objective-C declaration is not in the global scope.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58705 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index 3ccea6e..6c17d5f 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -124,6 +124,8 @@
     IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     IDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(IDecl);
   return IDecl;
 }
 
@@ -163,7 +165,10 @@
     ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
   
   ObjCAliasDecls[AliasName] = AliasDecl;
-  TUScope->AddDecl(AliasDecl);
+  
+  if (!CheckObjCDeclScope(AliasDecl))
+    TUScope->AddDecl(AliasDecl);
+  
   return AliasDecl;
 }
 
@@ -201,6 +206,8 @@
     PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     PDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(PDecl);  
   return PDecl;
 }
 
@@ -370,8 +377,13 @@
     
     Protocols.push_back(PDecl);
   }
-  return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
-                                         &Protocols[0], Protocols.size());
+  
+  ObjCForwardProtocolDecl *PDecl = 
+    ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
+                                    &Protocols[0], Protocols.size());
+  
+  CheckObjCDeclScope(PDecl);
+  return PDecl;
 }
 
 Sema::DeclTy *Sema::
@@ -410,6 +422,8 @@
     CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
     CDecl->setLocEnd(EndProtoLoc);
   }
+  
+  CheckObjCDeclScope(CDecl);
   return CDecl;
 }
 
@@ -430,6 +444,8 @@
   /// TODO: Check that CatName, category name, is not used in another
   // implementation.
   ObjCCategoryImpls.push_back(CDecl);
+  
+  CheckObjCDeclScope(CDecl);
   return CDecl;
 }
 
@@ -498,6 +514,9 @@
     ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName, 
                                    IDecl, SDecl);
   
+  if (CheckObjCDeclScope(IMPDecl))
+    return IMPDecl;
+  
   // Check that there is no duplicate implementation of this class.
   if (ObjCImplementations[ClassName])
     // FIXME: Don't leak everything!
@@ -730,8 +749,12 @@
     Interfaces.push_back(IDecl);
   }
   
-  return ObjCClassDecl::Create(Context, AtClassLoc,
-                               &Interfaces[0], Interfaces.size());
+  ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
+                                               &Interfaces[0],
+                                               Interfaces.size());
+  
+  CheckObjCDeclScope(CDecl);
+  return CDecl;  
 }
 
 
@@ -1327,3 +1350,14 @@
     
   return PIDecl;
 }
+
+bool Sema::CheckObjCDeclScope(Decl *D)
+{
+  if (isa<TranslationUnitDecl>(CurContext))
+    return false;
+  
+  Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
+  D->setInvalidDecl();
+  
+  return true;
+}