Greg Clayton | 1e591ce | 2010-07-16 18:28:27 +0000 | [diff] [blame] | 1 | //===-- ClangASTSource.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 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Expression/ClangASTSource.h" |
| 14 | #include "lldb/Expression/ClangExpression.h" |
| 15 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | ClangASTSource::~ClangASTSource() {} |
| 21 | |
| 22 | void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) { |
| 23 | // Tell Sema to ask us when looking into the translation unit's decl. |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 24 | m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 25 | m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // These are only required for AST source that want to lazily load |
| 29 | // the declarations (or parts thereof) that they return. |
| 30 | Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; } |
| 31 | Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; } |
| 32 | |
| 33 | // These are also optional, although it might help with ObjC |
| 34 | // debugging if we have respectable signatures. But a more |
| 35 | // efficient interface (that didn't require scanning all files |
| 36 | // for method signatures!) might help. |
| 37 | Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); } |
| 38 | uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; } |
| 39 | |
| 40 | // The core lookup interface. |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 41 | DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName |
| 42 | ( |
| 43 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 44 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 45 | ) |
| 46 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 47 | switch (clang_decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | // Normal identifiers. |
| 49 | case DeclarationName::Identifier: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 50 | if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0) |
| 51 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 52 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | |
| 54 | // Operator names. Not important for now. |
| 55 | case DeclarationName::CXXOperatorName: |
| 56 | case DeclarationName::CXXLiteralOperatorName: |
| 57 | return DeclContext::lookup_result(); |
| 58 | |
| 59 | // Using directives found in this context. |
| 60 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
| 61 | case DeclarationName::CXXUsingDirective: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 62 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
| 64 | // These aren't looked up like this. |
| 65 | case DeclarationName::ObjCZeroArgSelector: |
| 66 | case DeclarationName::ObjCOneArgSelector: |
| 67 | case DeclarationName::ObjCMultiArgSelector: |
| 68 | return DeclContext::lookup_result(); |
| 69 | |
| 70 | // These aren't possible in the global context. |
| 71 | case DeclarationName::CXXConstructorName: |
| 72 | case DeclarationName::CXXDestructorName: |
| 73 | case DeclarationName::CXXConversionFunctionName: |
| 74 | return DeclContext::lookup_result(); |
| 75 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 76 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 77 | std::string decl_name (clang_decl_name.getAsString()); |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 78 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 79 | if (!m_decl_map.GetLookupsEnabled()) |
| 80 | { |
| 81 | // Wait until we see a '$' at the start of a name before we start doing |
| 82 | // any lookups so we can avoid lookup up all of the builtin types. |
| 83 | if (!decl_name.empty() && decl_name[0] == '$') |
| 84 | { |
| 85 | m_decl_map.SetLookupsEnabled (true); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 90 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 91 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 92 | |
| 93 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 95 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
| 96 | ConstString const_decl_name(decl_name.c_str()); |
| 97 | m_decl_map.GetDecls(name_search_context, const_decl_name); |
| 98 | return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 101 | void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC) |
| 102 | { |
| 103 | return; |
| 104 | } |
| 105 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | // This is used to support iterating through an entire lexical context, |
| 107 | // which isn't something the debugger should ever need to do. |
| 108 | bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) { |
| 109 | // true is for error, that's good enough for me |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | clang::ASTContext *NameSearchContext::GetASTContext() { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 114 | return &m_ast_source.m_ast_context; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 117 | clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 118 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 119 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 120 | clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context, |
| 121 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 123 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | QualType::getFromOpaquePtr(type), |
| 125 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 126 | SC_Static, |
| 127 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 128 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | |
| 130 | return Decl; |
| 131 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 132 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 133 | clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) { |
| 134 | clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context, |
| 135 | const_cast<DeclContext*>(m_decl_context), |
| 136 | SourceLocation(), |
| 137 | m_decl_name.getAsIdentifierInfo(), |
| 138 | QualType::getFromOpaquePtr(type), |
| 139 | NULL, |
| 140 | SC_Static, |
| 141 | SC_Static, |
| 142 | false, |
| 143 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 144 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 145 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 146 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 147 | // this, we raid the function's FunctionProtoType for types. |
| 148 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 149 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
| 150 | const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 151 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 152 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 153 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 154 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 155 | unsigned ArgIndex; |
| 156 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 157 | ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs]; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 158 | |
| 159 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 160 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 161 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 162 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 163 | param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context, |
| 164 | const_cast<DeclContext*>(m_decl_context), |
| 165 | SourceLocation(), |
| 166 | NULL, |
| 167 | arg_qual_type, |
| 168 | NULL, |
| 169 | SC_Static, |
| 170 | SC_Static, |
| 171 | NULL); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 174 | func_decl->setParams(param_var_decls, NumArgs); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 175 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 176 | delete [] param_var_decls; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 179 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 180 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 181 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 182 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 183 | |
| 184 | clang::NamedDecl *NameSearchContext::AddGenericFunDecl() |
| 185 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 186 | QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result |
| 187 | NULL, // argument types |
| 188 | 0, // number of arguments |
| 189 | true, // variadic? |
| 190 | 0, // type qualifiers |
| 191 | false, // has exception specification? |
| 192 | false, // has any exception specification? |
| 193 | 0, // number of exceptions |
| 194 | NULL, // exceptions |
| 195 | FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention |
| 196 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 197 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 198 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 199 | |
| 200 | clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type) |
| 201 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 202 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 203 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 204 | if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type)) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 205 | { |
| 206 | TagDecl *tag_decl = tag_type->getDecl(); |
| 207 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame^] | 208 | m_decls.push_back(tag_decl); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 209 | |
| 210 | return tag_decl; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | return NULL; |
| 215 | } |
| 216 | } |