Use LLVM casting for TypeSystem so you can cast it to subclasses.

This will keep our code cleaner and it removes the need for intrusive additions to TypeSystem like:

class TypeSystem
{
    virtual ClangASTContext *
    AsClangASTContext() = 0;
}

As you can now just use the llvm::dyn_cast and other casts.

llvm-svn: 247041
This commit is contained in:
Greg Clayton 2015-09-08 18:15:05 +00:00
parent 21a145c341
commit f73034f99a
13 changed files with 143 additions and 85 deletions

View File

@ -40,7 +40,15 @@ class ClangASTContext : public TypeSystem
public:
typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton, clang::ObjCInterfaceDecl *);
//------------------------------------------------------------------
// llvm casting support
//------------------------------------------------------------------
static bool classof(const TypeSystem *ts)
{
return ts->getKind() == TypeSystem::eKindClang;
}
//------------------------------------------------------------------
// Constructors and Destructors
//------------------------------------------------------------------
@ -482,12 +490,6 @@ public:
// TypeSystem methods
//------------------------------------------------------------------
ClangASTContext*
AsClangASTContext() override
{
return this;
}
DWARFASTParser *
GetDWARFParser () override;
@ -532,7 +534,7 @@ public:
static bool
IsClangType (const CompilerType &ct)
{
return (ct.GetTypeSystem()->AsClangASTContext() != nullptr);
return llvm::dyn_cast_or_null<ClangASTContext>(ct.GetTypeSystem()) != nullptr;
}
//----------------------------------------------------------------------
@ -1049,7 +1051,9 @@ public:
static clang::QualType
GetQualType (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
// Make sure we have a clang type before making a clang::QualType
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
if (ast)
return clang::QualType::getFromOpaquePtr(type.GetOpaqueQualType());
return clang::QualType();
}
@ -1057,7 +1061,9 @@ public:
static clang::QualType
GetCanonicalQualType (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
// Make sure we have a clang type before making a clang::QualType
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
if (ast)
return clang::QualType::getFromOpaquePtr(type.GetOpaqueQualType()).getCanonicalType();
return clang::QualType();
}

View File

