Create RSExportMatrixType which is a subclass of RSExportType
to support rs_matrix{2x2,3x3,4x4} explicitly.
diff --git a/Android.mk b/Android.mk
index 1eb70ec..cb73452 100644
--- a/Android.mk
+++ b/Android.mk
@@ -16,13 +16,13 @@
 LOCAL_MODULE_CLASS := EXECUTABLES
 
 LOCAL_SRC_FILES :=	\
-        llvm-rs-link.cpp
+	llvm-rs-link.cpp
 
 LOCAL_STATIC_LIBRARIES :=	\
-        libLLVMLinker   \
+	libLLVMLinker   \
 	libLLVMipo	\
 	libLLVMBitWriter	\
-        libLLVMBitReader        \
+	libLLVMBitReader        \
 	libLLVMScalarOpts	\
 	libLLVMInstCombine	\
 	libLLVMTransformUtils	\
@@ -56,7 +56,7 @@
 	DeclNodes.inc    \
 	DiagnosticCommonKinds.inc   \
 	DiagnosticFrontendKinds.inc \
-	StmtNodes.inc               \
+	StmtNodes.inc	\
 	DiagnosticSemaKinds.inc
 
 LOCAL_SRC_FILES :=	\
@@ -97,7 +97,7 @@
 	libLLVMTarget	\
 	libLLVMMC	\
 	libLLVMCore	\
-        libclangParse   \
+	libclangParse   \
 	libclangSema	\
 	libclangAnalysis	\
 	libclangAST	\
diff --git a/slang_rs_context.h b/slang_rs_context.h
index e960099..7e95d56 100644
--- a/slang_rs_context.h
+++ b/slang_rs_context.h
@@ -31,7 +31,6 @@
 class RSExportVar;
 class RSExportFunc;
 class RSExportType;
-class RSPragmaHandler;
 
 class RSContext {
   typedef llvm::StringSet<> NeedExportVarSet;
diff --git a/slang_rs_export_element.cpp b/slang_rs_export_element.cpp
index 1ecacce..e646212 100644
--- a/slang_rs_export_element.cpp
+++ b/slang_rs_export_element.cpp
@@ -96,37 +96,6 @@
       ET = EVT;
       break;
     }
