Propagate the ASTContext to various AST traversal and lookup functions.
No functionality change (really).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68726 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index 77fa994..873806f 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -141,7 +141,7 @@
   bool FreeMemory;
   llvm::MallocAllocator MallocAlloc;
   llvm::BumpPtrAllocator BumpAlloc;
-public:
+public:  
   TargetInfo &Target;
   IdentifierTable &Idents;
   SelectorTable &Selectors;
@@ -189,7 +189,7 @@
              bool FreeMemory = true, unsigned size_reserve=0);
 
   ~ASTContext();
-  
+
   void PrintStats() const;
   const std::vector<Type*>& getTypes() const { return Types; }
   
@@ -364,7 +364,7 @@
   /// given type into \arg S. If \arg NameFields is specified then
   /// record field names are also encoded.
   void getObjCEncodingForType(QualType t, std::string &S, 
-                              FieldDecl *Field=NULL) const;
+                              FieldDecl *Field=NULL);
 
   void getLegacyIntegralTypeEncoding(QualType &t) const;
   
@@ -492,7 +492,7 @@
   const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
   const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D);
   void CollectObjCIvars(const ObjCInterfaceDecl *OI,
-                        llvm::SmallVectorImpl<FieldDecl*> &Fields) const;
+                        llvm::SmallVectorImpl<FieldDecl*> &Fields);
   const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) {
     llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I 
       = ASTFieldForIvarRef.find(MRef);
@@ -718,7 +718,7 @@
                                   bool ExpandStructures,
                                   FieldDecl *Field,
                                   bool OutermostType = false,
-                                  bool EncodingProperty = false) const;
+                                  bool EncodingProperty = false);
                                   
 };
 
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index a2ad290..f954934 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -85,6 +85,9 @@
   /// which may be a special name.
   DeclarationName getDeclName() const { return Name; }
 
+  /// \brief Set the name of this declaration.
+  void setDeclName(DeclarationName N) { Name = N; }
+
   /// getNameAsString - Get a human-readable name for the declaration, even if
   /// it is one of the special kinds of names (C++ constructor, Objective-C
   /// selector, etc).  Creating this name requires expensive string
@@ -257,6 +260,9 @@
   void setStorageClass(StorageClass SC) { SClass = SC; }
 
   SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; }
+  void setTypeSpecStartLoc(SourceLocation SL) {
+    TypeSpecStartLoc = SL;
+  }
 
   const Expr *getInit() const { return (const Expr*) Init; }
   Expr *getInit() { return (Expr*) Init; }
@@ -872,6 +878,7 @@
 public:
   // Low-level accessor
   Type *getTypeForDecl() const { return TypeForDecl; }