@ -17,6 +17,7 @@
#include "lldb/Symbol/CompilerDeclContext.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/Type.h"
#include "llvm/Support/Casting.h"
class DWARFDIE;
class DWARFASTParser;
@ -29,15 +30,52 @@ namespace lldb_private {
class TypeSystem
{
public:
//----------------------------------------------------------------------
// Intrusive type system that allows us to use llvm casting.
//
// To add a new type system:
//
// 1 - Add a new enumeration for llvm casting below for your TypeSystem
// subclass, here we will use eKindFoo
//
// 2 - Your TypeSystem subclass will inherit from TypeSystem and needs
// to implement a static classof() function that returns your
// enumeration:
//
// class Foo : public lldb_private::TypeSystem
// {
// static bool classof(const TypeSystem *ts)
// {
// return ts->getKind() == TypeSystem::eKindFoo;
// }
// };
//
// 3 - Contruct your TypeSystem subclass with the enumeration from below
//
// Foo() :
// TypeSystem(TypeSystem::eKindFoo),
// ...
// {
// }
//
// Then you can use the llvm casting on any "TypeSystem *" to get an
// instance of your subclass.
//----------------------------------------------------------------------
enum LLVMCastKind {
eKindClang,
eKindSwift,
eKindGo,
kNumKinds
};
LLVMCastKind getKind() const { return m_kind; }
//----------------------------------------------------------------------
// Constructors and Destructors
//----------------------------------------------------------------------
TypeSystem ();
TypeSystem (LLVMCastKind kind);
virtual ~TypeSystem ();
virtual ClangASTContext *
AsClangASTContext() = 0;
virtual DWARFASTParser *
GetDWARFParser ()
@ -395,6 +433,7 @@ public:
virtual bool
IsReferenceType (void *type, CompilerType *pointee_type, bool* is_rvalue) = 0;
protected:
const LLVMCastKind m_kind; // Support for llvm casting
SymbolFile *m_sym_file;
};

View File

@ -11,7 +11,7 @@
#include "lldb/Core/Flags.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Target/Target.h"
#include <inttypes.h>
using namespace lldb;
@ -21,9 +21,11 @@ using namespace lldb_private::formatters;
bool
lldb_private::formatters::CMTimeSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
{
if (!valobj.GetCompilerType().IsValid())
CompilerType type = valobj.GetCompilerType();
if (!type.IsValid())
return false;
ClangASTContext *ast_ctx = valobj.GetCompilerType().GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast_ctx = valobj.GetExecutionContextRef().GetTargetSP()->GetScratchClangASTContext();
if (!ast_ctx)
return false;

View File

@ -529,11 +529,15 @@ lldb_private::formatters::NSArrayISyntheticFrontEnd::NSArrayISyntheticFrontEnd (
m_items (0),
m_data_ptr (0)
{
if (valobj_sp && valobj_sp->GetCompilerType().IsValid())
if (valobj_sp)
{
ClangASTContext *ast = valobj_sp->GetCompilerType().GetTypeSystem()->AsClangASTContext();
if (ast)
m_id_type = CompilerType(ast->getASTContext(), ast->getASTContext()->ObjCBuiltinIdTy);
CompilerType type = valobj_sp->GetCompilerType();
if (type)
{
ClangASTContext *ast = valobj_sp->GetExecutionContextRef().GetTargetSP()->GetScratchClangASTContext();
if (ast)
m_id_type = CompilerType(ast->getASTContext(), ast->getASTContext()->ObjCBuiltinIdTy);
}
}
}

View File

@ -27,7 +27,6 @@ public:
NSIndexPathSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
SyntheticChildrenFrontEnd (*valobj_sp.get()),
m_ptr_size(0),
m_ast_ctx(nullptr),
m_uint_star_type()
{
m_ptr_size = m_backend.GetTargetSP()->GetArchitecture().GetAddressByteSize();
@ -53,11 +52,12 @@ public:
TypeSystem* type_system = m_backend.GetCompilerType().GetTypeSystem();
if (!type_system)
return false;
m_ast_ctx = type_system->AsClangASTContext();
if (!m_ast_ctx)
ClangASTContext *ast = m_backend.GetExecutionContextRef().GetTargetSP()->GetScratchClangASTContext();
if (!ast)
return false;
m_uint_star_type = m_ast_ctx->GetPointerSizedIntType(false);
m_uint_star_type = ast->GetPointerSizedIntType(false);
static ConstString g__indexes("_indexes");
static ConstString g__length("_length");
@ -325,7 +325,6 @@ protected:
} m_impl;
uint32_t m_ptr_size;
ClangASTContext* m_ast_ctx;
CompilerType m_uint_star_type;
};

View File

@ -232,7 +232,7 @@ namespace lldb_private {
CompilerType parent_type(m_backend.GetCompilerType());
CompilerType element_type;
parent_type.IsVectorType(&element_type, nullptr);
m_child_type = ::GetCompilerTypeForFormat(m_parent_format, element_type, parent_type.GetTypeSystem()->AsClangASTContext());
m_child_type = ::GetCompilerTypeForFormat(m_parent_format, element_type, llvm::dyn_cast_or_null<ClangASTContext>(parent_type.GetTypeSystem()));
m_num_children = ::CalculateNumChildren(parent_type,
m_child_type);
m_item_format = GetItemFormatForFormat(m_parent_format,

View File

@ -1889,15 +1889,12 @@ ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::Name
CompilerType
ClangASTSource::GuardedCopyType (const CompilerType &src_type)
{
if (!ClangASTContext::IsClangType(src_type))
ClangASTContext *src_ast = llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
if (src_ast == nullptr)
return CompilerType();
ClangASTMetrics::RegisterLLDBImport();
ClangASTContext* src_ast = src_type.GetTypeSystem()->AsClangASTContext();
if (!src_ast)
return CompilerType();
SetImportInProgress(true);
QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_ast->getASTContext(), ClangASTContext::GetQualType(src_type));
@ -1920,7 +1917,7 @@ NameSearchContext::AddVarDecl(const CompilerType &type)
if (!type.IsValid())
return NULL;
ClangASTContext* lldb_ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext* lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!lldb_ast)
return NULL;
@ -1952,7 +1949,7 @@ NameSearchContext::AddFunDecl (const CompilerType &type, bool extern_c)
if (m_function_types.count(type))
return NULL;
ClangASTContext* lldb_ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext* lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!lldb_ast)
return NULL;

View File

@ -194,19 +194,23 @@ ClangExpressionDeclMap::AddPersistentVariable
{
assert (m_parser_vars.get());
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem());
if (ast == nullptr)
return false;
if (m_parser_vars->m_materializer && is_result)
{
Error err;
ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
Target *target = exe_ctx.GetTargetPtr();
if (target == NULL)
if (target == nullptr)
return false;
ClangASTContext *context(target->GetScratchClangASTContext());
TypeFromUser user_type(m_ast_importer->DeportType(context->getASTContext(),
parser_type.GetTypeSystem()->AsClangASTContext()->getASTContext(),
ast->getASTContext(),
parser_type.GetOpaqueQualType()),
context);
@ -247,7 +251,7 @@ ClangExpressionDeclMap::AddPersistentVariable
ClangASTContext *context(target->GetScratchClangASTContext());
TypeFromUser user_type(m_ast_importer->DeportType(context->getASTContext(),
parser_type.GetTypeSystem()->AsClangASTContext()->getASTContext(),
ast->getASTContext(),
parser_type.GetOpaqueQualType()),
context);

View File

@ -423,13 +423,7 @@ ABIMacOSX_arm::GetReturnValueObjectImpl (Thread &thread,
if (!clang_type)
return return_valobj_sp;
ClangASTContext* ast = clang_type.GetTypeSystem()->AsClangASTContext();
clang::ASTContext *ast_context = ast ? ast->getASTContext() : nullptr;
if (!ast_context)
return return_valobj_sp;
//value.SetContext (Value::eContextTypeClangType, clang_type.GetOpaqueQualType());
value.SetCompilerType (clang_type);
RegisterContext *reg_ctx = thread.GetRegisterContext().get();

View File

@ -1597,8 +1597,7 @@ public:
bool
Finalize()
{
ClangASTContext* ast = m_class_opaque_type.GetTypeSystem()->AsClangASTContext();
assert(ast);
ClangASTContext *ast = llvm::cast<ClangASTContext>(m_class_opaque_type.GetTypeSystem());
return ast->AddObjCClassProperty (m_class_opaque_type,
m_property_name,
m_property_opaque_type,
@ -2360,7 +2359,7 @@ DWARFASTParserClang::ParseChildMembers (const SymbolContext& sc,
BitfieldInfo last_field_info;
ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
ClangASTContext* ast = class_clang_type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem());
if (ast == nullptr)
return 0;

View File

@ -285,7 +285,8 @@ ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
}
ClangASTContext::ClangASTContext (const char *target_triple) :
ClangASTContext::ClangASTContext (const char *target_triple) :
TypeSystem (TypeSystem::eKindClang),
m_target_triple (),
m_ast_ap (),
m_language_options_ap (),
@ -1118,7 +1119,7 @@ ClangASTContext::CopyType (ASTContext *dst_ast,
CompilerType src)
{
FileSystemOptions file_system_options;
ClangASTContext *src_ast = src.GetTypeSystem()->AsClangASTContext();
ClangASTContext *src_ast = llvm::dyn_cast_or_null<ClangASTContext>(src.GetTypeSystem());
if (src_ast == nullptr)
return CompilerType();
FileManager file_manager (file_system_options);
@ -1151,8 +1152,8 @@ ClangASTContext::AreTypesSame (CompilerType type1,
CompilerType type2,
bool ignore_qualifiers)
{
TypeSystem *ast = type1.GetTypeSystem();
if (!ast->AsClangASTContext() || ast != type2.GetTypeSystem())
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
if (!ast || ast != type2.GetTypeSystem())
return false;
if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
@ -1167,7 +1168,7 @@ ClangASTContext::AreTypesSame (CompilerType type1,
type2_qual = type2_qual.getUnqualifiedType();
}
return ast->AsClangASTContext()->getASTContext()->hasSameType (type1_qual, type2_qual);
return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
}
CompilerType
@ -3767,19 +3768,21 @@ ClangASTContext::GetTypeQualifiers(void* type)
CompilerType
ClangASTContext::AddConstModifier (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
if (type && llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()))
{
// Make sure this type is a clang AST type
clang::QualType result(GetQualType(type));
result.addConst();
return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
}
return CompilerType();
}
CompilerType
ClangASTContext::AddRestrictModifier (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
if (type && llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()))
{
clang::QualType result(GetQualType(type));
result.getQualifiers().setRestrict (true);
@ -3791,7 +3794,7 @@ ClangASTContext::AddRestrictModifier (const CompilerType& type)
CompilerType
ClangASTContext::AddVolatileModifier (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
if (type && llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()))
{
clang::QualType result(GetQualType(type));
result.getQualifiers().setVolatile (true);
@ -4094,7 +4097,7 @@ ClangASTContext::GetLValueReferenceType (const CompilerType& type)
{
if (type)
{
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (ast)
return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
}
@ -4106,7 +4109,7 @@ ClangASTContext::GetRValueReferenceType (const CompilerType& type)
{
if (type)
{
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (ast)
return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
}
@ -4128,7 +4131,7 @@ ClangASTContext::CreateTypedefType (const CompilerType& type,
{
if (type && typedef_name && typedef_name[0])
{
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return CompilerType();
clang::ASTContext* clang_ast = ast->getASTContext();
@ -4201,7 +4204,7 @@ ClangASTContext::GetTypedefedType (void* type)
CompilerType
ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
{
if (type && type.GetTypeSystem()->AsClangASTContext())
if (type && llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()))
{
clang::QualType qual_type(GetQualType(type));
qual_type.getQualifiers().removeFastQualifiers();
@ -6959,7 +6962,7 @@ ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *nam
{
if (!type.IsValid() || !field_clang_type.IsValid())
return nullptr;
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return nullptr;
clang::ASTContext* clang_ast = ast->getASTContext();
@ -7049,9 +7052,10 @@ ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *nam
void
ClangASTContext::BuildIndirectFields (const CompilerType& type)
{
ClangASTContext* ast = nullptr;
if (type)
ast = type.GetTypeSystem()->AsClangASTContext();
if (!type)
return;
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return;
@ -7160,12 +7164,19 @@ ClangASTContext::BuildIndirectFields (const CompilerType& type)
void
ClangASTContext::SetIsPacked (const CompilerType& type)
{
clang::RecordDecl *record_decl = GetAsRecordDecl(type);
if (type)
{
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (ast)
{
clang::RecordDecl *record_decl = GetAsRecordDecl(type);
if (!record_decl)
return;
if (!record_decl)
return;
record_decl->addAttr(clang::PackedAttr::CreateImplicit(*type.GetTypeSystem()->AsClangASTContext()->getASTContext()));
record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
}
}
}
clang::VarDecl *
@ -7177,7 +7188,7 @@ ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *
if (!type.IsValid() || !var_type.IsValid())
return nullptr;
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return nullptr;
@ -7460,7 +7471,7 @@ ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier
bool
ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
{
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return false;
clang::ASTContext* clang_ast = ast->getASTContext();
@ -7490,7 +7501,7 @@ ClangASTContext::AddObjCClassProperty (const CompilerType& type,
{
if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
return false;
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return false;
clang::ASTContext* clang_ast = ast->getASTContext();
@ -7695,8 +7706,11 @@ ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
if (class_interface_decl == nullptr)
return nullptr;
clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (lldb_ast == nullptr)
return nullptr;
clang::ASTContext *ast = lldb_ast->getASTContext();
const char *selector_start = ::strchr (name, ' ');
if (selector_start == nullptr)
return nullptr;
@ -7917,8 +7931,11 @@ ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
clang::QualType qual_type (GetQualType(type));
if (qual_type.isNull())
return false;
clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (lldb_ast == nullptr)
return false;
clang::ASTContext *ast = lldb_ast->getASTContext();
clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
if (cxx_record_decl)
@ -8033,7 +8050,7 @@ ClangASTContext::CreateMemberPointerType (const CompilerType& type, const Compil
{
if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
{
ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!ast)
return CompilerType();
return CompilerType (ast->getASTContext(),
@ -8934,13 +8951,9 @@ ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const vo
clang::ASTContext *
ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
{
TypeSystem *type_system = dc.GetTypeSystem();
if (type_system)
{
ClangASTContext *ast = type_system->AsClangASTContext();
if (ast)
return ast->getASTContext();
}
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
if (ast)
return ast->getASTContext();
return nullptr;
}

View File

@ -15,7 +15,7 @@ using namespace lldb_private;
bool
CompilerDeclContext::IsClang () const
{
return IsValid() && m_type_system->AsClangASTContext() != nullptr;
return IsValid() && m_type_system->getKind() == TypeSystem::eKindClang;
}
ConstString

View File

@ -10,7 +10,8 @@
using namespace lldb_private;
TypeSystem::TypeSystem() :
TypeSystem::TypeSystem(LLVMCastKind kind) :
m_kind (kind),
m_sym_file (nullptr)
{
}