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
18namespace clang {
19namespace ast_type_traits {
20
21const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
22  { NKI_None, "<None>" },
23  { NKI_None, "CXXCtorInitializer" },
24  { NKI_None, "TemplateArgument" },
25  { NKI_None, "NestedNameSpecifier" },
26  { NKI_None, "NestedNameSpecifierLoc" },
27  { NKI_None, "QualType" },
28  { NKI_None, "TypeLoc" },
29  { NKI_None, "Decl" },
30#define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
31#include "clang/AST/DeclNodes.inc"
32  { NKI_None, "Stmt" },
33#define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
34#include "clang/AST/StmtNodes.inc"
35  { NKI_None, "Type" },
36#define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
37#include "clang/AST/TypeNodes.def"
38};
39
40bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
41  return isBaseOf(KindId, Other.KindId);
42}
43
44bool ASTNodeKind::isSame(ASTNodeKind Other) const {
45  return KindId != NKI_None && KindId == Other.KindId;
46}
47
48bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived) {
49  if (Base == NKI_None || Derived == NKI_None) return false;
50  while (Derived != Base && Derived != NKI_None)
51    Derived = AllKindInfo[Derived].ParentId;
52  return Derived == Base;
53}
54
55StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
56
57} // end namespace ast_type_traits
58} // end namespace clang
59