Provide a Decl::getNonClosureContext to look through any "closure" (i.e.
block and, eventually, C++ lambda) contexts.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126252 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index be379d5..81df00d 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -465,6 +465,22 @@
 #endif
 }
 
+DeclContext *Decl::getNonClosureContext() {
+  DeclContext *DC = getDeclContext();
+
+  // This is basically "while (DC->isClosure()) DC = DC->getParent();"
+  // except that it's significantly more efficient to cast to a known
+  // decl type and call getDeclContext() than to call getParent().
+  do {
+    if (isa<BlockDecl>(DC)) {
+      DC = cast<BlockDecl>(DC)->getDeclContext();
+      continue;
+    }
+  } while (false);
+
+  assert(!DC->isClosure());
+  return DC;
+}
 
 //===----------------------------------------------------------------------===//
 // DeclContext Implementation