DeclContext is essentially a list of Decls and a lookup table (LookupPtr) but these are encapsulated.
E.g. LookupPtr is private. DeclContext::removeDecl has the responsibility to remove the given decl from the list and from the table too.
Template specializations cannot participate in normal (qualified) lookup.
Consequently no template specialization can be in the lookup table, but they will be in the list. When the lookup table is built or when a new Decl is added, then it is checked whether it is a template specialization and if so it is skipped from the lookup table.
Thus, whenever we want to remove a Decl from a DeclContext we must not reach the point to remove that from the lookup table (and that is what this patch do).
With respect to ASTImporter: At some point probably we will be reordering FriendDecls and possibly CXXMethodDecls in a RecordDecl at importDeclContext to be able to structurally compare two RecordDecls.
When that happens we most probably want to remove all Decls from a RecordDecl and then we would add them in the proper order.
Rather than make a hanging forward declaration, can you just move the definition of this function here?