+  void setTypeForDecl(Type *TD) { TypeForDecl = TD; }
 
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) {
@@ -1041,12 +1048,12 @@
   // enumeration.
   typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator;
 
-  enumerator_iterator enumerator_begin() const { 
-    return enumerator_iterator(this->decls_begin());
+  enumerator_iterator enumerator_begin(ASTContext &Context) const { 
+    return enumerator_iterator(this->decls_begin(Context));
   }
 
-  enumerator_iterator enumerator_end() const { 
-    return enumerator_iterator(this->decls_end());
+  enumerator_iterator enumerator_end(ASTContext &Context) const { 
+    return enumerator_iterator(this->decls_end(Context));
   }
 
   /// getIntegerType - Return the integer type this enum decl corresponds to.
@@ -1146,16 +1153,18 @@
   // data members, functions, constructors, destructors, etc.
   typedef specific_decl_iterator<FieldDecl> field_iterator;
 
-  field_iterator field_begin() const {
-    return field_iterator(decls_begin());
+  field_iterator field_begin(ASTContext &Context) const {
+    return field_iterator(decls_begin(Context));
   }
-  field_iterator field_end() const {
-    return field_iterator(decls_end());
+  field_iterator field_end(ASTContext &Context) const {
+    return field_iterator(decls_end(Context));
   }
 
   // field_empty - Whether there are any fields (non-static data
   // members) in this record.
-  bool field_empty() const { return field_begin() == field_end(); }
+  bool field_empty(ASTContext &Context) const { 
+    return field_begin(Context) == field_end(Context);
+  }
 
   /// completeDefinition - Notes that the definition of this type is
   /// now complete.
diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h
index 4675136..3244c63 100644
--- a/include/clang/AST/DeclBase.h
+++ b/include/clang/AST/DeclBase.h
@@ -1,4 +1,4 @@
-//===-- DeclBase.h - Base Classes for representing declarations *- C++ -*-===//
+//===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -178,10 +178,6 @@
 
   virtual ~Decl();
 
-  /// setDeclContext - Set both the semantic and lexical DeclContext
-  /// to DC.
-  void setDeclContext(DeclContext *DC);
-
 public:
   SourceLocation getLocation() const { return Loc; }
   void setLocation(SourceLocation L) { Loc = L; }
@@ -229,7 +225,7 @@
     
   /// setInvalidDecl - Indicates the Decl had a semantic error. This
   /// allows for graceful error recovery.
-  void setInvalidDecl() { InvalidDecl = 1; }
+  void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
   bool isInvalidDecl() const { return (bool) InvalidDecl; }
 
   /// isImplicit - Indicates whether the declaration was implicitly
@@ -266,6 +262,10 @@
     return const_cast<Decl*>(this)->getLexicalDeclContext();
   }
   
+  /// setDeclContext - Set both the semantic and lexical DeclContext
+  /// to DC.
+  void setDeclContext(DeclContext *DC);
+
   void setLexicalDeclContext(DeclContext *DC);
 
   // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
@@ -535,8 +535,8 @@
 
   /// decls_begin/decls_end - Iterate over the declarations stored in
   /// this context. 
-  decl_iterator decls_begin() const { return decl_iterator(FirstDecl); }
-  decl_iterator decls_end()   const { return decl_iterator(); }
+  decl_iterator decls_begin(ASTContext &Context) const;
+  decl_iterator decls_end(ASTContext &Context) const;
 
   /// specific_decl_iterator - Iterates over a subrange of
   /// declarations stored in a DeclContext, providing only those that
@@ -692,7 +692,7 @@
   ///
   /// If D is also a NamedDecl, it will be made visible within its
   /// semantic context via makeDeclVisibleInContext.
-  void addDecl(Decl *D);
+  void addDecl(ASTContext &Context, Decl *D);
 
   /// lookup_iterator - An iterator that provides access to the results
   /// of looking up a name within this context.
@@ -711,8 +711,8 @@
   /// the declarations with this name, with object, function, member,
   /// and enumerator names preceding any tag name. Note that this
   /// routine will not look into parent contexts.
-  lookup_result lookup(DeclarationName Name);
-  lookup_const_result lookup(DeclarationName Name) const;
+  lookup_result lookup(ASTContext &Context, DeclarationName Name);
+  lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const;
 
   /// @brief Makes a declaration visible within this context.
   ///
@@ -728,7 +728,7 @@
   /// visible from this context, as determined by
   /// NamedDecl::declarationReplaces, the previous declaration will be
   /// replaced with D.
-  void makeDeclVisibleInContext(NamedDecl *D);
+  void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D);
 
   /// udir_iterator - Iterates through the using-directives stored
   /// within this context.
@@ -736,14 +736,14 @@
   
   typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
 
-  udir_iterator_range getUsingDirectives() const;
+  udir_iterator_range getUsingDirectives(ASTContext &Context) const;
 
-  udir_iterator using_directives_begin() const {
-    return getUsingDirectives().first;
+  udir_iterator using_directives_begin(ASTContext &Context) const {
+    return getUsingDirectives(Context).first;
   }
 
-  udir_iterator using_directives_end() const {
-    return getUsingDirectives().second;
+  udir_iterator using_directives_end(ASTContext &Context) const {
+    return getUsingDirectives(Context).second;
   }
 
   // Low-level accessors
@@ -758,8 +758,8 @@
 #include "clang/AST/DeclNodes.def"
 
 private:
-  void buildLookup(DeclContext *DCtx);
-  void makeDeclVisibleInContextImpl(NamedDecl *D);
+  void buildLookup(ASTContext &Context, DeclContext *DCtx);
+  void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D);
 
   void EmitOutRec(llvm::Serializer& S) const;
   void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
diff --git a/include/clang/AST/DeclContextInternals.h b/include/clang/AST/DeclContextInternals.h
index 74e83f1..9341a7d 100644
--- a/include/clang/AST/DeclContextInternals.h
+++ b/include/clang/AST/DeclContextInternals.h
@@ -64,7 +64,7 @@
 
   /// getLookupResult - Return an array of all the decls that this list
   /// represents.
-  DeclContext::lookup_result getLookupResult() {
+  DeclContext::lookup_result getLookupResult(ASTContext &Context) {
     // If we have a single inline unit, return it.
     if (isInline()) {
       assert(!isNull() && "Empty list isn't allowed");
@@ -81,7 +81,7 @@
   
   /// HandleRedeclaration - If this is a redeclaration of an existing decl,
   /// replace the old one with D and return true.  Otherwise return false.
-  bool HandleRedeclaration(NamedDecl *D) {
+  bool HandleRedeclaration(ASTContext &Context, NamedDecl *D) {
     // Most decls only have one entry in their list, special case it.
     if (isInline()) {
       if (!D->declarationReplaces(Data.get<NamedDecl*>()))
diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h
index 9acce0f..62cd01e 100644
--- a/include/clang/AST/DeclObjC.h
+++ b/include/clang/AST/DeclObjC.h
@@ -256,50 +256,54 @@
 
   // Iterator access to properties.
   typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
-  prop_iterator prop_begin() const { 
-    return prop_iterator(decls_begin());
+  prop_iterator prop_begin(ASTContext &Context) const { 
+    return prop_iterator(decls_begin(Context));
   }
-  prop_iterator prop_end() const { 
-    return prop_iterator(decls_end());
+  prop_iterator prop_end(ASTContext &Context) const { 
+    return prop_iterator(decls_end(Context));
   }
   
   // Iterator access to instance/class methods.
   typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
-  method_iterator meth_begin() const { 
-    return method_iterator(decls_begin());
+  method_iterator meth_begin(ASTContext &Context) const { 
+    return method_iterator(decls_begin(Context));
   }
-  method_iterator meth_end() const { 
-    return method_iterator(decls_end());
+  method_iterator meth_end(ASTContext &Context) const { 
+    return method_iterator(decls_end(Context));
   }
 
   typedef filtered_decl_iterator<ObjCMethodDecl, 
                                  &ObjCMethodDecl::isInstanceMethod> 
     instmeth_iterator;
-  instmeth_iterator instmeth_begin() const {
-    return instmeth_iterator(decls_begin());
+  instmeth_iterator instmeth_begin(ASTContext &Context) const {
+    return instmeth_iterator(decls_begin(Context));
   }
-  instmeth_iterator instmeth_end() const {
-    return instmeth_iterator(decls_end());
+  instmeth_iterator instmeth_end(ASTContext &Context) const {
+    return instmeth_iterator(decls_end(Context));
   }
 
   typedef filtered_decl_iterator<ObjCMethodDecl, 
                                  &ObjCMethodDecl::isClassMethod> 
     classmeth_iterator;
-  classmeth_iterator classmeth_begin() const {
-    return classmeth_iterator(decls_begin());
+  classmeth_iterator classmeth_begin(ASTContext &Context) const {
+    return classmeth_iterator(decls_begin(Context));
   }
-  classmeth_iterator classmeth_end() const {
-    return classmeth_iterator(decls_end());
+  classmeth_iterator classmeth_end(ASTContext &Context) const {
+    return classmeth_iterator(decls_end(Context));
   }
 
   // Get the local instance/class method declared in this interface.
-  ObjCMethodDecl *getInstanceMethod(Selector Sel) const;
-  ObjCMethodDecl *getClassMethod(Selector Sel) const;
-  ObjCMethodDecl *getMethod(Selector Sel, bool isInstance) const {
-    return isInstance ? getInstanceMethod(Sel) : getClassMethod(Sel);
+  ObjCMethodDecl *getInstanceMethod(ASTContext &Context, Selector Sel) const;
+  ObjCMethodDecl *getClassMethod(ASTContext &Context, Selector Sel) const;
+
+  ObjCMethodDecl *
+  getMethod(ASTContext &Context, Selector Sel, bool isInstance) const {
+    return isInstance ? getInstanceMethod(Context, Sel) 
+                      : getClassMethod(Context, Sel);
   }
     
-  ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
+  ObjCPropertyDecl *FindPropertyDeclaration(ASTContext &Context, 
+                                            IdentifierInfo *PropertyId) const;
 
   // Marks the end of the container.
   SourceLocation getAtEndLoc() const { return AtEndLoc; }
@@ -440,17 +444,19 @@
     return false;
   }
   
-  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
+  ObjCIvarDecl *lookupInstanceVariable(ASTContext &Context, 
+                                       IdentifierInfo *IVarName,
                                        ObjCInterfaceDecl *&ClassDeclared);
-  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
+  ObjCIvarDecl *lookupInstanceVariable(ASTContext &Context, 
+                                       IdentifierInfo *IVarName) {
     ObjCInterfaceDecl *ClassDeclared;
-    return lookupInstanceVariable(IVarName, ClassDeclared);
+    return lookupInstanceVariable(Context, IVarName, ClassDeclared);
   }
 
   // Lookup a method. First, we search locally. If a method isn't
   // found, we search referenced protocols and class categories.
-  ObjCMethodDecl *lookupInstanceMethod(Selector Sel);
-  ObjCMethodDecl *lookupClassMethod(Selector Sel);
+  ObjCMethodDecl *lookupInstanceMethod(ASTContext &Context, Selector Sel);
+  ObjCMethodDecl *lookupClassMethod(ASTContext &Context, Selector Sel);
 
   // Location information, modeled after the Stmt API. 
   SourceLocation getLocStart() const { return getLocation(); } // '@'interface
@@ -605,8 +611,8 @@
   
   // Lookup a method. First, we search locally. If a method isn't
   // found, we search referenced protocols and class categories.
-  ObjCMethodDecl *lookupInstanceMethod(Selector Sel);
-  ObjCMethodDecl *lookupClassMethod(Selector Sel);
+  ObjCMethodDecl *lookupInstanceMethod(ASTContext &Context, Selector Sel);
+  ObjCMethodDecl *lookupClassMethod(ASTContext &Context, Selector Sel);
 
   bool isForwardDecl() const { return isForwardProtoDecl; }
   void setForwardDecl(bool val) { isForwardProtoDecl = val; }
diff --git a/include/clang/AST/DeclarationName.h b/include/clang/AST/DeclarationName.h
index a3c1e1f..7290459 100644
--- a/include/clang/AST/DeclarationName.h
+++ b/include/clang/AST/DeclarationName.h
@@ -133,11 +133,7 @@
   /// Construct a declaration name from a raw pointer.
   DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
 
-  /// getUsingDirectiveName - Return name for all using-directives.
-  static DeclarationName getUsingDirectiveName();
-
   friend class DeclarationNameTable;
-  friend class UsingDirectiveDecl;
   friend class NamedDecl;
 
   /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
@@ -157,6 +153,9 @@
   // Construct a declaration name from an Objective-C selector.
   DeclarationName(Selector Sel);
 
+  /// getUsingDirectiveName - Return name for all using-directives.
+  static DeclarationName getUsingDirectiveName();
+
   // operator bool() - Evaluates true when this declaration name is
   // non-empty.
   operator bool() const { 
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index aaec988..e976ccf 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -603,7 +603,7 @@
 }
 
 void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
-                             llvm::SmallVectorImpl<FieldDecl*> &Fields) const {
+                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
   const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
   if (SuperClass)
     CollectObjCIvars(SuperClass, Fields);
@@ -614,8 +614,8 @@
       Fields.push_back(cast<FieldDecl>(IVDecl));
   }
   // look into properties.
-  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
-       E = OI->prop_end(); I != E; ++I) {
+  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
+       E = OI->prop_end(*this); I != E; ++I) {
     if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
       Fields.push_back(cast<FieldDecl>(IV));
   }
@@ -648,7 +648,8 @@
   /// FIXME! Can do collection of ivars and adding to the record while
   /// doing it.
   for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
-    RD->addDecl(FieldDecl::Create(*this, RD, 
+    RD->addDecl(*this,
+                FieldDecl::Create(*this, RD, 
                                   RecFields[i]->getLocation(), 
                                   RecFields[i]->getIdentifier(),
                                   RecFields[i]->getType(), 
@@ -682,7 +683,7 @@
   // FIXME. Add actual count of synthesized ivars, instead of count
   // of properties which is the upper bound, but is safe.
   unsigned FieldCount = 
-    D->ivar_size() + std::distance(D->prop_begin(), D->prop_end());
+    D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
   if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
     FieldCount++;
     const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
@@ -714,8 +715,8 @@
     NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
   }
   // Also synthesized ivars
-  for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(),
-       E = D->prop_end(); I != E; ++I) {
+  for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
+       E = D->prop_end(*this); I != E; ++I) {
     if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
       NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
   }
@@ -743,7 +744,8 @@
   Entry = NewEntry;
 
   // FIXME: Avoid linear walk through the fields, if possible.
-  NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
+  NewEntry->InitializeLayout(std::distance(D->field_begin(*this), 
+                                           D->field_end(*this)));
   bool IsUnion = D->isUnion();
 
   unsigned StructPacking = 0;
@@ -757,8 +759,8 @@
   // Layout each field, for now, just sequentially, respecting alignment.  In
   // the future, this will need to be tweakable by targets.
   unsigned FieldIdx = 0;
-  for (RecordDecl::field_iterator Field = D->field_begin(),
-                               FieldEnd = D->field_end();
+  for (RecordDecl::field_iterator Field = D->field_begin(*this),
+                               FieldEnd = D->field_end(*this);
        Field != FieldEnd; (void)++Field, ++FieldIdx)
     NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
 
@@ -1962,7 +1964,7 @@
                                            SourceLocation(), 0,
                                            FieldTypes[i], /*BitWidth=*/0, 
                                            /*Mutable=*/false);
-      CFConstantStringTypeDecl->addDecl(Field);
+      CFConstantStringTypeDecl->addDecl(*this, Field);
     }
 
     CFConstantStringTypeDecl->completeDefinition(*this);
@@ -1992,7 +1994,7 @@
                                            SourceLocation(), 0, 
                                            FieldTypes[i], /*BitWidth=*/0, 
                                            /*Mutable=*/false);
-      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
+      ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
     }
     
     ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
@@ -2204,7 +2206,7 @@
 }
 
 void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
-                                        FieldDecl *Field) const {
+                                        FieldDecl *Field) {
   // We follow the behavior of gcc, expanding structures which are
   // directly pointed to, and expanding embedded structures. Note that
   // these rules are sufficient to prevent recursive encoding of the
@@ -2228,7 +2230,7 @@
                                             bool ExpandStructures,
                                             FieldDecl *FD,
                                             bool OutermostType,
-                                            bool EncodingProperty) const {
+                                            bool EncodingProperty) {
   if (const BuiltinType *BT = T->getAsBuiltinType()) {
     if (FD && FD->isBitField()) {
       EncodeBitField(this, S, FD);
@@ -2409,8 +2411,8 @@
     }
     if (ExpandStructures) {
       S += '=';
-      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
-                                   FieldEnd = RDecl->field_end();
+      for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
+                                   FieldEnd = RDecl->field_end(*this);
            Field != FieldEnd; ++Field) {
         if (FD) {
           S += '"';
diff --git a/lib/AST/Builtins.cpp b/lib/AST/Builtins.cpp
index 7eab267..97a40a4 100644
--- a/lib/AST/Builtins.cpp
+++ b/lib/AST/Builtins.cpp
@@ -201,7 +201,7 @@
   case 'P': {
     IdentifierInfo *II = &Context.Idents.get("FILE");
     DeclContext::lookup_result Lookup 
-      = Context.getTranslationUnitDecl()->lookup(II);
+      = Context.getTranslationUnitDecl()->lookup(Context, II);
     if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
       Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
       break;
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index f3cf781..763998e 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -369,7 +369,7 @@
 }
 
 void DeclContext::DestroyDecls(ASTContext &C) {
-  for (decl_iterator D = decls_begin(); D != decls_end(); )
+  for (decl_iterator D = decls_begin(C); D != decls_end(C); )
     (*D++)->Destroy(C);
 }
 
@@ -439,7 +439,15 @@
   }
 }
 
-void DeclContext::addDecl(Decl *D) {
+DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
+  return decl_iterator(FirstDecl); 
+}
+
+DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
+  return decl_iterator(); 
+}
+
+void DeclContext::addDecl(ASTContext &Context, Decl *D) {
   assert(D->getLexicalDeclContext() == this &&
          "Decl inserted into wrong lexical context");
   assert(!D->getNextDeclInContext() && D != LastDecl && 
@@ -453,40 +461,41 @@
   }
 
   if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
-    ND->getDeclContext()->makeDeclVisibleInContext(ND);
+    ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
 }
 
 /// buildLookup - Build the lookup data structure with all of the
 /// declarations in DCtx (and any other contexts linked to it or
 /// transparent contexts nested within it).
-void DeclContext::buildLookup(DeclContext *DCtx) {
+void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
   for (; DCtx; DCtx = DCtx->getNextContext()) {
-    for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end(); 
+    for (decl_iterator D = DCtx->decls_begin(Context), 
+                    DEnd = DCtx->decls_end(Context); 
          D != DEnd; ++D) {
       // Insert this declaration into the lookup structure
       if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
-        makeDeclVisibleInContextImpl(ND);
+        makeDeclVisibleInContextImpl(Context, ND);
 
       // If this declaration is itself a transparent declaration context,
       // add its members (recursively).
       if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
         if (InnerCtx->isTransparentContext())
-          buildLookup(InnerCtx->getPrimaryContext());
+          buildLookup(Context, InnerCtx->getPrimaryContext());
     }
   }
 }
 
 DeclContext::lookup_result 
-DeclContext::lookup(DeclarationName Name) {
+DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
   DeclContext *PrimaryContext = getPrimaryContext();
   if (PrimaryContext != this)
-    return PrimaryContext->lookup(Name);
+    return PrimaryContext->lookup(Context, Name);
 
   /// If there is no lookup data structure, build one now by walking
   /// all of the linked DeclContexts (in declaration order!) and
   /// inserting their values.
   if (!LookupPtr) {
-    buildLookup(this);
+    buildLookup(Context, this);
 
     if (!LookupPtr)
       return lookup_result(0, 0);
@@ -496,12 +505,12 @@
   StoredDeclsMap::iterator Pos = Map->find(Name);
   if (Pos == Map->end())
     return lookup_result(0, 0);
-  return Pos->second.getLookupResult();
+  return Pos->second.getLookupResult(Context);
 }
 
 DeclContext::lookup_const_result 
-DeclContext::lookup(DeclarationName Name) const {
-  return const_cast<DeclContext*>(this)->lookup(Name);
+DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
+  return const_cast<DeclContext*>(this)->lookup(Context, Name);
 }
 
 DeclContext *DeclContext::getLookupContext() {
@@ -520,7 +529,7 @@
   return Ctx->getPrimaryContext();
 }
 
-void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
+void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
   // FIXME: This feels like a hack. Should DeclarationName support
   // template-ids, or is there a better way to keep specializations
   // from being visible?
@@ -529,7 +538,7 @@
 
   DeclContext *PrimaryContext = getPrimaryContext();
   if (PrimaryContext != this) {
-    PrimaryContext->makeDeclVisibleInContext(D);
+    PrimaryContext->makeDeclVisibleInContext(Context, D);
     return;
   }
 
@@ -537,15 +546,16 @@
   // into it. Otherwise, be lazy and don't build that structure until
   // someone asks for it.
   if (LookupPtr)
-    makeDeclVisibleInContextImpl(D);
+    makeDeclVisibleInContextImpl(Context, D);
 
   // If we are a transparent context, insert into our parent context,
   // too. This operation is recursive.
   if (isTransparentContext())
-    getParent()->makeDeclVisibleInContext(D);
+    getParent()->makeDeclVisibleInContext(Context, D);
 }
 
-void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
+void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context, 
+                                               NamedDecl *D) {
   // Skip unnamed declarations.
   if (!D->getDeclName())
     return;
@@ -570,7 +580,7 @@
   // If it is possible that this is a redeclaration, check to see if there is
   // already a decl for which declarationReplaces returns true.  If there is
   // one, just replace it and return.
-  if (DeclNameEntries.HandleRedeclaration(D))
+  if (DeclNameEntries.HandleRedeclaration(Context, D))
     return;
   
   // Put this declaration into the appropriate slot.
@@ -579,9 +589,9 @@
 
 /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
 /// this context.
-DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const {
-  lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
+DeclContext::udir_iterator_range 
+DeclContext::getUsingDirectives(ASTContext &Context) const {
+  lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
   return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
                              reinterpret_cast<udir_iterator>(Result.second));
 }
-
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index 0fd83ef..ffc35fe 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -70,7 +70,7 @@
                                            Context.getCanonicalType(ClassType));
   unsigned TypeQuals;
   DeclContext::lookup_const_iterator Con, ConEnd;
-  for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName);
+  for (llvm::tie(Con, ConEnd) = this->lookup(Context, ConstructorName);
        Con != ConEnd; ++Con) {
     if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, TypeQuals) &&
         (TypeQuals & QualType::Const) != 0)
@@ -86,7 +86,7 @@
   DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal);
 
   DeclContext::lookup_const_iterator Op, OpEnd;
-  for (llvm::tie(Op, OpEnd) = this->lookup(OpName);
+  for (llvm::tie(Op, OpEnd) = this->lookup(Context, OpName);
        Op != OpEnd; ++Op) {
     // C++ [class.copy]p9:
     //   A user-declared copy assignment operator is a non-static non-template
diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp
index cd1b979..4bc7cd4 100644
--- a/lib/AST/DeclObjC.cpp
+++ b/lib/AST/DeclObjC.cpp
@@ -43,7 +43,8 @@
 //===----------------------------------------------------------------------===//
 
 // Get the local instance method declared in this interface.
-ObjCMethodDecl *ObjCContainerDecl::getInstanceMethod(Selector Sel) const {
+ObjCMethodDecl *
+ObjCContainerDecl::getInstanceMethod(ASTContext &Context, Selector Sel) const {
   // Since instance & class methods can have the same name, the loop below
   // ensures we get the correct method.
   //
@@ -53,7 +54,7 @@
   // @end
   //
   lookup_const_iterator Meth, MethEnd;
-  for (llvm::tie(Meth, MethEnd) = lookup(Sel);
+  for (llvm::tie(Meth, MethEnd) = lookup(Context, Sel);
        Meth != MethEnd; ++Meth) {
     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
     if (MD && MD->isInstanceMethod())
@@ -63,7 +64,8 @@
 }
 
 // Get the local class method declared in this interface.
-ObjCMethodDecl *ObjCContainerDecl::getClassMethod(Selector Sel) const {
+ObjCMethodDecl *
+ObjCContainerDecl::getClassMethod(ASTContext &Context, Selector Sel) const {
   // Since instance & class methods can have the same name, the loop below
   // ensures we get the correct method.
   //
@@ -73,7 +75,7 @@
   // @end
   //
   lookup_const_iterator Meth, MethEnd;
-  for (llvm::tie(Meth, MethEnd) = lookup(Sel);
+  for (llvm::tie(Meth, MethEnd) = lookup(Context, Sel);
        Meth != MethEnd; ++Meth) {
     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
     if (MD && MD->isClassMethod())
@@ -87,8 +89,10 @@
 /// FIXME: Convert to DeclContext lookup...
 ///
 ObjCPropertyDecl *
-ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
-  for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
+ObjCContainerDecl::FindPropertyDeclaration(ASTContext &Context, 
+                                           IdentifierInfo *PropertyId) const {
+  for (prop_iterator I = prop_begin(Context), E = prop_end(Context); 
+       I != E; ++I)
     if ((*I)->getIdentifier() == PropertyId)
       return *I;
   
@@ -96,7 +100,8 @@
   if (PID) {
     for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), 
          E = PID->protocol_end(); I != E; ++I)
-      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
+      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(Context, 
+                                                              PropertyId))
         return P;
   }
   
@@ -104,22 +109,26 @@
     // Look through categories.
     for (ObjCCategoryDecl *Category = OID->getCategoryList();
          Category; Category = Category->getNextClassCategory()) {
-      if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(PropertyId))
+      if (ObjCPropertyDecl *P = Category->FindPropertyDeclaration(Context,
+                                                                  PropertyId))
         return P;
     }
     // Look through protocols.
     for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
          E = OID->protocol_end(); I != E; ++I) {
-      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
+      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(Context,
+                                                              PropertyId))
         return P;
     }
     if (OID->getSuperClass())
-      return OID->getSuperClass()->FindPropertyDeclaration(PropertyId);
+      return OID->getSuperClass()->FindPropertyDeclaration(Context, 
+                                                           PropertyId);
   } else if (const ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(this)) {
     // Look through protocols.
     for (ObjCInterfaceDecl::protocol_iterator I = OCD->protocol_begin(),
          E = OCD->protocol_end(); I != E; ++I) {
-      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
+      if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(Context, 
+                                                              PropertyId))
         return P;
     }
   }
@@ -127,7 +136,7 @@
 }
 
 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
-  IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
+  ASTContext &Context, IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
   ObjCInterfaceDecl* ClassDecl = this;
   while (ClassDecl != NULL) {
     for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
@@ -138,8 +147,8 @@
       }
     }
     // look into properties.
-    for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(),
-         E = ClassDecl->prop_end(); I != E; ++I) {
+    for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(Context),
+         E = ClassDecl->prop_end(Context); I != E; ++I) {
       ObjCPropertyDecl *PDecl = (*I);
       if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
         if (IV->getIdentifier() == ID) {
@@ -154,12 +163,13 @@
 
 /// lookupInstanceMethod - This method returns an instance method by looking in
 /// the class, its categories, and its super classes (using a linear search).
-ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
+ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(ASTContext &Context,
+                                                        Selector Sel) {
   ObjCInterfaceDecl* ClassDecl = this;
   ObjCMethodDecl *MethodDecl = 0;
   
   while (ClassDecl != NULL) {
-    if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
+    if ((MethodDecl = ClassDecl->getInstanceMethod(Context, Sel)))
       return MethodDecl;
       
     // Didn't find one yet - look through protocols.
@@ -167,13 +177,13 @@
       ClassDecl->getReferencedProtocols();
     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
          E = Protocols.end(); I != E; ++I)
-      if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
+      if ((MethodDecl = (*I)->lookupInstanceMethod(Context, Sel)))
         return MethodDecl;
     
     // Didn't find one yet - now look through categories.
     ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
     while (CatDecl) {
-      if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
+      if ((MethodDecl = CatDecl->getInstanceMethod(Context, Sel)))
         return MethodDecl;
         
       // Didn't find one yet - look through protocols.
@@ -181,7 +191,7 @@
         CatDecl->getReferencedProtocols();
       for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
            E = Protocols.end(); I != E; ++I)
-        if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
+        if ((MethodDecl = (*I)->lookupInstanceMethod(Context, Sel)))
           return MethodDecl;
       CatDecl = CatDecl->getNextClassCategory();
     }
@@ -192,24 +202,25 @@
 
 // lookupClassMethod - This method returns a class method by looking in the
 // class, its categories, and its super classes (using a linear search).
-ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
+ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(ASTContext &Context, 
+                                                     Selector Sel) {
   ObjCInterfaceDecl* ClassDecl = this;
   ObjCMethodDecl *MethodDecl = 0;
 
   while (ClassDecl != NULL) {
-    if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
+    if ((MethodDecl = ClassDecl->getClassMethod(Context, Sel)))
       return MethodDecl;
 
     // Didn't find one yet - look through protocols.
     for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
          E = ClassDecl->protocol_end(); I != E; ++I)
-      if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
+      if ((MethodDecl = (*I)->lookupClassMethod(Context, Sel)))
         return MethodDecl;
     
     // Didn't find one yet - now look through categories.
     ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
     while (CatDecl) {
-      if ((MethodDecl = CatDecl->getClassMethod(Sel)))
+      if ((MethodDecl = CatDecl->getClassMethod(Context, Sel)))
         return MethodDecl;
         
       // Didn't find one yet - look through protocols.
@@ -217,7 +228,7 @@
         CatDecl->getReferencedProtocols();
       for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
            E = Protocols.end(); I != E; ++I)
-        if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
+        if ((MethodDecl = (*I)->lookupClassMethod(Context, Sel)))
           return MethodDecl;
       CatDecl = CatDecl->getNextClassCategory();
     }
@@ -372,7 +383,7 @@
   const RecordDecl *RecordForDecl = Context.addRecordToClass(this);
   assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class");
   DeclContext::lookup_const_result Lookup =
-    RecordForDecl->lookup(IVar->getDeclName());
+    RecordForDecl->lookup(Context, IVar->getDeclName());
   assert((Lookup.first != Lookup.second) && "field decl not found");
   return cast<FieldDecl>(*Lookup.first);
 }
@@ -434,28 +445,30 @@
 
 // lookupInstanceMethod - Lookup a instance method in the protocol and protocols
 // it inherited.
-ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
+ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(ASTContext &Context, 
+                                                       Selector Sel) {
   ObjCMethodDecl *MethodDecl = NULL;
   
-  if ((MethodDecl = getInstanceMethod(Sel)))
+  if ((MethodDecl = getInstanceMethod(Context, Sel)))
     return MethodDecl;
   
   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
-    if ((MethodDecl = (*I)->lookupInstanceMethod(Sel)))
+    if ((MethodDecl = (*I)->lookupInstanceMethod(Context, Sel)))
       return MethodDecl;
   return NULL;
 }
 
 // lookupInstanceMethod - Lookup a class method in the protocol and protocols
 // it inherited.
-ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
+ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(ASTContext &Context, 
+                                                    Selector Sel) {
   ObjCMethodDecl *MethodDecl = NULL;
   
-  if ((MethodDecl = getClassMethod(Sel)))
+  if ((MethodDecl = getClassMethod(Context, Sel)))
     return MethodDecl;
   
   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
-    if ((MethodDecl = (*I)->lookupClassMethod(Sel)))
+    if ((MethodDecl = (*I)->lookupClassMethod(Context, Sel)))
       return MethodDecl;
   return NULL;
 }
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 6458113..643e066 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -213,8 +213,8 @@
     
   // FIXME: This is linear time.
   unsigned i = 0;
-  for (RecordDecl::field_iterator Field = RD->field_begin(),
-                               FieldEnd = RD->field_end();
+  for (RecordDecl::field_iterator Field = RD->field_begin(Info.Ctx),
+                               FieldEnd = RD->field_end(Info.Ctx);
        Field != FieldEnd; (void)++Field, ++i) {
     if (*Field == FD)
       break;
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index cd0e882..b55bddf 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -148,7 +148,11 @@
     if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
       OS << "{\n";
       IndentLevel += 1;
-      for (RecordDecl::field_iterator i = RD->field_begin(); i != RD->field_end(); ++i) {
+      // FIXME: The context passed to field_begin/field_end should
+      // never be NULL!
+      ASTContext *Context = 0;
+      for (RecordDecl::field_iterator i = RD->field_begin(*Context);
+           i != RD->field_end(*Context); ++i) {
         PrintFieldDecl(*i);
       IndentLevel -= 1;
       }
diff --git a/lib/Analysis/CFRefCount.cpp b/lib/Analysis/CFRefCount.cpp
index 267fc0b..0a68ee4 100644
--- a/lib/Analysis/CFRefCount.cpp
+++ b/lib/Analysis/CFRefCount.cpp
@@ -1779,8 +1779,8 @@
               MemRegionManager &MRMgr = state.getManager().getRegionManager();
               
               // Iterate through the fields and construct new symbols.
-              for (RecordDecl::field_iterator FI=RD->field_begin(),
-                   FE=RD->field_end(); FI!=FE; ++FI) {
+              for (RecordDecl::field_iterator FI=RD->field_begin(Ctx),
+                   FE=RD->field_end(Ctx); FI!=FE; ++FI) {
                 
                 // For now just handle scalar fields.
                 FieldDecl *FD = *FI;
diff --git a/lib/Analysis/CheckObjCInstMethSignature.cpp b/lib/Analysis/CheckObjCInstMethSignature.cpp
index a59ed88..97e77cc 100644
--- a/lib/Analysis/CheckObjCInstMethSignature.cpp
+++ b/lib/Analysis/CheckObjCInstMethSignature.cpp
@@ -97,8 +97,8 @@
   ASTContext& Ctx = BR.getContext();
   
   while (C && NumMethods) {
-    for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(),
-         E=C->instmeth_end(); I!=E; ++I) {
+    for (ObjCInterfaceDecl::instmeth_iterator I=C->instmeth_begin(Ctx),
+         E=C->instmeth_end(Ctx); I!=E; ++I) {
 
       ObjCMethodDecl* M = *I;
       Selector S = M->getSelector();
diff --git a/lib/Analysis/RegionStore.cpp b/lib/Analysis/RegionStore.cpp
index a126792..5e60363 100644
--- a/lib/Analysis/RegionStore.cpp
+++ b/lib/Analysis/RegionStore.cpp
@@ -814,7 +814,8 @@
 
   llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
 
-  std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
+  std::vector<FieldDecl *> Fields(RD->field_begin(getContext()), 
+                                  RD->field_end(getContext()));
 
   for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
                                                FieldEnd = Fields.rend();
@@ -1166,7 +1167,8 @@
 
   nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
-  RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
+  RecordDecl::field_iterator FI = RD->field_begin(getContext()), 
+                             FE = RD->field_end(getContext());
 
   for (; FI != FE; ++FI, ++VI) {
 
diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp
index 2edf3bd..f3021dc 100644
--- a/lib/CodeGen/CGCall.cpp
+++ b/lib/CodeGen/CGCall.cpp
@@ -160,17 +160,17 @@
 /// isEmptyRecord - Return true iff a structure has no non-empty
 /// members. Note that a structure with a flexible array member is not
 /// considered empty.
-static bool isEmptyRecord(QualType T) {
+static bool isEmptyRecord(ASTContext &Context, QualType T) {
   const RecordType *RT = T->getAsRecordType();
   if (!RT)
     return 0;
   const RecordDecl *RD = RT->getDecl();
   if (RD->hasFlexibleArrayMember())
     return false;
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(Context), 
+         e = RD->field_end(Context); i != e; ++i) {
     const FieldDecl *FD = *i;
-    if (!isEmptyRecord(FD->getType()))
+    if (!isEmptyRecord(Context, FD->getType()))
       return false;
   }
   return true;
@@ -194,8 +194,8 @@
     return 0;
 
   const Type *Found = 0;
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(Context), 
+         e = RD->field_end(Context); i != e; ++i) {
     const FieldDecl *FD = *i;
     QualType FT = FD->getType();
 
@@ -204,7 +204,7 @@
       if (AT->getSize().getZExtValue() == 1)
         FT = AT->getElementType();
 
-    if (isEmptyRecord(FT)) {
+    if (isEmptyRecord(Context, FT)) {
       // Ignore
     } else if (Found) {
       return 0;
@@ -230,8 +230,8 @@
 
 static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
                                            ASTContext &Context) {
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(Context), 
+         e = RD->field_end(Context); i != e; ++i) {
     const FieldDecl *FD = *i;
 
     if (!is32Or64BitBasicType(FD->getType(), Context))
@@ -273,6 +273,7 @@
 
 /// X86_32ABIInfo - The X86-32 ABI information.
 class X86_32ABIInfo : public ABIInfo {
+  ASTContext &Context;
   bool IsDarwin;
 
   static bool isRegisterSize(unsigned Size) {
@@ -298,7 +299,8 @@
   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
                                  CodeGenFunction &CGF) const;
 
-  X86_32ABIInfo(bool d) : ABIInfo(), IsDarwin(d) {}
+  X86_32ABIInfo(ASTContext &Context, bool d) 
+    : ABIInfo(), Context(Context), IsDarwin(d) {}
 };
 }
 
@@ -336,8 +338,8 @@
 
   // Structure types are passed in register if all fields would be
   // passed in a register.
-  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), 
-         e = RT->getDecl()->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context), 
+         e = RT->getDecl()->field_end(Context); i != e; ++i) {
     const FieldDecl *FD = *i;
     
     // FIXME: Reject bitfields wholesale for now; this is incorrect.
@@ -345,7 +347,7 @@
       return false;
 
     // Empty structures are ignored.
-    if (isEmptyRecord(FD->getType()))
+    if (isEmptyRecord(Context, FD->getType()))
       continue;
 
     // Check fields recursively.
@@ -756,8 +758,8 @@
     // Reset Lo class, this will be recomputed.
     Current = NoClass;
     unsigned idx = 0;
-    for (RecordDecl::field_iterator i = RD->field_begin(), 
-           e = RD->field_end(); i != e; ++i, ++idx) {
+    for (RecordDecl::field_iterator i = RD->field_begin(Context), 
+           e = RD->field_end(Context); i != e; ++i, ++idx) {
       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
       bool BitField = i->isBitField();
 
@@ -1391,7 +1393,7 @@
     bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
     switch (getContext().Target.getPointerWidth(0)) {
     case 32:
-      return *(TheABIInfo = new X86_32ABIInfo(IsDarwin));
+      return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin));
     case 64:
       return *(TheABIInfo = new X86_64ABIInfo());
     }
@@ -1424,8 +1426,8 @@
   assert(!RD->hasFlexibleArrayMember() && 
          "Cannot expand structure with flexible array.");
   
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(Context), 
+         e = RD->field_end(Context); i != e; ++i) {
     const FieldDecl *FD = *i;
     assert(!FD->isBitField() && 
            "Cannot expand structure with bit-field members.");
@@ -1449,8 +1451,8 @@
   assert(LV.isSimple() && 
          "Unexpected non-simple lvalue during struct expansion.");  
   llvm::Value *Addr = LV.getAddress();
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(getContext()), 
+         e = RD->field_end(getContext()); i != e; ++i) {
     FieldDecl *FD = *i;    
     QualType FT = FD->getType();
 
@@ -1476,8 +1478,8 @@
   RecordDecl *RD = RT->getDecl();
   assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
   llvm::Value *Addr = RV.getAggregateAddr();
-  for (RecordDecl::field_iterator i = RD->field_begin(), 
-         e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator i = RD->field_begin(getContext()), 
+         e = RD->field_end(getContext()); i != e; ++i) {
     FieldDecl *FD = *i;    
     QualType FT = FD->getType();
     
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index 0d13299..f2fc90c 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -269,8 +269,8 @@
   const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl);
 
   unsigned FieldNo = 0;
-  for (RecordDecl::field_iterator I = Decl->field_begin(),
-                                  E = Decl->field_end(); 
+  for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()),
+                                  E = Decl->field_end(M->getContext()); 
        I != E; ++I, ++FieldNo) {
     FieldDecl *Field = *I;
     llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
@@ -450,8 +450,9 @@
   llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
 
   // Create DIEnumerator elements for each enumerator.
-  for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(),
-                                  EnumEnd = Decl->enumerator_end();
+  for (EnumDecl::enumerator_iterator 
+         Enum = Decl->enumerator_begin(M->getContext()),
+         EnumEnd = Decl->enumerator_end(M->getContext());
        Enum != EnumEnd; ++Enum) {
     Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(),
                                             Enum->getInitVal().getZExtValue()));
diff --git a/lib/CodeGen/CGExprAgg.cpp b/lib/CodeGen/CGExprAgg.cpp
index 095e224..3b1580c 100644
--- a/lib/CodeGen/CGExprAgg.cpp
+++ b/lib/CodeGen/CGExprAgg.cpp
@@ -133,7 +133,9 @@
   // GCC union extension
   if (E->getType()->isUnionType()) {
     RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
-    LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
+    LValue FieldLoc = CGF.EmitLValueForField(DestPtr, 
+                                             *SD->field_begin(CGF.getContext()),
+                                             true, 0);
     EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
     return;
   }
@@ -398,8 +400,8 @@
 #ifndef NDEBUG
       // Make sure that it's really an empty and not a failure of
       // semantic analysis.
-      for (RecordDecl::field_iterator Field = SD->field_begin(),
-                                   FieldEnd = SD->field_end();
+      for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
+                                   FieldEnd = SD->field_end(CGF.getContext());
            Field != FieldEnd; ++Field)
         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
 #endif
@@ -423,8 +425,8 @@
   
   // Here we iterate over the fields; this makes it simpler to both
   // default-initialize fields and skip over unnamed fields.
-  for (RecordDecl::field_iterator Field = SD->field_begin(),
-                               FieldEnd = SD->field_end();
+  for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
+                               FieldEnd = SD->field_end(CGF.getContext());
        Field != FieldEnd; ++Field) {
     // We're done once we hit the flexible array member
     if (Field->getType()->isIncompleteArrayType())
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index 35b3a37..c5f2202 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -196,8 +196,8 @@
     unsigned EltNo = 0;  // Element no in ILE
     int FieldNo = 0; // Field no in RecordDecl
     bool RewriteType = false;
-    for (RecordDecl::field_iterator Field = RD->field_begin(),
-                                 FieldEnd = RD->field_end();
+    for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
+                                 FieldEnd = RD->field_end(CGM.getContext());
          EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) {
       FieldNo++;
       if (!Field->getIdentifier())
@@ -267,8 +267,8 @@
       // Make sure that it's really an empty and not a failure of
       // semantic analysis.
       RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
-      for (RecordDecl::field_iterator Field = RD->field_begin(),
-                                   FieldEnd = RD->field_end();
+      for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
+                                   FieldEnd = RD->field_end(CGM.getContext());
            Field != FieldEnd; ++Field)
         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
 #endif
diff --git a/lib/CodeGen/CGObjCGNU.cpp b/lib/CodeGen/CGObjCGNU.cpp
index fc77c80..cd94290 100644
--- a/lib/CodeGen/CGObjCGNU.cpp
+++ b/lib/CodeGen/CGObjCGNU.cpp
@@ -618,8 +618,8 @@
     Protocols.push_back((*PI)->getNameAsString());
   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
   llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
-  for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
-       E = PD->instmeth_end(); iter != E; iter++) {
+  for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(Context),
+       E = PD->instmeth_end(Context); iter != E; iter++) {
     std::string TypeStr;
     Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
     InstanceMethodNames.push_back(
@@ -629,8 +629,9 @@
   // Collect information about class methods:
   llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
   llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
-  for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
-      endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
+  for (ObjCProtocolDecl::classmeth_iterator 
+         iter = PD->classmeth_begin(Context),
+         endIter = PD->classmeth_end(Context) ; iter != endIter ; iter++) {
     std::string TypeStr;
     Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
     ClassMethodNames.push_back(
diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp
index 116d8b0..a06959f 100644
--- a/lib/CodeGen/CGObjCMac.cpp
+++ b/lib/CodeGen/CGObjCMac.cpp
@@ -1100,8 +1100,9 @@
   // Construct method lists.
   std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
   std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
-  for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
-         e = PD->instmeth_end(); i != e; ++i) {
+  for (ObjCProtocolDecl::instmeth_iterator 
+         i = PD->instmeth_begin(CGM.getContext()),
+         e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
@@ -1111,8 +1112,9 @@
     }      
   }
 
-  for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
-         e = PD->classmeth_end(); i != e; ++i) {
+  for (ObjCProtocolDecl::classmeth_iterator 
+         i = PD->classmeth_begin(CGM.getContext()),
+         e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
@@ -1286,8 +1288,8 @@
                                       const ObjCContainerDecl *OCD,
                                       const ObjCCommonTypesHelper &ObjCTypes) {
   std::vector<llvm::Constant*> Properties, Prop(2);
-  for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), 
-       E = OCD->prop_end(); I != E; ++I) {
+  for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), 
+       E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
     const ObjCPropertyDecl *PD = *I;
     Prop[0] = GetPropertyName(PD->getIdentifier());
     Prop[1] = GetPropertyTypeString(PD, Container);
@@ -1691,19 +1693,20 @@
 
 /// countInheritedIvars - count number of ivars in class and its super class(s)
 ///
-static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
+static int countInheritedIvars(const ObjCInterfaceDecl *OI, 
+                               ASTContext &Context) {
   int count = 0;
   if (!OI)
     return 0;
   const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
   if (SuperClass)
-    count += countInheritedIvars(SuperClass);
+    count += countInheritedIvars(SuperClass, Context);
   for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
        E = OI->ivar_end(); I != E; ++I)
     ++count;
   // look into properties.
-  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
-       E = OI->prop_end(); I != E; ++I) {
+  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
+       E = OI->prop_end(Context); I != E; ++I) {
     if ((*I)->getPropertyIvarDecl())
       ++count;
   }
@@ -1719,7 +1722,8 @@
 ///
 static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
                                   const ObjCInterfaceDecl *OI,
-                                  const ObjCIvarDecl *IVD) {
+                                  const ObjCIvarDecl *IVD,
+                                  ASTContext &Context) {
   if (!OI)
     return 0;
   assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
@@ -1728,14 +1732,14 @@
     if ((*I)->getIdentifier() == IVD->getIdentifier())
       return OI;
   // look into properties.
-  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
-       E = OI->prop_end(); I != E; ++I) {
+  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
+       E = OI->prop_end(Context); I != E; ++I) {
     ObjCPropertyDecl *PDecl = (*I);
     if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
       if (IV->getIdentifier() == IVD->getIdentifier())
         return OI;
   }
-  return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
+  return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
 }
 
 /*
@@ -1768,7 +1772,8 @@
   
   RecordDecl::field_iterator ifield, pfield;
   const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
-  for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
+  for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext());
+       ifield != e; ++ifield) {
     FieldDecl *Field = *ifield;
     uint64_t  Offset = GetIvarBaseOffset(Layout, Field);
     if (Field->getIdentifier())
@@ -2611,7 +2616,8 @@
       const RecordType *RT = FQT->getAsRecordType();
       const RecordDecl *RD = RT->getDecl();
       // FIXME - Find a more efficiant way of passing records down.
-      TmpRecFields.append(RD->field_begin(), RD->field_end());
+      TmpRecFields.append(RD->field_begin(CGM.getContext()),
+                          RD->field_end(CGM.getContext()));
       // FIXME - Is Layout correct?
       BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields,
                           BytePos + GetFieldBaseOffset(OI, Layout, Field),
@@ -2643,7 +2649,8 @@
         const RecordType *RT = FQT->getAsRecordType();
         const RecordDecl *RD = RT->getDecl();
         // FIXME - Find a more efficiant way of passing records down.
-        TmpRecFields.append(RD->field_begin(), RD->field_end());
+        TmpRecFields.append(RD->field_begin(CGM.getContext()),
+                            RD->field_end(CGM.getContext()));
         
         BuildAggrIvarLayout(OI, Layout, RD,
                             TmpRecFields,
@@ -3075,10 +3082,11 @@
                                           const ObjCInterfaceDecl *OID,
                                           RecordDecl::field_iterator &FIV,
                                           RecordDecl::field_iterator &PIV) {
-  int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
+  int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(),
+                                                 CGM.getContext());
   const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
-  RecordDecl::field_iterator ifield = RD->field_begin();
-  RecordDecl::field_iterator pfield = RD->field_end();
+  RecordDecl::field_iterator ifield = RD->field_begin(CGM.getContext());
+  RecordDecl::field_iterator pfield = RD->field_end(CGM.getContext());
   while (countSuperClassIvars-- > 0) {
     pfield = ifield;
     ++ifield;
@@ -3194,10 +3202,10 @@
   RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
                                       SourceLocation(),
                                       &Ctx.Idents.get("_objc_super"));  
-  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 
-                                Ctx.getObjCIdType(), 0, false));
-  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
-                                Ctx.getObjCClassType(), 0, false));
+  RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 
+                                     Ctx.getObjCIdType(), 0, false));
+  RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
+                                     Ctx.getObjCClassType(), 0, false));
   RD->completeDefinition(Ctx);
   
   SuperCTy = Ctx.getTagDeclType(RD);
@@ -3819,10 +3827,10 @@
   RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
                                       SourceLocation(),
                                       &Ctx.Idents.get("_message_ref_t"));
-  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
-                                Ctx.VoidPtrTy, 0, false));
-  RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
-                                Ctx.getObjCSelType(), 0, false));
+  RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
+                                     Ctx.VoidPtrTy, 0, false));
+  RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
+                                     Ctx.getObjCSelType(), 0, false));
   RD->completeDefinition(Ctx);
   
   MessageRefCTy = Ctx.getTagDeclType(RD);
@@ -4273,17 +4281,17 @@
     RecordDecl::field_iterator firstField, lastField;
     const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
     
-    for (RecordDecl::field_iterator e = RD->field_end(),
+    for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()),
          ifield = firstField; ifield != e; ++ifield)
       lastField = ifield;
     
-    if (lastField != RD->field_end()) {
+    if (lastField != RD->field_end(CGM.getContext())) {
       FieldDecl *Field = *lastField;
       const llvm::Type *FieldTy =
         CGM.getTypes().ConvertTypeForMem(Field->getType());
       unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
       InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
-      if (firstField == RD->field_end())
+      if (firstField == RD->field_end(CGM.getContext()))
         InstanceStart = InstanceSize;
       else {
         Field = *firstField;
@@ -4495,8 +4503,8 @@
                               const ObjCInterfaceDecl *ID,
                               const ObjCIvarDecl *Ivar) {
   Name += "\01_OBJC_IVAR_$_" + 
-          getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
-          + Ivar->getNameAsString();
+    getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + 
+    '.' + Ivar->getNameAsString();
   llvm::GlobalVariable *IvarOffsetGV = 
     CGM.getModule().getGlobalVariable(Name);
   if (!IvarOffsetGV)
@@ -4585,13 +4593,14 @@
   for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
        E = OID->ivar_end(); I != E; ++I) 
      OIvars.push_back(*I);
-  for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
-       E = OID->prop_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
+       E = OID->prop_end(CGM.getContext()); I != E; ++I)
     if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
       OIvars.push_back(IV);
     
   unsigned iv = 0;
-  for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
+  for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); 
+       i != e; ++i) {
     FieldDecl *Field = *i;
     uint64_t offset = GetIvarBaseOffset(Layout, Field);
     Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset);
@@ -4693,8 +4702,10 @@
   // Construct method lists.
   std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
   std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
-  for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
-       e = PD->instmeth_end(); i != e; ++i) {
+  for (ObjCProtocolDecl::instmeth_iterator 
+         i = PD->instmeth_begin(CGM.getContext()),
+         e = PD->instmeth_end(CGM.getContext()); 
+       i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
@@ -4704,8 +4715,10 @@
     }      
   }
   
-  for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
-       e = PD->classmeth_end(); i != e; ++i) {
+  for (ObjCProtocolDecl::classmeth_iterator 
+         i = PD->classmeth_begin(CGM.getContext()),
+         e = PD->classmeth_end(CGM.getContext());
+       i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     llvm::Constant *C = GetMethodDescriptionConstant(MD);
     if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 2c1ab1f..eb190c7 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -1102,7 +1102,7 @@
     cast<llvm::StructType>(getTypes().ConvertType(CFTy));
 
   std::vector<llvm::Constant*> Fields;
-  RecordDecl::field_iterator Field = CFRD->field_begin();
+  RecordDecl::field_iterator Field = CFRD->field_begin(getContext());
 
   // Class pointer.
   FieldDecl *CurField = *Field++;
@@ -1297,7 +1297,8 @@
 
 /// EmitNamespace - Emit all declarations in a namespace.
 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
-  for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
+  for (RecordDecl::decl_iterator I = ND->decls_begin(getContext()),
+         E = ND->decls_end(getContext());
        I != E; ++I)
     EmitTopLevelDecl(*I);
 }
@@ -1309,7 +1310,8 @@
     return;
   }
 
-  for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end();
+  for (RecordDecl::decl_iterator I = LSD->decls_begin(getContext()),
+         E = LSD->decls_end(getContext());
        I != E; ++I)
     EmitTopLevelDecl(*I);
 }
diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp
index e13a4bc..984e49e 100644
--- a/lib/CodeGen/CodeGenTypes.cpp
+++ b/lib/CodeGen/CodeGenTypes.cpp
@@ -482,7 +482,7 @@
   } else if (TD->isUnion()) {
     // Just use the largest element of the union, breaking ties with the
     // highest aligned member.
-    if (!RD->field_empty()) {
+    if (!RD->field_empty(getContext())) {
       RecordOrganizer RO(*this, *RD);
       
       RO.layoutUnionFields(Context.getASTRecordLayout(RD));
@@ -563,8 +563,8 @@
   std::vector<const llvm::Type*> LLVMFields;
 
   unsigned curField = 0;
-  for (RecordDecl::field_iterator Field = RD.field_begin(),
-                               FieldEnd = RD.field_end();
+  for (RecordDecl::field_iterator Field = RD.field_begin(CGT.getContext()),
+                               FieldEnd = RD.field_end(CGT.getContext());
        Field != FieldEnd; ++Field) {
     uint64_t offset = RL.getFieldOffset(curField);
     const llvm::Type *Ty = CGT.ConvertTypeForMemRecursive(Field->getType());
@@ -614,8 +614,8 @@
 /// all fields are added.
 void RecordOrganizer::layoutUnionFields(const ASTRecordLayout &RL) {
   unsigned curField = 0;
-  for (RecordDecl::field_iterator Field = RD.field_begin(),
-                               FieldEnd = RD.field_end();
+  for (RecordDecl::field_iterator Field = RD.field_begin(CGT.getContext()),
+                               FieldEnd = RD.field_end(CGT.getContext());
        Field != FieldEnd; ++Field) {
     // The offset should usually be zero, but bitfields could be strange
     uint64_t offset = RL.getFieldOffset(curField);
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index a3ddb33..474c1e4 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -211,7 +211,7 @@
   // Add scoped declarations into their context, so that they can be
   // found later. Declarations without a context won't be inserted
   // into any context.
-  CurContext->addDecl(D);
+  CurContext->addDecl(Context, D);
 
   // C++ [basic.scope]p4:
   //   -- exactly one declaration shall declare a class name or
@@ -1004,8 +1004,8 @@
 bool Sema::InjectAnonymousStructOrUnionMembers(Scope *S, DeclContext *Owner,
                                                RecordDecl *AnonRecord) {
   bool Invalid = false;
-  for (RecordDecl::field_iterator F = AnonRecord->field_begin(),
-                               FEnd = AnonRecord->field_end();
+  for (RecordDecl::field_iterator F = AnonRecord->field_begin(Context),
+                               FEnd = AnonRecord->field_end(Context);
        F != FEnd; ++F) {
     if ((*F)->getDeclName()) {
       NamedDecl *PrevDecl = LookupQualifiedName(Owner, (*F)->getDeclName(),
@@ -1028,7 +1028,7 @@
         //   definition, the members of the anonymous union are
         //   considered to have been defined in the scope in which the
         //   anonymous union is declared.
-        Owner->makeDeclVisibleInContext(*F);
+        Owner->makeDeclVisibleInContext(Context, *F);
         S->AddDecl(DeclPtrTy::make(*F));
         IdResolver.AddDecl(*F);
       }
@@ -1094,8 +1094,8 @@
     //   The member-specification of an anonymous union shall only
     //   define non-static data members. [Note: nested types and
     //   functions cannot be declared within an anonymous union. ]
-    for (DeclContext::decl_iterator Mem = Record->decls_begin(),
-                                 MemEnd = Record->decls_end();
+    for (DeclContext::decl_iterator Mem = Record->decls_begin(Context),
+                                 MemEnd = Record->decls_end(Context);
          Mem != MemEnd; ++Mem) {
       if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) {
         // C++ [class.union]p3:
@@ -1183,7 +1183,7 @@
   // Add the anonymous struct/union object to the current
   // context. We'll be referencing this object when we refer to one of
   // its members.
-  Owner->addDecl(Anon);
+  Owner->addDecl(Context, Anon);
 
   // Inject the members of the anonymous struct/union into the owning
   // context and into the identifier resolver chain for name lookup
@@ -3473,7 +3473,7 @@
     S = getNonFieldDeclScope(S);
     PushOnScopeChains(New, S);
   } else {
-    CurContext->addDecl(New);
+    CurContext->addDecl(Context, New);
   }
 
   return DeclPtrTy::make(New);
@@ -3607,7 +3607,7 @@
   } else if (II) {
     PushOnScopeChains(NewFD, S);
   } else
-    Record->addDecl(NewFD);
+    Record->addDecl(Context, NewFD);
 
   return NewFD;
 }
@@ -3921,7 +3921,7 @@
           ObjCIvarDecl* Ivar = (*IVI);
           IdentifierInfo *II = Ivar->getIdentifier();
           ObjCIvarDecl* prevIvar =
-            ID->getSuperClass()->lookupInstanceVariable(II);
+            ID->getSuperClass()->lookupInstanceVariable(Context, II);
           if (prevIvar) {
             Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
             Diag(prevIvar->getLocation(), diag::note_previous_declaration);
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index 7f5909b..f3f04f0 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -1178,8 +1178,8 @@
 
   // FIXME: This isn't supposed to be restricted to pointers, but otherwise
   // we might silently generate incorrect code; see following code
-  for (RecordDecl::field_iterator Field = RD->field_begin(),
-                               FieldEnd = RD->field_end();
+  for (RecordDecl::field_iterator Field = RD->field_begin(S.Context),
+                               FieldEnd = RD->field_end(S.Context);
        Field != FieldEnd; ++Field) {
     if (!Field->getType()->isPointerType()) {
       S.Diag(Attr.getLoc(), diag::warn_transparent_union_nonpointer);
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index feb127b..feff707 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -633,7 +633,8 @@
   //   using a qualified name. ]
   // Look for a member, first.
   FieldDecl *Member = 0;
-  DeclContext::lookup_result Result = ClassDecl->lookup(MemberOrBase);
+  DeclContext::lookup_result Result 
+    = ClassDecl->lookup(Context, MemberOrBase);
   if (Result.first != Result.second)
     Member = dyn_cast<FieldDecl>(*Result.first);
 
@@ -772,7 +773,8 @@
         continue;
       
       DeclContext::lookup_const_iterator I, E;
-      for (llvm::tie(I, E) = RD->lookup(VMD->getDeclName()); I != E; ++I) {
+      for (llvm::tie(I, E) = RD->lookup(Context, VMD->getDeclName()); 
+           I != E; ++I) {
         if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I)) {
           if (Context.getCanonicalType(MD->getType()) == 
               Context.getCanonicalType(VMD->getType())) {
@@ -785,7 +787,8 @@
     }
     
     // Finally, add pure virtual methods from this class.
-    for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); 
+    for (RecordDecl::decl_iterator i = RD->decls_begin(Context), 
+                                   e = RD->decls_end(Context); 
          i != e; ++i) {
       if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) {
         if (MD->isPure())
@@ -863,8 +866,8 @@
     bool VisitDeclContext(const DeclContext *DC) {
       bool Invalid = false;
 
-      for (CXXRecordDecl::decl_iterator I = DC->decls_begin(),
-           E = DC->decls_end(); I != E; ++I)
+      for (CXXRecordDecl::decl_iterator I = DC->decls_begin(SemaRef.Context),
+           E = DC->decls_end(SemaRef.Context); I != E; ++I)
         Invalid |= Visit(*I);
 
       return Invalid;
@@ -968,7 +971,7 @@
                                  /*isImplicitlyDeclared=*/true);
     DefaultCon->setAccess(AS_public);
     DefaultCon->setImplicit();
-    ClassDecl->addDecl(DefaultCon);
+    ClassDecl->addDecl(Context, DefaultCon);
 
     // Notify the class that we've added a constructor.
     ClassDecl->addedConstructor(Context, DefaultCon);
@@ -1003,8 +1006,9 @@
     //        class type M (or array thereof), each such class type
     //        has a copy constructor whose first parameter is of type
     //        const M& or const volatile M&.
-    for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin();
-         HasConstCopyConstructor && Field != ClassDecl->field_end(); ++Field) {
+    for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(Context);
+         HasConstCopyConstructor && Field != ClassDecl->field_end(Context);
+         ++Field) {
       QualType FieldType = (*Field)->getType();
       if (const ArrayType *Array = Context.getAsArrayType(FieldType))
         FieldType = Array->getElementType();
@@ -1049,7 +1053,7 @@
     CopyConstructor->setParams(Context, &FromParam, 1);
 
     ClassDecl->addedConstructor(Context, CopyConstructor);
-    ClassDecl->addDecl(CopyConstructor);
+    ClassDecl->addDecl(Context, CopyConstructor);
   }
 
   if (!ClassDecl->hasUserDeclaredCopyAssignment()) {
@@ -1083,8 +1087,9 @@
     //          type M (or array thereof), each such class type has a copy
     //          assignment operator whose parameter is of type const M&,
     //          const volatile M& or M.
-    for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin();
-         HasConstCopyAssignment && Field != ClassDecl->field_end(); ++Field) {
+    for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(Context);
+         HasConstCopyAssignment && Field != ClassDecl->field_end(Context);
+         ++Field) {
       QualType FieldType = (*Field)->getType();
       if (const ArrayType *Array = Context.getAsArrayType(FieldType))
         FieldType = Array->getElementType();
@@ -1127,7 +1132,7 @@
 
     // Don't call addedAssignmentOperator. There is no way to distinguish an
     // implicit from an explicit assignment operator.
-    ClassDecl->addDecl(CopyAssignment);
+    ClassDecl->addDecl(Context, CopyAssignment);
   }
 
   if (!ClassDecl->hasUserDeclaredDestructor()) {
@@ -1146,7 +1151,7 @@
                                   /*isImplicitlyDeclared=*/true);
     Destructor->setAccess(AS_public);
     Destructor->setImplicit();
-    ClassDecl->addDecl(Destructor);
+    ClassDecl->addDecl(Context, Destructor);
   }
 }
 
@@ -1668,7 +1673,7 @@
   // or translation unit scope. We add UsingDirectiveDecls, into
   // it's lookup structure.
   if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()))
-    Ctx->addDecl(UDir);
+    Ctx->addDecl(Context, UDir);
   else
     // Otherwise it is block-sope. using-directives will affect lookup
     // only to the end of scope.
@@ -1724,7 +1729,7 @@
     NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, Alias, 
                                IdentLoc, R);
   
-  CurContext->addDecl(AliasDecl);
+  CurContext->addDecl(Context, AliasDecl);
   return DeclPtrTy::make(AliasDecl);
 }
 
@@ -1861,7 +1866,7 @@
     = Context.DeclarationNames.getCXXConstructorName(
                        Context.getCanonicalType(ClassType.getUnqualifiedType()));
   DeclContext::lookup_const_iterator Con, ConEnd;
-  for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName);
+  for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(Context, ConstructorName);
        Con != ConEnd; ++Con) {
     CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
     if ((Kind == IK_Direct) ||
@@ -2467,7 +2472,7 @@
   LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext,
                                                LangLoc, Language, 
                                                LBraceLoc.isValid());
-  CurContext->addDecl(D);
+  CurContext->addDecl(Context, D);
   PushDeclContext(S, D);
   return DeclPtrTy::make(D);
 }
@@ -2586,7 +2591,7 @@
   Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, 
                                         AssertExpr, AssertMessage);
   
-  CurContext->addDecl(Decl);
+  CurContext->addDecl(Context, Decl);
   return DeclPtrTy::make(Decl);
 }
 
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index e1583d7..edbb018 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -98,7 +98,7 @@
   
     ObjCInterfaceDecls[ClassName] = IDecl;
     // FIXME: PushOnScopeChains
-    CurContext->addDecl(IDecl);
+    CurContext->addDecl(Context, IDecl);
     // Remember that this needs to be removed when the scope is popped.
     TUScope->AddDecl(DeclPtrTy::make(IDecl));
   }
@@ -206,7 +206,7 @@
   ObjCAliasDecls[AliasName] = AliasDecl;
 
   // FIXME: PushOnScopeChains?
-  CurContext->addDecl(AliasDecl);
+  CurContext->addDecl(Context, AliasDecl);
   if (!CheckObjCDeclScope(AliasDecl))
     TUScope->AddDecl(DeclPtrTy::make(AliasDecl));
 
@@ -265,7 +265,7 @@
     PDecl = ObjCProtocolDecl::Create(Context, CurContext, 
                                      AtProtoInterfaceLoc,ProtocolName);
     // FIXME: PushOnScopeChains?
-    CurContext->addDecl(PDecl);
+    CurContext->addDecl(Context, PDecl);
     PDecl->setForwardDecl(false);
     ObjCProtocols[ProtocolName] = PDecl;
   }
@@ -367,12 +367,12 @@
   if (!SDecl)
     return;
   // FIXME: O(N^2)
-  for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
-       E = SDecl->prop_end(); S != E; ++S) {
+  for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(Context),
+       E = SDecl->prop_end(Context); S != E; ++S) {
     ObjCPropertyDecl *SuperPDecl = (*S);
     // Does property in super class has declaration in current class?
-    for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
-         E = IDecl->prop_end(); I != E; ++I) {
+    for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(Context),
+         E = IDecl->prop_end(Context); I != E; ++I) {
       ObjCPropertyDecl *PDecl = (*I);
       if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
           DiagnosePropertyMismatch(PDecl, SuperPDecl, 
@@ -392,12 +392,12 @@
     // Category
     ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
     assert (CatDecl && "MergeOneProtocolPropertiesIntoClass");
-    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
-         E = PDecl->prop_end(); P != E; ++P) {
+    for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
+         E = PDecl->prop_end(Context); P != E; ++P) {
       ObjCPropertyDecl *Pr = (*P);
       ObjCCategoryDecl::prop_iterator CP, CE;
       // Is this property already in  category's list of properties?
-      for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); 
+      for (CP = CatDecl->prop_begin(Context), CE = CatDecl->prop_end(Context); 
            CP != CE; ++CP)
         if ((*CP)->getIdentifier() == Pr->getIdentifier())
           break;
@@ -407,12 +407,12 @@
     }
     return;
   }
-  for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
-       E = PDecl->prop_end(); P != E; ++P) {
+  for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(Context),
+       E = PDecl->prop_end(Context); P != E; ++P) {
     ObjCPropertyDecl *Pr = (*P);
     ObjCInterfaceDecl::prop_iterator CP, CE;
     // Is this property already in  class's list of properties?
-    for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); 
+    for (CP = IDecl->prop_begin(Context), CE = IDecl->prop_end(Context); 
          CP != CE; ++CP)
       if ((*CP)->getIdentifier() == Pr->getIdentifier())
         break;
