summaryrefslogtreecommitdiffstats
path: root/clang/lib/AST
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-03-19 03:51:16 +0000
committerDouglas Gregor <dgregor@apple.com>2009-03-19 03:51:16 +0000
commit183539102563b85e9421d0fc5c3a650a20887840 (patch)
tree7a18d1af87575a34033cebf9778c66508d13eec9 /clang/lib/AST
parent3b7b301a2462c9982192c41de7e11cbd804792a3 (diff)
downloadbcm5719-llvm-183539102563b85e9421d0fc5c3a650a20887840.tar.gz
bcm5719-llvm-183539102563b85e9421d0fc5c3a650a20887840.zip
Generalize printing of nested-name-specifier sequences for use in both
QualifiedNameType and QualifiedDeclRefExpr. We now keep track of the exact nested-name-specifier spelling for a QualifiedDeclRefExpr, and use that spelling when printing ASTs. This fixes PR3493. llvm-svn: 67283
Diffstat (limited to 'clang/lib/AST')
-rw-r--r--clang/lib/AST/ExprCXX.cpp25
-rw-r--r--clang/lib/AST/NestedNameSpecifier.cpp27
-rw-r--r--clang/lib/AST/StmtPrinter.cpp22
-rw-r--r--clang/lib/AST/StmtSerialization.cpp3
-rw-r--r--clang/lib/AST/Type.cpp21
5 files changed, 61 insertions, 37 deletions
diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp
index 1d4a3ba3a63..f18a2888470 100644
--- a/clang/lib/AST/ExprCXX.cpp
+++ b/clang/lib/AST/ExprCXX.cpp
@@ -24,6 +24,31 @@ void CXXConditionDeclExpr::Destroy(ASTContext& C) {
C.Deallocate(this);
}
+QualifiedDeclRefExpr::QualifiedDeclRefExpr(NamedDecl *d, QualType t,
+ SourceLocation l, bool TD,
+ bool VD, SourceRange R,
+ const NestedNameSpecifier *Components,
+ unsigned NumComponents)
+ : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD),
+ QualifierRange(R), NumComponents(NumComponents) {
+ NestedNameSpecifier *Data
+ = reinterpret_cast<NestedNameSpecifier *>(this + 1);
+ for (unsigned I = 0; I < NumComponents; ++I)
+ Data[I] = Components[I];
+}
+
+QualifiedDeclRefExpr *
+QualifiedDeclRefExpr::Create(ASTContext &Context, NamedDecl *d, QualType t,
+ SourceLocation l, bool TD,
+ bool VD, SourceRange R,
+ const NestedNameSpecifier *Components,
+ unsigned NumComponents) {
+ void *Mem = Context.Allocate((sizeof(QualifiedDeclRefExpr) +
+ sizeof(NestedNameSpecifier) * NumComponents));
+ return new (Mem) QualifiedDeclRefExpr(d, t, l, TD, VD, R, Components,
+ NumComponents);
+}
+
//===----------------------------------------------------------------------===//
// Child Iterators for iterating over subexpressions/substatements
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/AST/NestedNameSpecifier.cpp b/clang/lib/AST/NestedNameSpecifier.cpp
index 318c05fe7ca..ea4b506e640 100644
--- a/clang/lib/AST/NestedNameSpecifier.cpp
+++ b/clang/lib/AST/NestedNameSpecifier.cpp
@@ -15,6 +15,8 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
+#include "llvm/Support/raw_ostream.h"
+
using namespace clang;
DeclContext *
@@ -32,3 +34,28 @@ NestedNameSpecifier::computeDeclContext(ASTContext &Context) const {
assert(TagT && "No DeclContext from a non-tag type");
return TagT->getDecl();
}
+
+void NestedNameSpecifier::Print(llvm::raw_ostream &OS,
+ const NestedNameSpecifier *First,
+ const NestedNameSpecifier *Last) {
+ for (; First != Last; ++First) {
+ if (Type *T = First->getAsType()) {
+ std::string TypeStr;
+
+ // If this is a qualified name type, suppress the qualification:
+ // it's part of our nested-name-specifier sequence anyway.
+ if (const QualifiedNameType *QualT = dyn_cast<QualifiedNameType>(T))
+ T = QualT->getNamedType().getTypePtr();
+
+ if (const TagType *TagT = dyn_cast<TagType>(T))
+ TagT->getAsStringInternal(TypeStr, true);
+ else
+ T->getAsStringInternal(TypeStr);
+ OS << TypeStr;
+ } else if (NamedDecl *NamedDC
+ = dyn_cast_or_null<NamedDecl>(First->getAsDeclContext()))
+ OS << NamedDC->getNameAsString();
+
+ OS << "::";
+ }
+}
diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp
index c52eb7e84ef..3a6a01c5686 100644
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -530,28 +530,10 @@ void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
OS << Node->getDecl()->getNameAsString();
}
-void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
- // FIXME: Should we keep enough information in QualifiedDeclRefExpr
- // to produce the same qualification that the user wrote?
- llvm::SmallVector<DeclContext *, 4> Contexts;
-
+void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
NamedDecl *D = Node->getDecl();
- // Build up a stack of contexts.
- DeclContext *Ctx = D->getDeclContext();
- for (; Ctx; Ctx = Ctx->getParent())
- if (!Ctx->isTransparentContext())
- Contexts.push_back(Ctx);
-
- while (!Contexts.empty()) {
- DeclContext *Ctx = Contexts.back();
- if (isa<TranslationUnitDecl>(Ctx))
- OS << "::";
- else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
- OS << ND->getNameAsString() << "::";
- Contexts.pop_back();
- }
-
+ NestedNameSpecifier::Print(OS, Node->begin(), Node->end());
OS << D->getNameAsString();
}
diff --git a/clang/lib/AST/StmtSerialization.cpp b/clang/lib/AST/StmtSerialization.cpp
index 78366a1514f..51843114661 100644
--- a/clang/lib/AST/StmtSerialization.cpp
+++ b/clang/lib/AST/StmtSerialization.cpp
@@ -1589,7 +1589,8 @@ CXXTryStmt::CreateImpl(llvm::Deserializer& D, ASTContext& C) {
void QualifiedDeclRefExpr::EmitImpl(llvm::Serializer& S) const {
DeclRefExpr::EmitImpl(S);
- S.Emit(NestedNameLoc);
+ S.Emit(QualifierRange);
+ // FIXME: Serialize nested-name-specifiers
}
QualifiedDeclRefExpr*
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index b066802c67a..1fb1e999bd5 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -18,7 +18,7 @@
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "llvm/ADT/StringExtras.h"
-
+#include "llvm/Support/raw_ostream.h"
using namespace clang;
bool QualType::isConstant(ASTContext &Ctx) const {
@@ -549,13 +549,12 @@ const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const {
}
const ClassTemplateSpecializationType *
-Type::getClassTemplateSpecializationType() const {
+Type::getAsClassTemplateSpecializationType() const {
// There is no sugar for class template specialization types, so
// just return the canonical type pointer if it is the right class.
return dyn_cast<ClassTemplateSpecializationType>(CanonicalType);
}
-
bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
@@ -1438,19 +1437,9 @@ getAsStringInternal(std::string &InnerString) const {
void QualifiedNameType::getAsStringInternal(std::string &InnerString) const {
std::string MyString;
- for (iterator Comp = begin(), CompEnd = end(); Comp != CompEnd; ++Comp) {
- if (Type *T = Comp->getAsType()) {
- std::string TypeStr;
- if (const TagType *TagT = dyn_cast<TagType>(T))
- TagT->getAsStringInternal(TypeStr, true);
- else
- T->getAsStringInternal(TypeStr);
-
- MyString += TypeStr;
- } else if (NamedDecl *NamedDC
- = dyn_cast_or_null<NamedDecl>(Comp->getAsDeclContext()))
- MyString += NamedDC->getNameAsString();
- MyString += "::";
+ {
+ llvm::raw_string_ostream OS(MyString);
+ NestedNameSpecifier::Print(OS, begin(), end());
}
std::string TypeStr;
OpenPOWER on IntegriCloud