-    case clang::Type::Record: {
-      // Must be RS object type
-
-      if (TypeName.equals(llvm::StringRef("rs_matrix2x2")) ||
-          TypeName.equals(llvm::StringRef("rs_matrix3x3")) ||
-          TypeName.equals(llvm::StringRef("rs_matrix4x4"))) {
-        const clang::RecordType *RT = static_cast<const clang::RecordType*> (T);
-        const clang::RecordDecl *RD = RT->getDecl();
-        RD = RD->getDefinition();
-        clang::RecordDecl::field_iterator fit = RD->field_begin();
-        clang::FieldDecl *FD = *fit;
-        const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
-        RSExportConstantArrayType *ECT =
-            RSExportConstantArrayType::Create(
-                Context,
-                static_cast<const clang::ConstantArrayType*> (FT),
-                TypeName);
-        ET = ECT;
-      } else {
-        RSExportPrimitiveType* EPT =
-            RSExportPrimitiveType::Create(Context,
-                                          T,
-                                          TypeName,
-                                          EI->kind,
-                                          EI->normalized);
-        // Verify
-        assert(EI->type == EPT->getType() && "Element has unexpected type");
-        ET = EPT;
-      }
-      break;
-    }
     default: {
       // TODO(zonr): warn that type is not exportable
       fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n",
@@ -147,8 +116,7 @@
   // Note: RS element like rs_pixel_rgb elements are either in the type of
   // primitive or vector.
   if ((CT->getTypeClass() != clang::Type::Builtin) &&
-      (CT->getTypeClass() != clang::Type::ExtVector) &&
-      (CT->getTypeClass() != clang::Type::Record)) {
+      (CT->getTypeClass() != clang::Type::ExtVector)) {
     return RSExportType::Create(Context, T);
   }
 
diff --git a/slang_rs_export_element_support.inc b/slang_rs_export_element_support.inc
index f148cd0..618ed96 100644
--- a/slang_rs_export_element_support.inc
+++ b/slang_rs_export_element_support.inc
@@ -137,9 +137,6 @@
 #   undef ELEMENT_DATA_TYPE_RS_PROGRAM_RASTER
 #   undef ELEMENT_DATA_TYPE_RS_PROGRAM_STORE
 #   undef ELEMENT_DATA_TYPE_RS_FONT
-#   undef ELEMENT_DATA_TYPE_RS_MATRIX_2X2
-#   undef ELEMENT_DATA_TYPE_RS_MATRIX_3X3
-#   undef ELEMENT_DATA_TYPE_RS_MATRIX_4X4
 
 #   undef USE_ELEMENT_DATA_TYPE
 
diff --git a/slang_rs_export_type.cpp b/slang_rs_export_type.cpp
index 6fa035c..9e7fe9e 100644
--- a/slang_rs_export_type.cpp
+++ b/slang_rs_export_type.cpp
@@ -275,29 +275,39 @@
       RSExportPrimitiveType::DataType dt =
           RSExportPrimitiveType::GetRSObjectType(TypeName);
       switch (dt) {
-        case RSExportPrimitiveType::DataTypeUnknown: {  // User-defined types
+        case RSExportPrimitiveType::DataTypeUnknown: {
+          // User-defined types
           ET = RSExportRecordType::Create(Context,
                                           T->getAsStructureType(),
                                           TypeName);
           break;
         }
-        case RSExportPrimitiveType::DataTypeRSMatrix2x2:
-        case RSExportPrimitiveType::DataTypeRSMatrix3x3:
+        case RSExportPrimitiveType::DataTypeRSMatrix2x2: {
+          // 2 x 2 Matrix type
+          ET = RSExportMatrixType::Create(Context,
+                                          T->getAsStructureType(),
+                                          TypeName,
+                                          2);
+          break;
+        }
+        case RSExportPrimitiveType::DataTypeRSMatrix3x3: {
+          // 3 x 3 Matrix type
+          ET = RSExportMatrixType::Create(Context,
+                                          T->getAsStructureType(),
+                                          TypeName,
+                                          3);
+          break;
+        }
         case RSExportPrimitiveType::DataTypeRSMatrix4x4: {
-          const clang::RecordType *RT =
-              static_cast<const clang::RecordType*> (T);
-          const clang::RecordDecl *RD = RT->getDecl();
-          RD = RD->getDefinition();
-          clang::RecordDecl::field_iterator FI = RD->field_begin();
-          clang::FieldDecl *FD = *FI;
-          const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
-          ET = RSExportConstantArrayType::Create(
-              Context,
-              static_cast<const clang::ConstantArrayType*> (FT),
-              TypeName);
+          // 4 x 4 Matrix type
+          ET = RSExportMatrixType::Create(Context,
+                                          T->getAsStructureType(),
+                                          TypeName,
+                                          4);
           break;
         }
         default: {
+          // Others are primitive types
           ET = RSExportPrimitiveType::Create(Context, T, TypeName);
           break;
         }
@@ -312,7 +322,7 @@
       ET = RSExportPointerType::Create(Context,
                                        UNSAFE_CAST_TYPE(clang::PointerType, T),
                                        TypeName);
-      // Free the name (allocated in RSExportType::GetTypeName)
+      // FIXME: free the name (allocated in RSExportType::GetTypeName)
       delete [] TypeName.data();
       break;
     }
@@ -808,7 +818,7 @@
                                                const llvm::StringRef &TypeName,
                                                DataKind DK,
                                                bool Normalized) {
-  assert(EVT != NULL && EVT->getTypeClass() == Type::ExtVector);
+  assert(EVT != NULL && EVT->getTypeClass() == clang::Type::ExtVector);
 
   const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
   RSExportPrimitiveType::DataType DT =
@@ -836,12 +846,88 @@
   return llvm::VectorType::get(ElementType, getNumElement());
 }
 
+/***************************** RSExportMatrixType *****************************/
+RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
+                                               const clang::RecordType *RT,
+                                               const llvm::StringRef &TypeName,
+                                               unsigned Dim) {
+  assert((RT != NULL) && (RT->getTypeClass() == clang::Type::Record));
+  assert((Dim > 1) && "Invalid dimension of matrix");
+
+  // Check whether the struct rs_matrix is in our expected form (but assume it's
+  // correct if we're not sure whether it's correct or not)
+  const clang::RecordDecl* RD = RT->getDecl();
+  RD = RD->getDefinition();
+  if (RD != NULL) {
+    // Find definition, perform further examination
+    if (RD->field_empty()) {
+      fprintf(stderr, "RSExportMatrixType::Create : invalid %s struct: "
+                      "must have 1 field for saving values", TypeName.data());
+      return NULL;
+    }
+
+    clang::RecordDecl::field_iterator FIT = RD->field_begin();
+    const clang::FieldDecl *FD = *FIT;
+    const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
+    if ((FT == NULL) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
+      fprintf(stderr, "RSExportMatrixType::Create : invalid %s struct: "
+                      "first field should be an array with constant size",
+              TypeName.data());
+      return NULL;
+    }
+    const clang::ConstantArrayType *CAT =
+      static_cast<const clang::ConstantArrayType *>(FT);
+    const clang::Type *ElementType =
+      GET_CANONICAL_TYPE(CAT->getElementType().getTypePtr());
+    if ((ElementType == NULL) ||
+        (ElementType->getTypeClass() != clang::Type::Builtin) ||
+        (static_cast<const clang::BuiltinType *>(ElementType)->getKind()
+          != clang::BuiltinType::Float)) {
+      fprintf(stderr, "RSExportMatrixType::Create : invalid %s struct: "
+                      "first field should be a float array", TypeName.data());
+      return NULL;
+    }
+
+    if (CAT->getSize() != Dim * Dim) {
+      fprintf(stderr, "RSExportMatrixType::Create : invalid %s struct: "
+                      "first field should be an array with size %d",
+              TypeName.data(), Dim * Dim);
+      return NULL;
+    }
+
+    FIT++;
+    if (FIT != RD->field_end()) {
+      fprintf(stderr, "RSExportMatrixType::Create : invalid %s struct: "
+                      "must have exactly 1 field", TypeName.data());
+      return NULL;
+    }
+  }
+
+  return new RSExportMatrixType(Context, TypeName, Dim);
+}
+
+RSExportType::ExportClass RSExportMatrixType::getClass() const {
+  return RSExportType::ExportClassMatrix;
+}
+
+const llvm::Type *RSExportMatrixType::convertToLLVMType() const {
+  // Construct LLVM type:
+  // struct {
+  //  float X[mDim * mDim];
+  // }
+
+  llvm::LLVMContext &C = getRSContext()->getLLVMContext();
+  llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
+                                            mDim * mDim);
+  return llvm::StructType::get(C, X, NULL);
+}
+
 /**************************** RSExportRecordType ****************************/
 RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
                                                const clang::RecordType *RT,
                                                const llvm::StringRef &TypeName,
                                                bool mIsArtificial) {
-  assert(RT != NULL && RT->getTypeClass() == Type::Record);
+  assert(RT != NULL && RT->getTypeClass() == clang::Type::Record);
 
   const clang::RecordDecl *RD = RT->getDecl();
   assert(RD->isStruct());
diff --git a/slang_rs_export_type.h b/slang_rs_export_type.h
index f9334b8..507ea7b 100644
--- a/slang_rs_export_type.h
+++ b/slang_rs_export_type.h
@@ -47,6 +47,7 @@
     ExportClassPointer,
     ExportClassConstantArray,
     ExportClassVector,
+    ExportClassMatrix,
     ExportClassRecord
   } ExportClass;
 