@@ -483,16 +483,16 @@
     return;  // Possibly due to previous error
 
   llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
-  for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
-       e =  ID->meth_end(); i != e; ++i) {
+  for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(Context),
+       e =  ID->meth_end(Context); i != e; ++i) {
     ObjCMethodDecl *MD = *i;
     MethodMap[MD->getSelector()] = MD;
   }
 
   if (MethodMap.empty())
     return;
-  for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
-       e =  CAT->meth_end(); i != e; ++i) {
+  for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(Context),
+       e =  CAT->meth_end(Context); i != e; ++i) {
     ObjCMethodDecl *Method = *i;
     const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
     if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
@@ -518,7 +518,7 @@
       PDecl = ObjCProtocolDecl::Create(Context, CurContext, 
                                        IdentList[i].second, Ident);
       // FIXME: PushOnScopeChains?
-      CurContext->addDecl(PDecl);
+      CurContext->addDecl(Context, PDecl);
     }
     if (attrList)
       ProcessDeclAttributeList(PDecl, attrList);
@@ -528,7 +528,7 @@
   ObjCForwardProtocolDecl *PDecl = 
     ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
                                     &Protocols[0], Protocols.size());
-  CurContext->addDecl(PDecl);
+  CurContext->addDecl(Context, PDecl);
   CheckObjCDeclScope(PDecl);
   return DeclPtrTy::make(PDecl);
 }
