1 //===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTLocation is Decl or a Stmt and its immediate Decl parent.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Index/ASTLocation.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Stmt.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprObjC.h"
20 using namespace clang;
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();
28 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
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());
39 Decl *ASTLocation::getReferencedDecl() {
44 default: llvm_unreachable("Invalid Kind");
52 return getDeclFromExpr(Stm);
58 SourceRange ASTLocation::getSourceRange() const {
63 default: llvm_unreachable("Invalid Kind");
65 return D->getSourceRange();
67 return Stm->getSourceRange();
69 return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc);
71 return AsTypeLoc().getLocalSourceRange();
77 void ASTLocation::print(raw_ostream &OS) const {
79 OS << "<< Invalid ASTLocation >>\n";
83 ASTContext &Ctx = getParentDecl()->getASTContext();
87 OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
88 if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
93 OS << "[Stmt: " << AsStmt()->getStmtClassName() << " ";
94 AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
98 OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " ";
99 OS << *AsNamedRef().ND;
103 QualType T = AsTypeLoc().getType();
104 OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
110 SourceRange Range = getSourceRange();
111 SourceManager &SourceMgr = Ctx.getSourceManager();
112 Range.getBegin().print(OS, SourceMgr);
114 Range.getEnd().print(OS, SourceMgr);