blob: 2d5dc88f8466772023b72ff4f0b0da4e49a42b29 [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()) {
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000044 case N_Type:
45 return 0;
46 case N_Decl:
47 return D;
48 case N_NamedRef:
49 return NDRef.ND;
50 case N_Stmt:
51 return getDeclFromExpr(Stm);
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000052 }
David Blaikie30263482012-01-20 21:50:17 +000053
54 llvm_unreachable("Invalid ASTLocation Kind!");
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000055}
56
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000057SourceRange ASTLocation::getSourceRange() const {
Argyrios Kyrtzidis9b9685d2009-07-18 21:17:43 +000058 if (isInvalid())
59 return SourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000060
61 switch (getKind()) {
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000062 case N_Decl:
63 return D->getSourceRange();
64 case N_Stmt:
65 return Stm->getSourceRange();
66 case N_NamedRef:
67 return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc);
68 case N_Type:
Abramo Bagnarabd054db2010-05-20 10:00:11 +000069 return AsTypeLoc().getLocalSourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000070 }
David Blaikie30263482012-01-20 21:50:17 +000071
72 llvm_unreachable("Invalid ASTLocation Kind!");
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000073}
74
Chris Lattner5f9e2722011-07-23 10:55:15 +000075void ASTLocation::print(raw_ostream &OS) const {
Argyrios Kyrtzidis9b9685d2009-07-18 21:17:43 +000076 if (isInvalid()) {
77 OS << "<< Invalid ASTLocation >>\n";
78 return;
79 }
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000080
81 ASTContext &Ctx = getParentDecl()->getASTContext();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000082
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000083 switch (getKind()) {
84 case N_Decl:
85 OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
86 if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
Benjamin Kramerb8989f22011-10-14 18:45:37 +000087 OS << *ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000088 break;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000090 case N_Stmt:
91 OS << "[Stmt: " << AsStmt()->getStmtClassName() << " ";
92 AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
93 break;
94
95 case N_NamedRef:
96 OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " ";
Benjamin Kramerb8989f22011-10-14 18:45:37 +000097 OS << *AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +000098 break;
99
100 case N_Type: {
John McCall51bd8032009-10-18 01:05:36 +0000101 QualType T = AsTypeLoc().getType();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000102 OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
103 }
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000104 }
105
106 OS << "] <";
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +0000108 SourceRange Range = getSourceRange();
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000109 SourceManager &SourceMgr = Ctx.getSourceManager();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +0000110 Range.getBegin().print(OS, SourceMgr);
111 OS << ", ";
112 Range.getEnd().print(OS, SourceMgr);
113 OS << ">\n";
114}