@@ -145,6 +146,11 @@
     DataTypeUnsigned5551,
     DataTypeUnsigned4444,
 
+    // Actually, there's no any instance of RSExportPrimitiveType which mType
+    // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is
+    // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct
+    // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as
+    // RSExportMatrixType.
     DataTypeRSMatrix2x2,
     DataTypeRSMatrix3x3,
     DataTypeRSMatrix4x4,
@@ -305,16 +311,15 @@
 class RSExportVectorType : public RSExportPrimitiveType {
   friend class RSExportType;
   friend class RSExportElement;
-
  private:
-  int mNumElement;   // number of element
+  unsigned mNumElement;   // number of element
 
   RSExportVectorType(RSContext *Context,
                      const llvm::StringRef &Name,
                      DataType DT,
                      DataKind DK,
                      bool Normalized,
-                     int NumElement)
+                     unsigned NumElement)
       : RSExportPrimitiveType(Context, Name, DT, DK, Normalized),
         mNumElement(NumElement) {
     return;
@@ -336,7 +341,43 @@
 
   virtual ExportClass getClass() const;
 
-  inline int getNumElement() const { return mNumElement; }
+  inline unsigned getNumElement() const { return mNumElement; }
+};
+
+// Only *square* *float* matrix is supported by now.
+//
+// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
+// form *exactly*:
+//  typedef struct {
+//    float m[{NxN}];
+//  } rs_matrixNxN;
+//
+//  where mDim will be N.
+class RSExportMatrixType : public RSExportType {
+  friend class RSExportType;
+ private:
+  unsigned mDim; // dimension
+
+  RSExportMatrixType(RSContext *Context,
+                     const llvm::StringRef &Name,
+                     unsigned Dim)
+    : RSExportType(Context, Name),
+      mDim(Dim) {
+    return;
+  }
+
+  virtual const llvm::Type *convertToLLVMType() const;
+ public:
+  virtual ExportClass getClass() const;
+
+  // @RT was normalized by calling RSExportType::TypeExportable() before
+  // calling this.
+  static RSExportMatrixType *Create(RSContext *Context,
+                                    const clang::RecordType *RT,
+                                    const llvm::StringRef &TypeName,
+                                    unsigned Dim);
+
+  inline unsigned getDim() const { return mDim; }
 };
 
 class RSExportRecordType : public RSExportType {
diff --git a/slang_rs_reflection.cpp b/slang_rs_reflection.cpp
index ce6aa85..e06c68d 100644
--- a/slang_rs_reflection.cpp
+++ b/slang_rs_reflection.cpp
@@ -66,16 +66,16 @@
     "int",      // RSExportPrimitiveType::DataTypeUnsigned5551
     "int",      // RSExportPrimitiveType::DataTypeUnsigned4444
 
-    "Matrix2f",     // RSExportPrimitiveType::DataTypeRSMatrix2x2
-    "Matrix3f",     // RSExportPrimitiveType::DataTypeRSMatrix3x3
-    "Matrix4f",     // RSExportPrimitiveType::DataTypeRSMatrix4x4
+    "",     // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2
+    "",     // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3
+    "",     // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4
 
-    "Element",  // RSExportPrimitiveType::DataTypeRSElement
-    "Type",     // RSExportPrimitiveType::DataTypeRSType
+    "Element",      // RSExportPrimitiveType::DataTypeRSElement
+    "Type",         // RSExportPrimitiveType::DataTypeRSType
     "Allocation",   // RSExportPrimitiveType::DataTypeRSAllocation
-    "Sampler",  // RSExportPrimitiveType::DataTypeRSSampler
-    "Script",   // RSExportPrimitiveType::DataTypeRSScript
-    "Mesh",       // RSExportPrimitiveType::DataTypeRSMesh
+    "Sampler",      // RSExportPrimitiveType::DataTypeRSSampler
+    "Script",       // RSExportPrimitiveType::DataTypeRSScript
+    "Mesh",         // RSExportPrimitiveType::DataTypeRSMesh
     "ProgramFragment",  // RSExportPrimitiveType::DataTypeRSProgramFragment
     "ProgramVertex",    // RSExportPrimitiveType::DataTypeRSProgramVertex
     "ProgramRaster",    // RSExportPrimitiveType::DataTypeRSProgramRaster
@@ -142,6 +142,21 @@
   return BaseElement[EVT->getNumElement() - 2];
 }
 