@@ -544,7 +544,7 @@
   ObjCCategoryDecl *CDecl = 
     ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName);
   // FIXME: PushOnScopeChains?
-  CurContext->addDecl(CDecl);
+  CurContext->addDecl(Context, CDecl);
 
   ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
   /// Check that class of this category is already completely declared.
@@ -598,7 +598,7 @@
     Diag(ClassLoc, diag::err_undef_interface) << ClassName;
 
   // FIXME: PushOnScopeChains?
-  CurContext->addDecl(CDecl);
+  CurContext->addDecl(Context, CDecl);
 
   /// TODO: Check that CatName, category name, is not used in another
   // implementation.
@@ -663,7 +663,7 @@
     IDecl->setLocEnd(ClassLoc);
     
     // FIXME: PushOnScopeChains?
-    CurContext->addDecl(IDecl);
+    CurContext->addDecl(Context, IDecl);
     // Remember that this needs to be removed when the scope is popped.
     TUScope->AddDecl(DeclPtrTy::make(IDecl));
   }
@@ -673,7 +673,7 @@
                                    IDecl, SDecl);
   
   // FIXME: PushOnScopeChains?
-  CurContext->addDecl(IMPDecl);
+  CurContext->addDecl(Context, IMPDecl);
 
   if (CheckObjCDeclScope(IMPDecl))
     return DeclPtrTy::make(IMPDecl);
