From: Chris Lattner Date: Thu, 12 Jul 2007 00:36:32 +0000 (+0000) Subject: remember the initializer for a variable in the AST and teach the X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=24c3990cd2469f9692c4b0c3edea4fba67baaf6e;p=clang remember the initializer for a variable in the AST and teach the pretty printer to print it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39770 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/StmtPrinter.cpp b/AST/StmtPrinter.cpp index e90b9f2c3f..08dec3c269 100644 --- a/AST/StmtPrinter.cpp +++ b/AST/StmtPrinter.cpp @@ -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(VD)) { + if (V->getInit()) { + OS << " = "; + PrintExpr(V->getInit()); + } + } } else { // FIXME: "struct x;" assert(0 && "Unexpected decl"); diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 9b98c4bce1..34bee52f6d 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -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(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; } diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 2d3b01d3ce..3411f1f9e3 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -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.