+static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) {
+  static const char *MatrixTypeJavaNameMap[] = {
+    /* 2x2 */ "Matrix2f",
+    /* 3x3 */ "Matrix3f",
+    /* 4x4 */ "Matrix4f",
+  };
+  unsigned Dim = EMT->getDim();
+
+  if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*)))
+    return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ];
+
+  assert(false && "GetMatrixTypeName : Unsupported matrix dimension");
+  return NULL;
+}
+
 static const char *GetVectorAccessor(int Index) {
   static const char *VectorAccessorMap[] = {
     /* 0 */ "x",
@@ -223,6 +238,10 @@
       return GetVectorTypeName(static_cast<const RSExportVectorType*>(ET));
       break;
     }
+    case RSExportType::ExportClassMatrix: {
+      return GetMatrixTypeName(static_cast<const RSExportMatrixType*>(ET));
+      break;
+    }
     case RSExportType::ExportClassRecord: {
       return RS_TYPE_CLASS_NAME_PREFIX + ET->getName() +
                  "."RS_TYPE_ITEM_CLASS_NAME;
@@ -260,9 +279,9 @@
         NULL,   // RSExportPrimitiveType::DataTypeUnsigned5551
         NULL,   // RSExportPrimitiveType::DataTypeUnsigned4444
 
-        "MATRIX_2X2",   // RSExportPrimitiveType::DataTypeRSMatrix2x2
-        "MATRIX_3X3",   // RSExportPrimitiveType::DataTypeRSMatrix3x3
-        "MATRIX_4X4",   // RSExportPrimitiveType::DataTypeRSMatrix4x4
+        NULL,   // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2
+        NULL,   // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3
+        NULL,   // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4
 
         "ELEMENT",      // RSExportPrimitiveType::DataTypeRSElement
         "TYPE",         // RSExportPrimitiveType::DataTypeRSType
@@ -312,6 +331,25 @@
           return "U8_4";
       }
     }
