2010-07-17 02:28:27 +08:00
|
|
|
//===-- ClangASTSource.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-10-15 11:36:13 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Expression/ClangASTSource.h"
|
|
|
|
#include "lldb/Expression/ClangExpression.h"
|
|
|
|
#include "lldb/Expression/ClangExpressionDeclMap.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
ClangASTSource::~ClangASTSource() {}
|
|
|
|
|
|
|
|
void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
|
|
|
|
// Tell Sema to ask us when looking into the translation unit's decl.
|
2010-10-16 06:48:33 +08:00
|
|
|
m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
|
|
|
|
m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// These are only required for AST source that want to lazily load
|
|
|
|
// the declarations (or parts thereof) that they return.
|
|
|
|
Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; }
|
|
|
|
Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; }
|
|
|
|
|
|
|
|
// These are also optional, although it might help with ObjC
|
|
|
|
// debugging if we have respectable signatures. But a more
|
|
|
|
// efficient interface (that didn't require scanning all files
|
|
|
|
// for method signatures!) might help.
|
|
|
|
Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); }
|
|
|
|
uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; }
|
2010-11-18 10:56:27 +08:00
|
|
|
CXXBaseSpecifier *ClangASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { return NULL; }
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// The core lookup interface.
|
2010-10-15 11:36:13 +08:00
|
|
|
DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName
|
|
|
|
(
|
|
|
|
const DeclContext *decl_ctx,
|
2010-10-16 06:48:33 +08:00
|
|
|
DeclarationName clang_decl_name
|
2010-10-15 11:36:13 +08:00
|
|
|
)
|
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
switch (clang_decl_name.getNameKind()) {
|
2010-06-09 00:52:24 +08:00
|
|
|
// Normal identifiers.
|
|
|
|
case DeclarationName::Identifier:
|
2010-10-16 06:48:33 +08:00
|
|
|
if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
|
|
|
|
return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
|
|
|
|
break;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// Operator names. Not important for now.
|
|
|
|
case DeclarationName::CXXOperatorName:
|
|
|
|
case DeclarationName::CXXLiteralOperatorName:
|
|
|
|
return DeclContext::lookup_result();
|
|
|
|
|
|
|
|
// Using directives found in this context.
|
|
|
|
// Tell Sema we didn't find any or we'll end up getting asked a *lot*.
|
|
|
|
case DeclarationName::CXXUsingDirective:
|
2010-10-16 06:48:33 +08:00
|
|
|
return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
// These aren't looked up like this.
|
|
|
|
case DeclarationName::ObjCZeroArgSelector:
|
|
|
|
case DeclarationName::ObjCOneArgSelector:
|
|
|
|
case DeclarationName::ObjCMultiArgSelector:
|
|
|
|
return DeclContext::lookup_result();
|
|
|
|
|
|
|
|
// These aren't possible in the global context.
|
|
|
|
case DeclarationName::CXXConstructorName:
|
|
|
|
case DeclarationName::CXXDestructorName:
|
|
|
|
case DeclarationName::CXXConversionFunctionName:
|
|
|
|
return DeclContext::lookup_result();
|
|
|
|
}
|
2010-10-15 11:36:13 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
std::string decl_name (clang_decl_name.getAsString());
|
2010-10-15 11:36:13 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
if (!m_decl_map.GetLookupsEnabled())
|
|
|
|
{
|
|
|
|
// Wait until we see a '$' at the start of a name before we start doing
|
|
|
|
// any lookups so we can avoid lookup up all of the builtin types.
|
|
|
|
if (!decl_name.empty() && decl_name[0] == '$')
|
|
|
|
{
|
2010-12-03 09:38:59 +08:00
|
|
|
m_decl_map.SetLookupsEnabled ();
|
2010-10-16 06:48:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
|
|
|
|
}
|
2010-10-15 11:36:13 +08:00
|
|
|
}
|
2010-10-16 06:48:33 +08:00
|
|
|
|
2010-11-13 12:18:24 +08:00
|
|
|
ConstString const_decl_name(decl_name.c_str());
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-11-13 12:18:24 +08:00
|
|
|
const char *uniqued_const_decl_name = const_decl_name.GetCString();
|
|
|
|
if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
|
|
|
|
{
|
|
|
|
// We are currently looking up this name...
|
|
|
|
return DeclContext::lookup_result();
|
|
|
|
}
|
|
|
|
m_active_lookups.insert(uniqued_const_decl_name);
|
2010-11-15 09:34:18 +08:00
|
|
|
// static uint32_t g_depth = 0;
|
|
|
|
// ++g_depth;
|
|
|
|
// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
|
2010-11-13 12:18:24 +08:00
|
|
|
llvm::SmallVector<NamedDecl*, 4> name_decls;
|
2010-10-16 06:48:33 +08:00
|
|
|
NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
|
|
|
|
m_decl_map.GetDecls(name_search_context, const_decl_name);
|
2010-11-13 12:18:24 +08:00
|
|
|
DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
|
2010-11-15 09:34:18 +08:00
|
|
|
// --g_depth;
|
2010-11-13 12:18:24 +08:00
|
|
|
m_active_lookups.erase (uniqued_const_decl_name);
|
|
|
|
return result;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2010-09-23 11:01:22 +08:00
|
|
|
void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
// This is used to support iterating through an entire lexical context,
|
|
|
|
// which isn't something the debugger should ever need to do.
|
2010-10-30 02:38:40 +08:00
|
|
|
bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC,
|
|
|
|
bool (*isKindWeWant)(Decl::Kind),
|
|
|
|
llvm::SmallVectorImpl<Decl*> &Decls) {
|
2010-06-09 00:52:24 +08:00
|
|
|
// true is for error, that's good enough for me
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
clang::ASTContext *NameSearchContext::GetASTContext() {
|
2010-10-16 06:48:33 +08:00
|
|
|
return &m_ast_source.m_ast_context;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
Removed the hacky "#define this ___clang_this" handler
for C++ classes. Replaced it with a less hacky approach:
- If an expression is defined in the context of a
method of class A, then that expression is wrapped as
___clang_class::___clang_expr(void*) { ... }
instead of ___clang_expr(void*) { ... }.
- ___clang_class is resolved as the type of the target
of the "this" pointer in the method the expression
is defined in.
- When reporting the type of ___clang_class, a method
with the signature ___clang_expr(void*) is added to
that class, so that Clang doesn't complain about a
method being defined without a corresponding
declaration.
- Whenever the expression gets called, "this" gets
looked up, type-checked, and then passed in as the
first argument.
This required the following changes:
- The ABIs were changed to support passing of the "this"
pointer as part of trivial calls.
- ThreadPlanCallFunction and ClangFunction were changed
to support passing of an optional "this" pointer.
- ClangUserExpression was extended to perform the
wrapping described above.
- ClangASTSource was changed to revert the changes
required by the hack.
- ClangExpressionParser, IRForTarget, and
ClangExpressionDeclMap were changed to handle
different manglings of ___clang_expr flexibly. This
meant no longer searching for a function called
___clang_expr, but rather looking for a function whose
name *contains* ___clang_expr.
- ClangExpressionParser and ClangExpressionDeclMap now
remember whether "this" is required, and know how to
look it up as necessary.
A few inheritance bugs remain, and I'm trying to resolve
these. But it is now possible to use "this" as well as
refer implicitly to member variables, when in the proper
context.
llvm-svn: 114384
2010-09-21 08:44:12 +08:00
|
|
|
clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
|
2010-10-16 06:48:33 +08:00
|
|
|
IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
|
2010-11-30 08:27:43 +08:00
|
|
|
|
|
|
|
assert (type && "Type for variable must be non-NULL!");
|
2010-09-15 05:59:34 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
|
|
|
|
const_cast<DeclContext*>(m_decl_context),
|
2010-06-09 00:52:24 +08:00
|
|
|
SourceLocation(),
|
2010-09-15 05:59:34 +08:00
|
|
|
ii,
|
2010-06-09 00:52:24 +08:00
|
|
|
QualType::getFromOpaquePtr(type),
|
|
|
|
0,
|
2010-09-23 11:01:22 +08:00
|
|
|
SC_Static,
|
|
|
|
SC_Static);
|
2010-10-16 06:48:33 +08:00
|
|
|
m_decls.push_back(Decl);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
return Decl;
|
|
|
|
}
|
2010-06-23 07:46:24 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
|
|
|
|
clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
|
|
|
|
const_cast<DeclContext*>(m_decl_context),
|
|
|
|
SourceLocation(),
|
|
|
|
m_decl_name.getAsIdentifierInfo(),
|
|
|
|
QualType::getFromOpaquePtr(type),
|
|
|
|
NULL,
|
|
|
|
SC_Static,
|
|
|
|
SC_Static,
|
|
|
|
false,
|
|
|
|
true);
|
2010-06-23 07:46:24 +08:00
|
|
|
|
2010-08-13 07:45:38 +08:00
|
|
|
// We have to do more than just synthesize the FunctionDecl. We have to
|
|
|
|
// synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
|
|
|
|
// this, we raid the function's FunctionProtoType for types.
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
QualType qual_type (QualType::getFromOpaquePtr(type));
|
|
|
|
const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
|
2010-06-23 07:46:24 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
if (func_proto_type)
|
2010-06-24 02:58:10 +08:00
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
unsigned NumArgs = func_proto_type->getNumArgs();
|
2010-06-23 07:46:24 +08:00
|
|
|
unsigned ArgIndex;
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
|
2010-06-23 07:46:24 +08:00
|
|
|
|
|
|
|
for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
|
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
|
2010-06-23 07:46:24 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
|
|
|
|
const_cast<DeclContext*>(m_decl_context),
|
|
|
|
SourceLocation(),
|
|
|
|
NULL,
|
|
|
|
arg_qual_type,
|
|
|
|
NULL,
|
|
|
|
SC_Static,
|
|
|
|
SC_Static,
|
|
|
|
NULL);
|
2010-06-23 07:46:24 +08:00
|
|
|
}
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
func_decl->setParams(param_var_decls, NumArgs);
|
2010-06-24 02:58:10 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
delete [] param_var_decls;
|
2010-06-23 07:46:24 +08:00
|
|
|
}
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
m_decls.push_back(func_decl);
|
2010-06-23 07:46:24 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
return func_decl;
|
2010-06-23 07:46:24 +08:00
|
|
|
}
|
2010-07-27 08:55:47 +08:00
|
|
|
|
|
|
|
clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
|
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
|
|
|
|
NULL, // argument types
|
|
|
|
0, // number of arguments
|
|
|
|
true, // variadic?
|
|
|
|
0, // type qualifiers
|
|
|
|
false, // has exception specification?
|
|
|
|
false, // has any exception specification?
|
|
|
|
0, // number of exceptions
|
|
|
|
NULL, // exceptions
|
|
|
|
FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
|
|
|
|
|
2010-07-27 08:55:47 +08:00
|
|
|
return AddFunDecl(generic_function_type.getAsOpaquePtr());
|
|
|
|
}
|
2010-08-04 09:02:13 +08:00
|
|
|
|
|
|
|
clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
|
|
|
|
{
|
2010-10-16 06:48:33 +08:00
|
|
|
QualType qual_type = QualType::getFromOpaquePtr(type);
|
2010-08-04 09:02:13 +08:00
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
|
2010-08-04 09:02:13 +08:00
|
|
|
{
|
|
|
|
TagDecl *tag_decl = tag_type->getDecl();
|
|
|
|
|
2010-10-16 06:48:33 +08:00
|
|
|
m_decls.push_back(tag_decl);
|
2010-08-04 09:02:13 +08:00
|
|
|
|
|
|
|
return tag_decl;
|
|
|
|
}
|
2010-12-14 06:46:15 +08:00
|
|
|
else if (ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
|
|
|
|
{
|
|
|
|
ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
|
|
|
|
|
|
|
|
m_decls.push_back((NamedDecl*)interface_decl);
|
|
|
|
|
|
|
|
return (NamedDecl*)interface_decl;
|
|
|
|
}
|
2010-08-04 09:02:13 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|