PCH support for record decls/types and their fields. Now that we can
handle the definition of __builtin_va_list on x86-64, eliminate the
forced -triple in PCH tests to get better coverage.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68988 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index f162689..03ef88a 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -779,6 +779,9 @@
   /// isMutable - Determines whether this field is mutable (C++ only).
   bool isMutable() const { return Mutable; }
 
+  /// \brief Set whether this field is mutable (C++ only).
+  void setMutable(bool M) { Mutable = M; }
+
   /// isBitfield - Determines whether this field is a bitfield.
   bool isBitField() const { return BitWidth != NULL; }
 
diff --git a/include/clang/Frontend/PCHBitCodes.h b/include/clang/Frontend/PCHBitCodes.h
index fe3d8ed..14c5971 100644
--- a/include/clang/Frontend/PCHBitCodes.h
+++ b/include/clang/Frontend/PCHBitCodes.h
@@ -316,8 +316,12 @@
       DECL_TYPEDEF,
       /// \brief An EnumDecl record.
       DECL_ENUM,
+      /// \brief A RecordDecl record.
+      DECL_RECORD,
       /// \brief An EnumConstantDecl record.
       DECL_ENUM_CONSTANT,
+      /// \brief A FieldDecl record.
+      DECL_FIELD,
       /// \brief A VarDecl record.
       DECL_VAR,
       /// \brief A record that stores the set of declarations that are
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index f735dd9..adb4e5f 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -50,8 +50,10 @@
     void VisitTypedefDecl(TypedefDecl *TD);
     void VisitTagDecl(TagDecl *TD);
     void VisitEnumDecl(EnumDecl *ED);
+    void VisitRecordDecl(RecordDecl *RD);
     void VisitValueDecl(ValueDecl *VD);
     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
+    void VisitFieldDecl(FieldDecl *FD);
     void VisitVarDecl(VarDecl *VD);
 
     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
@@ -106,6 +108,12 @@
   ED->setIntegerType(Reader.GetType(Record[Idx++]));
 }
 
+void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
+  VisitTagDecl(RD);
+  RD->setHasFlexibleArrayMember(Record[Idx++]);
+  RD->setAnonymousStructOrUnion(Record[Idx++]);
+}
+
 void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
   VisitNamedDecl(VD);
   VD->setType(Reader.GetType(Record[Idx++]));
@@ -113,10 +121,16 @@
 
 void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
   VisitValueDecl(ECD);
-  // FIXME: initialization expression
+  // FIXME: read the initialization expression
   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
 }
 
+void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
+  VisitValueDecl(FD);
+  FD->setMutable(Record[Idx++]);
+  // FIXME: Read the bit width.
+}
+
 void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
   VisitValueDecl(VD);
   VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
@@ -911,9 +925,8 @@
   }
     
   case pch::TYPE_RECORD:
-    // FIXME: Deserialize RecordType
-    assert(false && "Cannot de-serialize record types yet");
-    return QualType();
+    assert(Record.size() == 1 && "Incorrect encoding of record type");
+    return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
 
   case pch::TYPE_ENUM:
     assert(Record.size() == 1 && "Incorrect encoding of enum type");
@@ -989,6 +1002,15 @@
     break;
   }
 
+  case pch::DECL_RECORD: {
+    RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct, 
+                                            0, SourceLocation(), 0, 0);
+    LoadedDecl(Index, Record);
+    Reader.VisitRecordDecl(Record);
+    D = Record;
+    break;
+  }
+
   case pch::DECL_ENUM_CONSTANT: {
     EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0, 
                                                      SourceLocation(), 0,
@@ -1000,6 +1022,15 @@
     break;
   }
 
+  case pch::DECL_FIELD: {
+    FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0,
+                                         QualType(), 0, false);
+    LoadedDecl(Index, Field);
+    Reader.VisitFieldDecl(Field);
+    D = Field;
+    break;
+  }
+
   case pch::DECL_VAR: {
     VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
                                    VarDecl::None, SourceLocation());
@@ -1132,12 +1163,10 @@
   Decls.clear();
 
   unsigned Idx = 0;
-  //  llvm::SmallVector<uintptr_t, 16> DeclIDs;
   while (Idx < Record.size()) {
     Decls.push_back(VisibleDeclaration());
     Decls.back().Name = ReadDeclarationName(Record, Idx);
 
-    // FIXME: Don't actually read anything here!
     unsigned Size = Record[Idx++];
     llvm::SmallVector<unsigned, 4> & LoadedDecls
       = Decls.back().Declarations;
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index 2ba8e9e..6758a50 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -254,8 +254,10 @@
     void VisitTypedefDecl(TypedefDecl *D);
     void VisitTagDecl(TagDecl *D);
     void VisitEnumDecl(EnumDecl *D);
+    void VisitRecordDecl(RecordDecl *D);
     void VisitValueDecl(ValueDecl *D);
     void VisitEnumConstantDecl(EnumConstantDecl *D);
+    void VisitFieldDecl(FieldDecl *D);
     void VisitVarDecl(VarDecl *D);
     void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 
                           uint64_t VisibleOffset);
@@ -306,6 +308,13 @@
   Code = pch::DECL_ENUM;
 }
 
+void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
+  VisitTagDecl(D);
+  Record.push_back(D->hasFlexibleArrayMember());
+  Record.push_back(D->isAnonymousStructOrUnion());
+  Code = pch::DECL_RECORD;
+}
+
 void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
   VisitNamedDecl(D);
   Writer.AddTypeRef(D->getType(), Record);
@@ -318,6 +327,13 @@
   Code = pch::DECL_ENUM_CONSTANT;
 }
 
