Change struct forward declarations and definitions to use unique RecordDecls, as opposed to creating a single RecordDecl and reusing it.

This change effects both RecordDecls and CXXRecordDecls, but does not effect EnumDecls (yet).

The motivation of this patch is as follows:
- Capture more source information, necessary for refactoring/rewriting clients.

- Pave the way to resolve ownership issues with RecordDecls with the forthcoming
  addition of DeclGroups.

Current caveats:
- Until DeclGroups are in place, we will leak RecordDecls not explicitly
  referenced by the AST.  For example:

    typedef struct { ... } x;  

  The RecordDecl for the struct will be leaked because the TypedefDecl doesn't
  refer to it.  This will be solved with DeclGroups.
  
- This patch also (temporarily) breaks CodeGen.  More below.

High-level changes:
- As before, TagType still refers to a TagDecl, but it doesn't own it.  When
  a struct/union/class is first referenced, a RecordType and RecordDecl are
  created for it, and the RecordType refers to that RecordDecl.  Later, if
  a new RecordDecl is created, the pointer to a RecordDecl in RecordType is
  updated to point to the RecordDecl that defines the struct/union/class.

- TagDecl and RecordDecl now how a method 'getDefinition()' to return the
  TagDecl*/RecordDecl* that refers to the TagDecl* that defines a particular
  enum/struct/class/union. This is useful from going from a RecordDecl* that
  defines a forward declaration to the RecordDecl* that provides the actual
  definition. Note that this also works for EnumDecls, except that in this case
  there is no distinction between forward declarations and definitions (yet).

- Clients should no longer assume that 'isDefinition()' returns true from a
  RecordDecl if the corresponding struct/union/class has been defined.
  isDefinition() only returns true if a particular RecordDecl is the defining
  Decl. Use 'getDefinition()' instead to determine if a struct has been defined.

- The main changes to Sema happen in ActOnTag. To make the changes more
  incremental, I split off the processing of enums and structs et al into two
  code paths. Enums use the original code path (which is in ActOnTag) and
  structs use the ActOnTagStruct. Eventually the two code paths will be merged,
  but the idea was to preserve the original logic both for comparison and not to
  change the logic for both enums and structs all at once.

- There is NO CHAINING of RecordDecls for the same RecordType. All RecordDecls
  that correspond to the same type simply have a pointer to that type. If we
  need to figure out what are all the RecordDecls for a given type we can build
  a backmap.

- The diff in CXXRecordDecl.[cpp,h] is actually very small; it just mimics the
  changes to RecordDecl. For some reason 'svn' marks the entire file as changed.

Why is CodeGen broken:
- Codegen assumes that there is an equivalence between RecordDecl* and
  RecordType*. This was true before because we only created one RecordDecl* for
  a given RecordType*, but it is no longer true. I believe this shouldn't be too
  hard to change, but the patch was big enough as it is.
  
I have tested this patch on both the clang test suite, and by running the static analyzer over Postgresql and a large Apple-internal project (mix of Objective-C and C).  


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55839 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index 0cce7db..0a65ab3 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -1,65 +1,68 @@
-//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//

-//

-//                     The LLVM Compiler Infrastructure

-//

-// This file is distributed under the University of Illinois Open Source

-// License. See LICENSE.TXT for details.

-//

-//===----------------------------------------------------------------------===//

-//

-// This file implements the C++ related Decl classes.

-//

-//===----------------------------------------------------------------------===//

-

-#include "clang/AST/DeclCXX.h"

-#include "clang/AST/ASTContext.h"

-using namespace clang;

-

-//===----------------------------------------------------------------------===//

-// Decl Allocation/Deallocation Method Implementations

-//===----------------------------------------------------------------------===//

- 

-CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,

-                                   SourceLocation L, IdentifierInfo *Id,

-                                   QualType T, Expr *BW) {

-  void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();

-  return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);

-}

-

-CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,

-                                     SourceLocation L, IdentifierInfo *Id) {

-  Kind DK;

-  switch (TK) {

-    default: assert(0 && "Invalid TagKind!");

-    case TK_enum:   assert(0 && "Enum TagKind passed for Record!");

-    case TK_struct: DK = CXXStruct; break;

-    case TK_union:  DK = CXXUnion;  break;

-    case TK_class:  DK = CXXClass;  break;

-  }

-  void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();

-  return new (Mem) CXXRecordDecl(DK, DC, L, Id);

-}

-

-CXXMethodDecl *

-CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,

-                      SourceLocation L, IdentifierInfo *Id,

-                      QualType T, bool isStatic, bool isInline,

-                      ScopedDecl *PrevDecl) {

-  void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();

-  return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);

-}

-

-QualType CXXMethodDecl::getThisType(ASTContext &C) const {

-  assert(isInstance() && "No 'this' for static methods!");

-  QualType ClassTy = C.getTagDeclType(cast<CXXRecordDecl>(getParent()));

-  QualType ThisTy = C.getPointerType(ClassTy);

-  ThisTy.addConst();

-  return ThisTy;

-}

-

-CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,

-                                   SourceLocation L, IdentifierInfo *Id,

-                                   QualType T, ScopedDecl *PrevDecl) {

-  void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();

-  return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);

-}

+//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the C++ related Decl classes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/ASTContext.h"
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Decl Allocation/Deallocation Method Implementations
+//===----------------------------------------------------------------------===//
+ 
+CXXFieldDecl *CXXFieldDecl::Create(ASTContext &C, CXXRecordDecl *RD,
+                                   SourceLocation L, IdentifierInfo *Id,
+                                   QualType T, Expr *BW) {
+  void *Mem = C.getAllocator().Allocate<CXXFieldDecl>();
+  return new (Mem) CXXFieldDecl(RD, L, Id, T, BW);
+}
+
+CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
+                                     SourceLocation L, IdentifierInfo *Id,
+                                     CXXRecordDecl* PrevDecl) {
+  Kind DK;
+  switch (TK) {
+    default: assert(0 && "Invalid TagKind!");
+    case TK_enum:   assert(0 && "Enum TagKind passed for Record!");
+    case TK_struct: DK = CXXStruct; break;
+    case TK_union:  DK = CXXUnion;  break;
+    case TK_class:  DK = CXXClass;  break;
+  }
+  void *Mem = C.getAllocator().Allocate<CXXRecordDecl>();
+  CXXRecordDecl* R = new (Mem) CXXRecordDecl(DK, DC, L, Id);
+  C.getTypeDeclType(R, PrevDecl);  
+  return R;
+}
+
+CXXMethodDecl *
+CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
+                      SourceLocation L, IdentifierInfo *Id,
+                      QualType T, bool isStatic, bool isInline,
+                      ScopedDecl *PrevDecl) {
+  void *Mem = C.getAllocator().Allocate<CXXMethodDecl>();
+  return new (Mem) CXXMethodDecl(RD, L, Id, T, isStatic, isInline, PrevDecl);
+}
+
+QualType CXXMethodDecl::getThisType(ASTContext &C) const {
+  assert(isInstance() && "No 'this' for static methods!");
+  QualType ClassTy = C.getTagDeclType(cast<CXXRecordDecl>(getParent()));
+  QualType ThisTy = C.getPointerType(ClassTy);
+  ThisTy.addConst();
+  return ThisTy;
+}
+
+CXXClassVarDecl *CXXClassVarDecl::Create(ASTContext &C, CXXRecordDecl *RD,
+                                   SourceLocation L, IdentifierInfo *Id,
+                                   QualType T, ScopedDecl *PrevDecl) {
+  void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
+  return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
+}