+  } else if (ET->getClass() == RSExportType::ExportClassMatrix) {
+    const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET);
+    switch (EMT->getDim()) {
+      case 2: {
+        return "MATRIX_2X2";
+        break;
+      }
+      case 3: {
+        return "MATRIX_3X3";
+        break;
+      }
+      case 4: {
+        return "MATRIX_4X4";
+        break;
+      }
+      default: {
+        assert(false && "Unsupported dimension of matrix");
+      }
+    }
   } else if (ET->getClass() == RSExportType::ExportClassPointer) {
     // Treat pointer type variable as unsigned int
     // TODO(zonr): this is target dependent
@@ -360,12 +398,10 @@
     // RSExportPrimitiveType::DataTypeUnsigned4444
     "Element.DataType.UNSIGNED_4_4_4_4",
 
-    // RSExportPrimitiveType::DataTypeRSMatrix2x2
-    "Element.DataType.RS_MATRIX_2X2",
-    // RSExportPrimitiveType::DataTypeRSMatrix3x3
-    "Element.DataType.RS_MATRIX_3X3",
-    // RSExportPrimitiveType::DataTypeRSMatrix4x4
-    "Element.DataType.RS_MATRIX_4X4",
+    // DataTypeRSMatrix* must have been resolved in GetBuiltinElementConstruct()
+    NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2
+    NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3
+    NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4
 
     "Element.DataType.RS_ELEMENT",  // RSExportPrimitiveType::DataTypeRSElement
     "Element.DataType.RS_TYPE",     // RSExportPrimitiveType::DataTypeRSType
@@ -496,12 +532,8 @@
   assert((Val.getKind() == clang::APValue::Int) &&
          "Bool type has wrong initial APValue");
 
-  if (Val.getInt().getSExtValue() == 0) {
-    C.out() << "false";
-  } else {
-    C.out() << "true";
-  }
-  C.out() << ";" << std::endl;
+  C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true")
+          << ";" << std::endl;
 
   return;
 }
