Home | History | Annotate | Download | only in AST
      1 //===--- ASTTypeTraits.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 //
     10 //  Provides a dynamic type identifier and a dynamically typed node container
     11 //  that can be used to store an AST base node at runtime in the same storage in
     12 //  a type safe way.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #include "clang/AST/ASTTypeTraits.h"
     17 #include "clang/AST/ASTContext.h"
     18 #include "clang/AST/DeclCXX.h"
     19 
     20 namespace clang {
     21 namespace ast_type_traits {
     22 
     23 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
     24   { NKI_None, "<None>" },
     25   { NKI_None, "CXXCtorInitializer" },
     26   { NKI_None, "TemplateArgument" },
     27   { NKI_None, "NestedNameSpecifier" },
     28   { NKI_None, "NestedNameSpecifierLoc" },
     29   { NKI_None, "QualType" },
     30   { NKI_None, "TypeLoc" },
     31   { NKI_None, "Decl" },
     32 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
     33 #include "clang/AST/DeclNodes.inc"
     34   { NKI_None, "Stmt" },
     35 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
     36 #include "clang/AST/StmtNodes.inc"
     37   { NKI_None, "Type" },
     38 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
     39 #include "clang/AST/TypeNodes.def"
     40 };
     41 
     42 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
     43   return isBaseOf(KindId, Other.KindId, Distance);
     44 }
     45 
     46 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
     47   return KindId != NKI_None && KindId == Other.KindId;
     48 }
     49 
     50 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
     51                            unsigned *Distance) {
     52   if (Base == NKI_None || Derived == NKI_None) return false;
     53   unsigned Dist = 0;
     54   while (Derived != Base && Derived != NKI_None) {
     55     Derived = AllKindInfo[Derived].ParentId;
     56     ++Dist;
     57   }
     58   if (Distance)
     59     *Distance = Dist;
     60   return Derived == Base;
     61 }
     62 
     63 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
     64 
     65 void DynTypedNode::print(llvm::raw_ostream &OS,
     66                          const PrintingPolicy &PP) const {
     67   if (const TemplateArgument *TA = get<TemplateArgument>())
     68     TA->print(PP, OS);
     69   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
     70     NNS->print(OS, PP);
     71   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
     72     NNSL->getNestedNameSpecifier()->print(OS, PP);
     73   else if (const QualType *QT = get<QualType>())
     74     QT->print(OS, PP);
     75   else if (const TypeLoc *TL = get<TypeLoc>())
     76     TL->getType().print(OS, PP);
     77   else if (const Decl *D = get<Decl>())
     78     D->print(OS, PP);
     79   else if (const Stmt *S = get<Stmt>())
     80     S->printPretty(OS, nullptr, PP);
     81   else if (const Type *T = get<Type>())
     82     QualType(T, 0).print(OS, PP);
     83   else
     84     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
     85 }
     86 
     87 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
     88   if (const Decl *D = get<Decl>())
     89     D->dump(OS);
     90   else if (const Stmt *S = get<Stmt>())
     91     S->dump(OS, SM);
     92   else
     93     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
     94 }
     95 
     96 SourceRange DynTypedNode::getSourceRange() const {
     97   if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
     98     return CCI->getSourceRange();
     99   if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
    100     return NNSL->getSourceRange();
    101   if (const TypeLoc *TL = get<TypeLoc>())
    102     return TL->getSourceRange();
    103   if (const Decl *D = get<Decl>())
    104     return D->getSourceRange();
    105   if (const Stmt *S = get<Stmt>())
    106     return S->getSourceRange();
    107   return SourceRange();
    108 }
    109 
    110 } // end namespace ast_type_traits
    111 } // end namespace clang
    112