clang API Documentation

ASTTypeTraits.cpp
Go to the documentation of this file.
00001 //===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Provides a dynamic type identifier and a dynamically typed node container
00011 //  that can be used to store an AST base node at runtime in the same storage in
00012 //  a type safe way.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "clang/AST/ASTTypeTraits.h"
00017 #include "clang/AST/ASTContext.h"
00018 #include "clang/AST/DeclCXX.h"
00019 
00020 namespace clang {
00021 namespace ast_type_traits {
00022 
00023 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
00024   { NKI_None, "<None>" },
00025   { NKI_None, "CXXCtorInitializer" },
00026   { NKI_None, "TemplateArgument" },
00027   { NKI_None, "NestedNameSpecifier" },
00028   { NKI_None, "NestedNameSpecifierLoc" },
00029   { NKI_None, "QualType" },
00030   { NKI_None, "TypeLoc" },
00031   { NKI_None, "Decl" },
00032 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
00033 #include "clang/AST/DeclNodes.inc"
00034   { NKI_None, "Stmt" },
00035 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
00036 #include "clang/AST/StmtNodes.inc"
00037   { NKI_None, "Type" },
00038 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
00039 #include "clang/AST/TypeNodes.def"
00040 };
00041 
00042 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
00043   return isBaseOf(KindId, Other.KindId, Distance);
00044 }
00045 
00046 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
00047   return KindId != NKI_None && KindId == Other.KindId;
00048 }
00049 
00050 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
00051                            unsigned *Distance) {
00052   if (Base == NKI_None || Derived == NKI_None) return false;
00053   unsigned Dist = 0;
00054   while (Derived != Base && Derived != NKI_None) {
00055     Derived = AllKindInfo[Derived].ParentId;
00056     ++Dist;
00057   }
00058   if (Distance)
00059     *Distance = Dist;
00060   return Derived == Base;
00061 }
00062 
00063 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
00064 
00065 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
00066                                             ASTNodeKind Kind2) {
00067   if (Kind1.isBaseOf(Kind2)) return Kind2;
00068   if (Kind2.isBaseOf(Kind1)) return Kind1;
00069   return ASTNodeKind();
00070 }
00071 
00072 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
00073                                                       ASTNodeKind Kind2) {
00074   NodeKindId Parent = Kind1.KindId;
00075   while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
00076     Parent = AllKindInfo[Parent].ParentId;
00077   }
00078   return ASTNodeKind(Parent);
00079 }
00080 
00081 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
00082   switch (D.getKind()) {
00083 #define DECL(DERIVED, BASE)                                                    \
00084     case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
00085 #define ABSTRACT_DECL(D)
00086 #include "clang/AST/DeclNodes.inc"
00087   };
00088   llvm_unreachable("invalid decl kind");
00089 }
00090 
00091 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
00092   switch (S.getStmtClass()) {
00093     case Stmt::NoStmtClass: return NKI_None;
00094 #define STMT(CLASS, PARENT)                                                    \
00095     case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
00096 #define ABSTRACT_STMT(S)
00097 #include "clang/AST/StmtNodes.inc"
00098   }
00099   llvm_unreachable("invalid stmt kind");
00100 }
00101 
00102 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
00103   switch (T.getTypeClass()) {
00104 #define TYPE(Class, Base)                                                      \
00105     case Type::Class: return ASTNodeKind(NKI_##Class##Type);
00106 #define ABSTRACT_TYPE(Class, Base)
00107 #include "clang/AST/TypeNodes.def"
00108   }
00109   llvm_unreachable("invalid type kind");
00110 }
00111 
00112 void DynTypedNode::print(llvm::raw_ostream &OS,
00113                          const PrintingPolicy &PP) const {
00114   if (const TemplateArgument *TA = get<TemplateArgument>())
00115     TA->print(PP, OS);
00116   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
00117     NNS->print(OS, PP);
00118   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
00119     NNSL->getNestedNameSpecifier()->print(OS, PP);
00120   else if (const QualType *QT = get<QualType>())
00121     QT->print(OS, PP);
00122   else if (const TypeLoc *TL = get<TypeLoc>())
00123     TL->getType().print(OS, PP);
00124   else if (const Decl *D = get<Decl>())
00125     D->print(OS, PP);
00126   else if (const Stmt *S = get<Stmt>())
00127     S->printPretty(OS, nullptr, PP);
00128   else if (const Type *T = get<Type>())
00129     QualType(T, 0).print(OS, PP);
00130   else
00131     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
00132 }
00133 
00134 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
00135   if (const Decl *D = get<Decl>())
00136     D->dump(OS);
00137   else if (const Stmt *S = get<Stmt>())
00138     S->dump(OS, SM);
00139   else
00140     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
00141 }
00142 
00143 SourceRange DynTypedNode::getSourceRange() const {
00144   if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
00145     return CCI->getSourceRange();
00146   if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
00147     return NNSL->getSourceRange();
00148   if (const TypeLoc *TL = get<TypeLoc>())
00149     return TL->getSourceRange();
00150   if (const Decl *D = get<Decl>())
00151     return D->getSourceRange();
00152   if (const Stmt *S = get<Stmt>())
00153     return S->getSourceRange();
00154   return SourceRange();
00155 }
00156 
00157 } // end namespace ast_type_traits
00158 } // end namespace clang