+void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
+  VisitValueDecl(D);
+  Record.push_back(D->isMutable());
+  // FIXME: Writer.AddExprRef(D->getBitWidth());
+  Code = pch::DECL_FIELD;
+}
+
 void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
   VisitValueDecl(D);
   Record.push_back(D->getStorageClass());
@@ -798,6 +814,9 @@
   uint64_t Offset = S.GetCurrentBitNo();
   RecordData Record;
   StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
+  if (!Map)
+    return 0;
+
   for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
        D != DEnd; ++D) {
     AddDeclarationName(D->first, Record);
diff --git a/test/PCH/enum.c b/test/PCH/enum.c
index 92869b6..f3e8a09 100644
--- a/test/PCH/enum.c
+++ b/test/PCH/enum.c
@@ -1,9 +1,9 @@
 // Test this without pch.
-// RUN: clang-cc -triple=i686-apple-darwin9 -include %S/enum.h -fsyntax-only -verify %s
+// RUN: clang-cc -include %S/enum.h -fsyntax-only -verify %s
 
 // Test with pch.
-// RUN: clang-cc -emit-pch -triple=i686-apple-darwin9 -o %t %S/enum.h &&
-// RUN: clang-cc -triple=i686-apple-darwin9 -include-pch %t -fsyntax-only -verify %s 
+// RUN: clang-cc -emit-pch -o %t %S/enum.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -verify %s 
 
 int i = Red;
 
diff --git a/test/PCH/line-directive.c b/test/PCH/line-directive.c
index 87aa4b0..ed54842 100644
--- a/test/PCH/line-directive.c
+++ b/test/PCH/line-directive.c
@@ -1,9 +1,9 @@
 // Test this without pch.
-// RUN: clang-cc -triple=i686-apple-darwin9 -include %S/line-directive.h -fsyntax-only %s 2>&1|grep "25:5"
+// RUN: clang-cc -include %S/line-directive.h -fsyntax-only %s 2>&1|grep "25:5"
 
 // Test with pch.
-// RUN: clang-cc -emit-pch -triple=i686-apple-darwin9 -o %t %S/line-directive.h &&
-// RUN: clang-cc -triple=i686-apple-darwin9 -include-pch %t -fsyntax-only %s 2>&1|grep "25:5"  
+// RUN: clang-cc -emit-pch -o %t %S/line-directive.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only %s 2>&1|grep "25:5"  
 
 double x; // expected-error{{redefinition of 'x' with a different type}}
 
diff --git a/test/PCH/struct.c b/test/PCH/struct.c
new file mode 100644
index 0000000..c81ec46
--- /dev/null
+++ b/test/PCH/struct.c
@@ -0,0 +1,24 @@
+// Test this without pch.
+// RUN: clang-cc -include %S/struct.h -fsyntax-only -verify %s
+
+// Test with pch.
+// RUN: clang-cc -emit-pch -o %t %S/struct.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -verify %s 
+
+struct Point *p1;
+
+float getX(struct Point *p1) {
+  return p1->x;
+}
+
+void *get_fun_ptr() {
+  return fun->is_ptr? fun->ptr : 0;
+}
+
+struct Fun2 {
+  int very_fun;
+};
+
+int get_very_fun() {
+  return fun2->very_fun;
+}
diff --git a/test/PCH/struct.h b/test/PCH/struct.h
new file mode 100644
index 0000000..e3d85ab
--- /dev/null
+++ b/test/PCH/struct.h
@@ -0,0 +1,25 @@
+// Used with the struct.c test
+
+struct Point {
+  float x, y, z;
+};
+
+struct Point2 {
+  float xValue, yValue, zValue;
+};
+
+struct Fun;
+
+struct Fun *fun;
+
+struct Fun {
+  int is_ptr;
+
+  union {
+    void *ptr;
+    int  *integer;
+  };
+};
+
+struct Fun2;
+struct Fun2 *fun2;
diff --git a/test/PCH/types.c b/test/PCH/types.c
index 764efe7..e62a4bb 100644
--- a/test/PCH/types.c
+++ b/test/PCH/types.c
@@ -1,9 +1,9 @@
 // Test this without pch.
-// RUN: clang-cc -triple=i686-apple-darwin9 -fblocks -include %S/types.h -fsyntax-only -verify %s
+// RUN: clang-cc -fblocks -include %S/types.h -fsyntax-only -verify %s
 
 // Test with pch.
-// RUN: clang-cc -emit-pch -triple=i686-apple-darwin9 -fblocks -o %t %S/types.h &&
-// RUN: clang-cc -triple=i686-apple-darwin9 -fblocks -include-pch %t -fsyntax-only -verify %s 
+// RUN: clang-cc -emit-pch -fblocks -o %t %S/types.h &&
+// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -verify %s 
 
 // FIXME: TYPE_EXT_QUAL
 // FIXME: TYPE_FIXED_WIDTH_INT
diff --git a/test/PCH/variables.c b/test/PCH/variables.c
index ec16576..4f42e50 100644
--- a/test/PCH/variables.c
+++ b/test/PCH/variables.c
@@ -1,9 +1,9 @@
 // Test this without pch.
-// RUN: clang-cc -triple=i686-apple-darwin9 -include %S/variables.h -fsyntax-only -verify %s
+// RUN: clang-cc -include %S/variables.h -fsyntax-only -verify %s
 
 // Test with pch.
-// RUN: clang-cc -emit-pch -triple=i686-apple-darwin9 -o %t %S/variables.h &&
-// RUN: clang-cc -triple=i686-apple-darwin9 -include-pch %t -fsyntax-only -verify %s 
+// RUN: clang-cc -emit-pch -o %t %S/variables.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -verify %s 
 
 int *ip2 = &x;
 float *fp = &ip; // expected-warning{{incompatible pointer types}}