]> granicus.if.org Git - clang/commitdiff
Sema/AST work for capturing copy init expression
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 1 Dec 2010 22:29:46 +0000 (22:29 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 1 Dec 2010 22:29:46 +0000 (22:29 +0000)
to be used in copy helper synthesis of __block
variables. wip.

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

include/clang/AST/ASTContext.h
lib/AST/ASTContext.cpp
lib/Sema/SemaDecl.cpp

index 848f76bfa794509602a6d206bdb6eff44b74a1c9..87a210f6619556dc682c7270230daed3258eb6b8 100644 (file)
@@ -129,6 +129,9 @@ class ASTContext {
   /// \brief Mapping from ObjCContainers to their ObjCImplementations.
   llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls;
 
+  /// \brief Mapping from __block VarDecls to their copy initialization expr.
+  llvm::DenseMap<VarDecl*, Expr*> BlockVarCopyInits;
+    
   /// \brief Representation of a "canonical" template template parameter that
   /// is used in canonical template names.
   class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode {
@@ -1358,6 +1361,12 @@ public:
   /// \brief Set the implementation of ObjCCategoryDecl.
   void setObjCImplementation(ObjCCategoryDecl *CatD,
                              ObjCCategoryImplDecl *ImplD);
+  
+  /// \brief Set the copy inialization expression of a block var decl.
+  void setBlockVarCopyInits(VarDecl*VD, Expr* Init);
+  /// \brief Get the copy initialization expression of VarDecl,or NULL if 
+  /// none exists.
+  Expr *getBlockVarCopyInits(VarDecl*VD);
 
   /// \brief Allocate an uninitialized TypeSourceInfo.
   ///
index e5081e384f7b81a0c6c3e3c1064b3089d6bbd46b..e1797665c93973f76ddac84aceccb112f3a90e51 100644 (file)
@@ -1008,6 +1008,20 @@ void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
   ObjCImpls[CatD] = ImplD;
 }
 
+/// \brief Get the copy initialization expression of VarDecl,or NULL if 
+/// none exists.
+Expr *ASTContext::getBlockVarCopyInits(VarDecl*VD) {
+  llvm::DenseMap<VarDecl*, Expr*>::iterator
+  I = BlockVarCopyInits.find(VD);
+  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
+}
+
+/// \brief Set the copy inialization expression of a block var decl.
+void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
+  assert(VD && Init && "Passed null params");
+  BlockVarCopyInits[VD] = Init;
+}
+
 /// \brief Allocate an uninitialized TypeSourceInfo.
 ///
 /// The caller should initialize the memory held by TypeSourceInfo using
index 786a13f1d345eca777fbb7e422640f224ca91266..7b0afe1963a93487f86de118aa1c13b82b49778a 100644 (file)
@@ -3022,6 +3022,27 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
   if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
     AddPushedVisibilityAttribute(NewVD);
 
+
+  // For variables declared as __block which require copy construction,
+  // must capture copy initialization expression here.
+  if (getLangOptions().CPlusPlus && NewVD->hasAttr<BlocksAttr>()) {
+    QualType T = NewVD->getType();
+    if (!T->isDependentType() && !T->isReferenceType() &&
+        T->getAs<RecordType>() && !T->isUnionType()) {
+      Expr *E = new (Context) DeclRefExpr(NewVD, T,
+                                          VK_LValue, SourceLocation());
+      ExprResult Res = PerformCopyInitialization(
+                          InitializedEntity::InitializeBlock(NewVD->getLocation(), 
+                                                      T, false),
+                                                      SourceLocation(),
+                                                      Owned(E));
+      if (!Res.isInvalid()) {
+        Res = MaybeCreateCXXExprWithTemporaries(Res.get());
+        Expr *Init = Res.takeAs<Expr>();
+        Context.setBlockVarCopyInits(NewVD, Init);
+      }
+    }
+  }
   MarkUnusedFileScopedDecl(NewVD);
 
   return NewVD;