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: assert(0 && "Invalid Kind");
52 return getDeclFromExpr(Stm);
58 SourceRange ASTLocation::getSourceRange() const {
63 default: assert(0 && "Invalid Kind");
66 return D->getSourceRange();
68 return Stm->getSourceRange();
70 return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc);
72 return AsTypeLoc().getLocalSourceRange();
78 void ASTLocation::print(llvm::raw_ostream &OS) const {
80 OS << "<< Invalid ASTLocation >>\n";
84 ASTContext &Ctx = getParentDecl()->getASTContext();
88 OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
89 if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
94 OS << "[Stmt: " << AsStmt()->getStmtClassName() << " ";
95 AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
99 OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " ";
100 OS << AsNamedRef().ND;
104 QualType T = AsTypeLoc().getType();
105 OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
111 SourceRange Range = getSourceRange();
112 SourceManager &SourceMgr = Ctx.getSourceManager();
113 Range.getBegin().print(OS, SourceMgr);
115 Range.getEnd().print(OS, SourceMgr);