@@ -607,8 +639,10 @@
       break;
     }
 
-    // TODO(zonr): Resolving initializer of a record type variable is complex.
-    // It cannot obtain by just simply evaluating the initializer expression.
+    // TODO(zonr): Resolving initializer of a record (and matrix) type variable
+    // is complex. It cannot obtain by just simply evaluating the initializer
+    // expression.
+    case RSExportType::ExportClassMatrix:
     case RSExportType::ExportClassRecord: {
 #if 0
       unsigned InitIndex = 0;
@@ -662,22 +696,22 @@
       genPrimitiveTypeExportVariable(C, EV);
       break;
     }
-
     case RSExportType::ExportClassPointer: {
       genPointerTypeExportVariable(C, EV);
       break;
     }
-
     case RSExportType::ExportClassVector: {
       genVectorTypeExportVariable(C, EV);
       break;
     }
-
+    case RSExportType::ExportClassMatrix: {
+      genMatrixTypeExportVariable(C, EV);
+      break;
+    }
     case RSExportType::ExportClassRecord: {
       genRecordTypeExportVariable(C, EV);
       break;
     }
-
     default: {
       assert(false && "Unknown class of type");
     }
@@ -844,6 +878,41 @@
   return;
 }
 
+void RSReflection::genMatrixTypeExportVariable(Context &C,
+                                               const RSExportVar *EV) {
+  assert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) &&
+         "Variable should be type of matrix here");
+
+  const RSExportMatrixType *EMT =
+      static_cast<const RSExportMatrixType*>(EV->getType());
+  const char *TypeName = GetMatrixTypeName(EMT);
+  const char *FieldPackerName = "fp";
+
+  C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX
+             << EV->getName() << ";" << std::endl;
+
+  // set_*()
+  if (!EV->isConst()) {
+    C.startFunction(Context::AM_Public,
+                    false,
+                    "void",
+                    "set_" + EV->getName(),
+                    1,
+                    TypeName, "v");
+    C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl;
+
+    if (genCreateFieldPacker(C, EMT, FieldPackerName))
+      genPackVarOfType(C, EMT, "v", FieldPackerName);
+    C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", "
+               << FieldPackerName << ");" << std::endl;
+
+    C.endFunction();
+  }
+
+  genGetExportVariable(C, TypeName, EV->getName());
+  return;
+}
+
 void RSReflection::genRecordTypeExportVariable(Context &C,
                                                const RSExportVar *EV) {
   assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) &&
@@ -935,7 +1004,6 @@
       }
       break;
     }
-
     case RSExportType::ExportClassPointer: {
       // Must reflect as type Allocation in Java
       const RSExportType *PointeeType =
@@ -949,7 +1017,11 @@
                    << ".getAllocation().getPtr());" << std::endl;
       break;
     }
-
+    case RSExportType::ExportClassMatrix: {
+      C.indent() << FieldPackerName << ".addObj(" << VarName << ");"
+                 << std::endl;
+      break;
+    }
     case RSExportType::ExportClassRecord: {
       const RSExportRecordType *ERT =
           static_cast<const RSExportRecordType*>(ET);
@@ -1003,22 +1075,22 @@
 
 void RSReflection::genNewItemBufferIfNull(Context &C, const char *Index) {
   C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) "
-      RS_TYPE_ITEM_BUFFER_NAME" = "
-      "new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];"
+                  RS_TYPE_ITEM_BUFFER_NAME" = "
+                    "new "RS_TYPE_ITEM_CLASS_NAME"[mType.getX() /* count */];"
              << std::endl;
   if (Index != NULL)
     C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) "
