Fix rs_matrix destructor issue.

Bug: 7949301

This change fixes the destructor issue for rs_matrix types. We need to skip
creating a destructor if there are no reference-counted RS object types in
the struct. We still need to zero-init all RS object types (ref-counted or
not, as is the case for rs_matrix*). I also fix another issue where a missing
struct definition could cause an early parser crash (i.e. before the standard
clang errors kick in and notice that you are using an undefined struct).

Change-Id: If2009d96f35a8cb693431aaeae3cb4b5642695fa
diff --git a/slang_rs_export_type.cpp b/slang_rs_export_type.cpp
index ae13bf6..ba37860 100644
--- a/slang_rs_export_type.cpp
+++ b/slang_rs_export_type.cpp
@@ -907,8 +907,15 @@
   if (!RT) {
     return false;
   }
+
   const clang::RecordDecl *RD = RT->getDecl();
-  RD = RD->getDefinition();
+  if (RD) {
+    RD = RD->getDefinition();
+  }
+  if (!RD) {
+    return false;
+  }
+
   for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
          FE = RD->field_end();
        FI != FE;
diff --git a/slang_rs_object_ref_count.cpp b/slang_rs_object_ref_count.cpp
index f32eeb2..246ae83 100644
--- a/slang_rs_object_ref_count.cpp
+++ b/slang_rs_object_ref_count.cpp
@@ -680,6 +680,7 @@
               RSExportPrimitiveType::DataTypeUnknown);
 
   unsigned FieldsToDestroy = CountRSObjectTypes(C, BaseType, Loc);
+  slangAssert(FieldsToDestroy != 0);
 
   unsigned StmtCount = 0;
   clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy];
@@ -1092,7 +1093,7 @@
     }
   }
 
-  slangAssert(StmtCount > 0 && StmtCount < FieldsToSet);
+  slangAssert(StmtCount < FieldsToSet);
 
   // We still need to actually do the overall struct copy. For simplicity,
   // we just do a straight-up assignment (which will still preserve all
@@ -1467,8 +1468,14 @@
           RSExportPrimitiveType::DataTypeUnknown;
       clang::Expr *InitExpr = NULL;
       if (InitializeRSObject(VD, &DT, &InitExpr)) {
-        getCurrentScope()->addRSObject(VD);
+        // We need to zero-init all RS object types (including matrices), ...
         getCurrentScope()->AppendRSObjectInit(VD, DS, DT, InitExpr);
+        // ... but, only add to the list of RS objects if we have some
+        // non-matrix RS object fields.
+        if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(),
+                               VD->getLocation())) {
+          getCurrentScope()->addRSObject(VD);
+        }
       }
     }
   }
diff --git a/tests/P_refcount/refcount.rs b/tests/P_refcount/refcount.rs
index 361823b..3046dc4 100644
--- a/tests/P_refcount/refcount.rs
+++ b/tests/P_refcount/refcount.rs
@@ -4,6 +4,10 @@
 rs_font globalAlloc;
 rs_font globalAlloc2;
 
+struct hasMatrix {
+    rs_matrix3x3 m;
+} ghm;
+
 static void foo() {
 
     rs_font fontUninit;
@@ -17,6 +21,8 @@
 }
 
 void singleStmt() {
+    struct hasMatrix h = ghm;
+    ghm = h;
     globalAlloc = globalAlloc2;
 }
 
diff --git a/tests/P_refcount/stdout.txt.expect b/tests/P_refcount/stdout.txt.expect
index 7e2f82c..cfd6791 100644
--- a/tests/P_refcount/stdout.txt.expect
+++ b/tests/P_refcount/stdout.txt.expect
@@ -1 +1,2 @@
 Generating ScriptC_refcount.java ...
+Generating ScriptField_hasMatrix.java ...