@@ -797,7 +797,7 @@
     return false;
   // Even if property is ready only, if interface has a user defined setter, 
   // it is not considered read only.
-  if (IDecl->getInstanceMethod(PDecl->getSetterName()))
+  if (IDecl->getInstanceMethod(Context, PDecl->getSetterName()))
     return false;
   
   // Main class has the property as 'readonly'. Must search
@@ -807,10 +807,10 @@
        Category; Category = Category->getNextClassCategory()) {
     // Even if property is ready only, if a category has a user defined setter, 
     // it is not considered read only. 
-    if (Category->getInstanceMethod(PDecl->getSetterName()))
+    if (Category->getInstanceMethod(Context, PDecl->getSetterName()))
       return false;
     ObjCPropertyDecl *P = 
-    Category->FindPropertyDeclaration(PDecl->getIdentifier());
+      Category->FindPropertyDeclaration(Context, PDecl->getIdentifier());
     if (P && !P->isReadOnly())
       return false;
   }
@@ -863,28 +863,31 @@
   // and otherwise would terminate in a warning.
 
   // check unimplemented instance methods.
-  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 
-       E = PDecl->instmeth_end(); I != E; ++I) {
+  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(Context), 
+       E = PDecl->instmeth_end(Context); I != E; ++I) {
     ObjCMethodDecl *method = *I;
     if (method->getImplementationControl() != ObjCMethodDecl::Optional && 
         !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
-        (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
+        (!Super || 
+         !Super->lookupInstanceMethod(Context, method->getSelector()))) {
         // Ugly, but necessary. Method declared in protcol might have
         // have been synthesized due to a property declared in the class which
         // uses the protocol.
         ObjCMethodDecl *MethodInClass = 
-          IDecl->lookupInstanceMethod(method->getSelector());
+          IDecl->lookupInstanceMethod(Context, method->getSelector());
         if (!MethodInClass || !MethodInClass->isSynthesized())
           WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
       }
   }
   // check unimplemented class methods
-  for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 
-       E = PDecl->classmeth_end(); I != E; ++I) {
+  for (ObjCProtocolDecl::classmeth_iterator 
+         I = PDecl->classmeth_begin(Context),
+         E = PDecl->classmeth_end(Context);
+       I != E; ++I) {
     ObjCMethodDecl *method = *I;
     if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
         !ClsMap.count(method->getSelector()) &&
-        (!Super || !Super->lookupClassMethod(method->getSelector())))
+        (!Super || !Super->lookupClassMethod(Context, method->getSelector())))
       WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
   }
   // Check on this protocols's referenced protocols, recursively.
@@ -900,11 +903,11 @@
   // Check and see if instance methods in class interface have been
   // implemented in the implementation class.
   for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
-       E = IMPDecl->instmeth_end(); I != E; ++I)
+         E = IMPDecl->instmeth_end(); I != E; ++I)
     InsMap.insert((*I)->getSelector());
   
-  for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
-       E = CDecl->instmeth_end(); I != E; ++I) {
+  for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(Context),
+       E = CDecl->instmeth_end(Context); I != E; ++I) {
     if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) {
       WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
       continue;
@@ -913,7 +916,7 @@
     ObjCMethodDecl *ImpMethodDecl = 
       IMPDecl->getInstanceMethod((*I)->getSelector());
     ObjCMethodDecl *IntfMethodDecl = 
-      CDecl->getInstanceMethod((*I)->getSelector());
+      CDecl->getInstanceMethod(Context, (*I)->getSelector());
     assert(IntfMethodDecl && 
            "IntfMethodDecl is null in ImplMethodsVsClassMethods");
     // ImpMethodDecl may be null as in a @dynamic property.
@@ -928,15 +931,17 @@
        E = IMPDecl->classmeth_end(); I != E; ++I)
     ClsMap.insert((*I)->getSelector());
   
-  for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
-       E = CDecl->classmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::classmeth_iterator 
+         I = CDecl->classmeth_begin(Context),
+         E = CDecl->classmeth_end(Context);
+       I != E; ++I)
     if (!ClsMap.count((*I)->getSelector()))
       WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
     else {
       ObjCMethodDecl *ImpMethodDecl = 
         IMPDecl->getClassMethod((*I)->getSelector());
       ObjCMethodDecl *IntfMethodDecl = 
-        CDecl->getClassMethod((*I)->getSelector());
+        CDecl->getClassMethod(Context, (*I)->getSelector());
       WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
     }
   
@@ -1002,7 +1007,7 @@
       ObjCInterfaceDecls[IdentList[i]] = IDecl;
 
       // FIXME: PushOnScopeChains?
-      CurContext->addDecl(IDecl);
+      CurContext->addDecl(Context, IDecl);
       // Remember that this needs to be removed when the scope is popped.
       TUScope->AddDecl(DeclPtrTy::make(IDecl));
     }
@@ -1013,7 +1018,7 @@
   ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
                                                &Interfaces[0],
                                                Interfaces.size());
-  CurContext->addDecl(CDecl);
+  CurContext->addDecl(Context, CDecl);
   CheckObjCDeclScope(CDecl);
   return DeclPtrTy::make(CDecl);
 }
@@ -1138,8 +1143,8 @@
                                ObjCContainerDecl *CD) {
   ObjCMethodDecl *GetterMethod, *SetterMethod;
   
-  GetterMethod = CD->getInstanceMethod(property->getGetterName());  
-  SetterMethod = CD->getInstanceMethod(property->getSetterName());
+  GetterMethod = CD->getInstanceMethod(Context, property->getGetterName());  
+  SetterMethod = CD->getInstanceMethod(Context, property->getSetterName());
   
   if (GetterMethod &&
       GetterMethod->getResultType() != property->getType()) {
@@ -1182,7 +1187,7 @@
                               ObjCPropertyDecl::Optional) ? 
                              ObjCMethodDecl::Optional : 
                              ObjCMethodDecl::Required);
-    CD->addDecl(GetterMethod);
+    CD->addDecl(Context, GetterMethod);
   } else
     // A user declared getter will be synthesize when @synthesize of
     // the property with the same name is seen in the @implementation
@@ -1213,7 +1218,7 @@
                                                   VarDecl::None,
                                                   0);
       SetterMethod->setMethodParams(&Argument, 1, Context);
-      CD->addDecl(SetterMethod);
+      CD->addDecl(Context, SetterMethod);
     } else
       // A user declared setter will be synthesize when @synthesize of
       // the property with the same name is seen in the @implementation
@@ -1279,7 +1284,7 @@
             << Method->getDeclName();
           Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
       } else {
-        DC->addDecl(Method);
+        DC->addDecl(Context, Method);
         InsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "id".
         AddInstanceMethodToGlobalPool(Method);
@@ -1296,7 +1301,7 @@
           << Method->getDeclName();
         Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
       } else {
-        DC->addDecl(Method);
+        DC->addDecl(Context, Method);
         ClsMap[Method->getSelector()] = Method;
         /// The following allows us to typecheck messages to "Class".
         AddFactoryMethodToGlobalPool(Method);
@@ -1322,8 +1327,9 @@
     // ProcessPropertyDecl is responsible for diagnosing conflicts with any
     // user-defined setter/getter. It also synthesizes setter/getter methods
     // and adds them to the DeclContext and global method pools.
-    for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
-                                          E = CDecl->prop_end(); I != E; ++I)
+    for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(Context),
+                                          E = CDecl->prop_end(Context);
+         I != E; ++I)
       ProcessPropertyDecl(*I, CDecl);
     CDecl->setAtEndLoc(AtEndLoc);
   }
@@ -1612,8 +1618,10 @@
         // Find the property in continuation class's primary class only.
         ObjCPropertyDecl *PIDecl = 0;
         IdentifierInfo *PropertyId = FD.D.getIdentifier();