-        RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = "
-        "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
+                    RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = "
+                      "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl;
   return;
 }
 
 void RSReflection::genNewItemBufferPackerIfNull(Context &C) {
   C.indent() << "if ("RS_TYPE_ITEM_BUFFER_PACKER_NAME" == null) "
-      RS_TYPE_ITEM_BUFFER_PACKER_NAME" = "
-      "new FieldPacker("
-      RS_TYPE_ITEM_CLASS_NAME".sizeof * mType.getX() /* count */);"
-             << std::endl;
+                  RS_TYPE_ITEM_BUFFER_PACKER_NAME" = "
+                    "new FieldPacker("
+                      RS_TYPE_ITEM_CLASS_NAME".sizeof * mType.getX()/* count */"
+                    ");" << std::endl;
   return;
 }
 
@@ -1095,6 +1167,7 @@
        FI++) {
     const RSExportRecordType::Field *F = *FI;
     if ((F->getType()->getClass() == RSExportType::ExportClassVector) ||
+        (F->getType()->getClass() == RSExportType::ExportClassMatrix) ||
         (F->getType()->getClass() == RSExportType::ExportClassRecord) ||
         (F->getType()->getClass() == RSExportType::ExportClassConstantArray)) {
       C.indent() << F->getName() << " = new " << GetTypeName(F->getType())
@@ -1160,7 +1233,8 @@
 
   genNewItemBufferPackerIfNull(C);
   C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
-      ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" << std::endl;
+                ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
+             << std::endl;
 
   genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
 
@@ -1189,7 +1263,7 @@
 
   C.indent() << "copyToArray(i, index);" << std::endl;
   C.indent() << "mAllocation.subData1D(index, 1, "
-      RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << std::endl;
+                  RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << std::endl;
 
   // End of if (copyNow)
   C.endBlock();
@@ -1244,12 +1318,13 @@
     C.startBlock();
 
     if (FieldOffset > 0)
-      C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "
-          RS_TYPE_ITEM_CLASS_NAME".sizeof + " << FieldOffset << ");"
-                 << std::endl;
+      C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
+                    ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + "
+                 << FieldOffset << ");" << std::endl;
     else
-      C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME".reset(index * "
-          RS_TYPE_ITEM_CLASS_NAME".sizeof);" << std::endl;
+      C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME
+                    ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);"
+                 << std::endl;
     genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME);
 
     C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");"
@@ -1327,10 +1402,10 @@
   return;
 }
 
-#define EB_ADD(x)                                                       \
-  C.indent() << ElementBuilderName \
+#define EB_ADD(x)                                                   \
+  C.indent() << ElementBuilderName                                  \
              << ".add(Element." << x << ", \"" << VarName << "\");" \
-             << std::endl;                                               \
+             << std::endl;                                          \
   C.incFieldIndex()
 
 void RSReflection::genAddElementToElementBuilder(Context &C,
@@ -1345,8 +1420,7 @@
   } else {
     if ((ET->getClass() == RSExportType::ExportClassPrimitive) ||
         (ET->getClass() == RSExportType::ExportClassVector)    ||
-        (ET->getClass() == RSExportType::ExportClassConstantArray)
-        ) {
+        (ET->getClass() == RSExportType::ExportClassConstantArray)) {
       const RSExportPrimitiveType *EPT =
           static_cast<const RSExportPrimitiveType*>(ET);
       const char *DataKindName = GetElementDataKindName(EPT->getKind());
@@ -1375,8 +1449,8 @@
             EB_ADD("createUser(" << RenderScriptVar << ", "
                                  << DataTypeName << ")");
           } else {
-            // ET->getClass() == RSExportType::ExportClassVector must hold here
-            // Element.createVector()
+            assert((ET->getClass() == RSExportType::ExportClassVector) &&
+                   "Unexpected type.");
             EB_ADD("createVector(" << RenderScriptVar << ", "
                                    << DataTypeName << ", "
                                    << Size << ")");
@@ -1384,10 +1458,16 @@
           break;
         }
       }
