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()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 44 | default: llvm_unreachable("Invalid Kind"); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 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()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 63 | default: llvm_unreachable("Invalid Kind"); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 64 | case N_Decl: |
| 65 | return D->getSourceRange(); |
| 66 | case N_Stmt: |
| 67 | return Stm->getSourceRange(); |
| 68 | case N_NamedRef: |
| 69 | return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc); |
| 70 | case N_Type: |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 71 | return AsTypeLoc().getLocalSourceRange(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return SourceRange(); |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | void ASTLocation::print(raw_ostream &OS) const { |
Argyrios Kyrtzidis | 9b9685d | 2009-07-18 21:17:43 +0000 | [diff] [blame] | 78 | if (isInvalid()) { |
| 79 | OS << "<< Invalid ASTLocation >>\n"; |
| 80 | return; |
| 81 | } |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 82 | |
| 83 | ASTContext &Ctx = getParentDecl()->getASTContext(); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 84 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 85 | switch (getKind()) { |
| 86 | case N_Decl: |
| 87 | OS << "[Decl: " << AsDecl()->getDeclKindName() << " "; |
| 88 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl())) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 89 | OS << *ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 90 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 92 | case N_Stmt: |
| 93 | OS << "[Stmt: " << AsStmt()->getStmtClassName() << " "; |
| 94 | AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions())); |
| 95 | break; |
| 96 | |
| 97 | case N_NamedRef: |
| 98 | OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " "; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 99 | OS << *AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 100 | break; |
| 101 | |
| 102 | case N_Type: { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 103 | QualType T = AsTypeLoc().getType(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 104 | OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString(); |
| 105 | } |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | OS << "] <"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Argyrios Kyrtzidis | 755c6b4 | 2009-07-06 21:35:20 +0000 | [diff] [blame] | 110 | SourceRange Range = getSourceRange(); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 111 | SourceManager &SourceMgr = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | c082463 | 2009-07-05 22:21:28 +0000 | [diff] [blame] | 112 | Range.getBegin().print(OS, SourceMgr); |
| 113 | OS << ", "; |
| 114 | Range.getEnd().print(OS, SourceMgr); |
| 115 | OS << ">\n"; |
| 116 | } |