]> granicus.if.org Git - clang/commitdiff
remember the initializer for a variable in the AST and teach the
authorChris Lattner <sabre@nondot.org>
Thu, 12 Jul 2007 00:36:32 +0000 (00:36 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 12 Jul 2007 00:36:32 +0000 (00:36 +0000)
pretty printer to print it.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39770 91177308-0d34-0410-b5e6-96231b3b80d8

AST/StmtPrinter.cpp
Sema/SemaDecl.cpp
include/clang/AST/Decl.h

index e90b9f2c3f40120bd57289afa23e5a9451455401..08dec3c269262b039fc9eed2d44667a6cdf2804a 100644 (file)
@@ -111,7 +111,13 @@ void StmtPrinter::PrintRawDecl(Decl *D) {
     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");
index 9b98c4bce15c4245720a1b45424d2f3db79ea126..34bee52f6d45bf9d17cc44ef6032bcaffdf0c6b6 100644 (file)
@@ -272,9 +272,10 @@ Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
 }
 
 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 @@ Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
 
   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 @@ Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
       }
     }
   } 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 @@ Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
       NewVD = MergeVarDecl(NewVD, PrevDecl);
       if (NewVD == 0) return 0;
     }
+    
+    NewVD->setInit(Init);
     New = NewVD;
   }
   
index 2d3b01d3ced04915f2ff36e501da5d80c0bb5dfc..3411f1f9e3ee5ec3123b7f0188838c00e3338c6b 100644 (file)
@@ -147,6 +147,10 @@ public:
   };
   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 @@ protected:
     : ValueDecl(DK, L, Id, T, PrevDecl) { SClass = SC; }
 private:
   StorageClass SClass;
-  // TODO: Initializer.
+  Expr *Init;
 };
 
 /// BlockVarDecl - Represent a local variable declaration.