1 //===-- ClangExternalASTSourceCallbacks.cpp -------------------------------===//
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 #include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
10 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
11 
12 #include "clang/AST/Decl.h"
13 
14 using namespace lldb_private;
15 
16 char ClangExternalASTSourceCallbacks::ID;
17 
18 void ClangExternalASTSourceCallbacks::CompleteType(clang::TagDecl *tag_decl) {
19   m_ast.CompleteTagDecl(tag_decl);
20 }
21 
22 void ClangExternalASTSourceCallbacks::CompleteType(
23     clang::ObjCInterfaceDecl *objc_decl) {
24   m_ast.CompleteObjCInterfaceDecl(objc_decl);
25 }
26 
27 bool ClangExternalASTSourceCallbacks::layoutRecordType(
28     const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
29     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &FieldOffsets,
30     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &BaseOffsets,
31     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
32         &VirtualBaseOffsets) {
33   return m_ast.LayoutRecordType(Record, Size, Alignment, FieldOffsets,
34                                 BaseOffsets, VirtualBaseOffsets);
35 }
36 
37 void ClangExternalASTSourceCallbacks::FindExternalLexicalDecls(
38     const clang::DeclContext *decl_ctx,
39     llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
40     llvm::SmallVectorImpl<clang::Decl *> &decls) {
41   if (decl_ctx) {
42     clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(
43         const_cast<clang::DeclContext *>(decl_ctx));
44     if (tag_decl)
45       CompleteType(tag_decl);
46   }
47 }
48 
49 OptionalClangModuleID ClangExternalASTSourceCallbacks::RegisterModule(clang::Module *module) {
50   m_modules.push_back(module);
51   unsigned id = m_modules.size();
52   m_ids.insert({module, id});
53   return OptionalClangModuleID(id);
54 }
55 
56 llvm::Optional<clang::ASTSourceDescriptor>
57 ClangExternalASTSourceCallbacks::getSourceDescriptor(unsigned id) {
58   if (clang::Module *module = getModule(id))
59     return {*module};
60   return {};
61 }
62 
63 clang::Module *ClangExternalASTSourceCallbacks::getModule(unsigned id) {
64   if (id && id <= m_modules.size())
65     return m_modules[id - 1];
66   return nullptr;
67 }
68 
69 OptionalClangModuleID ClangExternalASTSourceCallbacks::GetIDForModule(clang::Module *module) {
70   return OptionalClangModuleID(m_ids[module]);
71 }
72