ClangASTImporter.h revision 2bc9eb3ba78efc64a273729b480bafc3bbaa433a
1//===-- ClangASTImporter.h --------------------------------------*- 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#ifndef liblldb_ClangASTImporter_h_ 11#define liblldb_ClangASTImporter_h_ 12 13#include <map> 14 15#include "lldb/lldb-types.h" 16 17#include "clang/AST/ASTImporter.h" 18#include "clang/Basic/FileManager.h" 19#include "clang/Basic/FileSystemOptions.h" 20 21namespace lldb_private { 22 23class ClangASTImporter 24{ 25public: 26 ClangASTImporter (clang::ASTContext *target_ctx) : 27 m_file_manager(clang::FileSystemOptions()), 28 m_target_ctx(target_ctx) 29 { 30 } 31 32 clang::ASTContext * 33 TargetASTContext () 34 { 35 return m_target_ctx; 36 } 37 38 clang::QualType 39 CopyType (clang::ASTContext *src_ctx, 40 clang::QualType type); 41 42 clang::Decl * 43 CopyDecl (clang::ASTContext *src_ctx, 44 clang::Decl *decl); 45 46 const clang::DeclContext * 47 CompleteDeclContext (const clang::DeclContext *decl_context); 48 49 bool 50 ResolveDeclOrigin (const clang::Decl *decl, clang::Decl **original_decl, clang::ASTContext **original_ctx) 51 { 52 DeclOrigin origin = GetDeclOrigin(decl); 53 54 if (original_decl) 55 *original_decl = origin.decl; 56 57 if (original_ctx) 58 *original_ctx = origin.ctx; 59 60 return origin.Valid(); 61 } 62 63private: 64 65 struct DeclOrigin 66 { 67 DeclOrigin () : 68 ctx(NULL), 69 decl(NULL) 70 { 71 } 72 73 DeclOrigin (clang::ASTContext *_ctx, 74 clang::Decl *_decl) : 75 ctx(_ctx), 76 decl(_decl) 77 { 78 } 79 80 DeclOrigin (const DeclOrigin &rhs) 81 { 82 ctx = rhs.ctx; 83 decl = rhs.decl; 84 } 85 86 bool 87 Valid () 88 { 89 return (ctx != NULL || decl != NULL); 90 } 91 92 clang::ASTContext *ctx; 93 clang::Decl *decl; 94 }; 95 96 typedef std::map<const clang::Decl *, DeclOrigin> OriginMap; 97 98 class Minion : public clang::ASTImporter 99 { 100 public: 101 Minion (ClangASTImporter &master, 102 clang::ASTContext *source_ctx, 103 bool minimal) : 104 clang::ASTImporter(*master.m_target_ctx, 105 master.m_file_manager, 106 *source_ctx, 107 master.m_file_manager, 108 minimal), 109 m_master(master), 110 m_source_ctx(source_ctx) 111 { 112 } 113 114 clang::Decl *Imported (clang::Decl *from, clang::Decl *to) 115 { 116 m_master.m_origins[to] = DeclOrigin (m_source_ctx, from); 117 118 return clang::ASTImporter::Imported(from, to); 119 } 120 121 ClangASTImporter &m_master; 122 clang::ASTContext *m_source_ctx; 123 }; 124 125 typedef lldb::SharedPtr<Minion>::Type MinionSP; 126 typedef std::map<clang::ASTContext *, MinionSP> MinionMap; 127 128 MinionSP 129 GetMinion (clang::ASTContext *source_ctx, bool minimal) 130 { 131 MinionMap *minions; 132 133 if (minimal) 134 minions = &m_minimal_minions; 135 else 136 minions = &m_minions; 137 138 if (minions->find(source_ctx) == minions->end()) 139 (*minions)[source_ctx] = MinionSP(new Minion(*this, source_ctx, minimal)); 140 141 return (*minions)[source_ctx]; 142 } 143 144 DeclOrigin 145 GetDeclOrigin (const clang::Decl *decl) 146 { 147 OriginMap::iterator iter = m_origins.find(decl); 148 149 if (iter != m_origins.end()) 150 return iter->second; 151 else 152 return DeclOrigin(); 153 } 154 155 clang::FileManager m_file_manager; 156 clang::ASTContext *m_target_ctx; 157 MinionMap m_minions; 158 MinionMap m_minimal_minions; 159 OriginMap m_origins; 160}; 161 162} 163 164#endif 165