Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 1 | //===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===// |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 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 | // |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 10 | // ASTLocation is Decl or a Stmt and its immediate Decl parent. |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 14 | #include "clang/Index/ASTLocation.h" |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Argyrios Kyrtzidis | cc1ccb7 | 2009-07-18 00:33:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
| 18 | #include "clang/AST/Expr.h" |
Argyrios Kyrtzidis | 80ede1d | 2009-07-21 00:05:38 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 20 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 21 | using namespace idx; |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 22 | |
Argyrios Kyrtzidis | dc50c64 | 2009-07-18 21:17:58 +0000 | [diff] [blame] | 23 | static Decl *getDeclFromExpr(Stmt *E) { |
| 24 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 25 | return RefExpr->getDecl(); |
| 26 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 27 | return ME->getMemberDecl(); |
Argyrios Kyrtzidis | 80ede1d | 2009-07-21 00:05:38 +0000 | [diff] [blame] | 28 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 29 | return RE->getDecl(); |
| 30 | |
Argyrios Kyrtzidis | dc50c64 | 2009-07-18 21:17:58 +0000 | [diff] [blame] | 31 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 32 | return getDeclFromExpr(CE->getCallee()); |
| 33 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 34 | return getDeclFromExpr(CE->getSubExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Argyrios Kyrtzidis | dc50c64 | 2009-07-18 21:17:58 +0000 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | Decl *ASTLocation::getReferencedDecl() { |
| 40 | if (isInvalid()) |
| 41 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 43 | switch (getKind()) { |
| 44 | default: assert(0 && "Invalid Kind"); |
| 45 | case N_Type: |
| 46 | return 0; |
| 47 | case N_Decl: |
| 48 | return D; |
| 49 | case N_NamedRef: |
| 50 | return NDRef.ND; |
| 51 | case N_Stmt: |
| 52 | return getDeclFromExpr(Stm); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 53 | } |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 54 | |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 58 | SourceRange ASTLocation::getSourceRange() const { |
Argyrios Kyrtzidis | 9b9685d | 2009-07-18 21:17:43 +0000 | [diff] [blame] | 59 | if (isInvalid()) |
| 60 | return SourceRange(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 61 | |
| 62 | switch (getKind()) { |
| 63 | default: assert(0 && "Invalid Kind"); |
| 64 | return SourceRange(); |
| 65 | case N_Decl: |
| 66 | return D->getSourceRange(); |
| 67 | case N_Stmt: |
| 68 | return Stm->getSourceRange(); |
| 69 | case N_NamedRef: |
| 70 | return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc); |
| 71 | case N_Type: |
| 72 | return AsTypeLoc().getSourceRange(); |
| 73 | } |
| 74 | |
| 75 | return SourceRange(); |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Argyrios Kyrtzidis | 6dbbc0e | 2009-07-29 23:39:35 +0000 | [diff] [blame] | 78 | void ASTLocation::print(llvm::raw_ostream &OS) const { |
Argyrios Kyrtzidis | 9b9685d | 2009-07-18 21:17:43 +0000 | [diff] [blame] | 79 | if (isInvalid()) { |
| 80 | OS << "<< Invalid ASTLocation >>\n"; |
| 81 | return; |
| 82 | } |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 83 | |
| 84 | ASTContext &Ctx = getParentDecl()->getASTContext(); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 85 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 86 | switch (getKind()) { |
| 87 | case N_Decl: |
| 88 | OS << "[Decl: " << AsDecl()->getDeclKindName() << " "; |
| 89 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl())) |
| 90 | OS << ND->getNameAsString(); |
| 91 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 93 | case N_Stmt: |
| 94 | OS << "[Stmt: " << AsStmt()->getStmtClassName() << " "; |
| 95 | AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
| 96 | break; |
| 97 | |
| 98 | case N_NamedRef: |
| 99 | OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " "; |
| 100 | OS << AsNamedRef().ND->getNameAsString(); |
| 101 | break; |
| 102 | |
| 103 | case N_Type: { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 104 | QualType T = AsTypeLoc().getType(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 105 | OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString(); |
| 106 | } |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | OS << "] <"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 111 | SourceRange Range = getSourceRange(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 112 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 113 | Range.getBegin().print(OS, SourceMgr); |
| 114 | OS << ", "; |
| 115 | Range.getEnd().print(OS, SourceMgr); |
| 116 | OS << ">\n"; |
| 117 | } |