diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-08-26 21:14:46 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-08-26 21:14:46 +0000 |
commit | 402250f2f9f607f0fc66d16021234f5b63021b58 (patch) | |
tree | 6493e732c3720e976fe19fdb5d2e6362f2c0fe6f /clang/lib | |
parent | 849416a388dc7a912f107f37b8639d452b59c038 (diff) | |
download | bcm5719-llvm-402250f2f9f607f0fc66d16021234f5b63021b58.tar.gz bcm5719-llvm-402250f2f9f607f0fc66d16021234f5b63021b58.zip |
Implement support for C++ direct initializers that involve dependent
types or type-dependent expressions.
llvm-svn: 80143
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 22 | ||||
-rw-r--r-- | clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 27 |
2 files changed, 45 insertions, 4 deletions
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 0dbefe1dd09..41a7fe653d8 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -2523,9 +2523,7 @@ void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, return; } - // FIXME: Need to handle dependent types and expressions here. - - // We will treat direct-initialization as a copy-initialization: + // We will represent direct-initialization similarly to copy-initialization: // int x(1); -as-> int x = 1; // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); // @@ -2535,6 +2533,24 @@ void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, // exactly form was it (like the CodeGen) can handle both cases without // special case code. + // If either the declaration has a dependent type or if any of the expressions + // is type-dependent, we represent the initialization via a ParenListExpr for + // later use during template instantiation. + if (VDecl->getType()->isDependentType() || + Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { + // Let clients know that initialization was done with a direct initializer. + VDecl->setCXXDirectInitializer(true); + + // Store the initialization expressions as a ParenListExpr. + unsigned NumExprs = Exprs.size(); + VDecl->setInit(Context, + new (Context) ParenListExpr(Context, LParenLoc, + (Expr **)Exprs.release(), + NumExprs, RParenLoc)); + return; + } + + // C++ 8.5p11: // The form of initialization (using parentheses or '=') is generally // insignificant, but does matter when the entity being initialized has a diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index bd25c8fb157..71fb36892b6 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -15,6 +15,7 @@ #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclVisitor.h" #include "clang/AST/Expr.h" +#include "clang/Lex/Preprocessor.h" #include "llvm/Support/Compiler.h" using namespace clang; @@ -153,7 +154,31 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { = SemaRef.SubstExpr(D->getInit(), TemplateArgs); if (Init.isInvalid()) Var->setInvalidDecl(); - else + else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { + // FIXME: We're faking all of the comma locations, which is suboptimal. + // Do we even need these comma locations? + llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; + if (PLE->getNumExprs() > 0) { + FakeCommaLocs.reserve(PLE->getNumExprs() - 1); + for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { + Expr *E = PLE->getExpr(I)->Retain(); + FakeCommaLocs.push_back( + SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); + } + } + + // Add the direct initializer to the declaration. + SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), + PLE->getLParenLoc(), + Sema::MultiExprArg(SemaRef, + (void**)PLE->getExprs(), + PLE->getNumExprs()), + FakeCommaLocs.data(), + PLE->getRParenLoc()); + + // When Init is destroyed, it will destroy the instantiated ParenListExpr; + // we've explicitly retained all of its subexpressions already. + } else SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), D->hasCXXDirectInitializer()); } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) |