Index: include/clang/AST/ASTContext.h
===================================================================
--- include/clang/AST/ASTContext.h	(revision 49068)
+++ include/clang/AST/ASTContext.h	(working copy)
@@ -75,6 +75,9 @@
   QualType ObjCConstantStringType;
   RecordDecl *CFConstantStringTypeDecl;
 
+  ContextDecl *CurContextDecl;
+  TranslationUnitDecl *TUDecl;
+
   SourceManager &SourceMgr;
   llvm::MallocAllocator Allocator;
 public:
@@ -84,6 +87,16 @@
   
   SourceManager& getSourceManager() { return SourceMgr; }
   llvm::MallocAllocator &getAllocator() { return Allocator; }
+
+  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
+  void setTranslationUnitDecl(TranslationUnitDecl *TUD) { TUDecl = TUD; }
+
+  ContextDecl *getContextDecl() const { return CurContextDecl; }
+
+  /// Set the declaration context. Until it gets popped, any decl that
+  /// is created will get this ContextDecl.
+  void PushContextDecl(ContextDecl *CD);
+  void PopContextDecl();
   
   FullSourceLoc getFullLoc(SourceLocation Loc) const { 
     return FullSourceLoc(Loc,SourceMgr);
@@ -108,7 +121,7 @@
   ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
              SelectorTable &sels, unsigned size_reserve=0 ) : 
     CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t), 
-    Idents(idents), Selectors(sels) {
+    Idents(idents), Selectors(sels), CurContextDecl(0), TUDecl(0) {
 
     if (size_reserve > 0) Types.reserve(size_reserve);    
     InitBuiltinTypes();
Index: include/clang/AST/Decl.h
===================================================================
--- include/clang/AST/Decl.h	(revision 49068)
+++ include/clang/AST/Decl.h	(working copy)
@@ -28,6 +28,8 @@
 class Expr;
 class Stmt;
 class StringLiteral;
+class ContextDecl;
+class TranslationUnitDecl;
 class FunctionDecl;
 class IdentifierInfo;
 
@@ -42,6 +44,7 @@
     // enums below.   The commented out names are abstract class names.
     
     // Decl
+         TranslationUnit,
     //   NamedDecl
            Field,
              ObjCIvar,
@@ -72,7 +75,8 @@
          ObjCClass,
          ObjCForwardProtocol,
          LinkageSpec,
-   FileScopeAsm,
+         FileScopeAsm,
+
   
     // For each non-leaf class, we now define a mapping to the first/last member
     // of the class, to allow efficient classof.
@@ -120,15 +124,21 @@
   
   /// HasAttrs - This indicates whether the decl has attributes or not.
   unsigned int HasAttrs : 1;
+
+  ContextDecl *CtxDecl;
+
 protected:
-  Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0),
-    HasAttrs(false) {
+  Decl(Kind DK, ContextDecl *CD, SourceLocation L)
+  : Loc(L), DeclKind(DK), InvalidDecl(0),
+    HasAttrs(false), CtxDecl(CD) {
     if (Decl::CollectingStats()) addDeclKind(DK);
   }
   
   virtual ~Decl();
   
 public:
+  ContextDecl *getContextDecl() const { return CtxDecl; }
+
   SourceLocation getLocation() const { return Loc; }
   void setLocation(SourceLocation L) { Loc = L; }
 
@@ -196,6 +206,132 @@
   void ReadInRec(llvm::Deserializer& D);
 };
 
+/// ContextDecl - This is used only as base class of specific decl types that
+/// can act as declaration contexts. This decls are:
+///
+///   TranslationUnitDecl
+///   FunctionDecl
+///   ObjCMethodDecl
+///
+class ContextDecl {
+  /// DeclKind - This indicates which class this is.
+  Decl::Kind DeclKind   :  8;
+
+  // Used only by the ToDecl and FromDecl methods
+  template<typename To, typename From>
+  static To *CastTo(const From *D) {
+    switch(D->getKind()) {
+      case Decl::TranslationUnit:
+        return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
+      case Decl::Function:
+        return static_cast<FunctionDecl*>(const_cast<From*>(D));
+      case Decl::ObjCMethod:
+        return static_cast<ObjCMethodDecl*>(const_cast<From*>(D));
+    }
+
+    // unreachable.
+    assert(false && "a decl that inherits ContextDecl isn't handled");
+    return 0;
+  }
+  
+protected:
+  ContextDecl(Decl::Kind K) : DeclKind(K) {}
+
+public:
+  Decl::Kind getKind() const { return DeclKind; }
+
+  /// getParent - Returns the containing ContextDecl.
+  ContextDecl *getParent() const;
+
+  static Decl        *ToDecl   (const ContextDecl *D);
+  static ContextDecl *FromDecl (const Decl *D);
+
+  static bool classof(const Decl *D) {
+    switch (D->getKind()) {
+      case Decl::TranslationUnit:
+      case Decl::Function:
+      case Decl::ObjCMethod:
+        return true;
+      default:
+        return false;
+    }
+  }
+  static bool classof(const ContextDecl *D) { return true; }
+  static bool classof(const TranslationUnitDecl *D) { return true; }
+  static bool classof(const FunctionDecl *D) { return true; }
+  static bool classof(const ObjCMethodDecl *D) { return true; }
+};
+
+} // end clang.
+
+namespace llvm {
+/// Implement simplify_type for ContextDecl, so that we can dyn_cast from 
+/// ContextDecl to a specific Decl class.
+  template<> struct simplify_type<const ::clang::ContextDecl*> {
+  typedef ::clang::Decl* SimpleType;
+  static SimpleType getSimplifiedValue(const ::clang::ContextDecl *Val) {
+    return ::clang::ContextDecl::ToDecl(Val);
+  }
+};
+template<> struct simplify_type<::clang::ContextDecl*>
+  : public simplify_type<const ::clang::ContextDecl*> {};
+
+template<> struct simplify_type<const ::clang::ContextDecl> {
+  typedef ::clang::Decl SimpleType;
+  static SimpleType &getSimplifiedValue(const ::clang::ContextDecl &Val) {
+    return *::clang::ContextDecl::ToDecl(&Val);
+  }
+};
+template<> struct simplify_type<::clang::ContextDecl>
+  : public simplify_type<const ::clang::ContextDecl> {};
+
+/// Implement cast_convert_val for ContextDecl, so that we can dyn_cast from 
+/// a Decl class to ContextDecl.
+template<class FromTy>
+struct cast_convert_val<::clang::ContextDecl,const FromTy,const FromTy> {
+  static ::clang::ContextDecl &doit(const FromTy &Val) {
+    return *::clang::ContextDecl::FromDecl(&Val);
+  }
+};
+template<class FromTy>
+struct cast_convert_val<::clang::ContextDecl,FromTy,FromTy>
+  : public cast_convert_val<::clang::ContextDecl,const FromTy,const FromTy> {};
+
+template<class FromTy>
+struct cast_convert_val<::clang::ContextDecl,const FromTy*,const FromTy*> {
+  static ::clang::ContextDecl *doit(const FromTy *Val) {
+    return ::clang::ContextDecl::FromDecl(Val);
+  }
+};
+template<class FromTy>
+struct cast_convert_val<::clang::ContextDecl,FromTy*,FromTy*> 
+  : public cast_convert_val<::clang::ContextDecl,const FromTy*,const FromTy*> {};
+
+} // end namespace llvm
+
+namespace clang {
+
+/// TranslationUnitDecl - The top declaration context.
+class TranslationUnitDecl : public Decl, public ContextDecl {
+  TranslationUnitDecl()
+    : Decl(TranslationUnit, 0, SourceLocation()),
+      ContextDecl(TranslationUnit) {}
+public:
+  static TranslationUnitDecl *Create(ASTContext &C);
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; }
+  static bool classof(const TranslationUnitDecl *D) { return true; }  
+
+protected:
+  /// EmitImpl - Serialize this TranslationUnitDecl. Called by Decl::Emit.
+  virtual void EmitImpl(llvm::Serializer& S) const;
+
+  /// CreateImpl - Deserialize a TranslationUnitDecl.  Called by Decl::Create.
+  static TranslationUnitDecl* CreateImpl(llvm::Deserializer& D);  
+
+  friend Decl* Decl::Create(llvm::Deserializer& D);
+};
+
 /// NamedDecl - This represents a decl with an identifier for a name.  Many
 /// decls have names, but not ObjCMethodDecl, @class, etc.
 class NamedDecl : public Decl {
@@ -203,8 +339,8 @@
   /// variable, the tag for a struct).
   IdentifierInfo *Identifier;
 public:
-  NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id)
-   : Decl(DK, L), Identifier(Id) {}
+  NamedDecl(Kind DK, ContextDecl *CD, SourceLocation L, IdentifierInfo *Id)
+   : Decl(DK, CD, L), Identifier(Id) {}
   
   IdentifierInfo *getIdentifier() const { return Identifier; }
   const char *getName() const;
