2011-06-25 08:44:06 +08:00
|
|
|
//===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Decl.h"
|
2011-07-30 10:42:06 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "lldb/Core/Log.h"
|
2011-10-22 07:04:20 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
2011-06-25 08:44:06 +08:00
|
|
|
#include "lldb/Symbol/ClangASTContext.h"
|
|
|
|
#include "lldb/Symbol/ClangASTImporter.h"
|
2011-10-22 06:18:07 +08:00
|
|
|
#include "lldb/Symbol/ClangNamespaceDecl.h"
|
2011-06-25 08:44:06 +08:00
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
clang::QualType
|
2011-11-17 02:20:47 +08:00
|
|
|
ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
|
|
|
|
clang::ASTContext *src_ast,
|
2011-06-25 08:44:06 +08:00
|
|
|
clang::QualType type)
|
|
|
|
{
|
2011-11-17 02:20:47 +08:00
|
|
|
MinionSP minion_sp (GetMinion(dst_ast, src_ast));
|
2011-07-30 10:42:06 +08:00
|
|
|
|
2011-07-07 02:55:08 +08:00
|
|
|
if (minion_sp)
|
|
|
|
return minion_sp->Import(type);
|
2011-07-30 10:42:06 +08:00
|
|
|
|
2011-07-07 02:55:08 +08:00
|
|
|
return QualType();
|
2011-06-25 08:44:06 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 03:07:39 +08:00
|
|
|
lldb::clang_type_t
|
|
|
|
ClangASTImporter::CopyType (clang::ASTContext *dst_ast,
|
|
|
|
clang::ASTContext *src_ast,
|
|
|
|
lldb::clang_type_t type)
|
|
|
|
{
|
|
|
|
return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr();
|
|
|
|
}
|
|
|
|
|
2011-06-25 08:44:06 +08:00
|
|
|
clang::Decl *
|
2011-11-17 02:20:47 +08:00
|
|
|
ClangASTImporter::CopyDecl (clang::ASTContext *dst_ast,
|
|
|
|
clang::ASTContext *src_ast,
|
2011-06-25 08:44:06 +08:00
|
|
|
clang::Decl *decl)
|
|
|
|
{
|
2011-07-07 02:55:08 +08:00
|
|
|
MinionSP minion_sp;
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-11-17 02:20:47 +08:00
|
|
|
minion_sp = GetMinion(dst_ast, src_ast);
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-07-07 02:55:08 +08:00
|
|
|
if (minion_sp)
|
2011-11-05 06:46:46 +08:00
|
|
|
{
|
|
|
|
clang::Decl *result = minion_sp->Import(decl);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
|
|
|
|
2011-11-05 08:08:12 +08:00
|
|
|
if (log)
|
|
|
|
{
|
|
|
|
if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
|
|
|
|
log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s'", decl->getDeclKindName(), named_decl->getNameAsString().c_str());
|
|
|
|
else
|
|
|
|
log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s", decl->getDeclKindName());
|
|
|
|
}
|
2011-11-05 06:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2011-07-30 10:42:06 +08:00
|
|
|
|
2011-07-07 02:55:08 +08:00
|
|
|
return NULL;
|
2011-06-25 08:44:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
void
|
|
|
|
ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
|
|
|
|
{
|
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
|
|
|
|
|
|
|
DeclOrigin decl_origin = GetDeclOrigin(decl);
|
|
|
|
|
|
|
|
if (!decl_origin.Valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
|
|
|
|
return;
|
|
|
|
|
2011-11-17 02:20:47 +08:00
|
|
|
MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx));
|
2011-07-30 10:42:06 +08:00
|
|
|
|
|
|
|
if (minion_sp)
|
|
|
|
minion_sp->ImportDefinition(decl_origin.decl);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
|
2011-06-25 08:44:06 +08:00
|
|
|
{
|
2011-07-30 10:42:06 +08:00
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
|
|
|
|
|
|
|
DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
if (!decl_origin.Valid())
|
|
|
|
return;
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
|
|
|
|
return;
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-11-17 02:20:47 +08:00
|
|
|
MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-07-07 02:55:08 +08:00
|
|
|
if (minion_sp)
|
2011-07-30 10:42:06 +08:00
|
|
|
minion_sp->ImportDefinition(decl_origin.decl);
|
2011-06-25 08:44:06 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
return;
|
2011-06-25 08:44:06 +08:00
|
|
|
}
|
2011-07-30 10:42:06 +08:00
|
|
|
|
2011-11-17 05:40:57 +08:00
|
|
|
ClangASTImporter::DeclOrigin
|
|
|
|
ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
|
|
|
|
{
|
|
|
|
ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
|
|
|
|
|
|
|
|
OriginMap &origins = context_md->m_origins;
|
|
|
|
|
|
|
|
OriginMap::iterator iter = origins.find(decl);
|
|
|
|
|
|
|
|
if (iter != origins.end())
|
|
|
|
return iter->second;
|
|
|
|
else
|
|
|
|
return DeclOrigin();
|
|
|
|
}
|
|
|
|
|
2011-10-12 08:12:34 +08:00
|
|
|
void
|
|
|
|
ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
|
|
|
|
NamespaceMapSP &namespace_map)
|
|
|
|
{
|
2011-11-17 05:40:57 +08:00
|
|
|
ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
|
|
|
|
|
|
|
|
context_md->m_namespace_maps[decl] = namespace_map;
|
2011-10-12 08:12:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ClangASTImporter::NamespaceMapSP
|
|
|
|
ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
|
|
|
|
{
|
2011-11-17 05:40:57 +08:00
|
|
|
ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
|
|
|
|
|
|
|
|
NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
|
2011-10-12 08:12:34 +08:00
|
|
|
|
2011-11-17 05:40:57 +08:00
|
|
|
NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
|
|
|
|
|
|
|
|
if (iter != namespace_maps.end())
|
2011-10-12 08:12:34 +08:00
|
|
|
return iter->second;
|
|
|
|
else
|
|
|
|
return NamespaceMapSP();
|
|
|
|
}
|
|
|
|
|
2011-10-22 06:18:07 +08:00
|
|
|
void
|
|
|
|
ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
|
|
|
|
{
|
2011-11-17 05:40:57 +08:00
|
|
|
ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
|
|
|
|
|
2011-10-22 06:18:07 +08:00
|
|
|
const DeclContext *parent_context = decl->getDeclContext();
|
|
|
|
const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
|
|
|
|
NamespaceMapSP parent_map;
|
|
|
|
|
|
|
|
if (parent_namespace)
|
|
|
|
parent_map = GetNamespaceMap(parent_namespace);
|
|
|
|
|
|
|
|
NamespaceMapSP new_map;
|
|
|
|
|
|
|
|
new_map.reset(new NamespaceMap);
|
|
|
|
|
2011-11-17 05:40:57 +08:00
|
|
|
if (context_md->m_map_completer)
|
2011-10-22 06:18:07 +08:00
|
|
|
{
|
|
|
|
std::string namespace_string = decl->getDeclName().getAsString();
|
|
|
|
|
2011-11-17 05:40:57 +08:00
|
|
|
context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
|
2011-10-22 06:18:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RegisterNamespaceMap (decl, new_map);
|
|
|
|
}
|
|
|
|
|
2011-11-17 02:20:47 +08:00
|
|
|
void
|
2011-11-29 08:42:02 +08:00
|
|
|
ClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast)
|
2011-11-17 02:20:47 +08:00
|
|
|
{
|
2011-11-17 05:40:57 +08:00
|
|
|
m_metadata_map.erase(dst_ast);
|
2011-11-17 02:20:47 +08:00
|
|
|
}
|
|
|
|
|
2011-11-29 08:42:02 +08:00
|
|
|
void
|
|
|
|
ClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast)
|
|
|
|
{
|
|
|
|
ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast);
|
|
|
|
|
|
|
|
if (!md)
|
|
|
|
return;
|
|
|
|
|
|
|
|
md->m_minions.erase(src_ast);
|
|
|
|
|
|
|
|
for (OriginMap::iterator iter = md->m_origins.begin();
|
|
|
|
iter != md->m_origins.end();
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (iter->second.ctx == src_ast)
|
|
|
|
md->m_origins.erase(iter++);
|
|
|
|
else
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 06:18:07 +08:00
|
|
|
ClangASTImporter::NamespaceMapCompleter::~NamespaceMapCompleter ()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
clang::Decl
|
|
|
|
*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
|
|
|
|
{
|
|
|
|
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
|
2011-11-18 11:28:09 +08:00
|
|
|
|
|
|
|
if (log)
|
|
|
|
{
|
|
|
|
if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
|
|
|
|
{
|
|
|
|
log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
|
|
|
|
from->getDeclKindName(),
|
|
|
|
to,
|
|
|
|
from_named_decl->getName().str().c_str(),
|
|
|
|
from);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
|
|
|
|
from->getDeclKindName(),
|
|
|
|
to,
|
|
|
|
from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-17 06:23:28 +08:00
|
|
|
ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext());
|
|
|
|
ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx);
|
2011-11-17 05:40:57 +08:00
|
|
|
|
2011-11-17 06:23:28 +08:00
|
|
|
if (from_context_md)
|
|
|
|
{
|
|
|
|
OriginMap &origins = from_context_md->m_origins;
|
|
|
|
|
|
|
|
OriginMap::iterator origin_iter = origins.find(from);
|
|
|
|
|
|
|
|
if (origin_iter != origins.end())
|
2011-11-18 11:28:09 +08:00
|
|
|
{
|
2011-11-17 06:23:28 +08:00
|
|
|
to_context_md->m_origins[to] = origin_iter->second;
|
2011-11-18 11:28:09 +08:00
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p",
|
|
|
|
origin_iter->second.decl,
|
|
|
|
origin_iter->second.ctx,
|
|
|
|
&from->getASTContext(),
|
|
|
|
&to->getASTContext());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (log)
|
|
|
|
log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p",
|
|
|
|
&from->getASTContext());
|
|
|
|
}
|
2011-11-17 06:23:28 +08:00
|
|
|
|
|
|
|
if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to))
|
|
|
|
{
|
|
|
|
clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from);
|
|
|
|
|
|
|
|
NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
|
|
|
|
|
|
|
|
NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace);
|
|
|
|
|
|
|
|
if (namespace_map_iter != namespace_maps.end())
|
|
|
|
to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from);
|
2011-11-18 11:28:09 +08:00
|
|
|
|
|
|
|
if (log)
|
|
|
|
log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
|
|
|
|
from,
|
|
|
|
m_source_ctx,
|
|
|
|
&to->getASTContext());
|
2011-11-17 06:23:28 +08:00
|
|
|
}
|
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
|
|
|
|
{
|
|
|
|
TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
|
|
|
|
|
|
|
|
to_tag_decl->setHasExternalLexicalStorage();
|
2011-11-10 03:33:21 +08:00
|
|
|
|
2011-07-30 10:42:06 +08:00
|
|
|
if (log)
|
2011-11-18 11:28:09 +08:00
|
|
|
log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
|
2011-07-30 10:42:06 +08:00
|
|
|
(to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
|
2011-11-10 03:33:21 +08:00
|
|
|
(to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
|
|
|
|
(from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
|
|
|
|
(to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
|
2011-11-18 11:28:09 +08:00
|
|
|
|
|
|
|
to_tag_decl = NULL;
|
2011-07-30 10:42:06 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 06:18:07 +08:00
|
|
|
if (isa<NamespaceDecl>(from))
|
|
|
|
{
|
|
|
|
NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
|
|
|
|
|
|
|
|
m_master.BuildNamespaceMap(to_namespace_decl);
|
|
|
|
|
|
|
|
to_namespace_decl->setHasExternalVisibleStorage();
|
|
|
|
}
|
|
|
|
|
2011-11-18 11:28:09 +08:00
|
|
|
if (isa<ObjCInterfaceDecl>(from))
|
2011-07-30 10:42:06 +08:00
|
|
|
{
|
|
|
|
ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
|
|
|
|
|
2011-11-15 10:11:17 +08:00
|
|
|
to_interface_decl->setHasExternalLexicalStorage();
|
2011-11-10 03:33:21 +08:00
|
|
|
to_interface_decl->setHasExternalVisibleStorage();
|
|
|
|
|
2011-11-15 10:11:17 +08:00
|
|
|
if (to_interface_decl->isForwardDecl())
|
|
|
|
to_interface_decl->completedForwardDecl();
|
|
|
|
|
|
|
|
to_interface_decl->setExternallyCompleted();
|
|
|
|
|
|
|
|
if (log)
|
2011-11-18 11:28:09 +08:00
|
|
|
log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s",
|
2011-11-15 10:11:17 +08:00
|
|
|
(to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
|
|
|
|
(to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
|
|
|
|
(to_interface_decl->isForwardDecl() ? " Forward" : ""));
|
2011-07-30 10:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang::ASTImporter::Imported(from, to);
|
2011-08-10 10:10:13 +08:00
|
|
|
}
|