[cfe-commits] r67283 - in /cfe/trunk: include/clang/AST/ExprCXX.h include/clang/AST/NestedNameSpecifier.h include/clang/AST/Type.h lib/AST/ExprCXX.cpp lib/AST/NestedNameSpecifier.cpp lib/AST/StmtPrinter.cpp lib/AST/StmtSerialization.cpp lib/AST/Type.cpp lib/Sema/SemaExpr.cpp test/SemaCXX/qualified-names-print.cpp

Douglas Gregor dgregor at apple.com
Wed Mar 18 20:51:17 PDT 2009


Author: dgregor
Date: Wed Mar 18 22:51:16 2009
New Revision: 67283

URL: http://llvm.org/viewvc/llvm-project?rev=67283&view=rev
Log:
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.


Added:
    cfe/trunk/test/SemaCXX/qualified-names-print.cpp   (with props)
Modified:
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/include/clang/AST/NestedNameSpecifier.h
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/lib/AST/ExprCXX.cpp
    cfe/trunk/lib/AST/NestedNameSpecifier.cpp
    cfe/trunk/lib/AST/StmtPrinter.cpp
    cfe/trunk/lib/AST/StmtSerialization.cpp
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Wed Mar 18 22:51:16 2009
@@ -852,18 +852,41 @@
 /// function, enum, etc., that includes a qualification, e.g.,
 /// "N::foo".
 class QualifiedDeclRefExpr : public DeclRefExpr {
-  /// NestedNameLoc - The location of the beginning of the
-  /// nested-name-specifier that qualifies this declaration.
-  SourceLocation NestedNameLoc;
+  /// QualifierRange - The source range that covers the
+  /// nested-name-specifier.
+  SourceRange QualifierRange;
+
+  /// The number of components in the complete nested-name-specifier.
+  unsigned NumComponents;
 
-public:
   QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, 
-                       bool VD, SourceLocation nnl)
-    : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD), 
-      NestedNameLoc(nnl) { }
+                       bool VD, SourceRange R,
+                       const NestedNameSpecifier *Components,
+                       unsigned NumComponents);
+
+public:
+  static QualifiedDeclRefExpr *Create(ASTContext &Context, NamedDecl *d, 
+                                      QualType t, SourceLocation l, bool TD, 
+                                      bool VD, SourceRange R,
+                                      const NestedNameSpecifier *Components,
+                                      unsigned NumComponents);
+
+  /// \brief Retrieve the source range of the nested-name-specifier.
+  SourceRange getQualifierRange() const { return QualifierRange; }
+
+  // Iteration over of the parts of the nested-name-specifier.
+  typedef const NestedNameSpecifier * iterator;
+
+  iterator begin() const { 
+    return reinterpret_cast<const NestedNameSpecifier *>(this + 1); 
+  }
+
+  iterator end() const { return begin() + NumComponents; }
+
+  unsigned size() const { return NumComponents; }
 
   virtual SourceRange getSourceRange() const { 
-    return SourceRange(NestedNameLoc, getLocation()); 
+    return SourceRange(QualifierRange.getBegin(), getLocation()); 
   }
 
   static bool classof(const Stmt *T) {

Modified: cfe/trunk/include/clang/AST/NestedNameSpecifier.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/NestedNameSpecifier.h?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/NestedNameSpecifier.h (original)
+++ cfe/trunk/include/clang/AST/NestedNameSpecifier.h Wed Mar 18 22:51:16 2009
@@ -17,6 +17,10 @@
 #include "llvm/Support/DataTypes.h"
 #include <cassert>
 
+namespace llvm {
+  class raw_ostream;
+}
+
 namespace clang {
 
 class ASTContext;
@@ -104,6 +108,9 @@
     NS.Data = reinterpret_cast<uintptr_t>(Ptr);
     return NS;
   }
+
+  static void Print(llvm::raw_ostream &OS, const NestedNameSpecifier *First,
+                    const NestedNameSpecifier *Last);
 };
 
 }

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Wed Mar 18 22:51:16 2009
@@ -414,7 +414,7 @@
   const TemplateTypeParmType *getAsTemplateTypeParmType() const;
 
   const ClassTemplateSpecializationType *
