[cfe-commits] r39770 - in /cfe/trunk: AST/StmtPrinter.cpp Sema/SemaDecl.cpp include/clang/AST/Decl.h

Chris Lattner sabre at nondot.org
Wed Jul 11 17:36:36 PDT 2007


Author: lattner
Date: Wed Jul 11 19:36:32 2007
New Revision: 39770

URL: http://llvm.org/viewvc/llvm-project?rev=39770&view=rev
Log:
remember the initializer for a variable in the AST and teach the
pretty printer to print it.

Modified:
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/AST/Decl.h

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

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Wed Jul 11 19:36:32 2007
@@ -111,7 +111,13 @@
     VD->getType().getAsStringInternal(Name);
     OS << Name;
     
-    // FIXME: Initializer for vardecl
+    // If this is a vardecl with an initializer, emit it.
+    if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
+      if (V->getInit()) {
+        OS << " = ";
+        PrintExpr(V->getInit());
+      }
+    }
   } else {
     // FIXME: "struct x;"
     assert(0 && "Unexpected decl");

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=39770&r1=39769&r2=39770&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Wed Jul 11 19:36:32 2007
@@ -272,9 +272,10 @@
 }
 
 Sema::DeclTy *
-Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, 
+Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *init,
                       DeclTy *lastDeclarator) {
   Decl *LastDeclarator = (Decl*)lastDeclarator;
+  Expr *Init = static_cast<Expr*>(init);
   IdentifierInfo *II = D.getIdentifier();
   
   // See if this is a redefinition of a variable in the same scope.
@@ -285,6 +286,7 @@
 
   Decl *New;
   if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
+    assert(Init == 0 && "Can't have initializer for a typedef!");
     TypedefDecl *NewTD = ParseTypedefDecl(S, D, LastDeclarator);
     if (!NewTD) return 0;
 
@@ -306,6 +308,7 @@
       }
     }
   } else if (D.isFunctionDeclarator()) {
+    assert(Init == 0 && "Can't have an initializer for a functiondecl!");
     QualType R = GetTypeForDeclarator(D, S);
     if (R.isNull()) return 0; // FIXME: "auto func();" passes through...
     
@@ -404,6 +407,8 @@
       NewVD = MergeVarDecl(NewVD, PrevDecl);
       if (NewVD == 0) return 0;
     }
+    
+    NewVD->setInit(Init);
     New = NewVD;
   }
   

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Wed Jul 11 19:36:32 2007
@@ -147,6 +147,10 @@
   };
   StorageClass getStorageClass() const { return SClass; }
 
+  const Expr *getInit() const { return Init; }
+  Expr *getInit() { return Init; }
+  void setInit(Expr *I) { Init = I; }
+  
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { 
     return D->getKind() >= BlockVariable && D->getKind() <= ParmVariable; 
@@ -158,7 +162,7 @@
     : ValueDecl(DK, L, Id, T, PrevDecl) { SClass = SC; }
 private:
   StorageClass SClass;
-  // TODO: Initializer.
+  Expr *Init;
 };
 
 /// BlockVarDecl - Represent a local variable declaration.





More information about the cfe-commits mailing list