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
diff --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp
index 04d21eb..7cccdda 100644
--- a/lldb/source/Symbol/ClangASTContext.cpp
+++ b/lldb/source/Symbol/ClangASTContext.cpp
@@ -285,7 +285,8 @@
 }
 
 
-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 @@
                            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 @@
                                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 @@
         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 @@
 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 @@
 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 @@
 {
     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 @@
 {
     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 @@
 {
     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 @@
 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 @@
 {
     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 @@
 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 @@
 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 @@
     
     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 @@
 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 @@
 {
     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 @@
     
     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 @@
         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 @@
 {
     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 @@
 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;
 }