-        for (ObjCInterfaceDecl::prop_iterator I = CCPrimary->prop_begin(), 
-             E = CCPrimary->prop_end(); I != E; ++I)
+        for (ObjCInterfaceDecl::prop_iterator 
+               I = CCPrimary->prop_begin(Context),
+               E = CCPrimary->prop_end(Context);
+             I != E; ++I)
           if ((*I)->getIdentifier() == PropertyId) {
             PIDecl = *I;
             break;
@@ -1671,7 +1679,7 @@
   assert(DC && "ClassDecl is not a DeclContext");
   ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc, 
                                                      FD.D.getIdentifier(), T);
-  DC->addDecl(PDecl);
+  DC->addDecl(Context, PDecl);
   
   ProcessDeclAttributes(PDecl, FD.D);
 
@@ -1747,7 +1755,7 @@
            "ActOnPropertyImplDecl - @implementation without @interface");
     
     // Look for this property declaration in the @implementation's @interface
-    property = IDecl->FindPropertyDeclaration(PropertyId);
+    property = IDecl->FindPropertyDeclaration(Context, PropertyId);
     if (!property) {
       Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
       return DeclPtrTy();
@@ -1771,7 +1779,7 @@
     if (!Category)
       return DeclPtrTy();
     // Look for this property declaration in @implementation's category
-    property = Category->FindPropertyDeclaration(PropertyId);
+    property = Category->FindPropertyDeclaration(Context, PropertyId);
     if (!property) {
       Diag(PropertyLoc, diag::error_bad_category_property_decl)
         << Category->getDeclName();
@@ -1789,7 +1797,7 @@
       PropertyIvar = PropertyId;
     QualType PropType = Context.getCanonicalType(property->getType());
     // Check that this is a previously declared 'ivar' in 'IDecl' interface
-    Ivar = IDecl->lookupInstanceVariable(PropertyIvar);
+    Ivar = IDecl->lookupInstanceVariable(Context, PropertyIvar);
     if (!Ivar) {
       if (getLangOptions().ObjCNonFragileABI) {
         Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc, 
@@ -1851,7 +1859,7 @@
                                   ObjCPropertyImplDecl::Synthesize 
                                   : ObjCPropertyImplDecl::Dynamic),
                                  Ivar);
-  CurContext->addDecl(PIDecl);
+  CurContext->addDecl(Context, PIDecl);
   if (IC) {
     if (Synthesize)
       if (ObjCPropertyImplDecl *PPIDecl = 
@@ -1943,7 +1951,7 @@
     if (getLangOptions().CPlusPlus)
       PushOnScopeChains(cast<FieldDecl>(FD), S);
     else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
-      Record->addDecl(FD);
+      Record->addDecl(Context, FD);
   }
 }
 
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index cb7a90f..dda8654 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -58,7 +58,8 @@
         if (ObjCImplementationDecl *Impl
               = dyn_cast<ObjCImplementationDecl>(MD->getParent())) {
           
-          MD = Impl->getClassInterface()->getMethod(MD->getSelector(),
+          MD = Impl->getClassInterface()->getMethod(Context, 
+                                                    MD->getSelector(),
                                                     MD->isInstanceMethod());
           isSilenced |= MD && MD->getAttr<DeprecatedAttr>();
         }
@@ -449,7 +450,8 @@
 /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or
 /// variable corresponding to the anonymous union or struct whose type
 /// is Record.
-static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) {
+static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context,
+                                             RecordDecl *Record) {
   assert(Record->isAnonymousStructOrUnion() && 
          "Record must be an anonymous struct or union!");
   
@@ -458,8 +460,8 @@
   // vector (which itself will be eliminated). DeclGroups might make
   // this even better.
   DeclContext *Ctx = Record->getDeclContext();
-  for (DeclContext::decl_iterator D = Ctx->decls_begin(), 
-                               DEnd = Ctx->decls_end();
+  for (DeclContext::decl_iterator D = Ctx->decls_begin(Context), 
+                               DEnd = Ctx->decls_end(Context);
        D != DEnd; ++D) {
     if (*D == Record) {
       // The object for the anonymous struct/union directly
@@ -495,7 +497,7 @@
   DeclContext *Ctx = Field->getDeclContext();
   do {
     RecordDecl *Record = cast<RecordDecl>(Ctx);
-    Decl *AnonObject = getObjectForAnonymousRecordDecl(Record);
+    Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record);
     if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject))
       AnonFields.push_back(AnonField);
     else {
@@ -643,7 +645,8 @@
     if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) {
       ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
       ObjCInterfaceDecl *ClassDeclared;
-      if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) {
+      if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, 
+                                                           ClassDeclared)) {
         // Check if referencing a field with __attribute__((deprecated)).
         if (DiagnoseUseOfDecl(IV, Loc))
           return ExprError();
@@ -675,7 +678,8 @@
       // We should warn if a local variable hides an ivar.
       ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
       ObjCInterfaceDecl *ClassDeclared;
-      if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) {
+      if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, 
+                                                           ClassDeclared)) {
         if (IV->getAccessControl() != ObjCIvarDecl::Private ||
             IFace == ClassDeclared)
           Diag(Loc, diag::warn_ivar_use_hidden)<<IV->getDeclName();
@@ -1695,16 +1699,18 @@
 
 static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
                                                 IdentifierInfo &Member,
-                                                const Selector &Sel) {
+                                                const Selector &Sel,
+                                                ASTContext &Context) {
   
-  if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(&Member))
+  if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Context, &Member))
     return PD;
-  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
+  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Context, Sel))
     return OMD;
   
   for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
        E = PDecl->protocol_end(); I != E; ++I) {
-    if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel))
+    if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, 
+                                                     Context))
       return D;
   }
   return 0;
@@ -1712,17 +1718,18 @@
 
 static Decl *FindGetterNameDecl(const ObjCQualifiedIdType *QIdTy,
                                 IdentifierInfo &Member,
-                                const Selector &Sel) {
+                                const Selector &Sel,
+                                ASTContext &Context) {
   // Check protocols on qualified interfaces.
   Decl *GDecl = 0;
   for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(),
        E = QIdTy->qual_end(); I != E; ++I) {
-    if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) {
+    if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, &Member)) {
       GDecl = PD;
       break;
     }
     // Also must look for a getter name which uses property syntax.
-    if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
+    if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Context, Sel)) {
       GDecl = OMD;
       break;
     }
@@ -1731,7 +1738,7 @@
     for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(),
          E = QIdTy->qual_end(); I != E; ++I) {
       // Search in the protocol-qualifier list of current protocol.
-      GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel);
+      GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context);
       if (GDecl)
         return GDecl;
     }
@@ -1875,7 +1882,8 @@
   // (*Obj).ivar.
   if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) {
     ObjCInterfaceDecl *ClassDeclared;
-    if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member, 
+    if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(Context,
+                                                                   &Member, 
                                                              ClassDeclared)) {
       // If the decl being referenced had an error, return an error for this
       // sub-expr without emitting another error, in order to avoid cascading
@@ -1937,7 +1945,8 @@
     ObjCInterfaceDecl *IFace = IFTy->getDecl();
 
     // Search for a declared property first.
-    if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) {
+    if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Context, 
+                                                              &Member)) {
       // Check whether we can reference this property.
       if (DiagnoseUseOfDecl(PD, MemberLoc))
         return ExprError();
@@ -1949,7 +1958,8 @@
     // Check protocols on qualified interfaces.
     for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(),
          E = IFTy->qual_end(); I != E; ++I)
-      if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) {
+      if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context,
+                                                               &Member)) {
         // Check whether we can reference this property.
         if (DiagnoseUseOfDecl(PD, MemberLoc))
           return ExprError();
@@ -1965,7 +1975,7 @@
     // shared with the code in ActOnInstanceMessage.
 
     Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
-    ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
+    ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel);
 
     // If this reference is in an @implementation, check for 'private' methods.
     if (!Getter)
@@ -1988,7 +1998,7 @@
     Selector SetterSel = 
       SelectorTable::constructSetterName(PP.getIdentifierTable(), 
                                          PP.getSelectorTable(), &Member);