-    getClassTemplateSpecializationType() const;
+    getAsClassTemplateSpecializationType() const;
   
   /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC
   /// interface, return the interface type, otherwise return null.

Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Wed Mar 18 22:51:16 2009
@@ -24,6 +24,31 @@
   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
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/lib/AST/NestedNameSpecifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/NestedNameSpecifier.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/AST/NestedNameSpecifier.cpp (original)
+++ cfe/trunk/lib/AST/NestedNameSpecifier.cpp Wed Mar 18 22:51:16 2009
@@ -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 @@
   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 <<  "::";
+  }
+}

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Wed Mar 18 22:51:16 2009
@@ -530,28 +530,10 @@
   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();
 }
 

Modified: cfe/trunk/lib/AST/StmtSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtSerialization.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Wed Mar 18 22:51:16 2009
@@ -1589,7 +1589,8 @@
 
 void QualifiedDeclRefExpr::EmitImpl(llvm::Serializer& S) const {
   DeclRefExpr::EmitImpl(S);
-  S.Emit(NestedNameLoc);
+  S.Emit(QualifierRange);
+  // FIXME: Serialize nested-name-specifiers
 }
 
 QualifiedDeclRefExpr* 

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Wed Mar 18 22:51:16 2009
@@ -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 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 @@
 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;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=67283&r1=67282&r2=67283&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Mar 18 22:51:16 2009
@@ -439,11 +439,15 @@
 Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc,
                        bool TypeDependent, bool ValueDependent,
                        const CXXScopeSpec *SS) {
-  if (SS && !SS->isEmpty())
-    return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, 
-                                              ValueDependent,
-                                              SS->getRange().getBegin());
-  else
+  if (SS && !SS->isEmpty()) {
+    llvm::SmallVector<NestedNameSpecifier, 16> Specs;
+    for (CXXScopeSpec::iterator Spec = SS->begin(), SpecEnd = SS->end();
+         Spec != SpecEnd; ++Spec)
+      Specs.push_back(NestedNameSpecifier::getFromOpaquePtr(*Spec));
+    return QualifiedDeclRefExpr::Create(Context, D, Ty, Loc, TypeDependent, 
+                                        ValueDependent, SS->getRange(),
+                                        &Specs[0], Specs.size());
+  } else
     return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent);
 }
 
@@ -2226,10 +2230,12 @@
       Expr *NewFn = 0;
       if (QualifiedDeclRefExpr *QDRExpr
             = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr))
-        NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(),
-                                                   QDRExpr->getLocation(),
-                                                   false, false,
-                                          QDRExpr->getSourceRange().getBegin());
+        NewFn = QualifiedDeclRefExpr::Create(Context, FDecl, FDecl->getType(),
+                                             QDRExpr->getLocation(),
+                                             false, false,
+                                             QDRExpr->getQualifierRange(),
+                                             QDRExpr->begin(), 
+                                             QDRExpr->size());
       else
         NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(),
                                           Fn->getSourceRange().getBegin());

Added: cfe/trunk/test/SemaCXX/qualified-names-print.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/qualified-names-print.cpp?rev=67283&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/qualified-names-print.cpp (added)
+++ cfe/trunk/test/SemaCXX/qualified-names-print.cpp Wed Mar 18 22:51:16 2009
@@ -0,0 +1,15 @@
+// RUN: clang -ast-print %s 2>&1 | grep "N::M::X<INT>::value"
+namespace N {
+  namespace M {
+    template<typename T>
+    struct X {
+      enum { value };
+    };
+  }
+}
+
+typedef int INT;
+
+int test() {
+  return N::M::X<INT>::value;
+}

Propchange: cfe/trunk/test/SemaCXX/qualified-names-print.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/qualified-names-print.cpp

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/qualified-names-print.cpp

------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list