blob: ef0dcfe4c1d44f8b87ada6b3e625eab42daffc16 [file] [log] [blame]
Greg Claytona2721472011-06-25 00:44:06 +00001//===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/Decl.h"
Sean Callanancc427fa2011-07-30 02:42:06 +000011#include "clang/AST/DeclObjC.h"
12#include "lldb/Core/Log.h"
Greg Clayton7c6d7b82011-10-21 23:04:20 +000013#include "lldb/Core/Module.h"
Greg Claytona2721472011-06-25 00:44:06 +000014#include "lldb/Symbol/ClangASTContext.h"
15#include "lldb/Symbol/ClangASTImporter.h"
Sean Callananb2269162011-10-21 22:18:07 +000016#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Claytona2721472011-06-25 00:44:06 +000017
18using namespace lldb_private;
19using namespace clang;
20
21clang::QualType
22ClangASTImporter::CopyType (clang::ASTContext *src_ast,
23 clang::QualType type)
24{
Greg Claytondd0649b2011-07-06 18:55:08 +000025 MinionSP minion_sp (GetMinion(src_ast, false));
Sean Callanancc427fa2011-07-30 02:42:06 +000026
Greg Claytondd0649b2011-07-06 18:55:08 +000027 if (minion_sp)
28 return minion_sp->Import(type);
Sean Callanancc427fa2011-07-30 02:42:06 +000029
Greg Claytondd0649b2011-07-06 18:55:08 +000030 return QualType();
Greg Claytona2721472011-06-25 00:44:06 +000031}
32
33clang::Decl *
34ClangASTImporter::CopyDecl (clang::ASTContext *src_ast,
35 clang::Decl *decl)
36{
Greg Claytondd0649b2011-07-06 18:55:08 +000037 MinionSP minion_sp;
Greg Claytona2721472011-06-25 00:44:06 +000038
39 if (isa<clang::NamespaceDecl>(decl))
Greg Claytondd0649b2011-07-06 18:55:08 +000040 minion_sp = GetMinion(src_ast, true);
Greg Claytona2721472011-06-25 00:44:06 +000041 else
Greg Claytondd0649b2011-07-06 18:55:08 +000042 minion_sp = GetMinion(src_ast, false);
Greg Claytona2721472011-06-25 00:44:06 +000043
Greg Claytondd0649b2011-07-06 18:55:08 +000044 if (minion_sp)
Sean Callananbfb237bc2011-11-04 22:46:46 +000045 {
46 clang::Decl *result = minion_sp->Import(decl);
47
48 if (!result)
49 {
50 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
51
Sean Callanand5145b32011-11-05 00:08:12 +000052 if (log)
53 {
54 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
55 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s'", decl->getDeclKindName(), named_decl->getNameAsString().c_str());
56 else
57 log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s", decl->getDeclKindName());
58 }
Sean Callananbfb237bc2011-11-04 22:46:46 +000059 }
60
61 return result;
62 }
Sean Callanancc427fa2011-07-30 02:42:06 +000063
Greg Claytondd0649b2011-07-06 18:55:08 +000064 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000065}
66
Sean Callanancc427fa2011-07-30 02:42:06 +000067void
68ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Greg Claytona2721472011-06-25 00:44:06 +000069{
Sean Callanancc427fa2011-07-30 02:42:06 +000070 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Greg Claytona2721472011-06-25 00:44:06 +000071
Sean Callanancc427fa2011-07-30 02:42:06 +000072 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +000073
Sean Callanancc427fa2011-07-30 02:42:06 +000074 if (!decl_origin.Valid())
75 return;
Greg Claytona2721472011-06-25 00:44:06 +000076
Sean Callanancc427fa2011-07-30 02:42:06 +000077 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
78 return;
Greg Claytona2721472011-06-25 00:44:06 +000079
Sean Callanancc427fa2011-07-30 02:42:06 +000080 MinionSP minion_sp (GetMinion(decl_origin.ctx, false));
Greg Claytona2721472011-06-25 00:44:06 +000081
Greg Claytondd0649b2011-07-06 18:55:08 +000082 if (minion_sp)
Sean Callanancc427fa2011-07-30 02:42:06 +000083 minion_sp->ImportDefinition(decl_origin.decl);
Greg Claytona2721472011-06-25 00:44:06 +000084
Sean Callanancc427fa2011-07-30 02:42:06 +000085 return;
Greg Claytona2721472011-06-25 00:44:06 +000086}
Sean Callanancc427fa2011-07-30 02:42:06 +000087
88void
89ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
90{
91 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
92
Sean Callanancc427fa2011-07-30 02:42:06 +000093 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
94
95 if (!decl_origin.Valid())
96 return;
97
98 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
99 return;
100
101 MinionSP minion_sp (GetMinion(decl_origin.ctx, false));
102
103 if (minion_sp)
104 minion_sp->ImportDefinition(decl_origin.decl);
105
106 return;
107}
108
Sean Callanan503aa522011-10-12 00:12:34 +0000109void
110ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
111 NamespaceMapSP &namespace_map)
112{
113 m_namespace_maps[decl] = namespace_map;
114}
115
116ClangASTImporter::NamespaceMapSP
117ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
118{
119 NamespaceMetaMap::iterator iter = m_namespace_maps.find(decl);
120
121 if (iter != m_namespace_maps.end())
122 return iter->second;
123 else
124 return NamespaceMapSP();
125}
126
Sean Callananb2269162011-10-21 22:18:07 +0000127void
128ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
129{
130 const DeclContext *parent_context = decl->getDeclContext();
131 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
132 NamespaceMapSP parent_map;
133
134 if (parent_namespace)
135 parent_map = GetNamespaceMap(parent_namespace);
136
137 NamespaceMapSP new_map;
138
139 new_map.reset(new NamespaceMap);
140
141 if (m_map_completer)
142 {
143 std::string namespace_string = decl->getDeclName().getAsString();
144
145 m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
146 }
147
148 RegisterNamespaceMap (decl, new_map);
149}
150
151ClangASTImporter::NamespaceMapCompleter::~NamespaceMapCompleter ()
152{
153 return;
154}
155
Sean Callanancc427fa2011-07-30 02:42:06 +0000156clang::Decl
157*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
158{
159 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
160
161 m_master.m_origins[to] = DeclOrigin (m_source_ctx, from);
162
163 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
164 {
165 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
166
167 to_tag_decl->setHasExternalLexicalStorage();
Sean Callanan0730e9c2011-11-09 19:33:21 +0000168
Sean Callanancc427fa2011-07-30 02:42:06 +0000169 if (log)
Sean Callanan0730e9c2011-11-09 19:33:21 +0000170 log->Printf(" [ClangASTImporter] Imported %p, a %s named %s%s%s [%s->%s]",
171 to,
172 ((clang::Decl*)from_tag_decl)->getDeclKindName(),
Sean Callanancc427fa2011-07-30 02:42:06 +0000173 from_tag_decl->getName().str().c_str(),
174 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
Sean Callanan0730e9c2011-11-09 19:33:21 +0000175 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
176 (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
177 (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
Sean Callanancc427fa2011-07-30 02:42:06 +0000178 }
179
Sean Callananb2269162011-10-21 22:18:07 +0000180 if (isa<NamespaceDecl>(from))
181 {
182 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
183
184 m_master.BuildNamespaceMap(to_namespace_decl);
185
186 to_namespace_decl->setHasExternalVisibleStorage();
187 }
188
Sean Callanancc427fa2011-07-30 02:42:06 +0000189 if (isa<ObjCInterfaceDecl>(from))
190 {
191 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
192
Sean Callanan0730e9c2011-11-09 19:33:21 +0000193 to_interface_decl->setHasExternalVisibleStorage();
194
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000195 if (!to_interface_decl->isForwardDecl())
196 to_interface_decl->setExternallyCompleted();
Sean Callanancc427fa2011-07-30 02:42:06 +0000197 }
198
199 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000200}