]> granicus.if.org Git - clang/commitdiff
In CXXBaseOrMemberInitializer, don't confuse CtorTocall with
authorDouglas Gregor <dgregor@apple.com>
Tue, 1 Sep 2009 21:04:42 +0000 (21:04 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 1 Sep 2009 21:04:42 +0000 (21:04 +0000)
AnonUnionMember. Fixes PR4826.

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

include/clang/AST/DeclCXX.h
lib/AST/DeclCXX.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
test/CodeGenCXX/constructor-template.cpp [new file with mode: 0644]

index a6d32476a733fa0be23b4ab2cfc0b9fec0ca70d4..4fcf51f107133172da6ab75e48b1ee18662faff1 100644 (file)
@@ -819,28 +819,25 @@ class CXXBaseOrMemberInitializer {
   Stmt **Args;
   unsigned NumArgs;
   
-  union {
-    /// CtorToCall - For a base or member needing a constructor for their
-    /// initialization, this is the constructor to call.
-    CXXConstructorDecl *CtorToCall;
-  
-    /// AnonUnionMember - When 'BaseOrMember' is class's anonymous union
-    /// data member, this field holds the FieldDecl for the member of the
-    /// anonymous union being initialized.
-    /// @code
-    /// struct X {
-    ///   X() : au_i1(123) {}
-    ///   union {
-    ///     int au_i1;
-    ///     float au_f1;
-    ///   };
-    /// };
-    /// @endcode
-    /// In above example, BaseOrMember holds the field decl. for anonymous union
-    /// and AnonUnionMember holds field decl for au_i1.
-    ///
-    FieldDecl *AnonUnionMember;
-  };
+  /// \brief Stores either the constructor to call to initialize this base or
+  /// member (a CXXConstructorDecl pointer), or stores the anonymous union of
+  /// which the initialized value is a member.
+  ///
+  /// When the value is a FieldDecl pointer, 'BaseOrMember' is class's 
+  /// anonymous union data member, this field holds the FieldDecl for the 
+  /// member of the anonymous union being initialized.
+  /// @code
+  /// struct X {
+  ///   X() : au_i1(123) {}
+  ///   union {
+  ///     int au_i1;
+  ///     float au_f1;
+  ///   };
+  /// };
+  /// @endcode
+  /// In above example, BaseOrMember holds the field decl. for anonymous union
+  /// and AnonUnionMember holds field decl for au_i1.
+  llvm::PointerUnion<CXXConstructorDecl *, FieldDecl *> CtorOrAnonUnion;
   
   /// IdLoc - Location of the id in ctor-initializer list.
   SourceLocation IdLoc;
@@ -921,13 +918,15 @@ public:
   }
   
   FieldDecl *getAnonUnionMember() const {
-    return AnonUnionMember;
+    return CtorOrAnonUnion.dyn_cast<FieldDecl *>();
   }
   void setAnonUnionMember(FieldDecl *anonMember) {
-    AnonUnionMember = anonMember;
+    CtorOrAnonUnion = anonMember;
   }
   
-  const CXXConstructorDecl *getConstructor() const { return CtorToCall; }
+  const CXXConstructorDecl *getConstructor() const { 
+    return CtorOrAnonUnion.dyn_cast<CXXConstructorDecl *>();
+  }
   
   SourceLocation getSourceLocation() const { return IdLoc; }
   SourceLocation getRParenLoc() const { return RParenLoc; }
index 206193ba8125811c38a003b16fd82a4d6b402d67..adcd3f9aa92e826545ea6e84f52509bedd936cb5 100644 (file)
@@ -401,7 +401,7 @@ CXXBaseOrMemberInitializer::
 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
                            CXXConstructorDecl *C,
                            SourceLocation L, SourceLocation R) 
-  : Args(0), NumArgs(0), IdLoc(L), RParenLoc(R) {
+  : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
   BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
   assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
   BaseOrMember |= 0x01;
@@ -413,14 +413,14 @@ CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
     for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
       this->Args[Idx] = Args[Idx];
   }
-  CtorToCall = C;
+  CtorOrAnonUnion = C;
 }
 
 CXXBaseOrMemberInitializer::
 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
                            CXXConstructorDecl *C,
                            SourceLocation L, SourceLocation R)
-  : Args(0), NumArgs(0), IdLoc(L), RParenLoc(R) {
+  : Args(0), NumArgs(0), CtorOrAnonUnion(), IdLoc(L), RParenLoc(R) {
   BaseOrMember = reinterpret_cast<uintptr_t>(Member);
   assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");  
 
@@ -430,7 +430,7 @@ CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
     for (unsigned Idx = 0; Idx < NumArgs; ++Idx)
       this->Args[Idx] = Args[Idx];
   }
-  CtorToCall = C;
+  CtorOrAnonUnion = C;
 }
 
 CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() {
index 0179160835ce990ffb03aca0d270b28884cc1fab..285cc620121786e0e1d5229d31bdd7e69bf4b5e1 100644 (file)
@@ -1123,7 +1123,8 @@ Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
 
   // Instantiate all the initializers.
   for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
-       InitsEnd = Tmpl->init_end(); Inits != InitsEnd; ++Inits) {
+                                            InitsEnd = Tmpl->init_end();
+       Inits != InitsEnd; ++Inits) {
     CXXBaseOrMemberInitializer *Init = *Inits;
 
     ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
diff --git a/test/CodeGenCXX/constructor-template.cpp b/test/CodeGenCXX/constructor-template.cpp
new file mode 100644 (file)
index 0000000..10577e9
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: clang-cc %s -emit-llvm -o -
+
+// PR4826
+struct A {
+  A() {
+  }
+};
+
+template<typename T>
+struct B {
+  B(T) {}
+  
+  A nodes;
+};
+
+int main() {
+  B<int> *n = new B<int>(4);
+}