-    ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
+    ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(Context, SetterSel);
     if (!Setter) {
       // If this reference is in an @implementation, also check for 'private'
       // methods.
@@ -2027,7 +2037,7 @@
   if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) {
     // Check protocols on qualified interfaces.
     Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
-    if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel)) {
+    if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) {
       if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
         // Check the use of this declaration
         if (DiagnoseUseOfDecl(PD, MemberLoc))
@@ -2059,7 +2069,7 @@
       ObjCInterfaceDecl *IFace = MD->getClassInterface();
       ObjCMethodDecl *Getter;
       // FIXME: need to also look locally in the implementation.
-      if ((Getter = IFace->lookupClassMethod(Sel))) {
+      if ((Getter = IFace->lookupClassMethod(Context, Sel))) {
         // Check the use of this method.
         if (DiagnoseUseOfDecl(Getter, MemberLoc))
           return ExprError();
@@ -2069,7 +2079,7 @@
       Selector SetterSel = 
         SelectorTable::constructSetterName(PP.getIdentifierTable(), 
                                            PP.getSelectorTable(), &Member);
-      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
+      ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel);
       if (!Setter) {
         // If this reference is in an @implementation, also check for 'private'
         // methods.
@@ -2481,7 +2491,7 @@
       // GCC cast to union extension
       RecordDecl *RD = castType->getAsRecordType()->getDecl();
       RecordDecl::field_iterator Field, FieldEnd;
-      for (Field = RD->field_begin(), FieldEnd = RD->field_end();
+      for (Field = RD->field_begin(Context), FieldEnd = RD->field_end(Context);
            Field != FieldEnd; ++Field) {
         if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() ==
             Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) {
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 361434a..4a5bfd5 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -454,7 +454,7 @@
                                   bool AllowMissing, FunctionDecl *&Operator)
 {
   DeclContext::lookup_iterator Alloc, AllocEnd;
-  llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name);
+  llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Context, Name);
   if (Alloc == AllocEnd) {
     if (AllowMissing)
       return false;
@@ -561,7 +561,7 @@
   // Check if this function is already declared.
   {
     DeclContext::lookup_iterator Alloc, AllocEnd;
-    for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
+    for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Context, Name);
          Alloc != AllocEnd; ++Alloc) {
       // FIXME: Do we need to check for default arguments here?
       FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
@@ -584,7 +584,7 @@
   // FIXME: Also add this declaration to the IdentifierResolver, but
   // make sure it is at the end of the chain to coincide with the
   // global scope.
-  ((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
+  ((DeclContext *)TUScope->getEntity())->addDecl(Context, Alloc);
 }
 
 /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp
index 54ed709..54a18ac 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -238,7 +238,7 @@
     // But only in the root. This matches gcc's behaviour and what the
     // runtime expects.
     if (!Method && !ClassDecl->getSuperClass()) {
-      Method = ClassDecl->lookupInstanceMethod(Sel);
+      Method = ClassDecl->lookupInstanceMethod(Context, Sel);
       // Look through local category implementations associated 
       // with the root class.
       if (!Method) 
@@ -282,7 +282,7 @@
   // Search for a declared property first.
   
   Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
-  ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
+  ObjCMethodDecl *Getter = IFace->lookupClassMethod(Context, Sel);
 
   // If this reference is in an @implementation, check for 'private' methods.
   if (!Getter)
@@ -304,7 +304,7 @@
     SelectorTable::constructSetterName(PP.getIdentifierTable(), 
                                        PP.getSelectorTable(), &propertyName);
     
-  ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
+  ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel);
   if (!Setter) {
     // If this reference is in an @implementation, also check for 'private'
     // methods.
@@ -422,7 +422,7 @@
   assert(ClassDecl && "missing interface declaration");
   ObjCMethodDecl *Method = 0;
   QualType returnType;
-  Method = ClassDecl->lookupClassMethod(Sel);
+  Method = ClassDecl->lookupClassMethod(Context, Sel);
   
   // If we have an implementation in scope, check "private" methods.
   if (!Method)
@@ -473,7 +473,7 @@
       // If we have an interface in scope, check 'super' methods.
       if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
         if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
-          Method = SuperDecl->lookupInstanceMethod(Sel);
+          Method = SuperDecl->lookupInstanceMethod(Context, Sel);
           
           if (!Method) 
             // If we have implementations in scope, check "private" methods.
@@ -512,7 +512,7 @@
     if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
       if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
         // First check the public methods in the class interface.
-        Method = ClassDecl->lookupClassMethod(Sel);
+        Method = ClassDecl->lookupClassMethod(Context, Sel);
         
         if (!Method)
           Method = LookupPrivateClassMethod(Sel, ClassDecl);
@@ -546,10 +546,10 @@
     // Search protocols for instance methods.
     for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
       ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
-      if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
+      if (PDecl && (Method = PDecl->lookupInstanceMethod(Context, Sel)))
         break;
       // Since we aren't supporting "Class<foo>", look for a class method.
-      if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
+      if (PDecl && (Method = PDecl->lookupClassMethod(Context, Sel)))
         break;
     }
   } else if (const ObjCInterfaceType *OCIType = 
@@ -560,13 +560,13 @@
     // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
     // faster than the following method (which can do *many* linear searches). 
     // The idea is to add class info to InstanceMethodPool.
-    Method = ClassDecl->lookupInstanceMethod(Sel);
+    Method = ClassDecl->lookupInstanceMethod(Context, Sel);
     
     if (!Method) {
       // Search protocol qualifiers.
       for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
            E = OCIType->qual_end(); QI != E; ++QI) {
-        if ((Method = (*QI)->lookupInstanceMethod(Sel)))
+        if ((Method = (*QI)->lookupInstanceMethod(Context, Sel)))
           break;
       }
     }
diff --git a/lib/Sema/SemaInherit.cpp b/lib/Sema/SemaInherit.cpp
index c01430c..8239f54 100644
--- a/lib/Sema/SemaInherit.cpp
+++ b/lib/Sema/SemaInherit.cpp
@@ -179,7 +179,7 @@
       FoundPathToThisBase 
         = (Context.getCanonicalType(BaseSpec->getType()) == Criteria.Base);
     } else {
-      Paths.ScratchPath.Decls = BaseRecord->lookup(Criteria.Name);
+      Paths.ScratchPath.Decls = BaseRecord->lookup(Context, Criteria.Name);
       while (Paths.ScratchPath.Decls.first != Paths.ScratchPath.Decls.second) {
         if (isAcceptableLookupResult(*Paths.ScratchPath.Decls.first,
                                      Criteria.NameKind, Criteria.IDNS)) {
diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp
index f8890c3..7da1f6b 100644
--- a/lib/Sema/SemaInit.cpp
+++ b/lib/Sema/SemaInit.cpp
@@ -324,8 +324,9 @@
   
   if (const RecordType *RType = ILE->getType()->getAsRecordType()) {
     unsigned Init = 0, NumInits = ILE->getNumInits();
-    for (RecordDecl::field_iterator Field = RType->getDecl()->field_begin(),
-                                 FieldEnd = RType->getDecl()->field_end();
+    for (RecordDecl::field_iterator 
+           Field = RType->getDecl()->field_begin(SemaRef.Context),
+           FieldEnd = RType->getDecl()->field_end(SemaRef.Context);
          Field != FieldEnd; ++Field) {
       if (Field->isUnnamedBitfield())
         continue;
@@ -431,8 +432,9 @@
 int InitListChecker::numStructUnionElements(QualType DeclType) {
   RecordDecl *structDecl = DeclType->getAsRecordType()->getDecl();
   int InitializableMembers = 0;
-  for (RecordDecl::field_iterator Field = structDecl->field_begin(),
-                               FieldEnd = structDecl->field_end();
+  for (RecordDecl::field_iterator 
+         Field = structDecl->field_begin(SemaRef.Context),
+         FieldEnd = structDecl->field_end(SemaRef.Context);
        Field != FieldEnd; ++Field) {
     if ((*Field)->getIdentifier() || !(*Field)->isBitField())
       ++InitializableMembers;
@@ -559,7 +561,7 @@
   } else if (DeclType->isAggregateType()) {
     if (DeclType->isRecordType()) {
       RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
-      CheckStructUnionTypes(IList, DeclType, RD->field_begin(), 
+      CheckStructUnionTypes(IList, DeclType, RD->field_begin(SemaRef.Context), 
                             SubobjectIsDesignatorContext, Index,
                             StructuredList, StructuredIndex,
                             TopLevelObject);
@@ -927,7 +929,7 @@
   if (DeclType->isUnionType() && IList->getNumInits() == 0) {
     // Value-initialize the first named member of the union.
     RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
-    for (RecordDecl::field_iterator FieldEnd = RD->field_end();
+    for (RecordDecl::field_iterator FieldEnd = RD->field_end(SemaRef.Context);
          Field != FieldEnd; ++Field) {
       if (Field->getDeclName()) {
         StructuredList->setInitializedFieldInUnion(*Field);
@@ -942,7 +944,7 @@
   // because an error should get printed out elsewhere. It might be
   // worthwhile to skip over the rest of the initializer, though.
   RecordDecl *RD = DeclType->getAsRecordType()->getDecl();
-  RecordDecl::field_iterator FieldEnd = RD->field_end();
+  RecordDecl::field_iterator FieldEnd = RD->field_end(SemaRef.Context);
   bool InitializedSomething = false;
   while (Index < IList->getNumInits()) {
     Expr *Init = IList->getInit(Index);
@@ -1136,8 +1138,9 @@
     // need to compute the field's index.
     IdentifierInfo *FieldName = D->getFieldName();
     unsigned FieldIndex = 0;
-    RecordDecl::field_iterator Field = RT->getDecl()->field_begin(),
-                            FieldEnd = RT->getDecl()->field_end();
+    RecordDecl::field_iterator 
+      Field = RT->getDecl()->field_begin(SemaRef.Context),
+      FieldEnd = RT->getDecl()->field_end(SemaRef.Context);
     for (; Field != FieldEnd; ++Field) {
       if (Field->isUnnamedBitfield())
         continue;
@@ -1151,7 +1154,8 @@
     if (Field == FieldEnd) {
       // We did not find the field we're looking for. Produce a
       // suitable diagnostic and return a failure.
-      DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
+      DeclContext::lookup_result Lookup 
+        = RT->getDecl()->lookup(SemaRef.Context, FieldName);
       if (Lookup.first == Lookup.second) {
         // Name lookup didn't find anything.
         SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
@@ -1478,7 +1482,8 @@
     if (RDecl->isUnion())
       NumElements = 1;
     else
-      NumElements = std::distance(RDecl->field_begin(), RDecl->field_end());
+      NumElements = std::distance(RDecl->field_begin(SemaRef.Context), 
+                                  RDecl->field_end(SemaRef.Context));
   }
 
   if (NumElements < NumInits)
diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp
index d85123c..0b11d9c 100644
--- a/lib/Sema/SemaLookup.cpp
+++ b/lib/Sema/SemaLookup.cpp
@@ -57,23 +57,25 @@
 /// AddNamespaceUsingDirectives - Adds all UsingDirectiveDecl's to heap UDirs
 /// (ordered by common ancestors), found in namespace NS,
 /// including all found (recursively) in their nominated namespaces.
-void AddNamespaceUsingDirectives(DeclContext *NS,
+void AddNamespaceUsingDirectives(ASTContext &Context, 
+                                 DeclContext *NS,
                                  UsingDirectivesTy &UDirs,
                                  NamespaceSet &Visited) {
   DeclContext::udir_iterator I, End;
 
-  for (llvm::tie(I, End) = NS->getUsingDirectives(); I !=End; ++I) {
+  for (llvm::tie(I, End) = NS->getUsingDirectives(Context); I !=End; ++I) {
     UDirs.push_back(*I);
     std::push_heap(UDirs.begin(), UDirs.end(), UsingDirAncestorCompare());
     NamespaceDecl *Nominated = (*I)->getNominatedNamespace();
     if (Visited.insert(Nominated).second)
-      AddNamespaceUsingDirectives(Nominated, UDirs, /*ref*/ Visited);
+      AddNamespaceUsingDirectives(Context, Nominated, UDirs, /*ref*/ Visited);
   }
 }
 
 /// AddScopeUsingDirectives - Adds all UsingDirectiveDecl's found in Scope S,
 /// including all found in the namespaces they nominate.
-static void AddScopeUsingDirectives(Scope *S, UsingDirectivesTy &UDirs) {
+static void AddScopeUsingDirectives(ASTContext &Context, Scope *S, 
+                                    UsingDirectivesTy &UDirs) {
   NamespaceSet VisitedNS;
 
   if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) {
@@ -81,7 +83,7 @@
     if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(Ctx))
       VisitedNS.insert(NS);
 
-    AddNamespaceUsingDirectives(Ctx, UDirs, /*ref*/ VisitedNS);
+    AddNamespaceUsingDirectives(Context, Ctx, UDirs, /*ref*/ VisitedNS);
 
   } else {
     Scope::udir_iterator I = S->using_directives_begin(),
@@ -95,7 +97,8 @@
       NamespaceDecl *Nominated = UD->getNominatedNamespace();
       if (!VisitedNS.count(Nominated)) {
         VisitedNS.insert(Nominated);
-        AddNamespaceUsingDirectives(Nominated, UDirs, /*ref*/ VisitedNS);
+        AddNamespaceUsingDirectives(Context, Nominated, UDirs, 
+                                    /*ref*/ VisitedNS);
       }
     }
   }
@@ -569,7 +572,7 @@
 
   // Perform qualified name lookup into the LookupCtx.
   DeclContext::lookup_iterator I, E;
-  for (llvm::tie(I, E) = NS->lookup(Name); I != E; ++I)
+  for (llvm::tie(I, E) = NS->lookup(Context, Name); I != E; ++I)
     if (Sema::isAcceptableLookupResult(*I, NameKind, IDNS)) {
       Results.push_back(Sema::LookupResult::CreateLookupResult(Context, I, E));
       break;
@@ -682,7 +685,7 @@
   UsingDirectivesTy UDirs;
   for (Scope *SC = Initial; SC; SC = SC->getParent())
     if (SC->getFlags() & Scope::DeclScope)
-      AddScopeUsingDirectives(SC, UDirs);
+      AddScopeUsingDirectives(Context, SC, UDirs);
 
   // Sort heapified UsingDirectiveDecls.
   std::sort_heap(UDirs.begin(), UDirs.end());
@@ -967,7 +970,7 @@
 
   // Perform qualified name lookup into the LookupCtx.
   DeclContext::lookup_iterator I, E;
-  for (llvm::tie(I, E) = LookupCtx->lookup(Name); I != E; ++I)
+  for (llvm::tie(I, E) = LookupCtx->lookup(Context, Name); I != E; ++I)
     if (isAcceptableLookupResult(*I, NameKind, IDNS))
       return LookupResult::CreateLookupResult(Context, I, E);
 
@@ -1544,7 +1547,7 @@
     //        namespaces even if they are not visible during an ordinary
     //        lookup (11.4).
     DeclContext::lookup_iterator I, E;
-    for (llvm::tie(I, E) = (*NS)->lookup(Name); I != E; ++I) {
+    for (llvm::tie(I, E) = (*NS)->lookup(Context, Name); I != E; ++I) {
       FunctionDecl *Func = dyn_cast<FunctionDecl>(*I);
       if (!Func)
         break;
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp
index 3afd454..0a12a71 100644
--- a/lib/Sema/SemaOverload.cpp
+++ b/lib/Sema/SemaOverload.cpp
@@ -1334,7 +1334,8 @@
         = Context.DeclarationNames.getCXXConstructorName(
                           Context.getCanonicalType(ToType).getUnqualifiedType());
       DeclContext::lookup_iterator Con, ConEnd;
-      for (llvm::tie(Con, ConEnd) = ToRecordDecl->lookup(ConstructorName);
+      for (llvm::tie(Con, ConEnd) 
+             = ToRecordDecl->lookup(Context, ConstructorName);
            Con != ConEnd; ++Con) {
         CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
         if (Constructor->isConvertingConstructor())
@@ -2395,7 +2396,7 @@
   // FIXME: Lookup in base classes, too!
   if (const RecordType *T1Rec = T1->getAsRecordType()) {
     DeclContext::lookup_const_iterator Oper, OperEnd;
-    for (llvm::tie(Oper, OperEnd) = T1Rec->getDecl()->lookup(OpName);
+    for (llvm::tie(Oper, OperEnd) = T1Rec->getDecl()->lookup(Context, OpName);
          Oper != OperEnd; ++Oper)
       AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], 
                          Args+1, NumArgs - 1, CandidateSet,
@@ -4033,7 +4034,7 @@
   OverloadCandidateSet CandidateSet;
   DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call);
   DeclContext::lookup_const_iterator Oper, OperEnd;
-  for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName);
+  for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(Context, OpName);
        Oper != OperEnd; ++Oper)
     AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, 
                        CandidateSet, /*SuppressUserConversions=*/false);
@@ -4234,7 +4235,8 @@
   const RecordType *BaseRecord = Base->getType()->getAsRecordType();
   
   DeclContext::lookup_const_iterator Oper, OperEnd;
-  for (llvm::tie(Oper, OperEnd) = BaseRecord->getDecl()->lookup(OpName);
+  for (llvm::tie(Oper, OperEnd) 
+         = BaseRecord->getDecl()->lookup(Context, OpName);
        Oper != OperEnd; ++Oper)
     AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet,
                        /*SuppressUserConversions=*/false);
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index d30726c..6e6021a 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -2097,7 +2097,7 @@
   // Add the specialization into its lexical context, so that it can
   // be seen when iterating through the list of declarations in that
   // context. However, specializations are not found by name lookup.
-  CurContext->addDecl(Specialization);
+  CurContext->addDecl(Context, Specialization);
   return DeclPtrTy::make(Specialization);
 }
 
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index a041d06..1a67fd1 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -728,8 +728,9 @@
     Invalid = true;
 
   llvm::SmallVector<DeclPtrTy, 32> Fields;
-  for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
-       MemberEnd = Pattern->decls_end(); Member != MemberEnd; ++Member) {
+  for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
+         MemberEnd = Pattern->decls_end(Context); 
+       Member != MemberEnd; ++Member) {
     Decl *NewMember = InstantiateDecl(*Member, Instantiation,
                                       TemplateArgs, NumTemplateArgs);
     if (NewMember) {
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index e7210b3..4639511 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -99,7 +99,7 @@
   if (Invalid)
     Typedef->setInvalidDecl();
 
-  Owner->addDecl(Typedef);
+  Owner->addDecl(SemaRef.Context, Typedef);
   return Typedef;
 }
 
@@ -127,7 +127,7 @@
   if (SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration))
     Var->setInvalidDecl();
 
-  Owner->addDecl(Var);
+  Owner->addDecl(SemaRef.Context, Var);
 
   if (D->getInit()) {
     OwningExprResult Init 
@@ -187,7 +187,7 @@
     if (Invalid)
       Field->setInvalidDecl();
     
-    Owner->addDecl(Field);
+    Owner->addDecl(SemaRef.Context, Field);
   }
 
   return Field;
@@ -214,14 +214,14 @@
                                     D->getLocation(), D->getIdentifier(),
                                     /*PrevDecl=*/0);
   Enum->setAccess(D->getAccess());
-  Owner->addDecl(Enum);
+  Owner->addDecl(SemaRef.Context, Enum);
   Enum->startDefinition();
 
   llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators;
 
   EnumConstantDecl *LastEnumConst = 0;
-  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
-         ECEnd = D->enumerator_end();
+  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context),
+         ECEnd = D->enumerator_end(SemaRef.Context);
        EC != ECEnd; ++EC) {
     // The specified value for the enumerator.
     OwningExprResult Value = SemaRef.Owned((Expr *)0);
@@ -248,7 +248,7 @@
     }
 
     if (EnumConst) {
-      Enum->addDecl(EnumConst);
+      Enum->addDecl(SemaRef.Context, EnumConst);
       Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
       LastEnumConst = EnumConst;
     }
@@ -281,7 +281,7 @@
   else
     Record->setDescribedClassTemplate(D->getDescribedClassTemplate());
 
-  Owner->addDecl(Record);
+  Owner->addDecl(SemaRef.Context, Record);
   return Record;
 }
 
@@ -327,7 +327,7 @@
     Method->setInvalidDecl();
 
   if (!Method->isInvalidDecl() || !PrevDecl)
-    Owner->addDecl(Method);
+    Owner->addDecl(SemaRef.Context, Method);
   return Method;
 }
 
@@ -371,7 +371,7 @@
     Constructor->setInvalidDecl();
 
   if (!Constructor->isInvalidDecl())
-    Owner->addDecl(Constructor);
+    Owner->addDecl(SemaRef.Context, Constructor);
   return Constructor;
 }
 
@@ -399,7 +399,7 @@
   if (SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration,
                                        /*FIXME:*/OverloadableAttrRequired))
     Destructor->setInvalidDecl();
-  Owner->addDecl(Destructor);
+  Owner->addDecl(SemaRef.Context, Destructor);
   return Destructor;
 }
 
@@ -429,7 +429,7 @@
   if (SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration,
                                        /*FIXME:*/OverloadableAttrRequired))
     Conversion->setInvalidDecl();
-  Owner->addDecl(Conversion);
+  Owner->addDecl(SemaRef.Context, Conversion);
   return Conversion;  
 }
 
diff --git a/tools/clang-cc/ASTConsumers.cpp b/tools/clang-cc/ASTConsumers.cpp
index 1b5cdd3..74683e3 100644
--- a/tools/clang-cc/ASTConsumers.cpp
+++ b/tools/clang-cc/ASTConsumers.cpp
@@ -125,8 +125,10 @@
     Out << ";\n";
   } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
     Out << "enum " << ED->getNameAsString() << " {\n";
-    for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(),
-                                    EEnd = ED->enumerator_end();
+    // FIXME: Shouldn't pass a NULL context
+    ASTContext *Context = 0;
+    for (EnumDecl::enumerator_iterator E = ED->enumerator_begin(*Context),
+                                    EEnd = ED->enumerator_end(*Context);
          E != EEnd; ++E)
       Out << "  " << (*E)->getNameAsString() << ",\n";
     Out << "};\n";
@@ -139,8 +141,10 @@
 
     Out << " {\n";
     ChangeIndent(1);
-    for (DeclContext::decl_iterator i = TD->decls_begin();
-         i != TD->decls_end();
+    // FIXME: Shouldn't pass a NULL context
+    ASTContext *Context = 0;
+    for (DeclContext::decl_iterator i = TD->decls_begin(*Context);
+         i != TD->decls_end(*Context);
          ++i)
       PrintDecl(*i);    
     ChangeIndent(-1);
@@ -198,8 +202,10 @@
 void DeclPrinter::Print(NamespaceDecl *NS) {
   Out << "namespace " << NS->getNameAsString() << " {\n";
   ChangeIndent(1);
-  for (DeclContext::decl_iterator i = NS->decls_begin();
-       i != NS->decls_end();
+  // FIXME: Shouldn't pass a NULL context
+  ASTContext *Context = 0;
+  for (DeclContext::decl_iterator i = NS->decls_begin(*Context);
+       i != NS->decls_end(*Context);
        ++i)
     PrintDecl(*i);    
   ChangeIndent(-1);
@@ -278,8 +284,10 @@
     ChangeIndent(1);
   }
 
-  for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(), 
-                                   DEnd = LS->decls_end();
+  // FIXME: Should not use a NULL DeclContext!
+  ASTContext *Context = 0;
+  for (LinkageSpecDecl::decl_iterator D = LS->decls_begin(*Context), 
+                                   DEnd = LS->decls_end(*Context);
        D != DEnd; ++D)
     PrintDecl(*D);
 
@@ -389,16 +397,18 @@
     Out << "}\n";
   }
   
-  for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(),
-       E = OID->prop_end(); I != E; ++I)
+  // FIXME: Should not use a NULL DeclContext!
+  ASTContext *Context = 0;
+  for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(*Context),
+       E = OID->prop_end(*Context); I != E; ++I)
     PrintObjCPropertyDecl(*I);
   bool eol_needed = false;
-  for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(),
-       E = OID->classmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::classmeth_iterator I = OID->classmeth_begin(*Context),
+       E = OID->classmeth_end(*Context); I != E; ++I)
     eol_needed = true, PrintObjCMethodDecl(*I);
   
-  for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(),
-       E = OID->instmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::instmeth_iterator I = OID->instmeth_begin(*Context),
+       E = OID->instmeth_end(*Context); I != E; ++I)
     eol_needed = true, PrintObjCMethodDecl(*I);
   
   Out << (eol_needed ? "\n@end\n" : "@end\n");
@@ -408,8 +418,10 @@
 void DeclPrinter::PrintObjCProtocolDecl(ObjCProtocolDecl *PID) {
   Out << "@protocol " << PID->getNameAsString() << '\n';
   
-  for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(),
-       E = PID->prop_end(); I != E; ++I)
+  // FIXME: Should not use a NULL DeclContext!
+  ASTContext *Context = 0;
+  for (ObjCProtocolDecl::prop_iterator I = PID->prop_begin(*Context),
+       E = PID->prop_end(*Context); I != E; ++I)
     PrintObjCPropertyDecl(*I);
   Out << "@end\n";
   // FIXME: implement the rest...
@@ -427,12 +439,14 @@
 }
 
 void DeclPrinter::PrintObjCCategoryDecl(ObjCCategoryDecl *PID) {
+  // FIXME: Should not use a NULL DeclContext!
+  ASTContext *Context = 0;
   Out << "@interface " 
       << PID->getClassInterface()->getNameAsString()
       << '(' << PID->getNameAsString() << ");\n";
   // Output property declarations.
-  for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(),
-       E = PID->prop_end(); I != E; ++I)
+  for (ObjCCategoryDecl::prop_iterator I = PID->prop_begin(*Context),
+       E = PID->prop_end(*Context); I != E; ++I)
     PrintObjCPropertyDecl(*I);
   Out << "@end\n";
   
@@ -867,7 +881,10 @@
   Out << "\n";
 
   // Print decls in the DeclContext.
-  for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
+  // FIXME: Should not use a NULL DeclContext!
+  ASTContext *Context = 0;
+  for (DeclContext::decl_iterator I = DC->decls_begin(*Context), 
+         E = DC->decls_end(*Context);
        I != E; ++I) {
     for (unsigned i = 0; i < Indentation; ++i)
       Out << "  ";
diff --git a/tools/clang-cc/ASTConsumers.h b/tools/clang-cc/ASTConsumers.h
index fd8416d..970dfec 100644
--- a/tools/clang-cc/ASTConsumers.h
+++ b/tools/clang-cc/ASTConsumers.h
@@ -67,7 +67,7 @@
 ASTConsumer *CreateASTSerializer(const std::string& InFile,
                                  const std::string& EmitDir,
                                  Diagnostic &Diags);
-
+  
 ASTConsumer *CreateBlockRewriter(const std::string& InFile,
                                  const std::string& OutFile,
                                  Diagnostic &Diags,
diff --git a/tools/clang-cc/AnalysisConsumer.cpp b/tools/clang-cc/AnalysisConsumer.cpp
index bc38593..c39b1bc 100644
--- a/tools/clang-cc/AnalysisConsumer.cpp
+++ b/tools/clang-cc/AnalysisConsumer.cpp
@@ -459,7 +459,8 @@
   if (!ObjCImplementationActions.empty()) {
     TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
     
-    for (DeclContext::decl_iterator I = TUD->decls_begin(),E = TUD->decls_end();
+    for (DeclContext::decl_iterator I = TUD->decls_begin(C),
+                                    E = TUD->decls_end(C);
          I != E; ++I)
       if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
         HandleCode(ID, 0, ObjCImplementationActions);
diff --git a/tools/clang-cc/RewriteBlocks.cpp b/tools/clang-cc/RewriteBlocks.cpp
index f302913..3610da6 100644
--- a/tools/clang-cc/RewriteBlocks.cpp
+++ b/tools/clang-cc/RewriteBlocks.cpp
@@ -310,29 +310,41 @@
 }
 
 void RewriteBlocks::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) {
-  for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), 
-       E = ClassDecl->instmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::instmeth_iterator 
+         I = ClassDecl->instmeth_begin(*Context), 
+         E = ClassDecl->instmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDecl(*I);
-  for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), 
-       E = ClassDecl->classmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::classmeth_iterator 
+         I = ClassDecl->classmeth_begin(*Context), 
+         E = ClassDecl->classmeth_end(*Context);
+       I != E; ++I)
     RewriteMethodDecl(*I);
 }
 
 void RewriteBlocks::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) {
-  for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), 
-       E = CatDecl->instmeth_end(); I != E; ++I)
+  for (ObjCCategoryDecl::instmeth_iterator 
+         I = CatDecl->instmeth_begin(*Context), 
+         E = CatDecl->instmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDecl(*I);
-  for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), 
-       E = CatDecl->classmeth_end(); I != E; ++I)
+  for (ObjCCategoryDecl::classmeth_iterator 
+         I = CatDecl->classmeth_begin(*Context), 
+         E = CatDecl->classmeth_end(*Context);
+       I != E; ++I)
     RewriteMethodDecl(*I);
 }
 
 void RewriteBlocks::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) {
-  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 
-       E = PDecl->instmeth_end(); I != E; ++I)
+  for (ObjCProtocolDecl::instmeth_iterator 
+         I = PDecl->instmeth_begin(*Context), 
+         E = PDecl->instmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDecl(*I);
-  for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 
-       E = PDecl->classmeth_end(); I != E; ++I)
+  for (ObjCProtocolDecl::classmeth_iterator 
+         I = PDecl->classmeth_begin(*Context), 
+         E = PDecl->classmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDecl(*I);
 }
 
@@ -1138,8 +1150,8 @@
   }
   if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
     if (RD->isDefinition()) {
-      for (RecordDecl::field_iterator i = RD->field_begin(), 
-             e = RD->field_end(); i != e; ++i) {
+      for (RecordDecl::field_iterator i = RD->field_begin(*Context), 
+             e = RD->field_end(*Context); i != e; ++i) {
         FieldDecl *FD = *i;
         if (isBlockPointerType(FD->getType()))
           RewriteBlockPointerDecl(FD);
diff --git a/tools/clang-cc/RewriteObjC.cpp b/tools/clang-cc/RewriteObjC.cpp
index 5fcecc6..44c0eb3 100644
--- a/tools/clang-cc/RewriteObjC.cpp
+++ b/tools/clang-cc/RewriteObjC.cpp
@@ -584,8 +584,8 @@
     RewriteForwardProtocolDecl(FP);
   } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) {
     // Recurse into linkage specifications
-    for (DeclContext::decl_iterator DI = LSD->decls_begin(),
-                                 DIEnd = LSD->decls_end();
+    for (DeclContext::decl_iterator DI = LSD->decls_begin(*Context),
+                                 DIEnd = LSD->decls_end(*Context);
          DI != DIEnd; ++DI)
       HandleTopLevelSingleDecl(*DI);
   }
@@ -791,11 +791,15 @@
   // FIXME: handle category headers that are declared across multiple lines.
   ReplaceText(LocStart, 0, "// ", 3);
   
-  for (ObjCCategoryDecl::instmeth_iterator I = CatDecl->instmeth_begin(), 
-       E = CatDecl->instmeth_end(); I != E; ++I)
+  for (ObjCCategoryDecl::instmeth_iterator 
+         I = CatDecl->instmeth_begin(*Context), 
+         E = CatDecl->instmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
-  for (ObjCCategoryDecl::classmeth_iterator I = CatDecl->classmeth_begin(), 
-       E = CatDecl->classmeth_end(); I != E; ++I)
+  for (ObjCCategoryDecl::classmeth_iterator 
+         I = CatDecl->classmeth_begin(*Context), 
+         E = CatDecl->classmeth_end(*Context);
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
 
   // Lastly, comment out the @end.
@@ -810,11 +814,15 @@
   // FIXME: handle protocol headers that are declared across multiple lines.
   ReplaceText(LocStart, 0, "// ", 3);
   
-  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 
-       E = PDecl->instmeth_end(); I != E; ++I)
+  for (ObjCProtocolDecl::instmeth_iterator 
+         I = PDecl->instmeth_begin(*Context), 
+         E = PDecl->instmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
-  for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 
-       E = PDecl->classmeth_end(); I != E; ++I)
+  for (ObjCProtocolDecl::classmeth_iterator
+         I = PDecl->classmeth_begin(*Context), 
+         E = PDecl->classmeth_end(*Context);
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
 
   // Lastly, comment out the @end.
@@ -1038,14 +1046,18 @@
   }
   SynthesizeObjCInternalStruct(ClassDecl, ResultStr);
     
-  for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), 
-       E = ClassDecl->prop_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(*Context), 
+         E = ClassDecl->prop_end(*Context); I != E; ++I)
     RewriteProperty(*I);
-  for (ObjCInterfaceDecl::instmeth_iterator I = ClassDecl->instmeth_begin(), 
-       E = ClassDecl->instmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::instmeth_iterator 
+         I = ClassDecl->instmeth_begin(*Context), 
+         E = ClassDecl->instmeth_end(*Context);
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
-  for (ObjCInterfaceDecl::classmeth_iterator I = ClassDecl->classmeth_begin(), 
-       E = ClassDecl->classmeth_end(); I != E; ++I)
+  for (ObjCInterfaceDecl::classmeth_iterator 
+         I = ClassDecl->classmeth_begin(*Context), 
+         E = ClassDecl->classmeth_end(*Context); 
+       I != E; ++I)
     RewriteMethodDeclaration(*I);
 
   // Lastly, comment out the @end.
@@ -1136,7 +1148,9 @@
         dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
       // lookup which class implements the instance variable.
       ObjCInterfaceDecl *clsDeclared = 0;
-      iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared);
+      iFaceDecl->getDecl()->lookupInstanceVariable(*Context, 
+                                                   D->getIdentifier(), 
+                                                   clsDeclared);
       assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
       
       // Synthesize an explicit cast to gain access to the ivar.
@@ -1180,7 +1194,9 @@
       ObjCInterfaceType *iFaceDecl = dyn_cast<ObjCInterfaceType>(pType->getPointeeType());
       // lookup which class implements the instance variable.
       ObjCInterfaceDecl *clsDeclared = 0;
-      iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), clsDeclared);
+      iFaceDecl->getDecl()->lookupInstanceVariable(*Context, 
+                                                   D->getIdentifier(), 
+                                                   clsDeclared);
       assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class");
       
       // Synthesize an explicit cast to gain access to the ivar.