+#ifndef NDEBUG
     } else if (ET->getClass() == RSExportType::ExportClassPointer) {
       // Pointer type variable should be resolved in
       // GetBuiltinElementConstruct()
       assert(false && "??");
+    } else if (ET->getClass() == RSExportType::ExportClassMatrix) {
+      // Matrix type variable should be resolved
+      // in GetBuiltinElementConstruct()
+      assert(false && "??");
+#endif
     } else if (ET->getClass() == RSExportType::ExportClassRecord) {
       // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType.
       //
diff --git a/slang_rs_reflection.h b/slang_rs_reflection.h
index bc5a71a..2750af6 100644
--- a/slang_rs_reflection.h
+++ b/slang_rs_reflection.h
@@ -112,17 +112,11 @@
       return;
     }
 
-    inline int getIndentLevel() {
-      return (mIndent.length() >> 2);
-    }
+    inline int getIndentLevel() { return (mIndent.length() >> 2); }
 
-    inline int getNextExportVarSlot() {
-      return mNextExportVarSlot++;
-    }
+    inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
 
-    inline int getNextExportFuncSlot() {
-      return mNextExportFuncSlot++;
-    }
+    inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
 
     // Will remove later due to field name information is not necessary for
     // C-reflect-to-Java
@@ -165,13 +159,9 @@
     void startTypeClass(const std::string &ClassName);
     void endTypeClass();
 
-    inline void incFieldIndex() {
-      mFieldIndex++;
-    }
+    inline void incFieldIndex() { mFieldIndex++; }
 
-    inline void resetFieldIndex() {
-      mFieldIndex = 0;
-    }
+    inline void resetFieldIndex() { mFieldIndex = 0; }
 
     inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
       assert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
@@ -186,9 +176,7 @@
       return I->second;
     }
 
-    inline void clearFieldIndexMap() {
-      mFieldIndexMap.clear();
-    }
+    inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
   };
 
   bool openScriptFile(Context &C,
@@ -213,6 +201,7 @@
   void genPrimitiveTypeExportVariable(Context &C, const RSExportVar *EV);
   void genPointerTypeExportVariable(Context &C, const RSExportVar *EV);
   void genVectorTypeExportVariable(Context &C, const RSExportVar *EV);
+  void genMatrixTypeExportVariable(Context &C, const RSExportVar *EV);
   void genRecordTypeExportVariable(Context &C, const RSExportVar *EV);
   void genGetExportVariable(Context &C,
                             const std::string &TypeName,
@@ -227,12 +216,9 @@
   bool genTypeItemClass(Context &C,
                         const RSExportRecordType *ERT,
                         std::string &ErrorMsg);
-  void genTypeClassConstructor(Context &C,
-                               const RSExportRecordType *ERT);
-  void genTypeClassCopyToArray(Context &C,
-                               const RSExportRecordType *ERT);
-  void genTypeClassItemSetter(Context &C,
-                              const RSExportRecordType *ERT);
+  void genTypeClassConstructor(Context &C, const RSExportRecordType *ERT);
+  void genTypeClassCopyToArray(Context &C, const RSExportRecordType *ERT);
+  void genTypeClassItemSetter(Context &C, const RSExportRecordType *ERT);
   void genTypeClassItemGetter(Context &C, const RSExportRecordType *ERT);
   void genTypeClassComponentSetter(Context &C, const RSExportRecordType *ERT);
   void genTypeClassComponentGetter(Context &C, const RSExportRecordType *ERT);