A few of the issue I have been trying to track down and fix have been due to
the way LLDB lazily gets complete definitions for types within the debug info.
When we run across a class/struct/union definition in the DWARF, we will only
parse the full definition if we need to. This works fine for top level types
that are assigned directly to variables and arguments, but when we have a 
variable with a class, lets say "A" for this example, that has a member:
"B *m_b". Initially we don't need to hunt down a definition for this class
unless we are ever asked to do something with it ("expr m_b->getDecl()" for
example). With my previous approach to lazy type completion, we would be able
to take a "A *a" and get a complete type for it, but we wouldn't be able to
then do an "a->m_b->getDecl()" unless we always expanded all types within a
class prior to handing out the type. Expanding everything is very costly and
it would be great if there were a better way.

A few months ago I worked with the llvm/clang folks to have the 
ExternalASTSource class be able to complete classes if there weren't completed
yet:

class ExternalASTSource {
....

    virtual void
    CompleteType (clang::TagDecl *Tag);
    
    virtual void 
    CompleteType (clang::ObjCInterfaceDecl *Class);
};

This was great, because we can now have the class that is producing the AST
(SymbolFileDWARF and SymbolFileDWARFDebugMap) sign up as external AST sources
and the object that creates the forward declaration types can now also
complete them anywhere within the clang type system.

This patch makes a few major changes:
- lldb_private::Module classes now own the AST context. Previously the TypeList
  objects did.
- The DWARF parsers now sign up as an external AST sources so they can complete
  types.
- All of the pure clang type system wrapper code we have in LLDB (ClangASTContext,
  ClangASTType, and more) can now be iterating through children of any type,
  and if a class/union/struct type (clang::RecordType or ObjC interface) 
  is found that is incomplete, we can ask the AST to get the definition. 
- The SymbolFileDWARFDebugMap class now will create and use a single AST that
  all child SymbolFileDWARF classes will share (much like what happens when
  we have a complete linked DWARF for an executable).
  
We will need to modify some of the ClangUserExpression code to take more 
advantage of this completion ability in the near future. Meanwhile we should
be better off now that we can be accessing any children of variables through
pointers and always be able to resolve the clang type if needed.



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@123613 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ClangASTSource.cpp b/source/Expression/ClangASTSource.cpp
index 34103a6..3ef9e75 100644
--- a/source/Expression/ClangASTSource.cpp
+++ b/source/Expression/ClangASTSource.cpp
@@ -17,34 +17,34 @@
 using namespace clang;
 using namespace lldb_private;
 
-ClangASTSource::~ClangASTSource() {}
+ClangASTSource::~ClangASTSource() 
+{
+}
 
-void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
+void
+ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) 
+{
     // Tell Sema to ask us when looking into the translation unit's decl.
     m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
     m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
 }
 
-// These are only required for AST source that want to lazily load
-// the declarations (or parts thereof) that they return.
-Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; }
-Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; }
-
-// These are also optional, although it might help with ObjC
-// debugging if we have respectable signatures.  But a more
-// efficient interface (that didn't require scanning all files
-// for method signatures!) might help.
-Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); }
-uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; }
-CXXBaseSpecifier *ClangASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { return NULL; }
-
 // The core lookup interface.
-DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName
+DeclContext::lookup_result 
+ClangASTSource::FindExternalVisibleDeclsByName
 (
     const DeclContext *decl_ctx, 
     DeclarationName clang_decl_name
 ) 
 {
+    if (m_decl_map.GetImportInProgress())
+        return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
+        
+    std::string decl_name (clang_decl_name.getAsString());
+
+//    if (m_decl_map.DoingASTImport ())
+//      return DeclContext::lookup_result();
+//        
     switch (clang_decl_name.getNameKind()) {
     // Normal identifiers.
     case DeclarationName::Identifier:
@@ -75,7 +75,6 @@
       return DeclContext::lookup_result();
     }
 
-    std::string decl_name (clang_decl_name.getAsString());
 
     if (!m_decl_map.GetLookupsEnabled())
     {
@@ -112,25 +111,47 @@
     return result;
 }
 
-void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
+void
+ClangASTSource::CompleteType (TagDecl *tag_decl)
+{
+    puts(__PRETTY_FUNCTION__);
+}
+
+void
+ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl)
+{
+    puts(__PRETTY_FUNCTION__);
+}
+
+void 
+ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
 {
     return;
 }
 
 // This is used to support iterating through an entire lexical context,
 // which isn't something the debugger should ever need to do.
-bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, 
-                                              bool (*isKindWeWant)(Decl::Kind),
-                                              llvm::SmallVectorImpl<Decl*> &Decls) {
+bool
+ClangASTSource::FindExternalLexicalDecls
+(
+    const DeclContext *DC, 
+    bool (*isKindWeWant)(Decl::Kind),
+    llvm::SmallVectorImpl<Decl*> &Decls
+)
+{
 	// true is for error, that's good enough for me
 	return true;
 }
 
-clang::ASTContext *NameSearchContext::GetASTContext() {
+clang::ASTContext *
+NameSearchContext::GetASTContext() 
+{
     return &m_ast_source.m_ast_context;
 }
 
-clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
+clang::NamedDecl *
+NameSearchContext::AddVarDecl(void *type) 
+{
     IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
     
     assert (type && "Type for variable must be non-NULL!");
@@ -148,7 +169,9 @@
     return Decl;
 }
 
-clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
+clang::NamedDecl *
+NameSearchContext::AddFunDecl (void *type) 
+{
     clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
                                                            const_cast<DeclContext*>(m_decl_context),
                                                            SourceLocation(),
@@ -199,7 +222,8 @@
     return func_decl;
 }
 
-clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
+clang::NamedDecl *
+NameSearchContext::AddGenericFunDecl()
 {
     QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(),   // result
                                                                                NULL,                              // argument types
@@ -215,7 +239,8 @@
     return AddFunDecl(generic_function_type.getAsOpaquePtr());
 }
 
-clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
+clang::NamedDecl *
+NameSearchContext::AddTypeDecl(void *type)
 {
     QualType qual_type = QualType::getFromOpaquePtr(type);
 
diff --git a/source/Expression/ClangExpressionDeclMap.cpp b/source/Expression/ClangExpressionDeclMap.cpp
index 854524c..08468fa 100644
--- a/source/Expression/ClangExpressionDeclMap.cpp
+++ b/source/Expression/ClangExpressionDeclMap.cpp
@@ -1711,8 +1711,8 @@
             log->PutCString (strm.GetData());
         }
 
-        TypeFromUser user_type(type_sp->GetClangType(),
-                                   type_sp->GetClangAST());
+        TypeFromUser user_type (type_sp->GetClangType(),
+                                type_sp->GetClangAST());
             
         AddOneType(context, user_type, false);
     }
@@ -1748,18 +1748,9 @@
         return NULL;
     }
     
-    TypeList *type_list = var_type->GetTypeList();
+    clang::ASTContext *ast = var_type->GetClangASTContext().getASTContext();
     
-    if (!type_list)
-    {
-        if (log)
-            log->PutCString("Skipped a definition because the type has no associated type list");
-        return NULL;
-    }
-    
-    clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
-    
-    if (!exe_ast_ctx)
+    if (!ast)
     {
         if (log)
             log->PutCString("There is no AST context for the current execution context");
@@ -1780,20 +1771,18 @@
     }
     Error err;
     
-    if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, loclist_base_load_addr, NULL, *var_location.get(), &err))
+    if (!var_location_expr.Evaluate(&exe_ctx, ast, NULL, loclist_base_load_addr, NULL, *var_location.get(), &err))
     {
         if (log)
             log->Printf("Error evaluating location: %s", err.AsCString());
         return NULL;
     }
-    
-    clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
-    
+        
     void *type_to_use;
     
     if (parser_ast_context)
     {
-        type_to_use = GuardedCopyType(parser_ast_context, var_ast_context, var_opaque_type);
+        type_to_use = GuardedCopyType(parser_ast_context, ast, var_opaque_type);
         
         if (!type_to_use)
         {
@@ -1834,14 +1823,13 @@
     }
     
     if (user_type)
-        *user_type = TypeFromUser(var_opaque_type, var_ast_context);
+        *user_type = TypeFromUser(var_opaque_type, ast);
     
     return var_location.release();
 }
 
 void
-ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
-                                       Variable* var)
+ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, Variable* var)
 {
     assert (m_parser_vars.get());
     
@@ -1965,7 +1953,6 @@
 {
     lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
     
-    
     clang::Decl *copied_decl = ClangASTContext::CopyDecl (context.GetASTContext(),
                                                           namespace_decl.GetASTContext(),
                                                           namespace_decl.GetNamespaceDecl());
@@ -2012,8 +1999,7 @@
         
         fun_address = &fun->GetAddressRange().GetBaseAddress();
         
-        TypeList *type_list = fun_type->GetTypeList();
-        fun_ast_context = type_list->GetClangASTContext().getASTContext();
+        fun_ast_context = fun_type->GetClangASTContext().getASTContext();
         void *copied_type = GuardedCopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
         
         fun_decl = context.AddFunDecl(copied_type);