2007-08-09 06:51:59 +08:00
|
|
|
//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-09 06:51:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Stmt::dump/Stmt::print methods, which dump out the
|
|
|
|
// AST in a form that exposes type details and other fields.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-10-18 02:36:42 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-02-04 03:21:40 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-05-30 04:38:28 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2007-08-30 14:17:34 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-12-03 17:13:13 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-08-09 06:51:59 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StmtDumper Visitor
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class StmtDumper : public StmtVisitor<StmtDumper> {
|
2007-08-30 14:17:34 +08:00
|
|
|
SourceManager *SM;
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &OS;
|
2007-08-09 06:51:59 +08:00
|
|
|
unsigned IndentLevel;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
/// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
|
|
|
|
/// the first few levels of an AST. This keeps track of how many ast levels
|
|
|
|
/// are left.
|
|
|
|
unsigned MaxDepth;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 14:17:34 +08:00
|
|
|
/// LastLocFilename/LastLocLine - Keep track of the last location we print
|
|
|
|
/// out so that we can print out deltas from then on out.
|
|
|
|
const char *LastLocFilename;
|
|
|
|
unsigned LastLocLine;
|
2009-05-30 04:38:28 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
public:
|
2011-07-23 18:55:15 +08:00
|
|
|
StmtDumper(SourceManager *sm, raw_ostream &os, unsigned maxDepth)
|
2009-12-03 17:13:13 +08:00
|
|
|
: SM(sm), OS(os), IndentLevel(0-1), MaxDepth(maxDepth) {
|
2007-08-30 14:17:34 +08:00
|
|
|
LastLocFilename = "";
|
|
|
|
LastLocLine = ~0U;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-10 02:03:18 +08:00
|
|
|
void DumpSubTree(Stmt *S) {
|
2007-08-09 06:51:59 +08:00
|
|
|
// Prune the recursion if not using dump all.
|
|
|
|
if (MaxDepth == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-10 02:03:18 +08:00
|
|
|
++IndentLevel;
|
2007-08-09 06:51:59 +08:00
|
|
|
if (S) {
|
2007-12-12 14:59:42 +08:00
|
|
|
if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
|
|
|
|
VisitDeclStmt(DS);
|
2009-09-09 23:08:12 +08:00
|
|
|
else {
|
2007-12-12 14:59:42 +08:00
|
|
|
Visit(S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 14:59:42 +08:00
|
|
|
// Print out children.
|
2011-02-13 12:07:26 +08:00
|
|
|
Stmt::child_range CI = S->children();
|
|
|
|
if (CI) {
|
|
|
|
while (CI) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '\n';
|
2007-12-12 14:59:42 +08:00
|
|
|
DumpSubTree(*CI++);
|
|
|
|
}
|
2007-08-30 08:53:54 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-26 01:56:43 +08:00
|
|
|
OS << ')';
|
2007-08-09 06:51:59 +08:00
|
|
|
} else {
|
|
|
|
Indent();
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "<<<NULL>>>";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2007-08-10 02:03:18 +08:00
|
|
|
--IndentLevel;
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-10 02:03:18 +08:00
|
|
|
void DumpDeclarator(Decl *D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void Indent() const {
|
|
|
|
for (int i = 0, e = IndentLevel; i < e; ++i)
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-02 05:08:38 +08:00
|
|
|
void DumpType(QualType T) {
|
2010-12-10 19:01:00 +08:00
|
|
|
SplitQualType T_split = T.split();
|
|
|
|
OS << "'" << QualType::getAsString(T_split) << "'";
|
2007-08-09 08:36:22 +08:00
|
|
|
|
2008-12-24 08:01:03 +08:00
|
|
|
if (!T.isNull()) {
|
2009-09-25 03:53:00 +08:00
|
|
|
// If the type is sugared, also dump a (shallow) desugared type.
|
2010-12-10 19:01:00 +08:00
|
|
|
SplitQualType D_split = T.getSplitDesugaredType();
|
|
|
|
if (T_split != D_split)
|
|
|
|
OS << ":'" << QualType::getAsString(D_split) << "'";
|
2008-04-02 13:06:23 +08:00
|
|
|
}
|
2007-08-09 08:36:22 +08:00
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
void DumpDeclRef(Decl *node);
|
2007-09-02 05:08:38 +08:00
|
|
|
void DumpStmt(const Stmt *Node) {
|
2007-08-09 06:51:59 +08:00
|
|
|
Indent();
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "(" << Node->getStmtClassName()
|
|
|
|
<< " " << (void*)Node;
|
2007-09-02 05:08:38 +08:00
|
|
|
DumpSourceRange(Node);
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2010-11-18 14:31:45 +08:00
|
|
|
void DumpValueKind(ExprValueKind K) {
|
|
|
|
switch (K) {
|
|
|
|
case VK_RValue: break;
|
|
|
|
case VK_LValue: OS << " lvalue"; break;
|
|
|
|
case VK_XValue: OS << " xvalue"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DumpObjectKind(ExprObjectKind K) {
|
|
|
|
switch (K) {
|
|
|
|
case OK_Ordinary: break;
|
|
|
|
case OK_BitField: OS << " bitfield"; break;
|
|
|
|
case OK_ObjCProperty: OS << " objcproperty"; break;
|
|
|
|
case OK_VectorComponent: OS << " vectorcomponent"; break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-02 05:08:38 +08:00
|
|
|
void DumpExpr(const Expr *Node) {
|
2007-08-09 06:51:59 +08:00
|
|
|
DumpStmt(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << ' ';
|
2007-08-09 08:36:22 +08:00
|
|
|
DumpType(Node->getType());
|
2010-11-18 14:31:45 +08:00
|
|
|
DumpValueKind(Node->getValueKind());
|
|
|
|
DumpObjectKind(Node->getObjectKind());
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2007-09-02 05:08:38 +08:00
|
|
|
void DumpSourceRange(const Stmt *Node);
|
2007-08-30 14:17:34 +08:00
|
|
|
void DumpLocation(SourceLocation Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 09:00:35 +08:00
|
|
|
// Stmts.
|
2007-08-21 12:04:25 +08:00
|
|
|
void VisitStmt(Stmt *Node);
|
2007-12-12 14:59:42 +08:00
|
|
|
void VisitDeclStmt(DeclStmt *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitLabelStmt(LabelStmt *Node);
|
|
|
|
void VisitGotoStmt(GotoStmt *Node);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 09:00:35 +08:00
|
|
|
// Exprs
|
|
|
|
void VisitExpr(Expr *Node);
|
2009-08-23 07:33:40 +08:00
|
|
|
void VisitCastExpr(CastExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *Node);
|
2008-08-10 09:53:14 +08:00
|
|
|
void VisitPredefinedExpr(PredefinedExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitCharacterLiteral(CharacterLiteral *Node);
|
|
|
|
void VisitIntegerLiteral(IntegerLiteral *Node);
|
|
|
|
void VisitFloatingLiteral(FloatingLiteral *Node);
|
|
|
|
void VisitStringLiteral(StringLiteral *Str);
|
|
|
|
void VisitUnaryOperator(UnaryOperator *Node);
|
2011-03-12 03:24:49 +08:00
|
|
|
void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitMemberExpr(MemberExpr *Node);
|
2008-04-19 07:10:10 +08:00
|
|
|
void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitBinaryOperator(BinaryOperator *Node);
|
|
|
|
void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
|
|
|
|
void VisitAddrLabelExpr(AddrLabelExpr *Node);
|
2011-02-07 18:33:21 +08:00
|
|
|
void VisitBlockExpr(BlockExpr *Node);
|
2011-11-06 17:01:30 +08:00
|
|
|
void VisitOpaqueValueExpr(OpaqueValueExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
|
|
|
|
// C++
|
2008-10-28 03:41:14 +08:00
|
|
|
void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
|
2008-11-04 22:56:14 +08:00
|
|
|
void VisitCXXThisExpr(CXXThisExpr *Node);
|
2008-10-28 03:41:14 +08:00
|
|
|
void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
|
2009-08-12 08:21:52 +08:00
|
|
|
void VisitCXXConstructExpr(CXXConstructExpr *Node);
|
|
|
|
void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
|
2010-12-06 16:20:24 +08:00
|
|
|
void VisitExprWithCleanups(ExprWithCleanups *Node);
|
2009-12-12 05:50:11 +08:00
|
|
|
void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node);
|
2009-08-12 08:21:52 +08:00
|
|
|
void DumpCXXTemporary(CXXTemporary *Temporary);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 09:00:35 +08:00
|
|
|
// ObjC
|
2010-04-24 06:50:49 +08:00
|
|
|
void VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node);
|
2007-08-30 09:00:35 +08:00
|
|
|
void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
|
2008-03-01 06:04:05 +08:00
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr* Node);
|
2007-10-17 04:40:23 +08:00
|
|
|
void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
|
2007-10-18 00:58:11 +08:00
|
|
|
void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
|
2008-08-30 13:35:15 +08:00
|
|
|
void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
|
2008-03-12 21:19:12 +08:00
|
|
|
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
|
2007-08-09 06:51:59 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-08-30 14:17:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void StmtDumper::DumpLocation(SourceLocation Loc) {
|
2009-01-16 15:00:02 +08:00
|
|
|
SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 14:17:34 +08:00
|
|
|
// The general format we print out is filename:line:col, but we drop pieces
|
|
|
|
// that haven't changed since the last loc printed.
|
2009-01-27 15:57:44 +08:00
|
|
|
PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
|
|
|
|
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isInvalid()) {
|
|
|
|
OS << "<invalid sloc>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-27 15:57:44 +08:00
|
|
|
if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << PLoc.getFilename() << ':' << PLoc.getLine()
|
|
|
|
<< ':' << PLoc.getColumn();
|
2009-01-27 15:57:44 +08:00
|
|
|
LastLocFilename = PLoc.getFilename();
|
|
|
|
LastLocLine = PLoc.getLine();
|
|
|
|
} else if (PLoc.getLine() != LastLocLine) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "line" << ':' << PLoc.getLine()
|
|
|
|
<< ':' << PLoc.getColumn();
|
2009-01-27 15:57:44 +08:00
|
|
|
LastLocLine = PLoc.getLine();
|
2007-08-30 14:17:34 +08:00
|
|
|
} else {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "col" << ':' << PLoc.getColumn();
|
2007-08-30 14:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-02 05:08:38 +08:00
|
|
|
void StmtDumper::DumpSourceRange(const Stmt *Node) {
|
2007-08-30 14:17:34 +08:00
|
|
|
// Can't translate locations if a SourceManager isn't available.
|
|
|
|
if (SM == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 14:17:34 +08:00
|
|
|
// TODO: If the parent expression is available, we can print a delta vs its
|
|
|
|
// location.
|
|
|
|
SourceRange R = Node->getSourceRange();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " <";
|
2007-10-17 06:36:42 +08:00
|
|
|
DumpLocation(R.getBegin());
|
|
|
|
if (R.getBegin() != R.getEnd()) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << ", ";
|
2007-10-17 06:36:42 +08:00
|
|
|
DumpLocation(R.getEnd());
|
2007-08-30 14:17:34 +08:00
|
|
|
}
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << ">";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-30 14:17:34 +08:00
|
|
|
// <t2.c:123:421[blah], t2.c:412:321>
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void StmtDumper::VisitStmt(Stmt *Node) {
|
2007-08-30 09:00:35 +08:00
|
|
|
DumpStmt(Node);
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2007-08-10 02:03:18 +08:00
|
|
|
void StmtDumper::DumpDeclarator(Decl *D) {
|
2007-08-09 06:51:59 +08:00
|
|
|
// FIXME: Need to complete/beautify this... this code simply shows the
|
|
|
|
// nodes are where they need to be.
|
|
|
|
if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "\"typedef " << localType->getUnderlyingType().getAsString()
|
2011-10-15 02:45:37 +08:00
|
|
|
<< ' ' << *localType << '"';
|
2011-04-15 22:24:37 +08:00
|
|
|
} else if (TypeAliasDecl *localType = dyn_cast<TypeAliasDecl>(D)) {
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << "\"using " << *localType << " = "
|
2011-04-15 22:24:37 +08:00
|
|
|
<< localType->getUnderlyingType().getAsString() << '"';
|
2007-08-09 06:51:59 +08:00
|
|
|
} else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "\"";
|
2007-08-09 06:51:59 +08:00
|
|
|
// Emit storage class for vardecls.
|
|
|
|
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
|
2010-08-26 11:08:43 +08:00
|
|
|
if (V->getStorageClass() != SC_None)
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
|
|
|
|
<< " ";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-24 12:00:27 +08:00
|
|
|
std::string Name = VD->getNameAsString();
|
2009-09-09 23:08:12 +08:00
|
|
|
VD->getType().getAsStringInternal(Name,
|
2009-06-30 09:26:17 +08:00
|
|
|
PrintingPolicy(VD->getASTContext().getLangOptions()));
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << Name;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
// If this is a vardecl with an initializer, emit it.
|
|
|
|
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
|
|
|
|
if (V->getInit()) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " =\n";
|
2007-08-10 02:03:18 +08:00
|
|
|
DumpSubTree(V->getInit());
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '"';
|
2007-11-18 05:37:36 +08:00
|
|
|
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
// print a free standing tag decl (e.g. "struct x;").
|
|
|
|
const char *tagname;
|
|
|
|
if (const IdentifierInfo *II = TD->getIdentifier())
|
2009-10-19 04:26:12 +08:00
|
|
|
tagname = II->getNameStart();
|
2007-11-18 05:37:36 +08:00
|
|
|
else
|
|
|
|
tagname = "<anonymous>";
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '"' << TD->getKindName() << ' ' << tagname << ";\"";
|
2007-11-18 05:37:36 +08:00
|
|
|
// FIXME: print tag bodies.
|
2009-02-04 03:21:40 +08:00
|
|
|
} else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
|
|
|
|
// print using-directive decl (e.g. "using namespace x;")
|
|
|
|
const char *ns;
|
|
|
|
if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
|
2009-10-19 04:26:12 +08:00
|
|
|
ns = II->getNameStart();
|
2009-02-04 03:21:40 +08:00
|
|
|
else
|
|
|
|
ns = "<anonymous>";
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '"' << UD->getDeclKindName() << ns << ";\"";
|
2010-05-04 18:20:17 +08:00
|
|
|
} else if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
|
|
|
|
// print using decl (e.g. "using std::string;")
|
|
|
|
const char *tn = UD->isTypeName() ? "typename " : "";
|
|
|
|
OS << '"' << UD->getDeclKindName() << tn;
|
2011-02-25 08:36:19 +08:00
|
|
|
UD->getQualifier()->print(OS,
|
|
|
|
PrintingPolicy(UD->getASTContext().getLangOptions()));
|
2010-05-04 18:20:17 +08:00
|
|
|
OS << ";\"";
|
2011-02-18 10:08:43 +08:00
|
|
|
} else if (LabelDecl *LD = dyn_cast<LabelDecl>(D)) {
|
|
|
|
OS << "label " << LD->getNameAsString();
|
2011-03-17 02:37:27 +08:00
|
|
|
} else if (StaticAssertDecl *SAD = dyn_cast<StaticAssertDecl>(D)) {
|
|
|
|
OS << "\"static_assert(\n";
|
|
|
|
DumpSubTree(SAD->getAssertExpr());
|
|
|
|
OS << ",\n";
|
|
|
|
DumpSubTree(SAD->getMessage());
|
|
|
|
OS << ");\"";
|
2007-08-09 06:51:59 +08:00
|
|
|
} else {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unexpected decl");
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-12 14:59:42 +08:00
|
|
|
void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
|
|
|
|
DumpStmt(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "\n";
|
2008-10-07 02:38:35 +08:00
|
|
|
for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
|
|
|
|
DI != DE; ++DI) {
|
2009-01-20 09:17:11 +08:00
|
|
|
Decl* D = *DI;
|
2007-12-12 14:59:42 +08:00
|
|
|
++IndentLevel;
|
|
|
|
Indent();
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << (void*) D << " ";
|
2007-12-12 14:59:42 +08:00
|
|
|
DumpDeclarator(D);
|
2009-03-30 00:04:50 +08:00
|
|
|
if (DI+1 != DE)
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "\n";
|
2007-12-12 14:59:42 +08:00
|
|
|
--IndentLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
|
|
|
|
DumpStmt(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " '" << Node->getName() << "'";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
|
|
|
|
DumpStmt(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " '" << Node->getLabel()->getName()
|
|
|
|
<< "':" << (void*)Node->getLabel();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Expr printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void StmtDumper::VisitExpr(Expr *Node) {
|
|
|
|
DumpExpr(Node);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DumpBasePath(raw_ostream &OS, CastExpr *Node) {
|
2010-08-07 14:22:56 +08:00
|
|
|
if (Node->path_empty())
|
2010-04-25 03:06:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
OS << " (";
|
|
|
|
bool First = true;
|
2010-08-07 14:22:56 +08:00
|
|
|
for (CastExpr::path_iterator
|
|
|
|
I = Node->path_begin(), E = Node->path_end(); I != E; ++I) {
|
2010-04-25 03:06:50 +08:00
|
|
|
const CXXBaseSpecifier *Base = *I;
|
|
|
|
if (!First)
|
|
|
|
OS << " -> ";
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (Base->isVirtual())
|
|
|
|
OS << "virtual ";
|
|
|
|
OS << RD->getName();
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
|
2009-08-23 07:33:40 +08:00
|
|
|
void StmtDumper::VisitCastExpr(CastExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2010-04-25 03:06:50 +08:00
|
|
|
OS << " <" << Node->getCastKindName();
|
|
|
|
DumpBasePath(OS, Node);
|
|
|
|
OS << ">";
|
2009-08-23 07:33:40 +08:00
|
|
|
}
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2007-09-11 01:32:55 +08:00
|
|
|
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ";
|
2011-02-07 18:33:21 +08:00
|
|
|
DumpDeclRef(Node->getDecl());
|
Add an optional field attached to a DeclRefExpr which points back to the
Decl actually found via name lookup & overload resolution when that Decl
is different from the ValueDecl which is actually referenced by the
expression.
This can be used by AST consumers to correctly attribute references to
the spelling location of a using declaration, and otherwise gain insight
into the name resolution performed by Clang.
The public interface to DRE is kept as narrow as possible: we provide
a getFoundDecl() which always returns a NamedDecl, either the ValueDecl
referenced or the new, more precise NamedDecl if present. This way AST
clients can code against getFoundDecl without know when exactly the AST
has a split representation.
For an example of the data this provides consider:
% cat x.cc
namespace N1 {
struct S {};
void f(const S&);
}
void test(N1::S s) {
f(s);
using N1::f;
f(s);
}
% ./bin/clang -fsyntax-only -Xclang -ast-dump x.cc
[...]
void test(N1::S s) (CompoundStmt 0x5b02010 <x.cc:5:20, line:9:1>
(CallExpr 0x5b01df0 <line:6:3, col:6> 'void'
(ImplicitCastExpr 0x5b01dd8 <col:3> 'void (*)(const struct N1::S &)' <FunctionToPointerDecay>
(DeclRefExpr 0x5b01d80 <col:3> 'void (const struct N1::S &)' lvalue Function 0x5b01a20 'f' 'void (const struct N1::S &)'))
(ImplicitCastExpr 0x5b01e20 <col:5> 'const struct N1::S' lvalue <NoOp>
(DeclRefExpr 0x5b01d58 <col:5> 'N1::S':'struct N1::S' lvalue ParmVar 0x5b01b60 's' 'N1::S':'struct N1::S')))
(DeclStmt 0x5b01ee0 <line:7:3, col:14>
0x5b01e40 "UsingN1::;")
(CallExpr 0x5b01fc8 <line:8:3, col:6> 'void'
(ImplicitCastExpr 0x5b01fb0 <col:3> 'void (*)(const struct N1::S &)' <FunctionToPointerDecay>
(DeclRefExpr 0x5b01f80 <col:3> 'void (const struct N1::S &)' lvalue Function 0x5b01a20 'f' 'void (const struct N1::S &)' (UsingShadow 0x5b01ea0 'f')))
(ImplicitCastExpr 0x5b01ff8 <col:5> 'const struct N1::S' lvalue <NoOp>
(DeclRefExpr 0x5b01f58 <col:5> 'N1::S':'struct N1::S' lvalue ParmVar 0x5b01b60 's' 'N1::S':'struct N1::S'))))
Now we can tell that the second call is 'using' (no pun intended) the using
declaration, and *which* using declaration it sees. Without this, we can
mistake calls that go through using declarations for ADL calls, and have no way
to attribute names looked up with using declarations to the appropriate
UsingDecl.
llvm-svn: 130670
2011-05-02 07:48:14 +08:00
|
|
|
if (Node->getDecl() != Node->getFoundDecl()) {
|
|
|
|
OS << " (";
|
|
|
|
DumpDeclRef(Node->getFoundDecl());
|
|
|
|
OS << ")";
|
|
|
|
}
|
2011-02-07 18:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::DumpDeclRef(Decl *d) {
|
|
|
|
OS << d->getDeclKindName() << ' ' << (void*) d;
|
|
|
|
|
|
|
|
if (NamedDecl *nd = dyn_cast<NamedDecl>(d)) {
|
|
|
|
OS << " '";
|
|
|
|
nd->getDeclName().printName(OS);
|
|
|
|
OS << "'";
|
2007-09-11 01:32:55 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
if (ValueDecl *vd = dyn_cast<ValueDecl>(d)) {
|
|
|
|
OS << ' '; DumpType(vd->getType());
|
|
|
|
}
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2009-12-12 05:50:11 +08:00
|
|
|
void StmtDumper::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
|
|
|
OS << " (";
|
|
|
|
if (!Node->requiresADL()) OS << "no ";
|
2010-04-17 17:33:03 +08:00
|
|
|
OS << "ADL) = '" << Node->getName() << '\'';
|
2009-12-12 05:50:11 +08:00
|
|
|
|
|
|
|
UnresolvedLookupExpr::decls_iterator
|
|
|
|
I = Node->decls_begin(), E = Node->decls_end();
|
|
|
|
if (I == E) OS << " empty";
|
|
|
|
for (; I != E; ++I)
|
|
|
|
OS << " " << (void*) *I;
|
|
|
|
}
|
|
|
|
|
2008-03-12 21:19:12 +08:00
|
|
|
void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
|
2008-05-23 08:59:14 +08:00
|
|
|
DumpExpr(Node);
|
2008-03-12 21:19:12 +08:00
|
|
|
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getDecl()->getDeclKindName()
|
2011-10-15 02:45:37 +08:00
|
|
|
<< "Decl='" << *Node->getDecl()
|
2009-12-03 17:13:13 +08:00
|
|
|
<< "' " << (void*)Node->getDecl();
|
2008-05-24 06:01:24 +08:00
|
|
|
if (Node->isFreeIvar())
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " isFreeIvar";
|
2008-03-12 21:19:12 +08:00
|
|
|
}
|
|
|
|
|
2008-08-10 09:53:14 +08:00
|
|
|
void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
|
2007-08-09 06:51:59 +08:00
|
|
|
DumpExpr(Node);
|
|
|
|
switch (Node->getIdentType()) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("unknown case");
|
2009-12-03 17:13:13 +08:00
|
|
|
case PredefinedExpr::Func: OS << " __func__"; break;
|
|
|
|
case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
|
|
|
|
case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
|
2007-08-09 09:04:32 +08:00
|
|
|
DumpExpr(Node);
|
2011-11-04 07:56:23 +08:00
|
|
|
OS << " " << Node->getValue();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
|
|
|
|
DumpExpr(Node);
|
|
|
|
|
|
|
|
bool isSigned = Node->getType()->isSignedIntegerType();
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getValue().toString(10, isSigned);
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getValueAsApproximateDouble();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2007-08-26 11:42:43 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
|
2007-08-09 09:04:32 +08:00
|
|
|
DumpExpr(Str);
|
|
|
|
// FIXME: this doesn't print wstrings right.
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ";
|
2011-07-27 13:40:30 +08:00
|
|
|
switch (Str->getKind()) {
|
|
|
|
case StringLiteral::Ascii: break; // No prefix
|
|
|
|
case StringLiteral::Wide: OS << 'L'; break;
|
|
|
|
case StringLiteral::UTF8: OS << "u8"; break;
|
|
|
|
case StringLiteral::UTF16: OS << 'u'; break;
|
|
|
|
case StringLiteral::UTF32: OS << 'U'; break;
|
|
|
|
}
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '"';
|
2010-08-17 20:54:38 +08:00
|
|
|
OS.write_escaped(Str->getString());
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << '"';
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2007-08-30 09:00:35 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
|
|
|
|
<< " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2011-03-12 03:24:49 +08:00
|
|
|
void StmtDumper::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2011-03-12 03:24:49 +08:00
|
|
|
switch(Node->getKind()) {
|
|
|
|
case UETT_SizeOf:
|
|
|
|
OS << " sizeof ";
|
|
|
|
break;
|
|
|
|
case UETT_AlignOf:
|
|
|
|
OS << " __alignof ";
|
|
|
|
break;
|
|
|
|
case UETT_VecStep:
|
|
|
|
OS << " vec_step ";
|
|
|
|
break;
|
|
|
|
}
|
2008-11-12 01:56:53 +08:00
|
|
|
if (Node->isArgumentType())
|
|
|
|
DumpType(Node->getArgumentType());
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2007-08-10 01:35:30 +08:00
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << (Node->isArrow() ? "->" : ".")
|
2011-10-15 02:45:37 +08:00
|
|
|
<< *Node->getMemberDecl() << ' '
|
2009-12-03 17:13:13 +08:00
|
|
|
<< (void*)Node->getMemberDecl();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
2008-04-19 07:10:10 +08:00
|
|
|
void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getAccessor().getNameStart();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
|
2007-08-25 10:00:02 +08:00
|
|
|
}
|
|
|
|
void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
|
|
|
|
<< "' ComputeLHSTy=";
|
2009-03-28 09:22:36 +08:00
|
|
|
DumpType(Node->getComputationLHSType());
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ComputeResultTy=";
|
2009-03-28 09:22:36 +08:00
|
|
|
DumpType(Node->getComputationResultType());
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
void StmtDumper::VisitBlockExpr(BlockExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
|
|
|
|
|
|
|
IndentLevel++;
|
|
|
|
BlockDecl *block = Node->getBlockDecl();
|
|
|
|
if (block->capturesCXXThis()) {
|
|
|
|
OS << '\n'; Indent(); OS << "(capture this)";
|
|
|
|
}
|
|
|
|
for (BlockDecl::capture_iterator
|
|
|
|
i = block->capture_begin(), e = block->capture_end(); i != e; ++i) {
|
|
|
|
OS << '\n';
|
|
|
|
Indent();
|
|
|
|
OS << "(capture ";
|
|
|
|
if (i->isByRef()) OS << "byref ";
|
|
|
|
if (i->isNested()) OS << "nested ";
|
|
|
|
DumpDeclRef(i->getVariable());
|
|
|
|
if (i->hasCopyExpr()) DumpSubTree(i->getCopyExpr());
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
IndentLevel--;
|
|
|
|
|
|
|
|
DumpSubTree(block->getBody());
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:30 +08:00
|
|
|
void StmtDumper::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
|
|
|
|
|
|
|
if (Expr *Source = Node->getSourceExpr()) {
|
|
|
|
OS << '\n';
|
|
|
|
DumpSubTree(Source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
// GNU extensions.
|
|
|
|
|
|
|
|
void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getLabel()->getName()
|
|
|
|
<< " " << (void*)Node->getLabel();
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2007-08-10 02:03:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C++ Expressions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-09 06:51:59 +08:00
|
|
|
|
2008-10-28 03:41:14 +08:00
|
|
|
void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getCastName()
|
|
|
|
<< "<" << Node->getTypeAsWritten().getAsString() << ">"
|
2010-04-25 03:06:50 +08:00
|
|
|
<< " <" << Node->getCastKindName();
|
|
|
|
DumpBasePath(OS, Node);
|
|
|
|
OS << ">";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
|
2007-08-10 01:35:30 +08:00
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << (Node->getValue() ? "true" : "false");
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
2008-11-04 22:56:14 +08:00
|
|
|
void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " this";
|
2008-11-04 22:56:14 +08:00
|
|
|
}
|
|
|
|
|
2008-10-28 03:41:14 +08:00
|
|
|
void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2011-09-03 01:38:59 +08:00
|
|
|
OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
|
|
|
|
<< " <" << Node->getCastKindName() << ">";
|
2008-10-28 03:41:14 +08:00
|
|
|
}
|
|
|
|
|
2009-08-12 08:21:52 +08:00
|
|
|
void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2010-02-03 03:03:45 +08:00
|
|
|
CXXConstructorDecl *Ctor = Node->getConstructor();
|
|
|
|
DumpType(Ctor->getType());
|
2009-08-12 08:21:52 +08:00
|
|
|
if (Node->isElidable())
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " elidable";
|
2010-08-07 14:38:55 +08:00
|
|
|
if (Node->requiresZeroInitialization())
|
|
|
|
OS << " zeroing";
|
2009-08-12 08:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ";
|
2009-08-12 08:21:52 +08:00
|
|
|
DumpCXXTemporary(Node->getTemporary());
|
|
|
|
}
|
|
|
|
|
2010-12-06 16:20:24 +08:00
|
|
|
void StmtDumper::VisitExprWithCleanups(ExprWithCleanups *Node) {
|
2009-08-12 08:21:52 +08:00
|
|
|
DumpExpr(Node);
|
|
|
|
++IndentLevel;
|
|
|
|
for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "\n";
|
2009-08-12 08:21:52 +08:00
|
|
|
Indent();
|
|
|
|
DumpCXXTemporary(Node->getTemporary(i));
|
|
|
|
}
|
|
|
|
--IndentLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << "(CXXTemporary " << (void *)Temporary << ")";
|
2009-08-12 08:21:52 +08:00
|
|
|
}
|
|
|
|
|
2007-08-22 01:43:55 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Obj-C Expressions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-01 06:04:05 +08:00
|
|
|
void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " selector=" << Node->getSelector().getAsString();
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
llvm-svn: 101972
2010-04-21 08:45:42 +08:00
|
|
|
switch (Node->getReceiverKind()) {
|
|
|
|
case ObjCMessageExpr::Instance:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ObjCMessageExpr::Class:
|
|
|
|
OS << " class=";
|
|
|
|
DumpType(Node->getClassReceiver());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ObjCMessageExpr::SuperInstance:
|
|
|
|
OS << " super (instance)";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ObjCMessageExpr::SuperClass:
|
|
|
|
OS << " super (class)";
|
|
|
|
break;
|
|
|
|
}
|
2008-03-01 06:04:05 +08:00
|
|
|
}
|
|
|
|
|
2010-04-24 06:50:49 +08:00
|
|
|
void StmtDumper::VisitObjCAtCatchStmt(ObjCAtCatchStmt *Node) {
|
|
|
|
DumpStmt(Node);
|
2010-04-27 00:46:50 +08:00
|
|
|
if (VarDecl *CatchParam = Node->getCatchParamDecl()) {
|
2010-04-24 06:50:49 +08:00
|
|
|
OS << " catch parm = ";
|
|
|
|
DumpDeclarator(CatchParam);
|
|
|
|
} else {
|
|
|
|
OS << " catch all";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-22 23:14:15 +08:00
|
|
|
void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " ";
|
2007-08-22 23:14:15 +08:00
|
|
|
DumpType(Node->getEncodedType());
|
|
|
|
}
|
|
|
|
|
2007-10-17 04:40:23 +08:00
|
|
|
void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-03 17:13:13 +08:00
|
|
|
OS << " " << Node->getSelector().getAsString();
|
2007-10-17 04:40:23 +08:00
|
|
|
}
|
|
|
|
|
2007-10-18 00:58:11 +08:00
|
|
|
void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << ' ' <<* Node->getProtocol();
|
2007-10-18 00:58:11 +08:00
|
|
|
}
|
2008-08-30 13:35:15 +08:00
|
|
|
|
|
|
|
void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
|
|
|
|
DumpExpr(Node);
|
2010-12-02 09:19:52 +08:00
|
|
|
if (Node->isImplicitProperty()) {
|
2010-12-23 03:46:35 +08:00
|
|
|
OS << " Kind=MethodRef Getter=\"";
|
|
|
|
if (Node->getImplicitPropertyGetter())
|
|
|
|
OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
|
|
|
|
else
|
|
|
|
OS << "(null)";
|
|
|
|
|
|
|
|
OS << "\" Setter=\"";
|
2010-12-02 09:19:52 +08:00
|
|
|
if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
|
|
|
|
OS << Setter->getSelector().getAsString();
|
|
|
|
else
|
|
|
|
OS << "(null)";
|
|
|
|
OS << "\"";
|
|
|
|
} else {
|
2011-10-15 02:45:37 +08:00
|
|
|
OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
|
2010-12-02 09:19:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-15 00:04:05 +08:00
|
|
|
if (Node->isSuperReceiver())
|
|
|
|
OS << " super";
|
2008-11-04 22:56:14 +08:00
|
|
|
}
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt method implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-08-30 08:40:08 +08:00
|
|
|
/// dump - This does a local dump of the specified AST fragment. It dumps the
|
|
|
|
/// specified node and a few nodes underneath it, but not the whole subtree.
|
|
|
|
/// This is useful in a debugger.
|
2007-08-30 14:17:34 +08:00
|
|
|
void Stmt::dump(SourceManager &SM) const {
|
2010-08-09 18:54:31 +08:00
|
|
|
dump(llvm::errs(), SM);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
|
2010-08-09 18:54:31 +08:00
|
|
|
StmtDumper P(&SM, OS, 4);
|
2007-08-30 08:53:54 +08:00
|
|
|
P.DumpSubTree(const_cast<Stmt*>(this));
|
2010-08-09 18:54:31 +08:00
|
|
|
OS << "\n";
|
2007-08-30 08:40:08 +08:00
|
|
|
}
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
/// dump - This does a local dump of the specified AST fragment. It dumps the
|
|
|
|
/// specified node and a few nodes underneath it, but not the whole subtree.
|
|
|
|
/// This is useful in a debugger.
|
|
|
|
void Stmt::dump() const {
|
2010-08-09 18:54:31 +08:00
|
|
|
StmtDumper P(0, llvm::errs(), 4);
|
2007-08-30 08:53:54 +08:00
|
|
|
P.DumpSubTree(const_cast<Stmt*>(this));
|
2010-08-09 18:54:31 +08:00
|
|
|
llvm::errs() << "\n";
|
2007-08-30 08:40:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
|
2007-08-30 14:17:34 +08:00
|
|
|
void Stmt::dumpAll(SourceManager &SM) const {
|
2010-08-09 18:54:31 +08:00
|
|
|
StmtDumper P(&SM, llvm::errs(), ~0U);
|
2007-08-30 08:53:54 +08:00
|
|
|
P.DumpSubTree(const_cast<Stmt*>(this));
|
2010-08-09 18:54:31 +08:00
|
|
|
llvm::errs() << "\n";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
|
|
|
|
void Stmt::dumpAll() const {
|
2010-08-09 18:54:31 +08:00
|
|
|
StmtDumper P(0, llvm::errs(), ~0U);
|
2007-08-30 08:53:54 +08:00
|
|
|
P.DumpSubTree(const_cast<Stmt*>(this));
|
2010-08-09 18:54:31 +08:00
|
|
|
llvm::errs() << "\n";
|
2007-08-09 06:51:59 +08:00
|
|
|
}
|