@@ -2187,7 +2203,8 @@
 
     // Create fields
     for (unsigned i = 0; i < 2; ++i) {
-      SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, 
+      SuperStructDecl->addDecl(*Context,
+                               FieldDecl::Create(*Context, SuperStructDecl, 
                                                  SourceLocation(), 0, 
                                                  FieldTypes[i], /*BitWidth=*/0,
                                                  /*Mutable=*/false));
@@ -2216,7 +2233,8 @@
 
     // Create fields
     for (unsigned i = 0; i < 4; ++i) {
-      ConstantStringDecl->addDecl(FieldDecl::Create(*Context, 
+      ConstantStringDecl->addDecl(*Context,
+                                  FieldDecl::Create(*Context, 
                                                     ConstantStringDecl, 
                                                     SourceLocation(), 0,
                                                     FieldTypes[i], 
@@ -2867,9 +2885,9 @@
     if (ObjCSynthesizedProtocols.count(PDecl))
       continue;
            
-    if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
-      unsigned NumMethods = std::distance(PDecl->instmeth_begin(),
-                                          PDecl->instmeth_end());
+    if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) {
+      unsigned NumMethods = std::distance(PDecl->instmeth_begin(*Context),
+                                          PDecl->instmeth_end(*Context));
       /* struct _objc_protocol_method_list {
        int protocol_method_count;
        struct protocol_methods protocols[];
@@ -2885,9 +2903,11 @@
         "{\n\t" + utostr(NumMethods) + "\n";
       
       // Output instance methods declared in this protocol.
-      for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), 
-           E = PDecl->instmeth_end(); I != E; ++I) {
-        if (I == PDecl->instmeth_begin())
+      for (ObjCProtocolDecl::instmeth_iterator 
+             I = PDecl->instmeth_begin(*Context), 
+             E = PDecl->instmeth_end(*Context); 
+           I != E; ++I) {
+        if (I == PDecl->instmeth_begin(*Context))
           Result += "\t  ,{{(SEL)\"";
         else
           Result += "\t  ,{(SEL)\"";
@@ -2902,8 +2922,8 @@
     }
     
     // Output class methods declared in this protocol.
-    unsigned NumMethods = std::distance(PDecl->classmeth_begin(),
-                                        PDecl->classmeth_end());
+    unsigned NumMethods = std::distance(PDecl->classmeth_begin(*Context),
+                                        PDecl->classmeth_end(*Context));
     if (NumMethods > 0) {
       /* struct _objc_protocol_method_list {
        int protocol_method_count;
@@ -2922,9 +2942,11 @@
       Result += "\n";
       
       // Output instance methods declared in this protocol.
-      for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), 
-           E = PDecl->classmeth_end(); I != E; ++I) {
-        if (I == PDecl->classmeth_begin())
+      for (ObjCProtocolDecl::classmeth_iterator 
+             I = PDecl->classmeth_begin(*Context), 
+             E = PDecl->classmeth_end(*Context);
+           I != E; ++I) {
+        if (I == PDecl->classmeth_begin(*Context))
           Result += "\t  ,{{(SEL)\"";
         else
           Result += "\t  ,{(SEL)\"";
@@ -2967,14 +2989,14 @@
       "{\n\t0, \"";
     Result += PDecl->getNameAsString();
     Result += "\", 0, ";
-    if (PDecl->instmeth_begin() != PDecl->instmeth_end()) {
+    if (PDecl->instmeth_begin(*Context) != PDecl->instmeth_end(*Context)) {
       Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_";
       Result += PDecl->getNameAsString();
       Result += ", ";
     }
     else
       Result += "0, ";
-    if (PDecl->classmeth_begin() != PDecl->classmeth_end()) {
+    if (PDecl->classmeth_begin(*Context) != PDecl->classmeth_end(*Context)) {
       Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_";
       Result += PDecl->getNameAsString();
       Result += "\n";
@@ -4507,8 +4529,8 @@
   }
   if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
     if (RD->isDefinition()) {
-      for (RecordDecl::field_iterator i = RD->field_begin(), 
-             e = RD->field_end(); i != e; ++i) {
+      for (RecordDecl::field_iterator i = RD->field_begin(*Context), 
+             e = RD->field_end(*Context); i != e; ++i) {
         FieldDecl *FD = *i;
         if (isTopLevelBlockPointerType(FD->getType()))
           RewriteBlockPointerDecl(FD);
diff --git a/tools/clang-cc/SerializationTest.cpp b/tools/clang-cc/SerializationTest.cpp
index b4fd8d7..708328d 100644
--- a/tools/clang-cc/SerializationTest.cpp
+++ b/tools/clang-cc/SerializationTest.cpp
@@ -72,7 +72,8 @@
     llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
     
     TranslationUnitDecl *TUD = Ctx.getTranslationUnitDecl();
-    for (DeclContext::decl_iterator I = TUD->decls_begin(), E =TUD->decls_end();
+    for (DeclContext::decl_iterator I = TUD->decls_begin(Ctx), 
+                                    E = TUD->decls_end(Ctx);
          I != E; ++I)
       FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I));
   }
@@ -123,7 +124,8 @@
     llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
     
     TranslationUnitDecl *TUD = NewCtx->getTranslationUnitDecl();
-    for (DeclContext::decl_iterator I = TUD->decls_begin(), E = TUD->decls_end();
+    for (DeclContext::decl_iterator I = TUD->decls_begin(*NewCtx), 
+                                    E = TUD->decls_end(*NewCtx);
          I != E; ++I)
       FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I));
   }
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index 3c06ea7..b10323a 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -1597,7 +1597,7 @@
     ClearSourceMgr = true;
     break;
   }      
-    
+
   case PrintPreprocessedInput: {      // -E mode.
     llvm::TimeRegion Timer(ClangFrontendTimer);
     DoPrintPreprocessedInput(PP, OutputFile);
@@ -1683,7 +1683,6 @@
                                       PP.getSelectorTable(),
                                       /* FreeMemory = */ !DisableFree));
     
-    
     ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats);
     
     if (FixItRewrite)
@@ -1763,7 +1762,8 @@
 
   // FIXME: We need to inform Consumer about completed TagDecls as well.
   TranslationUnitDecl *TUD = Ctx->getTranslationUnitDecl();
-  for (DeclContext::decl_iterator I = TUD->decls_begin(), E = TUD->decls_end();
+  for (DeclContext::decl_iterator I = TUD->decls_begin(*Ctx), 
+                                  E = TUD->decls_end(*Ctx);
        I != E; ++I)
     Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
 }