@@ -233,8 +369,9 @@
   ScopedDecl *Next;
 
 protected:
-  ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,ScopedDecl *PrevDecl)
-    : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0) {}
+  ScopedDecl(Kind DK, ContextDecl *CD, SourceLocation L,
+             IdentifierInfo *Id, ScopedDecl *PrevDecl)
+    : NamedDecl(DK, CD, L, Id), NextDeclarator(PrevDecl), Next(0) {}
   
 public:
   ScopedDecl *getNext() const { return Next; }
@@ -274,9 +411,9 @@
   QualType DeclType;
 
 protected:
-  ValueDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
-            ScopedDecl *PrevDecl) 
-    : ScopedDecl(DK, L, Id, PrevDecl), DeclType(T) {}
+  ValueDecl(Kind DK, ContextDecl *CD, SourceLocation L,
+            IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) 
+    : ScopedDecl(DK, CD, L, Id, PrevDecl), DeclType(T) {}
 public:
   QualType getType() const { return DeclType; }
   void setType(QualType newType) { DeclType = newType; }
@@ -307,9 +444,9 @@
   
   friend class StmtIteratorBase;
 protected:
-  VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
+  VarDecl(Kind DK, ContextDecl *CD, SourceLocation L, IdentifierInfo *Id, QualType T,
           StorageClass SC, ScopedDecl *PrevDecl)
-    : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
+    : ValueDecl(DK, CD, L, Id, T, PrevDecl), Init(0) { SClass = SC; }
 public:
   StorageClass getStorageClass() const { return (StorageClass)SClass; }
 
