blob: c24f3bf5b3c1f6fab3dba3eb3630e12aee2a23fd [file] [log] [blame]
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +00001//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +00002//
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 Kyrtzidis874012b2009-07-06 21:34:20 +000010// ASTLocation is Decl or a Stmt and its immediate Decl parent.
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000011//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +000014#include "clang/Index/ASTLocation.h"
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000015#include "clang/AST/Decl.h"
Argyrios Kyrtzidiscc1ccb72009-07-18 00:33:46 +000016#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000017#include "clang/AST/Stmt.h"
18#include "clang/AST/Expr.h"
Argyrios Kyrtzidis80ede1d2009-07-21 00:05:38 +000019#include "clang/AST/ExprObjC.h"
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000020using namespace clang;
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +000021using namespace idx;
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000022
Argyrios Kyrtzidisdc50c642009-07-18 21:17:58 +000023static 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 Kyrtzidis80ede1d2009-07-21 00:05:38 +000028 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
29 return RE->getDecl();
30
Argyrios Kyrtzidisdc50c642009-07-18 21:17:58 +000031 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 Stump1eb44332009-09-09 15:08:12 +000035
Argyrios Kyrtzidisdc50c642009-07-18 21:17:58 +000036 return 0;
37}
38
39Decl *ASTLocation::getReferencedDecl() {
40 if (isInvalid())
41 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000043 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 Kyrtzidisc0824632009-07-05 22:21:28 +000053 }
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000054
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000055 return 0;
56}
57
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000058SourceRange ASTLocation::getSourceRange() const {
Argyrios Kyrtzidis9b9685d2009-07-18 21:17:43 +000059 if (isInvalid())
60 return SourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000061
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 Kyrtzidis755c6b42009-07-06 21:35:20 +000076}
77
Argyrios Kyrtzidis6dbbc0e2009-07-29 23:39:35 +000078void ASTLocation::print(llvm::raw_ostream &OS) const {
Argyrios Kyrtzidis9b9685d2009-07-18 21:17:43 +000079 if (isInvalid()) {
80 OS << "<< Invalid ASTLocation >>\n";
81 return;
82 }
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000083
84 ASTContext &Ctx = getParentDecl()->getASTContext();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000085
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000086 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 Stump1eb44332009-09-09 15:08:12 +000092
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000093 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 McCall51bd8032009-10-18 01:05:36 +0000104 QualType T = AsTypeLoc().getType();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000105 OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
106 }
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000107 }
108
109 OS << "] <";
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +0000111 SourceRange Range = getSourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000112 SourceManager &SourceMgr = Ctx.getSourceManager();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000113 Range.getBegin().print(OS, SourceMgr);
114 OS << ", ";
115 Range.getEnd().print(OS, SourceMgr);
116 OS << ">\n";
117}