blob: 66b393eb6571d5f50489de38d103a6d02806dfa9 [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()) {
David Blaikieb219cfc2011-09-23 05:06:16 +000044 default: llvm_unreachable("Invalid Kind");
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000045 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()) {
David Blaikieb219cfc2011-09-23 05:06:16 +000063 default: llvm_unreachable("Invalid Kind");
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000064 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 Bagnarabd054db2010-05-20 10:00:11 +000071 return AsTypeLoc().getLocalSourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000072 }
73
74 return SourceRange();
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000075}
76
Chris Lattner5f9e2722011-07-23 10:55:15 +000077void ASTLocation::print(raw_ostream &OS) const {
Argyrios Kyrtzidis9b9685d2009-07-18 21:17:43 +000078 if (isInvalid()) {
79 OS << "<< Invalid ASTLocation >>\n";
80 return;
81 }
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000082
83 ASTContext &Ctx = getParentDecl()->getASTContext();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000084
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000085 switch (getKind()) {
86 case N_Decl:
87 OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
88 if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
Benjamin Kramerb8989f22011-10-14 18:45:37 +000089 OS << *ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000090 break;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000092 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 Kramerb8989f22011-10-14 18:45:37 +000099 OS << *AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000100 break;
101
102 case N_Type: {
John McCall51bd8032009-10-18 01:05:36 +0000103 QualType T = AsTypeLoc().getType();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000104 OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
105 }
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000106 }
107
108 OS << "] <";
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +0000110 SourceRange Range = getSourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000111 SourceManager &SourceMgr = Ctx.getSourceManager();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000112 Range.getBegin().print(OS, SourceMgr);
113 OS << ", ";
114 Range.getEnd().print(OS, SourceMgr);
115 OS << ">\n";
116}