@@ -360,9 +497,10 @@
 ///   void foo() { int x; static int y; extern int z; }
 ///
 class BlockVarDecl : public VarDecl {
-  BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
+  BlockVarDecl(ContextDecl *CD, SourceLocation L,
+               IdentifierInfo *Id, QualType T, StorageClass S,
                ScopedDecl *PrevDecl)
-    : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {}
+    : VarDecl(BlockVar, CD, L, Id, T, S, PrevDecl) {}
 public:
   static BlockVarDecl *Create(ASTContext &C, SourceLocation L,
                               IdentifierInfo *Id, QualType T, StorageClass S,
@@ -383,9 +521,10 @@
 /// definitions (C99 6.9.2p2) using our type system (without storing a
 /// pointer to the decl's scope, which is transient).
 class FileVarDecl : public VarDecl {
-  FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
+  FileVarDecl(ContextDecl *CD, SourceLocation L,
+              IdentifierInfo *Id, QualType T, StorageClass S,
               ScopedDecl *PrevDecl)
-    : VarDecl(FileVar, L, Id, T, S, PrevDecl) {}
+    : VarDecl(FileVar, CD, L, Id, T, S, PrevDecl) {}
 public:
   static FileVarDecl *Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
                              QualType T, StorageClass S, ScopedDecl *PrevDecl);
@@ -408,9 +547,10 @@
   /// in, inout, etc.
   unsigned objcDeclQualifier : 6;
   
-  ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S,
+  ParmVarDecl(ContextDecl *CD, SourceLocation L,
+              IdentifierInfo *Id, QualType T, StorageClass S,
               ScopedDecl *PrevDecl)
-    : VarDecl(ParmVar, L, Id, T, S, PrevDecl), 
+    : VarDecl(ParmVar, CD, L, Id, T, S, PrevDecl), 
     objcDeclQualifier(OBJC_TQ_None) {}
 public:
   static ParmVarDecl *Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
@@ -438,7 +578,7 @@
 
 /// FunctionDecl - An instance of this class is created to represent a function
 /// declaration or definition.
-class FunctionDecl : public ValueDecl {
+class FunctionDecl : public ValueDecl, public ContextDecl {
 public:
   enum StorageClass {
     None, Extern, Static, PrivateExtern
@@ -460,10 +600,12 @@
   unsigned SClass : 2;
   bool IsInline : 1;
   
-  FunctionDecl(SourceLocation L, IdentifierInfo *Id, QualType T,
+  FunctionDecl(ContextDecl *CD, SourceLocation L,
+               IdentifierInfo *Id, QualType T,
                StorageClass S, bool isInline, ScopedDecl *PrevDecl)
-    : ValueDecl(Function, L, Id, T, PrevDecl), 
-      ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline) {}
+    : ValueDecl(Function, CD, L, Id, T, PrevDecl), 
+      ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline),
+      ContextDecl(Function) {}
   virtual ~FunctionDecl();
 public:
   static FunctionDecl *Create(ASTContext &C, SourceLocation L,
@@ -524,11 +666,13 @@
   QualType DeclType;  
   Expr *BitWidth;
 protected:
-  FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T,
+  FieldDecl(Kind DK, ContextDecl *CD,
+            SourceLocation L, IdentifierInfo *Id, QualType T,
             Expr *BW = NULL)
-    : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {}
-  FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *BW)
-    : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {}
+    : NamedDecl(DK, CD, L, Id), DeclType(T), BitWidth(BW) {}
+  FieldDecl(ContextDecl *CD, SourceLocation L,
+            IdentifierInfo *Id, QualType T, Expr *BW)
+    : NamedDecl(Field, CD, L, Id), DeclType(T), BitWidth(BW) {}
 public:
   static FieldDecl *Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
                            QualType T, Expr *BW = NULL);
@@ -562,9 +706,10 @@
   Expr *Init; // an integer constant expression
   llvm::APSInt Val; // The value.
 protected:
-  EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E,
+  EnumConstantDecl(ContextDecl *CD, SourceLocation L,
+                   IdentifierInfo *Id, QualType T, Expr *E,
                    const llvm::APSInt &V, ScopedDecl *PrevDecl)
-    : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {}
+    : ValueDecl(EnumConstant, CD, L, Id, T, PrevDecl), Init(E), Val(V) {}
   ~EnumConstantDecl() {}
 public:
 
@@ -605,8 +750,9 @@
   Type *TypeForDecl;
   friend class ASTContext;
 protected:
-  TypeDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
-    : ScopedDecl(DK, L, Id, PrevDecl), TypeForDecl(0) {}
+  TypeDecl(Kind DK, ContextDecl *CD, SourceLocation L,
+           IdentifierInfo *Id, ScopedDecl *PrevDecl)
+    : ScopedDecl(DK, CD, L, Id, PrevDecl), TypeForDecl(0) {}
 public:
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) {
@@ -619,8 +765,9 @@
 class TypedefDecl : public TypeDecl {
   /// UnderlyingType - This is the type the typedef is set to.
   QualType UnderlyingType;
-  TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD) 
-    : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {}
+  TypedefDecl(ContextDecl *CD, SourceLocation L,
+              IdentifierInfo *Id, QualType T, ScopedDecl *PD) 
+    : TypeDecl(Typedef, CD, L, Id, PD), UnderlyingType(T) {}
   ~TypedefDecl() {}
 public:
   
@@ -651,8 +798,9 @@
   /// it is a declaration ("struct foo;").
   bool IsDefinition : 1;
 protected:
-  TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
-    : TypeDecl(DK, L, Id, PrevDecl) {
+  TagDecl(Kind DK, ContextDecl *CD, SourceLocation L,
+          IdentifierInfo *Id, ScopedDecl *PrevDecl)
+    : TypeDecl(DK, CD, L, Id, PrevDecl) {
     IsDefinition = false;
   }
 public:
@@ -693,8 +841,9 @@
   /// have a different type than this does.
   QualType IntegerType;
   
-  EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
-    : TagDecl(Enum, L, Id, PrevDecl) {
+  EnumDecl(ContextDecl *CD, SourceLocation L,
+           IdentifierInfo *Id, ScopedDecl *PrevDecl)
+    : TagDecl(Enum, CD, L, Id, PrevDecl) {
       ElementList = 0;
       IntegerType = QualType();
     }
@@ -751,8 +900,8 @@
   FieldDecl **Members;   // Null if not defined.
   int NumMembers;   // -1 if not defined.
   
-  RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, 
-             ScopedDecl *PrevDecl) : TagDecl(DK, L, Id, PrevDecl) {
+  RecordDecl(Kind DK, ContextDecl *CD, SourceLocation L, IdentifierInfo *Id, 
+             ScopedDecl *PrevDecl) : TagDecl(DK, CD, L, Id, PrevDecl) {
     HasFlexibleArrayMember = false;
     assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
     Members = 0;
@@ -798,8 +947,8 @@
 
 class FileScopeAsmDecl : public Decl {
   StringLiteral *AsmString;
-  FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring)
-    : Decl(FileScopeAsm, L), AsmString(asmstring) {}
+  FileScopeAsmDecl(ContextDecl *CD, SourceLocation L, StringLiteral *asmstring)
+    : Decl(FileScopeAsm, CD, L), AsmString(asmstring) {}
 public:
   static FileScopeAsmDecl *Create(ASTContext &C, SourceLocation L,
                                   StringLiteral *Str);
@@ -838,8 +987,8 @@
   /// D - This is the Decl of the linkage specification.
   Decl *D;
   
-  LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d)
-   : Decl(LinkageSpec, L), Language(lang), D(d) {}
+  LinkageSpecDecl(ContextDecl *CD, SourceLocation L, LanguageIDs lang, Decl *d)
+   : Decl(LinkageSpec, CD, L), Language(lang), D(d) {}
 public:
   static LinkageSpecDecl *Create(ASTContext &C, SourceLocation L,
                                  LanguageIDs Lang, Decl *D);
Index: include/clang/AST/DeclObjC.h
===================================================================
--- include/clang/AST/DeclObjC.h	(revision 49068)
+++ include/clang/AST/DeclObjC.h	(working copy)
@@ -48,7 +48,7 @@
 /// A selector represents a unique name for a method. The selector names for
 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
 ///
-class ObjCMethodDecl : public Decl {
+class ObjCMethodDecl : public Decl, public ContextDecl {
 public:
   enum ImplementationControl { None, Required, Optional };
 private:
@@ -89,13 +89,15 @@
   Stmt *Body;
   ParmVarDecl *SelfDecl;
   
-  ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
+  ObjCMethodDecl(ContextDecl *CD,
+                 SourceLocation beginLoc, SourceLocation endLoc,
                  Selector SelInfo, QualType T,
                  Decl *contextDecl,
                  AttributeList *M = 0, bool isInstance = true,
                  bool isVariadic = false,
                  ImplementationControl impControl = None)
-  : Decl(ObjCMethod, beginLoc),
+  : Decl(ObjCMethod, CD, beginLoc),
+    ContextDecl(ObjCMethod),
     IsInstance(isInstance), IsVariadic(isVariadic),
     DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
     MethodContext(static_cast<NamedDecl*>(contextDecl)),
@@ -225,9 +227,10 @@
   SourceLocation EndLoc; // marks the '>', '}', or identifier.
   SourceLocation AtEndLoc; // marks the end of the entire interface.
 
-  ObjCInterfaceDecl(SourceLocation atLoc, unsigned numRefProtos,
+  ObjCInterfaceDecl(ContextDecl *CD, SourceLocation atLoc,
+                    unsigned numRefProtos,
                     IdentifierInfo *Id, bool FD, bool isInternal)
-    : TypeDecl(ObjCInterface, atLoc, Id, 0), SuperClass(0),
+    : TypeDecl(ObjCInterface, CD, atLoc, Id, 0), SuperClass(0),
       ReferencedProtocols(0), NumReferencedProtocols(0), Ivars(0), 
       NumIvars(0),
       InstanceMethods(0), NumInstanceMethods(0), 
@@ -367,8 +370,9 @@
 ///   }
 ///
 class ObjCIvarDecl : public FieldDecl {
-  ObjCIvarDecl(SourceLocation L, IdentifierInfo *Id, QualType T) 
-    : FieldDecl(ObjCIvar, L, Id, T) {}
+  ObjCIvarDecl(ContextDecl *CD, SourceLocation L, 
+               IdentifierInfo *Id, QualType T)
+    : FieldDecl(ObjCIvar, CD, L, Id, T) {}
 public:
   static ObjCIvarDecl *Create(ASTContext &C, SourceLocation L,
                               IdentifierInfo *Id, QualType T);
@@ -426,8 +430,9 @@
   SourceLocation EndLoc; // marks the '>' or identifier.
   SourceLocation AtEndLoc; // marks the end of the entire interface.
   
-  ObjCProtocolDecl(SourceLocation L, unsigned numRefProtos, IdentifierInfo *Id)
-    : NamedDecl(ObjCProtocol, L, Id), 
+  ObjCProtocolDecl(ContextDecl *CD, SourceLocation L, unsigned numRefProtos,
+                   IdentifierInfo *Id)
+    : NamedDecl(ObjCProtocol, CD, L, Id), 
       ReferencedProtocols(0), NumReferencedProtocols(0),
       InstanceMethods(0), NumInstanceMethods(0), 
       ClassMethods(0), NumClassMethods(0),
@@ -521,8 +526,9 @@
   ObjCInterfaceDecl **ForwardDecls;
   unsigned NumForwardDecls;
   
-  ObjCClassDecl(SourceLocation L, ObjCInterfaceDecl **Elts, unsigned nElts)
-    : Decl(ObjCClass, L) { 
+  ObjCClassDecl(ContextDecl *CD, SourceLocation L,
+                ObjCInterfaceDecl **Elts, unsigned nElts)
+    : Decl(ObjCClass, CD, L) { 
     if (nElts) {
       ForwardDecls = new ObjCInterfaceDecl*[nElts];
       memcpy(ForwardDecls, Elts, nElts*sizeof(ObjCInterfaceDecl*));
@@ -555,9 +561,9 @@
   ObjCProtocolDecl **ReferencedProtocols;
   unsigned NumReferencedProtocols;
   
-  ObjCForwardProtocolDecl(SourceLocation L, 
+  ObjCForwardProtocolDecl(ContextDecl *CD, SourceLocation L, 
                           ObjCProtocolDecl **Elts, unsigned nElts)
-  : Decl(ObjCForwardProtocol, L) { 
+  : Decl(ObjCForwardProtocol, CD, L) { 
     NumReferencedProtocols = nElts;
     if (nElts) {
       ReferencedProtocols = new ObjCProtocolDecl*[nElts];
@@ -632,8 +638,8 @@
   SourceLocation EndLoc; // marks the '>' or identifier.
   SourceLocation AtEndLoc; // marks the end of the entire interface.
   
-  ObjCCategoryDecl(SourceLocation L, IdentifierInfo *Id)
-    : NamedDecl(ObjCCategory, L, Id),
+  ObjCCategoryDecl(ContextDecl *CD, SourceLocation L, IdentifierInfo *Id)
+    : NamedDecl(ObjCCategory, CD, L, Id),
       ClassInterface(0), ReferencedProtocols(0), NumReferencedProtocols(0),
       InstanceMethods(0), NumInstanceMethods(0),
       ClassMethods(0), NumClassMethods(0),
@@ -727,9 +733,9 @@
 
   SourceLocation EndLoc;  
 
-  ObjCCategoryImplDecl(SourceLocation L, IdentifierInfo *Id,
+  ObjCCategoryImplDecl(ContextDecl *CD, SourceLocation L, IdentifierInfo *Id,
                        ObjCInterfaceDecl *classInterface)
-    : NamedDecl(ObjCCategoryImpl, L, Id), ClassInterface(classInterface) {}
+    : NamedDecl(ObjCCategoryImpl, CD, L, Id), ClassInterface(classInterface) {}
 public:
   static ObjCCategoryImplDecl *Create(ASTContext &C, SourceLocation L,
                                       IdentifierInfo *Id,
@@ -806,10 +812,10 @@
 
   SourceLocation EndLoc;
 
-  ObjCImplementationDecl(SourceLocation L, IdentifierInfo *Id,
+  ObjCImplementationDecl(ContextDecl *CD, SourceLocation L, IdentifierInfo *Id,
                          ObjCInterfaceDecl *classInterface,
                          ObjCInterfaceDecl *superDecl)
-    : NamedDecl(ObjCImplementation, L, Id),
+    : NamedDecl(ObjCImplementation, CD, L, Id),
       ClassInterface(classInterface), SuperClass(superDecl),
       Ivars(0), NumIvars(0) {}
 public:  
@@ -875,9 +881,9 @@
   /// Class that this is an alias of.
   ObjCInterfaceDecl *AliasedClass;
   
-  ObjCCompatibleAliasDecl(SourceLocation L, IdentifierInfo *Id,
+  ObjCCompatibleAliasDecl(ContextDecl *CD, SourceLocation L, IdentifierInfo *Id,
                           ObjCInterfaceDecl* aliasedClass)
-    : NamedDecl(ObjCCompatibleAlias, L, Id), AliasedClass(aliasedClass) {}
+    : NamedDecl(ObjCCompatibleAlias, CD, L, Id), AliasedClass(aliasedClass) {}
 public:
   static ObjCCompatibleAliasDecl *Create(ASTContext &C, SourceLocation L,
                                          IdentifierInfo *Id,
@@ -916,8 +922,8 @@
   IdentifierInfo *GetterName;    // getter name of NULL if no getter
   IdentifierInfo *SetterName;    // setter name of NULL if no setter
   
-  ObjCPropertyDecl(SourceLocation L)
-    : Decl(PropertyDecl, L), PropertyDecls(0), NumPropertyDecls(0),
+  ObjCPropertyDecl(ContextDecl *CD, SourceLocation L)
+    : Decl(PropertyDecl, CD, L), PropertyDecls(0), NumPropertyDecls(0),
       PropertyAttributes(OBJC_PR_noattr), GetterName(0), SetterName(0) {}
 public:
   static ObjCPropertyDecl *Create(ASTContext &C, SourceLocation L);
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp	(revision 49068)
+++ lib/AST/ASTContext.cpp	(working copy)
@@ -26,6 +26,17 @@
   FloatRank, DoubleRank, LongDoubleRank
 };
 
+void ASTContext::PushContextDecl(ContextDecl *CD) {
+  assert(CD->getParent() == CurContextDecl &&
+      "The next ContextDecl should be directly contained in the current one.");
+  CurContextDecl = CD;
+}
+
+void ASTContext::PopContextDecl() {
+  assert(CurContextDecl && "ContextDecl imbalance!");
+  CurContextDecl = CurContextDecl->getParent();
+}
+
 ASTContext::~ASTContext() {
   // Deallocate all the types.
   while (!Types.empty()) {
@@ -1832,6 +1843,9 @@
                                           I!=E;++I)    
     (*I)->Emit(S);
 
+  S.EmitOwnedPtr(TUDecl);
+  S.EmitPtr(CurContextDecl);
+
   // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
 }
 
@@ -1840,7 +1854,7 @@
   TargetInfo &t = D.ReadRef<TargetInfo>();
   IdentifierTable &idents = D.ReadRef<IdentifierTable>();
   SelectorTable &sels = D.ReadRef<SelectorTable>();
-  
+
   unsigned size_reserve = D.ReadInt();
   
   ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
@@ -1848,6 +1862,9 @@
   for (unsigned i = 0; i < size_reserve; ++i)
     Type::Create(*A,i,D);
 
+  A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>());
+  D.ReadPtr(A->CurContextDecl);
+  
   // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
   
   return A;
Index: lib/AST/Decl.cpp
===================================================================
--- lib/AST/Decl.cpp	(revision 49068)
+++ lib/AST/Decl.cpp	(working copy)
@@ -204,11 +204,17 @@
 // Decl Allocation/Deallocation Method Implementations
 //===----------------------------------------------------------------------===//
 
+TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
+  void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
+  return new (Mem) TranslationUnitDecl();
+}
+
+
 BlockVarDecl *BlockVarDecl::Create(ASTContext &C, SourceLocation L,
                                    IdentifierInfo *Id, QualType T,
                                    StorageClass S, ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
-  return new (Mem) BlockVarDecl(L, Id, T, S, PrevDecl);
+  return new (Mem) BlockVarDecl(C.getContextDecl(), L, Id, T, S, PrevDecl);
 }
 
 
@@ -216,14 +222,14 @@
                                  IdentifierInfo *Id, QualType T, StorageClass S,
                                  ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<FileVarDecl>();
-  return new (Mem) FileVarDecl(L, Id, T, S, PrevDecl);
+  return new (Mem) FileVarDecl(C.getContextDecl(), L, Id, T, S, PrevDecl);
 }
 
 ParmVarDecl *ParmVarDecl::Create(ASTContext &C, SourceLocation L,
                                  IdentifierInfo *Id, QualType T, StorageClass S,
                                  ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
-  return new (Mem) ParmVarDecl(L, Id, T, S, PrevDecl);
+  return new (Mem) ParmVarDecl(C.getContextDecl(), L, Id, T, S, PrevDecl);
 }
 
 FunctionDecl *FunctionDecl::Create(ASTContext &C, SourceLocation L, 
@@ -231,13 +237,14 @@
                                    StorageClass S, bool isInline, 
                                    ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<FunctionDecl>();
-  return new (Mem) FunctionDecl(L, Id, T, S, isInline, PrevDecl);
+  return new (Mem) FunctionDecl(C.getContextDecl(), L,
+                                Id, T, S, isInline, PrevDecl);
 }
 
 FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
                              IdentifierInfo *Id, QualType T, Expr *BW) {
   void *Mem = C.getAllocator().Allocate<FieldDecl>();
-  return new (Mem) FieldDecl(L, Id, T, BW);
+  return new (Mem) FieldDecl(C.getContextDecl(), L, Id, T, BW);
 }
 
 
@@ -246,38 +253,39 @@
                                            Expr *E, const llvm::APSInt &V, 
                                            ScopedDecl *PrevDecl){
   void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
-  return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl);
+  return new (Mem) EnumConstantDecl(C.getContextDecl(), L,
+                                    Id, T, E, V, PrevDecl);
 }
 
 TypedefDecl *TypedefDecl::Create(ASTContext &C, SourceLocation L,
                                  IdentifierInfo *Id, QualType T,
                                  ScopedDecl *PD) {
   void *Mem = C.getAllocator().Allocate<TypedefDecl>();
-  return new (Mem) TypedefDecl(L, Id, T, PD);
+  return new (Mem) TypedefDecl(C.getContextDecl(), L, Id, T, PD);
 }
 
 EnumDecl *EnumDecl::Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
                            ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<EnumDecl>();
-  return new (Mem) EnumDecl(L, Id, PrevDecl);
+  return new (Mem) EnumDecl(C.getContextDecl(), L, Id, PrevDecl);
 }
 
 RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, SourceLocation L,
                                IdentifierInfo *Id, ScopedDecl *PrevDecl) {
   void *Mem = C.getAllocator().Allocate<RecordDecl>();
-  return new (Mem) RecordDecl(DK, L, Id, PrevDecl);
+  return new (Mem) RecordDecl(DK, C.getContextDecl(), L, Id, PrevDecl);
 }
 
 FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, SourceLocation L,
                                            StringLiteral *Str) {
   void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
-  return new (Mem) FileScopeAsmDecl(L, Str);
+  return new (Mem) FileScopeAsmDecl(C.getContextDecl(), L, Str);
 }
 
 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, SourceLocation L,
                                          LanguageIDs Lang, Decl *D) {
   void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
-  return new (Mem) LinkageSpecDecl(L, Lang, D);
+  return new (Mem) LinkageSpecDecl(C.getContextDecl(), L, Lang, D);
 }
 
 //===----------------------------------------------------------------------===//
@@ -320,6 +328,22 @@
 }
 
 //===----------------------------------------------------------------------===//
+// ContextDecl Implementation
+//===----------------------------------------------------------------------===//
+
+ContextDecl *ContextDecl::getParent() const {
+  return cast<Decl>(this)->getContextDecl();
+}
+
+Decl *ContextDecl::ToDecl (const ContextDecl *D) {
+  return CastTo<Decl>(D);
+}
+
+ContextDecl *ContextDecl::FromDecl (const Decl *D) {
+  return CastTo<ContextDecl>(D);
+}
+
+//===----------------------------------------------------------------------===//
 // NamedDecl Implementation
 //===----------------------------------------------------------------------===//
 
@@ -338,17 +362,7 @@
 // roughly global variables and functions, but also handles enums (which could
 // be defined inside or outside a function etc).
 bool ScopedDecl::isDefinedOutsideFunctionOrMethod() const {
-  if (const VarDecl *VD = dyn_cast<VarDecl>(this))
-    return VD->hasGlobalStorage();
-  if (isa<FunctionDecl>(this))
-    return true;
-  
-  // FIXME: This needs to check the context the decl was defined in!
-  if (isa<TypeDecl>(this) || isa<EnumConstantDecl>(this))
-    return true;
-  
-  assert(0 && "Unknown ScopedDecl!");
-  return false;
+  return isa<TranslationUnitDecl>(getContextDecl());
 }
 
 //===----------------------------------------------------------------------===//
Index: lib/AST/DeclObjC.cpp
===================================================================
--- lib/AST/DeclObjC.cpp	(revision 49068)
+++ lib/AST/DeclObjC.cpp	(working copy)
@@ -27,7 +27,8 @@
                                        bool isVariadic,
                                        ImplementationControl impControl) {
   void *Mem = C.getAllocator().Allocate<ObjCMethodDecl>();
-  return new (Mem) ObjCMethodDecl(beginLoc, endLoc, SelInfo, T, contextDecl,
+  return new (Mem) ObjCMethodDecl(C.getContextDecl(), beginLoc, endLoc,
+                                  SelInfo, T, contextDecl,
                                   M, isInstance, 
                                   isVariadic, impControl);
 }
@@ -37,47 +38,48 @@
                                              IdentifierInfo *Id,
                                              bool ForwardDecl, bool isInternal){
   void *Mem = C.getAllocator().Allocate<ObjCInterfaceDecl>();
-  return new (Mem) ObjCInterfaceDecl(atLoc, numRefProtos, Id, ForwardDecl,
+  return new (Mem) ObjCInterfaceDecl(C.getContextDecl(), atLoc, numRefProtos,
+                                     Id, ForwardDecl,
                                      isInternal);
 }
 
 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, SourceLocation L,
                                    IdentifierInfo *Id, QualType T) {
   void *Mem = C.getAllocator().Allocate<ObjCIvarDecl>();
-  return new (Mem) ObjCIvarDecl(L, Id, T);
+  return new (Mem) ObjCIvarDecl(C.getContextDecl(), L, Id, T);
 }
 
 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, SourceLocation L, 
                                            unsigned numRefProtos,
                                            IdentifierInfo *Id) {
   void *Mem = C.getAllocator().Allocate<ObjCProtocolDecl>();
-  return new (Mem) ObjCProtocolDecl(L, numRefProtos, Id);
+  return new (Mem) ObjCProtocolDecl(C.getContextDecl(), L, numRefProtos, Id);
 }
 
 ObjCClassDecl *ObjCClassDecl::Create(ASTContext &C, SourceLocation L,
                                      ObjCInterfaceDecl **Elts, unsigned nElts) {
   void *Mem = C.getAllocator().Allocate<ObjCClassDecl>();
-  return new (Mem) ObjCClassDecl(L, Elts, nElts);
+  return new (Mem) ObjCClassDecl(C.getContextDecl(), L, Elts, nElts);
 }
 
 ObjCForwardProtocolDecl *
 ObjCForwardProtocolDecl::Create(ASTContext &C, SourceLocation L, 
                                 ObjCProtocolDecl **Elts, unsigned NumElts) {
   void *Mem = C.getAllocator().Allocate<ObjCForwardProtocolDecl>();
-  return new (Mem) ObjCForwardProtocolDecl(L, Elts, NumElts);
+  return new (Mem) ObjCForwardProtocolDecl(C.getContextDecl(), L, Elts, NumElts);
 }
 
 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, SourceLocation L,
                                            IdentifierInfo *Id) {
   void *Mem = C.getAllocator().Allocate<ObjCCategoryDecl>();
-  return new (Mem) ObjCCategoryDecl(L, Id);
+  return new (Mem) ObjCCategoryDecl(C.getContextDecl(), L, Id);
 }
 
 ObjCCategoryImplDecl *
 ObjCCategoryImplDecl::Create(ASTContext &C, SourceLocation L,IdentifierInfo *Id,
                              ObjCInterfaceDecl *ClassInterface) {
   void *Mem = C.getAllocator().Allocate<ObjCCategoryImplDecl>();
-  return new (Mem) ObjCCategoryImplDecl(L, Id, ClassInterface);
+  return new (Mem) ObjCCategoryImplDecl(C.getContextDecl(), L, Id, ClassInterface);
 }
 
 ObjCImplementationDecl *
@@ -86,7 +88,7 @@
                                ObjCInterfaceDecl *ClassInterface,
                                ObjCInterfaceDecl *SuperDecl) {
   void *Mem = C.getAllocator().Allocate<ObjCImplementationDecl>();
-  return new (Mem) ObjCImplementationDecl(L, Id, ClassInterface, SuperDecl);
+  return new (Mem) ObjCImplementationDecl(C.getContextDecl(), L, Id, ClassInterface, SuperDecl);
 }
 
 ObjCCompatibleAliasDecl *
@@ -94,12 +96,13 @@
                                 IdentifierInfo *Id, 
                                 ObjCInterfaceDecl* AliasedClass) {
   void *Mem = C.getAllocator().Allocate<ObjCCompatibleAliasDecl>();
-  return new (Mem) ObjCCompatibleAliasDecl(L, Id, AliasedClass);
+  return new (Mem) ObjCCompatibleAliasDecl(C.getContextDecl(), L,
+                                           Id, AliasedClass);
 }
 
 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, SourceLocation L) {
   void *Mem = C.getAllocator().Allocate<ObjCPropertyDecl>();
-  return new (Mem) ObjCPropertyDecl(L);
+  return new (Mem) ObjCPropertyDecl(C.getContextDecl(), L);
 }
 
 //===----------------------------------------------------------------------===//
@@ -431,3 +434,4 @@
 }
 
 
+
Index: lib/AST/DeclSerialization.cpp
===================================================================
--- lib/AST/DeclSerialization.cpp	(revision 49068)
+++ lib/AST/DeclSerialization.cpp	(working copy)
@@ -39,6 +39,9 @@
     default:
       assert (false && "Not implemented.");
       break;
+
+    case TranslationUnit:
+      return TranslationUnitDecl::CreateImpl(D);
       
     case BlockVar:
       return BlockVarDecl::CreateImpl(D);
@@ -79,10 +82,14 @@
 
 void Decl::EmitInRec(Serializer& S) const {
   S.Emit(getLocation());                    // From Decl.
+  S.EmitPtr(cast_or_null<Decl>(getContextDecl()));  // From Decl.
 }
 
 void Decl::ReadInRec(Deserializer& D) {
   Loc = SourceLocation::ReadVal(D);                 // From Decl.
+  Decl *TmpD;
+  D.ReadPtr(TmpD);                                  // From Decl.
+  CtxDecl = cast_or_null<ContextDecl>(TmpD);
 }
 
 //===----------------------------------------------------------------------===//
@@ -189,12 +196,29 @@
 }
 
 //===----------------------------------------------------------------------===//
+//      TranslationUnitDecl Serialization.
+//===----------------------------------------------------------------------===//
+
+void TranslationUnitDecl::EmitImpl(llvm::Serializer& S) const
+{
+  Decl::EmitInRec(S);
+}
+
+TranslationUnitDecl* TranslationUnitDecl::CreateImpl(Deserializer& D) {  
+  TranslationUnitDecl* decl = new TranslationUnitDecl();
+
+  decl->Decl::ReadInRec(D);
+  
+  return decl;
+}
+
+//===----------------------------------------------------------------------===//
 //      BlockVarDecl Serialization.
 //===----------------------------------------------------------------------===//
 
 BlockVarDecl* BlockVarDecl::CreateImpl(Deserializer& D) {  
   BlockVarDecl* decl = 
-    new BlockVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
+    new BlockVarDecl(0, SourceLocation(),NULL,QualType(),None,NULL);
  
   decl->VarDecl::ReadImpl(D);
   
@@ -207,7 +231,7 @@
 
 FileVarDecl* FileVarDecl::CreateImpl(Deserializer& D) {
   FileVarDecl* decl =
-    new FileVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
+    new FileVarDecl(0, SourceLocation(),NULL,QualType(),None,NULL);
   
   decl->VarDecl::ReadImpl(D);
 
@@ -225,7 +249,7 @@
 
 ParmVarDecl* ParmVarDecl::CreateImpl(Deserializer& D) {
   ParmVarDecl* decl =
-    new ParmVarDecl(SourceLocation(),NULL,QualType(),None,NULL);
+    new ParmVarDecl(0, SourceLocation(),NULL,QualType(),None,NULL);
   
   decl->VarDecl::ReadImpl(D);
   decl->objcDeclQualifier = static_cast<ObjCDeclQualifier>(D.ReadInt());
@@ -245,7 +269,7 @@
 }
 
 EnumDecl* EnumDecl::CreateImpl(Deserializer& D) {
-  EnumDecl* decl = new EnumDecl(SourceLocation(),NULL,NULL);
+  EnumDecl* decl = new EnumDecl(0, SourceLocation(),NULL,NULL);
   
   decl->ScopedDecl::ReadInRec(D);
   decl->setDefinition(D.ReadBool());
@@ -277,7 +301,7 @@
   D.Read(val);
   
   EnumConstantDecl* decl = 
-    new EnumConstantDecl(SourceLocation(),NULL,QualType(),NULL,
+    new EnumConstantDecl(0, SourceLocation(),NULL,QualType(),NULL,
                          val,NULL);
   
   decl->ValueDecl::ReadInRec(D);
@@ -302,7 +326,7 @@
 }
 
 FieldDecl* FieldDecl::CreateImpl(Deserializer& D) {
-  FieldDecl* decl = new FieldDecl(SourceLocation(), NULL, QualType(), 0);
+  FieldDecl* decl = new FieldDecl(0, SourceLocation(), NULL, QualType(), 0);
   decl->DeclType.ReadBackpatch(D);  
   decl->ReadInRec(D);
   decl->BitWidth = D.ReadOwnedPtr<Expr>();
@@ -338,7 +362,7 @@
   bool IsInline = D.ReadBool();
   
   FunctionDecl* decl =
-    new FunctionDecl(SourceLocation(),NULL,QualType(),SClass, IsInline, 0);
+    new FunctionDecl(0, SourceLocation(),NULL,QualType(),SClass, IsInline, 0);
   
   decl->ValueDecl::ReadInRec(D);
   D.ReadPtr(decl->DeclChain);
@@ -382,7 +406,7 @@
 }
 
 RecordDecl* RecordDecl::CreateImpl(Decl::Kind DK, Deserializer& D) {
-  RecordDecl* decl = new RecordDecl(DK,SourceLocation(),NULL,NULL);
+  RecordDecl* decl = new RecordDecl(DK,0,SourceLocation(),NULL,NULL);
     
   decl->ScopedDecl::ReadInRec(D);
   decl->setDefinition(D.ReadBool());
@@ -418,7 +442,7 @@
 TypedefDecl* TypedefDecl::CreateImpl(Deserializer& D) {
   QualType T = QualType::ReadVal(D);
   
-  TypedefDecl* decl = new TypedefDecl(SourceLocation(),NULL,T,NULL);
+  TypedefDecl* decl = new TypedefDecl(0, SourceLocation(),NULL,T,NULL);
   
   decl->ScopedDecl::ReadInRec(D);
   decl->ScopedDecl::ReadOutRec(D);
@@ -453,7 +477,7 @@
 }
 
 FileScopeAsmDecl* FileScopeAsmDecl::CreateImpl(Deserializer& D) { 
-  FileScopeAsmDecl* decl = new FileScopeAsmDecl(SourceLocation(), 0);
+  FileScopeAsmDecl* decl = new FileScopeAsmDecl(0, SourceLocation(), 0);
 
   decl->Decl::ReadInRec(D);
   decl->AsmString = cast<StringLiteral>(D.ReadOwnedPtr<Expr>());
Index: lib/Sema/Sema.cpp
===================================================================
--- lib/Sema/Sema.cpp	(revision 49068)
+++ lib/Sema/Sema.cpp	(working copy)
@@ -42,6 +42,9 @@
 
 void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
   TUScope = S;
+  TranslationUnitDecl *TUD = TranslationUnitDecl::Create(Context);
+  Context.setTranslationUnitDecl(TUD);
+  Context.PushContextDecl(TUD);
   if (!PP.getLangOptions().ObjC1) return;
   
   TypedefType *t;
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp	(revision 49068)
+++ lib/Sema/SemaDecl.cpp	(working copy)
@@ -1081,6 +1081,7 @@
   Decl *decl = static_cast<Decl*>(ActOnDeclarator(GlobalScope, D, 0));
   FunctionDecl *FD = cast<FunctionDecl>(decl);
   CurFunctionDecl = FD;
+  Context.PushContextDecl(FD);
   
   // Create Decl objects for each parameter, adding them to the FunctionDecl.
   llvm::SmallVector<ParmVarDecl*, 16> Params;
@@ -1125,6 +1126,7 @@
     MD->setBody((Stmt*)Body);
     CurMethodDecl = 0;
   }  
+  Context.PopContextDecl();
   // Verify and clean out per-function state.
   
   // Check goto/label use.
Index: lib/Sema/SemaDeclObjC.cpp
===================================================================
--- lib/Sema/SemaDeclObjC.cpp	(revision 49068)
+++ lib/Sema/SemaDeclObjC.cpp	(working copy)
@@ -33,6 +33,7 @@
   
   // Allow all of Sema to see that we are entering a method definition.
   CurMethodDecl = MDecl;
+  Context.PushContextDecl(MDecl);
 
   // Create Decl objects for each parameter, entrring them in the scope for
   // binding to their use.
@@ -933,3 +934,4 @@
   return PDecl;
 }
 
+
