LLDB  mainline
ClangASTImporter.h
Go to the documentation of this file.
1 //===-- ClangASTImporter.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
11 
12 #include <map>
13 #include <memory>
14 #include <set>
15 #include <vector>
16 
17 #include "clang/AST/ASTImporter.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Basic/FileSystemOptions.h"
23 
24 #include "lldb/Host/FileSystem.h"
26 #include "lldb/lldb-types.h"
27 
29 
30 #include "llvm/ADT/DenseMap.h"
31 
32 namespace lldb_private {
33 
34 class ClangASTMetadata;
35 class TypeSystemClang;
36 
38 public:
39  struct LayoutInfo {
40  LayoutInfo() = default;
41  typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
43 
44  uint64_t bit_size = 0;
45  uint64_t alignment = 0;
46  llvm::DenseMap<const clang::FieldDecl *, uint64_t> field_offsets;
49  };
50 
52  : m_file_manager(clang::FileSystemOptions(),
53  FileSystem::Instance().GetVirtualFileSystem()) {}
54 
55  CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type);
56 
57  clang::Decl *CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
58 
59  CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type);
60 
61  clang::Decl *DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl);
62 
63  /// Sets the layout for the given RecordDecl. The layout will later be
64  /// used by Clang's during code generation. Not calling this function for
65  /// a RecordDecl will cause that Clang's codegen tries to layout the
66  /// record by itself.
67  ///
68  /// \param decl The RecordDecl to set the layout for.
69  /// \param layout The layout for the record.
70  void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout);
71 
72  bool LayoutRecordType(
73  const clang::RecordDecl *record_decl, uint64_t &bit_size,
74  uint64_t &alignment,
75  llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
76  llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
77  &base_offsets,
78  llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
79  &vbase_offsets);
80 
81  bool CanImport(const CompilerType &type);
82 
83  bool Import(const CompilerType &type);
84 
85  bool CompleteType(const CompilerType &compiler_type);
86 
87  bool CompleteTagDecl(clang::TagDecl *decl);
88 
89  bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin);
90 
91  bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl);
92 
93  bool CompleteAndFetchChildren(clang::QualType type);
94 
95  bool RequireCompleteType(clang::QualType type);
96 
97  void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl);
98 
99  ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);
100 
101  //
102  // Namespace maps
103  //
104 
105  typedef std::pair<lldb::ModuleSP, CompilerDeclContext> NamespaceMapItem;
106  typedef std::vector<NamespaceMapItem> NamespaceMap;
107  typedef std::shared_ptr<NamespaceMap> NamespaceMapSP;
108 
109  void RegisterNamespaceMap(const clang::NamespaceDecl *decl,
110  NamespaceMapSP &namespace_map);
111 
112  NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl);
113 
114  void BuildNamespaceMap(const clang::NamespaceDecl *decl);
115 
116  //
117  // Completers for maps
118  //
119 
120  class MapCompleter {
121  public:
122  virtual ~MapCompleter();
123 
124  virtual void CompleteNamespaceMap(NamespaceMapSP &namespace_map,
125  ConstString name,
126  NamespaceMapSP &parent_map) const = 0;
127  };
128 
129  void InstallMapCompleter(clang::ASTContext *dst_ctx,
130  MapCompleter &completer) {
131  ASTContextMetadataSP context_md;
132  ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
133 
134  if (context_md_iter == m_metadata_map.end()) {
135  context_md = ASTContextMetadataSP(new ASTContextMetadata(dst_ctx));
136  m_metadata_map[dst_ctx] = context_md;
137  } else {
138  context_md = context_md_iter->second;
139  }
140 
141  context_md->m_map_completer = &completer;
142  }
143 
144  void ForgetDestination(clang::ASTContext *dst_ctx);
145  void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx);
146 
147  struct DeclOrigin {
148  DeclOrigin() : ctx(nullptr), decl(nullptr) {}
149 
150  DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)
151  : ctx(_ctx), decl(_decl) {}
152 
153  DeclOrigin(const DeclOrigin &rhs) {
154  ctx = rhs.ctx;
155  decl = rhs.decl;
156  }
157 
158  void operator=(const DeclOrigin &rhs) {
159  ctx = rhs.ctx;
160  decl = rhs.decl;
161  }
162 
163  bool Valid() { return (ctx != nullptr || decl != nullptr); }
164 
165  clang::ASTContext *ctx;
166  clang::Decl *decl;
167  };
168 
169  typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap;
170 
171  /// Listener interface used by the ASTImporterDelegate to inform other code
172  /// about decls that have been imported the first time.
174  virtual ~NewDeclListener() = default;
175  /// A decl has been imported for the first time.
176  virtual void NewDeclImported(clang::Decl *from, clang::Decl *to) = 0;
177  };
178 
179  /// ASTImporter that intercepts and records the import process of the
180  /// underlying ASTImporter.
181  ///
182  /// This class updates the map from declarations to their original
183  /// declarations and can record declarations that have been imported in a
184  /// certain interval.
185  ///
186  /// When intercepting a declaration import, the ASTImporterDelegate uses the
187  /// CxxModuleHandler to replace any missing or malformed declarations with
188  /// their counterpart from a C++ module.
189  struct ASTImporterDelegate : public clang::ASTImporter {
190  ASTImporterDelegate(ClangASTImporter &master, clang::ASTContext *target_ctx,
191  clang::ASTContext *source_ctx)
192  : clang::ASTImporter(*target_ctx, master.m_file_manager, *source_ctx,
193  master.m_file_manager, true /*minimal*/),
194  m_master(master), m_source_ctx(source_ctx) {
195  setODRHandling(clang::ASTImporter::ODRHandlingType::Liberal);
196  }
197 
198  /// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate
199  /// and deattaches it at the end of the scope. Supports being used multiple
200  /// times on the same ASTImporterDelegate instance in nested scopes.
202  /// The handler we attach to the ASTImporterDelegate.
204  /// The ASTImporterDelegate we are supposed to attach the handler to.
206  /// True iff we attached the handler to the ASTImporterDelegate.
207  bool m_valid = false;
208 
209  public:
210  CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
211  : m_delegate(delegate) {
212  // If the delegate doesn't have a CxxModuleHandler yet, create one
213  // and attach it.
214  if (!delegate.m_std_handler) {
215  m_handler = CxxModuleHandler(delegate, dst_ctx);
216  m_valid = true;
217  delegate.m_std_handler = &m_handler;
218  }
219  }
221  if (m_valid) {
222  // Make sure no one messed with the handler we placed.
223  assert(m_delegate.m_std_handler == &m_handler);
224  m_delegate.m_std_handler = nullptr;
225  }
226  }
227  };
228 
229  void ImportDefinitionTo(clang::Decl *to, clang::Decl *from);
230 
231  void Imported(clang::Decl *from, clang::Decl *to) override;
232 
233  clang::Decl *GetOriginalDecl(clang::Decl *To) override;
234 
236  assert(m_new_decl_listener == nullptr && "Already attached a listener?");
237  m_new_decl_listener = listener;
238  }
239  void RemoveImportListener() { m_new_decl_listener = nullptr; }
240 
241  protected:
242  llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;
243 
244  private:
245  /// Decls we should ignore when mapping decls back to their original
246  /// ASTContext. Used by the CxxModuleHandler to mark declarations that
247  /// were created from the 'std' C++ module to prevent that the Importer
248  /// tries to sync them with the broken equivalent in the debug info AST.
249  llvm::SmallPtrSet<clang::Decl *, 16> m_decls_to_ignore;
251  clang::ASTContext *m_source_ctx;
252  CxxModuleHandler *m_std_handler = nullptr;
253  /// The currently attached listener.
254  NewDeclListener *m_new_decl_listener = nullptr;
255  };
256 
257  typedef std::shared_ptr<ASTImporterDelegate> ImporterDelegateSP;
258  typedef llvm::DenseMap<clang::ASTContext *, ImporterDelegateSP> DelegateMap;
259  typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP>
261 
263  ASTContextMetadata(clang::ASTContext *dst_ctx)
264  : m_dst_ctx(dst_ctx), m_delegates(), m_origins(), m_namespace_maps(),
265  m_map_completer(nullptr) {}
266 
267  clang::ASTContext *m_dst_ctx;
268  DelegateMap m_delegates;
269  OriginMap m_origins;
270 
273  };
274 
275  typedef std::shared_ptr<ASTContextMetadata> ASTContextMetadataSP;
276  typedef llvm::DenseMap<const clang::ASTContext *, ASTContextMetadataSP>
278 
280 
281  ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx) {
282  ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
283 
284  if (context_md_iter == m_metadata_map.end()) {
285  ASTContextMetadataSP context_md =
287  m_metadata_map[dst_ctx] = context_md;
288  return context_md;
289  }
290  return context_md_iter->second;
291  }
292 
293  ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx) {
294  ContextMetadataMap::iterator context_md_iter = m_metadata_map.find(dst_ctx);
295 
296  if (context_md_iter != m_metadata_map.end())
297  return context_md_iter->second;
298  return ASTContextMetadataSP();
299  }
300 
301  ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx,
302  clang::ASTContext *src_ctx) {
303  ASTContextMetadataSP context_md = GetContextMetadata(dst_ctx);
304 
305  DelegateMap &delegates = context_md->m_delegates;
306  DelegateMap::iterator delegate_iter = delegates.find(src_ctx);
307 
308  if (delegate_iter == delegates.end()) {
309  ImporterDelegateSP delegate =
310  ImporterDelegateSP(new ASTImporterDelegate(*this, dst_ctx, src_ctx));
311  delegates[src_ctx] = delegate;
312  return delegate;
313  }
314  return delegate_iter->second;
315  }
316 
317  DeclOrigin GetDeclOrigin(const clang::Decl *decl);
318 
319  clang::FileManager m_file_manager;
320  typedef llvm::DenseMap<const clang::RecordDecl *, LayoutInfo>
322 
324 };
325 
326 } // namespace lldb_private
327 
328 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTIMPORTER_H
bool CompleteType(const CompilerType &compiler_type)
NamespaceMapSP GetNamespaceMap(const clang::NamespaceDecl *decl)
Handles importing decls into an ASTContext with an attached C++ module.
bool CompleteTagDecl(clang::TagDecl *decl)
A class that represents a running process on the host machine.
bool CanImport(const CompilerType &type)
ASTContextMetadataSP MaybeGetContextMetadata(clang::ASTContext *dst_ctx)
std::pair< lldb::ModuleSP, CompilerDeclContext > NamespaceMapItem
std::vector< NamespaceMapItem > NamespaceMap
Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate and deattaches it at the end o...
A TypeSystem implementation based on Clang.
bool LayoutRecordType(const clang::RecordDecl *record_decl, uint64_t &bit_size, uint64_t &alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &field_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &vbase_offsets)
bool RequireCompleteType(clang::QualType type)
void BuildNamespaceMap(const clang::NamespaceDecl *decl)
llvm::SmallPtrSet< clang::Decl *, 16 > m_decls_to_ignore
Decls we should ignore when mapping decls back to their original ASTContext.
void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout)
Sets the layout for the given RecordDecl.
llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > OffsetMap
void SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl)
bool CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin)
llvm::DenseMap< const clang::RecordDecl *, LayoutInfo > RecordDeclToLayoutMap
void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx)
clang::Decl * DeportDecl(clang::ASTContext *dst_ctx, clang::Decl *decl)
llvm::DenseMap< clang::ASTContext *, ImporterDelegateSP > DelegateMap
DeclOrigin GetDeclOrigin(const clang::Decl *decl)
llvm::DenseMap< const clang::ASTContext *, ASTContextMetadataSP > ContextMetadataMap
void ForgetDestination(clang::ASTContext *dst_ctx)
void InstallMapCompleter(clang::ASTContext *dst_ctx, MapCompleter &completer)
ASTContextMetadataSP GetContextMetadata(clang::ASTContext *dst_ctx)
llvm::DenseMap< const clang::NamespaceDecl *, NamespaceMapSP > NamespaceMetaMap
ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx)
CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
std::shared_ptr< NamespaceMap > NamespaceMapSP
bool Import(const CompilerType &type)
ASTImporter that intercepts and records the import process of the underlying ASTImporter.
std::shared_ptr< ASTContextMetadata > ASTContextMetadataSP
void RegisterNamespaceMap(const clang::NamespaceDecl *decl, NamespaceMapSP &namespace_map)
CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type)
llvm::DenseMap< const clang::FieldDecl *, uint64_t > field_offsets
ASTImporterDelegate(ClangASTImporter &master, clang::ASTContext *target_ctx, clang::ASTContext *source_ctx)
RecordDeclToLayoutMap m_record_decl_to_layout_map
llvm::DenseMap< const clang::Decl *, DeclOrigin > OriginMap
bool CompleteAndFetchChildren(clang::QualType type)
A uniqued constant string class.
Definition: ConstString.h:40
bool CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *interface_decl)
clang::Decl * CopyDecl(clang::ASTContext *dst_ctx, clang::Decl *decl)
Represents a generic type in a programming language.
Definition: CompilerType.h:33
Listener interface used by the ASTImporterDelegate to inform other code about decls that have been im...
DeclOrigin(clang::ASTContext *_ctx, clang::Decl *_decl)
std::shared_ptr< ASTImporterDelegate > ImporterDelegateSP
CxxModuleHandler m_handler
The handler we attach to the ASTImporterDelegate.
ClangASTMetadata * GetDeclMetadata(const clang::Decl *decl)
ASTImporterDelegate & m_delegate
The ASTImporterDelegate we are supposed to attach the handler to.
CompilerType DeportType(TypeSystemClang &dst, const CompilerType &src_type)