blob: a1ff7d249e2bf1135611e8b6fe2e5f93bb4d4d66 [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)
45 return minion_sp->Import(decl);
Sean Callanancc427fa2011-07-30 02:42:06 +000046
Greg Claytondd0649b2011-07-06 18:55:08 +000047 return NULL;
Greg Claytona2721472011-06-25 00:44:06 +000048}
49
Sean Callanancc427fa2011-07-30 02:42:06 +000050void
51ClangASTImporter::CompleteTagDecl (clang::TagDecl *decl)
Greg Claytona2721472011-06-25 00:44:06 +000052{
Sean Callanancc427fa2011-07-30 02:42:06 +000053 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
54
55 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +000056 log->Printf(" [ClangASTImporter] Completing a TagDecl named %s", decl->getName().str().c_str());
Greg Claytona2721472011-06-25 00:44:06 +000057
Sean Callanancc427fa2011-07-30 02:42:06 +000058 DeclOrigin decl_origin = GetDeclOrigin(decl);
Greg Claytona2721472011-06-25 00:44:06 +000059
Sean Callanancc427fa2011-07-30 02:42:06 +000060 if (!decl_origin.Valid())
61 return;
Greg Claytona2721472011-06-25 00:44:06 +000062
Sean Callanancc427fa2011-07-30 02:42:06 +000063 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
64 return;
Greg Claytona2721472011-06-25 00:44:06 +000065
Sean Callanancc427fa2011-07-30 02:42:06 +000066 MinionSP minion_sp (GetMinion(decl_origin.ctx, false));
Greg Claytona2721472011-06-25 00:44:06 +000067
Greg Claytondd0649b2011-07-06 18:55:08 +000068 if (minion_sp)
Sean Callanancc427fa2011-07-30 02:42:06 +000069 minion_sp->ImportDefinition(decl_origin.decl);
Greg Claytona2721472011-06-25 00:44:06 +000070
Sean Callanancc427fa2011-07-30 02:42:06 +000071 return;
Greg Claytona2721472011-06-25 00:44:06 +000072}
Sean Callanancc427fa2011-07-30 02:42:06 +000073
74void
75ClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl)
76{
77 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
78
79 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +000080 log->Printf(" [ClangASTImporter] Completing an ObjCInterfaceDecl named %s", interface_decl->getName().str().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +000081
82 DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
83
84 if (!decl_origin.Valid())
85 return;
86
87 if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
88 return;
89
90 MinionSP minion_sp (GetMinion(decl_origin.ctx, false));
91
92 if (minion_sp)
93 minion_sp->ImportDefinition(decl_origin.decl);
94
95 return;
96}
97
Sean Callanan503aa522011-10-12 00:12:34 +000098void
99ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
100 NamespaceMapSP &namespace_map)
101{
102 m_namespace_maps[decl] = namespace_map;
103}
104
105ClangASTImporter::NamespaceMapSP
106ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl)
107{
108 NamespaceMetaMap::iterator iter = m_namespace_maps.find(decl);
109
110 if (iter != m_namespace_maps.end())
111 return iter->second;
112 else
113 return NamespaceMapSP();
114}
115
Sean Callananb2269162011-10-21 22:18:07 +0000116void
117ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl)
118{
119 const DeclContext *parent_context = decl->getDeclContext();
120 const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context);
121 NamespaceMapSP parent_map;
122
123 if (parent_namespace)
124 parent_map = GetNamespaceMap(parent_namespace);
125
126 NamespaceMapSP new_map;
127
128 new_map.reset(new NamespaceMap);
129
130 if (m_map_completer)
131 {
132 std::string namespace_string = decl->getDeclName().getAsString();
133
134 m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map);
135 }
136
137 RegisterNamespaceMap (decl, new_map);
138}
139
140ClangASTImporter::NamespaceMapCompleter::~NamespaceMapCompleter ()
141{
142 return;
143}
144
Sean Callanancc427fa2011-07-30 02:42:06 +0000145clang::Decl
146*ClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to)
147{
148 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
149
150 m_master.m_origins[to] = DeclOrigin (m_source_ctx, from);
151
152 if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from))
153 {
154 TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
155
156 to_tag_decl->setHasExternalLexicalStorage();
157
158 if (log)
Sean Callanan7dd98122011-10-14 20:34:21 +0000159 log->Printf(" [ClangASTImporter] Imported a TagDecl named %s%s%s",
Sean Callanancc427fa2011-07-30 02:42:06 +0000160 from_tag_decl->getName().str().c_str(),
161 (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
162 (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""));
163 }
164
Sean Callananb2269162011-10-21 22:18:07 +0000165 if (isa<NamespaceDecl>(from))
166 {
167 NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
168
169 m_master.BuildNamespaceMap(to_namespace_decl);
170
171 to_namespace_decl->setHasExternalVisibleStorage();
172 }
173
Sean Callanancc427fa2011-07-30 02:42:06 +0000174 if (isa<ObjCInterfaceDecl>(from))
175 {
176 ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to);
177
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000178 if (!to_interface_decl->isForwardDecl())
179 to_interface_decl->setExternallyCompleted();
Sean Callanancc427fa2011-07-30 02:42:06 +0000180 }
181
182 return clang::ASTImporter::Imported(from, to);
Greg Clayton3418c852011-08-10 02:10:13 +0000183}